1 package TAP::Parser::Result;
6 use TAP::Parser::Result::Bailout ();
7 use TAP::Parser::Result::Comment ();
8 use TAP::Parser::Result::Plan ();
9 use TAP::Parser::Result::Test ();
10 use TAP::Parser::Result::Unknown ();
11 use TAP::Parser::Result::Version ();
12 use TAP::Parser::Result::YAML ();
16 foreach my $token (qw( plan comment test bailout version unknown yaml )) {
17 my $method = "is_$token";
18 *$method = sub { return $token eq shift->type };
22 ##############################################################################
26 TAP::Parser::Result - TAP::Parser output
38 This is merely a factory class which returns an object representing the
39 current bit of test data from TAP (usually a line). It's for internal use
40 only and should not be relied upon.
44 # note that this is bad. Makes it very difficult to subclass, but then, it
45 # would be a lot of work to subclass this system.
47 plan => 'TAP::Parser::Result::Plan',
48 test => 'TAP::Parser::Result::Test',
49 comment => 'TAP::Parser::Result::Comment',
50 bailout => 'TAP::Parser::Result::Bailout',
51 version => 'TAP::Parser::Result::Version',
52 unknown => 'TAP::Parser::Result::Unknown',
53 yaml => 'TAP::Parser::Result::YAML',
56 ##############################################################################
62 my $result = TAP::Parser::Result->new($token);
64 Returns an instance the appropriate class for the test token passed in.
69 my ( $class, $token ) = @_;
70 my $type = $token->{type};
71 return bless $token => $class_for{$type}
72 if exists $class_for{$type};
75 # this should never happen!
76 Carp::croak("Could not determine class for\n$token->{type}");
79 =head2 Boolean methods
81 The following methods all return a boolean value and are to be overridden in
82 the appropriate subclass.
88 Indicates whether or not this is the test plan line.
94 Indicates whether or not this is a test line.
96 is $foo, $bar, $description;
100 Indicates whether or not this is a comment.
104 =item * C<is_bailout>
106 Indicates whether or not this is bailout line.
108 Bail out! We're out of dilithium crystals.
110 =item * C<is_version>
112 Indicates whether or not this is a TAP version line.
116 =item * C<is_unknown>
118 Indicates whether or not the current line could be parsed.
120 ... this line is junk ...
124 Indicates whether or not this is a YAML chunk.
130 ##############################################################################
136 Returns the original line of text which was parsed.
140 sub raw { shift->{raw} }
142 ##############################################################################
146 my $type = $result->type;
148 Returns the "type" of a token, such as C<comment> or C<test>.
152 sub type { shift->{type} }
154 ##############################################################################
158 print $result->as_string;
160 Prints a string representation of the token. This might not be the exact
161 output, however. Tests will have test numbers added if not present, TODO and
162 SKIP directives will be capitalized and, in general, things will be cleaned
163 up. If you need the original text for the token, see the C<raw> method.
167 sub as_string { shift->{raw} }
169 ##############################################################################
173 if ( $result->is_ok ) { ... }
175 Reports whether or not a given result has passed. Anything which is B<not> a
176 test result returns true. This is merely provided as a convenient shortcut.
182 ##############################################################################
186 Deprecated. Please use C<is_ok> instead.
191 warn 'passed() is deprecated. Please use "is_ok()"';
195 ##############################################################################
197 =head3 C<has_directive>
199 if ( $result->has_directive ) {
203 Indicates whether or not the given result has a TODO or SKIP directive.
209 return ( $self->has_todo || $self->has_skip );
212 ##############################################################################
216 if ( $result->has_todo ) {
220 Indicates whether or not the given result has a TODO directive.
224 sub has_todo { 'TODO' eq ( shift->{directive} || '' ) }
226 ##############################################################################
230 if ( $result->has_skip ) {
234 Indicates whether or not the given result has a SKIP directive.
238 sub has_skip { 'SKIP' eq ( shift->{directive} || '' ) }
240 =head3 C<set_directive>
242 Set the directive associated with this token. Used internally to fake
248 my ( $self, $dir ) = @_;
249 $self->{directive} = $dir;