aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/metatable_spec.lua
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2026-01-26 17:45:26 +0800
committerLi Jin <dragon-fly@qq.com>2026-01-26 17:45:56 +0800
commite02321107277a63e7dcb12ab163c9942ac101b87 (patch)
treef38c6f2fbf4ee35df4938933dbc1e645733356f4 /spec/outputs/test/metatable_spec.lua
parent5d5b657f606b5939062983b1f90c3359d542672e (diff)
downloadyuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.gz
yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.tar.bz2
yuescript-e02321107277a63e7dcb12ab163c9942ac101b87.zip
Updated tests.
Diffstat (limited to 'spec/outputs/test/metatable_spec.lua')
-rw-r--r--spec/outputs/test/metatable_spec.lua141
1 files changed, 141 insertions, 0 deletions
diff --git a/spec/outputs/test/metatable_spec.lua b/spec/outputs/test/metatable_spec.lua
new file mode 100644
index 0000000..4d2a886
--- /dev/null
+++ b/spec/outputs/test/metatable_spec.lua
@@ -0,0 +1,141 @@
1return describe("metatable", function()
2 it("should get metatable with <> syntax", function()
3 local obj = setmetatable({
4 value = 42
5 }, {
6 __index = {
7 extra = "data"
8 }
9 })
10 local mt = getmetatable(obj)
11 return assert.is_true(mt ~= nil)
12 end)
13 it("should set metatable with <>", function()
14 local obj = { }
15 setmetatable(obj, {
16 __index = {
17 value = 100
18 }
19 })
20 return assert.same(obj.value, 100)
21 end)
22 it("should access metatable with <>", function()
23 local obj = setmetatable({ }, {
24 __index = {
25 value = 50
26 }
27 })
28 local result = getmetatable(obj).__index.value
29 return assert.same(result, 50)
30 end)
31 it("should work with <index> metamethod", function()
32 local obj = setmetatable({ }, {
33 __index = function(self, key)
34 if key == "computed" then
35 return "computed_value"
36 end
37 end
38 })
39 return assert.same(obj.computed, "computed_value")
40 end)
41 it("should work with <newindex> metamethod", function()
42 local obj = setmetatable({ }, {
43 __newindex = function(self, key, value)
44 return rawset(self, "stored_" .. key, value)
45 end
46 })
47 obj.test = 123
48 return assert.same(obj.stored_test, 123)
49 end)
50 it("should work with <add> metamethod", function()
51 local obj = setmetatable({
52 value = 10
53 }, {
54 __add = function(a, b)
55 return a.value + b.value
56 end
57 })
58 local obj2 = setmetatable({
59 value = 20
60 }, {
61 __add = function(a, b)
62 return a.value + b.value
63 end
64 })
65 local result = obj + obj2
66 return assert.same(result, 30)
67 end)
68 it("should work with <call> metamethod", function()
69 local obj = setmetatable({ }, {
70 __call = function(self, x)
71 return x * 2
72 end
73 })
74 local result = obj(5)
75 return assert.same(result, 10)
76 end)
77 it("should work with <tostring> metamethod", function()
78 local obj = setmetatable({
79 value = 42
80 }, {
81 __tostring = function(self)
82 return "Value: " .. tostring(self.value)
83 end
84 })
85 local result = tostring(obj)
86 return assert.same(result, "Value: 42")
87 end)
88 it("should work with <eq> metamethod", function()
89 local obj1 = setmetatable({
90 id = 1
91 }, {
92 __eq = function(a, b)
93 return a.id == b.id
94 end
95 })
96 local obj2 = setmetatable({
97 id = 1
98 }, {
99 __eq = function(a, b)
100 return a.id == b.id
101 end
102 })
103 return assert.is_true(obj1 == obj2)
104 end)
105 it("should destructure metatable", function()
106 local obj = setmetatable({ }, {
107 new = function()
108 return "new result"
109 end,
110 update = function()
111 return "update result"
112 end
113 })
114 local new, update
115 do
116 local _obj_0 = getmetatable(obj)
117 new, update = _obj_0.new, _obj_0.update
118 end
119 assert.is_true(type(new) == "function")
120 return assert.is_true(type(update) == "function")
121 end)
122 it("should check if two objects have same metatable", function()
123 local mt = {
124 value = 100
125 }
126 local obj1 = setmetatable({ }, mt)
127 local obj2 = setmetatable({ }, mt)
128 return assert.is_true(getmetatable(obj1) == getmetatable(obj2))
129 end)
130 return it("should work with <concat> metamethod", function()
131 local obj = setmetatable({
132 value = "hello"
133 }, {
134 __concat = function(a, b)
135 return a.value .. b
136 end
137 })
138 local result = obj .. " world"
139 return assert.same(result, "hello world")
140 end)
141end)