add optional coordinate validation to coords

develop
myk002 2021-07-04 07:32:29 -07:00
parent b6e6f2d2fe
commit 0ef5134a33
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 49 additions and 17 deletions

@ -178,8 +178,10 @@ end
-- Parses a comma-separated coordinate string and returns a coordinate table of
-- {x=x, y=y, z=z}. If the string 'here' is passed, returns the coordinates of
-- the active game cursor, or throws an error if the cursor is not active.
function coords(arg, arg_name)
-- the active game cursor, or throws an error if the cursor is not active. This
-- function also verifies that the coordinates are valid for the current map and
-- throws if they are not (unless <skip_validation> is set to true).
function coords(arg, arg_name, skip_validation)
if arg == 'here' then
local cursor = guidm.getCursorPos()
if not cursor then
@ -187,12 +189,19 @@ function coords(arg, arg_name)
'"here" was specified for coordinates, but the game' ..
' cursor is not active!')
end
if not skip_validation and not dfhack.maps.isValidTilePos(cursor) then
arg_error(arg_name, 'cursor coordinates not on current map!')
end
return cursor
end
local numbers = numberList(arg, arg_name, 3)
return xyz2pos(check_nonnegative_int(numbers[1]),
check_nonnegative_int(numbers[2]),
check_nonnegative_int(numbers[3]))
local pos = xyz2pos(check_nonnegative_int(numbers[1]),
check_nonnegative_int(numbers[2]),
check_nonnegative_int(numbers[3]))
if not skip_validation and not dfhack.maps.isValidTilePos(pos) then
arg_error('specified coordinates not on current map: "%s"', arg)
end
return pos
end
return _ENV

@ -153,20 +153,43 @@ function test.numberList()
end
function test.coords()
expect.table_eq({x=0, y=4, z=3}, argparse.coords('0,4 , 3'), 'happy path')
expect.error_match('expected non%-negative integer',
function() argparse.coords('1,-2,3') end,
'negative coordinate')
mock.patch(guidm, 'getCursorPos', mock.func({x=1, y=2, z=3}),
mock.patch(dfhack.maps, "isValidTilePos", mock.func(true),
function()
expect.table_eq({x=1, y=2, z=3}, argparse.coords('here'))
expect.table_eq({x=0, y=4, z=3}, argparse.coords('0,4 , 3'),
'happy path')
expect.error_match('expected non%-negative integer',
function() argparse.coords('1,-2,3') end,
'negative coordinate')
mock.patch(guidm, 'getCursorPos', mock.func({x=1, y=2, z=3}),
function()
expect.table_eq({x=1, y=2, z=3}, argparse.coords('here'))
end)
mock.patch(guidm, 'getCursorPos', mock.func(),
function()
expect.error_match('cursor is not active',
function() argparse.coords('here') end,
'inactive cursor')
end)
end)
mock.patch(guidm, 'getCursorPos', mock.func(),
mock.patch(dfhack.maps, "isValidTilePos", mock.func(false),
function()
expect.error_match('cursor is not active',
function() argparse.coords('here') end,
'inactive cursor')
expect.error_match('not on current map',
function() argparse.coords('0,4,300') end)
expect.table_eq({x=0, y=4, z=300},
argparse.coords('0,4,300', nil, true))
mock.patch(guidm, 'getCursorPos', mock.func({x=1, y=2, z=300}),
function()
expect.error_match('not on current map',
function() argparse.coords('here') end)
end)
mock.patch(guidm, 'getCursorPos', mock.func({x=1, y=2, z=300}),
function()
expect.table_eq({x=1, y=2, z=300},
argparse.coords('here', nil, true))
end)
end)
end