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/anonymous_class_spec.lua | |
| parent | 220a10d0df3341b2bbb0beaee4f90d6480e7ae38 (diff) | |
| download | yuescript-8c3d786157ec7fef3072feac55c2d5450800568b.tar.gz yuescript-8c3d786157ec7fef3072feac55c2d5450800568b.tar.bz2 yuescript-8c3d786157ec7fef3072feac55c2d5450800568b.zip | |
Diffstat (limited to 'spec/outputs/test/anonymous_class_spec.lua')
| -rw-r--r-- | spec/outputs/test/anonymous_class_spec.lua | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/spec/outputs/test/anonymous_class_spec.lua b/spec/outputs/test/anonymous_class_spec.lua new file mode 100644 index 0000000..8f6a122 --- /dev/null +++ b/spec/outputs/test/anonymous_class_spec.lua | |||
| @@ -0,0 +1,142 @@ | |||
| 1 | return describe("anonymous class", function() | ||
| 2 | it("should create anonymous class", function() | ||
| 3 | local AnonymousClass | ||
| 4 | do | ||
| 5 | local _class_0 | ||
| 6 | local _base_0 = { | ||
| 7 | value = 100, | ||
| 8 | getValue = function(self) | ||
| 9 | return self.value | ||
| 10 | end | ||
| 11 | } | ||
| 12 | if _base_0.__index == nil then | ||
| 13 | _base_0.__index = _base_0 | ||
| 14 | end | ||
| 15 | _class_0 = setmetatable({ | ||
| 16 | __init = function() end, | ||
| 17 | __base = _base_0, | ||
| 18 | __name = "AnonymousClass" | ||
| 19 | }, { | ||
| 20 | __index = _base_0, | ||
| 21 | __call = function(cls, ...) | ||
| 22 | local _self_0 = setmetatable({ }, _base_0) | ||
| 23 | cls.__init(_self_0, ...) | ||
| 24 | return _self_0 | ||
| 25 | end | ||
| 26 | }) | ||
| 27 | _base_0.__class = _class_0 | ||
| 28 | AnonymousClass = _class_0 | ||
| 29 | end | ||
| 30 | local instance = AnonymousClass() | ||
| 31 | return assert.same(instance:getValue(), 100) | ||
| 32 | end) | ||
| 33 | it("should use assigned name", function() | ||
| 34 | local MyClass | ||
| 35 | do | ||
| 36 | local _class_0 | ||
| 37 | local _base_0 = { | ||
| 38 | value = 50 | ||
| 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 = "MyClass" | ||
| 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 | MyClass = _class_0 | ||
| 57 | end | ||
| 58 | local instance = MyClass() | ||
| 59 | assert.is_true(MyClass.__name == "MyClass") | ||
| 60 | return assert.same(instance.value, 50) | ||
| 61 | end) | ||
| 62 | return it("should support anonymous subclass", function() | ||
| 63 | local Base | ||
| 64 | do | ||
| 65 | local _class_0 | ||
| 66 | local _base_0 = { | ||
| 67 | baseMethod = function(self) | ||
| 68 | return "base" | ||
| 69 | end | ||
| 70 | } | ||
| 71 | if _base_0.__index == nil then | ||
| 72 | _base_0.__index = _base_0 | ||
| 73 | end | ||
| 74 | _class_0 = setmetatable({ | ||
| 75 | __init = function() end, | ||
| 76 | __base = _base_0, | ||
| 77 | __name = "Base" | ||
| 78 | }, { | ||
| 79 | __index = _base_0, | ||
| 80 | __call = function(cls, ...) | ||
| 81 | local _self_0 = setmetatable({ }, _base_0) | ||
| 82 | cls.__init(_self_0, ...) | ||
| 83 | return _self_0 | ||
| 84 | end | ||
| 85 | }) | ||
| 86 | _base_0.__class = _class_0 | ||
| 87 | Base = _class_0 | ||
| 88 | end | ||
| 89 | local Sub | ||
| 90 | do | ||
| 91 | local _class_0 | ||
| 92 | local _parent_0 = Base | ||
| 93 | local _base_0 = { | ||
| 94 | subMethod = function(self) | ||
| 95 | return "sub" | ||
| 96 | end | ||
| 97 | } | ||
| 98 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
| 99 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
| 100 | _base_0[_key_0] = _val_0 | ||
| 101 | end | ||
| 102 | end | ||
| 103 | if _base_0.__index == nil then | ||
| 104 | _base_0.__index = _base_0 | ||
| 105 | end | ||
| 106 | setmetatable(_base_0, _parent_0.__base) | ||
| 107 | _class_0 = setmetatable({ | ||
| 108 | __init = function(self, ...) | ||
| 109 | return _class_0.__parent.__init(self, ...) | ||
| 110 | end, | ||
| 111 | __base = _base_0, | ||
| 112 | __name = "Sub", | ||
| 113 | __parent = _parent_0 | ||
| 114 | }, { | ||
| 115 | __index = function(cls, name) | ||
| 116 | local val = rawget(_base_0, name) | ||
| 117 | if val == nil then | ||
| 118 | local parent = rawget(cls, "__parent") | ||
| 119 | if parent then | ||
| 120 | return parent[name] | ||
| 121 | end | ||
| 122 | else | ||
| 123 | return val | ||
| 124 | end | ||
| 125 | end, | ||
| 126 | __call = function(cls, ...) | ||
| 127 | local _self_0 = setmetatable({ }, _base_0) | ||
| 128 | cls.__init(_self_0, ...) | ||
| 129 | return _self_0 | ||
| 130 | end | ||
| 131 | }) | ||
| 132 | _base_0.__class = _class_0 | ||
| 133 | if _parent_0.__inherited then | ||
| 134 | _parent_0.__inherited(_parent_0, _class_0) | ||
| 135 | end | ||
| 136 | Sub = _class_0 | ||
| 137 | end | ||
| 138 | local instance = Sub() | ||
| 139 | assert.same(instance:baseMethod(), "base") | ||
| 140 | return assert.same(instance:subMethod(), "sub") | ||
| 141 | end) | ||
| 142 | end) | ||
