Skip to content

Commit 12865a8

Browse files
committed
ipairs_clone can be used with map (default lua ipairs can't)
1 parent 52e1e14 commit 12865a8

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local res_type = world.get_type_by_name("TestResourceWithVariousFields")
2+
local res = world.get_resource(res_type)
3+
4+
local map = res.string_map
5+
6+
local count = 0
7+
local found_keys = {}
8+
9+
-- ipairs_clone on a map returns (index, [key, value]) where value is a list
10+
for i, entry in map:ipairs_clone() do
11+
assert(i == count + 1, "Index should be sequential: expected " .. (count + 1) .. ", got " .. i)
12+
13+
-- entry should be a list with [key, value]
14+
assert(entry ~= nil, "Entry should not be nil")
15+
assert(entry[1] ~= nil, "Key should not be nil")
16+
assert(entry[2] ~= nil, "Value should not be nil")
17+
18+
local key = entry[1]
19+
local value = entry[2]
20+
21+
count = count + 1
22+
found_keys[key] = value
23+
end
24+
25+
assert(count == 2, "Expected 2 entries, got " .. count)
26+
assert(found_keys["foo"] == "bar", "Expected foo=>bar, got " .. tostring(found_keys["foo"]))
27+
assert(found_keys["zoo"] == "zed", "Expected zoo=>zed, got " .. tostring(found_keys["zoo"]))

0 commit comments

Comments
 (0)