diff options
Diffstat (limited to 'spec/inputs/test/named_varargs_spec.yue')
| -rw-r--r-- | spec/inputs/test/named_varargs_spec.yue | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/spec/inputs/test/named_varargs_spec.yue b/spec/inputs/test/named_varargs_spec.yue new file mode 100644 index 0000000..a5ab2b1 --- /dev/null +++ b/spec/inputs/test/named_varargs_spec.yue | |||
| @@ -0,0 +1,121 @@ | |||
| 1 | describe "named varargs", -> | ||
| 2 | it "should store varargs in named table", -> | ||
| 3 | f = (...t) -> | ||
| 4 | assert.same t.n, 3 | ||
| 5 | assert.same t[1], 1 | ||
| 6 | assert.same t[2], 2 | ||
| 7 | assert.same t[3], 3 | ||
| 8 | |||
| 9 | f 1, 2, 3 | ||
| 10 | |||
| 11 | it "should handle string arguments", -> | ||
| 12 | f = (...args) -> | ||
| 13 | assert.same args.n, 3 | ||
| 14 | assert.same args[1], "a" | ||
| 15 | assert.same args[2], "b" | ||
| 16 | assert.same args[3], "c" | ||
| 17 | |||
| 18 | f "a", "b", "c" | ||
| 19 | |||
| 20 | it "should handle empty varargs", -> | ||
| 21 | f = (...t) -> | ||
| 22 | assert.same t.n, 0 | ||
| 23 | assert.same #t, 0 | ||
| 24 | |||
| 25 | f! | ||
| 26 | |||
| 27 | it "should preserve nil values", -> | ||
| 28 | f = (...args) -> | ||
| 29 | assert.same args.n, 5 | ||
| 30 | assert.same args[1], 1 | ||
| 31 | assert.same args[2], nil | ||
| 32 | assert.same args[3], 3 | ||
| 33 | assert.same args[4], nil | ||
| 34 | assert.same args[5], 5 | ||
| 35 | |||
| 36 | f 1, nil, 3, nil, 5 | ||
| 37 | |||
| 38 | it "should work with loop", -> | ||
| 39 | f = (...t) -> | ||
| 40 | sum = 0 | ||
| 41 | for i = 1, t.n | ||
| 42 | sum += t[i] if type(t[i]) == "number" | ||
| 43 | sum | ||
| 44 | |||
| 45 | result = f 1, 2, 3, 4, 5 | ||
| 46 | assert.same result, 15 | ||
| 47 | |||
| 48 | it "should handle mixed types", -> | ||
| 49 | f = (...args) -> | ||
| 50 | types = [type(args[i]) for i = 1, args.n] | ||
| 51 | types | ||
| 52 | |||
| 53 | result = f "string", 123, true, nil, {} | ||
| 54 | assert.same result, {"string", "number", "boolean", "nil", "table"} | ||
| 55 | |||
| 56 | it "should work with table access", -> | ||
| 57 | f = (...t) -> | ||
| 58 | first = t[1] | ||
| 59 | last = t[t.n] | ||
| 60 | {first, last} | ||
| 61 | |||
| 62 | result = f 1, 2, 3, 4, 5 | ||
| 63 | assert.same result, {1, 5} | ||
| 64 | |||
| 65 | it "should support select with named args", -> | ||
| 66 | f = (...args) -> | ||
| 67 | second = select 2, table.unpack args | ||
| 68 | second | ||
| 69 | |||
| 70 | result = f "a", "b", "c" | ||
| 71 | assert.same result, "b" | ||
| 72 | |||
| 73 | it "should work with pcall", -> | ||
| 74 | f = (...t) -> | ||
| 75 | success = true | ||
| 76 | for i = 1, t.n | ||
| 77 | if t[i] == nil | ||
| 78 | success = false | ||
| 79 | success | ||
| 80 | |||
| 81 | result = f 1, nil, 3 | ||
| 82 | assert.is_false result | ||
| 83 | |||
| 84 | it "should handle function results", -> | ||
| 85 | g = -> 1, 2, 3 | ||
| 86 | f = (...t) -> | ||
| 87 | t.n | ||
| 88 | |||
| 89 | result = f g! | ||
| 90 | assert.same result, 3 | ||
| 91 | |||
| 92 | it "should work with unpacking", -> | ||
| 93 | f = (...args) -> | ||
| 94 | {table.unpack args} | ||
| 95 | |||
| 96 | result = f "a", "b", "c" | ||
| 97 | assert.same result, {"a", "b", "c"} | ||
| 98 | |||
| 99 | it "should support passing named varargs to another function", -> | ||
| 100 | outer = (...t) -> | ||
| 101 | inner (table.unpack t) | ||
| 102 | |||
| 103 | inner = (a, b, c) -> | ||
| 104 | {a, b, c} | ||
| 105 | |||
| 106 | result = outer 1, 2, 3 | ||
| 107 | assert.same result, {1, 2, 3} | ||
| 108 | |||
| 109 | it "should work with default parameter", -> | ||
| 110 | f = (x = 10, ...t) -> | ||
| 111 | x + t[1] or 0 | ||
| 112 | |||
| 113 | result = f 5, 15 | ||
| 114 | assert.same result, 20 | ||
| 115 | |||
| 116 | it "should handle single argument", -> | ||
| 117 | f = (...t) -> | ||
| 118 | {t.n, t[1]} | ||
| 119 | |||
| 120 | result = f 42 | ||
| 121 | assert.same result, {1, 42} | ||
