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 => 1 } }
33 use overload '~~' => sub { my %hash = %{ $_[0] }; $_[1] ~~ %hash };
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
93 # ! - expected to not match
94 # @ - expected to be a compilation failure
95 # = - expected to match symmetrically (runs test twice)
96 # Data types to test :
105 # Tied arrays and hashes
106 # Arrays that reference themselves
107 # Regex (// and qr//)
111 # Other syntactic items of interest:
113 # Values returned by a sub call
138 # Any ~~ object overloaded
139 # object overloaded ~~ Any
142 = $ov_obj {"key" => 2}
143 =! $ov_obj {"key" => 1, bar => 2}
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}
249 ! undef +{"" => "empty key"}
253 # - another array ref
256 ! [["foo"], ["bar"]] [qr/o/, qr/a/]
257 [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
258 ["foo", "bar"] [qr/o/, qr/a/]
259 ! ["foo", "bar"] [qr/o/, "foo"]
266 qr/x/ [qw(foo bar baz quux)]
267 ! qr/y/ [qw(foo bar baz quux)]
268 /x/ [qw(foo bar baz quux)]
269 ! /y/ [qw(foo bar baz quux)]
278 ! "2" [qw(1foo 2bar)]
279 "2bar" [qw(1foo 2bar)]
281 # Number against number
289 # Number against string
296 # Regex against string
300 # Regex against number
303 # Test the implicit referencing
318 %tied_hash %tied_hash
319 %hash { foo => 5, bar => 10 }
320 ! %hash { foo => 5, bar => 10, quux => 15 }
322 @nums { 1, '', 2, '' }
323 @nums { 1, '', 12, '' }
324 ! @nums { 11, '', 12, '' }