Skip to content

Commit

Permalink
Add a test for assertAll callstacks.
Browse files Browse the repository at this point in the history
This test can be useful if we ever refactor code and don't
realize we broke our logic for determining how callstacks
are collected.
  • Loading branch information
bitspittle committed Nov 24, 2024
1 parent b32ac41 commit 882e9a9
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/commonTest/kotlin/com/varabyte/truthish/AssertAllTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,54 @@ class AssertAllTest {
}
}
}

@Test
fun assertAllCallstacksAreCorrect() {
// This test can be useful if we ever refactor code and don't realize we broke our logic for determining how
// callstacks are collected.

val a = "A"
val b = "B"
val c = "C"

assertThrows<MultipleReportsError> {
assertAll {
that(a).isEqualTo(a) //
that(a).isEqualTo(b) // ❌ // First failure, line0
that(b).isEqualTo(b) //
that(b).isEqualTo(c) // ❌ // Second failure, line0 + 2
that(c).isEqualTo(a) // ❌ // Third failure, line0 + 3
that(c).isEqualTo(c) //
that(a).isEqualTo(c) // ❌ // Fourth failure, line0 + 5
}
}.let { e ->
// Use a regex to extract callstack values, so that this test will still pass even if the line numbers change
// Example callstack entry: com.varabyte.truthish.AssertAllTest$assertAllCallstacksAreCorrect$2$1.invoke(AssertAllTest.kt:123)
val expectedAt = Regex(
"""com\.varabyte\.truthish\.AssertAllTest${"\\$"}assertAllCallstacksAreCorrect${"\\$"}(\d+)${"\\$"}(\d+)\.invoke\(AssertAllTest\.kt:(\d+)\)"""
)
val match = expectedAt.matchEntire(e.reports[0].details.find(DetailsFor.AT).toString())!!
val outerLambdaId = match.groupValues[1].toInt() // assertThrows
val innerLambdaId = match.groupValues[2].toInt() // assertAll
val lineNumber = match.groupValues[3].toInt()

assertAll {
// "Error report" to "delta distance from the first report"
val reportsToCheck = listOf(
e.reports[1] to 2,
e.reports[2] to 3,
e.reports[3] to 5
)

reportsToCheck.forEach { (report, lineDelta) ->
with(expectedAt.matchEntire(report.details.find(DetailsFor.AT).toString())!!) {
that(groupValues[1].toInt()).isEqualTo(outerLambdaId)
that(groupValues[2].toInt()).isEqualTo(innerLambdaId)
that(groupValues[3].toInt()).isEqualTo(lineNumber + lineDelta)
}
}
}
}
}

}

0 comments on commit 882e9a9

Please sign in to comment.