test(vim.text): add test cases for error conditions

This commit is contained in:
Gregory Anders 2024-08-17 23:07:55 -05:00
parent d14f78ee0e
commit f6549cee26
2 changed files with 19 additions and 0 deletions

View File

@ -41,6 +41,9 @@ function M.hexdecode(enc)
for i = 1, #enc, 2 do
local u = lookup[enc:sub(i, i)] --[[@as integer]]
local l = lookup[enc:sub(i + 1, i + 1)] --[[@as integer]]
if not u or not l then
return nil, 'string must contain only hex characters'
end
str[#str + 1] = string.char(u * 16 + l)
end
return table.concat(str), nil

View File

@ -26,5 +26,21 @@ describe('vim.text', function()
eq(output, vim.text.hexencode(input))
eq(input, vim.text.hexdecode(output))
end)
it('errors on invalid input', function()
-- Odd number of hex characters
do
local res, err = vim.text.hexdecode('ABC')
eq(nil, res)
eq('string must have an even number of hex characters', err)
end
-- Non-hexadecimal input
do
local res, err = vim.text.hexdecode('nothex')
eq(nil, res)
eq('string must contain only hex characters', err)
end
end)
end)
end)