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/with_statement_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/with_statement_spec.lua')
| -rw-r--r-- | spec/outputs/test/with_statement_spec.lua | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/spec/outputs/test/with_statement_spec.lua b/spec/outputs/test/with_statement_spec.lua new file mode 100644 index 0000000..dcd2aa1 --- /dev/null +++ b/spec/outputs/test/with_statement_spec.lua | |||
| @@ -0,0 +1,279 @@ | |||
| 1 | return describe("with statement", function() | ||
| 2 | it("should access properties with dot", function() | ||
| 3 | local obj = { | ||
| 4 | x = 10, | ||
| 5 | y = 20 | ||
| 6 | } | ||
| 7 | local result = nil | ||
| 8 | do | ||
| 9 | result = obj.x + obj.y | ||
| 10 | end | ||
| 11 | return assert.same(result, 30) | ||
| 12 | end) | ||
| 13 | it("should chain property access", function() | ||
| 14 | local obj = { | ||
| 15 | nested = { | ||
| 16 | value = 42 | ||
| 17 | } | ||
| 18 | } | ||
| 19 | local result = nil | ||
| 20 | do | ||
| 21 | result = obj.nested.value | ||
| 22 | end | ||
| 23 | return assert.same(result, 42) | ||
| 24 | end) | ||
| 25 | it("should work with method calls", function() | ||
| 26 | local obj = { | ||
| 27 | value = 10, | ||
| 28 | double = function(self) | ||
| 29 | return self.value * 2 | ||
| 30 | end | ||
| 31 | } | ||
| 32 | local result = nil | ||
| 33 | do | ||
| 34 | result = obj:double() | ||
| 35 | end | ||
| 36 | return assert.same(result, 20) | ||
| 37 | end) | ||
| 38 | it("should handle nested with statements", function() | ||
| 39 | local obj = { | ||
| 40 | x = 1 | ||
| 41 | } | ||
| 42 | obj.x = 10 | ||
| 43 | local _with_0 = { | ||
| 44 | y = 2 | ||
| 45 | } | ||
| 46 | obj.nested = _with_0 | ||
| 47 | _with_0.y = 20 | ||
| 48 | assert.same(obj.x, 10) | ||
| 49 | return assert.same(obj.nested.y, 20) | ||
| 50 | end) | ||
| 51 | it("should work in expressions", function() | ||
| 52 | local obj = { | ||
| 53 | value = 5 | ||
| 54 | } | ||
| 55 | local result | ||
| 56 | do | ||
| 57 | local _accum_0 | ||
| 58 | repeat | ||
| 59 | _accum_0 = obj.value * 2 | ||
| 60 | break | ||
| 61 | until true | ||
| 62 | result = _accum_0 | ||
| 63 | end | ||
| 64 | return assert.same(result, 10) | ||
| 65 | end) | ||
| 66 | it("should support multiple statements", function() | ||
| 67 | local obj = { | ||
| 68 | a = 1, | ||
| 69 | b = 2 | ||
| 70 | } | ||
| 71 | local sum = nil | ||
| 72 | local product = nil | ||
| 73 | do | ||
| 74 | sum = obj.a + obj.b | ||
| 75 | product = obj.a * obj.b | ||
| 76 | end | ||
| 77 | assert.same(sum, 3) | ||
| 78 | return assert.same(product, 2) | ||
| 79 | end) | ||
| 80 | it("should work with table manipulation", function() | ||
| 81 | local obj = { | ||
| 82 | items = { | ||
| 83 | 1, | ||
| 84 | 2, | ||
| 85 | 3 | ||
| 86 | } | ||
| 87 | } | ||
| 88 | table.insert(obj.items, 4) | ||
| 89 | return assert.same(#obj.items, 4) | ||
| 90 | end) | ||
| 91 | it("should handle conditional inside with", function() | ||
| 92 | local obj = { | ||
| 93 | value = 10 | ||
| 94 | } | ||
| 95 | local result = nil | ||
| 96 | if obj.value > 5 then | ||
| 97 | result = "large" | ||
| 98 | else | ||
| 99 | result = "small" | ||
| 100 | end | ||
| 101 | return assert.same(result, "large") | ||
| 102 | end) | ||
| 103 | it("should work with loops", function() | ||
| 104 | local obj = { | ||
| 105 | items = { | ||
| 106 | 1, | ||
| 107 | 2, | ||
| 108 | 3 | ||
| 109 | } | ||
| 110 | } | ||
| 111 | local sum = nil | ||
| 112 | do | ||
| 113 | sum = 0 | ||
| 114 | local _list_0 = obj.items | ||
| 115 | for _index_0 = 1, #_list_0 do | ||
| 116 | local item = _list_0[_index_0] | ||
| 117 | sum = sum + item | ||
| 118 | end | ||
| 119 | end | ||
| 120 | return assert.same(sum, 6) | ||
| 121 | end) | ||
| 122 | it("should support with in assignment", function() | ||
| 123 | local obj = { | ||
| 124 | x = 5, | ||
| 125 | y = 10 | ||
| 126 | } | ||
| 127 | local result | ||
| 128 | do | ||
| 129 | local _accum_0 | ||
| 130 | repeat | ||
| 131 | _accum_0 = obj.x + obj.y | ||
| 132 | break | ||
| 133 | until true | ||
| 134 | result = _accum_0 | ||
| 135 | end | ||
| 136 | return assert.same(result, 15) | ||
| 137 | end) | ||
| 138 | it("should work with string methods", function() | ||
| 139 | local s = "hello" | ||
| 140 | local result | ||
| 141 | do | ||
| 142 | local _accum_0 | ||
| 143 | repeat | ||
| 144 | _accum_0 = s:upper() | ||
| 145 | break | ||
| 146 | until true | ||
| 147 | result = _accum_0 | ||
| 148 | end | ||
| 149 | return assert.same(result, "HELLO") | ||
| 150 | end) | ||
| 151 | it("should handle metatable access", function() | ||
| 152 | local obj = setmetatable({ | ||
| 153 | value = 10 | ||
| 154 | }, { | ||
| 155 | __index = { | ||
| 156 | extra = 5 | ||
| 157 | } | ||
| 158 | }) | ||
| 159 | local sum = nil | ||
| 160 | do | ||
| 161 | sum = obj.value + getmetatable(obj).__index.extra | ||
| 162 | end | ||
| 163 | return assert.same(sum, 15) | ||
| 164 | end) | ||
| 165 | it("should work in function", function() | ||
| 166 | local fn | ||
| 167 | fn = function() | ||
| 168 | local obj = { | ||
| 169 | x = 10 | ||
| 170 | } | ||
| 171 | local _val_0 | ||
| 172 | local _accum_0 | ||
| 173 | repeat | ||
| 174 | _accum_0 = obj.x * 2 | ||
| 175 | break | ||
| 176 | until true | ||
| 177 | _val_0 = _accum_0 | ||
| 178 | return _val_0 | ||
| 179 | end | ||
| 180 | local result = fn() | ||
| 181 | return assert.same(result, 20) | ||
| 182 | end) | ||
| 183 | it("should support with in return", function() | ||
| 184 | local get_value | ||
| 185 | get_value = function() | ||
| 186 | local obj = { | ||
| 187 | value = 42 | ||
| 188 | } | ||
| 189 | local _val_0 | ||
| 190 | local _accum_0 | ||
| 191 | repeat | ||
| 192 | _accum_0 = obj.value | ||
| 193 | break | ||
| 194 | until true | ||
| 195 | _val_0 = _accum_0 | ||
| 196 | return _val_0 | ||
| 197 | end | ||
| 198 | return assert.same(get_value(), 42) | ||
| 199 | end) | ||
| 200 | it("should work with existential operator", function() | ||
| 201 | local obj = { | ||
| 202 | value = 10 | ||
| 203 | } | ||
| 204 | local result | ||
| 205 | do | ||
| 206 | local _accum_0 | ||
| 207 | repeat | ||
| 208 | local _exp_0 = obj.value | ||
| 209 | if _exp_0 ~= nil then | ||
| 210 | _accum_0 = _exp_0 | ||
| 211 | else | ||
| 212 | _accum_0 = 0 | ||
| 213 | end | ||
| 214 | break | ||
| 215 | until true | ||
| 216 | result = _accum_0 | ||
| 217 | end | ||
| 218 | return assert.same(result, 10) | ||
| 219 | end) | ||
| 220 | it("should handle nil object safely", function() | ||
| 221 | local result | ||
| 222 | do | ||
| 223 | local _with_0 = nil | ||
| 224 | do | ||
| 225 | local _accum_0 | ||
| 226 | repeat | ||
| 227 | if _with_0 ~= nil then | ||
| 228 | _accum_0 = _with_0.value | ||
| 229 | break | ||
| 230 | end | ||
| 231 | until true | ||
| 232 | result = _accum_0 | ||
| 233 | end | ||
| 234 | end | ||
| 235 | return assert.same(result, nil) | ||
| 236 | end) | ||
| 237 | it("should work with method chaining", function() | ||
| 238 | local obj = { | ||
| 239 | value = 5, | ||
| 240 | add = function(self, n) | ||
| 241 | self.value = self.value + n | ||
| 242 | end, | ||
| 243 | get = function(self) | ||
| 244 | return self.value | ||
| 245 | end | ||
| 246 | } | ||
| 247 | local result | ||
| 248 | do | ||
| 249 | local _accum_0 | ||
| 250 | repeat | ||
| 251 | obj:add(10) | ||
| 252 | obj:add(5) | ||
| 253 | _accum_0 = obj:get() | ||
| 254 | break | ||
| 255 | until true | ||
| 256 | result = _accum_0 | ||
| 257 | end | ||
| 258 | return assert.same(result, 20) | ||
| 259 | end) | ||
| 260 | return it("should support nested property access", function() | ||
| 261 | local obj = { | ||
| 262 | level1 = { | ||
| 263 | level2 = { | ||
| 264 | level3 = "deep" | ||
| 265 | } | ||
| 266 | } | ||
| 267 | } | ||
| 268 | local result | ||
| 269 | do | ||
| 270 | local _accum_0 | ||
| 271 | repeat | ||
| 272 | _accum_0 = obj.level1.level2.level3 | ||
| 273 | break | ||
| 274 | until true | ||
| 275 | result = _accum_0 | ||
| 276 | end | ||
| 277 | return assert.same(result, "deep") | ||
| 278 | end) | ||
| 279 | end) | ||
