diff options
author | Paul Phillips <paulp@improving.org> | 2012-02-13 09:34:03 -0800 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-02-13 16:06:36 -0800 |
commit | 92fc4e351300e927ae1a8b0a6c383d00e3968c5d (patch) | |
tree | 3ff2366a45c04ad15013479995e34f3e5e43f7c0 /src/partest | |
parent | 6548dcf12d83e327df2f90048140fb95346b7e95 (diff) | |
download | scala-92fc4e351300e927ae1a8b0a6c383d00e3968c5d.tar.gz scala-92fc4e351300e927ae1a8b0a6c383d00e3968c5d.tar.bz2 scala-92fc4e351300e927ae1a8b0a6c383d00e3968c5d.zip |
Existential printing, plus more compiler testing infrastructure.
Direct compiler internals testing. It's really easy, you should probably
use it about 1000 times each. Look at the test:
run/existentials-in-compiler.scala
The checkfile contains the (string representations of the) actual
existentials from the compiler to make sure they correspond properly to
the ones in the source.
Existentials were being printed with wildcards too freely; this has been
tightened up.
Diffstat (limited to 'src/partest')
-rw-r--r-- | src/partest/scala/tools/partest/CompilerTest.scala | 27 | ||||
-rw-r--r-- | src/partest/scala/tools/partest/DirectTest.scala | 32 |
2 files changed, 55 insertions, 4 deletions
diff --git a/src/partest/scala/tools/partest/CompilerTest.scala b/src/partest/scala/tools/partest/CompilerTest.scala new file mode 100644 index 0000000000..dd06c051a4 --- /dev/null +++ b/src/partest/scala/tools/partest/CompilerTest.scala @@ -0,0 +1,27 @@ +/* NSC -- new Scala compiler + * Copyright 2005-2011 LAMP/EPFL + * @author Paul Phillips + */ + +package scala.tools.partest + +import scala.tools.nsc._ + +/** For testing compiler internals directly. + * Each source code string in "sources" will be compiled, and + * the check function will be called with the source code and the + * resulting CompilationUnit. The check implementation should + * test for what it wants to test and fail (via assert or other + * exception) if it is not happy. + */ +abstract class CompilerTest extends DirectTest { + def check(source: String, unit: global.CompilationUnit): Unit + + lazy val global: Global = newCompiler() + lazy val units = compilationUnits(global)(sources: _ *) + + override def extraSettings = "-usejavacp -d " + testOutput.path + + def sources: List[String] = List(code) + def show() = (sources, units).zipped foreach check +} diff --git a/src/partest/scala/tools/partest/DirectTest.scala b/src/partest/scala/tools/partest/DirectTest.scala index be8cac9147..74f511aa4e 100644 --- a/src/partest/scala/tools/partest/DirectTest.scala +++ b/src/partest/scala/tools/partest/DirectTest.scala @@ -35,13 +35,37 @@ abstract class DirectTest extends App { s processArguments (allArgs, true) s } - // compile the code, optionally first adding to the settings - def compile(args: String*) = { + // new compiler + def newCompiler(args: String*): Global = { val settings = newSettings((CommandLineParser tokenize extraSettings) ++ args.toList) - val global = new Global(settings) - new global.Run compileSources List(new BatchSourceFile("<partest>", code)) + new Global(settings) + } + def newSources(sourceCodes: String*) = sourceCodes.toList.zipWithIndex map { + case (src, idx) => new BatchSourceFile("newSource" + (idx + 1), src) + } + def compileString(global: Global)(sourceCode: String): Boolean = { + withRun(global)(_ compileSources newSources(sourceCode)) !global.reporter.hasErrors } + def compilationUnits(global: Global)(sourceCodes: String*): List[global.CompilationUnit] = { + val units = withRun(global) { run => + run compileSources newSources(sourceCodes: _*) + run.units.toList + } + if (global.reporter.hasErrors) { + global.reporter.flush() + sys.error("Compilation failure.") + } + units + } + + def withRun[T](global: Global)(f: global.Run => T): T = { + global.reporter.reset() + f(new global.Run) + } + + // compile the code, optionally first adding to the settings + def compile(args: String*) = compileString(newCompiler(args: _*))(code) /** Constructor/main body **/ try show() |