[Pdx-pm] Testing question

Tom Keller kellert at ohsu.edu
Fri Jan 13 11:52:55 PST 2012


join(', ', map {$prefix . sprintf("%02d", $_)} $start..$last); 
is much less clunky and elegantly handles the format issue. Thanks

May I ask aTesting question? 
Is this a reasonable way to test this function? 
Do you test just that the output looks like you expect, or do you test that your program gets the input too (even though GetOpt includes it's own test for the input?

 ( t/02-range_fix.t )
########## code snippet #########
#!perl -T

use lib '/Users/kellert/Computing/Git/Workrequest-Utilities/lib';		# for development on Edofleini
use Test::More  'no_plan';

BEGIN {
	use_ok( 'Workrequest::Utilities' );
}
$prefix = 'test';
$start = 8;
$count = 5;
$last = $start + $count -1;
$answer = 'test08, test09, test10, test11, test12';		## n.b. space between elements in csv list
$result =  join(', ', map {$prefix . sprintf("%02d", $_)} $start..$last);

diag( "Testing Workrequest::Utilities $Workrequest::Utilities::VERSION, Perl $], $^X" );
ok( defined $result, 'got a result from map' );
is( $result eq $answer, 1, "compare result to $answer" );		## 1 means TRUE
########  end code snippet #########

thanks,

Tom
kellert at ohsu.edu
503-494-2442







On Jan 13, 2012, at 10:58 AM, Eric Wilhelm wrote:

> # from Joshua Keroes
> # on Thursday 12 January 2012 17:06:
> 
>>    1. I suspect that $c++ line does nothing.
>>    2. sprintf or printf will handle that 0-prefixing with less code.
> 
> Using sprintf(), map(), and join() eliminates the four instances of 
> duplicate/pseudo-duplicate "$prefix . $something" code and reduces it 
> to:
> 
>  join(', ', map {$prefix . sprintf("%02d", $_)} $start..$last)
> 
> Note the concatenation of commas onto $samples, plus the trailing case 
> with special handling for "add last sample w/o a comma".  Using join() 
> eliminates this special case at the end.
> 
> It is also more efficient (for large strings, repeated concatenation can 
> be very expensive vs growing a list.)
> 
> Or, if you would rather read your code from left to right:
> 
>  use List::oo qw(L);
>  L($start..$last)->map(sub{$prefix . sprintf("%02d", $_)})->join(', ');
> 
> --Eric
> -- 
> Issues of control, repair, improvement, cost, or just plain
> understandability all come down strongly in favor of open source
> solutions to complex problems of any sort.
> --Robert G. Brown
> ---------------------------------------------------
>    http://scratchcomputing.com
> ---------------------------------------------------
> _______________________________________________
> Pdx-pm-list mailing list
> Pdx-pm-list at pm.org
> http://mail.pm.org/mailman/listinfo/pdx-pm-list



More information about the Pdx-pm-list mailing list