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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 37;
use Test::Exception;
BEGIN {
use_ok('Tree::Simple::Visitor');
};
use Tree::Simple;
my $SIMPLE_SUB = sub { "test sub" };
# execute this otherwise Devel::Cover gives odd stats
$SIMPLE_SUB->();
# check that we have a constructor
can_ok("Tree::Simple::Visitor", 'new');
# -----------------------------------------------
# test the new style interface
# -----------------------------------------------
my $visitor = Tree::Simple::Visitor->new();
isa_ok($visitor, 'Tree::Simple::Visitor');
my $tree = Tree::Simple->new(Tree::Simple->ROOT)
->addChildren(
Tree::Simple->new("1")
->addChildren(
Tree::Simple->new("1.1"),
Tree::Simple->new("1.2")
->addChild(Tree::Simple->new("1.2.1")),
Tree::Simple->new("1.3")
),
Tree::Simple->new("2"),
Tree::Simple->new("3"),
);
isa_ok($tree, 'Tree::Simple');
$tree->accept($visitor);
can_ok($visitor, 'getResults');
is_deeply(
[ $visitor->getResults() ],
[ qw(1 1.1 1.2 1.2.1 1.3 2 3)],
'... got what we expected');
can_ok($visitor, 'setNodeFilter');
my $node_filter = sub { return "_" . $_[0]->getNodeValue() };
$visitor->setNodeFilter($node_filter);
can_ok($visitor, 'getNodeFilter');
is($visitor->getNodeFilter(), "$node_filter", '... got back what we put in');
# visit the tree again to get new results now
$tree->accept($visitor);
is_deeply(
scalar $visitor->getResults(),
[ qw(_1 _1.1 _1.2 _1.2.1 _1.3 _2 _3)],
'... got what we expected');
# test some exceptions
throws_ok {
$visitor->setNodeFilter();
} qr/Insufficient Arguments/, '... this should die';
throws_ok {
$visitor->setNodeFilter([]);
} qr/Insufficient Arguments/, '... this should die';
# -----------------------------------------------
# test the old style interface for backwards
# compatibility
# -----------------------------------------------
# and that our RECURSIVE constant is properly defined
can_ok("Tree::Simple::Visitor", 'RECURSIVE');
# and that our CHILDREN_ONLY constant is properly defined
can_ok("Tree::Simple::Visitor", 'CHILDREN_ONLY');
# no depth
my $visitor1 = Tree::Simple::Visitor->new($SIMPLE_SUB);
isa_ok($visitor1, 'Tree::Simple::Visitor');
# children only
my $visitor2 = Tree::Simple::Visitor->new($SIMPLE_SUB, Tree::Simple::Visitor->CHILDREN_ONLY);
isa_ok($visitor2, 'Tree::Simple::Visitor');
# recursive
my $visitor3 = Tree::Simple::Visitor->new($SIMPLE_SUB, Tree::Simple::Visitor->RECURSIVE);
isa_ok($visitor3, 'Tree::Simple::Visitor');
# -----------------------------------------------
# test constructor exceptions
# -----------------------------------------------
# we pass a bad depth (string)
throws_ok {
my $test = Tree::Simple::Visitor->new($SIMPLE_SUB, "Fail")
} qr/Insufficient Arguments \: Depth arguement must be either RECURSIVE or CHILDREN_ONLY/,
'... we are expecting this error';
# we pass a bad depth (numeric)
throws_ok {
my $test = Tree::Simple::Visitor->new($SIMPLE_SUB, 100)
} qr/Insufficient Arguments \: Depth arguement must be either RECURSIVE or CHILDREN_ONLY/,
'... we are expecting this error';
# we pass a non-ref func argument
throws_ok {
my $test = Tree::Simple::Visitor->new("Fail");
} qr/Insufficient Arguments \: filter function argument must be a subroutine reference/,
'... we are expecting this error';
# we pass a non-code-ref func arguement
throws_ok {
my $test = Tree::Simple::Visitor->new([]);
} qr/Insufficient Arguments \: filter function argument must be a subroutine reference/,
'... we are expecting this error';
# -----------------------------------------------
# test other exceptions
# -----------------------------------------------
# and make sure we can call the visit method
can_ok($visitor1, 'visit');
# test no arg
throws_ok {
$visitor1->visit();
} qr/Insufficient Arguments \: You must supply a valid Tree\:\:Simple object/,
'... we are expecting this error';
# test non-ref arg
throws_ok {
$visitor1->visit("Fail");
} qr/Insufficient Arguments \: You must supply a valid Tree\:\:Simple object/,
'... we are expecting this error';
# test non-object ref arg
throws_ok {
$visitor1->visit([]);
} qr/Insufficient Arguments \: You must supply a valid Tree\:\:Simple object/,
'... we are expecting this error';
my $BAD_OBJECT = bless({}, "Test");
# test non-Tree::Simple object arg
throws_ok {
$visitor1->visit($BAD_OBJECT);
} qr/Insufficient Arguments \: You must supply a valid Tree\:\:Simple object/,
'... we are expecting this error';
# -----------------------------------------------
# Test accept & visit
# -----------------------------------------------
# Note:
# this test could be made more robust by actually
# getting results and testing them from the
# Visitor object. But for right now it is good
# enough to have the code coverage, and know
# all the pieces work.
# -----------------------------------------------
# now make a tree
my $tree1 = Tree::Simple->new(Tree::Simple->ROOT)
->addChildren(
Tree::Simple->new("1.0"),
Tree::Simple->new("2.0"),
Tree::Simple->new("3.0"),
);
isa_ok($tree1, 'Tree::Simple');
cmp_ok($tree1->getChildCount(), '==', 3, '... there are 3 children here');
# and pass the visitor1 to accept
lives_ok {
$tree1->accept($visitor1);
} '.. this passes fine';
# and pass the visitor2 to accept
lives_ok {
$tree1->accept($visitor2);
} '.. this passes fine';
# and pass the visitor3 to accept
lives_ok {
$tree1->accept($visitor3);
} '.. this passes fine';
# ----------------------------------------------------
# test some misc. weirdness to get the coverage up :P
# ----------------------------------------------------
# check that includeTrunk works as we expect it to
{
my $visitor = Tree::Simple::Visitor->new();
ok(!$visitor->includeTrunk(), '... this should be false right now');
$visitor->includeTrunk("true");
ok($visitor->includeTrunk(), '... this should be true now');
$visitor->includeTrunk(undef);
ok($visitor->includeTrunk(), '... this should be true still');
$visitor->includeTrunk("");
ok(!$visitor->includeTrunk(), '... this should be false again');
}
# check that clearNodeFilter works as we expect it to
{
my $visitor = Tree::Simple::Visitor->new();
my $filter = sub { "filter" };
$visitor->setNodeFilter($filter);
is($visitor->getNodeFilter(), $filter, 'our node filter is set correctly');
$visitor->clearNodeFilter();
ok(! defined($visitor->getNodeFilter()), '... our node filter has now been undefined');
}
|