File: IdempotencyTest.scala

package info (click to toggle)
scala 2.11.12-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 62,924 kB
  • sloc: javascript: 28,808; java: 13,415; xml: 3,135; sh: 1,620; python: 756; makefile: 38; awk: 36; ansic: 6
file content (73 lines) | stat: -rw-r--r-- 2,275 bytes parent folder | download | duplicates (4)
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
package scala.tools.nsc
package interactive
package tests.core

import reporters.{Reporter => CompilerReporter}
import scala.tools.nsc.interactive.InteractiveReporter
import scala.reflect.internal.util.SourceFile

/** Deterministically interrupts typechecking of `code` when a definition named
  * `MagicInterruptionMarker` is typechecked, and then performs a targeted
  * typecheck of the tree at the special comment marker marker
  */  
abstract class IdempotencyTest { self =>
  private val settings = new Settings
  settings.usejavacp.value = true

  private object Break extends scala.util.control.ControlThrowable

  private val compilerReporter: CompilerReporter = new InteractiveReporter {
    override def compiler = self.compiler
  }

  object compiler extends Global(settings, compilerReporter) {
    override def checkForMoreWork(pos: Position) {
    }
    override def signalDone(context: Context, old: Tree, result: Tree) {
      // println("signalDone: " + old.toString.take(50).replaceAll("\n", "\\n"))
      if (!interrupted && analyzer.lockedCount == 0 && interruptsEnabled && shouldInterrupt(result)) {
        interrupted = true
        val typed = typedTreeAt(markerPosition)
        checkTypedTree(typed)
        throw Break
      }
      super.signalDone(context, old, result)
    }

    // we're driving manually using our own thread, disable the check here.
    override def assertCorrectThread() {}
  }

  import compiler._

  private var interrupted = false

  // Extension points
  protected def code: String
  protected def shouldInterrupt(tree: Tree): Boolean = {
    tree.symbol != null && tree.symbol.name.toString == "MagicInterruptionMarker"
  }
  protected def checkTypedTree(tree: Tree): Unit = {}
  

  private val source: SourceFile = newSourceFile(code)
  private def markerPosition: Position = source.position(code.indexOf("/*?*/"))

  def assertNoProblems() {
    val problems = getUnit(source).get.problems
    assert(problems.isEmpty, problems.mkString("\n"))
  }

  def show() {
    reloadSource(source)
    try {
      typedTree(source, true)
      assert(false, "Expected to break out of typechecking.")
    } catch {
      case Break => // expected
    }
    assertNoProblems()
  }

  def main(args: Array[String]) { show() }
}