1
+ package fmt.kotlin.fundamentals
2
+
3
+ import org.junit.jupiter.api.Nested
4
+ import org.junit.jupiter.params.ParameterizedTest
5
+ import org.junit.jupiter.params.provider.Arguments
6
+ import org.junit.jupiter.params.provider.CsvSource
7
+ import org.junit.jupiter.params.provider.MethodSource
8
+ import strikt.api.expectThat
9
+ import strikt.assertions.isEqualTo
10
+ import java.util.stream.Stream
11
+ import kotlin.test.Test
12
+
13
+ class Tp1Test {
14
+
15
+ private val tp1 = Tp1 ()
16
+
17
+ @Nested
18
+ inner class Increment {
19
+
20
+ @Test
21
+ fun `should increment x and return last value` () {
22
+ expectThat(tp1.x).isEqualTo(0 );
23
+
24
+ val res1 = tp1.incrementXAndReturnOldValue()
25
+
26
+ expectThat(res1).isEqualTo(0 )
27
+ expectThat(tp1.x).isEqualTo(1 );
28
+
29
+ val res2 = tp1.incrementXAndReturnOldValue()
30
+
31
+ expectThat(res2).isEqualTo(1 )
32
+ expectThat(tp1.x).isEqualTo(2 );
33
+ }
34
+
35
+ @Test
36
+ fun `should increment x and return new value` () {
37
+ expectThat(tp1.x).isEqualTo(0 );
38
+
39
+ val res1 = tp1.incrementXAndReturnNewValue()
40
+
41
+ expectThat(res1).isEqualTo(1 )
42
+ expectThat(tp1.x).isEqualTo(1 );
43
+
44
+ val res2 = tp1.incrementXAndReturnNewValue()
45
+
46
+ expectThat(res2).isEqualTo(2 )
47
+ expectThat(tp1.x).isEqualTo(2 );
48
+ }
49
+ }
50
+
51
+ @Nested
52
+ inner class Sum {
53
+
54
+ @ParameterizedTest
55
+ @CsvSource(" 1, 2, 3" , " 2, 3, 5" , " 3, 8, 11" )
56
+ fun `should sum` (m : Int , n : Int , expectedSum : Int ) {
57
+ val sum = tp1.sum(m, n)
58
+
59
+ expectThat(sum).isEqualTo(expectedSum)
60
+ }
61
+ }
62
+
63
+ @Nested
64
+ inner class DescribeNbBottles {
65
+
66
+ @ParameterizedTest
67
+ @CsvSource(" 3, There are 3 bottles" , " 17, There are 17 bottles" )
68
+ fun `should describe bottles` (n : Int , expectedDescription : String ) {
69
+ val decription = tp1.describeNbBottles(n)
70
+
71
+ expectThat(decription).isEqualTo(expectedDescription)
72
+ }
73
+
74
+ @ParameterizedTest
75
+ @MethodSource(" fmt.kotlin.fundamentals.Tp1Test#provideShouldDescribeBottlesWithDetailParams" )
76
+ fun `should describe bottles with detail` (
77
+ totalBottles : Int ,
78
+ nbWhiteBottles : Int ,
79
+ nbRedBottles : Int ,
80
+ expectedDescription : String
81
+ ) {
82
+ val description = tp1.describeWithDetailNbBottles(totalBottles, nbWhiteBottles, nbRedBottles)
83
+
84
+ expectThat(description).isEqualTo(expectedDescription)
85
+ }
86
+ }
87
+
88
+ companion object {
89
+ @JvmStatic
90
+ private fun provideShouldDescribeBottlesWithDetailParams () = Stream .of(
91
+ Arguments .of(5 , 2 , 3 , """
92
+ There are 5 bottles :
93
+ - 2 bottles of white
94
+ - 3 bottles of red
95
+ """ .trimIndent()),
96
+ Arguments .of(8 , 3 , 5 , """
97
+ There are 8 bottles :
98
+ - 3 bottles of white
99
+ - 5 bottles of red
100
+ """ .trimIndent())
101
+ )
102
+ }
103
+ }
0 commit comments