summaryrefslogtreecommitdiff
path: root/spec/outputs/test/do_statement_spec.lua
blob: fb93fa042ce8ecd857375565a081dacfc6e79c92 (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
return describe("do statement", function()
	it("should create new scope", function()
		local x = 10
		do
			local x = 20
			assert.same(x, 20)
		end
		return assert.same(x, 10)
	end)
	it("should return value from do block", function()
		local result
		do
			local x = 5
			result = x * 2
		end
		return assert.same(result, 10)
	end)
	it("should work with multiple statements", function()
		local result
		do
			local a = 1
			local b = 2
			local c = 3
			result = a + b + c
		end
		return assert.same(result, 6)
	end)
	it("should handle nested do blocks", function()
		local result
		do
			local x = 10
			local y
			do
				local z = 5
				y = z * 2
			end
			result = x + y
		end
		return assert.same(result, 20)
	end)
	it("should support conditional in do block", function()
		local result
		do
			local value = 5
			if value > 3 then
				result = value * 2
			else
				result = value
			end
		end
		return assert.same(result, 10)
	end)
	it("should work with loops in do block", function()
		local result
		do
			local sum = 0
			for i = 1, 5 do
				sum = sum + i
			end
			result = sum
		end
		return assert.same(result, 15)
	end)
	it("should handle table operations", function()
		local result
		do
			local tb = {
				1,
				2,
				3
			}
			table.insert(tb, 4)
			result = #tb
		end
		return assert.same(result, 4)
	end)
	it("should work with function definition", function()
		local result
		do
			local fn
			fn = function(x)
				return x * 2
			end
			result = fn(5)
		end
		return assert.same(result, 10)
	end)
	it("should support variable shadowing", function()
		local x = "outer"
		local result
		do
			local x = "inner"
			result = x
		end
		assert.same(result, "inner")
		return assert.same(x, "outer")
	end)
	it("should work with method calls", function()
		local obj = {
			value = 10,
			double = function(self)
				return self.value * 2
			end
		}
		local result
		do
			local _accum_0
			repeat
				_accum_0 = obj:double()
				break
			until true
			result = _accum_0
		end
		return assert.same(result, 20)
	end)
	it("should handle comprehensions in do block", function()
		local result
		do
			local items = {
				1,
				2,
				3,
				4,
				5
			}
			local _accum_0 = { }
			local _len_0 = 1
			for _index_0 = 1, #items do
				local item = items[_index_0]
				_accum_0[_len_0] = item * 2
				_len_0 = _len_0 + 1
			end
			result = _accum_0
		end
		return assert.same(result, {
			2,
			4,
			6,
			8,
			10
		})
	end)
	it("should work with try-catch", function()
		local success, result = xpcall(function()
			error("test error")
			return false
		end, function(err)
			return true
		end)
		assert.is_false(success)
		return assert.is_true(result)
	end)
	it("should support return statement", function()
		local fn
		fn = function()
			do
				local x = 10
				return x * 2
			end
			return "never reached"
		end
		local result = fn()
		return assert.same(result, 20)
	end)
	it("should work with assignment", function()
		local result
		do
			local a, b, c = 1, 2, 3
			result = a + b + c
		end
		return assert.same(result, 6)
	end)
	it("should handle destructuring", function()
		local result
		do
			local tb = {
				x = 10,
				y = 20
			}
			local x, y = tb.x, tb.y
			result = x + y
		end
		return assert.same(result, 30)
	end)
	it("should work with string interpolation", function()
		local name = "world"
		local result
		do
			local greeting = "hello"
			result = tostring(greeting) .. " " .. tostring(name)
		end
		return assert.same(result, "hello world")
	end)
	it("should support implicit return", function()
		local result
		do
			local value = 42
			result = value
		end
		return assert.same(result, 42)
	end)
	it("should handle empty do block", function()
		local result
		do
			result = nil
		end
		return assert.same(result, nil)
	end)
	return it("should work with backcalls", function()
		local map
		map = function(f, items)
			local _accum_0 = { }
			local _len_0 = 1
			for _index_0 = 1, #items do
				local item = items[_index_0]
				_accum_0[_len_0] = f(item)
				_len_0 = _len_0 + 1
			end
			return _accum_0
		end
		local result
		do
			local items = {
				1,
				2,
				3
			}
			result = map(function(x)
				return x * 2
			end, items)
		end
		return assert.same(result, {
			2,
			4,
			6
		})
	end)
end)