Fix SemVersion.Requirement

This commit is contained in:
Karlatemp 2020-11-12 22:25:44 +08:00
parent 0b23d3a46f
commit 3b27a26b0a
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
2 changed files with 15 additions and 8 deletions

View File

@ -94,16 +94,13 @@ internal constructor(
*/
val rule: String,
) {
init {
kotlin.runCatching {
parseRangeRequirement(rule)
}.onFailure {
throw java.lang.IllegalArgumentException("Syntax error: $rule", it)
}
}
@Transient
private val impl = SemVersionInternal.parseRangeRequirement(rule)
internal val impl = kotlin.runCatching {
SemVersionInternal.parseRangeRequirement(rule)
}.getOrElse {
throw java.lang.IllegalArgumentException("Syntax error: $rule", it)
}
/** 在 [version] 满足此要求时返回 true */
public fun test(version: SemVersion): Boolean = impl.test(version)

View File

@ -43,6 +43,16 @@ internal class TestSemVersion {
assert("1.0.0-rc.1".sem() < "1.0.0".sem())
}
@Test
internal fun testRequirementCopy() {
fun SemVersion.Requirement.check(a: SemVersion.Requirement.() -> SemVersion.Requirement) {
assert(a().impl !== this.impl)
}
SemVersion.parseRangeRequirement("1.0").check { copy() }
SemVersion.parseRangeRequirement("1.0").check { copy("2.0") }
SemVersion.parseRangeRequirement("1.0").check { copy("1.0") }
}
@Test
internal fun testRequirement() {
fun SemVersion.Requirement.assert(version: String): SemVersion.Requirement {