Skip to content

Commit 92a4fef

Browse files
committed
Merge pull request #31 from DarkDimius/tests
Infrastructure for per-phase tests, with inline source as a string.
2 parents 0ebdbde + 301d0c8 commit 92a4fef

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import Periods._
77
import Symbols._
88
import typer.{FrontEnd, Typer, Mode, ImportInfo}
99
import reporting.ConsoleReporter
10+
import dotty.tools.dotc.core.Phases.Phase
1011

1112
class Compiler {
1213

13-
def phases = List(new FrontEnd)
14+
def phases: List[Phase] = List(new FrontEnd)
1415

1516
var runId = 1
1617
def nextRunId = { runId += 1; runId }

src/dotty/tools/dotc/Run.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Contexts._, Periods._, Symbols._
66
import io.PlainFile
77
import util.{SourceFile, NoSource, Stats, SimpleMap}
88
import reporting.Reporter
9+
import java.io.FileWriter
910

1011
class Run(comp: Compiler)(implicit ctx: Context) {
1112

@@ -29,6 +30,15 @@ class Run(comp: Compiler)(implicit ctx: Context) {
2930
}
3031
}
3132

33+
def compile(sourceCode: String): Unit = {
34+
val tmpFile = java.io.File.createTempFile("dotty-source-tmp", ".scala")
35+
tmpFile.createNewFile()
36+
val writer = new FileWriter(tmpFile)
37+
writer.write(sourceCode)
38+
writer.close()
39+
compile(List(tmpFile.getAbsolutePath))
40+
}
41+
3242
/** Print summary; return # of errors encountered */
3343
def printSummary(): Reporter = {
3444
ctx.runInfo.printMaxConstraint()

test/test/DottyTest.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import Types._, Symbols._, Decorators._
88
import dotty.tools.dotc.printing.Texts._
99
import dotty.tools.dotc.reporting.ConsoleReporter
1010
import dotty.tools.dotc.core.Decorators._
11+
import dotty.tools.dotc.ast.tpd
12+
import dotty.tools.dotc.Compiler
13+
14+
import dotty.tools.dotc
15+
import dotty.tools.dotc.core.Phases.Phase
1116

1217
class DottyTest {
1318

@@ -33,6 +38,26 @@ class DottyTest {
3338
ctx
3439
}
3540

41+
def checkCompile(checkAfterPhase: String, source:String)(assertion:tpd.Tree =>Unit): Unit = {
42+
val c = new Compiler {
43+
override def phases = {
44+
val allPhases = super.phases
45+
val targetPhase = allPhases.find{p=> p.name == checkAfterPhase}
46+
assert(targetPhase isDefined)
47+
val phasesBefore = allPhases.takeWhile(x=> ! (x eq targetPhase.get))
48+
49+
val checker = new Phase{
50+
def name = "assertionChecker"
51+
override def run(implicit ctx: Context): Unit = assertion(ctx.compilationUnit.tpdTree)
52+
}
53+
phasesBefore:::List(targetPhase.get, checker)
54+
}
55+
}
56+
c.rootContext(ctx)
57+
val run = c.newRun
58+
run.compile(source)
59+
}
60+
3661
def methType(names: String*)(paramTypes: Type*)(resultType: Type = defn.UnitType) =
3762
MethodType(names.toList map (_.toTermName), paramTypes.toList, resultType)
3863
}

test/test/SamplePhaseTest.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test
2+
3+
import org.junit.{Assert, Test}
4+
5+
class SamplePhaseTest extends DottyTest {
6+
7+
@Test
8+
def testTypechekingSimpleClass = checkCompile("frontend", "class A{}") {
9+
tree =>
10+
Assert.assertTrue("can typecheck simple class",
11+
tree.toString == "PackageDef(Ident(<empty>),List(TypeDef(Modifiers(,,List()),A,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class scala),Unit)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(ThisType(module class lang),Object)]),<init>),List())),ValDef(Modifiers(private,,List()),_,EmptyTree,EmptyTree),List()))))"
12+
)
13+
}
14+
15+
}

0 commit comments

Comments
 (0)