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] };
37 package Test::Object::OverloadCodeRef;
38 sub new { bless $_[1] }
39 use overload '~~' => sub { shift->($_[1]) };
42 our $ov_obj = Test::Object::CopyOverload->new;
43 our $obj = Test::Object::NoOverload->new;
44 our $false_obj = Test::Object::OverloadCodeRef->new(sub { 0 });
45 our $true_obj = Test::Object::OverloadCodeRef->new(sub { 1 });
48 # Load and run the tests
49 my @tests = map [chomp and split /\t+/, $_, 3], grep !/^#/ && /\S/, <DATA>;
50 plan tests => 2 * @tests;
52 for my $test (@tests) {
53 my ($yn, $left, $right) = @$test;
55 match_test($yn, $left, $right);
56 match_test($yn, $right, $left);
60 my ($yn, $left, $right) = @_;
62 die "Bad test spec: ($yn, $left, $right)"
63 unless $yn eq "" || $yn eq "!" || $yn eq '@';
65 my $tstr = "$left ~~ $right";
68 $res = eval $tstr // ""; #/ <- fix syntax colouring
73 ok( $@ ne '', sprintf "%s%s: %s", $tstr, $@ ? ( ', $@', $@ ) : ( '', $res ) );
76 fail("$tstr, \$\@: $@");
78 ok( ($yn eq '!' xor $res), "$tstr: $res");
88 sub fatal {die "fatal sub\n"}
90 sub a_const() {die "const\n" if @_; "a constant"}
91 sub b_const() {die "const\n" if @_; "a constant"}
95 # ! - expected to not match
96 # @ - expected to be a compilation failure
118 # CODE ref against argument
126 # - arg is not code ref
148 # - null-prototyped subs
153 ! \&a_const \&b_const
155 # - non-null-prototyped subs
167 \%main:: {map {$_ => 'x'} keys %main::}
171 \%tied_hash \%tied_hash
178 { foo => 1 } ["foo", "bar"]
179 \%hash ["foo", "bar"]
182 \%hash [qw(foo quux)]
185 {foo => 1} qr/^(fo[ox])$/
186 ! +{0..100} qr/[13579]$/
189 +{foo => 1, bar => 2} "foo"
190 ! +{foo => 1, bar => 2} "baz"
194 # - another array ref
197 [["foo"], ["bar"]] [qr/o/, qr/a/]
198 ["foo", "bar"] [qr/o/, qr/a/]
199 ! ["foo", "bar"] [qr/o/, "foo"]
206 [qw(foo bar baz quux)] qr/x/
207 ! [qw(foo bar baz quux)] qr/y/
216 ! [qw(1foo 2bar)] "2"
217 [qw(1foo 2bar)] "2bar"
219 # Number against number
223 # Number against string
229 # Regex against string
233 # Regex against number
237 # Test the implicit referencing
251 %tied_hash %tied_hash
252 %hash { foo => 5, bar => 10 }
253 ! %hash { foo => 5, bar => 10, quux => 15 }
255 @nums { 1, '', 2, '' }
256 @nums { 1, '', 12, '' }
257 ! @nums { 11, '', 12, '' }