13 # Predeclare vars used in the tests:
14 my $deep1 = []; push @$deep1, \$deep1;
15 my $deep2 = []; push @$deep2, \$deep2;
18 tie my @tied_nums, 'Tie::StdArray';
21 my %hash = (foo => 17, bar => 23);
22 tie my %tied_hash, 'Tie::StdHash';
26 package Test::Object::NoOverload;
27 sub new { bless { key => 1 } }
31 package Test::Object::CopyOverload;
32 sub new { bless { key => 'magic' } }
33 use overload '~~' => sub { my %hash = %{ $_[0] }; $_[1] eq $hash{key} };
36 our $ov_obj = Test::Object::CopyOverload->new;
37 our $obj = Test::Object::NoOverload->new;
39 my @keyandmore = qw(key and more);
40 my @fooormore = qw(foo or more);
41 my %keyandmore = map { $_ => 0 } @keyandmore;
42 my %fooormore = map { $_ => 0 } @fooormore;
44 # Load and run the tests
48 next if /^#/ || !/\S/;
50 my ($yn, $left, $right, $note) = split /\t+/;
52 local $::TODO = $note =~ /TODO/;
54 die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/;
56 my $tstr = "$left ~~ $right";
64 ok( $@ ne '', "$tstr dies" )
65 and print "# \$\@ was: $@\n";
67 my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches");
70 print "# \$\@ was: $@\n";
72 ok( ($yn =~ /!/ xor $res), $test_name );
77 $tstr = "$right ~~ $left";
85 sub fatal {die "fatal sub\n"}
87 # to test constant folding
90 sub NOT_DEF() { undef }
94 # ! - expected to not match
95 # @ - expected to be a compilation failure
96 # = - expected to match symmetrically (runs test twice)
97 # Data types to test :
106 # Tied arrays and hashes
107 # Arrays that reference themselves
108 # Regex (// and qr//)
112 # Other syntactic items of interest:
114 # Values returned by a sub call
141 # Any ~~ object overloaded
144 ! 'not magic' $ov_obj
165 # object (overloaded or not) ~~ Any
169 sub{0} sub { ref $_[0] eq "CODE" }
170 %fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
171 ! %fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
172 \%fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
173 ! \%fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
174 +{%fooormore} sub { $_[0] =~ /^(foo|or|more)$/ }
175 ! +{%fooormore} sub { $_[0] =~ /^(foo|or|less)$/ }
176 @fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
177 ! @fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
178 \@fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
179 ! \@fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
180 [@fooormore] sub { $_[0] =~ /^(foo|or|more)$/ }
181 ! [@fooormore] sub { $_[0] =~ /^(foo|or|less)$/ }
182 %fooormore sub{@_==1}
183 @fooormore sub{@_==1}
184 "foo" sub { $_[0] =~ /^(foo|or|more)$/ }
185 ! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
186 /fooormore/ sub{ref $_[0] eq 'Regexp'}
187 qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
198 # empty stuff matches, because the sub is never called:
210 # sub is not called on empty hashes / arrays
221 \%main:: {map {$_ => 'x'} keys %main::}
225 \%tied_hash \%tied_hash
235 ["foo", "bar"] { foo => 1 }
236 ["foo", "bar"] \%hash
239 [qw(foo quux)] \%hash
242 qr/^(fo[ox])$/ {foo => 1}
243 ! qr/[13579]$/ +{0..99}
248 "foo" +{foo => 1, bar => 2}
249 ! "baz" +{foo => 1, bar => 2}
252 ! undef { hop => 'zouu' }
254 ! undef +{"" => "empty key"}
258 # - another array ref
261 ! [["foo"], ["bar"]] [qr/o/, qr/a/]
262 [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
263 ["foo", "bar"] [qr/o/, qr/a/]
264 ["foo", "bar"] [["foo"], ["bar"]]
265 ! ["foo", "bar"] [qr/o/, "foo"]
266 ["foo", undef, "bar"] [qr/o/, undef, "bar"]
267 ["foo", undef, "bar"] [qr/o/, "", "bar"]
268 ! ["foo", "", "bar"] [qr/o/, undef, "bar"]
275 qr/x/ [qw(foo bar baz quux)]
276 ! qr/y/ [qw(foo bar baz quux)]
277 /x/ [qw(foo bar baz quux)]
278 ! /y/ [qw(foo bar baz quux)]
287 ! "2" [qw(1foo 2bar)]
288 "2bar" [qw(1foo 2bar)]
290 # Number against number
298 # Number against string
305 # Regex against string
309 # Regex against number
313 # array against string
314 @fooormore "".\@fooormore
315 ! @keyandmore "".\@fooormore
316 %fooormore "".\%fooormore
317 ! %keyandmore "".\%fooormore
319 # Test the implicit referencing
334 %tied_hash %tied_hash
335 %hash { foo => 5, bar => 10 }
336 ! %hash { foo => 5, bar => 10, quux => 15 }
338 @nums { 1, '', 2, '' }
339 @nums { 1, '', 12, '' }
340 ! @nums { 11, '', 12, '' }