aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/param_destructure_spec.yue
diff options
context:
space:
mode:
Diffstat (limited to 'spec/inputs/test/param_destructure_spec.yue')
-rw-r--r--spec/inputs/test/param_destructure_spec.yue110
1 files changed, 110 insertions, 0 deletions
diff --git a/spec/inputs/test/param_destructure_spec.yue b/spec/inputs/test/param_destructure_spec.yue
new file mode 100644
index 0000000..4659031
--- /dev/null
+++ b/spec/inputs/test/param_destructure_spec.yue
@@ -0,0 +1,110 @@
1describe "parameter destructuring", ->
2 it "should destructure simple object", ->
3 f = (:a, :b, :c) ->
4 {a, b, c}
5
6 result = f a: 1, b: "2", c: {}
7 assert.same result, {1, "2", {}}
8
9 it "should work with default values", ->
10 f = ({a: a1 = 123, :b = 'abc'}, c = {}) ->
11 {a1, b, c}
12
13 result1 = f {a: 0}, "test"
14 assert.same result1, {0, 'abc', 'test'}
15
16 result2 = f {}
17 assert.same result2, {123, 'abc', {}}
18
19 it "should destructure with mixed syntax", ->
20 f = (:a, b: b1, :c) ->
21 {a, b1, c}
22
23 result = f a: 1, b: 2, c: 3
24 assert.same result, {1, 2, 3}
25
26 it "should work with nested destructuring", ->
27 f = ({nested: {:x, :y}}) ->
28 {x, y}
29
30 result = f nested: {x: 10, y: 20}
31 assert.same result, {10, 20}
32
33 it "should handle array parameters", ->
34 f = ([a, b, c]) ->
35 {a, b, c}
36
37 result = f [1, 2, 3]
38 assert.same result, {1, 2, 3}
39
40 it "should support mixed array and object", ->
41 f = ([first], {key: :value}) ->
42 {first, value}
43
44 result = f [1], {key: "test"}
45 assert.same result, {1, "test"}
46
47 it "should work with fat arrow", ->
48 obj =
49 value: 100
50 f: ({:x, :y}) =>
51 @value + x + y
52
53 result = obj\f {x: 10, y: 20}
54 assert.same result, 130
55
56 it "should handle missing keys", ->
57 f = (:a, :b = "default", :c = "missing") ->
58 {a, b, c}
59
60 result = f a: 1
61 assert.same result, {1, 'default', 'missing'}
62
63 it "should work with complex defaults", ->
64 f = ({a: a1 = 100, b: b1 = a1 + 1000}) ->
65 a1 + b1
66
67 result = f {}
68 assert.same result, 1200
69
70 it "should support deep nesting", ->
71 f = ({data: {nested: {:value}}}) ->
72 value
73
74 result = f data: {nested: {value: 42}}
75 assert.same result, 42
76
77 it "should work with multiple parameters", ->
78 f = (:x, :y, :z, extra = "default") ->
79 {x, y, z, extra}
80
81 result = f x: 1, y: 2, z: 3
82 assert.same result, {1, 2, 3, 'default'}
83
84 it "should handle array destructuring in parameters", ->
85 f = ([first, ...rest]) ->
86 {first, rest}
87
88 result = f [1, 2, 3, 4]
89 assert.same result, {1, {2, 3, 4}}
90
91 it "should support spreading", ->
92 f = (...rest, :last) ->
93 {rest, last}
94
95 result = f 1, 2, 3, {last: "final"}
96 assert.same result, {{1, 2, 3}, 'final'}
97
98 it "should work with table comprehensions", ->
99 f = ({:items}) ->
100 [item * 2 for item in *items]
101
102 result = f items: {1, 2, 3}
103 assert.same result, {2, 4, 6}
104
105 it "should handle nil arguments", ->
106 f = (:a = "nil_a", :b = "nil_b") ->
107 {a, b}
108
109 result = f nil, nil
110 assert.same result, {'nil_a', 'nil_b'}