1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-03-03 02:10:04 +00:00

Use select('#', ...) instead of ipairs on varargs

This means we don't discard any value after nil, meaning that
calls such as colors.combine(nil, 1, 2, 3) will error, rather
than returning 0.
This commit is contained in:
SquidDev 2019-06-01 10:12:10 +01:00
parent 17645a79f0
commit d4b8650d21
4 changed files with 42 additions and 17 deletions

View File

@ -20,8 +20,9 @@ black = 32768
function combine( ... ) function combine( ... )
local r = 0 local r = 0
for n,c in ipairs( { ... } ) do for i = 1, select('#', ...) do
expect(n, c, "number") local c = select(i, ...)
expect(i, c, "number")
r = bit32.bor(r,c) r = bit32.bor(r,c)
end end
return r return r
@ -30,8 +31,9 @@ end
function subtract( colors, ... ) function subtract( colors, ... )
expect(1, colors, "number") expect(1, colors, "number")
local r = colors local r = colors
for n,c in ipairs( { ... } ) do for i = 1, select('#', ...) do
expect(n + 1, c, "number") local c = select(i, ...)
expect(i + 1, c, "number")
r = bit32.band(r, bit32.bnot(c)) r = bit32.band(r, bit32.bnot(c))
end end
return r return r

View File

@ -103,11 +103,9 @@ function pagedPrint( _sText, _nFreeLines )
end end
local function tabulateCommon( bPaged, ... ) local function tabulateCommon( bPaged, ... )
local tAll = { ... } local tAll = table.pack(...)
for k,v in ipairs( tAll ) do for i = 1, tAll.n do
if type( v ) ~= "number" and type( v ) ~= "table" then expect(i, tAll[i], "number", "table")
error( "bad argument #"..k.." (expected number or table, got " .. type( v ) .. ")", 3 )
end
end end
local w,h = term.getSize() local w,h = term.getSize()
@ -162,11 +160,11 @@ local function tabulateCommon( bPaged, ... )
end end
function tabulate( ... ) function tabulate( ... )
tabulateCommon( false, ... ) return tabulateCommon( false, ... )
end end
function pagedTabulate( ... ) function pagedTabulate( ... )
tabulateCommon( true, ... ) return tabulateCommon( true, ... )
end end
local g_tLuaKeywords = { local g_tLuaKeywords = {

View File

@ -1,9 +1,8 @@
describe("The colors library", function() describe("The colors library", function()
describe("colors.combine", function() describe("colors.combine", function()
it("validates arguments", function() it("validates arguments", function()
-- FIXME: Error when last argument is nil - use table.pack instead. expect.error(colors.combine, 1, nil):eq("bad argument #2 (expected number, got nil)")
expect.error(colors.combine, 1, false):eq("bad argument #2 (expected number, got boolean)") expect.error(colors.combine, 1, 1, nil):eq("bad argument #3 (expected number, got nil)")
expect.error(colors.combine, 1, 1, false):eq("bad argument #3 (expected number, got boolean)")
end) end)
it("combines colours", function() it("combines colours", function()
@ -15,8 +14,8 @@ describe("The colors library", function()
describe("colors.subtract", function() describe("colors.subtract", function()
it("validates arguments", function() it("validates arguments", function()
expect.error(colors.subtract, nil):eq("bad argument #1 (expected number, got nil)") 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, nil):eq("bad argument #2 (expected number, got nil)")
expect.error(colors.subtract, 1, 1, false):eq("bad argument #3 (expected number, got boolean)") expect.error(colors.subtract, 1, 1, nil):eq("bad argument #3 (expected number, got nil)")
end) end)
it("subtracts colours", function() it("subtracts colours", function()

View File

@ -20,10 +20,36 @@ describe("The textutils library", function()
end) end)
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() describe("textutils.empty_json_array", function()
it("is immutable", function() it("is immutable", function()
expect.error(function() textutils.empty_json_array[1] = true end) 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)
end) end)