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