From 4d0cd60b0e2b9071bba2641da95c75332458193b Mon Sep 17 00:00:00 2001 From: Kato Kazuyoshi Date: Mon, 21 Mar 2011 12:24:48 +0000 Subject: [scaladoc] Add HtmlFactoryTest and reorganize d... [scaladoc] Add HtmlFactoryTest and reorganize directory structure. Reviewed by pedrofurla. --- test/scaladoc/IndexTest.scala | 102 --------------------- test/scaladoc/model/CommentFactoryTest.scala | 76 --------------- test/scaladoc/resources/Trac3790.scala | 11 +++ test/scaladoc/scala/IndexTest.scala | 102 +++++++++++++++++++++ test/scaladoc/scala/html/HtmlFactoryTest.scala | 97 ++++++++++++++++++++ test/scaladoc/scala/model/CommentFactoryTest.scala | 76 +++++++++++++++ 6 files changed, 286 insertions(+), 178 deletions(-) delete mode 100644 test/scaladoc/IndexTest.scala delete mode 100644 test/scaladoc/model/CommentFactoryTest.scala create mode 100644 test/scaladoc/resources/Trac3790.scala create mode 100644 test/scaladoc/scala/IndexTest.scala create mode 100644 test/scaladoc/scala/html/HtmlFactoryTest.scala create mode 100644 test/scaladoc/scala/model/CommentFactoryTest.scala (limited to 'test') diff --git a/test/scaladoc/IndexTest.scala b/test/scaladoc/IndexTest.scala deleted file mode 100644 index 5e3d02e045..0000000000 --- a/test/scaladoc/IndexTest.scala +++ /dev/null @@ -1,102 +0,0 @@ -import org.scalacheck._ -import org.scalacheck.Prop._ - -import scala.tools.nsc.doc -import scala.tools.nsc.doc.html.page.Index -import java.net.URLClassLoader - -object Test extends Properties("Index") { - - 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) - } - - val docFactory = { - val settings = new doc.Settings({Console.err.println(_)}) - - settings.classpath.value = getClasspath - println(settings.classpath.value) - - val reporter = new scala.tools.nsc.reporters.ConsoleReporter(settings) - - new doc.DocFactory(reporter, settings) - } - - val indexModelFactory = doc.model.IndexModelFactory - - def createIndex(path: String): Option[Index] = { - - val maybeUniverse = { - //val stream = new java.io.ByteArrayOutputStream - //val original = Console.out - //Console.setOut(stream) - - val result = docFactory.makeUniverse(List(path)) - - // assert(stream.toString == "model contains 2 documentable templates\n") - //Console.setOut(original) - - result - } - - maybeUniverse match { - case Some(universe) => { - val index = new Index(universe, indexModelFactory.makeIndex(universe)) - return Some(index) - } - case _ => return None - } - - } - - property("path") = { - createIndex("src/compiler/scala/tools/nsc/doc/html/page/Index.scala") match { - case Some(index) => - index.path == List("index.html") - case None => false - } - } - - property("title") = { - createIndex("src/compiler/scala/tools/nsc/doc/html/page/Index.scala") match { - case Some(index) => - index.title == "" - - case None => false - } - } - property("browser contants a script element") = { - createIndex("src/compiler/scala/tools/nsc/doc/html/page/Index.scala") match { - case Some(index) => - (index.browser \ "script").size == 1 - - case None => false - } - } - - property("allPackages") = { - createIndex("src/compiler/scala/tools/nsc/doc/html/page/Index.scala") match { - - case Some(index) => - index.allPackages.map(_.toString) == List( - "scala", - "scala.tools", - "scala.tools.nsc", - "scala.tools.nsc.doc", - "scala.tools.nsc.doc.html", - "scala.tools.nsc.doc.html.page" - ) - - case None => - false - - } - } - -} diff --git a/test/scaladoc/model/CommentFactoryTest.scala b/test/scaladoc/model/CommentFactoryTest.scala deleted file mode 100644 index 204baaa05b..0000000000 --- a/test/scaladoc/model/CommentFactoryTest.scala +++ /dev/null @@ -1,76 +0,0 @@ -import org.scalacheck._ -import org.scalacheck.Prop._ - -import scala.tools.nsc.Global -import scala.tools.nsc.doc -import scala.tools.nsc.doc.model.comment._ - -class Factory(val g: Global, val s: doc.Settings) - extends doc.model.ModelFactory(g, s) { - thisFactory: Factory with CommentFactory with doc.model.TreeFactory => - - def strip(c: Comment): Option[Inline] = { - c.body match { - case Body(List(Paragraph(Chain(List(Summary(inner)))))) => Some(inner) - case _ => None - } - } - - def parseComment(s: String): Option[Inline] = - strip(parse(s, "", scala.tools.nsc.util.NoPosition)) -} - -object Test extends Properties("CommentFactory") { - val factory = { - val settings = new doc.Settings((str: String) => {}) - val reporter = new scala.tools.nsc.reporters.ConsoleReporter(settings) - val g = new Global(settings, reporter) - (new Factory(g, settings) with CommentFactory with doc.model.TreeFactory) - } - - def parse(src: String, dst: Inline) = { - factory.parseComment(src) match { - case Some(inline) => - inline == dst - case _ => - false - } - } - - property("parse") = parse( - "/** One two three */", - Text("One two three") - ) - property("parse") = parse( - "/** One `two` three */", - Chain(List(Text("One "), Monospace("two"), Text(" three"))) - ) - - property("parse") = parse( - """ -/** One two - * three */""", - Text("One two\nthree") - ) - property("parse") = parse( - """ -/** One `two` - * three */""", - Chain(List(Text("One "), Monospace("two"), Text("\n"), Text("three"))) - ) - - property("parse") = parse( - """ -/** One `two` - * three */""", - Chain(List(Text("One "), Monospace("two"), Text("\n"), Text(" three"))) - ) - - property("parse") = parse( - """ -/** One - * `two` three */""", - Chain(List(Text("One"), Text("\n"), Monospace("two"), Text(" three"))) - ) - -} diff --git a/test/scaladoc/resources/Trac3790.scala b/test/scaladoc/resources/Trac3790.scala new file mode 100644 index 0000000000..1ca80955a0 --- /dev/null +++ b/test/scaladoc/resources/Trac3790.scala @@ -0,0 +1,11 @@ +class Trac3790 { + /** + * A lazy String + */ + lazy val lazyString= "lazy" + + /** + * A non-lazy String + */ + val nonLazyString= "non-lazy" +} diff --git a/test/scaladoc/scala/IndexTest.scala b/test/scaladoc/scala/IndexTest.scala new file mode 100644 index 0000000000..5e3d02e045 --- /dev/null +++ b/test/scaladoc/scala/IndexTest.scala @@ -0,0 +1,102 @@ +import org.scalacheck._ +import org.scalacheck.Prop._ + +import scala.tools.nsc.doc +import scala.tools.nsc.doc.html.page.Index +import java.net.URLClassLoader + +object Test extends Properties("Index") { + + 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) + } + + val docFactory = { + val settings = new doc.Settings({Console.err.println(_)}) + + settings.classpath.value = getClasspath + println(settings.classpath.value) + + val reporter = new scala.tools.nsc.reporters.ConsoleReporter(settings) + + new doc.DocFactory(reporter, settings) + } + + val indexModelFactory = doc.model.IndexModelFactory + + def createIndex(path: String): Option[Index] = { + + val maybeUniverse = { + //val stream = new java.io.ByteArrayOutputStream + //val original = Console.out + //Console.setOut(stream) + + val result = docFactory.makeUniverse(List(path)) + + // assert(stream.toString == "model contains 2 documentable templates\n") + //Console.setOut(original) + + result + } + + maybeUniverse match { + case Some(universe) => { + val index = new Index(universe, indexModelFactory.makeIndex(universe)) + return Some(index) + } + case _ => return None + } + + } + + property("path") = { + createIndex("src/compiler/scala/tools/nsc/doc/html/page/Index.scala") match { + case Some(index) => + index.path == List("index.html") + case None => false + } + } + + property("title") = { + createIndex("src/compiler/scala/tools/nsc/doc/html/page/Index.scala") match { + case Some(index) => + index.title == "" + + case None => false + } + } + property("browser contants a script element") = { + createIndex("src/compiler/scala/tools/nsc/doc/html/page/Index.scala") match { + case Some(index) => + (index.browser \ "script").size == 1 + + case None => false + } + } + + property("allPackages") = { + createIndex("src/compiler/scala/tools/nsc/doc/html/page/Index.scala") match { + + case Some(index) => + index.allPackages.map(_.toString) == List( + "scala", + "scala.tools", + "scala.tools.nsc", + "scala.tools.nsc.doc", + "scala.tools.nsc.doc.html", + "scala.tools.nsc.doc.html.page" + ) + + case None => + false + + } + } + +} diff --git a/test/scaladoc/scala/html/HtmlFactoryTest.scala b/test/scaladoc/scala/html/HtmlFactoryTest.scala new file mode 100644 index 0000000000..98f0c94805 --- /dev/null +++ b/test/scaladoc/scala/html/HtmlFactoryTest.scala @@ -0,0 +1,97 @@ +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 => { +
{ group.nodes.map(stripGroup _) }
+ } + case e: Elem => { + val child = e.child.map(stripGroup _) + Elem(e.prefix, e.label, e.attributes, e.scope, child : _*) + } + case _ => seq + } + } + + def attributeIs(key: String, value: String) = { + (element: Node) => { + element.attribute(key) match { + case Some(v) => + v.toString == value + case _ => + false + } + } + } + + def textIs(value: String) = { + (node: Node) => { + node.descendant.exists((n) => n.toString.trim == value) + } + } +} + +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) + } + + val docFactory = { + 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]() + + docFactory.makeUniverse(List("test/scaladoc/resources/"+basename)) match { + case Some(universe) => { + val index = IndexModelFactory.makeIndex(universe) + (new HtmlFactory(universe, index)).writeTemplates((page) => { + result += (page.path.mkString("") -> page.body) + }) + } + case _ => ; + } + + result + } + + property("Trac #3790") = { + import XMLUtil._ + + val files = createTemplates("Trac3790.scala") + files("Trac3790.html") match { + case node: scala.xml.Node => { + val comments = (stripGroup(node) \\ "div").flatMap { + case e: scala.xml.Elem => Some(e) + case _ => None + }.filter { attributeIs("class", "fullcomment")(_) } + + comments.filter(textIs("A lazy String")(_)).length == 1 && + comments.filter(textIs("A non-lazy String")(_)).length == 1 + } + case _ => false + } + } + +} diff --git a/test/scaladoc/scala/model/CommentFactoryTest.scala b/test/scaladoc/scala/model/CommentFactoryTest.scala new file mode 100644 index 0000000000..204baaa05b --- /dev/null +++ b/test/scaladoc/scala/model/CommentFactoryTest.scala @@ -0,0 +1,76 @@ +import org.scalacheck._ +import org.scalacheck.Prop._ + +import scala.tools.nsc.Global +import scala.tools.nsc.doc +import scala.tools.nsc.doc.model.comment._ + +class Factory(val g: Global, val s: doc.Settings) + extends doc.model.ModelFactory(g, s) { + thisFactory: Factory with CommentFactory with doc.model.TreeFactory => + + def strip(c: Comment): Option[Inline] = { + c.body match { + case Body(List(Paragraph(Chain(List(Summary(inner)))))) => Some(inner) + case _ => None + } + } + + def parseComment(s: String): Option[Inline] = + strip(parse(s, "", scala.tools.nsc.util.NoPosition)) +} + +object Test extends Properties("CommentFactory") { + val factory = { + val settings = new doc.Settings((str: String) => {}) + val reporter = new scala.tools.nsc.reporters.ConsoleReporter(settings) + val g = new Global(settings, reporter) + (new Factory(g, settings) with CommentFactory with doc.model.TreeFactory) + } + + def parse(src: String, dst: Inline) = { + factory.parseComment(src) match { + case Some(inline) => + inline == dst + case _ => + false + } + } + + property("parse") = parse( + "/** One two three */", + Text("One two three") + ) + property("parse") = parse( + "/** One `two` three */", + Chain(List(Text("One "), Monospace("two"), Text(" three"))) + ) + + property("parse") = parse( + """ +/** One two + * three */""", + Text("One two\nthree") + ) + property("parse") = parse( + """ +/** One `two` + * three */""", + Chain(List(Text("One "), Monospace("two"), Text("\n"), Text("three"))) + ) + + property("parse") = parse( + """ +/** One `two` + * three */""", + Chain(List(Text("One "), Monospace("two"), Text("\n"), Text(" three"))) + ) + + property("parse") = parse( + """ +/** One + * `two` three */""", + Chain(List(Text("One"), Text("\n"), Monospace("two"), Text(" three"))) + ) + +} -- cgit v1.2.3