Bricolage Tests

Ovid on 2004-12-04T05:43:35

In doing some work for the next version of Bricolage, I quickly discovered a limitation of Sub::Override. If I need to repeatedly override a subroutine to test various aspects of some code's behavior, I had to restore the original subroutine before overriding it subsequent times. Thus, I kept typing this:

$sub->restore('Some::sub')
    ->replace('Some::sub' => sub { 0 });
# test it

$sub->restore('Some::sub')
    ->replace('Some::sub' => sub { 1 });
# test it again

$sub->restore('Some::sub')
    ->replace('Some::sub' => sub { 2 });
# test it yet again

Theory asked me why I didn't just allow the sub to directly overridden more than once. "Well," blathered Ovid, "I'd have to maintain a stack of preceding subroutine behavior so I can ensure that restoring the subroutine always restores the previous behavior."

"No. Just restore the original behavior."

Duh. So I did. The new version (0.06) is on it's way to the CPAN and will let me write this:

$sub->replace('Some::sub' => sub { 0 });
# test it

$sub->replace('Some::sub' => sub { 1 });
# test it again

$sub->replace('Some::sub' => sub { 2 });
# test it yet again

Now, calling restore will always restore the original behavior of the subroutine. Pair programming strikes again.