Skip to content

Commit 7a03b25

Browse files
Add assertContainsExactly assertion
1 parent 48890af commit 7a03b25

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- `assertNotContains` assertion
99
- `assertStartWith` and `assertEndWith` assertions
1010
- `assertGreaterThan` and `assertLessThan` assertions for `Duration`
11+
- `assertContainsExactly` assertion
1112

1213
### Changed
1314

kotlin-test/common/main/kotlin/com/javiersc/kotlin/test/iterableMatchers.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ public inline fun <reified I : Iterable<T>, reified T : Any> I.assertContains(
1010
return this
1111
}
1212

13+
public inline fun <reified I : Iterable<T>, reified T : Any> I.assertContainsExactly(
14+
elements: I,
15+
message: String? = null,
16+
): I {
17+
this.toList().assertEquals(elements.toList(), message)
18+
return this
19+
}
20+
1321
public inline fun <reified I : Iterable<T>, reified T : Any> I.assertCount(
1422
expectSize: Int,
1523
message: String? = null,

kotlin-test/common/test/kotlin/com/javiersc/kotlin/test/IterableMatchersTest.kt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ class IterableMatchersTest {
1919
)
2020
}
2121

22+
@Test
23+
fun `assertContains should fail`() {
24+
val list: List<Int> = listOf(1, 2, 3)
25+
val element = 4
26+
val exception: AssertionError = assertFailsWith { list.assertContains(element) }
27+
assertTrue(
28+
exception.message ==
29+
"Expected the collection to contain the element.\n" +
30+
"Collection <[1, 2, 3]>, element <4>."
31+
)
32+
}
33+
2234
@Test
2335
fun `assertContains should work with different types`() {
2436
val list: List<Double> = listOf(1.0, 2.0, 3.0)
@@ -27,6 +39,57 @@ class IterableMatchersTest {
2739
assertTrue(result === list)
2840
}
2941

42+
@Test
43+
fun `assertContains should work`() {
44+
val list: List<String> = listOf("AA", "BB", "CC")
45+
val element = "BB"
46+
val result: List<String> = list.assertContains(element)
47+
assertTrue(result === list)
48+
}
49+
50+
@Test
51+
fun `assertContainsExactly should fail with custom message`() {
52+
val list: List<Int> = listOf(1, 2, 3)
53+
val elements = listOf(1, 2, 3, 4)
54+
val message = "Custom error"
55+
val exception: AssertionError = assertFailsWith {
56+
list.assertContainsExactly(elements, message)
57+
}
58+
assertTrue(exception.message == "Custom error expected:<[1, 2, 3]> but was:<[1, 2, 3, 4]>")
59+
}
60+
61+
@Test
62+
fun `assertContainsExactly should fail`() {
63+
val list: List<Int> = listOf(1, 2, 3)
64+
val elements: List<Int> = listOf(1, 2, 3, 4)
65+
val exception: AssertionError = assertFailsWith { list.assertContainsExactly(elements) }
66+
assertTrue(exception.message == "expected:<[1, 2, 3]> but was:<[1, 2, 3, 4]>")
67+
}
68+
69+
@Test
70+
fun `assertContainsExactly should fail with more items`() {
71+
val list: List<Int> = listOf(1, 2, 3, 4, 5)
72+
val elements: List<Int> = listOf(1, 2, 3, 4)
73+
val exception: AssertionError = assertFailsWith { list.assertContainsExactly(elements) }
74+
assertTrue(exception.message == "expected:<[1, 2, 3, 4, 5]> but was:<[1, 2, 3, 4]>")
75+
}
76+
77+
@Test
78+
fun `assertContainsExactly should work with different types`() {
79+
val list: List<Double> = listOf(1.0, 2.0, 3.0)
80+
val elements: List<Double> = listOf(1.0, 2.0, 3.0)
81+
val result: List<Double> = list.assertContainsExactly(elements)
82+
assertTrue(result === list)
83+
}
84+
85+
@Test
86+
fun `assertContainsExactly should work`() {
87+
val list: List<String> = listOf("AA", "BB", "CC")
88+
val elements: List<String> = listOf("AA", "BB", "CC")
89+
val result: List<String> = list.assertContainsExactly(elements)
90+
assertTrue(result === list)
91+
}
92+
3093
@Test
3194
fun `assertCount should pass when count matches`() {
3295
val list: List<Int> = listOf(1, 2, 3)

0 commit comments

Comments
 (0)