From 4744a744f014e974aa9f7c092bac82752bab547b Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Tue, 16 Oct 2018 16:17:45 +0200 Subject: [PATCH] gears.table.join: Ignore nil arguments (#2440) When calling join with e.g. arguments (nil, {"a"}), then everything past the nil was ignored, because the code internally used ipairs() to iterate over the arguments and this stops at the first nil it encounters. Fix this by using select() to iterate over the arguments. This also adds a unit test for this problem. Signed-off-by: Uli Schlachter --- lib/gears/table.lua | 3 ++- spec/gears/table_spec.lua | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/gears/table.lua b/lib/gears/table.lua index 8b9bb6c26..76dff1d37 100644 --- a/lib/gears/table.lua +++ b/lib/gears/table.lua @@ -18,7 +18,8 @@ local gtable = {} -- @return A new table containing all keys from the arguments. function gtable.join(...) local ret = {} - for _, t in ipairs({...}) do + for i = 1, select("#", ...) do + local t = select(i, ...) if t then for k, v in pairs(t) do if type(k) == "number" then diff --git a/spec/gears/table_spec.lua b/spec/gears/table_spec.lua index 435a5b46a..5b69ba0b1 100644 --- a/spec/gears/table_spec.lua +++ b/spec/gears/table_spec.lua @@ -41,4 +41,11 @@ describe("gears.table", function() assert.is.equal(f(), nil) end) end) + + describe("table.join", function() + it("nil argument", function() + local t = gtable.join({"a"}, nil, {"b"}) + assert.is.same(t, {"a", "b"}) + end) + end) end)