Skip to content

Commit 5aa8558

Browse files
committed
fix linenumbers in failed assertion traces - take all asserts into account - include asserts inside of expressions
1 parent fc1200f commit 5aa8558

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

utest/src-3/utest/TestBuilder.scala

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package utest
22

3-
import scala.quoted.{ Type => QType, _ }
3+
import scala.quoted.{Type as QType, *}
4+
import utest.framework.{TestCallTree, TestPath, Tree as UTree}
45

5-
import utest.framework.{TestCallTree, Tree => UTree, TestPath }
6+
import scala.util.Success
67

78

89
object TestBuilder:
@@ -27,13 +28,15 @@ object TestBuilder:
2728

2829
private def testCallTreeExpr(using Quotes)(nestedBodyTrees: List[Expr[TestCallTree]], setupStats: List[quotes.reflect.Statement]): Expr[TestCallTree] =
2930
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
31+
val assertInliner = new TreeMap {
32+
override def transformTerm(term: Term)(owner: Symbol): Term = scala.util.Try(term.asExpr) match {
33+
case Success(expr) => expr match
34+
case '{utest.assert($_)} => Inlined(None,Nil,term) //Inlined results in proper line number generation
35+
case _ => super.transformTerm(term)(owner)
36+
case _ => super.transformTerm(term)(owner)
3437
}
35-
case other => other
3638
}
39+
val statsWithInlinedAsserts = assertInliner.transformStats(setupStats)(Symbol.spliceOwner)
3740
val inner =
3841
if nestedBodyTrees.nonEmpty then Block(statsWithInlinedAsserts, '{Right(${Expr.ofList(nestedBodyTrees)}.toIndexedSeq)}.asTerm)
3942
else

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,60 @@ object LineNumbersTests extends utest.TestSuite {
3636
test("test7") {
3737
val result = "testing".trim()
3838
assert(result == "notMatching")
39-
test("innerTest5") {
39+
test("innerTest55") {
40+
assert(1 == 1)
41+
}
42+
result
43+
}
44+
test("test8") {
45+
val result = "testing".trim()
46+
val blockWithFailingAssert = {
47+
assert("matching" == "notMatching")
48+
assert(1 == 2)
49+
}
50+
test("innerTest55") {
51+
assert(1 == 1)
52+
}
53+
result
54+
}
55+
test("test9") {
56+
val result = "testing".trim()
57+
val methodWithFailingAssert = println(
58+
assert(1 == 2)
59+
)
60+
test("innerTest55") {
4061
assert(1 == 1)
4162
}
4263
result
4364
}
65+
test("test10") - Obj(1).method{ x =>
66+
assert(x.elem == "notMatching")
67+
}
68+
test("test11"){
69+
def xx(body: Int)(rest:Int) = body == rest
70+
val partiallyApplied = xx{
71+
assert(1 == 2)
72+
1
73+
}
74+
75+
()
76+
}
77+
test("test12"){
78+
def xx(body: => Int)(rest:Int) = body == rest
79+
val partiallyAppliedByName = xx{
80+
assert(1 == 2)
81+
1
82+
}
83+
84+
partiallyAppliedByName(2)
85+
()
86+
}
4487
}
4588

89+
private case class Obj(arg:Int) {
90+
def method[T](tester: Obj => T):T = tester(this)
91+
val elem = "elem"
92+
}
4693
val testBody = {
4794

4895
val results = TestRunner.run(
@@ -59,6 +106,11 @@ object LineNumbersTests extends utest.TestSuite {
59106
stackTraceLinesFromThisFile(4).exists(_.getLineNumber == 28),
60107
stackTraceLinesFromThisFile(5).exists(_.getLineNumber == 33),
61108
stackTraceLinesFromThisFile(6).exists(_.getLineNumber == 38),
109+
stackTraceLinesFromThisFile(7).exists(_.getLineNumber == 47),
110+
stackTraceLinesFromThisFile(8).exists(_.getLineNumber == 58),
111+
stackTraceLinesFromThisFile(9).exists(_.getLineNumber == 66),
112+
stackTraceLinesFromThisFile(10).exists(_.getLineNumber == 71),
113+
stackTraceLinesFromThisFile(11).exists(_.getLineNumber == 80),
62114
)
63115

64116
}

0 commit comments

Comments
 (0)