Commit | Line | Data |
0d863452 |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | chdir 't'; |
5 | @INC = '../lib'; |
6 | require './test.pl'; |
7 | } |
8 | use strict; |
289d21b2 |
9 | use warnings; |
10 | no warnings 'uninitialized'; |
0d863452 |
11 | |
12 | use Tie::Array; |
13 | use Tie::Hash; |
b15feb55 |
14 | use Tie::RefHash; |
0d863452 |
15 | |
0d863452 |
16 | # Predeclare vars used in the tests: |
031a44ed |
17 | my @empty; |
18 | my %empty; |
015eb7b9 |
19 | my @sparse; $sparse[2] = 2; |
031a44ed |
20 | |
0d863452 |
21 | my $deep1 = []; push @$deep1, \$deep1; |
22 | my $deep2 = []; push @$deep2, \$deep2; |
23 | |
0d863452 |
24 | my @nums = (1..10); |
25 | tie my @tied_nums, 'Tie::StdArray'; |
26 | @tied_nums = (1..10); |
27 | |
28 | my %hash = (foo => 17, bar => 23); |
29 | tie my %tied_hash, 'Tie::StdHash'; |
30 | %tied_hash = %hash; |
31 | |
1cfb7049 |
32 | { |
33 | package Test::Object::NoOverload; |
34 | sub new { bless { key => 1 } } |
35 | } |
36 | |
37 | { |
90a32bcb |
38 | package Test::Object::WithOverload; |
2522c35a |
39 | sub new { bless { key => 'magic' } } |
2c9d2554 |
40 | use overload '~~' => sub { |
41 | my %hash = %{ $_[0] }; |
42 | if ($_[2]) { # arguments reversed ? |
43 | return $_[1] eq reverse $hash{key}; |
44 | } |
45 | else { |
46 | return $_[1] eq $hash{key}; |
47 | } |
48 | }; |
0483c672 |
49 | use overload '""' => sub { "stringified" }; |
90a32bcb |
50 | use overload 'eq' => sub {"$_[0]" eq "$_[1]"}; |
1cfb7049 |
51 | } |
52 | |
90a32bcb |
53 | our $ov_obj = Test::Object::WithOverload->new; |
1cfb7049 |
54 | our $obj = Test::Object::NoOverload->new; |
1cfb7049 |
55 | |
b15feb55 |
56 | tie my %refh, 'Tie::RefHash'; |
57 | $refh{$ov_obj} = 1; |
58 | |
73aec0b1 |
59 | my @keyandmore = qw(key and more); |
60 | my @fooormore = qw(foo or more); |
61 | my %keyandmore = map { $_ => 0 } @keyandmore; |
62 | my %fooormore = map { $_ => 0 } @fooormore; |
63 | |
0d863452 |
64 | # Load and run the tests |
9e079ace |
65 | plan "no_plan"; |
0d863452 |
66 | |
9e079ace |
67 | while (<DATA>) { |
68 | next if /^#/ || !/\S/; |
69 | chomp; |
73aec0b1 |
70 | my ($yn, $left, $right, $note) = split /\t+/; |
0d863452 |
71 | |
73aec0b1 |
72 | local $::TODO = $note =~ /TODO/; |
0d863452 |
73 | |
85af77a5 |
74 | die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/; |
9e079ace |
75 | |
0d863452 |
76 | my $tstr = "$left ~~ $right"; |
9e079ace |
77 | |
85af77a5 |
78 | test_again: |
289d21b2 |
79 | my $res; |
80 | if ($note =~ /NOWARNINGS/) { |
81 | $res = eval "no warnings; $tstr"; |
82 | } |
83 | else { |
84 | $res = eval $tstr; |
85 | } |
0d863452 |
86 | |
a86f5011 |
87 | chomp $@; |
88 | |
85af77a5 |
89 | if ( $yn =~ /@/ ) { |
9e079ace |
90 | ok( $@ ne '', "$tstr dies" ) |
91 | and print "# \$\@ was: $@\n"; |
a86f5011 |
92 | } else { |
85af77a5 |
93 | my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches"); |
a86f5011 |
94 | if ( $@ ne '' ) { |
9e079ace |
95 | fail($test_name); |
96 | print "# \$\@ was: $@\n"; |
a86f5011 |
97 | } else { |
85af77a5 |
98 | ok( ($yn =~ /!/ xor $res), $test_name ); |
a86f5011 |
99 | } |
100 | } |
85af77a5 |
101 | |
102 | if ( $yn =~ s/=// ) { |
103 | $tstr = "$right ~~ $left"; |
104 | goto test_again; |
105 | } |
0d863452 |
106 | } |
107 | |
0d863452 |
108 | sub foo {} |
73aec0b1 |
109 | sub bar {42} |
110 | sub gorch {42} |
1cfb7049 |
111 | sub fatal {die "fatal sub\n"} |
0d863452 |
112 | |
0cfbf1ea |
113 | # to test constant folding |
18d11902 |
114 | sub FALSE() { 0 } |
115 | sub TRUE() { 1 } |
2522c35a |
116 | sub NOT_DEF() { undef } |
0d863452 |
117 | |
e5de85fa |
118 | # Prefix character : |
119 | # - expected to match |
120 | # ! - expected to not match |
121 | # @ - expected to be a compilation failure |
85af77a5 |
122 | # = - expected to match symmetrically (runs test twice) |
73aec0b1 |
123 | # Data types to test : |
85af77a5 |
124 | # undef |
73aec0b1 |
125 | # Object-overloaded |
126 | # Object |
73aec0b1 |
127 | # Coderef |
128 | # Hash |
129 | # Hashref |
130 | # Array |
131 | # Arrayref |
85af77a5 |
132 | # Tied arrays and hashes |
133 | # Arrays that reference themselves |
73aec0b1 |
134 | # Regex (// and qr//) |
85af77a5 |
135 | # Range |
73aec0b1 |
136 | # Num |
137 | # Str |
85af77a5 |
138 | # Other syntactic items of interest: |
139 | # Constants |
140 | # Values returned by a sub call |
0d863452 |
141 | __DATA__ |
85af77a5 |
142 | # Any ~~ undef |
ad0781bc |
143 | ! $ov_obj undef |
85af77a5 |
144 | ! $obj undef |
145 | ! sub {} undef |
146 | ! %hash undef |
147 | ! \%hash undef |
148 | ! {} undef |
149 | ! @nums undef |
150 | ! \@nums undef |
151 | ! [] undef |
152 | ! %tied_hash undef |
153 | ! @tied_nums undef |
154 | ! $deep1 undef |
155 | ! /foo/ undef |
156 | ! qr/foo/ undef |
157 | ! 21..30 undef |
158 | ! 189 undef |
159 | ! "foo" undef |
160 | ! "" undef |
161 | ! !1 undef |
162 | undef undef |
62ec5f58 |
163 | (my $u) undef |
2522c35a |
164 | NOT_DEF undef |
165 | &NOT_DEF undef |
85af77a5 |
166 | |
167 | # Any ~~ object overloaded |
ad0781bc |
168 | ! \&fatal $ov_obj |
2c9d2554 |
169 | 'cigam' $ov_obj |
170 | ! 'cigam on' $ov_obj |
ad0781bc |
171 | ! $obj $ov_obj |
172 | ! undef $ov_obj |
1cfb7049 |
173 | |
174 | # regular object |
ad0781bc |
175 | @ $obj $obj |
6d743019 |
176 | @ $ov_obj $obj |
2c9d2554 |
177 | =@ \&fatal $obj |
ad0781bc |
178 | @ \&FALSE $obj |
179 | @ \&foo $obj |
180 | @ sub { 1 } $obj |
181 | @ sub { 0 } $obj |
182 | @ %keyandmore $obj |
183 | @ {"key" => 1} $obj |
184 | @ @fooormore $obj |
185 | @ ["key" => 1] $obj |
186 | @ /key/ $obj |
187 | @ qr/key/ $obj |
188 | @ "key" $obj |
189 | @ FALSE $obj |
190 | |
191 | # object (overloaded or not) ~~ Any |
0483c672 |
192 | $obj qr/NoOverload/ |
193 | $ov_obj qr/^stringified$/ |
2c9d2554 |
194 | "$ov_obj" "stringified" |
195 | $ov_obj 'magic' |
196 | ! $ov_obj 'not magic' |
1cfb7049 |
197 | |
a4a197da |
198 | # ~~ Coderef |
199 | sub{0} sub { ref $_[0] eq "CODE" } |
200 | %fooormore sub { $_[0] =~ /^(foo|or|more)$/ } |
201 | ! %fooormore sub { $_[0] =~ /^(foo|or|less)$/ } |
202 | \%fooormore sub { $_[0] =~ /^(foo|or|more)$/ } |
203 | ! \%fooormore sub { $_[0] =~ /^(foo|or|less)$/ } |
204 | +{%fooormore} sub { $_[0] =~ /^(foo|or|more)$/ } |
205 | ! +{%fooormore} sub { $_[0] =~ /^(foo|or|less)$/ } |
206 | @fooormore sub { $_[0] =~ /^(foo|or|more)$/ } |
207 | ! @fooormore sub { $_[0] =~ /^(foo|or|less)$/ } |
208 | \@fooormore sub { $_[0] =~ /^(foo|or|more)$/ } |
209 | ! \@fooormore sub { $_[0] =~ /^(foo|or|less)$/ } |
210 | [@fooormore] sub { $_[0] =~ /^(foo|or|more)$/ } |
211 | ! [@fooormore] sub { $_[0] =~ /^(foo|or|less)$/ } |
212 | %fooormore sub{@_==1} |
213 | @fooormore sub{@_==1} |
214 | "foo" sub { $_[0] =~ /^(foo|or|more)$/ } |
215 | ! "more" sub { $_[0] =~ /^(foo|or|less)$/ } |
73aec0b1 |
216 | /fooormore/ sub{ref $_[0] eq 'Regexp'} |
a4a197da |
217 | qr/fooormore/ sub{ref $_[0] eq 'Regexp'} |
218 | 1 sub{shift} |
219 | ! 0 sub{shift} |
220 | ! undef sub{shift} |
221 | undef sub{not shift} |
031a44ed |
222 | NOT_DEF sub{not shift} |
223 | &NOT_DEF sub{not shift} |
a4a197da |
224 | FALSE sub{not shift} |
225 | [1] \&bar |
226 | {a=>1} \&bar |
227 | qr// \&bar |
228 | ! [1] \&foo |
229 | ! {a=>1} \&foo |
41e726ac |
230 | $obj sub { ref($_[0]) =~ /NoOverload/ } |
90a32bcb |
231 | $ov_obj sub { ref($_[0]) =~ /WithOverload/ } |
a4a197da |
232 | # empty stuff matches, because the sub is never called: |
07edf497 |
233 | [] \&foo |
234 | {} \&foo |
031a44ed |
235 | @empty \&foo |
236 | %empty \&foo |
a4a197da |
237 | ! qr// \&foo |
238 | ! undef \&foo |
239 | undef \&bar |
240 | @ undef \&fatal |
241 | @ 1 \&fatal |
242 | @ [1] \&fatal |
203d1e89 |
243 | @ {a=>1} \&fatal |
a4a197da |
244 | @ "foo" \&fatal |
245 | @ qr// \&fatal |
203d1e89 |
246 | # sub is not called on empty hashes / arrays |
07edf497 |
247 | [] \&fatal |
248 | +{} \&fatal |
031a44ed |
249 | @empty \&fatal |
250 | %empty \&fatal |
0d863452 |
251 | |
0d863452 |
252 | # HASH ref against: |
253 | # - another hash ref |
254 | {} {} |
2a37c5e7 |
255 | =! {} {1 => 2} |
0d863452 |
256 | {1 => 2} {1 => 2} |
257 | {1 => 2} {1 => 3} |
031a44ed |
258 | =! {1 => 2} {2 => 3} |
259 | = \%main:: {map {$_ => 'x'} keys %main::} |
0d863452 |
260 | |
261 | # - tied hash ref |
2522c35a |
262 | = \%hash \%tied_hash |
0d863452 |
263 | \%tied_hash \%tied_hash |
031a44ed |
264 | != {"a"=>"b"} \%tied_hash |
265 | = %hash %tied_hash |
266 | %tied_hash %tied_hash |
267 | != {"a"=>"b"} %tied_hash |
b15feb55 |
268 | $ov_obj %refh |
269 | ! "$ov_obj" %refh |
270 | [$ov_obj] %refh |
271 | ! ["$ov_obj"] %refh |
272 | %refh %refh |
0d863452 |
273 | |
274 | # - an array ref |
031a44ed |
275 | # (since this is symmetrical, tests as well hash~~array) |
276 | = [keys %main::] \%:: |
277 | = [qw[STDIN STDOUT]] \%:: |
278 | =! [] \%:: |
279 | =! [""] {} |
280 | =! [] {} |
281 | =! @empty {} |
282 | = [undef] {"" => 1} |
283 | = [""] {"" => 1} |
284 | = ["foo"] { foo => 1 } |
285 | = ["foo", "bar"] { foo => 1 } |
286 | = ["foo", "bar"] \%hash |
287 | = ["foo"] \%hash |
288 | =! ["quux"] \%hash |
289 | = [qw(foo quux)] \%hash |
290 | = @fooormore { foo => 1, or => 2, more => 3 } |
291 | = @fooormore %fooormore |
292 | = @fooormore \%fooormore |
293 | = \@fooormore %fooormore |
0d863452 |
294 | |
295 | # - a regex |
ea0c2dbd |
296 | = qr/^(fo[ox])$/ {foo => 1} |
297 | = /^(fo[ox])$/ %fooormore |
031a44ed |
298 | =! qr/[13579]$/ +{0..99} |
ea0c2dbd |
299 | =! qr/a*/ {} |
031a44ed |
300 | = qr/a*/ {b=>2} |
ea0c2dbd |
301 | = qr/B/i {b=>2} |
302 | = /B/i {b=>2} |
303 | =! qr/a+/ {b=>2} |
304 | = qr/^à/ {"à"=>2} |
0d863452 |
305 | |
031a44ed |
306 | # - a scalar |
2e0e16c9 |
307 | "foo" +{foo => 1, bar => 2} |
031a44ed |
308 | "foo" %fooormore |
2e0e16c9 |
309 | ! "baz" +{foo => 1, bar => 2} |
031a44ed |
310 | ! "boz" %fooormore |
311 | ! 1 +{foo => 1, bar => 2} |
312 | ! 1 %fooormore |
313 | 1 { 1 => 3 } |
314 | 1.0 { 1 => 3 } |
315 | ! "1.0" { 1 => 3 } |
316 | ! "1.0" { 1.0 => 3 } |
317 | "1.0" { "1.0" => 3 } |
318 | "à" { "à" => "À" } |
0d863452 |
319 | |
61a621c6 |
320 | # - undef |
2522c35a |
321 | ! undef { hop => 'zouu' } |
61a621c6 |
322 | ! undef %hash |
323 | ! undef +{"" => "empty key"} |
2a37c5e7 |
324 | ! undef {} |
0d863452 |
325 | |
326 | # ARRAY ref against: |
327 | # - another array ref |
1cfb7049 |
328 | [] [] |
2522c35a |
329 | =! [] [1] |
ea0c2dbd |
330 | [["foo"], ["bar"]] [qr/o/, qr/a/] |
331 | ! [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/] |
0d863452 |
332 | ["foo", "bar"] [qr/o/, qr/a/] |
031a44ed |
333 | ! [qr/o/, qr/a/] ["foo", "bar"] |
2522c35a |
334 | ["foo", "bar"] [["foo"], ["bar"]] |
71b0fb34 |
335 | ! ["foo", "bar"] [qr/o/, "foo"] |
2522c35a |
336 | ["foo", undef, "bar"] [qr/o/, undef, "bar"] |
337 | ["foo", undef, "bar"] [qr/o/, "", "bar"] |
338 | ! ["foo", "", "bar"] [qr/o/, undef, "bar"] |
1cfb7049 |
339 | $deep1 $deep1 |
031a44ed |
340 | @$deep1 @$deep1 |
1cfb7049 |
341 | ! $deep1 $deep2 |
0d863452 |
342 | |
031a44ed |
343 | = \@nums \@tied_nums |
344 | = @nums \@tied_nums |
345 | = \@nums @tied_nums |
346 | = @nums @tied_nums |
347 | |
d0b243e3 |
348 | # - an object |
349 | ! $obj @fooormore |
41e726ac |
350 | $obj [sub{ref shift}] |
d0b243e3 |
351 | |
0d863452 |
352 | # - a regex |
ea0c2dbd |
353 | = qr/x/ [qw(foo bar baz quux)] |
354 | =! qr/y/ [qw(foo bar baz quux)] |
355 | = /x/ [qw(foo bar baz quux)] |
356 | =! /y/ [qw(foo bar baz quux)] |
357 | = /FOO/i @fooormore |
358 | =! /bar/ @fooormore |
0d863452 |
359 | |
360 | # - a number |
015eb7b9 |
361 | 2 [qw(1.00 2.00)] |
b0138e99 |
362 | 2 [qw(foo 2)] |
363 | 2.0_0e+0 [qw(foo 2)] |
364 | ! 2 [qw(1foo bar2)] |
0d863452 |
365 | |
366 | # - a string |
b0138e99 |
367 | ! "2" [qw(1foo 2bar)] |
368 | "2bar" [qw(1foo 2bar)] |
0d863452 |
369 | |
015eb7b9 |
370 | # - undef |
371 | undef [1, 2, undef, 4] |
372 | ! undef [1, 2, [undef], 4] |
373 | ! undef @fooormore |
374 | undef @sparse |
375 | |
376 | # - nested arrays and ~~ distributivity |
377 | 11 [[11]] |
378 | ! 11 [[12]] |
379 | "foo" [{foo => "bar"}] |
380 | ! "bar" [{foo => "bar"}] |
381 | |
0d863452 |
382 | # Number against number |
383 | 2 2 |
33ed63a2 |
384 | 20 2_0 |
0d863452 |
385 | ! 2 3 |
18d11902 |
386 | 0 FALSE |
387 | 3-2 TRUE |
33ed63a2 |
388 | undef 0 |
0d863452 |
389 | |
390 | # Number against string |
33ed63a2 |
391 | = 2 "2" |
392 | = 2 "2.0" |
0d863452 |
393 | ! 2 "2bananas" |
289d21b2 |
394 | != 2_3 "2_3" NOWARNINGS |
18d11902 |
395 | FALSE "0" |
0d863452 |
396 | |
397 | # Regex against string |
a566f585 |
398 | "x" qr/x/ |
399 | ! "x" qr/y/ |
0d863452 |
400 | |
401 | # Regex against number |
402 | 12345 qr/3/ |
2522c35a |
403 | ! 12345 qr/7/ |
0d863452 |
404 | |
031a44ed |
405 | # array/hash against string |
d444f7e3 |
406 | @fooormore "".\@fooormore |
407 | ! @keyandmore "".\@fooormore |
408 | %fooormore "".\%fooormore |
409 | ! %keyandmore "".\%fooormore |
f1bef09e |
410 | |
0d863452 |
411 | # Test the implicit referencing |
b0138e99 |
412 | 7 @nums |
0d863452 |
413 | @nums \@nums |
414 | ! @nums \\@nums |
415 | @nums [1..10] |
416 | ! @nums [0..9] |
417 | |
2e0e16c9 |
418 | "foo" %hash |
419 | /bar/ %hash |
420 | [qw(bar)] %hash |
421 | ! [qw(a b c)] %hash |
71b0fb34 |
422 | %hash %hash |
fceebc47 |
423 | %hash +{%hash} |
73aec0b1 |
424 | %hash \%hash |
71b0fb34 |
425 | %hash %tied_hash |
426 | %tied_hash %tied_hash |
427 | %hash { foo => 5, bar => 10 } |
428 | ! %hash { foo => 5, bar => 10, quux => 15 } |
429 | |
430 | @nums { 1, '', 2, '' } |
431 | @nums { 1, '', 12, '' } |
432 | ! @nums { 11, '', 12, '' } |