11_Tree_Simple_fixDepth_test.t [plain text]
use strict;
use warnings;
use Test::More tests => 46;
use Tree::Simple;
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")
);
ok($tree->isRoot(), '... our tree is a root');
ok(!$tree->isLeaf(), '... and it is not a leaf');
cmp_ok($tree->getDepth(), '==', -1, '... our depth should be -1');
cmp_ok($tree->getChildCount(), '==', 3, '... we have 3 children');
foreach my $sub_tree ($tree->getAllChildren()) {
ok(!$sub_tree->isRoot(), '... our subtree is not a root');
ok($sub_tree->isLeaf(), '... however it is a leaf');
is($sub_tree->getParent(), $tree, '... these should both be equal');
cmp_ok($sub_tree->getDepth(), '==', 0, '... our depth should be 0');
is_deeply(
[ $tree->getAllChildren() ],
[ $sub_tree->getAllSiblings() ],
'... our siblings are the same');
}
my $parent_tree = Tree::Simple->new(Tree::Simple->ROOT);
$parent_tree->addChildren(
Tree::Simple->new("1"),
Tree::Simple->new("2")
);
ok($parent_tree->isRoot(), '... our parent tree is a root');
ok(!$parent_tree->isLeaf(), '... our parent tree is a leaf');
cmp_ok($parent_tree->getDepth(), '==', -1, '... our depth should be -1');
cmp_ok($parent_tree->getChildCount(), '==', 2, '... we have 2 children');
foreach my $sub_tree ($parent_tree->getAllChildren()) {
ok(!$sub_tree->isRoot(), '... the sub tree is not a root');
ok($sub_tree->isLeaf(), '... but it is a leaf');
is($sub_tree->getParent(), $parent_tree, '... these should both be equal');
cmp_ok($sub_tree->getDepth(), '==', 0, '... our depth should be 0');
is_deeply(
[ $parent_tree->getAllChildren() ],
[ $sub_tree->getAllSiblings() ],
'... the siblings are the same as the children');
}
$parent_tree->getChild(1)->addChild($tree);
ok(!$tree->isRoot(), '... our tree is not longer a root');
cmp_ok($tree->getDepth(), '==', 1, '... our depth should be 1');
is($tree->getParent(), $parent_tree->getChild(1), '... these should both be equal');
foreach my $sub_tree ($tree->getAllChildren()) {
cmp_ok($sub_tree->getDepth(), '==', 2, '... our depth should be 2');
}
my $removed = $parent_tree->getChild(1)->removeChild($tree);
is($removed, $tree, '... we got the same tree');
ok($removed->isRoot(), '... our tree is a root again');
ok(!$removed->isLeaf(), '... and it is not a leaf');
cmp_ok($removed->getDepth(), '==', -1, '... our depth should be corrected to be -1');
foreach my $sub_tree ($removed->getAllChildren()) {
cmp_ok($sub_tree->getDepth(), '==', 0, '... our depth should be corrected to be 0');
}