Skip to content

Commit 9b09788

Browse files
committed
Add tests for enum attrs tables
ipairs() currently fails due to DFHack#1860
1 parent 8a12083 commit 9b09788

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

test/structures/enum_attrs.lua

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
function test.getmetatable()
2+
expect.eq(getmetatable(df.item_type.attrs), 'item_type.attrs')
3+
end
4+
5+
local function check_valid_attr_entry(enum_type, index)
6+
local entry = enum_type.attrs[index]
7+
local suffix = ('%s entry at index %s'):format(enum_type._attr_entry_type, index)
8+
expect.ne(entry, nil, 'nil ' .. suffix)
9+
expect.true_(enum_type._attr_entry_type:is_instance(entry), 'invalid ' .. suffix)
10+
end
11+
12+
function test.valid_items()
13+
for i in ipairs(df.item_type) do
14+
check_valid_attr_entry(df.item_type, i)
15+
-- expect.true_(df.item_type.attrs[i])
16+
-- expect.true_(df.item_type._attr_entry_type:is_instance(df.item_type.attrs[i]))
17+
end
18+
end
19+
20+
function test.valid_items_name()
21+
for i, name in ipairs(df.item_type) do
22+
expect.eq(df.item_type.attrs[i], df.item_type.attrs[name])
23+
end
24+
end
25+
26+
function test.valid_items_unique()
27+
-- check that every enum item has its own attrs entry
28+
local addr_to_name = {}
29+
for _, name in ipairs(df.item_type) do
30+
local _, addr = df.sizeof(df.item_type.attrs[name])
31+
if addr_to_name[addr] then
32+
expect.fail(('attrs shared between "%s" and "%s"'):format(name, addr_to_name[name]))
33+
else
34+
addr_to_name[addr] = name
35+
end
36+
end
37+
end
38+
39+
function test.invalid_items()
40+
check_valid_attr_entry(df.item_type, df.item_type._first_item - 1)
41+
check_valid_attr_entry(df.item_type, df.item_type._last_item + 1)
42+
end
43+
44+
function test.invalid_items_shared()
45+
expect.eq(df.item_type.attrs[df.item_type._first_item - 1], df.item_type.attrs[df.item_type._last_item + 1])
46+
end
47+
48+
function test.length()
49+
expect.eq(#df.item_type.attrs, 0)
50+
end
51+
52+
local function max_attrs_length(enum_type)
53+
return enum_type._last_item - enum_type._first_item + 1
54+
end
55+
56+
function test.pairs()
57+
local i = 0
58+
for _ in pairs(df.item_type.attrs) do
59+
i = i + 1
60+
if i > max_attrs_length(df.item_type) then
61+
expect.fail('pairs() returned too many items: ' .. tostring(i))
62+
break
63+
end
64+
end
65+
expect.eq(i, 0)
66+
end
67+
68+
function test.ipairs()
69+
local i = 0
70+
for _ in ipairs(df.item_type.attrs) do
71+
i = i + 1
72+
if i > max_attrs_length(df.item_type) then
73+
expect.fail('ipairs() returned too many items: ' .. tostring(i))
74+
break
75+
end
76+
end
77+
expect.eq(i, max_attrs_length(df.item_type))
78+
end

0 commit comments

Comments
 (0)