comparison vendor/gems/rubytree-0.5.2/test/.svn/text-base/test_tree.rb.svn-base @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:513646585e45
1 #!/usr/bin/env ruby
2
3 # testtree.rb
4 #
5 # $Revision: 1.6 $ by $Author: anupamsg $
6 # $Name: $
7 #
8 # Copyright (c) 2006, 2007 Anupam Sengupta
9 #
10 # All rights reserved.
11 #
12 # Redistribution and use in source and binary forms, with or without modification,
13 # are permitted provided that the following conditions are met:
14 #
15 # - Redistributions of source code must retain the above copyright notice, this
16 # list of conditions and the following disclaimer.
17 #
18 # - Redistributions in binary form must reproduce the above copyright notice, this
19 # list of conditions and the following disclaimer in the documentation and/or
20 # other materials provided with the distribution.
21 #
22 # - Neither the name of the organization nor the names of its contributors may
23 # be used to endorse or promote products derived from this software without
24 # specific prior written permission.
25 #
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
30 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
33 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #
37
38 require 'test/unit'
39 require 'tree'
40
41 module TestTree
42 # Test class for the Tree node.
43 class TestTreeNode < Test::Unit::TestCase
44
45 Person = Struct::new(:First, :last)
46
47 def setup
48 @root = Tree::TreeNode.new("ROOT", "Root Node")
49
50 @child1 = Tree::TreeNode.new("Child1", "Child Node 1")
51 @child2 = Tree::TreeNode.new("Child2", "Child Node 2")
52 @child3 = Tree::TreeNode.new("Child3", "Child Node 3")
53 @child4 = Tree::TreeNode.new("Child31", "Grand Child 1")
54
55 end
56
57 # Create this structure for the tests
58 #
59 # +----------+
60 # | ROOT |
61 # +-+--------+
62 # |
63 # | +---------------+
64 # +----+ CHILD1 |
65 # | +---------------+
66 # |
67 # | +---------------+
68 # +----+ CHILD2 |
69 # | +---------------+
70 # |
71 # | +---------------+ +------------------+
72 # +----+ CHILD3 +---+ CHILD4 |
73 # +---------------+ +------------------+
74 #
75 def loadChildren
76 @root << @child1
77 @root << @child2
78 @root << @child3 << @child4
79 end
80
81 def teardown
82 @root = nil
83 end
84
85 def test_root_setup
86 assert_not_nil(@root, "Root cannot be nil")
87 assert_nil(@root.parent, "Parent of root node should be nil")
88 assert_not_nil(@root.name, "Name should not be nil")
89 assert_equal("ROOT", @root.name, "Name should be 'ROOT'")
90 assert_equal("Root Node", @root.content, "Content should be 'Root Node'")
91 assert(@root.isRoot?, "Should identify as root")
92 assert(!@root.hasChildren?, "Cannot have any children")
93 assert(@root.hasContent?, "This root should have content")
94 assert_equal(1, @root.size, "Number of nodes should be one")
95 assert_nil(@root.siblings, "Root cannot have any children")
96
97 assert_raise(RuntimeError) { Tree::TreeNode.new(nil) }
98 end
99
100 def test_root
101 loadChildren
102
103 assert_same(@root, @root.root, "Root's root is self")
104 assert_same(@root, @child1.root, "Root should be ROOT")
105 assert_same(@root, @child4.root, "Root should be ROOT")
106 end
107
108 def test_hasContent_eh
109 aNode = Tree::TreeNode.new("A Node")
110 assert_nil(aNode.content, "The node should not have content")
111 assert(!aNode.hasContent?, "The node should not have content")
112
113 aNode.content = "Something"
114 assert_not_nil(aNode.content, "The node should now have content")
115 assert(aNode.hasContent?, "The node should now have content")
116 end
117
118 def test_length
119 loadChildren
120 assert_equal(@root.size, @root.length, "Length and size methods should return the same result")
121 end
122
123 def test_spaceship # Test the <=> operator.
124 firstNode = Tree::TreeNode.new(1)
125 secondNode = Tree::TreeNode.new(2)
126
127 assert_equal(firstNode <=> nil, +1)
128 assert_equal(firstNode <=> secondNode, -1)
129
130 secondNode = Tree::TreeNode.new(1)
131 assert_equal(firstNode <=> secondNode, 0)
132
133 firstNode = Tree::TreeNode.new("ABC")
134 secondNode = Tree::TreeNode.new("XYZ")
135
136 assert_equal(firstNode <=> nil, +1)
137 assert_equal(firstNode <=> secondNode, -1)
138
139 secondNode = Tree::TreeNode.new("ABC")
140 assert_equal(firstNode <=> secondNode, 0)
141 end
142
143 def test_to_s
144 aNode = Tree::TreeNode.new("A Node", "Some Content")
145
146 expectedString = "Node Name: A Node Content: Some Content Parent: <None> Children: 0 Total Nodes: 1"
147
148 assert_equal(expectedString, aNode.to_s, "The string representation should be same")
149 end
150
151 def test_firstSibling
152 loadChildren
153
154 assert_same(@root, @root.firstSibling, "Root's first sibling is itself")
155 assert_same(@child1, @child1.firstSibling, "Child1's first sibling is itself")
156 assert_same(@child1, @child2.firstSibling, "Child2's first sibling should be child1")
157 assert_same(@child1, @child3.firstSibling, "Child3's first sibling should be child1")
158 assert_not_same(@child1, @child4.firstSibling, "Child4's first sibling is itself")
159 end
160
161 def test_isFirstSibling_eh
162 loadChildren
163
164 assert(@root.isFirstSibling?, "Root's first sibling is itself")
165 assert( @child1.isFirstSibling?, "Child1's first sibling is itself")
166 assert(!@child2.isFirstSibling?, "Child2 is not the first sibling")
167 assert(!@child3.isFirstSibling?, "Child3 is not the first sibling")
168 assert( @child4.isFirstSibling?, "Child4's first sibling is itself")
169 end
170
171 def test_isLastSibling_eh
172 loadChildren
173
174 assert(@root.isLastSibling?, "Root's last sibling is itself")
175 assert(!@child1.isLastSibling?, "Child1 is not the last sibling")
176 assert(!@child2.isLastSibling?, "Child2 is not the last sibling")
177 assert( @child3.isLastSibling?, "Child3's last sibling is itself")
178 assert( @child4.isLastSibling?, "Child4's last sibling is itself")
179 end
180
181 def test_lastSibling
182 loadChildren
183
184 assert_same(@root, @root.lastSibling, "Root's last sibling is itself")
185 assert_same(@child3, @child1.lastSibling, "Child1's last sibling should be child3")
186 assert_same(@child3, @child2.lastSibling, "Child2's last sibling should be child3")
187 assert_same(@child3, @child3.lastSibling, "Child3's last sibling should be itself")
188 assert_not_same(@child3, @child4.lastSibling, "Child4's last sibling is itself")
189 end
190
191 def test_siblings
192 loadChildren
193
194 siblings = []
195 @child1.siblings { |sibling| siblings << sibling}
196 assert_equal(2, siblings.length, "Should have two siblings")
197 assert(siblings.include?(@child2), "Should have 2nd child as sibling")
198 assert(siblings.include?(@child3), "Should have 3rd child as sibling")
199
200 siblings.clear
201 siblings = @child1.siblings
202 assert_equal(2, siblings.length, "Should have two siblings")
203
204 siblings.clear
205 @child4.siblings {|sibling| siblings << sibling}
206 assert(siblings.empty?, "Should not have any children")
207
208 end
209
210 def test_isOnlyChild_eh
211 loadChildren
212
213 assert(!@child1.isOnlyChild?, "Child1 is not the only child")
214 assert(!@child2.isOnlyChild?, "Child2 is not the only child")
215 assert(!@child3.isOnlyChild?, "Child3 is not the only child")
216 assert( @child4.isOnlyChild?, "Child4 is not the only child")
217 end
218
219 def test_nextSibling
220 loadChildren
221
222 assert_equal(@child2, @child1.nextSibling, "Child1's next sibling is Child2")
223 assert_equal(@child3, @child2.nextSibling, "Child2's next sibling is Child3")
224 assert_nil(@child3.nextSibling, "Child3 does not have a next sibling")
225 assert_nil(@child4.nextSibling, "Child4 does not have a next sibling")
226 end
227
228 def test_previousSibling
229 loadChildren
230
231 assert_nil(@child1.previousSibling, "Child1 does not have previous sibling")
232 assert_equal(@child1, @child2.previousSibling, "Child2's previous sibling is Child1")
233 assert_equal(@child2, @child3.previousSibling, "Child3's previous sibling is Child2")
234 assert_nil(@child4.previousSibling, "Child4 does not have a previous sibling")
235 end
236
237 def test_add
238 assert(!@root.hasChildren?, "Should not have any children")
239
240 @root.add(@child1)
241
242 @root << @child2
243
244 assert(@root.hasChildren?, "Should have children")
245 assert_equal(3, @root.size, "Should have three nodes")
246
247 @root << @child3 << @child4
248
249 assert_equal(5, @root.size, "Should have five nodes")
250 assert_equal(2, @child3.size, "Should have two nodes")
251
252 assert_raise(RuntimeError) { @root.add(Tree::TreeNode.new(@child1.name)) }
253
254 end
255
256 def test_remove_bang
257 @root << @child1
258 @root << @child2
259
260 assert(@root.hasChildren?, "Should have children")
261 assert_equal(3, @root.size, "Should have three nodes")
262
263 @root.remove!(@child1)
264 assert_equal(2, @root.size, "Should have two nodes")
265 @root.remove!(@child2)
266
267 assert(!@root.hasChildren?, "Should have no children")
268 assert_equal(1, @root.size, "Should have one node")
269
270 @root << @child1
271 @root << @child2
272
273 assert(@root.hasChildren?, "Should have children")
274 assert_equal(3, @root.size, "Should have three nodes")
275
276 @root.removeAll!
277
278 assert(!@root.hasChildren?, "Should have no children")
279 assert_equal(1, @root.size, "Should have one node")
280
281 end
282
283 def test_removeAll_bang
284 loadChildren
285 assert(@root.hasChildren?, "Should have children")
286 @root.removeAll!
287
288 assert(!@root.hasChildren?, "Should have no children")
289 assert_equal(1, @root.size, "Should have one node")
290 end
291
292 def test_removeFromParent_bang
293 loadChildren
294 assert(@root.hasChildren?, "Should have children")
295 assert(!@root.isLeaf?, "Root is not a leaf here")
296
297 child1 = @root[0]
298 assert_not_nil(child1, "Child 1 should exist")
299 assert_same(@root, child1.root, "Child 1's root should be ROOT")
300 assert(@root.include?(child1), "root should have child1")
301 child1.removeFromParent!
302 assert_same(child1, child1.root, "Child 1's root should be self")
303 assert(!@root.include?(child1), "root should not have child1")
304
305 child1.removeFromParent!
306 assert_same(child1, child1.root, "Child 1's root should still be self")
307 end
308
309 def test_children
310 loadChildren
311
312 assert(@root.hasChildren?, "Should have children")
313 assert_equal(5, @root.size, "Should have four nodes")
314 assert(@child3.hasChildren?, "Should have children")
315 assert(!@child3.isLeaf?, "Should not be a leaf")
316
317 children = []
318 for child in @root.children
319 children << child
320 end
321
322 assert_equal(3, children.length, "Should have three direct children")
323 assert(!children.include?(@root), "Should not have root")
324 assert(children.include?(@child1), "Should have child 1")
325 assert(children.include?(@child2), "Should have child 2")
326 assert(children.include?(@child3), "Should have child 3")
327 assert(!children.include?(@child4), "Should not have child 4")
328
329 children.clear
330 children = @root.children
331 assert_equal(3, children.length, "Should have three children")
332
333 end
334
335 def test_firstChild
336 loadChildren
337
338 assert_equal(@child1, @root.firstChild, "Root's first child is Child1")
339 assert_nil(@child1.firstChild, "Child1 does not have any children")
340 assert_equal(@child4, @child3.firstChild, "Child3's first child is Child4")
341
342 end
343
344 def test_lastChild
345 loadChildren
346
347 assert_equal(@child3, @root.lastChild, "Root's last child is Child3")
348 assert_nil(@child1.lastChild, "Child1 does not have any children")
349 assert_equal(@child4, @child3.lastChild, "Child3's last child is Child4")
350
351 end
352
353 def test_find
354 loadChildren
355 foundNode = @root.find { |node| node == @child2}
356 assert_same(@child2, foundNode, "The node should be Child 2")
357
358 foundNode = @root.find { |node| node == @child4}
359 assert_same(@child4, foundNode, "The node should be Child 4")
360
361 foundNode = @root.find { |node| node.name == "Child31" }
362 assert_same(@child4, foundNode, "The node should be Child 4")
363 foundNode = @root.find { |node| node.name == "NOT PRESENT" }
364 assert_nil(foundNode, "The node should not be found")
365 end
366
367 def test_parentage
368 loadChildren
369
370 assert_nil(@root.parentage, "Root does not have any parentage")
371 assert_equal([@root], @child1.parentage, "Child1 has Root as its parent")
372 assert_equal([@child3, @root], @child4.parentage, "Child4 has Child3 and Root as ancestors")
373 end
374
375 def test_each
376 loadChildren
377 assert(@root.hasChildren?, "Should have children")
378 assert_equal(5, @root.size, "Should have five nodes")
379 assert(@child3.hasChildren?, "Should have children")
380
381 nodes = []
382 @root.each { |node| nodes << node }
383
384 assert_equal(5, nodes.length, "Should have FIVE NODES")
385 assert(nodes.include?(@root), "Should have root")
386 assert(nodes.include?(@child1), "Should have child 1")
387 assert(nodes.include?(@child2), "Should have child 2")
388 assert(nodes.include?(@child3), "Should have child 3")
389 assert(nodes.include?(@child4), "Should have child 4")
390 end
391
392 def test_each_leaf
393 loadChildren
394
395 nodes = []
396 @root.each_leaf { |node| nodes << node }
397
398 assert_equal(3, nodes.length, "Should have THREE LEAF NODES")
399 assert(!nodes.include?(@root), "Should not have root")
400 assert(nodes.include?(@child1), "Should have child 1")
401 assert(nodes.include?(@child2), "Should have child 2")
402 assert(!nodes.include?(@child3), "Should not have child 3")
403 assert(nodes.include?(@child4), "Should have child 4")
404 end
405
406 def test_parent
407 loadChildren
408 assert_nil(@root.parent, "Root's parent should be nil")
409 assert_equal(@root, @child1.parent, "Parent should be root")
410 assert_equal(@root, @child3.parent, "Parent should be root")
411 assert_equal(@child3, @child4.parent, "Parent should be child3")
412 assert_equal(@root, @child4.parent.parent, "Parent should be root")
413 end
414
415 def test_indexed_access
416 loadChildren
417 assert_equal(@child1, @root[0], "Should be the first child")
418 assert_equal(@child4, @root[2][0], "Should be the grandchild")
419 assert_nil(@root["TEST"], "Should be nil")
420 assert_raise(RuntimeError) { @root[nil] }
421 end
422
423 def test_printTree
424 loadChildren
425 #puts
426 #@root.printTree
427 end
428
429 # Tests the binary dumping mechanism with an Object content node
430 def test_marshal_dump
431 # Setup Test Data
432 test_root = Tree::TreeNode.new("ROOT", "Root Node")
433 test_content = {"KEY1" => "Value1", "KEY2" => "Value2" }
434 test_child = Tree::TreeNode.new("Child", test_content)
435 test_content2 = ["AValue1", "AValue2", "AValue3"]
436 test_grand_child = Tree::TreeNode.new("Grand Child 1", test_content2)
437 test_root << test_child << test_grand_child
438
439 # Perform the test operation
440 data = Marshal.dump(test_root) # Marshal
441 new_root = Marshal.load(data) # And unmarshal
442
443 # Test the root node
444 assert_equal(test_root.name, new_root.name, "Must identify as ROOT")
445 assert_equal(test_root.content, new_root.content, "Must have root's content")
446 assert(new_root.isRoot?, "Must be the ROOT node")
447 assert(new_root.hasChildren?, "Must have a child node")
448
449 # Test the child node
450 new_child = new_root[test_child.name]
451 assert_equal(test_child.name, new_child.name, "Must have child 1")
452 assert(new_child.hasContent?, "Child must have content")
453 assert(new_child.isOnlyChild?, "Child must be the only child")
454
455 new_child_content = new_child.content
456 assert_equal(Hash, new_child_content.class, "Class of child's content should be a hash")
457 assert_equal(test_child.content.size, new_child_content.size, "The content should have same size")
458
459 # Test the grand-child node
460 new_grand_child = new_child[test_grand_child.name]
461 assert_equal(test_grand_child.name, new_grand_child.name, "Must have grand child")
462 assert(new_grand_child.hasContent?, "Grand-child must have content")
463 assert(new_grand_child.isOnlyChild?, "Grand-child must be the only child")
464
465 new_grand_child_content = new_grand_child.content
466 assert_equal(Array, new_grand_child_content.class, "Class of grand-child's content should be an Array")
467 assert_equal(test_grand_child.content.size, new_grand_child_content.size, "The content should have same size")
468 end
469
470 # marshal_load and marshal_dump are symmetric methods
471 # This alias is for satisfying ZenTest
472 alias test_marshal_load test_marshal_dump
473
474 # Test the collect method from the mixed-in Enumerable functionality.
475 def test_collect
476 loadChildren
477 collectArray = @root.collect do |node|
478 node.content = "abc"
479 node
480 end
481 collectArray.each {|node| assert_equal("abc", node.content, "Should be 'abc'")}
482 end
483
484 # Test freezing the tree
485 def test_freezeTree_bang
486 loadChildren
487 @root.content = "ABC"
488 assert_equal("ABC", @root.content, "Content should be 'ABC'")
489 @root.freezeTree!
490 assert_raise(TypeError) {@root.content = "123"}
491 assert_raise(TypeError) {@root[0].content = "123"}
492 end
493
494 # Test whether the content is accesible
495 def test_content
496 pers = Person::new("John", "Doe")
497 @root.content = pers
498 assert_same(pers, @root.content, "Content should be the same")
499 end
500
501 # Test the depth computation algorithm
502 def test_depth
503 assert_equal(1, @root.depth, "A single node's depth is 1")
504
505 @root << @child1
506 assert_equal(2, @root.depth, "This should be of depth 2")
507
508 @root << @child2
509 assert_equal(2, @root.depth, "This should be of depth 2")
510
511 @child2 << @child3
512 assert_equal(3, @root.depth, "This should be of depth 3")
513 assert_equal(2, @child2.depth, "This should be of depth 2")
514
515 @child3 << @child4
516 assert_equal(4, @root.depth, "This should be of depth 4")
517 end
518
519 # Test the breadth computation algorithm
520 def test_breadth
521 assert_equal(1, @root.breadth, "A single node's breadth is 1")
522
523 @root << @child1
524 assert_equal(1, @root.breadth, "This should be of breadth 1")
525
526 @root << @child2
527 assert_equal(2, @child1.breadth, "This should be of breadth 2")
528 assert_equal(2, @child2.breadth, "This should be of breadth 2")
529
530 @root << @child3
531 assert_equal(3, @child1.breadth, "This should be of breadth 3")
532 assert_equal(3, @child2.breadth, "This should be of breadth 3")
533
534 @child3 << @child4
535 assert_equal(1, @child4.breadth, "This should be of breadth 1")
536 end
537
538 # Test the breadth for each
539 def test_breadth_each
540 j = Tree::TreeNode.new("j")
541 f = Tree::TreeNode.new("f")
542 k = Tree::TreeNode.new("k")
543 a = Tree::TreeNode.new("a")
544 d = Tree::TreeNode.new("d")
545 h = Tree::TreeNode.new("h")
546 z = Tree::TreeNode.new("z")
547
548 # The expected order of response
549 expected_array = [j,
550 f, k,
551 a, h, z,
552 d]
553
554 # Create the following Tree
555 # j <-- level 0 (Root)
556 # / \
557 # f k <-- level 1
558 # / \ \
559 # a h z <-- level 2
560 # \
561 # d <-- level 3
562 j << f << a << d
563 f << h
564 j << k << z
565
566 # Create the response
567 result_array = Array.new
568 j.breadth_each { |node| result_array << node.detached_copy }
569
570 expected_array.each_index do |i|
571 assert_equal(expected_array[i].name, result_array[i].name) # Match only the names.
572 end
573 end
574
575
576 def test_preordered_each
577 j = Tree::TreeNode.new("j")
578 f = Tree::TreeNode.new("f")
579 k = Tree::TreeNode.new("k")
580 a = Tree::TreeNode.new("a")
581 d = Tree::TreeNode.new("d")
582 h = Tree::TreeNode.new("h")
583 z = Tree::TreeNode.new("z")
584
585 # The expected order of response
586 expected_array = [j, f, a, d, h, k, z]
587
588 # Create the following Tree
589 # j <-- level 0 (Root)
590 # / \
591 # f k <-- level 1
592 # / \ \
593 # a h z <-- level 2
594 # \
595 # d <-- level 3
596 j << f << a << d
597 f << h
598 j << k << z
599
600 result_array = []
601 j.preordered_each { |node| result_array << node.detached_copy}
602
603 expected_array.each_index do |i|
604 # Match only the names.
605 assert_equal(expected_array[i].name, result_array[i].name)
606 end
607 end
608
609 def test_detached_copy
610 loadChildren
611
612 assert(@root.hasChildren?, "The root should have children")
613 copy_of_root = @root.detached_copy
614 assert(!copy_of_root.hasChildren?, "The copy should not have children")
615 assert_equal(@root.name, copy_of_root.name, "The names should be equal")
616
617 # Try the same test with a child node
618 assert(!@child3.isRoot?, "Child 3 is not a root")
619 assert(@child3.hasChildren?, "Child 3 has children")
620 copy_of_child3 = @child3.detached_copy
621 assert(copy_of_child3.isRoot?, "Child 3's copy is a root")
622 assert(!copy_of_child3.hasChildren?, "Child 3's copy does not have children")
623 end
624
625 def test_hasChildren_eh
626 loadChildren
627 assert(@root.hasChildren?, "The Root node MUST have children")
628 end
629
630 def test_isLeaf_eh
631 loadChildren
632 assert(!@child3.isLeaf?, "Child 3 is not a leaf node")
633 assert(@child4.isLeaf?, "Child 4 is a leaf node")
634 end
635
636 def test_isRoot_eh
637 loadChildren
638 assert(@root.isRoot?, "The ROOT node must respond as the root node")
639 end
640
641 def test_content_equals
642 @root.content = nil
643 assert_nil(@root.content, "Root's content should be nil")
644 @root.content = "ABCD"
645 assert_equal("ABCD", @root.content, "Root's content should now be 'ABCD'")
646 end
647
648 def test_size
649 assert_equal(1, @root.size, "Root's size should be 1")
650 loadChildren
651 assert_equal(5, @root.size, "Root's size should be 5")
652 assert_equal(2, @child3.size, "Child 3's size should be 2")
653 end
654
655 def test_lt2 # Test the << method
656 @root << @child1
657 @root << @child2
658 @root << @child3 << @child4
659 assert_not_nil(@root['Child1'], "Child 1 should have been added to Root")
660 assert_not_nil(@root['Child2'], "Child 2 should have been added to Root")
661 assert_not_nil(@root['Child3'], "Child 3 should have been added to Root")
662 assert_not_nil(@child3['Child31'], "Child 31 should have been added to Child3")
663 end
664
665 def test_index # Test the [] method
666 assert_raise(RuntimeError) {@root[nil]}
667
668 @root << @child1
669 @root << @child2
670 assert_equal(@child1.name, @root['Child1'].name, "Child 1 should be returned")
671 assert_equal(@child1.name, @root[0].name, "Child 1 should be returned")
672 assert_equal(@child2.name, @root['Child2'].name, "Child 2 should be returned")
673 assert_equal(@child2.name, @root[1].name, "Child 2 should be returned")
674
675 assert_nil(@root['Some Random Name'], "Should return nil")
676 assert_nil(@root[99], "Should return nil")
677 end
678 end
679 end
680
681 __END__
682
683 # $Log: test_tree.rb,v $
684 # Revision 1.6 2007/12/22 00:28:59 anupamsg
685 # Added more test cases, and enabled ZenTest compatibility.
686 #
687 # Revision 1.5 2007/12/19 02:24:18 anupamsg
688 # Updated the marshalling logic to handle non-string contents on the nodes.
689 #
690 # Revision 1.4 2007/10/02 03:38:11 anupamsg
691 # Removed dependency on the redundant "Person" class.
692 # (TC_TreeTest::test_comparator): Added a new test for the spaceship operator.
693 # (TC_TreeTest::test_hasContent): Added tests for hasContent? and length methods.
694 #
695 # Revision 1.3 2007/10/02 03:07:30 anupamsg
696 # * Rakefile: Added an optional task for rcov code coverage.
697 #
698 # * test/test_binarytree.rb: Removed the unnecessary dependency on "Person" class.
699 #
700 # * test/test_tree.rb: Removed dependency on the redundant "Person" class.
701 #
702 # Revision 1.2 2007/08/31 01:16:28 anupamsg
703 # Added breadth and pre-order traversals for the tree. Also added a method
704 # to return the detached copy of a node from the tree.
705 #
706 # Revision 1.1 2007/07/21 04:52:38 anupamsg
707 # Renamed the test files.
708 #
709 # Revision 1.13 2007/07/18 22:11:50 anupamsg
710 # Added depth and breadth methods for the TreeNode.
711 #
712 # Revision 1.12 2007/07/18 07:17:34 anupamsg
713 # Fixed a issue where TreeNode.ancestors was shadowing Module.ancestors. This method
714 # has been renamed to TreeNode.parentage.
715 #
716 # Revision 1.11 2007/07/17 03:39:29 anupamsg
717 # Moved the CVS Log keyword to end of the files.
718 #