# foo.pl
use strict;
use warnings;
use Log::Log4perl;
my $conf = qq(
log4perl.category.Script = INFO, ScreenAppender
log4perl.appender.ScreenAppender = Log::Log4perl::Appender::Screen
log4perl.appender.ScreenAppender.layout = PatternLayout
log4perl.appender.ScreenAppender.layout.ConversionPattern=[%p] %d %M %F:%L - %m%n
);
Log::Log4perl::init( \$conf );
my $log = Log::Log4perl::->get_logger(q(Script));
# Add two numbers, no larger than 32 each
sub add {
my ($i, $j) = @_;
$log->logdie("type error") unless ($i and $j and $i =~ m/^\d+/ and $j =~ m/^\d+/);
$log->warn("possible overflow") unless ($i < 2**5 and $j < 2**5);
return $i + $j;
}
# When testing: Disable all logging,
# When not testing: call add()
caller() ? Log::Log4perl->appender_thresholds_adjust(7) : print add(@ARGV);
1;
__END__
####
# foo.t
use strict;
use warnings;
use Test::More qw/no_plan/;
use Test::Exception;
require_ok(q{foo.pl});
# ------ negative tests for add() ------
throws_ok { add(); } qr/error/i, q{Expect error when no args};
throws_ok { add('a'); } qr/error/i, q{Expect error with non-numeric input};
throws_ok { add('a1'); } qr/error/i, q{Expect error with non-numeric input};
throws_ok { add(1,'b'); } qr/error/i, q{Expect error with non-numeric input};
throws_ok { add(1,'b1'); } qr/error/i, q{Expect error with non-numeric input};
# ------ positive tests for add() ------
cmp_ok(add(2,2), q{==}, 4, q{Expect 2+2=4});
# Note: I want a warning here:
cmp_ok(add(1000,1000), q{==}, 2000, q{Expect 1000+1000=2000});
__END__
####
$ prove --version
TAP::Harness v3.10 and Perl v5.10.0
$ prove foo.t
foo......ok
All tests successful.
Files=1, Tests=8, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.08 cusr 0.00 csys = 0.10 CPU)
Result: PASS