Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 279 lines (211 sloc) 6.202 kb
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
1 use v6;
2
3 use Test;
4
eafb05c [t/spec] Test for RT #69740
moritz authored
5 plan *;
efd91a9 [t/spec] constant declarator scopes like our
moritz authored
6
7 # L<S04/The Relationship of Blocks and Declarations/"The new constant declarator">
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
8
9 # Following tests test whether the declaration succeeded.
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
10 #?pugs todo 'feature'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
11 {
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
12 constant foo = 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
13
eafb05c [t/spec] Test for RT #69740
moritz authored
14 ok foo == 42, "declaring a sigilless constant using 'constant' works";
a55efad Typos
Util authored
15 dies_ok { foo = 3 }, "can't reassign to a sigil-less constant";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
16 }
17
18 {
19 my $ok;
20
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
21 constant $bar = 42;
eafb05c [t/spec] Test for RT #69740
moritz authored
22 ok $bar == 42, "declaring a constant with a sigil using 'constant' works";
a55efad Typos
Util authored
23 dies_ok { $bar = 2 }, "Can't reassign to a sigiled constant";
eafb05c [t/spec] Test for RT #69740
moritz authored
24 }
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
25
889573d [t/spec] tests for RT #69740 Defining several constants with parentheses
bbkr authored
26 # RT #69740
eafb05c [t/spec] Test for RT #69740
moritz authored
27 {
ff5acf9 [constant.t] tweaks for change of constant category to be more typish
lwall authored
28 eval_dies_ok 'constant ($a, $b) = (3, 4)', 'constant no longer takes list';
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
29 }
30
eafb05c [t/spec] Test for RT #69740
moritz authored
31
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
32 {
8fd6ff7 [t/spec] Tests that constants are lexically scoped and that they can be ...
jnthn authored
33 {
34 constant foo2 = 42;
35 }
5b8dc6b [t] warning suppression
lwall authored
36 eval_dies_ok 'foo2 == 42', 'constants are lexically scoped';
8fd6ff7 [t/spec] Tests that constants are lexically scoped and that they can be ...
jnthn authored
37 }
38
39 {
40 constant foo3 = 42;
41 lives_ok { my foo3 $x = 42 }, 'constant can be used as a type constraint';
42 dies_ok { my foo3 $x = 43 }, 'constant used as a type constraint enforces';
43 dies_ok { my foo3 $x = 42; $x =43 }, 'constant used as a type constraint enforces';
44 }
45
46 {
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
47 my $ok;
48
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
49 constant $foo = 582;
50 constant $bar = $foo;
51 $ok = $bar == 582;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
52
53 ok $ok, "declaring a constant in terms of another constant works";
54 }
55
b7b6a72 [t/spec] Label test for RT 66636: package-scoped constant
kyle authored
56 #?rakudo skip 'RT 66636: package-scoped constant'
efd91a9 [t/spec] constant declarator scopes like our
moritz authored
57 {
58 package ConstantTest {
59 constant yak = 'shaving';
60 }
61 is ConstantTest::yak, 'shaving', 'constant is "our"-scoped';
62 }
63
ff5acf9 [constant.t] tweaks for change of constant category to be more typish
lwall authored
64 #?rakudo skip 'RT 66636: package-scoped constant'
65 {
66 package ConstantTest2 {
67 our constant yak = 'shaving';
68 }
69 is ConstantTest2::yak, 'shaving', 'constant can be explicitly "our"-scoped';
70 }
71
5d7dd6c [spec/S04-declarations/constant.t] unbroke singly-quoted string w/ apost...
masak authored
72 #?rakudo skip "probably can't parse yet"
ff5acf9 [constant.t] tweaks for change of constant category to be more typish
lwall authored
73 {
74 package ConstantTest3 {
75 my constant yak = 'shaving';
76 }
77 ok !ConstantTest3::yak.defined, 'constant can be explicitly "my"-scoped';
78 }
79
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
80 #?rakudo skip 'COMPILING'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
81 {
82 my $ok;
83
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
84 constant $foo = 8224;
85 constant $bar = COMPILING::<$foo>;
86 $ok = $bar == 8224;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
87
88 ok $ok, "declaring a constant in terms of COMPILING constant works";
89 }
90
91 {
92 my $ok;
93
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
94 constant %foo = { :a(582) };
95 constant $bar = %foo<a>;
96 $ok = $bar == 582;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
97
98 ok $ok, "declaring a constant in terms of hash constant works";
99 }
100
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
101 #?rakudo skip 'COMPILING'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
102 {
103 my $ok;
104
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
105 constant %foo = { :b(8224) };
106 constant $bar = COMPILING::<%foo><b>;
107 $ok = $bar == 8224;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
108
109 ok $ok, "declaring a constant in terms of COMPILING hash constant works";
110 }
111
112 {
113 my $ok;
114
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
115 constant @foo = 0, 582;
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
116 constant $bar = @foo[1];
117 $ok = $bar == 582;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
118
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
119 ok $ok, "declaring a constant in terms of array constant works";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
120 }
121
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
122 #?rakudo skip 'COMPILING'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
123 {
124 my $ok;
125
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
126 constant @foo = [ 1, 2, 8224 ];
127 constant $bar = COMPILING::<@foo>[2];
128 $ok = $bar == 8224;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
129
130 ok $ok, "declaring a constant in terms of COMPILING hash constant works";
131 }
132
133 {
134 my $ok;
135
cdd35cf [constant.t] update to new constant declarator slot
lwall authored
136 my Num constant baz = 42;
8fd6ff7 [t/spec] Tests that constants are lexically scoped and that they can be ...
jnthn authored
137 $ok = baz == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
138
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
139 ok $ok, "declaring a sigilless constant with a type specification using 'constant' works";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
140 }
141
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
142 #?rakudo skip 'unicode constant name'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
143 {
144 my $ok;
145
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
146 constant λ = 42;
147 $ok = λ == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
148
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
149 ok $ok, "declaring an Unicode constant using 'constant' works";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
150 }
151
152 # Following tests test whether the constants are actually constant.
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
153 #?pugs todo 'feature'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
154 {
155 my $ok;
156
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
157 constant grtz = 42;
158 $ok++ if grtz == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
159
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
160 try { grtz = 23 };
161 $ok++ if $!;
162 $ok++ if grtz == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
163
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
164 is $ok, 3, "a constant declared using 'constant' is actually constant (1)";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
165 }
166
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
167 #?rakudo skip 'binding'
168 #?pugs todo 'feature'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
169 {
170 my $ok;
171
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
172 constant baka = 42;
173 $ok++ if baka == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
174
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
175 try { baka := 23 };
176 $ok++ if $!;
177 $ok++ if baka == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
178
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
179 is $ok, 3, "a constant declared using 'constant' is actually constant (2)";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
180 }
181
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
182 #?pugs todo 'feature'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
183 {
184 my $ok;
185
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
186 constant wobble = 42;
187 $ok++ if wobble == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
188
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
189 try { wobble++ };
190 $ok++ if $!;
191 $ok++ if wobble == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
192
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
193 is $ok, 3, "a constant declared using 'constant' is actually constant (3)";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
194 }
195
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
196 #?rakudo skip 'binding'
197 #?pugs todo 'feature'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
198 {
199 my $ok;
200
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
201 constant wibble = 42;
202 $ok++ if wibble == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
203
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
204 try { wibble := { 23 } };
205 $ok++ if $!;
206 $ok++ if wibble == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
207
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
208 is $ok, 3, "a constant declared using 'constant' is actually constant (4)";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
209 }
210
211 # L<S04/The Relationship of Blocks and Declarations/The initializing
212 # expression is evaluated at BEGIN time.>
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the wa...
jnthn authored
213 #?rakudo skip 'BEGIN and outer lexicals'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
214 {
215 my $ok;
216
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
217 my $foo = 42;
218 BEGIN { $foo = 23 }
219 constant timecheck = $foo;
220 $ok++ if timecheck == 23;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
221
ba8e84a [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
222 #?pugs todo 'feature'
223 ok $ok, "the initializing values for constants are evaluated at compile-time";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
224 }
efd91a9 [t/spec] constant declarator scopes like our
moritz authored
225
5e5f927 [t/spec] Test for RT #64522
kyle authored
226 # RT #64522
227 {
228 constant $x = 64522;
229 dies_ok { $x += 2 }, 'dies: constant += n';
230 is $x, 64522, 'constant after += has not changed';
231
232 sub con { 64522 }
e96008a [t/spec] fudge constant.t for Rakudo
kyle authored
233 #?rakudo todo '++constant_returning_sub()'
5e5f927 [t/spec] Test for RT #64522
kyle authored
234 dies_ok { ++con }, "constant-returning sub won't increment";
235 is con, 64522, 'constant-returning sub after ++ has not changed';
236 }
237
7e6f02d [t/spec] Nope, can't assign to a constant, even if it doesn't change it.
kyle authored
238 # identities -- can't assign to constant even if it doesn't change it.
a58193c [t/spec] Can one "modify" a constant to its original value? I assume so...
kyle authored
239 {
240 constant $change = 'alteration';
241
7e6f02d [t/spec] Nope, can't assign to a constant, even if it doesn't change it.
kyle authored
242 dies_ok { $change ~= '' }, 'append nothing to a constant';
243 dies_ok { $change = 'alteration' }, 'assign constant its own value';
a58193c [t/spec] Can one "modify" a constant to its original value? I assume so...
kyle authored
244 my $t = $change;
7e6f02d [t/spec] Nope, can't assign to a constant, even if it doesn't change it.
kyle authored
245 dies_ok { $change = $t }, 'assign constant its own value from var';
246 dies_ok { $change = 'alter' ~ 'ation' },
247 'assign constant its own value from expression';
a58193c [t/spec] Can one "modify" a constant to its original value? I assume so...
kyle authored
248
249 constant $five = 5;
250
7e6f02d [t/spec] Nope, can't assign to a constant, even if it doesn't change it.
kyle authored
251 dies_ok { $five += 0 }, 'add zero to constant number';
252 dies_ok { $five *= 1 }, 'multiply constant number by 1';
253 dies_ok { $five = 5 }, 'assign constant its own value';
a58193c [t/spec] Can one "modify" a constant to its original value? I assume so...
kyle authored
254 my $faux_five = $five;
7e6f02d [t/spec] Nope, can't assign to a constant, even if it doesn't change it.
kyle authored
255 dies_ok { $five = $faux_five },
256 'assign constant its own value from variable';
257 dies_ok { $five = 2 + 3 },
258 'assign constant its own value from expression';
a58193c [t/spec] Can one "modify" a constant to its original value? I assume so...
kyle authored
259 }
260
b2ae3d9 [t/spec] tests for RT #69967, constant declarations and classes
moritz authored
261 #?rakudo skip 'RT 69967'
262 {
263 constant C = 6;
264 class A {
265 constant B = 5;
266 has $.x = B;
267 has $.y = A::B;
268 has $.z = C;
269 }
270
271 is A.new.x, 5, 'Can declare and use a constant in a class';
272 is A.new.y, 5, 'Can declare and use a constant with FQN in a class';
273 is A.new.z, 6, 'Can use outer constants in a class';
274 }
275
eafb05c [t/spec] Test for RT #69740
moritz authored
276 done_testing;
277
efd91a9 [t/spec] constant declarator scopes like our
moritz authored
278 # vim: ft=perl6
Something went wrong with that request. Please try again.