Skip to content

Commit b99df0f

Browse files
committed
Port all tests to KotlinTest
This reduces boiler plate code and makes the tests more readable without loosing JUnit's reporting on failing tests.
1 parent 3c47cab commit b99df0f

File tree

5 files changed

+223
-264
lines changed

5 files changed

+223
-264
lines changed

build.gradle.kts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ repositories {
1111
dependencies {
1212
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.1")
1313

14-
testImplementation("org.junit.jupiter:junit-jupiter:5.11.4")
15-
testImplementation("org.junit.jupiter:junit-jupiter-params:5.11.4")
1614
testImplementation("io.kotlintest:kotlintest-runner-junit5:3.4.2")
1715
testImplementation("org.assertj:assertj-core:3.27.2")
16+
testImplementation("ch.qos.logback:logback-classic:1.5.16")
17+
18+
// for tapSystemOut
1819
testImplementation("com.github.stefanbirkner:system-lambda:1.2.1")
1920
}
2021

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,44 @@
11
package de.ronny_h.extensions
22

3-
import org.assertj.core.api.Assertions.assertThat
4-
import org.junit.jupiter.api.Test
3+
import io.kotlintest.shouldBe
4+
import io.kotlintest.specs.StringSpec
55

6-
class CombinationsTest {
6+
class CombinationsTest : StringSpec({
77

8-
@Test
9-
fun `an empty list yields an empty sequence`() {
10-
assertThat(listOf<Int>().combinations().toList()).isEmpty()
8+
"an empty list yields an empty sequence" {
9+
listOf<Int>().combinations().toList() shouldBe emptyList()
1110
}
1211

13-
@Test
14-
fun `a list with one element yields an empty sequence`() {
15-
assertThat(listOf(1).combinations().toList()).isEmpty()
12+
"a list with one element yields an empty sequence" {
13+
listOf(1).combinations().toList() shouldBe emptyList()
1614
}
1715

18-
@Test
19-
fun `a list with two elements yields both combinations`() {
20-
assertThat(listOf(1, 2).combinations().toList()).isEqualTo(listOf(1 to 2, 2 to 1))
16+
"a list with two elements yields both combinations" {
17+
listOf(1, 2).combinations().toList() shouldBe listOf(1 to 2, 2 to 1)
2118
}
2219

23-
@Test
24-
fun `a list with three elements yields all six combinations`() {
25-
assertThat(listOf(1, 2, 3).combinations().toList()).isEqualTo(
26-
listOf(
27-
1 to 2,
28-
1 to 3,
29-
2 to 1,
30-
2 to 3,
31-
3 to 1,
32-
3 to 2
33-
)
34-
)
20+
"a list with three elements yields all six combinations" {
21+
listOf(1, 2, 3).combinations().toList() shouldBe
22+
listOf(
23+
1 to 2,
24+
1 to 3,
25+
2 to 1,
26+
2 to 3,
27+
3 to 1,
28+
3 to 2
29+
)
3530
}
3631

37-
@Test
38-
fun `a list with non-unique elements yields non-unique combinations but still skips the identical ones`() {
39-
assertThat(listOf(1, 1, 2).combinations().toList()).isEqualTo(
40-
listOf(
41-
1 to 1,
42-
1 to 2,
43-
1 to 1,
44-
1 to 2,
45-
2 to 1,
46-
2 to 1
47-
)
48-
)
32+
"a list with non-unique elements yields non-unique combinations but still skips the identical ones" {
33+
listOf(1, 1, 2).combinations().toList() shouldBe
34+
listOf(
35+
1 to 1,
36+
1 to 2,
37+
1 to 1,
38+
1 to 2,
39+
2 to 1,
40+
2 to 1
41+
)
4942
}
5043

51-
}
44+
})
Lines changed: 56 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,75 @@
11
package de.ronny_h.extensions
22

33
import de.ronny_h.extensions.Direction.*
4-
import org.assertj.core.api.Assertions.assertThat
5-
import org.junit.jupiter.api.Test
6-
import org.junit.jupiter.api.TestInstance
7-
import org.junit.jupiter.params.ParameterizedTest
8-
import org.junit.jupiter.params.provider.Arguments
9-
import org.junit.jupiter.params.provider.MethodSource
10-
import java.util.stream.Stream
4+
import io.kotlintest.data.forall
5+
import io.kotlintest.shouldBe
6+
import io.kotlintest.specs.StringSpec
7+
import io.kotlintest.tables.row
118

129

13-
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
14-
class CoordinatesTest {
10+
class CoordinatesTest : StringSpec({
1511

16-
@ParameterizedTest
17-
@MethodSource("provideCoordinatesForPlus")
18-
fun `Coordinates are added`(first: Coordinates, second: Coordinates, result: Coordinates) {
19-
assertThat(first + second).isEqualTo(result)
12+
"Coordinates are added" {
13+
forall(
14+
row(Coordinates(1, 1), Coordinates(0, 0), Coordinates(1, 1)),
15+
row(Coordinates(0, 0), Coordinates(1, 1), Coordinates(1, 1)),
16+
row(Coordinates(1, 2), Coordinates(3, 4), Coordinates(4, 6)),
17+
) { first, second, result ->
18+
first + second shouldBe result
19+
}
2020
}
2121

22-
private fun provideCoordinatesForPlus(): Stream<Arguments> {
23-
return Stream.of(
24-
Arguments.of(Coordinates(1, 1), Coordinates(0, 0), Coordinates(1, 1)),
25-
Arguments.of(Coordinates(0, 0), Coordinates(1, 1), Coordinates(1, 1)),
26-
Arguments.of(Coordinates(1, 2), Coordinates(3, 4), Coordinates(4, 6)),
27-
)
22+
"Coordinates are subtracted" {
23+
forall(
24+
row(Coordinates(1, 1), Coordinates(0, 0), Coordinates(1, 1)),
25+
row(Coordinates(0, 0), Coordinates(1, 1), Coordinates(-1, -1)),
26+
row(Coordinates(3, 5), Coordinates(2, 1), Coordinates(1, 4)),
27+
) { first, second, result ->
28+
first - second shouldBe result
29+
}
2830
}
2931

30-
@ParameterizedTest
31-
@MethodSource("provideCoordinatesForMinus")
32-
fun `Coordinates are subtracted`(first: Coordinates, second: Coordinates, result: Coordinates) {
33-
assertThat(first - second).isEqualTo(result)
32+
"Multiplication with a scalar" {
33+
forall(
34+
row(0, Coordinates(5, 7), Coordinates(0, 0)),
35+
row(7, Coordinates(0, 0), Coordinates(0, 0)),
36+
row(3, Coordinates(5, 7), Coordinates(15, 21)),
37+
row(-3, Coordinates(5, 7), Coordinates(-15, -21)),
38+
) { scalar, coordinates, result ->
39+
scalar * coordinates shouldBe result
40+
coordinates * scalar shouldBe result
41+
}
3442
}
3543

36-
private fun provideCoordinatesForMinus(): Stream<Arguments> {
37-
return Stream.of(
38-
Arguments.of(Coordinates(1, 1), Coordinates(0, 0), Coordinates(1, 1)),
39-
Arguments.of(Coordinates(0, 0), Coordinates(1, 1), Coordinates(-1, -1)),
40-
Arguments.of(Coordinates(3, 5), Coordinates(2, 1), Coordinates(1, 4)),
41-
)
44+
"Add a direction" {
45+
forall(
46+
row(Coordinates(5, 5), NORTH, Coordinates(4, 5)),
47+
row(Coordinates(5, 5), SOUTH, Coordinates(6, 5)),
48+
row(Coordinates(5, 5), EAST, Coordinates(5, 6)),
49+
row(Coordinates(5, 5), WEST, Coordinates(5, 4)),
50+
) { coordinates, direction, result ->
51+
coordinates + direction shouldBe result
52+
}
4253
}
4354

44-
@ParameterizedTest
45-
@MethodSource("provideCoordinatesForScalarMultiplication")
46-
fun `Multiplication with a scalar`(scalar: Int, coordinates: Coordinates, result: Coordinates) {
47-
assertThat(scalar * coordinates).isEqualTo(result)
48-
assertThat(coordinates * scalar).isEqualTo(result)
55+
"Direction turnRight() turns right" {
56+
NORTH.turnRight() shouldBe EAST
57+
EAST.turnRight() shouldBe SOUTH
58+
SOUTH.turnRight() shouldBe WEST
59+
WEST.turnRight() shouldBe NORTH
4960
}
5061

51-
private fun provideCoordinatesForScalarMultiplication(): Stream<Arguments> {
52-
return Stream.of(
53-
Arguments.of(0, Coordinates(5, 7), Coordinates(0, 0)),
54-
Arguments.of(7, Coordinates(0, 0), Coordinates(0, 0)),
55-
Arguments.of(3, Coordinates(5, 7), Coordinates(15, 21)),
56-
Arguments.of(-3, Coordinates(5, 7), Coordinates(-15, -21)),
57-
)
62+
"Direction turnLeft() turns left" {
63+
NORTH.turnLeft() shouldBe WEST
64+
EAST.turnLeft() shouldBe NORTH
65+
SOUTH.turnLeft() shouldBe EAST
66+
WEST.turnLeft() shouldBe SOUTH
5867
}
5968

60-
@ParameterizedTest
61-
@MethodSource("provideCoordinatesForAddDirection")
62-
fun `Add a direction`(coordinates: Coordinates, direction: Direction, result: Coordinates) {
63-
assertThat(coordinates + direction).isEqualTo(result)
69+
"asChar gives a graphical representation" {
70+
NORTH.asChar() shouldBe ''
71+
EAST.asChar() shouldBe ''
72+
SOUTH.asChar() shouldBe ''
73+
WEST.asChar() shouldBe ''
6474
}
65-
66-
private fun provideCoordinatesForAddDirection(): Stream<Arguments> {
67-
return Stream.of(
68-
Arguments.of(Coordinates(5, 5), NORTH, Coordinates(4, 5)),
69-
Arguments.of(Coordinates(5, 5), SOUTH, Coordinates(6, 5)),
70-
Arguments.of(Coordinates(5, 5), EAST, Coordinates(5, 6)),
71-
Arguments.of(Coordinates(5, 5), Direction.WEST, Coordinates(5, 4)),
72-
)
73-
}
74-
75-
@Test
76-
fun `Direction turnRight() turns right`() {
77-
assertThat(NORTH.turnRight()).isEqualTo(EAST)
78-
assertThat(EAST.turnRight()).isEqualTo(SOUTH)
79-
assertThat(SOUTH.turnRight()).isEqualTo(WEST)
80-
assertThat(WEST.turnRight()).isEqualTo(NORTH)
81-
}
82-
83-
@Test
84-
fun `Direction turnLeft() turns left`() {
85-
assertThat(NORTH.turnLeft()).isEqualTo(WEST)
86-
assertThat(EAST.turnLeft()).isEqualTo(NORTH)
87-
assertThat(SOUTH.turnLeft()).isEqualTo(EAST)
88-
assertThat(WEST.turnLeft()).isEqualTo(SOUTH)
89-
}
90-
91-
@Test
92-
fun `asChar gives a graphical representation`() {
93-
assertThat(NORTH.asChar()).isEqualTo('')
94-
assertThat(EAST.asChar()).isEqualTo('')
95-
assertThat(SOUTH.asChar()).isEqualTo('')
96-
assertThat(WEST.asChar()).isEqualTo('')
97-
}
98-
}
75+
})

0 commit comments

Comments
 (0)