Skip to content

Commit fc1200f

Browse files
committed
fix linenumbers in failed assertion traces - take all asserts into account
1 parent 8b07406 commit fc1200f

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

utest/src-3/utest/TestBuilder.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ object TestBuilder:
2727

2828
private def testCallTreeExpr(using Quotes)(nestedBodyTrees: List[Expr[TestCallTree]], setupStats: List[quotes.reflect.Statement]): Expr[TestCallTree] =
2929
import quotes.reflect._
30+
val statsWithInlinedAsserts:List[Statement] = setupStats.map {
31+
case term:Term => term.asExpr match{
32+
case '{utest.assert($_)} => Inlined(None,Nil,term) //Inlined results in proper line number generation
33+
case _ => term
34+
}
35+
case other => other
36+
}
3037
val inner =
31-
if nestedBodyTrees.nonEmpty then Block(setupStats, '{Right(${Expr.ofList(nestedBodyTrees)}.toIndexedSeq)}.asTerm)
38+
if nestedBodyTrees.nonEmpty then Block(statsWithInlinedAsserts, '{Right(${Expr.ofList(nestedBodyTrees)}.toIndexedSeq)}.asTerm)
3239
else
33-
val term = setupStats.takeRight(1).head.asInstanceOf[Term]
34-
val inlinedTerm = Inlined(None,Nil,term) //Inlined results in proper line number generation
35-
Block(setupStats.dropRight(1), '{Left(${inlinedTerm.asExpr})}.asTerm)
40+
Block(statsWithInlinedAsserts.dropRight(1), '{Left(${statsWithInlinedAsserts.last.asExpr})}.asTerm)
3641
'{ TestCallTree(${inner.asExprOf[Either[Any, IndexedSeq[TestCallTree]]]}) }
3742

3843
private def processTest(using Quotes)(test: quotes.reflect.Apply, pathOld: Seq[String], index: Int): (Expr[UTree[String]], Expr[TestCallTree]) =

utest/test/src-jvm/test/utest/LineNumbersTests.scala

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ object LineNumbersTests extends utest.TestSuite {
2828
assert(1 == 2)
2929
}
3030
}
31+
test("test6") {
32+
val result = "testing".trim()
33+
assert(result == "notMatching")
34+
result
35+
}
36+
test("test7") {
37+
val result = "testing".trim()
38+
assert(result == "notMatching")
39+
test("innerTest5") {
40+
assert(1 == 1)
41+
}
42+
result
43+
}
3144
}
3245

3346
val testBody = {
@@ -44,7 +57,8 @@ object LineNumbersTests extends utest.TestSuite {
4457
stackTraceLinesFromThisFile(2).exists(_.getLineNumber == 17),
4558
stackTraceLinesFromThisFile(3).exists(_.getLineNumber == 21),
4659
stackTraceLinesFromThisFile(4).exists(_.getLineNumber == 28),
47-
60+
stackTraceLinesFromThisFile(5).exists(_.getLineNumber == 33),
61+
stackTraceLinesFromThisFile(6).exists(_.getLineNumber == 38),
4862
)
4963

5064
}

0 commit comments

Comments
 (0)