1require 'test/unit/testcase' 2require 'rexml/document' 3 4class AttributesTester < Test::Unit::TestCase 5 include REXML 6 def test_accessor 7 doc = Document.new("<a xmlns:foo='a' xmlns:bar='b' foo:att='1' bar:att='2' att='3'/>") 8 assert_equal '3', doc.root.attributes['att'] 9 assert_equal '2', doc.root.attributes['bar:att'] 10 doc.root.attributes['att'] = 5 11 assert_equal '5', doc.root.attributes['att'] 12 end 13 14 def test_each_attribute 15 doc = Document.new('<a x="1" y="2"/>') 16 doc.root.attributes.each_attribute {|attr| 17 if attr.expanded_name == 'x' 18 assert_equal '1', attr.value 19 elsif attr.expanded_name == 'y' 20 assert_equal '2', attr.value 21 else 22 assert_fail "No such attribute!!" 23 end 24 } 25 end 26 27 def test_each 28 doc = Document.new('<a x="1" y="2"/>') 29 doc.root.attributes.each {|name, value| 30 if name == 'x' 31 assert_equal '1', value 32 elsif name == 'y' 33 assert_equal '2', value 34 else 35 assert_fail "No such attribute!!" 36 end 37 } 38 end 39 40 def test_get_attribute 41 doc = Document.new('<a xmlns:x="a" x:foo="1" foo="2" bar="3"/>') 42 assert_equal '2', doc.root.attributes.get_attribute("foo").value 43 assert_equal '1', doc.root.attributes.get_attribute("x:foo").value 44 end 45 46 def test_size 47 doc = Document.new("<a xmlns:foo='a' x='1' y='2' foo:x='3'/>") 48 assert_equal 4, doc.root.attributes.length 49 end 50 51 def test_setter 52 doc = Document.new("<a xmlns:x='a' x:foo='1' foo='3'/>") 53 doc.root.attributes['y:foo'] = '2' 54 assert_equal '2', doc.root.attributes['y:foo'] 55 doc.root.attributes['foo'] = '4' 56 assert_equal '4', doc.root.attributes['foo'] 57 doc.root.attributes['x:foo'] = nil 58 assert_equal 3, doc.root.attributes.size 59 end 60 61 def test_delete 62 doc = Document.new("<a xmlns:y='a' xmlns:x='b' xmlns:z='c' y:foo='0' x:foo='1' foo='3' z:foo='4'/>") 63 doc.root.attributes.delete 'foo' 64 assert_equal 6, doc.root.attributes.size 65 assert_equal '1', doc.root.attributes['x:foo'] 66 67 doc.root.attributes.delete 'x:foo' 68 assert_equal 5, doc.root.attributes.size 69 70 attr = doc.root.attributes.get_attribute('y:foo') 71 doc.root.attributes.delete attr 72 assert_equal 4, doc.root.attributes.size 73 74 assert_equal '4', doc.root.attributes['z:foo'] 75 end 76 77 def test_prefixes 78 doc = Document.new("<a xmlns='foo' xmlns:x='bar' xmlns:y='twee' z='glorp' x:k='gru'/>") 79 prefixes = doc.root.attributes.prefixes 80 assert_equal 2, prefixes.size 81 assert_equal 0, (prefixes - ['x', 'y']).size 82 end 83 84 # Contributed by Mike Stok 85 def test_values_with_apostrophes 86 doc = Document.new(%q#<tag h1="1'2'" h2='1"2'/>#) 87 s = doc.to_s 88 assert(s =~ /h1='1'2''/) 89 assert(s =~ /h2='1"2'/) 90 end 91 92 # Submitted by Kou 93 def test_namespace_conflict 94 assert_raise( ParseException, 95 "Declaring two attributes with the same namespace should be an error" ) do 96 REXML::Document.new <<-XML 97 <x xmlns:n1="http://www.w3.org" 98 xmlns:n2="http://www.w3.org" > 99 <bad n1:a="1" n2:a="2" /> 100 </x> 101 XML 102 end 103 104 REXML::Document.new("<a xmlns:a='a' xmlns:b='a'></a>") 105 end 106 107 # Submitted by Kou 108 def test_attribute_deletion 109 e = REXML::Element.new 110 e.add_namespace("a", "http://a/") 111 e.add_namespace("b", "http://b/") 112 e.add_attributes({"c" => "cc", "a:c" => "cC", "b:c" => "CC"}) 113 114 e.attributes.delete("c") 115 assert_nil(e.attributes.get_attribute("c")) 116 117 before_size = e.attributes.size 118 e.attributes.delete("c") 119 assert_nil(e.attributes.get_attribute("c")) 120 assert_equal(before_size, e.attributes.size) 121 122 e.attributes.delete(e.attributes.get_attribute("a:c")) 123 assert_nil(e.attributes.get_attribute("a:c")) 124 125 e.attributes.delete("b:c") 126 assert_nil(e.attributes.get_attribute("b:c")) 127 128 before_size = e.attributes.size 129 e.attributes.delete(e.attributes.get_attribute("b:c")) 130 assert_nil(e.attributes.get_attribute("b:c")) 131 assert_equal(before_size, e.attributes.size) 132 133 before_size = e.attributes.size 134 e.attributes.delete("c") 135 assert_nil(e.attributes.get_attribute("c")) 136 assert_equal(before_size, e.attributes.size) 137 138 e.add_attribute("c", "cc") 139 140 e.attributes.delete(e.attributes.get_attribute("c")) 141 assert_nil(e.attributes.get_attribute("c")) 142 end 143 144 # Submitted by Kou 145 def test_element_usage 146 attr = Attribute.new("name", "value") 147 elem = Element.new("elem") 148 a = Attribute.new(attr, elem) 149 assert_equal(elem, a.element) 150 end 151 152 def attr_test(attr_name,attr_value) 153 a1 = REXML::Attribute.new(attr_name,attr_value) 154 155 s1 = a1.value 156 s2 = a1.value 157 158 #p s1 159 #p s2 160 assert_equal(s1,s2) 161 162 a2 = REXML::Attribute.new(attr_name,attr_value) 163 164 a2.to_s # NB invocation of to_s 165 s1 = a2.value 166 s2 = a2.value 167 168 #p s1 169 #p s2 170 assert_equal(s1,s2) 171 end 172 173 def test_amp_attributes 174 attr_test('name','value with & ampersand only') 175 end 176 177 def test_amp_and_lf_attributes 178 attr_test('name','value with LF 
 & ampersand') 179 end 180 181 def test_quoting 182 d = Document.new(%q{<a x='1' y="2"/>}) 183 assert_equal( %q{<a x='1' y='2'/>}, d.to_s ) 184 d.root.context[:attribute_quote] = :quote 185 assert_equal( %q{<a x="1" y="2"/>}, d.to_s ) 186 187 d = Document.new(%q{<a x='1' y="2"><b z='3'/></a>}) 188 assert_equal( %q{<a x='1' y='2'><b z='3'/></a>}, d.to_s ) 189 d.root.context[:attribute_quote] = :quote 190 assert_equal( %q{<a x="1" y="2"><b z="3"/></a>}, d.to_s ) 191 end 192 193 def test_ticket_127 194 doc = Document.new 195 doc.add_element 'a', { 'v' => 'x & y' } 196 assert doc.to_s.index(';') 197 end 198end 199