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