I'm writing an article about test-first programming for Linux Magazine. Unfortunately, my forte is more with black-box testing. Could I get some suggestions about implementation?

I have a legacy Perl script, not object-oriented but at least ported to Perl 5. I want to use a real-world example, rather than new code like what Kent Beck uses in the book Test-Driven Development. So I thought I'd implement a unit test for one of the functions in this old script. I'm using Test::Unit::Procedural.

I'm pulling my hair out trying to stub out the functions that the function under test is calling. Here's the function (complete with an error that will be corrected as part of the exercise):

sub abort_handler {
my ($event) = @_;
print STDERR "stress_driver: aborted by SIG$event->data()\n";
&log ("stress_driver: aborted by SIG$event->data()");
exit (&cleanup);
}

I want to stub out (replace) the &log and &cleanup functions and the builtin exit function. I'll probably also want to redirect STDERR to capture that output as well, or more likely I should refactor that line into a different log subroutine. Of course, I need to set up the test harness with minimal or no modifications to the code under test.

I can use "use subs" and the exporter to override the exit call. And I can use an ugly typeglob hack to override the user-defined subroutines. But I can't figure out how to turn unit test mode on and off. The only way I can get the override of exit() to work is to "use sd_stubs" to pull it in with the exporter. Here's the code that I added to the top of the main program that tries to do that:

if ($ARGV[0] && $ARGV[0] eq "-t") {
use sd_stubs;
require sd_unit_test;
}

If I try to run the program without "-t", so it functions as it did before, my exit stub is still in place. This code fixes that problem (after moving exit from @EXPORT to @EXPORT_OK):

BEGIN {
if ($ARGV[0] && $ARGV[0] eq "-t") {
my @overrides = qw(exit);
use sd_stubs @overrides;
require sd_unit_test;
}
}

But now the sd_unit_test package can't see the &main::abort_handler function. I'm lost in a mess of namespaces at this point.

Can someone point out in what way I'm being boneheaded here, and/or what the most reasonable way to stub out these functions would be? Here's sd_stubs.pm and sd_unit_test.pm. I split them into two files to solve a different problem I ran across.

-----------------------------------------
package sd_stubs;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(exit);
use warnings;
use strict;
use subs qw(exit);

sub log_stub {
print "LOG_STUB\n";
return 0;
}

sub exit {
print "EXIT_STUB\n";
return 0;
}

1;
--------------------------------------------
package sd_unit_test;
use warnings;
use strict;
use Test::Unit::Procedural;

my $log_main = \&log;
{
no warnings;
*main::log = \&sd_stubs::log_stub;
}

sub test_abort1 {
assert (main::abort_handler() == 0);
}
create_suite();
CORE::exit(run_suite());

1;
--
Danny Faught
Tejas Software Consulting
http://www.tejasconsulting.com

Reply via email to