summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/interactive/tests/InteractiveTest.scala
blob: cb46c0fdca37f18ac2be1756d2ebda631db56f71 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* NSC -- new Scala compiler
 * Copyright 2009-2012 Scala Solutions and LAMP/EPFL
 * @author Martin Odersky
 */
package scala.tools.nsc
package interactive
package tests

import core._

import java.io.File.pathSeparatorChar
import java.io.File.separatorChar

import scala.annotation.migration
import scala.reflect.internal.util.Position
import scala.reflect.internal.util.SourceFile

import scala.collection.mutable.ListBuffer

/** A base class for writing interactive compiler tests.
 *
 *  This class tries to cover common functionality needed when testing the presentation
 *  compiler: instantiation source files, reloading, creating positions, instantiating
 *  the presentation compiler, random stress testing.
 *
 *  By default, this class loads all scala and java classes found under `src/`, going
 *  recursively into subfolders. Loaded classes are found in `sourceFiles`. trait `TestResources`
 *  The presentation compiler is available through `compiler`.
 *
 *  It is easy to test member completion, type and hyperlinking at a given position. Source
 *  files are searched for `TextMarkers`. By default, the completion marker is `/*!*/`, the
 *  typedAt marker is `/*?*/` and the hyperlinking marker is `/*#*/`. Place these markers in
 *  your source files, and the test framework will automatically pick them up and test the
 *  corresponding actions. Sources are reloaded by `askReload(sourceFiles)` (blocking
 *  call). All ask operations are placed on the work queue without waiting for each one to
 *  complete before asking the next. After all asks, it waits for each response in turn and
 *  prints the result. The default timeout is 1 second per operation.
 *
 *  To define a custom operation you have to:
 *
 *  	(1) Define a new marker by extending `TestMarker`
 *  	(2) Provide an implementation for the operation you want to check by extending `PresentationCompilerTestDef`
 *  	(3) Add the class defined in (1) to the set of executed test actions by calling `++` on `InteractiveTest`.
 *
 *  Then you can simply use the new defined `marker` in your test sources and the testing
 *  framework will automatically pick it up.
 *
 *  @see   Check existing tests under test/files/presentation
 *
 *  @author Iulian Dragos
 *  @author Mirco Dotta
 */
abstract class InteractiveTest
  extends AskParse
  with AskShutdown
  with AskReload
  with AskLoadedTyped
  with AskType
  with PresentationCompilerInstance
  with CoreTestDefs
  with InteractiveTestSettings { self =>

  protected val runRandomTests = false

  /** Should askAllSources wait for each ask to finish before issuing the next? */
  override protected val synchronousRequests = true

  /** The core set of test actions that are executed during each test run are
   *  `CompletionAction`, `TypeAction` and `HyperlinkAction`.
   *  Override this member if you need to change the default set of executed test actions.
   */
  protected lazy val testActions: ListBuffer[PresentationCompilerTestDef] = {
    ListBuffer(new CompletionAction(compiler), new TypeAction(compiler), new HyperlinkAction(compiler))
  }

  /** Add new presentation compiler actions to test. Presentation compiler's test
   *  need to extends trait `PresentationCompilerTestDef`.
   */
  protected def ++(tests: PresentationCompilerTestDef*) {
    testActions ++= tests
  }

  /** Test's entry point */
  def main(args: Array[String]) {
    try execute()
    finally shutdown()
  }

  protected def execute(): Unit = {
    loadSources()
    runDefaultTests()
  }

  /** Load all sources before executing the test. */
  protected def loadSources() {
    // ask the presentation compiler to track all sources. We do
    // not wait for the file to be entirely typed because we do want
    // to exercise the presentation compiler on scoped type requests.
    askReload(sourceFiles)
    // make sure all sources are parsed before running the test. This
    // is because test may depend on the sources having been parsed at
    // least once
    askParse(sourceFiles)
  }

  /** Run all defined `PresentationCompilerTestDef` */
  protected def runDefaultTests() {
    //TODO: integrate random tests!, i.e.: if (runRandomTests) randomTests(20, sourceFiles)
    testActions.foreach(_.runTest())
  }

  /** Perform n random tests with random changes. */
  private def randomTests(n: Int, files: Array[SourceFile]) {
    val tester = new Tester(n, files, settings) {
      override val compiler = self.compiler
      override val reporter = new reporters.StoreReporter
    }
    tester.run()
  }

  /** shutdown the presentation compiler. */
  protected def shutdown() {
    askShutdown()

    // this is actually needed to force exit on test completion.
    // Note: May be a bug on either the testing framework or (less likely)
    //           the presentation compiler
    sys.exit(0)
  }
}