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