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
142 # object overloaded ~~ Any
145 =! $ov_obj 'not magic'
165 sub{0} sub { ref $_[0] eq "CODE" }
166 %fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
167 ! %fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
168 \%fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
169 ! \%fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
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{@_==1}
179 @fooormore sub{@_==1}
180 "foo" sub { $_[0] =~ /^(foo|or|more)$/ }
181 ! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
182 /fooormore/ sub{ref $_[0] eq 'Regexp'}
183 qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
194 # empty stuff matches, because the sub is never called:
206 # sub is not called on empty hashes / arrays
217 \%main:: {map {$_ => 'x'} keys %main::}
221 \%tied_hash \%tied_hash
231 ["foo", "bar"] { foo => 1 }
232 ["foo", "bar"] \%hash
235 [qw(foo quux)] \%hash
238 qr/^(fo[ox])$/ {foo => 1}
239 ! qr/[13579]$/ +{0..99}
244 "foo" +{foo => 1, bar => 2}
245 ! "baz" +{foo => 1, bar => 2}
248 ! undef { hop => 'zouu' }
250 ! undef +{"" => "empty key"}
254 # - another array ref
257 ! [["foo"], ["bar"]] [qr/o/, qr/a/]
258 [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
259 ["foo", "bar"] [qr/o/, qr/a/]
260 ["foo", "bar"] [["foo"], ["bar"]]
261 ! ["foo", "bar"] [qr/o/, "foo"]
262 ["foo", undef, "bar"] [qr/o/, undef, "bar"]
263 ["foo", undef, "bar"] [qr/o/, "", "bar"]
264 ! ["foo", "", "bar"] [qr/o/, undef, "bar"]
271 qr/x/ [qw(foo bar baz quux)]
272 ! qr/y/ [qw(foo bar baz quux)]
273 /x/ [qw(foo bar baz quux)]
274 ! /y/ [qw(foo bar baz quux)]
283 ! "2" [qw(1foo 2bar)]
284 "2bar" [qw(1foo 2bar)]
286 # Number against number
294 # Number against string
301 # Regex against string
305 # Regex against number
309 # Test the implicit referencing
324 %tied_hash %tied_hash
325 %hash { foo => 5, bar => 10 }
326 ! %hash { foo => 5, bar => 10, quux => 15 }
328 @nums { 1, '', 2, '' }
329 @nums { 1, '', 12, '' }
330 ! @nums { 11, '', 12, '' }