aboutsummaryrefslogtreecommitdiff
path: root/compiler/test/dotty/tools/dotc/reporting
diff options
context:
space:
mode:
authorFelix Mulder <felix.mulder@gmail.com>2017-02-10 14:35:00 +0100
committerFelix Mulder <felix.mulder@gmail.com>2017-02-10 14:35:00 +0100
commit14bfa1ab9a7ce95e99f44449b56f52ec4b6439e0 (patch)
tree43a4e6eaa2f6609b08a1a55a4993244371f22d32 /compiler/test/dotty/tools/dotc/reporting
parent75bea8dccce2bc3c0e8298ee71061c9871fd26ac (diff)
downloaddotty-14bfa1ab9a7ce95e99f44449b56f52ec4b6439e0.tar.gz
dotty-14bfa1ab9a7ce95e99f44449b56f52ec4b6439e0.tar.bz2
dotty-14bfa1ab9a7ce95e99f44449b56f52ec4b6439e0.zip
Fix #1965: add proper testing infrastructure for reporting tests
Diffstat (limited to 'compiler/test/dotty/tools/dotc/reporting')
-rw-r--r--compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTest.scala70
-rw-r--r--compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala45
2 files changed, 115 insertions, 0 deletions
diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTest.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTest.scala
new file mode 100644
index 000000000..807e3a40a
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTest.scala
@@ -0,0 +1,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
+ )
+}
diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
new file mode 100644
index 000000000..eb7b944b1
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
@@ -0,0 +1,45 @@
+package dotty.tools
+package dotc
+package reporting
+
+import core.Contexts.Context
+import diagnostic.messages._
+
+import org.junit.Assert._
+import org.junit.Test
+
+class ErrorMessagesTests extends ErrorMessagesTest {
+ // In the case where there are no errors, we can do "expectNoErrors" in the
+ // `Report`
+ @Test def noErrors =
+ checkMessages("""class Foo""")
+ .expectNoErrors
+
+ @Test def typeMismatch =
+ checkMessages {
+ """
+ |object Foo {
+ | def bar: String = 1
+ |}
+ """.stripMargin
+ }
+ .expect { (ictx, messages) =>
+ implicit val ctx: Context = ictx
+ val defn = ictx.definitions
+
+ // Assert that we only got one error message
+ messageCount(1, messages)
+
+ // Pattern match out the expected error
+ val TypeMismatch(found, expected, _, _) :: Nil = messages
+
+ // The type of the right hand side will actually be the constant 1,
+ // therefore we check if it "derivesFrom" `IntClass`
+ assert(found.derivesFrom(defn.IntClass), s"found was: $found")
+
+ // The expected type is `scala.String` which we dealias to
+ // `java.lang.String` and compare with `=:=` to `defn.StringType` which
+ // is a type reference to `java.lang.String`
+ assert(expected.dealias =:= defn.StringType, s"expected was: $expected")
+ }
+}