PageRenderTime 15ms CodeModel.GetById 6ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-core/src/test/scala/org/fusesource/scalate/scuery/support/CssParserTestSupport.scala

http://github.com/scalate/scalate
Scala | 78 lines | 50 code | 11 blank | 17 comment | 7 complexity | 4b7cd10551678bc257266ae1e6c21cc3 MD5 | raw file
 1/**
 2 * Copyright (C) 2009-2011 the original author or authors.
 3 * See the notice.md file distributed with this work for additional
 4 * information regarding copyright ownership.
 5 *
 6 * Licensed under the Apache License, Version 2.0 (the "License");
 7 * you may not use this file except in compliance with the License.
 8 * You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.fusesource.scalate.scuery.support
19
20import _root_.org.fusesource.scalate.FunSuiteSupport
21import org.fusesource.scalate.scuery.Selector
22import org.fusesource.scalate.scuery.XmlHelper._
23import xml.{ Elem, Node, NodeSeq }
24
25abstract class CssParserTestSupport extends FunSuiteSupport {
26  var parser = new CssParser
27
28  def xml: Node
29
30  def assertFilter(selector: String, expected: NodeSeq): Unit = {
31    test("assertFilter: " + selector) {
32      val actual = xml.$(selector)
33
34      debug("filtering selector: %s expected: %s actual: %s", selector, expected, actual)
35      assertResult(expected) { actual }
36    }
37  }
38
39  def assertMatches(css: String, node: Node): Unit = assertSelector("assertMatches ", css, node, true)
40
41  def assertNotMatches(css: String, node: Node): Unit = assertSelector("assertNotMatches ", css, node, false)
42
43  def assertSelector(message: String, css: String, node: Node, expected: Boolean): Unit = {
44    test(message + css + " on " + summary(node)) {
45      val selector = Selector(css)
46      val ancestors = ancestorsOf(node)
47      debug("testing selector: " + selector + " on " + summary(node) + " with ancestors: " + summary(ancestors))
48      assertResult(expected) { selector.matches(node, ancestors) }
49    }
50  }
51
52  def ancestorsOf(node: Node, ancestor: Node = xml): Seq[Node] = {
53    def findChild(node: Node, ancestor: Node): Option[Seq[Node]] = {
54      if (node == ancestor) {
55        Some(Nil)
56      } else if (ancestor.contains(node)) {
57        Some(ancestor :: Nil)
58      } else {
59        var a: Option[Seq[Node]] = None
60        for (c <- ancestor.child if a.isEmpty) {
61          a = findChild(node, c)
62        }
63        a match {
64          case Some(l) => Some(l ++ ancestor)
65          case _ => a
66        }
67      }
68    }
69    findChild(node, ancestor).getOrElse(Nil)
70  }
71
72  protected def summary(node: Node): String = node match {
73    case e: Elem => replaceContent(e, Nil).toString
74    case _ => node.toString
75  }
76
77  protected def summary(nodes: NodeSeq): String = nodes.map(summary(_)).mkString(" ")
78}