# Dyn.pm package Dyn; use strict; use warnings; sub _max { 10 } sub _rand { rand(shift) } sub _range { my $max = _max(); my $rand_max = _rand($max); return (0..int($rand_max)); } sub get_list { my @arr = (); push @arr, $_ for _range(); return @arr; } 1; __END__ #### #!/usr/bin/perl use Test::More tests => 10; use Dyn; use Test::Resub qw(resub); # _max { is( Dyn::_max, 10, 'Got expected max' ); } # _rand { my $max = 1045; srand($$); my $dyn_rand = Dyn::_rand($max); srand($$); my $core_rand = CORE::rand($max); is( $dyn_rand, $core_rand, 'We wrap CORE::rand' ); } # _range runs from 0.._max, returning only integers { my $max = 0; my $rs_max = resub 'Dyn::_max', sub { $max }; my $rs_rand = resub 'Dyn::_rand', sub { return shift }, capture => 1; is_deeply( [Dyn::_range], [0] ); is_deeply( $rs_rand->args, [[$max]] ); $max = 1.2; $rs_rand->reset; is_deeply( [Dyn::_range], [0, 1] ); is_deeply( $rs_rand->args, [[$max]] ); $max = 7.4; $rs_rand->reset; is_deeply( [Dyn::_range], [0..7] ); is_deeply( $rs_rand->args, [[$max]] ); } # get_list is our interface to _range { my @range; my $rs_range = resub 'Dyn::_range', sub { @range }; is_deeply( [Dyn::get_list], [] ); @range = ('a'..'f'); is_deeply( [Dyn::get_list], [qw(a b c d e f)] ); } __END__