1package TAP::Parser; 2 3use strict; 4use warnings; 5 6use TAP::Parser::Grammar (); 7use TAP::Parser::Result (); 8use TAP::Parser::ResultFactory (); 9use TAP::Parser::Source (); 10use TAP::Parser::Iterator (); 11use TAP::Parser::IteratorFactory (); 12use TAP::Parser::SourceHandler::Executable (); 13use TAP::Parser::SourceHandler::Perl (); 14use TAP::Parser::SourceHandler::File (); 15use TAP::Parser::SourceHandler::RawTAP (); 16use TAP::Parser::SourceHandler::Handle (); 17 18use Carp qw( confess ); 19 20use base 'TAP::Base'; 21 22=encoding utf8 23 24=head1 NAME 25 26TAP::Parser - Parse L<TAP|Test::Harness::TAP> output 27 28=head1 VERSION 29 30Version 3.30 31 32=cut 33 34our $VERSION = '3.30_01'; 35 36my $DEFAULT_TAP_VERSION = 12; 37my $MAX_TAP_VERSION = 13; 38 39$ENV{TAP_VERSION} = $MAX_TAP_VERSION; 40 41END { 42 43 # For VMS. 44 delete $ENV{TAP_VERSION}; 45} 46 47BEGIN { # making accessors 48 __PACKAGE__->mk_methods( 49 qw( 50 _iterator 51 _spool 52 exec 53 exit 54 is_good_plan 55 plan 56 tests_planned 57 tests_run 58 wait 59 version 60 in_todo 61 start_time 62 end_time 63 skip_all 64 grammar_class 65 result_factory_class 66 iterator_factory_class 67 ) 68 ); 69 70 sub _stream { # deprecated 71 my $self = shift; 72 $self->_iterator(@_); 73 } 74} # done making accessors 75 76=head1 SYNOPSIS 77 78 use TAP::Parser; 79 80 my $parser = TAP::Parser->new( { source => $source } ); 81 82 while ( my $result = $parser->next ) { 83 print $result->as_string; 84 } 85 86=head1 DESCRIPTION 87 88C<TAP::Parser> is designed to produce a proper parse of TAP output. For 89an example of how to run tests through this module, see the simple 90harnesses C<examples/>. 91 92There's a wiki dedicated to the Test Anything Protocol: 93 94L<http://testanything.org> 95 96It includes the TAP::Parser Cookbook: 97 98L<http://testanything.org/wiki/index.php/TAP::Parser_Cookbook> 99 100=head1 METHODS 101 102=head2 Class Methods 103 104=head3 C<new> 105 106 my $parser = TAP::Parser->new(\%args); 107 108Returns a new C<TAP::Parser> object. 109 110The arguments should be a hashref with I<one> of the following keys: 111 112=over 4 113 114=item * C<source> 115 116I<CHANGED in 3.18> 117 118This is the preferred method of passing input to the constructor. 119 120The C<source> is used to create a L<TAP::Parser::Source> that is passed to the 121L</iterator_factory_class> which in turn figures out how to handle the source and 122creates a <TAP::Parser::Iterator> for it. The iterator is used by the parser to 123read in the TAP stream. 124 125To configure the I<IteratorFactory> use the C<sources> parameter below. 126 127Note that C<source>, C<tap> and C<exec> are I<mutually exclusive>. 128 129=item * C<tap> 130 131I<CHANGED in 3.18> 132 133The value should be the complete TAP output. 134 135The I<tap> is used to create a L<TAP::Parser::Source> that is passed to the 136L</iterator_factory_class> which in turn figures out how to handle the source and 137creates a <TAP::Parser::Iterator> for it. The iterator is used by the parser to 138read in the TAP stream. 139 140To configure the I<IteratorFactory> use the C<sources> parameter below. 141 142Note that C<source>, C<tap> and C<exec> are I<mutually exclusive>. 143 144=item * C<exec> 145 146Must be passed an array reference. 147 148The I<exec> array ref is used to create a L<TAP::Parser::Source> that is passed 149to the L</iterator_factory_class> which in turn figures out how to handle the 150source and creates a <TAP::Parser::Iterator> for it. The iterator is used by 151the parser to read in the TAP stream. 152 153By default the L<TAP::Parser::SourceHandler::Executable> class will create a 154L<TAP::Parser::Iterator::Process> object to handle the source. This passes the 155array reference strings as command arguments to L<IPC::Open3::open3|IPC::Open3>: 156 157 exec => [ '/usr/bin/ruby', 't/my_test.rb' ] 158 159If any C<test_args> are given they will be appended to the end of the command 160argument list. 161 162To configure the I<IteratorFactory> use the C<sources> parameter below. 163 164Note that C<source>, C<tap> and C<exec> are I<mutually exclusive>. 165 166=back 167 168The following keys are optional. 169 170=over 4 171 172=item * C<sources> 173 174I<NEW to 3.18>. 175 176If set, C<sources> must be a hashref containing the names of the 177L<TAP::Parser::SourceHandler>s to load and/or configure. The values are a 178hash of configuration that will be accessible to the source handlers via 179L<TAP::Parser::Source/config_for>. 180 181For example: 182 183 sources => { 184 Perl => { exec => '/path/to/custom/perl' }, 185 File => { extensions => [ '.tap', '.txt' ] }, 186 MyCustom => { some => 'config' }, 187 } 188 189This will cause C<TAP::Parser> to pass custom configuration to two of the built- 190in source handlers - L<TAP::Parser::SourceHandler::Perl>, 191L<TAP::Parser::SourceHandler::File> - and attempt to load the C<MyCustom> 192class. See L<TAP::Parser::IteratorFactory/load_handlers> for more detail. 193 194The C<sources> parameter affects how C<source>, C<tap> and C<exec> parameters 195are handled. 196 197See L<TAP::Parser::IteratorFactory>, L<TAP::Parser::SourceHandler> and subclasses for 198more details. 199 200=item * C<callback> 201 202If present, each callback corresponding to a given result type will be called 203with the result as the argument if the C<run> method is used: 204 205 my %callbacks = ( 206 test => \&test_callback, 207 plan => \&plan_callback, 208 comment => \&comment_callback, 209 bailout => \&bailout_callback, 210 unknown => \&unknown_callback, 211 ); 212 213 my $aggregator = TAP::Parser::Aggregator->new; 214 for my $file ( @test_files ) { 215 my $parser = TAP::Parser->new( 216 { 217 source => $file, 218 callbacks => \%callbacks, 219 } 220 ); 221 $parser->run; 222 $aggregator->add( $file, $parser ); 223 } 224 225=item * C<switches> 226 227If using a Perl file as a source, optional switches may be passed which will 228be used when invoking the perl executable. 229 230 my $parser = TAP::Parser->new( { 231 source => $test_file, 232 switches => [ '-Ilib' ], 233 } ); 234 235=item * C<test_args> 236 237Used in conjunction with the C<source> and C<exec> option to supply a reference 238to an C<@ARGV> style array of arguments to pass to the test program. 239 240=item * C<spool> 241 242If passed a filehandle will write a copy of all parsed TAP to that handle. 243 244=item * C<merge> 245 246If false, STDERR is not captured (though it is 'relayed' to keep it 247somewhat synchronized with STDOUT.) 248 249If true, STDERR and STDOUT are the same filehandle. This may cause 250breakage if STDERR contains anything resembling TAP format, but does 251allow exact synchronization. 252 253Subtleties of this behavior may be platform-dependent and may change in 254the future. 255 256=item * C<grammar_class> 257 258This option was introduced to let you easily customize which I<grammar> class 259the parser should use. It defaults to L<TAP::Parser::Grammar>. 260 261See also L</make_grammar>. 262 263=item * C<result_factory_class> 264 265This option was introduced to let you easily customize which I<result> 266factory class the parser should use. It defaults to 267L<TAP::Parser::ResultFactory>. 268 269See also L</make_result>. 270 271=item * C<iterator_factory_class> 272 273I<CHANGED in 3.18> 274 275This option was introduced to let you easily customize which I<iterator> 276factory class the parser should use. It defaults to 277L<TAP::Parser::IteratorFactory>. 278 279=back 280 281=cut 282 283# new() implementation supplied by TAP::Base 284 285# This should make overriding behaviour of the Parser in subclasses easier: 286sub _default_grammar_class {'TAP::Parser::Grammar'} 287sub _default_result_factory_class {'TAP::Parser::ResultFactory'} 288sub _default_iterator_factory_class {'TAP::Parser::IteratorFactory'} 289 290############################################################################## 291 292=head2 Instance Methods 293 294=head3 C<next> 295 296 my $parser = TAP::Parser->new( { source => $file } ); 297 while ( my $result = $parser->next ) { 298 print $result->as_string, "\n"; 299 } 300 301This method returns the results of the parsing, one result at a time. Note 302that it is destructive. You can't rewind and examine previous results. 303 304If callbacks are used, they will be issued before this call returns. 305 306Each result returned is a subclass of L<TAP::Parser::Result>. See that 307module and related classes for more information on how to use them. 308 309=cut 310 311sub next { 312 my $self = shift; 313 return ( $self->{_iter} ||= $self->_iter )->(); 314} 315 316############################################################################## 317 318=head3 C<run> 319 320 $parser->run; 321 322This method merely runs the parser and parses all of the TAP. 323 324=cut 325 326sub run { 327 my $self = shift; 328 while ( defined( my $result = $self->next ) ) { 329 330 # do nothing 331 } 332} 333 334############################################################################## 335 336=head3 C<make_grammar> 337 338Make a new L<TAP::Parser::Grammar> object and return it. Passes through any 339arguments given. 340 341The C<grammar_class> can be customized, as described in L</new>. 342 343=head3 C<make_result> 344 345Make a new L<TAP::Parser::Result> object using the parser's 346L<TAP::Parser::ResultFactory>, and return it. Passes through any arguments 347given. 348 349The C<result_factory_class> can be customized, as described in L</new>. 350 351=head3 C<make_iterator_factory> 352 353I<NEW to 3.18>. 354 355Make a new L<TAP::Parser::IteratorFactory> object and return it. Passes through 356any arguments given. 357 358C<iterator_factory_class> can be customized, as described in L</new>. 359 360=cut 361 362# This should make overriding behaviour of the Parser in subclasses easier: 363sub make_iterator_factory { shift->iterator_factory_class->new(@_); } 364sub make_grammar { shift->grammar_class->new(@_); } 365sub make_result { shift->result_factory_class->make_result(@_); } 366 367{ 368 369 # of the following, anything beginning with an underscore is strictly 370 # internal and should not be exposed. 371 my %initialize = ( 372 version => $DEFAULT_TAP_VERSION, 373 plan => '', # the test plan (e.g., 1..3) 374 tests_run => 0, # actual current test numbers 375 skipped => [], # 376 todo => [], # 377 passed => [], # 378 failed => [], # 379 actual_failed => [], # how many tests really failed 380 actual_passed => [], # how many tests really passed 381 todo_passed => [], # tests which unexpectedly succeed 382 parse_errors => [], # perfect TAP should have none 383 ); 384 385 # We seem to have this list hanging around all over the place. We could 386 # probably get it from somewhere else to avoid the repetition. 387 my @legal_callback = qw( 388 test 389 version 390 plan 391 comment 392 bailout 393 unknown 394 yaml 395 ALL 396 ELSE 397 EOF 398 ); 399 400 my @class_overrides = qw( 401 grammar_class 402 result_factory_class 403 iterator_factory_class 404 ); 405 406 sub _initialize { 407 my ( $self, $arg_for ) = @_; 408 409 # everything here is basically designed to convert any TAP source to a 410 # TAP::Parser::Iterator. 411 412 # Shallow copy 413 my %args = %{ $arg_for || {} }; 414 415 $self->SUPER::_initialize( \%args, \@legal_callback ); 416 417 # get any class overrides out first: 418 for my $key (@class_overrides) { 419 my $default_method = "_default_$key"; 420 my $val = delete $args{$key} || $self->$default_method(); 421 $self->$key($val); 422 } 423 424 my $iterator = delete $args{iterator}; 425 $iterator ||= delete $args{stream}; # deprecated 426 my $tap = delete $args{tap}; 427 my $version = delete $args{version}; 428 my $raw_source = delete $args{source}; 429 my $sources = delete $args{sources}; 430 my $exec = delete $args{exec}; 431 my $merge = delete $args{merge}; 432 my $spool = delete $args{spool}; 433 my $switches = delete $args{switches}; 434 my $ignore_exit = delete $args{ignore_exit}; 435 my $test_args = delete $args{test_args} || []; 436 437 if ( 1 < grep {defined} $iterator, $tap, $raw_source, $exec ) { 438 $self->_croak( 439 "You may only choose one of 'exec', 'tap', 'source' or 'iterator'" 440 ); 441 } 442 443 if ( my @excess = sort keys %args ) { 444 $self->_croak("Unknown options: @excess"); 445 } 446 447 # convert $tap & $exec to $raw_source equiv. 448 my $type = ''; 449 my $source = TAP::Parser::Source->new; 450 if ($tap) { 451 $type = 'raw TAP'; 452 $source->raw( \$tap ); 453 } 454 elsif ($exec) { 455 $type = 'exec ' . $exec->[0]; 456 $source->raw( { exec => $exec } ); 457 } 458 elsif ($raw_source) { 459 $type = 'source ' . ref($raw_source) || $raw_source; 460 $source->raw( ref($raw_source) ? $raw_source : \$raw_source ); 461 } 462 elsif ($iterator) { 463 $type = 'iterator ' . ref($iterator); 464 } 465 466 if ( $source->raw ) { 467 my $src_factory = $self->make_iterator_factory($sources); 468 $source->merge($merge)->switches($switches) 469 ->test_args($test_args); 470 $iterator = $src_factory->make_iterator($source); 471 } 472 473 unless ($iterator) { 474 $self->_croak( 475 "PANIC: could not determine iterator for input $type"); 476 } 477 478 while ( my ( $k, $v ) = each %initialize ) { 479 $self->{$k} = 'ARRAY' eq ref $v ? [] : $v; 480 } 481 482 $self->version($version) if $version; 483 $self->_iterator($iterator); 484 $self->_spool($spool); 485 $self->ignore_exit($ignore_exit); 486 487 return $self; 488 } 489} 490 491=head1 INDIVIDUAL RESULTS 492 493If you've read this far in the docs, you've seen this: 494 495 while ( my $result = $parser->next ) { 496 print $result->as_string; 497 } 498 499Each result returned is a L<TAP::Parser::Result> subclass, referred to as 500I<result types>. 501 502=head2 Result types 503 504Basically, you fetch individual results from the TAP. The six types, with 505examples of each, are as follows: 506 507=over 4 508 509=item * Version 510 511 TAP version 12 512 513=item * Plan 514 515 1..42 516 517=item * Pragma 518 519 pragma +strict 520 521=item * Test 522 523 ok 3 - We should start with some foobar! 524 525=item * Comment 526 527 # Hope we don't use up the foobar. 528 529=item * Bailout 530 531 Bail out! We ran out of foobar! 532 533=item * Unknown 534 535 ... yo, this ain't TAP! ... 536 537=back 538 539Each result fetched is a result object of a different type. There are common 540methods to each result object and different types may have methods unique to 541their type. Sometimes a type method may be overridden in a subclass, but its 542use is guaranteed to be identical. 543 544=head2 Common type methods 545 546=head3 C<type> 547 548Returns the type of result, such as C<comment> or C<test>. 549 550=head3 C<as_string> 551 552Prints a string representation of the token. This might not be the exact 553output, however. Tests will have test numbers added if not present, TODO and 554SKIP directives will be capitalized and, in general, things will be cleaned 555up. If you need the original text for the token, see the C<raw> method. 556 557=head3 C<raw> 558 559Returns the original line of text which was parsed. 560 561=head3 C<is_plan> 562 563Indicates whether or not this is the test plan line. 564 565=head3 C<is_test> 566 567Indicates whether or not this is a test line. 568 569=head3 C<is_comment> 570 571Indicates whether or not this is a comment. Comments will generally only 572appear in the TAP stream if STDERR is merged to STDOUT. See the 573C<merge> option. 574 575=head3 C<is_bailout> 576 577Indicates whether or not this is bailout line. 578 579=head3 C<is_yaml> 580 581Indicates whether or not the current item is a YAML block. 582 583=head3 C<is_unknown> 584 585Indicates whether or not the current line could be parsed. 586 587=head3 C<is_ok> 588 589 if ( $result->is_ok ) { ... } 590 591Reports whether or not a given result has passed. Anything which is B<not> a 592test result returns true. This is merely provided as a convenient shortcut 593which allows you to do this: 594 595 my $parser = TAP::Parser->new( { source => $source } ); 596 while ( my $result = $parser->next ) { 597 # only print failing results 598 print $result->as_string unless $result->is_ok; 599 } 600 601=head2 C<plan> methods 602 603 if ( $result->is_plan ) { ... } 604 605If the above evaluates as true, the following methods will be available on the 606C<$result> object. 607 608=head3 C<plan> 609 610 if ( $result->is_plan ) { 611 print $result->plan; 612 } 613 614This is merely a synonym for C<as_string>. 615 616=head3 C<directive> 617 618 my $directive = $result->directive; 619 620If a SKIP directive is included with the plan, this method will return it. 621 622 1..0 # SKIP: why bother? 623 624=head3 C<explanation> 625 626 my $explanation = $result->explanation; 627 628If a SKIP directive was included with the plan, this method will return the 629explanation, if any. 630 631=head2 C<pragma> methods 632 633 if ( $result->is_pragma ) { ... } 634 635If the above evaluates as true, the following methods will be available on the 636C<$result> object. 637 638=head3 C<pragmas> 639 640Returns a list of pragmas each of which is a + or - followed by the 641pragma name. 642 643=head2 C<comment> methods 644 645 if ( $result->is_comment ) { ... } 646 647If the above evaluates as true, the following methods will be available on the 648C<$result> object. 649 650=head3 C<comment> 651 652 if ( $result->is_comment ) { 653 my $comment = $result->comment; 654 print "I have something to say: $comment"; 655 } 656 657=head2 C<bailout> methods 658 659 if ( $result->is_bailout ) { ... } 660 661If the above evaluates as true, the following methods will be available on the 662C<$result> object. 663 664=head3 C<explanation> 665 666 if ( $result->is_bailout ) { 667 my $explanation = $result->explanation; 668 print "We bailed out because ($explanation)"; 669 } 670 671If, and only if, a token is a bailout token, you can get an "explanation" via 672this method. The explanation is the text after the mystical "Bail out!" words 673which appear in the tap output. 674 675=head2 C<unknown> methods 676 677 if ( $result->is_unknown ) { ... } 678 679There are no unique methods for unknown results. 680 681=head2 C<test> methods 682 683 if ( $result->is_test ) { ... } 684 685If the above evaluates as true, the following methods will be available on the 686C<$result> object. 687 688=head3 C<ok> 689 690 my $ok = $result->ok; 691 692Returns the literal text of the C<ok> or C<not ok> status. 693 694=head3 C<number> 695 696 my $test_number = $result->number; 697 698Returns the number of the test, even if the original TAP output did not supply 699that number. 700 701=head3 C<description> 702 703 my $description = $result->description; 704 705Returns the description of the test, if any. This is the portion after the 706test number but before the directive. 707 708=head3 C<directive> 709 710 my $directive = $result->directive; 711 712Returns either C<TODO> or C<SKIP> if either directive was present for a test 713line. 714 715=head3 C<explanation> 716 717 my $explanation = $result->explanation; 718 719If a test had either a C<TODO> or C<SKIP> directive, this method will return 720the accompanying explanation, if present. 721 722 not ok 17 - 'Pigs can fly' # TODO not enough acid 723 724For the above line, the explanation is I<not enough acid>. 725 726=head3 C<is_ok> 727 728 if ( $result->is_ok ) { ... } 729 730Returns a boolean value indicating whether or not the test passed. Remember 731that for TODO tests, the test always passes. 732 733B<Note:> this was formerly C<passed>. The latter method is deprecated and 734will issue a warning. 735 736=head3 C<is_actual_ok> 737 738 if ( $result->is_actual_ok ) { ... } 739 740Returns a boolean value indicating whether or not the test passed, regardless 741of its TODO status. 742 743B<Note:> this was formerly C<actual_passed>. The latter method is deprecated 744and will issue a warning. 745 746=head3 C<is_unplanned> 747 748 if ( $test->is_unplanned ) { ... } 749 750If a test number is greater than the number of planned tests, this method will 751return true. Unplanned tests will I<always> return false for C<is_ok>, 752regardless of whether or not the test C<has_todo> (see 753L<TAP::Parser::Result::Test> for more information about this). 754 755=head3 C<has_skip> 756 757 if ( $result->has_skip ) { ... } 758 759Returns a boolean value indicating whether or not this test had a SKIP 760directive. 761 762=head3 C<has_todo> 763 764 if ( $result->has_todo ) { ... } 765 766Returns a boolean value indicating whether or not this test had a TODO 767directive. 768 769Note that TODO tests I<always> pass. If you need to know whether or not 770they really passed, check the C<is_actual_ok> method. 771 772=head3 C<in_todo> 773 774 if ( $parser->in_todo ) { ... } 775 776True while the most recent result was a TODO. Becomes true before the 777TODO result is returned and stays true until just before the next non- 778TODO test is returned. 779 780=head1 TOTAL RESULTS 781 782After parsing the TAP, there are many methods available to let you dig through 783the results and determine what is meaningful to you. 784 785=head2 Individual Results 786 787These results refer to individual tests which are run. 788 789=head3 C<passed> 790 791 my @passed = $parser->passed; # the test numbers which passed 792 my $passed = $parser->passed; # the number of tests which passed 793 794This method lets you know which (or how many) tests passed. If a test failed 795but had a TODO directive, it will be counted as a passed test. 796 797=cut 798 799sub passed { 800 return @{ $_[0]->{passed} } 801 if ref $_[0]->{passed}; 802 return wantarray ? 1 .. $_[0]->{passed} : $_[0]->{passed}; 803} 804 805=head3 C<failed> 806 807 my @failed = $parser->failed; # the test numbers which failed 808 my $failed = $parser->failed; # the number of tests which failed 809 810This method lets you know which (or how many) tests failed. If a test passed 811but had a TODO directive, it will B<NOT> be counted as a failed test. 812 813=cut 814 815sub failed { @{ shift->{failed} } } 816 817=head3 C<actual_passed> 818 819 # the test numbers which actually passed 820 my @actual_passed = $parser->actual_passed; 821 822 # the number of tests which actually passed 823 my $actual_passed = $parser->actual_passed; 824 825This method lets you know which (or how many) tests actually passed, 826regardless of whether or not a TODO directive was found. 827 828=cut 829 830sub actual_passed { 831 return @{ $_[0]->{actual_passed} } 832 if ref $_[0]->{actual_passed}; 833 return wantarray ? 1 .. $_[0]->{actual_passed} : $_[0]->{actual_passed}; 834} 835*actual_ok = \&actual_passed; 836 837=head3 C<actual_ok> 838 839This method is a synonym for C<actual_passed>. 840 841=head3 C<actual_failed> 842 843 # the test numbers which actually failed 844 my @actual_failed = $parser->actual_failed; 845 846 # the number of tests which actually failed 847 my $actual_failed = $parser->actual_failed; 848 849This method lets you know which (or how many) tests actually failed, 850regardless of whether or not a TODO directive was found. 851 852=cut 853 854sub actual_failed { @{ shift->{actual_failed} } } 855 856############################################################################## 857 858=head3 C<todo> 859 860 my @todo = $parser->todo; # the test numbers with todo directives 861 my $todo = $parser->todo; # the number of tests with todo directives 862 863This method lets you know which (or how many) tests had TODO directives. 864 865=cut 866 867sub todo { @{ shift->{todo} } } 868 869=head3 C<todo_passed> 870 871 # the test numbers which unexpectedly succeeded 872 my @todo_passed = $parser->todo_passed; 873 874 # the number of tests which unexpectedly succeeded 875 my $todo_passed = $parser->todo_passed; 876 877This method lets you know which (or how many) tests actually passed but were 878declared as "TODO" tests. 879 880=cut 881 882sub todo_passed { @{ shift->{todo_passed} } } 883 884############################################################################## 885 886=head3 C<todo_failed> 887 888 # deprecated in favor of 'todo_passed'. This method was horribly misnamed. 889 890This was a badly misnamed method. It indicates which TODO tests unexpectedly 891succeeded. Will now issue a warning and call C<todo_passed>. 892 893=cut 894 895sub todo_failed { 896 warn 897 '"todo_failed" is deprecated. Please use "todo_passed". See the docs.'; 898 goto &todo_passed; 899} 900 901=head3 C<skipped> 902 903 my @skipped = $parser->skipped; # the test numbers with SKIP directives 904 my $skipped = $parser->skipped; # the number of tests with SKIP directives 905 906This method lets you know which (or how many) tests had SKIP directives. 907 908=cut 909 910sub skipped { @{ shift->{skipped} } } 911 912=head2 Pragmas 913 914=head3 C<pragma> 915 916Get or set a pragma. To get the state of a pragma: 917 918 if ( $p->pragma('strict') ) { 919 # be strict 920 } 921 922To set the state of a pragma: 923 924 $p->pragma('strict', 1); # enable strict mode 925 926=cut 927 928sub pragma { 929 my ( $self, $pragma ) = splice @_, 0, 2; 930 931 return $self->{pragma}->{$pragma} unless @_; 932 933 if ( my $state = shift ) { 934 $self->{pragma}->{$pragma} = 1; 935 } 936 else { 937 delete $self->{pragma}->{$pragma}; 938 } 939 940 return; 941} 942 943=head3 C<pragmas> 944 945Get a list of all the currently enabled pragmas: 946 947 my @pragmas_enabled = $p->pragmas; 948 949=cut 950 951sub pragmas { sort keys %{ shift->{pragma} || {} } } 952 953=head2 Summary Results 954 955These results are "meta" information about the total results of an individual 956test program. 957 958=head3 C<plan> 959 960 my $plan = $parser->plan; 961 962Returns the test plan, if found. 963 964=head3 C<good_plan> 965 966Deprecated. Use C<is_good_plan> instead. 967 968=cut 969 970sub good_plan { 971 warn 'good_plan() is deprecated. Please use "is_good_plan()"'; 972 goto &is_good_plan; 973} 974 975############################################################################## 976 977=head3 C<is_good_plan> 978 979 if ( $parser->is_good_plan ) { ... } 980 981Returns a boolean value indicating whether or not the number of tests planned 982matches the number of tests run. 983 984B<Note:> this was formerly C<good_plan>. The latter method is deprecated and 985will issue a warning. 986 987And since we're on that subject ... 988 989=head3 C<tests_planned> 990 991 print $parser->tests_planned; 992 993Returns the number of tests planned, according to the plan. For example, a 994plan of '1..17' will mean that 17 tests were planned. 995 996=head3 C<tests_run> 997 998 print $parser->tests_run; 999 1000Returns the number of tests which actually were run. Hopefully this will 1001match the number of C<< $parser->tests_planned >>. 1002 1003=head3 C<skip_all> 1004 1005Returns a true value (actually the reason for skipping) if all tests 1006were skipped. 1007 1008=head3 C<start_time> 1009 1010Returns the time when the Parser was created. 1011 1012=head3 C<end_time> 1013 1014Returns the time when the end of TAP input was seen. 1015 1016=head3 C<has_problems> 1017 1018 if ( $parser->has_problems ) { 1019 ... 1020 } 1021 1022This is a 'catch-all' method which returns true if any tests have currently 1023failed, any TODO tests unexpectedly succeeded, or any parse errors occurred. 1024 1025=cut 1026 1027sub has_problems { 1028 my $self = shift; 1029 return 1030 $self->failed 1031 || $self->parse_errors 1032 || ( !$self->ignore_exit && ( $self->wait || $self->exit ) ); 1033} 1034 1035=head3 C<version> 1036 1037 $parser->version; 1038 1039Once the parser is done, this will return the version number for the 1040parsed TAP. Version numbers were introduced with TAP version 13 so if no 1041version number is found version 12 is assumed. 1042 1043=head3 C<exit> 1044 1045 $parser->exit; 1046 1047Once the parser is done, this will return the exit status. If the parser ran 1048an executable, it returns the exit status of the executable. 1049 1050=head3 C<wait> 1051 1052 $parser->wait; 1053 1054Once the parser is done, this will return the wait status. If the parser ran 1055an executable, it returns the wait status of the executable. Otherwise, this 1056merely returns the C<exit> status. 1057 1058=head2 C<ignore_exit> 1059 1060 $parser->ignore_exit(1); 1061 1062Tell the parser to ignore the exit status from the test when determining 1063whether the test passed. Normally tests with non-zero exit status are 1064considered to have failed even if all individual tests passed. In cases 1065where it is not possible to control the exit value of the test script 1066use this option to ignore it. 1067 1068=cut 1069 1070sub ignore_exit { shift->pragma( 'ignore_exit', @_ ) } 1071 1072=head3 C<parse_errors> 1073 1074 my @errors = $parser->parse_errors; # the parser errors 1075 my $errors = $parser->parse_errors; # the number of parser_errors 1076 1077Fortunately, all TAP output is perfect. In the event that it is not, this 1078method will return parser errors. Note that a junk line which the parser does 1079not recognize is C<not> an error. This allows this parser to handle future 1080versions of TAP. The following are all TAP errors reported by the parser: 1081 1082=over 4 1083 1084=item * Misplaced plan 1085 1086The plan (for example, '1..5'), must only come at the beginning or end of the 1087TAP output. 1088 1089=item * No plan 1090 1091Gotta have a plan! 1092 1093=item * More than one plan 1094 1095 1..3 1096 ok 1 - input file opened 1097 not ok 2 - first line of the input valid # todo some data 1098 ok 3 read the rest of the file 1099 1..3 1100 1101Right. Very funny. Don't do that. 1102 1103=item * Test numbers out of sequence 1104 1105 1..3 1106 ok 1 - input file opened 1107 not ok 2 - first line of the input valid # todo some data 1108 ok 2 read the rest of the file 1109 1110That last test line above should have the number '3' instead of '2'. 1111 1112Note that it's perfectly acceptable for some lines to have test numbers and 1113others to not have them. However, when a test number is found, it must be in 1114sequence. The following is also an error: 1115 1116 1..3 1117 ok 1 - input file opened 1118 not ok - first line of the input valid # todo some data 1119 ok 2 read the rest of the file 1120 1121But this is not: 1122 1123 1..3 1124 ok - input file opened 1125 not ok - first line of the input valid # todo some data 1126 ok 3 read the rest of the file 1127 1128=back 1129 1130=cut 1131 1132sub parse_errors { @{ shift->{parse_errors} } } 1133 1134sub _add_error { 1135 my ( $self, $error ) = @_; 1136 push @{ $self->{parse_errors} } => $error; 1137 return $self; 1138} 1139 1140sub _make_state_table { 1141 my $self = shift; 1142 my %states; 1143 my %planned_todo = (); 1144 1145 # These transitions are defaults for all states 1146 my %state_globals = ( 1147 comment => {}, 1148 bailout => {}, 1149 yaml => {}, 1150 version => { 1151 act => sub { 1152 $self->_add_error( 1153 'If TAP version is present it must be the first line of output' 1154 ); 1155 }, 1156 }, 1157 unknown => { 1158 act => sub { 1159 my $unk = shift; 1160 if ( $self->pragma('strict') ) { 1161 $self->_add_error( 1162 'Unknown TAP token: "' . $unk->raw . '"' ); 1163 } 1164 }, 1165 }, 1166 pragma => { 1167 act => sub { 1168 my ($pragma) = @_; 1169 for my $pr ( $pragma->pragmas ) { 1170 if ( $pr =~ /^ ([-+])(\w+) $/x ) { 1171 $self->pragma( $2, $1 eq '+' ); 1172 } 1173 } 1174 }, 1175 }, 1176 ); 1177 1178 # Provides default elements for transitions 1179 my %state_defaults = ( 1180 plan => { 1181 act => sub { 1182 my ($plan) = @_; 1183 $self->tests_planned( $plan->tests_planned ); 1184 $self->plan( $plan->plan ); 1185 if ( $plan->has_skip ) { 1186 $self->skip_all( $plan->explanation 1187 || '(no reason given)' ); 1188 } 1189 1190 $planned_todo{$_}++ for @{ $plan->todo_list }; 1191 }, 1192 }, 1193 test => { 1194 act => sub { 1195 my ($test) = @_; 1196 1197 my ( $number, $tests_run ) 1198 = ( $test->number, ++$self->{tests_run} ); 1199 1200 # Fake TODO state 1201 if ( defined $number && delete $planned_todo{$number} ) { 1202 $test->set_directive('TODO'); 1203 } 1204 1205 my $has_todo = $test->has_todo; 1206 1207 $self->in_todo($has_todo); 1208 if ( defined( my $tests_planned = $self->tests_planned ) ) { 1209 if ( $tests_run > $tests_planned ) { 1210 $test->is_unplanned(1); 1211 } 1212 } 1213 1214 if ( defined $number ) { 1215 if ( $number != $tests_run ) { 1216 my $count = $tests_run; 1217 $self->_add_error( "Tests out of sequence. Found " 1218 . "($number) but expected ($count)" ); 1219 } 1220 } 1221 else { 1222 $test->_number( $number = $tests_run ); 1223 } 1224 1225 push @{ $self->{todo} } => $number if $has_todo; 1226 push @{ $self->{todo_passed} } => $number 1227 if $test->todo_passed; 1228 push @{ $self->{skipped} } => $number 1229 if $test->has_skip; 1230 1231 push @{ $self->{ $test->is_ok ? 'passed' : 'failed' } } => 1232 $number; 1233 push @{ 1234 $self->{ 1235 $test->is_actual_ok 1236 ? 'actual_passed' 1237 : 'actual_failed' 1238 } 1239 } => $number; 1240 }, 1241 }, 1242 yaml => { act => sub { }, }, 1243 ); 1244 1245 # Each state contains a hash the keys of which match a token type. For 1246 # each token 1247 # type there may be: 1248 # act A coderef to run 1249 # goto The new state to move to. Stay in this state if 1250 # missing 1251 # continue Goto the new state and run the new state for the 1252 # current token 1253 %states = ( 1254 INIT => { 1255 version => { 1256 act => sub { 1257 my ($version) = @_; 1258 my $ver_num = $version->version; 1259 if ( $ver_num <= $DEFAULT_TAP_VERSION ) { 1260 my $ver_min = $DEFAULT_TAP_VERSION + 1; 1261 $self->_add_error( 1262 "Explicit TAP version must be at least " 1263 . "$ver_min. Got version $ver_num" ); 1264 $ver_num = $DEFAULT_TAP_VERSION; 1265 } 1266 if ( $ver_num > $MAX_TAP_VERSION ) { 1267 $self->_add_error( 1268 "TAP specified version $ver_num but " 1269 . "we don't know about versions later " 1270 . "than $MAX_TAP_VERSION" ); 1271 $ver_num = $MAX_TAP_VERSION; 1272 } 1273 $self->version($ver_num); 1274 $self->_grammar->set_version($ver_num); 1275 }, 1276 goto => 'PLAN' 1277 }, 1278 plan => { goto => 'PLANNED' }, 1279 test => { goto => 'UNPLANNED' }, 1280 }, 1281 PLAN => { 1282 plan => { goto => 'PLANNED' }, 1283 test => { goto => 'UNPLANNED' }, 1284 }, 1285 PLANNED => { 1286 test => { goto => 'PLANNED_AFTER_TEST' }, 1287 plan => { 1288 act => sub { 1289 my ($version) = @_; 1290 $self->_add_error( 1291 'More than one plan found in TAP output'); 1292 }, 1293 }, 1294 }, 1295 PLANNED_AFTER_TEST => { 1296 test => { goto => 'PLANNED_AFTER_TEST' }, 1297 plan => { act => sub { }, continue => 'PLANNED' }, 1298 yaml => { goto => 'PLANNED' }, 1299 }, 1300 GOT_PLAN => { 1301 test => { 1302 act => sub { 1303 my ($plan) = @_; 1304 my $line = $self->plan; 1305 $self->_add_error( 1306 "Plan ($line) must be at the beginning " 1307 . "or end of the TAP output" ); 1308 $self->is_good_plan(0); 1309 }, 1310 continue => 'PLANNED' 1311 }, 1312 plan => { continue => 'PLANNED' }, 1313 }, 1314 UNPLANNED => { 1315 test => { goto => 'UNPLANNED_AFTER_TEST' }, 1316 plan => { goto => 'GOT_PLAN' }, 1317 }, 1318 UNPLANNED_AFTER_TEST => { 1319 test => { act => sub { }, continue => 'UNPLANNED' }, 1320 plan => { act => sub { }, continue => 'UNPLANNED' }, 1321 yaml => { goto => 'UNPLANNED' }, 1322 }, 1323 ); 1324 1325 # Apply globals and defaults to state table 1326 for my $name ( keys %states ) { 1327 1328 # Merge with globals 1329 my $st = { %state_globals, %{ $states{$name} } }; 1330 1331 # Add defaults 1332 for my $next ( sort keys %{$st} ) { 1333 if ( my $default = $state_defaults{$next} ) { 1334 for my $def ( sort keys %{$default} ) { 1335 $st->{$next}->{$def} ||= $default->{$def}; 1336 } 1337 } 1338 } 1339 1340 # Stuff back in table 1341 $states{$name} = $st; 1342 } 1343 1344 return \%states; 1345} 1346 1347=head3 C<get_select_handles> 1348 1349Get an a list of file handles which can be passed to C<select> to 1350determine the readiness of this parser. 1351 1352=cut 1353 1354sub get_select_handles { shift->_iterator->get_select_handles } 1355 1356sub _grammar { 1357 my $self = shift; 1358 return $self->{_grammar} = shift if @_; 1359 1360 return $self->{_grammar} ||= $self->make_grammar( 1361 { iterator => $self->_iterator, 1362 parser => $self, 1363 version => $self->version 1364 } 1365 ); 1366} 1367 1368sub _iter { 1369 my $self = shift; 1370 my $iterator = $self->_iterator; 1371 my $grammar = $self->_grammar; 1372 my $spool = $self->_spool; 1373 my $state = 'INIT'; 1374 my $state_table = $self->_make_state_table; 1375 1376 $self->start_time( $self->get_time ); 1377 1378 # Make next_state closure 1379 my $next_state = sub { 1380 my $token = shift; 1381 my $type = $token->type; 1382 TRANS: { 1383 my $state_spec = $state_table->{$state} 1384 or die "Illegal state: $state"; 1385 1386 if ( my $next = $state_spec->{$type} ) { 1387 if ( my $act = $next->{act} ) { 1388 $act->($token); 1389 } 1390 if ( my $cont = $next->{continue} ) { 1391 $state = $cont; 1392 redo TRANS; 1393 } 1394 elsif ( my $goto = $next->{goto} ) { 1395 $state = $goto; 1396 } 1397 } 1398 else { 1399 confess("Unhandled token type: $type\n"); 1400 } 1401 } 1402 return $token; 1403 }; 1404 1405 # Handle end of stream - which means either pop a block or finish 1406 my $end_handler = sub { 1407 $self->exit( $iterator->exit ); 1408 $self->wait( $iterator->wait ); 1409 $self->_finish; 1410 return; 1411 }; 1412 1413 # Finally make the closure that we return. For performance reasons 1414 # there are two versions of the returned function: one that handles 1415 # callbacks and one that does not. 1416 if ( $self->_has_callbacks ) { 1417 return sub { 1418 my $result = eval { $grammar->tokenize }; 1419 $self->_add_error($@) if $@; 1420 1421 if ( defined $result ) { 1422 $result = $next_state->($result); 1423 1424 if ( my $code = $self->_callback_for( $result->type ) ) { 1425 $_->($result) for @{$code}; 1426 } 1427 else { 1428 $self->_make_callback( 'ELSE', $result ); 1429 } 1430 1431 $self->_make_callback( 'ALL', $result ); 1432 1433 # Echo TAP to spool file 1434 print {$spool} $result->raw, "\n" if $spool; 1435 } 1436 else { 1437 $result = $end_handler->(); 1438 $self->_make_callback( 'EOF', $self ) 1439 unless defined $result; 1440 } 1441 1442 return $result; 1443 }; 1444 } # _has_callbacks 1445 else { 1446 return sub { 1447 my $result = eval { $grammar->tokenize }; 1448 $self->_add_error($@) if $@; 1449 1450 if ( defined $result ) { 1451 $result = $next_state->($result); 1452 1453 # Echo TAP to spool file 1454 print {$spool} $result->raw, "\n" if $spool; 1455 } 1456 else { 1457 $result = $end_handler->(); 1458 } 1459 1460 return $result; 1461 }; 1462 } # no callbacks 1463} 1464 1465sub _finish { 1466 my $self = shift; 1467 1468 $self->end_time( $self->get_time ); 1469 1470 # Avoid leaks 1471 $self->_iterator(undef); 1472 $self->_grammar(undef); 1473 1474 # If we just delete the iter we won't get a fault if it's recreated. 1475 # Instead we set it to a sub that returns an infinite 1476 # stream of undef. This segfaults on 5.5.4, presumably because 1477 # we're still executing the closure that gets replaced and it hasn't 1478 # been protected with a refcount. 1479 $self->{_iter} = sub {return} 1480 if $] >= 5.006; 1481 1482 # sanity checks 1483 if ( !$self->plan ) { 1484 $self->_add_error('No plan found in TAP output'); 1485 } 1486 else { 1487 $self->is_good_plan(1) unless defined $self->is_good_plan; 1488 } 1489 if ( $self->tests_run != ( $self->tests_planned || 0 ) ) { 1490 $self->is_good_plan(0); 1491 if ( defined( my $planned = $self->tests_planned ) ) { 1492 my $ran = $self->tests_run; 1493 $self->_add_error( 1494 "Bad plan. You planned $planned tests but ran $ran."); 1495 } 1496 } 1497 if ( $self->tests_run != ( $self->passed + $self->failed ) ) { 1498 1499 # this should never happen 1500 my $actual = $self->tests_run; 1501 my $passed = $self->passed; 1502 my $failed = $self->failed; 1503 $self->_croak( "Panic: planned test count ($actual) did not equal " 1504 . "sum of passed ($passed) and failed ($failed) tests!" ); 1505 } 1506 1507 $self->is_good_plan(0) unless defined $self->is_good_plan; 1508 1509 unless ( $self->parse_errors ) { 1510 # Optimise storage where possible 1511 if ( $self->tests_run == @{$self->{passed}} ) { 1512 $self->{passed} = $self->tests_run; 1513 } 1514 if ( $self->tests_run == @{$self->{actual_passed}} ) { 1515 $self->{actual_passed} = $self->tests_run; 1516 } 1517 } 1518 1519 return $self; 1520} 1521 1522=head3 C<delete_spool> 1523 1524Delete and return the spool. 1525 1526 my $fh = $parser->delete_spool; 1527 1528=cut 1529 1530sub delete_spool { 1531 my $self = shift; 1532 1533 return delete $self->{_spool}; 1534} 1535 1536############################################################################## 1537 1538=head1 CALLBACKS 1539 1540As mentioned earlier, a "callback" key may be added to the 1541C<TAP::Parser> constructor. If present, each callback corresponding to a 1542given result type will be called with the result as the argument if the 1543C<run> method is used. The callback is expected to be a subroutine 1544reference (or anonymous subroutine) which is invoked with the parser 1545result as its argument. 1546 1547 my %callbacks = ( 1548 test => \&test_callback, 1549 plan => \&plan_callback, 1550 comment => \&comment_callback, 1551 bailout => \&bailout_callback, 1552 unknown => \&unknown_callback, 1553 ); 1554 1555 my $aggregator = TAP::Parser::Aggregator->new; 1556 for my $file ( @test_files ) { 1557 my $parser = TAP::Parser->new( 1558 { 1559 source => $file, 1560 callbacks => \%callbacks, 1561 } 1562 ); 1563 $parser->run; 1564 $aggregator->add( $file, $parser ); 1565 } 1566 1567Callbacks may also be added like this: 1568 1569 $parser->callback( test => \&test_callback ); 1570 $parser->callback( plan => \&plan_callback ); 1571 1572The following keys allowed for callbacks. These keys are case-sensitive. 1573 1574=over 4 1575 1576=item * C<test> 1577 1578Invoked if C<< $result->is_test >> returns true. 1579 1580=item * C<version> 1581 1582Invoked if C<< $result->is_version >> returns true. 1583 1584=item * C<plan> 1585 1586Invoked if C<< $result->is_plan >> returns true. 1587 1588=item * C<comment> 1589 1590Invoked if C<< $result->is_comment >> returns true. 1591 1592=item * C<bailout> 1593 1594Invoked if C<< $result->is_unknown >> returns true. 1595 1596=item * C<yaml> 1597 1598Invoked if C<< $result->is_yaml >> returns true. 1599 1600=item * C<unknown> 1601 1602Invoked if C<< $result->is_unknown >> returns true. 1603 1604=item * C<ELSE> 1605 1606If a result does not have a callback defined for it, this callback will 1607be invoked. Thus, if all of the previous result types are specified as 1608callbacks, this callback will I<never> be invoked. 1609 1610=item * C<ALL> 1611 1612This callback will always be invoked and this will happen for each 1613result after one of the above callbacks is invoked. For example, if 1614L<Term::ANSIColor> is loaded, you could use the following to color your 1615test output: 1616 1617 my %callbacks = ( 1618 test => sub { 1619 my $test = shift; 1620 if ( $test->is_ok && not $test->directive ) { 1621 # normal passing test 1622 print color 'green'; 1623 } 1624 elsif ( !$test->is_ok ) { # even if it's TODO 1625 print color 'white on_red'; 1626 } 1627 elsif ( $test->has_skip ) { 1628 print color 'white on_blue'; 1629 1630 } 1631 elsif ( $test->has_todo ) { 1632 print color 'white'; 1633 } 1634 }, 1635 ELSE => sub { 1636 # plan, comment, and so on (anything which isn't a test line) 1637 print color 'black on_white'; 1638 }, 1639 ALL => sub { 1640 # now print them 1641 print shift->as_string; 1642 print color 'reset'; 1643 print "\n"; 1644 }, 1645 ); 1646 1647=item * C<EOF> 1648 1649Invoked when there are no more lines to be parsed. Since there is no 1650accompanying L<TAP::Parser::Result> object the C<TAP::Parser> object is 1651passed instead. 1652 1653=back 1654 1655=head1 TAP GRAMMAR 1656 1657If you're looking for an EBNF grammar, see L<TAP::Parser::Grammar>. 1658 1659=head1 BACKWARDS COMPATIBILITY 1660 1661The Perl-QA list attempted to ensure backwards compatibility with 1662L<Test::Harness>. However, there are some minor differences. 1663 1664=head2 Differences 1665 1666=over 4 1667 1668=item * TODO plans 1669 1670A little-known feature of L<Test::Harness> is that it supported TODO 1671lists in the plan: 1672 1673 1..2 todo 2 1674 ok 1 - We have liftoff 1675 not ok 2 - Anti-gravity device activated 1676 1677Under L<Test::Harness>, test number 2 would I<pass> because it was 1678listed as a TODO test on the plan line. However, we are not aware of 1679anyone actually using this feature and hard-coding test numbers is 1680discouraged because it's very easy to add a test and break the test 1681number sequence. This makes test suites very fragile. Instead, the 1682following should be used: 1683 1684 1..2 1685 ok 1 - We have liftoff 1686 not ok 2 - Anti-gravity device activated # TODO 1687 1688=item * 'Missing' tests 1689 1690It rarely happens, but sometimes a harness might encounter 1691'missing tests: 1692 1693 ok 1 1694 ok 2 1695 ok 15 1696 ok 16 1697 ok 17 1698 1699L<Test::Harness> would report tests 3-14 as having failed. For the 1700C<TAP::Parser>, these tests are not considered failed because they've 1701never run. They're reported as parse failures (tests out of sequence). 1702 1703=back 1704 1705=head1 SUBCLASSING 1706 1707If you find you need to provide custom functionality (as you would have using 1708L<Test::Harness::Straps>), you're in luck: C<TAP::Parser> and friends are 1709designed to be easily plugged-into and/or subclassed. 1710 1711Before you start, it's important to know a few things: 1712 1713=over 2 1714 1715=item 1 1716 1717All C<TAP::*> objects inherit from L<TAP::Object>. 1718 1719=item 2 1720 1721Many C<TAP::*> classes have a I<SUBCLASSING> section to guide you. 1722 1723=item 3 1724 1725Note that C<TAP::Parser> is designed to be the central "maker" - ie: it is 1726responsible for creating most new objects in the C<TAP::Parser::*> namespace. 1727 1728This makes it possible for you to have a single point of configuring what 1729subclasses should be used, which means that in many cases you'll find 1730you only need to sub-class one of the parser's components. 1731 1732The exception to this rule are I<SourceHandlers> & I<Iterators>, but those are 1733both created with customizable I<IteratorFactory>. 1734 1735=item 4 1736 1737By subclassing, you may end up overriding undocumented methods. That's not 1738a bad thing per se, but be forewarned that undocumented methods may change 1739without warning from one release to the next - we cannot guarantee backwards 1740compatibility. If any I<documented> method needs changing, it will be 1741deprecated first, and changed in a later release. 1742 1743=back 1744 1745=head2 Parser Components 1746 1747=head3 Sources 1748 1749A TAP parser consumes input from a single I<raw source> of TAP, which could come 1750from anywhere (a file, an executable, a database, an IO handle, a URI, etc..). 1751The source gets bundled up in a L<TAP::Parser::Source> object which gathers some 1752meta data about it. The parser then uses a L<TAP::Parser::IteratorFactory> to 1753determine which L<TAP::Parser::SourceHandler> to use to turn the raw source 1754into a stream of TAP by way of L</Iterators>. 1755 1756If you simply want C<TAP::Parser> to handle a new source of TAP you probably 1757don't need to subclass C<TAP::Parser> itself. Rather, you'll need to create a 1758new L<TAP::Parser::SourceHandler> class, and just plug it into the parser using 1759the I<sources> param to L</new>. Before you start writing one, read through 1760L<TAP::Parser::IteratorFactory> to get a feel for how the system works first. 1761 1762If you find you really need to use your own iterator factory you can still do 1763so without sub-classing C<TAP::Parser> by setting L</iterator_factory_class>. 1764 1765If you just need to customize the objects on creation, subclass L<TAP::Parser> 1766and override L</make_iterator_factory>. 1767 1768Note that C<make_source> & C<make_perl_source> have been I<DEPRECATED> and 1769are now removed. 1770 1771=head3 Iterators 1772 1773A TAP parser uses I<iterators> to loop through the I<stream> of TAP read in 1774from the I<source> it was given. There are a few types of Iterators available 1775by default, all sub-classes of L<TAP::Parser::Iterator>. Choosing which 1776iterator to use is the responsibility of the I<iterator factory>, though it 1777simply delegates to the I<Source Handler> it uses. 1778 1779If you're writing your own L<TAP::Parser::SourceHandler>, you may need to 1780create your own iterators too. If so you'll need to subclass 1781L<TAP::Parser::Iterator>. 1782 1783Note that L</make_iterator> has been I<DEPRECATED> and is now removed. 1784 1785=head3 Results 1786 1787A TAP parser creates L<TAP::Parser::Result>s as it iterates through the 1788input I<stream>. There are quite a few result types available; choosing 1789which class to use is the responsibility of the I<result factory>. 1790 1791To create your own result types you have two options: 1792 1793=over 2 1794 1795=item option 1 1796 1797Subclass L<TAP::Parser::Result> and register your new result type/class with 1798the default L<TAP::Parser::ResultFactory>. 1799 1800=item option 2 1801 1802Subclass L<TAP::Parser::ResultFactory> itself and implement your own 1803L<TAP::Parser::Result> creation logic. Then you'll need to customize the 1804class used by your parser by setting the C<result_factory_class> parameter. 1805See L</new> for more details. 1806 1807=back 1808 1809If you need to customize the objects on creation, subclass L<TAP::Parser> and 1810override L</make_result>. 1811 1812=head3 Grammar 1813 1814L<TAP::Parser::Grammar> is the heart of the parser. It tokenizes the TAP 1815input I<stream> and produces results. If you need to customize its behaviour 1816you should probably familiarize yourself with the source first. Enough 1817lecturing. 1818 1819Subclass L<TAP::Parser::Grammar> and customize your parser by setting the 1820C<grammar_class> parameter. See L</new> for more details. 1821 1822If you need to customize the objects on creation, subclass L<TAP::Parser> and 1823override L</make_grammar> 1824 1825=head1 ACKNOWLEDGMENTS 1826 1827All of the following have helped. Bug reports, patches, (im)moral 1828support, or just words of encouragement have all been forthcoming. 1829 1830=over 4 1831 1832=item * Michael Schwern 1833 1834=item * Andy Lester 1835 1836=item * chromatic 1837 1838=item * GEOFFR 1839 1840=item * Shlomi Fish 1841 1842=item * Torsten Schoenfeld 1843 1844=item * Jerry Gay 1845 1846=item * Aristotle 1847 1848=item * Adam Kennedy 1849 1850=item * Yves Orton 1851 1852=item * Adrian Howard 1853 1854=item * Sean & Lil 1855 1856=item * Andreas J. Koenig 1857 1858=item * Florian Ragwitz 1859 1860=item * Corion 1861 1862=item * Mark Stosberg 1863 1864=item * Matt Kraai 1865 1866=item * David Wheeler 1867 1868=item * Alex Vandiver 1869 1870=item * Cosimo Streppone 1871 1872=item * Ville Skyttä 1873 1874=back 1875 1876=head1 AUTHORS 1877 1878Curtis "Ovid" Poe <ovid@cpan.org> 1879 1880Andy Armstong <andy@hexten.net> 1881 1882Eric Wilhelm @ <ewilhelm at cpan dot org> 1883 1884Michael Peters <mpeters at plusthree dot com> 1885 1886Leif Eriksen <leif dot eriksen at bigpond dot com> 1887 1888Steve Purkis <spurkis@cpan.org> 1889 1890Nicholas Clark <nick@ccl4.org> 1891 1892Lee Johnson <notfadeaway at btinternet dot com> 1893 1894Philippe Bruhat <book@cpan.org> 1895 1896=head1 BUGS 1897 1898Please report any bugs or feature requests to 1899C<bug-test-harness@rt.cpan.org>, or through the web interface at 1900L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Harness>. 1901We will be notified, and then you'll automatically be notified of 1902progress on your bug as we make changes. 1903 1904Obviously, bugs which include patches are best. If you prefer, you can 1905patch against bleed by via anonymous checkout of the latest version: 1906 1907 git clone git://github.com/Perl-Toolchain-Gang/Test-Harness.git 1908 1909=head1 COPYRIGHT & LICENSE 1910 1911Copyright 2006-2008 Curtis "Ovid" Poe, all rights reserved. 1912 1913This program is free software; you can redistribute it and/or modify it 1914under the same terms as Perl itself. 1915 1916=cut 1917 19181; 1919