aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/switch_spec.yue
blob: 0865e5a945ad217f669d880a3df36aa238285baa (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
describe "switch", ->
	it "should match single value", ->
		value = "cool"
		result = switch value
			when "cool"
				"matched"
			else
				"not matched"
		assert.same result, "matched"

	it "should match multiple values with or", ->
		hi = "world"
		matched = false
		switch hi
			when "one", "two"
				matched = true
		assert.is_false matched

		hi = "one"
		switch hi
			when "one", "two"
				matched = true
		assert.is_true matched

	it "should execute else branch when no match", ->
		value = "other"
		result = switch value
			when "cool"
				"matched cool"
			when "yeah"
				"matched yeah"
			else
				"else branch"
		assert.same result, "else branch"

	it "should destructure table with single key", ->
		tb = {x: 100}
		result = switch tb
			when :x
				x
			else
				"no match"
		assert.same result, 100

	it "should destructure table with multiple keys", ->
		tb = {x: 100, y: 200}
		result = switch tb
			when :x, :y
				x + y
			else
				"no match"
		assert.same result, 300

	it "should destructure table with default values", ->
		tb = {a: 1}
		switch tb
			when {:a = 1, :b = 2}
				assert.same a, 1
				assert.same b, 2

	it "should destructure nested tables", ->
		dict = {
			{}
			{1, 2, 3}
			a: b: c: 1
			x: y: z: 1
		}
		matched = false
		switch dict
			when {
					first
					{one, two, three}
					a: b: :c
					x: y: :z
				}
				matched = type(first) == 'table' and one == 1 and two == 2 and three == 3 and c == 1 and z == 1
		assert.is_true matched

	it "should destructure arrays with exact match", ->
		tb = {1, 2, 3}
		result = switch tb
			when [1, 2, 3]
				"exact match"
			else
				"no match"
		assert.same result, "exact match"

	it "should destructure arrays with variables", ->
		tb = {1, "b", 3}
		result = switch tb
			when [1, b, 3]
				b
			else
				"no match"
		assert.same result, "b"

	it "should destructure arrays with defaults", ->
		tb = {1, 2}
		result = switch tb
			when [1, 2, b = 3]
				b
			else
				"no match"
		assert.same result, 3

	it "should match pattern with __class", ->
		class ClassA
		class ClassB
		item = ClassA!
		result = switch item
			when __class: ClassA
				"Object A"
			when __class: ClassB
				"Object B"
			else
				"unknown"
		assert.same result, "Object A"

	it "should match pattern with metatable", ->
		tb = setmetatable {}, {__mode: "v"}
		metatable_matched = false
		switch tb
			when <>: mt
				metatable_matched = mt ~= nil
		assert.is_true metatable_matched

	it "should use switch as expression in assignment", ->
		tb = {x: "abc"}
		matched = switch tb
			when 1
				"1"
			when :x
				x
			when false
				"false"
			else
				nil
		assert.same matched, "abc"

	it "should use switch in return statement", ->
		fn = (tb) ->
			switch tb
				when nil
					"invalid"
				when :a, :b
					"#{a + b}"
				when 1, 2, 3, 4, 5
					"number 1 - 5"
				else
					"should not reach here"
		assert.same fn({a: 1, b: 2}), "3"
		assert.same fn(3), "number 1 - 5"
		assert.same fn(nil), "invalid"

	it "should support pattern matching assignment with :=", ->
		v = "hello"
		matched = false
		switch v := "hello"
			when "hello"
				matched = true
			else
				matched = false
		assert.is_true matched
		assert.same v, "hello"

	it "should match with computed expressions", ->
		hi = 4
		matched = false
		switch hi
			when 3+1, (-> 4)!, 5-1
				matched = true
		assert.is_true matched

	it "should handle nested array destructuring", ->
		tb = {
			{a: 1, b: 2}
			{a: 3, b: 4}
			{a: 5, b: 6}
			"fourth"
		}
		result = switch tb
			when [
					{a: 1, b: 2}
					{a: 3, b: 4}
					{a: 5, b: 6}
					fourth
				]
				fourth
			else
				"no match"
		assert.same result, "fourth"

	it "should match combined patterns", ->
		tb = {success: true, result: "data"}
		result = switch tb
			when success: true, :result
				{"success", result}
			when success: false
				{"failed", result}
			else
				{"invalid"}
		assert.same result, {"success", "data"}

	it "should match type discriminated patterns", ->
		tb = {type: "success", content: "data"}
		result = switch tb
			when {type: "success", :content}
				{"success", content}
			when {type: "error", :content}
				{"error", content}
			else
				{"invalid"}
		assert.same result, {"success", "data"}

	it "should match with wildcard array capture", ->
		clientData = {"Meta", "CUST_1001", "CHK123"}
		metadata = nil
		customerId = nil
		checksum = nil
		switch clientData
			when [...capturedMetadata, customerId, checksum]
				metadata = capturedMetadata
		assert.same metadata, {"Meta"}
		assert.same customerId, "CUST_1001"
		assert.same checksum, "CHK123"

	it "should work with complex tuple patterns", ->
		handlePath = (segments) ->
			switch segments
				when [..._, resource, action]
					{"Resource: #{resource}", "Action: #{action}"}
				else
					{"no match"}
		result = handlePath {"admin", "logs", "view"}
		assert.same result, {"Resource: logs", "Action: view"}

	it "should match boolean false correctly", ->
		items = {
			{x: 100, y: 200}
			{width: 300, height: 400}
			false
		}
		results = {}
		for item in *items
			switch item
				when :x, :y
					table.insert results, "Vec2"
				when :width, :height
					table.insert results, "Size"
				when false
					table.insert results, "None"
		assert.same results, {"Vec2", "Size", "None"}

	it "should handle switch with then syntax", ->
		value = "cool"
		result = switch value
			when "cool" then "matched cool"
			else "else branch"
		assert.same result, "matched cool"

	it "should handle switch in function call", ->
		something = 1
		getValue = ->
			switch something
				when 1 then "yes"
				else "no"
		assert.same getValue!, "yes"