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

* test/rexml/xpath/test_attribute.rb: Simplify.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45141 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
kou 2014-02-22 14:03:25 +00:00
parent 7322a22816
commit f8b76cd8fd
2 changed files with 21 additions and 75 deletions

View file

@ -1,3 +1,7 @@
2014-02-22 Kouhei Sutou <kou@cozmixng.org>
* test/rexml/xpath/test_attribute.rb: Simplify.
Sat Feb 22 20:28:47 2014 NAKAMURA Usaku <usa@ruby-lang.org> Sat Feb 22 20:28:47 2014 NAKAMURA Usaku <usa@ruby-lang.org>
* tool/redmine-backporter.rb: more friendly. * tool/redmine-backporter.rb: more friendly.

View file

@ -2,84 +2,26 @@ require 'test/unit'
require 'rexml/document' require 'rexml/document'
class TestXPathAttribute < Test::Unit::TestCase class TestXPathAttribute < Test::Unit::TestCase
def setup
# xmlstr1 and xmlstr2 only differ in the second line - namespaces in the root element @xml = <<-XML
@@xmlstr1 = '<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:gCal="http://schemas.google.com/gCal/2005"> <root>
<id>http://www.google.com/calendar/feeds/me%40gmail.com</id> <child name="one">child1</child>
<entry> <child name="two">child2</child>
<id>http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com</id> <child name="three">child3</child>
<published>2007-05-16T13:42:27.942Z</published> </root>
<updated>2007-05-15T03:29:28.000Z</updated> XML
<title type="text">My Calendar</title> @document = REXML::Document.new(@xml)
<link rel="alternate" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/private/full"/>
<link rel="http://schemas.google.com/acl/2007#accessControlList" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/acl/full"/>
<link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com"/>
<author>
<name>Me</name>
<email>me@gmail.com</email>
</author>
</entry>
</feed>'
@@xmlstr2 = '<?xml version="1.0" encoding="UTF-8"?>
<feed>
<id>http://www.google.com/calendar/feeds/me%40gmail.com</id>
<entry>
<id>http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com</id>
<published>2007-05-16T13:42:27.942Z</published>
<updated>2007-05-15T03:29:28.000Z</updated>
<title type="text">My Calendar</title>
<link rel="alternate" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/private/full"/>
<link rel="http://schemas.google.com/acl/2007#accessControlList" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/acl/full"/>
<link rel="self" type="application/atom+xml" href="http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com"/>
<author>
<name>Me</name>
<email>me@gmail.com</email>
</author>
</entry>
</feed>'
# Fails
def test_xpath_query
do_test @@xmlstr1
end end
# Passes def test_elements
def test_xpath_query_no_namespace root = @document.elements["root"]
do_test @@xmlstr2 second_child = root.elements["child[@name='two']"]
assert_equal("child2", second_child.text)
end end
def do_test(xmlString) def test_xpath_each
hrefs = [ children = REXML::XPath.each(@document, "/root/child[@name='two']")
"http://www.google.com/calendar/feeds/me%40gmail.com/private/full", assert_equal(["child2"], children.collect(&:text))
"http://www.google.com/calendar/feeds/me%40gmail.com/acl/full",
"http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com"
]
ctr=0
REXML::Document.new(xmlString).elements.each("feed/entry") do |element|
@alternate_link = element.elements["link[@rel='alternate']"]
assert_not_nil( @alternate_link )
assert_equal( hrefs[ctr], @alternate_link.attributes['href'])
ctr += 1
end
end
def test_another_way
doc = REXML::Document.new(@@xmlstr1)
hrefs = [
"http://www.google.com/calendar/feeds/me%40gmail.com/private/full",
"http://www.google.com/calendar/feeds/me%40gmail.com/acl/full",
"http://www.google.com/calendar/feeds/me%40gmail.com/me%40gmail.com"
]
ctr=0
REXML::XPath.each(doc, "//link[@rel='alternate']") do |element|
@alternate_link = element
assert_not_nil @alternate_link
assert_equal( hrefs[ctr], @alternate_link.attributes['href'])
ctr += 1
end
end end
end end