1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 57;
7use Test::Exception;
8
9BEGIN { 
10    use_ok('Tree::Simple::Visitor::CreateDirectoryTree');
11}
12
13use Tree::Simple;
14use File::Spec;
15
16my $dir_tree = Tree::Simple->new("test/")
17                    ->addChildren(
18                            Tree::Simple->new("sub_test/"),
19                            Tree::Simple->new("test.pm"),
20                            Tree::Simple->new("sub_test2\\")
21                                ->addChildren(
22                                    Tree::Simple->new("sub_sub_test/"),
23                                    Tree::Simple->new("sub_test.pm"),
24                                    Tree::Simple->new("sub_sub_sub_test\\")
25                                        ->addChildren(
26                                            Tree::Simple->new("sub_sub_sub_test.pm")
27                                        )
28                                )
29                    );
30isa_ok($dir_tree, 'Tree::Simple');                    
31
32can_ok("Tree::Simple::Visitor::CreateDirectoryTree", 'new');
33
34# test the default behavior
35{
36    my $visitor = Tree::Simple::Visitor::CreateDirectoryTree->new();
37    isa_ok($visitor, 'Tree::Simple::Visitor::CreateDirectoryTree');
38    isa_ok($visitor, 'Tree::Simple::Visitor');
39    
40    can_ok($visitor, 'visit');
41    
42    $dir_tree->accept($visitor);
43    
44    # these are all the files we created
45    my @files = (
46        File::Spec->catfile("test", "test.pm"),
47        File::Spec->catfile("test", "sub_test2", "sub_test.pm"),
48        File::Spec->catfile("test", "sub_test2", "sub_sub_sub_test", "sub_sub_sub_test.pm")
49        );
50        
51    # loop through and check them 
52    # and then remove them   
53    foreach my $filename (@files) {
54        ok(-e $filename, '... file exists');
55        ok(-f $filename, '... and it is a file');
56        # now remove it
57        cmp_ok(unlink($filename), '==', 1, '... removed file');
58        ok(!-e $filename, '... file is actually gone');    
59    }    
60    
61    # these are all the directories 
62    # we created (in reverse order)
63    my @directories = reverse(
64        "test",
65        File::Spec->catdir("test", "sub_test"),
66        File::Spec->catdir("test", "sub_test2"),
67        File::Spec->catdir("test", "sub_test2", "sub_sub_test"),
68        File::Spec->catdir("test", "sub_test2", "sub_sub_sub_test")
69        );    
70    
71    # loop through and check them    
72    # and remove them (reverse order
73    # insures that they are empty when
74    # we remove them)
75    foreach my $dirname (@directories) {
76        ok(-e $dirname, '... directory exists');
77        ok(-d $dirname, '... and it is a directory');
78        # now remove it
79        cmp_ok(rmdir($dirname), '==', 1, '... removed directory');
80        ok(!-e $dirname, '... directory is actually gone');    
81    }   
82}
83
84# test the file and dir handlers
85{
86    my $visitor = Tree::Simple::Visitor::CreateDirectoryTree->new();
87    isa_ok($visitor, 'Tree::Simple::Visitor::CreateDirectoryTree');
88    isa_ok($visitor, 'Tree::Simple::Visitor');
89    
90    can_ok($visitor, 'visit');
91    can_ok($visitor, 'setFileHandler');
92    can_ok($visitor, 'setDirectoryHandler');    
93    
94    $visitor->setNodeFilter(sub {
95        my ($node) = @_;
96        return "_$node";
97    });
98    
99    # capture the directories
100    # in an array, but don't bother 
101    # to create anything
102    my @dirs;
103    $visitor->setDirectoryHandler(sub {
104        my ($dir_path) = @_;
105        push @dirs => $dir_path;
106    });
107
108    # these are the expected values
109    my @expected_dirs = (
110        "_test",
111        File::Spec->catdir("_test", "_sub_test"),
112        File::Spec->catdir("_test", "_sub_test2"),
113        File::Spec->catdir("_test", "_sub_test2", "_sub_sub_test"),
114        File::Spec->catdir("_test", "_sub_test2", "_sub_sub_sub_test")
115        );   
116
117    # capture the files
118    # in an array, but don't bother 
119    # to create anything    
120    my @files;
121    $visitor->setFileHandler(sub {
122        my ($file_path) = @_;
123        push @files => $file_path;
124    });
125    
126    # these are the expected values
127    my @expected_files = (
128        File::Spec->catfile("_test", "_test.pm"),
129        File::Spec->catfile("_test", "_sub_test2", "_sub_test.pm"),
130        File::Spec->catfile("_test", "_sub_test2", "_sub_sub_sub_test", "_sub_sub_sub_test.pm")
131        );    
132    
133    $dir_tree->accept($visitor);
134    
135    is_deeply(\@dirs, \@expected_dirs, '... got the directories we expected');
136    is_deeply(\@files, \@expected_files, '... got the files we expected');
137}
138
139
140# test the errors
141{
142    my $visitor = Tree::Simple::Visitor::CreateDirectoryTree->new();
143    isa_ok($visitor, 'Tree::Simple::Visitor::CreateDirectoryTree');
144    isa_ok($visitor, 'Tree::Simple::Visitor');
145    
146    # check visit
147    throws_ok {
148        $visitor->visit();
149    } qr/Insufficient Arguments/, '... got the error we expected';  
150    
151    throws_ok {
152        $visitor->visit("Fail");
153    } qr/Insufficient Arguments/, '... got the error we expected';                           
154
155    throws_ok {
156        $visitor->visit([]);
157    } qr/Insufficient Arguments/, '... got the error we expected'; 
158    
159    throws_ok {
160        $visitor->visit(bless({}, "Fail"));
161    } qr/Insufficient Arguments/, '... got the error we expected';  
162    
163    # check the handler errors
164    throws_ok {
165        $visitor->setDirectoryHandler();
166    } qr/Insufficient Arguments/, '... got the error we expected';      
167    
168    throws_ok {
169        $visitor->setDirectoryHandler("Fail");
170    } qr/Insufficient Arguments/, '... got the error we expected';     
171    
172    throws_ok {
173        $visitor->setDirectoryHandler([]);
174    } qr/Insufficient Arguments/, '... got the error we expected';    
175    
176    # check the handler errors
177    throws_ok {
178        $visitor->setFileHandler();
179    } qr/Insufficient Arguments/, '... got the error we expected';      
180    
181    throws_ok {
182        $visitor->setFileHandler("Fail");
183    } qr/Insufficient Arguments/, '... got the error we expected';     
184    
185    throws_ok {
186        $visitor->setFileHandler([]);
187    } qr/Insufficient Arguments/, '... got the error we expected';        
188}
189