use Test::More; # you may need to install it
my @test_these = ( 'fruit' , 'animal' , 'tree' , 'goat' );
# The Test::More synopsis suggests you do this
# with the use above, but I found that I needed
# to set it dynamically after determining how
# many tests I was going to run
plan tests => scalar(@test_these) + 2;
# now we can create a "test"
ok( $test_these[0] eq 'fruit' , 'The first element is "fruit"' );
# that's it, that's all you have to do to "test" something.
# The ok is the exported function from Test::More and determines
# if the first argument is true or false ( 1 or 0 )
# Now I admit that was a stupid test, since we have a
# hard coded array so lets do something else
foreach (@test_these) {
ok( m/i/ , "our element has an 'i' in it" );
}
# That will test to see if the list item has an 'i'
# in it and in this case some of the tests will fail.
# one last test
undef @test_these
ok( !@test_these , 'no more @test_these!' );
1;
####
sub get_file_contents {
my ($self,$args) = @_;
my $contents;
my $file = $args->{file_name};
if (!$file) {
($file) = $self->fancy_magic_file_find($args);
}
if (!$file || !-e $file) {
return "ERROR: Invalid file passsed to 'get_file_contents'";
}
open(FILE,"$file") or $self->error_to_log("ERROR");
$contents = do {local $/;};
close FILE;
$self->error_to_log("retrieved $file") if $contents;
return $contents;
}
##
##
use Test::More tests => 3;
use My::Module;
my $object = My::Module->new();
# test that the object exists
ok( $object->isa('My::Module' , "Our object is in the right class");
my $content = $object->get_file_contents( {
file_name => '/home/trs80/file.txt'
} );
ok( $content ne '' , "\$content has something in it!" );
ok( $content =~ /string/ , "\$content contains string!" );
1;
##
##
sub get_file_contents {
my ($self,$args) = @_;
my $contents;
my $file;
if (ref($args) eq 'HASH') {
$file = $args->{file_path};
} else {
$file = $args;
}
if (!$file) {
($file) = $self->fancy_magic_file_find($args);
}
if (!$file || !-e $file) {
return "ERROR: Invalid file passsed to 'get_file_contents'";
}
open(FILE,"$file") or $self->error_to_log("ERROR: ");
$contents = do {local $/;};
close FILE;
$self->error_to_log("retrieved $file") if $contents;
return wantarray ? ($contents,$file) : $contents;
}
##
##
use Test::More tests => 13;
use My::Module;
# Make my object
my $object = My::Module->new();
my $dir = '/home/trs80';
my $test_file = 'file.txt';
# create hashref for use later on
my $session = {
base_dir => $dir,
}
# test that the object exists
ok( $object->isa('My::Module' , "Our object is in the right class");
# first we try a file name by itself
my $content = $object->get_file_contents( "$dir/$test_file" );
ok( $content ne '' , "\$content has something in it!" );
ok( $content =~ /string/ , "\$content contains string!" );
undef $content;
ok( !$content , "\$content is empty"
# now we try a file name like the first test.
$content = $object->get_file_contents( {
file_name => "$dir/$test_file",
} );
ok( $content ne '' , "\$content has something in it!" );
ok( $content =~ /string/ , "\$content contains string!" );
undef $content;
ok( !$content , "\$content is empty" );
# test of the 'fancy_magic_file_find' way
# and make sure it gives back a file name
my ($contents,$file_name) = $object->get_file_contents( $session );
ok( $file_name =~ m#^$dir#, "our file_name: $file_name" );
ok( $contents ne '' , "\$content has something in it!" );
ok( $contents =~ /string/ , "\$content contains string!" );
undef $contents;
ok( !$contents , "\$content is empty" );
# test the error condition
$content = $object->get_file_contents();
ok( $content =~ /^ERROR\:/ , 'error on retrieve with no arguments' );
1;