summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rexml/xpath/test_text.rb
blob: 46aba3c2efa16c1cfa0b4a68d68955e83dc7a37a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'test/unit'
require 'rexml/document'
require 'rexml/element'
require 'rexml/xpath'

module REXMLTests
  class TestXPathText < Test::Unit::TestCase
    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)
      REXML::Text.new('test', false, node2)
      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)
      comment = REXML::Comment.new('Test comment', node2)
      assert_same(node2, comment.parent)
    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
      assert_same node2, textnode.parent
      nodes = @doc.get_elements('//b/ancestor::*')
      assert_equal(1, nodes.size, "<b> has one element ancestor")
      nodes = @doc.get_elements('//b/ancestor::node()')
      assert_equal(2, nodes.size, "<b> has two node ancestors")
      assert nodes[1].kind_of?(REXML::Document)
    end
  end
end