Skip to content

Commit 106344c

Browse files
committed
Allow multiple calls to checkMessagesAfter in ErrorMessageTest
1 parent 0a7e6c9 commit 106344c

File tree

2 files changed

+21
-36
lines changed

2 files changed

+21
-36
lines changed

compiler/test/dotty/tools/DottyTest.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ trait DottyTest extends ContextEscapeDetection {
1818

1919
dotc.parsing.Scanners // initialize keywords
2020

21-
implicit var ctx: Context = {
21+
implicit var ctx: Context = initialCtx
22+
23+
protected def initialCtx: FreshContext = {
2224
val base = new ContextBase {}
2325
import base.settings._
2426
val ctx = base.initialCtx.fresh
@@ -55,18 +57,20 @@ trait DottyTest extends ContextEscapeDetection {
5557
}
5658
}
5759

58-
def checkCompile(checkAfterPhase: String, source: String)(assertion: (tpd.Tree, Context) => Unit): Unit = {
60+
def checkCompile(checkAfterPhase: String, source: String)(assertion: (tpd.Tree, Context) => Unit): Context = {
5961
val c = compilerWithChecker(checkAfterPhase)(assertion)
6062
c.rootContext(ctx)
6163
val run = c.newRun
6264
run.compile(source)
65+
run.runContext
6366
}
6467

65-
def checkCompile(checkAfterPhase: String, sources: List[String])(assertion: (tpd.Tree, Context) => Unit): Unit = {
68+
def checkCompile(checkAfterPhase: String, sources: List[String])(assertion: (tpd.Tree, Context) => Unit): Context = {
6669
val c = compilerWithChecker(checkAfterPhase)(assertion)
6770
c.rootContext(ctx)
6871
val run = c.newRun
6972
run.compile(sources)
73+
run.runContext
7074
}
7175

7276
def methType(names: String*)(paramTypes: Type*)(resultType: Type = defn.UnitType) =

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTest.scala

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import org.junit.Assert._
1111

1212
trait ErrorMessagesTest extends DottyTest {
1313

14-
ctx = freshReporter(ctx)
15-
16-
private def freshReporter(ctx: Context) =
17-
ctx.fresh.setReporter(new CapturingReporter)
14+
private def freshReporter(ctx: Context) = {
15+
val rep = new StoreReporter(null)
16+
with UniqueMessagePositions with HideNonSensicalMessages
17+
initialCtx.setReporter(rep)
18+
}
1819

1920
class Report(messages: List[Message], ictx: Context) {
20-
def expect(f: (Context, List[Message]) => Unit): Unit = {
21+
def expect(f: (Context, List[Message]) => Unit): Unit =
2122
f(ictx, messages)
22-
}
2323

2424
def expectNoErrors: Unit =
2525
assert(this.isInstanceOf[EmptyReport], "errors found when not expected")
@@ -32,35 +32,16 @@ trait ErrorMessagesTest extends DottyTest {
3232
|there are no errors or the compiler crashes.""".stripMargin)
3333
}
3434

35-
class CapturingReporter extends Reporter
36-
with UniqueMessagePositions with HideNonSensicalMessages {
37-
private[this] val buffer = new mutable.ListBuffer[Message]
38-
private[this] var capturedContext: Context = _
39-
40-
def doReport(m: MessageContainer)(implicit ctx: Context) = {
41-
capturedContext = ctx
42-
buffer append m.contained()
43-
}
44-
45-
def toReport: Report =
46-
if (capturedContext eq null)
47-
new EmptyReport
48-
else {
49-
val xs = buffer.reverse.toList
50-
buffer.clear()
51-
52-
val ctx = capturedContext
53-
capturedContext = null
54-
55-
new Report(xs, ctx)
56-
}
57-
}
58-
5935
def checkMessagesAfter(checkAfterPhase: String)(source: String): Report = {
60-
checkCompile(checkAfterPhase, source) { (_,ictx) => () }
61-
val rep = ctx.reporter.asInstanceOf[CapturingReporter].toReport
6236
ctx = freshReporter(ctx)
63-
rep
37+
val runCtx = checkCompile(checkAfterPhase, source) { (_, _) => () }
38+
39+
if (!runCtx.reporter.hasErrors) new EmptyReport
40+
else {
41+
val rep = runCtx.reporter.asInstanceOf[StoreReporter]
42+
val msgs = rep.removeBufferedMessages(runCtx).map(_.contained()).reverse
43+
new Report(msgs, runCtx)
44+
}
6445
}
6546

6647
def assertMessageCount(expected: Int, messages: List[Message]): Unit =

0 commit comments

Comments
 (0)