coordinates can be 0

develop
myk002 2021-07-03 16:26:39 -07:00
parent e1c8bdafba
commit 56a1c8b4d5
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 9 additions and 9 deletions

@ -167,11 +167,11 @@ function numberList(arg, arg_name, list_length)
return strings
end
-- throws if val is not a positive integer; otherwise returns val
local function check_positive_int(val, arg_name)
if not val or val <= 0 or val ~= math.floor(val) then
-- throws if val is not a nonnegative integer; otherwise returns val
local function check_nonnegative_int(val, arg_name)
if not val or val < 0 or val ~= math.floor(val) then
arg_error(arg_name,
'expected positive integer; got "%s"', tostring(val))
'expected non-negative integer; got "%s"', tostring(val))
end
return val
end
@ -190,9 +190,9 @@ function coords(arg, arg_name)
return cursor
end
local numbers = numberList(arg, arg_name, 3)
return xyz2pos(check_positive_int(numbers[1]),
check_positive_int(numbers[2]),
check_positive_int(numbers[3]))
return xyz2pos(check_nonnegative_int(numbers[1]),
check_nonnegative_int(numbers[2]),
check_nonnegative_int(numbers[3]))
end
return _ENV

@ -153,9 +153,9 @@ function test.numberList()
end
function test.coords()
expect.table_eq({x=5, y=4, z=3}, argparse.coords('5,4 , 3'), 'happy path')
expect.table_eq({x=0, y=4, z=3}, argparse.coords('0,4 , 3'), 'happy path')
expect.error_match('expected positive integer',
expect.error_match('expected non%-negative integer',
function() argparse.coords('1,-2,3') end,
'negative coordinate')