Skip to content

Commit 8c18893

Browse files
committed
BAEL-8537: formatting implementations and test coverage
1 parent 0778b48 commit 8c18893

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

core-kotlin-modules/core-kotlin-12/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@
1414
<version>1.0.0-SNAPSHOT</version>
1515
</parent>
1616

17+
<dependencies>
18+
<dependency>
19+
<groupId>io.kotest</groupId>
20+
<artifactId>kotest-runner-junit5</artifactId>
21+
<version>${kotest.version}</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>io.kotest</groupId>
26+
<artifactId>kotest-property-jvm</artifactId>
27+
<version>${kotest.version}</version>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
31+
1732
<build>
1833
<sourceDirectory>src/main/kotlin</sourceDirectory>
1934
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
@@ -34,4 +49,10 @@
3449
</plugin>
3550
</plugins>
3651
</build>
52+
53+
<properties>
54+
<kotest.version>5.9.0</kotest.version>
55+
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
56+
</properties>
57+
3758
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.formatting
2+
3+
import java.text.DecimalFormat
4+
import java.text.DecimalFormatSymbols
5+
import java.util.*
6+
7+
/**
8+
* Interface for the different formatting implementation.
9+
*/
10+
interface NumberFormat {
11+
12+
/**
13+
* Returns provided number as string with thousands separator.
14+
*
15+
* 1000 -> '1.000'
16+
* 750000 -> '750.000'
17+
*/
18+
fun formatted(number: Int): String
19+
}
20+
21+
object FormatByDecimalFormat : NumberFormat {
22+
override fun formatted(number: Int): String =
23+
DecimalFormat("#,###")
24+
.format(number)
25+
.replace(",", ".")
26+
}
27+
28+
object FormatByDecimalFormatGermany : NumberFormat {
29+
override fun formatted(number: Int): String =
30+
DecimalFormat("#,###", DecimalFormatSymbols(Locale.GERMANY))
31+
.format(number)
32+
}
33+
34+
object FormatByStringFormat : NumberFormat {
35+
override fun formatted(number: Int): String =
36+
String.format(Locale.GERMANY, "%,d", number)
37+
}
38+
39+
object FormatByChunking : NumberFormat {
40+
override fun formatted(number: Int): String =
41+
number.toString()
42+
.reversed() // 15000 -> 00051
43+
.chunked(3) // [000] [51]
44+
.joinToString(".") // 000.51
45+
.reversed() // 15.000
46+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.formatting
2+
3+
import io.kotest.core.spec.style.ShouldSpec
4+
import io.kotest.property.Arb
5+
import io.kotest.property.arbitrary.positiveInt
6+
import io.kotest.property.checkAll
7+
import org.assertj.core.api.Assertions.assertThat
8+
9+
class NumberFormatUnitTest : ShouldSpec({
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) ->
17+
18+
// Property based test (for each implementation)
19+
should("return correctly formatted string with $type") {
20+
checkAll(Arb.positiveInt()) { number ->
21+
var result = function(number)
22+
23+
assertThat(result).containsPattern("^(\\d{1,3}(\\.\\d{3})*|\\d+)$")
24+
assertThat(number.toString()).isEqualTo(result.replace(".", ""))
25+
if (number > 999) assertThat(result).contains(".")
26+
else assertThat(result).doesNotContain(".")
27+
}
28+
}
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) ->
37+
38+
// Parameterised; Example based test
39+
should("return expected string '$expected' for $number with $type") {
40+
assertThat(function(number)).isEqualTo(expected)
41+
}
42+
}
43+
44+
}
45+
})

0 commit comments

Comments
 (0)