Skip to content

Commit 3da02a7

Browse files
committed
Add test for LuaTable.to_dictionary with __pairs metamethod
1 parent 774c2a8 commit 3da02a7

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

test/gdscript_tests/LuaTable.gd

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,48 @@ func test_setgetmetatable() -> bool:
127127
assert(table.get_metatable() == null)
128128
assert(table.get_metatable() == lua_state.globals.getmetatable.invoke(table))
129129
return true
130+
131+
132+
func test_to_dictionary_pairs() -> bool:
133+
var table = lua_state.do_string("""
134+
local function iterate_index_then_self(t)
135+
-- iteration over `t` metatable's __index table, if there's any
136+
local mt = getmetatable(t)
137+
if mt and type(mt.__index) == "table" then
138+
for k, v in pairs(mt.__index) do
139+
coroutine.yield(k, v)
140+
end
141+
end
142+
143+
-- now iterate over `t` normally
144+
for k, v in next, t, nil do
145+
coroutine.yield(k, v)
146+
end
147+
end
148+
149+
local t = {
150+
key1 = 1,
151+
key2 = 2,
152+
}
153+
setmetatable(t, {
154+
-- Here's an example "__index" table
155+
__index = {
156+
meta_key1 = 1,
157+
meta_key2 = 2,
158+
},
159+
-- And here's the "__pairs" metamethod
160+
__pairs = function(t)
161+
return coroutine.wrap(iterate_index_then_self), t, nil
162+
end,
163+
})
164+
return t
165+
""")
166+
167+
assert(table.to_dictionary() == {
168+
key1 = 1,
169+
key2 = 2,
170+
meta_key1 = 1,
171+
meta_key2 = 2,
172+
})
173+
174+
return true

0 commit comments

Comments
 (0)