Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 352 lines (280 sloc) 7.761 kb
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
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
764328fc »
2008-09-01 [t] merged statements/grepa-and-sort-in-for.t into spec/S04-statement…
14 plan 43;
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
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'
4837029d »
2008-07-03 [spec] fudged for.t. Fails with 'Null PMC access in type()'
43 #?rakudo skip 'slice context'
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
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
bad43bc3 »
2008-07-04 [spec] added a few semicolons in for.t; fixed plan in uc.t. bacek++
55 #?rakudo skip 'parse error'
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
56 {
57 my $d;
6f92c450 »
2008-11-23 [t/spec] corrected test in for.t
58 class Int is also {
59 method some_meth_1 {
60 $d = $d ~ self
61 }
62 };
96fa2fb0 »
2008-11-23 [t/spec] corrected previous commit
63 for 0 .. 5 { .some_meth_1 };
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
64 is($d, '012345', 'for 0 .. 5 { .some_sub } works');
65 }
66
67 ## and now with parens around the range operator
68 {
69 my $e;
70 for (0 .. 5) { $e = $e ~ $_; };
71 is($e, '012345', 'for () {} works');
72 }
73
74 # ... with pointy blocks
75 {
76 my $f;
77 for (0 .. 5) -> $_ { $f = $f ~ $_; };
78 is($f, '012345', 'for () -> {} works');
79 }
80
81 # ... with implicit topic
82
83 {
84 $_ = "GLOBAL VALUE";
85 for "INNER VALUE" {
86 is( .lc, "inner value", "Implicit default topic is seen by lc()");
87 };
88 is($_,"GLOBAL VALUE","After the loop the implicit topic gets restored");
5a882b3c »
2008-07-29 [spectest] Unskip a test for Rakudo, and remove some tests for the no…
89 }
90 {
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
91 # as statement modifier
92 $_ = "GLOBAL VALUE";
93 is( .lc, "inner value", "Implicit default topic is seen by lc()" )
94 for "INNER VALUE";
95 is($_,"GLOBAL VALUE","After the loop the implicit topic gets restored");
96 }
97
98 ## and now for with 'topical' variables
99
100 # ... w/out parens
101
102 my $i;
103 for 0 .. 5 -> $topic { $i = $i ~ $topic; };
104 is($i, '012345', 'for 0 .. 5 -> $topic {} works');
105
106 # ... with parens
107
108 my $j;
109 for (0 .. 5) -> $topic { $j = $j ~ $topic; };
110 is($j, '012345', 'for () -> $topic {} works');
111
112
113 ## for with @array operator w/out parens
114
115 my @array_k = (0 .. 5);
116 my $k;
117 for @array_k { $k = $k ~ $_; };
118 is($k, '012345', 'for @array {} works');
119
120 # ... with pointy blocks
121
122 my @array_l = (0 .. 5);
123 my $l;
124 for @array_l -> $_ { $l = $l ~ $_; };
125 is($l, '012345', 'for @array -> {} works');
126
127 ## and now with parens around the @array
128
129 my @array_o = (0 .. 5);
130 my $o;
131 for (@array_o) { $o = $o ~ $_; };
132 is($o, '012345', 'for (@array) {} works');
133
134 # ... with pointy blocks
135 {
136 my @array_p = (0 .. 5);
137 my $p;
138 for (@array_p) -> $_ { $p = $p ~ $_; };
139 is($p, '012345', 'for (@array) -> {} works');
140 }
141
142 my @elems = <a b c d e>;
143
144 {
145 my @a;
146 for (@elems) {
147 push @a, $_;
148 }
149 my @e = <a b c d e>;
150 is(@a, @e, 'for (@a) { ... $_ ... } iterates all elems');
151 }
152
153 {
154 my @a;
155 for (@elems) -> $_ { push @a, $_ };
156 my @e = @elems;
157 is(@a, @e, 'for (@a)->$_ { ... $_ ... } iterates all elems' );
158 }
159
160 {
161 my @a;
162 for (@elems) { push @a, $_, $_; }
163 my @e = <a a b b c c d d e e>;
164 is(@a, @e, 'for (@a) { ... $_ ... $_ ... } iterates all elems, not just odd');
165 }
166
912dda9d »
2008-07-17 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added…
167 # "for @a -> $var" is ro by default.
168 {
169 my @a = <1 2 3 4>;
170
171 eval_dies_ok('for @a -> $elem {$elem = 5}', '-> $var is ro by default');
172
173 for @a <-> $elem {$elem++;}
174 is(@a, <2 3 4 5>, '<-> $var is rw');
175
176 for @a <-> $first, $second {$first++; $second++}
177 is(@a, <3 4 5 6>, '<-> $var, $var2 works');
178 }
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
179
180 # for with "is rw"
181 {
182 my @array_s = (0..2);
183 my @s = (1..3);
184 for @array_s { $_++ };
185 is(@array_s, @s, 'for @array { $_++ }');
186 }
187
188 {
189 my @array_t = (0..2);
190 my @t = (1..3);
191 for @array_t -> $val is rw { $val++ };
192 is(@array_t, @t, 'for @array -> $val is rw { $val++ }');
193 }
194
195 #?pugs eval 'todo'
196 {
197 my @array_v = (0..2);
198 my @v = (1..3);
199 for @array_v.values -> $val is rw { $val++ };
ea2ee24f »
2008-07-24 [STD] various bugs
200 is(@array_v, @v, 'for @array.values -> $val is rw { $val++ }');
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
201 }
202
203 #?pugs eval 'todo'
204 {
205 my @array_kv = (0..2);
206 my @kv = (1..3);
207 for @array_kv.kv -> $key, $val is rw { $val++ };
208 is(@array_kv, @kv, 'for @array.kv -> $key, $val is rw { $val++ }');
209 }
210
211 #?pugs eval 'todo'
212 {
213 my %hash_v = ( a => 1, b => 2, c => 3 );
214 my %v = ( a => 2, b => 3, c => 4 );
215 for %hash_v.values -> $val is rw { $val++ };
216 is(%hash_v, %v, 'for %hash.values -> $val is rw { $val++ }');
217 }
218
219 #?pugs eval 'todo'
220 {
221 my %hash_kv = ( a => 1, b => 2, c => 3 );
222 my %kv = ( a => 2, b => 3, c => 4 );
223 try { for %hash_kv.kv -> $key, $val is rw { $val++ }; };
83474c35 »
2008-12-12 [t/spec]: Clean up a few tests.
224 is( %hash_kv, %kv, 'for %hash.kv -> $key, $val is rw { $val++ }');
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
225 }
226
227 # .key //= ++$i for @array1;
4837029d »
2008-07-03 [spec] fudged for.t. Fails with 'Null PMC access in type()'
228 #?rakudo skip 'implicit invocant'
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
229 {
230 class TestClass is rw { has $.key; };
231 my @array1 = (TestClass.new(),TestClass.new(:key<2>));
232 my @array2 = (TestClass.new(:key<1>),TestClass.new(:key<3>));
233
234 my $i = 0;
235 try { .key //= ++$i for @array1 };
236 my $sum1 = @array1.map:{ $_.key };
237 my $sum2 = @array2.map:{ $_.key };
238 is( $sum1, $sum2, '.key //= ++$i for @array1;', :todo<bug>);
239
240 }
241
242 # .key = 1 for @array1;
4837029d »
2008-07-03 [spec] fudged for.t. Fails with 'Null PMC access in type()'
243 #?rakudo skip 'implicit invocant'
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
244 {
245 class TestClass is rw { has $.key; };
246 my @array1 = (TestClass.new(),TestClass.new(:key<2>));
247 my @array2 = (TestClass.new(:key<1>),TestClass.new(:key<1>));
248
249 try { .key = 1 for @array1 };
250 my $sum1 = @array1.map:{ $_.key };
251 my $sum2 = @array2.map:{ $_.key };
252 is($sum1, $sum2, '.key = 1 for @array1;');
253 }
254
255 # $_.key = 1 for @array1;
912dda9d »
2008-07-17 [gsoc_spectest] added tests to for.t and fudged it for rakudo. (added…
256 #?rakudo skip 'parsefail'
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
257 {
258 class TestClass is rw { has $.key; };
259 my @array1 = (TestClass.new(),TestClass.new(:key<2>));
260 my @array2 = (TestClass.new(:key<1>),TestClass.new(:key<1>));
261
4837029d »
2008-07-03 [spec] fudged for.t. Fails with 'Null PMC access in type()'
262 $_.key = 1 for @array1;
263 my $sum1 = @array1.map: { $_.key };
264 my $sum2 = @array2.map: { $_.key };
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
265 is( $sum1, $sum2, '$_.key = 1 for @array1;');
266
267 }
268
269 # rw scalars
270 #L<S04/The C<for> statement/implicit parameter to block read/write "by default">
271 {
272 my ($a, $b, $c) = 0..2;
273 try { for ($a, $b, $c) { $_++ } };
274 is( [$a,$b,$c], [1,2,3], 'for ($a,$b,$c) { $_++ }');
275
276 ($a, $b, $c) = 0..2;
277 try { for ($a, $b, $c) -> $x is rw { $x++ } };
278 is( [$a,$b,$c], [1,2,3], 'for ($a,$b,$c) -> $x is rw { $x++ }');
279 }
280
281 # list context
282
283 {
4837029d »
2008-07-03 [spec] fudged for.t. Fails with 'Null PMC access in type()'
284 my $a = '';
da463c43 »
2008-07-24 [spec] S04-statements/for.t it's neither '=~' nor '~~', it's '~='
285 for 1..3, 4..6 { $a ~= $_.WHAT };
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
286 is($a, 'IntIntIntIntIntInt', 'List context');
287
288 $a = '';
da463c43 »
2008-07-24 [spec] S04-statements/for.t it's neither '=~' nor '~~', it's '~='
289 for [1..3, 4..6] { $a ~= $_.WHAT };
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
290 is($a, 'Array', 'List context');
291
292 $a = '';
da463c43 »
2008-07-24 [spec] S04-statements/for.t it's neither '=~' nor '~~', it's '~='
293 for [1..3], [4..6] { $a ~= $_.WHAT };
89aedbe2 »
2008-07-03 [spec] moved for.t to spec/, updated POD
294 is($a, 'ArrayArray', 'List context');
295 }
033e98b2 »
2008-08-27 [t/spec] test for RT #58392, masak++
296
297 {
298 # this was a rakudo bug with mixed 'for' and recursion, which seems to
919fc4fa »
2008-11-25 [t/spec] unfudge passing rakudo test after lex branch merge (in for.t)
299 # confuse some lexical pads or the like, see RT #58392
033e98b2 »
2008-08-27 [t/spec] test for RT #58392, masak++
300 my $gather = '';
301 sub f($l) {
302 if $l <= 0 {
303 return $l;
304 }
305 $gather ~= $l;
306 for 1..3 {
307 f($l-1);
308 $gather ~= '.';
309 }
310 }
311 f(2);
312
313 is $gather, '21....1....1....', 'Can mix recursion and for';
314 }
315
764328fc »
2008-09-01 [t] merged statements/grepa-and-sort-in-for.t into spec/S04-statement…
316 # grep and sort in for - these were pugs bugs once, so let's
317 # keep them as regression tests
318
319 {
320 my @array = <1 2 3 4>;
321 my $output = '';
322
323 for (grep { 1 }, @array) -> $elem {
324 $output ~= "$elem,";
325 }
326
327 is $output, "1,2,3,4,", "grep and sort work in for";
328 }
329
330 {
331 my @array = <1 2 3 4>;
332 my $output = '';
333
334 for sort @array -> $elem {
335 $output ~= "$elem,";
336 }
337
338 is $output, "1,2,3,4,", "grep and sort work in for";
339 }
340
341 {
342 my @array = <1 2 3 4>;
959cd56f »
2008-09-01 [t/spec] initialize a variable to make rakudo happy
343 my $output = '';
764328fc »
2008-09-01 [t] merged statements/grepa-and-sort-in-for.t into spec/S04-statement…
344
345 for (grep { 1 }, sort @array) -> $elem {
346 $output ~= "$elem,";
347 }
348
349 is $output, "1,2,3,4,", "grep and sort work in for";
350 }
351
Something went wrong with that request. Please try again.