1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 58;
7use Test::Exception;
8
9BEGIN { 
10    use_ok('Tree::Simple::Visitor::FromNestedArray');
11}
12
13use Tree::Simple;
14
15my $array_tree = [ 
16            'Root', [ 
17                'Child1', [
18                        'GrandChild1',
19                        'GrandChild2'
20                        ],
21                'Child2'
22                ]
23            ];
24
25
26can_ok("Tree::Simple::Visitor::FromNestedArray", 'new');
27
28{ # check normal behavior
29    my $visitor = Tree::Simple::Visitor::FromNestedArray->new();
30    isa_ok($visitor, 'Tree::Simple::Visitor::FromNestedArray');
31    isa_ok($visitor, 'Tree::Simple::Visitor');
32    
33    can_ok($visitor, 'setArrayTree');
34    $visitor->setArrayTree($array_tree);
35    
36    can_ok($visitor, 'visit');
37    
38    my $tree = Tree::Simple->new(Tree::Simple->ROOT);
39    $tree->accept($visitor);
40    
41    my $root = $tree->getChild(0);
42    is($root->getNodeValue(), 'Root', '... got the value we expected from Root');
43    cmp_ok($root->getChildCount(), '==', 2, '... Root has 2 children');
44    
45        my ($child1, $child2) = $root->getAllChildren();
46        is($child1->getNodeValue(), 'Child1', '... got the value we expected from Child1');
47        cmp_ok($child1->getChildCount(), '==', 2, '... Child1 has 2 children');
48        
49            my ($grandchild1, $grandchild2) = $child1->getAllChildren();
50            is($grandchild1->getNodeValue(), 'GrandChild1', '... got the value we expected from GrandChild1');
51            ok($grandchild1->isLeaf(), '... GrandChild1 is a leaf node');
52            
53            is($grandchild2->getNodeValue(), 'GrandChild2', '... got the value we expected from GrandChild2');
54            ok($grandchild2->isLeaf(), '... GrandChild2 is a leaf node');
55        
56        is($child2->getNodeValue(), 'Child2', '... got the value we expected from Child2');
57        ok($child2->isLeaf(), '... Child2 is a leaf node');	    
58}
59
60{ # check includeTrunk behavior
61    my $visitor = Tree::Simple::Visitor::FromNestedArray->new();
62    isa_ok($visitor, 'Tree::Simple::Visitor::FromNestedArray');
63    isa_ok($visitor, 'Tree::Simple::Visitor');
64    
65    can_ok($visitor, 'setArrayTree');
66    $visitor->setArrayTree($array_tree);
67
68    can_ok($visitor, 'includeTrunk');
69    $visitor->includeTrunk(1);
70    
71    can_ok($visitor, 'visit');
72    
73    my $tree = Tree::Simple->new(Tree::Simple->ROOT);
74    $tree->accept($visitor);
75    
76    my $root = $tree;
77    is($root->getNodeValue(), 'Root', '... got the value we expected from Root');
78    cmp_ok($root->getChildCount(), '==', 2, '... Root has 2 children');
79    
80        my ($child1, $child2) = $root->getAllChildren();
81        is($child1->getNodeValue(), 'Child1', '... got the value we expected from Child1');
82        cmp_ok($child1->getChildCount(), '==', 2, '... Child1 has 2 children');
83        
84            my ($grandchild1, $grandchild2) = $child1->getAllChildren();
85            is($grandchild1->getNodeValue(), 'GrandChild1', '... got the value we expected from GrandChild1');
86            ok($grandchild1->isLeaf(), '... GrandChild1 is a leaf node');
87            
88            is($grandchild2->getNodeValue(), 'GrandChild2', '... got the value we expected from GrandChild2');
89            ok($grandchild2->isLeaf(), '... GrandChild2 is a leaf node');
90        
91        is($child2->getNodeValue(), 'Child2', '... got the value we expected from Child2');
92        ok($child2->isLeaf(), '... Child2 is a leaf node');	
93}
94
95{ # check nodeFilter behavior
96    my $visitor = Tree::Simple::Visitor::FromNestedArray->new();
97    isa_ok($visitor, 'Tree::Simple::Visitor::FromNestedArray');
98    isa_ok($visitor, 'Tree::Simple::Visitor');
99    
100    can_ok($visitor, 'setArrayTree');
101    $visitor->setArrayTree($array_tree);
102
103    can_ok($visitor, 'setNodeFilter');
104    $visitor->setNodeFilter(sub {
105        my ($node) = @_;
106        return uc($node);
107    });
108    
109    can_ok($visitor, 'visit');
110    
111    my $tree = Tree::Simple->new(Tree::Simple->ROOT);
112    $tree->accept($visitor);
113    
114    my $root = $tree->getChild(0);
115    is($root->getNodeValue(), 'ROOT', '... got the value we expected from Root');
116    cmp_ok($root->getChildCount(), '==', 2, '... Root has 2 children');
117    
118        my ($child1, $child2) = $root->getAllChildren();
119        is($child1->getNodeValue(), 'CHILD1', '... got the value we expected from Child1');
120        cmp_ok($child1->getChildCount(), '==', 2, '... Child1 has 2 children');
121        
122            my ($grandchild1, $grandchild2) = $child1->getAllChildren();
123            is($grandchild1->getNodeValue(), 'GRANDCHILD1', '... got the value we expected from GrandChild1');
124            ok($grandchild1->isLeaf(), '... GrandChild1 is a leaf node');
125            
126            is($grandchild2->getNodeValue(), 'GRANDCHILD2', '... got the value we expected from GrandChild2');
127            ok($grandchild2->isLeaf(), '... GrandChild2 is a leaf node');
128        
129        is($child2->getNodeValue(), 'CHILD2', '... got the value we expected from Child2');
130        ok($child2->isLeaf(), '... Child2 is a leaf node');
131}
132
133{
134    my $visitor = Tree::Simple::Visitor::FromNestedArray->new();
135    isa_ok($visitor, 'Tree::Simple::Visitor::FromNestedArray');
136    isa_ok($visitor, 'Tree::Simple::Visitor');
137    
138    # check visit
139    throws_ok {
140        $visitor->visit();
141    } qr/Insufficient Arguments/, '... got the error we expected';  
142    
143    throws_ok {
144        $visitor->visit("Fail");
145    } qr/Insufficient Arguments/, '... got the error we expected';                           
146
147    throws_ok {
148        $visitor->visit([]);
149    } qr/Insufficient Arguments/, '... got the error we expected'; 
150    
151    throws_ok {
152        $visitor->visit(bless({}, "Fail"));
153    } qr/Insufficient Arguments/, '... got the error we expected'; 
154    
155    # check setHashTree
156    throws_ok {
157        $visitor->setArrayTree();
158    } qr/Insufficient Arguments/, '... got the error we expected'; 
159    
160    throws_ok {
161        $visitor->setArrayTree("Fail");
162    } qr/Insufficient Arguments/, '... got the error we expected'; 
163    
164    throws_ok {
165        $visitor->setArrayTree([]);
166    } qr/Insufficient Arguments/, '... got the error we expected';     
167
168    throws_ok {
169        $visitor->setArrayTree([[]]);
170    } qr/Incorrect Object Type/, '... got the error we expected';
171
172    throws_ok {
173        $visitor->setArrayTree(['root', 'Fail']);
174    } qr/Incorrect Object Type/, '... got the error we expected';
175
176    $visitor->setArrayTree(['root', [[]]]);
177    
178    throws_ok {
179        $visitor->visit(Tree::Simple->new(Tree::Simple->ROOT));
180    } qr/Incorrect Object Type/, '... got the error we expected';    
181
182}
183