1#!/usr/bin/perl 2 3use strict; 4use warnings; 5 6use Test::More tests => 52; 7use Test::Exception; 8 9## ---------------------------------------------------------------------------- 10## Exception Tests for Tree::Simple 11## ---------------------------------------------------------------------------- 12 13use Tree::Simple; 14 15my $BAD_OBJECT = bless({}, "Fail"); 16my $TEST_SUB_TREE = Tree::Simple->new("test"); 17 18# ----------------------------------------------- 19# exceptions for new 20# ----------------------------------------------- 21 22# not giving a proper argument for parent 23throws_ok { 24 Tree::Simple->new("test", 0); 25} qr/^Insufficient Arguments \:/, '... this should die'; 26 27# not giving a proper argument for parent 28throws_ok { 29 Tree::Simple->new("test", []); 30} qr/^Insufficient Arguments \:/, '... this should die'; 31 32# not giving a proper argument for parent 33throws_ok { 34 Tree::Simple->new("test", $BAD_OBJECT); 35} qr/^Insufficient Arguments \:/, '... this should die'; 36 37# ----------------------------------------------- 38 39my $tree = Tree::Simple->new(Tree::Simple->ROOT); 40 41# ----------------------------------------------- 42# exceptions for setNodeValue 43# ----------------------------------------------- 44 45# not giving an argument for setNodeValue 46throws_ok { 47 $tree->setNodeValue(); 48} qr/^Insufficient Arguments \: must supply a value for node/, '... this should die'; 49 50# ----------------------------------------------- 51# exceptions for addChild 52# ----------------------------------------------- 53 54# not giving an argument for addChild 55throws_ok { 56 $tree->addChild(); 57} qr/^Insufficient Arguments : no tree\(s\) to insert/, '... this should die'; 58 59# giving an bad argument for addChild 60throws_ok { 61 $tree->addChild("fail"); 62} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die'; 63 64# giving an bad argument for addChild 65throws_ok { 66 $tree->addChild([]); 67} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die'; 68 69 70# giving an bad object argument for addChild 71throws_ok { 72 $tree->addChild($BAD_OBJECT); 73} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die'; 74 75# ----------------------------------------------- 76# exceptions for insertChild 77# ----------------------------------------------- 78 79# giving no index argument for insertChild 80throws_ok { 81 $tree->insertChild(); 82} qr/^Insufficient Arguments \: Cannot insert child without index/, '... this should die'; 83 84# giving an out of bounds index argument for insertChild 85throws_ok { 86 $tree->insertChild(5); 87} qr/^Index Out of Bounds \: got \(5\) expected no more than \(0\)/, '... this should die'; 88 89# giving an good index argument but no tree argument for insertChild 90throws_ok { 91 $tree->insertChild(0); 92} qr/^Insufficient Arguments \: no tree\(s\) to insert/, '... this should die'; 93 94# giving an good index argument but an undefined tree argument for insertChild 95throws_ok { 96 $tree->insertChild(0, undef); 97} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die'; 98 99# giving an good index argument but a non-object tree argument for insertChild 100throws_ok { 101 $tree->insertChild(0, "Fail"); 102} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die'; 103 104# giving an good index argument but a non-object-ref tree argument for insertChild 105throws_ok { 106 $tree->insertChild(0, []); 107} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die'; 108 109 110# giving an good index argument but a bad object tree argument for insertChild 111throws_ok { 112 $tree->insertChild(0, $BAD_OBJECT); 113} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die'; 114 115# ----------------------------------------------- 116# exceptions for insertChildren 117# ----------------------------------------------- 118# NOTE: 119# even though insertChild and insertChildren are 120# implemented in the same function, it makes sense 121# to future-proof our tests by checking it anyway 122# this will help to save us the trouble later on 123 124# giving no index argument for insertChild 125throws_ok { 126 $tree->insertChildren(); 127} qr/^Insufficient Arguments \: Cannot insert child without index/, '... this should die'; 128 129# giving an out of bounds index argument for insertChild 130throws_ok { 131 $tree->insertChildren(5); 132} qr/^Index Out of Bounds \: got \(5\) expected no more than \(0\)/, '... this should die'; 133 134# giving an good index argument but no tree argument for insertChild 135throws_ok { 136 $tree->insertChildren(0); 137} qr/^Insufficient Arguments \: no tree\(s\) to insert/, '... this should die'; 138 139# giving an good index argument but an undefined tree argument for insertChild 140throws_ok { 141 $tree->insertChildren(0, undef); 142} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die'; 143 144# giving an good index argument but a non-object tree argument for insertChild 145throws_ok { 146 $tree->insertChildren(0, "Fail"); 147} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die'; 148 149# giving an good index argument but a non-object-ref tree argument for insertChild 150throws_ok { 151 $tree->insertChildren(0, []); 152} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die'; 153 154 155# giving an good index argument but a bad object tree argument for insertChild 156throws_ok { 157 $tree->insertChildren(0, $BAD_OBJECT); 158} qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die'; 159 160 161# ----------------------------------------------- 162# exceptions for removeChildAt 163# ----------------------------------------------- 164 165# giving no index argument for removeChildAt 166throws_ok { 167 $tree->removeChildAt(); 168} qr/^Insufficient Arguments \: Cannot remove child without index/, '... this should die'; 169 170# attempt to remove a child when there are none 171throws_ok { 172 $tree->removeChildAt(5); 173} qr/^Illegal Operation \: There are no children to remove/, '... this should die'; 174 175# add a child now 176$tree->addChild($TEST_SUB_TREE); 177 178# giving no index argument for removeChildAt 179throws_ok { 180 $tree->removeChildAt(5); 181} qr/^Index Out of Bounds \: got \(5\) expected no more than \(1\)/, '... this should die'; 182 183is($tree->removeChildAt(0), $TEST_SUB_TREE, '... these should be the same'); 184 185# ----------------------------------------------- 186# exceptions for removeChild 187# ----------------------------------------------- 188 189# giving no index argument for removeChild 190throws_ok { 191 $tree->removeChild(); 192} qr/^Insufficient Arguments \: /, '... this should die'; 193 194# giving bad ref argument 195throws_ok { 196 $tree->removeChild([]); 197} qr/^Insufficient Arguments \: /, '... this should die'; 198 199# giving bad object argument 200throws_ok { 201 $tree->removeChild($BAD_OBJECT); 202} qr/^Insufficient Arguments \: /, '... this should die'; 203 204# giving bad object argument 205throws_ok { 206 $tree->removeChild($TEST_SUB_TREE); 207} qr/^Child Not Found \: /, '... this should die'; 208 209# ----------------------------------------------- 210# exceptions for *Sibling methods 211# ----------------------------------------------- 212 213# attempting to add sibling to root trees 214throws_ok { 215 $tree->addSibling($TEST_SUB_TREE); 216} qr/^Insufficient Arguments \: cannot add a sibling to a ROOT tree/, '... this should die'; 217 218# attempting to add siblings to root trees 219throws_ok { 220 $tree->addSiblings($TEST_SUB_TREE); 221} qr/^Insufficient Arguments \: cannot add siblings to a ROOT tree/, '... this should die'; 222 223# attempting to insert sibling to root trees 224throws_ok { 225 $tree->insertSibling(0, $TEST_SUB_TREE); 226} qr/^Insufficient Arguments \: cannot insert sibling\(s\) to a ROOT tree/, '... this should die'; 227 228# attempting to insert sibling to root trees 229throws_ok { 230 $tree->insertSiblings(0, $TEST_SUB_TREE); 231} qr/^Insufficient Arguments \: cannot insert sibling\(s\) to a ROOT tree/, '... this should die'; 232 233# ----------------------------------------------- 234# exceptions for getChild 235# ----------------------------------------------- 236 237# not giving an index to the getChild method 238throws_ok { 239 $tree->getChild(); 240} qr/^Insufficient Arguments \: Cannot get child without index/, '... this should die'; 241 242# ----------------------------------------------- 243# exceptions for getSibling 244# ----------------------------------------------- 245 246# trying to get siblings of a root tree 247throws_ok { 248 $tree->getSibling(); 249} qr/^Insufficient Arguments \: cannot get siblings from a ROOT tree/, '... this should die'; 250 251# trying to get siblings of a root tree 252throws_ok { 253 $tree->getAllSiblings(); 254} qr/^Insufficient Arguments \: cannot get siblings from a ROOT tree/, '... this should die'; 255 256# ----------------------------------------------- 257# exceptions for traverse 258# ----------------------------------------------- 259 260# passing no args to traverse 261throws_ok { 262 $tree->traverse(); 263} qr/^Insufficient Arguments \: Cannot traverse without traversal function/, '... this should die'; 264 265# passing non-ref arg to traverse 266throws_ok { 267 $tree->traverse("Fail"); 268} qr/^Incorrect Object Type \: traversal function is not a function/, '... this should die'; 269 270# passing non-code-ref arg to traverse 271throws_ok { 272 $tree->traverse($BAD_OBJECT); 273} qr/^Incorrect Object Type \: traversal function is not a function/, '... this should die'; 274 275# passing second non-ref arg to traverse 276throws_ok { 277 $tree->traverse(sub {}, "Fail"); 278} qr/^Incorrect Object Type \: post traversal function is not a function/, '... this should die'; 279 280# passing second non-code-ref arg to traverse 281throws_ok { 282 $tree->traverse(sub {}, $BAD_OBJECT); 283} qr/^Incorrect Object Type \: post traversal function is not a function/, '... this should die'; 284 285 286# ----------------------------------------------- 287# exceptions for accept 288# ----------------------------------------------- 289 290# passing no args to accept 291throws_ok { 292 $tree->accept(); 293} qr/^Insufficient Arguments \: You must supply a valid Visitor object/, '... this should die'; 294 295# passing non-ref arg to accept 296throws_ok { 297 $tree->accept("Fail"); 298} qr/^Insufficient Arguments \: You must supply a valid Visitor object/, '... this should die'; 299 300# passing non-object-ref arg to accept 301throws_ok { 302 $tree->accept([]); 303} qr/^Insufficient Arguments \: You must supply a valid Visitor object/, '... this should die'; 304 305# passing non-Tree::Simple::Visitor arg to accept 306throws_ok { 307 $tree->accept($BAD_OBJECT); 308} qr/^Insufficient Arguments \: You must supply a valid Visitor object/, '... this should die'; 309 310{ 311 package TestPackage; 312 sub visit {} 313} 314 315# passing non-Tree::Simple::Visitor arg to accept 316lives_ok { 317 $tree->accept(bless({}, "TestPackage")); 318} '... but, this should live'; 319 320# ----------------------------------------------- 321# exceptions for _setParent 322# ----------------------------------------------- 323 324# if no parent is given 325throws_ok { 326 $tree->_setParent(); 327} qr/^Insufficient Arguments/, '... this should croak'; 328 329# if the parent that is given is not an object 330throws_ok { 331 $tree->_setParent("Test"); 332} qr/^Insufficient Arguments/, '... this should croak'; 333 334# if the parent that is given is a ref but not an object 335throws_ok { 336 $tree->_setParent([]); 337} qr/^Insufficient Arguments/, '... this should croak'; 338 339# and if the parent that is given is an object but 340# is not a Tree::Simple object 341throws_ok { 342 $tree->_setParent($BAD_OBJECT); 343} qr/^Insufficient Arguments/, '... this should croak'; 344 345# ----------------------------------------------- 346# exceptions for setUID 347# ----------------------------------------------- 348 349throws_ok { 350 $tree->setUID(); 351} qr/^Insufficient Arguments/, '... this should croak'; 352 353## ---------------------------------------------------------------------------- 354## end Exception Tests for Tree::Simple 355## ---------------------------------------------------------------------------- 356