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