This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Assimilate Test-Simple 0.50
[perl5.git] / lib / Test / Simple / t / fail-more.t
CommitLineData
33459055 1#!perl -w
3f2ec160 2
15db8fc4 3BEGIN {
a9153838
MS
4 if( $ENV{PERL_CORE} ) {
5 chdir 't';
6 @INC = ('../lib', 'lib');
7 }
8 else {
9 unshift @INC, 't/lib';
10 }
15db8fc4
JH
11}
12
33459055 13use strict;
33459055
MS
14
15require Test::Simple::Catch;
16my($out, $err) = Test::Simple::Catch::caught();
30e302f8 17local $ENV{HARNESS_ACTIVE} = 0;
33459055
MS
18
19
3f2ec160
JH
20# Can't use Test.pm, that's a 5.005 thing.
21package My::Test;
22
30e302f8 23print "1..12\n";
3f2ec160
JH
24
25my $test_num = 1;
26# Utility testing functions.
27sub ok ($;$) {
28 my($test, $name) = @_;
1af51bd3
JH
29 my $ok = '';
30 $ok .= "not " unless $test;
31 $ok .= "ok $test_num";
32 $ok .= " - $name" if defined $name;
33 $ok .= "\n";
34 print $ok;
3f2ec160 35 $test_num++;
d020a79a
JH
36
37 return $test;
3f2ec160
JH
38}
39
40
30e302f8
NC
41sub main::err ($) {
42 my($expect) = @_;
43 my $got = $err->read;
44
45 my $ok = ok( $got eq $expect );
46
47 unless( $ok ) {
48 print STDERR "$got\n";
49 print STDERR "$expect\n";
50 }
51
52 return $ok;
53}
54
55
3f2ec160 56package main;
d020a79a 57
3f2ec160 58require Test::More;
30e302f8 59my $Total = 29;
a9153838 60Test::More->import(tests => $Total);
3f2ec160 61
30e302f8
NC
62my $tb = Test::More->builder;
63$tb->use_numbers(0);
64
d020a79a 65# Preserve the line numbers.
33459055 66#line 38
3f2ec160 67ok( 0, 'failing' );
30e302f8
NC
68err( <<ERR );
69# Failed test ($0 at line 38)
70ERR
a9153838
MS
71
72#line 40
73is( "foo", "bar", 'foo is bar?');
74is( undef, '', 'undef is empty string?');
75is( undef, 0, 'undef is 0?');
76is( '', 0, 'empty string is 0?' );
30e302f8 77err( <<ERR );
a9153838 78# Failed test ($0 at line 40)
3f2ec160
JH
79# got: 'foo'
80# expected: 'bar'
33459055 81# Failed test ($0 at line 41)
a9153838
MS
82# got: undef
83# expected: ''
84# Failed test ($0 at line 42)
85# got: undef
86# expected: '0'
33459055 87# Failed test ($0 at line 43)
a9153838
MS
88# got: ''
89# expected: '0'
30e302f8
NC
90ERR
91
92#line 45
93isnt("foo", "foo", 'foo isnt foo?' );
94isn't("foo", "foo",'foo isn\'t foo?' );
95isnt(undef, undef, 'undef isnt undef?');
96err( <<ERR );
a9153838
MS
97# Failed test ($0 at line 45)
98# 'foo'
99# ne
100# 'foo'
101# Failed test ($0 at line 46)
102# 'foo'
103# ne
104# 'foo'
30e302f8
NC
105# Failed test ($0 at line 47)
106# undef
107# ne
108# undef
109ERR
110
111#line 48
112like( "foo", '/that/', 'is foo like that' );
113unlike( "foo", '/foo/', 'is foo unlike foo' );
114err( <<ERR );
a9153838 115# Failed test ($0 at line 48)
3f2ec160
JH
116# 'foo'
117# doesn't match '/that/'
a9153838
MS
118# Failed test ($0 at line 49)
119# 'foo'
120# matches '/foo/'
30e302f8
NC
121ERR
122
123# Nick Clark found this was a bug. Fixed in 0.40.
124like( "bug", '/(%)/', 'regex with % in it' );
125err( <<ERR );
126# Failed test ($0 at line 60)
a9153838
MS
127# 'bug'
128# doesn't match '/(%)/'
30e302f8
NC
129ERR
130
131fail('fail()');
132err( <<ERR );
133# Failed test ($0 at line 67)
134ERR
135
136#line 52
137can_ok('Mooble::Hooble::Yooble', qw(this that));
138can_ok('Mooble::Hooble::Yooble', ());
139err( <<ERR );
a9153838 140# Failed test ($0 at line 52)
d020a79a
JH
141# Mooble::Hooble::Yooble->can('this') failed
142# Mooble::Hooble::Yooble->can('that') failed
a9153838
MS
143# Failed test ($0 at line 53)
144# can_ok() called with no methods
30e302f8
NC
145ERR
146
147#line 55
148isa_ok(bless([], "Foo"), "Wibble");
149isa_ok(42, "Wibble", "My Wibble");
150isa_ok(undef, "Wibble", "Another Wibble");
151isa_ok([], "HASH");
152err( <<ERR );
a9153838 153# Failed test ($0 at line 55)
6686786d 154# The object isn't a 'Wibble' it's a 'Foo'
a9153838 155# Failed test ($0 at line 56)
33459055 156# My Wibble isn't a reference
a9153838 157# Failed test ($0 at line 57)
33459055 158# Another Wibble isn't defined
a9153838 159# Failed test ($0 at line 58)
6686786d 160# The object isn't a 'HASH' it's a 'ARRAY'
30e302f8
NC
161ERR
162
163#line 68
164cmp_ok( 'foo', 'eq', 'bar', 'cmp_ok eq' );
165cmp_ok( 42.1, '==', 23, , ' ==' );
166cmp_ok( 42, '!=', 42 , ' !=' );
167cmp_ok( 1, '&&', 0 , ' &&' );
168cmp_ok( 42, '==', "foo", ' == with strings' );
169cmp_ok( 42, 'eq', "foo", ' eq with numbers' );
170cmp_ok( undef, 'eq', 'foo', ' eq with undef' );
171err( <<ERR );
a9153838
MS
172# Failed test ($0 at line 68)
173# got: 'foo'
174# expected: 'bar'
175# Failed test ($0 at line 69)
176# got: 42.1
177# expected: 23
178# Failed test ($0 at line 70)
179# '42'
180# !=
181# '42'
182# Failed test ($0 at line 71)
183# '1'
184# &&
185# '0'
186# Failed test ($0 at line 72)
187# got: 42
188# expected: 0
189# Failed test ($0 at line 73)
190# got: '42'
191# expected: 'foo'
192# Failed test ($0 at line 74)
193# got: undef
194# expected: 'foo'
30e302f8
NC
195ERR
196
197# generate a $!, it changes its value by context.
198-e "wibblehibble";
199my $Errno_Number = $!+0;
200my $Errno_String = $!.'';
201#line 80
202cmp_ok( $!, 'eq', '', ' eq with stringified errno' );
203cmp_ok( $!, '==', -1, ' eq with numerified errno' );
204err( <<ERR );
a9153838
MS
205# Failed test ($0 at line 80)
206# got: '$Errno_String'
207# expected: ''
208# Failed test ($0 at line 81)
209# got: $Errno_Number
210# expected: -1
3f2ec160
JH
211ERR
212
30e302f8
NC
213#line 84
214use_ok('Hooble::mooble::yooble');
215require_ok('ALL::YOUR::BASE::ARE::BELONG::TO::US::wibble');
216
217#line 88
218END {
219 My::Test::ok($$out eq <<OUT, 'failing output');
2201..$Total
221not ok - failing
222not ok - foo is bar?
223not ok - undef is empty string?
224not ok - undef is 0?
225not ok - empty string is 0?
226not ok - foo isnt foo?
227not ok - foo isn't foo?
228not ok - undef isnt undef?
229not ok - is foo like that
230not ok - is foo unlike foo
231not ok - regex with % in it
232not ok - fail()
233not ok - Mooble::Hooble::Yooble->can(...)
234not ok - Mooble::Hooble::Yooble->can(...)
235not ok - The object isa Wibble
236not ok - My Wibble isa Wibble
237not ok - Another Wibble isa Wibble
238not ok - The object isa HASH
239not ok - cmp_ok eq
240not ok - ==
241not ok - !=
242not ok - &&
243not ok - == with strings
244not ok - eq with numbers
245not ok - eq with undef
246not ok - eq with stringified errno
247not ok - eq with numerified errno
248not ok - use Hooble::mooble::yooble;
249not ok - require ALL::YOUR::BASE::ARE::BELONG::TO::US::wibble;
250OUT
251
1af51bd3 252 my $filename = quotemeta $0;
3f2ec160 253 my $more_err_re = <<ERR;
a9153838 254# Failed test \\($filename at line 84\\)
3f2ec160 255# Tried to use 'Hooble::mooble::yooble'.
ea071790 256# Error: Can't locate Hooble.* in \\\@INC .*
30e302f8 257# BEGIN failed--compilation aborted at $filename line 84.
a9153838 258# Failed test \\($filename at line 85\\)
3f2ec160 259# Tried to require 'ALL::YOUR::BASE::ARE::BELONG::TO::US::wibble'.
ea071790 260# Error: Can't locate ALL.* in \\\@INC .*
a9153838 261# Looks like you failed $Total tests of $Total.
3f2ec160
JH
262ERR
263
30e302f8 264 unless( My::Test::ok($$err =~ /^$more_err_re$/,
d020a79a 265 'failing errors') ) {
a9153838 266 print $$err;
d020a79a 267 }
3f2ec160
JH
268
269 exit(0);
270}