From 301d0c8a79d56d18fe0ae9c09f350d4364d4baf1 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Sat, 1 Mar 2014 14:52:04 +0100 Subject: [PATCH] Infrastructure for per-phase tests, with inline source as a string. --- src/dotty/tools/dotc/Compiler.scala | 3 ++- src/dotty/tools/dotc/Run.scala | 10 ++++++++++ test/test/DottyTest.scala | 25 +++++++++++++++++++++++++ test/test/SamplePhaseTest.scala | 15 +++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/test/SamplePhaseTest.scala diff --git a/src/dotty/tools/dotc/Compiler.scala b/src/dotty/tools/dotc/Compiler.scala index e48fed93bd31..9844557f5052 100644 --- a/src/dotty/tools/dotc/Compiler.scala +++ b/src/dotty/tools/dotc/Compiler.scala @@ -7,10 +7,11 @@ import Periods._ import Symbols._ import typer.{FrontEnd, Typer, Mode, ImportInfo} import reporting.ConsoleReporter +import dotty.tools.dotc.core.Phases.Phase class Compiler { - def phases = List(new FrontEnd) + def phases: List[Phase] = List(new FrontEnd) var runId = 1 def nextRunId = { runId += 1; runId } diff --git a/src/dotty/tools/dotc/Run.scala b/src/dotty/tools/dotc/Run.scala index 928bf753a992..cc9b8db3a025 100644 --- a/src/dotty/tools/dotc/Run.scala +++ b/src/dotty/tools/dotc/Run.scala @@ -6,6 +6,7 @@ import Contexts._, Periods._, Symbols._ import io.PlainFile import util.{SourceFile, NoSource, Stats, SimpleMap} import reporting.Reporter +import java.io.FileWriter class Run(comp: Compiler)(implicit ctx: Context) { @@ -29,6 +30,15 @@ class Run(comp: Compiler)(implicit ctx: Context) { } } + def compile(sourceCode: String): Unit = { + val tmpFile = java.io.File.createTempFile("dotty-source-tmp", ".scala") + tmpFile.createNewFile() + val writer = new FileWriter(tmpFile) + writer.write(sourceCode) + writer.close() + compile(List(tmpFile.getAbsolutePath)) + } + /** Print summary; return # of errors encountered */ def printSummary(): Reporter = { ctx.runInfo.printMaxConstraint() diff --git a/test/test/DottyTest.scala b/test/test/DottyTest.scala index f2d3ef92e5bb..07b29a5f0204 100644 --- a/test/test/DottyTest.scala +++ b/test/test/DottyTest.scala @@ -8,6 +8,11 @@ import Types._, Symbols._, Decorators._ import dotty.tools.dotc.printing.Texts._ import dotty.tools.dotc.reporting.ConsoleReporter import dotty.tools.dotc.core.Decorators._ +import dotty.tools.dotc.ast.tpd +import dotty.tools.dotc.Compiler + +import dotty.tools.dotc +import dotty.tools.dotc.core.Phases.Phase class DottyTest { @@ -33,6 +38,26 @@ class DottyTest { ctx } + def checkCompile(checkAfterPhase: String, source:String)(assertion:tpd.Tree =>Unit): Unit = { + val c = new Compiler { + override def phases = { + val allPhases = super.phases + val targetPhase = allPhases.find{p=> p.name == checkAfterPhase} + assert(targetPhase isDefined) + val phasesBefore = allPhases.takeWhile(x=> ! (x eq targetPhase.get)) + + val checker = new Phase{ + def name = "assertionChecker" + override def run(implicit ctx: Context): Unit = assertion(ctx.compilationUnit.tpdTree) + } + phasesBefore:::List(targetPhase.get, checker) + } + } + c.rootContext(ctx) + val run = c.newRun + run.compile(source) + } + def methType(names: String*)(paramTypes: Type*)(resultType: Type = defn.UnitType) = MethodType(names.toList map (_.toTermName), paramTypes.toList, resultType) } diff --git a/test/test/SamplePhaseTest.scala b/test/test/SamplePhaseTest.scala new file mode 100644 index 000000000000..dde414420902 --- /dev/null +++ b/test/test/SamplePhaseTest.scala @@ -0,0 +1,15 @@ +package test + +import org.junit.{Assert, Test} + +class SamplePhaseTest extends DottyTest { + + @Test + def testTypechekingSimpleClass = checkCompile("frontend", "class A{}") { + tree => + Assert.assertTrue("can typecheck simple class", + tree.toString == "PackageDef(Ident(),List(TypeDef(Modifiers(,,List()),A,Template(DefDef(Modifiers(,,List()),,List(),List(List()),TypeTree[TypeRef(ThisType(module class scala),Unit)],EmptyTree),List(),ValDef(Modifiers(private,,List()),_,EmptyTree,EmptyTree),List()))))" + ) + } + +}