forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
115 lines (96 loc) · 4.11 KB
/
Copy pathutils.lua
File metadata and controls
115 lines (96 loc) · 4.11 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
113
114
115
local utils = require 'utils'
function test.OrderedTable()
local t = utils.OrderedTable()
local keys = {'a', 'c', 'e', 'd', 'b', 'q', 58, -1.2}
for i = 1, #keys do
t[keys[i]] = i
end
local i = 1
for k, v in pairs(t) do
expect.eq(k, keys[i], 'key order')
expect.eq(v, i, 'correct value')
i = i + 1
end
end
function test.normalizePath()
expect.eq('imapath/file.csv', utils.normalizePath('imapath/file.csv'))
expect.eq('/ima/path', utils.normalizePath('/ima/path'))
expect.eq('ima/path', utils.normalizePath('ima//path'))
expect.eq('imapath', utils.normalizePath('imapath'))
expect.eq('/imapath', utils.normalizePath('/imapath'))
expect.eq('/imapath', utils.normalizePath('//imapath'))
expect.eq('/imapath', utils.normalizePath('///imapath'))
expect.eq('imapath/', utils.normalizePath('imapath/'))
expect.eq('imapath/', utils.normalizePath('imapath//'))
end
function test.invert()
local t = {}
local i = utils.invert{'a', 4.4, false, true, 5, t}
expect.eq(i.a, 1)
expect.eq(i[4.4], 2)
expect.eq(i[false], 3)
expect.eq(i[true], 4)
expect.eq(i[5], 5)
expect.eq(i[t], 6)
expect.eq(i[700], nil)
expect.eq(i.foo, nil)
expect.eq(i[{}], nil)
end
function test.invert_nil()
local i = utils.invert{'a', nil, 'b'}
expect.eq(i.a, 1)
expect.eq(i[nil], nil)
expect.eq(i.b, 3)
end
function test.invert_overwrite()
local i = utils.invert{'a', 'b', 'a'}
expect.eq(i.b, 2)
expect.eq(i.a, 3)
end
function test.df_expr_to_ref()
-- userdata field
expect.eq(utils.df_expr_to_ref('df.global.world.engravings'), df.global.world.engravings)
expect.eq(utils.df_expr_to_ref('df.global.world.engravings'), df.global.world:_field('engravings'))
-- primitive field
expect.eq(utils.df_expr_to_ref('df.global.world.original_save_version'), df.global.world:_field('original_save_version'))
-- table field
expect.eq(utils.df_expr_to_ref('df.global.world'), df.global.world)
expect.eq(utils.df_expr_to_ref('df.global'), df.global)
-- table
expect.eq(utils.df_expr_to_ref('df'), df)
-- userdata object
expect.eq(utils.df_expr_to_ref('scr'), dfhack.gui.getCurViewscreen())
local fake_unit
mock.patch(dfhack.gui, 'getSelectedUnit', function() return fake_unit end, function()
-- lightuserdata field
fake_unit = {
null_field=df.NULL,
}
expect.eq(utils.df_expr_to_ref('unit'), fake_unit)
expect.eq(utils.df_expr_to_ref('unit.null_field'), fake_unit.null_field)
dfhack.with_temp_object(df.unit:new(), function(u)
fake_unit = u
-- userdata field
expect.eq(utils.df_expr_to_ref('unit.name'), fake_unit.name)
expect.eq(utils.df_expr_to_ref('unit.name'), fake_unit:_field('name'))
-- primitive field
expect.eq(utils.df_expr_to_ref('unit.profession'), fake_unit:_field('profession'))
end)
-- vector items
dfhack.with_temp_object(df.new('ptr-vector'), function(vec)
fake_unit = vec
vec:insert('#', df.global.world)
vec:insert('#', df.global.plotinfo)
expect.eq(utils.df_expr_to_ref('unit'), vec)
expect.eq(utils.df_expr_to_ref('unit[0]'), utils.df_expr_to_ref('unit.0'))
expect.eq(df.reinterpret_cast(df.world, utils.df_expr_to_ref('unit[0]').value), df.global.world)
expect.eq(utils.df_expr_to_ref('unit[1]'), utils.df_expr_to_ref('unit.1'))
expect.eq(df.reinterpret_cast(df.ui, utils.df_expr_to_ref('unit[1]').value), df.global.plotinfo)
expect.error_match('index out of bounds', function() utils.df_expr_to_ref('unit.2') end)
expect.error_match('index out of bounds', function() utils.df_expr_to_ref('unit[2]') end)
expect.error_match('index out of bounds', function() utils.df_expr_to_ref('unit.-1') end)
expect.error_match('index out of bounds', function() utils.df_expr_to_ref('unit[-1]') end)
expect.error_match('not found', function() utils.df_expr_to_ref('unit.a') end)
end)
end)
end