aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/switch_spec.yue
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-01-26 06:38:38 +0000
committerLi Jin <dragon-fly@qq.com>2026-01-26 06:38:38 +0000
commit5d5b657f606b5939062983b1f90c3359d542672e (patch)
tree32132fd8908d6a8920d59362c572815a949f1a1f /spec/inputs/test/switch_spec.yue
parentf5006f449a7be1a2f655f1b178ecf1d2f0569dd5 (diff)
downloadyuescript-5d5b657f606b5939062983b1f90c3359d542672e.tar.gz
yuescript-5d5b657f606b5939062983b1f90c3359d542672e.tar.bz2
yuescript-5d5b657f606b5939062983b1f90c3359d542672e.zip
Fixed compiler improvements and added comprehensive test suite
- Fixed makefile preprocessor macro definitions (removed spaces in -D flags) - Added null pointer check in compiler class declaration handling - Added comprehensive test specifications for various language features: - attrib, backcall, cond, config, existential, export, goto - import, literals, macro, metatable, operators, return - string, switch, vararg, with Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'spec/inputs/test/switch_spec.yue')
-rw-r--r--spec/inputs/test/switch_spec.yue267
1 files changed, 267 insertions, 0 deletions
diff --git a/spec/inputs/test/switch_spec.yue b/spec/inputs/test/switch_spec.yue
new file mode 100644
index 0000000..3696cbe
--- /dev/null
+++ b/spec/inputs/test/switch_spec.yue
@@ -0,0 +1,267 @@
1describe "switch", ->
2 it "should match single value", ->
3 value = "cool"
4 result = switch value
5 when "cool"
6 "matched"
7 else
8 "not matched"
9 assert.same result, "matched"
10
11 it "should match multiple values with or", ->
12 hi = "world"
13 matched = false
14 switch hi
15 when "one", "two"
16 matched = true
17 assert.is_false matched
18
19 hi = "one"
20 switch hi
21 when "one", "two"
22 matched = true
23 assert.is_true matched
24
25 it "should execute else branch when no match", ->
26 value = "other"
27 result = switch value
28 when "cool"
29 "matched cool"
30 when "yeah"
31 "matched yeah"
32 else
33 "else branch"
34 assert.same result, "else branch"
35
36 it "should destructure table with single key", ->
37 tb = {x: 100}
38 result = switch tb
39 when :x
40 x
41 else
42 "no match"
43 assert.same result, 100
44
45 it "should destructure table with multiple keys", ->
46 tb = {x: 100, y: 200}
47 result = switch tb
48 when :x, :y
49 x + y
50 else
51 "no match"
52 assert.same result, 300
53
54 it "should destructure table with default values", ->
55 tb = {a: 1}
56 switch tb
57 when {:a = 1, :b = 2}
58 assert.same a, 1
59 assert.same b, 2
60
61 it "should destructure nested tables", ->
62 dict = {
63 {}
64 {1, 2, 3}
65 a: b: c: 1
66 x: y: z: 1
67 }
68 matched = false
69 switch dict
70 when {
71 first
72 {one, two, three}
73 a: b: :c
74 x: y: :z
75 }
76 matched = first == {} and one == 1 and two == 2 and three == 3 and c == 1 and z == 1
77 assert.is_true matched
78
79 it "should destructure arrays with exact match", ->
80 tb = {1, 2, 3}
81 result = switch tb
82 when [1, 2, 3]
83 "exact match"
84 else
85 "no match"
86 assert.same result, "exact match"
87
88 it "should destructure arrays with variables", ->
89 tb = {1, "b", 3}
90 result = switch tb
91 when [1, b, 3]
92 b
93 else
94 "no match"
95 assert.same result, "b"
96
97 it "should destructure arrays with defaults", ->
98 tb = {1, 2}
99 result = switch tb
100 when [1, 2, b = 3]
101 b
102 else
103 "no match"
104 assert.same result, 3
105
106 it "should match pattern with __class", ->
107 class ClassA
108 class ClassB
109 item = ClassA!
110 result = switch item
111 when __class: ClassA
112 "Object A"
113 when __class: ClassB
114 "Object B"
115 else
116 "unknown"
117 assert.same result, "Object A"
118
119 it "should match pattern with metatable", ->
120 tb = setmetatable {}, {__mode: "v"}
121 metatable_matched = false
122 switch tb
123 when <>: mt
124 metatable_matched = mt ~= nil
125 assert.is_true metatable_matched
126
127 it "should use switch as expression in assignment", ->
128 tb = {x: "abc"}
129 matched = switch tb
130 when 1
131 "1"
132 when :x
133 x
134 when false
135 "false"
136 else
137 nil
138 assert.same matched, "abc"
139
140 it "should use switch in return statement", ->
141 fn = (tb) ->
142 switch tb
143 when nil
144 "invalid"
145 when :a, :b
146 "#{a + b}"
147 when 1, 2, 3, 4, 5
148 "number 1 - 5"
149 else
150 "should not reach here"
151 assert.same fn({a: 1, b: 2}), "3"
152 assert.same fn(3), "number 1 - 5"
153 assert.same fn(nil), "invalid"
154
155 it "should support pattern matching assignment with :=", ->
156 v = "hello"
157 matched = false
158 switch v := "hello"
159 when "hello"
160 matched = true
161 else
162 matched = false
163 assert.is_true matched
164 assert.same v, "hello"
165
166 it "should match with computed expressions", ->
167 hi = 4
168 matched = false
169 switch hi
170 when 3+1, (-> 4)!, 5-1
171 matched = true
172 assert.is_true matched
173
174 it "should handle nested array destructuring", ->
175 tb = {
176 {a: 1, b: 2}
177 {a: 3, b: 4}
178 {a: 5, b: 6}
179 "fourth"
180 }
181 result = switch tb
182 when [
183 {a: 1, b: 2}
184 {a: 3, b: 4}
185 {a: 5, b: 6}
186 fourth
187 ]
188 fourth
189 else
190 "no match"
191 assert.same result, "fourth"
192
193 it "should match combined patterns", ->
194 tb = {success: true, result: "data"}
195 result = switch tb
196 when success: true, :result
197 {"success", result}
198 when success: false
199 {"failed", result}
200 else
201 {"invalid"}
202 assert.same result, {"success", "data"}
203
204 it "should match type discriminated patterns", ->
205 tb = {type: "success", content: "data"}
206 result = switch tb
207 when {type: "success", :content}
208 {"success", content}
209 when {type: "error", :content}
210 {"error", content}
211 else
212 {"invalid"}
213 assert.same result, {"success", "data"}
214
215 it "should match with wildcard array capture", ->
216 clientData = {"Meta", "CUST_1001", "CHK123"}
217 metadata = nil
218 customerId = nil
219 checksum = nil
220 switch clientData
221 when [...capturedMetadata, customerId, checksum]
222 metadata = capturedMetadata
223 assert.same metadata, {"Meta"}
224 assert.same customerId, "CUST_1001"
225 assert.same checksum, "CHK123"
226
227 it "should work with complex tuple patterns", ->
228 handlePath = (segments) ->
229 switch segments
230 when [..._, resource, action]
231 {"Resource: #{resource}", "Action: #{action}"}
232 else
233 {"no match"}
234 result = handlePath {"admin", "logs", "view"}
235 assert.same result, {"Resource: logs", "Action: view"}
236
237 it "should match boolean false correctly", ->
238 items = {
239 {x: 100, y: 200}
240 {width: 300, height: 400}
241 false
242 }
243 results = {}
244 for item in *items
245 switch item
246 when :x, :y
247 table.insert results, "Vec2"
248 when :width, :height
249 table.insert results, "Size"
250 when false
251 table.insert results, "None"
252 assert.same results, {"Vec2", "Size", "None"}
253
254 it "should handle switch with then syntax", ->
255 value = "cool"
256 result = switch value
257 when "cool" then "matched cool"
258 else "else branch"
259 assert.same result, "matched cool"
260
261 it "should handle switch in function call", ->
262 getValue = ->
263 switch something
264 when 1 then "yes"
265 else "no"
266 something = 1
267 assert.same getValue!, "yes"