summaryrefslogtreecommitdiff
path: root/src/partest-extras/scala/tools/partest/StubErrorMessageTest.scala
blob: f713b79e75535296e3a167c87936af214953bf86 (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
package scala.tools.partest

trait StubErrorMessageTest extends StoreReporterDirectTest {
  // Stub to feed to partest, unused
  def code = throw new Error("Use `userCode` instead of `code`.")

  val classpath = List(sys.props("partest.lib"), testOutput.path)
    .mkString(sys.props("path.separator"))

  def compileCode(codes: String*) = {
    val global = newCompiler("-cp", classpath, "-d", testOutput.path)
    val sourceFiles = newSources(codes: _*)
    withRun(global)(_ compileSources sourceFiles)
  }

  def removeClasses(inPackage: String, classNames: Seq[String]): Unit = {
    val pkg = new File(testOutput.path, inPackage)
    classNames.foreach { className =>
      val classFile = new File(pkg, s"$className.class")
      assert(classFile.exists)
      assert(classFile.delete())
    }
  }

  def removeFromClasspath(): Unit
  def codeA: String
  def codeB: String
  def userCode: String
  def extraUserCode: String = ""

  def show(): Unit = {
    compileCode(codeA)
    assert(filteredInfos.isEmpty, filteredInfos)

    compileCode(codeB)
    assert(filteredInfos.isEmpty, filteredInfos)
    removeFromClasspath()

    if (extraUserCode == "") compileCode(userCode)
    else compileCode(userCode, extraUserCode)
    import scala.reflect.internal.util.Position
    filteredInfos.map { report =>
      print(if (report.severity == storeReporter.ERROR) "error: " else "")
      println(Position.formatMessage(report.pos, report.msg, true))
    }
  }
}