Skip to content

Commit a75e5d1

Browse files
authored
[KTLN-627] Add samples (#586)
* [KTLN-627] Add samples * [KTLN-627] Add samples * [KTLN-627] Add samples * [KTLN-627] Add samples
1 parent 35d437b commit a75e5d1

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.kotest.datadriven
2+
3+
fun isPythagTriple(a: Int, b: Int, c: Int): Boolean {
4+
if(a <= 0 || b <= 0 || c <= 0) return false
5+
return a * a + b * b == c * c
6+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.baeldung.kotest.datadriven
2+
3+
import io.kotest.core.spec.style.FunSpec
4+
import io.kotest.datatest.IsStableType
5+
import io.kotest.datatest.WithDataTestName
6+
import io.kotest.datatest.withData
7+
import io.kotest.matchers.shouldBe
8+
9+
class PythagoreanTripleTest : FunSpec({
10+
context("Pythagorean triples tests") {
11+
withData(
12+
DefaultPythagTriple(3, 4, 5),
13+
DefaultPythagTriple(6, 8, 10),
14+
DefaultPythagTriple(8, 15, 17),
15+
DefaultPythagTriple(7, 24, 25)
16+
) { (a, b, c) ->
17+
isPythagTriple(a, b, c) shouldBe true
18+
}
19+
}
20+
21+
context("StablePythagorean triples tests") {
22+
withData(
23+
StablePythagTriple(3, 4, 5),
24+
StablePythagTriple(6, 8, 10),
25+
StablePythagTriple(8, 15, 17),
26+
StablePythagTriple(7, 24, 25)
27+
) { (a, b, c) ->
28+
isPythagTriple(a, b, c) shouldBe true
29+
}
30+
}
31+
32+
context("NamedPythagorean triples tests") {
33+
withData(
34+
NamedPythagTriple(3, 4, 5),
35+
NamedPythagTriple(6, 8, 10),
36+
NamedPythagTriple(8, 15, 17),
37+
NamedPythagTriple(7, 24, 25)
38+
) { (a, b, c) ->
39+
isPythagTriple(a, b, c) shouldBe true
40+
}
41+
}
42+
43+
context("Pythagorean triples tests with map") {
44+
withData(
45+
mapOf(
46+
"3, 4, 5" to DefaultPythagTriple(3, 4, 5),
47+
"6, 8, 10" to DefaultPythagTriple(6, 8, 10),
48+
"8, 15, 17" to DefaultPythagTriple(8, 15, 17),
49+
"7, 24, 25" to DefaultPythagTriple(7, 24, 25)
50+
)
51+
) { (a, b, c) ->
52+
isPythagTriple(a, b, c) shouldBe true
53+
}
54+
}
55+
56+
context("Pythagorean triples tests with name function") {
57+
withData(
58+
nameFn = { "${it.a}__${it.b}__${it.c}" },
59+
DefaultPythagTriple(3, 4, 5),
60+
DefaultPythagTriple(6, 8, 10),
61+
DefaultPythagTriple(8, 15, 17),
62+
DefaultPythagTriple(7, 24, 25)
63+
) { (a, b, c) ->
64+
isPythagTriple(a, b, c) shouldBe true
65+
}
66+
}
67+
68+
context("each service should support all HTTP methods") {
69+
val services = listOf("http://internal.foo", "http://internal.bar", "http://public.baz")
70+
val methods = listOf("GET", "POST", "PUT")
71+
withData(services) { service ->
72+
withData(methods) { method ->
73+
println("$service $method") // test service against method
74+
}
75+
}
76+
}
77+
78+
})
79+
80+
data class DefaultPythagTriple(val a: Int, val b: Int, val c: Int)
81+
82+
@IsStableType
83+
data class StablePythagTriple(val a: Int, val b: Int, val c: Int)
84+
85+
data class NamedPythagTriple(val a: Int, val b: Int, val c: Int) : WithDataTestName {
86+
override fun dataTestName() = "Pythagorean Triple: $a, $b, $c"
87+
}

0 commit comments

Comments
 (0)