Skip to content

Commit

Permalink
fix(utils.enum) don't allow varargs with nils
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Feb 13, 2022
1 parent db77fb8 commit c89a8b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lua/pl/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ function utils.enum(...)
if type(first) ~= "table" then
-- vararg with strings
lst = utils.pack(...)
for i, value in ipairs(lst) do
for i, value in utils.npairs(lst) do
utils.assert_arg(i, value, "string")
enum[value] = value
end
Expand All @@ -378,17 +378,15 @@ function utils.enum(...)
enum[value] = value
end
-- add key-ed part
for key, value in pairs(first) do
if not lst[key] then
if type(key) ~= "string" then
error(("expected key to be 'string' but got '%s'"):format(type(key)), 2)
end
if enum[key] then
error(("duplicate entry in array and hash part: '%s'"):format(key), 2)
end
enum[key] = value
lst[#lst+1] = key
for key, value in utils.kpairs(first) do
if type(key) ~= "string" then
error(("expected key to be 'string' but got '%s'"):format(type(key)), 2)
end
if enum[key] then
error(("duplicate entry in array and hash part: '%s'"):format(key), 2)
end
enum[key] = value
lst[#lst+1] = key
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/utils-enum_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ describe("pl.utils", function ()
assert.has.error(function()
t = enum("hello", true, "world")
end, "argument 2 expected a 'string', got a 'boolean'")
-- no holes
assert.has.error(function()
t = enum("hello", nil, "world")
end, "argument 2 expected a 'string', got a 'nil'")
end)


Expand Down

0 comments on commit c89a8b0

Please sign in to comment.