This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Test::Harness 3.05
[perl5.git] / lib / Test / Harness / t / errors.t
1 #!/usr/bin/perl -wT
2
3 use strict;
4 use lib 't/lib';
5
6 use Test::More tests => 23;
7
8 use TAP::Parser;
9
10 my $plan_line = 'TAP::Parser::Result::Plan';
11 my $test_line = 'TAP::Parser::Result::Test';
12
13 sub _parser {
14     my $parser = TAP::Parser->new( { tap => shift } );
15     $parser->run;
16     return $parser;
17 }
18
19 # validate that plan!
20
21 my $parser = _parser(<<'END_TAP');
22 ok 1 - input file opened
23 not ok 2 - first line of the input valid # todo some data
24 ok 3 - read the rest of the file
25 1..3
26 # comments are allowed after an ending plan
27 END_TAP
28
29 can_ok $parser, 'parse_errors';
30 ok !$parser->parse_errors,
31   '... comments should be allowed after a terminating plan';
32
33 $parser = _parser(<<'END_TAP');
34 ok 1 - input file opened
35 not ok 2 - first line of the input valid # todo some data
36 ok 3 - read the rest of the file
37 1..3
38 # yeah, yeah, I know.
39 ok
40 END_TAP
41
42 can_ok $parser, 'parse_errors';
43 is scalar $parser->parse_errors, 2, '... and we should have two parse errors';
44
45 is [ $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';
48 is [ $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');
53 ok 1 - input file opened
54 not ok 2 - first line of the input valid # todo some data
55 ok 3 - read the rest of the file
56 #1..3
57 # yo quiero tests!
58 1..3
59 END_TAP
60 ok !$parser->parse_errors, '... but test plan-like data can be in a comment';
61
62 $parser = _parser(<<'END_TAP');
63 ok 1 - input file opened
64 not ok 2 - first line of the input valid # todo some data
65 ok 3 - read the rest of the file 1..5
66 # yo quiero tests!
67 1..3
68 END_TAP
69 ok !$parser->parse_errors, '... or a description';
70
71 $parser = _parser(<<'END_TAP');
72 ok 1 - input file opened
73 not ok 2 - first line of the input valid # todo 1..4
74 ok 3 - read the rest of the file
75 # yo quiero tests!
76 1..3
77 END_TAP
78 ok !$parser->parse_errors, '... or a directive';
79
80 # test numbers included?
81
82 $parser = _parser(<<'END_TAP');
83 1..3
84 ok 1 - input file opened
85 not ok 2 - first line of the input valid # todo some data
86 ok read the rest of the file
87 # this is ...
88 END_TAP
89 eval { $parser->run };
90 ok !$@, 'We can mix and match the presence of test numbers';
91
92 $parser = _parser(<<'END_TAP');
93 1..3
94 ok 1 - input file opened
95 not ok 2 - first line of the input valid # todo some data
96 ok 2 read the rest of the file
97 END_TAP
98
99 is + ( $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');
104 ok 1 - input file opened
105 not ok 2 - first line of the input valid # todo some data
106 ok 2 read the rest of the file
107 END_TAP
108
109 is $parser->parse_errors, 2,
110   'Having two errors in the TAP should result in two errors (duh)';
111 my $expected = [
112     'Tests out of sequence.  Found (2) but expected (3)',
113     'No plan found in TAP output'
114 ];
115 is_deeply [ $parser->parse_errors ], $expected,
116   '... and they should be the correct errors';
117
118 $parser = _parser(<<'END_TAP');
119 ok 1 - input file opened
120 not ok 2 - first line of the input valid # todo some data
121 ok 3 read the rest of the file
122 END_TAP
123
124 is $parser->parse_errors, 1, 'Having no plan should cause an error';
125 is + ( $parser->parse_errors )[0], 'No plan found in TAP output',
126   '... with a correct error message';
127
128 $parser = _parser(<<'END_TAP');
129 1..3
130 ok 1 - input file opened
131 not ok 2 - first line of the input valid # todo some data
132 ok 3 read the rest of the file
133 1..3
134 END_TAP
135
136 is $parser->parse_errors, 1,
137   'Having more than one plan should cause an error';
138 is + ( $parser->parse_errors )[0], 'More than one plan found in TAP output',
139   '... with a correct error message';
140
141 can_ok $parser, 'is_good_plan';
142 $parser = _parser(<<'END_TAP');
143 1..2
144 ok 1 - input file opened
145 not ok 2 - first line of the input valid # todo some data
146 ok 3 read the rest of the file
147 END_TAP
148
149 is $parser->parse_errors, 1,
150   'Having the wrong number of planned tests is a parse error';
151 is + ( $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');
158 ok 1 - input file opened
159 1..1
160 END_TAP
161
162 ok $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 }