M: Improved and extended unit tests.
This commit is contained in:
@@ -131,8 +131,12 @@ sub entryList {
|
||||
sub walk {
|
||||
my ($self,$path) = @_;
|
||||
|
||||
$self->{currentDir} = dirname($path);
|
||||
$self->{currentBasename} = basename($path);
|
||||
my $currentDir = dirname($path);
|
||||
my $currentBasename = basename($path);
|
||||
my $currentPath = $path;
|
||||
|
||||
$self->{currentDir} = $currentDir;
|
||||
$self->{currentBasename} = $currentBasename;
|
||||
$self->{currentPath} = $path;
|
||||
|
||||
if ((my $r = $self->{onBeginWalk}->($path)) != SUCCESS) {
|
||||
@@ -179,6 +183,10 @@ sub walk {
|
||||
|
||||
closedir $dirh;
|
||||
|
||||
$self->{currentDir} = $currentDir;
|
||||
$self->{currentBasename} = $currentBasename;
|
||||
$self->{currentPath} = $path;
|
||||
|
||||
if ((my $r = $self->{onDirLeave}->($path)) != SUCCESS) {
|
||||
return $r;
|
||||
}
|
||||
|
||||
67
t/1.t
67
t/1.t
@@ -1,11 +1,8 @@
|
||||
use Test::More qw(no_plan);
|
||||
use Test::Exception;
|
||||
|
||||
use File::Basename;
|
||||
use File::DirWalk;
|
||||
|
||||
my $perl_path = dirname($^X);
|
||||
my $perl_interpreter = basename($^X);
|
||||
BEGIN { use_ok( 'File::DirWalk' ); }
|
||||
require_ok( 'File::DirWalk' );
|
||||
|
||||
$dw = new File::DirWalk();
|
||||
|
||||
@@ -28,64 +25,4 @@ dies_ok {$dw->setHandler(onLink => 0)};
|
||||
dies_ok {$dw->setHandler(onFile => 0)};
|
||||
dies_ok {$dw->setHandler(onDirEnter => 0)};
|
||||
dies_ok {$dw->setHandler(onDirLeave => 0)};
|
||||
|
||||
dies_ok {$dw->setHandler(Foo => sub {})};
|
||||
|
||||
$dw->onFile(sub {
|
||||
my ($path) = @_;
|
||||
|
||||
if (basename($path) eq "1.t") {
|
||||
return ABORTED;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
});
|
||||
|
||||
ok( $dw->walk($0) == ABORTED );
|
||||
|
||||
$dw->onDirEnter(sub {
|
||||
my ($path) = @_;
|
||||
|
||||
if ($path eq $perl_path) {
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
});
|
||||
|
||||
ok( $dw->walk($perl_path) == FAILED );
|
||||
|
||||
$dw->onBeginWalk(sub {
|
||||
my ($path) = @_;
|
||||
if (dirname($path) eq $dw->currentDir) {
|
||||
return ABORTED;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
});
|
||||
|
||||
ok( $dw->walk($perl_path) == ABORTED );
|
||||
|
||||
$dw->onBeginWalk(sub {
|
||||
my ($path) = @_;
|
||||
if ($path eq $dw->currentPath) {
|
||||
return ABORTED;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
});
|
||||
|
||||
ok( $dw->walk($perl_path) == ABORTED );
|
||||
|
||||
$dw = new File::DirWalk();
|
||||
$dw->onFile(sub {
|
||||
my ($path) = @_;
|
||||
|
||||
if ($dw->currentBasename() eq $perl_interpreter) {
|
||||
return 42;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
});
|
||||
|
||||
ok( $dw->walk($perl_path) == 42 );
|
||||
|
||||
29
t/2.t
Normal file
29
t/2.t
Normal file
@@ -0,0 +1,29 @@
|
||||
use Test::More qw(no_plan);
|
||||
use Test::Exception;
|
||||
|
||||
use File::Basename;
|
||||
|
||||
BEGIN { use_ok( 'File::DirWalk' ); }
|
||||
require_ok( 'File::DirWalk' );
|
||||
|
||||
ok(-e "t/");
|
||||
ok(-d "t/");
|
||||
|
||||
foreach my $f qw(1.t 2.t 3.t 4.t) {
|
||||
$dw = new File::DirWalk();
|
||||
$dw->setDepth(1);
|
||||
|
||||
$dw->onFile(sub {
|
||||
my ($path) = @_;
|
||||
ok(-e $path);
|
||||
ok(-f $path);
|
||||
|
||||
if ($dw->currentBasename() eq $f) {
|
||||
return 42;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
});
|
||||
|
||||
is($dw->walk("t/"), 42);
|
||||
}
|
||||
55
t/3.t
Normal file
55
t/3.t
Normal file
@@ -0,0 +1,55 @@
|
||||
use Test::More qw(no_plan);
|
||||
use Test::Exception;
|
||||
|
||||
use File::Basename;
|
||||
|
||||
BEGIN { use_ok( 'File::DirWalk' ); }
|
||||
require_ok( 'File::DirWalk' );
|
||||
|
||||
my $perl_path = dirname($^X);
|
||||
my $perl_interpreter = basename($^X);
|
||||
|
||||
$dw = new File::DirWalk();
|
||||
$dw->onDirEnter(sub {
|
||||
my ($path) = @_;
|
||||
ok(-e $path);
|
||||
ok(-d $path);
|
||||
|
||||
if ($dw->currentPath() eq $perl_path) {
|
||||
return 42;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
});
|
||||
|
||||
is( $dw->walk($perl_path), 42 );
|
||||
|
||||
$dw = new File::DirWalk();
|
||||
$dw->onDirEnter(sub {
|
||||
my ($path) = @_;
|
||||
ok(-e $path);
|
||||
ok(-d $path);
|
||||
|
||||
if ($dw->currentDir() eq $perl_path) {
|
||||
return 42;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
});
|
||||
|
||||
is( $dw->walk($perl_path), 42 );
|
||||
|
||||
$dw = new File::DirWalk();
|
||||
$dw->onFile(sub {
|
||||
my ($path) = @_;
|
||||
ok(-e $path);
|
||||
ok(-f $path);
|
||||
|
||||
if ($dw->currentBasename() eq $perl_interpreter) {
|
||||
return 42;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
});
|
||||
|
||||
is( $dw->walk($perl_path), 42 );
|
||||
88
t/4.t
Normal file
88
t/4.t
Normal file
@@ -0,0 +1,88 @@
|
||||
use Test::More qw(no_plan);
|
||||
use Test::Exception;
|
||||
|
||||
use File::Basename;
|
||||
use File::Spec::Functions;
|
||||
|
||||
BEGIN { use_ok( 'File::DirWalk' ); }
|
||||
require_ok( 'File::DirWalk' );
|
||||
|
||||
$dw = new File::DirWalk();
|
||||
|
||||
ok( -e "t/tree" );
|
||||
ok( -d "t/tree" );
|
||||
|
||||
foreach my $subdir (qw(dir1 dir2 dir3 dir4 dir5)) {
|
||||
ok( -e "t/tree/$subdir" );
|
||||
ok( -d "t/tree/$subdir" );
|
||||
|
||||
$dw = new File::DirWalk();
|
||||
$dw->onBeginWalk(sub {
|
||||
my ($path) = @_;
|
||||
ok(-e $path);
|
||||
|
||||
if ($dw->currentBasename() eq $subdir) {
|
||||
return 42;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
});
|
||||
is( $dw->walk("t/tree"), 42 );
|
||||
|
||||
$dw = new File::DirWalk();
|
||||
$dw->onDirEnter(sub {
|
||||
my ($path) = @_;
|
||||
ok(-e $path);
|
||||
ok(-d $path);
|
||||
|
||||
if ($dw->currentBasename() eq $subdir) {
|
||||
is( $dw->count(), 10 );
|
||||
is( $dw->currentDepth(), 2 );
|
||||
return 42;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
});
|
||||
is( $dw->walk("t/tree"), 42 );
|
||||
|
||||
$dw = new File::DirWalk();
|
||||
$dw->onDirLeave(sub {
|
||||
my ($path) = @_;
|
||||
ok(-e $path);
|
||||
ok(-d $path);
|
||||
|
||||
if ($dw->currentBasename() eq $subdir) {
|
||||
is( $dw->count(), 10 );
|
||||
is( $dw->currentDepth(), 2 );
|
||||
return 42;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
});
|
||||
|
||||
is( $dw->walk("t/tree"), 42 );
|
||||
|
||||
$dw = new File::DirWalk();
|
||||
$dw->onFile(sub {
|
||||
my ($path) = @_;
|
||||
ok(-e $path);
|
||||
ok(-f $path);
|
||||
return SUCCESS;
|
||||
});
|
||||
|
||||
is( $dw->walk("t/tree/$subdir"), SUCCESS );
|
||||
is( $dw->count(), 10 );
|
||||
}
|
||||
|
||||
$files = 0;
|
||||
$dw = new File::DirWalk();
|
||||
$dw->onFile(sub {
|
||||
my ($path) = @_;
|
||||
ok(-e $path);
|
||||
ok(-f $path);
|
||||
++$files;
|
||||
return SUCCESS;
|
||||
});
|
||||
|
||||
is( $dw->walk("t/tree"), SUCCESS );
|
||||
is( $files, 50 );
|
||||
0
t/tree/dir1/file0
Normal file
0
t/tree/dir1/file0
Normal file
0
t/tree/dir1/file1
Normal file
0
t/tree/dir1/file1
Normal file
0
t/tree/dir1/file2
Normal file
0
t/tree/dir1/file2
Normal file
0
t/tree/dir1/file3
Normal file
0
t/tree/dir1/file3
Normal file
0
t/tree/dir1/file4
Normal file
0
t/tree/dir1/file4
Normal file
0
t/tree/dir1/file5
Normal file
0
t/tree/dir1/file5
Normal file
0
t/tree/dir1/file6
Normal file
0
t/tree/dir1/file6
Normal file
0
t/tree/dir1/file7
Normal file
0
t/tree/dir1/file7
Normal file
0
t/tree/dir1/file8
Normal file
0
t/tree/dir1/file8
Normal file
0
t/tree/dir1/file9
Normal file
0
t/tree/dir1/file9
Normal file
0
t/tree/dir2/file0
Normal file
0
t/tree/dir2/file0
Normal file
0
t/tree/dir2/file1
Normal file
0
t/tree/dir2/file1
Normal file
0
t/tree/dir2/file2
Normal file
0
t/tree/dir2/file2
Normal file
0
t/tree/dir2/file3
Normal file
0
t/tree/dir2/file3
Normal file
0
t/tree/dir2/file4
Normal file
0
t/tree/dir2/file4
Normal file
0
t/tree/dir2/file5
Normal file
0
t/tree/dir2/file5
Normal file
0
t/tree/dir2/file6
Normal file
0
t/tree/dir2/file6
Normal file
0
t/tree/dir2/file7
Normal file
0
t/tree/dir2/file7
Normal file
0
t/tree/dir2/file8
Normal file
0
t/tree/dir2/file8
Normal file
0
t/tree/dir2/file9
Normal file
0
t/tree/dir2/file9
Normal file
0
t/tree/dir3/file0
Normal file
0
t/tree/dir3/file0
Normal file
0
t/tree/dir3/file1
Normal file
0
t/tree/dir3/file1
Normal file
0
t/tree/dir3/file2
Normal file
0
t/tree/dir3/file2
Normal file
0
t/tree/dir3/file3
Normal file
0
t/tree/dir3/file3
Normal file
0
t/tree/dir3/file4
Normal file
0
t/tree/dir3/file4
Normal file
0
t/tree/dir3/file5
Normal file
0
t/tree/dir3/file5
Normal file
0
t/tree/dir3/file6
Normal file
0
t/tree/dir3/file6
Normal file
0
t/tree/dir3/file7
Normal file
0
t/tree/dir3/file7
Normal file
0
t/tree/dir3/file8
Normal file
0
t/tree/dir3/file8
Normal file
0
t/tree/dir3/file9
Normal file
0
t/tree/dir3/file9
Normal file
0
t/tree/dir4/file0
Normal file
0
t/tree/dir4/file0
Normal file
0
t/tree/dir4/file1
Normal file
0
t/tree/dir4/file1
Normal file
0
t/tree/dir4/file2
Normal file
0
t/tree/dir4/file2
Normal file
0
t/tree/dir4/file3
Normal file
0
t/tree/dir4/file3
Normal file
0
t/tree/dir4/file4
Normal file
0
t/tree/dir4/file4
Normal file
0
t/tree/dir4/file5
Normal file
0
t/tree/dir4/file5
Normal file
0
t/tree/dir4/file6
Normal file
0
t/tree/dir4/file6
Normal file
0
t/tree/dir4/file7
Normal file
0
t/tree/dir4/file7
Normal file
0
t/tree/dir4/file8
Normal file
0
t/tree/dir4/file8
Normal file
0
t/tree/dir4/file9
Normal file
0
t/tree/dir4/file9
Normal file
0
t/tree/dir5/file0
Normal file
0
t/tree/dir5/file0
Normal file
0
t/tree/dir5/file1
Normal file
0
t/tree/dir5/file1
Normal file
0
t/tree/dir5/file2
Normal file
0
t/tree/dir5/file2
Normal file
0
t/tree/dir5/file3
Normal file
0
t/tree/dir5/file3
Normal file
0
t/tree/dir5/file4
Normal file
0
t/tree/dir5/file4
Normal file
0
t/tree/dir5/file5
Normal file
0
t/tree/dir5/file5
Normal file
0
t/tree/dir5/file6
Normal file
0
t/tree/dir5/file6
Normal file
0
t/tree/dir5/file7
Normal file
0
t/tree/dir5/file7
Normal file
0
t/tree/dir5/file8
Normal file
0
t/tree/dir5/file8
Normal file
0
t/tree/dir5/file9
Normal file
0
t/tree/dir5/file9
Normal file
Reference in New Issue
Block a user