13 # Predeclare vars used in the tests:
16 my @sparse; $sparse[2] = 2;
18 my $deep1 = []; push @$deep1, \$deep1;
19 my $deep2 = []; push @$deep2, \$deep2;
22 tie my @tied_nums, 'Tie::StdArray';
25 my %hash = (foo => 17, bar => 23);
26 tie my %tied_hash, 'Tie::StdHash';
30 package Test::Object::NoOverload;
31 sub new { bless { key => 1 } }
35 package Test::Object::CopyOverload;
36 sub new { bless { key => 'magic' } }
37 use overload '~~' => sub { my %hash = %{ $_[0] }; $_[1] eq $hash{key} };
40 our $ov_obj = Test::Object::CopyOverload->new;
41 our $obj = Test::Object::NoOverload->new;
43 my @keyandmore = qw(key and more);
44 my @fooormore = qw(foo or more);
45 my %keyandmore = map { $_ => 0 } @keyandmore;
46 my %fooormore = map { $_ => 0 } @fooormore;
48 # Load and run the tests
52 next if /^#/ || !/\S/;
54 my ($yn, $left, $right, $note) = split /\t+/;
56 local $::TODO = $note =~ /TODO/;
58 die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/;
60 my $tstr = "$left ~~ $right";
68 ok( $@ ne '', "$tstr dies" )
69 and print "# \$\@ was: $@\n";
71 my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches");
74 print "# \$\@ was: $@\n";
76 ok( ($yn =~ /!/ xor $res), $test_name );
81 $tstr = "$right ~~ $left";
89 sub fatal {die "fatal sub\n"}
91 # to test constant folding
94 sub NOT_DEF() { undef }
98 # ! - expected to not match
99 # @ - expected to be a compilation failure
100 # = - expected to match symmetrically (runs test twice)
101 # Data types to test :
110 # Tied arrays and hashes
111 # Arrays that reference themselves
112 # Regex (// and qr//)
116 # Other syntactic items of interest:
118 # Values returned by a sub call
145 # Any ~~ object overloaded
148 ! 'not magic' $ov_obj
169 # object (overloaded or not) ~~ Any
173 sub{0} sub { ref $_[0] eq "CODE" }
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 { $_[0] =~ /^(foo|or|more)$/ }
183 ! \@fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
184 [@fooormore] sub { $_[0] =~ /^(foo|or|more)$/ }
185 ! [@fooormore] sub { $_[0] =~ /^(foo|or|less)$/ }
186 %fooormore sub{@_==1}
187 @fooormore sub{@_==1}
188 "foo" sub { $_[0] =~ /^(foo|or|more)$/ }
189 ! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
190 /fooormore/ sub{ref $_[0] eq 'Regexp'}
191 qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
196 NOT_DEF sub{not shift}
197 &NOT_DEF sub{not shift}
204 $obj sub { ref $_[0] =~ /NoOverload/ } TODO
205 $ov_obj sub { ref $_[0] =~ /CopyOverload/ } TODO
206 # empty stuff matches, because the sub is never called:
220 # sub is not called on empty hashes / arrays
233 = \%main:: {map {$_ => 'x'} keys %main::}
237 \%tied_hash \%tied_hash
238 != {"a"=>"b"} \%tied_hash
240 %tied_hash %tied_hash
241 != {"a"=>"b"} %tied_hash
244 # (since this is symmetrical, tests as well hash~~array)
245 = [keys %main::] \%::
246 = [qw[STDIN STDOUT]] \%::
253 = ["foo"] { foo => 1 }
254 = ["foo", "bar"] { foo => 1 }
255 = ["foo", "bar"] \%hash
258 = [qw(foo quux)] \%hash
259 = @fooormore { foo => 1, or => 2, more => 3 }
260 = @fooormore %fooormore
261 = @fooormore \%fooormore
262 = \@fooormore %fooormore
265 # TODO those should be symmetrical
266 qr/^(fo[ox])$/ {foo => 1}
267 /^(fo[ox])$/ %fooormore
268 =! qr/[13579]$/ +{0..99}
277 "foo" +{foo => 1, bar => 2}
279 ! "baz" +{foo => 1, bar => 2}
281 ! 1 +{foo => 1, bar => 2}
291 ! undef { hop => 'zouu' }
293 ! undef +{"" => "empty key"}
297 # - another array ref
300 ! [["foo"], ["bar"]] [qr/o/, qr/a/]
301 [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
302 ["foo", "bar"] [qr/o/, qr/a/]
303 ! [qr/o/, qr/a/] ["foo", "bar"]
304 ["foo", "bar"] [["foo"], ["bar"]]
305 ! ["foo", "bar"] [qr/o/, "foo"]
306 ["foo", undef, "bar"] [qr/o/, undef, "bar"]
307 ["foo", undef, "bar"] [qr/o/, "", "bar"]
308 ! ["foo", "", "bar"] [qr/o/, undef, "bar"]
320 $obj [sub{ref shift}] TODO
322 # - works with lists instead of arrays
323 "foo" qw(foo bar) TODO
324 "foo" ('foo','bar') TODO
327 qr/x/ [qw(foo bar baz quux)]
328 ! qr/y/ [qw(foo bar baz quux)]
329 /x/ [qw(foo bar baz quux)]
330 ! /y/ [qw(foo bar baz quux)]
341 ! "2" [qw(1foo 2bar)]
342 "2bar" [qw(1foo 2bar)]
345 undef [1, 2, undef, 4]
346 ! undef [1, 2, [undef], 4]
350 # - nested arrays and ~~ distributivity
353 "foo" [{foo => "bar"}]
354 ! "bar" [{foo => "bar"}]
356 # Number against number
364 # Number against string
371 # Regex against string
375 # Regex against number
381 # array/hash against string
382 @fooormore "".\@fooormore
383 ! @keyandmore "".\@fooormore
384 %fooormore "".\%fooormore
385 ! %keyandmore "".\%fooormore
387 # Test the implicit referencing
402 %tied_hash %tied_hash
403 %hash { foo => 5, bar => 10 }
404 ! %hash { foo => 5, bar => 10, quux => 15 }
406 @nums { 1, '', 2, '' }
407 @nums { 1, '', 12, '' }
408 ! @nums { 11, '', 12, '' }