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] }; %hash ~~ $_[1] };
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";
87 sub fatal {die "fatal sub\n"}
95 # ! - expected to not match
96 # @ - expected to be a compilation failure
97 # = - expected to match symmetrically (runs test twice)
98 # Data types to test :
107 # Tied arrays and hashes
108 # Arrays that reference themselves
109 # Regex (// and qr//)
113 # Other syntactic items of interest:
115 # Values returned by a sub call
139 # Any ~~ object overloaded
140 # object overloaded ~~ Any
146 = $ov_obj sub { shift ~~ "key" }
147 =! $ov_obj sub { shift eq "key" }
148 =! $ov_obj sub { shift ~~ "foo" }
149 = $ov_obj %keyandmore TODO
150 =! $ov_obj %fooormore
151 = $ov_obj {"key" => 1}
152 = $ov_obj {"key" => 1, bar => 2} TODO
153 =! $ov_obj {"foo" => 1}
154 = $ov_obj @keyandmore
155 =! $ov_obj @fooormore
156 = $ov_obj ["key" => 1]
157 =! $ov_obj ["foo" => 1]
183 # CODE ref against argument
187 # - arg is not code ref
207 # pass argument by reference
208 @fooormore sub{scalar @_ == 1}
209 @fooormore sub{"@_" =~ /ARRAY/}
210 %fooormore sub{"@_" =~ /HASH/}
211 /fooormore/ sub{ref $_[0] eq 'Regexp'}
213 # - null-prototyped subs
222 # - non-null-prototyped subs
233 \%main:: {map {$_ => 'x'} keys %main::}
237 \%tied_hash \%tied_hash
244 { foo => 1 } ["foo", "bar"]
245 \%hash ["foo", "bar"]
248 \%hash [qw(foo quux)]
251 {foo => 1} qr/^(fo[ox])$/
252 ! +{0..99} qr/[13579]$/
255 +{foo => 1, bar => 2} "foo"
256 ! +{foo => 1, bar => 2} "baz"
260 # - another array ref
263 [["foo"], ["bar"]] [qr/o/, qr/a/]
264 ["foo", "bar"] [qr/o/, qr/a/]
265 ! ["foo", "bar"] [qr/o/, "foo"]
272 [qw(foo bar baz quux)] qr/x/
273 ! [qw(foo bar baz quux)] qr/y/
282 ! [qw(1foo 2bar)] "2"
283 [qw(1foo 2bar)] "2bar"
285 # Number against number
291 # Number against string
298 # Regex against string
302 # Regex against number
306 # Test the implicit referencing
321 %tied_hash %tied_hash
322 %hash { foo => 5, bar => 10 }
323 ! %hash { foo => 5, bar => 10, quux => 15 }
325 @nums { 1, '', 2, '' }
326 @nums { 1, '', 12, '' }
327 ! @nums { 11, '', 12, '' }