diff --git a/src/commonTest/kotlin/com/varabyte/truthish/AssertAllTest.kt b/src/commonTest/kotlin/com/varabyte/truthish/AssertAllTest.kt index d5f1ce9..c51928a 100644 --- a/src/commonTest/kotlin/com/varabyte/truthish/AssertAllTest.kt +++ b/src/commonTest/kotlin/com/varabyte/truthish/AssertAllTest.kt @@ -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 { + 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) + } + } + } + } + } + }