1#!/usr/bin/perl 2 3use strict; 4use warnings; 5 6use Test::More tests => 46; 7 8## ---------------------------------------------------------------------------- 9## fixDepth Tests for Tree::Simple 10## ---------------------------------------------------------------------------- 11# NOTE: 12# This specifically tests the fixDepth function, which is run when a non-leaf 13# tree is added to a tree. It basically fixes the depth field so that it 14# correctly reflects the new depth 15## ---------------------------------------------------------------------------- 16 17use Tree::Simple; 18 19# create our tree to later add-in 20my $tree = Tree::Simple->new("2.1") 21 ->addChildren( 22 Tree::Simple->new("2.1.1"), 23 Tree::Simple->new("2.1.2"), 24 Tree::Simple->new("2.1.2") 25 ); 26 27# make sure its a root 28ok($tree->isRoot(), '... our tree is a root'); 29 30# and it is not a leaf 31ok(!$tree->isLeaf(), '... and it is not a leaf'); 32 33# and that its depth is -1 34cmp_ok($tree->getDepth(), '==', -1, '... our depth should be -1'); 35 36# and check our child count 37# while we are at it 38cmp_ok($tree->getChildCount(), '==', 3, '... we have 3 children'); 39 40# now check each subtree 41foreach my $sub_tree ($tree->getAllChildren()) { 42 # they are not root 43 ok(!$sub_tree->isRoot(), '... our subtree is not a root'); 44 # they are leaves 45 ok($sub_tree->isLeaf(), '... however it is a leaf'); 46 # and their parent is $tree 47 is($sub_tree->getParent(), $tree, '... these should both be equal'); 48 # their depth should be 0 49 cmp_ok($sub_tree->getDepth(), '==', 0, '... our depth should be 0'); 50 # and their siblings should match 51 # the children of their parent 52 is_deeply( 53 [ $tree->getAllChildren() ], 54 [ $sub_tree->getAllSiblings() ], 55 '... our siblings are the same'); 56} 57 58# at this point we know we have a 59# solid correct structure in $tree 60# we can now test against that 61# correctness 62 63# now create our other tree 64# which we will add $tree too 65my $parent_tree = Tree::Simple->new(Tree::Simple->ROOT); 66$parent_tree->addChildren( 67 Tree::Simple->new("1"), 68 Tree::Simple->new("2") 69 ); 70 71# make sure its a root 72ok($parent_tree->isRoot(), '... our parent tree is a root'); 73 74# and that its not a leaf 75ok(!$parent_tree->isLeaf(), '... our parent tree is a leaf'); 76 77# check the depth, which should be -1 78cmp_ok($parent_tree->getDepth(), '==', -1, '... our depth should be -1'); 79 80# and our child count is 2 81cmp_ok($parent_tree->getChildCount(), '==', 2, '... we have 2 children'); 82 83# now check our subtrees 84foreach my $sub_tree ($parent_tree->getAllChildren()) { 85 # make sure they are not roots 86 ok(!$sub_tree->isRoot(), '... the sub tree is not a root'); 87 # and they are leaves 88 ok($sub_tree->isLeaf(), '... but it is a leaf'); 89 # and their parent is $parent_tree 90 is($sub_tree->getParent(), $parent_tree, '... these should both be equal'); 91 # and their depth is 0 92 cmp_ok($sub_tree->getDepth(), '==', 0, '... our depth should be 0'); 93 # and that all their siblinds match 94 # the children of their parent 95 is_deeply( 96 [ $parent_tree->getAllChildren() ], 97 [ $sub_tree->getAllSiblings() ], 98 '... the siblings are the same as the children'); 99} 100 101# now here comes the heart of this test 102# we now add in $tree (2.1) as a child 103# of the second child of the parent (2) 104$parent_tree->getChild(1)->addChild($tree); 105 106# now we verify that $tree no longer 107# thinks that its a root 108ok(!$tree->isRoot(), '... our tree is not longer a root'); 109 110# that $tree's depth has been 111# updated to reflect its new place 112# in the hierarchy (1) 113cmp_ok($tree->getDepth(), '==', 1, '... our depth should be 1'); 114 115# that $tree's parent is not shown to be 116# the second child of $parent_tree 117is($tree->getParent(), $parent_tree->getChild(1), '... these should both be equal'); 118 119# and now we check $tree's children 120foreach my $sub_tree ($tree->getAllChildren()) { 121 # their depth should have been 122 # updated to reflect their new 123 # place in the hierarchy, so they 124 # are now at a depth of 2 125 cmp_ok($sub_tree->getDepth(), '==', 2, '... our depth should be 2'); 126 127} 128 129# now we need to test what happens when we remove stuff 130 131my $removed = $parent_tree->getChild(1)->removeChild($tree); 132 133is($removed, $tree, '... we got the same tree'); 134 135# make sure its a root 136ok($removed->isRoot(), '... our tree is a root again'); 137 138# and it is not a leaf 139ok(!$removed->isLeaf(), '... and it is not a leaf'); 140 141# and that its depth is -1 142cmp_ok($removed->getDepth(), '==', -1, '... our depth should be corrected to be -1'); 143 144# now check each subtree 145foreach my $sub_tree ($removed->getAllChildren()) { 146 # their depth should be 0 now 147 cmp_ok($sub_tree->getDepth(), '==', 0, '... our depth should be corrected to be 0'); 148} 149 150## ---------------------------------------------------------------------------- 151## end fixDepth Tests for Tree::Simple 152## ---------------------------------------------------------------------------- 153 154