2013-06-20 16:21:46 +00:00
|
|
|
|
2015-12-07 02:06:06 +00:00
|
|
|
import unpack from require "moonscript.util"
|
|
|
|
|
2013-06-20 16:21:46 +00:00
|
|
|
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}
|
|
|
|
|
2013-07-01 03:13:12 +00:00
|
|
|
output = input
|
|
|
|
output = [i * 2 for _,i in ipairs input]
|
|
|
|
|
|
|
|
assert.same input, {1,2,3,4}
|
|
|
|
assert.same output, {2,4,6,8}
|
|
|
|
|
2013-06-22 01:47:46 +00:00
|
|
|
|
2013-07-01 04:05:52 +00:00
|
|
|
describe "table comprehension", ->
|
2013-06-22 01:47:46 +00:00
|
|
|
it "should copy table", ->
|
|
|
|
input = { 1,2,3, hello: "world", thing: true }
|
|
|
|
output = {k,v for k,v in pairs input }
|
|
|
|
|
|
|
|
assert.is_true input != output
|
|
|
|
assert.same input, output
|
2013-07-01 04:05:52 +00:00
|
|
|
|
|
|
|
it "should support when", ->
|
|
|
|
input = {
|
|
|
|
color: "red"
|
|
|
|
name: "fast"
|
|
|
|
width: 123
|
|
|
|
}
|
|
|
|
|
|
|
|
output = { k,v for k,v in pairs input when k != "color" }
|
|
|
|
|
|
|
|
assert.same output, { name: "fast", width: 123 }
|
|
|
|
|
|
|
|
it "should do unpack", ->
|
|
|
|
input = {4,9,16,25}
|
|
|
|
output = {i, math.sqrt i for i in *input}
|
|
|
|
|
|
|
|
assert.same output, { [4]: 2, [9]: 3, [16]: 4, [25]: 5 }
|
|
|
|
|
|
|
|
it "should use multiple return values", ->
|
|
|
|
input = { {"hello", "world"}, {"foo", "bar"} }
|
|
|
|
output = { unpack tuple for tuple in *input }
|
|
|
|
|
|
|
|
assert.same output, { foo: "bar", hello: "world" }
|
|
|
|
|