summaryrefslogtreecommitdiff
path: root/spec/outputs/test/close_attribute_spec.lua
blob: cc64da8bb484aa3ad4bdcb21cd02d5994ed7477a (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
return describe("close attribute", function()
	it("should declare close variable", function()
		local closed = false
		do
			local _ <close> = setmetatable({ }, {
				__close = function()
					closed = true
				end
			})
		end
		return assert.is_true(closed)
	end)
	it("should work with metatable syntax", function()
		local called = false
		do
			local _ <close> = setmetatable({ }, {
				__close = function()
					called = true
				end
			})
		end
		return assert.is_true(called)
	end)
	it("should handle multiple close scopes", function()
		local order = { }
		do
			local first <close> = setmetatable({ }, {
				__close = function()
					return table.insert(order, "first")
				end
			})
			local second <close> = setmetatable({ }, {
				__close = function()
					return table.insert(order, "second")
				end
			})
		end
		return assert.same(order, {
			"second",
			"first"
		})
	end)
	it("should work with resources", function()
		local resource_opened = false
		local resource_closed = false
		do
			resource_opened = true
			local _ <close> = setmetatable({ }, {
				__close = function()
					resource_closed = true
				end
			})
		end
		assert.is_true(resource_opened)
		return assert.is_true(resource_closed)
	end)
	it("should support close in function", function()
		local closed = false
		local fn
		fn = function()
			local _ <close> = setmetatable({ }, {
				__close = function()
					closed = true
				end
			})
			return "result"
		end
		local result = fn()
		assert.same(result, "result")
		return assert.is_true(closed)
	end)
	it("should work with fat arrow", function()
		local closed = false
		local obj = setmetatable({
			value = 10,
		}, {
			__close = function(self)
				closed = true
			end
		})
		do
			local _ <close> = obj
		end
		return assert.is_true(closed)
	end)
	it("should handle nested close scopes", function()
		local outer_closed = false
		local inner_closed = false
		do
			local outer <close> = setmetatable({ }, {
				__close = function()
					outer_closed = true
				end
			})
			do
				local inner <close> = setmetatable({ }, {
					__close = function()
						inner_closed = true
					end
				})
			end
		end
		assert.is_true(inner_closed)
		return assert.is_true(outer_closed)
	end)
	it("should work with conditional close", function()
		local closed = false
		local should_close = true
		if should_close then
			local _ <close> = setmetatable({ }, {
				__close = function()
					closed = true
				end
			})
		end
		return assert.is_true(closed)
	end)
	it("should support close in loop", function()
		local closed_count = 0
		for i = 1, 3 do
			do
				local _ <close> = setmetatable({ }, {
					__close = function()
						closed_count = closed_count + 1
					end
				})
			end
		end
		return assert.same(closed_count, 3)
	end)
	it("should work with table destructuring", function()
		local closed = false
		do
			local tb <close> = setmetatable({ }, {
				__close = function()
					closed = true
				end
			})
		end
		return assert.is_true(closed)
	end)
	it("should handle close with return value", function()
		local closed = false
		local fn
		fn = function()
			local _ <close> = setmetatable({ }, {
				__close = function()
					closed = true
				end
			})
			return 42
		end
		local result = fn()
		assert.same(result, 42)
		return assert.is_true(closed)
	end)
	it("should work with error handling", function()
		local closed = false
		local error_thrown = false
		do
			local _ <close> = setmetatable({ }, {
				__close = function()
					closed = true
				end
			})
			error_thrown = true
		end
		assert.is_true(closed)
		return assert.is_true(error_thrown)
	end)
	it("should support close in varargs function", function()
		local closed = false
		local fn
		fn = function(...)
			local _ <close> = setmetatable({ }, {
				__close = function()
					closed = true
				end
			})
			return {
				...
			}
		end
		local result = fn(1, 2, 3)
		assert.same(result, {
			1,
			2,
			3
		})
		return assert.is_true(closed)
	end)
	it("should work with multiple variables", function()
		local first_closed = false
		local second_closed = false
		do
			local first <close> = setmetatable({ }, {
				__close = function()
					first_closed = true
				end
			})
			local second <close> = setmetatable({ }, {
				__close = function()
					second_closed = true
				end
			})
		end
		assert.is_true(first_closed)
		return assert.is_true(second_closed)
	end)
	return it("should handle close in try block", function()
		local closed = false
		local success = false
		success = xpcall(function()
			local _ <close> = setmetatable({ }, {
				__close = function()
					closed = true
				end
			})
			return true
		end, function(err)
			return false
		end)
		assert.is_true(success)
		return assert.is_true(closed)
	end)
end)