Commit | Line | Data |
a9153838 |
1 | #!/usr/bin/perl -w |
33459055 |
2 | |
3 | BEGIN { |
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 | |
13 | use strict; |
33459055 |
14 | |
15 | use Test::Builder; |
16 | require Test::Simple::Catch; |
17 | my($out, $err) = Test::Simple::Catch::caught(); |
18 | Test::Builder->new->no_header(1); |
19 | Test::Builder->new->no_ending(1); |
30e302f8 |
20 | local $ENV{HARNESS_ACTIVE} = 0; |
21 | |
33459055 |
22 | |
23 | # Can't use Test.pm, that's a 5.005 thing. |
24 | package main; |
25 | |
30e302f8 |
26 | print "1..25\n"; |
33459055 |
27 | |
28 | my $test_num = 1; |
29 | # Utility testing functions. |
30 | sub 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 | |
51 | sub 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 | |
77 | require Test::More; |
78 | Test::More->import(tests => 11, import => ['is_deeply']); |
79 | |
80 | my $Filename = quotemeta $0; |
81 | |
82 | #line 68 |
83 | is_deeply('foo', 'bar', 'plain strings'); |
84 | is( $out, "not ok 1 - plain strings\n", 'plain strings' ); |
85 | is( $err, <<ERR, ' right diagnostic' ); |
86 | # Failed test ($0 at line 68) |
87 | # got: 'foo' |
88 | # expected: 'bar' |
89 | ERR |
90 | |
91 | |
92 | #line 78 |
93 | is_deeply({}, [], 'different types'); |
94 | is( $out, "not ok 2 - different types\n", 'different types' ); |
95 | like( $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 |
100 | ERR |
101 | |
102 | #line 88 |
103 | is_deeply({ this => 42 }, { this => 43 }, 'hashes with different values'); |
104 | is( $out, "not ok 3 - hashes with different values\n", |
105 | 'hashes with different values' ); |
106 | is( $err, <<ERR, ' right diagnostic' ); |
107 | # Failed test ($0 at line 88) |
108 | # Structures begin differing at: |
109 | # \$got->{this} = '42' |
110 | # \$expected->{this} = '43' |
111 | ERR |
112 | |
113 | #line 99 |
114 | is_deeply({ that => 42 }, { this => 42 }, 'hashes with different keys'); |
115 | is( $out, "not ok 4 - hashes with different keys\n", |
116 | 'hashes with different keys' ); |
117 | is( $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' |
122 | ERR |
123 | |
124 | #line 110 |
125 | is_deeply([1..9], [1..10], 'arrays of different length'); |
126 | is( $out, "not ok 5 - arrays of different length\n", |
127 | 'arrays of different length' ); |
128 | is( $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' |
133 | ERR |
134 | |
135 | #line 121 |
136 | is_deeply([undef, undef], [undef], 'arrays of undefs' ); |
137 | is( $out, "not ok 6 - arrays of undefs\n", 'arrays of undefs' ); |
138 | is( $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 |
143 | ERR |
144 | |
145 | #line 131 |
30e302f8 |
146 | is_deeply({ foo => undef }, {}, 'hashes of undefs' ); |
33459055 |
147 | is( $out, "not ok 7 - hashes of undefs\n", 'hashes of undefs' ); |
148 | is( $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 |
153 | ERR |
154 | |
155 | #line 141 |
156 | is_deeply(\42, \23, 'scalar refs'); |
157 | is( $out, "not ok 8 - scalar refs\n", 'scalar refs' ); |
158 | is( $err, <<ERR, ' right diagnostic' ); |
159 | # Failed test ($0 at line 141) |
160 | # Structures begin differing at: |
161 | # \${ \$got} = '42' |
162 | # \${\$expected} = '23' |
163 | ERR |
164 | |
165 | #line 151 |
166 | is_deeply([], \23, 'mixed scalar and array refs'); |
167 | is( $out, "not ok 9 - mixed scalar and array refs\n", |
168 | 'mixed scalar and array refs' ); |
169 | like( $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 |
174 | ERR |
175 | |
176 | |
177 | my($a1, $a2, $a3); |
178 | $a1 = \$a2; $a2 = \$a3; |
179 | $a3 = 42; |
180 | |
181 | my($b1, $b2, $b3); |
182 | $b1 = \$b2; $b2 = \$b3; |
183 | $b3 = 23; |
184 | |
185 | #line 173 |
186 | is_deeply($a1, $b1, 'deep scalar refs'); |
187 | is( $out, "not ok 10 - deep scalar refs\n", 'deep scalar refs' ); |
188 | is( $err, <<ERR, ' right diagnostic' ); |
189 | # Failed test ($0 at line 173) |
190 | # Structures begin differing at: |
191 | # \${\${ \$got}} = '42' |
192 | # \${\${\$expected}} = '23' |
193 | ERR |
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 | |
200 | my $foo = { |
201 | this => [1..10], |
202 | that => { up => "down", left => "right" }, |
203 | }; |
204 | |
205 | my $bar = { |
206 | this => [1..10], |
207 | that => { up => "down", left => "right", foo => 42 }, |
208 | }; |
209 | |
210 | #line 198 |
211 | is_deeply( $foo, $bar, 'deep structures' ); |
212 | is( $out, "not ok 11 - deep structures\n", 'deep structures' ); |
213 | is( $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' |
218 | ERR |
30e302f8 |
219 | |
220 | |
221 | #line 221 |
222 | my @tests = ([], |
223 | [qw(42)], |
224 | [qw(42 23), qw(42 23)] |
225 | ); |
226 | |
227 | foreach 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 | } |