aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Mulder <felix.mulder@gmail.com>2016-03-08 20:12:08 +0100
committerFelix Mulder <felix.mulder@gmail.com>2016-04-07 08:40:27 +0200
commit3b4906d9dc5621d595259254057dd5649e4f9862 (patch)
tree180569fcc9fabadf8f2f0677472ad4cff2873e31
parent0e6cce669a784ff1bb209372b191d0985e0c1e61 (diff)
downloaddotty-3b4906d9dc5621d595259254057dd5649e4f9862.tar.gz
dotty-3b4906d9dc5621d595259254057dd5649e4f9862.tar.bz2
dotty-3b4906d9dc5621d595259254057dd5649e4f9862.zip
Add tests for classes and traits
-rw-r--r--project/Build.scala2
-rw-r--r--src/dotty/tools/dotc/parsing/Parsers.scala3
-rw-r--r--test/test/DottyDocTests.scala159
-rw-r--r--test/test/DottyTest.scala3
4 files changed, 163 insertions, 4 deletions
diff --git a/project/Build.scala b/project/Build.scala
index 723a7e129..0e6e0a9bb 100644
--- a/project/Build.scala
+++ b/project/Build.scala
@@ -182,7 +182,7 @@ object DottyBuild extends Build {
}
).
settings(
- addCommandAlias("partest", ";test:package;package;test:runMain dotc.build;lockPartestFile;test:test;runPartestRunner") ++
+ addCommandAlias("partest", ";test:package;package;test:runMain dotc.build;lockPartestFile;test:test;runPartestRunner;test:runMain test.DottyDocTests") ++
addCommandAlias("partest-only", ";test:package;package;test:runMain dotc.build;lockPartestFile;test:test-only dotc.tests;runPartestRunner") ++
addCommandAlias("partest-only-no-bootstrap", ";test:package;package; lockPartestFile;test:test-only dotc.tests;runPartestRunner")
)
diff --git a/src/dotty/tools/dotc/parsing/Parsers.scala b/src/dotty/tools/dotc/parsing/Parsers.scala
index a3f15d4b7..ad50bf476 100644
--- a/src/dotty/tools/dotc/parsing/Parsers.scala
+++ b/src/dotty/tools/dotc/parsing/Parsers.scala
@@ -1909,6 +1909,7 @@ object Parsers {
*/
def classDef(mods: Modifiers): TypeDef = atPos(tokenRange) {
val name = ident().toTypeName
+ val docstring = in.getDocString()
val constr = atPos(in.offset) {
val tparams = typeParamClauseOpt(ParamOwner.Class)
val cmods = constrModsOpt()
@@ -1920,7 +1921,7 @@ object Parsers {
TypeDef(name, templ)
.withMods(mods)
- .withComment(in.getDocString())
+ .withComment(docstring)
}
/** ConstrMods ::= AccessModifier
diff --git a/test/test/DottyDocTests.scala b/test/test/DottyDocTests.scala
new file mode 100644
index 000000000..20c10cc4a
--- /dev/null
+++ b/test/test/DottyDocTests.scala
@@ -0,0 +1,159 @@
+package test
+
+import dotty.tools.dotc.Compiler
+import dotty.tools.dotc.core.Phases.Phase
+import dotty.tools.dotc.typer.FrontEnd
+import dotty.tools.dotc.core.Contexts.Context
+import dotty.tools.dotc.ast.tpd
+import dotty.tools.dotc.ast.Trees._
+
+/** All tests need to extend this trait and define `source` and `assertion`
+ *
+ * Instances also need to be placed in the `DottyDocTests::tests` sequence to
+ * be included in the run
+ */
+trait DottyDocTest extends DottyTest { self =>
+ /** Source code in string format */
+ def source: String
+
+ /** A test to run against the resulting code */
+ def assertion: PartialFunction[Tree[Untyped], Unit]
+
+ private def defaultAssertion: PartialFunction[Tree[Untyped], Unit] = {
+ case x => assert(false, "Couldn't match resulting AST to expected AST in: " + x.show)
+ }
+
+ private def compiler(assertion: PartialFunction[Tree[Untyped], Unit]) = new Compiler {
+ override def phases = {
+ val checker = new Phase {
+ def phaseName = "assertionChecker"
+ override def run(implicit ctx: Context): Unit =
+ (assertion orElse defaultAssertion)(ctx.compilationUnit.untpdTree)
+ }
+
+ List(List(new FrontEnd)) ::: List(List(checker))
+ }
+ }
+
+ def checkDocString(actual: Option[String], expected: String): Unit = actual match {
+ case Some(str) => {
+ assert(str == expected, s"""Docstring: "$str" didn't match expected "$expected"""")
+ }
+ case None =>
+ assert(false, s"""No docstring found, expected: "$expected"""")
+ }
+
+ def run(): Unit = {
+ val c = compiler(assertion)
+ c.rootContext(ctx)
+ c.newRun.compile(source)
+ println(s"${self.getClass.getSimpleName.split("\\$").last} passed")
+ }
+}
+
+/** Add tests to the `tests` sequence */
+object DottyDocTests extends DottyTest {
+ private[this] val tests = Seq(
+ SingleClassInPackage,
+ MultipleOpenedOnSingleClassInPackage,
+ MultipleClassesInPackage,
+ SingleCaseClassWithoutPackage,
+ SingleTraitWihoutPackage,
+ MultipleTraitsWithoutPackage
+ )
+
+ def main(args: Array[String]): Unit = {
+ println("------------ Testing DottyDoc ------------")
+ tests.foreach(_.run)
+ println("--------- DottyDoc tests passed! ----------")
+ }
+}
+
+case object SingleClassInPackage extends DottyDocTest {
+ override val source =
+ """
+ |package a
+ |
+ |/** Hello world! */
+ |class Class(val x: String)
+ """.stripMargin
+
+ override def assertion = {
+ case PackageDef(_, Seq(t @ TypeDef(name, _))) if name.toString == "Class" =>
+ checkDocString(t.rawComment, "/** Hello world! */")
+ }
+}
+
+case object MultipleOpenedOnSingleClassInPackage extends DottyDocTest {
+ override val source =
+ """
+ |package a
+ |
+ |/** Hello /* multiple open */ world! */
+ |class Class(val x: String)
+ """.stripMargin
+
+ override def assertion = {
+ case PackageDef(_, Seq(t @ TypeDef(name, _))) if name.toString == "Class" =>
+ checkDocString(t.rawComment, "/** Hello /* multiple open */ world! */")
+ }
+}
+
+case object MultipleClassesInPackage extends DottyDocTest {
+ override val source =
+ """
+ |package a
+ |
+ |/** Class1 docstring */
+ |class Class1(val x: String)
+ |
+ |/** Class2 docstring */
+ |class Class2(val x: String)
+ """.stripMargin
+
+ override def assertion = {
+ case PackageDef(_, Seq(c1 @ TypeDef(_,_), c2 @ TypeDef(_,_))) => {
+ checkDocString(c1.rawComment, "/** Class1 docstring */")
+ checkDocString(c2.rawComment, "/** Class2 docstring */")
+ }
+ }
+}
+
+case object SingleCaseClassWithoutPackage extends DottyDocTest {
+ override val source =
+ """
+ |/** Class without package */
+ |case class Class(val x: Int)
+ """.stripMargin
+
+ override def assertion = {
+ case PackageDef(_, Seq(t @ TypeDef(_,_))) => checkDocString(t.rawComment, "/** Class without package */")
+ }
+}
+
+case object SingleTraitWihoutPackage extends DottyDocTest {
+ override val source = "/** Trait docstring */\ntrait Trait"
+
+ override def assertion = {
+ case PackageDef(_, Seq(t @ TypeDef(_,_))) => checkDocString(t.rawComment, "/** Trait docstring */")
+ }
+}
+
+case object MultipleTraitsWithoutPackage extends DottyDocTest {
+ //TODO: This will fail if the tratis don't have bodies, because of the Scanner
+ override val source =
+ """
+ |/** Trait1 docstring */
+ |trait Trait1 {}
+ |
+ |/** Trait2 docstring */
+ |trait Trait2 {}
+ """.stripMargin
+
+ override def assertion = {
+ case PackageDef(_, Seq(t1 @ TypeDef(_,_), t2 @ TypeDef(_,_))) => {
+ checkDocString(t1.rawComment, "/** Trait1 docstring */")
+ checkDocString(t2.rawComment, "/** Trait2 docstring */")
+ }
+ }
+}
diff --git a/test/test/DottyTest.scala b/test/test/DottyTest.scala
index 77642561a..15d82c208 100644
--- a/test/test/DottyTest.scala
+++ b/test/test/DottyTest.scala
@@ -32,7 +32,6 @@ class DottyTest /*extends ContextEscapeDetection*/ {
}
*/
private def compilerWithChecker(phase: String)(assertion:(tpd.Tree, Context) => Unit) = new Compiler {
- self =>
override def phases = {
val allPhases = super.phases
val targetPhase = allPhases.flatten.find(p => p.phaseName == phase).get
@@ -48,7 +47,7 @@ class DottyTest /*extends ContextEscapeDetection*/ {
}
}
- def checkCompile(checkAfterPhase: String, source:String)(assertion:(tpd.Tree, Context) => Unit): Unit = {
+ def checkCompile(checkAfterPhase: String, source: String)(assertion: (tpd.Tree, Context) => Unit): Unit = {
val c = compilerWithChecker(checkAfterPhase)(assertion)
c.rootContext(ctx)
val run = c.newRun