forked from xml4r/libxml-ruby
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtc_traversal.rb
153 lines (120 loc) · 3.07 KB
/
tc_traversal.rb
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# encoding: UTF-8
require './test_helper'
require 'test/unit'
class TestTranversal < Test::Unit::TestCase
ROOT_NODES_LENGTH = 27
ROOT_ELEMENTS_LENGTH = 13
def setup
filename = File.join(File.dirname(__FILE__), 'model/books.xml')
@doc = XML::Document.file(filename)
end
def teardown
@doc = nil
end
def test_children
# Includes text nodes and such
assert_equal(ROOT_NODES_LENGTH, @doc.root.children.length)
end
def test_children_iteration
# Includes text nodes and such
nodes = @doc.root.children.inject([]) do |arr, node|
arr << node
arr
end
assert_equal(ROOT_NODES_LENGTH, nodes.length)
end
def test_no_children
# Get a node with no children
node = @doc.find_first('/catalog/book[@id="bk113"]/price')
assert_equal(0, node.children.length)
end
def test_no_children_inner_xml
# Get a node with no children
node = @doc.find_first('/catalog/book[@id="bk113"]/price')
assert_nil(node.inner_xml)
end
def test_each
# Includes text nodes and such
nodes = @doc.root.inject([]) do |arr, node|
arr << node
arr
end
assert_equal(ROOT_NODES_LENGTH, nodes.length)
end
def test_each_element
# Includes text nodes and such
nodes = []
@doc.root.each_element do |node|
nodes << node
end
assert_equal(ROOT_ELEMENTS_LENGTH, nodes.length)
end
def test_next
nodes = []
node = @doc.root.first
while node
nodes << node
node = node.next
end
assert_equal(ROOT_NODES_LENGTH, nodes.length)
end
def test_next?
first_node = @doc.root.first
assert(first_node.next?)
last_node = @doc.root.last
assert(!last_node.next?)
end
def test_prev
nodes = []
node = @doc.root.last
while node
nodes << node
node = node.prev
end
assert_equal(ROOT_NODES_LENGTH, nodes.length)
end
def test_prev?
first_node = @doc.root.first
assert(!first_node.prev?)
last_node = @doc.root.last
assert(last_node.prev?)
end
def test_parent?
assert([email protected]?)
assert(@doc.root.parent?)
end
def test_child?
assert(@doc.child?)
assert([email protected]?)
end
def test_next_prev_equivalence
next_nodes = []
last_nodes = []
node = @doc.root.first
while node
next_nodes << node
node = node.next
end
node = @doc.root.last
while node
last_nodes << node
node = node.prev
end
assert_equal(next_nodes, last_nodes.reverse)
end
def test_next_children_equivalence
next_nodes = []
node = @doc.root.first
while node
next_nodes << node
node = node.next
end
assert_equal(@doc.root.children, next_nodes)
end
def test_doc_class
assert_instance_of(XML::Document, @doc)
end
def test_root_class
assert_instance_of(XML::Node, @doc.root)
end
end