summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/00_syntax/16_for_loop
blob: 36ebe39e77d4e44c2b80e0e2802052abe365a6a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
Two for-loop variants are supported: a C-style counting for loop
consisting of an initialization expression, a test condition
and a step expression and a for-in-loop variant which allows
enumerating properties of objects or items of arrays.

Additionally, ucode supports an alternative syntax suitable for
template block tags.


-- Expect stdout --
A simple counting for-loop:
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Iteration 6
Iteration 7
Iteration 8
Iteration 9

If the loop body consists of only one statement, the curly braces
may be omitted:
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Iteration 6
Iteration 7
Iteration 8
Iteration 9

Any of the init-, test- and increment expressions may be omitted.

Loop without initialization statement:
Iteration null
Iteration 1
Iteration 2

Loop without test statement:
Iteration 0
Iteration 1
Iteration 2

Loop without init-, test- or increment statement:
Iteration 1
Iteration 2
Iteration 3

For-in loop enumerating object properties:
Key: foo
Key: bar

For-in loop enumerating array items:
Item: one
Item: two
Item: three

A counting for-loop using the alternative syntax:
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Iteration 6
Iteration 7
Iteration 8
Iteration 9

A for-in loop using the alternative syntax:
Item 123
Item 456
Item 789

For-in and counting for loops may declare variables:
Iteration 0
Iteration 1
Iteration 2

Item 123
Item 456
Item 789
-- End --

-- Testcase --
A simple counting for-loop:
{%
	for (i = 0; i < 10; i++) {
		print("Iteration ");
		print(i);
		print("\n");
	}
%}

If the loop body consists of only one statement, the curly braces
may be omitted:
{%
 	for (i = 0; i < 10; i++)
		print("Iteration ", i, "\n");
%}

Any of the init-, test- and increment expressions may be omitted.

Loop without initialization statement:
{%
	for (; j < 3; j++)
		print("Iteration " + j + "\n");
%}

Loop without test statement:
{%
	for (j = 0;; j++) {
		if (j == 3)
			break;

		print("Iteration ", j, "\n");
	}
%}

Loop without init-, test- or increment statement:
{%
	for (;;) {
		if (k++ == 3)
			break;

		print("Iteration ", k, "\n");
	}
%}

For-in loop enumerating object properties:
{%
	obj = { foo: true, bar: false };
	for (key in obj)
		print("Key: ", key, "\n");
%}

For-in loop enumerating array items:
{%
	arr = [ "one", "two", "three" ];
	for (item in arr)
		print("Item: ", item, "\n");
%}

A counting for-loop using the alternative syntax:
{% for (x = 0; x < 10; x++): -%}
Iteration {{ x }}
{% endfor %}

A for-in loop using the alternative syntax:
{% for (n in [123, 456, 789]): -%}
Item {{ n }}
{% endfor %}

For-in and counting for loops may declare variables:
{% for (let i = 0; i < 3; i++): %}
Iteration {{ i }}
{% endfor %}

{% for (let n in [123, 456, 789]): %}
Item {{ n }}
{% endfor %}
-- End --


By specifying two loop variables in for-in loop expressions, keys
and values can be iterated simultaneously.

-- Expect stdout --
true
false
123
456
[ 0, true ]
[ 1, false ]
[ 2, 123 ]
[ 3, 456 ]
foo
bar
baz
qrx
[ "foo", true ]
[ "bar", false ]
[ "baz", 123 ]
[ "qrx", 456 ]
-- End --

-- Testcase --
{%
	let arr = [ true, false, 123, 456 ];
	let obj = { foo: true, bar: false, baz: 123, qrx: 456 };

	// iterating arrays with one loop variable yields the array values
	for (let x in arr)
		print(x, "\n");

	// iterating arrays with two loop variables yields the array indexes
	// and their corresponding values
	for (let x, y in arr)
		print([x, y], "\n");

	// iterating objects with one loop variable yields the object keys
	for (let x in obj)
		print(x, "\n");

	// iterating objects with two loop variables yields the object keys
	// and their corresponding values
	for (let x, y in obj)
		print([x, y], "\n");
%}
-- End --


Ensure that for-in loop expressions with more than two variables are
rejected.

-- Expect stderr --
Syntax error: Unexpected token
Expecting ';'
In line 2, byte 19:

 `    for (let x, y, z in {})`
  Near here -----------^


-- End --

-- Testcase --
{%
	for (let x, y, z in {})
		;
%}
-- End --


Ensure that assignments in for-in loop expressions are rejected.

-- Expect stderr --
Syntax error: Unexpected token
Expecting ';'
In line 2, byte 20:

 `    for (let x = 1, y in {})`
  Near here ------------^


-- End --

-- Testcase --
{%
	for (let x = 1, y in {})
		;
%}
-- End --


Ensure that too short for-in loop expressions are rejected (1/2).

-- Expect stderr --
Syntax error: Unexpected token
Expecting ';'
In line 2, byte 12:

 `    for (let x)`
  Near here ----^


-- End --

-- Testcase --
{%
	for (let x)
		;
%}
-- End --


Ensure that too short for-in loop expressions are rejected (2/2).

-- Expect stderr --
Syntax error: Unexpected token
Expecting ';'
In line 2, byte 15:

 `    for (let x, y)`
  Near here -------^


-- End --

-- Testcase --
{%
	for (let x, y)
		;
%}
-- End --