blib/lib/TAP/Parser/Aggregator.pm

filter: Statementstmt 68.8% 93 / 135 Branchbran 52.2% 23 / 44 Conditioncond 68.4% 26 / 38 MC/DCmcdc 45.0% 9 / 20 Subroutinesub 77.4% 24 / 31 Podpod 78.9% 15 / 19 totaltotal 66.2% 190 / 287 CC 52 SCAR 15.8
CC
52
Cov
65%
add
14.6
add
14.6
_get_parsers
6.0
CRAP 4.8
« blib/lib/TAP/Parser/Iterator/Process.pm ↑ Summary blib/lib/TAP/Formatter/Color.pm »
1 package TAP::Parser::Aggregator;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Benchmark;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶use base 'TAP::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 TAP::Parser::Aggregator - Aggregate TAP::Parser results
12  
13 =head1 VERSION
14  
15 Version 3.52
16  
17 =cut
18  
19 18 our $VERSION = '3.52';
20  
21 =head1 SYNOPSIS
22  
23 use TAP::Parser::Aggregator;
24  
25 my $aggregate = TAP::Parser::Aggregator->new;
26 $aggregate->add( 't/00-load.t', $load_parser );
27 $aggregate->add( 't/10-lex.t', $lex_parser );
28  
29 my $summary = <<'END_SUMMARY';
30 Passed: %s
31 Failed: %s
32 Unexpectedly succeeded: %s
33 END_SUMMARY
34 printf $summary,
35 scalar $aggregate->passed,
36 scalar $aggregate->failed,
37 scalar $aggregate->todo_passed;
38  
39 =head1 DESCRIPTION
40  
41 C<TAP::Parser::Aggregator> collects parser objects and allows
42 reporting/querying their aggregate results.
43  
44 =head1 METHODS
45  
46 =head2 Class Methods
47  
48 =head3 C<new>
49  
50 my $aggregate = TAP::Parser::Aggregator->new;
51  
52 Returns a new C<TAP::Parser::Aggregator> object.
53  
54 =cut
55  
56 # new() implementation supplied by TAP::Object
57  
58 18 my %SUMMARY_METHOD_FOR;
59  
60 18 ▶BEGIN { # install summary methods
Subroutine BEGIN: called CC=3 SCAR=11.3
61 18 %SUMMARY_METHOD_FOR = map { $_ => $_ } qw(
62 failed
63 parse_errors
64 passed
65 skipped
66 todo
67 todo_passed
68 total
69 wait
70 exit
71 );
72 18 $SUMMARY_METHOD_FOR{total} = 'tests_run';
73 18 $SUMMARY_METHOD_FOR{planned} = 'tests_planned';
74  
75 18 for my $method ( keys %SUMMARY_METHOD_FOR ) {
76 180 ▶ next if 'total' eq $method;
Branch: 1 branch
BranchTrueFalseTotal
if "total" eq $method 18 162 180
77 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
78 535 ▶ *$method = sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
79 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=9.2
80 return wantarray
81 0 ? @{ $self->{"descriptions_for_$method"} }
82 0 ▶ : $self->{$method};
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 8 527 535
wantarray ? : 0 0 0
83 162 };
84 }
85 } # end install summary methods
86  
87 81 ▶sub _initialize {
Subroutine _initialize: called CC=3 SCAR=14.2
88 0 ▶ my ($self) = @_;
Subroutine _initialize: not called CC=3 SCAR=14.2
89 0 $self->{parser_for} = {};
90 0 $self->{parse_order} = [];
91 0 for my $summary ( keys %SUMMARY_METHOD_FOR ) {
92 0 $self->{$summary} = 0;
93 0 ▶ next if 'total' eq $summary;
Branch: 2 branches
BranchTrueFalseTotal
if "total" eq $summary 81 729 810
if "total" eq $summary 0 0 0
94 0 $self->{"descriptions_for_$summary"} = [];
95 }
96 0 return $self;
97 }
98  
99 ##############################################################################
100  
101 =head2 Instance Methods
102  
103 =head3 C<add>
104  
105 $aggregate->add( $description => $parser );
106  
107 The C<$description> is usually a test file name (but only by
108 convention.) It is used as a unique identifier (see e.g.
109 L<"parsers">.) Reusing a description is a fatal error.
110  
111 The C<$parser> is a L<TAP::Parser|TAP::Parser> object.
112  
113 =cut
114  
115 111 ▶sub add {
Subroutine add: called CC=7 SCAR=26.8
Pod documented
116 0 ▶ my ( $self, $description, $parser ) = @_;
Subroutine add: not called CC=7 SCAR=26.8
Pod documented
117 0 ▶ if ( exists $self->{parser_for}{$description} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{"parser_for"}{$description}) 1 110 111
if (exists $self->{"parser_for"}{$description}) 0 0 0
118 0 $self->_croak( "You already have a parser for ($description)."
119 . " Perhaps you have run the same test twice." );
120 }
121 0 push @{ $self->{parse_order} } => $description;
122 0 $self->{parser_for}{$description} = $parser;
123  
124 0 ▶ while ( my ( $summary, $method ) = each %SUMMARY_METHOD_FOR ) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = each %SUMMARY_METHOD_FOR) 1100 110 1210
if (() = each %SUMMARY_METHOD_FOR) 0 0 0
125  
126 # Slightly nasty. Instead we should maybe have 'cooked' accessors
127 # for results that may be masked by the parser.
128 next
129 0 ▶ if ( $method eq 'exit' || $method eq 'wait' )
Branch: 2 branches
BranchTrueFalseTotal
if $method eq "exit" || $method eq "wait" and $parser->ignore_exit 0 1100 1100
if $method eq "exit" || $method eq "wait" and $parser->ignore_exit 0 0 0
Condition41%5/12
$method eq "exit" || $method eq "wait" and $parser->ignore_exit
!ll&&!rl&&r
8802200
$method eq "exit" or $method eq "wait"
l!l&&r!l&&!r
110110880
$method eq "exit" || $method eq "wait" and $parser->ignore_exit
!ll&&!rl&&r
000
$method eq "exit" or $method eq "wait"
l!l&&r!l&&!r
000
MC/DC0%0/3
$method eq "exit" || $method eq "wait" and $parser->ignore_exit
$method eq "exit" $method eq "wait" $parser->ignore_exit
MC/DC0%0/3
$method eq "exit" || $method eq "wait" and $parser->ignore_exit
$method eq "exit" $method eq "wait" $parser->ignore_exit
Truth table60%3/5
A or B and C
A= $method eq "exit"B= $method eq "wait"C= $parser->ignore_exit
ABCresult
00X0
1X00
0100
1X11
0111
Truth table0%0/5
A or B and C
A= $method eq "exit"B= $method eq "wait"C= $parser->ignore_exit
ABCresult
00X0
1X00
0100
1X11
0111
130 && $parser->ignore_exit;
131  
132 0 ▶ if ( my $count = $parser->$method() ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $count = $parser->$method) 377 723 1100
if (my $count = $parser->$method) 0 0 0
133 0 $self->{$summary} += $count;
134 0 push @{ $self->{"descriptions_for_$summary"} } => $description;
135 }
136 }
137  
138 0 return $self;
139 }
140  
141 ##############################################################################
142  
143 =head3 C<parsers>
144  
145 my $count = $aggregate->parsers;
146 my @parsers = $aggregate->parsers;
147 my @parsers = $aggregate->parsers(@descriptions);
148  
149 In scalar context without arguments, this method returns the number of parsers
150 aggregated. In list context without arguments, returns the parsers in the
151 order they were added.
152  
153 If C<@descriptions> is given, these correspond to the keys used in each
154 call to the add() method. Returns an array of the requested parsers (in
155 the requested order) in list context or an array reference in scalar
156 context.
157  
158 Requesting an unknown identifier is a fatal error.
159  
160 =cut
161  
162 80 ▶sub parsers {
Subroutine parsers: called CC=2 SCAR=9.2
Pod documented
163 0 ▶ my $self = shift;
Subroutine parsers: not called CC=2 SCAR=9.2
Pod documented
164 0 ▶ return $self->_get_parsers(@_) if @_;
Branch: 2 branches
BranchTrueFalseTotal
if @_ 74 6 80
if @_ 0 0 0
165 0 my $descriptions = $self->{parse_order};
166 0 my @parsers = @{ $self->{parser_for} }{@$descriptions};
167  
168 # Note: Because of the way context works, we must assign the parsers to
169 # the @parsers array or else this method does not work as documented.
170 0 return @parsers;
171 }
172  
173 76 ▶sub _get_parsers {
Subroutine _get_parsers: called CC=4 SCAR=17.9
174 0 ▶ my ( $self, @descriptions ) = @_;
Subroutine _get_parsers: not called CC=4 SCAR=17.9
175 0 my @parsers;
176 0 for my $description (@descriptions) {
177 $self->_croak("A parser for ($description) could not be found")
178 0 ▶ unless exists $self->{parser_for}{$description};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $self->{"parser_for"}{$description} 1 77 78
unless exists $self->{"parser_for"}{$description} 0 0 0
179 0 push @parsers => $self->{parser_for}{$description};
180 }
181 0 ▶ return wantarray ? @parsers : \@parsers;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 74 1 75
wantarray ? : 0 0 0
182 }
183  
184 2 ▶sub total_files {
Subroutine total_files: called CC=1 SCAR=1.2
Pod undocumented
185 0 ▶ my $self = shift;
Subroutine total_files: not called CC=1 SCAR=1.2
Pod undocumented
186 0 return scalar $self->parsers;
187 }
188  
189 2 ▶sub failed_files {
Subroutine failed_files: called CC=1 SCAR=1.2
Pod undocumented
190 0 ▶ my $self = shift;
Subroutine failed_files: not called CC=1 SCAR=1.2
Pod undocumented
191 0 my $num_bad = grep { $_->has_problems } $self->parsers;
192 0 return $num_bad;
193 }
194  
195 =head3 C<descriptions>
196  
197 Get an array of descriptions in the order in which they were added to
198 the aggregator.
199  
200 =cut
201  
202 70 ▶sub descriptions { @{ shift->{parse_order} || [] } }
Subroutine descriptions: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
+(shift())->{'parse_order'} || []
l!l
700
MC/DC0%0/1
+(shift())->{'parse_order'} || []
+(shift())->{'parse_order'}
Truth table50%1/2
A
A= +(shift())->{'parse_order'}
Aresult
11
00
203  
204 =head3 C<start>
205  
206 Call C<start> immediately before adding any results to the aggregator.
207 Among other times it records the start time for the test run.
208  
209 =cut
210  
211 76 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
212 76 my $self = shift;
213 76 $self->{start_time} = Benchmark->new;
214 }
215  
216 =head3 C<stop>
217  
218 Call C<stop> immediately after adding all test results to the aggregator.
219  
220 =cut
221  
222 76 ▶sub stop {
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
223 76 my $self = shift;
224 76 $self->{end_time} = Benchmark->new;
225 }
226  
227 =head3 C<elapsed>
228  
229 Elapsed returns a L<Benchmark> object that represents the running time
230 of the aggregated tests. In order for C<elapsed> to be valid you must
231 call C<start> before running the tests and C<stop> immediately
232 afterwards.
233  
234 =cut
235  
236 70 ▶sub elapsed {
Subroutine elapsed: called CC=3 SCAR=12.0
Pod documented
237 70 my $self = shift;
238  
239 70 require Carp;
240 Carp::croak
241 q{Can't call elapsed without first calling start and then stop}
242 70 ▶ unless defined $self->{start_time} && defined $self->{end_time};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"start_time"} and defined $self->{"end_time"} 0 70 70
Condition33%1/3
defined $self->{"start_time"} and defined $self->{"end_time"}
!ll&&!rl&&r
0070
MC/DC0%0/2
defined $self->{"start_time"} and defined $self->{"end_time"}
defined $self->{"start_time"} defined $self->{"end_time"}
Truth table33%1/3
A and B
A= defined $self->{"start_time"}B= defined $self->{"end_time"}
ABresult
0X0
100
111
243 70 return timediff( $self->{end_time}, $self->{start_time} );
244 }
245  
246 =head3 C<elapsed_timestr>
247  
248 Returns a formatted string representing the runtime returned by
249 C<elapsed()>. This lets the caller not worry about Benchmark.
250  
251 =cut
252  
253 39 ▶sub elapsed_timestr {
Subroutine elapsed_timestr: called CC=1 SCAR=0.0
Pod documented
254 39 my $self = shift;
255  
256 39 my $elapsed = $self->elapsed;
257  
258 39 return timestr($elapsed);
259 }
260  
261 =head3 C<all_passed>
262  
263 Return true if all the tests passed and no parse errors were detected.
264  
265 =cut
266  
267 41 ▶sub all_passed {
Subroutine all_passed: called CC=3 SCAR=11.0
Pod documented
268 41 my $self = shift;
269 return
270 41 ▶ $self->total
Condition83%5/6
$self->total && $self->total == $self->passed && !$self->has_errors
!ll&&!rl&&r
16025
$self->total and $self->total == $self->passed
!ll&&!rl&&r
31325
MC/DC66%2/3
$self->total && $self->total == $self->passed && !$self->has_errors
$self->total $self->total == $self->passed !$self->has_errors
Truth table75%3/4
A and B && not(C)
A= $self->totalB= $self->total == $self->passedC= !$self->has_errors
ABCresult
0XX0
10X0
1100
1111
271 && $self->total == $self->passed
272 && !$self->has_errors;
273 }
274  
275 =head3 C<get_status>
276  
277 Get a single word describing the status of the aggregated tests.
278 Depending on the outcome of the tests returns 'PASS', 'FAIL' or
279 'NOTESTS'. This token is understood by L<CPAN::Reporter>.
280  
281 =cut
282  
283 41 ▶sub get_status {
Subroutine get_status: called CC=4 SCAR=14.1
Pod documented
284 41 my $self = shift;
285  
286 41 my $total = $self->total;
287 41 my $passed = $self->passed;
288  
289 return
290 41 ▶ ( $self->has_errors || $total != $passed ) ? 'FAIL'
Branch: 2 branches
BranchTrueFalseTotal
$self->has_errors || $total != $passed ? : 16 25 41
$total ? : 25 0 25
Condition66%2/3
$self->has_errors or $total != $passed
l!l&&r!l&&!r
16025
MC/DC50%1/2
$self->has_errors or $total != $passed
$self->has_errors $total != $passed
Truth table66%2/3
A or B
A= $self->has_errorsB= $total != $passed
ABresult
1X1
011
000
291 : $total ? 'PASS'
292 : 'NOTESTS';
293 }
294  
295 ##############################################################################
296  
297 =head2 Summary methods
298  
299 Each of the following methods will return the total number of corresponding
300 tests if called in scalar context. If called in list context, returns the
301 descriptions of the parsers which contain the corresponding tests (see C<add>
302 for an explanation of description.
303  
304 =over 4
305  
306 =item * failed
307  
308 =item * parse_errors
309  
310 =item * passed
311  
312 =item * planned
313  
314 =item * skipped
315  
316 =item * todo
317  
318 =item * todo_passed
319  
320 =item * wait
321  
322 =item * exit
323  
324 =back
325  
326 For example, to find out how many tests unexpectedly succeeded (TODO tests
327 which passed when they shouldn't):
328  
329 my $count = $aggregate->todo_passed;
330 my @descriptions = $aggregate->todo_passed;
331  
332 Note that C<wait> and C<exit> are the totals of the wait and exit
333 statuses of each of the tests. These values are totalled only to provide
334 a true value if any of them are non-zero.
335  
336 =cut
337  
338 ##############################################################################
339  
340 =head3 C<total>
341  
342 my $tests_run = $aggregate->total;
343  
344 Returns the total number of tests run.
345  
346 =cut
347  
348 164 ▶sub total { shift->{total} }
Subroutine total: called CC=1 SCAR=0.0
Pod documented
349  
350 ##############################################################################
351  
352 =head3 C<has_problems>
353  
354 if ( $parser->has_problems ) {
355 ...
356 }
357  
358 Identical to C<has_errors>, but also returns true if any TODO tests
359 unexpectedly succeeded. This is more akin to "warnings".
360  
361 =cut
362  
363 32 ▶sub has_problems {
Subroutine has_problems: called CC=2 SCAR=6.9
Pod documented
364 32 my $self = shift;
365 32 ▶ return $self->todo_passed
Condition100%3/3
$self->todo_passed || $self->has_errors
l!l&&r!l&&!r
3623
MC/DC100%2/2
$self->todo_passed || $self->has_errors
$self->todo_passed $self->has_errors
Truth table100%3/3
A || B
A= $self->todo_passedB= $self->has_errors
ABresult
1X1
011
000
366 || $self->has_errors;
367 }
368  
369 ##############################################################################
370  
371 =head3 C<has_errors>
372  
373 if ( $parser->has_errors ) {
374 ...
375 }
376  
377 Returns true if I<any> of the parsers failed. This includes:
378  
379 =over 4
380  
381 =item * Failed tests
382  
383 =item * Parse errors
384  
385 =item * Bad exit or wait status
386  
387 =back
388  
389 =cut
390  
391 100 ▶sub has_errors {
Subroutine has_errors: called CC=4 SCAR=13.9
Pod documented
392 100 my $self = shift;
393 return
394 100 ▶ $self->failed
Condition100%9/9
$self->failed || $self->parse_errors || $self->exit || $self->wait
l!l&&r!l&&!r
22177
$self->failed or $self->parse_errors or $self->exit
l!l&&r!l&&!r
21178
$self->failed or $self->parse_errors
l!l&&r!l&&!r
14779
MC/DC100%4/4
$self->failed || $self->parse_errors || $self->exit || $self->wait
$self->failed $self->parse_errors $self->exit $self->wait
Truth table100%5/5
A or B or C || D
A= $self->failedB= $self->parse_errorsC= $self->exitD= $self->wait
ABCDresult
1XXX1
01XX1
001X1
00011
00000
395 || $self->parse_errors
396 || $self->exit
397 || $self->wait;
398 }
399  
400 ##############################################################################
401  
402 =head3 C<todo_failed>
403  
404 # deprecated in favor of 'todo_passed'. This method was horribly misnamed.
405  
406 This was a badly misnamed method. It indicates which TODO tests unexpectedly
407 succeeded. Will now issue a warning and call C<todo_passed>.
408  
409 =cut
410  
411 1 ▶sub todo_failed {
Subroutine todo_failed: called CC=1 SCAR=0.0
Pod documented
412 1 warn
413 '"todo_failed" is deprecated. Please use "todo_passed". See the docs.';
414 1 goto &todo_passed;
415 }
416  
417 =head1 See Also
418  
419 L<TAP::Parser>
420  
421 L<TAP::Harness>
422  
423 =cut
424  
425 18 1;
« blib/lib/TAP/Parser/Iterator/Process.pm ↑ Summary blib/lib/TAP/Formatter/Color.pm »