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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 58;
use Test::Exception;
BEGIN {
use_ok('Tree::Simple::Visitor::FromNestedArray');
}
use Tree::Simple;
my $array_tree = [
'Root', [
'Child1', [
'GrandChild1',
'GrandChild2'
],
'Child2'
]
];
can_ok("Tree::Simple::Visitor::FromNestedArray", 'new');
{ # check normal behavior
my $visitor = Tree::Simple::Visitor::FromNestedArray->new();
isa_ok($visitor, 'Tree::Simple::Visitor::FromNestedArray');
isa_ok($visitor, 'Tree::Simple::Visitor');
can_ok($visitor, 'setArrayTree');
$visitor->setArrayTree($array_tree);
can_ok($visitor, 'visit');
my $tree = Tree::Simple->new(Tree::Simple->ROOT);
$tree->accept($visitor);
my $root = $tree->getChild(0);
is($root->getNodeValue(), 'Root', '... got the value we expected from Root');
cmp_ok($root->getChildCount(), '==', 2, '... Root has 2 children');
my ($child1, $child2) = $root->getAllChildren();
is($child1->getNodeValue(), 'Child1', '... got the value we expected from Child1');
cmp_ok($child1->getChildCount(), '==', 2, '... Child1 has 2 children');
my ($grandchild1, $grandchild2) = $child1->getAllChildren();
is($grandchild1->getNodeValue(), 'GrandChild1', '... got the value we expected from GrandChild1');
ok($grandchild1->isLeaf(), '... GrandChild1 is a leaf node');
is($grandchild2->getNodeValue(), 'GrandChild2', '... got the value we expected from GrandChild2');
ok($grandchild2->isLeaf(), '... GrandChild2 is a leaf node');
is($child2->getNodeValue(), 'Child2', '... got the value we expected from Child2');
ok($child2->isLeaf(), '... Child2 is a leaf node');
}
{ # check includeTrunk behavior
my $visitor = Tree::Simple::Visitor::FromNestedArray->new();
isa_ok($visitor, 'Tree::Simple::Visitor::FromNestedArray');
isa_ok($visitor, 'Tree::Simple::Visitor');
can_ok($visitor, 'setArrayTree');
$visitor->setArrayTree($array_tree);
can_ok($visitor, 'includeTrunk');
$visitor->includeTrunk(1);
can_ok($visitor, 'visit');
my $tree = Tree::Simple->new(Tree::Simple->ROOT);
$tree->accept($visitor);
my $root = $tree;
is($root->getNodeValue(), 'Root', '... got the value we expected from Root');
cmp_ok($root->getChildCount(), '==', 2, '... Root has 2 children');
my ($child1, $child2) = $root->getAllChildren();
is($child1->getNodeValue(), 'Child1', '... got the value we expected from Child1');
cmp_ok($child1->getChildCount(), '==', 2, '... Child1 has 2 children');
my ($grandchild1, $grandchild2) = $child1->getAllChildren();
is($grandchild1->getNodeValue(), 'GrandChild1', '... got the value we expected from GrandChild1');
ok($grandchild1->isLeaf(), '... GrandChild1 is a leaf node');
is($grandchild2->getNodeValue(), 'GrandChild2', '... got the value we expected from GrandChild2');
ok($grandchild2->isLeaf(), '... GrandChild2 is a leaf node');
is($child2->getNodeValue(), 'Child2', '... got the value we expected from Child2');
ok($child2->isLeaf(), '... Child2 is a leaf node');
}
{ # check nodeFilter behavior
my $visitor = Tree::Simple::Visitor::FromNestedArray->new();
isa_ok($visitor, 'Tree::Simple::Visitor::FromNestedArray');
isa_ok($visitor, 'Tree::Simple::Visitor');
can_ok($visitor, 'setArrayTree');
$visitor->setArrayTree($array_tree);
can_ok($visitor, 'setNodeFilter');
$visitor->setNodeFilter(sub {
my ($node) = @_;
return uc($node);
});
can_ok($visitor, 'visit');
my $tree = Tree::Simple->new(Tree::Simple->ROOT);
$tree->accept($visitor);
my $root = $tree->getChild(0);
is($root->getNodeValue(), 'ROOT', '... got the value we expected from Root');
cmp_ok($root->getChildCount(), '==', 2, '... Root has 2 children');
my ($child1, $child2) = $root->getAllChildren();
is($child1->getNodeValue(), 'CHILD1', '... got the value we expected from Child1');
cmp_ok($child1->getChildCount(), '==', 2, '... Child1 has 2 children');
my ($grandchild1, $grandchild2) = $child1->getAllChildren();
is($grandchild1->getNodeValue(), 'GRANDCHILD1', '... got the value we expected from GrandChild1');
ok($grandchild1->isLeaf(), '... GrandChild1 is a leaf node');
is($grandchild2->getNodeValue(), 'GRANDCHILD2', '... got the value we expected from GrandChild2');
ok($grandchild2->isLeaf(), '... GrandChild2 is a leaf node');
is($child2->getNodeValue(), 'CHILD2', '... got the value we expected from Child2');
ok($child2->isLeaf(), '... Child2 is a leaf node');
}
{
my $visitor = Tree::Simple::Visitor::FromNestedArray->new();
isa_ok($visitor, 'Tree::Simple::Visitor::FromNestedArray');
isa_ok($visitor, 'Tree::Simple::Visitor');
# check visit
throws_ok {
$visitor->visit();
} qr/Insufficient Arguments/, '... got the error we expected';
throws_ok {
$visitor->visit("Fail");
} qr/Insufficient Arguments/, '... got the error we expected';
throws_ok {
$visitor->visit([]);
} qr/Insufficient Arguments/, '... got the error we expected';
throws_ok {
$visitor->visit(bless({}, "Fail"));
} qr/Insufficient Arguments/, '... got the error we expected';
# check setHashTree
throws_ok {
$visitor->setArrayTree();
} qr/Insufficient Arguments/, '... got the error we expected';
throws_ok {
$visitor->setArrayTree("Fail");
} qr/Insufficient Arguments/, '... got the error we expected';
throws_ok {
$visitor->setArrayTree([]);
} qr/Insufficient Arguments/, '... got the error we expected';
throws_ok {
$visitor->setArrayTree([[]]);
} qr/Incorrect Object Type/, '... got the error we expected';
throws_ok {
$visitor->setArrayTree(['root', 'Fail']);
} qr/Incorrect Object Type/, '... got the error we expected';
$visitor->setArrayTree(['root', [[]]]);
throws_ok {
$visitor->visit(Tree::Simple->new(Tree::Simple->ROOT));
} qr/Incorrect Object Type/, '... got the error we expected';
}
|