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} };
38 use overload '""' => sub { "stringified" };
41 our $ov_obj = Test::Object::CopyOverload->new;
42 our $obj = Test::Object::NoOverload->new;
44 my @keyandmore = qw(key and more);
45 my @fooormore = qw(foo or more);
46 my %keyandmore = map { $_ => 0 } @keyandmore;
47 my %fooormore = map { $_ => 0 } @fooormore;
49 # Load and run the tests
53 next if /^#/ || !/\S/;
55 my ($yn, $left, $right, $note) = split /\t+/;
57 local $::TODO = $note =~ /TODO/;
59 die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/;
61 my $tstr = "$left ~~ $right";
69 ok( $@ ne '', "$tstr dies" )
70 and print "# \$\@ was: $@\n";
72 my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches");
75 print "# \$\@ was: $@\n";
77 ok( ($yn =~ /!/ xor $res), $test_name );
82 $tstr = "$right ~~ $left";
90 sub fatal {die "fatal sub\n"}
92 # to test constant folding
95 sub NOT_DEF() { undef }
99 # ! - expected to not match
100 # @ - expected to be a compilation failure
101 # = - expected to match symmetrically (runs test twice)
102 # Data types to test :
111 # Tied arrays and hashes
112 # Arrays that reference themselves
113 # Regex (// and qr//)
117 # Other syntactic items of interest:
119 # Values returned by a sub call
146 # Any ~~ object overloaded
149 ! 'not magic' $ov_obj
170 # object (overloaded or not) ~~ Any
172 $ov_obj qr/^stringified$/
175 sub{0} sub { ref $_[0] eq "CODE" }
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 { $_[0] =~ /^(foo|or|more)$/ }
187 ! [@fooormore] sub { $_[0] =~ /^(foo|or|less)$/ }
188 %fooormore sub{@_==1}
189 @fooormore sub{@_==1}
190 "foo" sub { $_[0] =~ /^(foo|or|more)$/ }
191 ! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
192 /fooormore/ sub{ref $_[0] eq 'Regexp'}
193 qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
198 NOT_DEF sub{not shift}
199 &NOT_DEF sub{not shift}
206 $obj sub { ref($_[0]) =~ /NoOverload/ }
207 $ov_obj sub { ref($_[0]) =~ /CopyOverload/ }
208 # empty stuff matches, because the sub is never called:
222 # sub is not called on empty hashes / arrays
235 = \%main:: {map {$_ => 'x'} keys %main::}
239 \%tied_hash \%tied_hash
240 != {"a"=>"b"} \%tied_hash
242 %tied_hash %tied_hash
243 != {"a"=>"b"} %tied_hash
246 # (since this is symmetrical, tests as well hash~~array)
247 = [keys %main::] \%::
248 = [qw[STDIN STDOUT]] \%::
255 = ["foo"] { foo => 1 }
256 = ["foo", "bar"] { foo => 1 }
257 = ["foo", "bar"] \%hash
260 = [qw(foo quux)] \%hash
261 = @fooormore { foo => 1, or => 2, more => 3 }
262 = @fooormore %fooormore
263 = @fooormore \%fooormore
264 = \@fooormore %fooormore
267 # TODO those should be symmetrical
268 qr/^(fo[ox])$/ {foo => 1}
269 /^(fo[ox])$/ %fooormore
270 =! qr/[13579]$/ +{0..99}
279 "foo" +{foo => 1, bar => 2}
281 ! "baz" +{foo => 1, bar => 2}
283 ! 1 +{foo => 1, bar => 2}
293 ! undef { hop => 'zouu' }
295 ! undef +{"" => "empty key"}
299 # - another array ref
302 ! [["foo"], ["bar"]] [qr/o/, qr/a/]
303 [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
304 ["foo", "bar"] [qr/o/, qr/a/]
305 ! [qr/o/, qr/a/] ["foo", "bar"]
306 ["foo", "bar"] [["foo"], ["bar"]]
307 ! ["foo", "bar"] [qr/o/, "foo"]
308 ["foo", undef, "bar"] [qr/o/, undef, "bar"]
309 ["foo", undef, "bar"] [qr/o/, "", "bar"]
310 ! ["foo", "", "bar"] [qr/o/, undef, "bar"]
322 $obj [sub{ref shift}]
324 # - works with lists instead of arrays
325 "foo" qw(foo bar) TODO
326 "foo" ('foo','bar') TODO
329 qr/x/ [qw(foo bar baz quux)]
330 ! qr/y/ [qw(foo bar baz quux)]
331 /x/ [qw(foo bar baz quux)]
332 ! /y/ [qw(foo bar baz quux)]
343 ! "2" [qw(1foo 2bar)]
344 "2bar" [qw(1foo 2bar)]
347 undef [1, 2, undef, 4]
348 ! undef [1, 2, [undef], 4]
352 # - nested arrays and ~~ distributivity
355 "foo" [{foo => "bar"}]
356 ! "bar" [{foo => "bar"}]
358 # Number against number
366 # Number against string
373 # Regex against string
377 # Regex against number
383 # array/hash against string
384 @fooormore "".\@fooormore
385 ! @keyandmore "".\@fooormore
386 %fooormore "".\%fooormore
387 ! %keyandmore "".\%fooormore
389 # Test the implicit referencing
404 %tied_hash %tied_hash
405 %hash { foo => 5, bar => 10 }
406 ! %hash { foo => 5, bar => 10, quux => 15 }
408 @nums { 1, '', 2, '' }
409 @nums { 1, '', 12, '' }
410 ! @nums { 11, '', 12, '' }