e2ea00483c015e7509489001eab760df0695c0a8
[p5sagit/p5-mst-13.2.git] / t / op / smartmatch.t
1 #!./perl
2
3 BEGIN {
4     chdir 't';
5     @INC = '../lib';
6     require './test.pl';
7 }
8 use strict;
9
10 use Tie::Array;
11 use Tie::Hash;
12
13 # Predeclare vars used in the tests:
14 my $deep1 = []; push @$deep1, \$deep1;
15 my $deep2 = []; push @$deep2, \$deep2;
16
17 my @nums = (1..10);
18 tie my @tied_nums, 'Tie::StdArray';
19 @tied_nums =  (1..10);
20
21 my %hash = (foo => 17, bar => 23);
22 tie my %tied_hash, 'Tie::StdHash';
23 %tied_hash = %hash;
24
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
36 our $ov_obj = Test::Object::CopyOverload->new;
37 our $obj = Test::Object::NoOverload->new;
38
39 # Load and run the tests
40 plan "no_plan";
41
42 while (<DATA>) {
43     next if /^#/ || !/\S/;
44     chomp;
45     my ($yn, $left, $right) = split /\t+/;
46
47     match_test($yn, $left, $right);
48     match_test($yn, $right, $left);
49 }
50
51 sub match_test {
52     my ($yn, $left, $right) = @_;
53
54     die "Bad test spec: ($yn, $left, $right)"
55         unless $yn eq "" || $yn eq "!" || $yn eq '@';
56
57     my $tstr = "$left ~~ $right";
58
59     my $res = eval $tstr;
60
61     chomp $@;
62
63     if ( $yn eq '@' ) {
64         ok( $@ ne '', "$tstr dies" )
65             and print "# \$\@ was: $@\n";
66     } else {
67         my $test_name = $tstr . ($yn eq '!' ? " does not match" : " matches");
68         if ( $@ ne '' ) {
69             fail($test_name);
70             print "# \$\@ was: $@\n";
71         } else {
72             ok( ($yn eq '!' xor $res), $test_name );
73         }
74     }
75 }
76
77
78
79 sub foo {}
80 sub bar {2}
81 sub gorch {2}
82 sub fatal {die "fatal sub\n"}
83
84 sub a_const() {die "const\n" if @_; "a constant"}
85 sub b_const() {die "const\n" if @_; "a constant"}
86 sub FALSE() { 0 }
87 sub TRUE() { 1 }
88 sub TWO() { 1 }
89
90 # Prefix character :
91 #   - expected to match
92 # ! - expected to not match
93 # @ - expected to be a compilation failure
94 __DATA__
95 # OBJECT
96 # - overloaded
97         $ov_obj         "key"
98         $ov_obj         {"key" => 1}
99 !       $ov_obj         "foo"
100         $ov_obj         sub { shift ~~ "key" }
101 !       $ov_obj         sub { shift ~~ "foo" }
102 !       $ov_obj         \&foo
103 @       $ov_obj         \&fatal
104 !       $ov_obj         FALSE
105 !       $ov_obj         \&FALSE
106 !       $ov_obj         undef
107
108 # regular object
109 @       $obj    "key"
110 @       $obj    {"key" => 1}
111 @       $obj    $obj
112 @       $obj    sub { 1 }
113 @       $obj    sub { 0 }
114 @       $obj    \&foo
115 @       $obj    \&fatal
116 @       $obj    FALSE
117 @       $obj    \&FALSE
118 !       $obj    undef
119
120 # CODE ref against argument
121 #  - arg is code ref
122         \&foo           \&foo
123 !       \&foo           sub {}
124 !       \&foo           sub { "$_[0]" =~ /^CODE/ }
125 !       \&foo           \&bar
126         \&fatal         \&fatal
127 !       \&foo           \&fatal
128
129 # - arg is not code ref
130         1       sub{shift}
131 !       0       sub{shift}
132 !       undef   sub{shift}
133         undef   sub{not shift}
134         FALSE   sub{not shift}
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
151
152 # - null-prototyped subs
153         a_const         "a constant"
154         a_const         a_const
155         a_const         b_const
156         \&a_const       \&a_const
157 !       \&a_const       \&b_const
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
172
173 # - non-null-prototyped subs
174 !       \&bar           \&gorch
175         bar             gorch
176 @       fatal           bar
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]
195         { foo => 1 }    ["foo"]
196         { foo => 1 }    ["foo", "bar"]
197         \%hash          ["foo", "bar"]
198         \%hash          ["foo"]
199 !       \%hash          ["quux"]
200         \%hash          [qw(foo quux)]
201
202 #  - a regex
203         {foo => 1}      qr/^(fo[ox])$/
204 !       +{0..99}        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
213         []                      []
214 !       []                      [1]
215         [["foo"], ["bar"]]      [qr/o/, qr/a/]
216         ["foo", "bar"]          [qr/o/, qr/a/]
217 !       ["foo", "bar"]          [qr/o/, "foo"]
218         $deep1                  $deep1
219 !       $deep1                  $deep2
220
221         \@nums                  \@tied_nums
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
229         [qw(foo 2)]             2
230         [qw(foo 2)]             2.0_0e+0
231 !       [qw(1foo bar2)]         2
232
233 # - a string
234 !       [qw(1foo 2bar)]         "2"
235         [qw(1foo 2bar)]         "2bar"
236
237 # Number against number
238         2               2
239 !       2               3
240         0               FALSE
241         3-2             TRUE
242
243 # Number against string
244         2               "2"
245         2               "2.0"
246 !       2               "2bananas"
247 !       2_3             "2_3"
248         FALSE           "0"
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/
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, '' }
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
294         undef           undef
295         $::undef        undef