mirror of
https://github.com/florisboard/florisboard.git
synced 2024-09-20 12:02:19 +02:00
Add ExtensionAuthor unit test, fix bugs
This commit is contained in:
parent
ba712a5417
commit
f6a686a79d
@ -158,9 +158,10 @@ dependencies {
|
||||
implementation("androidx.room:room-runtime:2.3.0")
|
||||
kapt("androidx.room:room-compiler:2.3.0")
|
||||
|
||||
testImplementation("io.kotest:kotest-runner-junit5:5.0.0.M2")
|
||||
testImplementation("io.kotest:kotest-assertions-core:5.0.0.M2")
|
||||
testImplementation("io.kotest:kotest-property:5.0.0.M2")
|
||||
testImplementation("io.kotest:kotest-runner-junit5:4.6.3")
|
||||
testImplementation("io.kotest:kotest-assertions-core:4.6.3")
|
||||
testImplementation("io.kotest:kotest-property:4.6.3")
|
||||
testImplementation("io.kotest.extensions:kotest-extensions-robolectric:0.4.0")
|
||||
|
||||
androidTestImplementation("androidx.test.ext", "junit", "1.1.2")
|
||||
androidTestImplementation("androidx.test.espresso", "espresso-core", "3.3.0")
|
||||
|
@ -27,25 +27,31 @@ import kotlinx.serialization.encoding.Encoder
|
||||
import kotlin.math.min
|
||||
|
||||
@Serializable(with = ExtensionAuthorSerializer::class)
|
||||
class ExtensionAuthor(
|
||||
data class ExtensionAuthor(
|
||||
val name: String,
|
||||
val email: String? = null,
|
||||
val url: FlorisRef? = null,
|
||||
) {
|
||||
companion object {
|
||||
private val ValidationRegex = """^\s*[\p{L}\d._-][\p{L}\d\s._-]*(<[^<>]+>)?\s*(\([^()]+\))?\s*${'$'}""".toRegex()
|
||||
|
||||
fun from(str: String): ExtensionAuthor? {
|
||||
if (str.isBlank()) {
|
||||
if (str.isBlank() || !ValidationRegex.matches(str)) {
|
||||
return null
|
||||
}
|
||||
val emailStart = str.indexOf('<').let { if (it < 0) str.length else it } + 1
|
||||
val emailEnd = str.indexOf('>').let { if (it < 0) str.length else it } - 1
|
||||
val urlStart = str.indexOf('(').let { if (it < 0) str.length else it } + 1
|
||||
val urlEnd = str.indexOf(')').let { if (it < 0) str.length else it } - 1
|
||||
val emailStart = str.indexOf('<').let { if (it < 0) str.length else (it + 1) }
|
||||
val emailEnd = str.indexOf('>').let { if (it < 0) str.length else it }
|
||||
val urlStart = str.indexOf('(').let { if (it < 0) str.length else (it + 1) }
|
||||
val urlEnd = str.indexOf(')').let { if (it < 0) str.length else it }
|
||||
val nameStart = 0
|
||||
val nameEnd = (min(emailStart, urlStart) - 2).coerceIn(str.indices)
|
||||
val name = str.substring(nameStart..nameEnd).trim()
|
||||
val email = str.substring(emailStart..emailEnd).trim().takeIf { it.isNotBlank() }
|
||||
val url = str.substring(urlStart..urlEnd).trim().takeIf { it.isNotBlank() }
|
||||
val nameEnd = if (emailStart == str.length && urlStart == str.length) {
|
||||
str.length
|
||||
} else {
|
||||
(min(emailStart, urlStart) - 1)
|
||||
}
|
||||
val name = str.substring(nameStart, nameEnd).trim()
|
||||
val email = str.substring(emailStart, emailEnd).trim().takeIf { it.isNotBlank() }
|
||||
val url = str.substring(urlStart, urlEnd).trim().takeIf { it.isNotBlank() }
|
||||
return ExtensionAuthor(name, email, url?.let { FlorisRef.from(it) })
|
||||
}
|
||||
|
||||
@ -59,7 +65,7 @@ class ExtensionAuthor(
|
||||
if (email != null && email.isNotBlank()) {
|
||||
append(" <$email>")
|
||||
}
|
||||
if (url != null && url.isValid) {
|
||||
if (url != null) {
|
||||
append(" ($url)")
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,66 @@
|
||||
package dev.patrickgold.florisboard.res.ext
|
||||
|
||||
import android.app.Application
|
||||
import dev.patrickgold.florisboard.res.FlorisRef
|
||||
import io.kotest.core.spec.style.FreeSpec
|
||||
import io.kotest.extensions.robolectric.RobolectricTest
|
||||
import io.kotest.matchers.shouldBe
|
||||
|
||||
@RobolectricTest(application = Application::class)
|
||||
class ExtensionAuthorTest : FreeSpec({
|
||||
val validAuthorPairs = listOf(
|
||||
"Jane Doe" to ExtensionAuthor(name = "Jane Doe"),
|
||||
"jane123" to ExtensionAuthor(name = "jane123"),
|
||||
"__jane__" to ExtensionAuthor(name = "__jane__"),
|
||||
"jane.doe" to ExtensionAuthor(name = "jane.doe"),
|
||||
"Jane Doe <jane.doe@gmail.com>" to ExtensionAuthor(name = "Jane Doe", email = "jane.doe@gmail.com"),
|
||||
"Jane Doe (jane-doe.com)" to ExtensionAuthor(name = "Jane Doe", url = FlorisRef.from("jane-doe.com")),
|
||||
"Jane Doe <jane.doe@gmail.com> (jane-doe.com)" to ExtensionAuthor(name = "Jane Doe", email = "jane.doe@gmail.com", url = FlorisRef.from("jane-doe.com")),
|
||||
)
|
||||
|
||||
"Test ExtensionAuthor.from()" - {
|
||||
"With valid, well-formatted input" - {
|
||||
validAuthorPairs.forEach { (authorStr, authorObj) ->
|
||||
"`$authorStr`" {
|
||||
ExtensionAuthor.from(authorStr) shouldBe authorObj
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"With valid, ill-formatted input" - {
|
||||
listOf(
|
||||
" Jane Doe " to ExtensionAuthor(name = "Jane Doe"),
|
||||
" jane123" to ExtensionAuthor(name = "jane123"),
|
||||
" Jane Doe <jane.doe@gmail.com> " to ExtensionAuthor(name = "Jane Doe", email = "jane.doe@gmail.com"),
|
||||
).forEach { (authorStr, authorObj) ->
|
||||
"`$authorStr`" {
|
||||
ExtensionAuthor.from(authorStr) shouldBe authorObj
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"With invalid input" - {
|
||||
listOf(
|
||||
"",
|
||||
" ",
|
||||
"<jane.doe@gmail.com>",
|
||||
" <jane.doe@gmail.com>",
|
||||
"<jane.doe@gmail.com> (jane-doe.com)",
|
||||
"Jane Doe <<jane.doe@gmail.com>> ((jane-doe.com))",
|
||||
"Jane Doe <jane.doe@gmail.com) (jane-doe.com)",
|
||||
).forEach { authorStr ->
|
||||
"`$authorStr` should be null" {
|
||||
ExtensionAuthor.from(authorStr) shouldBe null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
"Test ExtensionAuthor.toString()" - {
|
||||
validAuthorPairs.forEach { (authorStr, authorObj) ->
|
||||
"`$authorStr`" {
|
||||
authorObj.toString() shouldBe authorStr
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
Loading…
Reference in New Issue
Block a user