2015-12-16 05:07:31 +00:00
|
|
|
# frozen_string_literal: false
|
2010-09-17 13:14:14 +00:00
|
|
|
require 'test/unit'
|
|
|
|
require 'rexml/document'
|
|
|
|
|
2014-05-27 12:07:40 +00:00
|
|
|
module REXMLTests
|
2014-05-27 13:10:55 +00:00
|
|
|
class TestXPathAttribute < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@xml = <<-XML
|
2014-02-22 14:03:25 +00:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
2018-12-31 07:21:37 +09:00
|
|
|
<root xmlns="http://example.com/">
|
2014-02-22 14:03:25 +00:00
|
|
|
<child name="one">child1</child>
|
|
|
|
<child name="two">child2</child>
|
|
|
|
<child name="three">child3</child>
|
|
|
|
</root>
|
2014-05-27 13:10:55 +00:00
|
|
|
XML
|
|
|
|
@document = REXML::Document.new(@xml)
|
|
|
|
end
|
2011-05-15 11:55:52 +00:00
|
|
|
|
2014-05-27 13:10:55 +00:00
|
|
|
def test_elements
|
|
|
|
root = @document.elements["root"]
|
|
|
|
second_child = root.elements["child[@name='two']"]
|
|
|
|
assert_equal("child2", second_child.text)
|
|
|
|
end
|
2011-05-15 11:55:52 +00:00
|
|
|
|
2014-05-27 13:10:55 +00:00
|
|
|
def test_xpath_each
|
|
|
|
children = REXML::XPath.each(@document, "/root/child[@name='two']")
|
|
|
|
assert_equal(["child2"], children.collect(&:text))
|
|
|
|
end
|
2018-12-31 07:21:37 +09:00
|
|
|
|
|
|
|
def test_no_namespace
|
|
|
|
children = REXML::XPath.match(@document,
|
|
|
|
"/root/child[@nothing:name='two']",
|
|
|
|
"" => "http://example.com/",
|
|
|
|
"nothing" => "")
|
|
|
|
assert_equal(["child2"], children.collect(&:text))
|
|
|
|
end
|
2010-09-17 13:14:14 +00:00
|
|
|
end
|
|
|
|
end
|