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/macro_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/macro_spec.lua')
| -rw-r--r-- | spec/outputs/test/macro_spec.lua | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/spec/outputs/test/macro_spec.lua b/spec/outputs/test/macro_spec.lua new file mode 100644 index 0000000..9c50548 --- /dev/null +++ b/spec/outputs/test/macro_spec.lua | |||
| @@ -0,0 +1,166 @@ | |||
| 1 | return describe("macro", function() | ||
| 2 | it("should define and call basic macro", function() | ||
| 3 | local result = (5 * 2) | ||
| 4 | return assert.same(result, 10) | ||
| 5 | end) | ||
| 6 | it("should maintain hygiene in macros", function() | ||
| 7 | local a = 8 | ||
| 8 | local result = 2 | ||
| 9 | return assert.same(result, 2) | ||
| 10 | end) | ||
| 11 | it("should validate AST types", function() | ||
| 12 | local result = { | ||
| 13 | 123, | ||
| 14 | 'xyz' | ||
| 15 | } | ||
| 16 | return assert.same(result, { | ||
| 17 | 123, | ||
| 18 | 'xyz' | ||
| 19 | }) | ||
| 20 | end) | ||
| 21 | it("should support simple code generation", function() | ||
| 22 | local result = (10 + 1) | ||
| 23 | return assert.same(result, 11) | ||
| 24 | end) | ||
| 25 | it("should support nested macro calls", function() | ||
| 26 | local result = 7 | ||
| 27 | return assert.same(result, 7) | ||
| 28 | end) | ||
| 29 | it("should respect macro scope in do blocks", function() | ||
| 30 | do | ||
| 31 | local result = 'inner' | ||
| 32 | assert.same(result, "inner") | ||
| 33 | end | ||
| 34 | local result = 'outer' | ||
| 35 | return assert.same(result, "outer") | ||
| 36 | end) | ||
| 37 | it("should provide $LINE macro", function() | ||
| 38 | local line_num = 42 | ||
| 39 | return assert.is_true(line_num > 0) | ||
| 40 | end) | ||
| 41 | it("should inject Lua code", function() | ||
| 42 | local x = 0 | ||
| 43 | do | ||
| 44 | local function f(a) | ||
| 45 | return a + 1 | ||
| 46 | end | ||
| 47 | x = x + f(3) | ||
| 48 | end | ||
| 49 | return assert.same(x, 4) | ||
| 50 | end) | ||
| 51 | it("should work in conditional compilation", function() | ||
| 52 | local result = "debug mode" | ||
| 53 | return assert.same(result, "debug mode") | ||
| 54 | end) | ||
| 55 | it("should work with class system", function() | ||
| 56 | local Thing | ||
| 57 | do | ||
| 58 | local _class_0 | ||
| 59 | local _base_0 = { | ||
| 60 | value = 100, | ||
| 61 | get_value = function(self) | ||
| 62 | return self.value | ||
| 63 | end | ||
| 64 | } | ||
| 65 | if _base_0.__index == nil then | ||
| 66 | _base_0.__index = _base_0 | ||
| 67 | end | ||
| 68 | _class_0 = setmetatable({ | ||
| 69 | __init = function() end, | ||
| 70 | __base = _base_0, | ||
| 71 | __name = "Thing" | ||
| 72 | }, { | ||
| 73 | __index = _base_0, | ||
| 74 | __call = function(cls, ...) | ||
| 75 | local _self_0 = setmetatable({ }, _base_0) | ||
| 76 | cls.__init(_self_0, ...) | ||
| 77 | return _self_0 | ||
| 78 | end | ||
| 79 | }) | ||
| 80 | _base_0.__class = _class_0 | ||
| 81 | Thing = _class_0 | ||
| 82 | end | ||
| 83 | local instance = Thing() | ||
| 84 | return assert.same(instance:get_value(), 100) | ||
| 85 | end) | ||
| 86 | it("should handle macro in switch expressions", function() | ||
| 87 | local result | ||
| 88 | do | ||
| 89 | local _exp_0 = "test" | ||
| 90 | if "test" == _exp_0 then | ||
| 91 | result = "matched" | ||
| 92 | else | ||
| 93 | result = "no match" | ||
| 94 | end | ||
| 95 | end | ||
| 96 | return assert.same(result, "matched") | ||
| 97 | end) | ||
| 98 | it("should support macro in expression context", function() | ||
| 99 | local result = 5 + (2 * 3) | ||
| 100 | return assert.same(result, 11) | ||
| 101 | end) | ||
| 102 | it("should handle $is_ast for type checking", function() | ||
| 103 | local result = 42 | ||
| 104 | return assert.same(result, 42) | ||
| 105 | end) | ||
| 106 | it("should work with string interpolation", function() | ||
| 107 | local result = { | ||
| 108 | ["test"] = 123 | ||
| 109 | } | ||
| 110 | return assert.same(result, { | ||
| 111 | test = 123 | ||
| 112 | }) | ||
| 113 | end) | ||
| 114 | it("should support function call syntax", function() | ||
| 115 | local result = (5 + 10) | ||
| 116 | return assert.same(result, 15) | ||
| 117 | end) | ||
| 118 | it("should handle empty macro return", function() | ||
| 119 | local a = 1 | ||
| 120 | a = 2 | ||
| 121 | return assert.same(a, 2) | ||
| 122 | end) | ||
| 123 | it("should work with table literals", function() | ||
| 124 | local point = { | ||
| 125 | x = 10, | ||
| 126 | y = 20 | ||
| 127 | } | ||
| 128 | assert.same(point.x, 10) | ||
| 129 | return assert.same(point.y, 20) | ||
| 130 | end) | ||
| 131 | it("should support conditional expressions in macro", function() | ||
| 132 | local result = (5 + 1) | ||
| 133 | return assert.same(result, 6) | ||
| 134 | end) | ||
| 135 | it("should work with comprehension", function() | ||
| 136 | local result | ||
| 137 | do | ||
| 138 | local _accum_0 = { } | ||
| 139 | local _len_0 = 1 | ||
| 140 | local _list_0 = { | ||
| 141 | 1, | ||
| 142 | 2, | ||
| 143 | 3 | ||
| 144 | } | ||
| 145 | for _index_0 = 1, #_list_0 do | ||
| 146 | local _ = _list_0[_index_0] | ||
| 147 | _accum_0[_len_0] = _ * 2 | ||
| 148 | _len_0 = _len_0 + 1 | ||
| 149 | end | ||
| 150 | result = _accum_0 | ||
| 151 | end | ||
| 152 | return assert.same(result, { | ||
| 153 | 2, | ||
| 154 | 4, | ||
| 155 | 6 | ||
| 156 | }) | ||
| 157 | end) | ||
| 158 | it("should support complex expression macros", function() | ||
| 159 | local result = (1 + 2 * 3) | ||
| 160 | return assert.same(result, 7) | ||
| 161 | end) | ||
| 162 | return it("should work with string literals", function() | ||
| 163 | local result = ('Hello, ' .. "World") | ||
| 164 | return assert.same(result, "Hello, World") | ||
| 165 | end) | ||
| 166 | end) | ||
