6 if ( $ENV{PERL_CORE} ) {
8 @INC = ( '../lib', 'lib' );
11 unshift @INC, 't/lib';
15 use Test::More tests => 94;
18 use TAP::Parser::Grammar;
19 use TAP::Parser::Iterator::Array;
21 my $GRAMMAR = 'TAP::Parser::Grammar';
23 # Array based stream that we can push items in to
28 return bless [], $class;
41 sub handle_unicode { }
46 my $parser = EmptyParser->new;
47 can_ok $GRAMMAR, 'new';
48 my $grammar = $GRAMMAR->new( { stream => $stream, parser => $parser } );
49 isa_ok $grammar, $GRAMMAR, '... and the object it returns';
51 # Note: all methods are actually class methods. See the docs for the reason
52 # why. We'll still use the instance because that should be forward
55 my @V12 = sort qw(bailout comment plan simple_test test version);
56 my @V13 = sort ( @V12, 'pragma', 'yaml' );
58 can_ok $grammar, 'token_types';
59 ok my @types = sort( $grammar->token_types ),
60 '... and calling it should succeed (v12)';
61 is_deeply \@types, \@V12, '... and return the correct token types (v12)';
63 $grammar->set_version(13);
64 ok @types = sort( $grammar->token_types ),
65 '... and calling it should succeed (v13)';
66 is_deeply \@types, \@V13, '... and return the correct token types (v13)';
68 can_ok $grammar, 'syntax_for';
69 can_ok $grammar, 'handler_for';
71 my ( %syntax_for, %handler_for );
72 foreach my $type (@types) {
73 ok $syntax_for{$type} = $grammar->syntax_for($type),
74 '... and calling syntax_for() with a type name should succeed';
75 cmp_ok ref $syntax_for{$type}, 'eq', 'Regexp',
76 '... and it should return a regex';
78 ok $handler_for{$type} = $grammar->handler_for($type),
79 '... and calling handler_for() with a type name should succeed';
80 cmp_ok ref $handler_for{$type}, 'eq', 'CODE',
81 '... and it should return a code reference';
84 # Test the plan. Gotta have a plan.
86 like $plan, $syntax_for{'plan'}, 'A basic plan should match its syntax';
88 my $method = $handler_for{'plan'};
89 $plan =~ $syntax_for{'plan'};
90 ok my $plan_token = $grammar->$method($plan),
91 '... and the handler should return a token';
101 is_deeply $plan_token, $expected,
102 '... and it should contain the correct data';
104 can_ok $grammar, 'tokenize';
106 ok my $token = $grammar->tokenize,
107 '... and calling it with data should return a token';
108 is_deeply $token, $expected,
109 '... and the token should contain the correct data';
111 # a plan with a skip directive
113 $plan = '1..0 # SKIP why not?';
114 like $plan, $syntax_for{'plan'}, 'a basic plan should match its syntax';
116 $plan =~ $syntax_for{'plan'};
117 ok $plan_token = $grammar->$method($plan),
118 '... and the handler should return a token';
121 'explanation' => 'why not?',
122 'directive' => 'SKIP',
124 'tests_planned' => 0,
125 'raw' => '1..0 # SKIP why not?',
128 is_deeply $plan_token, $expected,
129 '... and it should contain the correct data';
132 ok $token = $grammar->tokenize,
133 '... and calling it with data should return a token';
134 is_deeply $token, $expected,
135 '... and the token should contain the correct data';
140 like $plan, $syntax_for{'plan'},
141 'A plan with an implied "skip all" should match its syntax';
143 $plan =~ $syntax_for{'plan'};
144 ok $plan_token = $grammar->$method($plan),
145 '... and the handler should return a token';
149 'directive' => 'SKIP',
151 'tests_planned' => 0,
155 is_deeply $plan_token, $expected,
156 '... and it should contain the correct data';
159 ok $token = $grammar->tokenize,
160 '... and calling it with data should return a token';
161 is_deeply $token, $expected,
162 '... and the token should contain the correct data';
166 $plan = '1..0 # TODO 3,4,5'; # old syntax. No longer supported
167 unlike $plan, $syntax_for{'plan'},
168 'Bad plans should not match the plan syntax';
172 my $bailout = 'Bail out!';
173 like $bailout, $syntax_for{'bailout'},
174 'Bail out! should match a bailout syntax';
176 $stream->put($bailout);
177 ok $token = $grammar->tokenize,
178 '... and calling it with data should return a token';
184 is_deeply $token, $expected,
185 '... and the token should contain the correct data';
187 $bailout = 'Bail out! some explanation';
188 like $bailout, $syntax_for{'bailout'},
189 'Bail out! should match a bailout syntax';
191 $stream->put($bailout);
192 ok $token = $grammar->tokenize,
193 '... and calling it with data should return a token';
195 'bailout' => 'some explanation',
197 'raw' => 'Bail out! some explanation'
199 is_deeply $token, $expected,
200 '... and the token should contain the correct data';
204 my $comment = '# this is a comment';
205 like $comment, $syntax_for{'comment'},
206 'Comments should match the comment syntax';
208 $stream->put($comment);
209 ok $token = $grammar->tokenize,
210 '... and calling it with data should return a token';
212 'comment' => 'this is a comment',
214 'raw' => '# this is a comment'
216 is_deeply $token, $expected,
217 '... and the token should contain the correct data';
221 my $test = 'ok 1 this is a test';
222 like $test, $syntax_for{'test'}, 'Tests should match the test syntax';
225 ok $token = $grammar->tokenize,
226 '... and calling it with data should return a token';
233 'description' => 'this is a test',
235 'raw' => 'ok 1 this is a test'
237 is_deeply $token, $expected,
238 '... and the token should contain the correct data';
242 $test = 'not ok 2 this is a test # TODO whee!';
243 like $test, $syntax_for{'test'}, 'Tests should match the test syntax';
246 ok $token = $grammar->tokenize,
247 '... and calling it with data should return a token';
251 'explanation' => 'whee!',
253 'directive' => 'TODO',
254 'description' => 'this is a test',
256 'raw' => 'not ok 2 this is a test # TODO whee!'
258 is_deeply $token, $expected, '... and the TODO should be parsed';
262 # escaping that hash mark ('#') means this should *not* be a TODO test
263 $test = 'ok 22 this is a test \# TODO whee!';
264 like $test, $syntax_for{'test'}, 'Tests should match the test syntax';
267 ok $token = $grammar->tokenize,
268 '... and calling it with data should return a token';
275 'description' => 'this is a test \# TODO whee!',
277 'raw' => 'ok 22 this is a test \# TODO whee!'
279 is_deeply $token, $expected,
280 '... and the token should contain the correct data';
284 my $pragma = 'pragma +strict';
285 like $pragma, $syntax_for{'pragma'}, 'Pragmas should match the pragma syntax';
287 $stream->put($pragma);
288 ok $token = $grammar->tokenize,
289 '... and calling it with data should return a token';
294 'pragmas' => ['+strict'],
297 is_deeply $token, $expected,
298 '... and the token should contain the correct data';
300 $pragma = 'pragma +strict,-foo';
301 like $pragma, $syntax_for{'pragma'}, 'Pragmas should match the pragma syntax';
303 $stream->put($pragma);
304 ok $token = $grammar->tokenize,
305 '... and calling it with data should return a token';
310 'pragmas' => [ '+strict', '-foo' ],
313 is_deeply $token, $expected,
314 '... and the token should contain the correct data';
316 $pragma = 'pragma +strict , -foo ';
317 like $pragma, $syntax_for{'pragma'}, 'Pragmas should match the pragma syntax';
319 $stream->put($pragma);
320 ok $token = $grammar->tokenize,
321 '... and calling it with data should return a token';
326 'pragmas' => [ '+strict', '-foo' ],
329 is_deeply $token, $expected,
330 '... and the token should contain the correct data';
340 local $SIG{__DIE__} = sub { push @die, @_ };
342 $grammar->set_version('no_such_version');
345 unless ( is @die, 1, 'set_version with bad version' ) {
346 diag " >>> $_ <<<\n" for @die;
349 like pop @die, qr/^Unsupported syntax version: no_such_version at /,
350 '... and got expected message';
355 my $stream = SS->new;
356 my $parser = EmptyParser->new;
357 my $grammar = $GRAMMAR->new( { stream => $stream, parser => $parser } );
363 my $result = $grammar->tokenize();
365 isa_ok $result, 'TAP::Parser::Result::Unknown';
371 my $parser = EmptyParser->new;
372 my $grammar = $GRAMMAR->new( { parser => $parser } );
375 = '1..1 # SKIP with explanation'; # trigger warning in _make_plan_token
377 my $method = $handler_for{'plan'};
379 $plan =~ $syntax_for{'plan'}; # perform regex to populate $1, $2
384 local $SIG{__WARN__} = sub { push @warn, @_ };
386 $grammar->$method($plan);
389 is @warn, 1, 'catch warning on inconsistent plan';
392 qr/^Specified SKIP directive in plan but more than 0 tests [(]1\.\.1 # SKIP with explanation[)]/,
393 '... and its what we expect';
399 my $stream = SS->new;
400 my $parser = EmptyParser->new;
401 my $grammar = $GRAMMAR->new( { stream => $stream, parser => $parser } );
403 $grammar->set_version(13);
405 # now this is badly formed YAML that is missing the
406 # leader padding - this is done for coverage testing
407 # the $reader code sub in _make_yaml_token, that is
408 # passed as the yaml consumer to T::P::YAMLish::Reader.
410 # because it isnt valid yaml, the yaml document is
411 # not done, and the _peek in the YAMLish::Reader
412 # code doesnt find the terminating '...' pattern.
413 # but we dont care as this is coverage testing, so
414 # if thats what we have to do to exercise that code,
416 my $yaml = [ ' ... ', '- 2', ' --- ', ];
425 my $iter = iter($yaml);
427 while ( my $line = $iter->() ) {
431 # pad == ' ', marker == '--- '
438 local $SIG{__DIE__} = sub { push @die, @_ };
442 is @die, 1, 'checking badly formed yaml for coverage testing';
444 like pop @die, qr/^Missing '[.][.][.]' at end of YAMLish/,
445 '...and it died like we expect';
450 # coverage testing for TAP::Parser::Iterator::Array
452 my $source = [qw( a b c )];
454 my $aiter = TAP::Parser::Iterator::Array->new($source);
456 my $first = $aiter->next_raw;
458 is $first, 'a', 'access raw iterator';
460 is $aiter->exit, undef, '... and note we didnt exhaust the source';