TAP::Parser - Parse TAP output
use TAP::Parser;
my $parser = TAP::Parser->new( { source => $source } );
while ( my $result = $parser->next ) { print $result->as_string; }
There's a wiki dedicated to the Test Anything Protocol:
It includes the TAP::Parser Cookbook:
<http://testanything.org/wiki/index.php/TAP::Parser_Cookbook>
my $parser = TAP::Parser->new(\%args);
Returns a new "TAP::Parser" object.
The arguments should be a hashref with one of the following keys:
If the source contains a newline, it's assumed to be a string of raw TAP output.
If the source is a reference, it's assumed to be something to pass to the TAP::Parser::Iterator::Stream constructor. This is used internally and you should not use it.
Otherwise, the parser does a "-e" check to see if the source exists. If so, it attempts to execute the source and read the output as a stream. This is by far the preferred method of using the parser.
foreach my $file ( @test_files ) { my $parser = TAP::Parser->new( { source => $file } ); # do stuff with the parser }
exec => [ '/usr/bin/ruby', 't/my_test.rb' ]
Note that "source" and "exec" are mutually exclusive.
The following keys are optional.
my %callbacks = ( test => \&test_callback, plan => \&plan_callback, comment => \&comment_callback, bailout => \&bailout_callback, unknown => \&unknown_callback, );
my $aggregator = TAP::Parser::Aggregator->new; foreach my $file ( @test_files ) { my $parser = TAP::Parser->new( { source => $file, callbacks => \%callbacks, } ); $parser->run; $aggregator->add( $file, $parser ); }
my $parser = TAP::Parser->new( { source => $test_file, switches => '-Ilib', } );
If true, STDERR and STDOUT are the same filehandle. This may cause breakage if STDERR contains anything resembling TAP format, but does allow exact synchronization.
Subtleties of this behavior may be platform-dependent and may change in the future.
my $parser = TAP::Parser->new( { source => $file } ); while ( my $result = $parser->next ) { print $result->as_string, "\n"; }
This method returns the results of the parsing, one result at a time. Note that it is destructive. You can't rewind and examine previous results.
If callbacks are used, they will be issued before this call returns.
Each result returned is a subclass of TAP::Parser::Result. See that module and related classes for more information on how to use them.
$parser->run;
This method merely runs the parser and parses all of the TAP.
while ( my $result = $parser->next ) { print $result->as_string; }
Each result returned is a TAP::Parser::Result subclass, referred to as result types.
TAP version 12
1..42
pragma +strict
ok 3 - We should start with some foobar!
# Hope we don't use up the foobar.
Bail out! We ran out of foobar!
... yo, this ain't TAP! ...
Each result fetched is a result object of a different type. There are common methods to each result object and different types may have methods unique to their type. Sometimes a type method may be overridden in a subclass, but its use is guaranteed to be identical.
Returns the type of result, such as "comment" or "test".
Prints a string representation of the token. This might not be the exact output, however. Tests will have test numbers added if not present, TODO and SKIP directives will be capitalized and, in general, things will be cleaned up. If you need the original text for the token, see the "raw" method.
Returns the original line of text which was parsed.
Indicates whether or not this is the test plan line.
Indicates whether or not this is a test line.
Indicates whether or not this is a comment. Comments will generally only appear in the TAP stream if STDERR is merged to STDOUT. See the "merge" option.
Indicates whether or not this is bailout line.
Indicates whether or not the current item is a YAML block.
Indicates whether or not the current line could be parsed.
if ( $result->is_ok ) { ... }
Reports whether or not a given result has passed. Anything which is not a test result returns true. This is merely provided as a convenient shortcut which allows you to do this:
my $parser = TAP::Parser->new( { source => $source } ); while ( my $result = $parser->next ) { # only print failing results print $result->as_string unless $result->is_ok; }
if ( $result->is_plan ) { ... }
If the above evaluates as true, the following methods will be available on the $result object.
if ( $result->is_plan ) { print $result->plan; }
This is merely a synonym for "as_string".
my $directive = $result->directive;
If a SKIP directive is included with the plan, this method will return it.
1..0 # SKIP: why bother?
my $explanation = $result->explanation;
If a SKIP directive was included with the plan, this method will return the explanation, if any.
if ( $result->is_pragma ) { ... }
If the above evaluates as true, the following methods will be available on the $result object.
Returns a list of pragmas each of which is a + or - followed by the pragma name.
if ( $result->is_comment ) { ... }
If the above evaluates as true, the following methods will be available on the $result object.
if ( $result->is_comment ) { my $comment = $result->comment; print "I have something to say: $comment"; }
if ( $result->is_bailout ) { ... }
If the above evaluates as true, the following methods will be available on the $result object.
if ( $result->is_bailout ) { my $explanation = $result->explanation; print "We bailed out because ($explanation)"; }
If, and only if, a token is a bailout token, you can get an ``explanation'' via this method. The explanation is the text after the mystical ``Bail out!'' words which appear in the tap output.
if ( $result->is_unknown ) { ... }
There are no unique methods for unknown results.
if ( $result->is_test ) { ... }
If the above evaluates as true, the following methods will be available on the $result object.
my $ok = $result->ok;
Returns the literal text of the "ok" or "not ok" status.
my $test_number = $result->number;
Returns the number of the test, even if the original TAP output did not supply that number.
my $description = $result->description;
Returns the description of the test, if any. This is the portion after the test number but before the directive.
my $directive = $result->directive;
Returns either "TODO" or "SKIP" if either directive was present for a test line.
my $explanation = $result->explanation;
If a test had either a "TODO" or "SKIP" directive, this method will return the accompanying explantion, if present.
not ok 17 - 'Pigs can fly' # TODO not enough acid
For the above line, the explanation is not enough acid.
if ( $result->is_ok ) { ... }
Returns a boolean value indicating whether or not the test passed. Remember that for TODO tests, the test always passes.
Note: this was formerly "passed". The latter method is deprecated and will issue a warning.
if ( $result->is_actual_ok ) { ... }
Returns a boolean value indicating whether or not the test passed, regardless of its TODO status.
Note: this was formerly "actual_passed". The latter method is deprecated and will issue a warning.
if ( $test->is_unplanned ) { ... }
If a test number is greater than the number of planned tests, this method will return true. Unplanned tests will always return false for "is_ok", regardless of whether or not the test "has_todo" (see TAP::Parser::Result::Test for more information about this).
if ( $result->has_skip ) { ... }
Returns a boolean value indicating whether or not this test had a SKIP directive.
if ( $result->has_todo ) { ... }
Returns a boolean value indicating whether or not this test had a TODO directive.
Note that TODO tests always pass. If you need to know whether or not they really passed, check the "is_actual_ok" method.
if ( $parser->in_todo ) { ... }
True while the most recent result was a TODO. Becomes true before the TODO result is returned and stays true until just before the next non- TODO test is returned.
my @passed = $parser->passed; # the test numbers which passed my $passed = $parser->passed; # the number of tests which passed
This method lets you know which (or how many) tests passed. If a test failed but had a TODO directive, it will be counted as a passed test.
my @failed = $parser->failed; # the test numbers which failed my $failed = $parser->failed; # the number of tests which failed
This method lets you know which (or how many) tests failed. If a test passed but had a TODO directive, it will NOT be counted as a failed test.
# the test numbers which actually passed my @actual_passed = $parser->actual_passed;
# the number of tests which actually passed my $actual_passed = $parser->actual_passed;
This method lets you know which (or how many) tests actually passed, regardless of whether or not a TODO directive was found.
This method is a synonym for "actual_passed".
# the test numbers which actually failed my @actual_failed = $parser->actual_failed;
# the number of tests which actually failed my $actual_failed = $parser->actual_failed;
This method lets you know which (or how many) tests actually failed, regardless of whether or not a TODO directive was found.
my @todo = $parser->todo; # the test numbers with todo directives my $todo = $parser->todo; # the number of tests with todo directives
This method lets you know which (or how many) tests had TODO directives.
# the test numbers which unexpectedly succeeded my @todo_passed = $parser->todo_passed;
# the number of tests which unexpectedly succeeded my $todo_passed = $parser->todo_passed;
This method lets you know which (or how many) tests actually passed but were declared as ``TODO'' tests.
# deprecated in favor of 'todo_passed'. This method was horribly misnamed.
This was a badly misnamed method. It indicates which TODO tests unexpectedly succeeded. Will now issue a warning and call "todo_passed".
my @skipped = $parser->skipped; # the test numbers with SKIP directives my $skipped = $parser->skipped; # the number of tests with SKIP directives
This method lets you know which (or how many) tests had SKIP directives.
Get or set a pragma. To get the state of a pragma:
if ( $p->pragma('strict') ) { # be strict }
To set the state of a pragma:
$p->pragma('strict', 1); # enable strict mode
Get a list of all the currently enabled pragmas:
my @pragmas_enabled = $p->pragmas;
my $plan = $parser->plan;
Returns the test plan, if found.
Deprecated. Use "is_good_plan" instead.
if ( $parser->is_good_plan ) { ... }
Returns a boolean value indicating whether or not the number of tests planned matches the number of tests run.
Note: this was formerly "good_plan". The latter method is deprecated and will issue a warning.
And since we're on that subject ...
print $parser->tests_planned;
Returns the number of tests planned, according to the plan. For example, a plan of '1..17' will mean that 17 tests were planned.
print $parser->tests_run;
Returns the number of tests which actually were run. Hopefully this will match the number of "$parser->tests_planned".
Returns a true value (actually the reason for skipping) if all tests were skipped.
Returns the time when the Parser was created.
Returns the time when the end of TAP input was seen.
if ( $parser->has_problems ) { ... }
This is a 'catch-all' method which returns true if any tests have currently failed, any TODO tests unexpectedly succeeded, or any parse errors occurred.
$parser->version;
Once the parser is done, this will return the version number for the parsed TAP. Version numbers were introduced with TAP version 13 so if no version number is found version 12 is assumed.
$parser->exit;
Once the parser is done, this will return the exit status. If the parser ran an executable, it returns the exit status of the executable.
$parser->wait;
Once the parser is done, this will return the wait status. If the parser ran an executable, it returns the wait status of the executable. Otherwise, this mererely returns the "exit" status.
my @errors = $parser->parse_errors; # the parser errors my $errors = $parser->parse_errors; # the number of parser_errors
Fortunately, all TAP output is perfect. In the event that it is not, this method will return parser errors. Note that a junk line which the parser does not recognize is "not" an error. This allows this parser to handle future versions of TAP. The following are all TAP errors reported by the parser:
1..3 ok 1 - input file opened not ok 2 - first line of the input valid # todo some data ok 3 read the rest of the file 1..3
Right. Very funny. Don't do that.
1..3 ok 1 - input file opened not ok 2 - first line of the input valid # todo some data ok 2 read the rest of the file
That last test line above should have the number '3' instead of '2'.
Note that it's perfectly acceptable for some lines to have test numbers and others to not have them. However, when a test number is found, it must be in sequence. The following is also an error:
1..3 ok 1 - input file opened not ok - first line of the input valid # todo some data ok 2 read the rest of the file
But this is not:
1..3 ok - input file opened not ok - first line of the input valid # todo some data ok 3 read the rest of the file
Get an a list of file handles which can be passed to "select" to determine the readiness of this parser.
Delete and return the spool.
my $fh = $parser->delete_spool;
my %callbacks = ( test => \&test_callback, plan => \&plan_callback, comment => \&comment_callback, bailout => \&bailout_callback, unknown => \&unknown_callback, );
my $aggregator = TAP::Parser::Aggregator->new; foreach my $file ( @test_files ) { my $parser = TAP::Parser->new( { source => $file, callbacks => \%callbacks, } ); $parser->run; $aggregator->add( $file, $parser ); }
Callbacks may also be added like this:
$parser->callback( test => \&test_callback ); $parser->callback( plan => \&plan_callback );
The following keys allowed for callbacks. These keys are case-sensitive.
my %callbacks = ( test => sub { my $test = shift; if ( $test->is_ok && not $test->directive ) { # normal passing test print color 'green'; } elsif ( !$test->is_ok ) { # even if it's TODO print color 'white on_red'; } elsif ( $test->has_skip ) { print color 'white on_blue';
} elsif ( $test->has_todo ) { print color 'white'; } }, ELSE => sub { # plan, comment, and so on (anything which isn't a test line) print color 'black on_white'; }, ALL => sub { # now print them print shift->as_string; print color 'reset'; print "\n"; }, );
1..2 todo 2 ok 1 - We have liftoff not ok 2 - Anti-gravity device activated
Under Test::Harness, test number 2 would pass because it was listed as a TODO test on the plan line. However, we are not aware of anyone actually using this feature and hard-coding test numbers is discouraged because it's very easy to add a test and break the test number sequence. This makes test suites very fragile. Instead, the following should be used:
1..2 ok 1 - We have liftoff not ok 2 - Anti-gravity device activated # TODO
ok 1 ok 2 ok 15 ok 16 ok 17
Test::Harness would report tests 3-14 as having failed. For the "TAP::Parser", these tests are not considered failed because they've never run. They're reported as parse failures (tests out of sequence).
Andy Armstong <andy@hexten.net>
Eric Wilhelm @ <ewilhelm at cpan dot org>
Michael Peters <mpeters at plusthree dot com>
Leif Eriksen <leif dot eriksen at bigpond dot com>
Obviously, bugs which include patches are best. If you prefer, you can patch against bleed by via anonymous checkout of the latest version:
svn checkout http://svn.hexten.net/tapx
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |