Konstantin Tskhovrebov
1 month ago
8 changed files with 116 additions and 6 deletions
@ -0,0 +1,31 @@
|
||||
package org.jetbrains.compose.internal |
||||
|
||||
internal data class Version( |
||||
val major: Int, |
||||
val minor: Int, |
||||
val patch: Int, |
||||
val meta: String |
||||
): Comparable<Version> { |
||||
override fun compareTo(other: Version): Int = when { |
||||
major != other.major -> major - other.major |
||||
minor != other.minor -> minor - other.minor |
||||
patch != other.patch -> patch - other.patch |
||||
else -> { |
||||
if (meta.isEmpty()) 1 |
||||
else if (other.meta.isEmpty()) -1 |
||||
else meta.compareTo(other.meta) |
||||
} |
||||
} |
||||
|
||||
companion object { |
||||
private val SEMVER_REGEXP = """^(\d+)(?:\.(\d*))?(?:\.(\d*))?(?:-(.*))?${'$'}""".toRegex() |
||||
fun fromString(versionString: String): Version { |
||||
val matchResult: MatchResult = SEMVER_REGEXP.matchEntire(versionString) ?: return Version(0,0,0, "") |
||||
val major: Int = matchResult.groups[1]?.value?.toInt() ?: 0 |
||||
val minor: Int = matchResult.groups[2]?.value?.toInt() ?: 0 |
||||
val patch: Int = matchResult.groups[3]?.value?.toInt() ?: 0 |
||||
val meta: String = matchResult.groups[4]?.value ?: "" |
||||
return Version(major, minor, patch, meta) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,19 @@
|
||||
package org.jetbrains.compose.test.tests.unit |
||||
|
||||
import org.jetbrains.compose.internal.Version |
||||
import kotlin.test.Test |
||||
|
||||
class SemVerTest { |
||||
@Test |
||||
fun testSemVersionParser() { |
||||
assert(Version.fromString("0") < Version.fromString("1.2.3")) |
||||
assert(Version.fromString("2") > Version.fromString("1.2.3")) |
||||
assert(Version.fromString("1.1") > Version.fromString("1-abc")) |
||||
assert(Version.fromString("1.1") > Version.fromString("1")) |
||||
assert(Version.fromString("2.0.0-RC1") > Version.fromString("2.0.0-Beta5")) |
||||
assert(Version.fromString("2.0.0-RC2") > Version.fromString("2.0.0-RC1")) |
||||
assert(Version.fromString("2.0.0-RC1") > Version.fromString("1.9.23")) |
||||
assert(Version.fromString("2.0.0") > Version.fromString("2.0.0-RC1")) |
||||
assert(Version.fromString("2.0.0-RC1") == Version.fromString("2.0.0-RC1")) |
||||
} |
||||
} |
Loading…
Reference in new issue