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::WithOverload;
36 sub new { bless { key => 'magic' } }
37 use overload '~~' => sub { my %hash = %{ $_[0] }; $_[1] eq $hash{key} };
38 use overload '""' => sub { "stringified" };
39 use overload 'eq' => sub {"$_[0]" eq "$_[1]"};
42 our $ov_obj = Test::Object::WithOverload->new;
43 our $obj = Test::Object::NoOverload->new;
45 my @keyandmore = qw(key and more);
46 my @fooormore = qw(foo or more);
47 my %keyandmore = map { $_ => 0 } @keyandmore;
48 my %fooormore = map { $_ => 0 } @fooormore;
50 # Load and run the tests
54 next if /^#/ || !/\S/;
56 my ($yn, $left, $right, $note) = split /\t+/;
58 local $::TODO = $note =~ /TODO/;
60 die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/;
62 my $tstr = "$left ~~ $right";
70 ok( $@ ne '', "$tstr dies" )
71 and print "# \$\@ was: $@\n";
73 my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches");
76 print "# \$\@ was: $@\n";
78 ok( ($yn =~ /!/ xor $res), $test_name );
83 $tstr = "$right ~~ $left";
91 sub fatal {die "fatal sub\n"}
93 # to test constant folding
96 sub NOT_DEF() { undef }
100 # ! - expected to not match
101 # @ - expected to be a compilation failure
102 # = - expected to match symmetrically (runs test twice)
103 # Data types to test :
112 # Tied arrays and hashes
113 # Arrays that reference themselves
114 # Regex (// and qr//)
118 # Other syntactic items of interest:
120 # Values returned by a sub call
147 # Any ~~ object overloaded
150 ! 'not magic' $ov_obj
171 # object (overloaded or not) ~~ Any
173 $ov_obj qr/^stringified$/
174 $ov_obj "stringified"
177 sub{0} sub { ref $_[0] eq "CODE" }
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 { $_[0] =~ /^(foo|or|more)$/ }
189 ! [@fooormore] sub { $_[0] =~ /^(foo|or|less)$/ }
190 %fooormore sub{@_==1}
191 @fooormore sub{@_==1}
192 "foo" sub { $_[0] =~ /^(foo|or|more)$/ }
193 ! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
194 /fooormore/ sub{ref $_[0] eq 'Regexp'}
195 qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
200 NOT_DEF sub{not shift}
201 &NOT_DEF sub{not shift}
208 $obj sub { ref($_[0]) =~ /NoOverload/ }
209 $ov_obj sub { ref($_[0]) =~ /WithOverload/ }
210 # empty stuff matches, because the sub is never called:
224 # sub is not called on empty hashes / arrays
237 = \%main:: {map {$_ => 'x'} keys %main::}
241 \%tied_hash \%tied_hash
242 != {"a"=>"b"} \%tied_hash
244 %tied_hash %tied_hash
245 != {"a"=>"b"} %tied_hash
248 # (since this is symmetrical, tests as well hash~~array)
249 = [keys %main::] \%::
250 = [qw[STDIN STDOUT]] \%::
257 = ["foo"] { foo => 1 }
258 = ["foo", "bar"] { foo => 1 }
259 = ["foo", "bar"] \%hash
262 = [qw(foo quux)] \%hash
263 = @fooormore { foo => 1, or => 2, more => 3 }
264 = @fooormore %fooormore
265 = @fooormore \%fooormore
266 = \@fooormore %fooormore
269 # TODO those should be symmetrical
270 qr/^(fo[ox])$/ {foo => 1}
271 /^(fo[ox])$/ %fooormore
272 =! qr/[13579]$/ +{0..99}
281 "foo" +{foo => 1, bar => 2}
283 ! "baz" +{foo => 1, bar => 2}
285 ! 1 +{foo => 1, bar => 2}
295 ! undef { hop => 'zouu' }
297 ! undef +{"" => "empty key"}
301 # - another array ref
304 ! [["foo"], ["bar"]] [qr/o/, qr/a/]
305 [["foo"], ["bar"]] [qr/ARRAY/, qr/ARRAY/]
306 ["foo", "bar"] [qr/o/, qr/a/]
307 ! [qr/o/, qr/a/] ["foo", "bar"]
308 ["foo", "bar"] [["foo"], ["bar"]]
309 ! ["foo", "bar"] [qr/o/, "foo"]
310 ["foo", undef, "bar"] [qr/o/, undef, "bar"]
311 ["foo", undef, "bar"] [qr/o/, "", "bar"]
312 ! ["foo", "", "bar"] [qr/o/, undef, "bar"]
324 $obj [sub{ref shift}]
326 # - works with lists instead of arrays
327 "foo" qw(foo bar) TODO
328 "foo" ('foo','bar') TODO
331 qr/x/ [qw(foo bar baz quux)]
332 ! qr/y/ [qw(foo bar baz quux)]
333 /x/ [qw(foo bar baz quux)]
334 ! /y/ [qw(foo bar baz quux)]
345 ! "2" [qw(1foo 2bar)]
346 "2bar" [qw(1foo 2bar)]
349 undef [1, 2, undef, 4]
350 ! undef [1, 2, [undef], 4]
354 # - nested arrays and ~~ distributivity
357 "foo" [{foo => "bar"}]
358 ! "bar" [{foo => "bar"}]
360 # Number against number
368 # Number against string
375 # Regex against string
379 # Regex against number
385 # array/hash against string
386 @fooormore "".\@fooormore
387 ! @keyandmore "".\@fooormore
388 %fooormore "".\%fooormore
389 ! %keyandmore "".\%fooormore
391 # Test the implicit referencing
406 %tied_hash %tied_hash
407 %hash { foo => 5, bar => 10 }
408 ! %hash { foo => 5, bar => 10, quux => 15 }
410 @nums { 1, '', 2, '' }
411 @nums { 1, '', 12, '' }
412 ! @nums { 11, '', 12, '' }