1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-03-02 09:50: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( ... )
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

View File

@ -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 = {

View File

@ -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()

View File

@ -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)