supporting homeserver versions without a patch value

This commit is contained in:
Adam Brown 2022-05-31 17:11:21 +01:00
parent ea151b37f0
commit 289ce7419d
2 changed files with 20 additions and 22 deletions

View file

@ -38,14 +38,14 @@ internal data class HomeServerVersion(
}
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? {
val result = pattern.matchEntire(value) ?: return null
return HomeServerVersion(
major = result.groupValues[1].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)
}
}
private fun List<String>.getOptional(index: Int, default: String) = getOrNull(index)?.ifEmpty { default } ?: default

View file

@ -22,27 +22,23 @@ import org.junit.Test
class HomeServerVersionTest {
@Test
fun `given a semantic version, when parsing then converts to home server version`() {
val cases = buildList {
addAll(
listOf(
case("0.5.1", expected = aVersion(0, 5, 1)),
case("1.0.0", expected = aVersion(1, 0, 0)),
case("1.10.3", expected = aVersion(1, 10, 3))
).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),
)
)
}
fun `given a semantic version, when parsing, then converts to home server version`() {
val supportedVersions = listOf(
case("1.5", expected = aVersion(1, 5, 0)),
case("0.5.1", expected = aVersion(0, 5, 1)),
case("1.0.0", expected = aVersion(1, 0, 0)),
case("1.10.3", expected = aVersion(1, 10, 3))
).withPrefixes("v", "r")
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)
assertEquals(expected, result, "Expected $input to be $expected but got $result")