in reply to Code review
use strict; use warnings; use Test::More; my $y = 5; my %Y; my @cases = ( [[], 'no @ARGs'], # 1 [[1,2,3], 'no matches'], # undef [[5,2,5], '2 matches'], # 2 ); plan tests=>3; foreach my $case (@cases) { @ARGV = @{$case->[0]}; %Y = (); my $Y_OP = OP(); @ARGV = @{$case->[0]}; %Y = (); my $Y_TEST = TEST(); is($Y_TEST, $Y_OP, $case->[1]); } sub OP { if(@ARGV) { foreach (@ARGV) { $Y{$y}++,next if $y eq $_; } } else { $Y{$y}++ } return $Y{$y}; } sub TEST{ my $number_of_matches = grep { $_ eq $y } @ARGV; $number_of_matches = undef if $number_of_matches == 0; $Y{$y} = (@ARGV != 0) ? $number_of_matches : 1; return $Y{$y}; }
OUTPUT:
1..3 ok 1 - no @ARGs ok 2 - no matches ok 3 - 2 matches
|
---|