diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/colors.lua b/src/main/resources/assets/computercraft/lua/rom/apis/colors.lua index 1d0056395..c113d324a 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/colors.lua +++ b/src/main/resources/assets/computercraft/lua/rom/apis/colors.lua @@ -20,8 +20,9 @@ black = 32768 function combine( ... ) local r = 0 - for n,c in ipairs( { ... } ) do - expect(n, c, "number") + for i = 1, select('#', ...) do + local c = select(i, ...) + expect(i, c, "number") r = bit32.bor(r,c) end return r @@ -30,8 +31,9 @@ end function subtract( colors, ... ) expect(1, colors, "number") local r = colors - for n,c in ipairs( { ... } ) do - expect(n + 1, c, "number") + for i = 1, select('#', ...) do + local c = select(i, ...) + expect(i + 1, c, "number") r = bit32.band(r, bit32.bnot(c)) end return r diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/textutils.lua b/src/main/resources/assets/computercraft/lua/rom/apis/textutils.lua index 80a4474f9..ba9f5e033 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/textutils.lua +++ b/src/main/resources/assets/computercraft/lua/rom/apis/textutils.lua @@ -103,11 +103,9 @@ function pagedPrint( _sText, _nFreeLines ) end local function tabulateCommon( bPaged, ... ) - local tAll = { ... } - for k,v in ipairs( tAll ) do - if type( v ) ~= "number" and type( v ) ~= "table" then - error( "bad argument #"..k.." (expected number or table, got " .. type( v ) .. ")", 3 ) - end + local tAll = table.pack(...) + for i = 1, tAll.n do + expect(i, tAll[i], "number", "table") end local w,h = term.getSize() @@ -162,11 +160,11 @@ local function tabulateCommon( bPaged, ... ) end function tabulate( ... ) - tabulateCommon( false, ... ) + return tabulateCommon( false, ... ) end function pagedTabulate( ... ) - tabulateCommon( true, ... ) + return tabulateCommon( true, ... ) end local g_tLuaKeywords = { diff --git a/src/test/resources/test-rom/spec/apis/colors_spec.lua b/src/test/resources/test-rom/spec/apis/colors_spec.lua index 91ebefc51..86b8adfe9 100644 --- a/src/test/resources/test-rom/spec/apis/colors_spec.lua +++ b/src/test/resources/test-rom/spec/apis/colors_spec.lua @@ -1,9 +1,8 @@ describe("The colors library", function() describe("colors.combine", function() it("validates arguments", function() - -- FIXME: Error when last argument is nil - use table.pack instead. - expect.error(colors.combine, 1, false):eq("bad argument #2 (expected number, got boolean)") - expect.error(colors.combine, 1, 1, false):eq("bad argument #3 (expected number, got boolean)") + expect.error(colors.combine, 1, nil):eq("bad argument #2 (expected number, got nil)") + expect.error(colors.combine, 1, 1, nil):eq("bad argument #3 (expected number, got nil)") end) it("combines colours", function() @@ -15,8 +14,8 @@ describe("The colors library", function() describe("colors.subtract", function() it("validates arguments", function() expect.error(colors.subtract, nil):eq("bad argument #1 (expected number, got nil)") - expect.error(colors.subtract, 1, false):eq("bad argument #2 (expected number, got boolean)") - expect.error(colors.subtract, 1, 1, false):eq("bad argument #3 (expected number, got boolean)") + expect.error(colors.subtract, 1, nil):eq("bad argument #2 (expected number, got nil)") + expect.error(colors.subtract, 1, 1, nil):eq("bad argument #3 (expected number, got nil)") end) it("subtracts colours", function() diff --git a/src/test/resources/test-rom/spec/apis/textutils_spec.lua b/src/test/resources/test-rom/spec/apis/textutils_spec.lua index 4435a23be..ab881d60b 100644 --- a/src/test/resources/test-rom/spec/apis/textutils_spec.lua +++ b/src/test/resources/test-rom/spec/apis/textutils_spec.lua @@ -20,10 +20,36 @@ describe("The textutils library", function() end) end) + describe("textutils.tabulate", function() + it("validates arguments", function() + term.redirect(window.create(term.current(), 1, 1, 5, 5, false)) + + textutils.tabulate() + textutils.tabulate({ "test" }) + textutils.tabulate(colors.white) + + expect.error(textutils.tabulate, nil):eq("bad argument #1 (expected number or table, got nil)") + expect.error(textutils.tabulate, { "test" }, nil):eq("bad argument #2 (expected number or table, got nil)") + end) + end) + + describe("textutils.pagedTabulate", function() + it("validates arguments", function() + term.redirect(window.create(term.current(), 1, 1, 5, 5, false)) + + textutils.pagedTabulate() + textutils.pagedTabulate({ "test" }) + textutils.pagedTabulate(colors.white) + + expect.error(textutils.pagedTabulate, nil):eq("bad argument #1 (expected number or table, got nil)") + expect.error(textutils.pagedTabulate, { "test" }, nil):eq("bad argument #2 (expected number or table, got nil)") + end) + end) + describe("textutils.empty_json_array", function() it("is immutable", function() expect.error(function() textutils.empty_json_array[1] = true end) - :eq("textutils_spec.lua:25: attempt to mutate textutils.empty_json_array") + :eq("textutils_spec.lua:51: attempt to mutate textutils.empty_json_array") end) end)