@@ -8,38 +8,49 @@ import org.assertj.core.api.Assertions.assertThat
8
8
9
9
class NumberFormatUnitTest : ShouldSpec ({
10
10
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) ->
17
13
18
14
// Property based test (for each implementation)
19
- should("return correctly formatted string with $type") {
15
+ should("return correctly formatted string by $type") {
20
16
checkAll(Arb .positiveInt()) { number ->
21
17
var result = function(number)
22
18
19
+ // Check with regex
23
20
assertThat(result).containsPattern("^(\\d{1,3}(\\.\\d{3})*|\\d+)$")
21
+ // Check against original, by removing the separators
24
22
assertThat(number.toString()).isEqualTo(result.replace(".", ""))
23
+
24
+ // Check for general presence & absence of separators
25
25
if (number > 999) assertThat(result).contains(".")
26
26
else assertThat(result).doesNotContain(".")
27
27
}
28
28
}
29
29
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) ->
37
31
38
32
// Parameterised; Example based test
39
- should("return expected string '$expected' for $number with $type") {
33
+ should("return expected string '$expected' for $number by $type") {
40
34
assertThat(function(number)).isEqualTo(expected)
41
35
}
42
36
}
43
37
44
38
}
45
39
})
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