-
Notifications
You must be signed in to change notification settings - Fork 134
/
constant.t
266 lines (201 loc) 路 5.84 KB
/
constant.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use v6;
use Test;
plan *;
# L<S04/The Relationship of Blocks and Declarations/"The new constant declarator">
# Following tests test whether the declaration succeeded.
#?pugs todo 'feature'
{
constant foo = 42;
ok foo == 42, "declaring a sigilless constant using 'constant' works";
dies_ok { foo = 3 }, "can't reasign to a sigil-less constant";
}
{
my $ok;
constant $bar = 42;
ok $bar == 42, "declaring a constant with a sigil using 'constant' works";
dies_ok { $bar = 2 }, "Can't reasign to a sigiled constant";
}
#?rakudo skip 'RT 69740'
{
constant ($a, $b) = (3, 4);
is $a, 3, 'multiple constant in one declaration(1)';
is $b, 4, 'multiple constant in one declaration(2)';
dies_ok { $a = 4 }, 'and they are really constant (1)';
dies_ok { $b = 4 }, 'and they are really constant (2)';
}
{
{
constant foo2 = 42;
}
eval_dies_ok 'foo2 == 42', 'constants are lexically scoped';
}
{
constant foo3 = 42;
lives_ok { my foo3 $x = 42 }, 'constant can be used as a type constraint';
dies_ok { my foo3 $x = 43 }, 'constant used as a type constraint enforces';
dies_ok { my foo3 $x = 42; $x =43 }, 'constant used as a type constraint enforces';
}
{
my $ok;
constant $foo = 582;
constant $bar = $foo;
$ok = $bar == 582;
ok $ok, "declaring a constant in terms of another constant works";
}
#?rakudo skip 'package-scoped constant'
{
package ConstantTest {
constant yak = 'shaving';
}
is ConstantTest::yak, 'shaving', 'constant is "our"-scoped';
}
#?rakudo skip 'COMPILING'
{
my $ok;
constant $foo = 8224;
constant $bar = COMPILING::<$foo>;
$ok = $bar == 8224;
ok $ok, "declaring a constant in terms of COMPILING constant works";
}
{
my $ok;
constant %foo = { :a(582) };
constant $bar = %foo<a>;
$ok = $bar == 582;
ok $ok, "declaring a constant in terms of hash constant works";
}
#?rakudo skip 'COMPILING'
{
my $ok;
constant %foo = { :b(8224) };
constant $bar = COMPILING::<%foo><b>;
$ok = $bar == 8224;
ok $ok, "declaring a constant in terms of COMPILING hash constant works";
}
{
my $ok;
constant @foo = 0, 582;
constant $bar = @foo[1];
$ok = $bar == 582;
ok $ok, "declaring a constant in terms of array constant works";
}
#?rakudo skip 'COMPILING'
{
my $ok;
constant @foo = [ 1, 2, 8224 ];
constant $bar = COMPILING::<@foo>[2];
$ok = $bar == 8224;
ok $ok, "declaring a constant in terms of COMPILING hash constant works";
}
{
my $ok;
constant Num baz = 42;
$ok = baz == 42;
ok $ok, "declaring a sigilless constant with a type specification using 'constant' works";
}
#?rakudo skip 'unicode constant name'
{
my $ok;
constant 位 = 42;
$ok = 位 == 42;
ok $ok, "declaring an Unicode constant using 'constant' works";
}
# Following tests test whether the constants are actually constant.
#?pugs todo 'feature'
{
my $ok;
constant grtz = 42;
$ok++ if grtz == 42;
try { grtz = 23 };
$ok++ if $!;
$ok++ if grtz == 42;
is $ok, 3, "a constant declared using 'constant' is actually constant (1)";
}
#?rakudo skip 'binding'
#?pugs todo 'feature'
{
my $ok;
constant baka = 42;
$ok++ if baka == 42;
try { baka := 23 };
$ok++ if $!;
$ok++ if baka == 42;
is $ok, 3, "a constant declared using 'constant' is actually constant (2)";
}
#?pugs todo 'feature'
{
my $ok;
constant wobble = 42;
$ok++ if wobble == 42;
try { wobble++ };
$ok++ if $!;
$ok++ if wobble == 42;
is $ok, 3, "a constant declared using 'constant' is actually constant (3)";
}
#?rakudo skip 'binding'
#?pugs todo 'feature'
{
my $ok;
constant wibble = 42;
$ok++ if wibble == 42;
try { wibble := { 23 } };
$ok++ if $!;
$ok++ if wibble == 42;
is $ok, 3, "a constant declared using 'constant' is actually constant (4)";
}
# L<S04/The Relationship of Blocks and Declarations/The initializing
# expression is evaluated at BEGIN time.>
#?rakudo skip 'BEGIN and outer lexicals'
{
my $ok;
my $foo = 42;
BEGIN { $foo = 23 }
constant timecheck = $foo;
$ok++ if timecheck == 23;
#?pugs todo 'feature'
ok $ok, "the initializing values for constants are evaluated at compile-time";
}
# RT #64522
{
constant $x = 64522;
dies_ok { $x += 2 }, 'dies: constant += n';
is $x, 64522, 'constant after += has not changed';
sub con { 64522 }
#?rakudo todo '++constant_returning_sub()'
dies_ok { ++con }, "constant-returning sub won't increment";
is con, 64522, 'constant-returning sub after ++ has not changed';
}
# identities -- can't assign to constant even if it doesn't change it.
{
constant $change = 'alteration';
dies_ok { $change ~= '' }, 'append nothing to a constant';
dies_ok { $change = 'alteration' }, 'assign constant its own value';
my $t = $change;
dies_ok { $change = $t }, 'assign constant its own value from var';
dies_ok { $change = 'alter' ~ 'ation' },
'assign constant its own value from expression';
constant $five = 5;
dies_ok { $five += 0 }, 'add zero to constant number';
dies_ok { $five *= 1 }, 'multiply constant number by 1';
dies_ok { $five = 5 }, 'assign constant its own value';
my $faux_five = $five;
dies_ok { $five = $faux_five },
'assign constant its own value from variable';
dies_ok { $five = 2 + 3 },
'assign constant its own value from expression';
}
#?rakudo skip 'RT 69967'
{
constant C = 6;
class A {
constant B = 5;
has $.x = B;
has $.y = A::B;
has $.z = C;
}
is A.new.x, 5, 'Can declare and use a constant in a class';
is A.new.y, 5, 'Can declare and use a constant with FQN in a class';
is A.new.z, 6, 'Can use outer constants in a class';
}
done_testing;
# vim: ft=perl6