Skip to content

Commit 4d1135c

Browse files
committed
BAEL-8537: test docs
1 parent 8c18893 commit 4d1135c

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

core-kotlin-modules/core-kotlin-12/src/test/kotlin/com/baeldung/formatting/NumberFormatUnitTest.kt

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,49 @@ import org.assertj.core.api.Assertions.assertThat
88

99
class NumberFormatUnitTest : ShouldSpec({
1010

11-
listOf( // different implementations of the formatting feature
12-
"FormatByChunking" to { it: Int -> FormatByChunking.formatted(it) },
13-
"FormatByStringFormat" to { it: Int -> FormatByStringFormat.formatted(it) },
14-
"FormatByDecimalFormatGermany" to { it: Int -> FormatByDecimalFormatGermany.formatted(it) },
15-
"FormatByDecimalFormat" to { it: Int -> FormatByDecimalFormat.formatted(it) }
16-
).forEach { (type, function) ->
11+
// Dynamic generation of tests for each implementation
12+
nameToImplementationPairs.forEach { (type, function) ->
1713

1814
// Property based test (for each implementation)
19-
should("return correctly formatted string with $type") {
15+
should("return correctly formatted string by $type") {
2016
checkAll(Arb.positiveInt()) { number ->
2117
var result = function(number)
2218

19+
// Check with regex
2320
assertThat(result).containsPattern("^(\\d{1,3}(\\.\\d{3})*|\\d+)$")
21+
// Check against original, by removing the separators
2422
assertThat(number.toString()).isEqualTo(result.replace(".", ""))
23+
24+
// Check for general presence & absence of separators
2525
if (number > 999) assertThat(result).contains(".")
2626
else assertThat(result).doesNotContain(".")
2727
}
2828
}
2929

30-
listOf( // examples with given number and expected string
31-
100_000 to "100.000",
32-
1_234_567 to "1.234.567",
33-
0 to "0",
34-
12 to "12",
35-
456 to "456"
36-
).forEach { (number, expected) ->
30+
givenToExpectedPairs.forEach { (number, expected) ->
3731

3832
// Parameterised; Example based test
39-
should("return expected string '$expected' for $number with $type") {
33+
should("return expected string '$expected' for $number by $type") {
4034
assertThat(function(number)).isEqualTo(expected)
4135
}
4236
}
4337

4438
}
4539
})
40+
41+
// Examples to check against, with given number and expected string
42+
private val givenToExpectedPairs = listOf(
43+
0 to "0",
44+
12 to "12",
45+
456 to "456",
46+
100_000 to "100.000",
47+
1_234_567 to "1.234.567"
48+
)
49+
50+
// Different implementations of the formatting feature with the display name
51+
private val nameToImplementationPairs = listOf(
52+
"FormatByChunking" to { it: Int -> FormatByChunking.formatted(it) },
53+
"FormatByStringFormat" to { it: Int -> FormatByStringFormat.formatted(it) },
54+
"FormatByDecimalFormatGermany" to { it: Int -> FormatByDecimalFormatGermany.formatted(it) },
55+
"FormatByDecimalFormat" to { it: Int -> FormatByDecimalFormat.formatted(it) }
56+
)

0 commit comments

Comments
 (0)