File: 35_Tree_Simple_Visitor_FindByUID_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 (236 lines) | stat: -rw-r--r-- 8,149 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/perl

use strict;
use warnings;

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

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

use Tree::Simple;

my $first_search = Tree::Simple->new("1.2.2");
isa_ok($first_search, 'Tree::Simple');

my $first_search_UID = $first_search->getUID();

my $second_search = Tree::Simple->new("3.2.1");
isa_ok($second_search, 'Tree::Simple');

my $second_search_UID = $second_search->getUID();

my $tree = Tree::Simple->new(Tree::Simple->ROOT)
                       ->addChildren(
                            Tree::Simple->new("1")
                                        ->addChildren(
                                            Tree::Simple->new("1.1"),
                                            Tree::Simple->new("1.2")
                                                        ->addChildren(
                                                            Tree::Simple->new("1.2.1"),
                                                            $first_search
                                                        ),
                                            Tree::Simple->new("1.3")                                                                                                
                                        ),
                            Tree::Simple->new("2")
                                        ->addChildren(
                                            Tree::Simple->new("2.1"),
                                            Tree::Simple->new("2.2")
                                        ),                            
                            Tree::Simple->new("3")
                                        ->addChildren(
                                            Tree::Simple->new("3.1"),
                                            Tree::Simple->new("3.2")->addChild($second_search),
                                            Tree::Simple->new("3.3")                                                                                                
                                        ),                            
                            Tree::Simple->new("4")                                                        
                                        ->addChildren(
                                            Tree::Simple->new("4.1")
                                        )                            
                       );
isa_ok($tree, 'Tree::Simple');

can_ok("Tree::Simple::Visitor::FindByUID", 'new');

# check the normal behavior
{
    my $visitor = Tree::Simple::Visitor::FindByUID->new();
    isa_ok($visitor, 'Tree::Simple::Visitor::FindByUID');
    isa_ok($visitor, 'Tree::Simple::Visitor');
    
    can_ok($visitor, 'searchForUID');
    can_ok($visitor, 'visit');
    can_ok($visitor, 'getResult');
    
    $visitor->searchForUID($first_search_UID);
    $tree->accept($visitor);
    
    my $match = $visitor->getResult();
    ok(defined($match), '... we got a result');
    is($match, $first_search, '... and it is our first search tree');
}

# test the node filter and make it fail
{
    my $visitor = Tree::Simple::Visitor::FindByUID->new();
    isa_ok($visitor, 'Tree::Simple::Visitor::FindByUID');
    isa_ok($visitor, 'Tree::Simple::Visitor');
    
    $visitor->searchForUID($first_search_UID);
    # make our search fail
    $visitor->setNodeFilter(sub {
        my ($tree) = @_;
        return $tree->getNodeValue() ne "1.2.2";
    });
    
    $tree->accept($visitor);
    
    my $match = $visitor->getResult();
    ok(!defined($match), '... match failed as expected');
}

# test the second match
{
    my $visitor = Tree::Simple::Visitor::FindByUID->new();
    isa_ok($visitor, 'Tree::Simple::Visitor::FindByUID');
    isa_ok($visitor, 'Tree::Simple::Visitor');
    
    $visitor->searchForUID($second_search_UID);
    # make our search succed
    $visitor->setNodeFilter(sub {
        my ($tree) = @_;
        return $tree->getNodeValue() eq "3.2.1";
    });
    
    $tree->accept($visitor);
    
    my $match = $visitor->getResult();
    ok(defined($match), '... match succedded as expected');
    is($match, $second_search, '... and it is our second search tree');    
}

# check the normal behavior with includeTrunk
{
    my $visitor = Tree::Simple::Visitor::FindByUID->new();
    isa_ok($visitor, 'Tree::Simple::Visitor::FindByUID');
    isa_ok($visitor, 'Tree::Simple::Visitor');
    
    can_ok($visitor, 'includeTrunk');
    $visitor->includeTrunk(1);
    
    $visitor->searchForUID($tree->getUID());
    $tree->accept($visitor);
    
    my $match = $visitor->getResult();
    ok(defined($match), '... we got a result');
    is($match, $tree, '... and it is our base tree');
}

# check the traversal method behavior
{
    my $visitor = Tree::Simple::Visitor::FindByUID->new();
    isa_ok($visitor, 'Tree::Simple::Visitor::FindByUID');
    isa_ok($visitor, 'Tree::Simple::Visitor');
    
    can_ok($visitor, 'setTraversalMethod');
    $visitor->setTraversalMethod(Tree::Simple::Visitor::BreadthFirstTraversal->new());    
    
    $visitor->searchForUID($first_search_UID);
    $tree->accept($visitor);
    
    my $match = $visitor->getResult();
    ok(defined($match), '... we got a result');
    is($match, $first_search, '... and it is our first search tree');
}

# check the traversal method behavior with includeTrunk
{
    my $visitor = Tree::Simple::Visitor::FindByUID->new();
    isa_ok($visitor, 'Tree::Simple::Visitor::FindByUID');
    isa_ok($visitor, 'Tree::Simple::Visitor');
    
    $visitor->setTraversalMethod(Tree::Simple::Visitor::BreadthFirstTraversal->new());    
    $visitor->includeTrunk(1);
    $visitor->searchForUID($tree->getUID());
    
    $tree->accept($visitor);
    
    my $match = $visitor->getResult();
    ok(defined($match), '... we got a result');
    is($match, $tree, '... and it is our base tree');
}


# check errors
{
    my $visitor = Tree::Simple::Visitor::FindByUID->new();
    isa_ok($visitor, 'Tree::Simple::Visitor::FindByUID');
    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'; 
    
    # check UID
    throws_ok {
        $visitor->searchForUID();
    } qr/Insufficient Arguments/, '... got the error we expected';      
    
    # try to visit without a UID
    throws_ok {
        $visitor->visit($tree);
    } qr/Illegal Operation/, '... got the error we expected';     
    
    # check setTraversalMethod
    throws_ok {
        $visitor->setTraversalMethod();
    } qr/Insufficient Arguments/, '... got the error we expected';  
    
    throws_ok {
        $visitor->setTraversalMethod("Fail");
    } qr/Insufficient Arguments/, '... got the error we expected';                           

    throws_ok {
        $visitor->setTraversalMethod([]);
    } qr/Insufficient Arguments/, '... got the error we expected'; 
    
    throws_ok {
        $visitor->setTraversalMethod(bless({}, "Fail"));
    } qr/Insufficient Arguments/, '... got the error we expected';  
    
    throws_ok {
        $visitor->searchForUID();
    } qr/Insufficient Arguments/, '... got the error we expected';    
    
    # test some edge cases
    $visitor->searchForUID($first_search_UID);
    
    $visitor->setNodeFilter(sub { die "Nothing really" });
    throws_ok {
        $visitor->visit($tree);
    } qr/Nothing really/, '... got the error we expected';  
    
    $visitor->setNodeFilter(sub { die bless({}, "NothingReally") });
    throws_ok {
        $visitor->visit($tree);
    } "NothingReally", '... got the error we expected';                    
        
}