File Coverage

blib/lib/TAPx/Parser.pm
Criterion Covered Total %
statement 172 190 90.5
branch 76 92 82.6
condition 15 19 78.9
subroutine 32 33 96.9
pod 13 13 100.0
total 308 347 88.7


line stmt bran cond sub pod time code
1             package TAPx::Parser;
2              
3 11     11   269015 use strict;
  11         30  
  11         469  
4 11     11   68 use vars qw($VERSION @ISA);
  11         36  
  11         637  
5              
6 11     11   5796 use TAPx::Base;
  11         27  
  11         293  
7 11     11   6693 use TAPx::Parser::Grammar;
  11         30  
  11         420  
8 11     11   80 use TAPx::Parser::Result;
  11         23  
  11         201  
9 11     11   5559 use TAPx::Parser::Source;
  11         36  
  11         416  
10 11     11   6243 use TAPx::Parser::Source::Perl;
  11         31  
  11         337  
11 11     11   431 use TAPx::Parser::Iterator;
  11         24  
  11         765  
12              
13             @ISA = qw(TAPx::Base);
14              
15             =head1 NAME
16              
17             TAPx::Parser - Parse L output
18              
19             =head1 VERSION
20              
21             Version 0.50_07
22              
23             =cut
24              
25             $VERSION = '0.50_07';
26              
27             BEGIN {
28 11     11   39 foreach my $method (
29             qw<
30             _can_ignore_output
31             _end_tap
32             _plan_found
33             _start_tap
34             _stream
35             _spool
36             _grammar
37             _end_plan_error
38             _plan_error_found
39             exec
40             exit
41             is_good_plan
42             plan
43             tests_planned
44             tests_run
45             wait
46             in_todo
47             >
48             )
49             {
50 11     11   55 no strict 'refs';
  11         27  
  11         2131  
51              
52             # another tiny performance hack
53 187 100       567 if ( $method =~ /^_/ ) {
54             *$method = sub {
55 2991     2991   4390 my $self = shift;
56 2991 100       21798 return $self->{$method} unless @_;
57 1084 50       8913 unless ( ( ref $self ) =~ /^TAPx::Parser/ )
58             { # trusted methods
59 0         0 $self->_croak("$method() may not be set externally");
60             }
61 1084         3973 $self->{$method} = shift;
62 99         647 };
63             }
64             else {
65             *$method = sub {
66 2414     2414   86491 my $self = shift;
67 2414 100       19802 return $self->{$method} unless @_;
68 808         4340 $self->{$method} = shift;
69 88         27737 };
70             }
71             }
72             }
73              
74             ##############################################################################
75              
76             =head3 C
77              
78             Deprecated. Use C instead.
79              
80             =cut
81              
82             sub good_plan {
83 30     30 1 8691 warn 'good_plan() is deprecated. Please use "is_good_plan()"';
84 30         13183 goto &is_good_plan;
85             }
86              
87             =head1 SYNOPSIS
88              
89             use TAPx::Parser;
90              
91             my $parser = TAPx::Parser->new( { source => $source } );
92            
93             while ( my $result = $parser->next ) {
94             print $result->as_string;
95             }
96              
97             =head1 DESCRIPTION
98              
99             C is designed to produce a proper parse of TAP output. It is
100             ALPHA code and should be treated as such. The interface is now solid, but it
101             is still subject to change.
102              
103             For an example of how to run tests through this module, see the simple
104             harnesses C.
105              
106             =head1 METHODS
107              
108             =head2 Class methods
109              
110             =head3 C
111              
112             my $parser = TAPx::Parser->new(\%args);
113              
114             Returns a new C object.
115              
116             The arguments should be a hashref with I of the following keys:
117              
118             =over 4
119              
120             =item * C
121              
122             This is the preferred method of passing arguments to the constructor. To
123             determine how to handle the source, the following steps are taken.
124              
125             If the source contains a newline, it's assumed to be a string of raw TAP
126             output.
127              
128             If the source is a reference, it's assumed to be something to pass to the
129             C constructor. This is used internally and you should
130             not use it.
131              
132             Otherwise, the parser does a C<-e> check to see if the source exists. If so,
133             it attempts to execute the source and read the output as a stream. This is by
134             far the preferred method of using the parser.
135              
136             foreach my $file ( @test_files ) {
137             my $parser = TAPx::Parser->new( { source => $file } );
138             # do stuff with the parser
139             }
140              
141             =item * C
142              
143             The value should be the complete TAP output.
144              
145             =item * C
146              
147             If passed an array reference, will attempt to create the iterator by passing a
148             C object to C, using the array
149             reference strings as the command arguments to C<&IPC::Open3::open3>:
150              
151             exec => [ '/usr/bin/ruby', 't/my_test.rb' ]
152              
153             Note that C and C are mutually exclusive.
154              
155             =back
156              
157             The following keys are optional.
158              
159             =over 4
160              
161             =item * C
162              
163             If present, each callback corresponding to a given result type will be called
164             with the result as the argument if the C method is used:
165              
166             my %callbacks = (
167             test => \&test_callback,
168             plan => \&plan_callback,
169             comment => \&comment_callback,
170             bailout => \&bailout_callback,
171             unknown => \&unknown_callback,
172             );
173            
174             my $aggregator = TAPx::Parser::Aggregator->new;
175             foreach my $file ( @test_files ) {
176             my $parser = TAPx::Parser->new(
177             {
178             source => $file,
179             callbacks => \%callbacks,
180             }
181             );
182             $parser->run;
183             $aggregator->add( $file, $parser );
184             }
185              
186             =item * C
187              
188             If using a Perl file as a source, optional switches may be passed which will
189             be used when invoking the perl executable.
190              
191             my $parser = TAPx::Parser->new( {
192             source => $test_file,
193             switches => '-Ilib',
194             } );
195              
196             =item * C
197              
198             If passed a filehandle will write a copy of all parsed TAP to that handle.
199              
200             =back
201              
202             =cut
203              
204             # new implementation supplied by TAPx::Base
205              
206             ##############################################################################
207              
208             =head2 Instance methods
209              
210             =head3 C
211              
212             my $parser = TAPx::Parser->new( { source => $file } );
213             while ( my $result = $parser->next ) {
214             print $result->as_string, "\n";
215             }
216              
217             This method returns the results of the parsing, one result at a time. Note
218             that it is destructive. You can't rewind and examine previous results.
219              
220             If callbacks are used, they will be issued before this call returns.
221              
222             Each result returned is a subclass of C. See that
223             module and related classes for more information on how to use them.
224              
225             =cut
226              
227             sub _next {
228 413     413   722 my $self = shift;
229 413         1228 my $stream = $self->_stream;
230 413 100       2212 return if $stream->is_last;
231              
232 356         1232 my $result = $self->_grammar->tokenize( $stream->next );
233 356         1463 $self->_start_tap( $stream->is_first ); # must be after $stream->next
234              
235             # we still have to test for $result because of all sort of strange TAP
236             # edge cases (such as '1..0' plans for skipping everything)
237 356 100 100     3904 if ( $result && $result->is_test ) {
238 240         987 $self->in_todo( $result->has_todo );
239 240         723 $self->tests_run( $self->tests_run + 1 );
240 240 100       17899 if ( defined ( my $tests_planned = $self->tests_planned ) ) {
241 173 100       507 if ( $self->tests_run > $tests_planned ) {
242 18         94 $result->is_unplanned(1);
243             }
244             }
245             }
246              
247             # must set _end_tap first or else _validate chokes on ending plans
248 356         7422 $self->_validate($result);
249 356 100 100     1397 if ( $stream->is_last ) {
    100          
250 65         1407 $self->_end_tap(1);
251 65         317 $self->exit( $stream->exit );
252 65         275 $self->wait( $stream->wait );
253 65         241 $self->_finish;
254             }
255             elsif ( !$result->is_unknown && !$result->is_comment ) {
256 249         696 $self->_can_ignore_output(0);
257             }
258 356         1512 return $result;
259             }
260              
261             sub next {
262 413     413 1 87325 my $self = shift;
263 413         1248 my $result = $self->_next;
264              
265 413 100       1081 if ( defined $result ) {
266 352         413 my $code;
267 352 100       1342 if ( $code = $self->_callback_for( $result->type ) ) {
268 12         40 $code->($result);
269             }
270             else {
271 340         1267 $self->_make_callback( 'ELSE', $result );
272             }
273 352         1219 $self->_make_callback( 'ALL', $result );
274              
275             # Echo TAP to spool file
276 352         1236 $self->_write_to_spool($result);
277             }
278              
279 413         1822 return $result;
280             }
281              
282             sub _write_to_spool {
283 352     352   689 my ( $self, $result ) = @_;
284 352 50       2001 my $spool = $self->_spool or return;
285 0         0 print $spool $result->raw, "\n";
286             }
287              
288             ##############################################################################
289              
290             =head3 C
291              
292             $parser->run;
293              
294             This method merely runs the parser and parses all of the TAP.
295              
296             =cut
297              
298             sub run {
299 17     17 1 994 my $self = shift;
300 17         57 while ( defined( my $result = $self->next ) ) {
301              
302             # do nothing
303             }
304             }
305              
306             {
307              
308             # of the following, anything beginning with an underscore is strictly
309             # internal and should not be exposed.
310             my %initialize = (
311             _can_ignore_output => 1,
312             _end_tap => 0,
313             _plan_found => 0, # how many plans were found
314             _start_tap => 0,
315             plan => '', # the test plan (e.g., 1..3)
316             tap => '', # the TAP
317             tests_run => 0, # actual current test numbers
318             results => [], # TAP parser results
319             skipped => [], #
320             todo => [], #
321             passed => [], #
322             failed => [], #
323             actual_failed => [], # how many tests really failed
324             actual_passed => [], # how many tests really passed
325             todo_passed => [], # tests which unexpectedly succeed
326             parse_errors => [], # perfect TAP should have none
327             );
328              
329             # We seem to have this list hanging around all over the place. We could
330             # probably get it from somewhere else to avoid the repetition.
331             my @legal_callback = qw(
332             test
333             plan
334             comment
335             bailout
336             unknown
337             ALL
338             ELSE
339             );
340              
341             sub _initialize {
342 67     67   229 my ( $self, $arg_for ) = @_;
343              
344             # everything here is basically designed to convert any TAP source to a
345             # stream.
346 67   50     308 $arg_for ||= {};
347              
348 67         448 $self->SUPER::_initialize( $arg_for, \@legal_callback );
349              
350 66         326 my $stream = delete $arg_for->{stream};
351 66         146 my $tap = delete $arg_for->{tap};
352 66         174 my $source = delete $arg_for->{source};
353 66         147 my $exec = delete $arg_for->{exec};
354 66         147 my $merge = delete $arg_for->{merge};
355 66         163 my $spool = delete $arg_for->{spool};
356 66 50       164 if ( 1 < grep {defined} $stream, $tap, $source ) {
  198         785  
357 0         0 $self->_croak(
358             "You may only choose one of 'stream', 'tap', or'source'");
359             }
360 66 50 66     533 if ( $source && $exec ) {
361 0         0 $self->_croak(
362             '"source" and "exec" are mutually exclusive options');
363             }
364 66 100       508 if ($tap) {
    100          
    100          
365 15         182 $stream = TAPx::Parser::Iterator->new( [ split "\n" => $tap ] );
366             }
367             elsif ($exec) {
368 2         19 my $source = TAPx::Parser::Source->new;
369 2         9 $source->source($exec);
370 2         10 $stream = $source->get_stream;
371 2 50       43 if ( defined $stream ) {
372 2 50       43 if ( defined $stream->exit ) {
373 0         0 $self->exit( $stream->exit );
374             }
375 2 50       38 if ( defined $stream->wait ) {
376 0         0 $self->wait( $stream->wait );
377             }
378             }
379             }
380             elsif ($source) {
381 39 50       1586 if ( ref $source ) {
    50          
382 0         0 $stream = TAPx::Parser::Iterator->new($source);
383             }
384             elsif ( -e $source ) {
385              
386 39         689 my $perl = TAPx::Parser::Source::Perl->new;
387 39 50       479 $perl->switches( $arg_for->{switches} )
388             if $arg_for->{switches};
389              
390 39         184 $stream = $perl->source($source)->get_stream;
391 39 50       277 if ( defined $stream ) {
392 39 50       376 if ( defined $stream->exit ) {
393 0         0 $self->exit( $stream->exit );
394             }
395 39 50       190 if ( defined $stream->wait ) {
396 0         0 $self->wait( $stream->wait );
397             }
398             }
399             }
400             else {
401 0         0 $self->_croak("Cannot determine source for $source");
402             }
403             }
404              
405 66 50       495 unless ($stream) {
406 0         0 $self->_croak("PANIC: could not determine stream");
407             }
408              
409 66         1162 $self->_stream($stream);
410 66         449 $self->_start_tap(undef);
411 66         314 $self->_end_tap(undef);
412 66         1203 $self->_grammar( TAPx::Parser::Grammar->new($self) )
413             ; # eventually pass a version
414 66         236 $self->_spool($spool);
415              
416 66         1024 while ( my ( $k, $v ) = each %initialize ) {
417 1056 100       11248 $self->{$k} = 'ARRAY' eq ref $v ? [] : $v;
418             }
419              
420 66         1867 return $self;
421             }
422             }
423              
424             =head1 INDIVIDUAL RESULTS
425              
426             If you've read this far in the docs, you've seen this:
427              
428             while ( my $result = $parser->next ) {
429             print $result->as_string;
430             }
431              
432             Each result returned is a C subclass, referred to as
433             I.
434              
435             =head2 Result types
436              
437             Basically, you fetch individual results from the TAP. The five types, with
438             examples of each, are as follows:
439              
440             =over 4
441              
442             =item * Plan
443              
444             1..42
445              
446             =item * Test
447              
448             ok 3 - We should start with some foobar!
449              
450             =item * Comment
451              
452             # Hope we don't use up the foobar.
453              
454             =item * Bailout
455              
456             Bail out! We ran out of foobar!
457              
458             =item * Unknown
459              
460             ... yo, this ain't TAP! ...
461              
462             =back
463              
464             Each result fetched is a result object of a different type. There are common
465             methods to each result object and different types may have methods unique to
466             their type. Sometimes a type method may be overridden in a subclass, but its
467             use is guaranteed to be identical.
468              
469             =head2 Common type methods
470              
471             =head3 C
472              
473             Returns the type of result, such as C or C.
474              
475             =head3 C
476              
477             Prints a string representation of the token. This might not be the exact
478             output, however. Tests will have test numbers added if not present, TODO and
479             SKIP directives will be capitalized and, in general, things will be cleaned
480             up. If you need the original text for the token, see the C method.
481              
482             =head3 C
483              
484             Returns the original line of text which was parsed.
485              
486             =head3 C
487              
488             Indicates whether or not this is the test plan line.
489              
490             =head3 C
491              
492             Indicates whether or not this is a test line.
493              
494             =head3 C
495              
496             Indicates whether or not this is a comment.
497              
498             =head3 C
499              
500             Indicates whether or not this is bailout line.
501              
502             =head3 C
503              
504             Indicates whether or not the current line could be parsed.
505              
506             =head3 C
507              
508             if ( $result->is_ok ) { ... }
509              
510             Reports whether or not a given result has passed. Anything which is B a
511             test result returns true. This is merely provided as a convenient shortcut
512             which allows you to do this:
513              
514             my $parser = TAPx::Parser->new( { source => $source } );
515             while ( my $result = $parser->next ) {
516             # only print failing results
517             print $result->as_string unless $result->is_ok;
518             }
519              
520             =head2 C methods
521              
522             if ( $result->is_plan ) { ... }
523              
524             If the above evaluates as true, the following methods will be available on the
525             C<$result> object.
526              
527             =head3 C
528              
529             if ( $result->is_plan ) {
530             print $result->plan;
531             }
532              
533             This is merely a synonym for C.
534              
535             =head3 C
536              
537             my $planned = $result->tests_planned;
538              
539             Returns the number of tests planned. For example, a plan of C<1..17> will
540             cause this method to return '17'.
541              
542             =head3 C
543              
544             my $directive = $result->directive;
545              
546             If a SKIP directive is included with the plan, this method will return it.
547              
548             1..0 # SKIP: why bother?
549              
550             =head3 C
551              
552             my $explanation = $result->explanation;
553              
554             If a SKIP directive was included with the plan, this method will return the
555             explanation, if any.
556              
557             =head2 C methods
558              
559             if ( $result->is_comment ) { ... }
560              
561             If the above evaluates as true, the following methods will be available on the
562             C<$result> object.
563              
564             =head3 C
565              
566             if ( $result->is_comment ) {
567             my $comment = $result->comment;
568             print "I have something to say: $comment";
569             }
570              
571             =head2 C methods
572              
573             if ( $result->is_bailout ) { ... }
574              
575             If the above evaluates as true, the following methods will be available on the
576             C<$result> object.
577              
578             =head3 C
579              
580             if ( $result->is_bailout ) {
581             my $explanation = $result->explanation;
582             print "We bailed out because ($explanation)";
583             }
584              
585             If, and only if, a token is a bailout token, you can get an "explanation" via
586             this method. The explanation is the text after the mystical "Bail out!" words
587             which appear in the tap output.
588              
589             =head2 C methods
590              
591             if ( $result->is_unknown ) { ... }
592              
593             There are no unique methods for unknown results.
594              
595             =head2 C methods
596              
597             if ( $result->is_test ) { ... }
598              
599             If the above evaluates as true, the following methods will be available on the
600             C<$result> object.
601              
602             =head3 C
603              
604             my $ok = $result->ok;
605              
606             Returns the literal text of the C or C status.
607              
608             =head3 C
609              
610             my $test_number = $result->number;
611              
612             Returns the number of the test, even if the original TAP output did not supply
613             that number.
614              
615             =head3 C
616              
617             my $description = $result->description;
618              
619             Returns the description of the test, if any. This is the portion after the
620             test number but before the directive.
621              
622             =head3 C
623              
624             my $directive = $result->directive;
625              
626             Returns either C or C if either directive was present for a test
627             line.
628              
629             =head3 C
630              
631             my $explanation = $result->explanation;
632              
633             If a test had either a C or C directive, this method will return
634             the accompanying explantion, if present.
635              
636             not ok 17 - 'Pigs can fly' # TODO not enough acid
637              
638             For the above line, the explanation is I.
639              
640             =head3 C
641              
642             if ( $result->is_ok ) { ... }
643              
644             Returns a boolean value indicating whether or not the test passed. Remember
645             that for TODO tests, the test always passes.
646              
647             B this was formerly C. The latter method is deprecated and
648             will issue a warning.
649              
650             =head3 C
651              
652             if ( $result->is_actual_ok ) { ... }
653              
654             Returns a boolean value indicating whether or not the test passed, regardless
655             of its TODO status.
656              
657             B this was formerly C. The latter method is deprecated
658             and will issue a warning.
659              
660             =head3 C
661              
662             if ( $test->is_unplanned ) { ... }
663              
664             If a test number is greater than the number of planned tests, this method will
665             return true. Unplanned tests will I return false for C,
666             regardless of whether or not the test C (see
667             L for more information about this).
668              
669             =head3 C
670              
671             if ( $result->has_skip ) { ... }
672              
673             Returns a boolean value indicating whether or not this test had a SKIP
674             directive.
675              
676             =head3 C
677              
678             if ( $result->has_todo ) { ... }
679              
680             Returns a boolean value indicating whether or not this test had a TODO
681             directive.
682              
683             Note that TODO tests I pass. If you need to know whether or not
684             they really passed, check the C method.
685              
686             =head3 C
687              
688             if ( $parser->in_todo ) { ... }
689            
690             True while the most recent result was a TODO. Becomes true before the
691             TODO result is returned and stays true until just before the next non-
692             TODO test is returned.
693              
694             =head1 TOTAL RESULTS
695              
696             After parsing the TAP, there are many methods available to let you dig through
697             the results and determine what is meaningful to you.
698              
699             =head3 C
700              
701             my $plan = $parser->plan;
702              
703             Returns the test plan, if found.
704              
705             =head3 C
706              
707             my @passed = $parser->passed; # the test numbers which passed
708             my $passed = $parser->passed; # the number of tests which passed
709              
710             This method lets you know which (or how many) tests passed. If a test failed
711             but had a TODO directive, it will be counted as a passed test.
712              
713             =cut
714              
715 147     147 1 6487 sub passed { @{ shift->{passed} } }
  147         976  
716              
717             =head3 C
718              
719             my @failed = $parser->failed; # the test numbers which failed
720             my $failed = $parser->failed; # the number of tests which failed
721              
722             This method lets you know which (or how many) tests failed. If a test passed
723             but had a TODO directive, it will be counted as a failed test.
724              
725             =cut
726              
727 202     202 1 17212 sub failed { @{ shift->{failed} } }
  202         1110  
728              
729             =head3 C
730              
731             # the test numbers which actually passed
732             my @actual_passed = $parser->actual_passed;
733              
734             # the number of tests which actually passed
735             my $actual_passed = $parser->actual_passed;
736              
737             This method lets you know which (or how many) tests actually passed,
738             regardless of whether or not a TODO directive was found.
739              
740             =cut
741              
742 66     66 1 8775 sub actual_passed { @{ shift->{actual_passed} } }
  66         575  
743             *actual_ok = \&actual_passed;
744              
745             =head3 C
746              
747             This method is a synonym for C.
748              
749             =head3 C
750              
751             # the test numbers which actually failed
752             my @actual_failed = $parser->actual_failed;
753             # the number of tests which actually failed
754             my $actual_failed = $parser->actual_failed;
755              
756             This method lets you know which (or how many) tests actually failed,
757             regardless of whether or not a TODO directive was found.
758              
759             =cut
760              
761 66     66 1 8289 sub actual_failed { @{ shift->{actual_failed} } }
  66         511  
762              
763             ##############################################################################
764              
765             =head3 C
766              
767             my @todo = $parser->todo; # the test numbers with todo directives
768             my $todo = $parser->todo; # the number of tests with todo directives
769              
770             This method lets you know which (or how many) tests had TODO directives.
771              
772             =cut
773              
774 78     78 1 8337 sub todo { @{ shift->{todo} } }
  78         556  
775              
776             =head3 C
777              
778             # the test numbers which unexpectedly succeeded
779             my @todo_passed = $parser->todo_passed;
780             # the number of tests which unexpectedly succeeded
781             my $todo_passed = $parser->todo_passed;
782              
783             This method lets you know which (or how many) tests actually passed but were
784             declared as "TODO" tests.
785              
786             =cut
787              
788 113     113 1 12632 sub todo_passed { @{ shift->{todo_passed} } }
  113         775  
789              
790             ##############################################################################
791              
792             =head3 C
793              
794             # deprecated in favor of 'todo_passed'. This method was horribly misnamed.
795              
796             This was a badly misnamed method. It indicates which TODO tests unexpectedly
797             succeeded. Will now issue a warning and call C.
798              
799             =cut
800              
801             sub todo_failed {
802 0     0 1 0 warn
803             '"todo_failed" is deprecated. Please use "todo_passed". See the docs.';
804 0         0 goto &todo_passed;
805             }
806              
807             =head3 C
808              
809             my @skipped = $parser->skipped; # the test numbers with SKIP directives
810             my $skipped = $parser->skipped; # the number of tests with SKIP directives
811              
812             This method lets you know which (or how many) tests had SKIP directives.
813              
814             =cut
815              
816 86     86 1 21093 sub skipped { @{ shift->{skipped} } }
  86         553  
817              
818             ##############################################################################
819              
820             =head3 C
821              
822             if ( $parser->has_problems ) {
823             ...
824             }
825              
826             This is a 'catch-all' method which returns true if any tests have currently
827             failed, any TODO tests unexpectedly succeeded, or any parse errors.
828              
829             =cut
830              
831             sub has_problems {
832 42     42 1 65 my $self = shift;
833 42   33     135 return $self->failed
834             || $self->todo_passed
835             || $self->parse_errors
836             || $self->wait
837             || $self->exit;
838             }
839              
840             ##############################################################################
841              
842             =head3 C
843              
844             if ( $parser->is_good_plan ) { ... }
845              
846             Returns a boolean value indicating whether or not the number of tests planned
847             matches the number of tests run.
848              
849             B this was formerly C. The latter method is deprecated and
850             will issue a warning.
851              
852             And since we're on that subject ...
853              
854             =head3 C
855              
856             print $parser->tests_planned;
857              
858             Returns the number of tests planned, according to the plan. For example, a
859             plan of '1..17' will mean that 17 tests were planned.
860              
861             =head3 C
862              
863             print $parser->tests_run;
864              
865             Returns the number of tests which actually were run. Hopefully this will
866             match the number of C<< $parser->tests_planned >>.
867              
868              
869             =head3 C
870              
871             $parser->exit;
872              
873             Once the parser is done, this will return the exit status. If the parser ran
874             an executable, it returns the exit status of the executable.
875              
876             =head3 C
877              
878             $parser->wait;
879              
880             Once the parser is done, this will return the wait status. If the parser ran
881             an executable, it returns the wait status of the executable. Otherwise, this
882             mererely returns the C status.
883              
884             =head3 C
885              
886             my @errors = $parser->parse_errors; # the parser errors
887             my $errors = $parser->parse_errors; # the number of parser_errors
888              
889             Fortunately, all TAP output is perfect. In the event that it is not, this
890             method will return parser errors. Note that a junk line which the parser does
891             not recognize is C an error. This allows this parser to handle future
892             versions of TAP. The following are all TAP errors reported by the parser:
893              
894             =over 4
895              
896             =item * Misplaced plan
897              
898             The plan (for example, '1..5'), must only come at the beginning or end of the
899             TAP output.
900              
901             =item * No plan
902              
903             Gotta have a plan!
904              
905             =item * More than one plan
906              
907             1..3
908             ok 1 - input file opened
909             not ok 2 - first line of the input valid # todo some data
910             ok 3 read the rest of the file
911             1..3
912              
913             Right. Very funny. Don't do that.
914              
915             =item * Test numbers out of sequence
916              
917             1..3
918             ok 1 - input file opened
919             not ok 2 - first line of the input valid # todo some data
920             ok 2 read the rest of the file
921              
922             That last test line above should have the number '3' instead of '2'.
923              
924             Note that it's perfectly acceptable for some lines to have test numbers and
925             others to not have them. However, when a test number is found, it must be in
926             sequence. The following is also an error:
927            
928             1..3
929             ok 1 - input file opened
930             not ok - first line of the input valid # todo some data
931             ok 2 read the rest of the file
932              
933             But this is not:
934              
935             1..3
936             ok - input file opened
937             not ok - first line of the input valid # todo some data
938             ok 3 read the rest of the file
939              
940             =back
941              
942             =cut
943              
944 130     130 1 13035 sub parse_errors { @{ shift->{parse_errors} } }
  130         1041  
945              
946             sub _add_error {
947 50     50   121 my ( $self, $error ) = @_;
948 50         85 push @{ $self->{parse_errors} } => $error;
  50         177  
949 50         271 return $self;
950             }
951              
952             sub _aggregate_results {
953 240     240   355 my ( $self, $test ) = @_;
954              
955 240         691 my $num = $test->number;
956              
957 240 100       791 push @{ $self->{todo} } => $num if $test->has_todo;
  31         246  
958 240 100       1245 push @{ $self->{todo_passed} } => $num if $test->todo_passed;
  4         16  
959 240 100       968 push @{ $self->{passed} } => $num if $test->is_ok;
  200         747  
960 240 100       883 push @{ $self->{actual_passed} } => $num if $test->is_actual_ok;
  191         559  
961 240 100       1048 push @{ $self->{skipped} } => $num if $test->has_skip;
  12         254  
962              
963 240 100       714 push @{ $self->{actual_failed} } => $num if !$test->is_actual_ok;
  49         186  
964 240 100       674 push @{ $self->{failed} } => $num if !$test->is_ok;
  40         11013  
965             }
966              
967             {
968             my %validation_for = (
969             test => sub {
970             my ( $self, $test ) = @_;
971             local *__ANON__ = '__ANON__test_validation';
972              
973             $self->_check_ending_plan;
974             if ( $test->number ) {
975             if ( $test->number != $self->tests_run ) {
976             my $number = $test->number;
977             my $count = $self->tests_run;
978             $self->_add_error(
979             "Tests out of sequence. Found ($number) but expected ($count)"
980             );
981             }
982             }
983             else {
984             $test->_number( $self->tests_run );
985             }
986             $self->_aggregate_results($test);
987             },
988             plan => sub {
989             my ( $self, $plan ) = @_;
990             local *__ANON__ = '__ANON__plan_validation';
991             $self->tests_planned( $plan->tests_planned );
992             $self->plan( $plan->plan );
993             $self->_plan_found( $self->_plan_found + 1 );
994             if ( !$self->_start_tap && !$self->_end_tap ) {
995             if ( !$self->_end_plan_error && !$self->_can_ignore_output ) {
996             my $line = $plan->as_string;
997             $self->_end_plan_error(
998             "Plan ($line) must be at the beginning or end of the TAP output"
999             );
1000             }
1001             }
1002             },
1003             bailout => sub {
1004             my ( $self, $bailout ) = @_;
1005             local *__ANON__ = '__ANON__bailout_validation';
1006             $self->_check_ending_plan;
1007             },
1008             unknown => sub { },
1009             comment => sub { },
1010             );
1011              
1012             sub _check_ending_plan {
1013 243     243   369 my $self = shift;
1014 243 100 100     834 if ( !$self->_plan_error_found
1015             && ( my $error = $self->_end_plan_error ) )
1016             {
1017              
1018             # test output found after ending plan
1019 4         31 $self->_add_error($error);
1020 4         19 $self->_plan_error_found(1);
1021 4         20 $self->is_good_plan(0);
1022             }
1023 243         777 return $self;
1024             }
1025              
1026             sub _validate {
1027 356     356   560 my ( $self, $token ) = @_;
1028 356 100       902 return unless $token; # XXX edge case for 'no output'
1029 352         1734 my $type = $token->type;
1030 352         965 my $validate = $validation_for{$type};
1031 352 50       894 unless ($validate) {
1032              
1033             # should never happen
1034             # We could simply leave off keys for which no validation is
1035             # required, but that means that new token types in the future are
1036             # easily skipped here.
1037 0         0 $self->_croak("Don't know how how to validate '$type'");
1038             }
1039 352         1054 $self->$validate($token);
1040             }
1041             }
1042              
1043             sub _finish {
1044 65     65   195 my $self = shift;
1045              
1046             # sanity checks
1047 65 100       186 if ( !$self->_plan_found ) {
    100          
1048 5         39 $self->_add_error("No plan found in TAP output");
1049             }
1050             elsif ( $self->_plan_found > 1 ) {
1051 2         20 $self->_add_error("More than one plan found in TAP output");
1052             }
1053             else {
1054 58 100       301 $self->is_good_plan(1) unless defined $self->is_good_plan;
1055             }
1056 65 100 100     215 if ( $self->tests_run != ( $self->tests_planned || 0 ) ) {
1057 9         25 $self->is_good_plan(0);
1058 9 100       24 if ( defined( my $planned = $self->tests_planned ) ) {
1059 6         21 my $ran = $self->tests_run;
1060 6         41 $self->_add_error(
1061             "Bad plan. You planned $planned tests but ran $ran.");
1062             }
1063             }
1064 65 50       214 if ( $self->tests_run != ( $self->passed + $self->failed ) ) {
1065              
1066             # this should never happen
1067 0         0 my $actual = $self->tests_run;
1068 0         0 my $passed = $self->passed;
1069 0         0 my $failed = $self->failed;
1070 0         0 $self->_croak(
1071             "Panic: planned test count ($actual) did not equal sum of passed ($passed) and failed ($failed) tests!"
1072             );
1073             }
1074              
1075 65 100       343 $self->is_good_plan(0) unless defined $self->is_good_plan;
1076 65         120 return $self;
1077             }
1078              
1079             ##############################################################################
1080              
1081             =head2 CALLBACKS
1082              
1083             As mentioned earlier, a "callback" key may be added may be added to the
1084             C constructor. If present, each callback corresponding to a
1085             given result type will be called with the result as the argument if the C
1086             method is used. The callback is expected to be a subroutine reference (or
1087             anonymous subroutine) which is invoked with the parser result as its argument.
1088              
1089             my %callbacks = (
1090             test => \&test_callback,
1091             plan => \&plan_callback,
1092             comment => \&comment_callback,
1093             bailout => \&bailout_callback,
1094             unknown => \&unknown_callback,
1095             );
1096            
1097             my $aggregator = TAPx::Parser::Aggregator->new;
1098             foreach my $file ( @test_files ) {
1099             my $parser = TAPx::Parser->new(
1100             {
1101             source => $file,
1102             callbacks => \%callbacks,
1103             }
1104             );
1105             $parser->run;
1106             $aggregator->add( $file, $parser );
1107             }
1108              
1109             Callbacks may also be added like this:
1110              
1111             $parser->callback( test => \&test_callback );
1112             $parser->callback( plan => \&plan_callback );
1113              
1114             There are, at the present time, seven keys allowed for callbacks. These keys
1115             are case-sensitive.
1116              
1117             =over 4
1118              
1119             =item 1 C
1120              
1121             Invoked if C<< $result->is_test >> returns true.
1122              
1123             =item 2 C
1124              
1125             Invoked if C<< $result->is_plan >> returns true.
1126              
1127             =item 3 C
1128              
1129             Invoked if C<< $result->is_comment >> returns true.
1130              
1131             =item 4 C
1132              
1133             Invoked if C<< $result->is_unknown >> returns true.
1134              
1135             =item 5 C
1136              
1137             Invoked if C<< $result->is_unknown >> returns true.
1138              
1139             =item 6 C
1140              
1141             If a result does not have a callback defined for it, this callback will be
1142             invoked. Thus, if all five of the previous result types are specified as
1143             callbacks, this callback will I be invoked.
1144              
1145             =item 7 C
1146              
1147             This callback will always be invoked and this will happen for each result
1148             after one of the above six callbacks is invoked. For example, if
1149             C is loaded, you could use the following to color your test
1150             output:
1151              
1152             my %callbacks = (
1153             test => sub {
1154             my $test = shift;
1155             if ( $test->is_ok && not $test->directive ) {
1156             # normal passing test
1157             print color 'green';
1158             }
1159             elsif ( !$test->is_ok ) { # even if it's TODO
1160             print color 'white on_red';
1161             }
1162             elsif ( $test->has_skip ) {
1163             print color 'white on_blue';
1164            
1165             }
1166             elsif ( $test->has_todo ) {
1167             print color 'white';
1168             }
1169             },
1170             ELSE => sub {
1171             # plan, comment, and so on (anything which isn't a test line)
1172             print color 'black on_white';
1173             },
1174             ALL => sub {
1175             # now print them
1176             print shift->as_string;
1177             print color 'reset';
1178             print "\n";
1179             },
1180             );
1181              
1182             See C for an example of this.
1183              
1184             =back
1185              
1186             =head1 TAP GRAMMAR
1187              
1188             If you're looking for an EBNF grammar, see L.
1189              
1190             =head1 BACKWARDS COMPATABILITY
1191              
1192             The Perl-QA list attempted to ensure backwards compatability with
1193             L. However, there are some minor differences.
1194              
1195             =head2 Differences
1196              
1197             =over 4
1198              
1199             =item * TODO plans
1200              
1201             A little-known feature of C is that it supported TODO lists in
1202             the plan:
1203              
1204             1..2 todo 2
1205             ok 1 - We have liftoff
1206             not ok 2 - Anti-gravity device activated
1207              
1208             Under L, test number 2 would I because it was listed as a
1209             TODO test on the plan line. However, we are not aware of anyone actually
1210             using this feature and hard-coding test numbers is discouraged because it's
1211             very easy to add a test and break the test number sequence. This makes test
1212             suites very fragile. Instead, the following should be used:
1213              
1214             1..2
1215             ok 1 - We have liftoff
1216             not ok 2 - Anti-gravity device activated # TODO
1217              
1218             =item * 'Missing' tests
1219              
1220             It rarely happens, but sometimes a harness might encounter 'missing tests:
1221              
1222             ok 1
1223             ok 2
1224             ok 15
1225             ok 16
1226             ok 17
1227              
1228             L would report tests 3-14 as having failed. For the
1229             C, these tests are not considered failed because they've never
1230             run. They're reported as parse failures (tests out of sequence).
1231              
1232             =back
1233              
1234             =head1 ACKNOWLEDGEMENTS
1235              
1236             All of the following have helped. Bug reports, patches, (im)moral support, or
1237             just words of encouragement have all been forthcoming.
1238              
1239             =over 4
1240              
1241             =item * Michael Schwern
1242              
1243             =item * Andy Lester
1244              
1245             =item * chromatic
1246              
1247             =item * GEOFFR
1248              
1249             =item * Shlomi Fish
1250            
1251             =item * Torsten Schoenfeld
1252              
1253             =item * Jerry Gay
1254              
1255             =item * Aristotle
1256              
1257             =item * Adam Kennedy
1258              
1259             =item * Yves Orton
1260              
1261             =item * Adrian Howard
1262              
1263             =item * Sean & Lil
1264              
1265             =item * Andreas J. Koenig
1266              
1267             =item * Florian Ragwitz
1268              
1269             =item * Corion
1270              
1271             =item * Mark Stosberg
1272              
1273             =item * Andy Armstrong
1274              
1275             =item * Matt Kraai
1276              
1277             =back
1278              
1279             =head1 AUTHOR
1280              
1281             Curtis "Ovid" Poe, C<< >>
1282             Andy Armstong, C<< >>
1283              
1284             =head1 BUGS
1285              
1286             Please report any bugs or feature requests to
1287             C, or through the web interface at
1288             L.
1289             I will be notified, and then you'll automatically be notified of progress on
1290             your bug as I make changes.
1291              
1292             Obviously, bugs which include patches are best. If you prefer, you can patch
1293             against bleed by via anonymous checkout of the latest version:
1294              
1295             svn checkout http://svn.hexten.net/tapx
1296              
1297             =head1 COPYRIGHT & LICENSE
1298              
1299             Copyright 2006 Curtis "Ovid" Poe, all rights reserved.
1300              
1301             This program is free software; you can redistribute it and/or modify it
1302             under the same terms as Perl itself.
1303              
1304             =cut
1305              
1306             1;