13 # The feature mechanism is tested in t/lib/feature/smartmatch:
14 # This file tests the semantics of the operator, without worrying
15 # about feature issues such as scoping etc.
17 # Predeclare vars used in the tests:
18 my $deep1 = []; push @$deep1, \$deep1;
19 my $deep2 = []; push @$deep2, \$deep2;
21 {my $const = "a constant"; sub a_const () {$const}}
24 tie my @tied_nums, 'Tie::StdArray';
27 my %hash = (foo => 17, bar => 23);
28 tie my %tied_hash, 'Tie::StdHash';
32 package Test::Object::NoOverload;
33 sub new { bless { key => 1 } }
37 package Test::Object::CopyOverload;
38 sub new { bless { key => 1 } }
39 use overload '~~' => sub { my %hash = %{ $_[0] }; %hash ~~ $_[1] };
43 package Test::Object::OverloadCodeRef;
44 sub new { bless $_[1] }
45 use overload '~~' => sub { shift->($_[1]) };
48 our $ov_obj = Test::Object::CopyOverload->new;
49 our $obj = Test::Object::NoOverload->new;
50 our $false_obj = Test::Object::OverloadCodeRef->new(sub { 0 });
51 our $true_obj = Test::Object::OverloadCodeRef->new(sub { 1 });
54 # Load and run the tests
55 my @tests = map [chomp and split /\t+/, $_, 3], grep !/^#/ && /\S/, <DATA>;
56 plan tests => 2 * @tests;
58 for my $test (@tests) {
59 my ($yn, $left, $right) = @$test;
61 match_test($yn, $left, $right);
62 match_test($yn, $right, $left);
66 my ($yn, $left, $right) = @_;
68 die "Bad test spec: ($yn, $left, $right)"
69 unless $yn eq "" || $yn eq "!" || $yn eq '@';
71 my $tstr = "$left ~~ $right";
74 $res = eval $tstr // ""; #/ <- fix syntax colouring
79 ok( $@ ne '', sprintf "%s%s: %s", $tstr, $@ ? ( ', $@', $@ ) : ( '', $res ) );
82 fail("$tstr, \$\@: $@");
84 ok( ($yn eq '!' xor $res), "$tstr: $res");
94 sub fatal {die "fatal sub\n"}
96 sub a_const() {die "const\n" if @_; "a constant"}
97 sub b_const() {die "const\n" if @_; "a constant"}
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/
213 ! [qw(1foo 2bar)] "2"
215 # Number against number
219 # Number against string
225 # Regex against string
229 # Regex against number
233 # Test the implicit referencing
247 %tied_hash %tied_hash
248 %hash { foo => 5, bar => 10 }
249 ! %hash { foo => 5, bar => 10, quux => 15 }
251 @nums { 1, '', 2, '' }
252 @nums { 1, '', 12, '' }
253 ! @nums { 11, '', 12, '' }