Better diagnostics for the ~~ test
[p5sagit/p5-mst-13.2.git] / t / op / smartmatch.t
CommitLineData
0d863452 1#!./perl
2
3BEGIN {
4 chdir 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8use strict;
9
10use Tie::Array;
11use Tie::Hash;
12
0d863452 13# Predeclare vars used in the tests:
14my $deep1 = []; push @$deep1, \$deep1;
15my $deep2 = []; push @$deep2, \$deep2;
16
0d863452 17my @nums = (1..10);
18tie my @tied_nums, 'Tie::StdArray';
19@tied_nums = (1..10);
20
21my %hash = (foo => 17, bar => 23);
22tie my %tied_hash, 'Tie::StdHash';
23%tied_hash = %hash;
24
1cfb7049 25{
26 package Test::Object::NoOverload;
27 sub new { bless { key => 1 } }
28}
29
30{
31 package Test::Object::CopyOverload;
32 sub new { bless { key => 1 } }
33 use overload '~~' => sub { my %hash = %{ $_[0] }; %hash ~~ $_[1] };
34}
35
1cfb7049 36our $ov_obj = Test::Object::CopyOverload->new;
37our $obj = Test::Object::NoOverload->new;
1cfb7049 38
0d863452 39# Load and run the tests
9e079ace 40plan "no_plan";
0d863452 41
9e079ace 42while (<DATA>) {
43 next if /^#/ || !/\S/;
44 chomp;
45 my ($yn, $left, $right) = split /\t+/;
0d863452 46
47 match_test($yn, $left, $right);
48 match_test($yn, $right, $left);
49}
50
51sub match_test {
52 my ($yn, $left, $right) = @_;
53
54 die "Bad test spec: ($yn, $left, $right)"
a86f5011 55 unless $yn eq "" || $yn eq "!" || $yn eq '@';
9e079ace 56
0d863452 57 my $tstr = "$left ~~ $right";
9e079ace 58
59 my $res = eval $tstr;
0d863452 60
a86f5011 61 chomp $@;
62
63 if ( $yn eq '@' ) {
9e079ace 64 ok( $@ ne '', "$tstr dies" )
65 and print "# \$\@ was: $@\n";
a86f5011 66 } else {
9e079ace 67 my $test_name = $tstr . ($yn eq '!' ? " does not match" : " matches");
a86f5011 68 if ( $@ ne '' ) {
9e079ace 69 fail($test_name);
70 print "# \$\@ was: $@\n";
a86f5011 71 } else {
9e079ace 72 ok( ($yn eq '!' xor $res), $test_name );
a86f5011 73 }
74 }
0d863452 75}
76
77
78
79sub foo {}
80sub bar {2}
1cfb7049 81sub gorch {2}
82sub fatal {die "fatal sub\n"}
0d863452 83
1cfb7049 84sub a_const() {die "const\n" if @_; "a constant"}
85sub b_const() {die "const\n" if @_; "a constant"}
18d11902 86sub FALSE() { 0 }
87sub TRUE() { 1 }
88sub TWO() { 1 }
0d863452 89
e5de85fa 90# Prefix character :
91# - expected to match
92# ! - expected to not match
93# @ - expected to be a compilation failure
0d863452 94__DATA__
1cfb7049 95# OBJECT
96# - overloaded
97 $ov_obj "key"
98 $ov_obj {"key" => 1}
99! $ov_obj "foo"
18d11902 100 $ov_obj sub { shift ~~ "key" }
101! $ov_obj sub { shift ~~ "foo" }
1cfb7049 102! $ov_obj \&foo
103@ $ov_obj \&fatal
18d11902 104! $ov_obj FALSE
105! $ov_obj \&FALSE
33570f8b 106! $ov_obj undef
1cfb7049 107
108# regular object
109@ $obj "key"
110@ $obj {"key" => 1}
1cfb7049 111@ $obj $obj
112@ $obj sub { 1 }
113@ $obj sub { 0 }
114@ $obj \&foo
115@ $obj \&fatal
18d11902 116@ $obj FALSE
117@ $obj \&FALSE
33570f8b 118! $obj undef
1cfb7049 119
0d863452 120# CODE ref against argument
121# - arg is code ref
122 \&foo \&foo
123! \&foo sub {}
18d11902 124! \&foo sub { "$_[0]" =~ /^CODE/ }
0d863452 125! \&foo \&bar
1cfb7049 126 \&fatal \&fatal
127! \&foo \&fatal
0d863452 128
129# - arg is not code ref
1cfb7049 130 1 sub{shift}
131! 0 sub{shift}
132! undef sub{shift}
133 undef sub{not shift}
18d11902 134 FALSE sub{not shift}
1cfb7049 135 1 sub{scalar @_}
136 [] \&bar
137 {} \&bar
138 qr// \&bar
139! [] \&foo
140! {} \&foo
141! qr// \&foo
142! undef \&foo
143 undef \&bar
144@ undef \&fatal
145@ 1 \&fatal
146@ [] \&fatal
147@ "foo" \&fatal
148@ qr// \&fatal
149@ $obj \&bar
150 $ov_obj \&bar
0d863452 151
152# - null-prototyped subs
153 a_const "a constant"
154 a_const a_const
155 a_const b_const
1cfb7049 156 \&a_const \&a_const
157! \&a_const \&b_const
18d11902 158! undef \&FALSE
159 undef \&TRUE
160! 0 \&FALSE
161 0 \&TRUE
162! 1 \&FALSE
163 1 \&TRUE
164 \&FALSE \&FALSE
165! \&FALSE \&foo
166! \&FALSE \&bar
167! \&TRUE \&foo
168! \&TRUE \&bar
169! \&TWO \&foo
170! \&TWO \&bar
171 \&FALSE \&FALSE
1cfb7049 172
173# - non-null-prototyped subs
174! \&bar \&gorch
175 bar gorch
176@ fatal bar
0d863452 177
178# HASH ref against:
179# - another hash ref
180 {} {}
181! {} {1 => 2}
182 {1 => 2} {1 => 2}
183 {1 => 2} {1 => 3}
184! {1 => 2} {2 => 3}
185 \%main:: {map {$_ => 'x'} keys %main::}
186
187# - tied hash ref
188 \%hash \%tied_hash
189 \%tied_hash \%tied_hash
190
191# - an array ref
192 \%:: [keys %main::]
193! \%:: []
194 {"" => 1} [undef]
71b0fb34 195 { foo => 1 } ["foo"]
196 { foo => 1 } ["foo", "bar"]
197 \%hash ["foo", "bar"]
198 \%hash ["foo"]
199! \%hash ["quux"]
200 \%hash [qw(foo quux)]
0d863452 201
202# - a regex
203 {foo => 1} qr/^(fo[ox])$/
204! +{0..100} qr/[13579]$/
205
206# - a string
207 +{foo => 1, bar => 2} "foo"
208! +{foo => 1, bar => 2} "baz"
209
210
211# ARRAY ref against:
212# - another array ref
1cfb7049 213 [] []
214! [] [1]
0d863452 215 [["foo"], ["bar"]] [qr/o/, qr/a/]
216 ["foo", "bar"] [qr/o/, qr/a/]
71b0fb34 217! ["foo", "bar"] [qr/o/, "foo"]
1cfb7049 218 $deep1 $deep1
219! $deep1 $deep2
0d863452 220
1cfb7049 221 \@nums \@tied_nums
0d863452 222
223# - a regex
224 [qw(foo bar baz quux)] qr/x/
225! [qw(foo bar baz quux)] qr/y/
226
227# - a number
228 [qw(1foo 2bar)] 2
25a0c96d 229 [qw(foo 2)] 2
230 [qw(foo 2)] 2.0_0e+0
231! [qw(1foo bar2)] 2
0d863452 232
233# - a string
234! [qw(1foo 2bar)] "2"
25a0c96d 235 [qw(1foo 2bar)] "2bar"
0d863452 236
237# Number against number
238 2 2
239! 2 3
18d11902 240 0 FALSE
241 3-2 TRUE
0d863452 242
243# Number against string
244 2 "2"
245 2 "2.0"
246! 2 "2bananas"
247! 2_3 "2_3"
18d11902 248 FALSE "0"
0d863452 249
250# Regex against string
251 qr/x/ "x"
252! qr/y/ "x"
253
254# Regex against number
255 12345 qr/3/
256
257
258# Test the implicit referencing
259 @nums 7
260 @nums \@nums
261! @nums \\@nums
262 @nums [1..10]
263! @nums [0..9]
264
265 %hash "foo"
266 %hash /bar/
71b0fb34 267 %hash [qw(bar)]
268! %hash [qw(a b c)]
269 %hash %hash
270 %hash {%hash}
271 %hash %tied_hash
272 %tied_hash %tied_hash
273 %hash { foo => 5, bar => 10 }
274! %hash { foo => 5, bar => 10, quux => 15 }
275
276 @nums { 1, '', 2, '' }
277 @nums { 1, '', 12, '' }
278! @nums { 11, '', 12, '' }
b472f62a 279
280# UNDEF
281! 3 undef
282! 1 undef
283! [] undef
284! {} undef
285! \%::main undef
286! [1,2] undef
287! %hash undef
288! @nums undef
289! "foo" undef
290! "" undef
291! !1 undef
292! \&foo undef
293! sub { } undef
161d9976 294 undef undef
295 $::undef undef