1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00
CC-Tweaked/src/test/resources/test-rom/spec/apis/peripheral_spec.lua
hydraz 9048deeb95 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)).
2019-05-30 19:36:28 +01:00

48 lines
1.8 KiB
Lua

describe("The peripheral library", function()
describe("peripheral.isPresent", function()
it("validates arguments", function()
peripheral.isPresent("")
expect.error(peripheral.isPresent, nil):eq("bad argument #1 (expected string, got nil)")
end)
end)
describe("peripheral.getType", function()
it("validates arguments", function()
peripheral.getType("")
expect.error(peripheral.getType, nil):eq("bad argument #1 (expected string, got nil)")
end)
end)
describe("peripheral.getMethods", function()
it("validates arguments", function()
peripheral.getMethods("")
expect.error(peripheral.getMethods, nil):eq("bad argument #1 (expected string, got nil)")
end)
end)
describe("peripheral.call", function()
it("validates arguments", function()
peripheral.call("", "")
expect.error(peripheral.call, nil):eq("bad argument #1 (expected string, got nil)")
expect.error(peripheral.call, "", nil):eq("bad argument #2 (expected string, got nil)")
end)
end)
describe("peripheral.wrap", function()
it("validates arguments", function()
peripheral.wrap("")
expect.error(peripheral.wrap, nil):eq("bad argument #1 (expected string, got nil)")
end)
end)
describe("peripheral.find", function()
it("validates arguments", function()
peripheral.find("")
peripheral.find("", function()
end)
expect.error(peripheral.find, nil):eq("bad argument #1 (expected string, got nil)")
expect.error(peripheral.find, "", false):eq("bad argument #2 (expected function, got boolean)")
end)
end)
end)