1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
#!./perl
BEGIN {
chdir 't';
@INC = '../lib';
require './test.pl';
}
use strict;
use Tie::Array;
use Tie::Hash;
# Predeclare vars used in the tests:
my $deep1 = []; push @$deep1, \$deep1;
my $deep2 = []; push @$deep2, \$deep2;
my @nums = (1..10);
tie my @tied_nums, 'Tie::StdArray';
@tied_nums = (1..10);
my %hash = (foo => 17, bar => 23);
tie my %tied_hash, 'Tie::StdHash';
%tied_hash = %hash;
{
package Test::Object::NoOverload;
sub new { bless { key => 1 } }
}
{
package Test::Object::CopyOverload;
sub new { bless { key => 1 } }
use overload '~~' => sub { my %hash = %{ $_[0] }; %hash ~~ $_[1] };
}
our $ov_obj = Test::Object::CopyOverload->new;
our $obj = Test::Object::NoOverload->new;
my @keyandmore = qw(key and more);
my @fooormore = qw(foo or more);
my %keyandmore = map { $_ => 0 } @keyandmore;
my %fooormore = map { $_ => 0 } @fooormore;
# Load and run the tests
plan "no_plan";
while (<DATA>) {
next if /^#/ || !/\S/;
chomp;
my ($yn, $left, $right, $note) = split /\t+/;
local $::TODO = $note =~ /TODO/;
die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/;
my $tstr = "$left ~~ $right";
test_again:
my $res = eval $tstr;
chomp $@;
if ( $yn =~ /@/ ) {
ok( $@ ne '', "$tstr dies" )
and print "# \$\@ was: $@\n";
} else {
my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches");
if ( $@ ne '' ) {
fail($test_name);
print "# \$\@ was: $@\n";
} else {
ok( ($yn =~ /!/ xor $res), $test_name );
}
}
if ( $yn =~ s/=// ) {
$tstr = "$right ~~ $left";
goto test_again;
}
}
sub foo {}
sub bar {42}
sub gorch {42}
sub fatal {die "fatal sub\n"}
# to test constant folding
sub FALSE() { 0 }
sub TRUE() { 1 }
# Prefix character :
# - expected to match
# ! - expected to not match
# @ - expected to be a compilation failure
# = - expected to match symmetrically (runs test twice)
# Data types to test :
# undef
# Object-overloaded
# Object
# Coderef
# Hash
# Hashref
# Array
# Arrayref
# Tied arrays and hashes
# Arrays that reference themselves
# Regex (// and qr//)
# Range
# Num
# Str
# Other syntactic items of interest:
# Constants
# Values returned by a sub call
__DATA__
# Any ~~ undef
! $ov_obj undef
! $obj undef
! sub {} undef
! %hash undef
! \%hash undef
! {} undef
! @nums undef
! \@nums undef
! [] undef
! %tied_hash undef
! @tied_nums undef
! $deep1 undef
! /foo/ undef
! qr/foo/ undef
! 21..30 undef
! 189 undef
! "foo" undef
! "" undef
! !1 undef
undef undef
(my $u) undef
# Any ~~ object overloaded
# object overloaded ~~ Any
$ov_obj $ov_obj
=@ $ov_obj \&fatal
=! $ov_obj \&FALSE
= $ov_obj \&TRUE
=! $ov_obj \&foo
= $ov_obj \&bar
= $ov_obj sub { shift ~~ "key" }
=! $ov_obj sub { shift ne "key" }
=! $ov_obj sub { shift ~~ "foo" }
= $ov_obj %keyandmore TODO
=! $ov_obj %fooormore
= $ov_obj {"key" => 1}
= $ov_obj {"key" => 1, bar => 2} TODO
=! $ov_obj {"foo" => 1}
= $ov_obj @keyandmore
=! $ov_obj @fooormore
= $ov_obj ["key" => 1]
=! $ov_obj ["foo" => 1]
= $ov_obj /key/ TODO
=! $ov_obj /foo/
= $ov_obj qr/Key/i TODO
=! $ov_obj qr/foo/
= $ov_obj "key" TODO
=! $ov_obj "foo"
=! $ov_obj FALSE
=! $ov_obj TRUE
# regular object
=@ $obj $ov_obj
@ $obj $obj
=@ $obj \&fatal
=@ $obj \&FALSE
=@ $obj \&foo
=@ $obj sub { 1 }
=@ $obj sub { 0 }
=@ $obj %keyandmore
=@ $obj {"key" => 1}
=@ $obj @fooormore
=@ $obj ["key" => 1]
=@ $obj /key/
=@ $obj qr/key/
=@ $obj "key"
=@ $obj FALSE
# ~~ Coderef
sub{0} sub { ref $_[0] eq "CODE" }
%fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
! %fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
\%fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
! \%fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
+{%fooormore} sub { $_[0] =~ /^(foo|or|more)$/ }
! +{%fooormore} sub { $_[0] =~ /^(foo|or|less)$/ }
@fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
! @fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
\@fooormore sub { $_[0] =~ /^(foo|or|more)$/ }
! \@fooormore sub { $_[0] =~ /^(foo|or|less)$/ }
[@fooormore] sub { $_[0] =~ /^(foo|or|more)$/ }
! [@fooormore] sub { $_[0] =~ /^(foo|or|less)$/ }
%fooormore sub{@_==1}
@fooormore sub{@_==1}
"foo" sub { $_[0] =~ /^(foo|or|more)$/ }
! "more" sub { $_[0] =~ /^(foo|or|less)$/ }
/fooormore/ sub{ref $_[0] eq 'Regexp'}
qr/fooormore/ sub{ref $_[0] eq 'Regexp'}
1 sub{shift}
! 0 sub{shift}
! undef sub{shift}
undef sub{not shift}
FALSE sub{not shift}
[1] \&bar
{a=>1} \&bar
qr// \&bar
! [1] \&foo
! {a=>1} \&foo
# empty stuff matches, because the sub is never called:
! [] \&foo
! {} \&foo
! qr// \&foo
! undef \&foo
undef \&bar
@ undef \&fatal
@ 1 \&fatal
@ [1] \&fatal
@ {a=>1} \&fatal
@ "foo" \&fatal
@ qr// \&fatal
# sub is not called on empty hashes / arrays
! [] \&fatal
! +{} \&fatal
# HASH ref against:
# - another hash ref
{} {}
=! {} {1 => 2}
{1 => 2} {1 => 2}
{1 => 2} {1 => 3}
! {1 => 2} {2 => 3}
\%main:: {map {$_ => 'x'} keys %main::}
# - tied hash ref
\%hash \%tied_hash
\%tied_hash \%tied_hash
# - an array ref
[keys %main::] \%::
! [] \%::
! [""] {}
! [] {}
[undef] {"" => 1}
[""] {"" => 1}
["foo"] { foo => 1 }
["foo", "bar"] { foo => 1 }
["foo", "bar"] \%hash
["foo"] \%hash
! ["quux"] \%hash
[qw(foo quux)] \%hash
# - a regex
qr/^(fo[ox])$/ {foo => 1}
! qr/[13579]$/ +{0..99}
! qr/a*/ {}
qr/a*/ {b=>2}
# - a string
"foo" +{foo => 1, bar => 2}
! "baz" +{foo => 1, bar => 2}
# - undef
! undef %hash
! undef +{"" => "empty key"}
! undef {}
# ARRAY ref against:
# - another array ref
[] []
! [] [1]
[["foo"], ["bar"]] [qr/o/, qr/a/]
["foo", "bar"] [qr/o/, qr/a/]
! ["foo", "bar"] [qr/o/, "foo"]
$deep1 $deep1
! $deep1 $deep2
\@nums \@tied_nums
# - a regex
qr/x/ [qw(foo bar baz quux)]
! qr/y/ [qw(foo bar baz quux)]
/x/ [qw(foo bar baz quux)]
! /y/ [qw(foo bar baz quux)]
# - a number
2 [qw(1foo 2bar)]
2 [qw(foo 2)]
2.0_0e+0 [qw(foo 2)]
! 2 [qw(1foo bar2)]
# - a string
! "2" [qw(1foo 2bar)]
"2bar" [qw(1foo 2bar)]
# Number against number
2 2
! 2 3
0 FALSE
3-2 TRUE
# Number against string
2 "2"
2 "2.0"
! 2 "2bananas"
! 2_3 "2_3"
FALSE "0"
# Regex against string
qr/x/ "x"
! qr/y/ "x"
# Regex against number
12345 qr/3/
# Test the implicit referencing
7 @nums
@nums \@nums
! @nums \\@nums
@nums [1..10]
! @nums [0..9]
"foo" %hash
/bar/ %hash
[qw(bar)] %hash
! [qw(a b c)] %hash
%hash %hash
%hash +{%hash}
%hash \%hash
%hash %tied_hash
%tied_hash %tied_hash
%hash { foo => 5, bar => 10 }
! %hash { foo => 5, bar => 10, quux => 15 }
@nums { 1, '', 2, '' }
@nums { 1, '', 12, '' }
! @nums { 11, '', 12, '' }
|