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::ToNestedHash'); 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::ToNestedHash", 'new'); 27 28{ 29 my $visitor = Tree::Simple::Visitor::ToNestedHash->new(); 30 isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedHash'); 31 isa_ok($visitor, 'Tree::Simple::Visitor'); 32 33 can_ok($visitor, 'visit'); 34 can_ok($visitor, 'getResults'); 35 36 $tree->accept($visitor); 37 is_deeply($visitor->getResults(), 38 { 'Child1' => { 'GrandChild1' => {}, 'GrandChild2' => {}}, 'Child2' => {}}, 39 '... got the whole tree'); 40} 41 42{ 43 my $visitor = Tree::Simple::Visitor::ToNestedHash->new(); 44 isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedHash'); 45 isa_ok($visitor, 'Tree::Simple::Visitor'); 46 47 can_ok($visitor, 'includeTrunk'); 48 can_ok($visitor, 'visit'); 49 can_ok($visitor, 'getResults'); 50 51 $visitor->includeTrunk(1); 52 $tree->accept($visitor); 53 is_deeply($visitor->getResults(), 54 { 'Root' => { 'Child1' => { 'GrandChild1' => {}, 'GrandChild2' => {}}, 'Child2' => {}}}, 55 '... got the whole tree'); 56} 57 58{ 59 my $visitor = Tree::Simple::Visitor::ToNestedHash->new(); 60 isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedHash'); 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 whole tree'); 75} 76 77{ 78 my $visitor = Tree::Simple::Visitor::ToNestedHash->new(); 79 isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedHash'); 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 $visitor->includeTrunk(1); 91 $tree->accept($visitor); 92 is_deeply($visitor->getResults(), 93 { 'ROOT' => { 'CHILD1' => { 'GRANDCHILD1' => {}, 'GRANDCHILD2' => {}}, 'CHILD2' => {}}}, 94 '... got the whole tree'); 95} 96 97{ 98 my $visitor = Tree::Simple::Visitor::ToNestedHash->new(); 99 isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedHash'); 100 isa_ok($visitor, 'Tree::Simple::Visitor'); 101 102 # check visit 103 throws_ok { 104 $visitor->visit(); 105 } qr/Insufficient Arguments/, '... got the error we expected'; 106 107 throws_ok { 108 $visitor->visit("Fail"); 109 } qr/Insufficient Arguments/, '... got the error we expected'; 110 111 throws_ok { 112 $visitor->visit([]); 113 } qr/Insufficient Arguments/, '... got the error we expected'; 114 115 throws_ok { 116 $visitor->visit(bless({}, "Fail")); 117 } qr/Insufficient Arguments/, '... got the error we expected'; 118} 119