summaryrefslogtreecommitdiff
path: root/test/rexml/test_attributes.rb
blob: fea4ee7636cd67c0c8240f5d4e996a50c6d810c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
require 'test/unit/testcase'
require 'rexml/document'

module REXMLTests
class AttributesTester < Test::Unit::TestCase
  include REXML
  def test_accessor
    doc = Document.new("<a xmlns:foo='a' xmlns:bar='b' foo:att='1' bar:att='2' att='3'/>")
    assert_equal '3', doc.root.attributes['att']
    assert_equal '2', doc.root.attributes['bar:att']
    doc.root.attributes['att'] = 5
    assert_equal '5', doc.root.attributes['att']
  end

  def test_each_attribute
    doc = Document.new('<a x="1" y="2"/>')
    doc.root.attributes.each_attribute {|attr|
      if attr.expanded_name == 'x'
        assert_equal '1', attr.value
      elsif attr.expanded_name == 'y'
        assert_equal '2', attr.value
      else
        assert_fail "No such attribute!!"
      end
    }
  end

  def test_each
    doc = Document.new('<a x="1" y="2"/>')
    doc.root.attributes.each {|name, value|
      if name == 'x'
        assert_equal '1', value
      elsif name == 'y'
        assert_equal '2', value
      else
        assert_fail "No such attribute!!"
      end
    }
  end

  def test_get_attribute
    doc = Document.new('<a xmlns:x="a" x:foo="1" foo="2" bar="3"/>')
    assert_equal '2', doc.root.attributes.get_attribute("foo").value
    assert_equal '1', doc.root.attributes.get_attribute("x:foo").value
  end

  def test_size
    doc = Document.new("<a xmlns:foo='a' x='1' y='2' foo:x='3'/>")
    assert_equal 4, doc.root.attributes.length
  end

  def test_setter
    doc = Document.new("<a xmlns:x='a' x:foo='1' foo='3'/>")
    doc.root.attributes['y:foo'] = '2'
    assert_equal '2', doc.root.attributes['y:foo']
    doc.root.attributes['foo'] = '4'
    assert_equal '4', doc.root.attributes['foo']
    doc.root.attributes['x:foo'] = nil
    assert_equal 3, doc.root.attributes.size
  end

  def test_delete
    doc = Document.new("<a xmlns:y='a' xmlns:x='b' xmlns:z='c' y:foo='0' x:foo='1' foo='3' z:foo='4'/>")
    doc.root.attributes.delete 'foo'
    assert_equal 6, doc.root.attributes.size
    assert_equal '1', doc.root.attributes['x:foo']

    doc.root.attributes.delete 'x:foo'
    assert_equal 5, doc.root.attributes.size

    attr = doc.root.attributes.get_attribute('y:foo')
    doc.root.attributes.delete attr
    assert_equal 4, doc.root.attributes.size

    assert_equal '4', doc.root.attributes['z:foo']
  end

  def test_prefixes
    doc = Document.new("<a xmlns='foo' xmlns:x='bar' xmlns:y='twee' z='glorp' x:k='gru'/>")
    prefixes = doc.root.attributes.prefixes
    assert_equal 2, prefixes.size
    assert_equal 0, (prefixes - ['x', 'y']).size
  end

  # Contributed by Mike Stok
  def test_values_with_apostrophes
    doc = Document.new(%q#<tag h1="1'2'" h2='1"2'/>#)
    s = doc.to_s
    assert(s =~ /h1='1&apos;2&apos;'/)
    assert(s =~ /h2='1"2'/)
  end

  # Submitted by Kou
  def test_namespace_conflict
    assert_raise( ParseException,
                  "Declaring two attributes with the same namespace should be an error" ) do
      REXML::Document.new <<-XML
      <x xmlns:n1="http://www.w3.org"
         xmlns:n2="http://www.w3.org" >
        <bad n1:a="1"  n2:a="2" />
      </x>
      XML
    end

    REXML::Document.new("<a xmlns:a='a' xmlns:b='a'></a>")
  end

  # Submitted by Kou
  def test_attribute_deletion
    e = REXML::Element.new
    e.add_namespace("a", "http://a/")
    e.add_namespace("b", "http://b/")
    e.add_attributes({"c" => "cc", "a:c" => "cC", "b:c" => "CC"})

    e.attributes.delete("c")
    assert_nil(e.attributes.get_attribute("c"))

    before_size = e.attributes.size
    e.attributes.delete("c")
    assert_nil(e.attributes.get_attribute("c"))
    assert_equal(before_size, e.attributes.size)

    e.attributes.delete(e.attributes.get_attribute("a:c"))
    assert_nil(e.attributes.get_attribute("a:c"))

    e.attributes.delete("b:c")
    assert_nil(e.attributes.get_attribute("b:c"))

    before_size = e.attributes.size
    e.attributes.delete(e.attributes.get_attribute("b:c"))
    assert_nil(e.attributes.get_attribute("b:c"))
    assert_equal(before_size, e.attributes.size)

    before_size = e.attributes.size
    e.attributes.delete("c")
    assert_nil(e.attributes.get_attribute("c"))
    assert_equal(before_size, e.attributes.size)

    e.add_attribute("c", "cc")

    e.attributes.delete(e.attributes.get_attribute("c"))
    assert_nil(e.attributes.get_attribute("c"))
  end

  # Submitted by Kou
  def test_element_usage
    attr = Attribute.new("name", "value")
    elem = Element.new("elem")
    a = Attribute.new(attr, elem)
    assert_equal(elem, a.element)
  end

  def attr_test(attr_name,attr_value)
    a1 = REXML::Attribute.new(attr_name,attr_value)

    s1 = a1.value
    s2 = a1.value

    #p s1
    #p s2
    assert_equal(s1,s2)

    a2 = REXML::Attribute.new(attr_name,attr_value)

    a2.to_s        # NB invocation of to_s
    s1 = a2.value
    s2 = a2.value

    #p s1
    #p s2
    assert_equal(s1,s2)
  end

  def test_amp_attributes
    attr_test('name','value with &amp; ampersand only')
  end

  def test_amp_and_lf_attributes
    attr_test('name','value with LF &#x000a; &amp; ampersand')
  end

  def test_quoting
    d = Document.new(%q{<a x='1' y="2"/>})
    assert_equal( %q{<a x='1' y='2'/>}, d.to_s )
    d.root.context[:attribute_quote] = :quote
    assert_equal( %q{<a x="1" y="2"/>}, d.to_s )

    d = Document.new(%q{<a x='1' y="2"><b z='3'/></a>})
    assert_equal( %q{<a x='1' y='2'><b z='3'/></a>}, d.to_s )
    d.root.context[:attribute_quote] = :quote
    assert_equal( %q{<a x="1" y="2"><b z="3"/></a>}, d.to_s )
  end

  def test_ticket_127
    doc = Document.new
    doc.add_element 'a', { 'v' => 'x & y' }
    assert doc.to_s.index(';')
  end

  def test_to_a_with_namespaces
    document = Document.new(<<-XML)
<root
  xmlns:ns1="http://example.org/ns1"
  xmlns:ns2="http://example.org/ns2">
  <child
    ns1:attribute="ns1"
    ns2:attribute="ns2"
        attribute="no-ns"
    other-attribute="other-value"/>
</root>
XML
    child = document.root.elements["child"]
    assert_equal([
                   "attribute='no-ns'",
                   "ns1:attribute='ns1'",
                   "ns2:attribute='ns2'",
                   "other-attribute='other-value'",
                 ],
                 child.attributes.to_a.collect(&:to_string).sort)
  end
end
end