Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 252 lines (188 sloc) 5.628 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…
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";
15 dies_ok { foo = 3 }, "can't reasign 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";
23 dies_ok { $bar = 2 }, "Can't reasign to a sigiled constant";
24 }
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
25
eafb05c [t/spec] Test for RT #69740
moritz authored
26 #?rakudo skip 'RT 69740'
27 {
28 constant ($a, $b) = (3, 4);
29 is $a, 3, 'multiple constant in one declaration(1)';
30 is $b, 4, 'multiple constant in one declaration(2)';
31 dies_ok { $a = 4 }, 'and they are really constant (1)';
32 dies_ok { $b = 4 }, 'and they are really constant (2)';
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
33 }
34
eafb05c [t/spec] Test for RT #69740
moritz authored
35
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
36 {
8fd6ff7 [t/spec] Tests that constants are lexically scoped and that they can …
jnthn authored
37 {
38 constant foo2 = 42;
39 }
5b8dc6b [t] warning suppression
lwall authored
40 eval_dies_ok 'foo2 == 42', 'constants are lexically scoped';
8fd6ff7 [t/spec] Tests that constants are lexically scoped and that they can …
jnthn authored
41 }
42
43 {
44 constant foo3 = 42;
45 lives_ok { my foo3 $x = 42 }, 'constant can be used as a type constraint';
46 dies_ok { my foo3 $x = 43 }, 'constant used as a type constraint enforces';
47 dies_ok { my foo3 $x = 42; $x =43 }, 'constant used as a type constraint enforces';
48 }
49
50 {
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
51 my $ok;
52
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
53 constant $foo = 582;
54 constant $bar = $foo;
55 $ok = $bar == 582;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
56
57 ok $ok, "declaring a constant in terms of another constant works";
58 }
59
30ee71c [t/spec]: Fix up some rakudo fudging.
pmichaud authored
60 #?rakudo skip 'package-scoped constant'
efd91a9 [t/spec] constant declarator scopes like our
moritz authored
61 {
62 package ConstantTest {
63 constant yak = 'shaving';
64 }
65 is ConstantTest::yak, 'shaving', 'constant is "our"-scoped';
66 }
67
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
68 #?rakudo skip 'COMPILING'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
69 {
70 my $ok;
71
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
72 constant $foo = 8224;
73 constant $bar = COMPILING::<$foo>;
74 $ok = $bar == 8224;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
75
76 ok $ok, "declaring a constant in terms of COMPILING constant works";
77 }
78
79 {
80 my $ok;
81
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
82 constant %foo = { :a(582) };
83 constant $bar = %foo<a>;
84 $ok = $bar == 582;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
85
86 ok $ok, "declaring a constant in terms of hash constant works";
87 }
88
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
89 #?rakudo skip 'COMPILING'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
90 {
91 my $ok;
92
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
93 constant %foo = { :b(8224) };
94 constant $bar = COMPILING::<%foo><b>;
95 $ok = $bar == 8224;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
96
97 ok $ok, "declaring a constant in terms of COMPILING hash constant works";
98 }
99
100 {
101 my $ok;
102
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
103 constant @foo = 0, 582;
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
104 constant $bar = @foo[1];
105 $ok = $bar == 582;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
106
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
107 ok $ok, "declaring a constant in terms of array constant works";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
108 }
109
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
110 #?rakudo skip 'COMPILING'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
111 {
112 my $ok;
113
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
114 constant @foo = [ 1, 2, 8224 ];
115 constant $bar = COMPILING::<@foo>[2];
116 $ok = $bar == 8224;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
117
118 ok $ok, "declaring a constant in terms of COMPILING hash constant works";
119 }
120
121 {
122 my $ok;
123
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
124 constant Num baz = 42;
8fd6ff7 [t/spec] Tests that constants are lexically scoped and that they can …
jnthn authored
125 $ok = baz == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
126
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
127 ok $ok, "declaring a sigilless constant with a type specification using 'constant' works";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
128 }
129
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
130 #?rakudo skip 'unicode constant name'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
131 {
132 my $ok;
133
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
134 constant λ = 42;
135 $ok = λ == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
136
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
137 ok $ok, "declaring an Unicode constant using 'constant' works";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
138 }
139
140 # Following tests test whether the constants are actually constant.
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
141 #?pugs todo 'feature'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
142 {
143 my $ok;
144
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
145 constant grtz = 42;
146 $ok++ if grtz == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
147
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
148 try { grtz = 23 };
149 $ok++ if $!;
150 $ok++ if grtz == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
151
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
152 is $ok, 3, "a constant declared using 'constant' is actually constant (1)";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
153 }
154
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
155 #?rakudo skip 'binding'
156 #?pugs todo 'feature'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
157 {
158 my $ok;
159
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
160 constant baka = 42;
161 $ok++ if baka == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
162
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
163 try { baka := 23 };
164 $ok++ if $!;
165 $ok++ if baka == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
166
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
167 is $ok, 3, "a constant declared using 'constant' is actually constant (2)";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
168 }
169
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
170 #?pugs todo 'feature'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
171 {
172 my $ok;
173
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
174 constant wobble = 42;
175 $ok++ if wobble == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
176
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
177 try { wobble++ };
178 $ok++ if $!;
179 $ok++ if wobble == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
180
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
181 is $ok, 3, "a constant declared using 'constant' is actually constant (3)";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
182 }
183
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
184 #?rakudo skip 'binding'
185 #?pugs todo 'feature'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
186 {
187 my $ok;
188
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
189 constant wibble = 42;
190 $ok++ if wibble == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
191
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
192 try { wibble := { 23 } };
193 $ok++ if $!;
194 $ok++ if wibble == 42;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
195
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
196 is $ok, 3, "a constant declared using 'constant' is actually constant (4)";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
197 }
198
199 # L<S04/The Relationship of Blocks and Declarations/The initializing
200 # expression is evaluated at BEGIN time.>
4fc7a4c [t/spec] Fudge constant.t for Rakudo; in a couple of places tweak the…
jnthn authored
201 #?rakudo skip 'BEGIN and outer lexicals'
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
202 {
203 my $ok;
204
498a5d4 [STD] treat COMPILING:: as deferred scope
lwall authored
205 my $foo = 42;
206 BEGIN { $foo = 23 }
207 constant timecheck = $foo;
208 $ok++ if timecheck == 23;
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
209
ba8e84a [t/spec] Change more Pugs todo flags to fudge commands
kyle authored
210 #?pugs todo 'feature'
211 ok $ok, "the initializing values for constants are evaluated at compile-time";
48ec4a7 [gsoc_spectest] move constant.t in spec
Auzon authored
212 }
efd91a9 [t/spec] constant declarator scopes like our
moritz authored
213
5e5f927 [t/spec] Test for RT #64522
kyle authored
214 # RT #64522
215 {
216 constant $x = 64522;
217 dies_ok { $x += 2 }, 'dies: constant += n';
218 is $x, 64522, 'constant after += has not changed';
219
220 sub con { 64522 }
e96008a [t/spec] fudge constant.t for Rakudo
kyle authored
221 #?rakudo todo '++constant_returning_sub()'
5e5f927 [t/spec] Test for RT #64522
kyle authored
222 dies_ok { ++con }, "constant-returning sub won't increment";
223 is con, 64522, 'constant-returning sub after ++ has not changed';
224 }
225
7e6f02d [t/spec] Nope, can't assign to a constant, even if it doesn't change it.
kyle authored
226 # 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…
kyle authored
227 {
228 constant $change = 'alteration';
229
7e6f02d [t/spec] Nope, can't assign to a constant, even if it doesn't change it.
kyle authored
230 dies_ok { $change ~= '' }, 'append nothing to a constant';
231 dies_ok { $change = 'alteration' }, 'assign constant its own value';
a58193c [t/spec] Can one "modify" a constant to its original value? I assume…
kyle authored
232 my $t = $change;
7e6f02d [t/spec] Nope, can't assign to a constant, even if it doesn't change it.
kyle authored
233 dies_ok { $change = $t }, 'assign constant its own value from var';
234 dies_ok { $change = 'alter' ~ 'ation' },
235 'assign constant its own value from expression';
a58193c [t/spec] Can one "modify" a constant to its original value? I assume…
kyle authored
236
237 constant $five = 5;
238
7e6f02d [t/spec] Nope, can't assign to a constant, even if it doesn't change it.
kyle authored
239 dies_ok { $five += 0 }, 'add zero to constant number';
240 dies_ok { $five *= 1 }, 'multiply constant number by 1';
241 dies_ok { $five = 5 }, 'assign constant its own value';
a58193c [t/spec] Can one "modify" a constant to its original value? I assume…
kyle authored
242 my $faux_five = $five;
7e6f02d [t/spec] Nope, can't assign to a constant, even if it doesn't change it.
kyle authored
243 dies_ok { $five = $faux_five },
244 'assign constant its own value from variable';
245 dies_ok { $five = 2 + 3 },
246 'assign constant its own value from expression';
a58193c [t/spec] Can one "modify" a constant to its original value? I assume…
kyle authored
247 }
248
eafb05c [t/spec] Test for RT #69740
moritz authored
249 done_testing;
250
efd91a9 [t/spec] constant declarator scopes like our
moritz authored
251 # vim: ft=perl6
Something went wrong with that request. Please try again.