I'm testing a function with signature `fun <T: ...
# strikt
r
I'm testing a function with signature
fun <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):
Copy code
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?
Copy code
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
}
r
you could consider writing a
fun Assertion.Builder<IntRange>.intersects(IntRange)
r
Right, ok, this seems to work well:
Copy code
fun Assertion.Builder<IntRange>.intersects(range: IntRange) {
    assert("intersects $range") {
      if (it.intersects(range).and(range.intersects(it))) pass() else fail()
    }
  }
Calling that like this:
Copy code
expectThat(intRangesIntersecting).all {
      intersects(intRangeRef)
    }
    expectThat(intRangesDisjoint).none {
      intersects(intRangeRef)
    }
r
that seems nice. I think the trick is always to ensure that if it fails you get good information about what the actual and expected values were
in that case you can pass expected/actual values to
pass
and
fail
which can help get really nice diagnostic output
r
Hmm I'm not seeing the value of adding expected/actual values to
pass
and
fail
here. The output is already really nice:
Copy code
▼ 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?
I guess you were talking more in general as opposed to this specific case.
r
oh yeah, if that’s what you’re getting now I think you’re good.
wasn’t 100% sure what it would look like just based on looking at the code
r
Kudos on a framework that makes it so easy to get nice output!
r
Thanks. It could still use some improvements for certain type of test subjects, but what you’ve got there is very nice & clear