1#!/usr/bin/perl -wT
2
3use strict;
4use warnings;
5use lib 't/lib';
6
7use Test::More tests => 23;
8
9use TAP::Parser;
10
11my $plan_line = 'TAP::Parser::Result::Plan';
12my $test_line = 'TAP::Parser::Result::Test';
13
14sub _parser {
15    my $parser = TAP::Parser->new( { tap => shift } );
16    $parser->run;
17    return $parser;
18}
19
20# validate that plan!
21
22my $parser = _parser(<<'END_TAP');
23ok 1 - input file opened
24not ok 2 - first line of the input valid # todo some data
25ok 3 - read the rest of the file
261..3
27# comments are allowed after an ending plan
28END_TAP
29
30can_ok $parser, 'parse_errors';
31ok !$parser->parse_errors,
32  '... comments should be allowed after a terminating plan';
33
34$parser = _parser(<<'END_TAP');
35ok 1 - input file opened
36not ok 2 - first line of the input valid # todo some data
37ok 3 - read the rest of the file
381..3
39# yeah, yeah, I know.
40ok
41END_TAP
42
43can_ok $parser, 'parse_errors';
44is scalar $parser->parse_errors, 2, '... and we should have two parse errors';
45
46is [ $parser->parse_errors ]->[0],
47  'Plan (1..3) must be at the beginning or end of the TAP output',
48  '... telling us that our plan was misplaced';
49is [ $parser->parse_errors ]->[1],
50  'Bad plan.  You planned 3 tests but ran 4.',
51  '... and telling us we ran the wrong number of tests.';
52
53$parser = _parser(<<'END_TAP');
54ok 1 - input file opened
55not ok 2 - first line of the input valid # todo some data
56ok 3 - read the rest of the file
57#1..3
58# yo quiero tests!
591..3
60END_TAP
61ok !$parser->parse_errors, '... but test plan-like data can be in a comment';
62
63$parser = _parser(<<'END_TAP');
64ok 1 - input file opened
65not ok 2 - first line of the input valid # todo some data
66ok 3 - read the rest of the file 1..5
67# yo quiero tests!
681..3
69END_TAP
70ok !$parser->parse_errors, '... or a description';
71
72$parser = _parser(<<'END_TAP');
73ok 1 - input file opened
74not ok 2 - first line of the input valid # todo 1..4
75ok 3 - read the rest of the file
76# yo quiero tests!
771..3
78END_TAP
79ok !$parser->parse_errors, '... or a directive';
80
81# test numbers included?
82
83$parser = _parser(<<'END_TAP');
841..3
85ok 1 - input file opened
86not ok 2 - first line of the input valid # todo some data
87ok read the rest of the file
88# this is ...
89END_TAP
90eval { $parser->run };
91ok !$@, 'We can mix and match the presence of test numbers';
92
93$parser = _parser(<<'END_TAP');
941..3
95ok 1 - input file opened
96not ok 2 - first line of the input valid # todo some data
97ok 2 read the rest of the file
98END_TAP
99
100is + ( $parser->parse_errors )[0],
101  'Tests out of sequence.  Found (2) but expected (3)',
102  '... and if the numbers are there, they cannot be out of sequence';
103
104$parser = _parser(<<'END_TAP');
105ok 1 - input file opened
106not ok 2 - first line of the input valid # todo some data
107ok 2 read the rest of the file
108END_TAP
109
110is $parser->parse_errors, 2,
111  'Having two errors in the TAP should result in two errors (duh)';
112my $expected = [
113    'Tests out of sequence.  Found (2) but expected (3)',
114    'No plan found in TAP output'
115];
116is_deeply [ $parser->parse_errors ], $expected,
117  '... and they should be the correct errors';
118
119$parser = _parser(<<'END_TAP');
120ok 1 - input file opened
121not ok 2 - first line of the input valid # todo some data
122ok 3 read the rest of the file
123END_TAP
124
125is $parser->parse_errors, 1, 'Having no plan should cause an error';
126is + ( $parser->parse_errors )[0], 'No plan found in TAP output',
127  '... with a correct error message';
128
129$parser = _parser(<<'END_TAP');
1301..3
131ok 1 - input file opened
132not ok 2 - first line of the input valid # todo some data
133ok 3 read the rest of the file
1341..3
135END_TAP
136
137is $parser->parse_errors, 1,
138  'Having more than one plan should cause an error';
139is + ( $parser->parse_errors )[0], 'More than one plan found in TAP output',
140  '... with a correct error message';
141
142can_ok $parser, 'is_good_plan';
143$parser = _parser(<<'END_TAP');
1441..2
145ok 1 - input file opened
146not ok 2 - first line of the input valid # todo some data
147ok 3 read the rest of the file
148END_TAP
149
150is $parser->parse_errors, 1,
151  'Having the wrong number of planned tests is a parse error';
152is + ( $parser->parse_errors )[0],
153  'Bad plan.  You planned 2 tests but ran 3.',
154  '... with a correct error message';
155
156# XXX internals:  plan will not set to true if defined
157$parser->is_good_plan(undef);
158$parser = _parser(<<'END_TAP');
159ok 1 - input file opened
1601..1
161END_TAP
162
163ok $parser->is_good_plan,
164  '... and it should return true if the plan is correct';
165
166# TAP::Parser coverage tests
167{
168
169    # good_plan coverage
170
171    my @warn;
172
173    eval {
174        local $SIG{__WARN__} = sub { push @warn, @_ };
175
176        $parser->good_plan;
177    };
178
179    is @warn, 1, 'coverage testing of good_plan';
180
181    like pop @warn,
182      qr/good_plan[(][)] is deprecated.  Please use "is_good_plan[(][)]"/,
183      '...and it fell-back like we expected';
184}
185