Skip to content

Commit

Permalink
♻️ Refactor Zero.Companion.orThrow(String) method
Browse files Browse the repository at this point in the history
This commit adds the `ErrorMessage.invalidZero(String)` internal method for dealing with invalid inputs.
  • Loading branch information
LVMVRQUXL committed Jan 9, 2025
1 parent 038bdcd commit a5a176e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit a5a176e

Please sign in to comment.