aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/vararg_spec.lua
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-01-26 17:45:26 +0800
committerLi Jin <dragon-fly@qq.com>2026-01-26 17:45:56 +0800
commite02321107277a63e7dcb12ab163c9942ac101b87 (patch)
treef38c6f2fbf4ee35df4938933dbc1e645733356f4 /spec/outputs/test/vararg_spec.lua
parent5d5b657f606b5939062983b1f90c3359d542672e (diff)
downloadyuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.gz
yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.bz2
yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.zip
Updated tests.
Diffstat (limited to 'spec/outputs/test/vararg_spec.lua')
-rw-r--r--spec/outputs/test/vararg_spec.lua164
1 files changed, 164 insertions, 0 deletions
diff --git a/spec/outputs/test/vararg_spec.lua b/spec/outputs/test/vararg_spec.lua
new file mode 100644
index 0000000..67a272b
--- /dev/null
+++ b/spec/outputs/test/vararg_spec.lua
@@ -0,0 +1,164 @@
1return describe("vararg", function()
2 it("should pass varargs to function", function()
3 local sum
4 sum = function(...)
5 local total = 0
6 for i = 1, select("#", ...) do
7 if type(select(i, ...)) == "number" then
8 total = total + select(i, ...)
9 end
10 end
11 return total
12 end
13 local result = sum(1, 2, 3, 4, 5)
14 return assert.same(result, 15)
15 end)
16 it("should handle empty varargs", function()
17 local fn
18 fn = function(...)
19 return select("#", ...)
20 end
21 local result = fn()
22 return assert.same(result, 0)
23 end)
24 it("should spread varargs in function call", function()
25 local receiver
26 receiver = function(a, b, c)
27 return {
28 a,
29 b,
30 c
31 }
32 end
33 local source
34 source = function()
35 return 1, 2, 3
36 end
37 local result = receiver(source())
38 return assert.same(result, {
39 1,
40 2,
41 3
42 })
43 end)
44 it("should use varargs in table", function()
45 local fn
46 fn = function(...)
47 return {
48 ...
49 }
50 end
51 local result = fn(1, 2, 3)
52 return assert.same(result, {
53 1,
54 2,
55 3
56 })
57 end)
58 it("should forward varargs", function()
59 local middle
60 middle = function(fn, ...)
61 return fn(...)
62 end
63 local inner
64 inner = function(a, b, c)
65 return a + b + c
66 end
67 local result = middle(inner, 1, 2, 3)
68 return assert.same(result, 6)
69 end)
70 it("should count varargs with select", function()
71 local fn
72 fn = function(...)
73 return select("#", ...)
74 end
75 assert.same(fn(1, 2, 3), 3)
76 assert.same(fn("a", "b"), 2)
77 return assert.same(fn(), 0)
78 end)
79 it("should select from varargs", function()
80 local fn
81 fn = function(...)
82 return select(2, ...)
83 end
84 local result = fn(1, 2, 3)
85 return assert.same(result, 2)
86 end)
87 it("should work with named parameters and varargs", function()
88 local fn
89 fn = function(first, ...)
90 return {
91 first,
92 select("#", ...)
93 }
94 end
95 local result = fn("first", "second", "third")
96 return assert.same(result, {
97 "first",
98 2
99 })
100 end)
101 it("should handle nil in varargs", function()
102 local fn
103 fn = function(...)
104 local count = select("#", ...)
105 local has_nil = false
106 for i = 1, count do
107 if select(i, ...) == nil then
108 has_nil = true
109 end
110 end
111 return {
112 count,
113 has_nil
114 }
115 end
116 local result = fn(1, nil, 3)
117 return assert.same(result, {
118 3,
119 true
120 })
121 end)
122 it("should work with table unpack", function()
123 local fn
124 fn = function(...)
125 return {
126 ...
127 }
128 end
129 local result = fn(table.unpack({
130 1,
131 2,
132 3
133 }))
134 return assert.same(result, {
135 1,
136 2,
137 3
138 })
139 end)
140 return it("should work with varargs in comprehension", function()
141 local fn
142 fn = function(...)
143 local _accum_0 = { }
144 local _len_0 = 1
145 local _list_0 = {
146 ...
147 }
148 for _index_0 = 1, #_list_0 do
149 local x = _list_0[_index_0]
150 _accum_0[_len_0] = x * 2
151 _len_0 = _len_0 + 1
152 end
153 return _accum_0
154 end
155 local result = fn(1, 2, 3, 4, 5)
156 return assert.same(result, {
157 2,
158 4,
159 6,
160 8,
161 10
162 })
163 end)
164end)