Commit | Line | Data |
33459055 |
1 | #!perl -w |
ccbd73a4 |
2 | # $Id: /mirror/googlecode/test-more/t/fail-more.t 60310 2008-09-07T23:47:22.837229Z schwern $ |
3f2ec160 |
3 | |
15db8fc4 |
4 | BEGIN { |
a9153838 |
5 | if( $ENV{PERL_CORE} ) { |
6 | chdir 't'; |
7 | @INC = ('../lib', 'lib'); |
8 | } |
9 | else { |
10 | unshift @INC, 't/lib'; |
11 | } |
15db8fc4 |
12 | } |
13 | |
33459055 |
14 | use strict; |
33459055 |
15 | |
16 | require Test::Simple::Catch; |
17 | my($out, $err) = Test::Simple::Catch::caught(); |
30e302f8 |
18 | local $ENV{HARNESS_ACTIVE} = 0; |
33459055 |
19 | |
20 | |
3f2ec160 |
21 | # Can't use Test.pm, that's a 5.005 thing. |
22 | package My::Test; |
23 | |
b1ddf169 |
24 | # This has to be a require or else the END block below runs before |
25 | # Test::Builder's own and the ending diagnostics don't come out right. |
26 | require Test::Builder; |
27 | my $TB = Test::Builder->create; |
ccbd73a4 |
28 | $TB->plan(tests => 23); |
b1ddf169 |
29 | |
30 | sub like ($$;$) { |
31 | $TB->like(@_); |
3f2ec160 |
32 | } |
33 | |
b1ddf169 |
34 | sub is ($$;$) { |
35 | $TB->is_eq(@_); |
36 | } |
3f2ec160 |
37 | |
0257f296 |
38 | sub main::err_ok ($) { |
30e302f8 |
39 | my($expect) = @_; |
40 | my $got = $err->read; |
41 | |
b1ddf169 |
42 | return $TB->is_eq( $got, $expect ); |
30e302f8 |
43 | } |
44 | |
ccbd73a4 |
45 | sub main::err_like ($) { |
46 | my($expect) = @_; |
47 | my $got = $err->read; |
48 | |
49 | return $TB->like( $got, qr/$expect/ ); |
50 | } |
51 | |
30e302f8 |
52 | |
3f2ec160 |
53 | package main; |
d020a79a |
54 | |
3f2ec160 |
55 | require Test::More; |
ccbd73a4 |
56 | my $Total = 36; |
a9153838 |
57 | Test::More->import(tests => $Total); |
3f2ec160 |
58 | |
c00d8759 |
59 | # This should all work in the presence of a __DIE__ handler. |
60 | local $SIG{__DIE__} = sub { $TB->ok(0, "DIE handler called: ".join "", @_); }; |
61 | |
62 | |
30e302f8 |
63 | my $tb = Test::More->builder; |
64 | $tb->use_numbers(0); |
65 | |
b1ddf169 |
66 | my $Filename = quotemeta $0; |
67 | |
d020a79a |
68 | # Preserve the line numbers. |
33459055 |
69 | #line 38 |
3f2ec160 |
70 | ok( 0, 'failing' ); |
0257f296 |
71 | err_ok( <<ERR ); |
b1ddf169 |
72 | # Failed test 'failing' |
b7f9bbeb |
73 | # at $0 line 38. |
30e302f8 |
74 | ERR |
a9153838 |
75 | |
76 | #line 40 |
77 | is( "foo", "bar", 'foo is bar?'); |
78 | is( undef, '', 'undef is empty string?'); |
79 | is( undef, 0, 'undef is 0?'); |
80 | is( '', 0, 'empty string is 0?' ); |
0257f296 |
81 | err_ok( <<ERR ); |
b1ddf169 |
82 | # Failed test 'foo is bar?' |
b7f9bbeb |
83 | # at $0 line 40. |
3f2ec160 |
84 | # got: 'foo' |
85 | # expected: 'bar' |
b1ddf169 |
86 | # Failed test 'undef is empty string?' |
b7f9bbeb |
87 | # at $0 line 41. |
a9153838 |
88 | # got: undef |
89 | # expected: '' |
b1ddf169 |
90 | # Failed test 'undef is 0?' |
b7f9bbeb |
91 | # at $0 line 42. |
a9153838 |
92 | # got: undef |
93 | # expected: '0' |
b1ddf169 |
94 | # Failed test 'empty string is 0?' |
b7f9bbeb |
95 | # at $0 line 43. |
a9153838 |
96 | # got: '' |
97 | # expected: '0' |
30e302f8 |
98 | ERR |
99 | |
100 | #line 45 |
101 | isnt("foo", "foo", 'foo isnt foo?' ); |
102 | isn't("foo", "foo",'foo isn\'t foo?' ); |
103 | isnt(undef, undef, 'undef isnt undef?'); |
0257f296 |
104 | err_ok( <<ERR ); |
b1ddf169 |
105 | # Failed test 'foo isnt foo?' |
b7f9bbeb |
106 | # at $0 line 45. |
ccbd73a4 |
107 | # got: 'foo' |
108 | # expected: anything else |
b1ddf169 |
109 | # Failed test 'foo isn\'t foo?' |
b7f9bbeb |
110 | # at $0 line 46. |
ccbd73a4 |
111 | # got: 'foo' |
112 | # expected: anything else |
b1ddf169 |
113 | # Failed test 'undef isnt undef?' |
b7f9bbeb |
114 | # at $0 line 47. |
ccbd73a4 |
115 | # got: undef |
116 | # expected: anything else |
30e302f8 |
117 | ERR |
118 | |
119 | #line 48 |
120 | like( "foo", '/that/', 'is foo like that' ); |
121 | unlike( "foo", '/foo/', 'is foo unlike foo' ); |
0257f296 |
122 | err_ok( <<ERR ); |
b1ddf169 |
123 | # Failed test 'is foo like that' |
b7f9bbeb |
124 | # at $0 line 48. |
3f2ec160 |
125 | # 'foo' |
126 | # doesn't match '/that/' |
b1ddf169 |
127 | # Failed test 'is foo unlike foo' |
b7f9bbeb |
128 | # at $0 line 49. |
a9153838 |
129 | # 'foo' |
130 | # matches '/foo/' |
30e302f8 |
131 | ERR |
132 | |
133 | # Nick Clark found this was a bug. Fixed in 0.40. |
b1ddf169 |
134 | # line 60 |
30e302f8 |
135 | like( "bug", '/(%)/', 'regex with % in it' ); |
0257f296 |
136 | err_ok( <<ERR ); |
b1ddf169 |
137 | # Failed test 'regex with % in it' |
b7f9bbeb |
138 | # at $0 line 60. |
a9153838 |
139 | # 'bug' |
140 | # doesn't match '/(%)/' |
30e302f8 |
141 | ERR |
142 | |
b1ddf169 |
143 | #line 67 |
30e302f8 |
144 | fail('fail()'); |
0257f296 |
145 | err_ok( <<ERR ); |
b1ddf169 |
146 | # Failed test 'fail()' |
b7f9bbeb |
147 | # at $0 line 67. |
30e302f8 |
148 | ERR |
149 | |
150 | #line 52 |
151 | can_ok('Mooble::Hooble::Yooble', qw(this that)); |
152 | can_ok('Mooble::Hooble::Yooble', ()); |
68938d83 |
153 | can_ok(undef, undef); |
c00d8759 |
154 | can_ok([], "foo"); |
0257f296 |
155 | err_ok( <<ERR ); |
b1ddf169 |
156 | # Failed test 'Mooble::Hooble::Yooble->can(...)' |
b7f9bbeb |
157 | # at $0 line 52. |
d020a79a |
158 | # Mooble::Hooble::Yooble->can('this') failed |
159 | # Mooble::Hooble::Yooble->can('that') failed |
b1ddf169 |
160 | # Failed test 'Mooble::Hooble::Yooble->can(...)' |
b7f9bbeb |
161 | # at $0 line 53. |
a9153838 |
162 | # can_ok() called with no methods |
68938d83 |
163 | # Failed test '->can(...)' |
b7f9bbeb |
164 | # at $0 line 54. |
68938d83 |
165 | # can_ok() called with empty class or reference |
c00d8759 |
166 | # Failed test 'ARRAY->can('foo')' |
878c69eb |
167 | # at $0 line 55. |
c00d8759 |
168 | # ARRAY->can('foo') failed |
30e302f8 |
169 | ERR |
170 | |
171 | #line 55 |
172 | isa_ok(bless([], "Foo"), "Wibble"); |
173 | isa_ok(42, "Wibble", "My Wibble"); |
174 | isa_ok(undef, "Wibble", "Another Wibble"); |
175 | isa_ok([], "HASH"); |
0257f296 |
176 | err_ok( <<ERR ); |
b1ddf169 |
177 | # Failed test 'The object isa Wibble' |
b7f9bbeb |
178 | # at $0 line 55. |
6686786d |
179 | # The object isn't a 'Wibble' it's a 'Foo' |
b1ddf169 |
180 | # Failed test 'My Wibble isa Wibble' |
b7f9bbeb |
181 | # at $0 line 56. |
33459055 |
182 | # My Wibble isn't a reference |
b1ddf169 |
183 | # Failed test 'Another Wibble isa Wibble' |
b7f9bbeb |
184 | # at $0 line 57. |
33459055 |
185 | # Another Wibble isn't defined |
b1ddf169 |
186 | # Failed test 'The object isa HASH' |
b7f9bbeb |
187 | # at $0 line 58. |
6686786d |
188 | # The object isn't a 'HASH' it's a 'ARRAY' |
30e302f8 |
189 | ERR |
190 | |
ccbd73a4 |
191 | |
192 | #line 188 |
193 | new_ok(undef); |
194 | err_like( <<ERR ); |
195 | # Failed test 'new\\(\\) died' |
196 | # at $Filename line 188. |
197 | # Error was: Can't call method "new" on an undefined value at .* |
198 | ERR |
199 | |
200 | #line 211 |
201 | new_ok( "Does::Not::Exist" ); |
202 | err_like( <<ERR ); |
203 | # Failed test 'new\\(\\) died' |
204 | # at $Filename line 211. |
205 | # Error was: Can't locate object method "new" via package "Does::Not::Exist" .* |
206 | ERR |
207 | |
208 | { package Foo; sub new { } } |
209 | { package Bar; sub new { {} } } |
210 | { package Baz; sub new { bless {}, "Wibble" } } |
211 | |
212 | #line 219 |
213 | new_ok( "Foo" ); |
214 | err_ok( <<ERR ); |
215 | # Failed test 'The object isa Foo' |
216 | # at $0 line 219. |
217 | # The object isn't defined |
218 | ERR |
219 | |
220 | # line 231 |
221 | new_ok( "Bar" ); |
222 | err_ok( <<ERR ); |
223 | # Failed test 'The object isa Bar' |
224 | # at $0 line 231. |
225 | # The object isn't a 'Bar' it's a 'HASH' |
226 | ERR |
227 | |
228 | #line 239 |
229 | new_ok( "Baz" ); |
230 | err_ok( <<ERR ); |
231 | # Failed test 'The object isa Baz' |
232 | # at $0 line 239. |
233 | # The object isn't a 'Baz' it's a 'Wibble' |
234 | ERR |
235 | |
236 | #line 247 |
237 | new_ok( "Baz", [], "no args" ); |
238 | err_ok( <<ERR ); |
239 | # Failed test 'no args isa Baz' |
240 | # at $0 line 247. |
241 | # no args isn't a 'Baz' it's a 'Wibble' |
242 | ERR |
243 | |
244 | |
30e302f8 |
245 | #line 68 |
246 | cmp_ok( 'foo', 'eq', 'bar', 'cmp_ok eq' ); |
247 | cmp_ok( 42.1, '==', 23, , ' ==' ); |
248 | cmp_ok( 42, '!=', 42 , ' !=' ); |
249 | cmp_ok( 1, '&&', 0 , ' &&' ); |
0257f296 |
250 | err_ok( <<ERR ); |
b1ddf169 |
251 | # Failed test 'cmp_ok eq' |
b7f9bbeb |
252 | # at $0 line 68. |
a9153838 |
253 | # got: 'foo' |
254 | # expected: 'bar' |
b1ddf169 |
255 | # Failed test ' ==' |
b7f9bbeb |
256 | # at $0 line 69. |
a9153838 |
257 | # got: 42.1 |
258 | # expected: 23 |
b1ddf169 |
259 | # Failed test ' !=' |
b7f9bbeb |
260 | # at $0 line 70. |
ccbd73a4 |
261 | # got: 42 |
262 | # expected: anything else |
b1ddf169 |
263 | # Failed test ' &&' |
b7f9bbeb |
264 | # at $0 line 71. |
a9153838 |
265 | # '1' |
266 | # && |
267 | # '0' |
b1ddf169 |
268 | ERR |
269 | |
270 | |
271 | # line 196 |
272 | cmp_ok( 42, 'eq', "foo", ' eq with numbers' ); |
273 | err_ok( <<ERR ); |
274 | # Failed test ' eq with numbers' |
b7f9bbeb |
275 | # at $0 line 196. |
a9153838 |
276 | # got: '42' |
277 | # expected: 'foo' |
30e302f8 |
278 | ERR |
279 | |
b1ddf169 |
280 | |
281 | { |
282 | my $warnings; |
283 | local $SIG{__WARN__} = sub { $warnings .= join '', @_ }; |
284 | |
285 | # line 211 |
286 | cmp_ok( 42, '==', "foo", ' == with strings' ); |
287 | err_ok( <<ERR ); |
288 | # Failed test ' == with strings' |
b7f9bbeb |
289 | # at $0 line 211. |
b1ddf169 |
290 | # got: 42 |
291 | # expected: foo |
292 | ERR |
293 | My::Test::like $warnings, |
294 | qq[/^Argument "foo" isn't numeric in .* at $Filename line 211\\\.\n\$/]; |
295 | |
296 | } |
297 | |
298 | |
30e302f8 |
299 | # generate a $!, it changes its value by context. |
300 | -e "wibblehibble"; |
301 | my $Errno_Number = $!+0; |
302 | my $Errno_String = $!.''; |
303 | #line 80 |
304 | cmp_ok( $!, 'eq', '', ' eq with stringified errno' ); |
305 | cmp_ok( $!, '==', -1, ' eq with numerified errno' ); |
0257f296 |
306 | err_ok( <<ERR ); |
b1ddf169 |
307 | # Failed test ' eq with stringified errno' |
b7f9bbeb |
308 | # at $0 line 80. |
a9153838 |
309 | # got: '$Errno_String' |
310 | # expected: '' |
b1ddf169 |
311 | # Failed test ' eq with numerified errno' |
b7f9bbeb |
312 | # at $0 line 81. |
a9153838 |
313 | # got: $Errno_Number |
314 | # expected: -1 |
3f2ec160 |
315 | ERR |
316 | |
30e302f8 |
317 | #line 84 |
318 | use_ok('Hooble::mooble::yooble'); |
b1ddf169 |
319 | |
320 | my $more_err_re = <<ERR; |
321 | # Failed test 'use Hooble::mooble::yooble;' |
b7f9bbeb |
322 | # at $Filename line 84\\. |
b1ddf169 |
323 | # Tried to use 'Hooble::mooble::yooble'. |
324 | # Error: Can't locate Hooble.* in \\\@INC .* |
b1ddf169 |
325 | ERR |
326 | |
327 | My::Test::like($err->read, "/^$more_err_re/"); |
328 | |
329 | |
330 | #line 85 |
30e302f8 |
331 | require_ok('ALL::YOUR::BASE::ARE::BELONG::TO::US::wibble'); |
b1ddf169 |
332 | $more_err_re = <<ERR; |
333 | # Failed test 'require ALL::YOUR::BASE::ARE::BELONG::TO::US::wibble;' |
b7f9bbeb |
334 | # at $Filename line 85\\. |
b1ddf169 |
335 | # Tried to require 'ALL::YOUR::BASE::ARE::BELONG::TO::US::wibble'. |
336 | # Error: Can't locate ALL.* in \\\@INC .* |
337 | ERR |
338 | |
339 | My::Test::like($err->read, "/^$more_err_re/"); |
340 | |
30e302f8 |
341 | |
342 | #line 88 |
343 | END { |
b1ddf169 |
344 | $TB->is_eq($$out, <<OUT, 'failing output'); |
30e302f8 |
345 | 1..$Total |
346 | not ok - failing |
347 | not ok - foo is bar? |
348 | not ok - undef is empty string? |
349 | not ok - undef is 0? |
350 | not ok - empty string is 0? |
351 | not ok - foo isnt foo? |
352 | not ok - foo isn't foo? |
353 | not ok - undef isnt undef? |
354 | not ok - is foo like that |
355 | not ok - is foo unlike foo |
356 | not ok - regex with % in it |
357 | not ok - fail() |
358 | not ok - Mooble::Hooble::Yooble->can(...) |
359 | not ok - Mooble::Hooble::Yooble->can(...) |
68938d83 |
360 | not ok - ->can(...) |
c00d8759 |
361 | not ok - ARRAY->can('foo') |
30e302f8 |
362 | not ok - The object isa Wibble |
363 | not ok - My Wibble isa Wibble |
364 | not ok - Another Wibble isa Wibble |
365 | not ok - The object isa HASH |
ccbd73a4 |
366 | not ok - new() died |
367 | not ok - new() died |
368 | not ok - The object isa Foo |
369 | not ok - The object isa Bar |
370 | not ok - The object isa Baz |
371 | not ok - no args isa Baz |
30e302f8 |
372 | not ok - cmp_ok eq |
373 | not ok - == |
374 | not ok - != |
375 | not ok - && |
30e302f8 |
376 | not ok - eq with numbers |
b1ddf169 |
377 | not ok - == with strings |
30e302f8 |
378 | not ok - eq with stringified errno |
379 | not ok - eq with numerified errno |
380 | not ok - use Hooble::mooble::yooble; |
381 | not ok - require ALL::YOUR::BASE::ARE::BELONG::TO::US::wibble; |
382 | OUT |
383 | |
b1ddf169 |
384 | err_ok( <<ERR ); |
a9153838 |
385 | # Looks like you failed $Total tests of $Total. |
3f2ec160 |
386 | ERR |
387 | |
3f2ec160 |
388 | exit(0); |
389 | } |