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';
31 # Load and run the tests
32 my @tests = map [chomp and split /\t+/, $_, 3], grep !/^#/ && /\S/, <DATA>;
33 plan tests => 2 * @tests;
35 for my $test (@tests) {
36 my ($yn, $left, $right) = @$test;
38 match_test($yn, $left, $right);
39 match_test($yn, $right, $left);
43 my ($yn, $left, $right) = @_;
45 die "Bad test spec: ($yn, $left, $right)"
46 unless $yn eq "" || $yn eq "!" || $yn eq '@';
48 my $tstr = "$left ~~ $right";
51 $res = eval $tstr // ""; #/ <- fix syntax colouring
56 ok( $@ ne '', sprintf "%s%s: %s", $tstr, $@ ? ( ', $@', $@ ) : ( '', $res ) );
59 fail("$tstr, \$\@: $@");
61 ok( ($yn eq '!' xor $res), "$tstr: $res");
72 sub a_const() {die if @_; "a constant"}
73 sub b_const() {die if @_; "a constant"}
76 # CODE ref against argument
82 # - arg is not code ref
90 # - null-prototyped subs
102 \%main:: {map {$_ => 'x'} keys %main::}
106 \%tied_hash \%tied_hash
113 { foo => 1 } ["foo", "bar"]
114 \%hash ["foo", "bar"]
117 \%hash [qw(foo quux)]
120 {foo => 1} qr/^(fo[ox])$/
121 ! +{0..100} qr/[13579]$/
124 +{foo => 1, bar => 2} "foo"
125 ! +{foo => 1, bar => 2} "baz"
129 # - another array ref
132 [["foo"], ["bar"]] [qr/o/, qr/a/]
133 ["foo", "bar"] [qr/o/, qr/a/]
134 ! ["foo", "bar"] [qr/o/, "foo"]
141 [qw(foo bar baz quux)] qr/x/
142 ! [qw(foo bar baz quux)] qr/y/
148 ! [qw(1foo 2bar)] "2"
150 # Number against number
154 # Number against string
160 # Regex against string
164 # Regex against number
168 # Test the implicit referencing
182 %tied_hash %tied_hash
183 %hash { foo => 5, bar => 10 }
184 ! %hash { foo => 5, bar => 10, quux => 15 }
186 @nums { 1, '', 2, '' }
187 @nums { 1, '', 12, '' }
188 ! @nums { 11, '', 12, '' }