Skip to content

Infrastructure for per-phase tests, with inline source as a string. #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/dotty/tools/dotc/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
10 changes: 10 additions & 0 deletions src/dotty/tools/dotc/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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()
Expand Down
25 changes: 25 additions & 0 deletions test/test/DottyTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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)
}
15 changes: 15 additions & 0 deletions test/test/SamplePhaseTest.scala
Original file line number Diff line number Diff line change
@@ -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(<empty>),List(TypeDef(Modifiers(,,List()),A,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class scala),Unit)],EmptyTree),List(),ValDef(Modifiers(private,,List()),_,EmptyTree,EmptyTree),List()))))"
)
}

}