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/import_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/import_spec.lua')
| -rw-r--r-- | spec/outputs/test/import_spec.lua | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/spec/outputs/test/import_spec.lua b/spec/outputs/test/import_spec.lua new file mode 100644 index 0000000..d1feafa --- /dev/null +++ b/spec/outputs/test/import_spec.lua | |||
| @@ -0,0 +1,196 @@ | |||
| 1 | return describe("import", function() | ||
| 2 | it("should import from table expression", function() | ||
| 3 | local source = { | ||
| 4 | hello = "world", | ||
| 5 | foo = "bar" | ||
| 6 | } | ||
| 7 | local hello, foo = source.hello, source.foo | ||
| 8 | assert.same(hello, "world") | ||
| 9 | return assert.same(foo, "bar") | ||
| 10 | end) | ||
| 11 | it("should import with backslash escaping", function() | ||
| 12 | local source = { | ||
| 13 | x = 1, | ||
| 14 | y = function(self) end, | ||
| 15 | z = 3 | ||
| 16 | } | ||
| 17 | local x, y, z = source.x, (function() | ||
| 18 | local _base_0 = source | ||
| 19 | local _fn_0 = _base_0.y | ||
| 20 | return _fn_0 and function(...) | ||
| 21 | return _fn_0(_base_0, ...) | ||
| 22 | end | ||
| 23 | end)(), source.z | ||
| 24 | assert.same(x, 1) | ||
| 25 | assert.same("function", type(y)) | ||
| 26 | return assert.same(z, 3) | ||
| 27 | end) | ||
| 28 | it("should import from string module", function() | ||
| 29 | local format | ||
| 30 | do | ||
| 31 | local _obj_0 = require("string") | ||
| 32 | format = _obj_0.format | ||
| 33 | end | ||
| 34 | return assert.is_true(type(format) == "function") | ||
| 35 | end) | ||
| 36 | it("should import from table with dot path", function() | ||
| 37 | local sub | ||
| 38 | do | ||
| 39 | local _obj_0 = require("string") | ||
| 40 | sub = _obj_0.sub | ||
| 41 | end | ||
| 42 | local result = sub("hello", 1, 2) | ||
| 43 | return assert.same(result, "he") | ||
| 44 | end) | ||
| 45 | it("should import multiple values with table destructuring", function() | ||
| 46 | local source = { | ||
| 47 | a = 1, | ||
| 48 | b = 2, | ||
| 49 | c = 3 | ||
| 50 | } | ||
| 51 | local a, b, c = source.a, source.b, source.c | ||
| 52 | assert.same(a, 1) | ||
| 53 | assert.same(b, 2) | ||
| 54 | return assert.same(c, 3) | ||
| 55 | end) | ||
| 56 | it("should import with multi-line format", function() | ||
| 57 | local source = { | ||
| 58 | x = 1, | ||
| 59 | y = 2, | ||
| 60 | z = 3 | ||
| 61 | } | ||
| 62 | local x, y, z = source.x, source.y, source.z | ||
| 63 | assert.same(x, 1) | ||
| 64 | assert.same(y, 2) | ||
| 65 | return assert.same(z, 3) | ||
| 66 | end) | ||
| 67 | it("should import using from syntax", function() | ||
| 68 | local source = { | ||
| 69 | foo = "bar", | ||
| 70 | baz = "qux" | ||
| 71 | } | ||
| 72 | local foo, baz = source.foo, source.baz | ||
| 73 | assert.same(foo, "bar") | ||
| 74 | return assert.same(baz, "qux") | ||
| 75 | end) | ||
| 76 | it("should handle import with computed expressions", function() | ||
| 77 | local source = { | ||
| 78 | first = 1, | ||
| 79 | second = 2 | ||
| 80 | } | ||
| 81 | local target = source | ||
| 82 | local first, second = target.first, target.second | ||
| 83 | assert.same(first, 1) | ||
| 84 | return assert.same(second, 2) | ||
| 85 | end) | ||
| 86 | it("should import from nested table paths", function() | ||
| 87 | local deep = { | ||
| 88 | outer = { | ||
| 89 | inner = "value" | ||
| 90 | } | ||
| 91 | } | ||
| 92 | local outer = deep.outer | ||
| 93 | return assert.same(outer.inner, "value") | ||
| 94 | end) | ||
| 95 | it("should support importing Lua standard library functions", function() | ||
| 96 | local print, type | ||
| 97 | do | ||
| 98 | local _obj_0 = require("_G") | ||
| 99 | print, type = _obj_0.print, _obj_0.type | ||
| 100 | end | ||
| 101 | assert.is_true(type(print) == "function") | ||
| 102 | return assert.is_true(type(type) == "function") | ||
| 103 | end) | ||
| 104 | it("should handle empty import gracefully", function() | ||
| 105 | local source = { } | ||
| 106 | local dummy = source.dummy | ||
| 107 | return assert.same(dummy, nil) | ||
| 108 | end) | ||
| 109 | it("should work with table index expressions", function() | ||
| 110 | local source = { | ||
| 111 | normal = "ok" | ||
| 112 | } | ||
| 113 | local normal = source.normal | ||
| 114 | return assert.same(normal, "ok") | ||
| 115 | end) | ||
| 116 | it("should support chaining imports from same source", function() | ||
| 117 | local source = { | ||
| 118 | a = 1, | ||
| 119 | b = 2, | ||
| 120 | c = 3 | ||
| 121 | } | ||
| 122 | local a, b = source.a, source.b | ||
| 123 | local c = source.c | ||
| 124 | assert.same(a, 1) | ||
| 125 | assert.same(b, 2) | ||
| 126 | return assert.same(c, 3) | ||
| 127 | end) | ||
| 128 | it("should handle importing from table returned by function", function() | ||
| 129 | local get_table | ||
| 130 | get_table = function() | ||
| 131 | return { | ||
| 132 | x = 100, | ||
| 133 | y = 200 | ||
| 134 | } | ||
| 135 | end | ||
| 136 | local x, y | ||
| 137 | do | ||
| 138 | local _obj_0 = get_table() | ||
| 139 | x, y = _obj_0.x, _obj_0.y | ||
| 140 | end | ||
| 141 | assert.same(x, 100) | ||
| 142 | return assert.same(y, 200) | ||
| 143 | end) | ||
| 144 | it("should support from with multi-line import", function() | ||
| 145 | local source = { | ||
| 146 | item1 = 1, | ||
| 147 | item2 = 2, | ||
| 148 | item3 = 3 | ||
| 149 | } | ||
| 150 | local item1, item2, item3 = source.item1, source.item2, source.item3 | ||
| 151 | assert.same(item1, 1) | ||
| 152 | assert.same(item2, 2) | ||
| 153 | return assert.same(item3, 3) | ||
| 154 | end) | ||
| 155 | it("should work with import from string literal", function() | ||
| 156 | local char | ||
| 157 | do | ||
| 158 | local _obj_0 = require("string") | ||
| 159 | char = _obj_0.char | ||
| 160 | end | ||
| 161 | return assert.same(char(65), "A") | ||
| 162 | end) | ||
| 163 | it("should support import with table literal keys", function() | ||
| 164 | local source = { | ||
| 165 | normal_key = "value2" | ||
| 166 | } | ||
| 167 | local normal_key = source.normal_key | ||
| 168 | return assert.same(normal_key, "value2") | ||
| 169 | end) | ||
| 170 | it("should handle consecutive imports", function() | ||
| 171 | local source1 = { | ||
| 172 | a = 1 | ||
| 173 | } | ||
| 174 | local source2 = { | ||
| 175 | b = 2 | ||
| 176 | } | ||
| 177 | local a = source1.a | ||
| 178 | local b = source2.b | ||
| 179 | assert.same(a, 1) | ||
| 180 | return assert.same(b, 2) | ||
| 181 | end) | ||
| 182 | return it("should support importing from complex expressions", function() | ||
| 183 | local get_source | ||
| 184 | get_source = function() | ||
| 185 | return { | ||
| 186 | result = 42 | ||
| 187 | } | ||
| 188 | end | ||
| 189 | local result | ||
| 190 | do | ||
| 191 | local _obj_0 = get_source() | ||
| 192 | result = _obj_0.result | ||
| 193 | end | ||
| 194 | return assert.same(result, 42) | ||
| 195 | end) | ||
| 196 | end) | ||
