Skip to content

Commit 14bfa1a

Browse files
committed
Fix #1965: add proper testing infrastructure for reporting tests
1 parent 75bea8d commit 14bfa1a

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

compiler/test/dotty/tools/dotc/parsing/DocstringTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ trait DocstringTest extends DottyTest {
2727

2828
def checkFrontend(source: String)(docAssert: PartialFunction[Tree[Untyped], Unit]) = {
2929
checkCompile("frontend", source) { (_, ctx) =>
30-
implicit val c = ctx
30+
implicit val c: Context = ctx
3131
(docAssert orElse defaultAssertion)(ctx.compilationUnit.untpdTree)
3232
}
3333
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package dotty.tools
2+
package dotc
3+
package reporting
4+
5+
import diagnostic._
6+
import core.Contexts.Context
7+
8+
import scala.collection.mutable
9+
10+
import org.junit.Assert._
11+
12+
trait ErrorMessagesTest extends DottyTest {
13+
14+
class Report(messages: List[Message], ictx: Context) {
15+
def expect(f: (Context, List[Message]) => Unit): Unit = {
16+
f(ictx, messages)
17+
}
18+
19+
def expectNoErrors: Unit = this.isInstanceOf[FailedReport]
20+
}
21+
22+
class FailedReport extends Report(Nil, null) {
23+
override def expect(f: (Context, List[Message]) => Unit) =
24+
fail("""|
25+
|Couldn't capture errors from compiled sources, this can happen if
26+
|there are no errors or the compiler crashes.""".stripMargin)
27+
}
28+
29+
class CapturingReporter extends Reporter
30+
with UniqueMessagePositions with HideNonSensicalMessages {
31+
private[this] val buffer = new mutable.ListBuffer[Message]
32+
private[this] var capturedContext: Context = _
33+
34+
def doReport(m: MessageContainer)(implicit ctx: Context) = {
35+
capturedContext = ctx
36+
buffer append m.contained
37+
}
38+
39+
def toReport: Report =
40+
if (capturedContext eq null)
41+
new FailedReport
42+
else {
43+
val xs = buffer.reverse.toList
44+
buffer.clear()
45+
46+
val ctx = capturedContext
47+
capturedContext = null
48+
49+
new Report(xs, ctx)
50+
}
51+
}
52+
53+
ctx = freshReporter(ctx)
54+
55+
private def freshReporter(ctx: Context) =
56+
ctx.fresh.setReporter(new CapturingReporter)
57+
58+
def checkMessages(source: String): Report = {
59+
checkCompile("frontend", source) { (_,ictx) => () }
60+
val rep = ctx.reporter.asInstanceOf[CapturingReporter].toReport
61+
ctx = freshReporter(ctx)
62+
rep
63+
}
64+
65+
def messageCount(expected: Int, messages: List[Message]): Unit =
66+
assertEquals(
67+
expected,
68+
messages.length
69+
)
70+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package dotty.tools
2+
package dotc
3+
package reporting
4+
5+
import core.Contexts.Context
6+
import diagnostic.messages._
7+
8+
import org.junit.Assert._
9+
import org.junit.Test
10+
11+
class ErrorMessagesTests extends ErrorMessagesTest {
12+
// In the case where there are no errors, we can do "expectNoErrors" in the
13+
// `Report`
14+
@Test def noErrors =
15+
checkMessages("""class Foo""")
16+
.expectNoErrors
17+
18+
@Test def typeMismatch =
19+
checkMessages {
20+
"""
21+
|object Foo {
22+
| def bar: String = 1
23+
|}
24+
""".stripMargin
25+
}
26+
.expect { (ictx, messages) =>
27+
implicit val ctx: Context = ictx
28+
val defn = ictx.definitions
29+
30+
// Assert that we only got one error message
31+
messageCount(1, messages)
32+
33+
// Pattern match out the expected error
34+
val TypeMismatch(found, expected, _, _) :: Nil = messages
35+
36+
// The type of the right hand side will actually be the constant 1,
37+
// therefore we check if it "derivesFrom" `IntClass`
38+
assert(found.derivesFrom(defn.IntClass), s"found was: $found")
39+
40+
// The expected type is `scala.String` which we dealias to
41+
// `java.lang.String` and compare with `=:=` to `defn.StringType` which
42+
// is a type reference to `java.lang.String`
43+
assert(expected.dealias =:= defn.StringType, s"expected was: $expected")
44+
}
45+
}

0 commit comments

Comments
 (0)