From 3b27a26b0ab57dc1f8bda1ca8c58e6dc58ac9cfc Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Thu, 12 Nov 2020 22:25:44 +0800 Subject: [PATCH] Fix SemVersion.Requirement --- backend/mirai-console/src/util/SemVersion.kt | 13 +++++-------- backend/mirai-console/test/util/TestSemVersion.kt | 10 ++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/backend/mirai-console/src/util/SemVersion.kt b/backend/mirai-console/src/util/SemVersion.kt index e86131864..59657ef86 100644 --- a/backend/mirai-console/src/util/SemVersion.kt +++ b/backend/mirai-console/src/util/SemVersion.kt @@ -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) diff --git a/backend/mirai-console/test/util/TestSemVersion.kt b/backend/mirai-console/test/util/TestSemVersion.kt index 3021de67b..781392f1a 100644 --- a/backend/mirai-console/test/util/TestSemVersion.kt +++ b/backend/mirai-console/test/util/TestSemVersion.kt @@ -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 {