Skip to content

Commit

Permalink
Merge pull request #78 from ashawley/issue-78-attribute-tests
Browse files Browse the repository at this point in the history
Add more XML attribute unit tests for SI-9047
  • Loading branch information
biswanaths committed Aug 22, 2015
2 parents 4b1998a + d5609e3 commit 6126554
Showing 1 changed file with 94 additions and 1 deletion.
95 changes: 94 additions & 1 deletion src/test/scala/scala/xml/AttributeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,97 @@ class AttributeTest {
assertEquals("apple", xml \@ "bar")
}

}
@Test
def attributePathRootNoAttribute: Unit = {
val xml = <foo />
assertEquals(NodeSeq.Empty, xml \ "@bar")
}

@Test(expected=classOf[IllegalArgumentException])
def attributePathIllegalEmptyAttribute: Unit = {
val xml = <foo />
xml \ "@"
}

@Test
def attributePathRootWithOneAttribute: Unit = {
val xml = <foo bar="apple" />
assertEquals(Group(Text("apple")), xml \ "@bar")
// assertEquals(NodeSeq.fromSeq(Seq(Text("apple"))), xml \ "@bar")
}

@Test
def attributePathRootWithMissingAttributes: Unit = {
val xml = <foo bar="apple" />
assertEquals(NodeSeq.Empty, xml \ "@oops")
}

@Test
def attributePathDuplicateAttribute: Unit = {
val xml = Elem(null, "foo",
Attribute("bar", Text("apple"),
Attribute("bar", Text("orange"), Null)), TopScope)
assertEquals(Group(Text("apple")), xml \ "@bar")
}

@Test
def attributePathDescendantAttributes: Unit = {
val xml = <a><b bar="1" /><b bar="2" /></a>
assertEquals(NodeSeq.fromSeq(Seq(Text("1"), Text("2"))), (xml \\ "@bar"))
}

@Test
def attributeDescendantPathChildAttributes: Unit = {
val xml = <a><b bar="1" /><b bar="2" /></a>
assertEquals(NodeSeq.fromSeq(Seq(Text("1"), Text("2"))), (xml \ "b" \\ "@bar"))
}

@Test
def attributeDescendantPathDescendantAttributes: Unit = {
val xml = <a><b bar="1" /><b bar="2" /></a>
assertEquals(NodeSeq.fromSeq(Seq(Text("1"), Text("2"))), (xml \\ "b" \\ "@bar"))
}

@Test
def attributeChildDescendantPathDescendantAttributes: Unit = {
val xml = <x><a><b bar="1" /><b bar="2" /></a></x>
assertEquals(NodeSeq.fromSeq(Seq(Text("1"), Text("2"))), (xml \ "a" \\ "@bar"))
}

@Test
def attributeDescendantDescendantPathDescendantAttributes: Unit = {
val xml = <x><a><b bar="1" /><b bar="2" /></a></x>
assertEquals(NodeSeq.fromSeq(Seq(Text("1"), Text("2"))), (xml \\ "b" \\ "@bar"))
}

@Test(expected=classOf[IllegalArgumentException])
def attributePathDescendantIllegalEmptyAttribute: Unit = {
val xml = <foo />
xml \\ "@"
}

@Test
def attributePathNoDescendantAttributes: Unit = {
val xml = <a><b bar="1" /><b bar="2" /></a>
assertEquals(NodeSeq.Empty, (xml \\ "@oops"))
}

@Test
def attributePathOneChildWithAttributes: Unit = {
val xml = <a><b bar="1" />></a>
assertEquals(Group(Seq(Text("1"))), (xml \ "b" \ "@bar"))
}

@Test
def attributePathTwoChildrenWithAttributes: Unit = {
val xml = <a><b bar="1" /><b bar="2" /></a>
val b = xml \ "b"
assertEquals(2, b.length)
assertEquals(NodeSeq.fromSeq(Seq(<b bar="1"/>, <b bar="2"/>)), b)
val barFail = b \ "@bar"
val barList = b.map(_ \ "@bar")
assertEquals(NodeSeq.Empty, barFail)
assertEquals(List(Group(Seq(Text("1"))), Group(Seq(Text("2")))), barList)
}

}

0 comments on commit 6126554

Please sign in to comment.