summaryrefslogtreecommitdiff
path: root/cpan/Test-Harness/t/grammar.t
blob: d145c13a2edc52b231399c69dfc8017bdb29f475 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/perl -w

use strict;

BEGIN {
    unshift @INC, 't/lib';
}

use Test::More tests => 94;

use EmptyParser;
use TAP::Parser::Grammar;
use TAP::Parser::Iterator::Array;

my $GRAMMAR = 'TAP::Parser::Grammar';

# Array based iterator that we can push items in to
package IT;

sub new {
    my $class = shift;
    return bless [], $class;
}

sub next {
    my $self = shift;
    return shift @$self;
}

sub put {
    my $self = shift;
    unshift @$self, @_;
}

sub handle_unicode { }

package main;

my $iterator = IT->new;
my $parser   = EmptyParser->new;
can_ok $GRAMMAR, 'new';
my $grammar = $GRAMMAR->new( { iterator => $iterator, parser => $parser } );
isa_ok $grammar, $GRAMMAR, '... and the object it returns';

# Note:  all methods are actually class methods.  See the docs for the reason
# why.  We'll still use the instance because that should be forward
# compatible.

my @V12 = sort qw(bailout comment plan simple_test test version);
my @V13 = sort ( @V12, 'pragma', 'yaml' );

can_ok $grammar, 'token_types';
ok my @types = sort( $grammar->token_types ),
  '... and calling it should succeed (v12)';
is_deeply \@types, \@V12, '... and return the correct token types (v12)';

$grammar->set_version(13);
ok @types = sort( $grammar->token_types ),
  '... and calling it should succeed (v13)';
is_deeply \@types, \@V13, '... and return the correct token types (v13)';

can_ok $grammar, 'syntax_for';
can_ok $grammar, 'handler_for';

my ( %syntax_for, %handler_for );
for my $type (@types) {
    ok $syntax_for{$type} = $grammar->syntax_for($type),
      '... and calling syntax_for() with a type name should succeed';
    cmp_ok ref $syntax_for{$type}, 'eq', 'Regexp',
      '... and it should return a regex';

    ok $handler_for{$type} = $grammar->handler_for($type),
      '... and calling handler_for() with a type name should succeed';
    cmp_ok ref $handler_for{$type}, 'eq', 'CODE',
      '... and it should return a code reference';
}

# Test the plan.  Gotta have a plan.
my $plan = '1..1';
like $plan, $syntax_for{'plan'}, 'A basic plan should match its syntax';

my $method = $handler_for{'plan'};
$plan =~ $syntax_for{'plan'};
ok my $plan_token = $grammar->$method($plan),
  '... and the handler should return a token';

my $expected = {
    'explanation'   => '',
    'directive'     => '',
    'type'          => 'plan',
    'tests_planned' => 1,
    'raw'           => '1..1',
    'todo_list'     => [],
};
is_deeply $plan_token, $expected,
  '... and it should contain the correct data';

can_ok $grammar, 'tokenize';
$iterator->put($plan);
ok my $token = $grammar->tokenize,
  '... and calling it with data should return a token';
is_deeply $token, $expected,
  '... and the token should contain the correct data';

# a plan with a skip directive

$plan = '1..0 # SKIP why not?';
like $plan, $syntax_for{'plan'}, 'a basic plan should match its syntax';

$plan =~ $syntax_for{'plan'};
ok $plan_token = $grammar->$method($plan),
  '... and the handler should return a token';

$expected = {
    'explanation'   => 'why not?',
    'directive'     => 'SKIP',
    'type'          => 'plan',
    'tests_planned' => 0,
    'raw'           => '1..0 # SKIP why not?',
    'todo_list'     => [],
};
is_deeply $plan_token, $expected,
  '... and it should contain the correct data';

$iterator->put($plan);
ok $token = $grammar->tokenize,
  '... and calling it with data should return a token';
is_deeply $token, $expected,
  '... and the token should contain the correct data';

# implied skip

$plan = '1..0';
like $plan, $syntax_for{'plan'},
  'A plan  with an implied "skip all" should match its syntax';

$plan =~ $syntax_for{'plan'};
ok $plan_token = $grammar->$method($plan),
  '... and the handler should return a token';

$expected = {
    'explanation'   => '',
    'directive'     => 'SKIP',
    'type'          => 'plan',
    'tests_planned' => 0,
    'raw'           => '1..0',
    'todo_list'     => [],
};
is_deeply $plan_token, $expected,
  '... and it should contain the correct data';

$iterator->put($plan);
ok $token = $grammar->tokenize,
  '... and calling it with data should return a token';
is_deeply $token, $expected,
  '... and the token should contain the correct data';

# bad plan

$plan = '1..0 # TODO 3,4,5';    # old syntax.  No longer supported
unlike $plan, $syntax_for{'plan'},
  'Bad plans should not match the plan syntax';

# Bail out!

my $bailout = 'Bail out!';
like $bailout, $syntax_for{'bailout'},
  'Bail out! should match a bailout syntax';

$iterator->put($bailout);
ok $token = $grammar->tokenize,
  '... and calling it with data should return a token';
$expected = {
    'bailout' => '',
    'type'    => 'bailout',
    'raw'     => 'Bail out!'
};
is_deeply $token, $expected,
  '... and the token should contain the correct data';

$bailout = 'Bail out! some explanation';
like $bailout, $syntax_for{'bailout'},
  'Bail out! should match a bailout syntax';

$iterator->put($bailout);
ok $token = $grammar->tokenize,
  '... and calling it with data should return a token';
$expected = {
    'bailout' => 'some explanation',
    'type'    => 'bailout',
    'raw'     => 'Bail out! some explanation'
};
is_deeply $token, $expected,
  '... and the token should contain the correct data';

# test comment

my $comment = '# this is a comment';
like $comment, $syntax_for{'comment'},
  'Comments should match the comment syntax';

$iterator->put($comment);
ok $token = $grammar->tokenize,
  '... and calling it with data should return a token';
$expected = {
    'comment' => 'this is a comment',
    'type'    => 'comment',
    'raw'     => '# this is a comment'
};
is_deeply $token, $expected,
  '... and the token should contain the correct data';

# test tests :/

my $test = 'ok 1 this is a test';
like $test, $syntax_for{'test'}, 'Tests should match the test syntax';

$iterator->put($test);
ok $token = $grammar->tokenize,
  '... and calling it with data should return a token';

$expected = {
    'ok'          => 'ok',
    'explanation' => '',
    'type'        => 'test',
    'directive'   => '',
    'description' => 'this is a test',
    'test_num'    => '1',
    'raw'         => 'ok 1 this is a test'
};
is_deeply $token, $expected,
  '... and the token should contain the correct data';

# TODO tests

$test = 'not ok 2 this is a test # TODO whee!';
like $test, $syntax_for{'test'}, 'Tests should match the test syntax';

$iterator->put($test);
ok $token = $grammar->tokenize,
  '... and calling it with data should return a token';

$expected = {
    'ok'          => 'not ok',
    'explanation' => 'whee!',
    'type'        => 'test',
    'directive'   => 'TODO',
    'description' => 'this is a test',
    'test_num'    => '2',
    'raw'         => 'not ok 2 this is a test # TODO whee!'
};
is_deeply $token, $expected, '... and the TODO should be parsed';

# false TODO tests

# escaping that hash mark ('#') means this should *not* be a TODO test
$test = 'ok 22 this is a test \# TODO whee!';
like $test, $syntax_for{'test'}, 'Tests should match the test syntax';

$iterator->put($test);
ok $token = $grammar->tokenize,
  '... and calling it with data should return a token';

$expected = {
    'ok'          => 'ok',
    'explanation' => '',
    'type'        => 'test',
    'directive'   => '',
    'description' => 'this is a test \# TODO whee!',
    'test_num'    => '22',
    'raw'         => 'ok 22 this is a test \# TODO whee!'
};
is_deeply $token, $expected,
  '... and the token should contain the correct data';

# pragmas

my $pragma = 'pragma +strict';
like $pragma, $syntax_for{'pragma'}, 'Pragmas should match the pragma syntax';

$iterator->put($pragma);
ok $token = $grammar->tokenize,
  '... and calling it with data should return a token';

$expected = {
    'type'    => 'pragma',
    'raw'     => $pragma,
    'pragmas' => ['+strict'],
};

is_deeply $token, $expected,
  '... and the token should contain the correct data';

$pragma = 'pragma +strict,-foo';
like $pragma, $syntax_for{'pragma'}, 'Pragmas should match the pragma syntax';

$iterator->put($pragma);
ok $token = $grammar->tokenize,
  '... and calling it with data should return a token';

$expected = {
    'type'    => 'pragma',
    'raw'     => $pragma,
    'pragmas' => [ '+strict', '-foo' ],
};

is_deeply $token, $expected,
  '... and the token should contain the correct data';

$pragma = 'pragma  +strict  ,  -foo ';
like $pragma, $syntax_for{'pragma'}, 'Pragmas should match the pragma syntax';

$iterator->put($pragma);
ok $token = $grammar->tokenize,
  '... and calling it with data should return a token';

$expected = {
    'type'    => 'pragma',
    'raw'     => $pragma,
    'pragmas' => [ '+strict', '-foo' ],
};

is_deeply $token, $expected,
  '... and the token should contain the correct data';

# coverage tests

# set_version

{
    my @die;

    eval {
        local $SIG{__DIE__} = sub { push @die, @_ };

        $grammar->set_version('no_such_version');
    };

    unless ( is @die, 1, 'set_version with bad version' ) {
        diag " >>> $_ <<<\n" for @die;
    }

    like pop @die, qr/^Unsupported syntax version: no_such_version at /,
      '... and got expected message';
}

# tokenize
{
    my $iterator = IT->new;
    my $parser   = EmptyParser->new;
    my $grammar
      = $GRAMMAR->new( { iterator => $iterator, parser => $parser } );

    my $plan = '';

    $iterator->put($plan);

    my $result = $grammar->tokenize();

    isa_ok $result, 'TAP::Parser::Result::Unknown';
}

# _make_plan_token

{
    my $parser = EmptyParser->new;
    my $grammar = $GRAMMAR->new( { parser => $parser } );

    my $plan
      = '1..1 # SKIP with explanation';  # trigger warning in _make_plan_token

    my $method = $handler_for{'plan'};

    $plan =~ $syntax_for{'plan'};        # perform regex to populate $1, $2

    my @warn;

    eval {
        local $SIG{__WARN__} = sub { push @warn, @_ };

        $grammar->$method($plan);
    };

    is @warn, 1, 'catch warning on inconsistent plan';

    like pop @warn,
      qr/^Specified SKIP directive in plan but more than 0 tests [(]1\.\.1 # SKIP with explanation[)]/,
      '... and its what we expect';
}

# _make_yaml_token

{
    my $iterator = IT->new;
    my $parser   = EmptyParser->new;
    my $grammar
      = $GRAMMAR->new( { iterator => $iterator, parser => $parser } );

    $grammar->set_version(13);

    # now this is badly formed YAML that is missing the
    # leader padding - this is done for coverage testing
    # the $reader code sub in _make_yaml_token, that is
    # passed as the yaml consumer to T::P::YAMLish::Reader.

    # because it isnt valid yaml, the yaml document is
    # not done, and the _peek in the YAMLish::Reader
    # code doesnt find the terminating '...' pattern.
    # but we dont care as this is coverage testing, so
    # if thats what we have to do to exercise that code,
    # so be it.
    my $yaml = [ '  ...  ', '- 2', '  ---  ', ];

    sub iter {
        my $ar = shift;
        return sub {
            return shift @$ar;
        };
    }

    my $iter = iter($yaml);

    while ( my $line = $iter->() ) {
        $iterator->put($line);
    }

    # pad == '   ', marker == '--- '
    # length $pad == 3
    # strip == pad

    my @die;

    eval {
        local $SIG{__DIE__} = sub { push @die, @_ };
        $grammar->tokenize;
    };

    is @die, 1, 'checking badly formed yaml for coverage testing';

    like pop @die, qr/^Missing '[.][.][.]' at end of YAMLish/,
      '...and it died like we expect';
}

{

    # coverage testing for TAP::Parser::Iterator::Array

    my $source = [qw( a b c )];

    my $aiter = TAP::Parser::Iterator::Array->new($source);

    my $first = $aiter->next_raw;

    is $first, 'a', 'access raw iterator';

    is $aiter->exit, undef, '... and note we didnt exhaust the source';
}