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::WithOverload;
38 sub new { bless { key => 'magic' } }
39 use overload '~~' => sub { my %hash = %{ $_[0] }; $_[1] eq $hash{key} };
40 use overload '""' => sub { "stringified" };
41 use overload 'eq' => sub {"$_[0]" eq "$_[1]"};
44 our $ov_obj = Test::Object::WithOverload->new;
45 our $obj = Test::Object::NoOverload->new;
47 my @keyandmore = qw(key and more);
48 my @fooormore = qw(foo or more);
49 my %keyandmore = map { $_ => 0 } @keyandmore;
50 my %fooormore = map { $_ => 0 } @fooormore;
52 # Load and run the tests
56 next if /^#/ || !/\S/;
58 my ($yn, $left, $right, $note) = split /\t+/;
60 local $::TODO = $note =~ /TODO/;
62 die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/;
64 my $tstr = "$left ~~ $right";
68 if ($note =~ /NOWARNINGS/) {
69 $res = eval "no warnings; $tstr";
78 ok( $@ ne '', "$tstr dies" )
79 and print "# \$\@ was: $@\n";
81 my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches");
84 print "# \$\@ was: $@\n";
86 ok( ($yn =~ /!/ xor $res), $test_name );
91 $tstr = "$right ~~ $left";
99 sub fatal {die "fatal sub\n"}
101 # to test constant folding
104 sub NOT_DEF() { undef }
107 # - expected to match
108 # ! - expected to not match
109 # @ - expected to be a compilation failure
110 # = - expected to match symmetrically (runs test twice)
111 # Data types to test :
120 # Tied arrays and hashes
121 # Arrays that reference themselves
122 # Regex (// and qr//)
126 # Other syntactic items of interest:
128 # Values returned by a sub call
155 # Any ~~ object overloaded
158 ! 'not magic' $ov_obj
179 # object (overloaded or not) ~~ Any
181 $ov_obj qr/^stringified$/
182 $ov_obj "stringified"
185 sub{0} sub { ref $_[0] eq "CODE" }
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 { $_[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{@_==1}
199 @fooormore sub{@_==1}
200 "foo" sub { $_[0] =~ /^(foo|or|more)$/ }
201 ! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
202 /fooormore/ sub{ref $_[0] eq 'Regexp'}
203 qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
208 NOT_DEF sub{not shift}
209 &NOT_DEF sub{not shift}
216 $obj sub { ref($_[0]) =~ /NoOverload/ }
217 $ov_obj sub { ref($_[0]) =~ /WithOverload/ }
218 # empty stuff matches, because the sub is never called:
232 # sub is not called on empty hashes / arrays
245 = \%main:: {map {$_ => 'x'} keys %main::}
249 \%tied_hash \%tied_hash
250 != {"a"=>"b"} \%tied_hash
252 %tied_hash %tied_hash
253 != {"a"=>"b"} %tied_hash
256 # (since this is symmetrical, tests as well hash~~array)
257 = [keys %main::] \%::
258 = [qw[STDIN STDOUT]] \%::
265 = ["foo"] { foo => 1 }
266 = ["foo", "bar"] { foo => 1 }
267 = ["foo", "bar"] \%hash
270 = [qw(foo quux)] \%hash
271 = @fooormore { foo => 1, or => 2, more => 3 }
272 = @fooormore %fooormore
273 = @fooormore \%fooormore
274 = \@fooormore %fooormore
277 = qr/^(fo[ox])$/ {foo => 1}
278 = /^(fo[ox])$/ %fooormore
279 =! qr/[13579]$/ +{0..99}
288 "foo" +{foo => 1, bar => 2}
290 ! "baz" +{foo => 1, bar => 2}
292 ! 1 +{foo => 1, bar => 2}
302 ! undef { hop => 'zouu' }
304 ! undef +{"" => "empty key"}
308 # - another array ref
311 [["foo"], ["bar"]] [qr/o/, qr/a/]
312 ! [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
313 ["foo", "bar"] [qr/o/, qr/a/]
314 ! [qr/o/, qr/a/] ["foo", "bar"]
315 ["foo", "bar"] [["foo"], ["bar"]]
316 ! ["foo", "bar"] [qr/o/, "foo"]
317 ["foo", undef, "bar"] [qr/o/, undef, "bar"]
318 ["foo", undef, "bar"] [qr/o/, "", "bar"]
319 ! ["foo", "", "bar"] [qr/o/, undef, "bar"]
331 $obj [sub{ref shift}]
334 = qr/x/ [qw(foo bar baz quux)]
335 =! qr/y/ [qw(foo bar baz quux)]
336 = /x/ [qw(foo bar baz quux)]
337 =! /y/ [qw(foo bar baz quux)]
348 ! "2" [qw(1foo 2bar)]
349 "2bar" [qw(1foo 2bar)]
352 undef [1, 2, undef, 4]
353 ! undef [1, 2, [undef], 4]
357 # - nested arrays and ~~ distributivity
360 "foo" [{foo => "bar"}]
361 ! "bar" [{foo => "bar"}]
363 # Number against number
371 # Number against string
375 != 2_3 "2_3" NOWARNINGS
378 # Regex against string
382 # Regex against number
386 # array/hash against string
387 @fooormore "".\@fooormore
388 ! @keyandmore "".\@fooormore
389 %fooormore "".\%fooormore
390 ! %keyandmore "".\%fooormore
392 # Test the implicit referencing
407 %tied_hash %tied_hash
408 %hash { foo => 5, bar => 10 }
409 ! %hash { foo => 5, bar => 10, quux => 15 }
411 @nums { 1, '', 2, '' }
412 @nums { 1, '', 12, '' }
413 ! @nums { 11, '', 12, '' }