2023-08-04 02:35:51 -06:00
|
|
|
config.targets = 'core'
|
|
|
|
|
2018-07-19 10:40:25 -06:00
|
|
|
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
|
2020-04-05 20:58:51 -06:00
|
|
|
|
2021-06-29 13:22:05 -06:00
|
|
|
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
|
|
|
|
|
2020-04-05 20:58:51 -06:00
|
|
|
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
|
2022-09-04 21:47:20 -06:00
|
|
|
|
|
|
|
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)
|
2022-09-04 22:08:23 -06:00
|
|
|
|
|
|
|
-- vector items
|
|
|
|
dfhack.with_temp_object(df.new('ptr-vector'), function(vec)
|
|
|
|
fake_unit = vec
|
|
|
|
vec:insert('#', df.global.world)
|
2023-01-05 18:35:33 -07:00
|
|
|
vec:insert('#', df.global.plotinfo)
|
2022-09-04 22:08:23 -06:00
|
|
|
|
|
|
|
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'))
|
2023-08-04 02:35:51 -06:00
|
|
|
expect.eq(df.reinterpret_cast(df.plotinfost, utils.df_expr_to_ref('unit[1]').value), df.global.plotinfo)
|
2022-09-04 22:08:23 -06:00
|
|
|
|
|
|
|
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)
|
2022-09-04 21:47:20 -06:00
|
|
|
end)
|
|
|
|
end
|