diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-01-30 18:16:45 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-01-30 18:16:45 +0800 |
| commit | 8c3d786157ec7fef3072feac55c2d5450800568b (patch) | |
| tree | 6c44a85e02abe74e6c3ccc4d7393ba8784c49ce7 /spec/outputs/test/constructor_promotion_spec.lua | |
| parent | 220a10d0df3341b2bbb0beaee4f90d6480e7ae38 (diff) | |
| download | yuescript-main.tar.gz yuescript-main.tar.bz2 yuescript-main.zip | |
Diffstat (limited to 'spec/outputs/test/constructor_promotion_spec.lua')
| -rw-r--r-- | spec/outputs/test/constructor_promotion_spec.lua | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/spec/outputs/test/constructor_promotion_spec.lua b/spec/outputs/test/constructor_promotion_spec.lua new file mode 100644 index 0000000..5caa762 --- /dev/null +++ b/spec/outputs/test/constructor_promotion_spec.lua | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | return describe("constructor promotion", function() | ||
| 2 | it("should promote simple arguments to assignment", function() | ||
| 3 | local Thing | ||
| 4 | do | ||
| 5 | local _class_0 | ||
| 6 | local _base_0 = { } | ||
| 7 | if _base_0.__index == nil then | ||
| 8 | _base_0.__index = _base_0 | ||
| 9 | end | ||
| 10 | _class_0 = setmetatable({ | ||
| 11 | __init = function(self, name, age) | ||
| 12 | self.name = name | ||
| 13 | self.age = age | ||
| 14 | end, | ||
| 15 | __base = _base_0, | ||
| 16 | __name = "Thing" | ||
| 17 | }, { | ||
| 18 | __index = _base_0, | ||
| 19 | __call = function(cls, ...) | ||
| 20 | local _self_0 = setmetatable({ }, _base_0) | ||
| 21 | cls.__init(_self_0, ...) | ||
| 22 | return _self_0 | ||
| 23 | end | ||
| 24 | }) | ||
| 25 | _base_0.__class = _class_0 | ||
| 26 | Thing = _class_0 | ||
| 27 | end | ||
| 28 | local instance = Thing("Alice", 30) | ||
| 29 | assert.same(instance.name, "Alice") | ||
| 30 | return assert.same(instance.age, 30) | ||
| 31 | end) | ||
| 32 | it("should promote multiple arguments", function() | ||
| 33 | local Point | ||
| 34 | do | ||
| 35 | local _class_0 | ||
| 36 | local _base_0 = { } | ||
| 37 | if _base_0.__index == nil then | ||
| 38 | _base_0.__index = _base_0 | ||
| 39 | end | ||
| 40 | _class_0 = setmetatable({ | ||
| 41 | __init = function(self, x, y, z) | ||
| 42 | self.x = x | ||
| 43 | self.y = y | ||
| 44 | self.z = z | ||
| 45 | end, | ||
| 46 | __base = _base_0, | ||
| 47 | __name = "Point" | ||
| 48 | }, { | ||
| 49 | __index = _base_0, | ||
| 50 | __call = function(cls, ...) | ||
| 51 | local _self_0 = setmetatable({ }, _base_0) | ||
| 52 | cls.__init(_self_0, ...) | ||
| 53 | return _self_0 | ||
| 54 | end | ||
| 55 | }) | ||
| 56 | _base_0.__class = _class_0 | ||
| 57 | Point = _class_0 | ||
| 58 | end | ||
| 59 | local p = Point(1, 2, 3) | ||
| 60 | assert.same(p.x, 1) | ||
| 61 | assert.same(p.y, 2) | ||
| 62 | return assert.same(p.z, 3) | ||
| 63 | end) | ||
| 64 | return it("should work with multiple parameters", function() | ||
| 65 | local Container | ||
| 66 | do | ||
| 67 | local _class_0 | ||
| 68 | local _base_0 = { } | ||
| 69 | if _base_0.__index == nil then | ||
| 70 | _base_0.__index = _base_0 | ||
| 71 | end | ||
| 72 | _class_0 = setmetatable({ | ||
| 73 | __init = function(self, a, b, c) | ||
| 74 | self.a = a | ||
| 75 | self.b = b | ||
| 76 | self.c = c | ||
| 77 | end, | ||
| 78 | __base = _base_0, | ||
| 79 | __name = "Container" | ||
| 80 | }, { | ||
| 81 | __index = _base_0, | ||
| 82 | __call = function(cls, ...) | ||
| 83 | local _self_0 = setmetatable({ }, _base_0) | ||
| 84 | cls.__init(_self_0, ...) | ||
| 85 | return _self_0 | ||
| 86 | end | ||
| 87 | }) | ||
| 88 | _base_0.__class = _class_0 | ||
| 89 | Container = _class_0 | ||
| 90 | end | ||
| 91 | local c = Container() | ||
| 92 | assert.same(c.a, nil) | ||
| 93 | assert.same(c.b, nil) | ||
| 94 | return assert.same(c.c, nil) | ||
| 95 | end) | ||
| 96 | end) | ||
