Assimilate Test-Simple 0.50
[p5sagit/p5-mst-13.2.git] / lib / Test / Simple / t / is_deeply.t
CommitLineData
a9153838 1#!/usr/bin/perl -w
33459055 2
3BEGIN {
a9153838 4 if( $ENV{PERL_CORE} ) {
5 chdir 't';
6 @INC = ('../lib', 'lib');
7 }
8 else {
9 unshift @INC, 't/lib';
10 }
33459055 11}
12
13use strict;
33459055 14
15use Test::Builder;
16require Test::Simple::Catch;
17my($out, $err) = Test::Simple::Catch::caught();
18Test::Builder->new->no_header(1);
19Test::Builder->new->no_ending(1);
30e302f8 20local $ENV{HARNESS_ACTIVE} = 0;
21
33459055 22
23# Can't use Test.pm, that's a 5.005 thing.
24package main;
25
30e302f8 26print "1..25\n";
33459055 27
28my $test_num = 1;
29# Utility testing functions.
30sub is ($$;$) {
31 my($this, $that, $name) = @_;
32 my $test = $$this eq $that;
33 my $ok = '';
34 $ok .= "not " unless $test;
35 $ok .= "ok $test_num";
36 $ok .= " - $name" if defined $name;
37 $ok .= "\n";
38 print $ok;
39
40 unless( $test ) {
41 print "# got \n$$this";
42 print "# expected \n$that";
43 }
44 $test_num++;
45
46 $$this = '';
47
48 return $test;
49}
50
51sub like ($$;$) {
52 my($this, $regex, $name) = @_;
30e302f8 53
54 $regex = qr/$regex/ unless ref $regex;
55 my $test = $$this =~ $regex;
33459055 56
57 my $ok = '';
58 $ok .= "not " unless $test;
59 $ok .= "ok $test_num";
60 $ok .= " - $name" if defined $name;
61 $ok .= "\n";
62 print $ok;
63
64 unless( $test ) {
65 print "# got \n$$this";
66 print "# expected \n$regex";
67 }
68 $test_num++;
69
70 $$this = '';
71
72
73 return $test;
74}
75
76
77require Test::More;
78Test::More->import(tests => 11, import => ['is_deeply']);
79
80my $Filename = quotemeta $0;
81
82#line 68
83is_deeply('foo', 'bar', 'plain strings');
84is( $out, "not ok 1 - plain strings\n", 'plain strings' );
85is( $err, <<ERR, ' right diagnostic' );
86# Failed test ($0 at line 68)
87# got: 'foo'
88# expected: 'bar'
89ERR
90
91
92#line 78
93is_deeply({}, [], 'different types');
94is( $out, "not ok 2 - different types\n", 'different types' );
95like( $err, <<ERR, ' right diagnostic' );
96# Failed test \\($Filename at line 78\\)
97# Structures begin differing at:
2f71ccc2 98# \\\$got = 'HASH\\(0x[0-9a-f]+\\)'
99# \\\$expected = 'ARRAY\\(0x[0-9a-f]+\\)'
33459055 100ERR
101
102#line 88
103is_deeply({ this => 42 }, { this => 43 }, 'hashes with different values');
104is( $out, "not ok 3 - hashes with different values\n",
105 'hashes with different values' );
106is( $err, <<ERR, ' right diagnostic' );
107# Failed test ($0 at line 88)
108# Structures begin differing at:
109# \$got->{this} = '42'
110# \$expected->{this} = '43'
111ERR
112
113#line 99
114is_deeply({ that => 42 }, { this => 42 }, 'hashes with different keys');
115is( $out, "not ok 4 - hashes with different keys\n",
116 'hashes with different keys' );
117is( $err, <<ERR, ' right diagnostic' );
118# Failed test ($0 at line 99)
119# Structures begin differing at:
120# \$got->{this} = Does not exist
121# \$expected->{this} = '42'
122ERR
123
124#line 110
125is_deeply([1..9], [1..10], 'arrays of different length');
126is( $out, "not ok 5 - arrays of different length\n",
127 'arrays of different length' );
128is( $err, <<ERR, ' right diagnostic' );
129# Failed test ($0 at line 110)
130# Structures begin differing at:
131# \$got->[9] = Does not exist
132# \$expected->[9] = '10'
133ERR
134
135#line 121
136is_deeply([undef, undef], [undef], 'arrays of undefs' );
137is( $out, "not ok 6 - arrays of undefs\n", 'arrays of undefs' );
138is( $err, <<ERR, ' right diagnostic' );
139# Failed test ($0 at line 121)
140# Structures begin differing at:
141# \$got->[1] = undef
142# \$expected->[1] = Does not exist
143ERR
144
145#line 131
30e302f8 146is_deeply({ foo => undef }, {}, 'hashes of undefs' );
33459055 147is( $out, "not ok 7 - hashes of undefs\n", 'hashes of undefs' );
148is( $err, <<ERR, ' right diagnostic' );
149# Failed test ($0 at line 131)
150# Structures begin differing at:
151# \$got->{foo} = undef
152# \$expected->{foo} = Does not exist
153ERR
154
155#line 141
156is_deeply(\42, \23, 'scalar refs');
157is( $out, "not ok 8 - scalar refs\n", 'scalar refs' );
158is( $err, <<ERR, ' right diagnostic' );
159# Failed test ($0 at line 141)
160# Structures begin differing at:
161# \${ \$got} = '42'
162# \${\$expected} = '23'
163ERR
164
165#line 151
166is_deeply([], \23, 'mixed scalar and array refs');
167is( $out, "not ok 9 - mixed scalar and array refs\n",
168 'mixed scalar and array refs' );
169like( $err, <<ERR, ' right diagnostic' );
170# Failed test \\($Filename at line 151\\)
171# Structures begin differing at:
2f71ccc2 172# \\\$got = 'ARRAY\\(0x[0-9a-f]+\\)'
173# \\\$expected = 'SCALAR\\(0x[0-9a-f]+\\)'
33459055 174ERR
175
176
177my($a1, $a2, $a3);
178$a1 = \$a2; $a2 = \$a3;
179$a3 = 42;
180
181my($b1, $b2, $b3);
182$b1 = \$b2; $b2 = \$b3;
183$b3 = 23;
184
185#line 173
186is_deeply($a1, $b1, 'deep scalar refs');
187is( $out, "not ok 10 - deep scalar refs\n", 'deep scalar refs' );
188is( $err, <<ERR, ' right diagnostic' );
189# Failed test ($0 at line 173)
190# Structures begin differing at:
191# \${\${ \$got}} = '42'
192# \${\${\$expected}} = '23'
193ERR
194
195# I don't know how to properly display this structure.
196# $a2 = { foo => \$a3 };
197# $b2 = { foo => \$b3 };
198# is_deeply([$a1], [$b1], 'deep mixed scalar refs');
199
200my $foo = {
201 this => [1..10],
202 that => { up => "down", left => "right" },
203 };
204
205my $bar = {
206 this => [1..10],
207 that => { up => "down", left => "right", foo => 42 },
208 };
209
210#line 198
211is_deeply( $foo, $bar, 'deep structures' );
212is( $out, "not ok 11 - deep structures\n", 'deep structures' );
213is( $err, <<ERR, ' right diagnostic' );
214# Failed test ($0 at line 198)
215# Structures begin differing at:
216# \$got->{that}{foo} = Does not exist
217# \$expected->{that}{foo} = '42'
218ERR
30e302f8 219
220
221#line 221
222my @tests = ([],
223 [qw(42)],
224 [qw(42 23), qw(42 23)]
225 );
226
227foreach my $test (@tests) {
228 my $num_args = @$test;
229
230 my $warning;
231 local $SIG{__WARN__} = sub { $warning .= join '', @_; };
232 is_deeply(@$test);
233
234 like \$warning,
235 qr/^is_deeply\(\) takes two or three args, you gave $num_args\.\n/;
236}