Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 357 lines (285 sloc) 8.164 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
764328f [t] merged statements/grepa-and-sort-in-for.t into spec/S04-statements/f...
moritz authored
14 plan 43;
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 implicit topic
83
84 {
85 $_ = "GLOBAL VALUE";
86 for "INNER VALUE" {
87 is( .lc, "inner value", "Implicit default topic is seen by lc()");
88 };
89 is($_,"GLOBAL VALUE","After the loop the implicit topic gets restored");
5a882b3 [spectest] Unskip a test for Rakudo, and remove some tests for the now-r...
jnthn authored
90 }
91 {
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
92 # as statement modifier
93 $_ = "GLOBAL VALUE";
94 is( .lc, "inner value", "Implicit default topic is seen by lc()" )
95 for "INNER VALUE";
96 is($_,"GLOBAL VALUE","After the loop the implicit topic gets restored");
97 }
98
99 ## and now for with 'topical' variables
100
101 # ... w/out parens
102
103 my $i;
104 for 0 .. 5 -> $topic { $i = $i ~ $topic; };
105 is($i, '012345', 'for 0 .. 5 -> $topic {} works');
106
107 # ... with parens
108
109 my $j;
110 for (0 .. 5) -> $topic { $j = $j ~ $topic; };
111 is($j, '012345', 'for () -> $topic {} works');
112
113
114 ## for with @array operator w/out parens
115
116 my @array_k = (0 .. 5);
117 my $k;
118 for @array_k { $k = $k ~ $_; };
119 is($k, '012345', 'for @array {} works');
120
121 # ... with pointy blocks
122
123 my @array_l = (0 .. 5);
124 my $l;
125 for @array_l -> $_ { $l = $l ~ $_; };
126 is($l, '012345', 'for @array -> {} works');
127
128 ## and now with parens around the @array
129
130 my @array_o = (0 .. 5);
131 my $o;
132 for (@array_o) { $o = $o ~ $_; };
133 is($o, '012345', 'for (@array) {} works');
134
135 # ... with pointy blocks
136 {
137 my @array_p = (0 .. 5);
138 my $p;
139 for (@array_p) -> $_ { $p = $p ~ $_; };
140 is($p, '012345', 'for (@array) -> {} works');
141 }
142
143 my @elems = <a b c d e>;
144
145 {
146 my @a;
147 for (@elems) {
148 push @a, $_;
149 }
150 my @e = <a b c d e>;
151 is(@a, @e, 'for (@a) { ... $_ ... } iterates all elems');
152 }
153
154 {
155 my @a;
156 for (@elems) -> $_ { push @a, $_ };
157 my @e = @elems;
158 is(@a, @e, 'for (@a)->$_ { ... $_ ... } iterates all elems' );
159 }
160
161 {
162 my @a;
163 for (@elems) { push @a, $_, $_; }
164 my @e = <a a b b c c d d e e>;
165 is(@a, @e, 'for (@a) { ... $_ ... $_ ... } iterates all elems, not just odd');
166 }
167
912dda9 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added 3 ...
Auzon authored
168 # "for @a -> $var" is ro by default.
169 {
170 my @a = <1 2 3 4>;
171
172 eval_dies_ok('for @a -> $elem {$elem = 5}', '-> $var is ro by default');
173
174 for @a <-> $elem {$elem++;}
175 is(@a, <2 3 4 5>, '<-> $var is rw');
176
177 for @a <-> $first, $second {$first++; $second++}
178 is(@a, <3 4 5 6>, '<-> $var, $var2 works');
179 }
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
180
181 # for with "is rw"
182 {
183 my @array_s = (0..2);
184 my @s = (1..3);
185 for @array_s { $_++ };
186 is(@array_s, @s, 'for @array { $_++ }');
187 }
188
189 {
190 my @array_t = (0..2);
191 my @t = (1..3);
192 for @array_t -> $val is rw { $val++ };
193 is(@array_t, @t, 'for @array -> $val is rw { $val++ }');
194 }
195
196 #?pugs eval 'todo'
197 {
198 my @array_v = (0..2);
199 my @v = (1..3);
200 for @array_v.values -> $val is rw { $val++ };
ea2ee24 [STD] various bugs
lwall authored
201 is(@array_v, @v, 'for @array.values -> $val is rw { $val++ }');
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
202 }
203
204 #?pugs eval 'todo'
205 {
206 my @array_kv = (0..2);
207 my @kv = (1..3);
208 for @array_kv.kv -> $key, $val is rw { $val++ };
209 is(@array_kv, @kv, 'for @array.kv -> $key, $val is rw { $val++ }');
210 }
211
212 #?pugs eval 'todo'
912dda9 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added 3 ...
Auzon authored
213 #?rakudo skip 'rakudo thinks that the values are strings'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
214 {
215 my %hash_v = ( a => 1, b => 2, c => 3 );
216 my %v = ( a => 2, b => 3, c => 4 );
217 for %hash_v.values -> $val is rw { $val++ };
218 is(%hash_v, %v, 'for %hash.values -> $val is rw { $val++ }');
219 }
220
221 #?pugs eval 'todo'
912dda9 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added 3 ...
Auzon authored
222 #?rakudo todo 'changing a hash'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
223 {
224 my %hash_kv = ( a => 1, b => 2, c => 3 );
225 my %kv = ( a => 2, b => 3, c => 4 );
226 try { for %hash_kv.kv -> $key, $val is rw { $val++ }; };
227 is( %hash_kv.sort, %kv.sort, 'for %hash.kv -> $key, $val is rw { $val++ }');
228 }
229
230 # .key //= ++$i for @array1;
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
231 #?rakudo skip 'implicit invocant'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
232 {
233 class TestClass is rw { has $.key; };
234 my @array1 = (TestClass.new(),TestClass.new(:key<2>));
235 my @array2 = (TestClass.new(:key<1>),TestClass.new(:key<3>));
236
237 my $i = 0;
238 try { .key //= ++$i for @array1 };
239 my $sum1 = @array1.map:{ $_.key };
240 my $sum2 = @array2.map:{ $_.key };
241 is( $sum1, $sum2, '.key //= ++$i for @array1;', :todo<bug>);
242
243 }
244
245 # .key = 1 for @array1;
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
246 #?rakudo skip 'implicit invocant'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
247 {
248 class TestClass is rw { has $.key; };
249 my @array1 = (TestClass.new(),TestClass.new(:key<2>));
250 my @array2 = (TestClass.new(:key<1>),TestClass.new(:key<1>));
251
252 try { .key = 1 for @array1 };
253 my $sum1 = @array1.map:{ $_.key };
254 my $sum2 = @array2.map:{ $_.key };
255 is($sum1, $sum2, '.key = 1 for @array1;');
256 }
257
258 # $_.key = 1 for @array1;
912dda9 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added 3 ...
Auzon authored
259 #?rakudo skip 'parsefail'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
260 {
261 class TestClass is rw { has $.key; };
262 my @array1 = (TestClass.new(),TestClass.new(:key<2>));
263 my @array2 = (TestClass.new(:key<1>),TestClass.new(:key<1>));
264
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
265 $_.key = 1 for @array1;
266 my $sum1 = @array1.map: { $_.key };
267 my $sum2 = @array2.map: { $_.key };
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
268 is( $sum1, $sum2, '$_.key = 1 for @array1;');
269
270 }
271
272 # rw scalars
273 #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
274 #?rakudo skip 'list assignment'
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
275 {
276 my ($a, $b, $c) = 0..2;
277 try { for ($a, $b, $c) { $_++ } };
278 is( [$a,$b,$c], [1,2,3], 'for ($a,$b,$c) { $_++ }');
279
280 ($a, $b, $c) = 0..2;
281 try { for ($a, $b, $c) -> $x is rw { $x++ } };
282 is( [$a,$b,$c], [1,2,3], 'for ($a,$b,$c) -> $x is rw { $x++ }');
283 }
284
285 # list context
286
287 {
4837029 [spec] fudged for.t. Fails with 'Null PMC access in type()'
moritz authored
288 my $a = '';
da463c4 [spec] S04-statements/for.t it's neither '=~' nor '~~', it's '~='
moritz authored
289 for 1..3, 4..6 { $a ~= $_.WHAT };
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
290 is($a, 'IntIntIntIntIntInt', 'List context');
291
292 $a = '';
da463c4 [spec] S04-statements/for.t it's neither '=~' nor '~~', it's '~='
moritz authored
293 for [1..3, 4..6] { $a ~= $_.WHAT };
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
294 is($a, 'Array', 'List context');
295
296 $a = '';
da463c4 [spec] S04-statements/for.t it's neither '=~' nor '~~', it's '~='
moritz authored
297 for [1..3], [4..6] { $a ~= $_.WHAT };
89aedbe [spec] moved for.t to spec/, updated POD
moritz authored
298 is($a, 'ArrayArray', 'List context');
299 }
033e98b [t/spec] test for RT #58392, masak++
moritz authored
300
301 {
302 # this was a rakudo bug with mixed 'for' and recursion, which seems to
303 # confuse some lexical pads or the like.
304 my $gather = '';
305 sub f($l) {
306 if $l <= 0 {
307 return $l;
308 }
309 $gather ~= $l;
310 for 1..3 {
311 f($l-1);
312 $gather ~= '.';
313 }
314 }
315 f(2);
316
317 #?rakudo todo 'bug in for/recursion interaction, RT #58392'
318 is $gather, '21....1....1....', 'Can mix recursion and for';
319 }
320
764328f [t] merged statements/grepa-and-sort-in-for.t into spec/S04-statements/f...
moritz authored
321 # grep and sort in for - these were pugs bugs once, so let's
322 # keep them as regression tests
323
324 {
325 my @array = <1 2 3 4>;
326 my $output = '';
327
328 for (grep { 1 }, @array) -> $elem {
329 $output ~= "$elem,";
330 }
331
332 is $output, "1,2,3,4,", "grep and sort work in for";
333 }
334
335 {
336 my @array = <1 2 3 4>;
337 my $output = '';
338
339 for sort @array -> $elem {
340 $output ~= "$elem,";
341 }
342
343 is $output, "1,2,3,4,", "grep and sort work in for";
344 }
345
346 {
347 my @array = <1 2 3 4>;
348 my $output;
349
350 for (grep { 1 }, sort @array) -> $elem {
351 $output ~= "$elem,";
352 }
353
354 is $output, "1,2,3,4,", "grep and sort work in for";
355 }
356
Something went wrong with that request. Please try again.