aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/varargs_assignment_spec.yue
blob: 1c3b6271e76bbe7aa1e104931947aeb56c8f7c3d (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
describe "varargs assignment", ->
	it "should assign varargs from function", ->
		list = [1, 2, 3, 4, 5]
		fn = (ok) -> ok, table.unpack list
		ok, ... = fn true
		count = select '#', ...
		assert.same count, 5
		assert.same ok, true

	it "should access varargs elements", ->
		list = [10, 20, 30]
		fn = -> table.unpack list
		... = fn!
		first = select 1, ...
		second = select 2, ...
		third = select 3, ...
		assert.same first, 10
		assert.same second, 20
		assert.same third, 30

	it "should work with pcall", ->
		fn = -> 1, 2, 3
		success, ... = pcall fn
		assert.is_true success
		assert.same select('#', ...), 3

	it "should handle empty varargs", ->
		fn = ->
		... = fn!
		count = select '#', ...
		assert.same count, 0

	it "should work with mixed return values", ->
		fn = -> "first", nil, "third", false
		a, ... = fn!
		assert.same a, "first"
		assert.same select('#', ...), 3

	it "should preserve nil values in varargs", ->
		fn = -> 1, nil, 2, nil, 3
		... = fn!
		count = select '#', ...
		assert.same count, 5
		assert.same select(1, ...), 1
		assert.same select(2, ...), nil
		assert.same select(3, ...), 2

	it "should work with table.unpack", ->
		tb = {a: 1, b: 2, c: 3}
		fn = -> table.unpack tb
		... = fn!
		count = select '#', ...
		assert.same count, 3

	it "should chain varargs assignment", ->
		fn1 = -> 1, 2, 3
		fn2 = -> table.unpack {4, 5, 6}
		a, ... = fn1!
		b, ... = fn2!
		assert.same a, 1
		assert.same b, 4
		assert.same select('#', ...), 2

	it "should work in expressions", ->
		sum = (...) ->
			total = 0
			for i = 1, select '#', ...
				total += select i, ... if type(select(i, ...)) == "number"
			total

		fn = -> 1, 2, 3, 4, 5
		... = fn!
		result = sum ...
		assert.same result, 15

	it "should work with string.format", ->
		... = "hello", 123, true
		result = string.format "str: %s, num: %d, bool: %s", ...
		assert.same result, "str: hello, num: 123, bool: true"

	it "should handle single return value", ->
		fn = -> 42
		... = fn!
		count = select '#', ...
		assert.same count, 1
		assert.same select(1, ...), 42

	it "should work with nested functions", ->
		outer = -> 1, 2, 3
		inner = -> 4, 5
		a, b, ... = outer!
		c, d = inner!
		assert.same a, 1
		assert.same b, 2
		assert.same c, 4
		assert.same d, 5