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