lib/VSGDR/UnitTest/reportGDRTests.pl untested

filter: Statementstmt 0.0% 0 / 388 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 45 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 531 SCAR 6.9
CC
1
Cov
0%
END
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/bin/perl
2  
3 use Modern::Perl;
4 use strict;
5 use warnings;
6  
7 use autodie qw(:all);
8 use version ; our $VERSION = qv('1.0.5');
9  
10 use Carp;
11  
12 our $opt_infile;
13 our $opt_warnings;
14 our $opt_disabled;
15 our $opt_reverse;
16 our $opt_totals;
17  
18  
19 use Getopt::Euclid qw( :vars<opt_> );
20 use List::MoreUtils qw{firstidx} ;
21  
22 use Array::Diff ;
23 use List::Util qw(first max maxstr min minstr reduce shuffle sum);
24 use List::MoreUtils qw( uniq none) ;
25  
26 use VSGDR::UnitTest::TestSet::Test;
27 use VSGDR::UnitTest::TestSet::Test::TestCondition;
28 use VSGDR::UnitTest::TestSet::Representation ;
29  
30 use Smart::Comments;
31  
32 use Data::Dumper;
33  
34 my $warningsLevel = 0 ;
35 $warningsLevel = $opt_warnings if ($opt_warnings) ;
36 my $includeDisabledConditions = 0 ;
37 $includeDisabledConditions = $opt_disabled if ($opt_disabled) ;
38  
39 if ($opt_reverse) {$opt_totals = 0 } ;
40  
41 croak 'no input file' unless defined($opt_infile) ;
42  
43 my %ValidParserMakeArgs = ( vb => "NET::VB"
44 , cs => "NET::CS"
45 , xls => "XLS"
46 , xml => "XML"
47 ) ;
48 my %ValidParserMakeArgs2 = ( vb => "NET2::VB"
49 , cs => "NET2::CS"
50 ) ;
51  
52 my %Parsers = () ;
53  
54 # #########################
55  
56  
57 my @testNames = () ;
58 my $tests_count = 0 ;
59 my $conditions_count = 0 ;
60 my $disabled_conditions_count = 0 ;
61 my $scalar_conditions_count = 0 ;
62 my $disabled_scalar_conditions_count = 0 ;
63  
64  
65 for my $testFile ( split /[,\s]/, $opt_infile ) {
66  
67 my %resSets = () ;
68 my %resSetBoundaries = () ;
69  
70 my %scalarConditionsByTestAndResultSetEtc = () ;
71 my %rowCountConditionsByTestAndResultSetEtc = () ;
72 my %resSetEmptyEtc = () ;
73 my %resSetNotEmptyEtc = () ;
74  
75 (my $insfx = $testFile) =~ s/^.*\.//g;
76 croak 'Invalid input file' unless defined $insfx ;
77 $insfx = lc $insfx ;
78  
79 die 'Invalid input file $testFile' unless exists $ValidParserMakeArgs{$insfx} ;
80  
81 $Parsers{${insfx}} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs{${insfx}} } )
82 if not exists $Parsers{${insfx}} ;
83 # if input is in a .net language, add in a .net2 parser to the list
84 if ( firstidx { $_ eq ${insfx} } ['cs','vb'] != -1 ) {
85 $Parsers{"${insfx}2"} = VSGDR::UnitTest::TestSet::Representation->make( { TYPE => $ValidParserMakeArgs2{${insfx}} } )
86 if not exists $Parsers{"${insfx}2"} ;
87 }
88  
89 my $testSet = undef ;
90 eval {
91 $testSet = $Parsers{$insfx}->deserialise($testFile);
92 } ;
93 if ( not defined $testSet ) {
94 if ( exists $Parsers{"${insfx}2"}) {
95 eval {
96 $testSet = $Parsers{"${insfx}2"}->deserialise($testFile);
97 }
98 }
99 else {
100 croak 'Parsing failed.';
101 }
102 }
103  
104  
105 my $ra_tests = $testSet->tests() ;
106 @testNames = map { $_->testName() } @{$ra_tests} ;
107  
108 $tests_count += @testNames ;
109  
110  
111 if ( ! $opt_reverse ) {
112 say "";
113 say "Test file: ${testFile}";
114 }
115 else {
116 }
117  
118 my $test_No = 0 ;
119 for my $test (@$ra_tests) {
120  
121  
122 $test_No++ ;
123  
124 my $ra_preTestConditions = $test->preTest_conditions() ;
125 my $ra_testConditions = $test->test_conditions() ;
126 my $ra_postTestConditions = $test->postTest_conditions() ;
127  
128 my @scalarTestConditionNames = map { $_->conditionName() } grep { $_->conditionEnabled() or $includeDisabledConditions } @{$ra_testConditions} ;
129 my @scalarPreTestConditionNames = map { $_->conditionName() } grep { $_->conditionEnabled() or $includeDisabledConditions } @{$ra_preTestConditions} ;
130 my @scalarPostTestConditionNames = map { $_->conditionName() } grep { $_->conditionEnabled() or $includeDisabledConditions } @{$ra_postTestConditions} ;
131  
132 #warn Dumper @{$ra_testConditions} ;
133  
134 map { push @{$resSets{$test->testName()}}, $_->conditionResultSet() }
135 @{$ra_testConditions} ;
136  
137 map { push @{$resSetBoundaries{$test->testName()}->{$_->conditionResultSet()}->{MAXROW}}, $_->conditionRowNumber() ;
138 push @{$resSetBoundaries{$test->testName()}->{$_->conditionResultSet()}->{MAXCOL}}, $_->conditionColumnNumber() ;
139 }
140 grep { $_->conditionISEnabled() }
141 grep { $_->testConditionType() eq 'ScalarValue' }
142 ( @{$ra_testConditions}, @{$ra_preTestConditions}, @{$ra_postTestConditions} ) ;
143  
144 map { # only add this info from rowcount assertion if we have a scalar value test for this resultset
145 if ( exists ($resSetBoundaries{$test->testName()}->{$_->conditionResultSet()}) ) {
146 push @{$resSetBoundaries{$test->testName()}->{$_->conditionResultSet()}->{MAXROW}}, $_->conditionRowCount() ;
147 }
148 }
149 grep { $_->conditionISEnabled() }
150 grep { $_->testConditionType() eq 'RowCount' }
151 ( @{$ra_testConditions}, @{$ra_preTestConditions}, @{$ra_postTestConditions} ) ;
152  
153 map { push @{$scalarConditionsByTestAndResultSetEtc{${test}->testName()}->{$_->conditionResultSet()}->{$_->conditionRowNumber()}->{$_->conditionColumnNumber()}}
154 , $_->conditionName() }
155 grep { $_->conditionISEnabled() }
156 grep { $_->testConditionType() eq 'ScalarValue' }
157 ( @{$ra_testConditions}, @{$ra_preTestConditions}, @{$ra_postTestConditions} ) ;
158  
159 map { push @{$rowCountConditionsByTestAndResultSetEtc{${test}->testName()}->{$_->conditionResultSet()}}
160 , $_->conditionName() }
161 grep { $_->conditionISEnabled() }
162 grep { $_->testConditionType() eq 'RowCount' }
163 ( @{$ra_testConditions}, @{$ra_preTestConditions}, @{$ra_postTestConditions} ) ;
164  
165 map { push @{$resSetEmptyEtc{${test}->testName()}->{$_->conditionResultSet()}}
166 , $_->conditionName() }
167 grep { $_->conditionISEnabled() }
168 grep { $_->testConditionType() eq 'EmptyResultSet' }
169 ( @{$ra_testConditions}, @{$ra_preTestConditions}, @{$ra_postTestConditions} ) ;
170  
171 map { push @{$resSetNotEmptyEtc{${test}->testName()}->{$_->conditionResultSet()}}
172 , $_->conditionName() }
173 grep { $_->conditionISEnabled() }
174 grep { $_->testConditionType() eq 'NotEmptyResultSet' }
175 ( @{$ra_testConditions}, @{$ra_preTestConditions}, @{$ra_postTestConditions} ) ;
176  
177  
178  
179  
180  
181 if ( ! $opt_reverse ) {
182  
183 {
184 local $" = "\n\t\t\t\t";
185 say "\tTest: @{[$test->testName()]}";
186 say "\t\tPre Conditions: @scalarPreTestConditionNames" if @scalarPreTestConditionNames ;
187 say "\t\t Conditions: @scalarTestConditionNames" if @scalarTestConditionNames ;
188 say "\t\tPostConditions: @scalarPostTestConditionNames" if @scalarPostTestConditionNames ;
189 }
190 }
191 else {
192 say "@{[$test->testName()]}: ${testFile}";
193 }
194  
195 $conditions_count += scalar @{$ra_preTestConditions} ;
196 $conditions_count += scalar @{$ra_testConditions} ;
197 $conditions_count += scalar @{$ra_postTestConditions} ;
198  
199 my @scalarTestConditions = grep { $_->testConditionType() eq 'ScalarValue' } @{$ra_testConditions} ;
200 my @scalarPreTestConditions = grep { $_->testConditionType() eq 'ScalarValue' } @{$ra_preTestConditions} ;
201 my @scalarPostTestConditions = grep { $_->testConditionType() eq 'ScalarValue' } @{$ra_postTestConditions} ;
202  
203 $scalar_conditions_count += @scalarTestConditions ;
204 $scalar_conditions_count += @scalarPreTestConditions ;
205 $scalar_conditions_count += @scalarPostTestConditions ;
206  
207 my @disabledTestConditions = grep { not $_->conditionISEnabled() } @{$ra_testConditions} ;
208 my @disabledPreTestConditions = grep { not $_->conditionISEnabled() } @{$ra_preTestConditions} ;
209 my @disabledPostTestConditions = grep { not $_->conditionISEnabled() } @{$ra_postTestConditions} ;
210  
211 $disabled_conditions_count += @disabledTestConditions ;
212 $disabled_conditions_count += @disabledPreTestConditions ;
213 $disabled_conditions_count += @disabledPostTestConditions ;
214  
215 my @disabledscalarTestConditions = grep { not $_->conditionISEnabled() and $_->testConditionType() eq 'ScalarValue' } @{$ra_testConditions} ;
216 my @disabledscalarPreTestConditions = grep { not $_->conditionISEnabled() and $_->testConditionType() eq 'ScalarValue' } @{$ra_preTestConditions} ;
217 my @disabledscalarPostTestConditions = grep { not $_->conditionISEnabled() and $_->testConditionType() eq 'ScalarValue' } @{$ra_postTestConditions} ;
218  
219 $disabled_scalar_conditions_count += @disabledscalarTestConditions ;
220 $disabled_scalar_conditions_count += @disabledscalarPreTestConditions ;
221 $disabled_scalar_conditions_count += @disabledscalarPostTestConditions ;
222  
223 }
224  
225 #warn Dumper %scalarConditionsByTestAndResultSetEtc;
226 #exit;
227  
228 #warn Dumper %resSetBoundaries ;
229 foreach my $test (keys %resSetBoundaries) {
230 foreach my $RS (keys %{$resSetBoundaries{$test}}) {
231  
232 my @rowVals = uniq(sort(@{$resSetBoundaries{$test}->{$RS}->{MAXROW}})) ;
233 my @colVals = uniq(sort(@{$resSetBoundaries{$test}->{$RS}->{MAXCOL}})) ;
234  
235 my $rowVal= max @rowVals;
236 my $colVal= max @colVals;
237  
238 $resSetBoundaries{$test}->{$RS}->{MAXROW} = $rowVal ;
239 $resSetBoundaries{$test}->{$RS}->{MAXCOL} = $colVal ;
240  
241 }}
242  
243  
244  
245  
246 if ( $warningsLevel >= 1 ) {
247 foreach my $test (keys %scalarConditionsByTestAndResultSetEtc) {
248 foreach my $RS (sort keys %{$scalarConditionsByTestAndResultSetEtc{$test}}) {
249 foreach my $row (sort keys %{$scalarConditionsByTestAndResultSetEtc{$test}{$RS}}) {
250 foreach my $col (sort keys %{$scalarConditionsByTestAndResultSetEtc{$test}{$RS}{$row}}) {
251 if (scalar(@{$scalarConditionsByTestAndResultSetEtc{$test}{$RS}{$row}{$col}}) > 1 ) {
252 local $" = ", ";
253 warn "Test:${test} ResultSet:${RS} Row:${row} Col:${col} has multiple conditions specified:-\n";
254 warn "\t@{$scalarConditionsByTestAndResultSetEtc{$test}{$RS}{$row}{$col}}\n";
255 }
256 }}}}
257  
258  
259 foreach my $test (keys %rowCountConditionsByTestAndResultSetEtc) {
260 foreach my $RS (sort keys %{$rowCountConditionsByTestAndResultSetEtc{$test}}) {
261 if (scalar(@{$rowCountConditionsByTestAndResultSetEtc{$test}{$RS}}) > 1 ) {
262 local $" = ", ";
263 warn "Test:${test} ResultSet:${RS} has multiple RowCount conditions specified:-\n";
264 warn "\t@{$rowCountConditionsByTestAndResultSetEtc{$test}{$RS}}\n";
265 }
266 # check we haven't overlapped with a less stringent test on rowset size
267 if (exists $resSetEmptyEtc{$test}->{$RS} or
268 exists $resSetNotEmptyEtc{$test}->{$RS}
269 ) {
270 warn "Test:${test} ResultSet:${RS} has a redundant empty or non-empty ResultSet test:-\n";
271 warn "\t$resSetEmptyEtc{$test}->{$RS}\n"
272 if exists $resSetEmptyEtc{$test}->{$RS};
273 warn "\t$resSetNotEmptyEtc{$test}->{$RS}\n"
274 if exists $resSetNotEmptyEtc{$test}->{$RS};
275 }
276 }}
277  
278 foreach my $test (keys %resSetEmptyEtc) {
279 foreach my $RS (sort keys %{$resSetEmptyEtc{$test}}) {
280 if (scalar(@{$resSetEmptyEtc{$test}{$RS}}) > 1 ) {
281 local $" = ", ";
282 warn "Test:${test} ResultSet:${RS} has multiple EmptyResultSet conditions specified:-\n";
283 warn "\t@{$resSetEmptyEtc{$test}{$RS}}\n";
284 }
285 }}
286  
287 foreach my $test (keys %resSetNotEmptyEtc) {
288 foreach my $RS (sort keys %{$resSetNotEmptyEtc{$test}}) {
289 if (scalar(@{$resSetNotEmptyEtc{$test}{$RS}}) > 1 ) {
290 local $" = ", ";
291 warn "Test:${test} ResultSet:${RS} has multiple NonEmptyResultSetconditions specified:-\n";
292 warn "\t@{$resSetNotEmptyEtc{$test}{$RS}}\n";
293 }
294 }}
295 }
296  
297  
298 if ( $warningsLevel >= 1 ) {
299  
300 foreach my $test (keys %resSets) {
301  
302 my @vals = uniq( sort { $a <=> $b } (@{$resSets{$test}}) ) ; # sort strings numerically, not alphabetically
303 #warn Dumper @vals;
304 my $MinKey= min @vals;
305 my $MaxKey= max @vals;
306  
307 my @range = 1 .. $MaxKey ;
308  
309 map { $_ = "$_" } @range ; # stringify sorted range numbers for comparison
310 #warn Dumper @range;
311 my $diff = Array::Diff->diff( \@vals, \@range );
312 #warn Dumper $diff ;
313 if ( ( $diff->count != 0) or ( $MinKey > 1) ) {
314 local $" = ", ";
315 my $added = $diff->added ;
316 warn "Test:${test} does not have tests to cover all ResultSets, gaps exist for these ResultSets:-\n";
317 warn "\t@{$added}\n";
318  
319 } ;
320 }
321 }
322  
323 my %missingScalarAssertions = () ;
324  
325 foreach my $test (keys %scalarConditionsByTestAndResultSetEtc) {
326 foreach my $RS (keys %{$scalarConditionsByTestAndResultSetEtc{$test}}) {
327 foreach my $row ( 1 .. $resSetBoundaries{$test}->{$RS}->{MAXROW} ) {
328 foreach my $col ( 1 .. $resSetBoundaries{$test}->{$RS}->{MAXCOL} ) {
329 if ( ( not exists ( $scalarConditionsByTestAndResultSetEtc{$test}->{$RS}->{$row}->{$col} ) )
330 or ( exists ( $scalarConditionsByTestAndResultSetEtc{$test}->{$RS}->{$row}->{$col} )
331 and scalar(@{$scalarConditionsByTestAndResultSetEtc{$test}->{$RS}->{$row}->{$col}}) < 1
332 )
333 ) {
334 $missingScalarAssertions{$test}->{$RS}->{ORIG}->[$row][$col] = 1 ;
335 }
336 else {
337 $missingScalarAssertions{$test}->{$RS}->{ORIG}->[$row][$col] = 0 ;
338 }
339 }}
340  
341 my @origTable = @{$missingScalarAssertions{$test}->{$RS}->{ORIG}} ;
342 my @pivottedTable = () ;
343  
344 for ( my $r = 1; $r <= $#origTable ; $r++ ) {
345 for ( my $c = 1; $c <= $#{$origTable[$r]} ; $c++ ) {
346 $pivottedTable[$c][$r] = $origTable[$r][$c] ;
347 }
348 }
349 $missingScalarAssertions{$test}->{$RS}->{PIVOTTED} = \@pivottedTable ;
350 }}
351  
352 if ( $warningsLevel >= 2 ) {
353  
354 foreach my $test (keys %missingScalarAssertions) {
355 foreach my $RS ( sort keys %{$missingScalarAssertions{$test}}) {
356  
357 my @Rows = @{$missingScalarAssertions{$test}->{$RS}->{ORIG}} ;
358 my @Cols = @{$missingScalarAssertions{$test}->{$RS}->{PIVOTTED}} ;
359  
360 my @allBlankRows = () ;
361 my @allBlankCols = () ;
362  
363  
364 for (my $i = 1; $i <= $#Rows; $i++) {
365 push @allBlankRows, $i
366 if none { $_ == 0 } grep {defined $_} @{$Rows[$i]} ;
367 }
368 for (my $i = 1; $i <= $#Cols; $i++) {
369 push @allBlankCols, $i
370 if none { $_ == 0 } grep {defined $_} @{$Cols[$i]} ;
371 }
372  
373 if ( $resSetBoundaries{$test}->{$RS}->{MAXROW} > 1 and $resSetBoundaries{$test}->{$RS}->{MAXCOL} > 1 ) {
374 local $" = "," ;
375 warn "Test:${test} ResultSet:${RS} has rows/columns with no scalar conditions:-\n"
376 if scalar @allBlankRows or scalar @allBlankCols ;
377 warn "\tRows :- @allBlankRows\n" if scalar @allBlankRows ;
378 warn "\tCols :- @allBlankCols\n" if scalar @allBlankCols ;
379 }
380 for ( my $row = 1; $row <= $resSetBoundaries{$test}->{$RS}->{MAXROW}; $row++ ) {
381 local $" = "," ;
382 # skip if there is more than one column and all the values in the row are missing
383 # as they've been reported above
384 next if $resSetBoundaries{$test}->{$RS}->{MAXCOL} > 1 and grep { $row } @allBlankRows ;
385 my @blankCols = () ;
386 for ( my $col = 1; $col <= $resSetBoundaries{$test}->{$RS}->{MAXCOL}; $col++ ) {
387 # skip if there is more than one row and all the values in the column are missing
388 # as they've been reported above
389 next if $resSetBoundaries{$test}->{$RS}->{MAXROW} > 1 and grep { $col } @allBlankCols ;
390 push @blankCols, $col
391 if exists $missingScalarAssertions{$test}->{$RS}->{ORIG}
392 and $missingScalarAssertions{$test}->{$RS}->{ORIG}[$row][$col] == 1 ;
393  
394 }
395 if ( scalar @blankCols ) {
396 warn "Test:${test} ResultSet:${RS} row:$row has no scalar conditions in columns :-\n";
397 warn "\t@blankCols\n";
398 }
399 }
400  
401 }}
402 }
403  
404 }
405  
406 if ( $opt_totals ) {
407 say "";
408 say "Total tests = ${tests_count}";
409 say "Total test conditions = ${conditions_count}";
410 say "Total disabled test conditions = ${disabled_conditions_count}";
411 say "Total scalar value test conditions = ${scalar_conditions_count}";
412 say "Total disabled scalar value test conditions = ${disabled_scalar_conditions_count}";
413 }
414  
415 exit ;
416  
417 END {}
418  
419  
420 __END__