in reply to How to test caching?
sub foo { my $arg = shift @_; state %cache; return $cache{$arg} if exists $cache{$arg}; return $cache{$arg} = something_costly( $arg ); }
Test code:
use Immutable::UntestableCode qw( foo ); my $real_thing = \&Immutable::UntestableCode::something_costly; my $calls = 0; *Immutable::UntestableCode::something_costly = sub { $calls++; return $real_thing->( @_ ); }; foo( 1 ); is( 1, $calls ); foo( 1 ); is( 1, $calls );
- tye
|
|---|