pyc-website
main website for pyc inc.
git clone https://9o.is/git/pyc-website.git
HtmlSourceSpec.scala
(1614B)
1 package inc.pyc
2
3 import java.io.File
4
5 import scala.xml.XML
6
7 import net.liftweb.common.Full
8 import net.liftweb.util.Html5
9
10 object HtmlSourceSpec extends BaseWordSpec {
11
12 "HTML Sources" should {
13
14 "be well-formed" in {
15 /**
16 * Tests to make sure the project's HTML files are well-formed.
17 *
18 * Finds every *.html and *.xml file in src/main/webapp (and its
19 * subdirectories) and tests to make sure they are well-formed.
20 */
21 var failed: List[File] = Nil
22
23 def handledXml(file: String) = file.endsWith(".xml")
24
25 def handledXHtml(file: String) =
26 file.endsWith(".html") || file.endsWith(".htm") || file.endsWith(".xhtml")
27
28 def wellFormed(file: File) {
29 if (file.isDirectory)
30 for (f <- file.listFiles) wellFormed(f)
31
32 if (file.isFile && handledXml(file.getName)) {
33 try {
34 XML.loadFile(file)
35 } catch {
36 case e: org.xml.sax.SAXParseException => failed = file :: failed
37 }
38 }
39 if (file.isFile && handledXHtml(file.getName)) {
40 Html5.parse(new java.io.FileInputStream(file.getAbsolutePath)) match {
41 case Full(_) => // file is ok
42 case _ => failed = file :: failed
43 }
44 }
45 }
46
47 wellFormed(new File("src/main/webapp"))
48
49 val numFails = failed.size
50 if (numFails > 0) {
51 val fileStr = if (numFails == 1) "file" else "files"
52 val msg = "Malformed HTML in " + numFails + " " + fileStr + ": " + failed.mkString(", ")
53 fail(msg)
54 }
55
56 numFails should equal (0)
57 }
58 }
59 }