10 no warnings 'uninitialized';
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::WithOverload;
39 sub new { bless { key => 'magic' } }
40 use overload '~~' => sub { my %hash = %{ $_[0] }; $_[1] eq $hash{key} };
41 use overload '""' => sub { "stringified" };
42 use overload 'eq' => sub {"$_[0]" eq "$_[1]"};
45 our $ov_obj = Test::Object::WithOverload->new;
46 our $obj = Test::Object::NoOverload->new;
48 tie my %refh, 'Tie::RefHash';
51 my @keyandmore = qw(key and more);
52 my @fooormore = qw(foo or more);
53 my %keyandmore = map { $_ => 0 } @keyandmore;
54 my %fooormore = map { $_ => 0 } @fooormore;
56 # Load and run the tests
60 next if /^#/ || !/\S/;
62 my ($yn, $left, $right, $note) = split /\t+/;
64 local $::TODO = $note =~ /TODO/;
66 die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/;
68 my $tstr = "$left ~~ $right";
72 if ($note =~ /NOWARNINGS/) {
73 $res = eval "no warnings; $tstr";
82 ok( $@ ne '', "$tstr dies" )
83 and print "# \$\@ was: $@\n";
85 my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches");
88 print "# \$\@ was: $@\n";
90 ok( ($yn =~ /!/ xor $res), $test_name );
95 $tstr = "$right ~~ $left";
103 sub fatal {die "fatal sub\n"}
105 # to test constant folding
108 sub NOT_DEF() { undef }
111 # - expected to match
112 # ! - expected to not match
113 # @ - expected to be a compilation failure
114 # = - expected to match symmetrically (runs test twice)
115 # Data types to test :
124 # Tied arrays and hashes
125 # Arrays that reference themselves
126 # Regex (// and qr//)
130 # Other syntactic items of interest:
132 # Values returned by a sub call
159 # Any ~~ object overloaded
162 ! 'not magic' $ov_obj
183 # object (overloaded or not) ~~ Any
185 $ov_obj qr/^stringified$/
186 $ov_obj "stringified"
189 sub{0} sub { ref $_[0] eq "CODE" }
190 %fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
191 ! %fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
192 \%fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
193 ! \%fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
194 +{%fooormore} sub { $_[0] =~ /^(foo|or|more)$/ }
195 ! +{%fooormore} sub { $_[0] =~ /^(foo|or|less)$/ }
196 @fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
197 ! @fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
198 \@fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
199 ! \@fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
200 [@fooormore] sub { $_[0] =~ /^(foo|or|more)$/ }
201 ! [@fooormore] sub { $_[0] =~ /^(foo|or|less)$/ }
202 %fooormore sub{@_==1}
203 @fooormore sub{@_==1}
204 "foo" sub { $_[0] =~ /^(foo|or|more)$/ }
205 ! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
206 /fooormore/ sub{ref $_[0] eq 'Regexp'}
207 qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
212 NOT_DEF sub{not shift}
213 &NOT_DEF sub{not shift}
220 $obj sub { ref($_[0]) =~ /NoOverload/ }
221 $ov_obj sub { ref($_[0]) =~ /WithOverload/ }
222 # empty stuff matches, because the sub is never called:
236 # sub is not called on empty hashes / arrays
249 = \%main:: {map {$_ => 'x'} keys %main::}
253 \%tied_hash \%tied_hash
254 != {"a"=>"b"} \%tied_hash
256 %tied_hash %tied_hash
257 != {"a"=>"b"} %tied_hash
265 # (since this is symmetrical, tests as well hash~~array)
266 = [keys %main::] \%::
267 = [qw[STDIN STDOUT]] \%::
274 = ["foo"] { foo => 1 }
275 = ["foo", "bar"] { foo => 1 }
276 = ["foo", "bar"] \%hash
279 = [qw(foo quux)] \%hash
280 = @fooormore { foo => 1, or => 2, more => 3 }
281 = @fooormore %fooormore
282 = @fooormore \%fooormore
283 = \@fooormore %fooormore
286 = qr/^(fo[ox])$/ {foo => 1}
287 = /^(fo[ox])$/ %fooormore
288 =! qr/[13579]$/ +{0..99}
297 "foo" +{foo => 1, bar => 2}
299 ! "baz" +{foo => 1, bar => 2}
301 ! 1 +{foo => 1, bar => 2}
311 ! undef { hop => 'zouu' }
313 ! undef +{"" => "empty key"}
317 # - another array ref
320 [["foo"], ["bar"]] [qr/o/, qr/a/]
321 ! [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
322 ["foo", "bar"] [qr/o/, qr/a/]
323 ! [qr/o/, qr/a/] ["foo", "bar"]
324 ["foo", "bar"] [["foo"], ["bar"]]
325 ! ["foo", "bar"] [qr/o/, "foo"]
326 ["foo", undef, "bar"] [qr/o/, undef, "bar"]
327 ["foo", undef, "bar"] [qr/o/, "", "bar"]
328 ! ["foo", "", "bar"] [qr/o/, undef, "bar"]
340 $obj [sub{ref shift}]
343 = qr/x/ [qw(foo bar baz quux)]
344 =! qr/y/ [qw(foo bar baz quux)]
345 = /x/ [qw(foo bar baz quux)]
346 =! /y/ [qw(foo bar baz quux)]
357 ! "2" [qw(1foo 2bar)]
358 "2bar" [qw(1foo 2bar)]
361 undef [1, 2, undef, 4]
362 ! undef [1, 2, [undef], 4]
366 # - nested arrays and ~~ distributivity
369 "foo" [{foo => "bar"}]
370 ! "bar" [{foo => "bar"}]
372 # Number against number
380 # Number against string
384 != 2_3 "2_3" NOWARNINGS
387 # Regex against string
391 # Regex against number
395 # array/hash against string
396 @fooormore "".\@fooormore
397 ! @keyandmore "".\@fooormore
398 %fooormore "".\%fooormore
399 ! %keyandmore "".\%fooormore
401 # Test the implicit referencing
416 %tied_hash %tied_hash
417 %hash { foo => 5, bar => 10 }
418 ! %hash { foo => 5, bar => 10, quux => 15 }
420 @nums { 1, '', 2, '' }
421 @nums { 1, '', 12, '' }
422 ! @nums { 11, '', 12, '' }