13 # Predeclare vars used in the tests:
17 my $deep1 = []; push @$deep1, \$deep1;
18 my $deep2 = []; push @$deep2, \$deep2;
21 tie my @tied_nums, 'Tie::StdArray';
24 my %hash = (foo => 17, bar => 23);
25 tie my %tied_hash, 'Tie::StdHash';
29 package Test::Object::NoOverload;
30 sub new { bless { key => 1 } }
34 package Test::Object::CopyOverload;
35 sub new { bless { key => 'magic' } }
36 use overload '~~' => sub { my %hash = %{ $_[0] }; $_[1] eq $hash{key} };
39 our $ov_obj = Test::Object::CopyOverload->new;
40 our $obj = Test::Object::NoOverload->new;
42 my @keyandmore = qw(key and more);
43 my @fooormore = qw(foo or more);
44 my %keyandmore = map { $_ => 0 } @keyandmore;
45 my %fooormore = map { $_ => 0 } @fooormore;
47 # Load and run the tests
51 next if /^#/ || !/\S/;
53 my ($yn, $left, $right, $note) = split /\t+/;
55 local $::TODO = $note =~ /TODO/;
57 die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/;
59 my $tstr = "$left ~~ $right";
67 ok( $@ ne '', "$tstr dies" )
68 and print "# \$\@ was: $@\n";
70 my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches");
73 print "# \$\@ was: $@\n";
75 ok( ($yn =~ /!/ xor $res), $test_name );
80 $tstr = "$right ~~ $left";
88 sub fatal {die "fatal sub\n"}
90 # to test constant folding
93 sub NOT_DEF() { undef }
97 # ! - expected to not match
98 # @ - expected to be a compilation failure
99 # = - expected to match symmetrically (runs test twice)
100 # Data types to test :
109 # Tied arrays and hashes
110 # Arrays that reference themselves
111 # Regex (// and qr//)
115 # Other syntactic items of interest:
117 # Values returned by a sub call
144 # Any ~~ object overloaded
147 ! 'not magic' $ov_obj
168 # object (overloaded or not) ~~ Any
172 sub{0} sub { ref $_[0] eq "CODE" }
173 %fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
174 ! %fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
175 \%fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
176 ! \%fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
177 +{%fooormore} sub { $_[0] =~ /^(foo|or|more)$/ }
178 ! +{%fooormore} sub { $_[0] =~ /^(foo|or|less)$/ }
179 @fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
180 ! @fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
181 \@fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
182 ! \@fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
183 [@fooormore] sub { $_[0] =~ /^(foo|or|more)$/ }
184 ! [@fooormore] sub { $_[0] =~ /^(foo|or|less)$/ }
185 %fooormore sub{@_==1}
186 @fooormore sub{@_==1}
187 "foo" sub { $_[0] =~ /^(foo|or|more)$/ }
188 ! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
189 /fooormore/ sub{ref $_[0] eq 'Regexp'}
190 qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
195 NOT_DEF sub{not shift}
196 &NOT_DEF sub{not shift}
203 $obj sub { ref $_[0] =~ /NoOverload/ } TODO
204 $ov_obj sub { ref $_[0] =~ /CopyOverload/ } TODO
205 # empty stuff matches, because the sub is never called:
219 # sub is not called on empty hashes / arrays
232 = \%main:: {map {$_ => 'x'} keys %main::}
236 \%tied_hash \%tied_hash
237 != {"a"=>"b"} \%tied_hash
239 %tied_hash %tied_hash
240 != {"a"=>"b"} %tied_hash
243 # (since this is symmetrical, tests as well hash~~array)
244 = [keys %main::] \%::
245 = [qw[STDIN STDOUT]] \%::
252 = ["foo"] { foo => 1 }
253 = ["foo", "bar"] { foo => 1 }
254 = ["foo", "bar"] \%hash
257 = [qw(foo quux)] \%hash
258 = @fooormore { foo => 1, or => 2, more => 3 }
259 = @fooormore %fooormore
260 = @fooormore \%fooormore
261 = \@fooormore %fooormore
264 # TODO those should be symmetrical
265 qr/^(fo[ox])$/ {foo => 1}
266 /^(fo[ox])$/ %fooormore
267 =! qr/[13579]$/ +{0..99}
276 "foo" +{foo => 1, bar => 2}
278 ! "baz" +{foo => 1, bar => 2}
280 ! 1 +{foo => 1, bar => 2}
290 ! undef { hop => 'zouu' }
292 ! undef +{"" => "empty key"}
296 # - another array ref
299 ! [["foo"], ["bar"]] [qr/o/, qr/a/]
300 [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
301 ["foo", "bar"] [qr/o/, qr/a/]
302 ! [qr/o/, qr/a/] ["foo", "bar"]
303 ["foo", "bar"] [["foo"], ["bar"]]
304 ! ["foo", "bar"] [qr/o/, "foo"]
305 ["foo", undef, "bar"] [qr/o/, undef, "bar"]
306 ["foo", undef, "bar"] [qr/o/, "", "bar"]
307 ! ["foo", "", "bar"] [qr/o/, undef, "bar"]
319 $obj [sub{ref shift}] TODO
321 # - works with lists instead of arrays
322 "foo" qw(foo bar) TODO
323 "foo" ('foo','bar') TODO
326 qr/x/ [qw(foo bar baz quux)]
327 ! qr/y/ [qw(foo bar baz quux)]
328 /x/ [qw(foo bar baz quux)]
329 ! /y/ [qw(foo bar baz quux)]
340 ! "2" [qw(1foo 2bar)]
341 "2bar" [qw(1foo 2bar)]
343 # Number against number
351 # Number against string
358 # Regex against string
362 # Regex against number
368 # array/hash against string
369 @fooormore "".\@fooormore
370 ! @keyandmore "".\@fooormore
371 %fooormore "".\%fooormore
372 ! %keyandmore "".\%fooormore
374 # Test the implicit referencing
389 %tied_hash %tied_hash
390 %hash { foo => 5, bar => 10 }
391 ! %hash { foo => 5, bar => 10, quux => 15 }
393 @nums { 1, '', 2, '' }
394 @nums { 1, '', 12, '' }
395 ! @nums { 11, '', 12, '' }