@@ -23,6 +23,7 @@ private[debug] object DebugStepAssert:
2323 val eval = s " eval (.*) " .r
2424 val result = " result (.*)" .r
2525 val error = " error (.*)" .r
26+ val multiLineError = s " error $trailing" .r
2627
2728 def loop (lines : List [String ], acc : List [DebugStepAssert [? ]]): List [DebugStepAssert [? ]] =
2829 lines match
@@ -37,24 +38,28 @@ private[debug] object DebugStepAssert:
3738 case next(pattern) :: tail =>
3839 val step = DebugStepAssert (Next , checkLineOrMethod(pattern))
3940 loop(tail, step :: acc)
40- case eval(expr) :: resOrError :: tail =>
41- val expected = parseResultOrError(resOrError )
42- val step = DebugStepAssert (Eval (expr), checkEval(expected) )
43- loop(tail , step :: acc)
41+ case eval(expr) :: tail0 =>
42+ val (assertion, tail1) = parseEvalAssertion(tail0 )
43+ val step = DebugStepAssert (Eval (expr), assertion )
44+ loop(tail1 , step :: acc)
4445 case multiLineEval() :: tail0 =>
4546 val (exprLines, tail1) = tail0.span(_.startsWith(" " ))
4647 val expr = exprLines.map(s => s.stripPrefix(" " )).mkString(" \n " )
47- val expected = parseResultOrError (tail1.head )
48- val step = DebugStepAssert (Eval (expr), checkEval(expected) )
49- loop(tail1.tail , step :: acc)
48+ val (assertion, tail2) = parseEvalAssertion (tail1)
49+ val step = DebugStepAssert (Eval (expr), assertion )
50+ loop(tail2 , step :: acc)
5051 case trailing() :: tail => loop(tail, acc)
5152 case invalid :: tail => throw new Exception (s " Cannot parse debug step: $invalid" )
5253
53- def parseResultOrError (line : String ): Either [String , String ] =
54- line match
55- case result(expected) => Right (expected)
56- case error(expected) => Left (expected)
57- case invalid => throw new Exception (s " Cannot parse as result or error: $invalid" )
54+ def parseEvalAssertion (lines : List [String ]): (Either [String , String ] => Unit , List [String ]) =
55+ lines match
56+ case Nil => throw new Exception (s " Missing result or error " )
57+ case result(expected) :: tail => (checkResult(expected), tail)
58+ case error(expected) :: tail => (checkError(Seq (expected)), tail)
59+ case multiLineError() :: tail0 =>
60+ val (expected, tail1) = tail0.span(_.startsWith(" " ))
61+ (checkError(expected.map(_.stripPrefix(" " ))), tail1)
62+ case invalid :: _ => throw new Exception (s " Cannot parse as result or error: $invalid" )
5863
5964 loop(readLines(checkFile), Nil )
6065 end parseCheckFile
@@ -71,12 +76,35 @@ private[debug] object DebugStepAssert:
7176
7277 private def checkMethod (methodName : String )(location : Location ): Unit = assert(methodName == location.method.name)
7378
74- private def checkEval (expected : Either [String , String ])(obtained : Either [String , String ]): Unit =
75- (expected, obtained) match
76- case (Left (expected), Left (obtained)) => assert(expected.r.matches(obtained), s " obtained $obtained, expected $expected" )
77- case (Right (expected), Right (obtained)) => assert(expected.r.matches(obtained.toString), s " obtained $obtained, expected $expected" )
78- case (Left (_), Right (_)) => throw new AssertionError (" evaluation succeeded but error expected" )
79- case (Right (_), Left (_)) => throw new AssertionError (" evaluation failed" )
79+ private def checkResult (expected : String )(obtained : Either [String , String ]): Unit =
80+ obtained match
81+ case Left (obtained) =>
82+ val message =
83+ s """ |Evaluation failed:
84+ | $obtained""" .stripMargin
85+ throw new AssertionError (message)
86+ case Right (obtained) =>
87+ val message =
88+ s """ |Expected: $expected
89+ |Obtained: $obtained""" .stripMargin
90+ assert(expected.r.matches(obtained.toString), message)
91+
92+ private def checkError (expected : Seq [String ])(obtained : Either [String , String ]): Unit =
93+ obtained match
94+ case Left (obtained) =>
95+ val message =
96+ s """ |Expected:
97+ | ${expected.mkString(" \n " )}
98+ |Obtained:
99+ | $obtained""" .stripMargin
100+ assert(expected.forall(e => e.r.findFirstMatchIn(obtained).isDefined), message)
101+ case Right (obtained) =>
102+ val message =
103+ s """ |Evaluation succeeded but failure expected.
104+ |Obtained: $obtained
105+ | """ .stripMargin
106+ throw new AssertionError (message)
107+
80108
81109end DebugStepAssert
82110
0 commit comments