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 {
41 my %hash = %{ $_[0] };
42 if ($_[2]) { # arguments reversed ?
43 return $_[1] eq reverse $hash{key};
46 return $_[1] eq $hash{key};
49 use overload '""' => sub { "stringified" };
50 use overload 'eq' => sub {"$_[0]" eq "$_[1]"};
53 our $ov_obj = Test::Object::WithOverload->new;
54 our $obj = Test::Object::NoOverload->new;
56 tie my %refh, 'Tie::RefHash';
59 my @keyandmore = qw(key and more);
60 my @fooormore = qw(foo or more);
61 my %keyandmore = map { $_ => 0 } @keyandmore;
62 my %fooormore = map { $_ => 0 } @fooormore;
64 # Load and run the tests
68 next if /^#/ || !/\S/;
70 my ($yn, $left, $right, $note) = split /\t+/;
72 local $::TODO = $note =~ /TODO/;
74 die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/;
76 my $tstr = "$left ~~ $right";
80 if ($note =~ /NOWARNINGS/) {
81 $res = eval "no warnings; $tstr";
90 ok( $@ ne '', "$tstr dies" )
91 and print "# \$\@ was: $@\n";
93 my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches");
96 print "# \$\@ was: $@\n";
98 ok( ($yn =~ /!/ xor $res), $test_name );
102 if ( $yn =~ s/=// ) {
103 $tstr = "$right ~~ $left";
111 sub fatal {die "fatal sub\n"}
113 # to test constant folding
116 sub NOT_DEF() { undef }
119 # - expected to match
120 # ! - expected to not match
121 # @ - expected to be a compilation failure
122 # = - expected to match symmetrically (runs test twice)
123 # Data types to test :
132 # Tied arrays and hashes
133 # Arrays that reference themselves
134 # Regex (// and qr//)
138 # Other syntactic items of interest:
140 # Values returned by a sub call
167 # Any ~~ object overloaded
172 ! ['stringified'] $ov_obj
173 ! { cigam => 1 } $ov_obj
174 ! { stringified => 1 } $ov_obj
195 # object (overloaded or not) ~~ Any
197 $ov_obj qr/^stringified$/
198 = "$ov_obj" "stringified"
199 != $ov_obj "stringified"
201 ! $ov_obj 'not magic'
204 sub{0} sub { ref $_[0] eq "CODE" }
205 %fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
206 ! %fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
207 \%fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
208 ! \%fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
209 +{%fooormore} sub { $_[0] =~ /^(foo|or|more)$/ }
210 ! +{%fooormore} sub { $_[0] =~ /^(foo|or|less)$/ }
211 @fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
212 ! @fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
213 \@fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
214 ! \@fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
215 [@fooormore] sub { $_[0] =~ /^(foo|or|more)$/ }
216 ! [@fooormore] sub { $_[0] =~ /^(foo|or|less)$/ }
217 %fooormore sub{@_==1}
218 @fooormore sub{@_==1}
219 "foo" sub { $_[0] =~ /^(foo|or|more)$/ }
220 ! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
221 /fooormore/ sub{ref $_[0] eq 'Regexp'}
222 qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
227 NOT_DEF sub{not shift}
228 &NOT_DEF sub{not shift}
235 $obj sub { ref($_[0]) =~ /NoOverload/ }
236 $ov_obj sub { ref($_[0]) =~ /WithOverload/ }
237 # empty stuff matches, because the sub is never called:
251 # sub is not called on empty hashes / arrays
256 # sub is not special on the left
258 sub {0} sub { ref shift eq "CODE" }
267 = \%main:: {map {$_ => 'x'} keys %main::}
271 \%tied_hash \%tied_hash
272 != {"a"=>"b"} \%tied_hash
274 %tied_hash %tied_hash
275 != {"a"=>"b"} %tied_hash
283 # (since this is symmetrical, tests as well hash~~array)
284 = [keys %main::] \%::
285 = [qw[STDIN STDOUT]] \%::
292 = ["foo"] { foo => 1 }
293 = ["foo", "bar"] { foo => 1 }
294 = ["foo", "bar"] \%hash
297 = [qw(foo quux)] \%hash
298 = @fooormore { foo => 1, or => 2, more => 3 }
299 = @fooormore %fooormore
300 = @fooormore \%fooormore
301 = \@fooormore %fooormore
304 = qr/^(fo[ox])$/ {foo => 1}
305 = /^(fo[ox])$/ %fooormore
306 =! qr/[13579]$/ +{0..99}
315 "foo" +{foo => 1, bar => 2}
317 ! "baz" +{foo => 1, bar => 2}
319 ! 1 +{foo => 1, bar => 2}
329 ! undef { hop => 'zouu' }
331 ! undef +{"" => "empty key"}
335 # - another array ref
338 [["foo"], ["bar"]] [qr/o/, qr/a/]
339 ! [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
340 ["foo", "bar"] [qr/o/, qr/a/]
341 ! [qr/o/, qr/a/] ["foo", "bar"]
342 ["foo", "bar"] [["foo"], ["bar"]]
343 ! ["foo", "bar"] [qr/o/, "foo"]
344 ["foo", undef, "bar"] [qr/o/, undef, "bar"]
345 ["foo", undef, "bar"] [qr/o/, "", "bar"]
346 ! ["foo", "", "bar"] [qr/o/, undef, "bar"]
358 $obj [sub{ref shift}]
361 = qr/x/ [qw(foo bar baz quux)]
362 =! qr/y/ [qw(foo bar baz quux)]
363 = /x/ [qw(foo bar baz quux)]
364 =! /y/ [qw(foo bar baz quux)]
375 ! "2" [qw(1foo 2bar)]
376 "2bar" [qw(1foo 2bar)]
379 undef [1, 2, undef, 4]
380 ! undef [1, 2, [undef], 4]
384 # - nested arrays and ~~ distributivity
387 "foo" [{foo => "bar"}]
388 ! "bar" [{foo => "bar"}]
390 # Number against number
398 # Number against string
402 != 2_3 "2_3" NOWARNINGS
405 # Regex against string
409 # Regex against number
413 # array/hash against string
414 @fooormore "".\@fooormore
415 ! @keyandmore "".\@fooormore
416 %fooormore "".\%fooormore
417 ! %keyandmore "".\%fooormore
419 # Test the implicit referencing
434 %tied_hash %tied_hash
435 %hash { foo => 5, bar => 10 }
436 ! %hash { foo => 5, bar => 10, quux => 15 }
438 @nums { 1, '', 2, '' }
439 @nums { 1, '', 12, '' }
440 ! @nums { 11, '', 12, '' }