1#!/usr/bin/perl 2 3use strict; 4use warnings; 5 6use Test::More tests => 33; 7use Test::Exception; 8 9BEGIN { 10 use_ok('Tree::Simple::Visitor::ToNestedArray'); 11} 12 13use Tree::Simple; 14 15my $tree = Tree::Simple->new("Root") 16 ->addChildren( 17 Tree::Simple->new("Child1") 18 ->addChildren( 19 Tree::Simple->new("GrandChild1"), 20 Tree::Simple->new("GrandChild2") 21 ), 22 Tree::Simple->new("Child2"), 23 ); 24isa_ok($tree, 'Tree::Simple'); 25 26can_ok("Tree::Simple::Visitor::ToNestedArray", 'new'); 27 28{ 29 my $visitor = Tree::Simple::Visitor::ToNestedArray->new(); 30 isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedArray'); 31 isa_ok($visitor, 'Tree::Simple::Visitor'); 32 33 can_ok($visitor, 'includeTrunk'); 34 can_ok($visitor, 'visit'); 35 can_ok($visitor, 'getResults'); 36 37 $visitor->includeTrunk(1); 38 $tree->accept($visitor); 39 is_deeply($visitor->getResults(), 40 [ 'Root', [ 'Child1', [ 'GrandChild1', 'GrandChild2' ], 'Child2' ]], 41 '... got the whole tree'); 42} 43 44{ 45 my $visitor = Tree::Simple::Visitor::ToNestedArray->new(); 46 isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedArray'); 47 isa_ok($visitor, 'Tree::Simple::Visitor'); 48 49 can_ok($visitor, 'visit'); 50 can_ok($visitor, 'getResults'); 51 52 $tree->accept($visitor); 53 is_deeply($visitor->getResults(), 54 [ 'Child1', [ 'GrandChild1', 'GrandChild2' ], 'Child2' ], 55 '... got the tree minus the root'); 56} 57 58{ 59 my $visitor = Tree::Simple::Visitor::ToNestedArray->new(); 60 isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedArray'); 61 isa_ok($visitor, 'Tree::Simple::Visitor'); 62 63 can_ok($visitor, 'visit'); 64 can_ok($visitor, 'getResults'); 65 can_ok($visitor, 'setNodeFilter'); 66 67 $visitor->setNodeFilter(sub { 68 return uc($_[0]->getNodeValue()); 69 }); 70 71 $tree->accept($visitor); 72 is_deeply($visitor->getResults(), 73 [ 'CHILD1', [ 'GRANDCHILD1', 'GRANDCHILD2' ], 'CHILD2' ], 74 '... got the tree minus the root and uppercased'); 75} 76 77{ 78 my $visitor = Tree::Simple::Visitor::ToNestedArray->new(); 79 isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedArray'); 80 isa_ok($visitor, 'Tree::Simple::Visitor'); 81 82 can_ok($visitor, 'includeTrunk'); 83 can_ok($visitor, 'visit'); 84 can_ok($visitor, 'getResults'); 85 can_ok($visitor, 'setNodeFilter'); 86 87 $visitor->setNodeFilter(sub { 88 return uc($_[0]->getNodeValue()); 89 }); 90 91 $visitor->includeTrunk(1); 92 $tree->accept($visitor); 93 is_deeply($visitor->getResults(), 94 [ 'ROOT', [ 'CHILD1', [ 'GRANDCHILD1', 'GRANDCHILD2' ], 'CHILD2' ]], 95 '... got the tree minus the root and uppercased'); 96} 97 98{ 99 my $visitor = Tree::Simple::Visitor::ToNestedArray->new(); 100 isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedArray'); 101 isa_ok($visitor, 'Tree::Simple::Visitor'); 102 103 # check visit 104 throws_ok { 105 $visitor->visit(); 106 } qr/Insufficient Arguments/, '... got the error we expected'; 107 108 throws_ok { 109 $visitor->visit("Fail"); 110 } qr/Insufficient Arguments/, '... got the error we expected'; 111 112 throws_ok { 113 $visitor->visit([]); 114 } qr/Insufficient Arguments/, '... got the error we expected'; 115 116 throws_ok { 117 $visitor->visit(bless({}, "Fail")); 118 } qr/Insufficient Arguments/, '... got the error we expected'; 119}