1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* test/rexml/: untabify.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29285 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
kou 2010-09-17 13:46:56 +00:00
parent 146bf4fdaf
commit 2a15d25a59
23 changed files with 1668 additions and 1673 deletions

View file

@ -1,3 +1,7 @@
Fri Sep 17 22:46:02 2010 Kouhei Sutou <kou@cozmixng.org>
* test/rexml/: untabify.
Fri Sep 17 22:29:56 2010 Kouhei Sutou <kou@cozmixng.org>
* test/rexml/: fix fixture data path. All REXML tests are worked.

View file

@ -2,152 +2,152 @@ require 'test/unit/testcase'
require 'rexml/document'
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'/)
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
# 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
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
REXML::Document.new("<a xmlns:a='a' xmlns:b='a'></a>")
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
# 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"})
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
e.attributes.delete("c")
assert_nil(e.attributes.get_attribute("c"))
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
before_size = e.attributes.size
e.attributes.delete("c")
assert_nil(e.attributes.get_attribute("c"))
assert_equal(before_size, e.attributes.size)
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
e.attributes.delete(e.attributes.get_attribute("a:c"))
assert_nil(e.attributes.get_attribute("a:c"))
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']
e.attributes.delete("b:c")
assert_nil(e.attributes.get_attribute("b:c"))
doc.root.attributes.delete 'x:foo'
assert_equal 5, doc.root.attributes.size
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)
attr = doc.root.attributes.get_attribute('y:foo')
doc.root.attributes.delete attr
assert_equal 4, doc.root.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)
assert_equal '4', doc.root.attributes['z:foo']
end
e.add_attribute("c", "cc")
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
e.attributes.delete(e.attributes.get_attribute("c"))
assert_nil(e.attributes.get_attribute("c"))
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_element_usage
attr = Attribute.new("name", "value")
elem = Element.new("elem")
a = Attribute.new(attr, elem)
assert_equal(elem, a.element)
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)

View file

@ -5,7 +5,6 @@ require 'test/unit'
require 'rexml/document'
class TestAttributes < Test::Unit::TestCase
def setup
@ns_a = "urn:x-test:a"
@ns_b = "urn:x-test:b"
@ -21,7 +20,7 @@ class TestAttributes < Test::Unit::TestCase
XMLEND
@attributes = REXML::Document.new(element_string).root.attributes
end
def test_get_attribute_ns
assert_equal("1", @attributes.get_attribute_ns("", "a").value)
assert_equal("2", @attributes.get_attribute_ns("", "b").value)
@ -30,5 +29,4 @@ class TestAttributes < Test::Unit::TestCase
assert_equal("5", @attributes.get_attribute_ns(@ns_a, "e").value)
assert_equal("6", @attributes.get_attribute_ns(@ns_b, "f").value)
end
end

View file

@ -8,9 +8,9 @@ require "rexml/formatters/default"
class ContribTester < Test::Unit::TestCase
include REXMLTestUtils
include REXML
include REXML
XML_STRING_01 = <<DELIMITER
XML_STRING_01 = <<DELIMITER
<?xml version="1.0" encoding="UTF-8"?>
<biblio>
<entry type="Book">
@ -31,7 +31,7 @@ XML_STRING_01 = <<DELIMITER
</biblio>
DELIMITER
XML_STRING_02 = <<DELIMITER
XML_STRING_02 = <<DELIMITER
<biblio>
<entry type="Book">
<language>english</language>
@ -51,129 +51,129 @@ XML_STRING_02 = <<DELIMITER
</biblio>
DELIMITER
# Tobias Reif <tobiasreif@pinkjuice.com>
def test_bad_doctype_Tobias
source = <<-EOF
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/SVG/DTD/svg10.dtd"
[
<!-- <!ENTITY % fast-slow "0 0 .5 1">-->
<!--<!ENTITY % slow-fast ".5 0 1 1">-->
<!ENTITY hover_ani
'<animateTransform attributeName="transform"
type="scale" restart="whenNotActive" values="1;0.96"
dur="0.5s" calcMode="spline" keySplines="0 0 .5 1"
fill="freeze" begin="mouseover"/>
<animateTransform attributeName="transform"
type="scale" restart="whenNotActive" values="0.96;1"
dur="0.5s" calcMode="spline" keySplines=".5 0 1 1"
fill="freeze" begin="mouseover+0.5s"/>'
>
]
>
EOF
doc = REXML::Document.new source
doc.write(out="")
assert(out[/>'>/] != nil, "Couldn't find >'>")
assert(out[/\]>/] != nil, "Couldn't find ]>")
end
# Tobias Reif <tobiasreif@pinkjuice.com>
def test_bad_doctype_Tobias
source = <<-EOF
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/SVG/DTD/svg10.dtd"
[
<!-- <!ENTITY % fast-slow "0 0 .5 1">-->
<!--<!ENTITY % slow-fast ".5 0 1 1">-->
<!ENTITY hover_ani
'<animateTransform attributeName="transform"
type="scale" restart="whenNotActive" values="1;0.96"
dur="0.5s" calcMode="spline" keySplines="0 0 .5 1"
fill="freeze" begin="mouseover"/>
<animateTransform attributeName="transform"
type="scale" restart="whenNotActive" values="0.96;1"
dur="0.5s" calcMode="spline" keySplines=".5 0 1 1"
fill="freeze" begin="mouseover+0.5s"/>'
>
]
>
EOF
doc = REXML::Document.new source
doc.write(out="")
assert(out[/>'>/] != nil, "Couldn't find >'>")
assert(out[/\]>/] != nil, "Couldn't find ]>")
end
# Peter Verhage
def test_namespace_Peter
source = <<-EOF
<?xml version="1.0"?>
<config:myprog-config xmlns:config="http://someurl/program/version">
<!-- main options -->
<config:main>
<config:parameter name="name" value="value"/>
</config:main>
</config:myprog-config>
EOF
doc = REXML::Document.new source
assert_equal "myprog-config", doc.root.name
count = 0
REXML::XPath.each(doc, "x:myprog-config/x:main/x:parameter",
{"x"=>"http://someurl/program/version"}) { |element|
assert_equal "name", element.attributes["name"]
count += 1;
}
assert_equal 1, count
assert_equal "myprog-config", doc.elements["config:myprog-config"].name
end
# Peter Verhage
def test_namespace_Peter
source = <<-EOF
<?xml version="1.0"?>
<config:myprog-config xmlns:config="http://someurl/program/version">
<!-- main options -->
<config:main>
<config:parameter name="name" value="value"/>
</config:main>
</config:myprog-config>
EOF
doc = REXML::Document.new source
assert_equal "myprog-config", doc.root.name
count = 0
REXML::XPath.each(doc, "x:myprog-config/x:main/x:parameter",
{"x"=>"http://someurl/program/version"}) { |element|
assert_equal "name", element.attributes["name"]
count += 1;
}
assert_equal 1, count
assert_equal "myprog-config", doc.elements["config:myprog-config"].name
end
# Tobias Reif <tobiasreif@pinkjuice.com>
def test_complex_xpath_Tobias
source = <<-EOF
<root>
<foo>
<bar style="baz"/>
<blah style="baz"/>
<blam style="baz"/>
</foo>
<wax>
<fudge>
<noodle/>
</fudge>
</wax>
</root>
EOF
# elements that have child elements
# but not grandchildren
# and not children that don't have a style attribute
# and not children that have a unique style attribute
complex_path = "*[* "+
"and not(*/node()) "+
"and not(*[not(@style)]) "+
"and not(*/@style != */@style)]"
doc = REXML::Document.new source
results = REXML::XPath.match( doc.root, complex_path )
assert(results)
assert_equal 1, results.size
assert_equal "foo", results[0].name
end
# Tobias Reif <tobiasreif@pinkjuice.com>
def test_complex_xpath_Tobias
source = <<-EOF
<root>
<foo>
<bar style="baz"/>
<blah style="baz"/>
<blam style="baz"/>
</foo>
<wax>
<fudge>
<noodle/>
</fudge>
</wax>
</root>
EOF
# elements that have child elements
# but not grandchildren
# and not children that don't have a style attribute
# and not children that have a unique style attribute
complex_path = "*[* "+
"and not(*/node()) "+
"and not(*[not(@style)]) "+
"and not(*/@style != */@style)]"
doc = REXML::Document.new source
results = REXML::XPath.match( doc.root, complex_path )
assert(results)
assert_equal 1, results.size
assert_equal "foo", results[0].name
end
# "Chris Morris" <chrismo@charter.net>
def test_extra_newline_on_read_Chris
text = 'test text'
e = REXML::Element.new('Test')
e.add_text(text)
# "Chris Morris" <chrismo@charter.net>
def test_extra_newline_on_read_Chris
text = 'test text'
e = REXML::Element.new('Test')
e.add_text(text)
REXML::Formatters::Default.new.write(e,out="")
doc = REXML::Document.new(out)
outtext = doc.root.text
doc = REXML::Document.new(out)
outtext = doc.root.text
assert_equal(text, outtext)
end
assert_equal(text, outtext)
end
# Tobias Reif <tobiasreif@pinkjuice.com>
def test_other_xpath_Tobias
schema = <<-DELIM
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="rect">
<xs:complexType>
<xs:attribute name="width" type="xs:byte" use="required"/>
<xs:attribute name="height" type="xs:byte" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="svg">
<xs:complexType>
<xs:sequence>
<xs:element ref="rect"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
DELIM
# Tobias Reif <tobiasreif@pinkjuice.com>
def test_other_xpath_Tobias
schema = <<-DELIM
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="rect">
<xs:complexType>
<xs:attribute name="width" type="xs:byte" use="required"/>
<xs:attribute name="height" type="xs:byte" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="svg">
<xs:complexType>
<xs:sequence>
<xs:element ref="rect"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
DELIM
doc = REXML::Document.new schema
doc = REXML::Document.new schema
result = REXML::XPath.first(doc.root, 'xs:element[descendant::xs:element[@ref]]')
assert result
assert_equal "svg", result.attributes['name']
result = REXML::XPath.first(doc, 'element[descendant::element[@ref]]')
assert_nil result
end
result = REXML::XPath.first(doc.root, 'xs:element[descendant::xs:element[@ref]]')
assert result
assert_equal "svg", result.attributes['name']
result = REXML::XPath.first(doc, 'element[descendant::element[@ref]]')
assert_nil result
end
#this first test succeeds, to check if stuff is set up correctly
def test_xpath_01_TobiasReif
@ -228,8 +228,8 @@ DELIMITER
end
def test_umlaut
koln_iso = "K\xf6ln"
koln_utf = "K\xc3\xb6ln"
koln_iso = "K\xf6ln"
koln_utf = "K\xc3\xb6ln"
source_iso = "<?xml version='1.0' encoding='ISO-8859-1'?><test>#{koln_iso}</test>"
source_utf = "<?xml version='1.0' encoding='UTF-8'?><test>#{koln_utf}</test>"
@ -240,16 +240,16 @@ DELIMITER
source_utf.force_encoding('utf-8')
end
doc = REXML::Document.new(source_iso)
assert_equal('ISO-8859-1', doc.xml_decl.encoding)
assert_equal(koln_utf, doc.root.text)
doc.write(out="")
assert_equal(source_iso, out )
doc.xml_decl.encoding = 'UTF-8'
doc.write(out="")
assert_equal(source_utf, out)
doc = REXML::Document.new(source_iso)
assert_equal('ISO-8859-1', doc.xml_decl.encoding)
assert_equal(koln_utf, doc.root.text)
doc.write(out="")
assert_equal(source_iso, out )
doc.xml_decl.encoding = 'UTF-8'
doc.write(out="")
assert_equal(source_utf, out)
doc = Document.new <<-EOF
doc = Document.new <<-EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<intranet>
<position><aktuell datum="01-10-11">Technik</aktuell></position>
@ -265,228 +265,228 @@ Die Technik ist das R
</nebenspalte>
</intranet>
EOF
tn = XPath.first(doc, "//nebenspalte/text()[2]")
expected_iso = "Nützliches von Flashern für Flasher."
tn = XPath.first(doc, "//nebenspalte/text()[2]")
expected_iso = "Nützliches von Flashern für Flasher."
expected_utf = expected_iso.unpack('C*').pack('U*')
if expected_utf.respond_to? :encode
expected_iso.force_encoding("iso-8859-1")
expected_utf.force_encoding(Encoding::UTF_8)
expected_iso.force_encoding("iso-8859-1")
expected_utf.force_encoding(Encoding::UTF_8)
end
assert_equal(expected_utf, tn.to_s.strip)
assert_equal(expected_utf, tn.to_s.strip)
f = REXML::Formatters::Default.new
f.write( tn, Output.new(o = "", "ISO-8859-1") )
assert_equal(expected_iso, o.strip)
assert_equal(expected_iso, o.strip)
doc = Document.new File.new(fixture_path('xmlfile-bug.xml'))
tn = XPath.first(doc, "//nebenspalte/text()[2]")
assert_equal(expected_utf, tn.to_s.strip)
doc = Document.new File.new(fixture_path('xmlfile-bug.xml'))
tn = XPath.first(doc, "//nebenspalte/text()[2]")
assert_equal(expected_utf, tn.to_s.strip)
f.write( tn, Output.new(o = "", "ISO-8859-1") )
assert_equal(expected_iso, o.strip)
assert_equal(expected_iso, o.strip)
end
def test_element_cloning_namespace_Chris
aDoc = REXML::Document.new '<h1 tpl:content="title" xmlns:tpl="1">Dummy title</h1>'
def test_element_cloning_namespace_Chris
aDoc = REXML::Document.new '<h1 tpl:content="title" xmlns:tpl="1">Dummy title</h1>'
anElement = anElement = aDoc.elements[1]
elementAttrPrefix = anElement.attributes.get_attribute('content').prefix
anElement = anElement = aDoc.elements[1]
elementAttrPrefix = anElement.attributes.get_attribute('content').prefix
aClone = anElement.clone
cloneAttrPrefix = aClone.attributes.get_attribute('content').prefix
aClone = anElement.clone
cloneAttrPrefix = aClone.attributes.get_attribute('content').prefix
assert_equal( elementAttrPrefix , cloneAttrPrefix )
end
assert_equal( elementAttrPrefix , cloneAttrPrefix )
end
def test_namespaces_in_attlist_tobias
in_string = File.open(fixture_path('foo.xml'), 'r') do |file|
file.read
end
def test_namespaces_in_attlist_tobias
in_string = File.open(fixture_path('foo.xml'), 'r') do |file|
file.read
end
doc = Document.new in_string
doc = Document.new in_string
assert_nil XPath.first(doc,'//leg')
assert_equal 'http://www.foo.com/human', doc.root.elements[1].namespace
assert_equal 'human leg',
XPath.first(doc, '//x:leg/text()', {'x'=>'http://www.foo.com/human'}).to_s
end
assert_nil XPath.first(doc,'//leg')
assert_equal 'http://www.foo.com/human', doc.root.elements[1].namespace
assert_equal 'human leg',
XPath.first(doc, '//x:leg/text()', {'x'=>'http://www.foo.com/human'}).to_s
end
# Alun ap Rhisiart
def test_less_than_in_element_content
source = File.new(fixture_path('ProductionSupport.xml'))
h = Hash.new
doc = REXML::Document.new source
doc.elements.each("//CommonError") { |el|
h[el.elements['Key'].text] = 'okay'
}
assert(h.include?('MotorInsuranceContract(Object)>>#error:'))
end
# Alun ap Rhisiart
def test_less_than_in_element_content
source = File.new(fixture_path('ProductionSupport.xml'))
h = Hash.new
doc = REXML::Document.new source
doc.elements.each("//CommonError") { |el|
h[el.elements['Key'].text] = 'okay'
}
assert(h.include?('MotorInsuranceContract(Object)>>#error:'))
end
# XPaths provided by Thomas Sawyer
def test_various_xpath
#@doc = REXML::Document.new('<r a="1"><p><c b="2"/></p></r>')
doc = REXML::Document.new('<r a="1"><p><c b="2">3</c></p></r>')
# XPaths provided by Thomas Sawyer
def test_various_xpath
#@doc = REXML::Document.new('<r a="1"><p><c b="2"/></p></r>')
doc = REXML::Document.new('<r a="1"><p><c b="2">3</c></p></r>')
[['/r', REXML::Element],
['/r/p/c', REXML::Element],
['/r/attribute::a', Attribute],
['/r/@a', Attribute],
['/r/attribute::*', Attribute],
['/r/@*', Attribute],
['/r/p/c/attribute::b', Attribute],
['/r/p/c/@b', Attribute],
['/r/p/c/attribute::*', Attribute],
['/r/p/c/@*', Attribute],
['//c/attribute::b', Attribute],
['//c/@b', Attribute],
['//c/attribute::*', Attribute],
['//c/@*', Attribute],
['.//node()', REXML::Node ],
['.//node()[@a]', REXML::Element ],
['.//node()[@a="1"]', REXML::Element ],
['.//node()[@b]', REXML::Element ], # no show, why?
['.//node()[@b="2"]', REXML::Element ]
].each do |xpath,kind|
begin
REXML::XPath.each( doc, xpath ) do |what|
assert_kind_of( kind, what, "\n\nWrong type (#{what.class}) returned for #{xpath} (expected #{kind.name})\n\n" )
end
rescue Exception
puts "PATH WAS: #{xpath}"
raise
end
end
[['/r', REXML::Element],
['/r/p/c', REXML::Element],
['/r/attribute::a', Attribute],
['/r/@a', Attribute],
['/r/attribute::*', Attribute],
['/r/@*', Attribute],
['/r/p/c/attribute::b', Attribute],
['/r/p/c/@b', Attribute],
['/r/p/c/attribute::*', Attribute],
['/r/p/c/@*', Attribute],
['//c/attribute::b', Attribute],
['//c/@b', Attribute],
['//c/attribute::*', Attribute],
['//c/@*', Attribute],
['.//node()', REXML::Node ],
['.//node()[@a]', REXML::Element ],
['.//node()[@a="1"]', REXML::Element ],
['.//node()[@b]', REXML::Element ], # no show, why?
['.//node()[@b="2"]', REXML::Element ]
].each do |xpath,kind|
begin
REXML::XPath.each( doc, xpath ) do |what|
assert_kind_of( kind, what, "\n\nWrong type (#{what.class}) returned for #{xpath} (expected #{kind.name})\n\n" )
end
rescue Exception
puts "PATH WAS: #{xpath}"
raise
end
end
[
['/r', 'attribute::a', Attribute ],
['/r', '@a', Attribute ],
['/r', 'attribute::*', Attribute ],
['/r', '@*', Attribute ],
['/r/p/c', 'attribute::b', Attribute ],
['/r/p/c', '@b', Attribute ],
['/r/p/c', 'attribute::*', Attribute ],
['/r/p/c', '@*', Attribute ]
].each do |nodepath, xpath, kind|
begin
context = REXML::XPath.first(doc, nodepath)
REXML::XPath.each( context, xpath ) do |what|
assert_kind_of kind, what, "Wrong type (#{what.class}) returned for #{xpath} (expected #{kind.name})\n"
end
rescue Exception
puts "PATH WAS: #{xpath}"
raise
end
end
end
[
['/r', 'attribute::a', Attribute ],
['/r', '@a', Attribute ],
['/r', 'attribute::*', Attribute ],
['/r', '@*', Attribute ],
['/r/p/c', 'attribute::b', Attribute ],
['/r/p/c', '@b', Attribute ],
['/r/p/c', 'attribute::*', Attribute ],
['/r/p/c', '@*', Attribute ]
].each do |nodepath, xpath, kind|
begin
context = REXML::XPath.first(doc, nodepath)
REXML::XPath.each( context, xpath ) do |what|
assert_kind_of kind, what, "Wrong type (#{what.class}) returned for #{xpath} (expected #{kind.name})\n"
end
rescue Exception
puts "PATH WAS: #{xpath}"
raise
end
end
end
def test_entities_Holden_Glova
document = <<-EOL
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rubynet [
<!ENTITY rbconfig.MAJOR "1">
<!ENTITY rbconfig.MINOR "7">
<!ENTITY rbconfig.TEENY "2">
<!ENTITY rbconfig.ruby_version "&rbconfig.MAJOR;.&rbconfig.MINOR;">
<!ENTITY rbconfig.arch "i386-freebsd5">
<!ENTITY rbconfig.prefix "/usr/local">
<!ENTITY rbconfig.libdir "&rbconfig.prefix;/lib">
<!ENTITY rbconfig.includedir "&rbconfig.prefix;/include">
<!ENTITY rbconfig.sitedir "&rbconfig.prefix;/lib/ruby/site_ruby">
<!ENTITY rbconfig.sitelibdir "&rbconfig.sitedir;/&rbconfig.ruby_version;">
<!ENTITY rbconfig.sitearchdir "&rbconfig.sitelibdir;/&rbconfig.arch;">
]>
<rubynet>
<pkg version="version1.0">
<files>
<file>
<filename>uga.rb</filename>
<mode>0444</mode>
<path>&rbconfig.libdir;/rexml</path>
<content encoding="xml">... the file here</content>
</file>
<file>
<filename>booga.h</filename>
<mode>0444</mode>
<path>&rbconfig.includedir;</path>
<content encoding="xml">... the file here</content>
</file>
<file>
<filename>foo.so</filename>
<mode>0555</mode>
<path>&rbconfig.sitearchdir;/rexml</path>
<content encoding="mime64">Li4uIHRoZSBmaWxlIGhlcmU=\n</content>
</file>
</files>
</pkg>
</rubynet>
EOL
def test_entities_Holden_Glova
document = <<-EOL
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rubynet [
<!ENTITY rbconfig.MAJOR "1">
<!ENTITY rbconfig.MINOR "7">
<!ENTITY rbconfig.TEENY "2">
<!ENTITY rbconfig.ruby_version "&rbconfig.MAJOR;.&rbconfig.MINOR;">
<!ENTITY rbconfig.arch "i386-freebsd5">
<!ENTITY rbconfig.prefix "/usr/local">
<!ENTITY rbconfig.libdir "&rbconfig.prefix;/lib">
<!ENTITY rbconfig.includedir "&rbconfig.prefix;/include">
<!ENTITY rbconfig.sitedir "&rbconfig.prefix;/lib/ruby/site_ruby">
<!ENTITY rbconfig.sitelibdir "&rbconfig.sitedir;/&rbconfig.ruby_version;">
<!ENTITY rbconfig.sitearchdir "&rbconfig.sitelibdir;/&rbconfig.arch;">
]>
<rubynet>
<pkg version="version1.0">
<files>
<file>
<filename>uga.rb</filename>
<mode>0444</mode>
<path>&rbconfig.libdir;/rexml</path>
<content encoding="xml">... the file here</content>
</file>
<file>
<filename>booga.h</filename>
<mode>0444</mode>
<path>&rbconfig.includedir;</path>
<content encoding="xml">... the file here</content>
</file>
<file>
<filename>foo.so</filename>
<mode>0555</mode>
<path>&rbconfig.sitearchdir;/rexml</path>
<content encoding="mime64">Li4uIHRoZSBmaWxlIGhlcmU=\n</content>
</file>
</files>
</pkg>
</rubynet>
EOL
file_xpath = '/rubynet/pkg/files/file'
file_xpath = '/rubynet/pkg/files/file'
root = REXML::Document.new(document)
root = REXML::Document.new(document)
root.elements.each(file_xpath) do |metadata|
text = metadata.elements['path'].get_text.value
assert text !~ /&rbconfig/, "'#{text}' failed"
end
root.elements.each(file_xpath) do |metadata|
text = metadata.elements['path'].get_text.value
assert text !~ /&rbconfig/, "'#{text}' failed"
end
#Error occurred in test_package_file_opens(TC_PackageInstall):
# ArgumentError:
#illegal access mode &rbconfig.prefix;/lib/rexml
#
#[synack@Evergreen] src $ ruby --version
#ruby 1.6.7 (2002-03-01) [i686-linux-gnu]
#
#It looks like it expanded the first entity, but didn't reparse it for more
#entities. possible bug - or have I mucked this up?
end
#Error occurred in test_package_file_opens(TC_PackageInstall):
# ArgumentError:
#illegal access mode &rbconfig.prefix;/lib/rexml
#
#[synack@Evergreen] src $ ruby --version
#ruby 1.6.7 (2002-03-01) [i686-linux-gnu]
#
#It looks like it expanded the first entity, but didn't reparse it for more
#entities. possible bug - or have I mucked this up?
end
def test_whitespace_after_xml_decl
d = Document.new <<EOL
def test_whitespace_after_xml_decl
d = Document.new <<EOL
<?xml version='1.0'?>
<blo>
<wak>
</wak>
<wak>
</wak>
</blo>
EOL
end
end
def test_external_entity
xp = '//channel/title'
def test_external_entity
xp = '//channel/title'
%w{working.rss broken.rss}.each do |path|
File.open(File.join(fixture_path(path))) do |file|
doc = REXML::Document.new file.readlines.join('')
%w{working.rss broken.rss}.each do |path|
File.open(File.join(fixture_path(path))) do |file|
doc = REXML::Document.new file.readlines.join('')
# check to make sure everything is kosher
assert_equal( doc.root.class, REXML::Element )
assert_equal( doc.root.elements.class, REXML::Elements )
# check to make sure everything is kosher
assert_equal( doc.root.class, REXML::Element )
assert_equal( doc.root.elements.class, REXML::Elements )
# get the title of the feed
assert( doc.root.elements[xp].kind_of?( REXML::Element ) )
end
end
end
# get the title of the feed
assert( doc.root.elements[xp].kind_of?( REXML::Element ) )
end
end
end
def test_maintain_dtd
src = %q{<?xml version="1.0" encoding="UTF-8"?>
def test_maintain_dtd
src = %q{<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ivattacks SYSTEM "../../ivacm.dtd" [
<!ENTITY % extern-packages SYSTEM "../../ivpackages.dtd">
<!ENTITY % extern-packages SYSTEM "../../common-declarations.dtd">
%extern-packages;
%extern-common;
]>}
doc = Document.new( src )
doc.write( out="" )
src = src.tr('"', "'")
out = out.tr('"', "'")
assert_equal( src, out )
end
doc = Document.new( src )
doc.write( out="" )
src = src.tr('"', "'")
out = out.tr('"', "'")
assert_equal( src, out )
end
def test_text_nodes_nomatch
source = "<root><child>test</child></root>"
d = REXML::Document.new( source )
r = REXML::XPath.match( d, %q{/root/child[text()="no-test"]} )
assert_equal( 0, r.size )
end
def test_text_nodes_nomatch
source = "<root><child>test</child></root>"
d = REXML::Document.new( source )
r = REXML::XPath.match( d, %q{/root/child[text()="no-test"]} )
assert_equal( 0, r.size )
end
def test_raw_Terje_Elde
f = REXML::Formatters::Default.new
@ -560,24 +560,24 @@ EOL
=begin
# This is a silly test, and is low priority
def test_namespace_serialization_tobi_reif
doc = Document.new '<doc xmlns:b="http://www.foo.foo">
doc = Document.new '<doc xmlns:b="http://www.foo.foo">
<b:p/>
</doc>'
ns = 'http://www.foo.foo'
ns_declaration={'f'=>ns}
returned = XPath.match(doc,'//f:p',ns_declaration)
# passes:
assert( (returned[0].namespace==ns), 'namespace should be '+ns)
serialized = returned.to_s
serialized_and_parsed = Document.new(serialized)
puts 'serialized: '+serialized
# ... currently brings <b:p/>
# prefix b is undeclared (!)
assert( (serialized_and_parsed.namespace==ns),
'namespace should still be '+ns.inspect+
' and not '+serialized_and_parsed.namespace.inspect)
# ... currently results in a failure:
# 'namespace should still be "http://www.foo.foo" and not ""'
ns = 'http://www.foo.foo'
ns_declaration={'f'=>ns}
returned = XPath.match(doc,'//f:p',ns_declaration)
# passes:
assert( (returned[0].namespace==ns), 'namespace should be '+ns)
serialized = returned.to_s
serialized_and_parsed = Document.new(serialized)
puts 'serialized: '+serialized
# ... currently brings <b:p/>
# prefix b is undeclared (!)
assert( (serialized_and_parsed.namespace==ns),
'namespace should still be '+ns.inspect+
' and not '+serialized_and_parsed.namespace.inspect)
# ... currently results in a failure:
# 'namespace should still be "http://www.foo.foo" and not ""'
end
=end
end

View file

@ -1104,9 +1104,9 @@ EOL
end
def test_transitive
doc = REXML::Document.new( "<a/>")
s = ""
doc.write( s, 0, true )
doc = REXML::Document.new( "<a/>")
s = ""
doc.write( s, 0, true )
end
# This is issue #40
@ -1145,25 +1145,25 @@ EOL
end
def test_ticket_58
doc = REXML::Document.new
doc << REXML::XMLDecl.default
doc << REXML::Element.new("a")
str = ""
doc.write(str)
assert_equal("<a/>", str)
def test_ticket_58
doc = REXML::Document.new
doc << REXML::XMLDecl.default
doc << REXML::Element.new("a")
str = ""
doc.write(str)
assert_equal("<a/>", str)
doc = REXML::Document.new
doc << REXML::XMLDecl.new("1.0", "UTF-8")
doc << REXML::Element.new("a")
str = ""
doc.write(str)
assert_equal("<?xml version='1.0' encoding='UTF-8'?><a/>", str)
end
doc = REXML::Document.new
doc << REXML::XMLDecl.new("1.0", "UTF-8")
doc << REXML::Element.new("a")
str = ""
doc.write(str)
assert_equal("<?xml version='1.0' encoding='UTF-8'?><a/>", str)
end
# Incomplete tags should generate an error
def test_ticket_53
@ -1251,9 +1251,9 @@ EOL
def test_ticket_85
xml = <<ENDXML
<foo>
<bar>
<bob name='jimmy'/>
</bar>
<bar>
<bob name='jimmy'/>
</bar>
</foo>
ENDXML
@ -1276,7 +1276,7 @@ ENDXML
# Output directives should override whitespace directives.
assert_equal( output1, output2 )
# The base case.
# The base case.
d = Document.new(yml)
f.write( d, output3="" )
@ -1286,7 +1286,7 @@ ENDXML
f.write( d, output4="" )
assert_equal( output3.strip, output4.strip )
end
end
def test_ticket_91
source="<root>

View file

@ -2,45 +2,45 @@ require 'test/unit/testcase'
require 'rexml/document'
class ElementsTester < Test::Unit::TestCase
include REXML
def test_elements_accessor
doc = Document.new '<a><b/><c id="1"/><c id="2"/><d/></a>'
assert_equal 'b', doc.root.elements[1].name
assert_equal '1', doc.root.elements['c'].attributes['id']
assert_equal '2', doc.root.elements[2,'c'].attributes['id']
end
include REXML
def test_elements_accessor
doc = Document.new '<a><b/><c id="1"/><c id="2"/><d/></a>'
assert_equal 'b', doc.root.elements[1].name
assert_equal '1', doc.root.elements['c'].attributes['id']
assert_equal '2', doc.root.elements[2,'c'].attributes['id']
end
def test_elements_indexing
doc = Document.new '<a/>'
doc.root.elements[10] = Element.new('b')
assert_equal 'b', doc.root.elements[1].name
doc.root.elements[1] = Element.new('c')
assert_equal 'c', doc.root.elements[1].name
doc.root.elements['c'] = Element.new('d')
assert_equal 'd', doc.root.elements[1].name
end
def test_elements_delete
doc = Document.new '<a><b/><c/><c id="1"/></a>'
block = proc { |str|
out = ''
doc.write out
assert_equal str, out
}
b = doc.root.elements[1]
doc.root.elements.delete b
block.call( "<a><c/><c id='1'/></a>" )
doc.elements.delete("a/c[@id='1']")
block.call( '<a><c/></a>' )
doc.root.elements.delete 1
def test_elements_indexing
doc = Document.new '<a/>'
doc.root.elements[10] = Element.new('b')
assert_equal 'b', doc.root.elements[1].name
doc.root.elements[1] = Element.new('c')
assert_equal 'c', doc.root.elements[1].name
doc.root.elements['c'] = Element.new('d')
assert_equal 'd', doc.root.elements[1].name
end
def test_elements_delete
doc = Document.new '<a><b/><c/><c id="1"/></a>'
block = proc { |str|
out = ''
doc.write out
assert_equal str, out
}
b = doc.root.elements[1]
doc.root.elements.delete b
block.call( "<a><c/><c id='1'/></a>" )
doc.elements.delete("a/c[@id='1']")
block.call( '<a><c/></a>' )
doc.root.elements.delete 1
block.call( '<a/>' )
end
end
def test_elements_delete_all
doc = Document.new '<a><c/><c/><c/><c/></a>'
deleted = doc.elements.delete_all 'a/c'
assert_equal 4, deleted.size
end
def test_elements_delete_all
doc = Document.new '<a><c/><c/><c/><c/></a>'
deleted = doc.elements.delete_all 'a/c'
assert_equal 4, deleted.size
end
def test_ticket_36
doc = Document.new( "<a xmlns:xi='foo'><b><xi:c id='1'/></b><xi:c id='2'/></a>" )
@ -53,55 +53,55 @@ class ElementsTester < Test::Unit::TestCase
assert_equal( 2, deleted.size )
end
def test_elements_add
a = Element.new 'a'
a.elements.add Element.new('b')
assert_equal 'b', a.elements[1].name
a.elements.add 'c'
assert_equal 'c', a.elements[2].name
end
def test_elements_add
a = Element.new 'a'
a.elements.add Element.new('b')
assert_equal 'b', a.elements[1].name
a.elements.add 'c'
assert_equal 'c', a.elements[2].name
end
def test_elements_size
doc = Document.new '<a>sean<b/>elliott<b/>russell<b/></a>'
assert_equal 6, doc.root.size
assert_equal 3, doc.root.elements.size
end
def test_elements_size
doc = Document.new '<a>sean<b/>elliott<b/>russell<b/></a>'
assert_equal 6, doc.root.size
assert_equal 3, doc.root.elements.size
end
def test_elements_each
doc = Document.new '<a><b/><c/><d/>sean<b/><c/><d/></a>'
count = 0
block = proc {|e| count += 1}
doc.root.elements.each(&block)
assert_equal 6, count
count = 0
doc.root.elements.each('b', &block)
assert_equal 2, count
count = 0
doc.root.elements.each('child::node()', &block)
assert_equal 6, count
count = 0
XPath.each(doc.root, 'child::node()', &block)
assert_equal 7, count
end
def test_elements_each
doc = Document.new '<a><b/><c/><d/>sean<b/><c/><d/></a>'
count = 0
block = proc {|e| count += 1}
doc.root.elements.each(&block)
assert_equal 6, count
count = 0
doc.root.elements.each('b', &block)
assert_equal 2, count
count = 0
doc.root.elements.each('child::node()', &block)
assert_equal 6, count
count = 0
XPath.each(doc.root, 'child::node()', &block)
assert_equal 7, count
end
def test_elements_to_a
doc = Document.new '<a>sean<b/>elliott<c/></a>'
assert_equal 2, doc.root.elements.to_a.size
assert_equal 2, doc.root.elements.to_a("child::node()").size
assert_equal 4, XPath.match(doc.root, "child::node()").size
end
def test_elements_collect
doc = Document.new( "<a><b id='1'/><b id='2'/></a>" )
r = doc.elements.collect( "/a/b" ) { |e| e.attributes["id"].to_i }
assert_equal( [1,2], r )
end
def test_elements_inject
doc = Document.new( "<a><b id='1'/><b id='2'/></a>" )
r = doc.elements.inject( "/a/b", 3 ) { |s, e|
s + e.attributes["id"].to_i
}
assert_equal 6, r
end
def test_elements_to_a
doc = Document.new '<a>sean<b/>elliott<c/></a>'
assert_equal 2, doc.root.elements.to_a.size
assert_equal 2, doc.root.elements.to_a("child::node()").size
assert_equal 4, XPath.match(doc.root, "child::node()").size
end
def test_elements_collect
doc = Document.new( "<a><b id='1'/><b id='2'/></a>" )
r = doc.elements.collect( "/a/b" ) { |e| e.attributes["id"].to_i }
assert_equal( [1,2], r )
end
def test_elements_inject
doc = Document.new( "<a><b id='1'/><b id='2'/></a>" )
r = doc.elements.inject( "/a/b", 3 ) { |s, e|
s + e.attributes["id"].to_i
}
assert_equal 6, r
end
end

View file

@ -15,7 +15,7 @@ class EncodingTester < Test::Unit::TestCase
end
# Given an encoded document, try to write out to that encoding
def test_encoded_in_encoded_out
def test_encoded_in_encoded_out
doc = Document.new( @encoded )
doc.write( out="" )
out.force_encoding('binary') if out.respond_to? :force_encoding

View file

@ -6,54 +6,54 @@ require "rexml/parseexception"
=begin
# THIS DOESN'T WORK
begin
require 'iconv'
require 'iconv'
UnixCharsets = open("| iconv -l") do |f|
f.readlines[5..-1].collect { |x| x.sub(/\/\/\n/,"").delete(' ') }
end
UnixCharsets = open("| iconv -l") do |f|
f.readlines[5..-1].collect { |x| x.sub(/\/\/\n/,"").delete(' ') }
end
DATA = <<END
DATA = <<END
<?xml version="1.0" encoding='ENC'?>
<Ruby xmlns="http://www.ruby-lang.org/ruby/1.8">
</Ruby>
END
class IConvTester < Test::Unit::TestCase
class IConvTester < Test::Unit::TestCase
def test_iconv
broken_encodings = 0
UnixCharsets.each do |enc|
begin
puts "Testing encoding #{enc}"
data = DATA.dup
data[/ENC/] = enc
REXML::Document.new(data).root
rescue REXML::ParseException => e
broken_encodings += 1
fail "Encoding #{enc} does not work with REXML: #{e.message}"
rescue Errno::EINVAL => e
broken_encodings += 1
fail "Encoding #{enc} does not work with REXML: #{e.message}"
rescue ArgumentError => e
broken_encodings += 1
fail "Encoding #{enc} does not work with REXML: #{e.message}"
rescue
broken_encodings += 1
fail "Encoding #{enc} does not work with REXML: #{$!.message}"
end
end
def test_iconv
broken_encodings = 0
UnixCharsets.each do |enc|
begin
puts "Testing encoding #{enc}"
data = DATA.dup
data[/ENC/] = enc
REXML::Document.new(data).root
rescue REXML::ParseException => e
broken_encodings += 1
fail "Encoding #{enc} does not work with REXML: #{e.message}"
rescue Errno::EINVAL => e
broken_encodings += 1
fail "Encoding #{enc} does not work with REXML: #{e.message}"
rescue ArgumentError => e
broken_encodings += 1
fail "Encoding #{enc} does not work with REXML: #{e.message}"
rescue
broken_encodings += 1
fail "Encoding #{enc} does not work with REXML: #{$!.message}"
end
end
if broken_encodings > 0
fail "There were #{broken_encodings} encoding failures out of #{UnixCharsets.size} plus some REXML internal encodings"
else
fail "There were no encoding failures"
end
if broken_encodings > 0
fail "There were #{broken_encodings} encoding failures out of #{UnixCharsets.size} plus some REXML internal encodings"
else
fail "There were no encoding failures"
end
puts "Full list of registered encodings in REXML:"
puts REXML::Encoding::ENCODING_CLAIMS.values.join(', ')
end
end
puts "Full list of registered encodings in REXML:"
puts REXML::Encoding::ENCODING_CLAIMS.values.join(', ')
end
end
rescue LoadError
end
=end

View file

@ -4,145 +4,145 @@ require 'rexml/entity'
require 'rexml/source'
class EntityTester < Test::Unit::TestCase
def test_parse_general_decl
simple = "<!ENTITY foo 'bar'>"
simple =~ /#{REXML::Entity::GEDECL}/
assert $&
assert_equal simple, $&
REXML::Entity::ENTITYDECL =~ simple
assert REXML::Entity::matches?(simple)
match = REXML::Entity::ENTITYDECL.match(simple)
assert_equal 'foo', match[1]
assert_equal "'bar'", match[2]
def test_parse_general_decl
simple = "<!ENTITY foo 'bar'>"
simple =~ /#{REXML::Entity::GEDECL}/
assert $&
assert_equal simple, $&
REXML::Entity::ENTITYDECL =~ simple
assert REXML::Entity::matches?(simple)
match = REXML::Entity::ENTITYDECL.match(simple)
assert_equal 'foo', match[1]
assert_equal "'bar'", match[2]
simple = '<!ENTITY Pub-Status
"This is a pre-release of the specification.">'
assert REXML::Entity::matches?(simple)
match = REXML::Entity::ENTITYDECL.match(simple)
assert_equal 'Pub-Status', match[1]
assert_equal '"This is a pre-release of the specification."', match[2]
simple = '<!ENTITY Pub-Status
"This is a pre-release of the specification.">'
assert REXML::Entity::matches?(simple)
match = REXML::Entity::ENTITYDECL.match(simple)
assert_equal 'Pub-Status', match[1]
assert_equal '"This is a pre-release of the specification."', match[2]
txt = '"This is a
pre-release of <the> specification."'
simple = "<!ENTITY Pub-Status
#{txt}>"
assert REXML::Entity::matches?(simple)
match = REXML::Entity::ENTITYDECL.match(simple)
assert_equal 'Pub-Status', match[1]
assert_equal txt, match[2]
end
txt = '"This is a
pre-release of <the> specification."'
simple = "<!ENTITY Pub-Status
#{txt}>"
assert REXML::Entity::matches?(simple)
match = REXML::Entity::ENTITYDECL.match(simple)
assert_equal 'Pub-Status', match[1]
assert_equal txt, match[2]
end
def test_parse_external_decl
zero = '<!ENTITY open-hatch SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml" >'
one = '<!ENTITY open-hatch
SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">'
two = '<!ENTITY open-hatch
PUBLIC "-//Textuality//TEXT Standard open-hatch boilerplate//EN"
"http://www.textuality.com/boilerplate/OpenHatch.xml">'
three = '<!ENTITY hatch-pic
SYSTEM "../grafix/OpenHatch.gif"
NDATA gif >'
assert REXML::Entity::matches?(zero)
assert REXML::Entity::matches?(one)
assert REXML::Entity::matches?(two)
assert REXML::Entity::matches?(three)
end
def test_parse_external_decl
zero = '<!ENTITY open-hatch SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml" >'
one = '<!ENTITY open-hatch
SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">'
two = '<!ENTITY open-hatch
PUBLIC "-//Textuality//TEXT Standard open-hatch boilerplate//EN"
"http://www.textuality.com/boilerplate/OpenHatch.xml">'
three = '<!ENTITY hatch-pic
SYSTEM "../grafix/OpenHatch.gif"
NDATA gif >'
assert REXML::Entity::matches?(zero)
assert REXML::Entity::matches?(one)
assert REXML::Entity::matches?(two)
assert REXML::Entity::matches?(three)
end
def test_parse_entity
one = %q{<!ENTITY % YN '"Yes"'>}
two = %q{<!ENTITY WhatHeSaid "He said %YN;">}
assert REXML::Entity::matches?(one)
assert REXML::Entity::matches?(two)
end
def test_parse_entity
one = %q{<!ENTITY % YN '"Yes"'>}
two = %q{<!ENTITY WhatHeSaid "He said %YN;">}
assert REXML::Entity::matches?(one)
assert REXML::Entity::matches?(two)
end
def test_constructor
one = [ %q{<!ENTITY % YN '"Yes"'>},
%q{<!ENTITY % YN2 "Yes">},
%q{<!ENTITY WhatHeSaid "He said %YN;">},
'<!ENTITY open-hatch
SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">',
'<!ENTITY open-hatch2
PUBLIC "-//Textuality//TEXT Standard open-hatch boilerplate//EN"
"http://www.textuality.com/boilerplate/OpenHatch.xml">',
'<!ENTITY hatch-pic
SYSTEM "../grafix/OpenHatch.gif"
NDATA gif>' ]
source = %q{<!DOCTYPE foo [
<!ENTITY % YN '"Yes"'>
<!ENTITY % YN2 "Yes">
<!ENTITY WhatHeSaid "He said %YN;">
<!ENTITY open-hatch
SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">
<!ENTITY open-hatch2
PUBLIC "-//Textuality//TEXT Standard open-hatch boilerplate//EN"
"http://www.textuality.com/boilerplate/OpenHatch.xml">
<!ENTITY hatch-pic
SYSTEM "../grafix/OpenHatch.gif"
NDATA gif>
]>}
def test_constructor
one = [ %q{<!ENTITY % YN '"Yes"'>},
%q{<!ENTITY % YN2 "Yes">},
%q{<!ENTITY WhatHeSaid "He said %YN;">},
'<!ENTITY open-hatch
SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">',
'<!ENTITY open-hatch2
PUBLIC "-//Textuality//TEXT Standard open-hatch boilerplate//EN"
"http://www.textuality.com/boilerplate/OpenHatch.xml">',
'<!ENTITY hatch-pic
SYSTEM "../grafix/OpenHatch.gif"
NDATA gif>' ]
source = %q{<!DOCTYPE foo [
<!ENTITY % YN '"Yes"'>
<!ENTITY % YN2 "Yes">
<!ENTITY WhatHeSaid "He said %YN;">
<!ENTITY open-hatch
SYSTEM "http://www.textuality.com/boilerplate/OpenHatch.xml">
<!ENTITY open-hatch2
PUBLIC "-//Textuality//TEXT Standard open-hatch boilerplate//EN"
"http://www.textuality.com/boilerplate/OpenHatch.xml">
<!ENTITY hatch-pic
SYSTEM "../grafix/OpenHatch.gif"
NDATA gif>
]>}
d = REXML::Document.new( source )
dt = d.doctype
c = 0
dt.each do |child|
if child.kind_of? REXML::Entity
str = one[c].tr("\r\n\t", ' ').squeeze(" ")
assert_equal str, child.to_s
c+=1
end
end
end
d = REXML::Document.new( source )
dt = d.doctype
c = 0
dt.each do |child|
if child.kind_of? REXML::Entity
str = one[c].tr("\r\n\t", ' ').squeeze(" ")
assert_equal str, child.to_s
c+=1
end
end
end
def test_replace_entities
source = "<!DOCTYPE blah [\n<!ENTITY foo \"bar\">\n]><a>&foo;</a>"
doc = REXML::Document.new(source)
assert_equal 'bar', doc.root.text
out = ''
doc.write out
assert_equal source, out
end
def test_replace_entities
source = "<!DOCTYPE blah [\n<!ENTITY foo \"bar\">\n]><a>&foo;</a>"
doc = REXML::Document.new(source)
assert_equal 'bar', doc.root.text
out = ''
doc.write out
assert_equal source, out
end
def test_raw
source = '<!DOCTYPE foo [
def test_raw
source = '<!DOCTYPE foo [
<!ENTITY ent "replace">
]><a>replace &ent;</a>'
doc = REXML::Document.new( source, {:raw=>:all})
assert_equal('replace &ent;', doc.root.get_text.to_s)
assert_equal(source, doc.to_s)
end
doc = REXML::Document.new( source, {:raw=>:all})
assert_equal('replace &ent;', doc.root.get_text.to_s)
assert_equal(source, doc.to_s)
end
def test_lazy_evaluation
source = '<!DOCTYPE foo [
def test_lazy_evaluation
source = '<!DOCTYPE foo [
<!ENTITY ent "replace">
]><a>replace &ent;</a>'
doc = REXML::Document.new( source )
assert_equal(source, doc.to_s)
assert_equal("replace replace", doc.root.text)
assert_equal(source, doc.to_s)
end
doc = REXML::Document.new( source )
assert_equal(source, doc.to_s)
assert_equal("replace replace", doc.root.text)
assert_equal(source, doc.to_s)
end
# Contributed (not only test, but bug fix!!) by Kouhei Sutou
def test_entity_replacement
source = %q{<!DOCTYPE foo [
<!ENTITY % YN '"Yes"'>
<!ENTITY WhatHeSaid "He said %YN;">]>
<a>&WhatHeSaid;</a>}
# Contributed (not only test, but bug fix!!) by Kouhei Sutou
def test_entity_replacement
source = %q{<!DOCTYPE foo [
<!ENTITY % YN '"Yes"'>
<!ENTITY WhatHeSaid "He said %YN;">]>
<a>&WhatHeSaid;</a>}
d = REXML::Document.new( source )
dt = d.doctype
assert_equal( '"Yes"', dt.entities[ "YN" ].value )
assert_equal( 'He said "Yes"', dt.entities[ "WhatHeSaid" ].value )
assert_equal( 'He said "Yes"', d.elements[1].text )
end
d = REXML::Document.new( source )
dt = d.doctype
assert_equal( '"Yes"', dt.entities[ "YN" ].value )
assert_equal( 'He said "Yes"', dt.entities[ "WhatHeSaid" ].value )
assert_equal( 'He said "Yes"', d.elements[1].text )
end
# More unit tests from Kouhei. I looove users who give me unit tests.
def test_entity_insertions
assert_equal("&amp;", REXML::Text.new("&amp;", false, nil, true).to_s)
#assert_equal("&", REXML::Text.new("&amp;", false, false).to_s)
end
# More unit tests from Kouhei. I looove users who give me unit tests.
def test_entity_insertions
assert_equal("&amp;", REXML::Text.new("&amp;", false, nil, true).to_s)
#assert_equal("&", REXML::Text.new("&amp;", false, false).to_s)
end
def test_single_pass_unnormalization # ticket 123
assert_equal '&amp;&', REXML::Text::unnormalize('&#38;amp;&amp;')
end
def test_single_pass_unnormalization # ticket 123
assert_equal '&amp;&', REXML::Text::unnormalize('&#38;amp;&amp;')
end
end

View file

@ -3,209 +3,209 @@ require "test/unit/testcase"
require "rexml/document"
class FunctionsTester < Test::Unit::TestCase
include REXML
def test_functions
# trivial text() test
# confuse-a-function
source = "<a>more <b id='1'/><b id='2'>dumb</b><b id='3'/><c/> text</a>"
doc = Document.new source
res = ""
XPath::each(doc.root, "text()") {|val| res << val.to_s}
assert_equal "more text", res
include REXML
def test_functions
# trivial text() test
# confuse-a-function
source = "<a>more <b id='1'/><b id='2'>dumb</b><b id='3'/><c/> text</a>"
doc = Document.new source
res = ""
XPath::each(doc.root, "text()") {|val| res << val.to_s}
assert_equal "more text", res
res = XPath::first(doc.root, "b[last()]")
assert_equal '3', res.attributes['id']
res = XPath::first(doc.root, "b[position()=2]")
assert_equal '2', res.attributes['id']
res = XPath::first(doc.root, "*[name()='c']")
assert_equal "c", res.name
end
res = XPath::first(doc.root, "b[last()]")
assert_equal '3', res.attributes['id']
res = XPath::first(doc.root, "b[position()=2]")
assert_equal '2', res.attributes['id']
res = XPath::first(doc.root, "*[name()='c']")
assert_equal "c", res.name
end
# Contributed by Mike Stok
def test_starts_with
source = <<-EOF
<foo>
<a href="mailto:a@b.c">a@b.c</a>
<a href="http://www.foo.com">http://www.foo.com</a>
</foo>
EOF
doc = Document.new source
mailtos = doc.elements.to_a("//a[starts-with(@href, 'mailto:')]")
assert_equal 1, mailtos.size
assert_equal "mailto:a@b.c", mailtos[0].attributes['href']
# Contributed by Mike Stok
def test_starts_with
source = <<-EOF
<foo>
<a href="mailto:a@b.c">a@b.c</a>
<a href="http://www.foo.com">http://www.foo.com</a>
</foo>
EOF
doc = Document.new source
mailtos = doc.elements.to_a("//a[starts-with(@href, 'mailto:')]")
assert_equal 1, mailtos.size
assert_equal "mailto:a@b.c", mailtos[0].attributes['href']
ailtos = doc.elements.to_a("//a[starts-with(@href, 'ailto:')]")
assert_equal 0, ailtos.size
end
ailtos = doc.elements.to_a("//a[starts-with(@href, 'ailto:')]")
assert_equal 0, ailtos.size
end
def test_string_length
doc = Document.new <<-EOF
<AAA>
<Q/>
<SSSS/>
<BB/>
<CCC/>
<DDDDDDDD/>
<EEEE/>
</AAA>
EOF
assert doc, "create doc"
doc = Document.new <<-EOF
<AAA>
<Q/>
<SSSS/>
<BB/>
<CCC/>
<DDDDDDDD/>
<EEEE/>
</AAA>
EOF
assert doc, "create doc"
set = doc.elements.to_a("//*[string-length(name()) = 3]")
assert_equal 2, set.size, "nodes with names length = 3"
set = doc.elements.to_a("//*[string-length(name()) = 3]")
assert_equal 2, set.size, "nodes with names length = 3"
set = doc.elements.to_a("//*[string-length(name()) < 3]")
assert_equal 2, set.size, "nodes with names length < 3"
set = doc.elements.to_a("//*[string-length(name()) < 3]")
assert_equal 2, set.size, "nodes with names length < 3"
set = doc.elements.to_a("//*[string-length(name()) > 3]")
assert_equal 3, set.size, "nodes with names length > 3"
end
set = doc.elements.to_a("//*[string-length(name()) > 3]")
assert_equal 3, set.size, "nodes with names length > 3"
end
# Test provided by Mike Stok
def test_contains
source = <<-EOF
<foo>
<a href="mailto:a@b.c">a@b.c</a>
<a href="http://www.foo.com">http://www.foo.com</a>
</foo>
EOF
doc = Document.new source
# Test provided by Mike Stok
def test_contains
source = <<-EOF
<foo>
<a href="mailto:a@b.c">a@b.c</a>
<a href="http://www.foo.com">http://www.foo.com</a>
</foo>
EOF
doc = Document.new source
[['o', 2], ['foo', 1], ['bar', 0]].each { |test|
search, expected = test
set = doc.elements.to_a("//a[contains(@href, '#{search}')]")
assert_equal expected, set.size
}
end
[['o', 2], ['foo', 1], ['bar', 0]].each { |test|
search, expected = test
set = doc.elements.to_a("//a[contains(@href, '#{search}')]")
assert_equal expected, set.size
}
end
# Mike Stok and Sean Russell
def test_substring
# examples from http://www.w3.org/TR/xpath#function-substring
doc = Document.new('<test string="12345" />')
# Mike Stok and Sean Russell
def test_substring
# examples from http://www.w3.org/TR/xpath#function-substring
doc = Document.new('<test string="12345" />')
d = Document.new("<a b='1'/>")
#puts XPath.first(d, 'node()[0 + 1]')
#d = Document.new("<a b='1'/>")
#puts XPath.first(d, 'a[0 mod 0]')
[ [1.5, 2.6, '234'],
[0, 3, '12'],
[0, '0 div 0', ''],
[1, '0 div 0', ''],
['-42', '1 div 0', '12345'],
d = Document.new("<a b='1'/>")
#puts XPath.first(d, 'node()[0 + 1]')
#d = Document.new("<a b='1'/>")
#puts XPath.first(d, 'a[0 mod 0]')
[ [1.5, 2.6, '234'],
[0, 3, '12'],
[0, '0 div 0', ''],
[1, '0 div 0', ''],
['-42', '1 div 0', '12345'],
['-1 div 0', '1 div 0', '']
].each { |start, length, expected|
set = doc.elements.to_a("//test[substring(@string, #{start}, #{length}) = '#{expected}']")
assert_equal 1, set.size, "#{start}, #{length}, '#{expected}'"
}
end
].each { |start, length, expected|
set = doc.elements.to_a("//test[substring(@string, #{start}, #{length}) = '#{expected}']")
assert_equal 1, set.size, "#{start}, #{length}, '#{expected}'"
}
end
def test_substring_angrez
testString = REXML::Functions::substring_after("helloworld","hello")
assert_equal( 'world', testString )
end
def test_translate
source = <<-EOF
<doc>
<case name='w3c one' result='BAr' /> <!-- w3c -->
<case name='w3c two' result='AAA' /> <!-- w3c -->
<case name='alchemy' result="gold" /> <!-- mike -->
<case name='vbxml one' result='A Space Odyssey' />
<case name='vbxml two' result='AbCdEf' />
</doc>
EOF
def test_translate
source = <<-EOF
<doc>
<case name='w3c one' result='BAr' /> <!-- w3c -->
<case name='w3c two' result='AAA' /> <!-- w3c -->
<case name='alchemy' result="gold" /> <!-- mike -->
<case name='vbxml one' result='A Space Odyssey' />
<case name='vbxml two' result='AbCdEf' />
</doc>
EOF
doc = Document.new(source)
doc = Document.new(source)
[ ['bar', 'abc', 'ABC', 'w3c one'],
['--aaa--','abc-','ABC', 'w3c two'],
['lead', 'dear language', 'doll groover', 'alchemy'],
['A Space Odissei', 'i', 'y', 'vbxml one'],
['abcdefg', 'aceg', 'ACE', 'vbxml two'],
].each { |arg1, arg2, arg3, name|
translate = "translate('#{arg1}', '#{arg2}', '#{arg3}')"
set = doc.elements.to_a("//case[@result = #{translate}]")
assert_equal 1, set.size, translate
assert_equal name, set[0].attributes['name']
}
end
[ ['bar', 'abc', 'ABC', 'w3c one'],
['--aaa--','abc-','ABC', 'w3c two'],
['lead', 'dear language', 'doll groover', 'alchemy'],
['A Space Odissei', 'i', 'y', 'vbxml one'],
['abcdefg', 'aceg', 'ACE', 'vbxml two'],
].each { |arg1, arg2, arg3, name|
translate = "translate('#{arg1}', '#{arg2}', '#{arg3}')"
set = doc.elements.to_a("//case[@result = #{translate}]")
assert_equal 1, set.size, translate
assert_equal name, set[0].attributes['name']
}
end
def test_name
d = REXML::Document.new("<a xmlns:x='foo'><b/><x:b/></a>")
assert_equal 1, d.root.elements.to_a('*[name() = "b"]').size
assert_equal 1, d.elements.to_a('//*[name() = "x:b"]').size
end
def test_name
d = REXML::Document.new("<a xmlns:x='foo'><b/><x:b/></a>")
assert_equal 1, d.root.elements.to_a('*[name() = "b"]').size
assert_equal 1, d.elements.to_a('//*[name() = "x:b"]').size
end
def test_local_name
d = REXML::Document.new("<a xmlns:x='foo'><b/><x:b/></a>")
assert_equal 2, d.root.elements.to_a('*[local_name() = "b"]').size
assert_equal 2, d.elements.to_a('//*[local_name() = "b"]').size
end
def test_local_name
d = REXML::Document.new("<a xmlns:x='foo'><b/><x:b/></a>")
assert_equal 2, d.root.elements.to_a('*[local_name() = "b"]').size
assert_equal 2, d.elements.to_a('//*[local_name() = "b"]').size
end
def test_substring2
doc = Document.new('<test string="12345" />')
assert_equal(1,doc.elements.to_a("//test[substring(@string,2)='2345']").size)
end
def test_substring2
doc = Document.new('<test string="12345" />')
assert_equal(1,doc.elements.to_a("//test[substring(@string,2)='2345']").size)
end
# Submitted by Kouhei
def test_floor_ceiling_round
source = "<a><b id='1'/><b id='2'/><b id='3'/></a>"
doc = REXML::Document.new(source)
# Submitted by Kouhei
def test_floor_ceiling_round
source = "<a><b id='1'/><b id='2'/><b id='3'/></a>"
doc = REXML::Document.new(source)
id_1 = doc.elements["/a/b[@id='1']"]
id_2 = doc.elements["/a/b[@id='2']"]
id_3 = doc.elements["/a/b[@id='3']"]
good = {
"floor" => [[], [id_1], [id_2], [id_3]],
"ceiling" => [[id_1], [id_2], [id_3], []],
"round" => [[id_1], [id_2], [id_3], []]
}
good.each do |key, value|
(0..3).each do |i|
xpath = "//b[number(@id) = #{key}(#{i+0.5})]"
assert_equal(value[i], REXML::XPath.match(doc, xpath))
end
end
id_1 = doc.elements["/a/b[@id='1']"]
id_2 = doc.elements["/a/b[@id='2']"]
id_3 = doc.elements["/a/b[@id='3']"]
good = {
"floor" => [[], [id_1], [id_2], [id_3]],
"ceiling" => [[id_1], [id_2], [id_3], []],
"round" => [[id_1], [id_2], [id_3], []]
}
good.each do |key, value|
(0..3).each do |i|
xpath = "//b[number(@id) = #{key}(#{i+0.5})]"
assert_equal(value[i], REXML::XPath.match(doc, xpath))
end
end
good["round"] = [[], [id_1], [id_2], [id_3]]
good.each do |key, value|
(0..3).each do |i|
xpath = "//b[number(@id) = #{key}(#{i+0.4})]"
assert_equal(value[i], REXML::XPath.match(doc, xpath))
end
end
end
good["round"] = [[], [id_1], [id_2], [id_3]]
good.each do |key, value|
(0..3).each do |i|
xpath = "//b[number(@id) = #{key}(#{i+0.4})]"
assert_equal(value[i], REXML::XPath.match(doc, xpath))
end
end
end
# Submitted by Kou
def test_lang
d = Document.new(<<-XML)
<a xml:lang="en">
<b xml:lang="ja">
<c xml:lang="fr"/>
<d/>
<e xml:lang="ja-JP"/>
<f xml:lang="en-US"/>
</b>
</a>
XML
# Submitted by Kou
def test_lang
d = Document.new(<<-XML)
<a xml:lang="en">
<b xml:lang="ja">
<c xml:lang="fr"/>
<d/>
<e xml:lang="ja-JP"/>
<f xml:lang="en-US"/>
</b>
</a>
XML
assert_equal(1, d.elements.to_a("//*[lang('fr')]").size)
assert_equal(3, d.elements.to_a("//*[lang('ja')]").size)
assert_equal(2, d.elements.to_a("//*[lang('en')]").size)
assert_equal(1, d.elements.to_a("//*[lang('en-us')]").size)
assert_equal(1, d.elements.to_a("//*[lang('fr')]").size)
assert_equal(3, d.elements.to_a("//*[lang('ja')]").size)
assert_equal(2, d.elements.to_a("//*[lang('en')]").size)
assert_equal(1, d.elements.to_a("//*[lang('en-us')]").size)
d = Document.new(<<-XML)
<root>
<para xml:lang="en"/>
<div xml:lang="en"><para/></div>
<para xml:lang="EN"/>
<para xml:lang="en-us"/>
</root>
XML
d = Document.new(<<-XML)
<root>
<para xml:lang="en"/>
<div xml:lang="en"><para/></div>
<para xml:lang="EN"/>
<para xml:lang="en-us"/>
</root>
XML
assert_equal(5, d.elements.to_a("//*[lang('en')]").size)
end
assert_equal(5, d.elements.to_a("//*[lang('en')]").size)
end
def test_ticket_60
document = REXML::Document.new("<a><b>A</b><b>1</b></a>")

View file

@ -28,5 +28,5 @@ class TC_Rexml_Functions_Number < Test::Unit::TestCase
# telem = REXML::Element.new("elem")
# telem.text="9.13E12"
# assert_equal(9.13E12, REXML::Functions::number(telem))
#end
#end
end

View file

@ -47,16 +47,16 @@ class JaxenTester < Test::Unit::TestCase
# processes a tests/document/context node
def handleContext( testDoc, ctxElement)
testCtx = XPath.match( testDoc, ctxElement.attributes["select"] )[0]
namespaces = {}
if testCtx.class == Element
testCtx.prefixes.each { |pre| handleNamespace( testCtx, pre, namespaces ) }
end
variables = {}
XPath.each( ctxElement, "@*[namespace-uri() = 'http://jaxen.org/test-harness/var']") { |attrib| handleVariable(testCtx, variables, attrib) }
XPath.each( ctxElement, "valueOf") { |e| handleValueOf(testCtx, variables, namespaces, e) }
XPath.each( ctxElement, "test[not(@exception) or (@exception != 'true') ]") { |e| handleNominalTest(testCtx,variables, namespaces, e) }
XPath.each( ctxElement, "test[@exception = 'true']") { |e| handleExceptionalTest(testCtx,variables, namespaces, e) }
testCtx = XPath.match( testDoc, ctxElement.attributes["select"] )[0]
namespaces = {}
if testCtx.class == Element
testCtx.prefixes.each { |pre| handleNamespace( testCtx, pre, namespaces ) }
end
variables = {}
XPath.each( ctxElement, "@*[namespace-uri() = 'http://jaxen.org/test-harness/var']") { |attrib| handleVariable(testCtx, variables, attrib) }
XPath.each( ctxElement, "valueOf") { |e| handleValueOf(testCtx, variables, namespaces, e) }
XPath.each( ctxElement, "test[not(@exception) or (@exception != 'true') ]") { |e| handleNominalTest(testCtx,variables, namespaces, e) }
XPath.each( ctxElement, "test[@exception = 'true']") { |e| handleExceptionalTest(testCtx,variables, namespaces, e) }
end
# processes a tests/document/context/valueOf or tests/document/context/test/valueOf node
@ -99,28 +99,28 @@ class JaxenTester < Test::Unit::TestCase
# processes a tests/document/context/test node ( where @exception is true )
def handleExceptionalTest(ctx, variables, namespaces, testElement)
assert_raise( Exception ) {
XPath.match( ctx, testElement.attributes["select"], namespaces, variables )
XPath.match( ctx, testElement.attributes["select"], namespaces, variables )
}
end
# processes a tests/document node
def handleDocument(docElement)
puts "- Processing document: #{docElement.attributes['url']}"
testFile = File.new( docElement.attributes["url"] )
testDoc = Document.new testFile
XPath.each( docElement, "context") { |e| handleContext(testDoc, e) }
puts "- Processing document: #{docElement.attributes['url']}"
testFile = File.new( docElement.attributes["url"] )
testDoc = Document.new testFile
XPath.each( docElement, "context") { |e| handleContext(testDoc, e) }
end
# processes a variable definition in a namespace like <test var:foo="bar">
def handleVariable( ctx, variables, attrib )
puts "--- Found attribute: #{attrib.name}"
variables[attrib.name] = attrib.value
puts "--- Found attribute: #{attrib.name}"
variables[attrib.name] = attrib.value
end
# processes a namespace definition like <test xmlns:foo="fiz:bang:bam">
def handleNamespace( ctx, prefix, namespaces )
puts "--- Found namespace: #{prefix}"
namespaces[prefix] = ctx.namespaces[prefix]
puts "--- Found namespace: #{prefix}"
namespaces[prefix] = ctx.namespaces[prefix]
end
end

View file

@ -6,97 +6,97 @@ class LightTester < Test::Unit::TestCase
include REXMLTestUtils
include REXML::Light
def test_parse_large
parser = REXML::Parsers::LightParser.new(fixture_path("documentation.xml"))
root = parser.parse
end
# FIXME INCOMPLETE
# This is because the light API is not yet ready to be used to produce
# trees.
def test_parse_large
parser = REXML::Parsers::LightParser.new(fixture_path("documentation.xml"))
root = parser.parse
end
# FIXME INCOMPLETE
# This is because the light API is not yet ready to be used to produce
# trees.
=begin
def test_add_element
doc = Node.new
foo = doc.add_element( 'foo' )
assert_equal( "foo", foo.name )
end
def test_add_attribute
foo = Node.new( "a" )
foo["attr"] = "bar"
assert_equal( "bar", foo["attr"] )
end
def test_add_element
doc = Node.new
foo = doc.add_element( 'foo' )
assert_equal( "foo", foo.name )
end
def test_add_attribute
foo = Node.new( "a" )
foo["attr"] = "bar"
assert_equal( "bar", foo["attr"] )
end
def test_write_document
r = make_small_document
assert_equal( "<a><b/><c/></a>", r.to_s )
end
def test_write_document
r = make_small_document
assert_equal( "<a><b/><c/></a>", r.to_s )
end
def test_add_attribute_under_namespace
foo = Node.new("a")
foo["attr", "a"] = "1"
foo["attr", "b"] = "2"
foo["attr"] = "3"
assert_equal( '1', foo['attr', 'a'] )
assert_equal( '2', foo['attr', 'b'] )
assert_equal( '3', foo['attr'] )
end
def test_add_attribute_under_namespace
foo = Node.new("a")
foo["attr", "a"] = "1"
foo["attr", "b"] = "2"
foo["attr"] = "3"
assert_equal( '1', foo['attr', 'a'] )
assert_equal( '2', foo['attr', 'b'] )
assert_equal( '3', foo['attr'] )
end
def test_change_namespace_of_element
foo = Node.new
assert_equal( '', foo.namespace )
foo.namespace = 'a'
assert_equal( 'a', foo.namespace )
end
def test_change_namespace_of_element
foo = Node.new
assert_equal( '', foo.namespace )
foo.namespace = 'a'
assert_equal( 'a', foo.namespace )
end
def test_access_child_elements
foo = make_small_document
assert_equal( 1, foo.size )
a = foo[0]
assert_equal( 2, a.size )
assert_equal( 'b', a[0].name )
assert_equal( 'c', a[1].name )
end
def test_access_child_elements
foo = make_small_document
assert_equal( 1, foo.size )
a = foo[0]
assert_equal( 2, a.size )
assert_equal( 'b', a[0].name )
assert_equal( 'c', a[1].name )
end
def test_itterate_over_children
foo = make_small_document
ctr = 0
foo[0].each { ctr += 1 }
assert_equal( 2, ctr )
end
def test_itterate_over_children
foo = make_small_document
ctr = 0
foo[0].each { ctr += 1 }
assert_equal( 2, ctr )
end
def test_add_text
foo = Node.new( "a" )
foo.add_text( "Sean" )
sean = foo[0]
assert( sean.node_type == :text )
end
def test_add_text
foo = Node.new( "a" )
foo.add_text( "Sean" )
sean = foo[0]
assert( sean.node_type == :text )
end
def test_add_instruction
foo = Node.new( "a" )
foo.add_instruction( "target", "value" )
assert( foo[0].node_type == :processing_instruction )
end
def test_add_instruction
foo = Node.new( "a" )
foo.add_instruction( "target", "value" )
assert( foo[0].node_type == :processing_instruction )
end
def test_add_comment
foo = Node.new( "a" )
foo.add_comment( "target", "value" )
assert( foo[0].node_type == :comment )
end
def test_add_comment
foo = Node.new( "a" )
foo.add_comment( "target", "value" )
assert( foo[0].node_type == :comment )
end
def test_get_root
foo = Node.new( 'a' )
10.times { foo = foo.add_element('b') }
assert_equals( 'b', foo.name )
assert_equals( 'a', foo.root.name )
end
def test_get_root
foo = Node.new( 'a' )
10.times { foo = foo.add_element('b') }
assert_equals( 'b', foo.name )
assert_equals( 'a', foo.root.name )
end
def make_small_document
r = Node.new
a = r.add_element( "a" )
a.add_element( 'b' )
a.add_element( 'c' )
r
end
def make_small_document
r = Node.new
a = r.add_element( "a" )
a.add_element( 'b' )
a.add_element( 'c' )
r
end
=end
end

View file

@ -3,10 +3,10 @@ require 'rexml/parsers/lightparser'
class LightParserTester < Test::Unit::TestCase
include REXMLTestUtils
include REXML
def test_parsing
f = File.new(fixture_path("documentation.xml"))
parser = REXML::Parsers::LightParser.new( f )
root = parser.parse
end
include REXML
def test_parsing
f = File.new(fixture_path("documentation.xml"))
parser = REXML::Parsers::LightParser.new( f )
root = parser.parse
end
end

View file

@ -7,96 +7,96 @@ require 'rexml/streamlistener'
class BaseTester < Test::Unit::TestCase
include REXMLTestUtils
def test_empty
return unless defined? @listener
# Empty.
t1 = %Q{<string></string>}
assert_equal( "", @listener.parse( t1 ),
"Empty" )
end
def test_empty
return unless defined? @listener
# Empty.
t1 = %Q{<string></string>}
assert_equal( "", @listener.parse( t1 ),
"Empty" )
end
def test_space
return unless defined? @listener
# Space.
t2 = %Q{<string> </string>}
assert_equal( " ", @listener.parse( t2 ),
"Space" )
end
def test_space
return unless defined? @listener
# Space.
t2 = %Q{<string> </string>}
assert_equal( " ", @listener.parse( t2 ),
"Space" )
end
def test_whitespace
return unless defined? @listener
# Whitespaces.
t3 = %Q{<string>RE\n \t \n \t XML</string>}
assert_equal( "RE\n \t \n \t XML", @listener.parse( t3 ),
"Whitespaces" )
end
def test_whitespace
return unless defined? @listener
# Whitespaces.
t3 = %Q{<string>RE\n \t \n \t XML</string>}
assert_equal( "RE\n \t \n \t XML", @listener.parse( t3 ),
"Whitespaces" )
end
def test_leading_trailing_whitespace
return unless defined? @listener
# Leading and trailing whitespaces.
t4 = %Q{<string> REXML </string>}
assert_equal( " REXML ", @listener.parse( t4 ),
"Leading and trailing whitespaces" )
end
def test_leading_trailing_whitespace
return unless defined? @listener
# Leading and trailing whitespaces.
t4 = %Q{<string> REXML </string>}
assert_equal( " REXML ", @listener.parse( t4 ),
"Leading and trailing whitespaces" )
end
def test_entity_reference
return unless defined? @listener
# Entity reference.
t5 = %Q{<string>&lt;&gt;&amp;lt;&amp;gt;</string>}
assert_equal( "<>&lt;&gt;", @listener.parse( t5 ),
"Entity reference" )
end
def test_entity_reference
return unless defined? @listener
# Entity reference.
t5 = %Q{<string>&lt;&gt;&amp;lt;&amp;gt;</string>}
assert_equal( "<>&lt;&gt;", @listener.parse( t5 ),
"Entity reference" )
end
def test_character_reference
return unless defined? @listener
# Character reference.
t6 = %Q{<string>&#xd;</string>}
assert_equal( "\r", @listener.parse( t6 ),
"Character reference." )
end
def test_character_reference
return unless defined? @listener
# Character reference.
t6 = %Q{<string>&#xd;</string>}
assert_equal( "\r", @listener.parse( t6 ),
"Character reference." )
end
def test_cr
return unless defined? @listener
# CR.
t7 = %Q{<string> \r\n \r \n </string>}
assert_equal( " \n \n \n ".unpack("C*").inspect,
@listener.parse( t7 ).unpack("C*").inspect, "CR" )
end
def test_cr
return unless defined? @listener
# CR.
t7 = %Q{<string> \r\n \r \n </string>}
assert_equal( " \n \n \n ".unpack("C*").inspect,
@listener.parse( t7 ).unpack("C*").inspect, "CR" )
end
# The accent bug, and the code that exibits the bug, was contributed by
# Guilhem Vellut
class AccentListener
def tag_start(name,attributes)
#p name
#p attributes
end
def tag_end(name)
#p "/"+name
end
def xmldecl(a,b,c)
#puts "#{a} #{b} #{c}"
end
def text(tx)
#p tx
end
end
# The accent bug, and the code that exibits the bug, was contributed by
# Guilhem Vellut
class AccentListener
def tag_start(name,attributes)
#p name
#p attributes
end
def tag_end(name)
#p "/"+name
end
def xmldecl(a,b,c)
#puts "#{a} #{b} #{c}"
end
def text(tx)
#p tx
end
end
def test_accents
source = '<?xml version="1.0" encoding="ISO-8859-1"?>
def test_accents
source = '<?xml version="1.0" encoding="ISO-8859-1"?>
<g>
<f a="é" />
</g>'
doc = REXML::Document.new( source )
a = doc.elements['/g/f'].attribute('a')
doc = REXML::Document.new( source )
a = doc.elements['/g/f'].attribute('a')
if a.value.respond_to? :force_encoding
a.value.force_encoding('binary')
end
assert_equal( 'é', a.value)
doc = REXML::Document.parse_stream(
File::new(fixture_path("stream_accents.xml")),
AccentListener::new
)
end
assert_equal( 'é', a.value)
doc = REXML::Document.parse_stream(
File::new(fixture_path("stream_accents.xml")),
AccentListener::new
)
end
end
class MyREXMLListener
@ -118,12 +118,12 @@ class MyREXMLListener
end
class REXMLTester < BaseTester
def setup
@listener = MyREXMLListener.new
end
def setup
@listener = MyREXMLListener.new
end
def test_character_reference_2
t6 = %Q{<string>&#xd;</string>}
assert_equal( t6.strip, REXML::Document.new(t6).to_s )
end
def test_character_reference_2
t6 = %Q{<string>&#xd;</string>}
assert_equal( t6.strip, REXML::Document.new(t6).to_s )
end
end

View file

@ -2,6 +2,20 @@ require 'test/unit'
require 'rexml/document'
class OrderTester < Test::Unit::TestCase
DOC = <<END
<paper>
<title>Remove this element and figs order differently</title>
<figure src="fig1"/>
<figure src="fig2"/>
<p>Para of text</p>
<p>Remove this and figs order differently</p>
<section>
<figure src="fig3"/>
</section>
<figure src="fig4"/>
</paper>
END
def initialize n
@doc = REXML::Document.new(DOC)
@figs = REXML::XPath.match(@doc,'//figure')
@ -21,18 +35,3 @@ class OrderTester < Test::Unit::TestCase
assert_equal 'fig4', @figs[3].attributes['src']
end
end
DOC = <<END
<paper>
<title>Remove this element and figs order differently</title>
<figure src="fig1"/>
<figure src="fig2"/>
<p>Para of text</p>
<p>Remove this and figs order differently</p>
<section>
<figure src="fig3"/>
</section>
<figure src="fig4"/>
</paper>
END

View file

@ -2,8 +2,6 @@
require 'test/unit'
require 'rexml/document'
p [REXML::VERSION, RUBY_VERSION, RUBY_RELEASE_DATE]
# daz - for report by Dan Kohn in:
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/156328
class XPathTesterDd < Test::Unit::TestCase

View file

@ -32,7 +32,7 @@ class StreamTester < Test::Unit::TestCase
<!DOCTYPE foo [
<!ENTITY ent "replace">
<!ATTLIST a
xmlns:human CDATA #FIXED "http://www.foo.com/human">
xmlns:human CDATA #FIXED "http://www.foo.com/human">
<!ELEMENT bar (#PCDATA)>
<!NOTATION n1 PUBLIC "-//HM//NOTATION TEST1//EN" 'urn:x-henrikmartensson.org:test5'>
]>

File diff suppressed because it is too large Load diff

View file

@ -10,39 +10,39 @@ class TestRexmlXpathAttributeQuery < Test::Unit::TestCase
# xmlstr1 and xmlstr2 only differ in the second line - namespaces in the root element
@@xmlstr1 = '<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:gCal="http://schemas.google.com/gCal/2005">
<id>http://www.google.com/calendar/feeds/me%40gmail.com</id>
<entry>
<id>http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com</id>
<published>2007-05-16T13:42:27.942Z</published>
<updated>2007-05-15T03:29:28.000Z</updated>
<title type="text">My Calendar</title>
<link rel="alternate" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/private/full"/>
<link rel="http://schemas.google.com/acl/2007#accessControlList" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/acl/full"/>
<link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com"/>
<author>
<name>Me</name>
<email>me@gmail.com</email>
</author>
</entry>
<id>http://www.google.com/calendar/feeds/me%40gmail.com</id>
<entry>
<id>http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com</id>
<published>2007-05-16T13:42:27.942Z</published>
<updated>2007-05-15T03:29:28.000Z</updated>
<title type="text">My Calendar</title>
<link rel="alternate" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/private/full"/>
<link rel="http://schemas.google.com/acl/2007#accessControlList" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/acl/full"/>
<link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com"/>
<author>
<name>Me</name>
<email>me@gmail.com</email>
</author>
</entry>
</feed>'
@@xmlstr2 = '<?xml version="1.0" encoding="UTF-8"?>
<feed>
<id>http://www.google.com/calendar/feeds/me%40gmail.com</id>
<entry>
<id>http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com</id>
<published>2007-05-16T13:42:27.942Z</published>
<updated>2007-05-15T03:29:28.000Z</updated>
<title type="text">My Calendar</title>
<link rel="alternate" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/private/full"/>
<link rel="http://schemas.google.com/acl/2007#accessControlList" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/acl/full"/>
<link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com"/>
<author>
<name>Me</name>
<email>me@gmail.com</email>
</author>
</entry>
<id>http://www.google.com/calendar/feeds/me%40gmail.com</id>
<entry>
<id>http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com</id>
<published>2007-05-16T13:42:27.942Z</published>
<updated>2007-05-15T03:29:28.000Z</updated>
<title type="text">My Calendar</title>
<link rel="alternate" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/private/full"/>
<link rel="http://schemas.google.com/acl/2007#accessControlList" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/acl/full"/>
<link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com"/>
<author>
<name>Me</name>
<email>me@gmail.com</email>
</author>
</entry>
</feed>'
# Fails

View file

@ -1,10 +1,9 @@
require "test/unit/testcase"
require "test/unit/ui/console/testrunner"
require "rexml/document"
class XPathAxesTester < Test::Unit::TestCase
include REXML
SOURCE = <<-EOF
include REXML
SOURCE = <<-EOF
<a id='1'>
<e id='2'>
<f id='3'/>
@ -15,9 +14,9 @@ class XPathAxesTester < Test::Unit::TestCase
</a>
EOF
def setup
@@doc = Document.new(SOURCE) unless defined? @@doc
end
def setup
@@doc = Document.new(SOURCE) unless defined? @@doc
end
def test_preceding_sibling_axis
context = XPath.first(@@doc,"/a/e/f[last()]")
@ -37,6 +36,3 @@ class XPathAxesTester < Test::Unit::TestCase
end
end
#Test::Unit::UI::Console::TestRunner.run(XPathAxesTester.suite)

View file

@ -4,7 +4,7 @@ require "rexml/xpath"
require "rexml/parsers/xpathparser"
class XPathPredicateTester < Test::Unit::TestCase
include REXML
include REXML
SRC=<<-EOL
<article>
<section role="subdivision" id="1">

View file

@ -53,8 +53,8 @@ class XpathTestCase < Test::Unit::TestCase
# why isn't the text's parent node2?
# Also look at Comment, etc.
assert_same(node2, textnode.parent)
comment = REXML::Comment.new('Test comment', node2)
assert_same(node2, comment.parent)
comment = REXML::Comment.new('Test comment', node2)
assert_same(node2, comment.parent)
end
def test_ancestors
@ -62,7 +62,7 @@ class XpathTestCase < Test::Unit::TestCase
node2 = REXML::Element.new('b', node1)
textnode = REXML::Text.new('test', false, node2)
#textnode.parent = node2 # should be unnecessary
assert_same node2, textnode.parent
assert_same node2, textnode.parent
nodes = @doc.get_elements('//b/ancestor::*')
assert_equal(1, nodes.size, "<b> has one element ancestor")
nodes = @doc.get_elements('//b/ancestor::node()')