diff options
Diffstat (limited to 'spec/inputs/test/vararg_spec.yue')
| -rw-r--r-- | spec/inputs/test/vararg_spec.yue | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/spec/inputs/test/vararg_spec.yue b/spec/inputs/test/vararg_spec.yue new file mode 100644 index 0000000..4d2557f --- /dev/null +++ b/spec/inputs/test/vararg_spec.yue | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | describe "vararg", -> | ||
| 2 | it "should pass varargs to function", -> | ||
| 3 | sum = (...) -> | ||
| 4 | total = 0 | ||
| 5 | for i = 1, select("#", ...) | ||
| 6 | if type(select(i, ...)) == "number" | ||
| 7 | total += select(i, ...) | ||
| 8 | total | ||
| 9 | result = sum 1, 2, 3, 4, 5 | ||
| 10 | assert.same result, 15 | ||
| 11 | |||
| 12 | it "should handle empty varargs", -> | ||
| 13 | fn = (...) -> select "#", ... | ||
| 14 | result = fn! | ||
| 15 | assert.same result, 0 | ||
| 16 | |||
| 17 | it "should spread varargs in function call", -> | ||
| 18 | receiver = (a, b, c) -> {a, b, c} | ||
| 19 | source = -> 1, 2, 3 | ||
| 20 | result = receiver source! | ||
| 21 | assert.same result, {1, 2, 3} | ||
| 22 | |||
| 23 | it "should use varargs in table", -> | ||
| 24 | fn = (...) -> {...} | ||
| 25 | result = fn 1, 2, 3 | ||
| 26 | assert.same result, {1, 2, 3} | ||
| 27 | |||
| 28 | it "should forward varargs", -> | ||
| 29 | middle = (fn, ...) -> fn(...) | ||
| 30 | inner = (a, b, c) -> a + b + c | ||
| 31 | result = middle inner, 1, 2, 3 | ||
| 32 | assert.same result, 6 | ||
| 33 | |||
| 34 | it "should count varargs with select", -> | ||
| 35 | fn = (...) -> select "#", ... | ||
| 36 | assert.same fn(1, 2, 3), 3 | ||
| 37 | assert.same fn("a", "b"), 2 | ||
| 38 | assert.same fn!, 0 | ||
| 39 | |||
| 40 | it "should select from varargs", -> | ||
| 41 | fn = (...) -> select 2, ... | ||
| 42 | result = fn 1, 2, 3 | ||
| 43 | assert.same result, 2 | ||
| 44 | |||
| 45 | it "should work with named parameters and varargs", -> | ||
| 46 | fn = (first, ...) -> | ||
| 47 | {first, select("#", ...)} | ||
| 48 | result = fn "first", "second", "third" | ||
| 49 | assert.same result, {"first", 2} | ||
| 50 | |||
| 51 | it "should handle nil in varargs", -> | ||
| 52 | fn = (...) -> | ||
| 53 | count = select "#", ... | ||
| 54 | has_nil = false | ||
| 55 | for i = 1, count | ||
| 56 | has_nil = true if select(i, ...) == nil | ||
| 57 | {count, has_nil} | ||
| 58 | result = fn 1, nil, 3 | ||
| 59 | assert.same result, {3, true} | ||
| 60 | |||
| 61 | it "should work with table unpack", -> | ||
| 62 | fn = (...) -> {...} | ||
| 63 | result = fn table.unpack {1, 2, 3} | ||
| 64 | assert.same result, {1, 2, 3} | ||
| 65 | |||
| 66 | it "should work with varargs in comprehension", -> | ||
| 67 | fn = (...) -> [x * 2 for x in *{...}] | ||
| 68 | result = fn 1, 2, 3, 4, 5 | ||
| 69 | assert.same result, {2, 4, 6, 8, 10} | ||
