From 3f6b9fcccb74fe91c9eb79bc7fab77be2bbca56a Mon Sep 17 00:00:00 2001 From: lbressler13 Date: Wed, 24 Jan 2024 00:38:39 -0500 Subject: [PATCH] kotlinutils v1.3.1 (#35) --- exact-numbers/build.gradle.kts | 2 +- .../exactfraction/exactFractionParsing.kt | 26 ++++--------------- .../lbres/exactnumbers/testutils/helpers.kt | 5 ++-- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/exact-numbers/build.gradle.kts b/exact-numbers/build.gradle.kts index bcf1ab2..ee99815 100644 --- a/exact-numbers/build.gradle.kts +++ b/exact-numbers/build.gradle.kts @@ -24,7 +24,7 @@ repositories { } dependencies { - val kotlinUtilsVersion = "1.3.0" + val kotlinUtilsVersion = "1.3.1" val mockkVersion = "1.12.4" implementation(kotlin("stdlib")) diff --git a/exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/exactfraction/exactFractionParsing.kt b/exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/exactfraction/exactFractionParsing.kt index 81d4512..5f9c8ad 100644 --- a/exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/exactfraction/exactFractionParsing.kt +++ b/exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/exactfraction/exactFractionParsing.kt @@ -1,6 +1,7 @@ package xyz.lbres.exactnumbers.exactfraction import xyz.lbres.kotlinutils.general.simpleIf +import xyz.lbres.kotlinutils.general.succeeds import xyz.lbres.kotlinutils.general.tryOrDefault import java.math.BigDecimal import java.math.BigInteger @@ -18,7 +19,10 @@ private const val efSuffix = "]" internal fun parseDecimal(s: String): ExactFraction { var currentState: String = s.trim() - validateDecimalString(currentState) + // validate string + if (!succeeds { BigDecimal(currentState) } || currentState.last() == '.') { + throw NumberFormatException("Error parsing $currentState") + } // remove negative sign val isNegative = currentState.startsWith("-") @@ -42,26 +46,6 @@ internal fun parseDecimal(s: String): ExactFraction { return simpleIf(isNegative, { ExactFraction(-numerator, denominator) }, { ExactFraction(numerator, denominator) }) } -/** - * Validate that a decimal string is in a parseable format, and throw [NumberFormatException] if it is not. - * Assumes string is trimmed and lowercase. - * - * @param s [String]: string to validate - */ -private fun validateDecimalString(s: String) { - val exception = NumberFormatException("Error parsing $s") - - try { - BigDecimal(s) - } catch (_: Exception) { - throw exception - } - - if (s.last() == '.') { - throw exception - } -} - /** * Parse a string from a EF string format into a ExactFraction. * EF string format is "EF[num denom]" diff --git a/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/testutils/helpers.kt b/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/testutils/helpers.kt index 0944e2c..fcc162b 100644 --- a/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/testutils/helpers.kt +++ b/exact-numbers/src/test/kotlin/xyz/lbres/exactnumbers/testutils/helpers.kt @@ -1,6 +1,7 @@ package xyz.lbres.exactnumbers.testutils import xyz.lbres.exactnumbers.exceptions.CastingOverflowException +import xyz.lbres.kotlinutils.general.succeeds import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -33,9 +34,7 @@ inline fun assertFailsWithMessage(message: String, test: * @param test () -> Unit: test to run */ fun assertSucceeds(errorMessage: String, test: () -> T) { - try { - test() - } catch (_: Exception) { + if (!succeeds { test() }) { throw AssertionError(errorMessage) } }