1+ package scala .xml
2+
3+ import org .junit .Test
4+ import org .junit .Ignore
5+ import org .junit .runner .RunWith
6+ import org .junit .runners .JUnit4
7+ import org .junit .Assert .assertTrue
8+ import org .junit .Assert .assertFalse
9+ import org .junit .Assert .assertEquals
10+
11+ class AttributeTest {
12+ @ Test
13+ def unprefixedAttribute : Unit = {
14+ val x = new UnprefixedAttribute (" foo" ," bar" , Null )
15+ assertEquals(Some (Text (" bar" )), x.get(" foo" ))
16+ assertEquals(Text (" bar" ), x(" foo" ))
17+ assertEquals(None , x.get(" no_foo" ))
18+ assertEquals(null , x(" no_foo" ))
19+
20+ val y = x.remove(" foo" )
21+ assertEquals(Null , y)
22+
23+ val z = new UnprefixedAttribute (" foo" , null : NodeSeq , x)
24+ assertEquals(None , z.get(" foo" ))
25+
26+ var appended = x append x append x append x
27+ var len = 0 ; while (appended ne Null ) {
28+ appended = appended.next
29+ len = len + 1
30+ }
31+ assertEquals(" removal of duplicates for unprefixed attributes in append" , 1 , len)
32+ }
33+
34+ @ Test
35+ def attributeWithOption : Unit = {
36+ val x = new UnprefixedAttribute (" foo" , Some (Text (" bar" )), Null )
37+
38+ assertEquals(Some (Text (" bar" )), x.get(" foo" ))
39+ assertEquals(Text (" bar" ), x(" foo" ))
40+ assertEquals(None , x.get(" no_foo" ))
41+ assertEquals(null , x(" no_foo" ))
42+
43+ val attr1 = Some (Text (" foo value" ))
44+ val attr2 = None
45+ val y = <b foo ={attr1} bar ={attr2} />
46+ assertEquals(Some (Text (" foo value" )), y.attributes.get(" foo" ))
47+ assertEquals(Text (" foo value" ), y.attributes(" foo" ))
48+ assertEquals(None , y.attributes.get(" bar" ))
49+ assertEquals(null , y.attributes(" bar" ))
50+
51+ val z = new UnprefixedAttribute (" foo" , None , x)
52+ assertEquals(None , z.get(" foo" ))
53+ }
54+
55+ @ Test
56+ def attributeToString : Unit = {
57+ val expected : String = """ <b x="&"/>"""
58+ assertEquals(expected, (<b x =" & " />).toString)
59+ assertEquals(expected, (<b x ={" &" }/>).toString)
60+ }
61+
62+ @ Test
63+ def attributeOperator : Unit = {
64+ val xml = <foo bar =" apple" />
65+ assertEquals(" apple" , xml \@ " bar" )
66+ }
67+
68+ }
0 commit comments