mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-06-25 22:53:22 +00:00
9048deeb95
- 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)).
124 lines
5.3 KiB
Lua
124 lines
5.3 KiB
Lua
describe("The window library", function()
|
|
local function mk()
|
|
return window.create(term.current(), 1, 1, 5, 5, false)
|
|
end
|
|
|
|
describe("window.create", function()
|
|
it("validates arguments", function()
|
|
local r = mk()
|
|
window.create(r, 1, 1, 5, 5)
|
|
window.create(r, 1, 1, 5, 5, false)
|
|
|
|
expect.error(window.create, nil):eq("bad argument #1 (expected table, got nil)")
|
|
expect.error(window.create, r, nil):eq("bad argument #2 (expected number, got nil)")
|
|
expect.error(window.create, r, 1, nil):eq("bad argument #3 (expected number, got nil)")
|
|
expect.error(window.create, r, 1, 1, nil):eq("bad argument #4 (expected number, got nil)")
|
|
expect.error(window.create, r, 1, 1, 1, nil):eq("bad argument #5 (expected number, got nil)")
|
|
expect.error(window.create, r, 1, 1, 1, 1, ""):eq("bad argument #6 (expected boolean, got string)")
|
|
end)
|
|
end)
|
|
|
|
describe("Window.blit", function()
|
|
it("validates arguments", function()
|
|
local w = mk()
|
|
w.blit("a", "a", "a")
|
|
|
|
expect.error(w.blit, nil):eq("bad argument #1 (expected string, got nil)")
|
|
expect.error(w.blit, "", nil):eq("bad argument #2 (expected string, got nil)")
|
|
expect.error(w.blit, "", "", nil):eq("bad argument #3 (expected string, got nil)")
|
|
expect.error(w.blit, "", "", "a"):eq("Arguments must be the same length")
|
|
end)
|
|
end)
|
|
|
|
describe("Window.setCursorPos", function()
|
|
it("validates arguments", function()
|
|
local w = mk()
|
|
w.setCursorPos(1, 1)
|
|
|
|
expect.error(w.setCursorPos, nil):eq("bad argument #1 (expected number, got nil)")
|
|
expect.error(w.setCursorPos, 1, nil):eq("bad argument #2 (expected number, got nil)")
|
|
end)
|
|
end)
|
|
|
|
describe("Window.setCursorBlink", function()
|
|
it("validates arguments", function()
|
|
local w = mk()
|
|
w.setCursorBlink(false)
|
|
expect.error(w.setCursorBlink, nil):eq("bad argument #1 (expected boolean, got nil)")
|
|
end)
|
|
end)
|
|
|
|
describe("Window.setTextColour", function()
|
|
it("validates arguments", function()
|
|
local w = mk()
|
|
w.setTextColour(colors.white)
|
|
|
|
expect.error(w.setTextColour, nil):eq("bad argument #1 (expected number, got nil)")
|
|
expect.error(w.setTextColour, -5):eq("Invalid color (got -5)")
|
|
end)
|
|
end)
|
|
|
|
describe("Window.setPaletteColour", function()
|
|
it("validates arguments", function()
|
|
local w = mk()
|
|
w.setPaletteColour(colors.white, 0, 0, 0)
|
|
w.setPaletteColour(colors.white, 0x000000)
|
|
|
|
expect.error(w.setPaletteColour, nil):eq("bad argument #1 (expected number, got nil)")
|
|
expect.error(w.setPaletteColour, -5):eq("Invalid color (got -5)")
|
|
expect.error(w.setPaletteColour, colors.white):eq("bad argument #2 (expected number, got nil)")
|
|
expect.error(w.setPaletteColour, colors.white, 1, false):eq("bad argument #3 (expected number, got boolean)")
|
|
expect.error(w.setPaletteColour, colors.white, 1, nil, 1):eq("bad argument #3 (expected number, got nil)")
|
|
expect.error(w.setPaletteColour, colors.white, 1, 1, nil):eq("bad argument #4 (expected number, got nil)")
|
|
end)
|
|
end)
|
|
|
|
describe("Window.getPaletteColour", function()
|
|
it("validates arguments", function()
|
|
local w = mk()
|
|
w.getPaletteColour(colors.white)
|
|
expect.error(w.getPaletteColour, nil):eq("bad argument #1 (expected number, got nil)")
|
|
expect.error(w.getPaletteColour, -5):eq("Invalid color (got -5)")
|
|
end)
|
|
end)
|
|
|
|
describe("Window.setBackgroundColour", function()
|
|
it("validates arguments", function()
|
|
local w = mk()
|
|
w.setBackgroundColour(colors.white)
|
|
|
|
expect.error(w.setBackgroundColour, nil):eq("bad argument #1 (expected number, got nil)")
|
|
expect.error(w.setBackgroundColour, -5):eq("Invalid color (got -5)")
|
|
end)
|
|
end)
|
|
|
|
describe("Window.scroll", function()
|
|
it("validates arguments", function()
|
|
local w = mk()
|
|
w.scroll(0)
|
|
expect.error(w.scroll, nil):eq("bad argument #1 (expected number, got nil)")
|
|
end)
|
|
end)
|
|
|
|
describe("Window.setVisible", function()
|
|
it("validates arguments", function()
|
|
local w = mk()
|
|
w.setVisible(false)
|
|
expect.error(w.setVisible, nil):eq("bad argument #1 (expected boolean, got nil)")
|
|
end)
|
|
end)
|
|
|
|
describe("Window.reposition", function()
|
|
it("validates arguments", function()
|
|
local w = mk()
|
|
w.reposition(1, 1)
|
|
w.reposition(1, 1, 5, 5)
|
|
expect.error(w.reposition, nil):eq("bad argument #1 (expected number, got nil)")
|
|
expect.error(w.reposition, 1, nil):eq("bad argument #2 (expected number, got nil)")
|
|
expect.error(w.reposition, 1, 1, false, 1):eq("bad argument #3 (expected number, got boolean)")
|
|
expect.error(w.reposition, 1, 1, nil, 1):eq("bad argument #3 (expected number, got nil)")
|
|
expect.error(w.reposition, 1, 1, 1, nil):eq("bad argument #4 (expected number, got nil)")
|
|
end)
|
|
end)
|
|
end)
|