1 package TAP::Parser::Aggregator;
9 TAP::Parser::Aggregator - Aggregate TAP::Parser results
21 use TAP::Parser::Aggregator;
23 my $aggregate = TAP::Parser::Aggregator->new;
24 $aggregate->add( 't/00-load.t', $load_parser );
25 $aggregate->add( 't/10-lex.t', $lex_parser );
27 my $summary = <<'END_SUMMARY';
30 Unexpectedly succeeded: %s
33 scalar $aggregate->passed,
34 scalar $aggregate->failed,
35 scalar $aggregate->todo_passed;
39 C<TAP::Parser::Aggregator> collects parser objects and allows
40 reporting/querying their aggregate results.
48 my $aggregate = TAP::Parser::Aggregator->new;
50 Returns a new C<TAP::Parser::Aggregator> object.
54 my %SUMMARY_METHOD_FOR;
56 BEGIN { # install summary methods
57 %SUMMARY_METHOD_FOR = map { $_ => $_ } qw(
68 $SUMMARY_METHOD_FOR{total} = 'tests_run';
70 foreach my $method ( keys %SUMMARY_METHOD_FOR ) {
71 next if 'total' eq $method;
76 ? @{ $self->{"descriptions_for_$method"} }
80 } # end install summary methods
84 my $self = bless {}, $class;
91 $self->{parser_for} = {};
92 $self->{parse_order} = [];
93 foreach my $summary ( keys %SUMMARY_METHOD_FOR ) {
94 $self->{$summary} = 0;
95 next if 'total' eq $summary;
96 $self->{"descriptions_for_$summary"} = [];
101 ##############################################################################
103 =head2 Instance Methods
107 $aggregate->add( $description => $parser );
109 The C<$description> is usually a test file name (but only by
110 convention.) It is used as a unique identifier (see e.g.
111 L<"parsers">.) Reusing a description is a fatal error.
113 The C<$parser> is a L<TAP::Parser|TAP::Parser> object.
118 my ( $self, $description, $parser ) = @_;
119 if ( exists $self->{parser_for}{$description} ) {
120 $self->_croak( "You already have a parser for ($description)."
121 . " Perhaps you have run the same test twice." );
123 push @{ $self->{parse_order} } => $description;
124 $self->{parser_for}{$description} = $parser;
126 while ( my ( $summary, $method ) = each %SUMMARY_METHOD_FOR ) {
127 if ( my $count = $parser->$method() ) {
128 $self->{$summary} += $count;
129 push @{ $self->{"descriptions_for_$summary"} } => $description;
136 ##############################################################################
140 my $count = $aggregate->parsers;
141 my @parsers = $aggregate->parsers;
142 my @parsers = $aggregate->parsers(@descriptions);
144 In scalar context without arguments, this method returns the number of parsers
145 aggregated. In list context without arguments, returns the parsers in the
146 order they were added.
148 If C<@descriptions> is given, these correspond to the keys used in each
149 call to the add() method. Returns an array of the requested parsers (in
150 the requested order) in list context or an array reference in scalar
153 Requesting an unknown identifier is a fatal error.
159 return $self->_get_parsers(@_) if @_;
160 my $descriptions = $self->{parse_order};
161 my @parsers = @{ $self->{parser_for} }{@$descriptions};
163 # Note: Because of the way context works, we must assign the parsers to
164 # the @parsers array or else this method does not work as documented.
169 my ( $self, @descriptions ) = @_;
171 foreach my $description (@descriptions) {
172 $self->_croak("A parser for ($description) could not be found")
173 unless exists $self->{parser_for}{$description};
174 push @parsers => $self->{parser_for}{$description};
176 return wantarray ? @parsers : \@parsers;
179 =head3 C<descriptions>
181 Get an array of descriptions in the order in which they were added to the aggregator.
185 sub descriptions { @{ shift->{parse_order} || [] } }
189 Call C<start> immediately before adding any results to the aggregator.
190 Among other times it records the start time for the test run.
196 $self->{start_time} = Benchmark->new;
201 Call C<stop> immediately after adding all test results to the aggregator.
207 $self->{end_time} = Benchmark->new;
212 Elapsed returns a L<Benchmark> object that represents the running time
213 of the aggregated tests. In order for C<elapsed> to be valid you must
214 call C<start> before running the tests and C<stop> immediately
224 q{Can't call elapsed without first calling start and then stop}
225 unless defined $self->{start_time} && defined $self->{end_time};
226 return timediff( $self->{end_time}, $self->{start_time} );
229 =head3 C<elapsed_timestr>
231 Returns a formatted string representing the runtime returned by
232 C<elapsed()>. This lets the caller not worry about Benchmark.
236 sub elapsed_timestr {
239 my $elapsed = $self->elapsed;
241 return timestr($elapsed);
246 Return true if all the tests passed and no parse errors were detected.
254 && $self->total == $self->passed
255 && !$self->has_errors;
260 Get a single word describing the status of the aggregated tests.
261 Depending on the outcome of the tests returns 'PASS', 'FAIL' or
262 'NOTESTS'. This token is understood by L<CPAN::Reporter>.
269 my $total = $self->total;
270 my $passed = $self->passed;
273 ( $self->has_errors || $total != $passed ) ? 'FAIL'
278 ##############################################################################
280 =head2 Summary methods
282 Each of the following methods will return the total number of corresponding
283 tests if called in scalar context. If called in list context, returns the
284 descriptions of the parsers which contain the corresponding tests (see C<add>
285 for an explanation of description.
307 For example, to find out how many tests unexpectedly succeeded (TODO tests
308 which passed when they shouldn't):
310 my $count = $aggregate->todo_passed;
311 my @descriptions = $aggregate->todo_passed;
313 Note that C<wait> and C<exit> are the totals of the wait and exit
314 statuses of each of the tests. These values are totalled only to provide
315 a true value if any of them are non-zero.
319 ##############################################################################
323 my $tests_run = $aggregate->total;
325 Returns the total number of tests run.
329 sub total { shift->{total} }
331 ##############################################################################
333 =head3 C<has_problems>
335 if ( $parser->has_problems ) {
339 Identical to C<has_errors>, but also returns true if any TODO tests
340 unexpectedly succeeded. This is more akin to "warnings".
346 return $self->todo_passed
347 || $self->has_errors;
350 ##############################################################################
354 if ( $parser->has_errors ) {
358 Returns true if I<any> of the parsers failed. This includes:
366 =item * Bad exit or wait status
376 || $self->parse_errors
381 ##############################################################################
383 =head3 C<todo_failed>
385 # deprecated in favor of 'todo_passed'. This method was horribly misnamed.
387 This was a badly misnamed method. It indicates which TODO tests unexpectedly
388 succeeded. Will now issue a warning and call C<todo_passed>.
394 '"todo_failed" is deprecated. Please use "todo_passed". See the docs.';