|
| 1 | +package dotty |
| 2 | +package tools |
| 3 | +package dotc |
| 4 | + |
| 5 | +import vulpix.TestConfiguration |
| 6 | +import vulpix.TestConfiguration |
| 7 | +import reporting.TestReporter |
| 8 | + |
| 9 | +import java.io._ |
| 10 | +import java.nio.file.{Path => JPath} |
| 11 | +import java.lang.System.{lineSeparator => EOL} |
| 12 | + |
| 13 | +import interfaces.Diagnostic.INFO |
| 14 | +import dotty.tools.io.Directory |
| 15 | + |
| 16 | +import scala.io.Source |
| 17 | +import org.junit.Test |
| 18 | + |
| 19 | +class PrintingTest { |
| 20 | + val testsDir = "tests/printing" |
| 21 | + val options = List("-Xprint:frontend", "-color:never", "-classpath", TestConfiguration.basicClasspath) |
| 22 | + |
| 23 | + private def fileContent(filePath: String): List[String] = |
| 24 | + if (new File(filePath).exists) |
| 25 | + Source.fromFile(filePath, "UTF-8").getLines().toList |
| 26 | + else Nil |
| 27 | + |
| 28 | + |
| 29 | + private def compileFile(path: JPath): Boolean = { |
| 30 | + val baseFilePath = path.toAbsolutePath.toString.stripSuffix(".scala") |
| 31 | + val outFilePath = baseFilePath + ".out" |
| 32 | + val checkFilePath = baseFilePath + ".check" |
| 33 | + val ps = new PrintStream(new File(outFilePath)) |
| 34 | + val reporter = TestReporter.reporter(ps, INFO) |
| 35 | + |
| 36 | + try { |
| 37 | + Main.process((path.toString::options).toArray, reporter, null) |
| 38 | + } catch { |
| 39 | + case e: Throwable => |
| 40 | + println(s"Compile $path exception:") |
| 41 | + e.printStackTrace() |
| 42 | + } |
| 43 | + finally ps.close() |
| 44 | + |
| 45 | + val actual = fileContent(outFilePath) |
| 46 | + val expected = fileContent(checkFilePath) |
| 47 | + |
| 48 | + val success = |
| 49 | + actual.length == expected.length && |
| 50 | + (actual, expected).zipped.forall(_ == _) |
| 51 | + |
| 52 | + success || { |
| 53 | + println( |
| 54 | + s"""|Output from '$outFilePath' did not match check file. Actual output: |
| 55 | + |${actual.mkString(EOL)} |
| 56 | + |""".stripMargin + "\n") |
| 57 | + |
| 58 | + println( |
| 59 | + s"""Test output dumped in: ${outFilePath} |
| 60 | + | See diff of the checkfile |
| 61 | + | > diff $checkFilePath $outFilePath |
| 62 | + | Replace checkfile with current output output |
| 63 | + | > mv $outFilePath $checkFilePath |
| 64 | + """.stripMargin) |
| 65 | + |
| 66 | + false |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + def printing: Unit = { |
| 72 | + val res = Directory(testsDir).list.toList |
| 73 | + .filter(f => f.extension == "scala") |
| 74 | + .map { f => compileFile(f.jpath) } |
| 75 | + |
| 76 | + val failed = res.filter(!_) |
| 77 | + |
| 78 | + val msg = s"Pass: ${res.length - failed.length}, Failed: ${failed.length}" |
| 79 | + |
| 80 | + assert(failed.length == 0, msg) |
| 81 | + |
| 82 | + println(msg) |
| 83 | + } |
| 84 | +} |
0 commit comments