|
1 | 1 | package de.ronny_h.extensions |
2 | 2 |
|
3 | 3 | 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 |
11 | 8 |
|
12 | 9 |
|
13 | | -@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
14 | | -class CoordinatesTest { |
| 10 | +class CoordinatesTest : StringSpec({ |
15 | 11 |
|
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 | + } |
20 | 20 | } |
21 | 21 |
|
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 | + } |
28 | 30 | } |
29 | 31 |
|
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 | + } |
34 | 42 | } |
35 | 43 |
|
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 | + } |
42 | 53 | } |
43 | 54 |
|
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 |
49 | 60 | } |
50 | 61 |
|
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 |
58 | 67 | } |
59 | 68 |
|
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 '←' |
64 | 74 | } |
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