2010-09-17 09:14:14 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'rexml/document'
|
|
|
|
require 'rexml/element'
|
|
|
|
require 'rexml/xpath'
|
|
|
|
|
2014-05-27 08:07:40 -04:00
|
|
|
module REXMLTests
|
2014-02-22 06:25:47 -05:00
|
|
|
class TestXPathText < Test::Unit::TestCase
|
2010-09-17 09:14:14 -04:00
|
|
|
def setup
|
|
|
|
@doc = REXML::Document.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def tear_down
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_text_as_element
|
|
|
|
node1 = REXML::Element.new('a', @doc)
|
|
|
|
node2 = REXML::Element.new('b', node1)
|
2012-07-23 17:37:39 -04:00
|
|
|
REXML::Text.new('test', false, node2)
|
2010-09-17 09:14:14 -04:00
|
|
|
assert_equal(1, @doc.elements.size, "doc owns 1 element node1")
|
|
|
|
assert_same(node1, @doc.elements[1], "doc owns 1 element node1")
|
|
|
|
assert_equal(1, node1.elements.size, "node1 owns 1 element node2")
|
|
|
|
assert_same(node2, node1.elements[1], "node1 owns 1 element node2")
|
|
|
|
assert_equal(1, node2.size, "node2 owns 1 text element")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_text_in_xpath_query
|
|
|
|
node1 = REXML::Element.new('a', @doc)
|
|
|
|
node2 = REXML::Element.new('b', node1)
|
|
|
|
textnode = REXML::Text.new('test', false, node2)
|
|
|
|
textnode.parent = node2 # should be unnecessary
|
|
|
|
nodes = @doc.get_elements('//b')
|
|
|
|
assert_equal(1, nodes.size, "document has one element")
|
|
|
|
# why doesn't this query work right?
|
|
|
|
nodes = REXML::XPath.match(@doc, '//text()')
|
|
|
|
assert_equal(1, nodes.size, "//text() should yield one Text element")
|
|
|
|
assert_equal(REXML::Text, nodes[0].class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_comment_in_xpath_query
|
|
|
|
node1 = REXML::Element.new('a', @doc)
|
|
|
|
node2 = REXML::Element.new('b', node1)
|
|
|
|
commentnode = REXML::Comment.new('test', node2)
|
|
|
|
nodes = REXML::XPath.match(@doc, '//comment()')
|
|
|
|
assert_equal(1, nodes.size, "//comment() should yield one Comment element")
|
|
|
|
assert_same commentnode, nodes[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parentage
|
|
|
|
node1 = REXML::Element.new('a', @doc)
|
|
|
|
assert_same(@doc, node1.parent, "node1 parent is document")
|
|
|
|
node2 = REXML::Element.new('b', node1)
|
|
|
|
assert_same(node1, node2.parent, "node2 parent is node1")
|
|
|
|
textnode = REXML::Text.new('test', false, node2)
|
|
|
|
# why isn't the text's parent node2?
|
|
|
|
# Also look at Comment, etc.
|
|
|
|
assert_same(node2, textnode.parent)
|
2010-09-17 09:46:56 -04:00
|
|
|
comment = REXML::Comment.new('Test comment', node2)
|
|
|
|
assert_same(node2, comment.parent)
|
2010-09-17 09:14:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ancestors
|
|
|
|
node1 = REXML::Element.new('a', @doc)
|
|
|
|
node2 = REXML::Element.new('b', node1)
|
|
|
|
textnode = REXML::Text.new('test', false, node2)
|
|
|
|
#textnode.parent = node2 # should be unnecessary
|
2010-09-17 09:46:56 -04:00
|
|
|
assert_same node2, textnode.parent
|
2010-09-17 09:14:14 -04:00
|
|
|
nodes = @doc.get_elements('//b/ancestor::*')
|
|
|
|
assert_equal(1, nodes.size, "<b> has one element ancestor")
|
|
|
|
nodes = @doc.get_elements('//b/ancestor::node()')
|
|
|
|
assert_equal(2, nodes.size, "<b> has two node ancestors")
|
|
|
|
assert nodes[1].kind_of?(REXML::Document)
|
|
|
|
end
|
|
|
|
end
|
2014-05-27 06:21:10 -04:00
|
|
|
end
|