blib/lib/Test/ManyParams.pm

filter: Statementstmt 96.9% 127 / 131 Branchbran 83.3% 40 / 48 Conditioncond 100% 5 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 26 / 26 Podpod 77.7% 7 / 9 totaltotal 93.2% 207 / 222 CC 34 SCAR 12.0
CC
34
Cov
93%
does_most
6.2
does_all
5.0
import
5.0
CRAP 3.3
↑ Summary
1 package Test::ManyParams;
2  
3 11 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 require Exporter;
8  
9 11 our @ISA = qw(Exporter);
10  
11 11 our @EXPORT = qw/
12 all_ok
13 all_are all_arent
14 any_ok
15 any_is any_isnt
16
17 most_ok
18
19 set_seed
20 /;
21  
22 11 our $VERSION = '0.10';
23  
24 11 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 11 ▶use Set::CrossProduct;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 11 our $seed;
28  
29 11 my $Tester = Test::Builder->new();
30  
31 1480 ▶sub does_all {
Subroutine does_all: called CC=5 SCAR=16.1
Pod undocumented
32 1480 my ($sub, $params) = @_;
33 1480 my $failed_param = undef;
34 1480 ▶ if (ref($params->[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->[0] eq 'ARRAY') { } 1346 134 1480
35 1346 ▶ if (grep {ref($params->[$_]) ne 'ARRAY'} (1 .. @$params-1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {ref $params->[$_] ne "ARRAY";} 1 .. @$params - 1) 6 1340 1346
36 6 die "If the first parameter is an arrayref, all other parameters must be also. " .
37 "Called with Parameter-Ref: " . _dump_params($params);
38 }
39 $failed_param = @$params > 1
40 ? _try_all_of_xproduct($sub, Set::CrossProduct->new( $params ))
41 1340 ▶ : _try_all_of_the_list($sub, @{$params->[0]});
Branch: 1 branch
BranchTrueFalseTotal
@$params > 1 ? : 1211 129 1340
42 } else {
43 134 $failed_param = _try_all_of_the_list($sub, @$params);
44 }
45 1474 my $ok = not defined($failed_param);
46 1474 ▶ my @diag = $ok
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 66 1408 1474
47 ? ()
48 : ("Tests with the parameters: " . _dump_params($params),
49 "Failed first using these parameters: " . _dump_params($failed_param));
50 1474 return ($ok, @diag);
51 }
52  
53 10 ▶sub does_most {
Subroutine does_most: called CC=6 SCAR=18.2
Pod undocumented
54 10 my ($sub, $params, $nr) = @_;
55 10 my $failed_param = undef;
56 10 ▶ $nr =~ /^\d+$/ or die "The number of tests that shall be done must be an integer, not '$nr'";
Branch: 1 branch
BranchTrueFalseTotal
$nr =~ /^\d+$/ or die "The number of tests that shall be done must be an integer, not '${nr}'" 10 0 10
57 10 ▶ if (ref($params->[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->[0] eq 'ARRAY') { } 4 6 10
58 4 ▶ if (grep {ref($params->[$_]) ne 'ARRAY'} (1 .. @$params-1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {ref $params->[$_] ne "ARRAY";} 1 .. @$params - 1) 0 4 4
59 0 die "If the first parameter is an arrayref, all other parameters must be also. " .
60 "Called with Parameter-Ref: " . _dump_params($params);
61 }
62 $failed_param = @$params > 1
63 ? _try_most_of_xproduct($sub,$nr, Set::CrossProduct->new( $params ))
64 4 ▶ : _try_most_of_the_list($sub,$nr, @{$params->[0]});
Branch: 1 branch
BranchTrueFalseTotal
@$params > 1 ? : 2 2 4
65 } else {
66 6 $failed_param = _try_most_of_the_list($sub,$nr, @$params);
67 }
68 8 my $ok = not defined($failed_param);
69 8 ▶ my @diag = $ok
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 8 0 8
70 ? ()
71 : ("Tests with most ($nr) of the parameters: " . _dump_params($params),
72 "Failed using these parameters: " . _dump_params($failed_param));
73 8 return ($ok, @diag);
74 }
75  
76  
77 245 ▶sub all_ok(&$;$) {
Subroutine all_ok: called CC=3 SCAR=11.0
Pod documented
78 245 my ($sub, $params, $test_name) = @_;
79 245 my ($ok, @diag) = does_all(@_);
80 244 ▶ $Tester->ok( $ok, $test_name ) or do { $Tester->diag($_) for @diag };
Branch: 1 branch
BranchTrueFalseTotal
$Tester->ok($ok, $test_name) or do { $Tester->diag($_) foreach (@diag) } 14 230 244
81 244 return $ok;
82 }
83  
84 10 ▶sub most_ok(&$$;$) {
Subroutine most_ok: called CC=3 SCAR=11.7
Pod documented
85 10 my ($sub, $params, $nr, $test_name) = @_;
86 10 my ($ok, @diag) = does_most(@_);
87 0 ▶ $Tester->ok( $ok, $test_name ) or do { $Tester->diag($_) for @diag };
Branch: 1 branch
BranchTrueFalseTotal
$Tester->ok($ok, $test_name) or do { $Tester->diag($_) foreach (@diag) } 8 0 8
88 8 return $ok;
89 }
90  
91  
92 249 ▶sub any_ok(&$;$) {
Subroutine any_ok: called CC=1 SCAR=0.0
Pod documented
93 249 my ($sub, $params, $test_name) = @_;
94
95 # Please recognise the logic
96 # To find out if any of the tests is O.K.,
97 # I ask whether all tests fail
98 # If so there isn't any_ok, otherwise there is at least one ok
99 249 ▶ my ($all_arent_ok) = does_all(sub {!$sub->(@_)}, $params, $test_name);
Subroutine __ANON__: called CC=1 SCAR=0.0
100 248 my $ok = !$all_arent_ok;
101 248 $Tester->ok( $ok, $test_name );
102 248 return $ok;
103 }
104  
105 250 ▶sub any_is(&$$;$) {
Subroutine any_is: called CC=3 SCAR=11.0
Pod documented
106 250 my ($sub, $expected_value, $params, $test_name) = @_;
107 my ($all_arent_ok, @diag) =
108 250 ▶ does_all(sub {!($sub->(@_) eq $expected_value)}, $params, $test_name);
Subroutine __ANON__: called CC=1 SCAR=0.0
109 248 my $ok = !$all_arent_ok;
110 $Tester->ok( $ok, $test_name)
111 248 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
$Tester->ok($ok, $test_name) or do { $Tester->diag($_) foreach (@diag); $Tester->diag('Expected: ' . _dump_params($expected_value)); $Tester->diag("but didn't found it with at least one parameter") } 240 8 248
112 8 $Tester->diag($_) for @diag;
113 8 $Tester->diag("Expected: " . _dump_params($expected_value));
114 8 $Tester->diag("but didn't found it with at least one parameter");
115 };
116 }
117  
118 246 ▶sub any_isnt(&$$;$) {
Subroutine any_isnt: called CC=3 SCAR=11.0
Pod documented
119 246 my ($sub, $expected_value, $params, $test_name) = @_;
120 my ($all_arent_ok, @diag) =
121 246 ▶ does_all(sub {!($sub->(@_) ne $expected_value)}, $params, $test_name);
Subroutine __ANON__: called CC=1 SCAR=0.0
122 246 my $ok = !$all_arent_ok;
123 $Tester->ok( $ok, $test_name)
124 246 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
$Tester->ok($ok, $test_name) or do { $Tester->diag($_) foreach (@diag); $Tester->diag('Expected to find any parameter where result is different to ' . _dump_params($expected_value)); $Tester->diag("but didn't found such parameters") } 238 8 246
125 8 $Tester->diag($_) for @diag;
126 8 $Tester->diag("Expected to find any parameter where result is different to " . _dump_params($expected_value));
127 8 $Tester->diag("but didn't found such parameters");
128 };
129 }
130  
131 245 ▶sub all_are(&$$;$) {
Subroutine all_are: called CC=3 SCAR=11.0
Pod documented
132 245 my ($sub, $expected, $params, $test_name) = @_;
133 245 my $found = undef;
134 my ($ok, @diag) =
135 245 ▶ does_all( sub { $found = $sub->(@_); $found eq $expected }, $params);
Subroutine __ANON__: called CC=1 SCAR=0.0
136 $Tester->ok($ok, $test_name)
137 244 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
$Tester->ok($ok, $test_name) or do { $Tester->diag($_) foreach (@diag); $Tester->diag('Expected: ' . _dump_params($expected)); $Tester->diag('but found: ' . _dump_params($found)) } 14 230 244
138 230 $Tester->diag($_) for @diag;
139 230 $Tester->diag("Expected: " . _dump_params($expected));
140 230 $Tester->diag("but found: " . _dump_params($found));
141 };
142 }
143  
144 245 ▶sub all_arent(&$$;$) {
Subroutine all_arent: called CC=3 SCAR=11.0
Pod documented
145 245 my ($sub, $unexpected, $params, $test_name) = @_;
146 245 my $found = undef;
147 my ($ok, @diag) =
148 245 ▶ does_all( sub { $found = $sub->(@_); $found ne $unexpected }, $params);
Subroutine __ANON__: called CC=1 SCAR=0.0
149 $Tester->ok($ok, $test_name)
150 244 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
$Tester->ok($ok, $test_name) or do { $Tester->diag($_) foreach (@diag); $Tester->diag('Expected not to find ' . _dump_params($unexpected) . ' but found it') } 14 230 244
151 230 $Tester->diag($_) for @diag;
152 230 $Tester->diag("Expected not to find " . _dump_params($unexpected) . " but found it");
153 };
154 }
155  
156 263 ▶sub _try_all_of_the_list {
Subroutine _try_all_of_the_list: called CC=3 SCAR=11.0
157 263 my ($sub, @param) = @_;
158 263 foreach my $p (@param) {
159 1397 local $_ = $p;
160 1397 ▶ $sub->($_) or return [$_];
Branch: 1 branch
BranchTrueFalseTotal
&$sub($_) or return [$_] 1167 230 1397
161 }
162 33 return undef;
163 }
164  
165 8 ▶sub _try_most_of_the_list {
Subroutine _try_most_of_the_list: called CC=3 SCAR=11.0
166 8 my ($sub,$nr,@param) = @_;
167 8 ▶ while ($nr-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nr-- > 0) 322 8 330
168 322 local $_ = $param[rand @param];
169 322 ▶ $sub->($_) or return [$_];
Branch: 1 branch
BranchTrueFalseTotal
&$sub($_) or return [$_] 322 0 322
170 }
171 8 return undef;
172 }
173  
174  
175 1211 ▶sub _try_all_of_xproduct {
Subroutine _try_all_of_xproduct: called CC=3 SCAR=11.0
176 1211 my ($sub, $iterator) = @_;
177 1211 my $tuple = undef;
178 1211 ▶ while ($tuple = $iterator->get()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tuple = $iterator->get) 45095 33 45128
179 45095 ▶ $sub->(@$tuple) or last;
Branch: 1 branch
BranchTrueFalseTotal
&$sub(@$tuple) or last 43917 1178 45095
180 }
181 1211 return $tuple;
182 }
183  
184 2 ▶sub _try_most_of_xproduct {
Subroutine _try_most_of_xproduct: called CC=3 SCAR=15.1
185 2 my ($sub, $nr, $iterator) = @_;
186 2 ▶ while ($nr-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nr-- > 0) 2 0 2
187 2 my $tuple = $iterator->random;
188 0 ▶ $sub->(@$tuple) or return $tuple;
Branch: 1 branch
BranchTrueFalseTotal
&$sub(@$tuple) or return $tuple 0 0 0
189 }
190 0 return undef;
191 }
192  
193 3528 ▶sub _dump_params {
Subroutine _dump_params: called CC=1 SCAR=0.0
194 3528 local $_ = Dumper($_[0]);
195 3528 s/\s+//gs; # remove all indents, but I didn't want to set
196 # $Data::Dumper::Indent as it could have global effects
197 3528 s/^.*? = //; # remove the variable name of the dumped output
198 3528 return $_;
199 }
200  
201 17 ▶sub import {
Subroutine import: called CC=5 SCAR=16.1
202 17 my @import_arg;
203 17 my $seed_now = time ^ $$; # default value for the seed
204 17 ▶ while (local $_ = shift @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (local $_ = shift @_) 21 16 37
205 21 ▶ /seed/ && do { $seed_now = shift();
Branch: 1 branch
BranchTrueFalseTotal
/seed/ and do { $seed_now = shift(); $seed_now =~ /^\d+$/ or die "The seed must be an integer" } or push @import_arg, $_ 3 17 20
Condition100%3/3
/seed/ and do { $seed_now = shift(); $seed_now =~ /^\d+$/ or die "The seed must be an integer" }
!ll&&!rl&&r
1713
MC/DC50%1/2
/seed/ and do { $seed_now = shift(); $seed_now =~ /^\d+$/ or die "The seed must be an integer" }
/seed/ do { $seed_now = shift(); $seed_now =~ /^\d+$/ or die "The seed must be an integer" }
Truth table66%2/3
A and B
A= /seed/B= do { $seed_now = shift(); $seed_now =~ /^\d+$/ or die "The seed must be an integer" }
ABresult
0X0
100
111
206 4 ▶ $seed_now =~ /^\d+$/ or die "The seed must be an integer";
Condition100%2/2
$seed_now =~ /^\d+$/ || die('The seed must be an integer')
l!l
31
MC/DC100%1/1
$seed_now =~ /^\d+$/ || die('The seed must be an integer')
$seed_now =~ /^\d+$/
Truth table100%2/2
A
A= $seed_now =~ /^\d+$/
Aresult
11
00
207 }
208 or "DEFAULT" && push @import_arg, $_;
209 }
210 16 srand($seed_now);
211 #Readonly::Scalar $seed => $seed_now;
212 16 $seed = $seed_now;
213 16 Test::ManyParams->export_to_level(1, @import_arg);
214 }
215  
216 11 1;
217  
218 __END__
↑ Summary