diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-01-26 17:45:26 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-01-26 17:45:56 +0800 |
| commit | e02321107277a63e7dcb12ab163c9942ac101b87 (patch) | |
| tree | f38c6f2fbf4ee35df4938933dbc1e645733356f4 /spec/outputs/test/config_spec.lua | |
| parent | 5d5b657f606b5939062983b1f90c3359d542672e (diff) | |
| download | yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.gz yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.bz2 yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.zip | |
Updated tests.
Diffstat (limited to 'spec/outputs/test/config_spec.lua')
| -rw-r--r-- | spec/outputs/test/config_spec.lua | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/spec/outputs/test/config_spec.lua b/spec/outputs/test/config_spec.lua new file mode 100644 index 0000000..410bacc --- /dev/null +++ b/spec/outputs/test/config_spec.lua | |||
| @@ -0,0 +1,195 @@ | |||
| 1 | return describe("config", function() | ||
| 2 | it("should handle implicit return", function() | ||
| 3 | local fn | ||
| 4 | fn = function() | ||
| 5 | return 42 | ||
| 6 | end | ||
| 7 | return assert.same(fn(), 42) | ||
| 8 | end) | ||
| 9 | it("should handle return in last position", function() | ||
| 10 | local fn | ||
| 11 | fn = function() | ||
| 12 | if true then | ||
| 13 | return 100 | ||
| 14 | else | ||
| 15 | return 200 | ||
| 16 | end | ||
| 17 | end | ||
| 18 | return assert.same(fn(), 100) | ||
| 19 | end) | ||
| 20 | it("should work with various code patterns", function() | ||
| 21 | local x = 1 + 2 | ||
| 22 | local y | ||
| 23 | if x > 0 then | ||
| 24 | y = "positive" | ||
| 25 | else | ||
| 26 | y = "negative" | ||
| 27 | end | ||
| 28 | return assert.same(y, "positive") | ||
| 29 | end) | ||
| 30 | it("should handle class definitions", function() | ||
| 31 | local TestClass | ||
| 32 | do | ||
| 33 | local _class_0 | ||
| 34 | local _base_0 = { | ||
| 35 | value = 100, | ||
| 36 | get_value = function(self) | ||
| 37 | return self.value | ||
| 38 | end | ||
| 39 | } | ||
| 40 | if _base_0.__index == nil then | ||
| 41 | _base_0.__index = _base_0 | ||
| 42 | end | ||
| 43 | _class_0 = setmetatable({ | ||
| 44 | __init = function() end, | ||
| 45 | __base = _base_0, | ||
| 46 | __name = "TestClass" | ||
| 47 | }, { | ||
| 48 | __index = _base_0, | ||
| 49 | __call = function(cls, ...) | ||
| 50 | local _self_0 = setmetatable({ }, _base_0) | ||
| 51 | cls.__init(_self_0, ...) | ||
| 52 | return _self_0 | ||
| 53 | end | ||
| 54 | }) | ||
| 55 | _base_0.__class = _class_0 | ||
| 56 | TestClass = _class_0 | ||
| 57 | end | ||
| 58 | local instance = TestClass() | ||
| 59 | return assert.same(instance:get_value(), 100) | ||
| 60 | end) | ||
| 61 | it("should handle macro definitions", function() | ||
| 62 | local result = (5 + 1) | ||
| 63 | return assert.same(result, 6) | ||
| 64 | end) | ||
| 65 | it("should handle import statements", function() | ||
| 66 | local format | ||
| 67 | do | ||
| 68 | local _obj_0 = require("string") | ||
| 69 | format = _obj_0.format | ||
| 70 | end | ||
| 71 | return assert.is_true(type(format) == "function") | ||
| 72 | end) | ||
| 73 | it("should handle string interpolation", function() | ||
| 74 | local name = "world" | ||
| 75 | local result = "hello " .. tostring(name) | ||
| 76 | return assert.same(result, "hello world") | ||
| 77 | end) | ||
| 78 | it("should handle comprehensions", function() | ||
| 79 | local result | ||
| 80 | do | ||
| 81 | local _accum_0 = { } | ||
| 82 | local _len_0 = 1 | ||
| 83 | for x = 1, 5 do | ||
| 84 | _accum_0[_len_0] = x * 2 | ||
| 85 | _len_0 = _len_0 + 1 | ||
| 86 | end | ||
| 87 | result = _accum_0 | ||
| 88 | end | ||
| 89 | return assert.same(result, { | ||
| 90 | 2, | ||
| 91 | 4, | ||
| 92 | 6, | ||
| 93 | 8, | ||
| 94 | 10 | ||
| 95 | }) | ||
| 96 | end) | ||
| 97 | it("should handle switch expressions", function() | ||
| 98 | local result | ||
| 99 | do | ||
| 100 | local _exp_0 = 2 | ||
| 101 | if 1 == _exp_0 then | ||
| 102 | result = "one" | ||
| 103 | elseif 2 == _exp_0 then | ||
| 104 | result = "two" | ||
| 105 | else | ||
| 106 | result = "other" | ||
| 107 | end | ||
| 108 | end | ||
| 109 | return assert.same(result, "two") | ||
| 110 | end) | ||
| 111 | it("should handle with statements", function() | ||
| 112 | local obj = { | ||
| 113 | x = 10, | ||
| 114 | y = 20 | ||
| 115 | } | ||
| 116 | local result | ||
| 117 | do | ||
| 118 | local _accum_0 | ||
| 119 | repeat | ||
| 120 | _accum_0 = obj.x + obj.y | ||
| 121 | break | ||
| 122 | until true | ||
| 123 | result = _accum_0 | ||
| 124 | end | ||
| 125 | return assert.same(result, 30) | ||
| 126 | end) | ||
| 127 | it("should handle existential operators", function() | ||
| 128 | local obj = { | ||
| 129 | value = 100 | ||
| 130 | } | ||
| 131 | local result | ||
| 132 | if obj ~= nil then | ||
| 133 | result = obj.value | ||
| 134 | end | ||
| 135 | return assert.same(result, 100) | ||
| 136 | end) | ||
| 137 | it("should handle pipe operator", function() | ||
| 138 | local result = table.concat({ | ||
| 139 | 1, | ||
| 140 | 2, | ||
| 141 | 3 | ||
| 142 | }) | ||
| 143 | return assert.same(result, "123") | ||
| 144 | end) | ||
| 145 | it("should handle loops", function() | ||
| 146 | local sum = 0 | ||
| 147 | for i = 1, 5 do | ||
| 148 | sum = sum + i | ||
| 149 | end | ||
| 150 | return assert.same(sum, 15) | ||
| 151 | end) | ||
| 152 | it("should handle while loops", function() | ||
| 153 | local count = 0 | ||
| 154 | while count < 3 do | ||
| 155 | count = count + 1 | ||
| 156 | end | ||
| 157 | return assert.same(count, 3) | ||
| 158 | end) | ||
| 159 | it("should handle table literals", function() | ||
| 160 | local t = { | ||
| 161 | key1 = "value1", | ||
| 162 | key2 = "value2" | ||
| 163 | } | ||
| 164 | return assert.same(t.key1, "value1") | ||
| 165 | end) | ||
| 166 | it("should handle function definitions", function() | ||
| 167 | local fn | ||
| 168 | fn = function(a, b) | ||
| 169 | return a + b | ||
| 170 | end | ||
| 171 | return assert.same(fn(5, 3), 8) | ||
| 172 | end) | ||
| 173 | it("should handle nested functions", function() | ||
| 174 | local outer | ||
| 175 | outer = function() | ||
| 176 | local inner | ||
| 177 | inner = function(x) | ||
| 178 | return x * 2 | ||
| 179 | end | ||
| 180 | return inner(10) | ||
| 181 | end | ||
| 182 | return assert.same(outer(), 20) | ||
| 183 | end) | ||
| 184 | return it("should handle destructure", function() | ||
| 185 | local t = { | ||
| 186 | x = 1, | ||
| 187 | y = 2, | ||
| 188 | z = 3 | ||
| 189 | } | ||
| 190 | local x, y, z = t.x, t.y, t.z | ||
| 191 | assert.same(x, 1) | ||
| 192 | assert.same(y, 2) | ||
| 193 | return assert.same(z, 3) | ||
| 194 | end) | ||
| 195 | end) | ||
