1 package TAP::Parser::Result;
4 use vars qw($VERSION @ISA);
12 my @attrs = qw( plan pragma test comment bailout version unknown yaml );
14 for my $token (@attrs) {
15 my $method = "is_$token";
16 *$method = sub { return $token eq shift->type };
20 ##############################################################################
24 TAP::Parser::Result - Base class for TAP::Parser output objects
36 # abstract class - not meany to be used directly
37 # see TAP::Parser::ResultFactory for preferred usage
40 use TAP::Parser::Result;
42 my $result = TAP::Parser::Result->new( $token );
46 This is a simple base class used by L<TAP::Parser> to store objects that
47 represent the current bit of test output data from TAP (usually a single
48 line). Unless you're subclassing, you probably won't need to use this module
55 # see TAP::Parser::ResultFactory for preferred usage
58 my $result = TAP::Parser::Result->new($token);
60 Returns an instance the appropriate class for the test token passed in.
64 # new() implementation provided by TAP::Object
67 my ($self, $token) = @_;
69 # make a shallow copy of the token:
70 $self->{$_} = $token->{$_} for (keys %$token);
75 ##############################################################################
77 =head2 Boolean methods
79 The following methods all return a boolean value and are to be overridden in
80 the appropriate subclass.
86 Indicates whether or not this is the test plan line.
92 Indicates whether or not this is a pragma line.
98 Indicates whether or not this is a test line.
102 =item * C<is_comment>
104 Indicates whether or not this is a comment.
108 =item * C<is_bailout>
110 Indicates whether or not this is bailout line.
112 Bail out! We're out of dilithium crystals.
114 =item * C<is_version>
116 Indicates whether or not this is a TAP version line.
120 =item * C<is_unknown>
122 Indicates whether or not the current line could be parsed.
124 ... this line is junk ...
128 Indicates whether or not this is a YAML chunk.
134 ##############################################################################
140 Returns the original line of text which was parsed.
144 sub raw { shift->{raw} }
146 ##############################################################################
150 my $type = $result->type;
152 Returns the "type" of a token, such as C<comment> or C<test>.
156 sub type { shift->{type} }
158 ##############################################################################
162 print $result->as_string;
164 Prints a string representation of the token. This might not be the exact
165 output, however. Tests will have test numbers added if not present, TODO and
166 SKIP directives will be capitalized and, in general, things will be cleaned
167 up. If you need the original text for the token, see the C<raw> method.
171 sub as_string { shift->{raw} }
173 ##############################################################################
177 if ( $result->is_ok ) { ... }
179 Reports whether or not a given result has passed. Anything which is B<not> a
180 test result returns true. This is merely provided as a convenient shortcut.
186 ##############################################################################
190 Deprecated. Please use C<is_ok> instead.
195 warn 'passed() is deprecated. Please use "is_ok()"';
199 ##############################################################################
201 =head3 C<has_directive>
203 if ( $result->has_directive ) {
207 Indicates whether or not the given result has a TODO or SKIP directive.
213 return ( $self->has_todo || $self->has_skip );
216 ##############################################################################
220 if ( $result->has_todo ) {
224 Indicates whether or not the given result has a TODO directive.
228 sub has_todo { 'TODO' eq ( shift->{directive} || '' ) }
230 ##############################################################################
234 if ( $result->has_skip ) {
238 Indicates whether or not the given result has a SKIP directive.
242 sub has_skip { 'SKIP' eq ( shift->{directive} || '' ) }
244 =head3 C<set_directive>
246 Set the directive associated with this token. Used internally to fake
252 my ( $self, $dir ) = @_;
253 $self->{directive} = $dir;
261 Please see L<TAP::Parser/SUBCLASSING> for a subclassing overview.
263 Remember: if you want your subclass to be automatically used by the parser,
264 you'll have to register it with L<TAP::Parser::ResultFactory/register_type>.
266 If you're creating a completely new result I<type>, you'll probably need to
267 subclass L<TAP::Parser::Grammar> too, or else it'll never get used.
276 @ISA = 'TAP::Parser::Result';
278 # register with the factory:
279 TAP::Parser::ResultFactory->register_type( 'my_type' => __PACKAGE__ );
281 sub as_string { 'My results all look the same' }
287 L<TAP::Parser::ResultFactory>,
288 L<TAP::Parser::Result::Bailout>,
289 L<TAP::Parser::Result::Comment>,
290 L<TAP::Parser::Result::Plan>,
291 L<TAP::Parser::Result::Pragma>,
292 L<TAP::Parser::Result::Test>,
293 L<TAP::Parser::Result::Unknown>,
294 L<TAP::Parser::Result::Version>,
295 L<TAP::PARSER::RESULT::YAML>,