aboutsummaryrefslogtreecommitdiff
path: root/compiler/test/dotty/tools/dotc/transform/PatmatExhaustivityTest.scala
blob: 1ec4a70a56257006110d634b5003c1101260c4f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package dotty
package tools
package dotc
package transform

import java.io._

import scala.io.Source._
import scala.reflect.io.Directory
import org.junit.Test
import reporting.TestReporter
import vulpix.TestConfiguration

class PatmatExhaustivityTest {
  val testsDir = "../tests/patmat"
  // stop-after: patmatexhaust-huge.scala crash compiler
  val options = List("-color:never", "-Ystop-after:splitter", "-Ycheck-all-patmat") ++ TestConfiguration.classPath

  private def compileFile(file: File) = {
    val stringBuffer = new StringWriter()
    val reporter = TestReporter.simplifiedReporter(new PrintWriter(stringBuffer))

    try {
      Main.process((file.getPath::options).toArray, reporter, null)
    } catch {
      case e: Throwable =>
        println(s"Compile $file exception:")
        e.printStackTrace()
    }

    val actual = stringBuffer.toString.trim
    val checkFilePath = file.getAbsolutePath.stripSuffix(".scala") + ".check"
    val checkContent =
      if (new File(checkFilePath).exists)
        fromFile(checkFilePath).getLines.mkString("\n").trim
      else ""

    (file, checkContent, actual)
  }

  /** A single test with multiple files grouped in a folder */
  private def compileDir(file: File) = {
    val stringBuffer = new StringWriter()
    val reporter = TestReporter.simplifiedReporter(new PrintWriter(stringBuffer))

    val files = Directory(file.getPath).list.toList
      .filter(f => f.extension == "scala" || f.extension == "java" )
      .map(_.jfile.getPath)

    try {
      Main.process((options ++ files).toArray, reporter, null)
    } catch {
      case e: Throwable =>
        println(s"Compile $file exception:")
        e.printStackTrace()
    }

    val actual = stringBuffer.toString.trim
    val checkFilePath = file.getPath + File.separator + "expected.check"
    val checkContent =
      if (new File(checkFilePath).exists)
        fromFile(checkFilePath).getLines.mkString("\n").trim
      else ""

    (file, checkContent, actual)
  }

  @Test def patmatExhaustivity: Unit = {
    val res = Directory(testsDir).list.toList
      .filter(f => f.extension == "scala" || f.isDirectory)
      .map { f =>
        if (f.isDirectory)
          compileDir(f.jfile)
        else
          compileFile(f.jfile)
      }

    val failed = res.filter { case (_, expected, actual) => expected != actual }
    val ignored = Directory(testsDir).list.toList.filter(_.extension == "ignore")

    failed.foreach { case (file, expected, actual) =>
      println(s"\n----------------- incorrect output for $file --------------\n" +
        s"Expected:\n---------\n$expected\n\nActual:\n-------\n$actual\n"
      )
    }

    val msg = s"Total: ${res.length + ignored.length}, Failed: ${failed.length}, Ignored: ${ignored.length}"

    assert(failed.length == 0, msg)

    println(msg)
  }
}