aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/with_statement_spec.lua
blob: dcd2aa1f2c9752dcf071f8bbc3ed7582ac5983cd (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
268
269
270
271
272
273
274
275
276
277
278
279
return describe("with statement", function()
	it("should access properties with dot", function()
		local obj = {
			x = 10,
			y = 20
		}
		local result = nil
		do
			result = obj.x + obj.y
		end
		return assert.same(result, 30)
	end)
	it("should chain property access", function()
		local obj = {
			nested = {
				value = 42
			}
		}
		local result = nil
		do
			result = obj.nested.value
		end
		return assert.same(result, 42)
	end)
	it("should work with method calls", function()
		local obj = {
			value = 10,
			double = function(self)
				return self.value * 2
			end
		}
		local result = nil
		do
			result = obj:double()
		end
		return assert.same(result, 20)
	end)
	it("should handle nested with statements", function()
		local obj = {
			x = 1
		}
		obj.x = 10
		local _with_0 = {
			y = 2
		}
		obj.nested = _with_0
		_with_0.y = 20
		assert.same(obj.x, 10)
		return assert.same(obj.nested.y, 20)
	end)
	it("should work in expressions", function()
		local obj = {
			value = 5
		}
		local result
		do
			local _accum_0
			repeat
				_accum_0 = obj.value * 2
				break
			until true
			result = _accum_0
		end
		return assert.same(result, 10)
	end)
	it("should support multiple statements", function()
		local obj = {
			a = 1,
			b = 2
		}
		local sum = nil
		local product = nil
		do
			sum = obj.a + obj.b
			product = obj.a * obj.b
		end
		assert.same(sum, 3)
		return assert.same(product, 2)
	end)
	it("should work with table manipulation", function()
		local obj = {
			items = {
				1,
				2,
				3
			}
		}
		table.insert(obj.items, 4)
		return assert.same(#obj.items, 4)
	end)
	it("should handle conditional inside with", function()
		local obj = {
			value = 10
		}
		local result = nil
		if obj.value > 5 then
			result = "large"
		else
			result = "small"
		end
		return assert.same(result, "large")
	end)
	it("should work with loops", function()
		local obj = {
			items = {
				1,
				2,
				3
			}
		}
		local sum = nil
		do
			sum = 0
			local _list_0 = obj.items
			for _index_0 = 1, #_list_0 do
				local item = _list_0[_index_0]
				sum = sum + item
			end
		end
		return assert.same(sum, 6)
	end)
	it("should support with in assignment", function()
		local obj = {
			x = 5,
			y = 10
		}
		local result
		do
			local _accum_0
			repeat
				_accum_0 = obj.x + obj.y
				break
			until true
			result = _accum_0
		end
		return assert.same(result, 15)
	end)
	it("should work with string methods", function()
		local s = "hello"
		local result
		do
			local _accum_0
			repeat
				_accum_0 = s:upper()
				break
			until true
			result = _accum_0
		end
		return assert.same(result, "HELLO")
	end)
	it("should handle metatable access", function()
		local obj = setmetatable({
			value = 10
		}, {
			__index = {
				extra = 5
			}
		})
		local sum = nil
		do
			sum = obj.value + getmetatable(obj).__index.extra
		end
		return assert.same(sum, 15)
	end)
	it("should work in function", function()
		local fn
		fn = function()
			local obj = {
				x = 10
			}
			local _val_0
			local _accum_0
			repeat
				_accum_0 = obj.x * 2
				break
			until true
			_val_0 = _accum_0
			return _val_0
		end
		local result = fn()
		return assert.same(result, 20)
	end)
	it("should support with in return", function()
		local get_value
		get_value = function()
			local obj = {
				value = 42
			}
			local _val_0
			local _accum_0
			repeat
				_accum_0 = obj.value
				break
			until true
			_val_0 = _accum_0
			return _val_0
		end
		return assert.same(get_value(), 42)
	end)
	it("should work with existential operator", function()
		local obj = {
			value = 10
		}
		local result
		do
			local _accum_0
			repeat
				local _exp_0 = obj.value
				if _exp_0 ~= nil then
					_accum_0 = _exp_0
				else
					_accum_0 = 0
				end
				break
			until true
			result = _accum_0
		end
		return assert.same(result, 10)
	end)
	it("should handle nil object safely", function()
		local result
		do
			local _with_0 = nil
			do
				local _accum_0
				repeat
					if _with_0 ~= nil then
						_accum_0 = _with_0.value
						break
					end
				until true
				result = _accum_0
			end
		end
		return assert.same(result, nil)
	end)
	it("should work with method chaining", function()
		local obj = {
			value = 5,
			add = function(self, n)
				self.value = self.value + n
			end,
			get = function(self)
				return self.value
			end
		}
		local result
		do
			local _accum_0
			repeat
				obj:add(10)
				obj:add(5)
				_accum_0 = obj:get()
				break
			until true
			result = _accum_0
		end
		return assert.same(result, 20)
	end)
	return it("should support nested property access", function()
		local obj = {
			level1 = {
				level2 = {
					level3 = "deep"
				}
			}
		}
		local result
		do
			local _accum_0
			repeat
				_accum_0 = obj.level1.level2.level3
				break
			until true
			result = _accum_0
		end
		return assert.same(result, "deep")
	end)
end)