aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/named_varargs_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/outputs/test/named_varargs_spec.lua')
-rw-r--r--spec/outputs/test/named_varargs_spec.lua246
1 files changed, 246 insertions, 0 deletions
diff --git a/spec/outputs/test/named_varargs_spec.lua b/spec/outputs/test/named_varargs_spec.lua
new file mode 100644
index 0000000..2a71cea
--- /dev/null
+++ b/spec/outputs/test/named_varargs_spec.lua
@@ -0,0 +1,246 @@
1return describe("named varargs", function()
2 it("should store varargs in named table", function()
3 local f
4 f = function(...)
5 local t = {
6 n = select("#", ...),
7 ...
8 }
9 assert.same(t.n, 3)
10 assert.same(t[1], 1)
11 assert.same(t[2], 2)
12 return assert.same(t[3], 3)
13 end
14 return f(1, 2, 3)
15 end)
16 it("should handle string arguments", function()
17 local f
18 f = function(...)
19 local args = {
20 n = select("#", ...),
21 ...
22 }
23 assert.same(args.n, 3)
24 assert.same(args[1], "a")
25 assert.same(args[2], "b")
26 return assert.same(args[3], "c")
27 end
28 return f("a", "b", "c")
29 end)
30 it("should handle empty varargs", function()
31 local f
32 f = function(...)
33 local t = {
34 n = select("#", ...),
35 ...
36 }
37 assert.same(t.n, 0)
38 return assert.same(#t, 0)
39 end
40 return f()
41 end)
42 it("should preserve nil values", function()
43 local f
44 f = function(...)
45 local args = {
46 n = select("#", ...),
47 ...
48 }
49 assert.same(args.n, 5)
50 assert.same(args[1], 1)
51 assert.same(args[2], nil)
52 assert.same(args[3], 3)
53 assert.same(args[4], nil)
54 return assert.same(args[5], 5)
55 end
56 return f(1, nil, 3, nil, 5)
57 end)
58 it("should work with loop", function()
59 local f
60 f = function(...)
61 local t = {
62 n = select("#", ...),
63 ...
64 }
65 local sum = 0
66 for i = 1, t.n do
67 if type(t[i]) == "number" then
68 sum = sum + t[i]
69 end
70 end
71 return sum
72 end
73 local result = f(1, 2, 3, 4, 5)
74 return assert.same(result, 15)
75 end)
76 it("should handle mixed types", function()
77 local f
78 f = function(...)
79 local args = {
80 n = select("#", ...),
81 ...
82 }
83 local types
84 do
85 local _accum_0 = { }
86 local _len_0 = 1
87 for i = 1, args.n do
88 _accum_0[_len_0] = type(args[i])
89 _len_0 = _len_0 + 1
90 end
91 types = _accum_0
92 end
93 return types
94 end
95 local result = f("string", 123, true, nil, { })
96 return assert.same(result, {
97 "string",
98 "number",
99 "boolean",
100 "nil",
101 "table"
102 })
103 end)
104 it("should work with table access", function()
105 local f
106 f = function(...)
107 local t = {
108 n = select("#", ...),
109 ...
110 }
111 local first = t[1]
112 local last = t[t.n]
113 return {
114 first,
115 last
116 }
117 end
118 local result = f(1, 2, 3, 4, 5)
119 return assert.same(result, {
120 1,
121 5
122 })
123 end)
124 it("should support select with named args", function()
125 local f
126 f = function(...)
127 local args = {
128 n = select("#", ...),
129 ...
130 }
131 local second = select(2, table.unpack(args))
132 return second
133 end
134 local result = f("a", "b", "c")
135 return assert.same(result, "b")
136 end)
137 it("should work with pcall", function()
138 local f
139 f = function(...)
140 local t = {
141 n = select("#", ...),
142 ...
143 }
144 local success = true
145 for i = 1, t.n do
146 if t[i] == nil then
147 success = false
148 end
149 end
150 return success
151 end
152 local result = f(1, nil, 3)
153 return assert.is_false(result)
154 end)
155 it("should handle function results", function()
156 local g
157 g = function()
158 return 1, 2, 3
159 end
160 local f
161 f = function(...)
162 local t = {
163 n = select("#", ...),
164 ...
165 }
166 return t.n
167 end
168 local result = f(g())
169 return assert.same(result, 3)
170 end)
171 it("should work with unpacking", function()
172 local f
173 f = function(...)
174 local args = {
175 n = select("#", ...),
176 ...
177 }
178 return {
179 table.unpack(args)
180 }
181 end
182 local result = f("a", "b", "c")
183 return assert.same(result, {
184 "a",
185 "b",
186 "c"
187 })
188 end)
189 it("should support passing named varargs to another function", function()
190 local outer
191 outer = function(...)
192 local t = {
193 n = select("#", ...),
194 ...
195 }
196 return inner((table.unpack(t)))
197 end
198 local inner
199 inner = function(a, b, c)
200 return {
201 a,
202 b,
203 c
204 }
205 end
206 local result = outer(1, 2, 3)
207 return assert.same(result, {
208 1,
209 2,
210 3
211 })
212 end)
213 it("should work with default parameter", function()
214 local f
215 f = function(x, ...)
216 if x == nil then
217 x = 10
218 end
219 local t = {
220 n = select("#", ...),
221 ...
222 }
223 return x + t[1] or 0
224 end
225 local result = f(5, 15)
226 return assert.same(result, 20)
227 end)
228 return it("should handle single argument", function()
229 local f
230 f = function(...)
231 local t = {
232 n = select("#", ...),
233 ...
234 }
235 return {
236 t.n,
237 t[1]
238 }
239 end
240 local result = f(42)
241 return assert.same(result, {
242 1,
243 42
244 })
245 end)
246end)