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