From a5a176e4b2cb77e91107ec22c7fcbca4cd7a181f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Lamarque?= Date: Thu, 9 Jan 2025 20:26:26 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20`Zero.Companion?= =?UTF-8?q?.orThrow(String)`=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds the `ErrorMessage.invalidZero(String)` internal method for dealing with invalid inputs. --- .../kotlin/org/kotools/types/internal/ErrorMessage.kt | 7 +++++++ .../src/commonMain/kotlin/org/kotools/types/Zero.kt | 4 +--- .../kotlin/org/kotools/types/ZeroCompanionTest.kt | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/subprojects/internal/src/commonMain/kotlin/org/kotools/types/internal/ErrorMessage.kt b/subprojects/internal/src/commonMain/kotlin/org/kotools/types/internal/ErrorMessage.kt index 88ac4c6e9..76b4af0ab 100644 --- a/subprojects/internal/src/commonMain/kotlin/org/kotools/types/internal/ErrorMessage.kt +++ b/subprojects/internal/src/commonMain/kotlin/org/kotools/types/internal/ErrorMessage.kt @@ -11,4 +11,11 @@ public object ErrorMessage { */ public fun invalidZero(number: Number): String = "'$number' shouldn't be other than zero." + + /** + * Returns an error message indicating that the specified [text] is not a + * valid representation of zero. + */ + public fun invalidZero(text: String): String = + "'$text' is not a valid representation of zero." } diff --git a/subprojects/library/src/commonMain/kotlin/org/kotools/types/Zero.kt b/subprojects/library/src/commonMain/kotlin/org/kotools/types/Zero.kt index 382c665e8..8c5abfc2d 100644 --- a/subprojects/library/src/commonMain/kotlin/org/kotools/types/Zero.kt +++ b/subprojects/library/src/commonMain/kotlin/org/kotools/types/Zero.kt @@ -1000,9 +1000,7 @@ public class Zero { @JvmStatic public fun orThrow(text: String): Zero { val regex = Regex("""^0+(?:\.0+)?$""") - require(text matches regex) { - "'$text' is not a valid representation of zero." - } + require(text matches regex) { ErrorMessage.invalidZero(text) } return Zero() } } diff --git a/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroCompanionTest.kt b/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroCompanionTest.kt index 56f7e13cd..b1fb53532 100644 --- a/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroCompanionTest.kt +++ b/subprojects/library/src/commonTest/kotlin/org/kotools/types/ZeroCompanionTest.kt @@ -221,7 +221,7 @@ class ZeroCompanionTest { val exception: IllegalArgumentException = assertFailsWith { Zero.orThrow(it) } val actual: String? = exception.message - val expected = "'$it' is not a valid representation of zero." + val expected: String = ErrorMessage.invalidZero(it) assertEquals(expected, actual) } }