Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 333 lines (269 sloc) 7.935 kb
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
1 use v6;
2
3 use Test;
4
5 =begin description
6
7 Tests the "for" statement
8
9 This attempts to test as many variations of the
10 for statement as possible
11
12 =end description
13
912dda9 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added 3 ...
Auzon authored
14 plan 42;
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
15
16 ## No foreach
17 # L<S04/The C<for> statement/"no foreach statement any more">
18 {
19 my $times_run = 0;
20 eval_dies_ok 'foreach 1..10 { $times_run++ }; 1', "foreach is gone";
21 eval_dies_ok 'foreach (1..10) { $times_run++}; 1',
22 "foreach is gone, even with parens";
23 is $times_run, 0, "foreach doesn't work";
24 }
25
26 ## for with plain old range operator w/out parens
27
28 {
29 my $a;
30 for 0 .. 5 { $a = $a ~ $_; };
31 is($a, '012345', 'for 0..5 {} works');
32 }
33
34 # ... with pointy blocks
35
36 {
37 my $b;
38 for 0 .. 5 -> $_ { $b = $b ~ $_; };
39 is($b, '012345', 'for 0 .. 5 -> {} works');
40 }
41
42 #?pugs eval 'todo: slice context'
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
43 #?rakudo skip 'slice context'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
44 {
45 my $str;
46 my @a = 1..3;
47 my @b = 5..6;
48 for zip(@a; @b) -> $x, $y {
49 $str ~= "($x $y)";
50 }
51 is $str, "(1 5)(2 4)(3 6)", 'for zip(@a; @b) -> $x, $y works';
52 }
53
54 # ... with referential sub
bad43bc [spec] added a few semicolons in for.t; fixed plan in uc.t. bacek++
moritz authored
55 #?rakudo skip 'parse error'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
56 {
bad43bc [spec] added a few semicolons in for.t; fixed plan in uc.t. bacek++
moritz authored
57 # XXX this test is wrong. Since some_sub_1 isn't a method in class
58 # Int, the method call will never work.
59 # There are other wrong occurences of this below
60 # what to do? Maybe something like this:
61 # class Int is also { method some_meth_1 { $d = $d ~ self } }
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
62 my $d;
bad43bc [spec] added a few semicolons in for.t; fixed plan in uc.t. bacek++
moritz authored
63 sub some_sub_1 ($arg) { $d = $d ~ $arg; };
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
64 for 0 .. 5 { .some_sub_1 };
65 is($d, '012345', 'for 0 .. 5 { .some_sub } works');
66 }
67
68 ## and now with parens around the range operator
69 {
70 my $e;
71 for (0 .. 5) { $e = $e ~ $_; };
72 is($e, '012345', 'for () {} works');
73 }
74
75 # ... with pointy blocks
76 {
77 my $f;
78 for (0 .. 5) -> $_ { $f = $f ~ $_; };
79 is($f, '012345', 'for () -> {} works');
80 }
81
82 # ... with referential sub
83 #?rakudo skip 'implicit invocant'
84 {
85 my $h;
bad43bc [spec] added a few semicolons in for.t; fixed plan in uc.t. bacek++
moritz authored
86 sub some_sub_2 ($arg) { $h = $h ~ $arg; };
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
87 for (0 .. 5) { .some_sub_2 };
88 is($h, '012345', 'for (0 .. 5) { .some_sub } works');
89 }
90
91 # ... with implicit topic
92
93 #?rakudo skip 'implicit invocant'
94 {
95 $_ = "GLOBAL VALUE";
96 for "INNER VALUE" {
97 is( .lc, "inner value", "Implicit default topic is seen by lc()");
98 };
99 is($_,"GLOBAL VALUE","After the loop the implicit topic gets restored");
100
101 # as statement modifier
102 $_ = "GLOBAL VALUE";
103 is( .lc, "inner value", "Implicit default topic is seen by lc()" )
104 for "INNER VALUE";
105 is($_,"GLOBAL VALUE","After the loop the implicit topic gets restored");
106 }
107
108 ## and now for with 'topical' variables
109
110 # ... w/out parens
111
112 my $i;
113 for 0 .. 5 -> $topic { $i = $i ~ $topic; };
114 is($i, '012345', 'for 0 .. 5 -> $topic {} works');
115
116 # ... with parens
117
118 my $j;
119 for (0 .. 5) -> $topic { $j = $j ~ $topic; };
120 is($j, '012345', 'for () -> $topic {} works');
121
122
123 ## for with @array operator w/out parens
124
125 my @array_k = (0 .. 5);
126 my $k;
127 for @array_k { $k = $k ~ $_; };
128 is($k, '012345', 'for @array {} works');
129
130 # ... with pointy blocks
131
132 my @array_l = (0 .. 5);
133 my $l;
134 for @array_l -> $_ { $l = $l ~ $_; };
135 is($l, '012345', 'for @array -> {} works');
136
137 # ... with referential sub
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
138 #?rakudo skip 'implicit invocant'
139 {
140 my @array_n = (0 .. 5);
141 my $n;
142 sub some_sub_3 ($arg) { $n = $n ~ $arg; }
143 for (@array_n) { .some_sub_3 };
144 is($n, '012345', 'for @array { .some_sub } works');
145 }
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
146
147 ## and now with parens around the @array
148
149 my @array_o = (0 .. 5);
150 my $o;
151 for (@array_o) { $o = $o ~ $_; };
152 is($o, '012345', 'for (@array) {} works');
153
154 # ... with pointy blocks
155 {
156 my @array_p = (0 .. 5);
157 my $p;
158 for (@array_p) -> $_ { $p = $p ~ $_; };
159 is($p, '012345', 'for (@array) -> {} works');
160 }
161
162
163 # ... with referential sub
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
164 #?rakudo skip 'implicit invocant'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
165 {
166 my @array_r = (0 .. 5);
167 my $r;
168 sub some_sub_4 ($arg) { $r ~= $arg; }
169 for (@array_r) { .some_sub_4 };
170 is($r, '012345', 'for (@array) { .some_sub } works');
171 }
172
173 my @elems = <a b c d e>;
174
175 {
176 my @a;
177 for (@elems) {
178 push @a, $_;
179 }
180 my @e = <a b c d e>;
181 is(@a, @e, 'for (@a) { ... $_ ... } iterates all elems');
182 }
183
184 {
185 my @a;
186 for (@elems) -> $_ { push @a, $_ };
187 my @e = @elems;
188 is(@a, @e, 'for (@a)->$_ { ... $_ ... } iterates all elems' );
189 }
190
191 {
192 my @a;
193 for (@elems) { push @a, $_, $_; }
194 my @e = <a a b b c c d d e e>;
195 is(@a, @e, 'for (@a) { ... $_ ... $_ ... } iterates all elems, not just odd');
196 }
197
912dda9 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added 3 ...
Auzon authored
198 # "for @a -> $var" is ro by default.
199 {
200 my @a = <1 2 3 4>;
201
202 eval_dies_ok('for @a -> $elem {$elem = 5}', '-> $var is ro by default');
203
204 for @a <-> $elem {$elem++;}
205 is(@a, <2 3 4 5>, '<-> $var is rw');
206
207 for @a <-> $first, $second {$first++; $second++}
208 is(@a, <3 4 5 6>, '<-> $var, $var2 works');
209 }
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
210
211 # for with "is rw"
212 {
213 my @array_s = (0..2);
214 my @s = (1..3);
215 for @array_s { $_++ };
216 is(@array_s, @s, 'for @array { $_++ }');
217 }
218
219 {
220 my @array_t = (0..2);
221 my @t = (1..3);
222 for @array_t -> $val is rw { $val++ };
223 is(@array_t, @t, 'for @array -> $val is rw { $val++ }');
224 }
225
226 #?pugs eval 'todo'
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
227 #?rakudo skip 'parse error'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
228 {
229 my @array_v = (0..2);
230 my @v = (1..3);
231 for @array_v.values -> $val is rw { $val++ };
ea2ee24 [STD] various bugs
lwall authored
232 is(@array_v, @v, 'for @array.values -> $val is rw { $val++ }');
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
233 }
234
235 #?pugs eval 'todo'
236 {
237 my @array_kv = (0..2);
238 my @kv = (1..3);
239 for @array_kv.kv -> $key, $val is rw { $val++ };
240 is(@array_kv, @kv, 'for @array.kv -> $key, $val is rw { $val++ }');
241 }
242
243 #?pugs eval 'todo'
912dda9 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added 3 ...
Auzon authored
244 #?rakudo skip 'rakudo thinks that the values are strings'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
245 {
246 my %hash_v = ( a => 1, b => 2, c => 3 );
247 my %v = ( a => 2, b => 3, c => 4 );
248 for %hash_v.values -> $val is rw { $val++ };
249 is(%hash_v, %v, 'for %hash.values -> $val is rw { $val++ }');
250 }
251
252 #?pugs eval 'todo'
912dda9 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added 3 ...
Auzon authored
253 #?rakudo todo 'changing a hash'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
254 {
255 my %hash_kv = ( a => 1, b => 2, c => 3 );
256 my %kv = ( a => 2, b => 3, c => 4 );
257 try { for %hash_kv.kv -> $key, $val is rw { $val++ }; };
258 is( %hash_kv.sort, %kv.sort, 'for %hash.kv -> $key, $val is rw { $val++ }');
259 }
260
261 # .key //= ++$i for @array1;
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
262 #?rakudo skip 'implicit invocant'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
263 {
264 class TestClass is rw { has $.key; };
265 my @array1 = (TestClass.new(),TestClass.new(:key<2>));
266 my @array2 = (TestClass.new(:key<1>),TestClass.new(:key<3>));
267
268 my $i = 0;
269 try { .key //= ++$i for @array1 };
270 my $sum1 = @array1.map:{ $_.key };
271 my $sum2 = @array2.map:{ $_.key };
272 is( $sum1, $sum2, '.key //= ++$i for @array1;', :todo<bug>);
273
274 }
275
276 # .key = 1 for @array1;
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
277 #?rakudo skip 'implicit invocant'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
278 {
279 class TestClass is rw { has $.key; };
280 my @array1 = (TestClass.new(),TestClass.new(:key<2>));
281 my @array2 = (TestClass.new(:key<1>),TestClass.new(:key<1>));
282
283 try { .key = 1 for @array1 };
284 my $sum1 = @array1.map:{ $_.key };
285 my $sum2 = @array2.map:{ $_.key };
286 is($sum1, $sum2, '.key = 1 for @array1;');
287 }
288
289 # $_.key = 1 for @array1;
912dda9 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added 3 ...
Auzon authored
290 #?rakudo skip 'parsefail'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
291 {
292 class TestClass is rw { has $.key; };
293 my @array1 = (TestClass.new(),TestClass.new(:key<2>));
294 my @array2 = (TestClass.new(:key<1>),TestClass.new(:key<1>));
295
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
296 $_.key = 1 for @array1;
297 my $sum1 = @array1.map: { $_.key };
298 my $sum2 = @array2.map: { $_.key };
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
299 is( $sum1, $sum2, '$_.key = 1 for @array1;');
300
301 }
302
303 # rw scalars
304 #L<S04/The C<for> statement/implicit parameter to block read/write "by default">
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
305 #?rakudo skip 'list assignment'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
306 {
307 my ($a, $b, $c) = 0..2;
308 try { for ($a, $b, $c) { $_++ } };
309 is( [$a,$b,$c], [1,2,3], 'for ($a,$b,$c) { $_++ }');
310
311 ($a, $b, $c) = 0..2;
312 try { for ($a, $b, $c) -> $x is rw { $x++ } };
313 is( [$a,$b,$c], [1,2,3], 'for ($a,$b,$c) -> $x is rw { $x++ }');
314 }
315
316 # list context
317
318 {
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
319 my $a = '';
ea2ee24 [STD] various bugs
lwall authored
320 for 1..3, 4..6 { $a ~~ $_.WHAT };
912dda9 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added 3 ...
Auzon authored
321 #?rakudo todo 'type is incorrect'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
322 is($a, 'IntIntIntIntIntInt', 'List context');
323
324 $a = '';
ea2ee24 [STD] various bugs
lwall authored
325 for [1..3, 4..6] { $a ~~ $_.WHAT };
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
326 is($a, 'Array', 'List context');
327
328 $a = '';
ea2ee24 [STD] various bugs
lwall authored
329 for [1..3], [4..6] { $a ~~ $_.WHAT };
912dda9 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added 3 ...
Auzon authored
330 #?rakudo todo 'type is incorrect'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
331 is($a, 'ArrayArray', 'List context');
332 }
Something went wrong with that request. Please try again.