10 no warnings 'uninitialized';
11 no warnings 'experimental::smartmatch';
16 # Predeclare vars used in the tests:
19 my @sparse; $sparse[2] = 2;
21 my $deep1 = []; push @$deep1, $deep1;
22 my $deep2 = []; push @$deep2, $deep2;
25 tie my @tied_nums, 'Tie::StdArray';
28 my %hash = (foo => 17, bar => 23);
29 tie my %tied_hash, 'Tie::StdHash';
33 package Test::Object::NoOverload;
34 sub new { bless { key => 1 } }
38 package Test::Object::StringOverload;
39 use overload '""' => sub { "object" }, fallback => 1;
40 sub new { bless { key => 1 } }
44 package Test::Object::WithOverload;
45 sub new { bless { key => ($_[1] // 'magic') } }
46 use overload '~~' => sub {
47 my %hash = %{ $_[0] };
48 if ($_[2]) { # arguments reversed ?
49 return $_[1] eq reverse $hash{key};
52 return $_[1] eq $hash{key};
55 use overload '""' => sub { "stringified" };
56 use overload 'eq' => sub {"$_[0]" eq "$_[1]"};
59 our $ov_obj = Test::Object::WithOverload->new;
60 our $ov_obj_2 = Test::Object::WithOverload->new("object");
61 our $obj = Test::Object::NoOverload->new;
62 our $str_obj = Test::Object::StringOverload->new;
65 unless (is_miniperl()) {
67 tie %refh, 'Tie::RefHash';
71 my @keyandmore = qw(key and more);
72 my @fooormore = qw(foo or more);
73 my %keyandmore = map { $_ => 0 } @keyandmore;
74 my %fooormore = map { $_ => 0 } @fooormore;
76 # Load and run the tests
81 next if /^#/ || !/\S/;
83 my ($yn, $left, $right, $note) = split /\t+/;
85 local $::TODO = $note =~ /TODO/;
87 die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/;
89 my $tstr = "$left ~~ $right";
93 if ($note =~ /NOWARNINGS/) {
94 $res = eval "no warnings; $tstr";
97 skip_if_miniperl("Doesn't work with miniperl", $yn =~ /=/ ? 2 : 1)
98 if $note =~ /MINISKIP/;
105 ok( $@ ne '', "$tstr dies" )
106 and print "# \$\@ was: $@\n";
108 my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches");
111 print "# \$\@ was: $@\n";
113 ok( ($yn =~ /!/ xor $res), $test_name );
117 if ( $yn =~ s/=// ) {
118 $tstr = "$right ~~ $left";
127 sub fatal {die "fatal sub\n"}
129 # to test constant folding
132 sub NOT_DEF() { undef }
135 # - expected to match
136 # ! - expected to not match
137 # @ - expected to be a compilation failure
138 # = - expected to match symmetrically (runs test twice)
139 # Data types to test :
148 # Tied arrays and hashes
149 # Arrays that reference themselves
150 # Regex (// and qr//)
154 # Other syntactic items of interest:
156 # Values returned by a sub call
183 # Any ~~ object overloaded
188 ! ['stringified'] $ov_obj
189 ! { cigam => 1 } $ov_obj
190 ! { stringified => 1 } $ov_obj
211 # regular object with "" overload
218 @ %keyandmore $str_obj
219 @ {"object" => 1} $str_obj
220 @ @fooormore $str_obj
221 @ ["object" => 1] $str_obj
223 @ qr/object/ $str_obj
226 # Those will treat the $str_obj as a string because of fallback:
228 # object (overloaded or not) ~~ Any
230 $ov_obj qr/^stringified$/
231 = "$ov_obj" "stringified"
232 = "$str_obj" "object"
233 != $ov_obj "stringified"
236 ! $ov_obj 'not magic'
239 sub{0} sub { ref $_[0] eq "CODE" }
240 %fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
241 ! %fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
242 \%fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
243 ! \%fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
244 +{%fooormore} sub { $_[0] =~ /^(foo|or|more)$/ }
245 ! +{%fooormore} sub { $_[0] =~ /^(foo|or|less)$/ }
246 @fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
247 ! @fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
248 \@fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
249 ! \@fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
250 [@fooormore] sub { $_[0] =~ /^(foo|or|more)$/ }
251 ! [@fooormore] sub { $_[0] =~ /^(foo|or|less)$/ }
252 %fooormore sub{@_==1}
253 @fooormore sub{@_==1}
254 "foo" sub { $_[0] =~ /^(foo|or|more)$/ }
255 ! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
256 /fooormore/ sub{ref $_[0] eq 'Regexp'}
257 qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
262 NOT_DEF sub{not shift}
263 &NOT_DEF sub{not shift}
270 $obj sub { ref($_[0]) =~ /NoOverload/ }
271 $ov_obj sub { ref($_[0]) =~ /WithOverload/ }
272 # empty stuff matches, because the sub is never called:
286 # sub is not called on empty hashes / arrays
291 # sub is not special on the left
293 sub {0} sub { ref shift eq "CODE" }
302 = \%main:: {map {$_ => 'x'} keys %main::}
306 \%tied_hash \%tied_hash
307 != {"a"=>"b"} \%tied_hash
309 %tied_hash %tied_hash
310 != {"a"=>"b"} %tied_hash
311 $ov_obj %refh MINISKIP
312 ! "$ov_obj" %refh MINISKIP
313 [$ov_obj] %refh MINISKIP
314 ! ["$ov_obj"] %refh MINISKIP
318 # (since this is symmetrical, tests as well hash~~array)
319 = [keys %main::] \%::
320 = [qw[STDIN STDOUT]] \%::
327 = ["foo"] { foo => 1 }
328 = ["foo", "bar"] { foo => 1 }
329 = ["foo", "bar"] \%hash
332 = [qw(foo quux)] \%hash
333 = @fooormore { foo => 1, or => 2, more => 3 }
334 = @fooormore %fooormore
335 = @fooormore \%fooormore
336 = \@fooormore %fooormore
339 = qr/^(fo[ox])$/ {foo => 1}
340 = /^(fo[ox])$/ %fooormore
341 =! qr/[13579]$/ +{0..99}
350 "foo" +{foo => 1, bar => 2}
352 ! "baz" +{foo => 1, bar => 2}
354 ! 1 +{foo => 1, bar => 2}
364 ! undef { hop => 'zouu' }
366 ! undef +{"" => "empty key"}
370 # - another array ref
373 [["foo"], ["bar"]] [qr/o/, qr/a/]
374 ! [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
375 ["foo", "bar"] [qr/o/, qr/a/]
376 ! [qr/o/, qr/a/] ["foo", "bar"]
377 ["foo", "bar"] [["foo"], ["bar"]]
378 ! ["foo", "bar"] [qr/o/, "foo"]
379 ["foo", undef, "bar"] [qr/o/, undef, "bar"]
380 ! ["foo", undef, "bar"] [qr/o/, "", "bar"]
381 ! ["foo", "", "bar"] [qr/o/, undef, "bar"]
393 $obj [sub{ref shift}]
396 = qr/x/ [qw(foo bar baz quux)]
397 =! qr/y/ [qw(foo bar baz quux)]
398 = /x/ [qw(foo bar baz quux)]
399 =! /y/ [qw(foo bar baz quux)]
410 ! "2" [qw(1foo 2bar)]
411 "2bar" [qw(1foo 2bar)]
414 undef [1, 2, undef, 4]
415 ! undef [1, 2, [undef], 4]
424 # - nested arrays and ~~ distributivity
427 "foo" [{foo => "bar"}]
428 ! "bar" [{foo => "bar"}]
430 # Number against number
439 # Number against string
443 != 2_3 "2_3" NOWARNINGS
448 # Regex against string
452 # Regex against number
456 # array/hash against string
457 @fooormore "".\@fooormore
458 ! @keyandmore "".\@fooormore
459 %fooormore "".\%fooormore
460 ! %keyandmore "".\%fooormore
462 # Test the implicit referencing
477 %tied_hash %tied_hash
478 %hash { foo => 5, bar => 10 }
479 ! %hash { foo => 5, bar => 10, quux => 15 }
481 @nums { 1, '', 2, '' }
482 @nums { 1, '', 12, '' }
483 ! @nums { 11, '', 12, '' }
496 @nums[0..1] @nums[0..1]
499 @keyandmore{qw(not)} [undef]
500 @keyandmore{qw(key)} [0]
502 undef @keyandmore{qw(not)}
503 0 @keyandmore{qw(key and more)}
504 ! 2 @keyandmore{qw(key and)}
506 @fooormore{qw(foo)} @keyandmore{qw(key)}
507 @fooormore{qw(foo or more)} @keyandmore{qw(key and more)}