I'm doing some testing, and am trying to test each function in isolation, as much as is possible.
In many cases, when testing a function that calls other, complex functions, I just want to call "stub functions" with known outputs. This simplifies my testing.
I know that I can locally override the functions that I want (within the framework of a single test function), by using code like the following:
sub test_foo {
my $temp;
$temp = $^W; # save warning flag status
$^W=0; # ignore "function is redefined" warning
local *module::bar=\&stub_for_bar;
$^W = $temp; # restore warnings
# test code for module::foo() goes here
}
I'd like to save myself some typing, and create a macro that overrides a given function with a given stub, local to the current scope. I see it looking something like this:
sub test_foo {
override_with_stub_fn('module::bar',\&stub_for_bar);
# test code for module::foo goes here
}
I can't think of a way to do this in perl, however. If I try to move the code into a function, the local() command has the wrong scope. If there were a way to make it run in the parent's scope, (like TCL's uplevel() command), the problem would be solved, but I don't know of a way to do that.
Is there some tricky caller/AUTOLOAD/goto trick I can do to accomplish this? I'd just as soon not use the -P (C-preprocessor) option if I can avoid it.
--
Ytrew