Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 505 lines (341 sloc) 9.937 kb
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
1 # Comprehensions
2 # --------------
3
fb20197 @michaelficarra test reorganization waypoint #2
michaelficarra authored
4 # * Array Comprehensions
5 # * Range Comprehensions
6 # * Object Comprehensions
6421c86 @michaelficarra finished reorganizing test suite
michaelficarra authored
7 # * Implicit Destructuring Assignment
fb20197 @michaelficarra test reorganization waypoint #2
michaelficarra authored
8 # * Comprehensions with Nonstandard Step
9
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
10 # TODO: refactor comprehension tests
11
b994e23 @jashkenas test-ified comprehensions.coffee
authored
12 test "Basic array comprehensions.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
13
b994e23 @jashkenas test-ified comprehensions.coffee
authored
14 nums = (n * n for n in [1, 2, 3] when n & 1)
15 results = (n * 2 for n in nums)
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
16
b994e23 @jashkenas test-ified comprehensions.coffee
authored
17 ok results.join(',') is '2,18'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
18
19
b994e23 @jashkenas test-ified comprehensions.coffee
authored
20 test "Basic object comprehensions.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
21
b994e23 @jashkenas test-ified comprehensions.coffee
authored
22 obj = {one: 1, two: 2, three: 3}
23 names = (prop + '!' for prop of obj)
24 odds = (prop + '!' for prop, value of obj when value & 1)
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
25
b994e23 @jashkenas test-ified comprehensions.coffee
authored
26 ok names.join(' ') is "one! two! three!"
27 ok odds.join(' ') is "one! three!"
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
28
29
b994e23 @jashkenas test-ified comprehensions.coffee
authored
30 test "Basic range comprehensions.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
31
b994e23 @jashkenas test-ified comprehensions.coffee
authored
32 nums = (i * 3 for i in [1..3])
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
33
b994e23 @jashkenas test-ified comprehensions.coffee
authored
34 negs = (x for x in [-20..-5*2])
35 negs = negs[0..2]
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
36
b994e23 @jashkenas test-ified comprehensions.coffee
authored
37 result = nums.concat(negs).join(', ')
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
38
b994e23 @jashkenas test-ified comprehensions.coffee
authored
39 ok result is '3, 6, 9, -20, -19, -18'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
40
41
b994e23 @jashkenas test-ified comprehensions.coffee
authored
42 test "With range comprehensions, you can loop in steps.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
43
b994e23 @jashkenas test-ified comprehensions.coffee
authored
44 results = (x for x in [0...15] by 5)
45 ok results.join(' ') is '0 5 10'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
46
b994e23 @jashkenas test-ified comprehensions.coffee
authored
47 results = (x for x in [0..100] by 10)
48 ok results.join(' ') is '0 10 20 30 40 50 60 70 80 90 100'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
49
50
b994e23 @jashkenas test-ified comprehensions.coffee
authored
51 test "And can loop downwards, with a negative step.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
52
b994e23 @jashkenas test-ified comprehensions.coffee
authored
53 results = (x for x in [5..1])
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
54
b994e23 @jashkenas test-ified comprehensions.coffee
authored
55 ok results.join(' ') is '5 4 3 2 1'
56 ok results.join(' ') is [(10-5)..(-2+3)].join(' ')
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
57
b994e23 @jashkenas test-ified comprehensions.coffee
authored
58 results = (x for x in [10..1])
59 ok results.join(' ') is [10..1].join(' ')
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
60
b994e23 @jashkenas test-ified comprehensions.coffee
authored
61 results = (x for x in [10...0] by -2)
62 ok results.join(' ') is [10, 8, 6, 4, 2].join(' ')
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
63
64
b994e23 @jashkenas test-ified comprehensions.coffee
authored
65 test "Range comprehension gymnastics.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
66
b994e23 @jashkenas test-ified comprehensions.coffee
authored
67 eq "#{i for i in [5..1]}", '5,4,3,2,1'
68 eq "#{i for i in [5..-5] by -5}", '5,0,-5'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
69
b994e23 @jashkenas test-ified comprehensions.coffee
authored
70 a = 6
71 b = 0
72 c = -2
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
73
b994e23 @jashkenas test-ified comprehensions.coffee
authored
74 eq "#{i for i in [a..b]}", '6,5,4,3,2,1,0'
75 eq "#{i for i in [a..b] by c}", '6,4,2,0'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
76
77
b994e23 @jashkenas test-ified comprehensions.coffee
authored
78 test "Multiline array comprehension with filter.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
79
b994e23 @jashkenas test-ified comprehensions.coffee
authored
80 evens = for num in [1, 2, 3, 4, 5, 6] when not (num & 1)
81 num *= -1
82 num -= 2
83 num * -1
84 eq evens + '', '4,6,8'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
85
86
b994e23 @jashkenas test-ified comprehensions.coffee
authored
87 test "The in operator still works, standalone.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
88
b994e23 @jashkenas test-ified comprehensions.coffee
authored
89 ok 2 of evens
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
90
91
b994e23 @jashkenas test-ified comprehensions.coffee
authored
92 test "all isn't reserved.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
93
b994e23 @jashkenas test-ified comprehensions.coffee
authored
94 all = 1
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
95
96
b994e23 @jashkenas test-ified comprehensions.coffee
authored
97 test "Ensure that the closure wrapper preserves local variables.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
98
b994e23 @jashkenas test-ified comprehensions.coffee
authored
99 obj = {}
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
100
b994e23 @jashkenas test-ified comprehensions.coffee
authored
101 for method in ['one', 'two', 'three'] then do (method) ->
102 obj[method] = ->
103 "I'm " + method
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
104
b994e23 @jashkenas test-ified comprehensions.coffee
authored
105 ok obj.one() is "I'm one"
106 ok obj.two() is "I'm two"
107 ok obj.three() is "I'm three"
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
108
109
b994e23 @jashkenas test-ified comprehensions.coffee
authored
110 test "Index values at the end of a loop.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
111
b994e23 @jashkenas test-ified comprehensions.coffee
authored
112 i = 0
113 for i in [1..3]
114 -> 'func'
115 break if false
116 ok i is 4
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
117
118
b994e23 @jashkenas test-ified comprehensions.coffee
authored
119 test "Ensure that local variables are closed over for range comprehensions.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
120
b994e23 @jashkenas test-ified comprehensions.coffee
authored
121 funcs = for i in [1..3]
122 do (i) ->
123 -> -i
124
125 eq (func() for func in funcs).join(' '), '-1 -2 -3'
126 ok i is 4
127
128
129 test "Even when referenced in the filter.", ->
130
131 list = ['one', 'two', 'three']
132
133 methods = for num, i in list when num isnt 'two' and i isnt 1
134 do (num, i) ->
135 -> num + ' ' + i
136
137 ok methods.length is 2
138 ok methods[0]() is 'one 0'
139 ok methods[1]() is 'three 2'
140
141
142 test "Even a convoluted one.", ->
143
144 funcs = []
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
145
b994e23 @jashkenas test-ified comprehensions.coffee
authored
146 for i in [1..3]
147 do (i) ->
148 x = i * 2
149 ((z)->
150 funcs.push -> z + ' ' + i
151 )(x)
152
153 ok (func() for func in funcs).join(', ') is '2 1, 4 2, 6 3'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
154
b994e23 @jashkenas test-ified comprehensions.coffee
authored
155 funcs = []
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
156
b994e23 @jashkenas test-ified comprehensions.coffee
authored
157 results = for i in [1..3]
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
158 do (i) ->
b994e23 @jashkenas test-ified comprehensions.coffee
authored
159 z = (x * 3 for x in [1..i])
160 ((a, b, c) -> [a, b, c].join(' ')).apply this, z
161
162 ok results.join(', ') is '3 , 3 6 , 3 6 9'
163
164
165 test "Naked ranges are expanded into arrays.", ->
166
167 array = [0..10]
168 ok(num % 2 is 0 for num in array by 2)
169
170
171 test "Nested shared scopes.", ->
172
173 foo = ->
174 for i in [0..7]
175 do (i) ->
176 for j in [0..7]
177 do (j) ->
178 -> i + j
179
180 eq foo()[3][4](), 7
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
181
182
b994e23 @jashkenas test-ified comprehensions.coffee
authored
183 test "Scoped loop pattern matching.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
184
b994e23 @jashkenas test-ified comprehensions.coffee
authored
185 a = [[0], [1]]
186 funcs = []
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
187
b994e23 @jashkenas test-ified comprehensions.coffee
authored
188 for [v] in a
189 do (v) ->
190 funcs.push -> v
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
191
b994e23 @jashkenas test-ified comprehensions.coffee
authored
192 eq funcs[0](), 0
193 eq funcs[1](), 1
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
194
195
b994e23 @jashkenas test-ified comprehensions.coffee
authored
196 test "Nested comprehensions.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
197
b994e23 @jashkenas test-ified comprehensions.coffee
authored
198 multiLiner =
199 for x in [3..5]
200 for y in [3..5]
201 [x, y]
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
202
b994e23 @jashkenas test-ified comprehensions.coffee
authored
203 singleLiner =
204 (([x, y] for y in [3..5]) for x in [3..5])
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
205
b994e23 @jashkenas test-ified comprehensions.coffee
authored
206 ok multiLiner.length is singleLiner.length
207 ok 5 is multiLiner[2][2][1]
208 ok 5 is singleLiner[2][2][1]
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
209
210
b994e23 @jashkenas test-ified comprehensions.coffee
authored
211 test "Comprehensions within parentheses.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
212
b994e23 @jashkenas test-ified comprehensions.coffee
authored
213 result = null
214 store = (obj) -> result = obj
215 store (x * 2 for x in [3, 2, 1])
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
216
b994e23 @jashkenas test-ified comprehensions.coffee
authored
217 ok result.join(' ') is '6 4 2'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
218
219
b994e23 @jashkenas test-ified comprehensions.coffee
authored
220 test "Closure-wrapped comprehensions that refer to the 'arguments' object.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
221
b994e23 @jashkenas test-ified comprehensions.coffee
authored
222 expr = ->
223 result = (item * item for item in arguments)
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
224
b994e23 @jashkenas test-ified comprehensions.coffee
authored
225 ok expr(2, 4, 8).join(' ') is '4 16 64'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
226
227
b994e23 @jashkenas test-ified comprehensions.coffee
authored
228 test "Fast object comprehensions over all properties, including prototypal ones.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
229
b994e23 @jashkenas test-ified comprehensions.coffee
authored
230 class Cat
231 constructor: -> @name = 'Whiskers'
232 breed: 'tabby'
233 hair: 'cream'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
234
b994e23 @jashkenas test-ified comprehensions.coffee
authored
235 whiskers = new Cat
236 own = (value for own key, value of whiskers)
237 all = (value for key, value of whiskers)
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
238
b994e23 @jashkenas test-ified comprehensions.coffee
authored
239 ok own.join(' ') is 'Whiskers'
240 ok all.sort().join(' ') is 'Whiskers cream tabby'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
241
242
b994e23 @jashkenas test-ified comprehensions.coffee
authored
243 test "Optimized range comprehensions.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
244
b994e23 @jashkenas test-ified comprehensions.coffee
authored
245 exxes = ('x' for [0...10])
246 ok exxes.join(' ') is 'x x x x x x x x x x'
e433098 @jashkenas Adding a test for #2273
authored
247
248
249 test "Loop variables should be able to reference outer variables", ->
250 outer = 1
251 do ->
252 null for outer in [1, 2, 3]
253 eq outer, 3
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
254
255
b994e23 @jashkenas test-ified comprehensions.coffee
authored
256 test "Lenient on pure statements not trying to reach out of the closure", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
257
b994e23 @jashkenas test-ified comprehensions.coffee
authored
258 val = for i in [1]
259 for j in [] then break
260 i
261 ok val[0] is i
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
262
263
b994e23 @jashkenas test-ified comprehensions.coffee
authored
264 test "Comprehensions only wrap their last line in a closure, allowing other lines
265 to have pure expressions in them.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
266
b994e23 @jashkenas test-ified comprehensions.coffee
authored
267 func = -> for i in [1]
268 break if i is 2
269 j for j in [1]
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
270
b994e23 @jashkenas test-ified comprehensions.coffee
authored
271 ok func()[0][0] is 1
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
272
b994e23 @jashkenas test-ified comprehensions.coffee
authored
273 i = 6
274 odds = while i--
275 continue unless i & 1
276 i
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
277
b994e23 @jashkenas test-ified comprehensions.coffee
authored
278 ok odds.join(', ') is '5, 3, 1'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
279
280
b994e23 @jashkenas test-ified comprehensions.coffee
authored
281 test "Issue #897: Ensure that plucked function variables aren't leaked.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
282
b994e23 @jashkenas test-ified comprehensions.coffee
authored
283 facets = {}
284 list = ['one', 'two']
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
285
b994e23 @jashkenas test-ified comprehensions.coffee
authored
286 (->
287 for entity in list
288 facets[entity] = -> entity
289 )()
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
290
b994e23 @jashkenas test-ified comprehensions.coffee
authored
291 eq typeof entity, 'undefined'
292 eq facets['two'](), 'two'
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
293
294
b994e23 @jashkenas test-ified comprehensions.coffee
authored
295 test "Issue #905. Soaks as the for loop subject.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
296
b994e23 @jashkenas test-ified comprehensions.coffee
authored
297 a = {b: {c: [1, 2, 3]}}
298 for d in a.b?.c
299 e = d
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
300
b994e23 @jashkenas test-ified comprehensions.coffee
authored
301 eq e, 3
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
302
303
b994e23 @jashkenas test-ified comprehensions.coffee
authored
304 test "Issue #948. Capturing loop variables.", ->
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
305
b994e23 @jashkenas test-ified comprehensions.coffee
authored
306 funcs = []
307 list = ->
308 [1, 2, 3]
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
309
b994e23 @jashkenas test-ified comprehensions.coffee
authored
310 for y in list()
311 do (y) ->
312 z = y
313 funcs.push -> "y is #{y} and z is #{z}"
dcfdd14 @michaelficarra test reorganization waypoint
michaelficarra authored
314
b994e23 @jashkenas test-ified comprehensions.coffee
authored
315 eq funcs[1](), "y is 2 and z is 2"
316
317
318 test "Cancel the comprehension if there's a jump inside the loop.", ->
319
320 result = try
321 for i in [0...10]
322 continue if i < 5
323 i
324
325 eq result, 10
326
327
328 test "Comprehensions over break.", ->
329
330 arrayEq (break for [1..10]), []
331
332
333 test "Comprehensions over continue.", ->
334
335 arrayEq (continue for [1..10]), []
336
337
338 test "Comprehensions over function literals.", ->
339
340 a = 0
341 for f in [-> a = 1]
342 do (f) ->
343 do f
344
345 eq a, 1
346
347
348 test "Comprehensions that mention arguments.", ->
349
350 list = [arguments: 10]
351 args = for f in list
352 do (f) ->
353 f.arguments
354 eq args[0], 10
ccae9ea @michaelficarra final waypoint; remaining files to be sorted:
michaelficarra authored
355
356
357 test "expression conversion under explicit returns", ->
358 nonce = {}
359 fn = ->
360 return (nonce for x in [1,2,3])
361 arrayEq [nonce,nonce,nonce], fn()
362 fn = ->
363 return [nonce for x in [1,2,3]][0]
364 arrayEq [nonce,nonce,nonce], fn()
365 fn = ->
366 return [(nonce for x in [1..3])][0]
367 arrayEq [nonce,nonce,nonce], fn()
6421c86 @michaelficarra finished reorganizing test suite
michaelficarra authored
368
369
370 test "implicit destructuring assignment in object of objects", ->
371 a={}; b={}; c={}
372 obj = {
373 a: { d: a },
374 b: { d: b }
375 c: { d: c }
376 }
377 result = ([y,z] for y, { d: z } of obj)
378 arrayEq [['a',a],['b',b],['c',c]], result
379
b994e23 @jashkenas test-ified comprehensions.coffee
authored
380
6421c86 @michaelficarra finished reorganizing test suite
michaelficarra authored
381 test "implicit destructuring assignment in array of objects", ->
382 a={}; b={}; c={}; d={}; e={}; f={}
383 arr = [
384 { a: a, b: { c: b } },
385 { a: c, b: { c: d } },
386 { a: e, b: { c: f } }
387 ]
388 result = ([y,z] for { a: y, b: { c: z } } in arr)
389 arrayEq [[a,b],[c,d],[e,f]], result
390
b994e23 @jashkenas test-ified comprehensions.coffee
authored
391
6421c86 @michaelficarra finished reorganizing test suite
michaelficarra authored
392 test "implicit destructuring assignment in array of arrays", ->
393 a={}; b={}; c={}; d={}; e={}; f={}
394 arr = [[a, [b]], [c, [d]], [e, [f]]]
395 result = ([y,z] for [y, [z]] in arr)
396 arrayEq [[a,b],[c,d],[e,f]], result
8b443b9 @thejh added a test
thejh authored
397
398 test "issue #1124: don't assign a variable in two scopes", ->
399 lista = [1, 2, 3, 4, 5]
400 listb = (_i + 1 for _i in lista)
401 arrayEq [2, 3, 4, 5, 6], listb
ac46ede @geraldalewis Fix for #1326 by value is uncached
geraldalewis authored
402
56b2b02 @michaelficarra some cleanup, renaming temporary variable from `_by` to `_step`
michaelficarra authored
403 test "#1326: `by` value is uncached", ->
ac46ede @geraldalewis Fix for #1326 by value is uncached
geraldalewis authored
404 a = [0,1,2]
405 fi = gi = hi = 0
406 f = -> ++fi
407 g = -> ++gi
408 h = -> ++hi
56b2b02 @michaelficarra some cleanup, renaming temporary variable from `_by` to `_step`
michaelficarra authored
409
ac46ede @geraldalewis Fix for #1326 by value is uncached
geraldalewis authored
410 forCompile = []
411 rangeCompileSimple = []
56b2b02 @michaelficarra some cleanup, renaming temporary variable from `_by` to `_step`
michaelficarra authored
412
ac46ede @geraldalewis Fix for #1326 by value is uncached
geraldalewis authored
413 #exercises For.compile
414 for v,i in a by f() then forCompile.push i
56b2b02 @michaelficarra some cleanup, renaming temporary variable from `_by` to `_step`
michaelficarra authored
415
ac46ede @geraldalewis Fix for #1326 by value is uncached
geraldalewis authored
416 #exercises Range.compileSimple
417 rangeCompileSimple = (i for i in [0..2] by g())
418
419 arrayEq a, forCompile
420 arrayEq a, rangeCompileSimple
421 #exercises Range.compile
56b2b02 @michaelficarra some cleanup, renaming temporary variable from `_by` to `_step`
michaelficarra authored
422 eq "#{i for i in [0..2] by h()}", '0,1,2'
ad1bc1e @satyr closes #1669; loop results are now collected in the same way as auto-`re...
satyr authored
423
424 test "#1669: break/continue should skip the result only for that branch", ->
425 ns = for n in [0..99]
426 if n > 9
427 break
428 else if n & 1
429 continue
430 else
431 n
432 eq "#{ns}", '0,2,4,6,8'
433
434 # `else undefined` is implied.
435 ns = for n in [1..9]
436 if n % 2
437 continue unless n % 5
438 n
439 eq "#{ns}", "1,,3,,,7,,9"
440
441 # Ditto.
442 ns = for n in [1..9]
443 switch
444 when n % 2
445 continue unless n % 5
446 n
447 eq "#{ns}", "1,,3,,,7,,9"
5bf8b42 @satyr nodes: added missing jump-guard in While::makeReturn, fixing #1850
satyr authored
448
449 test "#1850: inner `for` should not be expression-ized if `return`ing", ->
450 eq '3,4,5', do ->
451 for a in [1..9] then \
452 for b in [1..9]
453 c = Math.sqrt a*a + b*b
454 return String [a, b, c] unless c % 1
fc0a169 @michaelficarra fixes #1910: loop index should be mutable within a loop iteration and im...
michaelficarra authored
455
456 test "#1910: loop index should be mutable within a loop iteration and immutable between loop iterations", ->
457 n = 1
458 iterations = 0
459 arr = [0..n]
460 for v, k in arr
461 ++iterations
462 v = k = 5
463 eq 5, k
464 eq 2, k
465 eq 2, iterations
466
467 iterations = 0
468 for v in [0..n]
469 ++iterations
470 eq 2, k
471 eq 2, iterations
472
473 arr = ([v, v + 1] for v in [0..5])
474 iterations = 0
475 for own [v0, v1], k in arr when v0
476 k += 3
477 ++iterations
478 eq 6, k
479 eq 5, iterations
28a1101 @michaelficarra merging/improving @rolftimmermans fix/tests for #2007
michaelficarra authored
480
481 test "#2007: Return object literal from comprehension", ->
482 y = for x in [1, 2]
483 foo: "foo" + x
484 eq 2, y.length
485 eq "foo1", y[0].foo
486 eq "foo2", y[1].foo
487
488 x = 2
489 y = while x
490 x: --x
491 eq 2, y.length
492 eq 1, y[0].x
493 eq 0, y[1].x
4fc9a34 @jashkenas Fixes #2274 -- allow @variables as loop variables
authored
494
495 test "#2274: Allow @values as loop variables", ->
496 obj = {
497 item: null
498 method: ->
499 for @item in [1, 2, 3]
500 null
501 }
502 eq obj.item, null
503 obj.method()
504 eq obj.item, 3
Something went wrong with that request. Please try again.