I have a module that I wrote with some utility functions.
One of the functions is just a usage statement (recommended by user @zdim)
use 5.008_008;
use strict;
use warnings;
# Function Name: 'usage'
# Function Inputs: 'none'
# Function Returns: 'none'
# Function Description: 'Prints usage on STDERR, for when invalid options are passed'
sub usage ## no critic qw(RequireArgUnpacking)
{
require File::Basename;
my $PROG = File::Basename::basename($0);
for (@_)
{
print {*STDERR} $_;
}
print {*STDERR} "Try $PROG --help for more information.\n";
exit 1;
}
I know the subroutine works as expected, and it's simple enough to test, but... For coverage reports, I'd like to include it in my unit tests.
Is there any way to test it using Test::More
?
You can use Test::Exit.
If for any reason you cannot use it, just copy the code below:
our $exit_handler = sub {
CORE::exit $_[0];
};
BEGIN {
*CORE::GLOBAL::exit = sub (;$) {
$exit_handler->(@_ ? 0 + $_[0] : 0)
};
}
{
my $exit = 0;
local $exit_handler = sub {
$exit = $_[0];
no warnings qw( exiting );
last TEST;
};
TEST: {
# Your test here
}
cmp_ok($exit, '==', 1, "exited with 1");
}
Be sure to load your module after the BEGIN block.
Alternatively, you can use END block to handle exit calls.
Inside an END code block, $? contains the value that the program is going to pass to exit(). You can modify $? to change the exit value of the program.
usage();
END {
use Test::More;
ok($?);
done_testing();
}
Demo: https://ideone.com/AQx395
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With