Move the require './test.pl' to the end of t/comp/hints.t
[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 use warnings;
10 no warnings 'uninitialized';
11
12 use Tie::Array;
13 use Tie::Hash;
14 use if !$ENV{PERL_CORE_MINITEST}, "Tie::RefHash";
15
16 # Predeclare vars used in the tests:
17 my @empty;
18 my %empty;
19 my @sparse; $sparse[2] = 2;
20
21 my $deep1 = []; push @$deep1, \$deep1;
22 my $deep2 = []; push @$deep2, \$deep2;
23
24 my @nums = (1..10);
25 tie my @tied_nums, 'Tie::StdArray';
26 @tied_nums =  (1..10);
27
28 my %hash = (foo => 17, bar => 23);
29 tie my %tied_hash, 'Tie::StdHash';
30 %tied_hash = %hash;
31
32 {
33     package Test::Object::NoOverload;
34     sub new { bless { key => 1 } }
35 }
36
37 {
38     package Test::Object::StringOverload;
39     use overload '""' => sub { "object" }, fallback => 1;
40     sub new { bless { key => 1 } }
41 }
42
43 {
44     package Test::Object::WithOverload;
45     sub new { bless { key => ($_[1] // 'magic') } }
46     use overload '~~' => sub {
47         my %hash = %{ $_[0] };
48         if ($_[2]) { # arguments reversed ?
49             return $_[1] eq reverse $hash{key};
50         }
51         else {
52             return $_[1] eq $hash{key};
53         }
54     };
55     use overload '""' => sub { "stringified" };
56     use overload 'eq' => sub {"$_[0]" eq "$_[1]"};
57 }
58
59 our $ov_obj = Test::Object::WithOverload->new;
60 our $ov_obj_2 = Test::Object::WithOverload->new("object");
61 our $obj = Test::Object::NoOverload->new;
62 our $str_obj = Test::Object::StringOverload->new;
63
64 my %refh;
65 if (!$ENV{PERL_CORE_MINITEST}) {
66     tie %refh, 'Tie::RefHash';
67     $refh{$ov_obj} = 1;
68 }
69
70 my @keyandmore = qw(key and more);
71 my @fooormore = qw(foo or more);
72 my %keyandmore = map { $_ => 0 } @keyandmore;
73 my %fooormore = map { $_ => 0 } @fooormore;
74
75 # Load and run the tests
76 plan tests => 322;
77
78 while (<DATA>) {
79   SKIP: {
80     next if /^#/ || !/\S/;
81     chomp;
82     my ($yn, $left, $right, $note) = split /\t+/;
83
84     local $::TODO = $note =~ /TODO/;
85
86     die "Bad test spec: ($yn, $left, $right)" if $yn =~ /[^!@=]/;
87
88     my $tstr = "$left ~~ $right";
89
90     test_again:
91     my $res;
92     if ($note =~ /NOWARNINGS/) {
93         $res = eval "no warnings; $tstr";
94     }
95     elsif ($note =~ /MINISKIP/ && $ENV{PERL_CORE_MINITEST}) {
96         skip("Doesn't work with miniperl", $yn =~ /=/ ? 2 : 1);
97     }
98     else {
99         $res = eval $tstr;
100     }
101
102     chomp $@;
103
104     if ( $yn =~ /@/ ) {
105         ok( $@ ne '', "$tstr dies" )
106             and print "# \$\@ was: $@\n";
107     } else {
108         my $test_name = $tstr . ($yn =~ /!/ ? " does not match" : " matches");
109         if ( $@ ne '' ) {
110             fail($test_name);
111             print "# \$\@ was: $@\n";
112         } else {
113             ok( ($yn =~ /!/ xor $res), $test_name );
114         }
115     }
116
117     if ( $yn =~ s/=// ) {
118         $tstr = "$right ~~ $left";
119         goto test_again;
120     }
121   }
122 }
123
124 sub foo {}
125 sub bar {42}
126 sub gorch {42}
127 sub fatal {die "fatal sub\n"}
128
129 # to test constant folding
130 sub FALSE() { 0 }
131 sub TRUE() { 1 }
132 sub NOT_DEF() { undef }
133
134 # Prefix character :
135 #   - expected to match
136 # ! - expected to not match
137 # @ - expected to be a compilation failure
138 # = - expected to match symmetrically (runs test twice)
139 # Data types to test :
140 #   undef
141 #   Object-overloaded
142 #   Object
143 #   Coderef
144 #   Hash
145 #   Hashref
146 #   Array
147 #   Arrayref
148 #   Tied arrays and hashes
149 #   Arrays that reference themselves
150 #   Regex (// and qr//)
151 #   Range
152 #   Num
153 #   Str
154 # Other syntactic items of interest:
155 #   Constants
156 #   Values returned by a sub call
157 __DATA__
158 # Any ~~ undef
159 !       $ov_obj         undef
160 !       $obj            undef
161 !       sub {}          undef
162 !       %hash           undef
163 !       \%hash          undef
164 !       {}              undef
165 !       @nums           undef
166 !       \@nums          undef
167 !       []              undef
168 !       %tied_hash      undef
169 !       @tied_nums      undef
170 !       $deep1          undef
171 !       /foo/           undef
172 !       qr/foo/         undef
173 !       21..30          undef
174 !       189             undef
175 !       "foo"           undef
176 !       ""              undef
177 !       !1              undef
178         undef           undef
179         (my $u)         undef
180         NOT_DEF         undef
181         &NOT_DEF        undef
182
183 # Any ~~ object overloaded
184 !       \&fatal         $ov_obj
185         'cigam'         $ov_obj
186 !       'cigam on'      $ov_obj
187 !       ['cigam']       $ov_obj
188 !       ['stringified'] $ov_obj
189 !       { cigam => 1 }  $ov_obj
190 !       { stringified => 1 }    $ov_obj
191 !       $obj            $ov_obj
192 !       undef           $ov_obj
193
194 # regular object
195 @       $obj            $obj
196 @       $ov_obj         $obj
197 =@      \&fatal         $obj
198 @       \&FALSE         $obj
199 @       \&foo           $obj
200 @       sub { 1 }       $obj
201 @       sub { 0 }       $obj
202 @       %keyandmore     $obj
203 @       {"key" => 1}    $obj
204 @       @fooormore      $obj
205 @       ["key" => 1]    $obj
206 @       /key/           $obj
207 @       qr/key/         $obj
208 @       "key"           $obj
209 @       FALSE           $obj
210
211 # regular object with "" overload
212 @       $obj            $str_obj
213 =@      \&fatal         $str_obj
214 @       \&FALSE         $str_obj
215 @       \&foo           $str_obj
216 @       sub { 1 }       $str_obj
217 @       sub { 0 }       $str_obj
218 @       %keyandmore     $str_obj
219 @       {"object" => 1} $str_obj
220 @       @fooormore      $str_obj
221 @       ["object" => 1] $str_obj
222 @       /object/        $str_obj
223 @       qr/object/      $str_obj
224 @       "object"        $str_obj
225 @       FALSE           $str_obj
226 # Those will treat the $str_obj as a string because of fallback:
227 !       $ov_obj         $str_obj
228         $ov_obj_2       $str_obj
229
230 # object (overloaded or not) ~~ Any
231         $obj            qr/NoOverload/
232         $ov_obj         qr/^stringified$/
233 =       "$ov_obj"       "stringified"
234 =       "$str_obj"      "object"
235 !=      $ov_obj         "stringified"
236         $str_obj        "object"
237         $ov_obj         'magic'
238 !       $ov_obj         'not magic'
239
240 # ~~ Coderef
241         sub{0}          sub { ref $_[0] eq "CODE" }
242         %fooormore      sub { $_[0] =~ /^(foo|or|more)$/ }
243 !       %fooormore      sub { $_[0] =~ /^(foo|or|less)$/ }
244         \%fooormore     sub { $_[0] =~ /^(foo|or|more)$/ }
245 !       \%fooormore     sub { $_[0] =~ /^(foo|or|less)$/ }
246         +{%fooormore}   sub { $_[0] =~ /^(foo|or|more)$/ }
247 !       +{%fooormore}   sub { $_[0] =~ /^(foo|or|less)$/ }
248         @fooormore      sub { $_[0] =~ /^(foo|or|more)$/ }
249 !       @fooormore      sub { $_[0] =~ /^(foo|or|less)$/ }
250         \@fooormore     sub { $_[0] =~ /^(foo|or|more)$/ }
251 !       \@fooormore     sub { $_[0] =~ /^(foo|or|less)$/ }
252         [@fooormore]    sub { $_[0] =~ /^(foo|or|more)$/ }
253 !       [@fooormore]    sub { $_[0] =~ /^(foo|or|less)$/ }
254         %fooormore      sub{@_==1}
255         @fooormore      sub{@_==1}
256         "foo"           sub { $_[0] =~ /^(foo|or|more)$/ }
257 !       "more"          sub { $_[0] =~ /^(foo|or|less)$/ }
258         /fooormore/     sub{ref $_[0] eq 'Regexp'}
259         qr/fooormore/   sub{ref $_[0] eq 'Regexp'}
260         1               sub{shift}
261 !       0               sub{shift}
262 !       undef           sub{shift}
263         undef           sub{not shift}
264         NOT_DEF         sub{not shift}
265         &NOT_DEF        sub{not shift}
266         FALSE           sub{not shift}
267         [1]             \&bar
268         {a=>1}          \&bar
269         qr//            \&bar
270 !       [1]             \&foo
271 !       {a=>1}          \&foo
272         $obj            sub { ref($_[0]) =~ /NoOverload/ }
273         $ov_obj         sub { ref($_[0]) =~ /WithOverload/ }
274 # empty stuff matches, because the sub is never called:
275         []              \&foo
276         {}              \&foo
277         @empty          \&foo
278         %empty          \&foo
279 !       qr//            \&foo
280 !       undef           \&foo
281         undef           \&bar
282 @       undef           \&fatal
283 @       1               \&fatal
284 @       [1]             \&fatal
285 @       {a=>1}          \&fatal
286 @       "foo"           \&fatal
287 @       qr//            \&fatal
288 # sub is not called on empty hashes / arrays
289         []              \&fatal
290         +{}             \&fatal
291         @empty          \&fatal
292         %empty          \&fatal
293 # sub is not special on the left
294         sub {0}         qr/^CODE/
295         sub {0}         sub { ref shift eq "CODE" }
296
297 # HASH ref against:
298 #   - another hash ref
299         {}              {}
300 =!      {}              {1 => 2}
301         {1 => 2}        {1 => 2}
302         {1 => 2}        {1 => 3}
303 =!      {1 => 2}        {2 => 3}
304 =       \%main::        {map {$_ => 'x'} keys %main::}
305
306 #  - tied hash ref
307 =       \%hash          \%tied_hash
308         \%tied_hash     \%tied_hash
309 !=      {"a"=>"b"}      \%tied_hash
310 =       %hash           %tied_hash
311         %tied_hash      %tied_hash
312 !=      {"a"=>"b"}      %tied_hash
313         $ov_obj         %refh           MINISKIP
314 !       "$ov_obj"       %refh           MINISKIP
315         [$ov_obj]       %refh           MINISKIP
316 !       ["$ov_obj"]     %refh           MINISKIP
317         %refh           %refh           MINISKIP
318
319 #  - an array ref
320 #  (since this is symmetrical, tests as well hash~~array)
321 =       [keys %main::]  \%::
322 =       [qw[STDIN STDOUT]]      \%::
323 =!      []              \%::
324 =!      [""]            {}
325 =!      []              {}
326 =!      @empty          {}
327 =       [undef]         {"" => 1}
328 =       [""]            {"" => 1}
329 =       ["foo"]         { foo => 1 }
330 =       ["foo", "bar"]  { foo => 1 }
331 =       ["foo", "bar"]  \%hash
332 =       ["foo"]         \%hash
333 =!      ["quux"]        \%hash
334 =       [qw(foo quux)]  \%hash
335 =       @fooormore      { foo => 1, or => 2, more => 3 }
336 =       @fooormore      %fooormore
337 =       @fooormore      \%fooormore
338 =       \@fooormore     %fooormore
339
340 #  - a regex
341 =       qr/^(fo[ox])$/          {foo => 1}
342 =       /^(fo[ox])$/            %fooormore
343 =!      qr/[13579]$/            +{0..99}
344 =!      qr/a*/                  {}
345 =       qr/a*/                  {b=>2}
346 =       qr/B/i                  {b=>2}
347 =       /B/i                    {b=>2}
348 =!      qr/a+/                  {b=>2}
349 =       qr/^à/                 {"à"=>2}
350
351 #  - a scalar
352         "foo"           +{foo => 1, bar => 2}
353         "foo"           %fooormore
354 !       "baz"           +{foo => 1, bar => 2}
355 !       "boz"           %fooormore
356 !       1               +{foo => 1, bar => 2}
357 !       1               %fooormore
358         1               { 1 => 3 }
359         1.0             { 1 => 3 }
360 !       "1.0"           { 1 => 3 }
361 !       "1.0"           { 1.0 => 3 }
362         "1.0"           { "1.0" => 3 }
363         "à"            { "à" => "À" }
364
365 #  - undef
366 !       undef           { hop => 'zouu' }
367 !       undef           %hash
368 !       undef           +{"" => "empty key"}
369 !       undef           {}
370
371 # ARRAY ref against:
372 #  - another array ref
373         []                      []
374 =!      []                      [1]
375         [["foo"], ["bar"]]      [qr/o/, qr/a/]
376 !       [["foo"], ["bar"]]      [qr/ARRAY/, qr/ARRAY/]
377         ["foo", "bar"]          [qr/o/, qr/a/]
378 !       [qr/o/, qr/a/]          ["foo", "bar"]
379         ["foo", "bar"]          [["foo"], ["bar"]]
380 !       ["foo", "bar"]          [qr/o/, "foo"]
381         ["foo", undef, "bar"]   [qr/o/, undef, "bar"]
382 !       ["foo", undef, "bar"]   [qr/o/, "",    "bar"]
383 !       ["foo", "", "bar"]      [qr/o/, undef, "bar"]
384         $deep1                  $deep1
385         @$deep1                 @$deep1
386 !       $deep1                  $deep2
387
388 =       \@nums                  \@tied_nums
389 =       @nums                   \@tied_nums
390 =       \@nums                  @tied_nums
391 =       @nums                   @tied_nums
392
393 #  - an object
394 !       $obj            @fooormore
395         $obj            [sub{ref shift}]
396
397 #  - a regex
398 =       qr/x/           [qw(foo bar baz quux)]
399 =!      qr/y/           [qw(foo bar baz quux)]
400 =       /x/             [qw(foo bar baz quux)]
401 =!      /y/             [qw(foo bar baz quux)]
402 =       /FOO/i          @fooormore
403 =!      /bar/           @fooormore
404
405 # - a number
406         2               [qw(1.00 2.00)]
407         2               [qw(foo 2)]
408         2.0_0e+0        [qw(foo 2)]
409 !       2               [qw(1foo bar2)]
410
411 # - a string
412 !       "2"             [qw(1foo 2bar)]
413         "2bar"          [qw(1foo 2bar)]
414
415 # - undef
416         undef           [1, 2, undef, 4]
417 !       undef           [1, 2, [undef], 4]
418 !       undef           @fooormore
419         undef           @sparse
420         undef           [undef]
421 !       0               [undef]
422 !       ""              [undef]
423 !       undef           [0]
424 !       undef           [""]
425
426 # - nested arrays and ~~ distributivity
427         11              [[11]]
428 !       11              [[12]]
429         "foo"           [{foo => "bar"}]
430 !       "bar"           [{foo => "bar"}]
431
432 # Number against number
433         2               2
434         20              2_0
435 !       2               3
436         0               FALSE
437         3-2             TRUE
438 !       undef           0
439 !       (my $u)         0
440
441 # Number against string
442 =       2               "2"
443 =       2               "2.0"
444 !       2               "2bananas"
445 !=      2_3             "2_3"           NOWARNINGS
446         FALSE           "0"
447 !       undef           "0"
448 !       undef           ""
449
450 # Regex against string
451         "x"             qr/x/
452 !       "x"             qr/y/
453
454 # Regex against number
455         12345           qr/3/
456 !       12345           qr/7/
457
458 # array/hash against string
459         @fooormore      "".\@fooormore
460 !       @keyandmore     "".\@fooormore
461         %fooormore      "".\%fooormore
462 !       %keyandmore     "".\%fooormore
463
464 # Test the implicit referencing
465         7               @nums
466         @nums           \@nums
467 !       @nums           \\@nums
468         @nums           [1..10]
469 !       @nums           [0..9]
470
471         "foo"           %hash
472         /bar/           %hash
473         [qw(bar)]       %hash
474 !       [qw(a b c)]     %hash
475         %hash           %hash
476         %hash           +{%hash}
477         %hash           \%hash
478         %hash           %tied_hash
479         %tied_hash      %tied_hash
480         %hash           { foo => 5, bar => 10 }
481 !       %hash           { foo => 5, bar => 10, quux => 15 }
482
483         @nums           {  1, '',  2, '' }
484         @nums           {  1, '', 12, '' }
485 !       @nums           { 11, '', 12, '' }