2 # $Id: /mirror/googlecode/test-more/t/fail-more.t 60310 2008-09-07T23:47:22.837229Z schwern $
5 if( $ENV{PERL_CORE} ) {
7 @INC = ('../lib', 'lib');
10 unshift @INC, 't/lib';
16 require Test::Simple::Catch;
17 my($out, $err) = Test::Simple::Catch::caught();
18 local $ENV{HARNESS_ACTIVE} = 0;
21 # Can't use Test.pm, that's a 5.005 thing.
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;
28 $TB->plan(tests => 23);
38 sub main::err_ok ($) {
42 return $TB->is_eq( $got, $expect );
45 sub main::err_like ($) {
49 return $TB->like( $got, qr/$expect/ );
57 Test::More->import(tests => $Total);
59 # This should all work in the presence of a __DIE__ handler.
60 local $SIG{__DIE__} = sub { $TB->ok(0, "DIE handler called: ".join "", @_); };
63 my $tb = Test::More->builder;
66 my $Filename = quotemeta $0;
68 # Preserve the line numbers.
72 # Failed test 'failing'
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?' );
82 # Failed test 'foo is bar?'
86 # Failed test 'undef is empty string?'
90 # Failed test 'undef is 0?'
94 # Failed test 'empty string is 0?'
101 isnt("foo", "foo", 'foo isnt foo?' );
102 isn't("foo", "foo",'foo isn\'t foo?' );
103 isnt(undef, undef, 'undef isnt undef?');
105 # Failed test 'foo isnt foo?'
108 # expected: anything else
109 # Failed test 'foo isn\'t foo?'
112 # expected: anything else
113 # Failed test 'undef isnt undef?'
116 # expected: anything else
120 like( "foo", '/that/', 'is foo like that' );
121 unlike( "foo", '/foo/', 'is foo unlike foo' );
123 # Failed test 'is foo like that'
126 # doesn't match '/that/'
127 # Failed test 'is foo unlike foo'
133 # Nick Clark found this was a bug. Fixed in 0.40.
135 like( "bug", '/(%)/', 'regex with % in it' );
137 # Failed test 'regex with % in it'
140 # doesn't match '/(%)/'
146 # Failed test 'fail()'
151 can_ok('Mooble::Hooble::Yooble', qw(this that));
152 can_ok('Mooble::Hooble::Yooble', ());
153 can_ok(undef, undef);
156 # Failed test 'Mooble::Hooble::Yooble->can(...)'
158 # Mooble::Hooble::Yooble->can('this') failed
159 # Mooble::Hooble::Yooble->can('that') failed
160 # Failed test 'Mooble::Hooble::Yooble->can(...)'
162 # can_ok() called with no methods
163 # Failed test '->can(...)'
165 # can_ok() called with empty class or reference
166 # Failed test 'ARRAY->can('foo')'
168 # ARRAY->can('foo') failed
172 isa_ok(bless([], "Foo"), "Wibble");
173 isa_ok(42, "Wibble", "My Wibble");
174 isa_ok(undef, "Wibble", "Another Wibble");
177 # Failed test 'The object isa Wibble'
179 # The object isn't a 'Wibble' it's a 'Foo'
180 # Failed test 'My Wibble isa Wibble'
182 # My Wibble isn't a reference
183 # Failed test 'Another Wibble isa Wibble'
185 # Another Wibble isn't defined
186 # Failed test 'The object isa HASH'
188 # The object isn't a 'HASH' it's a 'ARRAY'
195 # Failed test 'new\\(\\) died'
196 # at $Filename line 188.
197 # Error was: Can't call method "new" on an undefined value at .*
201 new_ok( "Does::Not::Exist" );
203 # Failed test 'new\\(\\) died'
204 # at $Filename line 211.
205 # Error was: Can't locate object method "new" via package "Does::Not::Exist" .*
208 { package Foo; sub new { } }
209 { package Bar; sub new { {} } }
210 { package Baz; sub new { bless {}, "Wibble" } }
215 # Failed test 'The object isa Foo'
217 # The object isn't defined
223 # Failed test 'The object isa Bar'
225 # The object isn't a 'Bar' it's a 'HASH'
231 # Failed test 'The object isa Baz'
233 # The object isn't a 'Baz' it's a 'Wibble'
237 new_ok( "Baz", [], "no args" );
239 # Failed test 'no args isa Baz'
241 # no args isn't a 'Baz' it's a 'Wibble'
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 , ' &&' );
251 # Failed test 'cmp_ok eq'
262 # expected: anything else
272 cmp_ok( 42, 'eq', "foo", ' eq with numbers' );
274 # Failed test ' eq with numbers'
283 local $SIG{__WARN__} = sub { $warnings .= join '', @_ };
286 cmp_ok( 42, '==', "foo", ' == with strings' );
288 # Failed test ' == with strings'
293 My::Test::like $warnings,
294 qq[/^Argument "foo" isn't numeric in .* at $Filename line 211\\\.\n\$/];
299 # generate a $!, it changes its value by context.
301 my $Errno_Number = $!+0;
302 my $Errno_String = $!.'';
304 cmp_ok( $!, 'eq', '', ' eq with stringified errno' );
305 cmp_ok( $!, '==', -1, ' eq with numerified errno' );
307 # Failed test ' eq with stringified errno'
309 # got: '$Errno_String'
311 # Failed test ' eq with numerified errno'
318 use_ok('Hooble::mooble::yooble');
320 my $more_err_re = <<ERR;
321 # Failed test 'use Hooble::mooble::yooble;'
322 # at $Filename line 84\\.
323 # Tried to use 'Hooble::mooble::yooble'.
324 # Error: Can't locate Hooble.* in \\\@INC .*
327 My::Test::like($err->read, "/^$more_err_re/");
331 require_ok('ALL::YOUR::BASE::ARE::BELONG::TO::US::wibble');
332 $more_err_re = <<ERR;
333 # Failed test 'require ALL::YOUR::BASE::ARE::BELONG::TO::US::wibble;'
334 # at $Filename line 85\\.
335 # Tried to require 'ALL::YOUR::BASE::ARE::BELONG::TO::US::wibble'.
336 # Error: Can't locate ALL.* in \\\@INC .*
339 My::Test::like($err->read, "/^$more_err_re/");
344 $TB->is_eq($$out, <<OUT, 'failing output');
348 not ok - undef is empty string?
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
358 not ok - Mooble::Hooble::Yooble->can(...)
359 not ok - Mooble::Hooble::Yooble->can(...)
361 not ok - ARRAY->can('foo')
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
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
376 not ok - eq with numbers
377 not ok - == with strings
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;
385 # Looks like you failed $Total tests of $Total.