10 no warnings 'uninitialized';
14 use if !$ENV{PERL_CORE_MINITEST}, "Tie::RefHash";
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 if (!$ENV{PERL_CORE_MINITEST}) {
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";
95 elsif ($note =~ /MINISKIP/ && $ENV{PERL_CORE_MINITEST}) {
96 skip("Doesn't work with miniperl", $yn =~ /=/ ? 2 : 1);
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:
230 # object (overloaded or not) ~~ Any
232 $ov_obj qr/^stringified$/
233 = "$ov_obj" "stringified"
234 = "$str_obj" "object"
235 != $ov_obj "stringified"
238 ! $ov_obj 'not magic'
241 sub{0} sub { ref $_[0] eq "CODE" }
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 { $_[0] =~ /^(foo|or|more)$/ }
253 ! [@fooormore] sub { $_[0] =~ /^(foo|or|less)$/ }
254 %fooormore sub{@_==1}
255 @fooormore sub{@_==1}
256 "foo" sub { $_[0] =~ /^(foo|or|more)$/ }
257 ! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
258 /fooormore/ sub{ref $_[0] eq 'Regexp'}
259 qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
264 NOT_DEF sub{not shift}
265 &NOT_DEF sub{not shift}
272 $obj sub { ref($_[0]) =~ /NoOverload/ }
273 $ov_obj sub { ref($_[0]) =~ /WithOverload/ }
274 # empty stuff matches, because the sub is never called:
288 # sub is not called on empty hashes / arrays
293 # sub is not special on the left
295 sub {0} sub { ref shift eq "CODE" }
304 = \%main:: {map {$_ => 'x'} keys %main::}
308 \%tied_hash \%tied_hash
309 != {"a"=>"b"} \%tied_hash
311 %tied_hash %tied_hash
312 != {"a"=>"b"} %tied_hash
313 $ov_obj %refh MINISKIP
314 ! "$ov_obj" %refh MINISKIP
315 [$ov_obj] %refh MINISKIP
316 ! ["$ov_obj"] %refh MINISKIP
320 # (since this is symmetrical, tests as well hash~~array)
321 = [keys %main::] \%::
322 = [qw[STDIN STDOUT]] \%::
329 = ["foo"] { foo => 1 }
330 = ["foo", "bar"] { foo => 1 }
331 = ["foo", "bar"] \%hash
334 = [qw(foo quux)] \%hash
335 = @fooormore { foo => 1, or => 2, more => 3 }
336 = @fooormore %fooormore
337 = @fooormore \%fooormore
338 = \@fooormore %fooormore
341 = qr/^(fo[ox])$/ {foo => 1}
342 = /^(fo[ox])$/ %fooormore
343 =! qr/[13579]$/ +{0..99}
352 "foo" +{foo => 1, bar => 2}
354 ! "baz" +{foo => 1, bar => 2}
356 ! 1 +{foo => 1, bar => 2}
366 ! undef { hop => 'zouu' }
368 ! undef +{"" => "empty key"}
372 # - another array ref
375 [["foo"], ["bar"]] [qr/o/, qr/a/]
376 ! [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
377 ["foo", "bar"] [qr/o/, qr/a/]
378 ! [qr/o/, qr/a/] ["foo", "bar"]
379 ["foo", "bar"] [["foo"], ["bar"]]
380 ! ["foo", "bar"] [qr/o/, "foo"]
381 ["foo", undef, "bar"] [qr/o/, undef, "bar"]
382 ! ["foo", undef, "bar"] [qr/o/, "", "bar"]
383 ! ["foo", "", "bar"] [qr/o/, undef, "bar"]
395 $obj [sub{ref shift}]
398 = qr/x/ [qw(foo bar baz quux)]
399 =! qr/y/ [qw(foo bar baz quux)]
400 = /x/ [qw(foo bar baz quux)]
401 =! /y/ [qw(foo bar baz quux)]
412 ! "2" [qw(1foo 2bar)]
413 "2bar" [qw(1foo 2bar)]
416 undef [1, 2, undef, 4]
417 ! undef [1, 2, [undef], 4]
426 # - nested arrays and ~~ distributivity
429 "foo" [{foo => "bar"}]
430 ! "bar" [{foo => "bar"}]
432 # Number against number
441 # Number against string
445 != 2_3 "2_3" NOWARNINGS
450 # Regex against string
454 # Regex against number
458 # array/hash against string
459 @fooormore "".\@fooormore
460 ! @keyandmore "".\@fooormore
461 %fooormore "".\%fooormore
462 ! %keyandmore "".\%fooormore
464 # Test the implicit referencing
479 %tied_hash %tied_hash
480 %hash { foo => 5, bar => 10 }
481 ! %hash { foo => 5, bar => 10, quux => 15 }
483 @nums { 1, '', 2, '' }
484 @nums { 1, '', 12, '' }
485 ! @nums { 11, '', 12, '' }