$object ~~ undef should always test for definedness
[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 {
37     package Test::Object::OverloadCodeRef;
38     sub new { bless $_[1] }
39     use overload '~~' => sub { shift->($_[1]) };
40 }
41
42 our $ov_obj = Test::Object::CopyOverload->new;
43 our $obj = Test::Object::NoOverload->new;
44 our $false_obj = Test::Object::OverloadCodeRef->new(sub { 0 });
45 our $true_obj = Test::Object::OverloadCodeRef->new(sub { 1 });
46
47
48 # Load and run the tests
49 my @tests = map [chomp and split /\t+/, $_, 3], grep !/^#/ && /\S/, <DATA>;
50 plan tests => 2 * @tests;
51
52 for my $test (@tests) {
53     my ($yn, $left, $right) = @$test;
54
55     match_test($yn, $left, $right);
56     match_test($yn, $right, $left);
57 }
58
59 sub match_test {
60     my ($yn, $left, $right) = @_;
61
62     die "Bad test spec: ($yn, $left, $right)"
63         unless $yn eq "" || $yn eq "!" || $yn eq '@';
64     
65     my $tstr = "$left ~~ $right";
66     
67     my $res;
68     $res = eval $tstr // "";    #/ <- fix syntax colouring
69
70     chomp $@;
71
72     if ( $yn eq '@' ) {
73         ok( $@ ne '', sprintf "%s%s: %s", $tstr, $@ ? ( ', $@', $@ ) : ( '', $res ) );
74     } else {
75         if ( $@ ne '' ) {
76             fail("$tstr, \$\@: $@");
77         } else {
78             ok( ($yn eq '!' xor $res), "$tstr: $res");
79         }
80     }
81 }
82
83
84
85 sub foo {}
86 sub bar {2}
87 sub gorch {2}
88 sub fatal {die "fatal sub\n"}
89
90 sub a_const() {die "const\n" if @_; "a constant"}
91 sub b_const() {die "const\n" if @_; "a constant"}
92
93 # Prefix character :
94 #   - expected to match
95 # ! - expected to not match
96 # @ - expected to be a compilation failure
97 __DATA__
98 # OBJECT
99 # - overloaded
100         $ov_obj         "key"
101         $ov_obj         {"key" => 1}
102 !       $ov_obj         "foo"
103 !       $ov_obj         \&foo
104 @       $ov_obj         \&fatal
105 !       $ov_obj         undef
106
107 # regular object
108 @       $obj    "key"
109 @       $obj    {"key" => 1}
110 @       $obj    "foo"
111 @       $obj    $obj
112 @       $obj    sub { 1 }
113 @       $obj    sub { 0 }
114 @       $obj    \&foo
115 @       $obj    \&fatal
116 !       $obj    undef
117
118 # CODE ref against argument
119 #  - arg is code ref
120         \&foo           \&foo
121 !       \&foo           sub {}
122 !       \&foo           \&bar
123         \&fatal         \&fatal
124 !       \&foo           \&fatal
125
126 # - arg is not code ref
127         1       sub{shift}
128 !       0       sub{shift}
129 !       undef   sub{shift}
130         undef   sub{not shift}
131         1       sub{scalar @_}
132         []      \&bar
133         {}      \&bar
134         qr//    \&bar
135 !       []      \&foo
136 !       {}      \&foo
137 !       qr//    \&foo
138 !       undef   \&foo
139         undef   \&bar
140 @       undef   \&fatal
141 @       1       \&fatal
142 @       []      \&fatal
143 @       "foo"   \&fatal
144 @       qr//    \&fatal
145 @       $obj    \&bar
146         $ov_obj \&bar
147
148 # - null-prototyped subs
149         a_const         "a constant"
150         a_const         a_const
151         a_const         b_const
152         \&a_const       \&a_const
153 !       \&a_const       \&b_const
154
155 # - non-null-prototyped subs
156 !       \&bar           \&gorch
157         bar             gorch
158 @       fatal           bar
159
160 # HASH ref against:
161 #   - another hash ref
162         {}              {}
163 !       {}              {1 => 2}
164         {1 => 2}        {1 => 2}
165         {1 => 2}        {1 => 3}
166 !       {1 => 2}        {2 => 3}
167         \%main::        {map {$_ => 'x'} keys %main::}
168
169 #  - tied hash ref
170         \%hash          \%tied_hash
171         \%tied_hash     \%tied_hash
172
173 #  - an array ref
174         \%::            [keys %main::]
175 !       \%::            []
176         {"" => 1}       [undef]
177         { foo => 1 }    ["foo"]
178         { foo => 1 }    ["foo", "bar"]
179         \%hash          ["foo", "bar"]
180         \%hash          ["foo"]
181 !       \%hash          ["quux"]
182         \%hash          [qw(foo quux)]
183
184 #  - a regex
185         {foo => 1}      qr/^(fo[ox])$/
186 !       +{0..100}       qr/[13579]$/
187
188 #  - a string
189         +{foo => 1, bar => 2}   "foo"
190 !       +{foo => 1, bar => 2}   "baz"
191
192
193 # ARRAY ref against:
194 #  - another array ref
195         []                      []
196 !       []                      [1]
197         [["foo"], ["bar"]]      [qr/o/, qr/a/]
198         ["foo", "bar"]          [qr/o/, qr/a/]
199 !       ["foo", "bar"]          [qr/o/, "foo"]
200         $deep1                  $deep1
201 !       $deep1                  $deep2
202
203         \@nums                  \@tied_nums
204
205 #  - a regex
206         [qw(foo bar baz quux)]  qr/x/
207 !       [qw(foo bar baz quux)]  qr/y/
208
209 # - a number
210         [qw(1foo 2bar)]         2
211         [qw(foo 2)]             2
212         [qw(foo 2)]             2.0_0e+0
213 !       [qw(1foo bar2)]         2
214
215 # - a string
216 !       [qw(1foo 2bar)]         "2"
217         [qw(1foo 2bar)]         "2bar"
218
219 # Number against number
220         2               2
221 !       2               3
222
223 # Number against string
224         2               "2"
225         2               "2.0"
226 !       2               "2bananas"
227 !       2_3             "2_3"
228
229 # Regex against string
230         qr/x/           "x"
231 !       qr/y/           "x"
232
233 # Regex against number
234         12345           qr/3/
235
236
237 # Test the implicit referencing
238         @nums           7
239         @nums           \@nums
240 !       @nums           \\@nums
241         @nums           [1..10]
242 !       @nums           [0..9]
243
244         %hash           "foo"
245         %hash           /bar/
246         %hash           [qw(bar)]
247 !       %hash           [qw(a b c)]
248         %hash           %hash
249         %hash           {%hash}
250         %hash           %tied_hash
251         %tied_hash      %tied_hash
252         %hash           { foo => 5, bar => 10 }
253 !       %hash           { foo => 5, bar => 10, quux => 15 }
254
255         @nums           {  1, '',  2, '' }
256         @nums           {  1, '', 12, '' }
257 !       @nums           { 11, '', 12, '' }
258
259 # UNDEF
260 !       3               undef
261 !       1               undef
262 !       []              undef
263 !       {}              undef
264 !       \%::main        undef
265 !       [1,2]           undef
266 !       %hash           undef
267 !       @nums           undef
268 !       "foo"           undef
269 !       ""              undef
270 !       !1              undef
271 !       \&foo           undef
272 !       sub { }         undef
273         undef           undef
274         $::undef        undef