File: 92_Tree_Simple_Visitor_ToNestedHash_test.t

package info (click to toggle)
libtree-simple-visitorfactory-perl 0.16-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 412 kB
  • sloc: perl: 3,167; makefile: 2
file content (118 lines) | stat: -rw-r--r-- 3,679 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 33;
use Test::Exception;

BEGIN { 
    use_ok('Tree::Simple::Visitor::ToNestedHash');
}

use Tree::Simple;

my $tree = Tree::Simple->new("Root")
                ->addChildren(
                    Tree::Simple->new("Child1")
                        ->addChildren(
                            Tree::Simple->new("GrandChild1"),                
                            Tree::Simple->new("GrandChild2")
                        ),
                    Tree::Simple->new("Child2"),
                );
isa_ok($tree, 'Tree::Simple');

can_ok("Tree::Simple::Visitor::ToNestedHash", 'new');
    
{
    my $visitor = Tree::Simple::Visitor::ToNestedHash->new();
    isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedHash');
    isa_ok($visitor, 'Tree::Simple::Visitor');
    
    can_ok($visitor, 'visit');
    can_ok($visitor, 'getResults');    
        
    $tree->accept($visitor);
    is_deeply($visitor->getResults(),
            { 'Child1' => { 'GrandChild1' => {}, 'GrandChild2' => {}}, 'Child2' => {}},
            '... got the whole tree');
}

{
    my $visitor = Tree::Simple::Visitor::ToNestedHash->new();
    isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedHash');
    isa_ok($visitor, 'Tree::Simple::Visitor');
    
    can_ok($visitor, 'includeTrunk');
    can_ok($visitor, 'visit');
    can_ok($visitor, 'getResults');    
        
    $visitor->includeTrunk(1);
    $tree->accept($visitor);
    is_deeply($visitor->getResults(),
            { 'Root' => { 'Child1' => { 'GrandChild1' => {}, 'GrandChild2' => {}}, 'Child2' => {}}},
            '... got the whole tree');
}

{
    my $visitor = Tree::Simple::Visitor::ToNestedHash->new();
    isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedHash');
    isa_ok($visitor, 'Tree::Simple::Visitor');
    
    can_ok($visitor, 'visit');
    can_ok($visitor, 'getResults');    
    can_ok($visitor, 'setNodeFilter');                                    
    
    $visitor->setNodeFilter(sub {
        return uc($_[0]->getNodeValue());
    });    
        
    $tree->accept($visitor);
    is_deeply($visitor->getResults(),
            { 'CHILD1' => { 'GRANDCHILD1' => {}, 'GRANDCHILD2' => {}}, 'CHILD2' => {}},
            '... got the whole tree');
}

{
    my $visitor = Tree::Simple::Visitor::ToNestedHash->new();
    isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedHash');
    isa_ok($visitor, 'Tree::Simple::Visitor');
    
    can_ok($visitor, 'includeTrunk');    
    can_ok($visitor, 'visit');
    can_ok($visitor, 'getResults');    
    can_ok($visitor, 'setNodeFilter');                                    
    
    $visitor->setNodeFilter(sub {
        return uc($_[0]->getNodeValue());
    });    
    $visitor->includeTrunk(1);        
    $tree->accept($visitor);
    is_deeply($visitor->getResults(),
            { 'ROOT' => { 'CHILD1' => { 'GRANDCHILD1' => {}, 'GRANDCHILD2' => {}}, 'CHILD2' => {}}},
            '... got the whole tree');
}

{
    my $visitor = Tree::Simple::Visitor::ToNestedHash->new();
    isa_ok($visitor, 'Tree::Simple::Visitor::ToNestedHash');
    isa_ok($visitor, 'Tree::Simple::Visitor');
    
    # check visit
    throws_ok {
        $visitor->visit();
    } qr/Insufficient Arguments/, '... got the error we expected';  
    
    throws_ok {
        $visitor->visit("Fail");
    } qr/Insufficient Arguments/, '... got the error we expected';                           

    throws_ok {
        $visitor->visit([]);
    } qr/Insufficient Arguments/, '... got the error we expected'; 
    
    throws_ok {
        $visitor->visit(bless({}, "Fail"));
    } qr/Insufficient Arguments/, '... got the error we expected'; 
}