Skip to content

Commit

Permalink
tp1
Browse files Browse the repository at this point in the history
  • Loading branch information
ctruchi committed Feb 6, 2025
1 parent 47d85e4 commit 29a8987
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tp1/src/main/kotlin/fmt/kotlin/fundamentals/Tp1.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package fmt.kotlin.fundamentals

class Tp1 {

var x = 0

fun incrementXAndReturnOldValue(): Int {
return -1;
}

fun incrementXAndReturnNewValue(): Int {
return -1;
}

fun sum(m: Int, n: Int): Int {
return -1
}

/**
* n Number of bottles. Always more than 2.
*/
fun describeNbBottles(n: Int): String {
return "";
}

fun describeWithDetailNbBottles(totalBottles: Int, nbWhiteBottles: Int, nbRedBottles: Int): String {
return ""
}
}
103 changes: 103 additions & 0 deletions tp1/src/test/kotlin/fmt/kotlin/fundamentals/Tp1Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package fmt.kotlin.fundamentals

import org.junit.jupiter.api.Nested
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.CsvSource
import org.junit.jupiter.params.provider.MethodSource
import strikt.api.expectThat
import strikt.assertions.isEqualTo
import java.util.stream.Stream
import kotlin.test.Test

class Tp1Test {

private val tp1 = Tp1()

@Nested
inner class Increment {

@Test
fun `should increment x and return last value`() {
expectThat(tp1.x).isEqualTo(0);

val res1 = tp1.incrementXAndReturnOldValue()

expectThat(res1).isEqualTo(0)
expectThat(tp1.x).isEqualTo(1);

val res2 = tp1.incrementXAndReturnOldValue()

expectThat(res2).isEqualTo(1)
expectThat(tp1.x).isEqualTo(2);
}

@Test
fun `should increment x and return new value`() {
expectThat(tp1.x).isEqualTo(0);

val res1 = tp1.incrementXAndReturnNewValue()

expectThat(res1).isEqualTo(1)
expectThat(tp1.x).isEqualTo(1);

val res2 = tp1.incrementXAndReturnNewValue()

expectThat(res2).isEqualTo(2)
expectThat(tp1.x).isEqualTo(2);
}
}

@Nested
inner class Sum {

@ParameterizedTest
@CsvSource("1, 2, 3", "2, 3, 5", "3, 8, 11")
fun `should sum`(m: Int, n: Int, expectedSum: Int) {
val sum = tp1.sum(m, n)

expectThat(sum).isEqualTo(expectedSum)
}
}

@Nested
inner class DescribeNbBottles {

@ParameterizedTest
@CsvSource("3, There are 3 bottles", "17, There are 17 bottles")
fun `should describe bottles`(n: Int, expectedDescription: String) {
val decription = tp1.describeNbBottles(n)

expectThat(decription).isEqualTo(expectedDescription)
}

@ParameterizedTest
@MethodSource("fmt.kotlin.fundamentals.Tp1Test#provideShouldDescribeBottlesWithDetailParams")
fun `should describe bottles with detail`(
totalBottles: Int,
nbWhiteBottles: Int,
nbRedBottles: Int,
expectedDescription: String
) {
val description = tp1.describeWithDetailNbBottles(totalBottles, nbWhiteBottles, nbRedBottles)

expectThat(description).isEqualTo(expectedDescription)
}
}

companion object {
@JvmStatic
private fun provideShouldDescribeBottlesWithDetailParams() = Stream.of(
Arguments.of(5, 2, 3, """
There are 5 bottles :
- 2 bottles of white
- 3 bottles of red
""".trimIndent()),
Arguments.of(8, 3, 5, """
There are 8 bottles :
- 3 bottles of white
- 5 bottles of red
""".trimIndent())
)
}
}

0 comments on commit 29a8987

Please sign in to comment.