If we use @{[]} (a.k.a. baby-cart) interpolation, we got warnings
[p5sagit/p5-mst-13.2.git] / lib / constant.t
CommitLineData
6515510f 1#!./perl -T
54310121 2
3BEGIN {
6515510f 4 if ($ENV{PERL_CORE}) {
5 chdir 't' if -d 't';
6 @INC = '../lib';
7 }
54310121 8}
9
9f1b1f2d 10use warnings;
69e7dc3c 11use vars qw{ @warnings $fagwoosh $putt $kloong};
54310121 12BEGIN { # ...and save 'em for later
13 $SIG{'__WARN__'} = sub { push @warnings, @_ }
14}
803b07a7 15END { print STDERR @warnings }
54310121 16
54310121 17
18use strict;
6515510f 19use Test::More tests => 97;
10a0e555 20my $TB = Test::More->builder;
21
22BEGIN { use_ok('constant'); }
54310121 23
54310121 24use constant PI => 4 * atan2 1, 1;
25
10a0e555 26ok defined PI, 'basic scalar constant';
27is substr(PI, 0, 7), '3.14159', ' in substr()';
54310121 28
29sub deg2rad { PI * $_[0] / 180 }
30
31my $ninety = deg2rad 90;
32
10a0e555 33cmp_ok abs($ninety - 1.5707), '<', 0.0001, ' in math expression';
54310121 34
35use constant UNDEF1 => undef; # the right way
36use constant UNDEF2 => ; # the weird way
37use constant 'UNDEF3' ; # the 'short' way
38use constant EMPTY => ( ) ; # the right way for lists
39
10a0e555 40is UNDEF1, undef, 'right way to declare an undef';
41is UNDEF2, undef, ' weird way';
42is UNDEF3, undef, ' short way';
43
44# XXX Why is this way different than the other ones?
54310121 45my @undef = UNDEF1;
10a0e555 46is @undef, 1;
47is $undef[0], undef;
48
54310121 49@undef = UNDEF2;
10a0e555 50is @undef, 0;
54310121 51@undef = UNDEF3;
10a0e555 52is @undef, 0;
54310121 53@undef = EMPTY;
10a0e555 54is @undef, 0;
54310121 55
56use constant COUNTDOWN => scalar reverse 1, 2, 3, 4, 5;
57use constant COUNTLIST => reverse 1, 2, 3, 4, 5;
58use constant COUNTLAST => (COUNTLIST)[-1];
59
10a0e555 60is COUNTDOWN, '54321';
54310121 61my @cl = COUNTLIST;
10a0e555 62is @cl, 5;
63is COUNTDOWN, join '', @cl;
64is COUNTLAST, 1;
65is((COUNTLIST)[1], 4);
54310121 66
67use constant ABC => 'ABC';
10a0e555 68is "abc${\( ABC )}abc", "abcABCabc";
54310121 69
9d116dd7 70use constant DEF => 'D', 'E', chr ord 'F';
10a0e555 71is "d e f @{[ DEF ]} d e f", "d e f D E F d e f";
54310121 72
73use constant SINGLE => "'";
74use constant DOUBLE => '"';
75use constant BACK => '\\';
76my $tt = BACK . SINGLE . DOUBLE ;
10a0e555 77is $tt, q(\\'");
54310121 78
79use constant MESS => q('"'\\"'"\\);
10a0e555 80is MESS, q('"'\\"'"\\);
81is length(MESS), 8;
54310121 82
83use constant TRAILING => '12 cats';
84{
6515510f 85 local $^W;
10a0e555 86 cmp_ok TRAILING, '==', 12;
54310121 87}
10a0e555 88is TRAILING, '12 cats';
54310121 89
c1b0f331 90use constant LEADING => " \t1234";
10a0e555 91cmp_ok LEADING, '==', 1234;
92is LEADING, " \t1234";
54310121 93
94use constant ZERO1 => 0;
95use constant ZERO2 => 0.0;
96use constant ZERO3 => '0.0';
10a0e555 97is ZERO1, '0';
98is ZERO2, '0';
99is ZERO3, '0.0';
54310121 100
101{
102 package Other;
103 use constant PI => 3.141;
104}
105
10a0e555 106cmp_ok(abs(PI - 3.1416), '<', 0.0001);
107is Other::PI, 3.141;
54310121 108
109use constant E2BIG => $! = 7;
10a0e555 110cmp_ok E2BIG, '==', 7;
54310121 111# This is something like "Arg list too long", but the actual message
112# text may vary, so we can't test much better than this.
10a0e555 113cmp_ok length(E2BIG), '>', 6;
54310121 114
10a0e555 115is @warnings, 0 or diag join "\n", "unexpected warning", @warnings;
54310121 116@warnings = (); # just in case
117undef &PI;
10a0e555 118ok @warnings && ($warnings[0] =~ /Constant sub.* undefined/) or
119 diag join "\n", "unexpected warning", @warnings;
120shift @warnings;
54310121 121
10a0e555 122is @warnings, 0, "unexpected warning";
779c5bc9 123
10a0e555 124my $curr_test = $TB->current_test;
125use constant CSCALAR => \"ok 37\n";
126use constant CHASH => { foo => "ok 38\n" };
127use constant CARRAY => [ undef, "ok 39\n" ];
779c5bc9 128use constant CCODE => sub { "ok $_[0]\n" };
129
6515510f 130my $output = $TB->output ;
131print $output ${+CSCALAR};
132print $output CHASH->{foo};
133print $output CARRAY->[1];
134print $output CCODE->($curr_test+4);
10a0e555 135
136$TB->current_test($curr_test+4);
137
779c5bc9 138eval q{ CCODE->{foo} };
10a0e555 139ok scalar($@ =~ /^Constant is not a HASH/);
140
83763826 141
142# Allow leading underscore
143use constant _PRIVATE => 47;
10a0e555 144is _PRIVATE, 47;
83763826 145
146# Disallow doubled leading underscore
147eval q{
148 use constant __DISALLOWED => "Oops";
149};
10a0e555 150like $@, qr/begins with '__'/;
83763826 151
152# Check on declared() and %declared. This sub should be EXACTLY the
153# same as the one quoted in the docs!
154sub declared ($) {
155 use constant 1.01; # don't omit this!
156 my $name = shift;
157 $name =~ s/^::/main::/;
158 my $pkg = caller;
159 my $full_name = $name =~ /::/ ? $name : "${pkg}::$name";
160 $constant::declared{$full_name};
161}
162
10a0e555 163ok declared 'PI';
164ok $constant::declared{'main::PI'};
83763826 165
10a0e555 166ok !declared 'PIE';
167ok !$constant::declared{'main::PIE'};
83763826 168
169{
170 package Other;
171 use constant IN_OTHER_PACK => 42;
10a0e555 172 ::ok ::declared 'IN_OTHER_PACK';
173 ::ok $constant::declared{'Other::IN_OTHER_PACK'};
174 ::ok ::declared 'main::PI';
175 ::ok $constant::declared{'main::PI'};
83763826 176}
177
10a0e555 178ok declared 'Other::IN_OTHER_PACK';
179ok $constant::declared{'Other::IN_OTHER_PACK'};
d3a7d8c7 180
181@warnings = ();
182eval q{
9f1b1f2d 183 no warnings;
6515510f 184 #local $^W if $] < 5.006;
d3a7d8c7 185 use warnings 'constant';
186 use constant 'BEGIN' => 1 ;
187 use constant 'INIT' => 1 ;
188 use constant 'CHECK' => 1 ;
6a761ace 189 use constant 'UNITCHECK' => 1;
d3a7d8c7 190 use constant 'END' => 1 ;
191 use constant 'DESTROY' => 1 ;
192 use constant 'AUTOLOAD' => 1 ;
193 use constant 'STDIN' => 1 ;
194 use constant 'STDOUT' => 1 ;
195 use constant 'STDERR' => 1 ;
196 use constant 'ARGV' => 1 ;
197 use constant 'ARGVOUT' => 1 ;
198 use constant 'ENV' => 1 ;
199 use constant 'INC' => 1 ;
200 use constant 'SIG' => 1 ;
d3a7d8c7 201};
202
10a0e555 203my @Expected_Warnings =
204 (
205 qr/^Constant name 'BEGIN' is a Perl keyword at/,
206 qr/^Constant subroutine BEGIN redefined at/,
207 qr/^Constant name 'INIT' is a Perl keyword at/,
208 qr/^Constant name 'CHECK' is a Perl keyword at/,
6a761ace 209 qr/^Constant name 'UNITCHECK' is a Perl keyword at/,
10a0e555 210 qr/^Constant name 'END' is a Perl keyword at/,
211 qr/^Constant name 'DESTROY' is a Perl keyword at/,
212 qr/^Constant name 'AUTOLOAD' is a Perl keyword at/,
213 qr/^Constant name 'STDIN' is forced into package main:: a/,
214 qr/^Constant name 'STDOUT' is forced into package main:: at/,
215 qr/^Constant name 'STDERR' is forced into package main:: at/,
216 qr/^Constant name 'ARGV' is forced into package main:: at/,
217 qr/^Constant name 'ARGVOUT' is forced into package main:: at/,
218 qr/^Constant name 'ENV' is forced into package main:: at/,
219 qr/^Constant name 'INC' is forced into package main:: at/,
220 qr/^Constant name 'SIG' is forced into package main:: at/,
221);
6515510f 222
223# when run under "make test"
224if (@warnings == 16) {
225 push @warnings, "";
226 push @Expected_Warnings, qr/^$/;
227}
228# when run directly: perl -wT -Ilib t/constant.t
229elsif (@warnings == 17) {
230 splice @Expected_Warnings, 1, 0,
231 qr/^Prototype mismatch: sub main::BEGIN \(\) vs none at/;
232}
233# when run directly under 5.6.2: perl -wT -Ilib t/constant.t
234elsif (@warnings == 15) {
235 splice @Expected_Warnings, 1, 1;
236 push @warnings, "", "";
237 push @Expected_Warnings, qr/^$/, qr/^$/;
238}
239else {
240 my $rule = " -" x 20;
241 diag "/!\\ unexpected case: ", scalar @warnings, " warnings\n$rule\n";
242 diag map { " $_" } @warnings;
243 diag $rule, $/;
244}
245
246is @warnings, 17;
247
10a0e555 248for my $idx (0..$#warnings) {
249 like $warnings[$idx], $Expected_Warnings[$idx];
250}
6515510f 251
d3a7d8c7 252@warnings = ();
c7206c54 253
254
255use constant {
256 THREE => 3,
257 FAMILY => [ qw( John Jane Sally ) ],
258 AGES => { John => 33, Jane => 28, Sally => 3 },
259 RFAM => [ [ qw( John Jane Sally ) ] ],
260 SPIT => sub { shift },
c7206c54 261};
262
10a0e555 263is @{+FAMILY}, THREE;
264is @{+FAMILY}, @{RFAM->[0]};
265is FAMILY->[2], RFAM->[0]->[2];
266is AGES->{FAMILY->[1]}, 28;
267is THREE**3, SPIT->(@{+FAMILY}**3);
5b673cda 268
269# Allow name of digits/underscores only if it begins with underscore
270{
271 use warnings FATAL => 'constant';
272 eval q{
273 use constant _1_2_3 => 'allowed';
274 };
275 ok( $@ eq '' );
276}
69e7dc3c 277
278sub slotch ();
279
280{
281 my @warnings;
282 local $SIG{'__WARN__'} = sub { push @warnings, @_ };
283 eval 'use constant slotch => 3; 1' or die $@;
284
285 is ("@warnings", "", "No warnings if a prototype exists");
286
287 my $value = eval 'slotch';
288 is ($@, '');
289 is ($value, 3);
290}
291
292sub zit;
293
294{
295 my @warnings;
296 local $SIG{'__WARN__'} = sub { push @warnings, @_ };
297 eval 'use constant zit => 4; 1' or die $@;
298
6515510f 299 # empty prototypes are reported differently in different versions
300 my $no_proto = $] < 5.008 ? "" : ": none";
301
69e7dc3c 302 is(scalar @warnings, 1, "1 warning");
6515510f 303 like ($warnings[0], qr/^Prototype mismatch: sub main::zit$no_proto vs \(\)/,
69e7dc3c 304 "about the prototype mismatch");
305
306 my $value = eval 'zit';
307 is ($@, '');
308 is ($value, 4);
309}
310
311$fagwoosh = 'geronimo';
312$putt = 'leutwein';
313$kloong = 'schlozhauer';
314
315{
316 my @warnings;
317 local $SIG{'__WARN__'} = sub { push @warnings, @_ };
318 eval 'use constant fagwoosh => 5; 1' or die $@;
319
320 is ("@warnings", "", "No warnings if the typeglob exists already");
321
322 my $value = eval 'fagwoosh';
323 is ($@, '');
324 is ($value, 5);
325
326 my @value = eval 'fagwoosh';
327 is ($@, '');
328 is_deeply (\@value, [5]);
329
330 eval 'use constant putt => 6, 7; 1' or die $@;
331
332 is ("@warnings", "", "No warnings if the typeglob exists already");
333
334 @value = eval 'putt';
335 is ($@, '');
336 is_deeply (\@value, [6, 7]);
337
338 eval 'use constant "klong"; 1' or die $@;
339
340 is ("@warnings", "", "No warnings if the typeglob exists already");
341
342 $value = eval 'klong';
343 is ($@, '');
344 is ($value, undef);
345
346 @value = eval 'klong';
347 is ($@, '');
348 is_deeply (\@value, []);
349}