-
Notifications
You must be signed in to change notification settings - Fork 495
Expand file tree
/
Copy pathstruct_fields.lua
More file actions
112 lines (95 loc) · 3.31 KB
/
struct_fields.lua
File metadata and controls
112 lines (95 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
config.target = 'core'
local COORD_FIELD_NAMES = {'x', 'y', 'z', 'isValid', 'clear'}
local COORD_FIELD_EXPECTED_DATA = {
x = {type_name='int16_t'},
y = {type_name='int16_t'},
z = {type_name='int16_t'},
isValid = {type_name='function'},
clear = {type_name='function'},
}
local READONLY_MSG = 'Attempt to change a read%-only table.'
local function listFieldNames(t)
local names = {}
for name in pairs(t._fields) do
table.insert(names, name)
end
return names
end
function test.access()
local fields = df.coord._fields
expect.true_(fields)
expect.eq(fields, df.coord._fields)
for name, expected in pairs(COORD_FIELD_EXPECTED_DATA) do
expect.true_(fields[name], name)
expect.eq(fields[name].name, name, name)
expect.eq(fields[name].type_name, expected.type_name, name)
expect.eq(type(fields[name].offset), 'number', name)
expect.eq(type(fields[name].mode), 'number', name)
expect.eq(type(fields[name].count), 'number', name)
end
end
function test.globals_original_name()
for name, info in pairs(df.global._fields) do
expect.eq(type(info.original_name), 'string', name)
end
end
function test.order()
expect.table_eq(listFieldNames(df.coord), COORD_FIELD_NAMES)
end
function test.nonexistent()
expect.nil_(df.coord._fields.nonexistent)
expect.error_match('string expected', function()
expect.nil_(df.coord._fields[2])
end)
expect.error_match('string expected', function()
expect.nil_(df.coord._fields[nil])
end)
end
function test.count()
expect.eq(df.unit._fields.relationship_ids.count, 9)
end
function test.index_enum()
expect.eq(df.unit._fields.relationship_ids.index_enum, df.unit_relationship_type)
end
function test.ref_target()
expect.eq(df.unit._fields.hist_figure_id.ref_target, df.historical_figure)
end
function test.readonly()
expect.error_match(READONLY_MSG, function()
df.coord._fields.x = 'foo'
end)
expect.error_match(READONLY_MSG, function()
df.coord._fields.nonexistent = 'foo'
end)
expect.nil_(df.coord._fields.nonexistent)
-- should have no effect
df.coord._fields.x.name = 'foo'
expect.eq(df.coord._fields.x.name, 'x')
end
function test.circular_refs_init()
expect.eq(df.job._fields.list_link.type, df.job_list_link)
expect.eq(df.job_list_link._fields.item.type, df.job)
end
function test.subclass_match()
for f, parent in pairs(df.viewscreen._fields) do
local child = df.viewscreen_titlest._fields[f]
expect.table_eq(parent, child, f)
end
end
function test.subclass_order()
-- ensure that parent class fields come before subclass fields
local hierarchy = {df.item, df.item_actual, df.item_crafted, df.item_constructed, df.item_bedst}
local field_names = {}
for _, t in pairs(hierarchy) do
field_names[t] = listFieldNames(t)
end
for ic = 1, #hierarchy do
for ip = 1, ic - 1 do
local parent_fields = listFieldNames(hierarchy[ip])
local child_fields = listFieldNames(hierarchy[ic])
child_fields = table.pack(table.unpack(child_fields, 1, #parent_fields))
child_fields.n = nil
expect.table_eq(child_fields, parent_fields, ('compare %s to %s'):format(hierarchy[ip], hierarchy[ic]))
end
end
end