diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-01-28 18:43:14 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-01-28 18:43:14 +0800 |
| commit | dd64edd58fe25ec74ae5958128cf3f74b0692f3b (patch) | |
| tree | 0f2de1df55897e2713977c5a53936757e14b477a /spec/outputs/test/close_attribute_spec.lua | |
| parent | 7c2a92b82e9808d3c5ea29b47d1c59d663fe984a (diff) | |
| download | yuescript-dd64edd58fe25ec74ae5958128cf3f74b0692f3b.tar.gz yuescript-dd64edd58fe25ec74ae5958128cf3f74b0692f3b.tar.bz2 yuescript-dd64edd58fe25ec74ae5958128cf3f74b0692f3b.zip | |
Fixed compiler issues and added 800+ test cases.
Diffstat (limited to 'spec/outputs/test/close_attribute_spec.lua')
| -rw-r--r-- | spec/outputs/test/close_attribute_spec.lua | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/spec/outputs/test/close_attribute_spec.lua b/spec/outputs/test/close_attribute_spec.lua new file mode 100644 index 0000000..cc64da8 --- /dev/null +++ b/spec/outputs/test/close_attribute_spec.lua | |||
| @@ -0,0 +1,226 @@ | |||
| 1 | return describe("close attribute", function() | ||
| 2 | it("should declare close variable", function() | ||
| 3 | local closed = false | ||
| 4 | do | ||
| 5 | local _ <close> = setmetatable({ }, { | ||
| 6 | __close = function() | ||
| 7 | closed = true | ||
| 8 | end | ||
| 9 | }) | ||
| 10 | end | ||
| 11 | return assert.is_true(closed) | ||
| 12 | end) | ||
| 13 | it("should work with metatable syntax", function() | ||
| 14 | local called = false | ||
| 15 | do | ||
| 16 | local _ <close> = setmetatable({ }, { | ||
| 17 | __close = function() | ||
| 18 | called = true | ||
| 19 | end | ||
| 20 | }) | ||
| 21 | end | ||
| 22 | return assert.is_true(called) | ||
| 23 | end) | ||
| 24 | it("should handle multiple close scopes", function() | ||
| 25 | local order = { } | ||
| 26 | do | ||
| 27 | local first <close> = setmetatable({ }, { | ||
| 28 | __close = function() | ||
| 29 | return table.insert(order, "first") | ||
| 30 | end | ||
| 31 | }) | ||
| 32 | local second <close> = setmetatable({ }, { | ||
| 33 | __close = function() | ||
| 34 | return table.insert(order, "second") | ||
| 35 | end | ||
| 36 | }) | ||
| 37 | end | ||
| 38 | return assert.same(order, { | ||
| 39 | "second", | ||
| 40 | "first" | ||
| 41 | }) | ||
| 42 | end) | ||
| 43 | it("should work with resources", function() | ||
| 44 | local resource_opened = false | ||
| 45 | local resource_closed = false | ||
| 46 | do | ||
| 47 | resource_opened = true | ||
| 48 | local _ <close> = setmetatable({ }, { | ||
| 49 | __close = function() | ||
| 50 | resource_closed = true | ||
| 51 | end | ||
| 52 | }) | ||
| 53 | end | ||
| 54 | assert.is_true(resource_opened) | ||
| 55 | return assert.is_true(resource_closed) | ||
| 56 | end) | ||
| 57 | it("should support close in function", function() | ||
| 58 | local closed = false | ||
| 59 | local fn | ||
| 60 | fn = function() | ||
| 61 | local _ <close> = setmetatable({ }, { | ||
| 62 | __close = function() | ||
| 63 | closed = true | ||
| 64 | end | ||
| 65 | }) | ||
| 66 | return "result" | ||
| 67 | end | ||
| 68 | local result = fn() | ||
| 69 | assert.same(result, "result") | ||
| 70 | return assert.is_true(closed) | ||
| 71 | end) | ||
| 72 | it("should work with fat arrow", function() | ||
| 73 | local closed = false | ||
| 74 | local obj = setmetatable({ | ||
| 75 | value = 10, | ||
| 76 | }, { | ||
| 77 | __close = function(self) | ||
| 78 | closed = true | ||
| 79 | end | ||
| 80 | }) | ||
| 81 | do | ||
| 82 | local _ <close> = obj | ||
| 83 | end | ||
| 84 | return assert.is_true(closed) | ||
| 85 | end) | ||
| 86 | it("should handle nested close scopes", function() | ||
| 87 | local outer_closed = false | ||
| 88 | local inner_closed = false | ||
| 89 | do | ||
| 90 | local outer <close> = setmetatable({ }, { | ||
| 91 | __close = function() | ||
| 92 | outer_closed = true | ||
| 93 | end | ||
| 94 | }) | ||
| 95 | do | ||
| 96 | local inner <close> = setmetatable({ }, { | ||
| 97 | __close = function() | ||
| 98 | inner_closed = true | ||
| 99 | end | ||
| 100 | }) | ||
| 101 | end | ||
| 102 | end | ||
| 103 | assert.is_true(inner_closed) | ||
| 104 | return assert.is_true(outer_closed) | ||
| 105 | end) | ||
| 106 | it("should work with conditional close", function() | ||
| 107 | local closed = false | ||
| 108 | local should_close = true | ||
| 109 | if should_close then | ||
| 110 | local _ <close> = setmetatable({ }, { | ||
| 111 | __close = function() | ||
| 112 | closed = true | ||
| 113 | end | ||
| 114 | }) | ||
| 115 | end | ||
| 116 | return assert.is_true(closed) | ||
| 117 | end) | ||
| 118 | it("should support close in loop", function() | ||
| 119 | local closed_count = 0 | ||
| 120 | for i = 1, 3 do | ||
| 121 | do | ||
| 122 | local _ <close> = setmetatable({ }, { | ||
| 123 | __close = function() | ||
| 124 | closed_count = closed_count + 1 | ||
| 125 | end | ||
| 126 | }) | ||
| 127 | end | ||
| 128 | end | ||
| 129 | return assert.same(closed_count, 3) | ||
| 130 | end) | ||
| 131 | it("should work with table destructuring", function() | ||
| 132 | local closed = false | ||
| 133 | do | ||
| 134 | local tb <close> = setmetatable({ }, { | ||
| 135 | __close = function() | ||
| 136 | closed = true | ||
| 137 | end | ||
| 138 | }) | ||
| 139 | end | ||
| 140 | return assert.is_true(closed) | ||
| 141 | end) | ||
| 142 | it("should handle close with return value", function() | ||
| 143 | local closed = false | ||
| 144 | local fn | ||
| 145 | fn = function() | ||
| 146 | local _ <close> = setmetatable({ }, { | ||
| 147 | __close = function() | ||
| 148 | closed = true | ||
| 149 | end | ||
| 150 | }) | ||
| 151 | return 42 | ||
| 152 | end | ||
| 153 | local result = fn() | ||
| 154 | assert.same(result, 42) | ||
| 155 | return assert.is_true(closed) | ||
| 156 | end) | ||
| 157 | it("should work with error handling", function() | ||
| 158 | local closed = false | ||
| 159 | local error_thrown = false | ||
| 160 | do | ||
| 161 | local _ <close> = setmetatable({ }, { | ||
| 162 | __close = function() | ||
| 163 | closed = true | ||
| 164 | end | ||
| 165 | }) | ||
| 166 | error_thrown = true | ||
| 167 | end | ||
| 168 | assert.is_true(closed) | ||
| 169 | return assert.is_true(error_thrown) | ||
| 170 | end) | ||
| 171 | it("should support close in varargs function", function() | ||
| 172 | local closed = false | ||
| 173 | local fn | ||
| 174 | fn = function(...) | ||
| 175 | local _ <close> = setmetatable({ }, { | ||
| 176 | __close = function() | ||
| 177 | closed = true | ||
| 178 | end | ||
| 179 | }) | ||
| 180 | return { | ||
| 181 | ... | ||
| 182 | } | ||
| 183 | end | ||
| 184 | local result = fn(1, 2, 3) | ||
| 185 | assert.same(result, { | ||
| 186 | 1, | ||
| 187 | 2, | ||
| 188 | 3 | ||
| 189 | }) | ||
| 190 | return assert.is_true(closed) | ||
| 191 | end) | ||
| 192 | it("should work with multiple variables", function() | ||
| 193 | local first_closed = false | ||
| 194 | local second_closed = false | ||
| 195 | do | ||
| 196 | local first <close> = setmetatable({ }, { | ||
| 197 | __close = function() | ||
| 198 | first_closed = true | ||
| 199 | end | ||
| 200 | }) | ||
| 201 | local second <close> = setmetatable({ }, { | ||
| 202 | __close = function() | ||
| 203 | second_closed = true | ||
| 204 | end | ||
| 205 | }) | ||
| 206 | end | ||
| 207 | assert.is_true(first_closed) | ||
| 208 | return assert.is_true(second_closed) | ||
| 209 | end) | ||
| 210 | return it("should handle close in try block", function() | ||
| 211 | local closed = false | ||
| 212 | local success = false | ||
| 213 | success = xpcall(function() | ||
| 214 | local _ <close> = setmetatable({ }, { | ||
| 215 | __close = function() | ||
| 216 | closed = true | ||
| 217 | end | ||
| 218 | }) | ||
| 219 | return true | ||
| 220 | end, function(err) | ||
| 221 | return false | ||
| 222 | end) | ||
| 223 | assert.is_true(success) | ||
| 224 | return assert.is_true(closed) | ||
| 225 | end) | ||
| 226 | end) | ||
