mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-02-17 04:20:00 +03:00
supporting homeserver versions without a patch value
This commit is contained in:
parent
ea151b37f0
commit
289ce7419d
2 changed files with 20 additions and 22 deletions
|
@ -38,14 +38,14 @@ internal data class HomeServerVersion(
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
internal val pattern = Regex("""[r|v](\d+)\.(\d+)\.(\d+)""")
|
internal val pattern = Regex("""[r|v](\d+)\.(\d+)(?:\.(\d+))?""")
|
||||||
|
|
||||||
internal fun parse(value: String): HomeServerVersion? {
|
internal fun parse(value: String): HomeServerVersion? {
|
||||||
val result = pattern.matchEntire(value) ?: return null
|
val result = pattern.matchEntire(value) ?: return null
|
||||||
return HomeServerVersion(
|
return HomeServerVersion(
|
||||||
major = result.groupValues[1].toInt(),
|
major = result.groupValues[1].toInt(),
|
||||||
minor = result.groupValues[2].toInt(),
|
minor = result.groupValues[2].toInt(),
|
||||||
patch = result.groupValues[3].toInt()
|
patch = result.groupValues.getOptional(index = 3, default = "0").toInt()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,3 +59,5 @@ internal data class HomeServerVersion(
|
||||||
val v1_3_0 = HomeServerVersion(major = 1, minor = 3, patch = 0)
|
val v1_3_0 = HomeServerVersion(major = 1, minor = 3, patch = 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun List<String>.getOptional(index: Int, default: String) = getOrNull(index)?.ifEmpty { default } ?: default
|
||||||
|
|
|
@ -22,27 +22,23 @@ import org.junit.Test
|
||||||
class HomeServerVersionTest {
|
class HomeServerVersionTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `given a semantic version, when parsing then converts to home server version`() {
|
fun `given a semantic version, when parsing, then converts to home server version`() {
|
||||||
val cases = buildList {
|
val supportedVersions = listOf(
|
||||||
addAll(
|
case("1.5", expected = aVersion(1, 5, 0)),
|
||||||
listOf(
|
|
||||||
case("0.5.1", expected = aVersion(0, 5, 1)),
|
case("0.5.1", expected = aVersion(0, 5, 1)),
|
||||||
case("1.0.0", expected = aVersion(1, 0, 0)),
|
case("1.0.0", expected = aVersion(1, 0, 0)),
|
||||||
case("1.10.3", expected = aVersion(1, 10, 3))
|
case("1.10.3", expected = aVersion(1, 10, 3))
|
||||||
).withPrefixes("v", "r"),
|
).withPrefixes("v", "r")
|
||||||
)
|
|
||||||
addAll(
|
|
||||||
listOf(
|
|
||||||
case("-1.5.1", expected = null),
|
|
||||||
case("1", expected = null),
|
|
||||||
case("a", expected = null),
|
|
||||||
case("1.0", expected = null),
|
|
||||||
case("1a.2b.3c", expected = null),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
cases.forEach { (input, expected) ->
|
val unsupportedVersions = listOf(
|
||||||
|
case("v-1.5.1", expected = null),
|
||||||
|
case("r1", expected = null),
|
||||||
|
case("a", expected = null),
|
||||||
|
case("1a.2b.3c", expected = null),
|
||||||
|
case("r", expected = null)
|
||||||
|
)
|
||||||
|
|
||||||
|
(supportedVersions + unsupportedVersions).forEach { (input, expected) ->
|
||||||
val result = HomeServerVersion.parse(input)
|
val result = HomeServerVersion.parse(input)
|
||||||
|
|
||||||
assertEquals(expected, result, "Expected $input to be $expected but got $result")
|
assertEquals(expected, result, "Expected $input to be $expected but got $result")
|
||||||
|
|
Loading…
Add table
Reference in a new issue