Skip to content
This repository has been archived by the owner on May 15, 2019. It is now read-only.

Commit

Permalink
Actually, I really do like \ better than >
Browse files Browse the repository at this point in the history
  • Loading branch information
djspiewak committed Feb 15, 2011
1 parent 4accf6d commit fe2777b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/main/scala/com/codecommit/antixml/NodeSeq.scala
Expand Up @@ -51,7 +51,7 @@ class NodeSeq private (private val nodes: Vector[Node]) extends IndexedSeq[Node]
def updated(index: Int, node: Node) = new NodeSeq(nodes.updated(index, node))

// TODO optimize
def >(name: String): NodeSeq = {
def \(name: String): NodeSeq = {
this flatMap {
case Elem(_, _, _, children) => {
children filter {
Expand All @@ -64,13 +64,13 @@ class NodeSeq private (private val nodes: Vector[Node]) extends IndexedSeq[Node]
}

// TODO optimize
def >>(name: String): NodeSeq = {
def \\(name: String): NodeSeq = {
val recursive = this flatMap {
case Elem(_, _, _, children) => children >> name
case Elem(_, _, _, children) => children \\ name
case _ => Nil
}

(this > name) ++ recursive
(this \ name) ++ recursive
}

override def toString = nodes.mkString
Expand Down
14 changes: 7 additions & 7 deletions src/test/scala/com/codecommit/antixml/NodeSeqSpecs.scala
Expand Up @@ -8,38 +8,38 @@ object NodeSeqSpecs extends Specification {
"shallow selector" should {
"find an immediate descendant" in {
val ns = fromString("<parent><parent/></parent>")
ns > "parent" mustEqual NodeSeq(elem("parent"))
ns \ "parent" mustEqual NodeSeq(elem("parent"))
}

"be referentially transparent" in {
val ns = fromString("<parent><parent/></parent>")
ns > "parent" mustEqual NodeSeq(elem("parent"))
ns > "parent" mustEqual NodeSeq(elem("parent"))
ns \ "parent" mustEqual NodeSeq(elem("parent"))
ns \ "parent" mustEqual NodeSeq(elem("parent"))
}

"find a subset of nodes" in {
val ns = fromString("<parent>Some<a/>text<b/>to\nreally<c/>confuse<a/><b/><d/>things<e/><a/><f/></parent>")
val result = NodeSeq(elem("a"), elem("a"), elem("a"))
ns > "a" mustEqual result
ns \ "a" mustEqual result
}
}

"deep selector" should {
"find an immediate descendant" in {
val ns = fromString("<parent><parent/></parent>")
ns >> "parent" mustEqual NodeSeq(elem("parent"))
ns \\ "parent" mustEqual NodeSeq(elem("parent"))
}

"find a subset of nodes" in {
val ns = fromString("<parent>Some<a/>text<b/>to\nreally<c/>confuse<a/><b/><d/>things<e/><a/><f/></parent>")
val result = NodeSeq(elem("a"), elem("a"), elem("a"))
ns >> "a" mustEqual result
ns \\ "a" mustEqual result
}

"find and linearize a deep subset of nodes" in {
val ns = fromString("<parent>Some text<sub1><target>sub1</target></sub1><target>top<sub1><target>top1</target><target>top2</target></sub1><target>top3-outer</target></target><phoney><target>phoney</target></phoney>More text<target>outside</target></parent>")
val result = fromString("<parent><target>top<sub1><target>top1</target><target>top2</target></sub1><target>top3-outer</target></target><target>outside</target><target>sub1</target><target>top3-outer</target><target>phoney</target><target>top1</target><target>top2</target></parent>")
ns >> "target" mustEqual result.head.asInstanceOf[Elem].children
ns \\ "target" mustEqual result.head.asInstanceOf[Elem].children
}
}

Expand Down

0 comments on commit fe2777b

Please sign in to comment.