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

@ -153,9 +153,9 @@ function test.numberList()
end end
function test.coords() 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, function() argparse.coords('1,-2,3') end,
'negative coordinate') 'negative coordinate')