in reply to Testing with non-trivial input
What I did was write a little module that let me re-define and restore complex sections of code on the fly. For example, if I wanted to test "spiffy_little_function":
sub spiffy_little_function { my ($x,$y) = @_; my @params; # parameters to Foo::new my $o; # an object of class Foo my $z; # computed result before taxes my $t; # computed result after taxes @params=get_huge_list_of_parameters($x); $o = Foo->new(@params); $z=$o->compute_result($y); $t = $z * 0.10; return($t) }
I'd probably do something like this in my test code:
# test spiffy_little_function with options 'foo' and 'bar' <P> # make test run faster by eliminating slow function call redefine_function("main::get_huge_list_of_params", sub {} ); <P> # use minimal constructor redefine_function("Foo::new",sub { return bless {},"Foo" }); <P> # This method should be called with "bar", and return 42, # for the purposes of this test. redefine_function("Foo::compute_result", sub { 42 } ); <P> $result = spiffy_little_function("foo","bar") is($result,4.2,"spiffy_little_function:foo bar test"); <P> restore_all_functions();
If I wanted to, and felt the time was worth it, I could write better tests by checking the input paramters in my stub functions, as well. For example, I could have written:
redefine_function(Foo::compute_result, sub { is($_[0],"bar","spiffy_little_function passed right value to c +ompute_result") return(42); }
and so forth.
This way, I don't have to create a lot of different versions of my program, with different stub modules for each test: I can just redefine the complex bits to be simple, on a temporary basis.
My redefine_function() just manipulates the symbol table entry, and saves the old code reference in a hash: the restore_function() and restore_all_functions restore the original functions again.
--
Ytrew
|
|---|