Add test for change #3568 plus general cleanup.
[p5sagit/p5-mst-13.2.git] / t / pragma / constant.t
CommitLineData
54310121 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
93430cb4 5 unshift @INC, '../lib' if -d '../lib';
54310121 6}
7
8BEGIN {$^W |= 1} # Insist upon warnings
9use vars qw{ @warnings };
10BEGIN { # ...and save 'em for later
11 $SIG{'__WARN__'} = sub { push @warnings, @_ }
12}
13END { print @warnings }
14
15######################### We start with some black magic to print on failure.
16
779c5bc9 17BEGIN { $| = 1; print "1..46\n"; }
54310121 18END {print "not ok 1\n" unless $loaded;}
19use constant;
20$loaded = 1;
21#print "# Version: $constant::VERSION\n";
22print "ok 1\n";
23
24######################### End of black magic.
25
26use strict;
27
28sub test ($$;$) {
29 my($num, $bool, $diag) = @_;
30 if ($bool) {
31 print "ok $num\n";
32 return;
33 }
34 print "not ok $num\n";
35 return unless defined $diag;
36 $diag =~ s/\Z\n?/\n/; # unchomp
37 print map "# $num : $_", split m/^/m, $diag;
38}
39
40use constant PI => 4 * atan2 1, 1;
41
42test 2, substr(PI, 0, 7) eq '3.14159';
43test 3, defined PI;
44
45sub deg2rad { PI * $_[0] / 180 }
46
47my $ninety = deg2rad 90;
48
49test 4, $ninety > 1.5707;
50test 5, $ninety < 1.5708;
51
52use constant UNDEF1 => undef; # the right way
53use constant UNDEF2 => ; # the weird way
54use constant 'UNDEF3' ; # the 'short' way
55use constant EMPTY => ( ) ; # the right way for lists
56
57test 6, not defined UNDEF1;
58test 7, not defined UNDEF2;
59test 8, not defined UNDEF3;
60my @undef = UNDEF1;
61test 9, @undef == 1;
62test 10, not defined $undef[0];
63@undef = UNDEF2;
64test 11, @undef == 0;
65@undef = UNDEF3;
66test 12, @undef == 0;
67@undef = EMPTY;
68test 13, @undef == 0;
69
70use constant COUNTDOWN => scalar reverse 1, 2, 3, 4, 5;
71use constant COUNTLIST => reverse 1, 2, 3, 4, 5;
72use constant COUNTLAST => (COUNTLIST)[-1];
73
74test 14, COUNTDOWN eq '54321';
75my @cl = COUNTLIST;
76test 15, @cl == 5;
77test 16, COUNTDOWN eq join '', @cl;
78test 17, COUNTLAST == 1;
79test 18, (COUNTLIST)[1] == 4;
80
81use constant ABC => 'ABC';
82test 19, "abc${\( ABC )}abc" eq "abcABCabc";
83
9d116dd7 84use constant DEF => 'D', 'E', chr ord 'F';
54310121 85test 20, "d e f @{[ DEF ]} d e f" eq "d e f D E F d e f";
86
87use constant SINGLE => "'";
88use constant DOUBLE => '"';
89use constant BACK => '\\';
90my $tt = BACK . SINGLE . DOUBLE ;
91test 21, $tt eq q(\\'");
92
93use constant MESS => q('"'\\"'"\\);
94test 22, MESS eq q('"'\\"'"\\);
95test 23, length(MESS) == 8;
96
97use constant TRAILING => '12 cats';
98{
99 my $save_warn;
100 local $^W;
101 BEGIN { $save_warn = $^W; $^W = 0 }
102 test 24, TRAILING == 12;
103 BEGIN { $^W = $save_warn }
104}
105test 25, TRAILING eq '12 cats';
106
c1b0f331 107use constant LEADING => " \t1234";
54310121 108test 26, LEADING == 1234;
c1b0f331 109test 27, LEADING eq " \t1234";
54310121 110
111use constant ZERO1 => 0;
112use constant ZERO2 => 0.0;
113use constant ZERO3 => '0.0';
114test 28, ZERO1 eq '0';
115test 29, ZERO2 eq '0';
116test 30, ZERO3 eq '0.0';
117
118{
119 package Other;
120 use constant PI => 3.141;
121}
122
123test 31, (PI > 3.1415 and PI < 3.1416);
124test 32, Other::PI == 3.141;
125
126use constant E2BIG => $! = 7;
127test 33, E2BIG == 7;
128# This is something like "Arg list too long", but the actual message
129# text may vary, so we can't test much better than this.
130test 34, length(E2BIG) > 6;
131test 35, index(E2BIG, " ") > 0;
132
133test 36, @warnings == 0, join "\n", "unexpected warning", @warnings;
134@warnings = (); # just in case
135undef &PI;
136test 37, @warnings &&
137 ($warnings[0] =~ /Constant sub.* undefined/),
138 shift @warnings;
139
140test 38, @warnings == 0, "unexpected warning";
141test 39, $^W & 1, "Who disabled the warnings?";
779c5bc9 142
143use constant CSCALAR => \"ok 40\n";
144use constant CHASH => { foo => "ok 41\n" };
145use constant CARRAY => [ undef, "ok 42\n" ];
146use constant CPHASH => [ { foo => 1 }, "ok 43\n" ];
147use constant CCODE => sub { "ok $_[0]\n" };
148
149print ${+CSCALAR};
150print CHASH->{foo};
151print CARRAY->[1];
152print CPHASH->{foo};
153eval q{ CPHASH->{bar} };
154test 44, scalar($@ =~ /^No such array/);
155print CCODE->(45);
156eval q{ CCODE->{foo} };
157test 46, scalar($@ =~ /^Constant is not a HASH/);