Skip to content

Commit

Permalink
Make TextInputDecimal decimal property nullable (#140)
Browse files Browse the repository at this point in the history
* make TextInputDecimal decimal property nullable, to reflect potential parse error

* update sargon version
  • Loading branch information
jakub-rdx authored May 14, 2024
1 parent a1921a4 commit ae43c28
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "0.7.16"
version = "0.7.17"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,15 @@ class TextInputDecimal(
val input: String,
decimalSeparator: Char
) {
val decimal: Decimal192 = Decimal192.init(
formattedString = input,
config = LocaleConfig(
decimalSeparator = decimalSeparator.toString(),
groupingSeparator = null // We do not allow grouping separator characters in input
val decimal: Decimal192? = runCatching {
Decimal192.init(
formattedString = input,
config = LocaleConfig(
decimalSeparator = decimalSeparator.toString(),
groupingSeparator = null // We do not allow grouping separator characters in input
)
)
)
}.getOrNull()
}

fun Decimal192.Companion.init(
Expand Down Expand Up @@ -188,7 +190,7 @@ fun Decimal192.rounded(decimalPlaces: UByte, roundingMode: RoundingMode): Decima
return try {
decimalRound(
decimal = this,
decimalPlaces = decimalPlaces.toUByte(),
decimalPlaces = decimalPlaces,
roundingMode = roundingMode
)
} catch (exception: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class Decimal192Test : SampleTestable<Decimal192> {
input: String,
decimal: Char,
grouping: Char,
formattedTextField: String,
formattedTextField: String?,
sanitizedInput: String
) {
val decimalFormatSymbols = mockk<DecimalFormatSymbols>(relaxed = true).apply {
Expand All @@ -233,7 +233,7 @@ class Decimal192Test : SampleTestable<Decimal192> {
decimalFormat = decimalFormatSymbols
)

assertEquals(formattedTextField, result.decimal.formattedTextField(
assertEquals(formattedTextField, result.decimal?.formattedTextField(
format = decimalFormatSymbols
))

Expand Down Expand Up @@ -266,7 +266,9 @@ class Decimal192Test : SampleTestable<Decimal192> {
of(" ", ' ', ',', "0", " "), // Blank with space as decimal separator resolves to 0 prints space

of("1,000,000.10", '.', ',', "1000000.1", "1000000.10"), // Large number resolves to decimal without trailing zero, prints the same number with 0
of("1.000.000,10", ',', '.', "1000000,1", "1000000,10")
of("1.000.000,10", ',', '.', "1000000,1", "1000000,10"),
of("2,", ',', '.', null, "2,"),
of("2.", '.', ',', null, "2.")
)
}
}

0 comments on commit ae43c28

Please sign in to comment.