blob: 807e3a40aee767f7216c77e16a8f2836bd84b3d0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package dotty.tools
package dotc
package reporting
import diagnostic._
import core.Contexts.Context
import scala.collection.mutable
import org.junit.Assert._
trait ErrorMessagesTest extends DottyTest {
class Report(messages: List[Message], ictx: Context) {
def expect(f: (Context, List[Message]) => Unit): Unit = {
f(ictx, messages)
}
def expectNoErrors: Unit = this.isInstanceOf[FailedReport]
}
class FailedReport extends Report(Nil, null) {
override def expect(f: (Context, List[Message]) => Unit) =
fail("""|
|Couldn't capture errors from compiled sources, this can happen if
|there are no errors or the compiler crashes.""".stripMargin)
}
class CapturingReporter extends Reporter
with UniqueMessagePositions with HideNonSensicalMessages {
private[this] val buffer = new mutable.ListBuffer[Message]
private[this] var capturedContext: Context = _
def doReport(m: MessageContainer)(implicit ctx: Context) = {
capturedContext = ctx
buffer append m.contained
}
def toReport: Report =
if (capturedContext eq null)
new FailedReport
else {
val xs = buffer.reverse.toList
buffer.clear()
val ctx = capturedContext
capturedContext = null
new Report(xs, ctx)
}
}
ctx = freshReporter(ctx)
private def freshReporter(ctx: Context) =
ctx.fresh.setReporter(new CapturingReporter)
def checkMessages(source: String): Report = {
checkCompile("frontend", source) { (_,ictx) => () }
val rep = ctx.reporter.asInstanceOf[CapturingReporter].toReport
ctx = freshReporter(ctx)
rep
}
def messageCount(expected: Int, messages: List[Message]): Unit =
assertEquals(
expected,
messages.length
)
}
|