Upgrade to Test-Simple-0.82.
[p5sagit/p5-mst-13.2.git] / lib / Test / Simple / t / fail-more.t
CommitLineData
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 4BEGIN {
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 14use strict;
33459055 15
16require Test::Simple::Catch;
17my($out, $err) = Test::Simple::Catch::caught();
30e302f8 18local $ENV{HARNESS_ACTIVE} = 0;
33459055 19
20
3f2ec160 21# Can't use Test.pm, that's a 5.005 thing.
22package 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.
26require Test::Builder;
27my $TB = Test::Builder->create;
ccbd73a4 28$TB->plan(tests => 23);
b1ddf169 29
30sub like ($$;$) {
31 $TB->like(@_);
3f2ec160 32}
33
b1ddf169 34sub is ($$;$) {
35 $TB->is_eq(@_);
36}
3f2ec160 37
0257f296 38sub 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 45sub main::err_like ($) {
46 my($expect) = @_;
47 my $got = $err->read;
48
49 return $TB->like( $got, qr/$expect/ );
50}
51
30e302f8 52
3f2ec160 53package main;
d020a79a 54
3f2ec160 55require Test::More;
ccbd73a4 56my $Total = 36;
a9153838 57Test::More->import(tests => $Total);
3f2ec160 58
c00d8759 59# This should all work in the presence of a __DIE__ handler.
60local $SIG{__DIE__} = sub { $TB->ok(0, "DIE handler called: ".join "", @_); };
61
62
30e302f8 63my $tb = Test::More->builder;
64$tb->use_numbers(0);
65
b1ddf169 66my $Filename = quotemeta $0;
67
d020a79a 68# Preserve the line numbers.
33459055 69#line 38
3f2ec160 70ok( 0, 'failing' );
0257f296 71err_ok( <<ERR );
b1ddf169 72# Failed test 'failing'
b7f9bbeb 73# at $0 line 38.
30e302f8 74ERR
a9153838 75
76#line 40
77is( "foo", "bar", 'foo is bar?');
78is( undef, '', 'undef is empty string?');
79is( undef, 0, 'undef is 0?');
80is( '', 0, 'empty string is 0?' );
0257f296 81err_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 98ERR
99
100#line 45
101isnt("foo", "foo", 'foo isnt foo?' );
102isn't("foo", "foo",'foo isn\'t foo?' );
103isnt(undef, undef, 'undef isnt undef?');
0257f296 104err_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 117ERR
118
119#line 48
120like( "foo", '/that/', 'is foo like that' );
121unlike( "foo", '/foo/', 'is foo unlike foo' );
0257f296 122err_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 131ERR
132
133# Nick Clark found this was a bug. Fixed in 0.40.
b1ddf169 134# line 60
30e302f8 135like( "bug", '/(%)/', 'regex with % in it' );
0257f296 136err_ok( <<ERR );
b1ddf169 137# Failed test 'regex with % in it'
b7f9bbeb 138# at $0 line 60.
a9153838 139# 'bug'
140# doesn't match '/(%)/'
30e302f8 141ERR
142
b1ddf169 143#line 67
30e302f8 144fail('fail()');
0257f296 145err_ok( <<ERR );
b1ddf169 146# Failed test 'fail()'
b7f9bbeb 147# at $0 line 67.
30e302f8 148ERR
149
150#line 52
151can_ok('Mooble::Hooble::Yooble', qw(this that));
152can_ok('Mooble::Hooble::Yooble', ());
68938d83 153can_ok(undef, undef);
c00d8759 154can_ok([], "foo");
0257f296 155err_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 169ERR
170
171#line 55
172isa_ok(bless([], "Foo"), "Wibble");
173isa_ok(42, "Wibble", "My Wibble");
174isa_ok(undef, "Wibble", "Another Wibble");
175isa_ok([], "HASH");
0257f296 176err_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 189ERR
190
ccbd73a4 191
192#line 188
193new_ok(undef);
194err_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 .*
198ERR
199
200#line 211
201new_ok( "Does::Not::Exist" );
202err_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" .*
206ERR
207
208{ package Foo; sub new { } }
209{ package Bar; sub new { {} } }
210{ package Baz; sub new { bless {}, "Wibble" } }
211
212#line 219
213new_ok( "Foo" );
214err_ok( <<ERR );
215# Failed test 'The object isa Foo'
216# at $0 line 219.
217# The object isn't defined
218ERR
219
220# line 231
221new_ok( "Bar" );
222err_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'
226ERR
227
228#line 239
229new_ok( "Baz" );
230err_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'
234ERR
235
236#line 247
237new_ok( "Baz", [], "no args" );
238err_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'
242ERR
243
244
30e302f8 245#line 68
246cmp_ok( 'foo', 'eq', 'bar', 'cmp_ok eq' );
247cmp_ok( 42.1, '==', 23, , ' ==' );
248cmp_ok( 42, '!=', 42 , ' !=' );
249cmp_ok( 1, '&&', 0 , ' &&' );
0257f296 250err_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 268ERR
269
270
271# line 196
272cmp_ok( 42, 'eq', "foo", ' eq with numbers' );
273err_ok( <<ERR );
274# Failed test ' eq with numbers'
b7f9bbeb 275# at $0 line 196.
a9153838 276# got: '42'
277# expected: 'foo'
30e302f8 278ERR
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
292ERR
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";
301my $Errno_Number = $!+0;
302my $Errno_String = $!.'';
303#line 80
304cmp_ok( $!, 'eq', '', ' eq with stringified errno' );
305cmp_ok( $!, '==', -1, ' eq with numerified errno' );
0257f296 306err_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 315ERR
316
30e302f8 317#line 84
318use_ok('Hooble::mooble::yooble');
b1ddf169 319
320my $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 325ERR
326
327My::Test::like($err->read, "/^$more_err_re/");
328
329
330#line 85
30e302f8 331require_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 .*
337ERR
338
339My::Test::like($err->read, "/^$more_err_re/");
340
30e302f8 341
342#line 88
343END {
b1ddf169 344 $TB->is_eq($$out, <<OUT, 'failing output');
30e302f8 3451..$Total
346not ok - failing
347not ok - foo is bar?
348not ok - undef is empty string?
349not ok - undef is 0?
350not ok - empty string is 0?
351not ok - foo isnt foo?
352not ok - foo isn't foo?
353not ok - undef isnt undef?
354not ok - is foo like that
355not ok - is foo unlike foo
356not ok - regex with % in it
357not ok - fail()
358not ok - Mooble::Hooble::Yooble->can(...)
359not ok - Mooble::Hooble::Yooble->can(...)
68938d83 360not ok - ->can(...)
c00d8759 361not ok - ARRAY->can('foo')
30e302f8 362not ok - The object isa Wibble
363not ok - My Wibble isa Wibble
364not ok - Another Wibble isa Wibble
365not ok - The object isa HASH
ccbd73a4 366not ok - new() died
367not ok - new() died
368not ok - The object isa Foo
369not ok - The object isa Bar
370not ok - The object isa Baz
371not ok - no args isa Baz
30e302f8 372not ok - cmp_ok eq
373not ok - ==
374not ok - !=
375not ok - &&
30e302f8 376not ok - eq with numbers
b1ddf169 377not ok - == with strings
30e302f8 378not ok - eq with stringified errno
379not ok - eq with numerified errno
380not ok - use Hooble::mooble::yooble;
381not ok - require ALL::YOUR::BASE::ARE::BELONG::TO::US::wibble;
382OUT
383
b1ddf169 384err_ok( <<ERR );
a9153838 385# Looks like you failed $Total tests of $Total.
3f2ec160 386ERR
387
3f2ec160 388 exit(0);
389}