rubytree 0.9.7 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/API-CHANGES.md +153 -0
- data/Gemfile +2 -13
- data/Gemfile.lock +60 -61
- data/History.md +410 -0
- data/LICENSE.md +1 -2
- data/README.md +24 -28
- data/Rakefile +65 -48
- data/TODO.org +19 -15
- data/examples/example_basic.rb +19 -12
- data/lib/rubytree.rb +3 -4
- data/lib/tree/binarytree.rb +27 -27
- data/lib/tree/tree_deps.rb +9 -12
- data/lib/tree/utils/hash_converter.rb +127 -121
- data/lib/tree/utils/json_converter.rb +81 -79
- data/lib/tree/utils/metrics_methods.rb +18 -48
- data/lib/tree/utils/path_methods.rb +15 -17
- data/lib/tree/utils/tree_merge_handler.rb +79 -80
- data/lib/tree/utils/utils.rb +9 -6
- data/lib/tree/version.rb +3 -5
- data/lib/tree.rb +194 -177
- data/rubytree.gemspec +67 -44
- data/spec/spec_helper.rb +5 -3
- data/spec/tree_spec.rb +136 -37
- data/test/run_test.rb +9 -8
- data/test/test_binarytree.rb +86 -105
- data/test/test_rubytree_require.rb +4 -5
- data/test/test_subclassed_node.rb +5 -26
- data/test/test_thread_and_fiber.rb +13 -16
- data/test/test_tree.rb +577 -657
- metadata +142 -55
- data/API-CHANGES.rdoc +0 -99
- data/History.rdoc +0 -303
- data/TAGS +0 -248
- data/gem_graph.png +0 -0
- data/lib/tree/utils/camel_case_method_handler.rb +0 -79
- data/setup.rb +0 -1585
data/test/test_tree.rb
CHANGED
@@ -2,9 +2,7 @@
|
|
2
2
|
|
3
3
|
# test_tree.rb - This file is part of the RubyTree package.
|
4
4
|
#
|
5
|
-
# Copyright (c) 2006
|
6
|
-
#
|
7
|
-
# All rights reserved.
|
5
|
+
# Copyright (c) 2006-2022 Anupam Sengupta. All rights reserved.
|
8
6
|
#
|
9
7
|
# Redistribution and use in source and binary forms, with or without modification,
|
10
8
|
# are permitted provided that the following conditions are met:
|
@@ -31,17 +29,17 @@
|
|
31
29
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
32
30
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
33
31
|
#
|
32
|
+
# frozen_string_literal: true
|
34
33
|
|
35
34
|
require 'test/unit'
|
36
35
|
require 'json'
|
37
|
-
|
36
|
+
require_relative '../lib/tree/tree_deps'
|
38
37
|
|
39
38
|
module TestTree
|
40
39
|
# Test class for the Tree node.
|
40
|
+
# noinspection RubyTooManyInstanceVariablesInspection
|
41
41
|
class TestTreeNode < Test::Unit::TestCase
|
42
|
-
|
43
|
-
Person = Struct::new(:First, :last) # A simple structure to use as the content for the nodes.
|
44
|
-
|
42
|
+
Person = Struct.new(:First, :last) # A simple structure to use as the content for the nodes.
|
45
43
|
|
46
44
|
# Create this structure for the tests
|
47
45
|
#
|
@@ -63,14 +61,13 @@ module TestTree
|
|
63
61
|
#
|
64
62
|
# Some basic setup to create the nodes for the test tree.
|
65
63
|
def setup
|
66
|
-
@root = Tree::TreeNode.new(
|
67
|
-
|
68
|
-
@child1 = Tree::TreeNode.new("Child1", "Child Node 1")
|
69
|
-
@child2 = Tree::TreeNode.new("Child2", "Child Node 2")
|
70
|
-
@child3 = Tree::TreeNode.new("Child3", "Child Node 3")
|
71
|
-
@child4 = Tree::TreeNode.new("Child4", "Grand Child 1")
|
72
|
-
@child5 = Tree::TreeNode.new("Child5", "Child Node 4")
|
64
|
+
@root = Tree::TreeNode.new('ROOT', 'Root Node')
|
73
65
|
|
66
|
+
@child1 = Tree::TreeNode.new('Child1', 'Child Node 1')
|
67
|
+
@child2 = Tree::TreeNode.new('Child2', 'Child Node 2')
|
68
|
+
@child3 = Tree::TreeNode.new('Child3', 'Child Node 3')
|
69
|
+
@child4 = Tree::TreeNode.new('Child4', 'Grand Child 1')
|
70
|
+
@child5 = Tree::TreeNode.new('Child5', 'Child Node 4')
|
74
71
|
end
|
75
72
|
|
76
73
|
# Create the actual test tree.
|
@@ -92,17 +89,17 @@ module TestTree
|
|
92
89
|
|
93
90
|
# This test is for the root alone - without any children being linked
|
94
91
|
def test_root_setup
|
95
|
-
assert_not_nil(@root
|
96
|
-
assert_nil(@root.parent
|
97
|
-
assert_not_nil(@root.name
|
98
|
-
assert_equal(
|
99
|
-
assert_equal(
|
100
|
-
assert(@root.
|
101
|
-
assert(!@root.
|
102
|
-
assert(@root.
|
103
|
-
assert_equal(1
|
104
|
-
assert_equal(0, @root.siblings.length,
|
105
|
-
assert_equal(0, @root.in_degree,
|
92
|
+
assert_not_nil(@root, 'Root cannot be nil')
|
93
|
+
assert_nil(@root.parent, 'Parent of root node should be nil')
|
94
|
+
assert_not_nil(@root.name, 'Name should not be nil')
|
95
|
+
assert_equal('ROOT', @root.name, "Name should be 'ROOT'")
|
96
|
+
assert_equal('Root Node', @root.content, "Content should be 'Root Node'")
|
97
|
+
assert(@root.root?, 'Should identify as root')
|
98
|
+
assert(!@root.children?, 'Cannot have any children')
|
99
|
+
assert(@root.content?, 'This root should have content')
|
100
|
+
assert_equal(1, @root.size, 'Number of nodes should be one')
|
101
|
+
assert_equal(0, @root.siblings.length, 'This root does not have any children')
|
102
|
+
assert_equal(0, @root.in_degree, 'Root should have an in-degree of 0')
|
106
103
|
assert_equal(0, @root.node_height, "Root's height before adding any children is 0")
|
107
104
|
assert_raise(ArgumentError) { Tree::TreeNode.new(nil) }
|
108
105
|
end
|
@@ -111,13 +108,13 @@ module TestTree
|
|
111
108
|
def test_root
|
112
109
|
setup_test_tree
|
113
110
|
|
114
|
-
#
|
111
|
+
# @todo: Should probably change this logic. Root's root should
|
115
112
|
# return nil so that the possibility of a recursive error does not exist
|
116
113
|
# at all.
|
117
|
-
assert_same(@root
|
118
|
-
assert_same(@root
|
119
|
-
assert_same(@root
|
120
|
-
assert_equal(2
|
114
|
+
assert_same(@root, @root.root, "Root's root is self")
|
115
|
+
assert_same(@root, @child1.root, 'Root should be ROOT')
|
116
|
+
assert_same(@root, @child4.root, 'Root should be ROOT')
|
117
|
+
assert_equal(2, @root.node_height, "Root's height after adding the children should be 2")
|
121
118
|
end
|
122
119
|
|
123
120
|
def test_from_hash
|
@@ -129,47 +126,45 @@ module TestTree
|
|
129
126
|
# / \
|
130
127
|
# H I
|
131
128
|
|
132
|
-
hash = {[:A,
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
}
|
129
|
+
hash = { [:A, 'Root content'] => {
|
130
|
+
B: {
|
131
|
+
E: {},
|
132
|
+
F: {
|
133
|
+
H: {},
|
134
|
+
[:I, 'Leaf content'] => {}
|
135
|
+
}
|
136
|
+
},
|
137
|
+
C: {},
|
138
|
+
D: {
|
139
|
+
G: {}
|
140
|
+
}
|
141
|
+
} }
|
146
142
|
|
147
143
|
tree = Tree::TreeNode.from_hash(hash)
|
148
144
|
|
149
145
|
assert_same(Tree::TreeNode, tree.class)
|
150
146
|
assert_same(tree.name, :A)
|
151
|
-
assert_equal(true, tree.
|
152
|
-
assert_equal(false, tree.
|
147
|
+
assert_equal(true, tree.root?)
|
148
|
+
assert_equal(false, tree.leaf?)
|
153
149
|
assert_equal(9, tree.size)
|
154
|
-
assert_equal(
|
150
|
+
assert_equal('Root content', tree.content)
|
155
151
|
assert_equal(3, tree.children.count) # B, C, D
|
156
152
|
|
157
153
|
leaf_with_content = tree[:B][:F][:I]
|
158
|
-
assert_equal(
|
159
|
-
assert_equal(true, leaf_with_content.
|
154
|
+
assert_equal('Leaf content', leaf_with_content.content)
|
155
|
+
assert_equal(true, leaf_with_content.leaf?)
|
160
156
|
|
161
157
|
leaf_without_content = tree[:C]
|
162
|
-
assert_equal(true, leaf_without_content.
|
158
|
+
assert_equal(true, leaf_without_content.leaf?)
|
163
159
|
|
164
160
|
interior_node = tree[:B][:F]
|
165
|
-
assert_equal(false, interior_node.
|
161
|
+
assert_equal(false, interior_node.leaf?)
|
166
162
|
assert_equal(2, interior_node.children.count)
|
167
163
|
|
168
164
|
# Can't make a node without a name
|
169
|
-
assert_raise
|
165
|
+
assert_raise(ArgumentError) { Tree::TreeNode.from_hash({}) }
|
170
166
|
# Can't have multiple roots
|
171
|
-
assert_raise
|
172
|
-
|
167
|
+
assert_raise(ArgumentError) { Tree::TreeNode.from_hash({ A: {}, B: {} }) }
|
173
168
|
end
|
174
169
|
|
175
170
|
def test_from_hash_with_nils
|
@@ -181,40 +176,39 @@ module TestTree
|
|
181
176
|
# / \
|
182
177
|
# H I
|
183
178
|
|
184
|
-
hash = {[:A,
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
}
|
179
|
+
hash = { [:A, 'Root content'] => {
|
180
|
+
B: {
|
181
|
+
E: nil,
|
182
|
+
F: {
|
183
|
+
H: nil,
|
184
|
+
[:I, 'Leaf content'] => nil
|
185
|
+
}
|
186
|
+
},
|
187
|
+
C: nil,
|
188
|
+
D: {
|
189
|
+
G: nil
|
190
|
+
}
|
191
|
+
} }
|
198
192
|
|
199
193
|
tree = Tree::TreeNode.from_hash(hash)
|
200
194
|
|
201
195
|
assert_same(Tree::TreeNode, tree.class)
|
202
196
|
assert_same(:A, tree.name)
|
203
|
-
assert_equal(true, tree.
|
204
|
-
assert_equal(false, tree.
|
197
|
+
assert_equal(true, tree.root?)
|
198
|
+
assert_equal(false, tree.leaf?)
|
205
199
|
assert_equal(9, tree.size)
|
206
|
-
assert_equal(
|
200
|
+
assert_equal('Root content', tree.content)
|
207
201
|
assert_equal(3, tree.children.count) # B, C, D
|
208
202
|
|
209
203
|
leaf_with_content = tree[:B][:F][:I]
|
210
|
-
assert_equal(
|
211
|
-
assert_equal(true, leaf_with_content.
|
204
|
+
assert_equal('Leaf content', leaf_with_content.content)
|
205
|
+
assert_equal(true, leaf_with_content.leaf?)
|
212
206
|
|
213
207
|
leaf_without_content = tree[:C]
|
214
|
-
assert_equal(true, leaf_without_content.
|
208
|
+
assert_equal(true, leaf_without_content.leaf?)
|
215
209
|
|
216
210
|
interior_node = tree[:B][:F]
|
217
|
-
assert_equal(false, interior_node.
|
211
|
+
assert_equal(false, interior_node.leaf?)
|
218
212
|
assert_equal(2, interior_node.children.count)
|
219
213
|
end
|
220
214
|
|
@@ -226,7 +220,7 @@ module TestTree
|
|
226
220
|
assert_equal([], tree.add_from_hash(hash))
|
227
221
|
|
228
222
|
# Okay, now try a real hash
|
229
|
-
hash = {:
|
223
|
+
hash = { B: { C: { D: nil }, E: {}, F: {} }, [:G, 'G content'] => {} }
|
230
224
|
# A
|
231
225
|
# / \
|
232
226
|
# B G
|
@@ -239,14 +233,14 @@ module TestTree
|
|
239
233
|
assert_equal(Array, added_children.class)
|
240
234
|
assert_equal(2, added_children.count)
|
241
235
|
assert_equal(7, tree.size)
|
242
|
-
assert_equal(
|
243
|
-
assert_equal(true, tree[:G].
|
236
|
+
assert_equal('G content', tree[:G].content)
|
237
|
+
assert_equal(true, tree[:G].leaf?)
|
244
238
|
assert_equal(5, tree[:B].size)
|
245
239
|
assert_equal(3, tree[:B].children.count)
|
246
240
|
|
247
|
-
assert_raise
|
248
|
-
assert_raise
|
249
|
-
assert_raise
|
241
|
+
assert_raise(ArgumentError) { tree.add_from_hash([]) }
|
242
|
+
assert_raise(ArgumentError) { tree.add_from_hash('not a hash') }
|
243
|
+
assert_raise(ArgumentError) { tree.add_from_hash({ X: 'Not a hash or nil' }) }
|
250
244
|
end
|
251
245
|
|
252
246
|
# Test exporting to ruby Hash
|
@@ -254,7 +248,7 @@ module TestTree
|
|
254
248
|
a = Tree::TreeNode.new(:A)
|
255
249
|
b = Tree::TreeNode.new(:B)
|
256
250
|
c = Tree::TreeNode.new(:C)
|
257
|
-
d = Tree::TreeNode.new(:D)
|
251
|
+
# d = Tree::TreeNode.new(:D)
|
258
252
|
e = Tree::TreeNode.new(:E)
|
259
253
|
f = Tree::TreeNode.new(:F)
|
260
254
|
g = Tree::TreeNode.new(:G)
|
@@ -271,7 +265,7 @@ module TestTree
|
|
271
265
|
b << e
|
272
266
|
|
273
267
|
exported = a.to_h
|
274
|
-
expected = {:
|
268
|
+
expected = { A: { B: { E: {} }, C: { F: {}, G: {} } } }
|
275
269
|
assert_equal(expected, exported)
|
276
270
|
end
|
277
271
|
|
@@ -285,7 +279,7 @@ module TestTree
|
|
285
279
|
# |\ |
|
286
280
|
# I J K
|
287
281
|
|
288
|
-
input = {:
|
282
|
+
input = { A: { B: { E: { I: {}, J: {} }, F: {}, G: {} }, C: { H: { K: {} } } } }
|
289
283
|
|
290
284
|
node = Tree::TreeNode.from_hash(input)
|
291
285
|
assert_equal(input, node.to_h)
|
@@ -293,26 +287,17 @@ module TestTree
|
|
293
287
|
|
294
288
|
# Test the presence of content in the nodes.
|
295
289
|
def test_has_content_eh
|
296
|
-
a_node = Tree::TreeNode.new(
|
297
|
-
assert_nil(a_node.content
|
298
|
-
assert(!a_node.
|
290
|
+
a_node = Tree::TreeNode.new('A Node')
|
291
|
+
assert_nil(a_node.content, 'The node should not have content')
|
292
|
+
assert(!a_node.content?, 'The node should not have content')
|
299
293
|
|
300
|
-
a_node.content =
|
301
|
-
assert_not_nil(a_node.content,
|
302
|
-
assert(a_node.
|
303
|
-
end
|
304
|
-
|
305
|
-
# Test the equivalence of size and length methods.
|
306
|
-
def test_length_is_size
|
307
|
-
setup_test_tree
|
308
|
-
assert_equal(@root.size, @root.length, "Length and size methods should return the same result")
|
294
|
+
a_node.content = 'Something'
|
295
|
+
assert_not_nil(a_node.content, 'The node should now have content')
|
296
|
+
assert(a_node.content?, 'The node should now have content')
|
309
297
|
end
|
310
298
|
|
311
299
|
# Test the <=> operator.
|
312
300
|
def test_spaceship
|
313
|
-
require 'structured_warnings'
|
314
|
-
StandardWarning.disable # Disable the warnings for using integers as node names
|
315
|
-
|
316
301
|
first_node = Tree::TreeNode.new(1)
|
317
302
|
second_node = Tree::TreeNode.new(2)
|
318
303
|
|
@@ -322,74 +307,69 @@ module TestTree
|
|
322
307
|
second_node = Tree::TreeNode.new(1)
|
323
308
|
assert_equal(0, first_node <=> second_node)
|
324
309
|
|
325
|
-
first_node = Tree::TreeNode.new(
|
326
|
-
second_node = Tree::TreeNode.new(
|
310
|
+
first_node = Tree::TreeNode.new('ABC')
|
311
|
+
second_node = Tree::TreeNode.new('XYZ')
|
327
312
|
|
328
313
|
assert_nil(first_node <=> nil)
|
329
314
|
assert_equal(-1, first_node <=> second_node)
|
330
315
|
|
331
|
-
second_node = Tree::TreeNode.new(
|
316
|
+
second_node = Tree::TreeNode.new('ABC')
|
332
317
|
assert_equal(0, first_node <=> second_node)
|
333
|
-
|
334
|
-
StandardWarning.enable
|
335
318
|
end
|
336
319
|
|
337
320
|
# Test the inclusion of Comparable
|
338
321
|
def test_is_comparable
|
339
|
-
|
340
|
-
|
341
|
-
|
322
|
+
node_a = Tree::TreeNode.new('NodeA', 'Some Content')
|
323
|
+
node_b = Tree::TreeNode.new('NodeB', 'Some Content')
|
324
|
+
node_c = Tree::TreeNode.new('NodeC', 'Some Content')
|
342
325
|
|
343
326
|
# Check if the nodes compare correctly
|
344
|
-
assert(
|
345
|
-
assert(
|
346
|
-
assert(
|
347
|
-
assert(
|
348
|
-
|
349
|
-
assert(!(nodeA == nodeB), "Node A and Node B are not equal")
|
350
|
-
assert(nodeB.between?(nodeA, nodeC), "Node B is lexically between node A and node C")
|
351
|
-
|
327
|
+
assert(node_a < node_b, "Node A is lexically 'less than' node B")
|
328
|
+
assert(node_a <= node_b, "Node A is lexically 'less than' node B")
|
329
|
+
assert(node_b > node_a, "Node B is lexically 'greater than' node A")
|
330
|
+
assert(node_b >= node_a, "Node B is lexically 'greater than' node A")
|
352
331
|
|
332
|
+
assert(node_a != node_b, 'Node A and Node B are not equal')
|
333
|
+
assert(node_b.between?(node_a, node_c), 'Node B is lexically between node A and node C')
|
353
334
|
end
|
354
335
|
|
355
336
|
# Test the to_s method. This is probably a little fragile right now.
|
356
337
|
def test_to_s
|
357
|
-
a_node = Tree::TreeNode.new(
|
338
|
+
a_node = Tree::TreeNode.new('A Node', 'Some Content')
|
358
339
|
|
359
|
-
expected_string =
|
340
|
+
expected_string = 'Node Name: A Node Content: Some Content Parent: <None> Children: 0 Total Nodes: 1'
|
360
341
|
|
361
|
-
assert_equal(expected_string, a_node.to_s,
|
342
|
+
assert_equal(expected_string, a_node.to_s, 'The string representation should be same')
|
362
343
|
|
363
344
|
# Now test with a symbol as a key.
|
364
|
-
a_node = Tree::TreeNode.new(:Node_Name,
|
365
|
-
expected_string =
|
366
|
-
assert_equal(expected_string, a_node.to_s,
|
345
|
+
a_node = Tree::TreeNode.new(:Node_Name, 'Some Content')
|
346
|
+
expected_string = 'Node Name: Node_Name Content: Some Content Parent: <None> Children: 0 Total Nodes: 1'
|
347
|
+
assert_equal(expected_string, a_node.to_s, 'The string representation should be same')
|
367
348
|
|
368
349
|
# Now test with a symbol as a key and another symbol as the content.
|
369
350
|
a_node = Tree::TreeNode.new(:Node_Name, :Content)
|
370
|
-
expected_string =
|
371
|
-
assert_equal(expected_string, a_node.to_s,
|
351
|
+
expected_string = 'Node Name: Node_Name Content: Content Parent: <None> Children: 0 Total Nodes: 1'
|
352
|
+
assert_equal(expected_string, a_node.to_s, 'The string representation should be same')
|
372
353
|
|
373
354
|
# Now test with a symbol as a key, and a hash as the content.
|
374
|
-
a_hash = {:
|
355
|
+
a_hash = { a_key: 'Some Value' }
|
375
356
|
a_node = Tree::TreeNode.new(:Node_Name, a_hash)
|
376
357
|
expected_string = "Node Name: Node_Name Content: #{a_hash} Parent: <None> Children: 0 Total Nodes: 1"
|
377
|
-
assert_equal(expected_string, a_node.to_s,
|
358
|
+
assert_equal(expected_string, a_node.to_s, 'The string representation should be same')
|
378
359
|
|
379
360
|
# Lets now add a child to the previous node, and test the to_s for the child
|
380
|
-
child_node = Tree::TreeNode.new(:Child_node,
|
361
|
+
child_node = Tree::TreeNode.new(:Child_node, 'Child Node')
|
381
362
|
a_node << child_node
|
382
363
|
|
383
|
-
expected_string =
|
384
|
-
assert_equal(expected_string, child_node.to_s,
|
385
|
-
|
364
|
+
expected_string = 'Node Name: Child_node Content: Child Node Parent: Node_Name Children: 0 Total Nodes: 1'
|
365
|
+
assert_equal(expected_string, child_node.to_s, 'The string representation should be same')
|
386
366
|
end
|
387
367
|
|
388
368
|
# Test the first_sibling method.
|
389
369
|
def test_first_sibling
|
390
370
|
setup_test_tree
|
391
371
|
|
392
|
-
#
|
372
|
+
# @todo: Need to fix the first_sibling method to return nil for nodes with no siblings.
|
393
373
|
assert_same(@root, @root.first_sibling, "Root's first sibling is itself")
|
394
374
|
assert_same(@child1, @child1.first_sibling, "Child1's first sibling is itself")
|
395
375
|
assert_same(@child1, @child2.first_sibling, "Child2's first sibling should be child1")
|
@@ -398,26 +378,26 @@ module TestTree
|
|
398
378
|
assert_not_same(@child1, @child4.first_sibling, "Child4's first sibling is itself")
|
399
379
|
end
|
400
380
|
|
401
|
-
# Test the
|
381
|
+
# Test the first_sibling? method.
|
402
382
|
def test_is_first_sibling_eh
|
403
383
|
setup_test_tree
|
404
384
|
|
405
|
-
assert(@root.
|
406
|
-
assert(
|
407
|
-
assert(!@child2.
|
408
|
-
assert(!@child3.
|
409
|
-
assert(
|
385
|
+
assert(@root.first_sibling?, "Root's first sibling is itself")
|
386
|
+
assert(@child1.first_sibling?, "Child1's first sibling is itself")
|
387
|
+
assert(!@child2.first_sibling?, 'Child2 is not the first sibling')
|
388
|
+
assert(!@child3.first_sibling?, 'Child3 is not the first sibling')
|
389
|
+
assert(@child4.first_sibling?, "Child4's first sibling is itself")
|
410
390
|
end
|
411
391
|
|
412
|
-
# Test the
|
392
|
+
# Test the last_sibling? method.
|
413
393
|
def test_is_last_sibling_eh
|
414
394
|
setup_test_tree
|
415
395
|
|
416
|
-
assert(@root.
|
417
|
-
assert(!@child1.
|
418
|
-
assert(!@child2.
|
419
|
-
assert(
|
420
|
-
assert(
|
396
|
+
assert(@root.last_sibling?, "Root's last sibling is itself")
|
397
|
+
assert(!@child1.last_sibling?, 'Child1 is not the last sibling')
|
398
|
+
assert(!@child2.last_sibling?, 'Child2 is not the last sibling')
|
399
|
+
assert(@child3.last_sibling?, "Child3's last sibling is itself")
|
400
|
+
assert(@child4.last_sibling?, "Child4's last sibling is itself")
|
421
401
|
end
|
422
402
|
|
423
403
|
# Test the last_sibling method.
|
@@ -438,81 +418,79 @@ module TestTree
|
|
438
418
|
|
439
419
|
# Lets first collect the siblings in an array.
|
440
420
|
siblings = []
|
441
|
-
result = @child1.siblings { |sibling| siblings << sibling}
|
421
|
+
result = @child1.siblings { |sibling| siblings << sibling }
|
442
422
|
|
443
423
|
assert_equal(@child1, result)
|
444
|
-
assert_equal(2, siblings.length,
|
445
|
-
assert(siblings.include?(@child2),
|
446
|
-
assert(siblings.include?(@child3),
|
424
|
+
assert_equal(2, siblings.length, 'Should have two siblings')
|
425
|
+
assert(siblings.include?(@child2), 'Should have 2nd child as sibling')
|
426
|
+
assert(siblings.include?(@child3), 'Should have 3rd child as sibling')
|
447
427
|
|
448
428
|
siblings.clear
|
449
429
|
siblings = @child1.siblings
|
450
430
|
assert_equal(Array, siblings.class)
|
451
|
-
assert_equal(2, siblings.length,
|
431
|
+
assert_equal(2, siblings.length, 'Should have two siblings')
|
452
432
|
|
453
433
|
siblings.clear
|
454
|
-
@child4.siblings {|sibling| siblings << sibling}
|
455
|
-
assert(siblings.empty?,
|
434
|
+
@child4.siblings { |sibling| siblings << sibling }
|
435
|
+
assert(siblings.empty?, 'Should not have any siblings')
|
456
436
|
|
457
437
|
siblings.clear
|
458
438
|
siblings = @root.siblings
|
459
|
-
assert_equal(0, siblings.length,
|
460
|
-
|
439
|
+
assert_equal(0, siblings.length, 'Root should not have any siblings')
|
461
440
|
end
|
462
441
|
|
463
|
-
# Test the
|
442
|
+
# Test the only_child? method.
|
464
443
|
def test_is_only_child_eh
|
465
444
|
setup_test_tree
|
466
445
|
|
467
|
-
assert(
|
468
|
-
assert(!@child1.
|
469
|
-
assert(!@child2.
|
470
|
-
assert(!@child3.
|
471
|
-
assert(
|
446
|
+
assert(@root.only_child?, 'Root is an only child')
|
447
|
+
assert(!@child1.only_child?, 'Child1 is not the only child')
|
448
|
+
assert(!@child2.only_child?, 'Child2 is not the only child')
|
449
|
+
assert(!@child3.only_child?, 'Child3 is not the only child')
|
450
|
+
assert(@child4.only_child?, 'Child4 is an only child')
|
472
451
|
end
|
473
452
|
|
474
453
|
# Test the next_sibling method.
|
475
454
|
def test_next_sibling
|
476
455
|
setup_test_tree
|
477
456
|
|
478
|
-
assert_nil(@root.next_sibling,
|
457
|
+
assert_nil(@root.next_sibling, 'Root does not have any next sibling')
|
479
458
|
assert_equal(@child2, @child1.next_sibling, "Child1's next sibling is Child2")
|
480
459
|
assert_equal(@child3, @child2.next_sibling, "Child2's next sibling is Child3")
|
481
|
-
assert_nil(@child3.next_sibling,
|
482
|
-
assert_nil(@child4.next_sibling,
|
460
|
+
assert_nil(@child3.next_sibling, 'Child3 does not have a next sibling')
|
461
|
+
assert_nil(@child4.next_sibling, 'Child4 does not have a next sibling')
|
483
462
|
end
|
484
463
|
|
485
464
|
# Test the previous_sibling method.
|
486
465
|
def test_previous_sibling
|
487
466
|
setup_test_tree
|
488
467
|
|
489
|
-
assert_nil(@root.previous_sibling,
|
490
|
-
assert_nil(@child1.previous_sibling,
|
468
|
+
assert_nil(@root.previous_sibling, 'Root does not have any previous sibling')
|
469
|
+
assert_nil(@child1.previous_sibling, 'Child1 does not have previous sibling')
|
491
470
|
assert_equal(@child1, @child2.previous_sibling, "Child2's previous sibling is Child1")
|
492
471
|
assert_equal(@child2, @child3.previous_sibling, "Child3's previous sibling is Child2")
|
493
|
-
assert_nil(@child4.previous_sibling,
|
472
|
+
assert_nil(@child4.previous_sibling, 'Child4 does not have a previous sibling')
|
494
473
|
end
|
495
474
|
|
496
475
|
# Test the add method.
|
497
476
|
def test_add
|
498
|
-
assert(!@root.
|
477
|
+
assert(!@root.children?, 'Should not have any children')
|
499
478
|
|
500
|
-
assert_equal(1, @root.size,
|
479
|
+
assert_equal(1, @root.size, 'Should have 1 node (the root)')
|
501
480
|
@root.add(@child1)
|
502
481
|
|
503
482
|
@root << @child2
|
504
483
|
|
505
|
-
assert(@root.
|
506
|
-
assert_equal(3, @root.size,
|
484
|
+
assert(@root.children?, 'Should have children')
|
485
|
+
assert_equal(3, @root.size, 'Should have three nodes')
|
507
486
|
|
508
487
|
@root << @child3 << @child4
|
509
488
|
|
510
|
-
assert_equal(5, @root.size,
|
511
|
-
assert_equal(2, @child3.size,
|
489
|
+
assert_equal(5, @root.size, 'Should have five nodes')
|
490
|
+
assert_equal(2, @child3.size, 'Should have two nodes')
|
512
491
|
|
513
492
|
# Test the addition of a nil node.
|
514
493
|
assert_raise(ArgumentError) { @root.add(nil) }
|
515
|
-
|
516
494
|
end
|
517
495
|
|
518
496
|
# Test the addition of a duplicate node (duplicate being defined as a node with the same name).
|
@@ -528,11 +506,11 @@ module TestTree
|
|
528
506
|
#
|
529
507
|
# In this case, the two 'deep' nodes should not be considered duplicates
|
530
508
|
|
531
|
-
root = Tree::TreeNode.new(
|
532
|
-
one = Tree::TreeNode.new(
|
533
|
-
two = Tree::TreeNode.new(
|
534
|
-
three= Tree::TreeNode.new(
|
535
|
-
deep = Tree::TreeNode.new(
|
509
|
+
root = Tree::TreeNode.new('root')
|
510
|
+
one = Tree::TreeNode.new('one')
|
511
|
+
two = Tree::TreeNode.new('two')
|
512
|
+
three = Tree::TreeNode.new('three')
|
513
|
+
deep = Tree::TreeNode.new('deep')
|
536
514
|
|
537
515
|
root << one << deep
|
538
516
|
# The same child cannot be added under any circumstance
|
@@ -542,82 +520,91 @@ module TestTree
|
|
542
520
|
begin
|
543
521
|
root << two << deep
|
544
522
|
rescue RuntimeError => e
|
545
|
-
|
523
|
+
raise("Error! The RuntimeError #{e} should not have been thrown. " \
|
524
|
+
'The same node can be added to different branches.')
|
546
525
|
end
|
547
526
|
|
548
|
-
assert_raise(ArgumentError) {root << three << three }
|
527
|
+
assert_raise(ArgumentError) { root << three << three }
|
549
528
|
|
550
|
-
root.remove_all!
|
529
|
+
root.remove_all! # Because the first child 'three' would have been added.
|
551
530
|
begin
|
552
|
-
three_dup = Tree::TreeNode.new(
|
531
|
+
three_dup = Tree::TreeNode.new('three')
|
553
532
|
root << three << three_dup
|
554
533
|
rescue RuntimeError => e
|
555
|
-
|
534
|
+
raise("Error! The RuntimeError #{e} should not have been thrown. The same node name can be used in the branch.")
|
556
535
|
end
|
557
536
|
end
|
558
537
|
|
559
538
|
# Test Addition at a specific position
|
560
539
|
def test_add_at_specific_position
|
561
|
-
assert(!@root.
|
540
|
+
assert(!@root.children?, 'Should not have any children')
|
562
541
|
|
563
|
-
assert_equal(1, @root.size,
|
564
|
-
@root.add(@child1)
|
542
|
+
assert_equal(1, @root.size, 'Should have 1 node (the root)')
|
543
|
+
@root.add(@child1) # First Child added at position 0
|
565
544
|
# Validate that children = [@child1]
|
566
545
|
assert_equal(@child1, @root[0])
|
567
546
|
|
568
|
-
@root << @child2
|
547
|
+
@root << @child2 # Second child appended at position 1.
|
569
548
|
# Validate that children = [@child1, @child2]
|
570
549
|
assert_equal(@child1, @root[0])
|
571
550
|
assert_equal(@child2, @root[1])
|
572
|
-
assert_equal(2, @root.children.size,
|
551
|
+
assert_equal(2, @root.children.size, 'Should have two child nodes')
|
573
552
|
|
574
|
-
@root.add(@child3, 1)
|
553
|
+
@root.add(@child3, 1) # Third child inserted at position 1 (before @child2)
|
575
554
|
# Validate that children = [@child1, @child3, @child2]
|
576
555
|
assert_equal(@child1, @root[0])
|
577
556
|
assert_equal(@child3, @root[1])
|
578
557
|
assert_equal(@child2, @root[2])
|
579
|
-
assert_equal(3, @root.children.size,
|
558
|
+
assert_equal(3, @root.children.size, 'Should have three child nodes')
|
580
559
|
|
581
|
-
@root.add(@child4, @root.children.size)
|
560
|
+
@root.add(@child4, @root.children.size) # Fourth child inserted at the end (equivalent to plain #add(child4)
|
582
561
|
# Validate that children = [@child1, @child3, @child2, @child4]
|
583
562
|
assert_equal(@child1, @root[0])
|
584
563
|
assert_equal(@child3, @root[1])
|
585
564
|
assert_equal(@child2, @root[2])
|
586
565
|
assert_equal(@child4, @root[3])
|
587
|
-
assert_equal(4, @root.children.size,
|
566
|
+
assert_equal(4, @root.children.size, 'Should have four child nodes')
|
588
567
|
|
589
568
|
# Now, a negative test. We are preventing addition to a position that does not exist.
|
590
|
-
assert_raise(RuntimeError)
|
591
|
-
|
592
|
-
|
569
|
+
assert_raise(RuntimeError) do
|
570
|
+
# Fifth child inserted beyond the last position that is valid (at 5th
|
571
|
+
# pos).
|
572
|
+
@root.add(@child5, @root.children.size + 1)
|
573
|
+
end
|
593
574
|
# Validate that we still have children = [@child1, @child3, @child2, @child4]
|
594
575
|
assert_equal(@child1, @root[0])
|
595
576
|
assert_equal(@child3, @root[1])
|
596
577
|
assert_equal(@child2, @root[2])
|
597
578
|
assert_equal(@child4, @root[3])
|
598
579
|
assert_nil(@root[4])
|
599
|
-
assert_equal(4, @root.children.size,
|
600
|
-
|
601
|
-
# Another negative test.
|
602
|
-
|
603
|
-
|
604
|
-
|
580
|
+
assert_equal(4, @root.children.size, 'Should have four child nodes')
|
581
|
+
|
582
|
+
# Another negative test. Lets attempt to add from the end at a position
|
583
|
+
# that is not available
|
584
|
+
assert_raise(RuntimeError) do
|
585
|
+
# Fifth child inserted beyond the first position that is valid; i.e. at
|
586
|
+
# -6
|
587
|
+
@root.add(@child5, -(@root.children.size + 2))
|
588
|
+
end
|
605
589
|
assert_nil(@root[-5])
|
606
590
|
assert_equal(@child1, @root[-4])
|
607
591
|
assert_equal(@child3, @root[-3])
|
608
592
|
assert_equal(@child2, @root[-2])
|
609
593
|
assert_equal(@child4, @root[-1])
|
610
|
-
assert_equal(4, @root.children.size,
|
594
|
+
assert_equal(4, @root.children.size, 'Should have four child nodes')
|
611
595
|
|
612
|
-
# Lets correctly add the fifth child from the end to effectively prepend
|
613
|
-
|
596
|
+
# Lets correctly add the fifth child from the end to effectively prepend
|
597
|
+
# the node.
|
598
|
+
|
599
|
+
# Fifth child inserted beyond the first position; i.e. at -5
|
600
|
+
@root.add(@child5, -(@root.children.size + 1))
|
614
601
|
assert_nil(@root[-6])
|
615
602
|
assert_equal(@child5, @root[-5])
|
616
603
|
assert_equal(@child1, @root[-4])
|
617
604
|
assert_equal(@child3, @root[-3])
|
618
605
|
assert_equal(@child2, @root[-2])
|
619
606
|
assert_equal(@child4, @root[-1])
|
620
|
-
assert_equal(5, @root.children.size,
|
607
|
+
assert_equal(5, @root.children.size, 'Should have five child nodes')
|
621
608
|
end
|
622
609
|
|
623
610
|
# Test the replace! and replace_with! methods
|
@@ -626,40 +613,40 @@ module TestTree
|
|
626
613
|
@root << @child2
|
627
614
|
@root << @child3
|
628
615
|
|
629
|
-
assert_equal(4, @root.size,
|
630
|
-
assert(@root.children.include?(@child1),
|
631
|
-
assert(@root.children.include?(@child2),
|
632
|
-
assert(@root.children.include?(@child3),
|
633
|
-
assert(!@root.children.include?(@child4),
|
616
|
+
assert_equal(4, @root.size, 'Should have four nodes')
|
617
|
+
assert(@root.children.include?(@child1), 'Should parent child1')
|
618
|
+
assert(@root.children.include?(@child2), 'Should parent child2')
|
619
|
+
assert(@root.children.include?(@child3), 'Should parent child3')
|
620
|
+
assert(!@root.children.include?(@child4), 'Should not parent child4')
|
634
621
|
|
635
622
|
@root.replace!(@child2, @child4)
|
636
623
|
|
637
624
|
# Also test replacing with a node of the same name
|
638
625
|
@root.replace! @child4, @child4.detached_copy
|
639
626
|
|
640
|
-
assert_equal(4, @root.size,
|
641
|
-
assert(@root.children.include?(@child1),
|
642
|
-
assert(!@root.children.include?(@child2),
|
643
|
-
assert(@root.children.include?(@child3),
|
644
|
-
assert(@root.children.include?(@child4),
|
645
|
-
assert_equal(1, @root.children.find_index(@child4),
|
627
|
+
assert_equal(4, @root.size, 'Should have three nodes')
|
628
|
+
assert(@root.children.include?(@child1), 'Should parent child1')
|
629
|
+
assert(!@root.children.include?(@child2), 'Should not parent child2')
|
630
|
+
assert(@root.children.include?(@child3), 'Should parent child3')
|
631
|
+
assert(@root.children.include?(@child4), 'Should parent child4')
|
632
|
+
assert_equal(1, @root.children.find_index(@child4), 'Should add child4 to index 1')
|
646
633
|
end
|
647
634
|
|
648
635
|
def test_replace_with
|
649
636
|
@root << @child1
|
650
637
|
@root << @child2
|
651
638
|
|
652
|
-
assert_equal(3, @root.size,
|
653
|
-
assert(@root.children.include?(@child1),
|
654
|
-
assert(@root.children.include?(@child2),
|
655
|
-
assert(!@root.children.include?(@child3),
|
639
|
+
assert_equal(3, @root.size, 'Should have three nodes')
|
640
|
+
assert(@root.children.include?(@child1), 'Should parent child1')
|
641
|
+
assert(@root.children.include?(@child2), 'Should parent child2')
|
642
|
+
assert(!@root.children.include?(@child3), 'Should not parent child3')
|
656
643
|
|
657
644
|
@child2.replace_with @child3
|
658
645
|
|
659
|
-
assert_equal(3, @root.size,
|
660
|
-
assert(@root.children.include?(@child1),
|
661
|
-
assert(!@root.children.include?(@child2),
|
662
|
-
assert(@root.children.include?(@child3),
|
646
|
+
assert_equal(3, @root.size, 'Should have three nodes')
|
647
|
+
assert(@root.children.include?(@child1), 'Should parent child1')
|
648
|
+
assert(!@root.children.include?(@child2), 'Should not parent child2')
|
649
|
+
assert(@root.children.include?(@child3), 'Should parent child3')
|
663
650
|
end
|
664
651
|
|
665
652
|
# Test the remove! and remove_all! methods.
|
@@ -667,57 +654,57 @@ module TestTree
|
|
667
654
|
@root << @child1
|
668
655
|
@root << @child2
|
669
656
|
|
670
|
-
assert(@root.
|
671
|
-
assert_equal(3, @root.size,
|
657
|
+
assert(@root.children?, 'Should have children')
|
658
|
+
assert_equal(3, @root.size, 'Should have three nodes')
|
672
659
|
|
673
660
|
@root.remove!(@child1)
|
674
|
-
assert_equal(2, @root.size,
|
661
|
+
assert_equal(2, @root.size, 'Should have two nodes')
|
675
662
|
@root.remove!(@child2)
|
676
663
|
|
677
|
-
assert(!@root.
|
678
|
-
assert_equal(1, @root.size,
|
664
|
+
assert(!@root.children?, 'Should have no children')
|
665
|
+
assert_equal(1, @root.size, 'Should have one node')
|
679
666
|
|
680
667
|
@root << @child1
|
681
668
|
@root << @child2
|
682
669
|
|
683
|
-
assert(@root.
|
684
|
-
assert_equal(3, @root.size,
|
670
|
+
assert(@root.children?, 'Should have children')
|
671
|
+
assert_equal(3, @root.size, 'Should have three nodes')
|
685
672
|
|
686
673
|
@root.remove_all!
|
687
674
|
|
688
|
-
assert(!@root.
|
689
|
-
assert_equal(1, @root.size,
|
675
|
+
assert(!@root.children?, 'Should have no children')
|
676
|
+
assert_equal(1, @root.size, 'Should have one node')
|
690
677
|
|
691
678
|
# Some negative testing
|
692
679
|
@root.remove!(nil)
|
693
|
-
assert(!@root.
|
694
|
-
assert_equal(1, @root.size,
|
680
|
+
assert(!@root.children?, 'Should have no children')
|
681
|
+
assert_equal(1, @root.size, 'Should have one node')
|
695
682
|
end
|
696
683
|
|
697
684
|
# Test the remove_all! method.
|
698
685
|
def test_remove_all_bang
|
699
686
|
setup_test_tree
|
700
|
-
assert(@root.
|
687
|
+
assert(@root.children?, 'Should have children')
|
701
688
|
@root.remove_all!
|
702
689
|
|
703
|
-
assert(!@root.
|
704
|
-
assert_equal(1, @root.size,
|
690
|
+
assert(!@root.children?, 'Should have no children')
|
691
|
+
assert_equal(1, @root.size, 'Should have one node')
|
705
692
|
end
|
706
693
|
|
707
694
|
# Test the remove_from_parent! method.
|
708
695
|
def test_remove_from_parent_bang
|
709
696
|
setup_test_tree
|
710
697
|
|
711
|
-
assert(@root.
|
712
|
-
assert(!@root.
|
698
|
+
assert(@root.children?, 'Should have children')
|
699
|
+
assert(!@root.leaf?, 'Root is not a leaf here')
|
713
700
|
|
714
701
|
child1 = @root[0]
|
715
|
-
assert_not_nil(child1,
|
702
|
+
assert_not_nil(child1, 'Child 1 should exist')
|
716
703
|
assert_same(@root, child1.root, "Child 1's root should be ROOT")
|
717
|
-
assert(@root.include?(child1),
|
704
|
+
assert(@root.include?(child1), 'root should have child1')
|
718
705
|
child1.remove_from_parent!
|
719
706
|
assert_same(child1, child1.root, "Child 1's root should be self")
|
720
|
-
assert(!@root.include?(child1),
|
707
|
+
assert(!@root.include?(child1), 'root should not have child1')
|
721
708
|
|
722
709
|
child1.remove_from_parent!
|
723
710
|
assert_same(child1, child1.root, "Child 1's root should still be self")
|
@@ -727,36 +714,35 @@ module TestTree
|
|
727
714
|
def test_children
|
728
715
|
setup_test_tree
|
729
716
|
|
730
|
-
assert(@root.
|
731
|
-
assert_equal(5, @root.size,
|
732
|
-
assert(@child3.
|
733
|
-
assert(!@child3.
|
717
|
+
assert(@root.children?, 'Should have children')
|
718
|
+
assert_equal(5, @root.size, 'Should have five nodes')
|
719
|
+
assert(@child3.children?, 'Should have children')
|
720
|
+
assert(!@child3.leaf?, 'Should not be a leaf')
|
734
721
|
|
735
|
-
assert_equal(1, @child3.node_height,
|
736
|
-
|
722
|
+
assert_equal(1, @child3.node_height, 'The subtree at Child 3 should have a height of 1')
|
723
|
+
[@child1, @child2, @child4].each do |child|
|
737
724
|
assert_equal(0, child.node_height, "The subtree at #{child.name} should have a height of 0")
|
738
725
|
end
|
739
726
|
|
740
727
|
result_array = @root.children
|
741
728
|
|
742
|
-
assert_equal(3, result_array.length,
|
743
|
-
assert(!result_array.include?(@root),
|
744
|
-
assert_equal(result_array[0], @child1,
|
745
|
-
assert_equal(result_array[1], @child2,
|
746
|
-
assert_equal(result_array[2], @child3,
|
747
|
-
assert(!result_array.include?(@child4),
|
729
|
+
assert_equal(3, result_array.length, 'Should have three direct children')
|
730
|
+
assert(!result_array.include?(@root), 'Should not have root')
|
731
|
+
assert_equal(result_array[0], @child1, 'Should have child 1')
|
732
|
+
assert_equal(result_array[1], @child2, 'Should have child 2')
|
733
|
+
assert_equal(result_array[2], @child3, 'Should have child 3')
|
734
|
+
assert(!result_array.include?(@child4), 'Should not have child 4')
|
748
735
|
|
749
736
|
# Lets try the block version of the method.
|
750
737
|
result_array.clear
|
751
|
-
result = @root.children {|child| result_array << child}
|
738
|
+
result = @root.children { |child| result_array << child }
|
752
739
|
assert_equal(@root, result)
|
753
740
|
result_array.length
|
754
|
-
assert_equal(3, result_array.length,
|
755
|
-
assert_equal(result_array[0], @child1,
|
756
|
-
assert_equal(result_array[1], @child2,
|
757
|
-
assert_equal(result_array[2], @child3,
|
758
|
-
assert(!result_array.include?(@child4),
|
759
|
-
|
741
|
+
assert_equal(3, result_array.length, 'Should have three children')
|
742
|
+
assert_equal(result_array[0], @child1, 'Should have child 1')
|
743
|
+
assert_equal(result_array[1], @child2, 'Should have child 2')
|
744
|
+
assert_equal(result_array[2], @child3, 'Should have child 3')
|
745
|
+
assert(!result_array.include?(@child4), 'Should not have child 4')
|
760
746
|
end
|
761
747
|
|
762
748
|
# Test the first_child method.
|
@@ -764,7 +750,7 @@ module TestTree
|
|
764
750
|
setup_test_tree
|
765
751
|
|
766
752
|
assert_equal(@child1, @root.first_child, "Root's first child is Child1")
|
767
|
-
assert_nil(@child1.first_child,
|
753
|
+
assert_nil(@child1.first_child, 'Child1 does not have any children')
|
768
754
|
assert_equal(@child4, @child3.first_child, "Child3's first child is Child4")
|
769
755
|
end
|
770
756
|
|
@@ -773,51 +759,51 @@ module TestTree
|
|
773
759
|
setup_test_tree
|
774
760
|
|
775
761
|
assert_equal(@child3, @root.last_child, "Root's last child is Child3")
|
776
|
-
assert_nil(@child1.last_child,
|
762
|
+
assert_nil(@child1.last_child, 'Child1 does not have any children')
|
777
763
|
assert_equal(@child4, @child3.last_child, "Child3's last child is Child4")
|
778
764
|
end
|
779
765
|
|
780
766
|
# Test the find method.
|
781
767
|
def test_find
|
782
768
|
setup_test_tree
|
783
|
-
found_node = @root.find { |node| node == @child2}
|
784
|
-
assert_same(@child2, found_node,
|
769
|
+
found_node = @root.find { |node| node == @child2 }
|
770
|
+
assert_same(@child2, found_node, 'The node should be Child 2')
|
785
771
|
|
786
|
-
found_node = @root.find { |node| node == @child4}
|
787
|
-
assert_same(@child4, found_node,
|
772
|
+
found_node = @root.find { |node| node == @child4 }
|
773
|
+
assert_same(@child4, found_node, 'The node should be Child 4')
|
788
774
|
|
789
|
-
found_node = @root.find { |node| node.name ==
|
790
|
-
assert_same(@child4, found_node,
|
791
|
-
found_node = @root.find { |node| node.name ==
|
792
|
-
assert_nil(found_node,
|
775
|
+
found_node = @root.find { |node| node.name == 'Child4' }
|
776
|
+
assert_same(@child4, found_node, 'The node should be Child 4')
|
777
|
+
found_node = @root.find { |node| node.name == 'NOT PRESENT' }
|
778
|
+
assert_nil(found_node, 'The node should not be found')
|
793
779
|
end
|
794
780
|
|
795
781
|
# Test the parentage method.
|
796
782
|
def test_parentage
|
797
783
|
setup_test_tree
|
798
784
|
|
799
|
-
assert_nil(@root.parentage,
|
800
|
-
assert_equal([@root], @child1.parentage,
|
801
|
-
assert_equal([@child3, @root], @child4.parentage,
|
785
|
+
assert_nil(@root.parentage, 'Root does not have any parentage')
|
786
|
+
assert_equal([@root], @child1.parentage, 'Child1 has Root as its parent')
|
787
|
+
assert_equal([@child3, @root], @child4.parentage, 'Child4 has Child3 and Root as ancestors')
|
802
788
|
end
|
803
789
|
|
804
790
|
# Test the each method.
|
805
791
|
def test_each
|
806
792
|
setup_test_tree
|
807
793
|
|
808
|
-
assert(@root.
|
809
|
-
assert_equal(5, @root.size,
|
810
|
-
assert(@child3.
|
794
|
+
assert(@root.children?, 'Should have children')
|
795
|
+
assert_equal(5, @root.size, 'Should have five nodes')
|
796
|
+
assert(@child3.children?, 'Should have children')
|
811
797
|
|
812
798
|
nodes = []
|
813
799
|
@root.each { |node| nodes << node }
|
814
800
|
|
815
|
-
assert_equal(5, nodes.length,
|
816
|
-
assert(nodes.include?(@root),
|
817
|
-
assert(nodes.include?(@child1),
|
818
|
-
assert(nodes.include?(@child2),
|
819
|
-
assert(nodes.include?(@child3),
|
820
|
-
assert(nodes.include?(@child4),
|
801
|
+
assert_equal(5, nodes.length, 'Should have FIVE NODES')
|
802
|
+
assert(nodes.include?(@root), 'Should have root')
|
803
|
+
assert(nodes.include?(@child1), 'Should have child 1')
|
804
|
+
assert(nodes.include?(@child2), 'Should have child 2')
|
805
|
+
assert(nodes.include?(@child3), 'Should have child 3')
|
806
|
+
assert(nodes.include?(@child4), 'Should have child 4')
|
821
807
|
end
|
822
808
|
|
823
809
|
# Test the each_leaf method.
|
@@ -827,24 +813,23 @@ module TestTree
|
|
827
813
|
result_array = []
|
828
814
|
result = @root.each_leaf { |node| result_array << node }
|
829
815
|
assert_equal(@root, result)
|
830
|
-
assert_equal(3, result_array.length,
|
831
|
-
assert(!result_array.include?(@root),
|
832
|
-
assert(result_array.include?(@child1),
|
833
|
-
assert(result_array.include?(@child2),
|
834
|
-
assert(!result_array.include?(@child3),
|
835
|
-
assert(result_array.include?(@child4),
|
816
|
+
assert_equal(3, result_array.length, 'Should have THREE LEAF NODES')
|
817
|
+
assert(!result_array.include?(@root), 'Should not have root')
|
818
|
+
assert(result_array.include?(@child1), 'Should have child 1')
|
819
|
+
assert(result_array.include?(@child2), 'Should have child 2')
|
820
|
+
assert(!result_array.include?(@child3), 'Should not have child 3')
|
821
|
+
assert(result_array.include?(@child4), 'Should have child 4')
|
836
822
|
|
837
823
|
# Now lets try without the block
|
838
824
|
result_array.clear
|
839
825
|
result_array = @root.each_leaf
|
840
826
|
assert_equal(Array, result_array.class)
|
841
|
-
assert_equal(3, result_array.length,
|
842
|
-
assert(!result_array.include?(@root),
|
843
|
-
assert(result_array.include?(@child1),
|
844
|
-
assert(result_array.include?(@child2),
|
845
|
-
assert(!result_array.include?(@child3),
|
846
|
-
assert(result_array.include?(@child4),
|
847
|
-
|
827
|
+
assert_equal(3, result_array.length, 'Should have THREE LEAF NODES')
|
828
|
+
assert(!result_array.include?(@root), 'Should not have root')
|
829
|
+
assert(result_array.include?(@child1), 'Should have child 1')
|
830
|
+
assert(result_array.include?(@child2), 'Should have child 2')
|
831
|
+
assert(!result_array.include?(@child3), 'Should not have child 3')
|
832
|
+
assert(result_array.include?(@child4), 'Should have child 4')
|
848
833
|
end
|
849
834
|
|
850
835
|
# Test the parent method.
|
@@ -852,38 +837,38 @@ module TestTree
|
|
852
837
|
setup_test_tree
|
853
838
|
|
854
839
|
assert_nil(@root.parent, "Root's parent should be nil")
|
855
|
-
assert_equal(@root, @child1.parent,
|
856
|
-
assert_equal(@root, @child3.parent,
|
857
|
-
assert_equal(@child3, @child4.parent,
|
858
|
-
assert_equal(@root, @child4.parent.parent,
|
840
|
+
assert_equal(@root, @child1.parent, 'Parent should be root')
|
841
|
+
assert_equal(@root, @child3.parent, 'Parent should be root')
|
842
|
+
assert_equal(@child3, @child4.parent, 'Parent should be child3')
|
843
|
+
assert_equal(@root, @child4.parent.parent, 'Parent should be root')
|
859
844
|
end
|
860
845
|
|
861
846
|
# Test the [] method.
|
862
847
|
def test_indexed_access
|
863
848
|
setup_test_tree
|
864
849
|
|
865
|
-
assert_equal(@child1, @root[0],
|
866
|
-
assert_equal(@child4, @root[2][0],
|
867
|
-
assert_nil(@root[
|
868
|
-
assert_nil(@root[99],
|
850
|
+
assert_equal(@child1, @root[0], 'Should be the first child')
|
851
|
+
assert_equal(@child4, @root[2][0], 'Should be the grandchild')
|
852
|
+
assert_nil(@root['TEST'], 'Should be nil')
|
853
|
+
assert_nil(@root[99], 'Should be nil')
|
869
854
|
assert_raise(ArgumentError) { @root[nil] }
|
870
855
|
end
|
871
856
|
|
872
857
|
# Test the print_tree method.
|
873
858
|
def test_print_tree
|
874
859
|
setup_test_tree
|
875
|
-
#puts
|
876
|
-
|
860
|
+
# puts
|
861
|
+
# @root.print_tree
|
877
862
|
end
|
878
863
|
|
879
864
|
# Tests the binary dumping mechanism with an Object content node
|
880
865
|
def test_marshal_dump
|
881
866
|
# Setup Test Data
|
882
|
-
test_root = Tree::TreeNode.new(
|
883
|
-
test_content = {
|
884
|
-
test_child
|
885
|
-
test_content2 = [
|
886
|
-
test_grand_child = Tree::TreeNode.new(
|
867
|
+
test_root = Tree::TreeNode.new('ROOT', 'Root Node')
|
868
|
+
test_content = { 'KEY1' => 'Value1', 'KEY2' => 'Value2' }
|
869
|
+
test_child = Tree::TreeNode.new('Child', test_content)
|
870
|
+
test_content2 = %w[AValue1 AValue2 AValue3]
|
871
|
+
test_grand_child = Tree::TreeNode.new('Grand Child 1', test_content2)
|
887
872
|
test_root << test_child << test_grand_child
|
888
873
|
|
889
874
|
# Perform the test operation
|
@@ -891,30 +876,30 @@ module TestTree
|
|
891
876
|
new_root = Marshal.load(data) # And unmarshal
|
892
877
|
|
893
878
|
# Test the root node
|
894
|
-
assert_equal(test_root.name, new_root.name,
|
879
|
+
assert_equal(test_root.name, new_root.name, 'Must identify as ROOT')
|
895
880
|
assert_equal(test_root.content, new_root.content, "Must have root's content")
|
896
|
-
assert(new_root.
|
897
|
-
assert(new_root.
|
881
|
+
assert(new_root.root?, 'Must be the ROOT node')
|
882
|
+
assert(new_root.children?, 'Must have a child node')
|
898
883
|
|
899
884
|
# Test the child node
|
900
885
|
new_child = new_root[test_child.name]
|
901
|
-
assert_equal(test_child.name, new_child.name,
|
902
|
-
assert(new_child.
|
903
|
-
assert(new_child.
|
886
|
+
assert_equal(test_child.name, new_child.name, 'Must have child 1')
|
887
|
+
assert(new_child.content?, 'Child must have content')
|
888
|
+
assert(new_child.only_child?, 'Child must be the only child')
|
904
889
|
|
905
890
|
new_child_content = new_child.content
|
906
891
|
assert_equal(Hash, new_child_content.class, "Class of child's content should be a hash")
|
907
|
-
assert_equal(test_child.content.size, new_child_content.size,
|
892
|
+
assert_equal(test_child.content.size, new_child_content.size, 'The content should have same size')
|
908
893
|
|
909
894
|
# Test the grand-child node
|
910
895
|
new_grand_child = new_child[test_grand_child.name]
|
911
|
-
assert_equal(test_grand_child.name, new_grand_child.name,
|
912
|
-
assert(new_grand_child.
|
913
|
-
assert(new_grand_child.
|
896
|
+
assert_equal(test_grand_child.name, new_grand_child.name, 'Must have grand child')
|
897
|
+
assert(new_grand_child.content?, 'Grand-child must have content')
|
898
|
+
assert(new_grand_child.only_child?, 'Grand-child must be the only child')
|
914
899
|
|
915
900
|
new_grand_child_content = new_grand_child.content
|
916
901
|
assert_equal(Array, new_grand_child_content.class, "Class of grand-child's content should be an Array")
|
917
|
-
assert_equal(test_grand_child.content.size, new_grand_child_content.size,
|
902
|
+
assert_equal(test_grand_child.content.size, new_grand_child_content.size, 'The content should have same size')
|
918
903
|
end
|
919
904
|
|
920
905
|
# marshal_load and marshal_dump are symmetric methods
|
@@ -925,61 +910,38 @@ module TestTree
|
|
925
910
|
def test_collect
|
926
911
|
setup_test_tree
|
927
912
|
collect_array = @root.collect do |node|
|
928
|
-
node.content =
|
913
|
+
node.content = 'abc'
|
929
914
|
node
|
930
915
|
end
|
931
|
-
collect_array.each {|node| assert_equal(
|
916
|
+
collect_array.each { |node| assert_equal('abc', node.content, "Should be 'abc'") }
|
932
917
|
end
|
933
918
|
|
934
919
|
# Test freezing the tree
|
935
920
|
def test_freeze_tree_bang
|
936
921
|
setup_test_tree
|
937
922
|
|
938
|
-
@root.content =
|
939
|
-
assert_equal(
|
923
|
+
@root.content = 'ABC'
|
924
|
+
assert_equal('ABC', @root.content, "Content should be 'ABC'")
|
940
925
|
@root.freeze_tree!
|
941
|
-
#
|
926
|
+
# NOTE: The error raised here depends on the Ruby version.
|
927
|
+
# For Ruby > 2.5, FrozenError is raised
|
942
928
|
# For Ruby > 1.9, RuntimeError is raised
|
943
929
|
# For Ruby ~ 1.8, TypeError is raised
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
@root.content = pers
|
952
|
-
assert_same(pers, @root.content, "Content should be the same")
|
953
|
-
end
|
954
|
-
|
955
|
-
# Test the depth computation algorithm. Note that this is an incorrect computation and actually returns height+1
|
956
|
-
# instead of depth. This method has been deprecated in this release and may be removed in the future.
|
957
|
-
def test_depth
|
958
|
-
begin
|
959
|
-
require 'structured_warnings'
|
960
|
-
assert_warn(DeprecatedMethodWarning) { do_deprecated_depth }
|
961
|
-
rescue LoadError
|
962
|
-
# Since the structued_warnings package is not present, we revert to good old Kernel#warn behavior.
|
963
|
-
do_deprecated_depth
|
930
|
+
require 'rubygems' # Only needed for ruby pre-1.9.0 but it's safe for later versions (evaluates to false).
|
931
|
+
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5')
|
932
|
+
assert_raise(RuntimeError, TypeError) { @root.content = '123' }
|
933
|
+
assert_raise(RuntimeError, TypeError) { @root[0].content = '123' }
|
934
|
+
else
|
935
|
+
assert_raise(FrozenError) { @root.content = '123' }
|
936
|
+
assert_raise(FrozenError) { @root[0].content = '123' }
|
964
937
|
end
|
965
938
|
end
|
966
939
|
|
967
|
-
#
|
968
|
-
def
|
969
|
-
|
970
|
-
|
971
|
-
@root
|
972
|
-
assert_equal(2, @root.depth, "This should be of depth 2")
|
973
|
-
|
974
|
-
@root << @child2
|
975
|
-
assert_equal(2, @root.depth, "This should be of depth 2")
|
976
|
-
|
977
|
-
@child2 << @child3
|
978
|
-
assert_equal(3, @root.depth, "This should be of depth 3")
|
979
|
-
assert_equal(2, @child2.depth, "This should be of depth 2")
|
980
|
-
|
981
|
-
@child3 << @child4
|
982
|
-
assert_equal(4, @root.depth, "This should be of depth 4")
|
940
|
+
# Test whether the content is accessible
|
941
|
+
def test_content
|
942
|
+
person = Person.new('John', 'Doe')
|
943
|
+
@root.content = person
|
944
|
+
assert_same(person, @root.content, 'Content should be the same')
|
983
945
|
end
|
984
946
|
|
985
947
|
# Test the height computation algorithm
|
@@ -987,55 +949,54 @@ module TestTree
|
|
987
949
|
assert_equal(0, @root.node_height, "A single node's height is 0")
|
988
950
|
|
989
951
|
@root << @child1
|
990
|
-
assert_equal(1, @root.node_height,
|
991
|
-
assert_equal(0, @child1.node_height,
|
952
|
+
assert_equal(1, @root.node_height, 'This should be of height 1')
|
953
|
+
assert_equal(0, @child1.node_height, 'This should be of height 0')
|
992
954
|
|
993
955
|
@root << @child2
|
994
|
-
assert_equal(1, @root.node_height,
|
995
|
-
assert_equal(0, @child2.node_height,
|
956
|
+
assert_equal(1, @root.node_height, 'This should be of height 1')
|
957
|
+
assert_equal(0, @child2.node_height, 'This should be of height 0')
|
996
958
|
|
997
959
|
@child2 << @child3
|
998
|
-
assert_equal(2, @root.node_height,
|
999
|
-
assert_equal(1, @child2.node_height,
|
1000
|
-
assert_equal(0, @child3.node_height,
|
960
|
+
assert_equal(2, @root.node_height, 'This should be of height 2')
|
961
|
+
assert_equal(1, @child2.node_height, 'This should be of height 1')
|
962
|
+
assert_equal(0, @child3.node_height, 'This should be of height 0')
|
1001
963
|
|
1002
964
|
@child3 << @child4
|
1003
|
-
assert_equal(3, @root.node_height,
|
1004
|
-
assert_equal(2, @child2.node_height,
|
1005
|
-
assert_equal(1, @child3.node_height,
|
1006
|
-
assert_equal(0, @child4.node_height,
|
965
|
+
assert_equal(3, @root.node_height, 'This should be of height 3')
|
966
|
+
assert_equal(2, @child2.node_height, 'This should be of height 2')
|
967
|
+
assert_equal(1, @child3.node_height, 'This should be of height 1')
|
968
|
+
assert_equal(0, @child4.node_height, 'This should be of height 0')
|
1007
969
|
end
|
1008
970
|
|
1009
|
-
# Test the depth computation algorithm.
|
1010
|
-
# Tree::TreeNode#depth was incorrectly computing the height of the node - instead of its depth.
|
971
|
+
# Test the depth computation algorithm.
|
1011
972
|
def test_node_depth
|
1012
973
|
assert_equal(0, @root.node_depth, "A root node's depth is 0")
|
1013
974
|
|
1014
975
|
setup_test_tree
|
1015
976
|
|
1016
|
-
|
977
|
+
[@child1, @child2, @child3].each do |child|
|
1017
978
|
assert_equal(1, child.node_depth, "Node #{child.name} should have depth 1")
|
1018
979
|
end
|
1019
980
|
|
1020
|
-
assert_equal(2, @child4.node_depth,
|
981
|
+
assert_equal(2, @child4.node_depth, 'Child 4 should have depth 2')
|
1021
982
|
|
1022
983
|
@root << @child5 << @child3
|
1023
|
-
assert_equal(3, @child4.node_depth,
|
984
|
+
assert_equal(3, @child4.node_depth, 'Child 4 should have depth 3 after Child 5 inserted above')
|
1024
985
|
end
|
1025
986
|
|
1026
987
|
# Test the level method. Since this is an alias of node_depth, we just test for equivalence
|
1027
988
|
def test_level
|
1028
989
|
assert_equal(0, @root.level, "A root node's level is 0")
|
1029
990
|
|
1030
|
-
assert_equal(@root.node_depth, @root.level,
|
991
|
+
assert_equal(@root.node_depth, @root.level, 'Level and depth should be the same')
|
1031
992
|
|
1032
993
|
setup_test_tree
|
1033
|
-
|
994
|
+
[@child1, @child2, @child3].each do |child|
|
1034
995
|
assert_equal(1, child.level, "Node #{child.name} should have level 1")
|
1035
|
-
assert_equal(@root.node_depth, @root.level,
|
996
|
+
assert_equal(@root.node_depth, @root.level, 'Level and depth should be the same')
|
1036
997
|
end
|
1037
998
|
|
1038
|
-
assert_equal(2, @child4.level,
|
999
|
+
assert_equal(2, @child4.level, 'Child 4 should have level 2')
|
1039
1000
|
end
|
1040
1001
|
|
1041
1002
|
# Test the breadth computation algorithm
|
@@ -1043,29 +1004,29 @@ module TestTree
|
|
1043
1004
|
assert_equal(1, @root.breadth, "A single node's breadth is 1")
|
1044
1005
|
|
1045
1006
|
@root << @child1
|
1046
|
-
assert_equal(1, @root.breadth,
|
1007
|
+
assert_equal(1, @root.breadth, 'This should be of breadth 1')
|
1047
1008
|
|
1048
1009
|
@root << @child2
|
1049
|
-
assert_equal(2, @child1.breadth,
|
1050
|
-
assert_equal(2, @child2.breadth,
|
1010
|
+
assert_equal(2, @child1.breadth, 'This should be of breadth 2')
|
1011
|
+
assert_equal(2, @child2.breadth, 'This should be of breadth 2')
|
1051
1012
|
|
1052
1013
|
@root << @child3
|
1053
|
-
assert_equal(3, @child1.breadth,
|
1054
|
-
assert_equal(3, @child2.breadth,
|
1014
|
+
assert_equal(3, @child1.breadth, 'This should be of breadth 3')
|
1015
|
+
assert_equal(3, @child2.breadth, 'This should be of breadth 3')
|
1055
1016
|
|
1056
1017
|
@child3 << @child4
|
1057
|
-
assert_equal(1, @child4.breadth,
|
1018
|
+
assert_equal(1, @child4.breadth, 'This should be of breadth 1')
|
1058
1019
|
end
|
1059
1020
|
|
1060
1021
|
# Test the breadth for each
|
1061
1022
|
def test_breadth_each
|
1062
|
-
j = Tree::TreeNode.new(
|
1063
|
-
f = Tree::TreeNode.new(
|
1064
|
-
k = Tree::TreeNode.new(
|
1065
|
-
a = Tree::TreeNode.new(
|
1066
|
-
d = Tree::TreeNode.new(
|
1067
|
-
h = Tree::TreeNode.new(
|
1068
|
-
z = Tree::TreeNode.new(
|
1023
|
+
j = Tree::TreeNode.new('j')
|
1024
|
+
f = Tree::TreeNode.new('f')
|
1025
|
+
k = Tree::TreeNode.new('k')
|
1026
|
+
a = Tree::TreeNode.new('a')
|
1027
|
+
d = Tree::TreeNode.new('d')
|
1028
|
+
h = Tree::TreeNode.new('h')
|
1029
|
+
z = Tree::TreeNode.new('z')
|
1069
1030
|
|
1070
1031
|
# The expected order of response
|
1071
1032
|
expected_array = [j,
|
@@ -1089,32 +1050,32 @@ module TestTree
|
|
1089
1050
|
result_array = []
|
1090
1051
|
result = j.breadth_each { |node| result_array << node.detached_copy }
|
1091
1052
|
|
1092
|
-
assert_equal(j, result)
|
1053
|
+
assert_equal(j, result) # The invocation target is returned
|
1093
1054
|
|
1094
1055
|
expected_array.each_index do |i|
|
1095
|
-
assert_equal(expected_array[i].name, result_array[i].name)
|
1056
|
+
assert_equal(expected_array[i].name, result_array[i].name) # Match only the names.
|
1096
1057
|
end
|
1097
1058
|
|
1098
|
-
assert_equal(Enumerator, j.breadth_each.class) if defined?(Enumerator.class
|
1099
|
-
|
1059
|
+
assert_equal(Enumerator, j.breadth_each.class) if defined?(Enumerator.class) # Without a block
|
1060
|
+
# Without a block
|
1061
|
+
assert_equal(Enumerable::Enumerator, j.breadth_each.class) if defined?(Enumerable::Enumerator.class)
|
1100
1062
|
|
1101
1063
|
# Now test without a block
|
1102
|
-
result_array = j.breadth_each.collect { |node| node}
|
1064
|
+
result_array = j.breadth_each.collect { |node| node }
|
1103
1065
|
expected_array.each_index do |i|
|
1104
|
-
assert_equal(expected_array[i].name, result_array[i].name)
|
1066
|
+
assert_equal(expected_array[i].name, result_array[i].name) # Match only the names.
|
1105
1067
|
end
|
1106
|
-
|
1107
1068
|
end
|
1108
1069
|
|
1109
1070
|
# Test the preordered_each method.
|
1110
1071
|
def test_preordered_each
|
1111
|
-
j = Tree::TreeNode.new(
|
1112
|
-
f = Tree::TreeNode.new(
|
1113
|
-
k = Tree::TreeNode.new(
|
1114
|
-
a = Tree::TreeNode.new(
|
1115
|
-
d = Tree::TreeNode.new(
|
1116
|
-
h = Tree::TreeNode.new(
|
1117
|
-
z = Tree::TreeNode.new(
|
1072
|
+
j = Tree::TreeNode.new('j')
|
1073
|
+
f = Tree::TreeNode.new('f')
|
1074
|
+
k = Tree::TreeNode.new('k')
|
1075
|
+
a = Tree::TreeNode.new('a')
|
1076
|
+
d = Tree::TreeNode.new('d')
|
1077
|
+
h = Tree::TreeNode.new('h')
|
1078
|
+
z = Tree::TreeNode.new('z')
|
1118
1079
|
|
1119
1080
|
# The expected order of response
|
1120
1081
|
expected_array = [j, f, a, d, h, k, z]
|
@@ -1132,28 +1093,31 @@ module TestTree
|
|
1132
1093
|
j << k << z
|
1133
1094
|
|
1134
1095
|
result_array = []
|
1135
|
-
result = j.preordered_each { |node| result_array << node.detached_copy}
|
1096
|
+
result = j.preordered_each { |node| result_array << node.detached_copy }
|
1136
1097
|
|
1137
|
-
assert_equal(j, result)
|
1098
|
+
assert_equal(j, result) # Each returns the invocation target
|
1138
1099
|
|
1139
1100
|
expected_array.each_index do |i|
|
1140
1101
|
# Match only the names.
|
1141
1102
|
assert_equal(expected_array[i].name, result_array[i].name)
|
1142
1103
|
end
|
1143
1104
|
|
1144
|
-
assert_equal(Enumerator, j.preordered_each.class)
|
1145
|
-
|
1105
|
+
assert_equal(Enumerator, j.preordered_each.class) \
|
1106
|
+
if defined?(Enumerator.class) # Without a block
|
1107
|
+
|
1108
|
+
assert_equal(Enumerable::Enumerator, j.preordered_each.class) \
|
1109
|
+
if defined?(Enumerable::Enumerator.class)
|
1146
1110
|
end
|
1147
1111
|
|
1148
1112
|
# Test the postordered_each method.
|
1149
1113
|
def test_postordered_each
|
1150
|
-
j = Tree::TreeNode.new(
|
1151
|
-
f = Tree::TreeNode.new(
|
1152
|
-
k = Tree::TreeNode.new(
|
1153
|
-
a = Tree::TreeNode.new(
|
1154
|
-
d = Tree::TreeNode.new(
|
1155
|
-
h = Tree::TreeNode.new(
|
1156
|
-
z = Tree::TreeNode.new(
|
1114
|
+
j = Tree::TreeNode.new('j')
|
1115
|
+
f = Tree::TreeNode.new('f')
|
1116
|
+
k = Tree::TreeNode.new('k')
|
1117
|
+
a = Tree::TreeNode.new('a')
|
1118
|
+
d = Tree::TreeNode.new('d')
|
1119
|
+
h = Tree::TreeNode.new('h')
|
1120
|
+
z = Tree::TreeNode.new('z')
|
1157
1121
|
|
1158
1122
|
# The expected order of response
|
1159
1123
|
expected_array = [d, a, h, f, z, k, j]
|
@@ -1172,9 +1136,9 @@ module TestTree
|
|
1172
1136
|
|
1173
1137
|
# Test when a block is given
|
1174
1138
|
result_array = []
|
1175
|
-
result = j.postordered_each { |node| result_array << node.detached_copy}
|
1139
|
+
result = j.postordered_each { |node| result_array << node.detached_copy }
|
1176
1140
|
|
1177
|
-
assert_equal(j, result)
|
1141
|
+
assert_equal(j, result) # The invocation target is returned
|
1178
1142
|
|
1179
1143
|
expected_array.each_index do |i|
|
1180
1144
|
# Match only the names.
|
@@ -1182,7 +1146,9 @@ module TestTree
|
|
1182
1146
|
end
|
1183
1147
|
|
1184
1148
|
assert_equal(Enumerator, j.postordered_each.class) if defined?(Enumerator.class) # Without a block
|
1185
|
-
|
1149
|
+
|
1150
|
+
assert_equal(Enumerable::Enumerator, j.postordered_each.class) \
|
1151
|
+
if defined?(Enumerable::Enumerator.class)
|
1186
1152
|
|
1187
1153
|
# Now test without a block
|
1188
1154
|
result_array = j.postordered_each.collect { |node| node }
|
@@ -1191,85 +1157,89 @@ module TestTree
|
|
1191
1157
|
# Match only the names.
|
1192
1158
|
assert_equal(expected_array[i].name, result_array[i].name)
|
1193
1159
|
end
|
1194
|
-
|
1195
1160
|
end
|
1196
1161
|
|
1197
1162
|
# test the detached_copy method.
|
1198
1163
|
def test_detached_copy
|
1199
1164
|
setup_test_tree
|
1200
1165
|
|
1201
|
-
assert(@root.
|
1166
|
+
assert(@root.children?, 'The root should have children')
|
1202
1167
|
copy_of_root = @root.detached_copy
|
1203
|
-
assert(!copy_of_root.
|
1204
|
-
assert_equal(@root.name, copy_of_root.name,
|
1168
|
+
assert(!copy_of_root.children?, 'The copy should not have children')
|
1169
|
+
assert_equal(@root.name, copy_of_root.name, 'The names should be equal')
|
1205
1170
|
|
1206
1171
|
# Try the same test with a child node
|
1207
|
-
assert(!@child3.
|
1208
|
-
assert(@child3.
|
1172
|
+
assert(!@child3.root?, 'Child 3 is not a root')
|
1173
|
+
assert(@child3.children?, 'Child 3 has children')
|
1209
1174
|
copy_of_child3 = @child3.detached_copy
|
1210
|
-
assert(copy_of_child3.
|
1211
|
-
assert(!copy_of_child3.
|
1175
|
+
assert(copy_of_child3.root?, "Child 3's copy is a root")
|
1176
|
+
assert(!copy_of_child3.children?, "Child 3's copy does not have children")
|
1212
1177
|
end
|
1213
1178
|
|
1214
1179
|
# Test the detached_subtree_copy method.
|
1215
1180
|
def test_detached_subtree_copy
|
1216
1181
|
setup_test_tree
|
1217
1182
|
|
1218
|
-
assert(@root.
|
1183
|
+
assert(@root.children?, 'The root should have children.')
|
1219
1184
|
tree_copy = @root.detached_subtree_copy
|
1220
1185
|
|
1221
|
-
assert_equal(@root.name, tree_copy.name,
|
1222
|
-
assert_not_equal(@root.object_id, tree_copy.object_id,
|
1223
|
-
assert(tree_copy.
|
1224
|
-
assert(tree_copy.
|
1225
|
-
assert_equal(tree_copy.children.count, @root.children.count,
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
assert(
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1186
|
+
assert_equal(@root.name, tree_copy.name, 'The names should be equal.')
|
1187
|
+
assert_not_equal(@root.object_id, tree_copy.object_id, 'Object_ids should differ.')
|
1188
|
+
assert(tree_copy.root?, 'Copied root should be a root node.')
|
1189
|
+
assert(tree_copy.children?, 'Copied tree should have children.')
|
1190
|
+
assert_equal(tree_copy.children.count, @root.children.count,
|
1191
|
+
'Copied tree and the original tree should have same number of children.')
|
1192
|
+
|
1193
|
+
assert_equal(tree_copy[0].name, @child1.name, 'The names of Child1 (original and copy) should be same.')
|
1194
|
+
assert_not_equal(tree_copy[0].object_id, @child1.object_id,
|
1195
|
+
'Child1 Object_ids (original and copy) should differ.')
|
1196
|
+
assert(!tree_copy[0].root?, 'Child1 copied should not be root.')
|
1197
|
+
assert(!tree_copy[0].children?, 'Child1 copied should not have children.')
|
1198
|
+
|
1199
|
+
assert_equal(tree_copy[1].name, @child2.name, 'The names of Child2 (original and copy) should be same.')
|
1200
|
+
assert_not_equal(tree_copy[1].object_id, @child2.object_id,
|
1201
|
+
'Child2 Object_ids (original and copy) should differ.')
|
1202
|
+
assert(!tree_copy[1].root?, 'Child2 copied should not be root.')
|
1203
|
+
assert(!tree_copy[1].children?, 'Child2 copied should not have children.')
|
1204
|
+
|
1205
|
+
assert_equal(tree_copy[2].name, @child3.name, 'The names of Child3 (original and copy) should be same.')
|
1206
|
+
assert_not_equal(tree_copy[2].object_id, @child3.object_id,
|
1207
|
+
'Child3 Object_ids (original and copy) should differ.')
|
1208
|
+
assert(!tree_copy[2].root?, 'Child3 copied should not be root.')
|
1209
|
+
assert(tree_copy[2].children?, 'Child3 copied should have children.')
|
1210
|
+
|
1211
|
+
assert_equal(tree_copy[2][0].name, @child4.name, 'The names of Child4 (original and copy) should be same.')
|
1212
|
+
assert_not_equal(tree_copy[2][0].object_id, @child4.object_id,
|
1213
|
+
'Child4 Object_ids (original and copy) should differ.')
|
1214
|
+
assert(!tree_copy[2][0].root?, 'Child4 copied should not be root.')
|
1215
|
+
assert(!tree_copy[2][0].children?, 'Child4 copied should not have children.')
|
1216
|
+
end
|
1217
|
+
|
1218
|
+
# Test the children? method.
|
1249
1219
|
def test_has_children_eh
|
1250
1220
|
setup_test_tree
|
1251
|
-
assert(@root.
|
1221
|
+
assert(@root.children?, 'The Root node MUST have children')
|
1252
1222
|
end
|
1253
1223
|
|
1254
|
-
# test the
|
1224
|
+
# test the leaf? method.
|
1255
1225
|
def test_is_leaf_eh
|
1256
1226
|
setup_test_tree
|
1257
|
-
assert(!@child3.
|
1258
|
-
assert(@child4.
|
1227
|
+
assert(!@child3.leaf?, 'Child 3 is not a leaf node')
|
1228
|
+
assert(@child4.leaf?, 'Child 4 is a leaf node')
|
1259
1229
|
end
|
1260
1230
|
|
1261
|
-
# Test the
|
1231
|
+
# Test the root? method.
|
1262
1232
|
def test_is_root_eh
|
1263
1233
|
setup_test_tree
|
1264
|
-
assert(@root.
|
1234
|
+
assert(@root.root?, 'The ROOT node must respond as the root node')
|
1265
1235
|
end
|
1266
1236
|
|
1267
1237
|
# Test the content= method.
|
1268
1238
|
def test_content_equals
|
1269
1239
|
@root.content = nil
|
1270
1240
|
assert_nil(@root.content, "Root's content should be nil")
|
1271
|
-
@root.content =
|
1272
|
-
assert_equal(
|
1241
|
+
@root.content = 'dummy content'
|
1242
|
+
assert_equal('dummy content', @root.content, "Root's content should now be 'dummy content'")
|
1273
1243
|
end
|
1274
1244
|
|
1275
1245
|
# Test the size method.
|
@@ -1282,35 +1252,36 @@ module TestTree
|
|
1282
1252
|
end
|
1283
1253
|
|
1284
1254
|
# Test the << method.
|
1285
|
-
def test_lt2
|
1255
|
+
def test_lt2
|
1286
1256
|
@root << @child1
|
1287
1257
|
@root << @child2
|
1288
1258
|
@root << @child3 << @child4
|
1289
|
-
assert_not_nil(@root['Child1'],
|
1290
|
-
assert_not_nil(@root['Child2'],
|
1291
|
-
assert_not_nil(@root['Child3'],
|
1292
|
-
assert_not_nil(@child3['Child4'],
|
1259
|
+
assert_not_nil(@root['Child1'], 'Child 1 should have been added to Root')
|
1260
|
+
assert_not_nil(@root['Child2'], 'Child 2 should have been added to Root')
|
1261
|
+
assert_not_nil(@root['Child3'], 'Child 3 should have been added to Root')
|
1262
|
+
assert_not_nil(@child3['Child4'], 'Child 4 should have been added to Child3')
|
1293
1263
|
end
|
1294
1264
|
|
1295
1265
|
# Test the [] method.
|
1296
|
-
def test_index
|
1297
|
-
assert_raise(ArgumentError) {@root[nil]}
|
1266
|
+
def test_index
|
1267
|
+
assert_raise(ArgumentError) { @root[nil] }
|
1298
1268
|
|
1299
1269
|
@root << @child1
|
1300
1270
|
@root << @child2
|
1301
|
-
assert_equal(@child1.name, @root['Child1'].name,
|
1302
|
-
assert_equal(@child1.name, @root[0].name,
|
1303
|
-
assert_equal(@child1.name, @root[-2].name,
|
1304
|
-
assert_equal(@child1.name,
|
1271
|
+
assert_equal(@child1.name, @root['Child1'].name, 'Child 1 should be returned')
|
1272
|
+
assert_equal(@child1.name, @root[0].name, 'Child 1 should be returned')
|
1273
|
+
assert_equal(@child1.name, @root[-2].name, 'Child 1 should be returned') # Negative access also works
|
1274
|
+
assert_equal(@child1.name,
|
1275
|
+
@root[-@root.children.size].name, 'Child 1 should be returned') # Negative access also works
|
1305
1276
|
|
1306
|
-
assert_equal(@child2.name, @root['Child2'].name,
|
1307
|
-
assert_equal(@child2.name, @root[1].name,
|
1308
|
-
assert_equal(@child2.name, @root[-1].name,
|
1277
|
+
assert_equal(@child2.name, @root['Child2'].name, 'Child 2 should be returned')
|
1278
|
+
assert_equal(@child2.name, @root[1].name, 'Child 2 should be returned')
|
1279
|
+
assert_equal(@child2.name, @root[-1].name, 'Child 2 should be returned') # Negative access also works
|
1309
1280
|
|
1310
|
-
assert_nil(@root['Some Random Name'],
|
1311
|
-
assert_nil(@root[99],
|
1312
|
-
assert_nil(@root[-(@root.children.size+1)],
|
1313
|
-
assert_nil(@root[-3],
|
1281
|
+
assert_nil(@root['Some Random Name'], 'Should return nil')
|
1282
|
+
assert_nil(@root[99], 'Should return nil')
|
1283
|
+
assert_nil(@root[-(@root.children.size + 1)], 'Should return nil')
|
1284
|
+
assert_nil(@root[-3], 'Should return nil')
|
1314
1285
|
end
|
1315
1286
|
|
1316
1287
|
# Test the in_degree method.
|
@@ -1340,18 +1311,18 @@ module TestTree
|
|
1340
1311
|
setup_test_tree
|
1341
1312
|
|
1342
1313
|
expected_json = {
|
1343
|
-
|
1344
|
-
|
1345
|
-
JSON.create_id =>
|
1346
|
-
|
1347
|
-
{
|
1348
|
-
{
|
1314
|
+
'name' => 'ROOT',
|
1315
|
+
'content' => 'Root Node',
|
1316
|
+
JSON.create_id => 'Tree::TreeNode',
|
1317
|
+
'children' => [
|
1318
|
+
{ 'name' => 'Child1', 'content' => 'Child Node 1', JSON.create_id => 'Tree::TreeNode' },
|
1319
|
+
{ 'name' => 'Child2', 'content' => 'Child Node 2', JSON.create_id => 'Tree::TreeNode' },
|
1349
1320
|
{
|
1350
|
-
|
1351
|
-
|
1352
|
-
JSON.create_id =>
|
1353
|
-
|
1354
|
-
{
|
1321
|
+
'name' => 'Child3',
|
1322
|
+
'content' => 'Child Node 3',
|
1323
|
+
JSON.create_id => 'Tree::TreeNode',
|
1324
|
+
'children' => [
|
1325
|
+
{ 'name' => 'Child4', 'content' => 'Grand Child 1', JSON.create_id => 'Tree::TreeNode' }
|
1355
1326
|
]
|
1356
1327
|
}
|
1357
1328
|
]
|
@@ -1362,88 +1333,54 @@ module TestTree
|
|
1362
1333
|
|
1363
1334
|
def test_json_deserialization
|
1364
1335
|
tree_as_json = {
|
1365
|
-
|
1366
|
-
|
1367
|
-
JSON.create_id =>
|
1368
|
-
|
1369
|
-
{
|
1370
|
-
{
|
1336
|
+
'name' => 'ROOT',
|
1337
|
+
'content' => 'Root Node',
|
1338
|
+
JSON.create_id => 'Tree::TreeNode',
|
1339
|
+
'children' => [
|
1340
|
+
{ 'name' => 'Child1', 'content' => 'Child Node 1', JSON.create_id => 'Tree::TreeNode' },
|
1341
|
+
{ 'name' => 'Child2', 'content' => 'Child Node 2', JSON.create_id => 'Tree::TreeNode' },
|
1371
1342
|
{
|
1372
|
-
|
1373
|
-
|
1374
|
-
JSON.create_id =>
|
1375
|
-
|
1376
|
-
{
|
1343
|
+
'name' => 'Child3',
|
1344
|
+
'content' => 'Child Node 3',
|
1345
|
+
JSON.create_id => 'Tree::TreeNode',
|
1346
|
+
'children' => [
|
1347
|
+
{ 'name' => 'Child4', 'content' => 'Grand Child 1', JSON.create_id => 'Tree::TreeNode' }
|
1377
1348
|
]
|
1378
1349
|
}
|
1379
1350
|
]
|
1380
1351
|
}.to_json
|
1381
1352
|
|
1382
|
-
tree = JSON.parse(tree_as_json, :
|
1353
|
+
tree = JSON.parse(tree_as_json, create_additions: true)
|
1383
1354
|
|
1384
|
-
assert_equal(@root.name, tree.root.name,
|
1385
|
-
assert_equal(@child1.name, tree[0].name,
|
1386
|
-
assert_equal(@child2.name, tree[1].name,
|
1387
|
-
assert_equal(@child3.name, tree[2].name,
|
1388
|
-
assert_equal(@child4.name, tree[2][0].name,
|
1355
|
+
assert_equal(@root.name, tree.root.name, 'Root should be returned')
|
1356
|
+
assert_equal(@child1.name, tree[0].name, 'Child 1 should be returned')
|
1357
|
+
assert_equal(@child2.name, tree[1].name, 'Child 2 should be returned')
|
1358
|
+
assert_equal(@child3.name, tree[2].name, 'Child 3 should be returned')
|
1359
|
+
assert_equal(@child4.name, tree[2][0].name, 'Grand Child 1 should be returned')
|
1389
1360
|
end
|
1390
1361
|
|
1391
|
-
def
|
1392
|
-
root_node = Tree::TreeNode.new(
|
1393
|
-
root_node << Tree::TreeNode.new(
|
1394
|
-
|
1362
|
+
def test_json_round_trip
|
1363
|
+
root_node = Tree::TreeNode.new('ROOT', 'Root Content')
|
1364
|
+
root_node << Tree::TreeNode.new('CHILD1',
|
1365
|
+
'Child1 Content') << Tree::TreeNode.new('GRAND_CHILD1', 'GrandChild1 Content')
|
1366
|
+
root_node << Tree::TreeNode.new('CHILD2', 'Child2 Content')
|
1395
1367
|
|
1396
1368
|
j = root_node.to_json
|
1397
1369
|
|
1398
|
-
k = JSON.parse(j, :
|
1399
|
-
|
1400
|
-
assert_equal(k.name, root_node.name, "Root should be returned")
|
1401
|
-
assert_equal(k[0].name, root_node[0].name, "Child 1 should be returned")
|
1402
|
-
assert_equal(k[0][0].name, root_node[0][0].name, "Grand Child 1 should be returned")
|
1403
|
-
assert_equal(k[1].name, root_node[1].name, "Child 2 should be returned")
|
1404
|
-
end
|
1405
|
-
|
1406
|
-
# Test the old CamelCase method names
|
1407
|
-
def test_old_camelCase_method_names
|
1408
|
-
setup_test_tree
|
1409
|
-
|
1410
|
-
meth_names_to_test = %w{isRoot? isLeaf? hasContent?
|
1411
|
-
hasChildren? firstChild lastChild
|
1412
|
-
firstSibling isFirstSibling? lastSibling isLastSibling?
|
1413
|
-
isOnlyChild? nextSibling previousSibling nodeHeight nodeDepth
|
1414
|
-
removeFromParent! removeAll! freezeTree! }
|
1415
|
-
|
1416
|
-
require 'structured_warnings'
|
1417
|
-
|
1418
|
-
DeprecatedMethodWarning.disable do
|
1419
|
-
assert(@root.isRoot?) # Test if the original method is really called
|
1420
|
-
end
|
1421
|
-
|
1422
|
-
meth_names_to_test.each do |meth_name|
|
1423
|
-
assert_warn(DeprecatedMethodWarning) {@root.send(meth_name)}
|
1424
|
-
end
|
1425
|
-
|
1426
|
-
# Special Case for printTree to avoid putting stuff on the STDOUT during the unit test.
|
1427
|
-
begin
|
1428
|
-
require 'stringio'
|
1429
|
-
$stdout = StringIO.new
|
1430
|
-
assert_warn(DeprecatedMethodWarning) { @root.send('printTree') }
|
1431
|
-
ensure
|
1432
|
-
$stdout = STDOUT
|
1433
|
-
end
|
1370
|
+
k = JSON.parse(j, create_additions: true)
|
1434
1371
|
|
1372
|
+
assert_equal(k.name, root_node.name, 'Root should be returned')
|
1373
|
+
assert_equal(k[0].name, root_node[0].name, 'Child 1 should be returned')
|
1374
|
+
assert_equal(k[0][0].name, root_node[0][0].name, 'Grand Child 1 should be returned')
|
1375
|
+
assert_equal(k[1].name, root_node[1].name, 'Child 2 should be returned')
|
1435
1376
|
end
|
1436
1377
|
|
1437
1378
|
# Test usage of integers as node names
|
1438
1379
|
def test_integer_node_names
|
1439
|
-
|
1440
|
-
|
1441
|
-
|
1442
|
-
|
1443
|
-
@n_child1 = Tree::TreeNode.new(1, "Child Node 1")
|
1444
|
-
@n_child2 = Tree::TreeNode.new(2, "Child Node 2")
|
1445
|
-
@n_child3 = Tree::TreeNode.new("three", "Child Node 3")
|
1446
|
-
end
|
1380
|
+
@n_root = Tree::TreeNode.new(0, 'Root Node')
|
1381
|
+
@n_child1 = Tree::TreeNode.new(1, 'Child Node 1')
|
1382
|
+
@n_child2 = Tree::TreeNode.new(2, 'Child Node 2')
|
1383
|
+
@n_child3 = Tree::TreeNode.new('three', 'Child Node 3')
|
1447
1384
|
|
1448
1385
|
@n_root << @n_child1
|
1449
1386
|
@n_root << @n_child2
|
@@ -1451,29 +1388,22 @@ module TestTree
|
|
1451
1388
|
|
1452
1389
|
# Node[n] is really accessing the nth child with a zero-base
|
1453
1390
|
assert_not_equal(@n_root[1].name, 1) # This is really the second child
|
1454
|
-
assert_equal(@n_root[0].name, 1) # This will work, as it is the first child
|
1455
|
-
assert_equal(@n_root[1
|
1391
|
+
assert_equal(@n_root[0].name, '1') # This will work, as it is the first child
|
1392
|
+
assert_equal(@n_root[1].name, '2') # This will work, as the flag is now enabled
|
1456
1393
|
|
1457
1394
|
# Sanity check for the "normal" string name cases. Both cases should work.
|
1458
|
-
assert_equal(@n_root[
|
1459
|
-
|
1460
|
-
StandardWarning.disable
|
1461
|
-
assert_equal(@n_root["three", true].name, "three")
|
1462
|
-
|
1463
|
-
# Also ensure that the warning is actually being thrown
|
1464
|
-
StandardWarning.enable
|
1465
|
-
assert_warn(StandardWarning) {assert_equal(@n_root["three", true].name, "three") }
|
1395
|
+
assert_equal(@n_root['three'].name, 'three')
|
1466
1396
|
end
|
1467
1397
|
|
1468
1398
|
# Test the addition of a node to itself as a child
|
1469
1399
|
def test_add_node_to_self_as_child
|
1470
|
-
root =
|
1400
|
+
root = Tree::TreeNode.new('root')
|
1471
1401
|
|
1472
1402
|
# Lets check the direct parentage scenario
|
1473
|
-
assert_raise(ArgumentError) {root << root}
|
1403
|
+
assert_raise(ArgumentError) { root << root }
|
1474
1404
|
|
1475
1405
|
# And now a scenario where the node addition is done down the hierarchy
|
1476
|
-
child =
|
1406
|
+
child = Tree::TreeNode.new('child')
|
1477
1407
|
assert_raise(ArgumentError) { root << child << root }
|
1478
1408
|
end
|
1479
1409
|
|
@@ -1482,10 +1412,11 @@ module TestTree
|
|
1482
1412
|
setup_test_tree
|
1483
1413
|
|
1484
1414
|
leafs = @root.each_leaf
|
1485
|
-
parents = leafs.collect
|
1486
|
-
leafs.each
|
1487
|
-
parents.each
|
1488
|
-
|
1415
|
+
parents = leafs.collect(&:parent)
|
1416
|
+
leafs.each(&:remove_from_parent!)
|
1417
|
+
parents.each do |parent|
|
1418
|
+
assert(parent.leaf?) unless parent.children?
|
1419
|
+
end
|
1489
1420
|
end
|
1490
1421
|
|
1491
1422
|
# Test if node names are really unique in the child array.
|
@@ -1498,9 +1429,8 @@ module TestTree
|
|
1498
1429
|
begin
|
1499
1430
|
@root.first_child << @child2
|
1500
1431
|
rescue RuntimeError => e
|
1501
|
-
|
1432
|
+
raise("No error #{e} should have been raised for adding a non-sibling duplicate.")
|
1502
1433
|
end
|
1503
|
-
|
1504
1434
|
end
|
1505
1435
|
|
1506
1436
|
# Setup function to build some extra trees to play with.
|
@@ -1520,15 +1450,15 @@ module TestTree
|
|
1520
1450
|
#
|
1521
1451
|
@other_tree = @root.detached_copy
|
1522
1452
|
@other_tree << @child1.detached_copy
|
1523
|
-
@other_tree[
|
1524
|
-
@other_tree[
|
1453
|
+
@other_tree['Child1'] << Tree::TreeNode.new('Child1a', 'GrandChild Node 1a')
|
1454
|
+
@other_tree['Child1'] << Tree::TreeNode.new('Child1b', 'GrandChild Node 1b')
|
1525
1455
|
@other_tree << @child3.detached_copy
|
1526
|
-
@other_tree[
|
1527
|
-
@other_tree[
|
1456
|
+
@other_tree['Child3'] << Tree::TreeNode.new('Child3a', 'GrandChild Node 3a')
|
1457
|
+
@other_tree['Child3']['Child3a'] << Tree::TreeNode.new('Child3a1', 'GreatGrandChild Node 3a1')
|
1528
1458
|
|
1529
1459
|
# And another (different) one so we can test exceptions...
|
1530
|
-
@other_tree2 = Tree::TreeNode.new(
|
1531
|
-
@other_tree2 << Tree::TreeNode.new(
|
1460
|
+
@other_tree2 = Tree::TreeNode.new('ROOT2', 'A different root')
|
1461
|
+
@other_tree2 << Tree::TreeNode.new('new_child1', 'New Child 1')
|
1532
1462
|
end
|
1533
1463
|
|
1534
1464
|
# Test tree merging.
|
@@ -1538,23 +1468,20 @@ module TestTree
|
|
1538
1468
|
|
1539
1469
|
merged_tree = @root.merge(@other_tree)
|
1540
1470
|
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
|
1549
|
-
assert(
|
1550
|
-
|
1551
|
-
assert(
|
1552
|
-
assert(
|
1553
|
-
|
1554
|
-
assert(
|
1555
|
-
assert( !merged_tree["Child2"].nil?, ".merge() has not included ['Child2'] from self." )
|
1556
|
-
assert( !merged_tree["Child3"]["Child3a"]["Child3a1"].nil?, ".merge() has not included ['Child3']['Child3a']['Child3a1'] from other_tree." )
|
1557
|
-
assert( !merged_tree["Child3"]["Child4"].nil?, ".merge() has not included ['Child3']['Child4'] from self." )
|
1471
|
+
assert(@root['Child1']['Child1a'].nil?, '.merge() has altered self.')
|
1472
|
+
assert(@root['Child1']['Child1b'].nil?, '.merge() has altered self.')
|
1473
|
+
assert(@root['Child3']['Child3a'].nil?, '.merge() has altered self.')
|
1474
|
+
assert(merged_tree.is_a?(Tree::TreeNode))
|
1475
|
+
assert(!merged_tree['Child1']['Child1a'].nil?,
|
1476
|
+
".merge() has not included ['Child1']['Child1a'] from other_tree.")
|
1477
|
+
assert(!merged_tree['Child1']['Child1b'].nil?,
|
1478
|
+
".merge() has not included ['Child1']['Child1b'] from other_tree.")
|
1479
|
+
assert(!merged_tree['Child3']['Child3a'].nil?,
|
1480
|
+
".merge() has not included ['Child3']['Child3a'] from other_tree.")
|
1481
|
+
assert(!merged_tree['Child2'].nil?, ".merge() has not included ['Child2'] from self.")
|
1482
|
+
assert(!merged_tree['Child3']['Child3a']['Child3a1'].nil?,
|
1483
|
+
".merge() has not included ['Child3']['Child3a']['Child3a1'] from other_tree.")
|
1484
|
+
assert(!merged_tree['Child3']['Child4'].nil?, ".merge() has not included ['Child3']['Child4'] from self.")
|
1558
1485
|
|
1559
1486
|
assert_raise(ArgumentError) { @root.merge(@other_tree2) }
|
1560
1487
|
assert_raise(TypeError) { @root.merge('ROOT') }
|
@@ -1565,22 +1492,18 @@ module TestTree
|
|
1565
1492
|
setup_test_tree
|
1566
1493
|
setup_other_test_tree
|
1567
1494
|
|
1568
|
-
# puts "\n\ntest_merge_bang:\n\n"
|
1569
|
-
# @root.print_tree
|
1570
|
-
# puts "\n"
|
1571
|
-
# @other_tree.print_tree
|
1572
|
-
|
1573
1495
|
@root.merge!(@other_tree)
|
1574
1496
|
|
1575
1497
|
# puts "\n"
|
1576
1498
|
# @root.print_tree
|
1577
1499
|
|
1578
|
-
assert(
|
1579
|
-
assert(
|
1580
|
-
assert(
|
1581
|
-
assert(
|
1582
|
-
assert(
|
1583
|
-
|
1500
|
+
assert(!@root['Child1']['Child1a'].nil?, ".merge() has not included ['Child1']['Child1a'] from other_tree.")
|
1501
|
+
assert(!@root['Child1']['Child1b'].nil?, ".merge() has not included ['Child1']['Child1b'] from other_tree.")
|
1502
|
+
assert(!@root['Child3']['Child3a'].nil?, ".merge() has not included ['Child3']['Child3a'] from other_tree.")
|
1503
|
+
assert(!@root['Child2'].nil?, ".merge() has not included ['Child2'] from self.")
|
1504
|
+
assert(!@root['Child3']['Child3a']['Child3a1'].nil?,
|
1505
|
+
".merge() has not included ['Child3']['Child3a']['Child3a1'] from other_tree.")
|
1506
|
+
assert(!@root['Child3']['Child4'].nil?, ".merge() has not included ['Child3']['Child4'] from self.")
|
1584
1507
|
|
1585
1508
|
assert_raise(ArgumentError) { @root.merge!(@other_tree2) }
|
1586
1509
|
assert_raise(TypeError) { @root.merge!('ROOT') }
|
@@ -1590,7 +1513,6 @@ module TestTree
|
|
1590
1513
|
setup_test_tree
|
1591
1514
|
|
1592
1515
|
assert_equal 'ROOT', @root.name, "Name should be 'ROOT'"
|
1593
|
-
|
1594
1516
|
end
|
1595
1517
|
|
1596
1518
|
def test_rename
|
@@ -1607,47 +1529,46 @@ module TestTree
|
|
1607
1529
|
def test_rename_child
|
1608
1530
|
setup_test_tree
|
1609
1531
|
|
1610
|
-
assert_raise(ArgumentError) {@root.rename_child('Not_Present_Child1', 'ALT_Child1')}
|
1532
|
+
assert_raise(ArgumentError) { @root.rename_child('Not_Present_Child1', 'ALT_Child1') }
|
1611
1533
|
|
1612
1534
|
@root.rename_child('Child1', 'ALT_Child1')
|
1613
1535
|
assert_equal('ALT_Child1', @child1.name, "Name should be 'ALT_Child1'")
|
1614
1536
|
assert_equal(@child1, @root['ALT_Child1'], 'Should be able to access from parent using new name')
|
1615
|
-
|
1616
1537
|
end
|
1617
1538
|
|
1618
1539
|
def test_change_parent
|
1619
|
-
root_node = Tree::TreeNode.new(
|
1540
|
+
root_node = Tree::TreeNode.new('OLD_ROOT')
|
1620
1541
|
|
1621
|
-
child_node = Tree::TreeNode.new(
|
1542
|
+
child_node = Tree::TreeNode.new('CHILD')
|
1622
1543
|
assert_equal(0, child_node.node_depth)
|
1623
1544
|
|
1624
1545
|
root_node << child_node
|
1625
|
-
assert_equal(root_node[
|
1546
|
+
assert_equal(root_node['CHILD'].name, 'CHILD')
|
1626
1547
|
assert_equal(0, root_node.node_depth)
|
1627
1548
|
assert_equal(1, child_node.node_depth)
|
1628
1549
|
|
1629
|
-
grandchild_node = Tree::TreeNode.new(
|
1550
|
+
grandchild_node = Tree::TreeNode.new('GRANDCHILD')
|
1630
1551
|
child_node << grandchild_node
|
1631
|
-
assert_equal(root_node[
|
1552
|
+
assert_equal(root_node['CHILD']['GRANDCHILD'].name, 'GRANDCHILD')
|
1632
1553
|
assert_equal(0, root_node.node_depth)
|
1633
1554
|
assert_equal(1, child_node.node_depth)
|
1634
1555
|
assert_equal(2, grandchild_node.node_depth)
|
1635
1556
|
|
1636
|
-
root2_node = Tree::TreeNode.new(
|
1557
|
+
root2_node = Tree::TreeNode.new('NEW_ROOT')
|
1637
1558
|
assert_equal(0, root2_node.node_depth)
|
1638
1559
|
|
1639
1560
|
# Move the grand child to a new root.
|
1640
1561
|
root2_node << grandchild_node
|
1641
|
-
assert_equal(root2_node[
|
1562
|
+
assert_equal(root2_node['GRANDCHILD'].name, 'GRANDCHILD')
|
1642
1563
|
assert_equal(root2_node, grandchild_node.parent)
|
1643
1564
|
assert_equal(1, grandchild_node.node_depth)
|
1644
1565
|
|
1645
1566
|
# Test the move semantics for addition of an existing child node
|
1646
|
-
root1 = Tree::TreeNode.new(
|
1647
|
-
root1 << Tree::TreeNode.new(
|
1648
|
-
root1 << Tree::TreeNode.new(
|
1649
|
-
root1[
|
1650
|
-
assert_equal(root1[
|
1567
|
+
root1 = Tree::TreeNode.new('1')
|
1568
|
+
root1 << Tree::TreeNode.new('2') << Tree::TreeNode.new('4')
|
1569
|
+
root1 << Tree::TreeNode.new('3') << Tree::TreeNode.new('5')
|
1570
|
+
root1['3'] << Tree::TreeNode.new('6')
|
1571
|
+
assert_equal(root1['3']['6'].name, '6')
|
1651
1572
|
|
1652
1573
|
# Create a new tree
|
1653
1574
|
root2 = root1.dup
|
@@ -1655,24 +1576,23 @@ module TestTree
|
|
1655
1576
|
assert_not_same(root1, root2)
|
1656
1577
|
|
1657
1578
|
# Now 'move' the "4" node to the new tree. This should have 'dup' semantics.
|
1658
|
-
root2[
|
1659
|
-
assert_equal(
|
1660
|
-
assert_nil(root1[
|
1661
|
-
|
1579
|
+
root2['3'] << root1['2']['4']
|
1580
|
+
assert_equal('3', root2['3']['4'].parent.name) # This is on the new tree
|
1581
|
+
assert_nil(root1['2']['4']) # This is on the old tree
|
1662
1582
|
end
|
1663
1583
|
|
1664
1584
|
# Test the path_as_string method.
|
1665
1585
|
def test_path_as_string
|
1666
|
-
j = Tree::TreeNode.new(
|
1667
|
-
f = Tree::TreeNode.new(
|
1668
|
-
k = Tree::TreeNode.new(
|
1669
|
-
a = Tree::TreeNode.new(
|
1670
|
-
d = Tree::TreeNode.new(
|
1671
|
-
h = Tree::TreeNode.new(
|
1672
|
-
z = Tree::TreeNode.new(
|
1673
|
-
p = Tree::TreeNode.new(
|
1674
|
-
t = Tree::TreeNode.new(
|
1675
|
-
e = Tree::TreeNode.new(
|
1586
|
+
j = Tree::TreeNode.new('j')
|
1587
|
+
f = Tree::TreeNode.new('f')
|
1588
|
+
k = Tree::TreeNode.new('k')
|
1589
|
+
a = Tree::TreeNode.new('a')
|
1590
|
+
d = Tree::TreeNode.new('d')
|
1591
|
+
h = Tree::TreeNode.new('h')
|
1592
|
+
z = Tree::TreeNode.new('z')
|
1593
|
+
p = Tree::TreeNode.new('p')
|
1594
|
+
t = Tree::TreeNode.new('t')
|
1595
|
+
e = Tree::TreeNode.new('e')
|
1676
1596
|
|
1677
1597
|
# Create the following Tree
|
1678
1598
|
# j <-- level 0 (Root)
|
@@ -1690,7 +1610,7 @@ module TestTree
|
|
1690
1610
|
h << t
|
1691
1611
|
j << k << z
|
1692
1612
|
|
1693
|
-
assert_equal(t.path_as_string
|
1613
|
+
assert_equal(t.path_as_string, 'j=>f=>h=>t') # Check the default sep.
|
1694
1614
|
|
1695
1615
|
assert_equal(t.path_as_string(' => '), 'j => f => h => t')
|
1696
1616
|
assert_equal(z.path_as_string(' => '), 'j => k => z')
|
@@ -1699,16 +1619,16 @@ module TestTree
|
|
1699
1619
|
|
1700
1620
|
# Test the path_as_array method.
|
1701
1621
|
def test_path_as_array
|
1702
|
-
j = Tree::TreeNode.new(
|
1703
|
-
f = Tree::TreeNode.new(
|
1704
|
-
k = Tree::TreeNode.new(
|
1705
|
-
a = Tree::TreeNode.new(
|
1706
|
-
d = Tree::TreeNode.new(
|
1707
|
-
h = Tree::TreeNode.new(
|
1708
|
-
z = Tree::TreeNode.new(
|
1709
|
-
p = Tree::TreeNode.new(
|
1710
|
-
t = Tree::TreeNode.new(
|
1711
|
-
e = Tree::TreeNode.new(
|
1622
|
+
j = Tree::TreeNode.new('j')
|
1623
|
+
f = Tree::TreeNode.new('f')
|
1624
|
+
k = Tree::TreeNode.new('k')
|
1625
|
+
a = Tree::TreeNode.new('a')
|
1626
|
+
d = Tree::TreeNode.new('d')
|
1627
|
+
h = Tree::TreeNode.new('h')
|
1628
|
+
z = Tree::TreeNode.new('z')
|
1629
|
+
p = Tree::TreeNode.new('p')
|
1630
|
+
t = Tree::TreeNode.new('t')
|
1631
|
+
e = Tree::TreeNode.new('e')
|
1712
1632
|
|
1713
1633
|
# Create the following Tree
|
1714
1634
|
# j <-- level 0 (Root)
|
@@ -1726,9 +1646,9 @@ module TestTree
|
|
1726
1646
|
h << t
|
1727
1647
|
j << k << z
|
1728
1648
|
|
1729
|
-
assert_equal(e.path_as_array, [
|
1730
|
-
assert_equal(p.path_as_array, [
|
1731
|
-
assert_equal(k.path_as_array, [
|
1649
|
+
assert_equal(e.path_as_array, %w[j f a d e])
|
1650
|
+
assert_equal(p.path_as_array, %w[j f h p])
|
1651
|
+
assert_equal(k.path_as_array, %w[j k])
|
1732
1652
|
end
|
1733
1653
|
end
|
1734
1654
|
end
|