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/export_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/export_spec.lua')
| -rw-r--r-- | spec/outputs/test/export_spec.lua | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/spec/outputs/test/export_spec.lua b/spec/outputs/test/export_spec.lua new file mode 100644 index 0000000..92cb402 --- /dev/null +++ b/spec/outputs/test/export_spec.lua | |||
| @@ -0,0 +1,153 @@ | |||
| 1 | return describe("export", function() | ||
| 2 | it("should export basic variables", function() | ||
| 3 | local a = 1 | ||
| 4 | local b = 2 | ||
| 5 | local c = 3 | ||
| 6 | assert.same(a, 1) | ||
| 7 | assert.same(b, 2) | ||
| 8 | return assert.same(c, 3) | ||
| 9 | end) | ||
| 10 | it("should export multiple variables at once", function() | ||
| 11 | local x, y, z = 10, 20, 30 | ||
| 12 | assert.same(x, 10) | ||
| 13 | assert.same(y, 20) | ||
| 14 | return assert.same(z, 30) | ||
| 15 | end) | ||
| 16 | it("should export class definitions", function() | ||
| 17 | local MyClass | ||
| 18 | do | ||
| 19 | local _class_0 | ||
| 20 | local _base_0 = { | ||
| 21 | value = 100 | ||
| 22 | } | ||
| 23 | if _base_0.__index == nil then | ||
| 24 | _base_0.__index = _base_0 | ||
| 25 | end | ||
| 26 | _class_0 = setmetatable({ | ||
| 27 | __init = function() end, | ||
| 28 | __base = _base_0, | ||
| 29 | __name = "MyClass" | ||
| 30 | }, { | ||
| 31 | __index = _base_0, | ||
| 32 | __call = function(cls, ...) | ||
| 33 | local _self_0 = setmetatable({ }, _base_0) | ||
| 34 | cls.__init(_self_0, ...) | ||
| 35 | return _self_0 | ||
| 36 | end | ||
| 37 | }) | ||
| 38 | _base_0.__class = _class_0 | ||
| 39 | MyClass = _class_0 | ||
| 40 | end | ||
| 41 | return assert.same(MyClass.value, 100) | ||
| 42 | end) | ||
| 43 | it("should export function expressions", function() | ||
| 44 | local my_func | ||
| 45 | my_func = function() | ||
| 46 | return 42 | ||
| 47 | end | ||
| 48 | return assert.same(my_func(), 42) | ||
| 49 | end) | ||
| 50 | it("should export conditional expressions", function() | ||
| 51 | local result | ||
| 52 | if true then | ||
| 53 | result = "yes" | ||
| 54 | else | ||
| 55 | result = "no" | ||
| 56 | end | ||
| 57 | return assert.same(result, "yes") | ||
| 58 | end) | ||
| 59 | it("should export switch expressions", function() | ||
| 60 | local value | ||
| 61 | do | ||
| 62 | local _exp_0 = 5 | ||
| 63 | if 5 == _exp_0 then | ||
| 64 | value = 100 | ||
| 65 | else | ||
| 66 | value = 0 | ||
| 67 | end | ||
| 68 | end | ||
| 69 | return assert.same(value, 100) | ||
| 70 | end) | ||
| 71 | it("should export with do block", function() | ||
| 72 | local result | ||
| 73 | do | ||
| 74 | local x = 5 | ||
| 75 | result = x * 2 | ||
| 76 | end | ||
| 77 | return assert.same(result, 10) | ||
| 78 | end) | ||
| 79 | it("should export comprehension", function() | ||
| 80 | local doubled | ||
| 81 | do | ||
| 82 | local _accum_0 = { } | ||
| 83 | local _len_0 = 1 | ||
| 84 | for i = 1, 5 do | ||
| 85 | _accum_0[_len_0] = i * 2 | ||
| 86 | _len_0 = _len_0 + 1 | ||
| 87 | end | ||
| 88 | doubled = _accum_0 | ||
| 89 | end | ||
| 90 | return assert.same(doubled, { | ||
| 91 | 2, | ||
| 92 | 4, | ||
| 93 | 6, | ||
| 94 | 8, | ||
| 95 | 10 | ||
| 96 | }) | ||
| 97 | end) | ||
| 98 | it("should export with pipe operator", function() | ||
| 99 | local result = table.concat({ | ||
| 100 | 1, | ||
| 101 | 2, | ||
| 102 | 3 | ||
| 103 | }) | ||
| 104 | return assert.same(result, "123") | ||
| 105 | end) | ||
| 106 | it("should export nil values", function() | ||
| 107 | local empty = nil | ||
| 108 | return assert.same(empty, nil) | ||
| 109 | end) | ||
| 110 | it("should export tables", function() | ||
| 111 | local config = { | ||
| 112 | key1 = "value1", | ||
| 113 | key2 = "value2" | ||
| 114 | } | ||
| 115 | assert.same(config.key1, "value1") | ||
| 116 | return assert.same(config.key2, "value2") | ||
| 117 | end) | ||
| 118 | it("should export string values", function() | ||
| 119 | local message = "hello world" | ||
| 120 | return assert.same(message, "hello world") | ||
| 121 | end) | ||
| 122 | it("should export boolean values", function() | ||
| 123 | local flag_true = true | ||
| 124 | local flag_false = false | ||
| 125 | assert.is_true(flag_true) | ||
| 126 | return assert.is_false(flag_false) | ||
| 127 | end) | ||
| 128 | it("should export number values", function() | ||
| 129 | local count = 42 | ||
| 130 | local price = 19.99 | ||
| 131 | assert.same(count, 42) | ||
| 132 | return assert.same(price, 19.99) | ||
| 133 | end) | ||
| 134 | it("should export function with parameters", function() | ||
| 135 | local add | ||
| 136 | add = function(a, b) | ||
| 137 | return a + b | ||
| 138 | end | ||
| 139 | return assert.same(add(5, 3), 8) | ||
| 140 | end) | ||
| 141 | it("should maintain export order", function() | ||
| 142 | local first = 1 | ||
| 143 | local second = 2 | ||
| 144 | local third = 3 | ||
| 145 | assert.same(first, 1) | ||
| 146 | assert.same(second, 2) | ||
| 147 | return assert.same(third, 3) | ||
| 148 | end) | ||
| 149 | return it("should work with complex expressions", function() | ||
| 150 | local calc = (10 + 20) * 2 | ||
| 151 | return assert.same(calc, 60) | ||
| 152 | end) | ||
| 153 | end) | ||
