Upgrade to Test-Simple-0.82.
[p5sagit/p5-mst-13.2.git] / lib / Test / Simple / t / is_deeply_fail.t
CommitLineData
43fd88df 1#!/usr/bin/perl -w
ccbd73a4 2# $Id: /mirror/googlecode/test-more/t/is_deeply_fail.t 57943 2008-08-18T02:09:22.275428Z brooklyn.kid51 $
43fd88df 3
4BEGIN {
5 if( $ENV{PERL_CORE} ) {
6 chdir 't';
7 @INC = ('../lib', 'lib');
8 }
9 else {
10 unshift @INC, 't/lib';
11 }
12}
13
14use strict;
15
16use Test::Builder;
17require Test::Simple::Catch;
18my($out, $err) = Test::Simple::Catch::caught();
19Test::Builder->new->no_header(1);
20Test::Builder->new->no_ending(1);
21local $ENV{HARNESS_ACTIVE} = 0;
22
23
24# Can't use Test.pm, that's a 5.005 thing.
25package main;
26
27
28my $TB = Test::Builder->create;
845d7e37 29$TB->plan(tests => 73);
43fd88df 30
31# Utility testing functions.
32sub ok ($;$) {
33 return $TB->ok(@_);
34}
35
36sub is ($$;$) {
37 my($this, $that, $name) = @_;
38
39 my $ok = $TB->is_eq($$this, $that, $name);
40
41 $$this = '';
42
43 return $ok;
44}
45
46sub like ($$;$) {
47 my($this, $regex, $name) = @_;
b1ddf169 48 $regex = "/$regex/" if !ref $regex and $regex !~ m{^/.*/$}s;
43fd88df 49
50 my $ok = $TB->like($$this, $regex, $name);
51
52 $$this = '';
53
54 return $ok;
55}
56
57
58require Test::More;
59Test::More->import(tests => 11, import => ['is_deeply']);
60
61my $Filename = quotemeta $0;
62
63#line 68
64ok !is_deeply('foo', 'bar', 'plain strings');
65is( $out, "not ok 1 - plain strings\n", 'plain strings' );
66is( $err, <<ERR, ' right diagnostic' );
b1ddf169 67# Failed test 'plain strings'
b7f9bbeb 68# at $0 line 68.
43fd88df 69# got: 'foo'
70# expected: 'bar'
71ERR
72
73
74#line 78
75ok !is_deeply({}, [], 'different types');
76is( $out, "not ok 2 - different types\n", 'different types' );
77like( $err, <<ERR, ' right diagnostic' );
b1ddf169 78# Failed test 'different types'
b7f9bbeb 79# at $Filename line 78.
43fd88df 80# Structures begin differing at:
81# \\\$got = HASH\\(0x[0-9a-f]+\\)
82# \\\$expected = ARRAY\\(0x[0-9a-f]+\\)
83ERR
84
85#line 88
86ok !is_deeply({ this => 42 }, { this => 43 }, 'hashes with different values');
87is( $out, "not ok 3 - hashes with different values\n",
88 'hashes with different values' );
89is( $err, <<ERR, ' right diagnostic' );
b1ddf169 90# Failed test 'hashes with different values'
b7f9bbeb 91# at $0 line 88.
43fd88df 92# Structures begin differing at:
93# \$got->{this} = '42'
94# \$expected->{this} = '43'
95ERR
96
97#line 99
98ok !is_deeply({ that => 42 }, { this => 42 }, 'hashes with different keys');
99is( $out, "not ok 4 - hashes with different keys\n",
100 'hashes with different keys' );
101is( $err, <<ERR, ' right diagnostic' );
b1ddf169 102# Failed test 'hashes with different keys'
b7f9bbeb 103# at $0 line 99.
43fd88df 104# Structures begin differing at:
105# \$got->{this} = Does not exist
106# \$expected->{this} = '42'
107ERR
108
109#line 110
110ok !is_deeply([1..9], [1..10], 'arrays of different length');
111is( $out, "not ok 5 - arrays of different length\n",
112 'arrays of different length' );
113is( $err, <<ERR, ' right diagnostic' );
b1ddf169 114# Failed test 'arrays of different length'
b7f9bbeb 115# at $0 line 110.
43fd88df 116# Structures begin differing at:
117# \$got->[9] = Does not exist
118# \$expected->[9] = '10'
119ERR
120
121#line 121
122ok !is_deeply([undef, undef], [undef], 'arrays of undefs' );
123is( $out, "not ok 6 - arrays of undefs\n", 'arrays of undefs' );
124is( $err, <<ERR, ' right diagnostic' );
b1ddf169 125# Failed test 'arrays of undefs'
b7f9bbeb 126# at $0 line 121.
43fd88df 127# Structures begin differing at:
128# \$got->[1] = undef
129# \$expected->[1] = Does not exist
130ERR
131
132#line 131
133ok !is_deeply({ foo => undef }, {}, 'hashes of undefs' );
134is( $out, "not ok 7 - hashes of undefs\n", 'hashes of undefs' );
135is( $err, <<ERR, ' right diagnostic' );
b1ddf169 136# Failed test 'hashes of undefs'
b7f9bbeb 137# at $0 line 131.
43fd88df 138# Structures begin differing at:
139# \$got->{foo} = undef
140# \$expected->{foo} = Does not exist
141ERR
142
143#line 141
144ok !is_deeply(\42, \23, 'scalar refs');
145is( $out, "not ok 8 - scalar refs\n", 'scalar refs' );
146is( $err, <<ERR, ' right diagnostic' );
b1ddf169 147# Failed test 'scalar refs'
b7f9bbeb 148# at $0 line 141.
43fd88df 149# Structures begin differing at:
150# \${ \$got} = '42'
151# \${\$expected} = '23'
152ERR
153
154#line 151
155ok !is_deeply([], \23, 'mixed scalar and array refs');
156is( $out, "not ok 9 - mixed scalar and array refs\n",
157 'mixed scalar and array refs' );
158like( $err, <<ERR, ' right diagnostic' );
b1ddf169 159# Failed test 'mixed scalar and array refs'
b7f9bbeb 160# at $Filename line 151.
43fd88df 161# Structures begin differing at:
162# \\\$got = ARRAY\\(0x[0-9a-f]+\\)
163# \\\$expected = SCALAR\\(0x[0-9a-f]+\\)
164ERR
165
166
167my($a1, $a2, $a3);
168$a1 = \$a2; $a2 = \$a3;
169$a3 = 42;
170
171my($b1, $b2, $b3);
172$b1 = \$b2; $b2 = \$b3;
173$b3 = 23;
174
175#line 173
176ok !is_deeply($a1, $b1, 'deep scalar refs');
177is( $out, "not ok 10 - deep scalar refs\n", 'deep scalar refs' );
178is( $err, <<ERR, ' right diagnostic' );
b1ddf169 179# Failed test 'deep scalar refs'
b7f9bbeb 180# at $0 line 173.
43fd88df 181# Structures begin differing at:
182# \${\${ \$got}} = '42'
183# \${\${\$expected}} = '23'
184ERR
185
186# I don't know how to properly display this structure.
187# $a2 = { foo => \$a3 };
188# $b2 = { foo => \$b3 };
189# is_deeply([$a1], [$b1], 'deep mixed scalar refs');
190
191my $foo = {
192 this => [1..10],
193 that => { up => "down", left => "right" },
194 };
195
196my $bar = {
197 this => [1..10],
198 that => { up => "down", left => "right", foo => 42 },
199 };
200
201#line 198
202ok !is_deeply( $foo, $bar, 'deep structures' );
203ok( @Test::More::Data_Stack == 0, '@Data_Stack not holding onto things' );
204is( $out, "not ok 11 - deep structures\n", 'deep structures' );
205is( $err, <<ERR, ' right diagnostic' );
b1ddf169 206# Failed test 'deep structures'
b7f9bbeb 207# at $0 line 198.
43fd88df 208# Structures begin differing at:
209# \$got->{that}{foo} = Does not exist
210# \$expected->{that}{foo} = '42'
211ERR
212
213
214#line 221
215my @tests = ([],
216 [qw(42)],
217 [qw(42 23), qw(42 23)]
218 );
219
220foreach my $test (@tests) {
221 my $num_args = @$test;
222
223 my $warning;
224 local $SIG{__WARN__} = sub { $warning .= join '', @_; };
225 ok !is_deeply(@$test);
226
227 like \$warning,
b1ddf169 228 "/^is_deeply\\(\\) takes two or three args, you gave $num_args\.\n/";
43fd88df 229}
230
231
232#line 240
233# [rt.cpan.org 6837]
234ok !is_deeply([{Foo => undef}],[{Foo => ""}]), 'undef != ""';
235ok( @Test::More::Data_Stack == 0, '@Data_Stack not holding onto things' );
236
237
238#line 258
239# [rt.cpan.org 7031]
240my $a = [];
241ok !is_deeply($a, $a.''), "don't compare refs like strings";
242ok !is_deeply([$a], [$a.'']), " even deep inside";
243
244
245#line 265
246# [rt.cpan.org 7030]
247ok !is_deeply( {}, {key => []} ), '[] could match non-existent values';
248ok !is_deeply( [], [[]] );
249
250
251#line 273
252$$err = $$out = '';
253ok !is_deeply( [\'a', 'b'], [\'a', 'c'] );
254is( $out, "not ok 20\n", 'scalar refs in an array' );
255is( $err, <<ERR, ' right diagnostic' );
b7f9bbeb 256# Failed test at $0 line 274.
43fd88df 257# Structures begin differing at:
258# \$got->[1] = 'b'
259# \$expected->[1] = 'c'
260ERR
261
262
263#line 285
264my $ref = \23;
265ok !is_deeply( 23, $ref );
266is( $out, "not ok 21\n", 'scalar vs ref' );
267is( $err, <<ERR, ' right diagnostic');
b7f9bbeb 268# Failed test at $0 line 286.
43fd88df 269# Structures begin differing at:
270# \$got = '23'
271# \$expected = $ref
272ERR
273
274#line 296
275ok !is_deeply( $ref, 23 );
276is( $out, "not ok 22\n", 'ref vs scalar' );
277is( $err, <<ERR, ' right diagnostic');
b7f9bbeb 278# Failed test at $0 line 296.
43fd88df 279# Structures begin differing at:
280# \$got = $ref
281# \$expected = '23'
282ERR
283
284#line 306
285ok !is_deeply( undef, [] );
286is( $out, "not ok 23\n", 'is_deeply and undef [RT 9441]' );
287like( $err, <<ERR, ' right diagnostic' );
b7f9bbeb 288# Failed test at $Filename line 306\\.
43fd88df 289# Structures begin differing at:
290# \\\$got = undef
291# \\\$expected = ARRAY\\(0x[0-9a-f]+\\)
292ERR
293
294
295# rt.cpan.org 8865
296{
297 my $array = [];
298 my $hash = {};
299
300#line 321
301 ok !is_deeply( $array, $hash );
302 is( $out, "not ok 24\n", 'is_deeply and different reference types' );
303 is( $err, <<ERR, ' right diagnostic' );
b7f9bbeb 304# Failed test at $0 line 321.
43fd88df 305# Structures begin differing at:
306# \$got = $array
307# \$expected = $hash
308ERR
309
310#line 332
311 ok !is_deeply( [$array], [$hash] );
312 is( $out, "not ok 25\n", 'nested different ref types' );
313 is( $err, <<ERR, ' right diagnostic' );
b7f9bbeb 314# Failed test at $0 line 332.
43fd88df 315# Structures begin differing at:
316# \$got->[0] = $array
317# \$expected->[0] = $hash
318ERR
319
320
321 if( eval { require overload } ) {
322 my $foo = bless [], "Foo";
323 my $bar = bless {}, "Bar";
324
325 {
326 package Bar;
b1ddf169 327 "overload"->import(q[""] => sub { "wibble" });
43fd88df 328 }
329
330#line 353
331 ok !is_deeply( [$foo], [$bar] );
332 is( $out, "not ok 26\n", 'string overloaded refs respected in diag' );
333 is( $err, <<ERR, ' right diagnostic' );
b7f9bbeb 334# Failed test at $0 line 353.
43fd88df 335# Structures begin differing at:
336# \$got->[0] = $foo
337# \$expected->[0] = 'wibble'
338ERR
339
340 }
341 else {
342 $TB->skip("Needs overload.pm") for 1..3;
343 }
344}
845d7e37 345
346
347# rt.cpan.org 14746
348{
349# line 349
350 ok !is_deeply( sub {"foo"}, sub {"bar"} ), 'function refs';
351 is( $out, "not ok 27\n" );
352 like( $err, <<ERR, ' right diagnostic' );
b7f9bbeb 353# Failed test at $Filename line 349.
845d7e37 354# Structures begin differing at:
355# \\\$got = CODE\\(0x[0-9a-f]+\\)
356# \\\$expected = CODE\\(0x[0-9a-f]+\\)
357ERR
358
359
360 use Symbol;
361 my $glob1 = gensym;
362 my $glob2 = gensym;
363
364#line 357
365 ok !is_deeply( $glob1, $glob2 ), 'typeglobs';
366 is( $out, "not ok 28\n" );
367 like( $err, <<ERR, ' right diagnostic' );
b7f9bbeb 368# Failed test at $Filename line 357.
845d7e37 369# Structures begin differing at:
370# \\\$got = GLOB\\(0x[0-9a-f]+\\)
371# \\\$expected = GLOB\\(0x[0-9a-f]+\\)
372ERR
373
374}