aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/vararg_spec.lua
blob: 67a272b83c5731213a15f76698a1436b0c33a242 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
return describe("vararg", function()
	it("should pass varargs to function", function()
		local sum
		sum = function(...)
			local total = 0
			for i = 1, select("#", ...) do
				if type(select(i, ...)) == "number" then
					total = total + select(i, ...)
				end
			end
			return total
		end
		local result = sum(1, 2, 3, 4, 5)
		return assert.same(result, 15)
	end)
	it("should handle empty varargs", function()
		local fn
		fn = function(...)
			return select("#", ...)
		end
		local result = fn()
		return assert.same(result, 0)
	end)
	it("should spread varargs in function call", function()
		local receiver
		receiver = function(a, b, c)
			return {
				a,
				b,
				c
			}
		end
		local source
		source = function()
			return 1, 2, 3
		end
		local result = receiver(source())
		return assert.same(result, {
			1,
			2,
			3
		})
	end)
	it("should use varargs in table", function()
		local fn
		fn = function(...)
			return {
				...
			}
		end
		local result = fn(1, 2, 3)
		return assert.same(result, {
			1,
			2,
			3
		})
	end)
	it("should forward varargs", function()
		local middle
		middle = function(fn, ...)
			return fn(...)
		end
		local inner
		inner = function(a, b, c)
			return a + b + c
		end
		local result = middle(inner, 1, 2, 3)
		return assert.same(result, 6)
	end)
	it("should count varargs with select", function()
		local fn
		fn = function(...)
			return select("#", ...)
		end
		assert.same(fn(1, 2, 3), 3)
		assert.same(fn("a", "b"), 2)
		return assert.same(fn(), 0)
	end)
	it("should select from varargs", function()
		local fn
		fn = function(...)
			return select(2, ...)
		end
		local result = fn(1, 2, 3)
		return assert.same(result, 2)
	end)
	it("should work with named parameters and varargs", function()
		local fn
		fn = function(first, ...)
			return {
				first,
				select("#", ...)
			}
		end
		local result = fn("first", "second", "third")
		return assert.same(result, {
			"first",
			2
		})
	end)
	it("should handle nil in varargs", function()
		local fn
		fn = function(...)
			local count = select("#", ...)
			local has_nil = false
			for i = 1, count do
				if select(i, ...) == nil then
					has_nil = true
				end
			end
			return {
				count,
				has_nil
			}
		end
		local result = fn(1, nil, 3)
		return assert.same(result, {
			3,
			true
		})
	end)
	it("should work with table unpack", function()
		local fn
		fn = function(...)
			return {
				...
			}
		end
		local result = fn(table.unpack({
			1,
			2,
			3
		}))
		return assert.same(result, {
			1,
			2,
			3
		})
	end)
	return it("should work with varargs in comprehension", function()
		local fn
		fn = function(...)
			local _accum_0 = { }
			local _len_0 = 1
			local _list_0 = {
				...
			}
			for _index_0 = 1, #_list_0 do
				local x = _list_0[_index_0]
				_accum_0[_len_0] = x * 2
				_len_0 = _len_0 + 1
			end
			return _accum_0
		end
		local result = fn(1, 2, 3, 4, 5)
		return assert.same(result, {
			2,
			4,
			6,
			8,
			10
		})
	end)
end)