aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/param_destructure_spec.yue
blob: 46590310c6d80aee67f87def1d73a0f0822ed7d9 (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
describe "parameter destructuring", ->
	it "should destructure simple object", ->
		f = (:a, :b, :c) ->
			{a, b, c}

		result = f a: 1, b: "2", c: {}
		assert.same result, {1, "2", {}}

	it "should work with default values", ->
		f = ({a: a1 = 123, :b = 'abc'}, c = {}) ->
			{a1, b, c}

		result1 = f {a: 0}, "test"
		assert.same result1, {0, 'abc', 'test'}

		result2 = f {}
		assert.same result2, {123, 'abc', {}}

	it "should destructure with mixed syntax", ->
		f = (:a, b: b1, :c) ->
			{a, b1, c}

		result = f a: 1, b: 2, c: 3
		assert.same result, {1, 2, 3}

	it "should work with nested destructuring", ->
		f = ({nested: {:x, :y}}) ->
			{x, y}

		result = f nested: {x: 10, y: 20}
		assert.same result, {10, 20}

	it "should handle array parameters", ->
		f = ([a, b, c]) ->
			{a, b, c}

		result = f [1, 2, 3]
		assert.same result, {1, 2, 3}

	it "should support mixed array and object", ->
		f = ([first], {key: :value}) ->
			{first, value}

		result = f [1], {key: "test"}
		assert.same result, {1, "test"}

	it "should work with fat arrow", ->
		obj =
			value: 100
			f: ({:x, :y}) =>
				@value + x + y

		result = obj\f {x: 10, y: 20}
		assert.same result, 130

	it "should handle missing keys", ->
		f = (:a, :b = "default", :c = "missing") ->
			{a, b, c}

		result = f a: 1
		assert.same result, {1, 'default', 'missing'}

	it "should work with complex defaults", ->
		f = ({a: a1 = 100, b: b1 = a1 + 1000}) ->
			a1 + b1

		result = f {}
		assert.same result, 1200

	it "should support deep nesting", ->
		f = ({data: {nested: {:value}}}) ->
			value

		result = f data: {nested: {value: 42}}
		assert.same result, 42

	it "should work with multiple parameters", ->
		f = (:x, :y, :z, extra = "default") ->
			{x, y, z, extra}

		result = f x: 1, y: 2, z: 3
		assert.same result, {1, 2, 3, 'default'}

	it "should handle array destructuring in parameters", ->
		f = ([first, ...rest]) ->
			{first, rest}

		result = f [1, 2, 3, 4]
		assert.same result, {1, {2, 3, 4}}

	it "should support spreading", ->
		f = (...rest, :last) ->
			{rest, last}

		result = f 1, 2, 3, {last: "final"}
		assert.same result, {{1, 2, 3}, 'final'}

	it "should work with table comprehensions", ->
		f = ({:items}) ->
			[item * 2 for item in *items]

		result = f items: {1, 2, 3}
		assert.same result, {2, 4, 6}

	it "should handle nil arguments", ->
		f = (:a = "nil_a", :b = "nil_b") ->
			{a, b}

		result = f nil, nil
		assert.same result, {'nil_a', 'nil_b'}