F
freesoft12
Hi,
I have a module that calls 'croak' if the input argument to my
function is wrong. I want to write a unit test that tests if 'croak'
is called when I give a bad input.
---- PathUtils.t ----
use Test::More qw( no_plan );
use PathUtils;
my $path = '/a/b/../c';
deleteDoubleDotsInPath(\$path);
ok($path eq '/a/c'); # success test
$path = '../c'; # bad input. a fail/negative test
# how can write a 'ok()' or 'is()' that checks that 'croak' function
is called
# ok(deleteDoubleDotsInPath(\$path));
------------ end ----------
----------- PathUtils.pm ----------
package PathUtils;
use Carp;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(&deleteDoubleDotsInPath);
# This function removes the double dots in a path '..' eg: a/b/../c/
d/../e
sub deleteDoubleDotsInPath {
my $text = shift;
die "Malformed path" if !check_path_($text);
while($$text =~ m/\.\./) {
$$text =~ s/\/[^\/]*\/\.\.//;
}
}
sub check_path_ {
my $text = shift;
my @dirs = split(/\//,$$text);
my @double_dots = grep(/\.\./,@dirs);
if (scalar(@dirs) - scalar(@double_dots) > scalar(@double_dots)) {
return 1;
}
return 0;
}
------------ end ------------
Regards
John
I have a module that calls 'croak' if the input argument to my
function is wrong. I want to write a unit test that tests if 'croak'
is called when I give a bad input.
---- PathUtils.t ----
use Test::More qw( no_plan );
use PathUtils;
my $path = '/a/b/../c';
deleteDoubleDotsInPath(\$path);
ok($path eq '/a/c'); # success test
$path = '../c'; # bad input. a fail/negative test
# how can write a 'ok()' or 'is()' that checks that 'croak' function
is called
# ok(deleteDoubleDotsInPath(\$path));
------------ end ----------
----------- PathUtils.pm ----------
package PathUtils;
use Carp;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(&deleteDoubleDotsInPath);
# This function removes the double dots in a path '..' eg: a/b/../c/
d/../e
sub deleteDoubleDotsInPath {
my $text = shift;
die "Malformed path" if !check_path_($text);
while($$text =~ m/\.\./) {
$$text =~ s/\/[^\/]*\/\.\.//;
}
}
sub check_path_ {
my $text = shift;
my @dirs = split(/\//,$$text);
my @double_dots = grep(/\.\./,@dirs);
if (scalar(@dirs) - scalar(@double_dots) > scalar(@double_dots)) {
return 1;
}
return 0;
}
------------ end ------------
Regards
John