Skip to content

Commit

Permalink
chore: bump kotlin-utils version
Browse files Browse the repository at this point in the history
  • Loading branch information
lbressler13 committed Jan 24, 2024
1 parent f1e5c0f commit bcd8f1b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 25 deletions.
2 changes: 1 addition & 1 deletion exact-numbers/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repositories {
}

dependencies {
val kotlinUtilsVersion = "1.3.0"
val kotlinUtilsVersion = "1.3.1"
val mockkVersion = "1.12.4"

implementation(kotlin("stdlib"))
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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("-")
Expand All @@ -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]"
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -33,9 +34,7 @@ inline fun <reified T : Exception> assertFailsWithMessage(message: String, test:
* @param test () -> Unit: test to run
*/
fun <T> assertSucceeds(errorMessage: String, test: () -> T) {
try {
test()
} catch (_: Exception) {
if (!succeeds { test() }) {
throw AssertionError(errorMessage)
}
}
Expand Down

0 comments on commit bcd8f1b

Please sign in to comment.