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