aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/functions_advanced_spec.yue
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-01-27 00:30:56 +0000
committerLi Jin <dragon-fly@qq.com>2026-01-27 00:30:56 +0000
commit7c2a92b82e9808d3c5ea29b47d1c59d663fe984a (patch)
treeceba95c48bd8d5d9fff3d1206483ddf073c0e03d /spec/inputs/test/functions_advanced_spec.yue
parente70e63a9737ed3a9e72f1329901075498190e6b4 (diff)
downloadyuescript-compiler-improvements.tar.gz
yuescript-compiler-improvements.tar.bz2
yuescript-compiler-improvements.zip
Add compiler improvements and comprehensive test suitecompiler-improvements
- Fixed path option handling to avoid semicolon concatenation issues - Added exception handling for std::length_error and general exceptions - Added comprehensive test specifications for advanced language features Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'spec/inputs/test/functions_advanced_spec.yue')
-rw-r--r--spec/inputs/test/functions_advanced_spec.yue158
1 files changed, 158 insertions, 0 deletions
diff --git a/spec/inputs/test/functions_advanced_spec.yue b/spec/inputs/test/functions_advanced_spec.yue
new file mode 100644
index 0000000..d0e0cf5
--- /dev/null
+++ b/spec/inputs/test/functions_advanced_spec.yue
@@ -0,0 +1,158 @@
1describe "advanced functions", ->
2 it "should support fat arrow with self", ->
3 obj =
4 value: 10
5 getValue: => @value
6
7 assert.same obj\getValue!, 10
8
9 it "should work with argument defaults", ->
10 fn = (name = "something", height = 100) ->
11 "#{name}, #{height}"
12
13 assert.same fn!, "something, 100"
14 assert.same fn("test"), "test, 100"
15 assert.same fn("test", 50), "test, 50"
16
17 it "should handle defaults with previous arguments", ->
18 fn = (x = 100, y = x + 1000) ->
19 x + y
20
21 assert.same fn!, 1200
22 assert.same fn(50), 1150
23
24 it "should work with multi-line arguments", ->
25 my_func = (a, b, c, d, e, f) -> a + b + c + d + e + f
26 result = my_func 5, 4, 3,
27 8, 9, 10
28 assert.same result, 39
29
30 it "should support nested function calls", ->
31 result = my_func 5, 6, 7,
32 6, another_func 6, 7, 8,
33 9, 1, 2,
34 5, 4
35
36 another_func = (a, b, c, d, e, f) -> a + b + c + d + e + f
37 my_func = (a, b, c, d, e, f) -> a + b + c + d + e + f
38
39 assert.same result, 52
40
41 it "should handle implicit return", ->
42 sum = (x, y) -> x + y
43 assert.same sum 10, 20, 30
44
45 it "should work with explicit return", ->
46 difference = (x, y) -> return x - y
47 assert.same difference 20, 10, 10
48
49 it "should support multiple return values", ->
50 mystery = (x, y) -> x + y, x - y
51 a, b = mystery 10, 20
52 assert.same a, 30
53 assert.same b, -10
54
55 it "should work with function as argument", ->
56 apply = (fn, x, y) -> fn x, y
57 result = apply ((a, b) -> a + b), 5, 10
58 assert.same result, 15
59
60 it "should handle function returning function", ->
61 create_adder = (x) -> (y) -> x + y
62 add_five = create_adder 5
63 assert.same add_five(10), 15
64
65 it "should support immediately invoked function", ->
66 result = ((x) -> x * 2) 5
67 assert.same result, 10
68
69 it "should work with varargs", ->
70 sum_all = (...) ->
71 total = 0
72 for i = 1, select '#', ...
73 total += select(i, ...) if type(select(i, ...)) == "number"
74 total
75
76 assert.same sum_all(1, 2, 3, 4, 5), 15
77
78 it "should handle named varargs", ->
79 fn = (...t) ->
80 count = 0
81 for i = 1, t.n
82 count += 1
83 count
84
85 assert.same fn(1, 2, 3), 3
86
87 it "should support prefixed return", ->
88 findValue: "not found" ->
89 items = [1, 2, 3]
90 for item in *items
91 if item == 5
92 return item
93
94 result = findValue!
95 assert.same result, "not found"
96
97 it "should work with parameter destructuring", ->
98 fn = (:a, :b, :c) ->
99 a + b + c
100
101 assert.same fn(a: 1, b: 2, c: 3), 6
102
103 it "should handle default values in destructuring", ->
104 fn = ({a: a1 = 123, :b = 'abc'}) ->
105 a1 .. " " .. b
106
107 assert.same fn{}, "123 abc"
108 assert.same fn({a: 456}), "456 abc"
109
110 it "should support empty function body", ->
111 empty_fn = ->
112 assert.same empty_fn!, nil
113
114 it "should work with function in table", ->
115 tb =
116 value: 10
117 double: => @value * 2
118
119 assert.same tb\double!, 20
120
121 it "should handle function with no arguments", ->
122 fn = ->
123 "result"
124
125 assert.same fn!, "result"
126 assert.same fn(), "result"
127
128 it "should support calling function with !", ->
129 fn = -> 42
130 assert.same fn!, 42
131
132 it "should work with nested functions", ->
133 outer = (x) ->
134 inner = (y) -> x + y
135 inner
136
137 add_five = outer 5
138 assert.same add_five(10), 15
139
140 it "should handle function in expression", ->
141 result = if ((x) -> x > 10) 15
142 "large"
143 else
144 "small"
145 assert.same result, "large"
146
147 it "should support function as return value", ->
148 get_operation = (op) ->
149 switch op
150 when "add"
151 (a, b) -> a + b
152 when "subtract"
153 (a, b) -> a - b
154 else
155 -> 0
156
157 add = get_operation "add"
158 assert.same add 5, 3, 8