2 # $Id: /mirror/googlecode/test-more/t/is_deeply_fail.t 57943 2008-08-18T02:09:22.275428Z brooklyn.kid51 $
5 if( $ENV{PERL_CORE} ) {
7 @INC = ('../lib', 'lib');
10 unshift @INC, 't/lib';
17 require Test::Simple::Catch;
18 my($out, $err) = Test::Simple::Catch::caught();
19 Test::Builder->new->no_header(1);
20 Test::Builder->new->no_ending(1);
21 local $ENV{HARNESS_ACTIVE} = 0;
24 # Can't use Test.pm, that's a 5.005 thing.
28 my $TB = Test::Builder->create;
29 $TB->plan(tests => 73);
31 # Utility testing functions.
37 my($this, $that, $name) = @_;
39 my $ok = $TB->is_eq($$this, $that, $name);
47 my($this, $regex, $name) = @_;
48 $regex = "/$regex/" if !ref $regex and $regex !~ m{^/.*/$}s;
50 my $ok = $TB->like($$this, $regex, $name);
59 Test::More->import(tests => 11, import => ['is_deeply']);
61 my $Filename = quotemeta $0;
64 ok !is_deeply('foo', 'bar', 'plain strings');
65 is( $out, "not ok 1 - plain strings\n", 'plain strings' );
66 is( $err, <<ERR, ' right diagnostic' );
67 # Failed test 'plain strings'
75 ok !is_deeply({}, [], 'different types');
76 is( $out, "not ok 2 - different types\n", 'different types' );
77 like( $err, <<ERR, ' right diagnostic' );
78 # Failed test 'different types'
79 # at $Filename line 78.
80 # Structures begin differing at:
81 # \\\$got = HASH\\(0x[0-9a-f]+\\)
82 # \\\$expected = ARRAY\\(0x[0-9a-f]+\\)
86 ok !is_deeply({ this => 42 }, { this => 43 }, 'hashes with different values');
87 is( $out, "not ok 3 - hashes with different values\n",
88 'hashes with different values' );
89 is( $err, <<ERR, ' right diagnostic' );
90 # Failed test 'hashes with different values'
92 # Structures begin differing at:
93 # \$got->{this} = '42'
94 # \$expected->{this} = '43'
98 ok !is_deeply({ that => 42 }, { this => 42 }, 'hashes with different keys');
99 is( $out, "not ok 4 - hashes with different keys\n",
100 'hashes with different keys' );
101 is( $err, <<ERR, ' right diagnostic' );
102 # Failed test 'hashes with different keys'
104 # Structures begin differing at:
105 # \$got->{this} = Does not exist
106 # \$expected->{this} = '42'
110 ok !is_deeply([1..9], [1..10], 'arrays of different length');
111 is( $out, "not ok 5 - arrays of different length\n",
112 'arrays of different length' );
113 is( $err, <<ERR, ' right diagnostic' );
114 # Failed test 'arrays of different length'
116 # Structures begin differing at:
117 # \$got->[9] = Does not exist
118 # \$expected->[9] = '10'
122 ok !is_deeply([undef, undef], [undef], 'arrays of undefs' );
123 is( $out, "not ok 6 - arrays of undefs\n", 'arrays of undefs' );
124 is( $err, <<ERR, ' right diagnostic' );
125 # Failed test 'arrays of undefs'
127 # Structures begin differing at:
129 # \$expected->[1] = Does not exist
133 ok !is_deeply({ foo => undef }, {}, 'hashes of undefs' );
134 is( $out, "not ok 7 - hashes of undefs\n", 'hashes of undefs' );
135 is( $err, <<ERR, ' right diagnostic' );
136 # Failed test 'hashes of undefs'
138 # Structures begin differing at:
139 # \$got->{foo} = undef
140 # \$expected->{foo} = Does not exist
144 ok !is_deeply(\42, \23, 'scalar refs');
145 is( $out, "not ok 8 - scalar refs\n", 'scalar refs' );
146 is( $err, <<ERR, ' right diagnostic' );
147 # Failed test 'scalar refs'
149 # Structures begin differing at:
151 # \${\$expected} = '23'
155 ok !is_deeply([], \23, 'mixed scalar and array refs');
156 is( $out, "not ok 9 - mixed scalar and array refs\n",
157 'mixed scalar and array refs' );
158 like( $err, <<ERR, ' right diagnostic' );
159 # Failed test 'mixed scalar and array refs'
160 # at $Filename line 151.
161 # Structures begin differing at:
162 # \\\$got = ARRAY\\(0x[0-9a-f]+\\)
163 # \\\$expected = SCALAR\\(0x[0-9a-f]+\\)
168 $a1 = \$a2; $a2 = \$a3;
172 $b1 = \$b2; $b2 = \$b3;
176 ok !is_deeply($a1, $b1, 'deep scalar refs');
177 is( $out, "not ok 10 - deep scalar refs\n", 'deep scalar refs' );
178 is( $err, <<ERR, ' right diagnostic' );
179 # Failed test 'deep scalar refs'
181 # Structures begin differing at:
182 # \${\${ \$got}} = '42'
183 # \${\${\$expected}} = '23'
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');
193 that => { up => "down", left => "right" },
198 that => { up => "down", left => "right", foo => 42 },
202 ok !is_deeply( $foo, $bar, 'deep structures' );
203 ok( @Test::More::Data_Stack == 0, '@Data_Stack not holding onto things' );
204 is( $out, "not ok 11 - deep structures\n", 'deep structures' );
205 is( $err, <<ERR, ' right diagnostic' );
206 # Failed test 'deep structures'
208 # Structures begin differing at:
209 # \$got->{that}{foo} = Does not exist
210 # \$expected->{that}{foo} = '42'
217 [qw(42 23), qw(42 23)]
220 foreach my $test (@tests) {
221 my $num_args = @$test;
224 local $SIG{__WARN__} = sub { $warning .= join '', @_; };
225 ok !is_deeply(@$test);
228 "/^is_deeply\\(\\) takes two or three args, you gave $num_args\.\n/";
234 ok !is_deeply([{Foo => undef}],[{Foo => ""}]), 'undef != ""';
235 ok( @Test::More::Data_Stack == 0, '@Data_Stack not holding onto things' );
241 ok !is_deeply($a, $a.''), "don't compare refs like strings";
242 ok !is_deeply([$a], [$a.'']), " even deep inside";
247 ok !is_deeply( {}, {key => []} ), '[] could match non-existent values';
248 ok !is_deeply( [], [[]] );
253 ok !is_deeply( [\'a', 'b'], [\'a', 'c'] );
254 is( $out, "not ok 20\n", 'scalar refs in an array' );
255 is( $err, <<ERR, ' right diagnostic' );
256 # Failed test at $0 line 274.
257 # Structures begin differing at:
259 # \$expected->[1] = 'c'
265 ok !is_deeply( 23, $ref );
266 is( $out, "not ok 21\n", 'scalar vs ref' );
267 is( $err, <<ERR, ' right diagnostic');
268 # Failed test at $0 line 286.
269 # Structures begin differing at:
275 ok !is_deeply( $ref, 23 );
276 is( $out, "not ok 22\n", 'ref vs scalar' );
277 is( $err, <<ERR, ' right diagnostic');
278 # Failed test at $0 line 296.
279 # Structures begin differing at:
285 ok !is_deeply( undef, [] );
286 is( $out, "not ok 23\n", 'is_deeply and undef [RT 9441]' );
287 like( $err, <<ERR, ' right diagnostic' );
288 # Failed test at $Filename line 306\\.
289 # Structures begin differing at:
291 # \\\$expected = ARRAY\\(0x[0-9a-f]+\\)
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' );
304 # Failed test at $0 line 321.
305 # Structures begin differing at:
311 ok !is_deeply( [$array], [$hash] );
312 is( $out, "not ok 25\n", 'nested different ref types' );
313 is( $err, <<ERR, ' right diagnostic' );
314 # Failed test at $0 line 332.
315 # Structures begin differing at:
316 # \$got->[0] = $array
317 # \$expected->[0] = $hash
321 if( eval { require overload } ) {
322 my $foo = bless [], "Foo";
323 my $bar = bless {}, "Bar";
327 "overload"->import(q[""] => sub { "wibble" });
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' );
334 # Failed test at $0 line 353.
335 # Structures begin differing at:
337 # \$expected->[0] = 'wibble'
342 $TB->skip("Needs overload.pm") for 1..3;
350 ok !is_deeply( sub {"foo"}, sub {"bar"} ), 'function refs';
351 is( $out, "not ok 27\n" );
352 like( $err, <<ERR, ' right diagnostic' );
353 # Failed test at $Filename line 349.
354 # Structures begin differing at:
355 # \\\$got = CODE\\(0x[0-9a-f]+\\)
356 # \\\$expected = CODE\\(0x[0-9a-f]+\\)
365 ok !is_deeply( $glob1, $glob2 ), 'typeglobs';
366 is( $out, "not ok 28\n" );
367 like( $err, <<ERR, ' right diagnostic' );
368 # Failed test at $Filename line 357.
369 # Structures begin differing at:
370 # \\\$got = GLOB\\(0x[0-9a-f]+\\)
371 # \\\$expected = GLOB\\(0x[0-9a-f]+\\)