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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 52;
use Test::Exception;
## ----------------------------------------------------------------------------
## Exception Tests for Tree::Simple
## ----------------------------------------------------------------------------
use Tree::Simple;
my $BAD_OBJECT = bless({}, "Fail");
my $TEST_SUB_TREE = Tree::Simple->new("test");
# -----------------------------------------------
# exceptions for new
# -----------------------------------------------
# not giving a proper argument for parent
throws_ok {
Tree::Simple->new("test", 0);
} qr/^Insufficient Arguments \:/, '... this should die';
# not giving a proper argument for parent
throws_ok {
Tree::Simple->new("test", []);
} qr/^Insufficient Arguments \:/, '... this should die';
# not giving a proper argument for parent
throws_ok {
Tree::Simple->new("test", $BAD_OBJECT);
} qr/^Insufficient Arguments \:/, '... this should die';
# -----------------------------------------------
my $tree = Tree::Simple->new(Tree::Simple->ROOT);
# -----------------------------------------------
# exceptions for setNodeValue
# -----------------------------------------------
# not giving an argument for setNodeValue
throws_ok {
$tree->setNodeValue();
} qr/^Insufficient Arguments \: must supply a value for node/, '... this should die';
# -----------------------------------------------
# exceptions for addChild
# -----------------------------------------------
# not giving an argument for addChild
throws_ok {
$tree->addChild();
} qr/^Insufficient Arguments : no tree\(s\) to insert/, '... this should die';
# giving an bad argument for addChild
throws_ok {
$tree->addChild("fail");
} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an bad argument for addChild
throws_ok {
$tree->addChild([]);
} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an bad object argument for addChild
throws_ok {
$tree->addChild($BAD_OBJECT);
} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# -----------------------------------------------
# exceptions for insertChild
# -----------------------------------------------
# giving no index argument for insertChild
throws_ok {
$tree->insertChild();
} qr/^Insufficient Arguments \: Cannot insert child without index/, '... this should die';
# giving an out of bounds index argument for insertChild
throws_ok {
$tree->insertChild(5);
} qr/^Index Out of Bounds \: got \(5\) expected no more than \(0\)/, '... this should die';
# giving an good index argument but no tree argument for insertChild
throws_ok {
$tree->insertChild(0);
} qr/^Insufficient Arguments \: no tree\(s\) to insert/, '... this should die';
# giving an good index argument but an undefined tree argument for insertChild
throws_ok {
$tree->insertChild(0, undef);
} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an good index argument but a non-object tree argument for insertChild
throws_ok {
$tree->insertChild(0, "Fail");
} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an good index argument but a non-object-ref tree argument for insertChild
throws_ok {
$tree->insertChild(0, []);
} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an good index argument but a bad object tree argument for insertChild
throws_ok {
$tree->insertChild(0, $BAD_OBJECT);
} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# -----------------------------------------------
# exceptions for insertChildren
# -----------------------------------------------
# NOTE:
# even though insertChild and insertChildren are
# implemented in the same function, it makes sense
# to future-proof our tests by checking it anyway
# this will help to save us the trouble later on
# giving no index argument for insertChild
throws_ok {
$tree->insertChildren();
} qr/^Insufficient Arguments \: Cannot insert child without index/, '... this should die';
# giving an out of bounds index argument for insertChild
throws_ok {
$tree->insertChildren(5);
} qr/^Index Out of Bounds \: got \(5\) expected no more than \(0\)/, '... this should die';
# giving an good index argument but no tree argument for insertChild
throws_ok {
$tree->insertChildren(0);
} qr/^Insufficient Arguments \: no tree\(s\) to insert/, '... this should die';
# giving an good index argument but an undefined tree argument for insertChild
throws_ok {
$tree->insertChildren(0, undef);
} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an good index argument but a non-object tree argument for insertChild
throws_ok {
$tree->insertChildren(0, "Fail");
} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an good index argument but a non-object-ref tree argument for insertChild
throws_ok {
$tree->insertChildren(0, []);
} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an good index argument but a bad object tree argument for insertChild
throws_ok {
$tree->insertChildren(0, $BAD_OBJECT);
} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# -----------------------------------------------
# exceptions for removeChildAt
# -----------------------------------------------
# giving no index argument for removeChildAt
throws_ok {
$tree->removeChildAt();
} qr/^Insufficient Arguments \: Cannot remove child without index/, '... this should die';
# attempt to remove a child when there are none
throws_ok {
$tree->removeChildAt(5);
} qr/^Illegal Operation \: There are no children to remove/, '... this should die';
# add a child now
$tree->addChild($TEST_SUB_TREE);
# giving no index argument for removeChildAt
throws_ok {
$tree->removeChildAt(5);
} qr/^Index Out of Bounds \: got \(5\) expected no more than \(1\)/, '... this should die';
is($tree->removeChildAt(0), $TEST_SUB_TREE, '... these should be the same');
# -----------------------------------------------
# exceptions for removeChild
# -----------------------------------------------
# giving no index argument for removeChild
throws_ok {
$tree->removeChild();
} qr/^Insufficient Arguments \: /, '... this should die';
# giving bad ref argument
throws_ok {
$tree->removeChild([]);
} qr/^Insufficient Arguments \: /, '... this should die';
# giving bad object argument
throws_ok {
$tree->removeChild($BAD_OBJECT);
} qr/^Insufficient Arguments \: /, '... this should die';
# giving bad object argument
throws_ok {
$tree->removeChild($TEST_SUB_TREE);
} qr/^Child Not Found \: /, '... this should die';
# -----------------------------------------------
# exceptions for *Sibling methods
# -----------------------------------------------
# attempting to add sibling to root trees
throws_ok {
$tree->addSibling($TEST_SUB_TREE);
} qr/^Insufficient Arguments \: cannot add a sibling to a ROOT tree/, '... this should die';
# attempting to add siblings to root trees
throws_ok {
$tree->addSiblings($TEST_SUB_TREE);
} qr/^Insufficient Arguments \: cannot add siblings to a ROOT tree/, '... this should die';
# attempting to insert sibling to root trees
throws_ok {
$tree->insertSibling(0, $TEST_SUB_TREE);
} qr/^Insufficient Arguments \: cannot insert sibling\(s\) to a ROOT tree/, '... this should die';
# attempting to insert sibling to root trees
throws_ok {
$tree->insertSiblings(0, $TEST_SUB_TREE);
} qr/^Insufficient Arguments \: cannot insert sibling\(s\) to a ROOT tree/, '... this should die';
# -----------------------------------------------
# exceptions for getChild
# -----------------------------------------------
# not giving an index to the getChild method
throws_ok {
$tree->getChild();
} qr/^Insufficient Arguments \: Cannot get child without index/, '... this should die';
# -----------------------------------------------
# exceptions for getSibling
# -----------------------------------------------
# trying to get siblings of a root tree
throws_ok {
$tree->getSibling();
} qr/^Insufficient Arguments \: cannot get siblings from a ROOT tree/, '... this should die';
# trying to get siblings of a root tree
throws_ok {
$tree->getAllSiblings();
} qr/^Insufficient Arguments \: cannot get siblings from a ROOT tree/, '... this should die';
# -----------------------------------------------
# exceptions for traverse
# -----------------------------------------------
# passing no args to traverse
throws_ok {
$tree->traverse();
} qr/^Insufficient Arguments \: Cannot traverse without traversal function/, '... this should die';
# passing non-ref arg to traverse
throws_ok {
$tree->traverse("Fail");
} qr/^Incorrect Object Type \: traversal function is not a function/, '... this should die';
# passing non-code-ref arg to traverse
throws_ok {
$tree->traverse($BAD_OBJECT);
} qr/^Incorrect Object Type \: traversal function is not a function/, '... this should die';
# passing second non-ref arg to traverse
throws_ok {
$tree->traverse(sub {}, "Fail");
} qr/^Incorrect Object Type \: post traversal function is not a function/, '... this should die';
# passing second non-code-ref arg to traverse
throws_ok {
$tree->traverse(sub {}, $BAD_OBJECT);
} qr/^Incorrect Object Type \: post traversal function is not a function/, '... this should die';
# -----------------------------------------------
# exceptions for accept
# -----------------------------------------------
# passing no args to accept
throws_ok {
$tree->accept();
} qr/^Insufficient Arguments \: You must supply a valid Visitor object/, '... this should die';
# passing non-ref arg to accept
throws_ok {
$tree->accept("Fail");
} qr/^Insufficient Arguments \: You must supply a valid Visitor object/, '... this should die';
# passing non-object-ref arg to accept
throws_ok {
$tree->accept([]);
} qr/^Insufficient Arguments \: You must supply a valid Visitor object/, '... this should die';
# passing non-Tree::Simple::Visitor arg to accept
throws_ok {
$tree->accept($BAD_OBJECT);
} qr/^Insufficient Arguments \: You must supply a valid Visitor object/, '... this should die';
{
package TestPackage;
sub visit {}
}
# passing non-Tree::Simple::Visitor arg to accept
lives_ok {
$tree->accept(bless({}, "TestPackage"));
} '... but, this should live';
# -----------------------------------------------
# exceptions for _setParent
# -----------------------------------------------
# if no parent is given
throws_ok {
$tree->_setParent();
} qr/^Insufficient Arguments/, '... this should croak';
# if the parent that is given is not an object
throws_ok {
$tree->_setParent("Test");
} qr/^Insufficient Arguments/, '... this should croak';
# if the parent that is given is a ref but not an object
throws_ok {
$tree->_setParent([]);
} qr/^Insufficient Arguments/, '... this should croak';
# and if the parent that is given is an object but
# is not a Tree::Simple object
throws_ok {
$tree->_setParent($BAD_OBJECT);
} qr/^Insufficient Arguments/, '... this should croak';
# -----------------------------------------------
# exceptions for setUID
# -----------------------------------------------
throws_ok {
$tree->setUID();
} qr/^Insufficient Arguments/, '... this should croak';
## ----------------------------------------------------------------------------
## end Exception Tests for Tree::Simple
## ----------------------------------------------------------------------------
|