diff options
author | Li Jin <dragon-fly@qq.com> | 2022-11-08 10:20:52 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-11-09 11:29:32 +0800 |
commit | b9b1cf94e1324e344df6f7204c8d479329c56718 (patch) | |
tree | b288f3c48bfd17f673968f101f0f6f886f00c3c9 /spec/outputs/test/class_spec.lua | |
parent | 95fd6ea93c2af6fb1707e480669d84ebe37fccbe (diff) | |
download | yuescript-b9b1cf94e1324e344df6f7204c8d479329c56718.tar.gz yuescript-b9b1cf94e1324e344df6f7204c8d479329c56718.tar.bz2 yuescript-b9b1cf94e1324e344df6f7204c8d479329c56718.zip |
fix issue #114.
Diffstat (limited to 'spec/outputs/test/class_spec.lua')
-rw-r--r-- | spec/outputs/test/class_spec.lua | 186 |
1 files changed, 185 insertions, 1 deletions
diff --git a/spec/outputs/test/class_spec.lua b/spec/outputs/test/class_spec.lua index b4062e0..e08b5aa 100644 --- a/spec/outputs/test/class_spec.lua +++ b/spec/outputs/test/class_spec.lua | |||
@@ -129,6 +129,11 @@ return describe("class", function() | |||
129 | local _base_0 = { | 129 | local _base_0 = { |
130 | color = "green" | 130 | color = "green" |
131 | } | 131 | } |
132 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
133 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
134 | _base_0[_key_0] = _val_0 | ||
135 | end | ||
136 | end | ||
132 | if _base_0.__index == nil then | 137 | if _base_0.__index == nil then |
133 | _base_0.__index = _base_0 | 138 | _base_0.__index = _base_0 |
134 | end | 139 | end |
@@ -195,6 +200,11 @@ return describe("class", function() | |||
195 | local _class_0 | 200 | local _class_0 |
196 | local _parent_0 = Base | 201 | local _parent_0 = Base |
197 | local _base_0 = { } | 202 | local _base_0 = { } |
203 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
204 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
205 | _base_0[_key_0] = _val_0 | ||
206 | end | ||
207 | end | ||
198 | if _base_0.__index == nil then | 208 | if _base_0.__index == nil then |
199 | _base_0.__index = _base_0 | 209 | _base_0.__index = _base_0 |
200 | end | 210 | end |
@@ -416,6 +426,11 @@ return describe("class", function() | |||
416 | local _class_0 | 426 | local _class_0 |
417 | local _parent_0 = Base | 427 | local _parent_0 = Base |
418 | local _base_0 = { } | 428 | local _base_0 = { } |
429 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
430 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
431 | _base_0[_key_0] = _val_0 | ||
432 | end | ||
433 | end | ||
419 | if _base_0.__index == nil then | 434 | if _base_0.__index == nil then |
420 | _base_0.__index = _base_0 | 435 | _base_0.__index = _base_0 |
421 | end | 436 | end |
@@ -493,6 +508,11 @@ return describe("class", function() | |||
493 | return ("%08d"):format(_class_0.__parent.__base.counter(self)) | 508 | return ("%08d"):format(_class_0.__parent.__base.counter(self)) |
494 | end | 509 | end |
495 | } | 510 | } |
511 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
512 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
513 | _base_0[_key_0] = _val_0 | ||
514 | end | ||
515 | end | ||
496 | if _base_0.__index == nil then | 516 | if _base_0.__index == nil then |
497 | _base_0.__index = _base_0 | 517 | _base_0.__index = _base_0 |
498 | end | 518 | end |
@@ -568,6 +588,11 @@ return describe("class", function() | |||
568 | return _class_0.__parent.counter(self) | 588 | return _class_0.__parent.counter(self) |
569 | end | 589 | end |
570 | } | 590 | } |
591 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
592 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
593 | _base_0[_key_0] = _val_0 | ||
594 | end | ||
595 | end | ||
571 | if _base_0.__index == nil then | 596 | if _base_0.__index == nil then |
572 | _base_0.__index = _base_0 | 597 | _base_0.__index = _base_0 |
573 | end | 598 | end |
@@ -638,6 +663,11 @@ return describe("class", function() | |||
638 | return _class_0.__parent | 663 | return _class_0.__parent |
639 | end | 664 | end |
640 | } | 665 | } |
666 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
667 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
668 | _base_0[_key_0] = _val_0 | ||
669 | end | ||
670 | end | ||
641 | if _base_0.__index == nil then | 671 | if _base_0.__index == nil then |
642 | _base_0.__index = _base_0 | 672 | _base_0.__index = _base_0 |
643 | end | 673 | end |
@@ -720,6 +750,11 @@ return describe("class", function() | |||
720 | end | 750 | end |
721 | end | 751 | end |
722 | } | 752 | } |
753 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
754 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
755 | _base_0[_key_0] = _val_0 | ||
756 | end | ||
757 | end | ||
723 | if _base_0.__index == nil then | 758 | if _base_0.__index == nil then |
724 | _base_0.__index = _base_0 | 759 | _base_0.__index = _base_0 |
725 | end | 760 | end |
@@ -790,6 +825,11 @@ return describe("class", function() | |||
790 | local _class_0 | 825 | local _class_0 |
791 | local _parent_0 = Thing | 826 | local _parent_0 = Thing |
792 | local _base_0 = { } | 827 | local _base_0 = { } |
828 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
829 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
830 | _base_0[_key_0] = _val_0 | ||
831 | end | ||
832 | end | ||
793 | if _base_0.__index == nil then | 833 | if _base_0.__index == nil then |
794 | _base_0.__index = _base_0 | 834 | _base_0.__index = _base_0 |
795 | end | 835 | end |
@@ -857,6 +897,11 @@ return describe("class", function() | |||
857 | local _class_0 | 897 | local _class_0 |
858 | local _parent_0 = Thing | 898 | local _parent_0 = Thing |
859 | local _base_0 = { } | 899 | local _base_0 = { } |
900 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
901 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
902 | _base_0[_key_0] = _val_0 | ||
903 | end | ||
904 | end | ||
860 | if _base_0.__index == nil then | 905 | if _base_0.__index == nil then |
861 | _base_0.__index = _base_0 | 906 | _base_0.__index = _base_0 |
862 | end | 907 | end |
@@ -969,6 +1014,11 @@ return describe("class", function() | |||
969 | return _class_0.__parent | 1014 | return _class_0.__parent |
970 | end | 1015 | end |
971 | } | 1016 | } |
1017 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1018 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1019 | _base_0[_key_0] = _val_0 | ||
1020 | end | ||
1021 | end | ||
972 | if _base_0.__index == nil then | 1022 | if _base_0.__index == nil then |
973 | _base_0.__index = _base_0 | 1023 | _base_0.__index = _base_0 |
974 | end | 1024 | end |
@@ -1050,6 +1100,11 @@ return describe("class", function() | |||
1050 | return _class_0.__parent.__base.a(self) + 2 | 1100 | return _class_0.__parent.__base.a(self) + 2 |
1051 | end | 1101 | end |
1052 | } | 1102 | } |
1103 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1104 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1105 | _base_0[_key_0] = _val_0 | ||
1106 | end | ||
1107 | end | ||
1053 | if _base_0.__index == nil then | 1108 | if _base_0.__index == nil then |
1054 | _base_0.__index = _base_0 | 1109 | _base_0.__index = _base_0 |
1055 | end | 1110 | end |
@@ -1094,6 +1149,11 @@ return describe("class", function() | |||
1094 | return _class_0.__parent.__base.a(self) + 3 | 1149 | return _class_0.__parent.__base.a(self) + 3 |
1095 | end | 1150 | end |
1096 | } | 1151 | } |
1152 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1153 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1154 | _base_0[_key_0] = _val_0 | ||
1155 | end | ||
1156 | end | ||
1097 | if _base_0.__index == nil then | 1157 | if _base_0.__index == nil then |
1098 | _base_0.__index = _base_0 | 1158 | _base_0.__index = _base_0 |
1099 | end | 1159 | end |
@@ -1164,6 +1224,11 @@ return describe("class", function() | |||
1164 | local _class_0 | 1224 | local _class_0 |
1165 | local _parent_0 = One | 1225 | local _parent_0 = One |
1166 | local _base_0 = { } | 1226 | local _base_0 = { } |
1227 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1228 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1229 | _base_0[_key_0] = _val_0 | ||
1230 | end | ||
1231 | end | ||
1167 | if _base_0.__index == nil then | 1232 | if _base_0.__index == nil then |
1168 | _base_0.__index = _base_0 | 1233 | _base_0.__index = _base_0 |
1169 | end | 1234 | end |
@@ -1208,6 +1273,11 @@ return describe("class", function() | |||
1208 | return _class_0.__parent.__base.a(self) + 3 | 1273 | return _class_0.__parent.__base.a(self) + 3 |
1209 | end | 1274 | end |
1210 | } | 1275 | } |
1276 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1277 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1278 | _base_0[_key_0] = _val_0 | ||
1279 | end | ||
1280 | end | ||
1211 | if _base_0.__index == nil then | 1281 | if _base_0.__index == nil then |
1212 | _base_0.__index = _base_0 | 1282 | _base_0.__index = _base_0 |
1213 | end | 1283 | end |
@@ -1252,6 +1322,11 @@ return describe("class", function() | |||
1252 | return _class_0.__parent.__base.a(self) + 4 | 1322 | return _class_0.__parent.__base.a(self) + 4 |
1253 | end | 1323 | end |
1254 | } | 1324 | } |
1325 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1326 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1327 | _base_0[_key_0] = _val_0 | ||
1328 | end | ||
1329 | end | ||
1255 | if _base_0.__index == nil then | 1330 | if _base_0.__index == nil then |
1256 | _base_0.__index = _base_0 | 1331 | _base_0.__index = _base_0 |
1257 | end | 1332 | end |
@@ -1330,6 +1405,11 @@ return describe("class", function() | |||
1330 | return _class_0.__parent.__base.doit(self) | 1405 | return _class_0.__parent.__base.doit(self) |
1331 | end | 1406 | end |
1332 | } | 1407 | } |
1408 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1409 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1410 | _base_0[_key_0] = _val_0 | ||
1411 | end | ||
1412 | end | ||
1333 | if _base_0.__index == nil then | 1413 | if _base_0.__index == nil then |
1334 | _base_0.__index = _base_0 | 1414 | _base_0.__index = _base_0 |
1335 | end | 1415 | end |
@@ -1404,6 +1484,11 @@ return describe("class", function() | |||
1404 | local _class_0 | 1484 | local _class_0 |
1405 | local _parent_0 = One | 1485 | local _parent_0 = One |
1406 | local _base_0 = { } | 1486 | local _base_0 = { } |
1487 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1488 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1489 | _base_0[_key_0] = _val_0 | ||
1490 | end | ||
1491 | end | ||
1407 | if _base_0.__index == nil then | 1492 | if _base_0.__index == nil then |
1408 | _base_0.__index = _base_0 | 1493 | _base_0.__index = _base_0 |
1409 | end | 1494 | end |
@@ -1444,6 +1529,11 @@ return describe("class", function() | |||
1444 | local _class_0 | 1529 | local _class_0 |
1445 | local _parent_0 = Two | 1530 | local _parent_0 = Two |
1446 | local _base_0 = { } | 1531 | local _base_0 = { } |
1532 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1533 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1534 | _base_0[_key_0] = _val_0 | ||
1535 | end | ||
1536 | end | ||
1447 | if _base_0.__index == nil then | 1537 | if _base_0.__index == nil then |
1448 | _base_0.__index = _base_0 | 1538 | _base_0.__index = _base_0 |
1449 | end | 1539 | end |
@@ -1488,6 +1578,11 @@ return describe("class", function() | |||
1488 | local _class_0 | 1578 | local _class_0 |
1489 | local _parent_0 = Three | 1579 | local _parent_0 = Three |
1490 | local _base_0 = { } | 1580 | local _base_0 = { } |
1581 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1582 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1583 | _base_0[_key_0] = _val_0 | ||
1584 | end | ||
1585 | end | ||
1491 | if _base_0.__index == nil then | 1586 | if _base_0.__index == nil then |
1492 | _base_0.__index = _base_0 | 1587 | _base_0.__index = _base_0 |
1493 | end | 1588 | end |
@@ -1529,7 +1624,7 @@ return describe("class", function() | |||
1529 | end | 1624 | end |
1530 | return assert.same(8, Four:a()) | 1625 | return assert.same(8, Four:a()) |
1531 | end) | 1626 | end) |
1532 | return it("super should still work when method wrapped", function() | 1627 | it("super should still work when method wrapped", function() |
1533 | local add_some | 1628 | local add_some |
1534 | add_some = function(opts) | 1629 | add_some = function(opts) |
1535 | return function(self) | 1630 | return function(self) |
@@ -1574,6 +1669,11 @@ return describe("class", function() | |||
1574 | end | 1669 | end |
1575 | }) | 1670 | }) |
1576 | } | 1671 | } |
1672 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1673 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1674 | _base_0[_key_0] = _val_0 | ||
1675 | end | ||
1676 | end | ||
1577 | if _base_0.__index == nil then | 1677 | if _base_0.__index == nil then |
1578 | _base_0.__index = _base_0 | 1678 | _base_0.__index = _base_0 |
1579 | end | 1679 | end |
@@ -1626,6 +1726,11 @@ return describe("class", function() | |||
1626 | end | 1726 | end |
1627 | end)() | 1727 | end)() |
1628 | } | 1728 | } |
1729 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1730 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1731 | _base_0[_key_0] = _val_0 | ||
1732 | end | ||
1733 | end | ||
1629 | if _base_0.__index == nil then | 1734 | if _base_0.__index == nil then |
1630 | _base_0.__index = _base_0 | 1735 | _base_0.__index = _base_0 |
1631 | end | 1736 | end |
@@ -1664,5 +1769,84 @@ return describe("class", function() | |||
1664 | assert.same(1 + 100 + 12, Sub():value()) | 1769 | assert.same(1 + 100 + 12, Sub():value()) |
1665 | return assert.same(6, OtherSub():value()) | 1770 | return assert.same(6, OtherSub():value()) |
1666 | end) | 1771 | end) |
1772 | return it("should copy metamethod from super", function() | ||
1773 | local A | ||
1774 | do | ||
1775 | local _class_0 | ||
1776 | local _base_0 = { | ||
1777 | val = 1, | ||
1778 | __tostring = function(self) | ||
1779 | return "Object " .. tostring(self.val) | ||
1780 | end | ||
1781 | } | ||
1782 | if _base_0.__index == nil then | ||
1783 | _base_0.__index = _base_0 | ||
1784 | end | ||
1785 | _class_0 = setmetatable({ | ||
1786 | __init = function() end, | ||
1787 | __base = _base_0, | ||
1788 | __name = "A" | ||
1789 | }, { | ||
1790 | __index = _base_0, | ||
1791 | __call = function(cls, ...) | ||
1792 | local _self_0 = setmetatable({ }, _base_0) | ||
1793 | cls.__init(_self_0, ...) | ||
1794 | return _self_0 | ||
1795 | end | ||
1796 | }) | ||
1797 | _base_0.__class = _class_0 | ||
1798 | A = _class_0 | ||
1799 | end | ||
1800 | local B | ||
1801 | do | ||
1802 | local _class_0 | ||
1803 | local _parent_0 = A | ||
1804 | local _base_0 = { | ||
1805 | val = 2 | ||
1806 | } | ||
1807 | for _key_0, _val_0 in pairs(_parent_0.__base) do | ||
1808 | if _base_0[_key_0] == nil and _key_0:match("^__") and not (_key_0 == "__index" and _val_0 == _parent_0.__base) then | ||
1809 | _base_0[_key_0] = _val_0 | ||
1810 | end | ||
1811 | end | ||
1812 | if _base_0.__index == nil then | ||
1813 | _base_0.__index = _base_0 | ||
1814 | end | ||
1815 | setmetatable(_base_0, _parent_0.__base) | ||
1816 | _class_0 = setmetatable({ | ||
1817 | __init = function(self, ...) | ||
1818 | return _class_0.__parent.__init(self, ...) | ||
1819 | end, | ||
1820 | __base = _base_0, | ||
1821 | __name = "B", | ||
1822 | __parent = _parent_0 | ||
1823 | }, { | ||
1824 | __index = function(cls, name) | ||
1825 | local val = rawget(_base_0, name) | ||
1826 | if val == nil then | ||
1827 | local parent = rawget(cls, "__parent") | ||
1828 | if parent then | ||
1829 | return parent[name] | ||
1830 | end | ||
1831 | else | ||
1832 | return val | ||
1833 | end | ||
1834 | end, | ||
1835 | __call = function(cls, ...) | ||
1836 | local _self_0 = setmetatable({ }, _base_0) | ||
1837 | cls.__init(_self_0, ...) | ||
1838 | return _self_0 | ||
1839 | end | ||
1840 | }) | ||
1841 | _base_0.__class = _class_0 | ||
1842 | if _parent_0.__inherited then | ||
1843 | _parent_0.__inherited(_parent_0, _class_0) | ||
1844 | end | ||
1845 | B = _class_0 | ||
1846 | end | ||
1847 | local a, b = A(), B() | ||
1848 | assert.same("Object 1", tostring(a)) | ||
1849 | return assert.same("Object 2", tostring(b)) | ||
1850 | end) | ||
1667 | end) | 1851 | end) |
1668 | end) | 1852 | end) |