mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
comprehension spec
This commit is contained in:
parent
3320ba20d4
commit
f3d92e7b4c
37
spec/comprehension_spec.moon
Normal file
37
spec/comprehension_spec.moon
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
|
||||||
|
describe "comprehension", ->
|
||||||
|
it "should double every number", ->
|
||||||
|
input = {1,2,3,4,5,6}
|
||||||
|
output_1 = [i * 2 for _, i in pairs input ]
|
||||||
|
output_2 = [i * 2 for i in *input ]
|
||||||
|
|
||||||
|
assert.same output_1, {2,4,6,8,10,12}
|
||||||
|
|
||||||
|
it "should create a slice", ->
|
||||||
|
input = {1,2,3,4,5,6}
|
||||||
|
|
||||||
|
slice_1 = [i for i in *input[1,3]]
|
||||||
|
slice_2 = [i for i in *input[,3]]
|
||||||
|
|
||||||
|
slice_3 = [i for i in *input[3,]]
|
||||||
|
slice_4 = [i for i in *input[,]]
|
||||||
|
|
||||||
|
slice_5 = [i for i in *input[,,2]]
|
||||||
|
slice_6 = [i for i in *input[2,,2]]
|
||||||
|
|
||||||
|
assert.same slice_1, {1,2,3}
|
||||||
|
assert.same slice_1, slice_2
|
||||||
|
assert.same slice_3, {3,4,5,6}
|
||||||
|
assert.same slice_4, input
|
||||||
|
|
||||||
|
assert.same slice_5, {1,3,5}
|
||||||
|
assert.same slice_6, {2,4,6}
|
||||||
|
|
||||||
|
it "should be able to assign to self", ->
|
||||||
|
input = {1,2,3,4}
|
||||||
|
output = input
|
||||||
|
output = [i * 2 for i in *output]
|
||||||
|
|
||||||
|
assert.same input, {1,2,3,4}
|
||||||
|
assert.same output, {2,4,6,8}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user