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/do_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/do_statement_spec.lua')
| -rw-r--r-- | spec/outputs/test/do_statement_spec.lua | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/spec/outputs/test/do_statement_spec.lua b/spec/outputs/test/do_statement_spec.lua new file mode 100644 index 0000000..fb93fa0 --- /dev/null +++ b/spec/outputs/test/do_statement_spec.lua | |||
| @@ -0,0 +1,238 @@ | |||
| 1 | return describe("do statement", function() | ||
| 2 | it("should create new scope", function() | ||
| 3 | local x = 10 | ||
| 4 | do | ||
| 5 | local x = 20 | ||
| 6 | assert.same(x, 20) | ||
| 7 | end | ||
| 8 | return assert.same(x, 10) | ||
| 9 | end) | ||
| 10 | it("should return value from do block", function() | ||
| 11 | local result | ||
| 12 | do | ||
| 13 | local x = 5 | ||
| 14 | result = x * 2 | ||
| 15 | end | ||
| 16 | return assert.same(result, 10) | ||
| 17 | end) | ||
| 18 | it("should work with multiple statements", function() | ||
| 19 | local result | ||
| 20 | do | ||
| 21 | local a = 1 | ||
| 22 | local b = 2 | ||
| 23 | local c = 3 | ||
| 24 | result = a + b + c | ||
| 25 | end | ||
| 26 | return assert.same(result, 6) | ||
| 27 | end) | ||
| 28 | it("should handle nested do blocks", function() | ||
| 29 | local result | ||
| 30 | do | ||
| 31 | local x = 10 | ||
| 32 | local y | ||
| 33 | do | ||
| 34 | local z = 5 | ||
| 35 | y = z * 2 | ||
| 36 | end | ||
| 37 | result = x + y | ||
| 38 | end | ||
| 39 | return assert.same(result, 20) | ||
| 40 | end) | ||
| 41 | it("should support conditional in do block", function() | ||
| 42 | local result | ||
| 43 | do | ||
| 44 | local value = 5 | ||
| 45 | if value > 3 then | ||
| 46 | result = value * 2 | ||
| 47 | else | ||
| 48 | result = value | ||
| 49 | end | ||
| 50 | end | ||
| 51 | return assert.same(result, 10) | ||
| 52 | end) | ||
| 53 | it("should work with loops in do block", function() | ||
| 54 | local result | ||
| 55 | do | ||
| 56 | local sum = 0 | ||
| 57 | for i = 1, 5 do | ||
| 58 | sum = sum + i | ||
| 59 | end | ||
| 60 | result = sum | ||
| 61 | end | ||
| 62 | return assert.same(result, 15) | ||
| 63 | end) | ||
| 64 | it("should handle table operations", function() | ||
| 65 | local result | ||
| 66 | do | ||
| 67 | local tb = { | ||
| 68 | 1, | ||
| 69 | 2, | ||
| 70 | 3 | ||
| 71 | } | ||
| 72 | table.insert(tb, 4) | ||
| 73 | result = #tb | ||
| 74 | end | ||
| 75 | return assert.same(result, 4) | ||
| 76 | end) | ||
| 77 | it("should work with function definition", function() | ||
| 78 | local result | ||
| 79 | do | ||
| 80 | local fn | ||
| 81 | fn = function(x) | ||
| 82 | return x * 2 | ||
| 83 | end | ||
| 84 | result = fn(5) | ||
| 85 | end | ||
| 86 | return assert.same(result, 10) | ||
| 87 | end) | ||
| 88 | it("should support variable shadowing", function() | ||
| 89 | local x = "outer" | ||
| 90 | local result | ||
| 91 | do | ||
| 92 | local x = "inner" | ||
| 93 | result = x | ||
| 94 | end | ||
| 95 | assert.same(result, "inner") | ||
| 96 | return assert.same(x, "outer") | ||
| 97 | end) | ||
| 98 | it("should work with method calls", function() | ||
| 99 | local obj = { | ||
| 100 | value = 10, | ||
| 101 | double = function(self) | ||
| 102 | return self.value * 2 | ||
| 103 | end | ||
| 104 | } | ||
| 105 | local result | ||
| 106 | do | ||
| 107 | local _accum_0 | ||
| 108 | repeat | ||
| 109 | _accum_0 = obj:double() | ||
| 110 | break | ||
| 111 | until true | ||
| 112 | result = _accum_0 | ||
| 113 | end | ||
| 114 | return assert.same(result, 20) | ||
| 115 | end) | ||
| 116 | it("should handle comprehensions in do block", function() | ||
| 117 | local result | ||
| 118 | do | ||
| 119 | local items = { | ||
| 120 | 1, | ||
| 121 | 2, | ||
| 122 | 3, | ||
| 123 | 4, | ||
| 124 | 5 | ||
| 125 | } | ||
| 126 | local _accum_0 = { } | ||
| 127 | local _len_0 = 1 | ||
| 128 | for _index_0 = 1, #items do | ||
| 129 | local item = items[_index_0] | ||
| 130 | _accum_0[_len_0] = item * 2 | ||
| 131 | _len_0 = _len_0 + 1 | ||
| 132 | end | ||
| 133 | result = _accum_0 | ||
| 134 | end | ||
| 135 | return assert.same(result, { | ||
| 136 | 2, | ||
| 137 | 4, | ||
| 138 | 6, | ||
| 139 | 8, | ||
| 140 | 10 | ||
| 141 | }) | ||
| 142 | end) | ||
| 143 | it("should work with try-catch", function() | ||
| 144 | local success, result = xpcall(function() | ||
| 145 | error("test error") | ||
| 146 | return false | ||
| 147 | end, function(err) | ||
| 148 | return true | ||
| 149 | end) | ||
| 150 | assert.is_false(success) | ||
| 151 | return assert.is_true(result) | ||
| 152 | end) | ||
| 153 | it("should support return statement", function() | ||
| 154 | local fn | ||
| 155 | fn = function() | ||
| 156 | do | ||
| 157 | local x = 10 | ||
| 158 | return x * 2 | ||
| 159 | end | ||
| 160 | return "never reached" | ||
| 161 | end | ||
| 162 | local result = fn() | ||
| 163 | return assert.same(result, 20) | ||
| 164 | end) | ||
| 165 | it("should work with assignment", function() | ||
| 166 | local result | ||
| 167 | do | ||
| 168 | local a, b, c = 1, 2, 3 | ||
| 169 | result = a + b + c | ||
| 170 | end | ||
| 171 | return assert.same(result, 6) | ||
| 172 | end) | ||
| 173 | it("should handle destructuring", function() | ||
| 174 | local result | ||
| 175 | do | ||
| 176 | local tb = { | ||
| 177 | x = 10, | ||
| 178 | y = 20 | ||
| 179 | } | ||
| 180 | local x, y = tb.x, tb.y | ||
| 181 | result = x + y | ||
| 182 | end | ||
| 183 | return assert.same(result, 30) | ||
| 184 | end) | ||
| 185 | it("should work with string interpolation", function() | ||
| 186 | local name = "world" | ||
| 187 | local result | ||
| 188 | do | ||
| 189 | local greeting = "hello" | ||
| 190 | result = tostring(greeting) .. " " .. tostring(name) | ||
| 191 | end | ||
| 192 | return assert.same(result, "hello world") | ||
| 193 | end) | ||
| 194 | it("should support implicit return", function() | ||
| 195 | local result | ||
| 196 | do | ||
| 197 | local value = 42 | ||
| 198 | result = value | ||
| 199 | end | ||
| 200 | return assert.same(result, 42) | ||
| 201 | end) | ||
| 202 | it("should handle empty do block", function() | ||
| 203 | local result | ||
| 204 | do | ||
| 205 | result = nil | ||
| 206 | end | ||
| 207 | return assert.same(result, nil) | ||
| 208 | end) | ||
| 209 | return it("should work with backcalls", function() | ||
| 210 | local map | ||
| 211 | map = function(f, items) | ||
| 212 | local _accum_0 = { } | ||
| 213 | local _len_0 = 1 | ||
| 214 | for _index_0 = 1, #items do | ||
| 215 | local item = items[_index_0] | ||
| 216 | _accum_0[_len_0] = f(item) | ||
| 217 | _len_0 = _len_0 + 1 | ||
| 218 | end | ||
| 219 | return _accum_0 | ||
| 220 | end | ||
| 221 | local result | ||
| 222 | do | ||
| 223 | local items = { | ||
| 224 | 1, | ||
| 225 | 2, | ||
| 226 | 3 | ||
| 227 | } | ||
| 228 | result = map(function(x) | ||
| 229 | return x * 2 | ||
| 230 | end, items) | ||
| 231 | end | ||
| 232 | return assert.same(result, { | ||
| 233 | 2, | ||
| 234 | 4, | ||
| 235 | 6 | ||
| 236 | }) | ||
| 237 | end) | ||
| 238 | end) | ||
