10 no warnings 'uninitialized';
15 # Predeclare vars used in the tests:
18 my @sparse; $sparse[2] = 2;
20 my $deep1 = []; push @$deep1, $deep1;
21 my $deep2 = []; push @$deep2, $deep2;
24 tie my @tied_nums, 'Tie::StdArray';
27 my %hash = (foo => 17, bar => 23);
28 tie my %tied_hash, 'Tie::StdHash';
32 package Test::Object::NoOverload;
33 sub new { bless { key => 1 } }
37 package Test::Object::StringOverload;
38 use overload '""' => sub { "object" }, fallback => 1;
39 sub new { bless { key => 1 } }
43 package Test::Object::WithOverload;
44 sub new { bless { key => ($_[1] // 'magic') } }
45 use overload '~~' => sub {
46 my %hash = %{ $_[0] };
47 if ($_[2]) { # arguments reversed ?
48 return $_[1] eq reverse $hash{key};
51 return $_[1] eq $hash{key};
54 use overload '""' => sub { "stringified" };
55 use overload 'eq' => sub {"$_[0]" eq "$_[1]"};
58 our $ov_obj = Test::Object::WithOverload->new;
59 our $ov_obj_2 = Test::Object::WithOverload->new("object");
60 our $obj = Test::Object::NoOverload->new;
61 our $str_obj = Test::Object::StringOverload->new;
64 unless (is_miniperl()) {
66 tie %refh, 'Tie::RefHash';
70 my @keyandmore = qw(key and more);
71 my @fooormore = qw(foo or more);
72 my %keyandmore = map { $_ => 0 } @keyandmore;
73 my %fooormore = map { $_ => 0 } @fooormore;
75 # Load and run the tests
80 next if /^#/ || !/\S/;
82 my ($yn, $left, $right, $note) = split /\t+/;
84 local $::TODO = $note =~ /TODO/;
86 die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/;
88 my $tstr = "$left ~~ $right";
92 if ($note =~ /NOWARNINGS/) {
93 $res = eval "no warnings; $tstr";
96 skip_if_miniperl("Doesn't work with miniperl", $yn =~ /=/ ? 2 : 1)
97 if $note =~ /MINISKIP/;
104 ok( $@ ne '', "$tstr dies" )
105 and print "# \$\@ was: $@\n";
107 my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches");
110 print "# \$\@ was: $@\n";
112 ok( ($yn =~ /!/ xor $res), $test_name );
116 if ( $yn =~ s/=// ) {
117 $tstr = "$right ~~ $left";
126 sub fatal {die "fatal sub\n"}
128 # to test constant folding
131 sub NOT_DEF() { undef }
134 # - expected to match
135 # ! - expected to not match
136 # @ - expected to be a compilation failure
137 # = - expected to match symmetrically (runs test twice)
138 # Data types to test :
147 # Tied arrays and hashes
148 # Arrays that reference themselves
149 # Regex (// and qr//)
153 # Other syntactic items of interest:
155 # Values returned by a sub call
182 # Any ~~ object overloaded
187 ! ['stringified'] $ov_obj
188 ! { cigam => 1 } $ov_obj
189 ! { stringified => 1 } $ov_obj
210 # regular object with "" overload
217 @ %keyandmore $str_obj
218 @ {"object" => 1} $str_obj
219 @ @fooormore $str_obj
220 @ ["object" => 1] $str_obj
222 @ qr/object/ $str_obj
225 # Those will treat the $str_obj as a string because of fallback:
227 # object (overloaded or not) ~~ Any
229 $ov_obj qr/^stringified$/
230 = "$ov_obj" "stringified"
231 = "$str_obj" "object"
232 != $ov_obj "stringified"
235 ! $ov_obj 'not magic'
238 sub{0} sub { ref $_[0] eq "CODE" }
239 %fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
240 ! %fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
241 \%fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
242 ! \%fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
243 +{%fooormore} sub { $_[0] =~ /^(foo|or|more)$/ }
244 ! +{%fooormore} sub { $_[0] =~ /^(foo|or|less)$/ }
245 @fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
246 ! @fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
247 \@fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
248 ! \@fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
249 [@fooormore] sub { $_[0] =~ /^(foo|or|more)$/ }
250 ! [@fooormore] sub { $_[0] =~ /^(foo|or|less)$/ }
251 %fooormore sub{@_==1}
252 @fooormore sub{@_==1}
253 "foo" sub { $_[0] =~ /^(foo|or|more)$/ }
254 ! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
255 /fooormore/ sub{ref $_[0] eq 'Regexp'}
256 qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
261 NOT_DEF sub{not shift}
262 &NOT_DEF sub{not shift}
269 $obj sub { ref($_[0]) =~ /NoOverload/ }
270 $ov_obj sub { ref($_[0]) =~ /WithOverload/ }
271 # empty stuff matches, because the sub is never called:
285 # sub is not called on empty hashes / arrays
290 # sub is not special on the left
292 sub {0} sub { ref shift eq "CODE" }
301 = \%main:: {map {$_ => 'x'} keys %main::}
305 \%tied_hash \%tied_hash
306 != {"a"=>"b"} \%tied_hash
308 %tied_hash %tied_hash
309 != {"a"=>"b"} %tied_hash
310 $ov_obj %refh MINISKIP
311 ! "$ov_obj" %refh MINISKIP
312 [$ov_obj] %refh MINISKIP
313 ! ["$ov_obj"] %refh MINISKIP
317 # (since this is symmetrical, tests as well hash~~array)
318 = [keys %main::] \%::
319 = [qw[STDIN STDOUT]] \%::
326 = ["foo"] { foo => 1 }
327 = ["foo", "bar"] { foo => 1 }
328 = ["foo", "bar"] \%hash
331 = [qw(foo quux)] \%hash
332 = @fooormore { foo => 1, or => 2, more => 3 }
333 = @fooormore %fooormore
334 = @fooormore \%fooormore
335 = \@fooormore %fooormore
338 = qr/^(fo[ox])$/ {foo => 1}
339 = /^(fo[ox])$/ %fooormore
340 =! qr/[13579]$/ +{0..99}
349 "foo" +{foo => 1, bar => 2}
351 ! "baz" +{foo => 1, bar => 2}
353 ! 1 +{foo => 1, bar => 2}
363 ! undef { hop => 'zouu' }
365 ! undef +{"" => "empty key"}
369 # - another array ref
372 [["foo"], ["bar"]] [qr/o/, qr/a/]
373 ! [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
374 ["foo", "bar"] [qr/o/, qr/a/]
375 ! [qr/o/, qr/a/] ["foo", "bar"]
376 ["foo", "bar"] [["foo"], ["bar"]]
377 ! ["foo", "bar"] [qr/o/, "foo"]
378 ["foo", undef, "bar"] [qr/o/, undef, "bar"]
379 ! ["foo", undef, "bar"] [qr/o/, "", "bar"]
380 ! ["foo", "", "bar"] [qr/o/, undef, "bar"]
392 $obj [sub{ref shift}]
395 = qr/x/ [qw(foo bar baz quux)]
396 =! qr/y/ [qw(foo bar baz quux)]
397 = /x/ [qw(foo bar baz quux)]
398 =! /y/ [qw(foo bar baz quux)]
409 ! "2" [qw(1foo 2bar)]
410 "2bar" [qw(1foo 2bar)]
413 undef [1, 2, undef, 4]
414 ! undef [1, 2, [undef], 4]
423 # - nested arrays and ~~ distributivity
426 "foo" [{foo => "bar"}]
427 ! "bar" [{foo => "bar"}]
429 # Number against number
438 # Number against string
442 != 2_3 "2_3" NOWARNINGS
447 # Regex against string
451 # Regex against number
455 # array/hash against string
456 @fooormore "".\@fooormore
457 ! @keyandmore "".\@fooormore
458 %fooormore "".\%fooormore
459 ! %keyandmore "".\%fooormore
461 # Test the implicit referencing
476 %tied_hash %tied_hash
477 %hash { foo => 5, bar => 10 }
478 ! %hash { foo => 5, bar => 10, quux => 15 }
480 @nums { 1, '', 2, '' }
481 @nums { 1, '', 12, '' }
482 ! @nums { 11, '', 12, '' }
495 @nums[0..1] @nums[0..1]
498 @keyandmore{qw(not)} [undef]
499 @keyandmore{qw(key)} [0]
501 undef @keyandmore{qw(not)}
502 0 @keyandmore{qw(key and more)}
503 ! 2 @keyandmore{qw(key and)}
505 @fooormore{qw(foo)} @keyandmore{qw(key)}
506 @fooormore{qw(foo or more)} @keyandmore{qw(key and more)}