root/lang/perl/Path-Extended/trunk/t/lib/Path/Extended/Test/Compatibility/Filesystem.pm @ 30855

Revision 30855, 5.9 kB (checked in by charsbar, 4 years ago)

Path-Extended: reorganized methods; added "recurse" and "new_foreign"; 0.07 -> CPAN

  • Property svn:eol-style set to native
Line 
1package Path::Extended::Test::Compatibility::Filesystem;
2
3use strict;
4use warnings;
5use Test::Classy::Base;
6use Path::Extended::Class;
7
8# ripped from Path::Class' t/03-filesystem.t
9
10sub tests00_file : Tests(9) {
11  my $class = shift;
12
13  my $file = file('t', 'testfile');
14  ok $file, $class->message("test 02");
15
16  {
17    my $fh = $file->open('w');
18    ok $fh, $class->message("test 03");
19    ok( (print $fh "Foo\n"), $class->message("test 04"));
20  }
21
22  ok -e $file, $class->message("test 05");
23
24  {
25    my $fh = $file->open;
26    is scalar <$fh>, "Foo\n", $class->message("test 06");
27  }
28
29  my $stat = $file->stat;
30  ok $stat, $class->message("test 07");
31  cmp_ok $stat->mtime, '>', time() - 20, $class->message("test 08");
32
33  $stat = $file->dir->stat;
34  ok $stat, $class->message("test 09");
35
36  1 while unlink $file;
37  ok( (not -e $file), $class->message("test 10"));
38}
39
40sub tests01_dir : Tests(26) {
41  my $class = shift;
42
43  my $air = dir('t', 'testdir');
44  ok $air, $class->message("test 11");
45
46  $air->remove if $air->exists;
47
48  ok mkdir($air, 0777), $class->message("test 12");
49  ok -d $air, $class->message("test 13");
50
51  my $file = $air->file('foo.x');
52  $file->touch;
53  ok -e $file, $class->message("test 14");
54
55  {
56    my $ah = $air->open;
57    ok $ah, $class->message("test 15");
58
59    my @files = readdir $ah;
60    is scalar @files, 3, $class->message("test 16");
61    ok( (scalar grep { $_ eq 'foo.x' } @files), $class->message("test 17"));
62  }
63
64  ok $air->rmtree, $class->message("test 18");
65  ok !-e $air, $class->message("test 19");
66
67  $air = dir('t', 'foo', 'bar');
68  ok $air->mkpath, $class->message("test 20");
69  ok -d $air, $class->message("test 21");
70
71  $air = $air->parent;
72  ok $air->rmtree, $class->message("test 22");
73  ok !-e $air, $class->message("test 23");
74
75  $air = dir('t', 'foo');
76  ok $air->mkpath, $class->message("test 24");
77  ok $air->subdir('dir')->mkpath, $class->message("test 25");
78  ok -d $air->subdir('dir'), $class->message("test 26");
79
80  ok $air->file('file.x')->open('w'), $class->message("test 27");
81  ok $air->file('0')->open('w'), $class->message("test 28");
82
83  my @contents;
84  while (my $file = $air->next) {
85    push @contents, $file;
86  }
87  is scalar @contents, 5, $class->message("test 29");
88
89  my $joined = join ' ', map $_->basename, sort grep {-f $_} @contents;
90  is $joined, '0 file.x', $class->message("test 30");
91  my ($subdir) = grep {$_ eq $air->subdir('dir')} @contents;
92  ok $subdir, $class->message("test 31");
93  is -d $subdir, 1, $class->message("test 32");
94
95  ($file) = grep {$_ eq $air->file('file.x')} @contents;
96  ok $file, $class->message("test 33");
97  is -d $file, '', $class->message("test 34");
98
99  ok $air->rmtree, $class->message("test 35");
100  ok !-e $air, $class->message("test 36");
101}
102
103sub tests02_slurp : Tests(6) {
104  my $class = shift;
105
106  my $file = file('t', 'slurp');
107  ok $file, $class->message("test 37");
108
109  my $fh = $file->open('w') or die "Can't create $file: $!";
110  print $fh "Line1\nLine2\n";
111  close $fh;
112  ok -e $file, $class->message("test 38");
113
114  my $content = $file->slurp;
115  is $content, "Line1\nLine2\n", $class->message("test 39");
116
117  my @content = $file->slurp;
118  is_deeply \@content, ["Line1\n", "Line2\n"], $class->message("test 40");
119
120  @content = $file->slurp(chomp => 1);
121  is_deeply \@content, ["Line1", "Line2"], $class->message("test 41");
122
123  $file->remove;
124  ok((not -e $file), $class->message("test 42"));
125}
126
127sub test03_absolute_relative : Test Skip('known incompatibility') {
128  my $class = shift;
129
130  my $cwd = dir();
131  is $cwd, $cwd->absolute->relative, $class->message("test 43");
132}
133
134sub tests04_subsumes : Tests(4) {
135  my $class = shift;
136
137  my $t = dir('t');
138  my $foo_bar = $t->subdir('foo','bar');
139  $foo_bar->rmtree;
140
141  ok  $t->subsumes($foo_bar), $class->message("test 44");
142  ok !$t->contains($foo_bar), $class->message("test 45");
143
144  $foo_bar->mkpath;
145  ok  $t->subsumes($foo_bar), $class->message("test 46");
146  ok  $t->contains($foo_bar), $class->message("test 47");
147
148  $t->subdir('foo')->rmtree;
149}
150
151sub tests05_recurse : Tests(17) {
152  my $class = shift;
153
154  (my $abe = dir(qw(a b e)))->mkpath;
155  (my $acf = dir(qw(a c f)))->mkpath;
156  file($acf, 'i')->touch;
157  file($abe, 'h')->touch;
158  file($abe, 'g')->touch;
159  file('a', 'b', 'd')->touch;
160
161  my $d = dir('a');
162  my @children = $d->children;
163
164  is_deeply \@children, ['a/b', 'a/c'];
165
166  {
167    recurse_test( $d,
168      preorder => 1, depthfirst => 0,  # The default
169      precedence => [qw(
170        a           a/b
171        a           a/c
172        a/b         a/b/e/h
173        a/b         a/c/f/i
174        a/c         a/b/e/h
175        a/c         a/c/f/i
176      )],
177    );
178  }
179
180  {
181    my $files =
182      recurse_test( $d,
183        preorder => 1, depthfirst => 1,
184        precedence => [qw(
185          a           a/b
186          a           a/c
187          a/b         a/b/e/h
188          a/c         a/c/f/i
189        )],
190      );
191    is_depthfirst($files);
192  }
193
194  {
195    my $files =
196      recurse_test( $d,
197        preorder => 0, depthfirst => 1,
198        precedence => [qw(
199          a/b         a
200          a/c         a
201          a/b/e/h     a/b
202          a/c/f/i     a/c
203        )],
204      );
205    is_depthfirst($files);
206  }
207
208  $d->rmtree;
209
210  sub is_depthfirst {
211    my $files = shift;
212    if ($files->{'a/b'} < $files->{'a/c'}) {
213      cmp_ok $files->{'a/b/e'}, '<', $files->{'a/c'}, "Ensure depth-first search";
214    } else {
215      cmp_ok $files->{'a/c/f'}, '<', $files->{'a/b'}, "Ensure depth-first search";
216    }
217  }
218
219  sub recurse_test {
220    my ($dir, %args) = @_;
221    my $precedence = delete $args{precedence};
222    my ($i, %files) = (0);
223    $dir->recurse( callback => sub {$files{shift->as_foreign('Unix')->stringify} = ++$i},
224                 %args );
225    while (my ($pre, $post) = splice @$precedence, 0, 2) {
226      cmp_ok $files{$pre}, '<', $files{$post}, "$pre should come before $post";
227    }
228    return \%files;
229  }
230}
231
232sub END {
233  my $class = shift;
234
235  dir('a')->rmtree;
236  dir('t/foo')->remove;
237  dir('t/testdir')->remove;
238  file('t/testfile')->remove;
239}
240
2411;
Note: See TracBrowser for help on using the browser.