From 2a15d25a5941301b5be6bc29eb4ac97d5aafe3dc Mon Sep 17 00:00:00 2001 From: kou Date: Fri, 17 Sep 2010 13:46:56 +0000 Subject: * test/rexml/: untabify. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29285 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 4 + test/rexml/test_attributes.rb | 288 ++++---- test/rexml/test_attributes_mixin.rb | 4 +- test/rexml/test_contrib.rb | 698 +++++++++--------- test/rexml/test_core.rb | 52 +- test/rexml/test_elements.rb | 168 ++--- test/rexml/test_encoding.rb | 2 +- test/rexml/test_encoding_2.rb | 80 +-- test/rexml/test_entity.rb | 248 +++---- test/rexml/test_functions.rb | 388 +++++----- test/rexml/test_functions_number.rb | 2 +- test/rexml/test_jaxen.rb | 38 +- test/rexml/test_light.rb | 162 ++--- test/rexml/test_lightparser.rb | 12 +- test/rexml/test_listener.rb | 184 ++--- test/rexml/test_martin_fowler.rb | 29 +- test/rexml/test_preceding_sibling.rb | 2 - test/rexml/test_stream.rb | 2 +- test/rexml/test_xpath.rb | 1146 +++++++++++++++--------------- test/rexml/test_xpath_attribute_query.rb | 56 +- test/rexml/test_xpath_msw.rb | 14 +- test/rexml/test_xpath_pred.rb | 2 +- test/rexml/test_xpathtext.rb | 6 +- 23 files changed, 1791 insertions(+), 1796 deletions(-) diff --git a/ChangeLog b/ChangeLog index dfbf78badc..fcda202d2f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Fri Sep 17 22:46:02 2010 Kouhei Sutou + + * test/rexml/: untabify. + Fri Sep 17 22:29:56 2010 Kouhei Sutou * test/rexml/: fix fixture data path. All REXML tests are worked. diff --git a/test/rexml/test_attributes.rb b/test/rexml/test_attributes.rb index 56b45576aa..51a7774b0f 100644 --- a/test/rexml/test_attributes.rb +++ b/test/rexml/test_attributes.rb @@ -2,152 +2,152 @@ require 'test/unit/testcase' require 'rexml/document' class AttributesTester < Test::Unit::TestCase - include REXML - def test_accessor - doc = Document.new("") - 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('') - 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('') - 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('') - 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("") - assert_equal 4, doc.root.attributes.length - end - - def test_setter - doc = Document.new("") - 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("") - 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("") - prefixes = doc.root.attributes.prefixes - assert_equal 2, prefixes.size - assert_equal 0, (prefixes - ['x', 'y']).size - end - - # Contributed by Mike Stok + include REXML + def test_accessor + doc = Document.new("") + 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('') + 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('') + 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('') + 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("") + assert_equal 4, doc.root.attributes.length + end + + def test_setter + doc = Document.new("") + 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("") + 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("") + 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##) - s = doc.to_s - assert(s =~ /h1='1'2''/) - 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 - - - - XML - end - - REXML::Document.new("") - 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 + s = doc.to_s + assert(s =~ /h1='1'2''/) + 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 + + + + XML + end + + REXML::Document.new("") + 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) diff --git a/test/rexml/test_attributes_mixin.rb b/test/rexml/test_attributes_mixin.rb index 44ab4f0d89..50899d0607 100644 --- a/test/rexml/test_attributes_mixin.rb +++ b/test/rexml/test_attributes_mixin.rb @@ -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 diff --git a/test/rexml/test_contrib.rb b/test/rexml/test_contrib.rb index 54ec64299e..785169bdd0 100644 --- a/test/rexml/test_contrib.rb +++ b/test/rexml/test_contrib.rb @@ -8,9 +8,9 @@ require "rexml/formatters/default" class ContribTester < Test::Unit::TestCase include REXMLTestUtils - include REXML + include REXML -XML_STRING_01 = < @@ -31,7 +31,7 @@ XML_STRING_01 = < DELIMITER -XML_STRING_02 = < english @@ -51,129 +51,129 @@ XML_STRING_02 = < DELIMITER - # Tobias Reif - def test_bad_doctype_Tobias - source = <<-EOF - --> - - - ' - > - ] - > - 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 - - - - - - - - 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 - def test_complex_xpath_Tobias - source = <<-EOF - - - - - - - - - - - - - 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" - def test_extra_newline_on_read_Chris - text = 'test text' - e = REXML::Element.new('Test') - e.add_text(text) + # Tobias Reif + def test_bad_doctype_Tobias + source = <<-EOF + --> + + + ' + > + ] + > + 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 + + + + + + + + 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 + def test_complex_xpath_Tobias + source = <<-EOF + + + + + + + + + + + + + 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" + 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 - - assert_equal(text, outtext) - end - - # Tobias Reif - def test_other_xpath_Tobias - schema = <<-DELIM - - - - - - - - - - - - - - - - DELIM - - 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 + doc = REXML::Document.new(out) + outtext = doc.root.text + + assert_equal(text, outtext) + end + + # Tobias Reif + def test_other_xpath_Tobias + schema = <<-DELIM + + + + + + + + + + + + + + + + DELIM + + 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 #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 = "#{koln_iso}" source_utf = "#{koln_utf}" @@ -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 Technik @@ -265,228 +265,228 @@ Die Technik ist das R 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 '

Dummy title

' - - anElement = anElement = aDoc.elements[1] - elementAttrPrefix = anElement.attributes.get_attribute('content').prefix - - aClone = anElement.clone - cloneAttrPrefix = aClone.attributes.get_attribute('content').prefix - - 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 - - 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 - - # 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('

') - doc = REXML::Document.new('

3

') - - [['/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 - - def test_entities_Holden_Glova - document = <<-EOL - - - - - - - - - - - - - ]> - - - - - uga.rb - 0444 - &rbconfig.libdir;/rexml - ... the file here - - - booga.h - 0444 - &rbconfig.includedir; - ... the file here - - - foo.so - 0555 - &rbconfig.sitearchdir;/rexml - Li4uIHRoZSBmaWxlIGhlcmU=\n - - - - - EOL - - file_xpath = '/rubynet/pkg/files/file' - - 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 - - #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 <Dummy title' + + anElement = anElement = aDoc.elements[1] + elementAttrPrefix = anElement.attributes.get_attribute('content').prefix + + aClone = anElement.clone + cloneAttrPrefix = aClone.attributes.get_attribute('content').prefix + + 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 + + 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 + + # 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('

') + doc = REXML::Document.new('

3

') + + [['/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 + + def test_entities_Holden_Glova + document = <<-EOL + + + + + + + + + + + + + ]> + + + + + uga.rb + 0444 + &rbconfig.libdir;/rexml + ... the file here + + + booga.h + 0444 + &rbconfig.includedir; + ... the file here + + + foo.so + 0555 + &rbconfig.sitearchdir;/rexml + Li4uIHRoZSBmaWxlIGhlcmU=\n + + + + + EOL + + file_xpath = '/rubynet/pkg/files/file' + + 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 + + #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 - 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{ + def test_maintain_dtd + src = %q{ %extern-packages; %extern-common; ]>} - doc = Document.new( src ) - doc.write( out="" ) - src = src.tr('"', "'") - out = out.tr('"', "'") - assert_equal( src, out ) - end - - def test_text_nodes_nomatch - source = "test" - d = REXML::Document.new( source ) - r = REXML::XPath.match( d, %q{/root/child[text()="no-test"]} ) - assert_equal( 0, r.size ) - 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 = "test" + 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 = Document.new ' ' - 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 - # 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 + # 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 diff --git a/test/rexml/test_core.rb b/test/rexml/test_core.rb index 4fd5b07c99..79c6dd8487 100644 --- a/test/rexml/test_core.rb +++ b/test/rexml/test_core.rb @@ -1104,9 +1104,9 @@ EOL end def test_transitive - doc = REXML::Document.new( "") - s = "" - doc.write( s, 0, true ) + doc = REXML::Document.new( "") + 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("", str) + def test_ticket_58 + doc = REXML::Document.new + doc << REXML::XMLDecl.default + doc << REXML::Element.new("a") + + str = "" + doc.write(str) + + assert_equal("", str) - doc = REXML::Document.new - doc << REXML::XMLDecl.new("1.0", "UTF-8") - doc << REXML::Element.new("a") - - str = "" - doc.write(str) - - assert_equal("", 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("", str) + end # Incomplete tags should generate an error def test_ticket_53 @@ -1251,9 +1251,9 @@ EOL def test_ticket_85 xml = < - - - + + + 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=" diff --git a/test/rexml/test_elements.rb b/test/rexml/test_elements.rb index e140214f0a..210004a1c0 100644 --- a/test/rexml/test_elements.rb +++ b/test/rexml/test_elements.rb @@ -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 '' - 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 '' + 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 '' - 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 '' - block = proc { |str| - out = '' - doc.write out - assert_equal str, out - } - b = doc.root.elements[1] - doc.root.elements.delete b - block.call( "" ) - doc.elements.delete("a/c[@id='1']") - block.call( '' ) - doc.root.elements.delete 1 + def test_elements_indexing + doc = Document.new '' + 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 '' + block = proc { |str| + out = '' + doc.write out + assert_equal str, out + } + b = doc.root.elements[1] + doc.root.elements.delete b + block.call( "" ) + doc.elements.delete("a/c[@id='1']") + block.call( '' ) + doc.root.elements.delete 1 block.call( '' ) - end + end - def test_elements_delete_all - doc = Document.new '' - deleted = doc.elements.delete_all 'a/c' - assert_equal 4, deleted.size - end + def test_elements_delete_all + doc = Document.new '' + deleted = doc.elements.delete_all 'a/c' + assert_equal 4, deleted.size + end def test_ticket_36 doc = Document.new( "" ) @@ -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 'seanelliottrussell' - assert_equal 6, doc.root.size - assert_equal 3, doc.root.elements.size - end + def test_elements_size + doc = Document.new 'seanelliottrussell' + assert_equal 6, doc.root.size + assert_equal 3, doc.root.elements.size + end - def test_elements_each - doc = Document.new 'sean' - 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 'sean' + 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 'seanelliott' - 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( "" ) - 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( "" ) - 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 'seanelliott' + 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( "" ) + 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( "" ) + r = doc.elements.inject( "/a/b", 3 ) { |s, e| + s + e.attributes["id"].to_i + } + assert_equal 6, r + end end diff --git a/test/rexml/test_encoding.rb b/test/rexml/test_encoding.rb index 19896ea8a8..23e4802927 100644 --- a/test/rexml/test_encoding.rb +++ b/test/rexml/test_encoding.rb @@ -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 diff --git a/test/rexml/test_encoding_2.rb b/test/rexml/test_encoding_2.rb index 5dbe46801b..2e845b58a0 100644 --- a/test/rexml/test_encoding_2.rb +++ b/test/rexml/test_encoding_2.rb @@ -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 - 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 - - 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 + 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 + + 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 rescue LoadError end =end diff --git a/test/rexml/test_entity.rb b/test/rexml/test_entity.rb index 3895cdce31..3bf310a4ca 100644 --- a/test/rexml/test_entity.rb +++ b/test/rexml/test_entity.rb @@ -4,145 +4,145 @@ require 'rexml/entity' require 'rexml/source' class EntityTester < Test::Unit::TestCase - def test_parse_general_decl - simple = "" - 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 = "" + 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 = '' - 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 = '' + 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 specification."' - simple = "" - 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 specification."' + simple = "" + 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 = '' - one = '' - two = '' - three = '' - 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 = '' + one = '' + two = '' + three = '' + 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{} - two = %q{} - assert REXML::Entity::matches?(one) - assert REXML::Entity::matches?(two) - end + def test_parse_entity + one = %q{} + two = %q{} + assert REXML::Entity::matches?(one) + assert REXML::Entity::matches?(two) + end - def test_constructor - one = [ %q{}, - %q{}, - %q{}, - '', - '', - '' ] - source = %q{ - - - - - - ]>} + def test_constructor + one = [ %q{}, + %q{}, + %q{}, + '', + '', + '' ] + source = %q{ + + + + + + ]>} - 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 = "\n]>&foo;" - 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 = "\n]>&foo;" + doc = REXML::Document.new(source) + assert_equal 'bar', doc.root.text + out = '' + doc.write out + assert_equal source, out + end - def test_raw - source = ' ]>replace &ent;' - 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 = ' ]>replace &ent;' - 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{ - ]> - &WhatHeSaid;} + # Contributed (not only test, but bug fix!!) by Kouhei Sutou + def test_entity_replacement + source = %q{ + ]> + &WhatHeSaid;} - 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("&", REXML::Text.new("&", false, nil, true).to_s) - #assert_equal("&", REXML::Text.new("&", false, false).to_s) - end + # More unit tests from Kouhei. I looove users who give me unit tests. + def test_entity_insertions + assert_equal("&", REXML::Text.new("&", false, nil, true).to_s) + #assert_equal("&", REXML::Text.new("&", false, false).to_s) + end - def test_single_pass_unnormalization # ticket 123 - assert_equal '&&', REXML::Text::unnormalize('&amp;&') - end + def test_single_pass_unnormalization # ticket 123 + assert_equal '&&', REXML::Text::unnormalize('&amp;&') + end end diff --git a/test/rexml/test_functions.rb b/test/rexml/test_functions.rb index eac36d5278..e0f8a67fc1 100644 --- a/test/rexml/test_functions.rb +++ b/test/rexml/test_functions.rb @@ -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 = "more dumb text" - 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 - - # Contributed by Mike Stok - def test_starts_with - source = <<-EOF - - a@b.c - http://www.foo.com - - 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 + include REXML + def test_functions + # trivial text() test + # confuse-a-function + source = "more dumb text" + 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 + + # Contributed by Mike Stok + def test_starts_with + source = <<-EOF + + a@b.c + http://www.foo.com + + 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 def test_string_length - doc = Document.new <<-EOF - - - - - - - - - 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 3, set.size, "nodes with names length > 3" - end - - # Test provided by Mike Stok - def test_contains - source = <<-EOF - - a@b.c - http://www.foo.com - - 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 - - # Mike Stok and Sean Russell - def test_substring - # examples from http://www.w3.org/TR/xpath#function-substring - doc = Document.new('') - - d = Document.new("") - #puts XPath.first(d, 'node()[0 + 1]') - #d = Document.new("") - #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'], + doc = Document.new <<-EOF + + + + + + + + + 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 3, set.size, "nodes with names length > 3" + end + + # Test provided by Mike Stok + def test_contains + source = <<-EOF + + a@b.c + http://www.foo.com + + 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 + + # Mike Stok and Sean Russell + def test_substring + # examples from http://www.w3.org/TR/xpath#function-substring + doc = Document.new('') + + d = Document.new("") + #puts XPath.first(d, 'node()[0 + 1]') + #d = Document.new("") + #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 - - - - - - - - EOF - - 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 - - def test_name - d = REXML::Document.new("") - 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("") - 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('') - assert_equal(1,doc.elements.to_a("//test[substring(@string,2)='2345']").size) - end - - # Submitted by Kouhei - def test_floor_ceiling_round - source = "" - 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 - - 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) - - - - - - - - - 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) - - d = Document.new(<<-XML) - - -
- - -
- XML - - assert_equal(5, d.elements.to_a("//*[lang('en')]").size) - end + def test_translate + source = <<-EOF + + + + + + + + EOF + + 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 + + def test_name + d = REXML::Document.new("") + 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("") + 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('') + assert_equal(1,doc.elements.to_a("//test[substring(@string,2)='2345']").size) + end + + # Submitted by Kouhei + def test_floor_ceiling_round + source = "" + 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 + + 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) + + + + + + + + + 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) + + d = Document.new(<<-XML) + + +
+ + +
+ XML + + assert_equal(5, d.elements.to_a("//*[lang('en')]").size) + end def test_ticket_60 document = REXML::Document.new("A1") diff --git a/test/rexml/test_functions_number.rb b/test/rexml/test_functions_number.rb index e569ce4cb0..849b60edb8 100644 --- a/test/rexml/test_functions_number.rb +++ b/test/rexml/test_functions_number.rb @@ -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 diff --git a/test/rexml/test_jaxen.rb b/test/rexml/test_jaxen.rb index 0d5396264b..96c0c7bf48 100644 --- a/test/rexml/test_jaxen.rb +++ b/test/rexml/test_jaxen.rb @@ -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 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 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 diff --git a/test/rexml/test_light.rb b/test/rexml/test_light.rb index 8db830147d..7ccf61298c 100644 --- a/test/rexml/test_light.rb +++ b/test/rexml/test_light.rb @@ -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( "", r.to_s ) - end + def test_write_document + r = make_small_document + assert_equal( "", 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 diff --git a/test/rexml/test_lightparser.rb b/test/rexml/test_lightparser.rb index e0d44231c6..ee33dc08fd 100644 --- a/test/rexml/test_lightparser.rb +++ b/test/rexml/test_lightparser.rb @@ -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 diff --git a/test/rexml/test_listener.rb b/test/rexml/test_listener.rb index c9b49d9abb..f2cc4b7c35 100644 --- a/test/rexml/test_listener.rb +++ b/test/rexml/test_listener.rb @@ -7,96 +7,96 @@ require 'rexml/streamlistener' class BaseTester < Test::Unit::TestCase include REXMLTestUtils - def test_empty - return unless defined? @listener - # Empty. - t1 = %Q{} - assert_equal( "", @listener.parse( t1 ), - "Empty" ) - end - - def test_space - return unless defined? @listener - # Space. - t2 = %Q{ } - assert_equal( " ", @listener.parse( t2 ), - "Space" ) - end - - def test_whitespace - return unless defined? @listener - # Whitespaces. - t3 = %Q{RE\n \t \n \t XML} - 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{ REXML } - assert_equal( " REXML ", @listener.parse( t4 ), - "Leading and trailing whitespaces" ) - end - - def test_entity_reference - return unless defined? @listener - # Entity reference. - t5 = %Q{<>&lt;&gt;} - assert_equal( "<><>", @listener.parse( t5 ), - "Entity reference" ) - end - - def test_character_reference - return unless defined? @listener - # Character reference. - t6 = %Q{ } - assert_equal( "\r", @listener.parse( t6 ), - "Character reference." ) - end - - def test_cr - return unless defined? @listener - # CR. - t7 = %Q{ \r\n \r \n } - 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 - - def test_accents - source = ' + def test_empty + return unless defined? @listener + # Empty. + t1 = %Q{} + assert_equal( "", @listener.parse( t1 ), + "Empty" ) + end + + def test_space + return unless defined? @listener + # Space. + t2 = %Q{ } + assert_equal( " ", @listener.parse( t2 ), + "Space" ) + end + + def test_whitespace + return unless defined? @listener + # Whitespaces. + t3 = %Q{RE\n \t \n \t XML} + 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{ REXML } + assert_equal( " REXML ", @listener.parse( t4 ), + "Leading and trailing whitespaces" ) + end + + def test_entity_reference + return unless defined? @listener + # Entity reference. + t5 = %Q{<>&lt;&gt;} + assert_equal( "<><>", @listener.parse( t5 ), + "Entity reference" ) + end + + def test_character_reference + return unless defined? @listener + # Character reference. + t6 = %Q{ } + assert_equal( "\r", @listener.parse( t6 ), + "Character reference." ) + end + + def test_cr + return unless defined? @listener + # CR. + t7 = %Q{ \r\n \r \n } + 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 + + def test_accents + source = ' ' - 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 test_character_reference_2 - t6 = %Q{ } - assert_equal( t6.strip, REXML::Document.new(t6).to_s ) - end + def setup + @listener = MyREXMLListener.new + end + + def test_character_reference_2 + t6 = %Q{ } + assert_equal( t6.strip, REXML::Document.new(t6).to_s ) + end end diff --git a/test/rexml/test_martin_fowler.rb b/test/rexml/test_martin_fowler.rb index bdbf487ed4..1b29c3df88 100644 --- a/test/rexml/test_martin_fowler.rb +++ b/test/rexml/test_martin_fowler.rb @@ -2,6 +2,20 @@ require 'test/unit' require 'rexml/document' class OrderTester < Test::Unit::TestCase + DOC = < +Remove this element and figs order differently +
+
+

Para of text

+

Remove this and figs order differently

+
+
+
+
+ +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 = < -Remove this element and figs order differently -
-
-

Para of text

-

Remove this and figs order differently

-
-
-
-
- -END diff --git a/test/rexml/test_preceding_sibling.rb b/test/rexml/test_preceding_sibling.rb index c892b9400e..632e4dad85 100644 --- a/test/rexml/test_preceding_sibling.rb +++ b/test/rexml/test_preceding_sibling.rb @@ -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 diff --git a/test/rexml/test_stream.rb b/test/rexml/test_stream.rb index 4e3f0f7327..2d9c177008 100644 --- a/test/rexml/test_stream.rb +++ b/test/rexml/test_stream.rb @@ -32,7 +32,7 @@ class StreamTester < Test::Unit::TestCase + xmlns:human CDATA #FIXED "http://www.foo.com/human"> ]> diff --git a/test/rexml/test_xpath.rb b/test/rexml/test_xpath.rb index 119d4c2ab5..f3a27cf154 100644 --- a/test/rexml/test_xpath.rb +++ b/test/rexml/test_xpath.rb @@ -4,8 +4,8 @@ require "rexml/document" class XPathTester < Test::Unit::TestCase include REXMLTestUtils - include REXML - SOURCE = <<-EOF + include REXML + SOURCE = <<-EOF @@ -34,370 +34,370 @@ class XPathTester < Test::Unit::TestCase EOF - JENI_TENNISON = <<-EOJT - - - - - - - - - - - - - - - - - - - - - - - - EOJT - - def setup - @@doc = Document.new(SOURCE) unless defined? @@doc - @@jeni = Document.new( JENI_TENNISON ) unless defined? @@jeni - end - - def each_test( element, xpath ) - count = 0 - XPath::each( element, xpath ) { |child| - count += 1 - yield child if block_given? - } - count - end - - def test_descendant - doc = Document.new("") - p = XPath.match( doc, "//c" ) - assert_equal( 2, p.size ) - p = XPath.first( @@doc, "//p" ) - assert_equal "p", p.name - c = each_test( @@doc, "//c" ) { |child| assert_equal "c", child.name } - assert_equal 5, c - c = each_test( @@doc.root, "b//c" ) { |child| - assert_equal "c", child.name - } - assert_equal 2, c - - doc = Document.new( "" ) - # //para[1] : all descendants which are the first para child of their parent - assert_equal( 4, XPath.match( doc, "//z[1]" ).size ) - # /descendant::para[1] : the first descendant para element - assert_equal( 1, XPath.match( doc, "/descendant::z[1]" ).size ) - end - - def test_root - source = "" - doc = Document.new( source ) - assert_equal doc, doc.root_node - assert_equal "a", XPath::first( doc, "/" ).elements[1].name - end - - def test_abbreviated_simple_child - assert_equal "a", XPath::first(@@doc, "a").name - end - - def test_child - c = XPath::first( @@doc, "a/b/c" ) - assert_equal "c", c.name - assert_equal "3", XPath::first(@@doc, "a/b/c").attributes["id"] - end - - def test_root_child - assert_equal "a", XPath::first(@@doc, "/a").name - c = XPath::first( @@doc, "a/b/c" ) - assert_equal "a", XPath::first(c, "/a").name - end - - def test_root_children - c = XPath::first( @@doc, "a/b/c" ) - assert_equal "2", XPath::first(c, "/a/b").attributes["id"] - end - - def test_abbreviated_step - c = XPath::first( @@doc, "a/b/c" ) + JENI_TENNISON = <<-EOJT + + + + + + + + + + + + + + + + + + + + + + + + EOJT + + def setup + @@doc = Document.new(SOURCE) unless defined? @@doc + @@jeni = Document.new( JENI_TENNISON ) unless defined? @@jeni + end + + def each_test( element, xpath ) + count = 0 + XPath::each( element, xpath ) { |child| + count += 1 + yield child if block_given? + } + count + end + + def test_descendant + doc = Document.new("") + p = XPath.match( doc, "//c" ) + assert_equal( 2, p.size ) + p = XPath.first( @@doc, "//p" ) + assert_equal "p", p.name + c = each_test( @@doc, "//c" ) { |child| assert_equal "c", child.name } + assert_equal 5, c + c = each_test( @@doc.root, "b//c" ) { |child| + assert_equal "c", child.name + } + assert_equal 2, c + + doc = Document.new( "" ) + # //para[1] : all descendants which are the first para child of their parent + assert_equal( 4, XPath.match( doc, "//z[1]" ).size ) + # /descendant::para[1] : the first descendant para element + assert_equal( 1, XPath.match( doc, "/descendant::z[1]" ).size ) + end + + def test_root + source = "" + doc = Document.new( source ) + assert_equal doc, doc.root_node + assert_equal "a", XPath::first( doc, "/" ).elements[1].name + end + + def test_abbreviated_simple_child + assert_equal "a", XPath::first(@@doc, "a").name + end + + def test_child + c = XPath::first( @@doc, "a/b/c" ) + assert_equal "c", c.name + assert_equal "3", XPath::first(@@doc, "a/b/c").attributes["id"] + end + + def test_root_child + assert_equal "a", XPath::first(@@doc, "/a").name + c = XPath::first( @@doc, "a/b/c" ) + assert_equal "a", XPath::first(c, "/a").name + end + + def test_root_children + c = XPath::first( @@doc, "a/b/c" ) + assert_equal "2", XPath::first(c, "/a/b").attributes["id"] + end + + def test_abbreviated_step + c = XPath::first( @@doc, "a/b/c" ) assert_equal("c", c.name) - assert_equal("a", XPath::first(@@doc.root, ".").name) - assert_equal("b", XPath::first(c, "..").name) - assert_equal("a", XPath::first(@@doc, "a/b/..").name) - - doc = REXML::Document.new(File.new(fixture_path("project.xml"))) - c = each_test(doc.root, "./Description" ) { |child| - assert_equal("Description",child.name) - } - assert_equal 1, c - end - - # Things that aren't tested elsewhere - def test_predicates - assert_equal "12", XPath::first(@@doc, "a/e/f[3]").attributes["id"] - assert_equal "13", XPath::first(@@doc, "a/e/f[3]/g").attributes["id"] - assert_equal "14", XPath::first(@@doc, "a/e/f[@a='d'][2]").attributes["id"] - assert_equal "14", XPath::first(@@doc, "a/e/f[@a='d'][@id='14']").attributes["id"] - assert_equal "a", XPath::first( @@doc, "*[name()='a' and @id='1']" ).name - c=each_test( @@doc, "//*[name()='f' and @a='d']") { |i| - assert_equal "f", i.name - } - assert_equal 2, c - c=each_test( @@doc, "//*[name()='m' or @a='d']") { |i| - assert ["m","f"].include?(i.name) - } - assert_equal 3, c - - assert_equal "b", XPath::first( @@doc, "//b[@x]" ).name - end - - def test_node_type - doc = Document.new "text" + assert_equal("a", XPath::first(@@doc.root, ".").name) + assert_equal("b", XPath::first(c, "..").name) + assert_equal("a", XPath::first(@@doc, "a/b/..").name) + + doc = REXML::Document.new(File.new(fixture_path("project.xml"))) + c = each_test(doc.root, "./Description" ) { |child| + assert_equal("Description",child.name) + } + assert_equal 1, c + end + + # Things that aren't tested elsewhere + def test_predicates + assert_equal "12", XPath::first(@@doc, "a/e/f[3]").attributes["id"] + assert_equal "13", XPath::first(@@doc, "a/e/f[3]/g").attributes["id"] + assert_equal "14", XPath::first(@@doc, "a/e/f[@a='d'][2]").attributes["id"] + assert_equal "14", XPath::first(@@doc, "a/e/f[@a='d'][@id='14']").attributes["id"] + assert_equal "a", XPath::first( @@doc, "*[name()='a' and @id='1']" ).name + c=each_test( @@doc, "//*[name()='f' and @a='d']") { |i| + assert_equal "f", i.name + } + assert_equal 2, c + c=each_test( @@doc, "//*[name()='m' or @a='d']") { |i| + assert ["m","f"].include?(i.name) + } + assert_equal 3, c + + assert_equal "b", XPath::first( @@doc, "//b[@x]" ).name + end + + def test_node_type + doc = Document.new "text" #res = XPath::first(doc.root, "text()") #assert_equal "text", res.to_s #res = XPath::first(doc, "*") #assert_equal "a", res.name - assert_equal( :processing_instruction, + assert_equal( :processing_instruction, XPath::first(doc.root, "processing-instruction()").node_type) - assert_equal( :comment, XPath::first(doc.root, "comment()").node_type) - end - - def test_functions - # trivial text() test - # confuse-a-function - source = "more dumb text" - 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 - - def no_test_ancestor - doc = REXML::Document.new(File.new(fixture_path("testsrc.xml"))) - doc.elements.each("//item") { |el| print el.name - if el.attributes['x'] - puts " -- "+el.attributes['x'] - else - puts - end - } - doc.elements.each("//item/ancestor::") { |el| print el.name - if el.attributes['x'] - puts " -- "+el.attributes['x'] - else - puts - end - } - end - - # Here are some XPath tests that were originally submitted by ... - # The code has changed some, but the logic and the source documents are the - # same. - # This method reads a document from a file, and then a series of xpaths, - # also from a file. It then checks each xpath against the source file. - def test_more + assert_equal( :comment, XPath::first(doc.root, "comment()").node_type) + end + + def test_functions + # trivial text() test + # confuse-a-function + source = "more dumb text" + 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 + + def no_test_ancestor + doc = REXML::Document.new(File.new(fixture_path("testsrc.xml"))) + doc.elements.each("//item") { |el| print el.name + if el.attributes['x'] + puts " -- "+el.attributes['x'] + else + puts + end + } + doc.elements.each("//item/ancestor::") { |el| print el.name + if el.attributes['x'] + puts " -- "+el.attributes['x'] + else + puts + end + } + end + + # Here are some XPath tests that were originally submitted by ... + # The code has changed some, but the logic and the source documents are the + # same. + # This method reads a document from a file, and then a series of xpaths, + # also from a file. It then checks each xpath against the source file. + def test_more xmlsource = fixture_path("testsrc.xml") xpathtests = fixture_path("xp.tst") - doc = REXML::Document.new(File.new(xmlsource)) - #results = "" - results = REXML::Document.new - results.add_element "test-results" - for line in File.new(xpathtests) - line.strip! - begin - rt = doc.root - #puts "#"*80 - #print "\nDoing #{line} " ; $stdout.flush - doc.elements.each(line) do |el| - #print "." ; $stdout.flush - results.root << el.clone - #results << el.to_s - end - #ObjectSpace.garbage_collect - GC::start - rescue Exception => z - #puts "\n'#{line}' failed" - fail("Error on line #{line}:\n#{z.message}\n"+z.backtrace[0,10].join("\n")) - #results.root.add_element( "error", {"path"=>line}).text = z.message+"\n"+z.backtrace[0,10].join("\n") - #results << ""+z.message+"" - end - end - end - - def test_axe_descendant - assert_equal "f", XPath::first( @@doc, "descendant::f").name - end - - def test_axe_parent - q = XPath.first( @@doc, "a/d/c/parent::*/q" ) - assert_equal 19, q.attributes["id"].to_i - end - - def test_abbreviated_attribute - assert_equal 'a', XPath::first( @@doc, "a[@id='1']" ).name - c = XPath::first( @@doc, "a/b/c[@id='4']" ) - assert_equal 'c', c.name - assert_equal '4', c.attributes['id'] - - result = XPath::first( @@doc, "descendant::f[@a='c']") - assert_equal "11", result.attributes['id'] - - assert_equal "11", XPath::first(@@doc, "a/e/f[@a='c']").attributes["id"] - assert_equal "11", XPath::first(@@doc, "a/e/*[@a='c']").attributes["id"] - end - - def test_axe_self - c = XPath::first( @@doc, "a/b/c" ) - assert c - assert_equal "c", c.name - assert_equal "c", XPath::first( c, "self::node()" ).name - end - - def test_axe_ancestor - doc = REXML::Document.new " - - - - - - - - - " - - d = XPath.first( doc, "//d" ) - assert_equal "d", d.name - b = each_test( d, "ancestor::b" ) { |el| - assert((1..2) === el.attributes['id'].to_i, - "Expected #{el.attributes['id']} to be either 1 or 2" - ) - } - assert_equal 2, b - end - - def test_axe_child - m = XPath.first( @@doc, "a/child::m" ) - assert_equal 15, m.attributes['id'].to_i - end - - def test_axe_attribute - a = XPath.first( @@doc, "a/attribute::id" ) - assert_equal "1", a.value - a = XPath.first( @@doc, "a/e/f[@id='14']/attribute::a" ) - assert_equal "d", a.value - end - - def test_axe_sibling - doc = Document.new "" - first_f = XPath.first( doc, "a/e/f" ) - assert first_f - assert_equal '10', first_f.attributes['id'] - next_f = XPath.first( doc, "a/e/f/following-sibling::node()" ) - assert_equal '11', next_f.attributes['id'] - - b = XPath.first( doc, "a/e/preceding-sibling::node()" ) - assert_equal 'b', b.name - end - - def test_lang - doc = Document.new(File.new(fixture_path("lang0.xml"))) - #puts IO.read( "test/lang.xml" ) - - #puts XPath.match( doc, "//language/*" ).size - c = each_test( doc, "//language/*" ) { |element| - #puts "#{element.name}: #{element.text}" - } - assert_equal 4, c - end - - def test_namespaces_1 - source = <<-EOF - - this bar - that bar - - EOF - doc = Document.new source - result = XPath.each( doc, "//bar" ) { - fail "'bar' should match nothing in this case" - } - - namespace = {"t"=>"this"} - results = XPath.first( doc, "//t:bar", namespace ) - assert_equal "this bar", results.text - end - - def test_namespaces_2 - source = <<-EOF - - this bar - that bar - - EOF - doc = Document.new source - res = XPath::first(doc, "//*[local_name()='bar']") - assert res, "looking for //*[name()='bar']" - assert_equal 'this', res.namespace - res = XPath::first(doc.root, "*[namespace_uri()='that']") - assert_equal 'that bar', res.text - end - - def test_complex - next_f = XPath.first( @@doc, "a/e/f[@id='11']/following-sibling::*" ) - assert_equal 12, next_f.attributes['id'].to_i - prev_f = XPath.first( @@doc, "a/e/f[@id='11']/preceding-sibling::*" ) - assert_equal 10, prev_f.attributes['id'].to_i - c = each_test( @@doc, "descendant-or-self::*[@x='y']" ) - assert_equal 2, c - end - - def test_grouping + doc = REXML::Document.new(File.new(xmlsource)) + #results = "" + results = REXML::Document.new + results.add_element "test-results" + for line in File.new(xpathtests) + line.strip! + begin + rt = doc.root + #puts "#"*80 + #print "\nDoing #{line} " ; $stdout.flush + doc.elements.each(line) do |el| + #print "." ; $stdout.flush + results.root << el.clone + #results << el.to_s + end + #ObjectSpace.garbage_collect + GC::start + rescue Exception => z + #puts "\n'#{line}' failed" + fail("Error on line #{line}:\n#{z.message}\n"+z.backtrace[0,10].join("\n")) + #results.root.add_element( "error", {"path"=>line}).text = z.message+"\n"+z.backtrace[0,10].join("\n") + #results << ""+z.message+"" + end + end + end + + def test_axe_descendant + assert_equal "f", XPath::first( @@doc, "descendant::f").name + end + + def test_axe_parent + q = XPath.first( @@doc, "a/d/c/parent::*/q" ) + assert_equal 19, q.attributes["id"].to_i + end + + def test_abbreviated_attribute + assert_equal 'a', XPath::first( @@doc, "a[@id='1']" ).name + c = XPath::first( @@doc, "a/b/c[@id='4']" ) + assert_equal 'c', c.name + assert_equal '4', c.attributes['id'] + + result = XPath::first( @@doc, "descendant::f[@a='c']") + assert_equal "11", result.attributes['id'] + + assert_equal "11", XPath::first(@@doc, "a/e/f[@a='c']").attributes["id"] + assert_equal "11", XPath::first(@@doc, "a/e/*[@a='c']").attributes["id"] + end + + def test_axe_self + c = XPath::first( @@doc, "a/b/c" ) + assert c + assert_equal "c", c.name + assert_equal "c", XPath::first( c, "self::node()" ).name + end + + def test_axe_ancestor + doc = REXML::Document.new " + + + + + + + + + " + + d = XPath.first( doc, "//d" ) + assert_equal "d", d.name + b = each_test( d, "ancestor::b" ) { |el| + assert((1..2) === el.attributes['id'].to_i, + "Expected #{el.attributes['id']} to be either 1 or 2" + ) + } + assert_equal 2, b + end + + def test_axe_child + m = XPath.first( @@doc, "a/child::m" ) + assert_equal 15, m.attributes['id'].to_i + end + + def test_axe_attribute + a = XPath.first( @@doc, "a/attribute::id" ) + assert_equal "1", a.value + a = XPath.first( @@doc, "a/e/f[@id='14']/attribute::a" ) + assert_equal "d", a.value + end + + def test_axe_sibling + doc = Document.new "" + first_f = XPath.first( doc, "a/e/f" ) + assert first_f + assert_equal '10', first_f.attributes['id'] + next_f = XPath.first( doc, "a/e/f/following-sibling::node()" ) + assert_equal '11', next_f.attributes['id'] + + b = XPath.first( doc, "a/e/preceding-sibling::node()" ) + assert_equal 'b', b.name + end + + def test_lang + doc = Document.new(File.new(fixture_path("lang0.xml"))) + #puts IO.read( "test/lang.xml" ) + + #puts XPath.match( doc, "//language/*" ).size + c = each_test( doc, "//language/*" ) { |element| + #puts "#{element.name}: #{element.text}" + } + assert_equal 4, c + end + + def test_namespaces_1 + source = <<-EOF + + this bar + that bar + + EOF + doc = Document.new source + result = XPath.each( doc, "//bar" ) { + fail "'bar' should match nothing in this case" + } + + namespace = {"t"=>"this"} + results = XPath.first( doc, "//t:bar", namespace ) + assert_equal "this bar", results.text + end + + def test_namespaces_2 + source = <<-EOF + + this bar + that bar + + EOF + doc = Document.new source + res = XPath::first(doc, "//*[local_name()='bar']") + assert res, "looking for //*[name()='bar']" + assert_equal 'this', res.namespace + res = XPath::first(doc.root, "*[namespace_uri()='that']") + assert_equal 'that bar', res.text + end + + def test_complex + next_f = XPath.first( @@doc, "a/e/f[@id='11']/following-sibling::*" ) + assert_equal 12, next_f.attributes['id'].to_i + prev_f = XPath.first( @@doc, "a/e/f[@id='11']/preceding-sibling::*" ) + assert_equal 10, prev_f.attributes['id'].to_i + c = each_test( @@doc, "descendant-or-self::*[@x='y']" ) + assert_equal 2, c + end + + def test_grouping t = XPath.first( @@doc, "a/d/*[name()='d' and (name()='f' or name()='q')]" ) assert_nil t - t = XPath.first( @@doc, "a/d/*[(name()='d' and name()='f') or name()='q']" ) - assert_equal 'q', t.name - end + t = XPath.first( @@doc, "a/d/*[(name()='d' and name()='f') or name()='q']" ) + assert_equal 'q', t.name + end - def test_preceding + def test_preceding d = Document.new "" - start = XPath.first( d, "/a/b[@id='1']" ) + start = XPath.first( d, "/a/b[@id='1']" ) assert_equal 'b', start.name - c = XPath.first( start, "preceding::c" ) - assert_equal '2', c.attributes['id'] + c = XPath.first( start, "preceding::c" ) + assert_equal '2', c.attributes['id'] - c1, c0 = XPath.match( d, "/a/b/c[@id='2']/preceding::node()" ) - assert_equal '1', c1.attributes['id'] - assert_equal '0', c0.attributes['id'] + c1, c0 = XPath.match( d, "/a/b/c[@id='2']/preceding::node()" ) + assert_equal '1', c1.attributes['id'] + assert_equal '0', c0.attributes['id'] c2, c1, c0, b, b2, b0 = XPath.match( start, "preceding::node()" ) - assert_equal 'c', c2.name - assert_equal 'c', c1.name - assert_equal 'c', c0.name - assert_equal 'b', b.name - assert_equal 'b', b2.name - assert_equal 'b', b0.name + assert_equal 'c', c2.name + assert_equal 'c', c1.name + assert_equal 'c', c0.name + assert_equal 'b', b.name + assert_equal 'b', b2.name + assert_equal 'b', b0.name - assert_equal '2', c2.attributes['id'] - assert_equal '1', c1.attributes['id'] - assert_equal '0', c0.attributes['id'] - assert b.attributes.empty? - assert_equal '2', b2.attributes['id'] - assert_equal '0', b0.attributes['id'] + assert_equal '2', c2.attributes['id'] + assert_equal '1', c1.attributes['id'] + assert_equal '0', c0.attributes['id'] + assert b.attributes.empty? + assert_equal '2', b2.attributes['id'] + assert_equal '0', b0.attributes['id'] d = REXML::Document.new("") matches = REXML::XPath.match(d, "/a/d/preceding::node()") @@ -409,9 +409,9 @@ class XPathTester < Test::Unit::TestCase c = REXML::XPath.match( d, "//c[@id = '5']") cs = REXML::XPath.match( c, "preceding::c" ) assert_equal( 4, cs.length ) - end + end - def test_following + def test_following d = Document.new "" start = XPath.first( d, "/a/b[@id='0']" ) assert_equal 'b', start.name @@ -426,118 +426,118 @@ class XPathTester < Test::Unit::TestCase assert_equal 6, res.size res = XPath.match( c, 'following::i' ) assert_equal 2, res.size - end - - # The following three paths were provided by - # Jeni Tennison - # a consultant who is also an XSL and XPath expert - #def test_child_cubed - # els = @@jeni.elements.to_a("*****") - # assert_equal 3, els.size - #end - - #def test_div_2 - # results = doc.elements.to_a("/ div 2") - #end - - #def test_nested_predicates - # puts @@jeni.root.elements[1].elements[1].name - # results = @@jeni.root.elements[1].elements[1].elements.to_a("../following-sibling::*[*[name() = name(current())]]") - # puts results - #end + end + + # The following three paths were provided by + # Jeni Tennison + # a consultant who is also an XSL and XPath expert + #def test_child_cubed + # els = @@jeni.elements.to_a("*****") + # assert_equal 3, els.size + #end + + #def test_div_2 + # results = doc.elements.to_a("/ div 2") + #end + + #def test_nested_predicates + # puts @@jeni.root.elements[1].elements[1].name + # results = @@jeni.root.elements[1].elements[1].elements.to_a("../following-sibling::*[*[name() = name(current())]]") + # puts results + #end - # Contributed by Mike Stok - def test_starts_with - source = <<-EOF - - a@b.c - http://www.foo.com - - 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 - - def test_toms_text_node - file = "ABCDEF" - doc = Document.new(file) + # Contributed by Mike Stok + def test_starts_with + source = <<-EOF + + a@b.c + http://www.foo.com + + 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 + + def test_toms_text_node + file = "ABCDEF" + doc = Document.new(file) assert_equal 'A', XPath.first(doc[0], 'text()').to_s - assert_equal 'AF', XPath.match(doc[0], 'text()').collect { |n| - n.to_s - }.join('') - assert_equal 'B', XPath.first(doc[0], 'b/text()').to_s - assert_equal 'D', XPath.first(doc[0], '//d/text()').to_s - assert_equal 'ABCDEF', XPath.match(doc[0], '//text()').collect {|n| - n.to_s - }.join('') - end + assert_equal 'AF', XPath.match(doc[0], 'text()').collect { |n| + n.to_s + }.join('') + assert_equal 'B', XPath.first(doc[0], 'b/text()').to_s + assert_equal 'D', XPath.first(doc[0], '//d/text()').to_s + assert_equal 'ABCDEF', XPath.match(doc[0], '//text()').collect {|n| + n.to_s + }.join('') + end def test_string_length - doc = Document.new <<-EOF - - - - - - - - - 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 3, set.size, "nodes with names length > 3" - end - - # Test provided by Mike Stok - def test_contains - source = <<-EOF - - a@b.c - http://www.foo.com - - EOF - doc = Document.new source - - [ + doc = Document.new <<-EOF + + + + + + + + + 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 3, set.size, "nodes with names length > 3" + end + + # Test provided by Mike Stok + def test_contains + source = <<-EOF + + a@b.c + http://www.foo.com + + EOF + doc = Document.new source + + [ #['o', 2], ['foo', 1], ['bar', 0]].each { |search, expected| - 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('') - - d = Document.new("") - #puts XPath.first(d, 'node()[0 + 1]') - #d = Document.new("") - #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 + 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('') + + d = Document.new("") + #puts XPath.first(d, 'node()[0 + 1]') + #d = Document.new("") + #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 def test_translate source = <<-EOF @@ -565,138 +565,138 @@ class XPathTester < Test::Unit::TestCase } end - def test_math - d = Document.new( '' ) + def test_math + d = Document.new( '' ) assert XPath.first( d.root, 'node()[1]' ) - assert_equal 'b', XPath.first( d.root, 'node()[1]' ).name - assert XPath.first( d.root, 'node()[0 + 1]' ) - assert_equal 'b', XPath.first( d.root, './node()[0 + 1]' ).name - assert XPath.first( d.root, 'node()[1 + 1]' ) - assert_equal 'c', XPath.first( d.root, './node()[1 + 1]' ).name - assert XPath.first( d.root, 'node()[4 div 2]' ) - assert_equal 'c', XPath.first( d.root, './node()[4 div 2]' ).name - assert XPath.first( d.root, 'node()[2 - 1]' ) - assert_equal 'b', XPath.first( d.root, './node()[2 - 1]' ).name - assert XPath.first( d.root, 'node()[5 mod 2]' ) - assert_equal 'b', XPath.first( d.root, './node()[5 mod 2]' ).name - assert XPath.first( d.root, 'node()[8 mod 3]' ) - assert_equal 'c', XPath.first( d.root, './node()[8 mod 3]' ).name - assert XPath.first( d.root, 'node()[1 * 2]' ) - assert_equal 'c', XPath.first( d.root, './node()[1 * 2]' ).name - assert XPath.first( d.root, 'node()[2 + -1]' ) - assert_equal 'b', XPath.first( d.root, './node()[2 + -1]' ).name - end - - def test_name + assert_equal 'b', XPath.first( d.root, 'node()[1]' ).name + assert XPath.first( d.root, 'node()[0 + 1]' ) + assert_equal 'b', XPath.first( d.root, './node()[0 + 1]' ).name + assert XPath.first( d.root, 'node()[1 + 1]' ) + assert_equal 'c', XPath.first( d.root, './node()[1 + 1]' ).name + assert XPath.first( d.root, 'node()[4 div 2]' ) + assert_equal 'c', XPath.first( d.root, './node()[4 div 2]' ).name + assert XPath.first( d.root, 'node()[2 - 1]' ) + assert_equal 'b', XPath.first( d.root, './node()[2 - 1]' ).name + assert XPath.first( d.root, 'node()[5 mod 2]' ) + assert_equal 'b', XPath.first( d.root, './node()[5 mod 2]' ).name + assert XPath.first( d.root, 'node()[8 mod 3]' ) + assert_equal 'c', XPath.first( d.root, './node()[8 mod 3]' ).name + assert XPath.first( d.root, 'node()[1 * 2]' ) + assert_equal 'c', XPath.first( d.root, './node()[1 * 2]' ).name + assert XPath.first( d.root, 'node()[2 + -1]' ) + assert_equal 'b', XPath.first( d.root, './node()[2 + -1]' ).name + end + + def test_name assert_raise( UndefinedNamespaceException, "x should be undefined" ) { d = REXML::Document.new("") } - d = REXML::Document.new("") - 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("") - 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_comparisons - source = "" - doc = REXML::Document.new(source) - - # NOTE TO SER: check that number() is required - assert_equal 2, REXML::XPath.match(doc, "//b[number(@id) > 1]").size - assert_equal 3, REXML::XPath.match(doc, "//b[number(@id) >= 1]").size - assert_equal 1, REXML::XPath.match(doc, "//b[number(@id) <= 1]").size - assert_equal 1, REXML::XPath.match(doc, "//b[number(@id) = (1 * 1)]").size - assert_equal 1, REXML::XPath.match(doc, "//b[number(@id) = (1 mod 2)]").size - assert_equal 1, REXML::XPath.match(doc, "//b[number(@id) = (4 div 2)]").size - end - - # Contributed by Kouhei - def test_substring_before - doc = Document.new("") - assert_equal("a", doc.root.elements.to_a("*[name()=substring-before('abc', 'b')]")[0].name) - assert_equal("c", doc.root.elements.to_a("*[name()=substring-after('abc', 'b')]")[0].name) - end - - def test_spaces - doc = Document.new(" - - - - - ") + d = REXML::Document.new("") + 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("") + 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_comparisons + source = "" + doc = REXML::Document.new(source) + + # NOTE TO SER: check that number() is required + assert_equal 2, REXML::XPath.match(doc, "//b[number(@id) > 1]").size + assert_equal 3, REXML::XPath.match(doc, "//b[number(@id) >= 1]").size + assert_equal 1, REXML::XPath.match(doc, "//b[number(@id) <= 1]").size + assert_equal 1, REXML::XPath.match(doc, "//b[number(@id) = (1 * 1)]").size + assert_equal 1, REXML::XPath.match(doc, "//b[number(@id) = (1 mod 2)]").size + assert_equal 1, REXML::XPath.match(doc, "//b[number(@id) = (4 div 2)]").size + end + + # Contributed by Kouhei + def test_substring_before + doc = Document.new("") + assert_equal("a", doc.root.elements.to_a("*[name()=substring-before('abc', 'b')]")[0].name) + assert_equal("c", doc.root.elements.to_a("*[name()=substring-after('abc', 'b')]")[0].name) + end + + def test_spaces + doc = Document.new(" + + + + + ") + assert_equal( 1, REXML::XPath.match(doc, + "//*[local-name()='c' and @id='b']").size ) + assert_equal( 1, REXML::XPath.match(doc, + "//*[ local-name()='c' and @id='b' ]").size ) assert_equal( 1, REXML::XPath.match(doc, - "//*[local-name()='c' and @id='b']").size ) - assert_equal( 1, REXML::XPath.match(doc, - "//*[ local-name()='c' and @id='b' ]").size ) - assert_equal( 1, REXML::XPath.match(doc, - "//*[ local-name() = 'c' and @id = 'b' ]").size ) - assert_equal( 1, - REXML::XPath.match(doc, '/a/c[@id]').size ) - assert_equal( 1, - REXML::XPath.match(doc, '/a/c[(@id)]').size ) - assert_equal( 1, - REXML::XPath.match(doc, '/a/c[ @id ]').size ) - assert_equal( 1, - REXML::XPath.match(doc, '/a/c[ (@id) ]').size ) - assert_equal( 1, - REXML::XPath.match(doc, '/a/c[( @id )]').size ) - assert_equal( 1, REXML::XPath.match(doc.root, - '/a/c[ ( @id ) ]').size ) - assert_equal( 1, REXML::XPath.match(doc, - '/a/c [ ( @id ) ] ').size ) - assert_equal( 1, REXML::XPath.match(doc, - ' / a / c [ ( @id ) ] ').size ) - end - - def test_text_nodes - # source = " - # - #test - #" - source = "test" - d = REXML::Document.new( source ) - r = REXML::XPath.match( d, %q{/root/child[text()="test"]} ) - assert_equal( 1, r.size ) - assert_equal( "child", r[0].name ) - assert_equal( "test", r[0].text ) - end - - def test_auto_string_value - source = "Introduction" - d = REXML::Document.new( source ) - #r = REXML::XPath.match( d, %q{/root[title="Introduction"]} ) - #assert_equal( 1, r.size ) - source = "test" - d = REXML::Document.new( source ) - r = REXML::XPath.match( d, %q{/a[c='test']} ) - assert_equal( 1, r.size ) - r = REXML::XPath.match( d, %q{a[c='test']} ) - assert_equal( 1, r.size ) - r = d.elements["/a[c='test']"] - assert_not_nil( r ) - r = d.elements["a[c='test']"] - assert_not_nil( r ) + "//*[ local-name() = 'c' and @id = 'b' ]").size ) + assert_equal( 1, + REXML::XPath.match(doc, '/a/c[@id]').size ) + assert_equal( 1, + REXML::XPath.match(doc, '/a/c[(@id)]').size ) + assert_equal( 1, + REXML::XPath.match(doc, '/a/c[ @id ]').size ) + assert_equal( 1, + REXML::XPath.match(doc, '/a/c[ (@id) ]').size ) + assert_equal( 1, + REXML::XPath.match(doc, '/a/c[( @id )]').size ) + assert_equal( 1, REXML::XPath.match(doc.root, + '/a/c[ ( @id ) ]').size ) + assert_equal( 1, REXML::XPath.match(doc, + '/a/c [ ( @id ) ] ').size ) + assert_equal( 1, REXML::XPath.match(doc, + ' / a / c [ ( @id ) ] ').size ) + end + + def test_text_nodes + # source = " + # + #test + #" + source = "test" + d = REXML::Document.new( source ) + r = REXML::XPath.match( d, %q{/root/child[text()="test"]} ) + assert_equal( 1, r.size ) + assert_equal( "child", r[0].name ) + assert_equal( "test", r[0].text ) + end + + def test_auto_string_value + source = "Introduction" + d = REXML::Document.new( source ) + #r = REXML::XPath.match( d, %q{/root[title="Introduction"]} ) + #assert_equal( 1, r.size ) + source = "test" + d = REXML::Document.new( source ) + r = REXML::XPath.match( d, %q{/a[c='test']} ) + assert_equal( 1, r.size ) + r = REXML::XPath.match( d, %q{a[c='test']} ) + assert_equal( 1, r.size ) + r = d.elements["/a[c='test']"] + assert_not_nil( r ) + r = d.elements["a[c='test']"] + assert_not_nil( r ) r = d.elements["a[c='xtest']"] assert_nil( r ) r = REXML::XPath.match( d, %q{a[c='xtest']} ) assert_equal( 0, r.size ) - end + end - def test_ordering - source = "" - d = REXML::Document.new( source ) - r = REXML::XPath.match( d, %q{/a/*/*[1]} ) - assert_equal( 1, r.size ) - r.each { |el| assert_equal( '1', el.attribute('id').value ) } - end + def test_ordering + source = "" + d = REXML::Document.new( source ) + r = REXML::XPath.match( d, %q{/a/*/*[1]} ) + assert_equal( 1, r.size ) + r.each { |el| assert_equal( '1', el.attribute('id').value ) } + end def test_descendant_or_self_ordering - source = " + source = " diff --git a/test/rexml/test_xpath_attribute_query.rb b/test/rexml/test_xpath_attribute_query.rb index 8d5102bb89..99439f2286 100644 --- a/test/rexml/test_xpath_attribute_query.rb +++ b/test/rexml/test_xpath_attribute_query.rb @@ -10,39 +10,39 @@ class TestRexmlXpathAttributeQuery < Test::Unit::TestCase # xmlstr1 and xmlstr2 only differ in the second line - namespaces in the root element @@xmlstr1 = ' - http://www.google.com/calendar/feeds/me%40gmail.com - - http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com - 2007-05-16T13:42:27.942Z - 2007-05-15T03:29:28.000Z - My Calendar - - - - - Me - me@gmail.com - - + http://www.google.com/calendar/feeds/me%40gmail.com + + http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com + 2007-05-16T13:42:27.942Z + 2007-05-15T03:29:28.000Z + My Calendar + + + + + Me + me@gmail.com + + ' @@xmlstr2 = ' - http://www.google.com/calendar/feeds/me%40gmail.com - - http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com - 2007-05-16T13:42:27.942Z - 2007-05-15T03:29:28.000Z - My Calendar - - - - - Me - me@gmail.com - - + http://www.google.com/calendar/feeds/me%40gmail.com + + http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com + 2007-05-16T13:42:27.942Z + 2007-05-15T03:29:28.000Z + My Calendar + + + + + Me + me@gmail.com + + ' # Fails diff --git a/test/rexml/test_xpath_msw.rb b/test/rexml/test_xpath_msw.rb index b40e366afb..c03835e2f3 100644 --- a/test/rexml/test_xpath_msw.rb +++ b/test/rexml/test_xpath_msw.rb @@ -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 @@ -15,9 +14,9 @@ class XPathAxesTester < Test::Unit::TestCase 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) - diff --git a/test/rexml/test_xpath_pred.rb b/test/rexml/test_xpath_pred.rb index 64eab3e3f9..373a7f562b 100644 --- a/test/rexml/test_xpath_pred.rb +++ b/test/rexml/test_xpath_pred.rb @@ -4,7 +4,7 @@ require "rexml/xpath" require "rexml/parsers/xpathparser" class XPathPredicateTester < Test::Unit::TestCase - include REXML + include REXML SRC=<<-EOL
diff --git a/test/rexml/test_xpathtext.rb b/test/rexml/test_xpathtext.rb index 0ae48acffa..c3393987f2 100644 --- a/test/rexml/test_xpathtext.rb +++ b/test/rexml/test_xpathtext.rb @@ -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, " has one element ancestor") nodes = @doc.get_elements('//b/ancestor::node()') -- cgit v1.2.3