forked from scala-ide/scala-ide
/
HyperlinkTester.scala
81 lines (69 loc) · 3.67 KB
/
HyperlinkTester.scala
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
package scala.tools.eclipse.hyperlink
import scala.tools.eclipse.testsetup.TestProjectSetup
import scala.tools.eclipse.ScalaWordFinder
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import scala.tools.eclipse.hyperlink.text.detector.ScalaDeclarationHyperlinkComputer
import scala.tools.eclipse.javaelements.ScalaSourceFile
import scala.tools.eclipse.InteractiveCompilationUnit
import org.eclipse.jface.text.IRegion
import scala.tools.eclipse.hyperlink.text.detector.DeclarationHyperlinkDetector
import scala.tools.eclipse.hyperlink.text.detector.JavaSelectionEngine
import org.eclipse.jdt.internal.core.Openable
import org.eclipse.jdt.core.IJavaElement
import org.eclipse.jdt.core.IType
import scala.language.reflectiveCalls
trait HyperlinkTester extends TestProjectSetup {
trait VerifyHyperlink {
def andCheckAgainst(expectations: List[Link], checker: (InteractiveCompilationUnit, IRegion, String, Link) => Unit = checkScalaLinks): Unit
}
private final val HyperlinkMarker = "/*^*/"
case class Link(text: String*)
/** Given a source `path`, load the corresponding scala `unit`. */
def loadTestUnit(path: String, forceTypeChecking: Boolean = false): VerifyHyperlink = {
val unit = scalaCompilationUnit(path)
loadTestUnit(unit, forceTypeChecking)
}
/** Load a scala `unit` that contains text markers used
* to generate hyperlinking requests to the presentation compiler.
*/
def loadTestUnit(unit: ScalaSourceFile, forceTypeChecking: Boolean): VerifyHyperlink = {
reload(unit)
if(forceTypeChecking) waitUntilTypechecked(unit)
new VerifyHyperlink {
/** @param expectations A collection of expected `Link` (test's oracle). */
def andCheckAgainst(expectations: List[Link], checker: (InteractiveCompilationUnit, IRegion, String, Link) => Unit = checkScalaLinks) = {
val positions = findMarker(HyperlinkMarker).in(unit)
println("checking %d positions".format(positions.size))
assertEquals(positions.size, expectations.size)
for ((pos, oracle) <- positions.zip(expectations)) {
val wordRegion = ScalaWordFinder.findWord(unit.getContents, pos)
val word = new String(unit.getContents.slice(wordRegion.getOffset, wordRegion.getOffset + wordRegion.getLength))
println("hyperlinking at position %d (%s)".format(pos, word))
checker(unit, wordRegion, word, oracle)
}
}
}
}
def checkScalaLinks(unit: InteractiveCompilationUnit, wordRegion: IRegion, word: String, oracle: Link) {
val resolver = new ScalaDeclarationHyperlinkComputer
val maybeLinks = resolver.findHyperlinks(unit, wordRegion)
// Verify Expectations
assertTrue("no links found for `%s`".format(word), maybeLinks.isDefined)
val links = maybeLinks.get
assertEquals("expected %d link, found %d".format(oracle.text.size, links.size), oracle.text.size, links.size)
val linkResults = links map (_.getTypeLabel)
assertEquals("text", oracle.text.toList.toString, linkResults.toList.toString)
}
def checkJavaElements(unit: InteractiveCompilationUnit, wordRegion: IRegion, word: String, oracle: Link) {
val elements = JavaSelectionEngine.getJavaElements(unit, unit.asInstanceOf[Openable], wordRegion)
// Verify Expectations
assertTrue("no links found for `%s`".format(word), elements.nonEmpty)
assertEquals("expected %d link, found %d".format(oracle.text.size, elements.size), oracle.text.size, elements.size)
val linkResults = for {
e <- elements
tpe = e.getAncestor(IJavaElement.TYPE).asInstanceOf[IType]
} yield tpe.getFullyQualifiedName() + "." + e.getElementName()
assertEquals("text", oracle.text.toList.toString, linkResults.toList.toString)
}
}