Skip to content

Commit 1578b3e

Browse files
committed
add the forgotten patmat test
1 parent d5fc6a5 commit 1578b3e

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

tests/patmat/i947.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
./tests/patmat/i947.scala:10: warning: unreachable code
22
case ys: List[d18383] => false
3-
^
3+
^
44
one warning found

0 commit comments

Comments
 (0)