rocketraman
08/25/2021, 4:36 PMfun <T: Comparable<T>> ClosedRange<T>.intersects(other: ClosedRange<T>): Boolean
.
My test has inputs (note there is a fake failure case here, 0..0
is not intersecting):
val intRangeRef = 1..5
val intRangesIntersecting = listOf(0..0, 0..2, 4..5, 4..6)
val intRangesDisjoint = listOf(-2..-1, 6..7)
How would you structure the assertions? So far I've got this, which produces nice output on failures, but thoughts on style?
expectThat(intRangesIntersecting).all {
get { intRangeRef.intersects(this) } isEqualTo true
get { this.intersects(intRangeRef) } isEqualTo true
}
expectThat(intRangesDisjoint).all {
get { intRangeRef.intersects(this) } isEqualTo false
get { this.intersects(intRangeRef) } isEqualTo false
}
robfletcher
08/25/2021, 5:04 PMfun Assertion.Builder<IntRange>.intersects(IntRange)
rocketraman
08/25/2021, 5:09 PMfun Assertion.Builder<IntRange>.intersects(range: IntRange) {
assert("intersects $range") {
if (it.intersects(range).and(range.intersects(it))) pass() else fail()
}
}
rocketraman
08/25/2021, 5:09 PMexpectThat(intRangesIntersecting).all {
intersects(intRangeRef)
}
expectThat(intRangesDisjoint).none {
intersects(intRangeRef)
}
robfletcher
08/25/2021, 5:15 PMrobfletcher
08/25/2021, 5:16 PMpass
and fail
which can help get really nice diagnostic outputrocketraman
08/25/2021, 5:18 PMpass
and fail
here. The output is already really nice:
▼ Expect that [0..0, 0..2, 4..5, 4..6]:
✗ all elements match:
▼ 0..0:
✗ intersects 1..5
▼ 0..2:
✓ intersects 1..5
▼ 4..5:
✓ intersects 1..5
▼ 4..6:
✓ intersects 1..5
Can you elaborate?rocketraman
08/25/2021, 5:22 PMrobfletcher
08/25/2021, 5:26 PMrobfletcher
08/25/2021, 5:26 PMrocketraman
08/25/2021, 5:27 PMrobfletcher
08/25/2021, 5:31 PM