diff options
author | Iulian Dragos <jaguarul@gmail.com> | 2011-01-24 19:17:32 +0000 |
---|---|---|
committer | Iulian Dragos <jaguarul@gmail.com> | 2011-01-24 19:17:32 +0000 |
commit | f253b67d4a50a066fb91ce03fa1eb12db9a9c1e0 (patch) | |
tree | 17921974620182355dab935e09f864a24cd5789d /src | |
parent | e07ca49a24d15ab1b279203ad208153a44732550 (diff) | |
download | scala-f253b67d4a50a066fb91ce03fa1eb12db9a9c1e0.tar.gz scala-f253b67d4a50a066fb91ce03fa1eb12db9a9c1e0.tar.bz2 scala-f253b67d4a50a066fb91ce03fa1eb12db9a9c1e0.zip |
Added presentation compiler tests.
Diffstat (limited to 'src')
7 files changed, 216 insertions, 14 deletions
diff --git a/src/compiler/scala/tools/nsc/interactive/tests/InteractiveTest.scala b/src/compiler/scala/tools/nsc/interactive/tests/InteractiveTest.scala new file mode 100644 index 0000000000..325151b528 --- /dev/null +++ b/src/compiler/scala/tools/nsc/interactive/tests/InteractiveTest.scala @@ -0,0 +1,175 @@ +package scala.tools.nsc.interactive +package tests + +import scala.tools.nsc.Settings +import scala.tools.nsc.reporters.StoreReporter +import scala.tools.nsc.util.{BatchSourceFile, SourceFile, Position} +import scala.tools.nsc.io._ + +import scala.collection.{immutable, mutable} + +/** A base class for writing interactive compiler tests. + * + * This class tries to cover common functionality needed when testing the presentation + * compiler: instantiation source files, reloading, creating positions, instantiating + * the presentation compiler, random stress testing. + * + * By default, this class loads all classes found under `src/`. They are found in + * `sourceFiles`. Positions can be created using `pos(file, line, col)`. The presentation + * compiler is available through `compiler`. + * + * It is easy to test member completion and type at a given position. Source + * files are searched for /markers/. By default, the completion marker is `/*!*/` and the + * typedAt marker is `/*?*/`. Place these markers in your source files, and call `completionTests` + * and `typedAtTests` to print the results at all these positions. Sources are reloaded by `reloadSources` + * (blocking call). All ask operations are placed on the work queue without waiting for each one to + * complete before asking the next. After all asks, it waits for each response in turn and prints the result. + * The default timout is 5 seconds per operation. + * + * The same mechanism can be used for custom operations. Use `askAllSources(marker)(f)(g)`. Give your custom + * marker, and provide the two functions: one for creating the request, and the second for processing the + * response, if it didn't time out and there was no error. + * + * @see Check existing tests under test/files/presentation + * + * @author Iulian Dragos + */ +abstract class InteractiveTest { + + val completionMarker = "/*!*/" + val typedAtMarker = "/*?*/" + val TIMEOUT = 5000 // 5 seconds + + val settings = new Settings + val reporter= new StoreReporter + + // need this so that the classpath comes from what partest + // instead of scala.home + settings.usejavacp.value = true + + /** The root directory for this test suite, usually the test kind ("test/files/presentation"). */ + val outDir = Path(Option(System.getProperty("partest.cwd")).getOrElse(".")) + + /** The base directory for this test, usually a subdirectory of "test/files/presentation/" */ + val baseDir = Option(System.getProperty("partest.testname")).map(outDir / _).getOrElse(Path(".")) + +// settings.YpresentationDebug.value = true + lazy val compiler = new Global(settings, reporter) + + def sources(filename: String*): Seq[SourceFile] = + for (f <- filename) yield + source(if (f.startsWith("/")) Path(f) else baseDir / f) + + def source(file: Path) = new BatchSourceFile(AbstractFile.getFile(file.toFile)) + def source(filename: String): SourceFile = new BatchSourceFile(AbstractFile.getFile(filename)) + + def pos(file: SourceFile, line: Int, col: Int): Position = + file.position(line, col) + + def filesInDir(dir: Path): Iterator[Path] = { + dir.toDirectory.list.filter(_.isFile) + } + + /** Where source files are placed. */ + val sourceDir = "src" + + /** All .scala files below "src" directory. */ + lazy val sourceFiles: Array[SourceFile] = + filesInDir(baseDir / sourceDir).filter(_.extension == "scala").map(source).toArray + + /** All positions of the given string in all source files. */ + def allPositionsOf(sources: Seq[SourceFile] = sourceFiles, str: String): immutable.Map[SourceFile, Seq[Position]] = { + (for (s <- sources; p <- positionsOf(s, str)) yield p).groupBy(_.source) + } + + /** Return all positions of the given str in the given source file. */ + def positionsOf(source: SourceFile, str: String): Seq[Position] = { + val buf = new mutable.ListBuffer[Position] + var pos = source.content.indexOfSlice(str) + while (pos >= 0) { +// buf += compiler.rangePos(source, pos - 1, pos - 1, pos - 1) + buf += source.position(pos - 1) // we need the position before the first character of this marker + pos = source.content.indexOfSlice(str, pos + 1) + } + buf.toList + } + + /** Perform an operation on all sources at all positions that match the given + * marker string. For instance, askAllSources("/*!*/")(askTypeAt)(println) would + * ask the tyep at all positions marked with /*!*/ and println the result. + */ + def askAllSources[T](marker: String)(askAt: Position => Response[T])(f: (Position, T) => Unit) { + val positions = allPositionsOf(str = marker).valuesIterator.toList.flatten + val responses = for (pos <- positions) yield askAt(pos) + + for ((pos, r) <- positions zip responses) r.get(TIMEOUT) match { + case Some(Left(members)) => + f(pos, members) + case None => + println("TIMEOUT: " + r) + case _ => + println("ERROR: " + r) + } + } + + /** Ask completion for all marked positions in all sources. + * A completion position is marked with /*!*/. + */ + def completionTests() { + askAllSources(completionMarker) { pos => + println("askTypeCompletion at " + pos) + val r = new Response[List[compiler.Member]] + compiler.askTypeCompletion(pos, r) + r + } { (pos, members) => + println("\n" + "=" * 80) + println("[response] aksTypeCompletion at " + (pos.line, pos.column)) + println(members.sortBy(_.sym.name.toString).mkString("\n")) + } + } + + /** Ask for typedAt for all marker positions in all sources. + */ + def typeAtTests() { + askAllSources(typedAtMarker) { pos => + println("askTypeAt at " + pos) + val r = new Response[compiler.Tree] + compiler.askTypeAt(pos, r) + r + } { (pos, tree) => + println("[response] askTypeAt at " + (pos.line, pos.column)) + println(tree) + } + } + + /** Reload the given source files and wait for them to be reloaded. */ + def reloadSources(sources: Seq[SourceFile] = sourceFiles) { +// println("basedir: " + baseDir.path) +// println("sourcedir: " + (baseDir / sourceDir).path) + println("reload: " + sourceFiles.mkString("", ", ", "")) + val reload = new Response[Unit] + compiler.askReload(sourceFiles.toList, reload) + reload.get + } + + def runTest: Unit = { + if (runRandomTests) randomTests(20, sourceFiles) + completionTests() + typeAtTests() + } + + /** Perform n random tests with random changes. */ + def randomTests(n: Int, files: Array[SourceFile]) { + val tester = new Tester(n, files, settings) + tester.run() + } + + val runRandomTests = true + + def main(args: Array[String]) { + reloadSources() + runTest + compiler.askShutdown() + } +} + diff --git a/src/compiler/scala/tools/nsc/interactive/tests/Tester.scala b/src/compiler/scala/tools/nsc/interactive/tests/Tester.scala index 36a715ba26..a7e1f74aaa 100644 --- a/src/compiler/scala/tools/nsc/interactive/tests/Tester.scala +++ b/src/compiler/scala/tools/nsc/interactive/tests/Tester.scala @@ -153,7 +153,7 @@ class Tester(ntests: Int, inputs: Array[SourceFile], settings: Settings) { changes foreach (_.deleteAll()) otherTest() def errorCount() = compiler.ask(() => reporter.ERROR.count) - println("\nhalf test round: "+errorCount()) +// println("\nhalf test round: "+errorCount()) changes.view.reverse foreach (_.insertAll()) otherTest() println("done test round: "+errorCount()) diff --git a/src/partest/scala/tools/partest/PartestTask.scala b/src/partest/scala/tools/partest/PartestTask.scala index e49c5a5c6e..551500e626 100644 --- a/src/partest/scala/tools/partest/PartestTask.scala +++ b/src/partest/scala/tools/partest/PartestTask.scala @@ -72,6 +72,11 @@ class PartestTask extends Task with CompilationPathProperty { specializedFiles = Some(input) } + def addConfiguredPresentationTests(input: FileSet) { + presentationFiles = Some(input) + } + + def setSrcDir(input: String) { srcDir = Some(input) } @@ -146,6 +151,7 @@ class PartestTask extends Task with CompilationPathProperty { private var shootoutFiles: Option[FileSet] = None private var scalapFiles: Option[FileSet] = None private var specializedFiles: Option[FileSet] = None + private var presentationFiles: Option[FileSet] = None private var errorOnFailed: Boolean = false private var scalacOpts: Option[String] = None private var timeout: Option[String] = None @@ -170,14 +176,25 @@ class PartestTask extends Task with CompilationPathProperty { // println("----> " + fileSet) val fileTests = getFiles(Some(fs)) filterNot (x => shouldExclude(x.getName)) - val dirTests: Iterator[SPath] = fileSetToDir(fs).dirs filterNot (x => shouldExclude(x.name)) - val dirResult = dirTests.toList.toArray map (_.jfile) + val dirResult = getDirs(Some(fs)) filterNot (x => shouldExclude(x.getName)) // println("dirs: " + dirResult.toList) // println("files: " + fileTests.toList) dirResult ++ fileTests } + private def getDirs(fileSet: Option[FileSet]): Array[File] = fileSet match { + case None => Array() + case Some(fs) => + def shouldExclude(name: String) = (name endsWith ".obj") || (name startsWith ".") + + val dirTests: Iterator[SPath] = fileSetToDir(fs).dirs filterNot (x => shouldExclude(x.name)) + val dirResult = dirTests.toList.toArray map (_.jfile) + + dirResult + } + + private def getPosFiles = getFilesAndDirs(posFiles) private def getNegFiles = getFilesAndDirs(negFiles) private def getRunFiles = getFilesAndDirs(runFiles) @@ -189,6 +206,7 @@ class PartestTask extends Task with CompilationPathProperty { private def getShootoutFiles = getFiles(shootoutFiles) private def getScalapFiles = getFiles(scalapFiles) private def getSpecializedFiles = getFiles(specializedFiles) + private def getPresentationFiles = getDirs(presentationFiles) override def execute() { if (isPartestDebug || debug) { @@ -236,7 +254,8 @@ class PartestTask extends Task with CompilationPathProperty { (getScriptFiles, "script", "Running script files"), (getShootoutFiles, "shootout", "Running shootout tests"), (getScalapFiles, "scalap", "Running scalap tests"), - (getSpecializedFiles, "specialized", "Running specialized files") + (getSpecializedFiles, "specialized", "Running specialized files"), + (getPresentationFiles, "presentation", "Running presentation compiler test files") ) def runSet(set: TFSet): (Int, Int, Iterable[String]) = { diff --git a/src/partest/scala/tools/partest/nest/CompileManager.scala b/src/partest/scala/tools/partest/nest/CompileManager.scala index b9067a95ac..42e4d02934 100644 --- a/src/partest/scala/tools/partest/nest/CompileManager.scala +++ b/src/partest/scala/tools/partest/nest/CompileManager.scala @@ -84,14 +84,15 @@ class DirectCompiler(val fileManager: FileManager) extends SimpleCompiler { val testRep: ExtConsoleReporter = global.reporter.asInstanceOf[ExtConsoleReporter] val testFileFn: (File, FileManager) => TestFile = kind match { - case "pos" => PosTestFile.apply - case "neg" => NegTestFile.apply - case "run" => RunTestFile.apply - case "jvm" => JvmTestFile.apply - case "shootout" => ShootoutTestFile.apply - case "scalap" => ScalapTestFile.apply - case "scalacheck" => ScalaCheckTestFile.apply - case "specialized" => SpecializedTestFile.apply + case "pos" => PosTestFile.apply + case "neg" => NegTestFile.apply + case "run" => RunTestFile.apply + case "jvm" => JvmTestFile.apply + case "shootout" => ShootoutTestFile.apply + case "scalap" => ScalapTestFile.apply + case "scalacheck" => ScalaCheckTestFile.apply + case "specialized" => SpecializedTestFile.apply + case "presentation" => PresentationTestFile.apply } val test: TestFile = testFileFn(files.head, fileManager) test.defineSettings(command.settings, out.isEmpty) diff --git a/src/partest/scala/tools/partest/nest/ConsoleRunner.scala b/src/partest/scala/tools/partest/nest/ConsoleRunner.scala index b99f3d37bd..b665974ec8 100644 --- a/src/partest/scala/tools/partest/nest/ConsoleRunner.scala +++ b/src/partest/scala/tools/partest/nest/ConsoleRunner.scala @@ -36,7 +36,8 @@ class ConsoleRunner extends DirectRunner { TestSet("script", pathFilter, "Testing script tests"), TestSet("scalacheck", x => pathFilter(x) || x.isDirectory, "Testing ScalaCheck tests"), TestSet("scalap", _.isDirectory, "Run scalap decompiler tests"), - TestSet("specialized", pathFilter, "Testing specialized tests") + TestSet("specialized", pathFilter, "Testing specialized tests"), + TestSet("presentation", _.isDirectory, "Testing presentation compiler tests.") ) } @@ -57,7 +58,7 @@ class ConsoleRunner extends DirectRunner { private val unaryArgs = List( "--pack", "--all", "--verbose", "--show-diff", "--show-log", - "--failed", "--update-check", "--version", "--ansi", "--debug" + "--failed", "--update-check", "--version", "--ansi", "--debug", "--help" ) ::: testSetArgs private val binaryArgs = List( @@ -71,6 +72,7 @@ class ConsoleRunner extends DirectRunner { /** Early return on no args, version, or invalid args */ if (argstr == "") return NestUI.usage() if (parsed isSet "--version") return printVersion + if (parsed isSet "--help") return NestUI.usage() if (args exists (x => !denotesTestPath(x))) { val invalid = (args filterNot denotesTestPath).head NestUI.failure("Invalid argument '%s'\n" format invalid) diff --git a/src/partest/scala/tools/partest/nest/TestFile.scala b/src/partest/scala/tools/partest/nest/TestFile.scala index 636c29a050..9bfb4a992a 100644 --- a/src/partest/scala/tools/partest/nest/TestFile.scala +++ b/src/partest/scala/tools/partest/nest/TestFile.scala @@ -55,3 +55,4 @@ case class SpecializedTestFile(file: JFile, fileManager: FileManager) extends Te settings.classpath.value = ClassPath.join(PathSettings.srcSpecLib.toString, settings.classpath.value) } } +case class PresentationTestFile(file: JFile, fileManager: FileManager) extends TestFile("presentation") diff --git a/src/partest/scala/tools/partest/nest/Worker.scala b/src/partest/scala/tools/partest/nest/Worker.scala index 71d446f4ef..2278cb4def 100644 --- a/src/partest/scala/tools/partest/nest/Worker.scala +++ b/src/partest/scala/tools/partest/nest/Worker.scala @@ -315,6 +315,7 @@ class Worker(val fileManager: FileManager, params: TestRunParams) extends Actor "-Dpartest.output="+outDir.getAbsolutePath, "-Dpartest.lib="+LATEST_LIB, "-Dpartest.cwd="+outDir.getParent, + "-Dpartest.testname="+fileBase, "-Djavacmd="+JAVACMD, "-Djavaccmd="+javacCmd, "-Duser.language=en -Duser.country=US" @@ -593,6 +594,9 @@ class Worker(val fileManager: FileManager, params: TestRunParams) extends Actor case "specialized" => runSpecializedTest(file) + case "presentation" => + runJvmTest(file) // for the moment, it's exactly the same as for a run test + case "buildmanager" => val logFile = createLogFile(file) if (!fileManager.failed || logFile.canRead) { |