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;
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 => 1 } }
37 use overload '~~' => sub { my %hash = %{ $_[0] }; %hash ~~ $_[1] };
41 package Test::Object::OverloadCodeRef;
42 sub new { bless $_[1] }
43 use overload '~~' => sub { shift->($_[1]) };
46 our $ov_obj = Test::Object::CopyOverload->new;
47 our $obj = Test::Object::NoOverload->new;
48 our $false_obj = Test::Object::OverloadCodeRef->new(sub { 0 });
49 our $true_obj = Test::Object::OverloadCodeRef->new(sub { 1 });
52 # Load and run the tests
53 my @tests = map [chomp and split /\t+/, $_, 3], grep !/^#/ && /\S/, <DATA>;
54 plan tests => 2 * @tests;
56 for my $test (@tests) {
57 my ($yn, $left, $right) = @$test;
59 match_test($yn, $left, $right);
60 match_test($yn, $right, $left);
64 my ($yn, $left, $right) = @_;
66 die "Bad test spec: ($yn, $left, $right)"
67 unless $yn eq "" || $yn eq "!" || $yn eq '@';
69 my $tstr = "$left ~~ $right";
72 $res = eval $tstr // ""; #/ <- fix syntax colouring
77 ok( $@ ne '', sprintf "%s%s: %s", $tstr, $@ ? ( ', $@', $@ ) : ( '', $res ) );
80 fail("$tstr, \$\@: $@");
82 ok( ($yn eq '!' xor $res), "$tstr: $res");
92 sub fatal {die "fatal sub\n"}
94 sub a_const() {die "const\n" if @_; "a constant"}
95 sub b_const() {die "const\n" if @_; "a constant"}
116 # CODE ref against argument
124 # - arg is not code ref
146 # - null-prototyped subs
151 ! \&a_const \&b_const
153 # - non-null-prototyped subs
165 \%main:: {map {$_ => 'x'} keys %main::}
169 \%tied_hash \%tied_hash
176 { foo => 1 } ["foo", "bar"]
177 \%hash ["foo", "bar"]
180 \%hash [qw(foo quux)]
183 {foo => 1} qr/^(fo[ox])$/
184 ! +{0..100} qr/[13579]$/
187 +{foo => 1, bar => 2} "foo"
188 ! +{foo => 1, bar => 2} "baz"
192 # - another array ref
195 [["foo"], ["bar"]] [qr/o/, qr/a/]
196 ["foo", "bar"] [qr/o/, qr/a/]
197 ! ["foo", "bar"] [qr/o/, "foo"]
204 [qw(foo bar baz quux)] qr/x/
205 ! [qw(foo bar baz quux)] qr/y/
214 ! [qw(1foo 2bar)] "2"
215 [qw(1foo 2bar)] "2bar"
217 # Number against number
221 # Number against string
227 # Regex against string
231 # Regex against number
235 # Test the implicit referencing
249 %tied_hash %tied_hash
250 %hash { foo => 5, bar => 10 }
251 ! %hash { foo => 5, bar => 10, quux => 15 }
253 @nums { 1, '', 2, '' }
254 @nums { 1, '', 12, '' }
255 ! @nums { 11, '', 12, '' }