|
| 1 | +package test.transform |
| 2 | + |
| 3 | +import java.io._ |
| 4 | + |
| 5 | +import scala.io.Source._ |
| 6 | +import scala.reflect.io.Directory |
| 7 | +import org.junit.Test |
| 8 | +import dotty.tools.dotc.Main |
| 9 | +import dotty.tools.dotc.reporting.ClassicReporter |
| 10 | + |
| 11 | +class PatmatExhaustivityTest { |
| 12 | + val testsDir = "./tests/patmat" |
| 13 | + // stop-after: patmatexhaust-huge.scala crash compiler |
| 14 | + val options = List("-color:never", "-Ystop-after:splitter", "-Ycheck-all-patmat") |
| 15 | + |
| 16 | + private def compileFile(file: File) = { |
| 17 | + val stringBuffer = new StringWriter() |
| 18 | + val reporter = new ClassicReporter(writer = new PrintWriter(stringBuffer)) |
| 19 | + |
| 20 | + try { |
| 21 | + Main.process((file.getPath::options).toArray, reporter, null) |
| 22 | + } catch { |
| 23 | + case e: Throwable => |
| 24 | + println(s"Compile $file exception:") |
| 25 | + e.printStackTrace() |
| 26 | + } |
| 27 | + |
| 28 | + val actual = stringBuffer.toString.trim |
| 29 | + val checkFilePath = file.getAbsolutePath.stripSuffix(".scala") + ".check" |
| 30 | + val checkContent = |
| 31 | + if (new File(checkFilePath).exists) |
| 32 | + fromFile(checkFilePath).getLines.mkString("\n").trim |
| 33 | + else "" |
| 34 | + |
| 35 | + (file, checkContent, actual) |
| 36 | + } |
| 37 | + |
| 38 | + /** A single test with multiple files grouped in a folder */ |
| 39 | + private def compileDir(file: File) = { |
| 40 | + val stringBuffer = new StringWriter() |
| 41 | + val reporter = new ClassicReporter(writer = new PrintWriter(stringBuffer)) |
| 42 | + |
| 43 | + val files = Directory(file.getPath).list.toList |
| 44 | + .filter(f => f.extension == "scala" || f.extension == "java" ) |
| 45 | + .map(_.jfile.getPath) |
| 46 | + |
| 47 | + try { |
| 48 | + Main.process((options ++ files).toArray, reporter, null) |
| 49 | + } catch { |
| 50 | + case e: Throwable => |
| 51 | + println(s"Compile $file exception:") |
| 52 | + e.printStackTrace() |
| 53 | + } |
| 54 | + |
| 55 | + val actual = stringBuffer.toString.trim |
| 56 | + val checkFilePath = file.getPath + File.separator + "expected.check" |
| 57 | + val checkContent = |
| 58 | + if (new File(checkFilePath).exists) |
| 59 | + fromFile(checkFilePath).getLines.mkString("\n").trim |
| 60 | + else "" |
| 61 | + |
| 62 | + (file, checkContent, actual) |
| 63 | + } |
| 64 | + |
| 65 | + @Test def patmatExhaustivity: Unit = { |
| 66 | + val res = Directory(testsDir).list.toList |
| 67 | + .filter(f => f.extension == "scala" || f.isDirectory) |
| 68 | + .map { f => |
| 69 | + if (f.isDirectory) |
| 70 | + compileDir(f.jfile) |
| 71 | + else |
| 72 | + compileFile(f.jfile) |
| 73 | + } |
| 74 | + |
| 75 | + val failed = res.filter { case (_, expected, actual) => expected != actual } |
| 76 | + val ignored = Directory(testsDir).list.toList.filter(_.extension == "ignore") |
| 77 | + |
| 78 | + failed.foreach { case (file, expected, actual) => |
| 79 | + println(s"\n----------------- incorrect output for $file --------------\n" + |
| 80 | + s"Expected:\n-------\n$expected\n\nActual\n----------\n$actual\n" |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + val msg = s"Total: ${res.length + ignored.length}, Failed: ${failed.length}, Ignored: ${ignored.length}" |
| 85 | + |
| 86 | + assert(failed.length == 0, msg) |
| 87 | + |
| 88 | + println(msg) |
| 89 | + } |
| 90 | +} |
0 commit comments