1#!/usr/bin/perl 2 3use strict; 4use warnings; 5 6use Test::More tests => 15; 7use Test::Exception; 8 9BEGIN { 10 use_ok('Tree::Simple::Visitor::PostOrderTraversal'); 11} 12 13use Tree::Simple; 14 15my $tree = Tree::Simple->new(Tree::Simple->ROOT) 16 ->addChildren( 17 Tree::Simple->new("1") 18 ->addChildren( 19 Tree::Simple->new("1.1"), 20 Tree::Simple->new("1.2") 21 ->addChildren( 22 Tree::Simple->new("1.2.1"), 23 Tree::Simple->new("1.2.2") 24 ), 25 Tree::Simple->new("1.3") 26 ), 27 Tree::Simple->new("2") 28 ->addChildren( 29 Tree::Simple->new("2.1"), 30 Tree::Simple->new("2.2") 31 ), 32 Tree::Simple->new("3") 33 ->addChildren( 34 Tree::Simple->new("3.1"), 35 Tree::Simple->new("3.2"), 36 Tree::Simple->new("3.3") 37 ), 38 Tree::Simple->new("4") 39 ->addChildren( 40 Tree::Simple->new("4.1") 41 ) 42 ); 43isa_ok($tree, 'Tree::Simple'); 44 45can_ok("Tree::Simple::Visitor::PostOrderTraversal", 'new'); 46 47my $visitor = Tree::Simple::Visitor::PostOrderTraversal->new(); 48isa_ok($visitor, 'Tree::Simple::Visitor::PostOrderTraversal'); 49isa_ok($visitor, 'Tree::Simple::Visitor'); 50 51can_ok($visitor, 'visit'); 52can_ok($visitor, 'getResults'); 53 54$tree->accept($visitor); 55is_deeply( 56 [ $visitor->getResults() ], 57 [ qw(1.1 1.2.1 1.2.2 1.2 1.3 1 2.1 2.2 2 3.1 3.2 3.3 3 4.1 4) ], 58 '... our results are as expected'); 59 60can_ok($visitor, 'setNodeFilter'); 61$visitor->setNodeFilter(sub { "Tree_" . $_[0]->getNodeValue() }); 62 63can_ok($visitor, 'includeTrunk'); 64$visitor->includeTrunk(1); 65 66$tree->accept($visitor); 67is_deeply( 68 [ $visitor->getResults() ], 69 [ qw(Tree_1.1 Tree_1.2.1 Tree_1.2.2 Tree_1.2 Tree_1.3 Tree_1 70 Tree_2.1 Tree_2.2 Tree_2 Tree_3.1 Tree_3.2 Tree_3.3 71 Tree_3 Tree_4.1 Tree_4 Tree_root) ], 72 '... our results are as expected'); 73 74# test some error conditions 75 76throws_ok { 77 $visitor->visit(); 78} qr/Insufficient Arguments/, '... this should die'; 79 80throws_ok { 81 $visitor->visit("Fail"); 82} qr/Insufficient Arguments/, '... this should die'; 83 84throws_ok { 85 $visitor->visit([]); 86} qr/Insufficient Arguments/, '... this should die'; 87 88throws_ok { 89 $visitor->visit(bless({}, "Fail")); 90} qr/Insufficient Arguments/, '... this should die';