2010-09-17 09:14:14 -04:00
|
|
|
require 'test/unit/testcase'
|
|
|
|
require 'rexml/document'
|
|
|
|
|
2014-05-27 08:07:40 -04:00
|
|
|
module REXMLTests
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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']
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
doc.root.attributes.delete 'x:foo'
|
|
|
|
assert_equal 5, doc.root.attributes.size
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
attr = doc.root.attributes.get_attribute('y:foo')
|
|
|
|
doc.root.attributes.delete attr
|
|
|
|
assert_equal 4, doc.root.attributes.size
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
assert_equal '4', doc.root.attributes['z:foo']
|
|
|
|
end
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
# 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'2''/)
|
|
|
|
assert(s =~ /h2='1"2'/)
|
2010-09-17 09:46:56 -04:00
|
|
|
end
|
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
# 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
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
REXML::Document.new("<a xmlns:a='a' xmlns:b='a'></a>")
|
|
|
|
end
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
# 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"})
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
e.attributes.delete("c")
|
|
|
|
assert_nil(e.attributes.get_attribute("c"))
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
before_size = e.attributes.size
|
|
|
|
e.attributes.delete("c")
|
|
|
|
assert_nil(e.attributes.get_attribute("c"))
|
|
|
|
assert_equal(before_size, e.attributes.size)
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
e.attributes.delete(e.attributes.get_attribute("a:c"))
|
|
|
|
assert_nil(e.attributes.get_attribute("a:c"))
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
e.attributes.delete("b:c")
|
|
|
|
assert_nil(e.attributes.get_attribute("b:c"))
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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)
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
before_size = e.attributes.size
|
|
|
|
e.attributes.delete("c")
|
|
|
|
assert_nil(e.attributes.get_attribute("c"))
|
|
|
|
assert_equal(before_size, e.attributes.size)
|
2010-09-17 09:46:56 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
e.add_attribute("c", "cc")
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
e.attributes.delete(e.attributes.get_attribute("c"))
|
|
|
|
assert_nil(e.attributes.get_attribute("c"))
|
|
|
|
end
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
# 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
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
def attr_test(attr_name,attr_value)
|
|
|
|
a1 = REXML::Attribute.new(attr_name,attr_value)
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
s1 = a1.value
|
|
|
|
s2 = a1.value
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
#p s1
|
|
|
|
#p s2
|
|
|
|
assert_equal(s1,s2)
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
a2 = REXML::Attribute.new(attr_name,attr_value)
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
a2.to_s # NB invocation of to_s
|
|
|
|
s1 = a2.value
|
|
|
|
s2 = a2.value
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
#p s1
|
|
|
|
#p s2
|
|
|
|
assert_equal(s1,s2)
|
|
|
|
end
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
def test_amp_attributes
|
|
|
|
attr_test('name','value with & ampersand only')
|
|
|
|
end
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
def test_amp_and_lf_attributes
|
|
|
|
attr_test('name','value with LF 
 & ampersand')
|
|
|
|
end
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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 )
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2013-04-26 09:56:34 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
def test_to_a_with_namespaces
|
|
|
|
document = Document.new(<<-XML)
|
2013-04-26 09:56:34 -04:00
|
|
|
<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
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2013-04-26 09:56:34 -04:00
|
|
|
end
|
2010-09-17 09:14:14 -04:00
|
|
|
end
|