rubytree 1.0.0 → 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 -7
- data/Gemfile.lock +69 -40
- data/LICENSE.md +1 -2
- data/README.md +1 -2
- data/Rakefile +18 -22
- data/examples/example_basic.rb +1 -1
- data/lib/rubytree.rb +1 -1
- data/lib/tree/binarytree.rb +7 -11
- data/lib/tree/utils/camel_case_method_handler.rb +7 -10
- data/lib/tree/utils/hash_converter.rb +60 -64
- data/lib/tree/utils/json_converter.rb +10 -15
- data/lib/tree/utils/metrics_methods.rb +8 -7
- data/lib/tree/utils/path_methods.rb +3 -7
- data/lib/tree/utils/tree_merge_handler.rb +5 -7
- data/lib/tree/utils/utils.rb +2 -4
- data/lib/tree/version.rb +2 -3
- data/lib/tree.rb +99 -85
- data/rubytree.gemspec +90 -0
- data/setup.rb +1565 -0
- data/spec/tree_spec.rb +0 -2
- data/test/test_binarytree.rb +21 -22
- data/test/test_rubytree_require.rb +0 -2
- data/test/test_subclassed_node.rb +0 -4
- data/test/test_thread_and_fiber.rb +7 -10
- data/test/test_tree.rb +201 -203
- metadata +86 -29
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, 2017 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
|
#
|
@@ -41,9 +41,7 @@ module TestTree
|
|
41
41
|
# Test class for the Tree node.
|
42
42
|
# noinspection RubyTooManyInstanceVariablesInspection
|
43
43
|
class TestTreeNode < Test::Unit::TestCase
|
44
|
-
|
45
|
-
Person = Struct::new(:First, :last) # A simple structure to use as the content for the nodes.
|
46
|
-
|
44
|
+
Person = Struct.new(:First, :last) # A simple structure to use as the content for the nodes.
|
47
45
|
|
48
46
|
# Create this structure for the tests
|
49
47
|
#
|
@@ -72,7 +70,6 @@ module TestTree
|
|
72
70
|
@child3 = Tree::TreeNode.new('Child3', 'Child Node 3')
|
73
71
|
@child4 = Tree::TreeNode.new('Child4', 'Grand Child 1')
|
74
72
|
@child5 = Tree::TreeNode.new('Child5', 'Child Node 4')
|
75
|
-
|
76
73
|
end
|
77
74
|
|
78
75
|
# Create the actual test tree.
|
@@ -94,15 +91,15 @@ module TestTree
|
|
94
91
|
|
95
92
|
# This test is for the root alone - without any children being linked
|
96
93
|
def test_root_setup
|
97
|
-
assert_not_nil(@root
|
98
|
-
assert_nil(@root.parent
|
99
|
-
assert_not_nil(@root.name
|
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')
|
100
97
|
assert_equal('ROOT', @root.name, "Name should be 'ROOT'")
|
101
98
|
assert_equal('Root Node', @root.content, "Content should be 'Root Node'")
|
102
|
-
assert(@root.is_root
|
103
|
-
assert(!@root.has_children
|
104
|
-
assert(@root.has_content
|
105
|
-
assert_equal(1
|
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')
|
106
103
|
assert_equal(0, @root.siblings.length, 'This root does not have any children')
|
107
104
|
assert_equal(0, @root.in_degree, 'Root should have an in-degree of 0')
|
108
105
|
assert_equal(0, @root.node_height, "Root's height before adding any children is 0")
|
@@ -116,10 +113,10 @@ module TestTree
|
|
116
113
|
# TODO: Should probably change this logic. Root's root should
|
117
114
|
# return nil so that the possibility of a recursive error does not exist
|
118
115
|
# at all.
|
119
|
-
assert_same(@root
|
120
|
-
assert_same(@root
|
121
|
-
assert_same(@root
|
122
|
-
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")
|
123
120
|
end
|
124
121
|
|
125
122
|
def test_from_hash
|
@@ -131,20 +128,19 @@ module TestTree
|
|
131
128
|
# / \
|
132
129
|
# H I
|
133
130
|
|
134
|
-
hash = {[:A, 'Root content'] => {
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
}
|
141
|
-
},
|
142
|
-
C: {},
|
143
|
-
D: {
|
144
|
-
G: {}
|
131
|
+
hash = { [:A, 'Root content'] => {
|
132
|
+
B: {
|
133
|
+
E: {},
|
134
|
+
F: {
|
135
|
+
H: {},
|
136
|
+
[:I, 'Leaf content'] => {}
|
145
137
|
}
|
146
|
-
|
147
|
-
|
138
|
+
},
|
139
|
+
C: {},
|
140
|
+
D: {
|
141
|
+
G: {}
|
142
|
+
}
|
143
|
+
} }
|
148
144
|
|
149
145
|
tree = Tree::TreeNode.from_hash(hash)
|
150
146
|
|
@@ -168,10 +164,9 @@ module TestTree
|
|
168
164
|
assert_equal(2, interior_node.children.count)
|
169
165
|
|
170
166
|
# Can't make a node without a name
|
171
|
-
assert_raise
|
167
|
+
assert_raise(ArgumentError) { Tree::TreeNode.from_hash({}) }
|
172
168
|
# Can't have multiple roots
|
173
|
-
assert_raise
|
174
|
-
|
169
|
+
assert_raise(ArgumentError) { Tree::TreeNode.from_hash({ A: {}, B: {} }) }
|
175
170
|
end
|
176
171
|
|
177
172
|
def test_from_hash_with_nils
|
@@ -183,20 +178,19 @@ module TestTree
|
|
183
178
|
# / \
|
184
179
|
# H I
|
185
180
|
|
186
|
-
hash = {[:A, 'Root content'] => {
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
}
|
193
|
-
},
|
194
|
-
C: nil,
|
195
|
-
D: {
|
196
|
-
G: nil
|
181
|
+
hash = { [:A, 'Root content'] => {
|
182
|
+
B: {
|
183
|
+
E: nil,
|
184
|
+
F: {
|
185
|
+
H: nil,
|
186
|
+
[:I, 'Leaf content'] => nil
|
197
187
|
}
|
198
|
-
|
199
|
-
|
188
|
+
},
|
189
|
+
C: nil,
|
190
|
+
D: {
|
191
|
+
G: nil
|
192
|
+
}
|
193
|
+
} }
|
200
194
|
|
201
195
|
tree = Tree::TreeNode.from_hash(hash)
|
202
196
|
|
@@ -228,7 +222,7 @@ module TestTree
|
|
228
222
|
assert_equal([], tree.add_from_hash(hash))
|
229
223
|
|
230
224
|
# Okay, now try a real hash
|
231
|
-
hash = {B: {C: {D: nil}, E: {}, F: {}}, [:G, 'G content'] => {}}
|
225
|
+
hash = { B: { C: { D: nil }, E: {}, F: {} }, [:G, 'G content'] => {} }
|
232
226
|
# A
|
233
227
|
# / \
|
234
228
|
# B G
|
@@ -246,9 +240,9 @@ module TestTree
|
|
246
240
|
assert_equal(5, tree[:B].size)
|
247
241
|
assert_equal(3, tree[:B].children.count)
|
248
242
|
|
249
|
-
assert_raise
|
250
|
-
assert_raise
|
251
|
-
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' }) }
|
252
246
|
end
|
253
247
|
|
254
248
|
# Test exporting to ruby Hash
|
@@ -273,7 +267,7 @@ module TestTree
|
|
273
267
|
b << e
|
274
268
|
|
275
269
|
exported = a.to_h
|
276
|
-
expected = {A: {B: {E: {}}, C: {F: {}, G: {}}}}
|
270
|
+
expected = { A: { B: { E: {} }, C: { F: {}, G: {} } } }
|
277
271
|
assert_equal(expected, exported)
|
278
272
|
end
|
279
273
|
|
@@ -287,7 +281,7 @@ module TestTree
|
|
287
281
|
# |\ |
|
288
282
|
# I J K
|
289
283
|
|
290
|
-
input = {A: {B: {E: {I: {}, J: {}}, F: {}, G: {}}, C: {H: {K: {}}}}}
|
284
|
+
input = { A: { B: { E: { I: {}, J: {} }, F: {}, G: {} }, C: { H: { K: {} } } } }
|
291
285
|
|
292
286
|
node = Tree::TreeNode.from_hash(input)
|
293
287
|
assert_equal(input, node.to_h)
|
@@ -296,8 +290,8 @@ module TestTree
|
|
296
290
|
# Test the presence of content in the nodes.
|
297
291
|
def test_has_content_eh
|
298
292
|
a_node = Tree::TreeNode.new('A Node')
|
299
|
-
assert_nil(a_node.content
|
300
|
-
assert(!a_node.has_content
|
293
|
+
assert_nil(a_node.content, 'The node should not have content')
|
294
|
+
assert(!a_node.has_content?, 'The node should not have content')
|
301
295
|
|
302
296
|
a_node.content = 'Something'
|
303
297
|
assert_not_nil(a_node.content, 'The node should now have content')
|
@@ -313,7 +307,7 @@ module TestTree
|
|
313
307
|
# Test the <=> operator.
|
314
308
|
def test_spaceship
|
315
309
|
require 'structured_warnings'
|
316
|
-
StructuredWarnings::StandardWarning.disable
|
310
|
+
StructuredWarnings::StandardWarning.disable # Disable the warnings for using integers as node names
|
317
311
|
|
318
312
|
first_node = Tree::TreeNode.new(1)
|
319
313
|
second_node = Tree::TreeNode.new(2)
|
@@ -350,8 +344,6 @@ module TestTree
|
|
350
344
|
|
351
345
|
assert(!(node_a == node_b), 'Node A and Node B are not equal')
|
352
346
|
assert(node_b.between?(node_a, node_c), 'Node B is lexically between node A and node C')
|
353
|
-
|
354
|
-
|
355
347
|
end
|
356
348
|
|
357
349
|
# Test the to_s method. This is probably a little fragile right now.
|
@@ -373,7 +365,7 @@ module TestTree
|
|
373
365
|
assert_equal(expected_string, a_node.to_s, 'The string representation should be same')
|
374
366
|
|
375
367
|
# Now test with a symbol as a key, and a hash as the content.
|
376
|
-
a_hash = {a_key: 'Some Value'}
|
368
|
+
a_hash = { a_key: 'Some Value' }
|
377
369
|
a_node = Tree::TreeNode.new(:Node_Name, a_hash)
|
378
370
|
expected_string = "Node Name: Node_Name Content: #{a_hash} Parent: <None> Children: 0 Total Nodes: 1"
|
379
371
|
assert_equal(expected_string, a_node.to_s, 'The string representation should be same')
|
@@ -384,7 +376,6 @@ module TestTree
|
|
384
376
|
|
385
377
|
expected_string = 'Node Name: Child_node Content: Child Node Parent: Node_Name Children: 0 Total Nodes: 1'
|
386
378
|
assert_equal(expected_string, child_node.to_s, 'The string representation should be same')
|
387
|
-
|
388
379
|
end
|
389
380
|
|
390
381
|
# Test the first_sibling method.
|
@@ -405,10 +396,10 @@ module TestTree
|
|
405
396
|
setup_test_tree
|
406
397
|
|
407
398
|
assert(@root.is_first_sibling?, "Root's first sibling is itself")
|
408
|
-
assert(
|
399
|
+
assert(@child1.is_first_sibling?, "Child1's first sibling is itself")
|
409
400
|
assert(!@child2.is_first_sibling?, 'Child2 is not the first sibling')
|
410
401
|
assert(!@child3.is_first_sibling?, 'Child3 is not the first sibling')
|
411
|
-
assert(
|
402
|
+
assert(@child4.is_first_sibling?, "Child4's first sibling is itself")
|
412
403
|
end
|
413
404
|
|
414
405
|
# Test the is_last_sibling? method.
|
@@ -418,8 +409,8 @@ module TestTree
|
|
418
409
|
assert(@root.is_last_sibling?, "Root's last sibling is itself")
|
419
410
|
assert(!@child1.is_last_sibling?, 'Child1 is not the last sibling')
|
420
411
|
assert(!@child2.is_last_sibling?, 'Child2 is not the last sibling')
|
421
|
-
assert(
|
422
|
-
assert(
|
412
|
+
assert(@child3.is_last_sibling?, "Child3's last sibling is itself")
|
413
|
+
assert(@child4.is_last_sibling?, "Child4's last sibling is itself")
|
423
414
|
end
|
424
415
|
|
425
416
|
# Test the last_sibling method.
|
@@ -440,7 +431,7 @@ module TestTree
|
|
440
431
|
|
441
432
|
# Lets first collect the siblings in an array.
|
442
433
|
siblings = []
|
443
|
-
result = @child1.siblings { |sibling| siblings << sibling}
|
434
|
+
result = @child1.siblings { |sibling| siblings << sibling }
|
444
435
|
|
445
436
|
assert_equal(@child1, result)
|
446
437
|
assert_equal(2, siblings.length, 'Should have two siblings')
|
@@ -453,20 +444,19 @@ module TestTree
|
|
453
444
|
assert_equal(2, siblings.length, 'Should have two siblings')
|
454
445
|
|
455
446
|
siblings.clear
|
456
|
-
@child4.siblings {|sibling| siblings << sibling}
|
447
|
+
@child4.siblings { |sibling| siblings << sibling }
|
457
448
|
assert(siblings.empty?, 'Should not have any siblings')
|
458
449
|
|
459
450
|
siblings.clear
|
460
451
|
siblings = @root.siblings
|
461
452
|
assert_equal(0, siblings.length, 'Root should not have any siblings')
|
462
|
-
|
463
453
|
end
|
464
454
|
|
465
455
|
# Test the is_only_child? method.
|
466
456
|
def test_is_only_child_eh
|
467
457
|
setup_test_tree
|
468
458
|
|
469
|
-
assert(@root.is_only_child
|
459
|
+
assert(@root.is_only_child?, 'Root is an only child')
|
470
460
|
assert(!@child1.is_only_child?, 'Child1 is not the only child')
|
471
461
|
assert(!@child2.is_only_child?, 'Child2 is not the only child')
|
472
462
|
assert(!@child3.is_only_child?, 'Child3 is not the only child')
|
@@ -514,7 +504,6 @@ module TestTree
|
|
514
504
|
|
515
505
|
# Test the addition of a nil node.
|
516
506
|
assert_raise(ArgumentError) { @root.add(nil) }
|
517
|
-
|
518
507
|
end
|
519
508
|
|
520
509
|
# Test the addition of a duplicate node (duplicate being defined as a node with the same name).
|
@@ -533,7 +522,7 @@ module TestTree
|
|
533
522
|
root = Tree::TreeNode.new('root')
|
534
523
|
one = Tree::TreeNode.new('one')
|
535
524
|
two = Tree::TreeNode.new('two')
|
536
|
-
three= Tree::TreeNode.new('three')
|
525
|
+
three = Tree::TreeNode.new('three')
|
537
526
|
deep = Tree::TreeNode.new('deep')
|
538
527
|
|
539
528
|
root << one << deep
|
@@ -544,17 +533,17 @@ module TestTree
|
|
544
533
|
begin
|
545
534
|
root << two << deep
|
546
535
|
rescue RuntimeError => e
|
547
|
-
|
536
|
+
raise("Error! The RuntimeError #{e} should not have been thrown. The same node can be added to different branches.")
|
548
537
|
end
|
549
538
|
|
550
|
-
assert_raise(ArgumentError) {root << three << three }
|
539
|
+
assert_raise(ArgumentError) { root << three << three }
|
551
540
|
|
552
|
-
root.remove_all!
|
541
|
+
root.remove_all! # Because the first child 'three' would have been added.
|
553
542
|
begin
|
554
543
|
three_dup = Tree::TreeNode.new('three')
|
555
544
|
root << three << three_dup
|
556
545
|
rescue RuntimeError => e
|
557
|
-
|
546
|
+
raise("Error! The RuntimeError #{e} should not have been thrown. The same node name can be used in the branch.")
|
558
547
|
end
|
559
548
|
end
|
560
549
|
|
@@ -563,24 +552,24 @@ module TestTree
|
|
563
552
|
assert(!@root.has_children?, 'Should not have any children')
|
564
553
|
|
565
554
|
assert_equal(1, @root.size, 'Should have 1 node (the root)')
|
566
|
-
@root.add(@child1)
|
555
|
+
@root.add(@child1) # First Child added at position 0
|
567
556
|
# Validate that children = [@child1]
|
568
557
|
assert_equal(@child1, @root[0])
|
569
558
|
|
570
|
-
@root << @child2
|
559
|
+
@root << @child2 # Second child appended at position 1.
|
571
560
|
# Validate that children = [@child1, @child2]
|
572
561
|
assert_equal(@child1, @root[0])
|
573
562
|
assert_equal(@child2, @root[1])
|
574
563
|
assert_equal(2, @root.children.size, 'Should have two child nodes')
|
575
564
|
|
576
|
-
@root.add(@child3, 1)
|
565
|
+
@root.add(@child3, 1) # Third child inserted at position 1 (before @child2)
|
577
566
|
# Validate that children = [@child1, @child3, @child2]
|
578
567
|
assert_equal(@child1, @root[0])
|
579
568
|
assert_equal(@child3, @root[1])
|
580
569
|
assert_equal(@child2, @root[2])
|
581
570
|
assert_equal(3, @root.children.size, 'Should have three child nodes')
|
582
571
|
|
583
|
-
@root.add(@child4, @root.children.size)
|
572
|
+
@root.add(@child4, @root.children.size) # Fourth child inserted at the end (equivalent to plain #add(child4)
|
584
573
|
# Validate that children = [@child1, @child3, @child2, @child4]
|
585
574
|
assert_equal(@child1, @root[0])
|
586
575
|
assert_equal(@child3, @root[1])
|
@@ -589,9 +578,9 @@ module TestTree
|
|
589
578
|
assert_equal(4, @root.children.size, 'Should have four child nodes')
|
590
579
|
|
591
580
|
# Now, a negative test. We are preventing addition to a position that does not exist.
|
592
|
-
assert_raise(RuntimeError)
|
593
|
-
@root.add(@child5, @root.children.size + 1)
|
594
|
-
|
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
|
595
584
|
# Validate that we still have children = [@child1, @child3, @child2, @child4]
|
596
585
|
assert_equal(@child1, @root[0])
|
597
586
|
assert_equal(@child3, @root[1])
|
@@ -601,9 +590,9 @@ module TestTree
|
|
601
590
|
assert_equal(4, @root.children.size, 'Should have four child nodes')
|
602
591
|
|
603
592
|
# Another negative test. Lets attempt to add from the end at a position that is not available
|
604
|
-
assert_raise(RuntimeError)
|
605
|
-
@root.add(@child5, -(@root.children.size+2))
|
606
|
-
|
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
|
607
596
|
assert_nil(@root[-5])
|
608
597
|
assert_equal(@child1, @root[-4])
|
609
598
|
assert_equal(@child3, @root[-3])
|
@@ -612,7 +601,7 @@ module TestTree
|
|
612
601
|
assert_equal(4, @root.children.size, 'Should have four child nodes')
|
613
602
|
|
614
603
|
# Lets correctly add the fifth child from the end to effectively prepend the node.
|
615
|
-
@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
|
616
605
|
assert_nil(@root[-6])
|
617
606
|
assert_equal(@child5, @root[-5])
|
618
607
|
assert_equal(@child1, @root[-4])
|
@@ -735,9 +724,9 @@ module TestTree
|
|
735
724
|
assert(!@child3.is_leaf?, 'Should not be a leaf')
|
736
725
|
|
737
726
|
assert_equal(1, @child3.node_height, 'The subtree at Child 3 should have a height of 1')
|
738
|
-
[@child1, @child2, @child4].each
|
727
|
+
[@child1, @child2, @child4].each do |child|
|
739
728
|
assert_equal(0, child.node_height, "The subtree at #{child.name} should have a height of 0")
|
740
|
-
|
729
|
+
end
|
741
730
|
|
742
731
|
result_array = @root.children
|
743
732
|
|
@@ -750,7 +739,7 @@ module TestTree
|
|
750
739
|
|
751
740
|
# Lets try the block version of the method.
|
752
741
|
result_array.clear
|
753
|
-
result = @root.children {|child| result_array << child}
|
742
|
+
result = @root.children { |child| result_array << child }
|
754
743
|
assert_equal(@root, result)
|
755
744
|
result_array.length
|
756
745
|
assert_equal(3, result_array.length, 'Should have three children')
|
@@ -758,7 +747,6 @@ module TestTree
|
|
758
747
|
assert_equal(result_array[1], @child2, 'Should have child 2')
|
759
748
|
assert_equal(result_array[2], @child3, 'Should have child 3')
|
760
749
|
assert(!result_array.include?(@child4), 'Should not have child 4')
|
761
|
-
|
762
750
|
end
|
763
751
|
|
764
752
|
# Test the first_child method.
|
@@ -782,10 +770,10 @@ module TestTree
|
|
782
770
|
# Test the find method.
|
783
771
|
def test_find
|
784
772
|
setup_test_tree
|
785
|
-
found_node = @root.find { |node| node == @child2}
|
773
|
+
found_node = @root.find { |node| node == @child2 }
|
786
774
|
assert_same(@child2, found_node, 'The node should be Child 2')
|
787
775
|
|
788
|
-
found_node = @root.find { |node| node == @child4}
|
776
|
+
found_node = @root.find { |node| node == @child4 }
|
789
777
|
assert_same(@child4, found_node, 'The node should be Child 4')
|
790
778
|
|
791
779
|
found_node = @root.find { |node| node.name == 'Child4' }
|
@@ -846,7 +834,6 @@ module TestTree
|
|
846
834
|
assert(result_array.include?(@child2), 'Should have child 2')
|
847
835
|
assert(!result_array.include?(@child3), 'Should not have child 3')
|
848
836
|
assert(result_array.include?(@child4), 'Should have child 4')
|
849
|
-
|
850
837
|
end
|
851
838
|
|
852
839
|
# Test the parent method.
|
@@ -874,17 +861,17 @@ module TestTree
|
|
874
861
|
# Test the print_tree method.
|
875
862
|
def test_print_tree
|
876
863
|
setup_test_tree
|
877
|
-
#puts
|
878
|
-
|
864
|
+
# puts
|
865
|
+
# @root.print_tree
|
879
866
|
end
|
880
867
|
|
881
868
|
# Tests the binary dumping mechanism with an Object content node
|
882
869
|
def test_marshal_dump
|
883
870
|
# Setup Test Data
|
884
871
|
test_root = Tree::TreeNode.new('ROOT', 'Root Node')
|
885
|
-
test_content = {'KEY1' => 'Value1', 'KEY2' => 'Value2'}
|
886
|
-
test_child
|
887
|
-
test_content2 = %w
|
872
|
+
test_content = { 'KEY1' => 'Value1', 'KEY2' => 'Value2' }
|
873
|
+
test_child = Tree::TreeNode.new('Child', test_content)
|
874
|
+
test_content2 = %w[AValue1 AValue2 AValue3]
|
888
875
|
test_grand_child = Tree::TreeNode.new('Grand Child 1', test_content2)
|
889
876
|
test_root << test_child << test_grand_child
|
890
877
|
|
@@ -930,7 +917,7 @@ module TestTree
|
|
930
917
|
node.content = 'abc'
|
931
918
|
node
|
932
919
|
end
|
933
|
-
collect_array.each {|node| assert_equal('abc', node.content, "Should be 'abc'")}
|
920
|
+
collect_array.each { |node| assert_equal('abc', node.content, "Should be 'abc'") }
|
934
921
|
end
|
935
922
|
|
936
923
|
# Test freezing the tree
|
@@ -940,18 +927,23 @@ module TestTree
|
|
940
927
|
@root.content = 'ABC'
|
941
928
|
assert_equal('ABC', @root.content, "Content should be 'ABC'")
|
942
929
|
@root.freeze_tree!
|
943
|
-
#
|
930
|
+
# NOTE: The error raised here depends on the Ruby version.
|
931
|
+
# For Ruby > 2.5, FrozenError is raised
|
944
932
|
# For Ruby > 1.9, RuntimeError is raised
|
945
933
|
# For Ruby ~ 1.8, TypeError is raised
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
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
|
950
942
|
end
|
951
943
|
|
952
944
|
# Test whether the content is accessible
|
953
945
|
def test_content
|
954
|
-
person = Person
|
946
|
+
person = Person.new('John', 'Doe')
|
955
947
|
@root.content = person
|
956
948
|
assert_same(person, @root.content, 'Content should be the same')
|
957
949
|
end
|
@@ -959,13 +951,11 @@ module TestTree
|
|
959
951
|
# Test the depth computation algorithm. Note that this is an incorrect computation and actually returns height+1
|
960
952
|
# instead of depth. This method has been deprecated in this release and may be removed in the future.
|
961
953
|
def test_depth
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
do_deprecated_depth
|
968
|
-
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
|
969
959
|
end
|
970
960
|
|
971
961
|
# Run the assertions for the deprecated depth method.
|
@@ -1017,9 +1007,9 @@ module TestTree
|
|
1017
1007
|
|
1018
1008
|
setup_test_tree
|
1019
1009
|
|
1020
|
-
[@child1, @child2, @child3].each
|
1010
|
+
[@child1, @child2, @child3].each do |child|
|
1021
1011
|
assert_equal(1, child.node_depth, "Node #{child.name} should have depth 1")
|
1022
|
-
|
1012
|
+
end
|
1023
1013
|
|
1024
1014
|
assert_equal(2, @child4.node_depth, 'Child 4 should have depth 2')
|
1025
1015
|
|
@@ -1034,10 +1024,10 @@ module TestTree
|
|
1034
1024
|
assert_equal(@root.node_depth, @root.level, 'Level and depth should be the same')
|
1035
1025
|
|
1036
1026
|
setup_test_tree
|
1037
|
-
[@child1, @child2, @child3].each
|
1027
|
+
[@child1, @child2, @child3].each do |child|
|
1038
1028
|
assert_equal(1, child.level, "Node #{child.name} should have level 1")
|
1039
1029
|
assert_equal(@root.node_depth, @root.level, 'Level and depth should be the same')
|
1040
|
-
|
1030
|
+
end
|
1041
1031
|
|
1042
1032
|
assert_equal(2, @child4.level, 'Child 4 should have level 2')
|
1043
1033
|
end
|
@@ -1093,21 +1083,22 @@ module TestTree
|
|
1093
1083
|
result_array = []
|
1094
1084
|
result = j.breadth_each { |node| result_array << node.detached_copy }
|
1095
1085
|
|
1096
|
-
assert_equal(j, result)
|
1086
|
+
assert_equal(j, result) # The invocation target is returned
|
1097
1087
|
|
1098
1088
|
expected_array.each_index do |i|
|
1099
|
-
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.
|
1100
1090
|
end
|
1101
1091
|
|
1102
|
-
assert_equal(Enumerator, j.breadth_each.class) if defined?(Enumerator.class
|
1103
|
-
|
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
|
1104
1096
|
|
1105
1097
|
# Now test without a block
|
1106
|
-
result_array = j.breadth_each.collect { |node| node}
|
1098
|
+
result_array = j.breadth_each.collect { |node| node }
|
1107
1099
|
expected_array.each_index do |i|
|
1108
|
-
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.
|
1109
1101
|
end
|
1110
|
-
|
1111
1102
|
end
|
1112
1103
|
|
1113
1104
|
# Test the preordered_each method.
|
@@ -1136,17 +1127,19 @@ module TestTree
|
|
1136
1127
|
j << k << z
|
1137
1128
|
|
1138
1129
|
result_array = []
|
1139
|
-
result = j.preordered_each { |node| result_array << node.detached_copy}
|
1130
|
+
result = j.preordered_each { |node| result_array << node.detached_copy }
|
1140
1131
|
|
1141
|
-
assert_equal(j, result)
|
1132
|
+
assert_equal(j, result) # Each returns the invocation target
|
1142
1133
|
|
1143
1134
|
expected_array.each_index do |i|
|
1144
1135
|
# Match only the names.
|
1145
1136
|
assert_equal(expected_array[i].name, result_array[i].name)
|
1146
1137
|
end
|
1147
1138
|
|
1148
|
-
assert_equal(Enumerator, j.preordered_each.class) if defined?(Enumerator.class
|
1149
|
-
|
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
|
1150
1143
|
end
|
1151
1144
|
|
1152
1145
|
# Test the postordered_each method.
|
@@ -1176,9 +1169,9 @@ module TestTree
|
|
1176
1169
|
|
1177
1170
|
# Test when a block is given
|
1178
1171
|
result_array = []
|
1179
|
-
result = j.postordered_each { |node| result_array << node.detached_copy}
|
1172
|
+
result = j.postordered_each { |node| result_array << node.detached_copy }
|
1180
1173
|
|
1181
|
-
assert_equal(j, result)
|
1174
|
+
assert_equal(j, result) # The invocation target is returned
|
1182
1175
|
|
1183
1176
|
expected_array.each_index do |i|
|
1184
1177
|
# Match only the names.
|
@@ -1186,7 +1179,9 @@ module TestTree
|
|
1186
1179
|
end
|
1187
1180
|
|
1188
1181
|
assert_equal(Enumerator, j.postordered_each.class) if defined?(Enumerator.class) # Without a block
|
1189
|
-
|
1182
|
+
if defined?(Enumerable::Enumerator.class)
|
1183
|
+
assert_equal(Enumerable::Enumerator, j.postordered_each.class)
|
1184
|
+
end # Without a block
|
1190
1185
|
|
1191
1186
|
# Now test without a block
|
1192
1187
|
result_array = j.postordered_each.collect { |node| node }
|
@@ -1195,7 +1190,6 @@ module TestTree
|
|
1195
1190
|
# Match only the names.
|
1196
1191
|
assert_equal(expected_array[i].name, result_array[i].name)
|
1197
1192
|
end
|
1198
|
-
|
1199
1193
|
end
|
1200
1194
|
|
1201
1195
|
# test the detached_copy method.
|
@@ -1226,25 +1220,30 @@ module TestTree
|
|
1226
1220
|
assert_not_equal(@root.object_id, tree_copy.object_id, 'Object_ids should differ.')
|
1227
1221
|
assert(tree_copy.is_root?, 'Copied root should be a root node.')
|
1228
1222
|
assert(tree_copy.has_children?, 'Copied tree should have children.')
|
1229
|
-
assert_equal(tree_copy.children.count, @root.children.count,
|
1223
|
+
assert_equal(tree_copy.children.count, @root.children.count,
|
1224
|
+
'Copied tree and the original tree should have same number of children.')
|
1230
1225
|
|
1231
1226
|
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,
|
1227
|
+
assert_not_equal(tree_copy[0].object_id, @child1.object_id,
|
1228
|
+
'Child1 Object_ids (original and copy) should differ.')
|
1233
1229
|
assert(!tree_copy[0].is_root?, 'Child1 copied should not be root.')
|
1234
1230
|
assert(!tree_copy[0].has_children?, 'Child1 copied should not have children.')
|
1235
1231
|
|
1236
1232
|
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,
|
1233
|
+
assert_not_equal(tree_copy[1].object_id, @child2.object_id,
|
1234
|
+
'Child2 Object_ids (original and copy) should differ.')
|
1238
1235
|
assert(!tree_copy[1].is_root?, 'Child2 copied should not be root.')
|
1239
1236
|
assert(!tree_copy[1].has_children?, 'Child2 copied should not have children.')
|
1240
1237
|
|
1241
1238
|
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,
|
1239
|
+
assert_not_equal(tree_copy[2].object_id, @child3.object_id,
|
1240
|
+
'Child3 Object_ids (original and copy) should differ.')
|
1243
1241
|
assert(!tree_copy[2].is_root?, 'Child3 copied should not be root.')
|
1244
1242
|
assert(tree_copy[2].has_children?, 'Child3 copied should have children.')
|
1245
1243
|
|
1246
1244
|
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,
|
1245
|
+
assert_not_equal(tree_copy[2][0].object_id, @child4.object_id,
|
1246
|
+
'Child4 Object_ids (original and copy) should differ.')
|
1248
1247
|
assert(!tree_copy[2][0].is_root?, 'Child4 copied should not be root.')
|
1249
1248
|
assert(!tree_copy[2][0].has_children?, 'Child4 copied should not have children.')
|
1250
1249
|
end
|
@@ -1286,7 +1285,7 @@ module TestTree
|
|
1286
1285
|
end
|
1287
1286
|
|
1288
1287
|
# Test the << method.
|
1289
|
-
def test_lt2
|
1288
|
+
def test_lt2 # Test the << method
|
1290
1289
|
@root << @child1
|
1291
1290
|
@root << @child2
|
1292
1291
|
@root << @child3 << @child4
|
@@ -1297,15 +1296,15 @@ module TestTree
|
|
1297
1296
|
end
|
1298
1297
|
|
1299
1298
|
# Test the [] method.
|
1300
|
-
def test_index
|
1301
|
-
assert_raise(ArgumentError) {@root[nil]}
|
1299
|
+
def test_index # Test the [] method
|
1300
|
+
assert_raise(ArgumentError) { @root[nil] }
|
1302
1301
|
|
1303
1302
|
@root << @child1
|
1304
1303
|
@root << @child2
|
1305
1304
|
assert_equal(@child1.name, @root['Child1'].name, 'Child 1 should be returned')
|
1306
1305
|
assert_equal(@child1.name, @root[0].name, 'Child 1 should be returned')
|
1307
1306
|
assert_equal(@child1.name, @root[-2].name, 'Child 1 should be returned') # Negative access also works
|
1308
|
-
assert_equal(@child1.name, @root[
|
1307
|
+
assert_equal(@child1.name, @root[-@root.children.size].name, 'Child 1 should be returned') # Negative access also works
|
1309
1308
|
|
1310
1309
|
assert_equal(@child2.name, @root['Child2'].name, 'Child 2 should be returned')
|
1311
1310
|
assert_equal(@child2.name, @root[1].name, 'Child 2 should be returned')
|
@@ -1313,7 +1312,7 @@ module TestTree
|
|
1313
1312
|
|
1314
1313
|
assert_nil(@root['Some Random Name'], 'Should return nil')
|
1315
1314
|
assert_nil(@root[99], 'Should return nil')
|
1316
|
-
assert_nil(@root[-(@root.children.size+1)], 'Should return nil')
|
1315
|
+
assert_nil(@root[-(@root.children.size + 1)], 'Should return nil')
|
1317
1316
|
assert_nil(@root[-3], 'Should return nil')
|
1318
1317
|
end
|
1319
1318
|
|
@@ -1344,18 +1343,18 @@ module TestTree
|
|
1344
1343
|
setup_test_tree
|
1345
1344
|
|
1346
1345
|
expected_json = {
|
1347
|
-
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1351
|
-
{'name' => 'Child1', 'content' => 'Child Node 1', JSON.create_id => 'Tree::TreeNode'},
|
1352
|
-
{'name' => 'Child2', 'content' => 'Child Node 2', JSON.create_id => 'Tree::TreeNode'},
|
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' },
|
1353
1352
|
{
|
1354
|
-
|
1355
|
-
|
1356
|
-
|
1357
|
-
|
1358
|
-
{'name' => 'Child4', 'content' => 'Grand Child 1', JSON.create_id => 'Tree::TreeNode'}
|
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' }
|
1359
1358
|
]
|
1360
1359
|
}
|
1361
1360
|
]
|
@@ -1366,24 +1365,24 @@ module TestTree
|
|
1366
1365
|
|
1367
1366
|
def test_json_deserialization
|
1368
1367
|
tree_as_json = {
|
1369
|
-
|
1370
|
-
|
1371
|
-
|
1372
|
-
|
1373
|
-
{'name' => 'Child1', 'content' => 'Child Node 1', JSON.create_id => 'Tree::TreeNode'},
|
1374
|
-
{'name' => 'Child2', 'content' => 'Child Node 2', JSON.create_id => 'Tree::TreeNode'},
|
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' },
|
1375
1374
|
{
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
{'name' => 'Child4', 'content' => 'Grand Child 1', JSON.create_id => 'Tree::TreeNode'}
|
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' }
|
1381
1380
|
]
|
1382
1381
|
}
|
1383
1382
|
]
|
1384
1383
|
}.to_json
|
1385
1384
|
|
1386
|
-
tree = JSON.parse(tree_as_json, :
|
1385
|
+
tree = JSON.parse(tree_as_json, create_additions: true)
|
1387
1386
|
|
1388
1387
|
assert_equal(@root.name, tree.root.name, 'Root should be returned')
|
1389
1388
|
assert_equal(@child1.name, tree[0].name, 'Child 1 should be returned')
|
@@ -1394,12 +1393,13 @@ module TestTree
|
|
1394
1393
|
|
1395
1394
|
def test_json_round_trip
|
1396
1395
|
root_node = Tree::TreeNode.new('ROOT', 'Root Content')
|
1397
|
-
root_node << Tree::TreeNode.new('CHILD1',
|
1396
|
+
root_node << Tree::TreeNode.new('CHILD1',
|
1397
|
+
'Child1 Content') << Tree::TreeNode.new('GRAND_CHILD1', 'GrandChild1 Content')
|
1398
1398
|
root_node << Tree::TreeNode.new('CHILD2', 'Child2 Content')
|
1399
1399
|
|
1400
1400
|
j = root_node.to_json
|
1401
1401
|
|
1402
|
-
k = JSON.parse(j, :
|
1402
|
+
k = JSON.parse(j, create_additions: true)
|
1403
1403
|
|
1404
1404
|
assert_equal(k.name, root_node.name, 'Root should be returned')
|
1405
1405
|
assert_equal(k[0].name, root_node[0].name, 'Child 1 should be returned')
|
@@ -1411,21 +1411,21 @@ module TestTree
|
|
1411
1411
|
def test_old_camel_case_names
|
1412
1412
|
setup_test_tree
|
1413
1413
|
|
1414
|
-
meth_names_to_test = %w
|
1414
|
+
meth_names_to_test = %w[isRoot? isLeaf? hasContent?
|
1415
1415
|
hasChildren? firstChild lastChild
|
1416
1416
|
firstSibling isFirstSibling? lastSibling isLastSibling?
|
1417
1417
|
isOnlyChild? nextSibling previousSibling nodeHeight nodeDepth
|
1418
|
-
removeFromParent! removeAll! freezeTree!
|
1418
|
+
removeFromParent! removeAll! freezeTree! ]
|
1419
1419
|
|
1420
1420
|
require 'structured_warnings'
|
1421
1421
|
|
1422
1422
|
StructuredWarnings::DeprecatedMethodWarning.disable do
|
1423
1423
|
# noinspection RubyResolve
|
1424
|
-
assert(@root.isRoot?)
|
1424
|
+
assert(@root.isRoot?) # Test if the original method is really called
|
1425
1425
|
end
|
1426
1426
|
|
1427
1427
|
meth_names_to_test.each do |meth_name|
|
1428
|
-
assert_warn(StructuredWarnings::DeprecatedMethodWarning) {@root.send(meth_name)}
|
1428
|
+
assert_warn(StructuredWarnings::DeprecatedMethodWarning) { @root.send(meth_name) }
|
1429
1429
|
end
|
1430
1430
|
|
1431
1431
|
# Special Case for printTree to avoid putting stuff on the STDOUT during the unit test.
|
@@ -1436,12 +1436,10 @@ module TestTree
|
|
1436
1436
|
ensure
|
1437
1437
|
$stdout = STDOUT
|
1438
1438
|
end
|
1439
|
-
|
1440
1439
|
end
|
1441
1440
|
|
1442
1441
|
# Test usage of integers as node names
|
1443
1442
|
def test_integer_node_names
|
1444
|
-
|
1445
1443
|
require 'structured_warnings'
|
1446
1444
|
assert_warn(StructuredWarnings::StandardWarning) do
|
1447
1445
|
@n_root = Tree::TreeNode.new(0, 'Root Node')
|
@@ -1457,7 +1455,7 @@ module TestTree
|
|
1457
1455
|
# Node[n] is really accessing the nth child with a zero-base
|
1458
1456
|
assert_not_equal(@n_root[1].name, 1) # This is really the second child
|
1459
1457
|
assert_equal(@n_root[0].name, 1) # This will work, as it is the first child
|
1460
|
-
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
|
1461
1459
|
|
1462
1460
|
# Sanity check for the "normal" string name cases. Both cases should work.
|
1463
1461
|
assert_equal(@n_root['three', false].name, 'three')
|
@@ -1467,18 +1465,18 @@ module TestTree
|
|
1467
1465
|
|
1468
1466
|
# Also ensure that the warning is actually being thrown
|
1469
1467
|
StructuredWarnings::StandardWarning.enable
|
1470
|
-
assert_warn(StructuredWarnings::StandardWarning) {assert_equal(@n_root['three', true].name, 'three') }
|
1468
|
+
assert_warn(StructuredWarnings::StandardWarning) { assert_equal(@n_root['three', true].name, 'three') }
|
1471
1469
|
end
|
1472
1470
|
|
1473
1471
|
# Test the addition of a node to itself as a child
|
1474
1472
|
def test_add_node_to_self_as_child
|
1475
|
-
root =
|
1473
|
+
root = Tree::TreeNode.new('root')
|
1476
1474
|
|
1477
1475
|
# Lets check the direct parentage scenario
|
1478
|
-
assert_raise(ArgumentError) {root << root}
|
1476
|
+
assert_raise(ArgumentError) { root << root }
|
1479
1477
|
|
1480
1478
|
# And now a scenario where the node addition is done down the hierarchy
|
1481
|
-
child =
|
1479
|
+
child = Tree::TreeNode.new('child')
|
1482
1480
|
assert_raise(ArgumentError) { root << child << root }
|
1483
1481
|
end
|
1484
1482
|
|
@@ -1487,11 +1485,11 @@ module TestTree
|
|
1487
1485
|
setup_test_tree
|
1488
1486
|
|
1489
1487
|
leafs = @root.each_leaf
|
1490
|
-
parents = leafs.collect {|leaf| leaf.parent }
|
1491
|
-
leafs.each {|leaf| leaf.remove_from_parent!}
|
1492
|
-
parents.each
|
1493
|
-
|
1494
|
-
|
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
|
1495
1493
|
end
|
1496
1494
|
|
1497
1495
|
# Test if node names are really unique in the child array.
|
@@ -1504,9 +1502,8 @@ module TestTree
|
|
1504
1502
|
begin
|
1505
1503
|
@root.first_child << @child2
|
1506
1504
|
rescue RuntimeError => e
|
1507
|
-
|
1505
|
+
raise("No error #{e} should have been raised for adding a non-sibling duplicate.")
|
1508
1506
|
end
|
1509
|
-
|
1510
1507
|
end
|
1511
1508
|
|
1512
1509
|
# Setup function to build some extra trees to play with.
|
@@ -1544,17 +1541,20 @@ module TestTree
|
|
1544
1541
|
|
1545
1542
|
merged_tree = @root.merge(@other_tree)
|
1546
1543
|
|
1547
|
-
|
1548
1544
|
assert(@root['Child1']['Child1a'].nil?, '.merge() has altered self.')
|
1549
1545
|
assert(@root['Child1']['Child1b'].nil?, '.merge() has altered self.')
|
1550
1546
|
assert(@root['Child3']['Child3a'].nil?, '.merge() has altered self.')
|
1551
|
-
assert(
|
1552
|
-
assert(!merged_tree['Child1']['Child1a'].nil?,
|
1553
|
-
|
1554
|
-
assert(!merged_tree['
|
1555
|
-
|
1556
|
-
assert(!merged_tree['Child3']['Child3a']
|
1557
|
-
|
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,17 +1565,18 @@ module TestTree
|
|
1565
1565
|
setup_test_tree
|
1566
1566
|
setup_other_test_tree
|
1567
1567
|
|
1568
|
-
|
1568
|
+
@root.merge!(@other_tree)
|
1569
1569
|
|
1570
1570
|
# puts "\n"
|
1571
1571
|
# @root.print_tree
|
1572
1572
|
|
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
|
-
|
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.")
|
1579
1580
|
|
1580
1581
|
assert_raise(ArgumentError) { @root.merge!(@other_tree2) }
|
1581
1582
|
assert_raise(TypeError) { @root.merge!('ROOT') }
|
@@ -1585,7 +1586,6 @@ module TestTree
|
|
1585
1586
|
setup_test_tree
|
1586
1587
|
|
1587
1588
|
assert_equal 'ROOT', @root.name, "Name should be 'ROOT'"
|
1588
|
-
|
1589
1589
|
end
|
1590
1590
|
|
1591
1591
|
def test_rename
|
@@ -1602,12 +1602,11 @@ module TestTree
|
|
1602
1602
|
def test_rename_child
|
1603
1603
|
setup_test_tree
|
1604
1604
|
|
1605
|
-
assert_raise(ArgumentError) {@root.rename_child('Not_Present_Child1', 'ALT_Child1')}
|
1605
|
+
assert_raise(ArgumentError) { @root.rename_child('Not_Present_Child1', 'ALT_Child1') }
|
1606
1606
|
|
1607
1607
|
@root.rename_child('Child1', 'ALT_Child1')
|
1608
1608
|
assert_equal('ALT_Child1', @child1.name, "Name should be 'ALT_Child1'")
|
1609
1609
|
assert_equal(@child1, @root['ALT_Child1'], 'Should be able to access from parent using new name')
|
1610
|
-
|
1611
1610
|
end
|
1612
1611
|
|
1613
1612
|
def test_change_parent
|
@@ -1653,7 +1652,6 @@ module TestTree
|
|
1653
1652
|
root2['3'] << root1['2']['4']
|
1654
1653
|
assert_equal('3', root2['3']['4'].parent.name) # This is on the new tree
|
1655
1654
|
assert_nil(root1['2']['4']) # This is on the old tree
|
1656
|
-
|
1657
1655
|
end
|
1658
1656
|
|
1659
1657
|
# Test the path_as_string method.
|
@@ -1721,9 +1719,9 @@ module TestTree
|
|
1721
1719
|
h << t
|
1722
1720
|
j << k << z
|
1723
1721
|
|
1724
|
-
assert_equal(e.path_as_array, %w
|
1725
|
-
assert_equal(p.path_as_array, %w
|
1726
|
-
assert_equal(k.path_as_array, %w
|
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])
|
1727
1725
|
end
|
1728
1726
|
end
|
1729
1727
|
end
|