<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package TAP::Parser::Aggregator;

use strict;
use Benchmark;
use vars qw($VERSION @ISA);

use TAP::Object ();

@ISA = qw(TAP::Object);

=head1 NAME

TAP::Parser::Aggregator - Aggregate TAP::Parser results

=head1 VERSION

Version 3.26

=cut

$VERSION = '3.26';

=head1 SYNOPSIS

    use TAP::Parser::Aggregator;

    my $aggregate = TAP::Parser::Aggregator-&gt;new;
    $aggregate-&gt;add( 't/00-load.t', $load_parser );
    $aggregate-&gt;add( 't/10-lex.t',  $lex_parser  );

    my $summary = &lt;&lt;'END_SUMMARY';
    Passed:  %s
    Failed:  %s
    Unexpectedly succeeded: %s
    END_SUMMARY
    printf $summary,
           scalar $aggregate-&gt;passed,
           scalar $aggregate-&gt;failed,
           scalar $aggregate-&gt;todo_passed;

=head1 DESCRIPTION

C&lt;TAP::Parser::Aggregator&gt; collects parser objects and allows
reporting/querying their aggregate results.

=head1 METHODS

=head2 Class Methods

=head3 C&lt;new&gt;

 my $aggregate = TAP::Parser::Aggregator-&gt;new;

Returns a new C&lt;TAP::Parser::Aggregator&gt; object.

=cut

# new() implementation supplied by TAP::Object

my %SUMMARY_METHOD_FOR;

BEGIN {    # install summary methods
    %SUMMARY_METHOD_FOR = map { $_ =&gt; $_ } qw(
      failed
      parse_errors
      passed
      skipped
      todo
      todo_passed
      total
      wait
      exit
    );
    $SUMMARY_METHOD_FOR{total}   = 'tests_run';
    $SUMMARY_METHOD_FOR{planned} = 'tests_planned';

    for my $method ( keys %SUMMARY_METHOD_FOR ) {
        next if 'total' eq $method;
        no strict 'refs';
        *$method = sub {
            my $self = shift;
            return wantarray
              ? @{ $self-&gt;{"descriptions_for_$method"} }
              : $self-&gt;{$method};
        };
    }
}    # end install summary methods

sub _initialize {
    my ($self) = @_;
    $self-&gt;{parser_for}  = {};
    $self-&gt;{parse_order} = [];
    for my $summary ( keys %SUMMARY_METHOD_FOR ) {
        $self-&gt;{$summary} = 0;
        next if 'total' eq $summary;
        $self-&gt;{"descriptions_for_$summary"} = [];
    }
    return $self;
}

##############################################################################

=head2 Instance Methods

=head3 C&lt;add&gt;

  $aggregate-&gt;add( $description =&gt; $parser );

The C&lt;$description&gt; is usually a test file name (but only by
convention.)  It is used as a unique identifier (see e.g.
L&lt;"parsers"&gt;.)  Reusing a description is a fatal error.

The C&lt;$parser&gt; is a L&lt;TAP::Parser|TAP::Parser&gt; object.

=cut

sub add {
    my ( $self, $description, $parser ) = @_;
    if ( exists $self-&gt;{parser_for}{$description} ) {
        $self-&gt;_croak( "You already have a parser for ($description)."
              . " Perhaps you have run the same test twice." );
    }
    push @{ $self-&gt;{parse_order} } =&gt; $description;
    $self-&gt;{parser_for}{$description} = $parser;

    while ( my ( $summary, $method ) = each %SUMMARY_METHOD_FOR ) {

        # Slightly nasty. Instead we should maybe have 'cooked' accessors
        # for results that may be masked by the parser.
        next
          if ( $method eq 'exit' || $method eq 'wait' )
          &amp;&amp; $parser-&gt;ignore_exit;

        if ( my $count = $parser-&gt;$method() ) {
            $self-&gt;{$summary} += $count;
            push @{ $self-&gt;{"descriptions_for_$summary"} } =&gt; $description;
        }
    }

    return $self;
}

##############################################################################

=head3 C&lt;parsers&gt;

  my $count   = $aggregate-&gt;parsers;
  my @parsers = $aggregate-&gt;parsers;
  my @parsers = $aggregate-&gt;parsers(@descriptions);

In scalar context without arguments, this method returns the number of parsers
aggregated.  In list context without arguments, returns the parsers in the
order they were added.

If C&lt;@descriptions&gt; is given, these correspond to the keys used in each
call to the add() method.  Returns an array of the requested parsers (in
the requested order) in list context or an array reference in scalar
context.

Requesting an unknown identifier is a fatal error.

=cut

sub parsers {
    my $self = shift;
    return $self-&gt;_get_parsers(@_) if @_;
    my $descriptions = $self-&gt;{parse_order};
    my @parsers      = @{ $self-&gt;{parser_for} }{@$descriptions};

    # Note:  Because of the way context works, we must assign the parsers to
    # the @parsers array or else this method does not work as documented.
    return @parsers;
}

sub _get_parsers {
    my ( $self, @descriptions ) = @_;
    my @parsers;
    for my $description (@descriptions) {
        $self-&gt;_croak("A parser for ($description) could not be found")
          unless exists $self-&gt;{parser_for}{$description};
        push @parsers =&gt; $self-&gt;{parser_for}{$description};
    }
    return wantarray ? @parsers : \@parsers;
}

=head3 C&lt;descriptions&gt;

Get an array of descriptions in the order in which they were added to
the aggregator.

=cut

sub descriptions { @{ shift-&gt;{parse_order} || [] } }

=head3 C&lt;start&gt;

Call C&lt;start&gt; immediately before adding any results to the aggregator.
Among other times it records the start time for the test run.

=cut

sub start {
    my $self = shift;
    $self-&gt;{start_time} = Benchmark-&gt;new;
}

=head3 C&lt;stop&gt;

Call C&lt;stop&gt; immediately after adding all test results to the aggregator.

=cut

sub stop {
    my $self = shift;
    $self-&gt;{end_time} = Benchmark-&gt;new;
}

=head3 C&lt;elapsed&gt;

Elapsed returns a L&lt;Benchmark&gt; object that represents the running time
of the aggregated tests. In order for C&lt;elapsed&gt; to be valid you must
call C&lt;start&gt; before running the tests and C&lt;stop&gt; immediately
afterwards.

=cut

sub elapsed {
    my $self = shift;

    require Carp;
    Carp::croak
      q{Can't call elapsed without first calling start and then stop}
      unless defined $self-&gt;{start_time} &amp;&amp; defined $self-&gt;{end_time};
    return timediff( $self-&gt;{end_time}, $self-&gt;{start_time} );
}

=head3 C&lt;elapsed_timestr&gt;

Returns a formatted string representing the runtime returned by
C&lt;elapsed()&gt;.  This lets the caller not worry about Benchmark.

=cut

sub elapsed_timestr {
    my $self = shift;

    my $elapsed = $self-&gt;elapsed;

    return timestr($elapsed);
}

=head3 C&lt;all_passed&gt;

Return true if all the tests passed and no parse errors were detected.

=cut

sub all_passed {
    my $self = shift;
    return
         $self-&gt;total
      &amp;&amp; $self-&gt;total == $self-&gt;passed
      &amp;&amp; !$self-&gt;has_errors;
}

=head3 C&lt;get_status&gt;

Get a single word describing the status of the aggregated tests.
Depending on the outcome of the tests returns 'PASS', 'FAIL' or
'NOTESTS'. This token is understood by L&lt;CPAN::Reporter&gt;.

=cut

sub get_status {
    my $self = shift;

    my $total  = $self-&gt;total;
    my $passed = $self-&gt;passed;

    return
        ( $self-&gt;has_errors || $total != $passed ) ? 'FAIL'
      : $total ? 'PASS'
      :          'NOTESTS';
}

##############################################################################

=head2 Summary methods

Each of the following methods will return the total number of corresponding
tests if called in scalar context.  If called in list context, returns the
descriptions of the parsers which contain the corresponding tests (see C&lt;add&gt;
for an explanation of description.

=over 4

=item * failed

=item * parse_errors

=item * passed

=item * planned

=item * skipped

=item * todo

=item * todo_passed

=item * wait

=item * exit

=back

For example, to find out how many tests unexpectedly succeeded (TODO tests
which passed when they shouldn't):

 my $count        = $aggregate-&gt;todo_passed;
 my @descriptions = $aggregate-&gt;todo_passed;

Note that C&lt;wait&gt; and C&lt;exit&gt; are the totals of the wait and exit
statuses of each of the tests. These values are totalled only to provide
a true value if any of them are non-zero.

=cut

##############################################################################

=head3 C&lt;total&gt;

  my $tests_run = $aggregate-&gt;total;

Returns the total number of tests run.

=cut

sub total { shift-&gt;{total} }

##############################################################################

=head3 C&lt;has_problems&gt;

  if ( $parser-&gt;has_problems ) {
      ...
  }

Identical to C&lt;has_errors&gt;, but also returns true if any TODO tests
unexpectedly succeeded.  This is more akin to "warnings".

=cut

sub has_problems {
    my $self = shift;
    return $self-&gt;todo_passed
      || $self-&gt;has_errors;
}

##############################################################################

=head3 C&lt;has_errors&gt;

  if ( $parser-&gt;has_errors ) {
      ...
  }

Returns true if I&lt;any&gt; of the parsers failed.  This includes:

=over 4

=item * Failed tests

=item * Parse errors

=item * Bad exit or wait status

=back

=cut

sub has_errors {
    my $self = shift;
    return
         $self-&gt;failed
      || $self-&gt;parse_errors
      || $self-&gt;exit
      || $self-&gt;wait;
}

##############################################################################

=head3 C&lt;todo_failed&gt;

  # deprecated in favor of 'todo_passed'.  This method was horribly misnamed.

This was a badly misnamed method.  It indicates which TODO tests unexpectedly
succeeded.  Will now issue a warning and call C&lt;todo_passed&gt;.

=cut

sub todo_failed {
    warn
      '"todo_failed" is deprecated.  Please use "todo_passed".  See the docs.';
    goto &amp;todo_passed;
}

=head1 See Also

L&lt;TAP::Parser&gt;

L&lt;TAP::Harness&gt;

=cut

1;
</pre></body></html>