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
|
describe "vararg", ->
it "should pass varargs to function", ->
sum = (...) ->
total = 0
for i = 1, select("#", ...)
if type(select(i, ...)) == "number"
total += select(i, ...)
total
result = sum 1, 2, 3, 4, 5
assert.same result, 15
it "should handle empty varargs", ->
fn = (...) -> select "#", ...
result = fn!
assert.same result, 0
it "should spread varargs in function call", ->
receiver = (a, b, c) -> {a, b, c}
source = -> 1, 2, 3
result = receiver source!
assert.same result, {1, 2, 3}
it "should use varargs in table", ->
fn = (...) -> {...}
result = fn 1, 2, 3
assert.same result, {1, 2, 3}
it "should forward varargs", ->
middle = (fn, ...) -> fn(...)
inner = (a, b, c) -> a + b + c
result = middle inner, 1, 2, 3
assert.same result, 6
it "should count varargs with select", ->
fn = (...) -> select "#", ...
assert.same fn(1, 2, 3), 3
assert.same fn("a", "b"), 2
assert.same fn!, 0
it "should select from varargs", ->
fn = (...) -> select 2, ...
result = fn 1, 2, 3
assert.same result, 2
it "should work with named parameters and varargs", ->
fn = (first, ...) ->
{first, select("#", ...)}
result = fn "first", "second", "third"
assert.same result, {"first", 2}
it "should handle nil in varargs", ->
fn = (...) ->
count = select "#", ...
has_nil = false
for i = 1, count
has_nil = true if select(i, ...) == nil
{count, has_nil}
result = fn 1, nil, 3
assert.same result, {3, true}
it "should work with table unpack", ->
fn = (...) -> {...}
result = fn table.unpack {1, 2, 3}
assert.same result, {1, 2, 3}
it "should work with varargs in comprehension", ->
fn = (...) -> [x * 2 for x in *{...}]
result = fn 1, 2, 3, 4, 5
assert.same result, {2, 4, 6, 8, 10}
|