4 use vars
qw($VERSION @ISA);
7 use TAP::Parser::Grammar ();
8 use TAP::Parser::Result ();
9 use TAP::Parser::ResultFactory ();
10 use TAP::Parser::Source ();
11 use TAP::Parser::Iterator ();
12 use TAP::Parser::IteratorFactory ();
13 use TAP::Parser::SourceHandler::Executable ();
14 use TAP::Parser::SourceHandler::Perl ();
15 use TAP::Parser::SourceHandler::File ();
16 use TAP::Parser::SourceHandler::RawTAP ();
17 use TAP::Parser::SourceHandler::Handle ();
19 use Carp qw( confess );
23 TAP::Parser - Parse L<TAP|Test::Harness::TAP> output
33 my $DEFAULT_TAP_VERSION = 12;
34 my $MAX_TAP_VERSION = 13;
36 $ENV{TAP_VERSION
} = $MAX_TAP_VERSION;
41 delete $ENV{TAP_VERSION
};
44 BEGIN { # making accessors
47 __PACKAGE__
->mk_methods(
65 iterator_factory_class
69 sub _stream
{ # deprecated
73 } # done making accessors
79 my $parser = TAP::Parser->new( { source => $source } );
81 while ( my $result = $parser->next ) {
82 print $result->as_string;
87 C<TAP::Parser> is designed to produce a proper parse of TAP output. For
88 an example of how to run tests through this module, see the simple
89 harnesses C<examples/>.
91 There's a wiki dedicated to the Test Anything Protocol:
93 L<http://testanything.org>
95 It includes the TAP::Parser Cookbook:
97 L<http://testanything.org/wiki/index.php/TAP::Parser_Cookbook>
105 my $parser = TAP::Parser->new(\%args);
107 Returns a new C<TAP::Parser> object.
109 The arguments should be a hashref with I<one> of the following keys:
117 This is the preferred method of passing input to the constructor.
119 The C<source> is used to create a L<TAP::Parser::Source> that is passed to the
120 L</iterator_factory_class> which in turn figures out how to handle the source and
121 creates a <TAP::Parser::Iterator> for it. The iterator is used by the parser to
122 read in the TAP stream.
124 To configure the I<IteratorFactory> use the C<sources> parameter below.
126 Note that C<source>, C<tap> and C<exec> are I<mutually exclusive>.
132 The value should be the complete TAP output.
134 The I<tap> is used to create a L<TAP::Parser::Source> that is passed to the
135 L</iterator_factory_class> which in turn figures out how to handle the source and
136 creates a <TAP::Parser::Iterator> for it. The iterator is used by the parser to
137 read in the TAP stream.
139 To configure the I<IteratorFactory> use the C<sources> parameter below.
141 Note that C<source>, C<tap> and C<exec> are I<mutually exclusive>.
145 Must be passed an array reference.
147 The I<exec> array ref is used to create a L<TAP::Parser::Source> that is passed
148 to the L</iterator_factory_class> which in turn figures out how to handle the
149 source and creates a <TAP::Parser::Iterator> for it. The iterator is used by
150 the parser to read in the TAP stream.
152 By default the L<TAP::Parser::SourceHandler::Executable> class will create a
153 L<TAP::Parser::Iterator::Process> object to handle the source. This passes the
154 array reference strings as command arguments to L<IPC::Open3::open3|IPC::Open3>:
156 exec => [ '/usr/bin/ruby', 't/my_test.rb' ]
158 If any C<test_args> are given they will be appended to the end of the command
161 To configure the I<IteratorFactory> use the C<sources> parameter below.
163 Note that C<source>, C<tap> and C<exec> are I<mutually exclusive>.
167 The following keys are optional.
175 If set, C<sources> must be a hashref containing the names of the
176 L<TAP::Parser::SourceHandler>s to load and/or configure. The values are a
177 hash of configuration that will be accessible to to the source handlers via
178 L<TAP::Parser::Source/config_for>.
183 Perl => { exec => '/path/to/custom/perl' },
184 File => { extensions => [ '.tap', '.txt' ] },
185 MyCustom => { some => 'config' },
188 This will cause C<TAP::Parser> to pass custom configuration to two of the built-
189 in source handlers - L<TAP::Parser::SourceHandler::Perl>,
190 L<TAP::Parser::SourceHandler::File> - and attempt to load the C<MyCustom>
191 class. See L<TAP::Parser::IteratorFactory/load_handlers> for more detail.
193 The C<sources> parameter affects how C<source>, C<tap> and C<exec> parameters
196 See L<TAP::Parser::IteratorFactory>, L<TAP::Parser::SourceHandler> and subclasses for
201 If present, each callback corresponding to a given result type will be called
202 with the result as the argument if the C<run> method is used:
205 test => \&test_callback,
206 plan => \&plan_callback,
207 comment => \&comment_callback,
208 bailout => \&bailout_callback,
209 unknown => \&unknown_callback,
212 my $aggregator = TAP::Parser::Aggregator->new;
213 for my $file ( @test_files ) {
214 my $parser = TAP::Parser->new(
217 callbacks => \%callbacks,
221 $aggregator->add( $file, $parser );
226 If using a Perl file as a source, optional switches may be passed which will
227 be used when invoking the perl executable.
229 my $parser = TAP::Parser->new( {
230 source => $test_file,
231 switches => [ '-Ilib' ],
236 Used in conjunction with the C<source> and C<exec> option to supply a reference
237 to an C<@ARGV> style array of arguments to pass to the test program.
241 If passed a filehandle will write a copy of all parsed TAP to that handle.
245 If false, STDERR is not captured (though it is 'relayed' to keep it
246 somewhat synchronized with STDOUT.)
248 If true, STDERR and STDOUT are the same filehandle. This may cause
249 breakage if STDERR contains anything resembling TAP format, but does
250 allow exact synchronization.
252 Subtleties of this behavior may be platform-dependent and may change in
255 =item * C<grammar_class>
257 This option was introduced to let you easily customize which I<grammar> class
258 the parser should use. It defaults to L<TAP::Parser::Grammar>.
260 See also L</make_grammar>.
262 =item * C<result_factory_class>
264 This option was introduced to let you easily customize which I<result>
265 factory class the parser should use. It defaults to
266 L<TAP::Parser::ResultFactory>.
268 See also L</make_result>.
270 =item * C<iterator_factory_class>
274 This option was introduced to let you easily customize which I<iterator>
275 factory class the parser should use. It defaults to
276 L<TAP::Parser::IteratorFactory>.
282 # new() implementation supplied by TAP::Base
284 # This should make overriding behaviour of the Parser in subclasses easier:
285 sub _default_grammar_class
{'TAP::Parser::Grammar'}
286 sub _default_result_factory_class
{'TAP::Parser::ResultFactory'}
287 sub _default_iterator_factory_class
{'TAP::Parser::IteratorFactory'}
289 ##############################################################################
291 =head2 Instance Methods
295 my $parser = TAP::Parser->new( { source => $file } );
296 while ( my $result = $parser->next ) {
297 print $result->as_string, "\n";
300 This method returns the results of the parsing, one result at a time. Note
301 that it is destructive. You can't rewind and examine previous results.
303 If callbacks are used, they will be issued before this call returns.
305 Each result returned is a subclass of L<TAP::Parser::Result>. See that
306 module and related classes for more information on how to use them.
312 return ( $self->{_iter
} ||= $self->_iter )->();
315 ##############################################################################
321 This method merely runs the parser and parses all of the TAP.
327 while ( defined( my $result = $self->next ) ) {
333 ##############################################################################
335 =head3 C<make_grammar>
337 Make a new L<TAP::Parser::Grammar> object and return it. Passes through any
340 The C<grammar_class> can be customized, as described in L</new>.
342 =head3 C<make_result>
344 Make a new L<TAP::Parser::Result> object using the parser's
345 L<TAP::Parser::ResultFactory>, and return it. Passes through any arguments
348 The C<result_factory_class> can be customized, as described in L</new>.
350 =head3 C<make_iterator_factory>
354 Make a new L<TAP::Parser::IteratorFactory> object and return it. Passes through
357 C<iterator_factory_class> can be customized, as described in L</new>.
361 # This should make overriding behaviour of the Parser in subclasses easier:
362 sub make_iterator_factory
{ shift->iterator_factory_class->new(@_); }
363 sub make_grammar
{ shift->grammar_class->new(@_); }
364 sub make_result
{ shift->result_factory_class->make_result(@_); }
368 # of the following, anything beginning with an underscore is strictly
369 # internal and should not be exposed.
371 version
=> $DEFAULT_TAP_VERSION,
372 plan
=> '', # the test plan (e.g., 1..3)
373 tests_run
=> 0, # actual current test numbers
378 actual_failed
=> [], # how many tests really failed
379 actual_passed
=> [], # how many tests really passed
380 todo_passed
=> [], # tests which unexpectedly succeed
381 parse_errors
=> [], # perfect TAP should have none
384 # We seem to have this list hanging around all over the place. We could
385 # probably get it from somewhere else to avoid the repetition.
386 my @legal_callback = qw(
399 my @class_overrides = qw(
402 iterator_factory_class
406 my ( $self, $arg_for ) = @_;
408 # everything here is basically designed to convert any TAP source to a
409 # TAP::Parser::Iterator.
412 my %args = %{ $arg_for || {} };
414 $self->SUPER::_initialize
( \
%args, \
@legal_callback );
416 # get any class overrides out first:
417 for my $key (@class_overrides) {
418 my $default_method = "_default_$key";
419 my $val = delete $args{$key} || $self->$default_method();
423 my $iterator = delete $args{iterator
};
424 $iterator ||= delete $args{stream
}; # deprecated
425 my $tap = delete $args{tap
};
426 my $version = delete $args{version
};
427 my $raw_source = delete $args{source
};
428 my $sources = delete $args{sources
};
429 my $exec = delete $args{exec};
430 my $merge = delete $args{merge
};
431 my $spool = delete $args{spool
};
432 my $switches = delete $args{switches
};
433 my $ignore_exit = delete $args{ignore_exit
};
434 my $test_args = delete $args{test_args
} || [];
436 if ( 1 < grep {defined} $iterator, $tap, $raw_source, $exec ) {
438 "You may only choose one of 'exec', 'tap', 'source' or 'iterator'"
442 if ( my @excess = sort keys %args ) {
443 $self->_croak("Unknown options: @excess");
446 # convert $tap & $exec to $raw_source equiv.
448 my $source = TAP
::Parser
::Source
->new;
451 $source->raw( \
$tap );
454 $type = 'exec ' . $exec->[0];
455 $source->raw( { exec => $exec } );
457 elsif ($raw_source) {
458 $type = 'source ' . ref($raw_source) || $raw_source;
459 $source->raw( ref($raw_source) ?
$raw_source : \
$raw_source );
462 $type = 'iterator ' . ref($iterator);
465 if ( $source->raw ) {
466 my $src_factory = $self->make_iterator_factory($sources);
467 $source->merge($merge)->switches($switches)
468 ->test_args($test_args);
469 $iterator = $src_factory->make_iterator($source);
474 "PANIC: could not determine iterator for input $type");
477 while ( my ( $k, $v ) = each %initialize ) {
478 $self->{$k} = 'ARRAY' eq ref $v ?
[] : $v;
481 $self->version($version) if $version;
482 $self->_iterator($iterator);
483 $self->_spool($spool);
484 $self->ignore_exit($ignore_exit);
490 =head1 INDIVIDUAL RESULTS
492 If you've read this far in the docs, you've seen this:
494 while ( my $result = $parser->next ) {
495 print $result->as_string;
498 Each result returned is a L<TAP::Parser::Result> subclass, referred to as
503 Basically, you fetch individual results from the TAP. The six types, with
504 examples of each, are as follows:
522 ok 3 - We should start with some foobar!
526 # Hope we don't use up the foobar.
530 Bail out! We ran out of foobar!
534 ... yo, this ain't TAP! ...
538 Each result fetched is a result object of a different type. There are common
539 methods to each result object and different types may have methods unique to
540 their type. Sometimes a type method may be overridden in a subclass, but its
541 use is guaranteed to be identical.
543 =head2 Common type methods
547 Returns the type of result, such as C<comment> or C<test>.
551 Prints a string representation of the token. This might not be the exact
552 output, however. Tests will have test numbers added if not present, TODO and
553 SKIP directives will be capitalized and, in general, things will be cleaned
554 up. If you need the original text for the token, see the C<raw> method.
558 Returns the original line of text which was parsed.
562 Indicates whether or not this is the test plan line.
566 Indicates whether or not this is a test line.
570 Indicates whether or not this is a comment. Comments will generally only
571 appear in the TAP stream if STDERR is merged to STDOUT. See the
576 Indicates whether or not this is bailout line.
580 Indicates whether or not the current item is a YAML block.
584 Indicates whether or not the current line could be parsed.
588 if ( $result->is_ok ) { ... }
590 Reports whether or not a given result has passed. Anything which is B<not> a
591 test result returns true. This is merely provided as a convenient shortcut
592 which allows you to do this:
594 my $parser = TAP::Parser->new( { source => $source } );
595 while ( my $result = $parser->next ) {
596 # only print failing results
597 print $result->as_string unless $result->is_ok;
600 =head2 C<plan> methods
602 if ( $result->is_plan ) { ... }
604 If the above evaluates as true, the following methods will be available on the
609 if ( $result->is_plan ) {
613 This is merely a synonym for C<as_string>.
617 my $directive = $result->directive;
619 If a SKIP directive is included with the plan, this method will return it.
621 1..0 # SKIP: why bother?
623 =head3 C<explanation>
625 my $explanation = $result->explanation;
627 If a SKIP directive was included with the plan, this method will return the
630 =head2 C<pragma> methods
632 if ( $result->is_pragma ) { ... }
634 If the above evaluates as true, the following methods will be available on the
639 Returns a list of pragmas each of which is a + or - followed by the
642 =head2 C<comment> methods
644 if ( $result->is_comment ) { ... }
646 If the above evaluates as true, the following methods will be available on the
651 if ( $result->is_comment ) {
652 my $comment = $result->comment;
653 print "I have something to say: $comment";
656 =head2 C<bailout> methods
658 if ( $result->is_bailout ) { ... }
660 If the above evaluates as true, the following methods will be available on the
663 =head3 C<explanation>
665 if ( $result->is_bailout ) {
666 my $explanation = $result->explanation;
667 print "We bailed out because ($explanation)";
670 If, and only if, a token is a bailout token, you can get an "explanation" via
671 this method. The explanation is the text after the mystical "Bail out!" words
672 which appear in the tap output.
674 =head2 C<unknown> methods
676 if ( $result->is_unknown ) { ... }
678 There are no unique methods for unknown results.
680 =head2 C<test> methods
682 if ( $result->is_test ) { ... }
684 If the above evaluates as true, the following methods will be available on the
689 my $ok = $result->ok;
691 Returns the literal text of the C<ok> or C<not ok> status.
695 my $test_number = $result->number;
697 Returns the number of the test, even if the original TAP output did not supply
700 =head3 C<description>
702 my $description = $result->description;
704 Returns the description of the test, if any. This is the portion after the
705 test number but before the directive.
709 my $directive = $result->directive;
711 Returns either C<TODO> or C<SKIP> if either directive was present for a test
714 =head3 C<explanation>
716 my $explanation = $result->explanation;
718 If a test had either a C<TODO> or C<SKIP> directive, this method will return
719 the accompanying explanation, if present.
721 not ok 17 - 'Pigs can fly' # TODO not enough acid
723 For the above line, the explanation is I<not enough acid>.
727 if ( $result->is_ok ) { ... }
729 Returns a boolean value indicating whether or not the test passed. Remember
730 that for TODO tests, the test always passes.
732 B<Note:> this was formerly C<passed>. The latter method is deprecated and
733 will issue a warning.
735 =head3 C<is_actual_ok>
737 if ( $result->is_actual_ok ) { ... }
739 Returns a boolean value indicating whether or not the test passed, regardless
742 B<Note:> this was formerly C<actual_passed>. The latter method is deprecated
743 and will issue a warning.
745 =head3 C<is_unplanned>
747 if ( $test->is_unplanned ) { ... }
749 If a test number is greater than the number of planned tests, this method will
750 return true. Unplanned tests will I<always> return false for C<is_ok>,
751 regardless of whether or not the test C<has_todo> (see
752 L<TAP::Parser::Result::Test> for more information about this).
756 if ( $result->has_skip ) { ... }
758 Returns a boolean value indicating whether or not this test had a SKIP
763 if ( $result->has_todo ) { ... }
765 Returns a boolean value indicating whether or not this test had a TODO
768 Note that TODO tests I<always> pass. If you need to know whether or not
769 they really passed, check the C<is_actual_ok> method.
773 if ( $parser->in_todo ) { ... }
775 True while the most recent result was a TODO. Becomes true before the
776 TODO result is returned and stays true until just before the next non-
777 TODO test is returned.
781 After parsing the TAP, there are many methods available to let you dig through
782 the results and determine what is meaningful to you.
784 =head2 Individual Results
786 These results refer to individual tests which are run.
790 my @passed = $parser->passed; # the test numbers which passed
791 my $passed = $parser->passed; # the number of tests which passed
793 This method lets you know which (or how many) tests passed. If a test failed
794 but had a TODO directive, it will be counted as a passed test.
798 sub passed
{ @
{ shift->{passed
} } }
802 my @failed = $parser->failed; # the test numbers which failed
803 my $failed = $parser->failed; # the number of tests which failed
805 This method lets you know which (or how many) tests failed. If a test passed
806 but had a TODO directive, it will B<NOT> be counted as a failed test.
810 sub failed
{ @
{ shift->{failed
} } }
812 =head3 C<actual_passed>
814 # the test numbers which actually passed
815 my @actual_passed = $parser->actual_passed;
817 # the number of tests which actually passed
818 my $actual_passed = $parser->actual_passed;
820 This method lets you know which (or how many) tests actually passed,
821 regardless of whether or not a TODO directive was found.
825 sub actual_passed
{ @
{ shift->{actual_passed
} } }
826 *actual_ok
= \
&actual_passed
;
830 This method is a synonym for C<actual_passed>.
832 =head3 C<actual_failed>
834 # the test numbers which actually failed
835 my @actual_failed = $parser->actual_failed;
837 # the number of tests which actually failed
838 my $actual_failed = $parser->actual_failed;
840 This method lets you know which (or how many) tests actually failed,
841 regardless of whether or not a TODO directive was found.
845 sub actual_failed
{ @
{ shift->{actual_failed
} } }
847 ##############################################################################
851 my @todo = $parser->todo; # the test numbers with todo directives
852 my $todo = $parser->todo; # the number of tests with todo directives
854 This method lets you know which (or how many) tests had TODO directives.
858 sub todo
{ @
{ shift->{todo
} } }
860 =head3 C<todo_passed>
862 # the test numbers which unexpectedly succeeded
863 my @todo_passed = $parser->todo_passed;
865 # the number of tests which unexpectedly succeeded
866 my $todo_passed = $parser->todo_passed;
868 This method lets you know which (or how many) tests actually passed but were
869 declared as "TODO" tests.
873 sub todo_passed
{ @
{ shift->{todo_passed
} } }
875 ##############################################################################
877 =head3 C<todo_failed>
879 # deprecated in favor of 'todo_passed'. This method was horribly misnamed.
881 This was a badly misnamed method. It indicates which TODO tests unexpectedly
882 succeeded. Will now issue a warning and call C<todo_passed>.
888 '"todo_failed" is deprecated. Please use "todo_passed". See the docs.';
894 my @skipped = $parser->skipped; # the test numbers with SKIP directives
895 my $skipped = $parser->skipped; # the number of tests with SKIP directives
897 This method lets you know which (or how many) tests had SKIP directives.
901 sub skipped
{ @
{ shift->{skipped
} } }
907 Get or set a pragma. To get the state of a pragma:
909 if ( $p->pragma('strict') ) {
913 To set the state of a pragma:
915 $p->pragma('strict', 1); # enable strict mode
920 my ( $self, $pragma ) = splice @_, 0, 2;
922 return $self->{pragma
}->{$pragma} unless @_;
924 if ( my $state = shift ) {
925 $self->{pragma
}->{$pragma} = 1;
928 delete $self->{pragma
}->{$pragma};
936 Get a list of all the currently enabled pragmas:
938 my @pragmas_enabled = $p->pragmas;
942 sub pragmas
{ sort keys %{ shift->{pragma
} || {} } }
944 =head2 Summary Results
946 These results are "meta" information about the total results of an individual
951 my $plan = $parser->plan;
953 Returns the test plan, if found.
957 Deprecated. Use C<is_good_plan> instead.
962 warn 'good_plan() is deprecated. Please use "is_good_plan()"';
966 ##############################################################################
968 =head3 C<is_good_plan>
970 if ( $parser->is_good_plan ) { ... }
972 Returns a boolean value indicating whether or not the number of tests planned
973 matches the number of tests run.
975 B<Note:> this was formerly C<good_plan>. The latter method is deprecated and
976 will issue a warning.
978 And since we're on that subject ...
980 =head3 C<tests_planned>
982 print $parser->tests_planned;
984 Returns the number of tests planned, according to the plan. For example, a
985 plan of '1..17' will mean that 17 tests were planned.
989 print $parser->tests_run;
991 Returns the number of tests which actually were run. Hopefully this will
992 match the number of C<< $parser->tests_planned >>.
996 Returns a true value (actually the reason for skipping) if all tests
1001 Returns the time when the Parser was created.
1005 Returns the time when the end of TAP input was seen.
1007 =head3 C<has_problems>
1009 if ( $parser->has_problems ) {
1013 This is a 'catch-all' method which returns true if any tests have currently
1014 failed, any TODO tests unexpectedly succeeded, or any parse errors occurred.
1022 || $self->parse_errors
1023 || ( !$self->ignore_exit && ( $self->wait || $self->exit ) );
1030 Once the parser is done, this will return the version number for the
1031 parsed TAP. Version numbers were introduced with TAP version 13 so if no
1032 version number is found version 12 is assumed.
1038 Once the parser is done, this will return the exit status. If the parser ran
1039 an executable, it returns the exit status of the executable.
1045 Once the parser is done, this will return the wait status. If the parser ran
1046 an executable, it returns the wait status of the executable. Otherwise, this
1047 merely returns the C<exit> status.
1049 =head2 C<ignore_exit>
1051 $parser->ignore_exit(1);
1053 Tell the parser to ignore the exit status from the test when determining
1054 whether the test passed. Normally tests with non-zero exit status are
1055 considered to have failed even if all individual tests passed. In cases
1056 where it is not possible to control the exit value of the test script
1057 use this option to ignore it.
1061 sub ignore_exit
{ shift->pragma( 'ignore_exit', @_ ) }
1063 =head3 C<parse_errors>
1065 my @errors = $parser->parse_errors; # the parser errors
1066 my $errors = $parser->parse_errors; # the number of parser_errors
1068 Fortunately, all TAP output is perfect. In the event that it is not, this
1069 method will return parser errors. Note that a junk line which the parser does
1070 not recognize is C<not> an error. This allows this parser to handle future
1071 versions of TAP. The following are all TAP errors reported by the parser:
1075 =item * Misplaced plan
1077 The plan (for example, '1..5'), must only come at the beginning or end of the
1084 =item * More than one plan
1087 ok 1 - input file opened
1088 not ok 2 - first line of the input valid # todo some data
1089 ok 3 read the rest of the file
1092 Right. Very funny. Don't do that.
1094 =item * Test numbers out of sequence
1097 ok 1 - input file opened
1098 not ok 2 - first line of the input valid # todo some data
1099 ok 2 read the rest of the file
1101 That last test line above should have the number '3' instead of '2'.
1103 Note that it's perfectly acceptable for some lines to have test numbers and
1104 others to not have them. However, when a test number is found, it must be in
1105 sequence. The following is also an error:
1108 ok 1 - input file opened
1109 not ok - first line of the input valid # todo some data
1110 ok 2 read the rest of the file
1115 ok - input file opened
1116 not ok - first line of the input valid # todo some data
1117 ok 3 read the rest of the file
1123 sub parse_errors
{ @
{ shift->{parse_errors
} } }
1126 my ( $self, $error ) = @_;
1127 push @
{ $self->{parse_errors
} } => $error;
1131 sub _make_state_table
{
1134 my %planned_todo = ();
1136 # These transitions are defaults for all states
1137 my %state_globals = (
1144 'If TAP version is present it must be the first line of output'
1151 if ( $self->pragma('strict') ) {
1153 'Unknown TAP token: "' . $unk->raw . '"' );
1160 for my $pr ( $pragma->pragmas ) {
1161 if ( $pr =~ /^ ([-+])(\w+) $/x ) {
1162 $self->pragma( $2, $1 eq '+' );
1169 # Provides default elements for transitions
1170 my %state_defaults = (
1174 $self->tests_planned( $plan->tests_planned );
1175 $self->plan( $plan->plan );
1176 if ( $plan->has_skip ) {
1177 $self->skip_all( $plan->explanation
1178 || '(no reason given)' );
1181 $planned_todo{$_}++ for @
{ $plan->todo_list };
1188 my ( $number, $tests_run )
1189 = ( $test->number, ++$self->{tests_run
} );
1192 if ( defined $number && delete $planned_todo{$number} ) {
1193 $test->set_directive('TODO');
1196 my $has_todo = $test->has_todo;
1198 $self->in_todo($has_todo);
1199 if ( defined( my $tests_planned = $self->tests_planned ) ) {
1200 if ( $tests_run > $tests_planned ) {
1201 $test->is_unplanned(1);
1205 if ( defined $number ) {
1206 if ( $number != $tests_run ) {
1207 my $count = $tests_run;
1208 $self->_add_error( "Tests out of sequence. Found "
1209 . "($number) but expected ($count)" );
1213 $test->_number( $number = $tests_run );
1216 push @
{ $self->{todo
} } => $number if $has_todo;
1217 push @
{ $self->{todo_passed
} } => $number
1218 if $test->todo_passed;
1219 push @
{ $self->{skipped
} } => $number
1222 push @
{ $self->{ $test->is_ok ?
'passed' : 'failed' } } =>
1233 yaml
=> { act
=> sub { }, },
1236 # Each state contains a hash the keys of which match a token type. For
1238 # type there may be:
1239 # act A coderef to run
1240 # goto The new state to move to. Stay in this state if
1242 # continue Goto the new state and run the new state for the
1249 my $ver_num = $version->version;
1250 if ( $ver_num <= $DEFAULT_TAP_VERSION ) {
1251 my $ver_min = $DEFAULT_TAP_VERSION + 1;
1253 "Explicit TAP version must be at least "
1254 . "$ver_min. Got version $ver_num" );
1255 $ver_num = $DEFAULT_TAP_VERSION;
1257 if ( $ver_num > $MAX_TAP_VERSION ) {
1259 "TAP specified version $ver_num but "
1260 . "we don't know about versions later "
1261 . "than $MAX_TAP_VERSION" );
1262 $ver_num = $MAX_TAP_VERSION;
1264 $self->version($ver_num);
1265 $self->_grammar->set_version($ver_num);
1269 plan
=> { goto => 'PLANNED' },
1270 test
=> { goto => 'UNPLANNED' },
1273 plan
=> { goto => 'PLANNED' },
1274 test
=> { goto => 'UNPLANNED' },
1277 test
=> { goto => 'PLANNED_AFTER_TEST' },
1282 'More than one plan found in TAP output');
1286 PLANNED_AFTER_TEST
=> {
1287 test
=> { goto => 'PLANNED_AFTER_TEST' },
1288 plan
=> { act
=> sub { }, continue => 'PLANNED' },
1289 yaml
=> { goto => 'PLANNED' },
1295 my $line = $self->plan;
1297 "Plan ($line) must be at the beginning "
1298 . "or end of the TAP output" );
1299 $self->is_good_plan(0);
1301 continue => 'PLANNED'
1303 plan
=> { continue => 'PLANNED' },
1306 test
=> { goto => 'UNPLANNED_AFTER_TEST' },
1307 plan
=> { goto => 'GOT_PLAN' },
1309 UNPLANNED_AFTER_TEST
=> {
1310 test
=> { act
=> sub { }, continue => 'UNPLANNED' },
1311 plan
=> { act
=> sub { }, continue => 'UNPLANNED' },
1312 yaml
=> { goto => 'UNPLANNED' },
1316 # Apply globals and defaults to state table
1317 for my $name ( keys %states ) {
1319 # Merge with globals
1320 my $st = { %state_globals, %{ $states{$name} } };
1323 for my $next ( sort keys %{$st} ) {
1324 if ( my $default = $state_defaults{$next} ) {
1325 for my $def ( sort keys %{$default} ) {
1326 $st->{$next}->{$def} ||= $default->{$def};
1331 # Stuff back in table
1332 $states{$name} = $st;
1338 =head3 C<get_select_handles>
1340 Get an a list of file handles which can be passed to C<select> to
1341 determine the readiness of this parser.
1345 sub get_select_handles
{ shift->_iterator->get_select_handles }
1349 return $self->{_grammar
} = shift if @_;
1351 return $self->{_grammar
} ||= $self->make_grammar(
1352 { iterator
=> $self->_iterator,
1354 version
=> $self->version
1361 my $iterator = $self->_iterator;
1362 my $grammar = $self->_grammar;
1363 my $spool = $self->_spool;
1365 my $state_table = $self->_make_state_table;
1367 $self->start_time( $self->get_time );
1369 # Make next_state closure
1370 my $next_state = sub {
1372 my $type = $token->type;
1374 my $state_spec = $state_table->{$state}
1375 or die "Illegal state: $state";
1377 if ( my $next = $state_spec->{$type} ) {
1378 if ( my $act = $next->{act
} ) {
1381 if ( my $cont = $next->{continue} ) {
1385 elsif ( my $goto = $next->{goto} ) {
1390 confess
("Unhandled token type: $type\n");
1396 # Handle end of stream - which means either pop a block or finish
1397 my $end_handler = sub {
1398 $self->exit( $iterator->exit );
1399 $self->wait( $iterator->wait );
1404 # Finally make the closure that we return. For performance reasons
1405 # there are two versions of the returned function: one that handles
1406 # callbacks and one that does not.
1407 if ( $self->_has_callbacks ) {
1409 my $result = eval { $grammar->tokenize };
1410 $self->_add_error($@
) if $@
;
1412 if ( defined $result ) {
1413 $result = $next_state->($result);
1415 if ( my $code = $self->_callback_for( $result->type ) ) {
1416 $_->($result) for @
{$code};
1419 $self->_make_callback( 'ELSE', $result );
1422 $self->_make_callback( 'ALL', $result );
1424 # Echo TAP to spool file
1425 print {$spool} $result->raw, "\n" if $spool;
1428 $result = $end_handler->();
1429 $self->_make_callback( 'EOF', $self )
1430 unless defined $result;
1438 my $result = eval { $grammar->tokenize };
1439 $self->_add_error($@
) if $@
;
1441 if ( defined $result ) {
1442 $result = $next_state->($result);
1444 # Echo TAP to spool file
1445 print {$spool} $result->raw, "\n" if $spool;
1448 $result = $end_handler->();
1459 $self->end_time( $self->get_time );
1462 $self->_iterator(undef);
1463 $self->_grammar(undef);
1465 # If we just delete the iter we won't get a fault if it's recreated.
1466 # Instead we set it to a sub that returns an infinite
1467 # stream of undef. This segfaults on 5.5.4, presumably because
1468 # we're still executing the closure that gets replaced and it hasn't
1469 # been protected with a refcount.
1470 $self->{_iter
} = sub {return}
1474 if ( !$self->plan ) {
1475 $self->_add_error('No plan found in TAP output');
1478 $self->is_good_plan(1) unless defined $self->is_good_plan;
1480 if ( $self->tests_run != ( $self->tests_planned || 0 ) ) {
1481 $self->is_good_plan(0);
1482 if ( defined( my $planned = $self->tests_planned ) ) {
1483 my $ran = $self->tests_run;
1485 "Bad plan. You planned $planned tests but ran $ran.");
1488 if ( $self->tests_run != ( $self->passed + $self->failed ) ) {
1490 # this should never happen
1491 my $actual = $self->tests_run;
1492 my $passed = $self->passed;
1493 my $failed = $self->failed;
1494 $self->_croak( "Panic: planned test count ($actual) did not equal "
1495 . "sum of passed ($passed) and failed ($failed) tests!" );
1498 $self->is_good_plan(0) unless defined $self->is_good_plan;
1502 =head3 C<delete_spool>
1504 Delete and return the spool.
1506 my $fh = $parser->delete_spool;
1513 return delete $self->{_spool
};
1516 ##############################################################################
1520 As mentioned earlier, a "callback" key may be added to the
1521 C<TAP::Parser> constructor. If present, each callback corresponding to a
1522 given result type will be called with the result as the argument if the
1523 C<run> method is used. The callback is expected to be a subroutine
1524 reference (or anonymous subroutine) which is invoked with the parser
1525 result as its argument.
1528 test => \&test_callback,
1529 plan => \&plan_callback,
1530 comment => \&comment_callback,
1531 bailout => \&bailout_callback,
1532 unknown => \&unknown_callback,
1535 my $aggregator = TAP::Parser::Aggregator->new;
1536 for my $file ( @test_files ) {
1537 my $parser = TAP::Parser->new(
1540 callbacks => \%callbacks,
1544 $aggregator->add( $file, $parser );
1547 Callbacks may also be added like this:
1549 $parser->callback( test => \&test_callback );
1550 $parser->callback( plan => \&plan_callback );
1552 The following keys allowed for callbacks. These keys are case-sensitive.
1558 Invoked if C<< $result->is_test >> returns true.
1562 Invoked if C<< $result->is_version >> returns true.
1566 Invoked if C<< $result->is_plan >> returns true.
1570 Invoked if C<< $result->is_comment >> returns true.
1574 Invoked if C<< $result->is_unknown >> returns true.
1578 Invoked if C<< $result->is_yaml >> returns true.
1582 Invoked if C<< $result->is_unknown >> returns true.
1586 If a result does not have a callback defined for it, this callback will
1587 be invoked. Thus, if all of the previous result types are specified as
1588 callbacks, this callback will I<never> be invoked.
1592 This callback will always be invoked and this will happen for each
1593 result after one of the above callbacks is invoked. For example, if
1594 L<Term::ANSIColor> is loaded, you could use the following to color your
1600 if ( $test->is_ok && not $test->directive ) {
1601 # normal passing test
1602 print color 'green';
1604 elsif ( !$test->is_ok ) { # even if it's TODO
1605 print color 'white on_red';
1607 elsif ( $test->has_skip ) {
1608 print color 'white on_blue';
1611 elsif ( $test->has_todo ) {
1612 print color 'white';
1616 # plan, comment, and so on (anything which isn't a test line)
1617 print color 'black on_white';
1621 print shift->as_string;
1622 print color 'reset';
1629 Invoked when there are no more lines to be parsed. Since there is no
1630 accompanying L<TAP::Parser::Result> object the C<TAP::Parser> object is
1637 If you're looking for an EBNF grammar, see L<TAP::Parser::Grammar>.
1639 =head1 BACKWARDS COMPATIBILITY
1641 The Perl-QA list attempted to ensure backwards compatibility with
1642 L<Test::Harness>. However, there are some minor differences.
1650 A little-known feature of L<Test::Harness> is that it supported TODO
1654 ok 1 - We have liftoff
1655 not ok 2 - Anti-gravity device activated
1657 Under L<Test::Harness>, test number 2 would I<pass> because it was
1658 listed as a TODO test on the plan line. However, we are not aware of
1659 anyone actually using this feature and hard-coding test numbers is
1660 discouraged because it's very easy to add a test and break the test
1661 number sequence. This makes test suites very fragile. Instead, the
1662 following should be used:
1665 ok 1 - We have liftoff
1666 not ok 2 - Anti-gravity device activated # TODO
1668 =item * 'Missing' tests
1670 It rarely happens, but sometimes a harness might encounter
1679 L<Test::Harness> would report tests 3-14 as having failed. For the
1680 C<TAP::Parser>, these tests are not considered failed because they've
1681 never run. They're reported as parse failures (tests out of sequence).
1687 If you find you need to provide custom functionality (as you would have using
1688 L<Test::Harness::Straps>), you're in luck: C<TAP::Parser> and friends are
1689 designed to be easily plugged-into and/or subclassed.
1691 Before you start, it's important to know a few things:
1697 All C<TAP::*> objects inherit from L<TAP::Object>.
1701 Many C<TAP::*> classes have a I<SUBCLASSING> section to guide you.
1705 Note that C<TAP::Parser> is designed to be the central "maker" - ie: it is
1706 responsible for creating most new objects in the C<TAP::Parser::*> namespace.
1708 This makes it possible for you to have a single point of configuring what
1709 subclasses should be used, which means that in many cases you'll find
1710 you only need to sub-class one of the parser's components.
1712 The exception to this rule are I<SourceHandlers> & I<Iterators>, but those are
1713 both created with customizable I<IteratorFactory>.
1717 By subclassing, you may end up overriding undocumented methods. That's not
1718 a bad thing per se, but be forewarned that undocumented methods may change
1719 without warning from one release to the next - we cannot guarantee backwards
1720 compatibility. If any I<documented> method needs changing, it will be
1721 deprecated first, and changed in a later release.
1725 =head2 Parser Components
1729 A TAP parser consumes input from a single I<raw source> of TAP, which could come
1730 from anywhere (a file, an executable, a database, an IO handle, a URI, etc..).
1731 The source gets bundled up in a L<TAP::Parser::Source> object which gathers some
1732 meta data about it. The parser then uses a L<TAP::Parser::IteratorFactory> to
1733 determine which L<TAP::Parser::SourceHandler> to use to turn the raw source
1734 into a stream of TAP by way of L</Iterators>.
1736 If you simply want C<TAP::Parser> to handle a new source of TAP you probably
1737 don't need to subclass C<TAP::Parser> itself. Rather, you'll need to create a
1738 new L<TAP::Parser::SourceHandler> class, and just plug it into the parser using
1739 the I<sources> param to L</new>. Before you start writing one, read through
1740 L<TAP::Parser::IteratorFactory> to get a feel for how the system works first.
1742 If you find you really need to use your own iterator factory you can still do
1743 so without sub-classing C<TAP::Parser> by setting L</iterator_factory_class>.
1745 If you just need to customize the objects on creation, subclass L<TAP::Parser>
1746 and override L</make_iterator_factory>.
1748 Note that L</make_source> & L</make_perl_source> have been I<DEPRECATED> and
1753 A TAP parser uses I<iterators> to loop through the I<stream> of TAP read in
1754 from the I<source> it was given. There are a few types of Iterators available
1755 by default, all sub-classes of L<TAP::Parser::Iterator>. Choosing which
1756 iterator to use is the responsibility of the I<iterator factory>, though it
1757 simply delegates to the I<Source Handler> it uses.
1759 If you're writing your own L<TAP::Parser::SourceHandler>, you may need to
1760 create your own iterators too. If so you'll need to subclass
1761 L<TAP::Parser::Iterator>.
1763 Note that L</make_iterator> has been I<DEPRECATED> and is now removed.
1767 A TAP parser creates L<TAP::Parser::Result>s as it iterates through the
1768 input I<stream>. There are quite a few result types available; choosing
1769 which class to use is the responsibility of the I<result factory>.
1771 To create your own result types you have two options:
1777 Subclass L<TAP::Parser::Result> and register your new result type/class with
1778 the default L<TAP::Parser::ResultFactory>.
1782 Subclass L<TAP::Parser::ResultFactory> itself and implement your own
1783 L<TAP::Parser::Result> creation logic. Then you'll need to customize the
1784 class used by your parser by setting the C<result_factory_class> parameter.
1785 See L</new> for more details.
1789 If you need to customize the objects on creation, subclass L<TAP::Parser> and
1790 override L</make_result>.
1794 L<TAP::Parser::Grammar> is the heart of the parser. It tokenizes the TAP
1795 input I<stream> and produces results. If you need to customize its behaviour
1796 you should probably familiarize yourself with the source first. Enough
1799 Subclass L<TAP::Parser::Grammar> and customize your parser by setting the
1800 C<grammar_class> parameter. See L</new> for more details.
1802 If you need to customize the objects on creation, subclass L<TAP::Parser> and
1803 override L</make_grammar>
1805 =head1 ACKNOWLEDGMENTS
1807 All of the following have helped. Bug reports, patches, (im)moral
1808 support, or just words of encouragement have all been forthcoming.
1812 =item * Michael Schwern
1822 =item * Torsten Schoenfeld
1828 =item * Adam Kennedy
1832 =item * Adrian Howard
1836 =item * Andreas J. Koenig
1838 =item * Florian Ragwitz
1842 =item * Mark Stosberg
1846 =item * David Wheeler
1848 =item * Alex Vandiver
1850 =item * Cosimo Streppone
1852 =item * Ville Skyttä
1858 Curtis "Ovid" Poe <ovid@cpan.org>
1860 Andy Armstong <andy@hexten.net>
1862 Eric Wilhelm @ <ewilhelm at cpan dot org>
1864 Michael Peters <mpeters at plusthree dot com>
1866 Leif Eriksen <leif dot eriksen at bigpond dot com>
1868 Steve Purkis <spurkis@cpan.org>
1870 Nicholas Clark <nick@ccl4.org>
1872 Lee Johnson <notfadeaway at btinternet dot com>
1874 Philippe Bruhat <book@cpan.org>
1878 Please report any bugs or feature requests to
1879 C<bug-test-harness@rt.cpan.org>, or through the web interface at
1880 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Harness>.
1881 We will be notified, and then you'll automatically be notified of
1882 progress on your bug as we make changes.
1884 Obviously, bugs which include patches are best. If you prefer, you can
1885 patch against bleed by via anonymous checkout of the latest version:
1887 git clone git://github.com/AndyA/Test-Harness.git
1889 =head1 COPYRIGHT & LICENSE
1891 Copyright 2006-2008 Curtis "Ovid" Poe, all rights reserved.
1893 This program is free software; you can redistribute it and/or modify it
1894 under the same terms as Perl itself.