Skip to content

Commit 6126554

Browse files
committed
Merge pull request #78 from ashawley/issue-78-attribute-tests
Add more XML attribute unit tests for SI-9047
2 parents 4b1998a + d5609e3 commit 6126554

File tree

1 file changed

+94
-1
lines changed

1 file changed

+94
-1
lines changed

src/test/scala/scala/xml/AttributeTest.scala

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,97 @@ class AttributeTest {
6565
assertEquals("apple", xml \@ "bar")
6666
}
6767

68-
}
68+
@Test
69+
def attributePathRootNoAttribute: Unit = {
70+
val xml = <foo />
71+
assertEquals(NodeSeq.Empty, xml \ "@bar")
72+
}
73+
74+
@Test(expected=classOf[IllegalArgumentException])
75+
def attributePathIllegalEmptyAttribute: Unit = {
76+
val xml = <foo />
77+
xml \ "@"
78+
}
79+
80+
@Test
81+
def attributePathRootWithOneAttribute: Unit = {
82+
val xml = <foo bar="apple" />
83+
assertEquals(Group(Text("apple")), xml \ "@bar")
84+
// assertEquals(NodeSeq.fromSeq(Seq(Text("apple"))), xml \ "@bar")
85+
}
86+
87+
@Test
88+
def attributePathRootWithMissingAttributes: Unit = {
89+
val xml = <foo bar="apple" />
90+
assertEquals(NodeSeq.Empty, xml \ "@oops")
91+
}
92+
93+
@Test
94+
def attributePathDuplicateAttribute: Unit = {
95+
val xml = Elem(null, "foo",
96+
Attribute("bar", Text("apple"),
97+
Attribute("bar", Text("orange"), Null)), TopScope)
98+
assertEquals(Group(Text("apple")), xml \ "@bar")
99+
}
100+
101+
@Test
102+
def attributePathDescendantAttributes: Unit = {
103+
val xml = <a><b bar="1" /><b bar="2" /></a>
104+
assertEquals(NodeSeq.fromSeq(Seq(Text("1"), Text("2"))), (xml \\ "@bar"))
105+
}
106+
107+
@Test
108+
def attributeDescendantPathChildAttributes: Unit = {
109+
val xml = <a><b bar="1" /><b bar="2" /></a>
110+
assertEquals(NodeSeq.fromSeq(Seq(Text("1"), Text("2"))), (xml \ "b" \\ "@bar"))
111+
}
112+
113+
@Test
114+
def attributeDescendantPathDescendantAttributes: Unit = {
115+
val xml = <a><b bar="1" /><b bar="2" /></a>
116+
assertEquals(NodeSeq.fromSeq(Seq(Text("1"), Text("2"))), (xml \\ "b" \\ "@bar"))
117+
}
118+
119+
@Test
120+
def attributeChildDescendantPathDescendantAttributes: Unit = {
121+
val xml = <x><a><b bar="1" /><b bar="2" /></a></x>
122+
assertEquals(NodeSeq.fromSeq(Seq(Text("1"), Text("2"))), (xml \ "a" \\ "@bar"))
123+
}
124+
125+
@Test
126+
def attributeDescendantDescendantPathDescendantAttributes: Unit = {
127+
val xml = <x><a><b bar="1" /><b bar="2" /></a></x>
128+
assertEquals(NodeSeq.fromSeq(Seq(Text("1"), Text("2"))), (xml \\ "b" \\ "@bar"))
129+
}
130+
131+
@Test(expected=classOf[IllegalArgumentException])
132+
def attributePathDescendantIllegalEmptyAttribute: Unit = {
133+
val xml = <foo />
134+
xml \\ "@"
135+
}
136+
137+
@Test
138+
def attributePathNoDescendantAttributes: Unit = {
139+
val xml = <a><b bar="1" /><b bar="2" /></a>
140+
assertEquals(NodeSeq.Empty, (xml \\ "@oops"))
141+
}
142+
143+
@Test
144+
def attributePathOneChildWithAttributes: Unit = {
145+
val xml = <a><b bar="1" />></a>
146+
assertEquals(Group(Seq(Text("1"))), (xml \ "b" \ "@bar"))
147+
}
148+
149+
@Test
150+
def attributePathTwoChildrenWithAttributes: Unit = {
151+
val xml = <a><b bar="1" /><b bar="2" /></a>
152+
val b = xml \ "b"
153+
assertEquals(2, b.length)
154+
assertEquals(NodeSeq.fromSeq(Seq(<b bar="1"/>, <b bar="2"/>)), b)
155+
val barFail = b \ "@bar"
156+
val barList = b.map(_ \ "@bar")
157+
assertEquals(NodeSeq.Empty, barFail)
158+
assertEquals(List(Group(Seq(Text("1"))), Group(Seq(Text("2")))), barList)
159+
}
160+
161+
}

0 commit comments

Comments
 (0)