Skip to content

Commit 05c8085

Browse files
CopilotbedaHovorka
andcommitted
Add custom AssertK extensions for hasSizeGreaterThanOrEqualTo and containsAnyOf, fix fail() call
Co-authored-by: bedaHovorka <5263405+bedaHovorka@users.noreply.github.com>
1 parent ee9c1e5 commit 05c8085

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

src/test/kotlin/cz/vutbr/fit/interlockSim/context/PropertyChangeTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import cz.vutbr.fit.interlockSim.util.Point
1616
import cz.vutbr.fit.interlockSim.xml.XMLContextFactory
1717
import assertk.assertThat
1818
import assertk.assertions.contains
19-
import assertk.assertions.containsAnyOf
2019
import assertk.assertions.hasSize
21-
import assertk.assertions.hasSizeGreaterThanOrEqualTo
2220
import assertk.assertions.isEmpty
2321
import assertk.assertions.isEqualTo
22+
import cz.vutbr.fit.interlockSim.testutil.containsAnyOf
23+
import cz.vutbr.fit.interlockSim.testutil.hasSizeGreaterThanOrEqualTo
2424
import org.junit.jupiter.api.BeforeEach
2525
import org.junit.jupiter.api.DisplayName
2626
import org.junit.jupiter.api.Test

src/test/kotlin/cz/vutbr/fit/interlockSim/objects/cells/CellTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class CellTest {
9898
clazz == RailSemaphore::class.java -> RailSemaphore(o, t) as OrientedNodeCell
9999
clazz == InOut::class.java -> InOut("xx", o, t) as OrientedNodeCell
100100
else -> {
101-
fail<OrientedNodeCell>("Unexpected cell class: $clazz, objects: ${objects.contentToString()}")
101+
fail("Unexpected cell class: $clazz, objects: ${objects.contentToString()}")
102102
}
103103
}
104104
}

src/test/kotlin/cz/vutbr/fit/interlockSim/testutil/AssertKExtensions.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,23 @@ fun <T> Assert<KProperty0<T?>>.isNotNull() = given { actual ->
7676
fun <T> Assert<T>.withMessage(message: String): Assert<T> = transform { actual ->
7777
assertk.assertThat(actual, name = message)
7878
}
79+
80+
/**
81+
* Extension function to assert that a collection has size greater than or equal to expected.
82+
*
83+
* Usage: assertThat(collection).hasSizeGreaterThanOrEqualTo(1)
84+
*/
85+
fun <T> Assert<Collection<T>>.hasSizeGreaterThanOrEqualTo(expected: Int) = given { actual ->
86+
if (actual.size >= expected) return@given
87+
expected("to have size at least:${show(expected)} but was:${show(actual.size)}")
88+
}
89+
90+
/**
91+
* Extension function to assert that a collection contains any of the given elements.
92+
*
93+
* Usage: assertThat(collection).containsAnyOf(element1, element2)
94+
*/
95+
fun <T> Assert<Collection<T>>.containsAnyOf(vararg elements: T) = given { actual ->
96+
if (elements.any { it in actual }) return@given
97+
expected("to contain any of:${show(elements.toList())} but was:${show(actual)}")
98+
}

0 commit comments

Comments
 (0)