rocketraman
01/19/2021, 3:11 PMList<Int>
and I want to check that the first value from the list is less than the second. How can I express this in Strikt?
expectThat(listOf(5, 6)) {
// this[1] is a builder, so can't do this:
this[0].isLessThan(this[1])
}
Note because of the way the unit test is structured, I don't actually have access to the original list, only the assertion builder. So I can't just do this[0].isLessThan(originalList[1])
.christophsturm
01/19/2021, 3:16 PMexpectThat(listOf(5, 6)) {
get {get(0)<get(1)}.isTrue()
}
rocketraman
01/19/2021, 3:19 PMrobfletcher
01/19/2021, 3:22 PMexpectThat(listOf(5, 6))
.assert("first element < second") {
if (it[0] < it[1]) pass(it)
else fail(it)
}
You could express it more simply with:
expectThat(listOf(5, 6))
.assertThat("first element < second") {
it[0] < it[1]
}
And if this is something you want to use in multiple places extract it as a re-usable custom assertion:
fun Assertion.Builder<List<Int>>.firstElementIsLessThanSecond() =
assert("first element < second") {
if (it[0] < it[1]) pass(it)
else fail(it)
}
robfletcher
01/19/2021, 3:24 PMAssertion.Builder<List<Comparable<*>>>.isOrdered
assertion would be even betterrocketraman
01/19/2021, 3:26 PMrobfletcher
01/19/2021, 3:29 PMget { this[0] < this[1] }.isTrue()
form is that I think the error message wouldnāt be very helpful if it failedrocketraman
01/19/2021, 3:29 PMchristophsturm
01/19/2021, 3:32 PMchristophsturm
01/19/2021, 3:35 PMrocketraman
01/19/2021, 3:36 PMexpectThat(input) {
filter { interestingElements }
.hasSize(2)
.map { extractIntAttribute }
.assertThat("first element < second) {
it.toList().let { it[0] < it[1] }
}
}
rocketraman
01/19/2021, 3:38 PMisOrdered
would indeed work well instead of that last assertThat
.robfletcher
01/19/2021, 3:41 PMrobfletcher
01/19/2021, 3:43 PMAssertion.Builder<List<*>>.isOrderedAccordingTo(Comparator)
overloadrocketraman
01/19/2021, 3:49 PMget
form isn't too bad, its just not quite as clear as it doesn't show the actual values:
ā¼ toList().let { it[0] < it[1] }:
ā is true
found false
vs
ā¼ [0, 0]:
ā first element < second
robfletcher
01/19/2021, 3:52 PMrobfletcher
01/19/2021, 3:54 PMrocketraman
01/19/2021, 3:57 PMrocketraman
01/19/2021, 3:57 PMrobfletcher
01/19/2021, 4:16 PMrobfletcher
01/19/2021, 5:18 PMexpectThat(myList.slice(0..1)) isEqualTo myList.slice(0..1).sorted()
rocketraman
01/19/2021, 6:40 PMrocketraman
01/19/2021, 6:41 PMfun <T: Any> PAssert.IterableAssert<T>.expectThat(block: Assertion.Builder<Iterable<T>>.() -> Unit): PAssert.IterableAssert<T> {
satisfies {
expectThat(it, block)
null
}
return this
}