[Pdx-pm] Testing question

Jonathan "Duke" Leto jonathan at leto.net
Wed Jan 18 07:53:26 PST 2012


Howdy,

The test seems reasonable.

Some suggestions:

1) use strict + use warnings

2) Instead of

is($x eq $y, 'blarg')

this is better:

cmp_ok($x, 'eq', $y, 'blarg')

because you will get better error reporting from cmp_ok. It knows that
you are using 'eq', so it can say "hey, $x != $y", whereas the first
test will just say "this thing isn't true", which isn't nearly as
useful.

3) Consider using Test::Most. It has lots of awesomesauce.

Duke

On Fri, Jan 13, 2012 at 11:52 AM, Tom Keller <kellert at ohsu.edu> wrote:
> 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
>
> _______________________________________________
> Pdx-pm-list mailing list
> Pdx-pm-list at pm.org
> http://mail.pm.org/mailman/listinfo/pdx-pm-list



-- 
Jonathan "Duke" Leto <jonathan at leto.net>
Leto Labs LLC
209.691.DUKE // http://labs.leto.net
NOTE: Personal email is only checked twice a day at 10am/2pm PST,
please call/text for time-sensitive matters.


More information about the Pdx-pm-list mailing list