Accept negative indices, add vector tests

develop
lethosor 2022-09-05 00:08:23 -04:00 committed by Myk
parent 483652e193
commit 2fdbcaebf5
2 changed files with 23 additions and 1 deletions

@ -608,7 +608,7 @@ df_env = df_shortcut_env()
function df_expr_to_ref(expr)
expr = expr:gsub('%["(.-)"%]', function(field) return '.' .. field end)
:gsub('%[\'(.-)\'%]', function(field) return '.' .. field end)
:gsub('%[(%d+)]', function(field) return '.' .. field end)
:gsub('%[(%-?%d+)%]', function(field) return '.' .. field end)
local parts = expr:split('.', true)
local obj = df_env[parts[1]]
for i = 2, #parts do

@ -89,5 +89,27 @@ function test.df_expr_to_ref()
-- 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.ui)
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.ui)
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