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