1#!/usr/bin/perl 2 3use strict; 4use warnings; 5 6use Test::More tests => 22; 7use Test::Exception; 8 9BEGIN { 10 use_ok('Tree::Simple::Visitor::GetAllDescendents'); 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::GetAllDescendents", 'new'); 46 47my $visitor = Tree::Simple::Visitor::GetAllDescendents->new(); 48isa_ok($visitor, 'Tree::Simple::Visitor::GetAllDescendents'); 49isa_ok($visitor, 'Tree::Simple::Visitor'); 50 51can_ok($visitor, 'visit'); 52can_ok($visitor, 'getAllDescendents'); 53 54can_ok($visitor, 'setTraversalMethod'); 55can_ok($visitor, 'setNodeFilter'); 56 57$tree->accept($visitor); 58 59is_deeply( 60 [ $visitor->getAllDescendents() ], 61 [ qw/1 1.1 1.2 1.2.1 1.2.2 1.3 2 2.1 2.2 3 3.1 3.2 3.3 4 4.1/ ], 62 '... our descendents match'); 63 64can_ok($visitor, 'setNodeFilter'); 65$visitor->setNodeFilter(sub { "*" . $_[0]->getNodeValue() }); 66 67$tree->accept($visitor); 68 69is_deeply( 70 [ $visitor->getAllDescendents() ], 71 [ qw/*1 *1.1 *1.2 *1.2.1 *1.2.2 *1.3 *2 *2.1 *2.2 *3 *3.1 *3.2 *3.3 *4 *4.1/ ], 72 '... our paths descendents again'); 73 74use_ok('Tree::Simple::Visitor::BreadthFirstTraversal'); 75 76$visitor->setNodeFilter(sub { $_[0]->getNodeValue() }); 77$visitor->setTraversalMethod(Tree::Simple::Visitor::BreadthFirstTraversal->new()); 78$tree->accept($visitor); 79 80is_deeply( 81 [ $visitor->getAllDescendents() ], 82 [ qw/1 2 3 4 1.1 1.2 1.3 2.1 2.2 3.1 3.2 3.3 4.1 1.2.1 1.2.2/ ], 83 '... our bredth-first descendents match'); 84 85# test some error conditions 86 87throws_ok { 88 $visitor->visit(); 89} qr/Insufficient Arguments/, '... this should die'; 90 91throws_ok { 92 $visitor->visit("Fail"); 93} qr/Insufficient Arguments/, '... this should die'; 94 95throws_ok { 96 $visitor->visit([]); 97} qr/Insufficient Arguments/, '... this should die'; 98 99throws_ok { 100 $visitor->visit(bless({}, "Fail")); 101} qr/Insufficient Arguments/, '... this should die'; 102 103throws_ok { 104 $visitor->setTraversalMethod(); 105} qr/Insufficient Arguments/, '... this should die'; 106 107throws_ok { 108 $visitor->setTraversalMethod("Fail"); 109} qr/Insufficient Arguments/, '... this should die'; 110 111throws_ok { 112 $visitor->setTraversalMethod([]); 113} qr/Insufficient Arguments/, '... this should die'; 114 115throws_ok { 116 $visitor->setTraversalMethod(bless({}, "Fail")); 117} qr/Insufficient Arguments/, '... this should die'; 118