1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-10-22 17:37:38 +00:00

Add a function for type-checking arguments (#207)

- Define an expect(index, actual_value, types...) helper function which
   takes an argument index, value and list of permissable types and
   ensures the value is of one of those types.
   If not, it will produce an error message with the expected and actual
   type, as well as the argument number and (if available) the function
   name.
 - Expose expect in the global scope as _G["~expect"], hopefully making
   it clear it is internal.
 - Replace most manual type checks with this helper method.
 - Write tests to ensure this argument validation works as expected

Also fix a couple of bugs exposed by this refactor and the subsequent
tests:
 - Make rednet checks a little more strict - rednet.close(false) is no
   longer valid.
 - Error when attempting to redirect the terminal to itself 
   (term.redirect(term)).
This commit is contained in:
hydraz
2019-05-30 15:36:28 -03:00
committed by SquidDev
parent 5592ebae7d
commit 9048deeb95
34 changed files with 1070 additions and 428 deletions

View File

@@ -0,0 +1,60 @@
describe("The paintutils library", function()
describe("paintutils.parseImage", function()
it("validates arguments", function()
paintutils.parseImage("")
expect.error(paintutils.parseImage, nil):eq("bad argument #1 (expected string, got nil)")
end)
end)
describe("paintutils.loadImage", function()
it("validates arguments", function()
expect.error(paintutils.loadImage, nil):eq("bad argument #1 (expected string, got nil)")
end)
end)
describe("paintutils.drawPixel", function()
it("validates arguments", function()
expect.error(paintutils.drawPixel, nil):eq("bad argument #1 (expected number, got nil)")
expect.error(paintutils.drawPixel, 1, nil):eq("bad argument #2 (expected number, got nil)")
expect.error(paintutils.drawPixel, 1, 1, false):eq("bad argument #3 (expected number, got boolean)")
end)
end)
describe("paintutils.drawLine", function()
it("validates arguments", function()
expect.error(paintutils.drawLine, nil):eq("bad argument #1 (expected number, got nil)")
expect.error(paintutils.drawLine, 1, nil):eq("bad argument #2 (expected number, got nil)")
expect.error(paintutils.drawLine, 1, 1, nil):eq("bad argument #3 (expected number, got nil)")
expect.error(paintutils.drawLine, 1, 1, 1, nil):eq("bad argument #4 (expected number, got nil)")
expect.error(paintutils.drawLine, 1, 1, 1, 1, false):eq("bad argument #5 (expected number, got boolean)")
end)
end)
describe("paintutils.drawBox", function()
it("validates arguments", function()
expect.error(paintutils.drawBox, nil):eq("bad argument #1 (expected number, got nil)")
expect.error(paintutils.drawBox, 1, nil):eq("bad argument #2 (expected number, got nil)")
expect.error(paintutils.drawBox, 1, 1, nil):eq("bad argument #3 (expected number, got nil)")
expect.error(paintutils.drawBox, 1, 1, 1, nil):eq("bad argument #4 (expected number, got nil)")
expect.error(paintutils.drawBox, 1, 1, 1, 1, false):eq("bad argument #5 (expected number, got boolean)")
end)
end)
describe("paintutils.drawFilledBox", function()
it("validates arguments", function()
expect.error(paintutils.drawFilledBox, nil):eq("bad argument #1 (expected number, got nil)")
expect.error(paintutils.drawFilledBox, 1, nil):eq("bad argument #2 (expected number, got nil)")
expect.error(paintutils.drawFilledBox, 1, 1, nil):eq("bad argument #3 (expected number, got nil)")
expect.error(paintutils.drawFilledBox, 1, 1, 1, nil):eq("bad argument #4 (expected number, got nil)")
expect.error(paintutils.drawFilledBox, 1, 1, 1, 1, false):eq("bad argument #5 (expected number, got boolean)")
end)
end)
describe("paintutils.drawImage", function()
it("validates arguments", function()
expect.error(paintutils.drawImage, nil):eq("bad argument #1 (expected table, got nil)")
expect.error(paintutils.drawImage, {}, nil):eq("bad argument #2 (expected number, got nil)")
expect.error(paintutils.drawImage, {}, 1, nil):eq("bad argument #3 (expected number, got nil)")
end)
end)
end)