/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. */
  18. package org.fusesource.scalate.scuery.support
  19. import _root_.org.fusesource.scalate.FunSuiteSupport
  20. import org.fusesource.scalate.scuery.Selector
  21. import org.fusesource.scalate.scuery.XmlHelper._
  22. import xml.{ Elem, Node, NodeSeq }
  23. abstract class CssParserTestSupport extends FunSuiteSupport {
  24. var parser = new CssParser
  25. def xml: Node
  26. def assertFilter(selector: String, expected: NodeSeq): Unit = {
  27. test("assertFilter: " + selector) {
  28. val actual = xml.$(selector)
  29. debug("filtering selector: %s expected: %s actual: %s", selector, expected, actual)
  30. assertResult(expected) { actual }
  31. }
  32. }
  33. def assertMatches(css: String, node: Node): Unit = assertSelector("assertMatches ", css, node, true)
  34. def assertNotMatches(css: String, node: Node): Unit = assertSelector("assertNotMatches ", css, node, false)
  35. def assertSelector(message: String, css: String, node: Node, expected: Boolean): Unit = {
  36. test(message + css + " on " + summary(node)) {
  37. val selector = Selector(css)
  38. val ancestors = ancestorsOf(node)
  39. debug("testing selector: " + selector + " on " + summary(node) + " with ancestors: " + summary(ancestors))
  40. assertResult(expected) { selector.matches(node, ancestors) }
  41. }
  42. }
  43. def ancestorsOf(node: Node, ancestor: Node = xml): Seq[Node] = {
  44. def findChild(node: Node, ancestor: Node): Option[Seq[Node]] = {
  45. if (node == ancestor) {
  46. Some(Nil)
  47. } else if (ancestor.contains(node)) {
  48. Some(ancestor :: Nil)
  49. } else {
  50. var a: Option[Seq[Node]] = None
  51. for (c <- ancestor.child if a.isEmpty) {
  52. a = findChild(node, c)
  53. }
  54. a match {
  55. case Some(l) => Some(l ++ ancestor)
  56. case _ => a
  57. }
  58. }
  59. }
  60. findChild(node, ancestor).getOrElse(Nil)
  61. }
  62. protected def summary(node: Node): String = node match {
  63. case e: Elem => replaceContent(e, Nil).toString
  64. case _ => node.toString
  65. }
  66. protected def summary(nodes: NodeSeq): String = nodes.map(summary(_)).mkString(" ")
  67. }