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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
import org.scalacheck._
import org.scalacheck.Prop._
import java.net.URLClassLoader
object XMLUtil {
import scala.xml._
def stripGroup(seq: Node): Node = {
seq match {
case group: Group => {
<div class="group">{ group.nodes.map(stripGroup _) }</div>
}
case e: Elem => {
val child = e.child.map(stripGroup _)
Elem(e.prefix, e.label, e.attributes, e.scope, child : _*)
}
case _ => seq
}
}
}
object Test extends Properties("HtmlFactory") {
import scala.tools.nsc.doc.{DocFactory, Settings}
import scala.tools.nsc.doc.model.IndexModelFactory
import scala.tools.nsc.doc.html.HtmlFactory
def getClasspath = {
// these things can be tricky
// this test previously relied on the assumption that the current thread's classloader is an url classloader and contains all the classpaths
// does partest actually guarantee this? to quote Leonard Nimoy: The answer, of course, is no.
// this test _will_ fail again some time in the future.
val paths = Thread.currentThread.getContextClassLoader.asInstanceOf[URLClassLoader].getURLs.map(_.getPath)
val morepaths = Thread.currentThread.getContextClassLoader.getParent.asInstanceOf[URLClassLoader].getURLs.map(_.getPath)
(paths ++ morepaths).mkString(java.io.File.pathSeparator)
}
def createFactory = {
val settings = new Settings({Console.err.println(_)})
settings.classpath.value = getClasspath
val reporter = new scala.tools.nsc.reporters.ConsoleReporter(settings)
new DocFactory(reporter, settings)
}
def createTemplates(basename: String) = {
val result = scala.collection.mutable.Map[String, scala.xml.NodeSeq]()
createFactory.makeUniverse(List("test/scaladoc/resources/"+basename)) match {
case Some(universe) => {
val index = IndexModelFactory.makeIndex(universe)
(new HtmlFactory(universe, index)).writeTemplates((page) => {
result += (page.absoluteLinkTo(page.path) -> page.body)
})
}
case _ => ;
}
result
}
def createTemplate(scala: String) = {
val html = scala.stripSuffix(".scala") + ".html"
createTemplates(scala)(html)
}
def shortComments(root: scala.xml.Node) =
XMLUtil.stripGroup(root).descendant.flatMap {
case e: scala.xml.Elem => {
if (e.attribute("class").toString.contains("shortcomment")) {
Some(e)
} else {
None
}
}
case _ => None
}
property("Trac #3790") = {
createTemplate("Trac3790.scala") match {
case node: scala.xml.Node => {
val comments = shortComments(node)
comments.exists { _.toString.contains(">A lazy String\n</p>") } &&
comments.exists { _.toString.contains(">A non-lazy String\n</p>") }
}
case _ => false
}
}
property("Trac #4306") = {
val files = createTemplates("Trac4306.scala")
files("com/example/trac4306/foo/package$$Bar.html") != None
}
property("Trac #4366") = {
createTemplate("Trac4366.scala") match {
case node: scala.xml.Node => {
shortComments(node).exists { n => {
val str = n.toString
str.contains("<code>foo</code>") && str.contains("</strong>")
} }
}
case _ => false
}
}
property("Trac #4358") = {
createTemplate("Trac4358.scala") match {
case node: scala.xml.Node =>
! shortComments(node).exists {
_.toString.contains("<em>i.</em>")
}
case _ => false
}
}
property("Trac #4180") = {
createTemplate("Trac4180.scala") != None
}
property("Trac #4372") = {
createTemplate("Trac4372.scala") match {
case node: scala.xml.Node => {
val html = node.toString
html.contains("<span class=\"name\">+:</span>\n") &&
html.contains("<span class=\"name\">-:</span>\n") &&
html.contains("""<span class="params">(<span name="n">n: <span name="scala.Int" class="extype">Int</span></span>)</span><span class="result">: <span name="scala.Int" class="extype">Int</span></span>""")
}
case _ => false
}
}
property("Trac #4374 - public") = {
val files = createTemplates("Trac4374.scala")
files("WithPublic.html") match {
case node: scala.xml.Node => {
val s = node.toString
s.contains("""go to: <a href="WithPublic$.html">companion</a>""") &&
files.get("WithPublic$.html") != None
}
case _ => false
}
}
property("Trac #4374 - private") = {
val files = createTemplates("Trac4374.scala")
files("WithPrivate.html") match {
case node: scala.xml.Node => {
val s = node.toString
! s.contains("""go to: <a href="WithPrivate$.html">companion</a>""") &&
files.get("WithPrivate$.html") == None
}
case _ => false
}
}
property("Trac #3484") = {
val files = createTemplates("Trac3484.scala")
files("Collection.html") match {
case node: scala.xml.Node => {
val s = node.toString
s.contains("""<span class="result">: Traversable[B]</span>""")
}
case _ => false
}
}
property("Trac #3484 - SR704") = {
val files = createTemplates("Trac3484.scala")
files("SR704.html") match {
case node: scala.xml.Node => {
val s = node.toString
s.contains("Hello Mister John.")
}
case _ => false
}
}
property("Trac #4325 - files") = {
val files = createTemplates("Trac4325.scala")
files.get("WithSynthetic.html") != None &&
files.get("WithSynthetic$.html") == None &&
files.get("WithObject.html") != None &&
files.get("WithObject$.html") != None
}
property("Trac #4325 - Don't link to syntetic companion") = {
val files = createTemplates("Trac4325.scala")
files("WithSynthetic.html") match {
case node: scala.xml.Node => {
val s = node.toString
! s.contains("""go to: <a href="WithSynthetic$.html">companion</a>""")
}
case _ => false
}
}
property("Trac #4325 - Link to companion") = {
val files = createTemplates("Trac4325.scala")
files("WithObject.html") match {
case node: scala.xml.Node => {
val s = node.toString
s.contains("""go to: <a href="WithObject$.html">companion</a>""")
}
case _ => false
}
}
property("Trac #484 - refinements and existentials") = {
val files = createTemplates("Trac484.scala")
val lines = """
|type Bar = AnyRef { type Dingus <: T forSome { type T <: String } }
|type Foo = AnyRef { ... /* 3 definitions in type refinement */ }
|def g (x: T forSome { type T <: String }): String
|def h (x: Float): AnyRef { def quux(x: Int,y: Int): Int }
|def hh (x: Float): AnyRef { def quux(x: Int,y: Int): Int }
|def j (x: Int): Bar
|def k (): AnyRef { type Dingus <: T forSome { type T <: String } }
""".stripMargin.trim.lines map (_.trim)
files("RefinementAndExistentials.html") match {
case node: scala.xml.Node => {
val s = node.text.replaceAll("\\s+", " ")
lines forall (s contains _)
}
case _ => false
}
}
}
|