Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more XML attribute unit tests for SI-9047 #78

Merged
merged 2 commits into from
Aug 22, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we get rid of this comment ? Advance sorries if it feels very nit picky.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Looks like I put that there on purpose. It is what the test would be if we used the proposed change in issue 43. Granted, I didn't leave these comments consistently.

}

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

@Test
def attributePathDuplicateAttribute: Unit = {
val xml = Elem(null, "foo",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reasons you are not using xml literals in this unit test ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using xml literals where I can. This particular unit test doesn't because you can't: The literal syntax won't let you actually create an element with duplicate attributes. I'm trying to force it by manually constructing a scala.xml.Elem object. This is a hoax though, since it just gets removed by MetaData.normalize in the Elem constructor.

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)
}

}