aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/close_attribute_spec.yue
blob: 2354df74939b5380456d536c7027432e9b7e736d (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
describe "close attribute", ->
	it "should declare close variable", ->
		closed = false
		do
			close _ = <close>: -> closed = true
		assert.is_true closed

	it "should work with metatable syntax", ->
		called = false
		do
			close _ = <close>: -> called = true
		assert.is_true called

	it "should handle multiple close scopes", ->
		order = []
		do
			close first = <close>: -> table.insert order, "first"
			close second = <close>: -> table.insert order, "second"
		assert.same order, {"second", "first"}

	it "should work with resources", ->
		resource_opened = false
		resource_closed = false

		do
			resource_opened = true
			close _ = <close>: -> resource_closed = true

		assert.is_true resource_opened
		assert.is_true resource_closed

	it "should support close in function", ->
		closed = false
		fn = ->
			close _ = <close>: -> closed = true
			return "result"

		result = fn!
		assert.same result, "result"
		assert.is_true closed

	it "should work with fat arrow", ->
		closed = false
		obj =
			value: 10
			close_method: <close>: =>
				closed = true

		do
			close _ = obj

		assert.is_true closed

	it "should handle nested close scopes", ->
		outer_closed = false
		inner_closed = false

		do
			close outer = <close>: -> outer_closed = true
			do
				close inner = <close>: -> inner_closed = true

		assert.is_true inner_closed
		assert.is_true outer_closed

	it "should work with conditional close", ->
		closed = false
		should_close = true

		if should_close
			close _ = <close>: -> closed = true

		assert.is_true closed

	it "should support close in loop", ->
		closed_count = 0
		for i = 1, 3
			do
				close _ = <close>: -> closed_count += 1

		assert.same closed_count, 3

	it "should work with table destructuring", ->
		closed = false
		tb = {close: <close>: -> closed = true}
		do
			{:close} = tb
		assert.is_true closed

	it "should handle close with return value", ->
		closed = false
		fn = ->
			close _ = <close>: -> closed = true
			return 42

		result = fn!
		assert.same result, 42
		assert.is_true closed

	it "should work with error handling", ->
		closed = false
		error_thrown = false

		do
			close _ = <close>: -> closed = true
			error_thrown = true

		assert.is_true closed
		assert.is_true error_thrown

	it "should support close in varargs function", ->
		closed = false
		fn = (...) ->
			close _ = <close>: -> closed = true
			{...}

		result = fn 1, 2, 3
		assert.same result, {1, 2, 3}
		assert.is_true closed

	it "should work with multiple variables", ->
		first_closed = false
		second_closed = false

		do
			close first = <close>: -> first_closed = true
			close second = <close>: -> second_closed = true

		assert.is_true first_closed
		assert.is_true second_closed

	it "should handle close in try block", ->
		closed = false
		success = false

		success = try
			close _ = <close>: -> closed = true
			true
		catch err
			false

		assert.is_true success
		assert.is_true closed