1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 23;
7use Test::Exception;
8
9BEGIN { 
10    use_ok('Tree::Simple::Visitor::PathToRoot');
11}
12
13use Tree::Simple;
14
15my $very_deep = Tree::Simple->new("1.2.2.1");
16isa_ok($very_deep, 'Tree::Simple');
17
18my $kind_of_deep = Tree::Simple->new("2.2.1");
19isa_ok($kind_of_deep, 'Tree::Simple');
20
21my $tree = Tree::Simple->new(Tree::Simple->ROOT)
22                       ->addChildren(
23                            Tree::Simple->new("1")
24                                        ->addChildren(
25                                            Tree::Simple->new("1.1"),
26                                            Tree::Simple->new("1.2")
27                                                        ->addChildren(
28                                                            Tree::Simple->new("1.2.1"),
29                                                            Tree::Simple->new("1.2.2")
30                                                                        ->addChild($very_deep)
31                                                        ),
32                                            Tree::Simple->new("1.3")                                                                                                
33                                        ),
34                            Tree::Simple->new("2")
35                                        ->addChildren(
36                                            Tree::Simple->new("2.1"),
37                                            Tree::Simple->new("2.2")
38                                                        ->addChild($kind_of_deep)
39                                        ),                            
40                            Tree::Simple->new("3")
41                                        ->addChildren(
42                                            Tree::Simple->new("3.1"),
43                                            Tree::Simple->new("3.2"),
44                                            Tree::Simple->new("3.3")                                                                                                
45                                        ),                            
46                            Tree::Simple->new("4")                                                        
47                                        ->addChildren(
48                                            Tree::Simple->new("4.1")
49                                        )                            
50                       );
51isa_ok($tree, 'Tree::Simple');
52
53can_ok("Tree::Simple::Visitor::PathToRoot", 'new');
54
55my $visitor = Tree::Simple::Visitor::PathToRoot->new();
56isa_ok($visitor, 'Tree::Simple::Visitor::PathToRoot');
57isa_ok($visitor, 'Tree::Simple::Visitor');
58
59can_ok($visitor, 'visit');
60can_ok($visitor, 'getPathAsString');
61can_ok($visitor, 'getPath');
62
63$kind_of_deep->accept($visitor);
64
65is($visitor->getPathAsString("/"), "2/2.2/2.2.1", '... our paths match');
66is_deeply(
67    [ $visitor->getPath() ], 
68    [ qw/2 2.2 2.2.1/ ], 
69    '... our paths match');
70
71can_ok($visitor, 'setNodeFilter');
72$visitor->setNodeFilter(sub { "~" . $_[0]->getNodeValue() . "~" });
73
74$visitor->includeTrunk(1);
75
76$very_deep->accept($visitor);
77
78is($visitor->getPathAsString(), "~root~, ~1~, ~1.2~, ~1.2.2~, ~1.2.2.1~", '... our paths match again');
79is_deeply(
80    [ $visitor->getPath() ], 
81    [ qw/~root~ ~1~ ~1.2~ ~1.2.2~ ~1.2.2.1~/ ], 
82    '... our paths match again');
83
84$visitor->includeTrunk(0);
85
86$tree->accept($visitor);
87is($visitor->getPathAsString("|"), "", '... we got nothing');
88is_deeply(
89    scalar $visitor->getPath(), 
90    [ ], 
91    '... no path means no results');
92
93$visitor->includeTrunk(1);
94
95$tree->accept($visitor);
96is($visitor->getPathAsString(), "~root~", '... we got nothing');
97is_deeply(
98    scalar $visitor->getPath(), 
99    [ "~root~" ], 
100    '... but include root and we have something at least');
101
102# test some error conditions
103
104throws_ok {
105    $visitor->visit();
106} qr/Insufficient Arguments/, '... this should die';
107
108throws_ok {
109    $visitor->visit("Fail");
110} qr/Insufficient Arguments/, '... this should die';
111
112throws_ok {
113    $visitor->visit([]);
114} qr/Insufficient Arguments/, '... this should die';
115
116throws_ok {
117    $visitor->visit(bless({}, "Fail"));
118} qr/Insufficient Arguments/, '... this should die';
119