1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 06:33:23 +00:00

Add more tests (#253)

I'm not entirely sure how useful all of these will be yet - still
trying to work out what/when to test things, but hopefully this'll
be a useful datapoint.
This commit is contained in:
JakobDev 2019-07-08 10:24:05 +02:00 committed by SquidDev
parent 7a3f7d3bba
commit e05c262468
32 changed files with 727 additions and 20 deletions

View File

@ -17,7 +17,8 @@ ignore = {
-- are largely unsupported.
include_files = {
'src/main/resources/assets/computercraft/lua/rom',
'src/main/resources/assets/computercraft/lua/bios.lua'
'src/main/resources/assets/computercraft/lua/bios.lua',
'src/test/resources/test-rom',
}
files['src/main/resources/assets/computercraft/lua/bios.lua'] = {

View File

@ -43,14 +43,18 @@ end
local active_stubs = {}
local function default_stub() end
--- Stub a table entry with a new value.
--
-- @tparam table
-- @tparam string key The variable to stub
-- @param value The value to stub it with. If this is a function, one can use
-- the various stub expectation methods to determine what it was called with.
-- @param[opt] value The value to stub it with. If this is a function, one can
-- use the various stub expectation methods to determine what it was called
-- with. Defaults to an empty function - pass @{nil} in explicitly to set the
-- value to nil.
-- @treturn Stub The resulting stub
local function stub(tbl, key, value)
local function stub(tbl, key, ...)
check('stub', 1, 'table', tbl)
check('stub', 2, 'string', key)
@ -61,6 +65,8 @@ local function stub(tbl, key, value)
original = rawget(tbl, key),
}, stub_mt)
local value = ...
if select('#', ...) == 0 then value = default_stub end
if type(value) == "function" then
local arguments, delegate = {}, value
stub.arguments = arguments
@ -85,6 +91,7 @@ local function push_state()
output = io.output(),
dir = shell.dir(),
path = shell.path(),
aliases = shell.aliases(),
stubs = stubs,
}
end
@ -100,6 +107,14 @@ local function pop_state(state)
io.output(state.output)
shell.setDir(state.dir)
shell.setPath(state.path)
local aliases = shell.aliases()
for k in pairs(aliases) do
if not state.aliases[k] then shell.clearAlias(k) end
end
for k, v in pairs(state.aliases) do
if aliases[k] ~= v then shell.setAlias(k, v) end
end
end
local error_mt = { __tostring = function(self) return self.message end }
@ -303,10 +318,7 @@ function expect_mt:called(times)
return self
end
--- Assert that this stub was called with a set of arguments
--
-- Arguments are compared using exact equality.
function expect_mt:called_with(...)
local function called_with_check(eq, self, ...)
if getmetatable(self.value) ~= stub_mt or self.value.arguments == nil then
fail(("Expected stubbed function, got %s"):format(type(self.value)))
end
@ -314,7 +326,7 @@ function expect_mt:called_with(...)
local exp_args = table.pack(...)
local actual_args = self.value.arguments
for i = 1, #actual_args do
if pairwise_equal(actual_args[i], exp_args) then return self end
if eq(actual_args[i], exp_args) then return self end
end
local head = ("Expected stub to be called with %s\nbut was"):format(format(exp_args))
@ -330,6 +342,20 @@ function expect_mt:called_with(...)
end
end
--- Assert that this stub was called with a set of arguments
--
-- Arguments are compared using exact equality.
function expect_mt:called_with(...)
return called_with_check(pairwise_equal, self, ...)
end
--- Assert that this stub was called with a set of arguments
--
-- Arguments are compared using matching.
function expect_mt:called_with_matching(...)
return called_with_check(matches, self, ...)
end
local expect = setmetatable( {
--- Construct an expectation on the error message calling this function
-- produces

View File

@ -0,0 +1,11 @@
local capture = require "test_helpers".capture_program
describe("The bg program", function()
it("opens a tab in the background", function()
local openTab = stub(shell, "openTab", function() return 12 end)
local switchTab = stub(shell, "switchTab")
capture(stub, "bg")
expect(openTab):called_with("shell")
expect(switchTab):called(0)
end)
end)

View File

@ -0,0 +1,11 @@
local capture = require "test_helpers".capture_program
describe("The fg program", function()
it("opens the shell in the foreground", function()
local openTab = stub(shell, "openTab", function() return 12 end)
local switchTab = stub(shell, "switchTab")
capture(stub, "fg")
expect(openTab):called_with("shell")
expect(switchTab):called_with(12)
end)
end)

View File

@ -0,0 +1,28 @@
local capture = require "test_helpers".capture_program
describe("The alias program", function()
it("displays its usage when given too many arguments", function()
expect(capture(stub, "alias a b c"))
:matches { ok = true, output = "Usage: alias <alias> <program>\n", error = "" }
end)
it("lists aliases", function()
local pagedTabulate = stub(textutils, "pagedTabulate", function(x) print(table.unpack(x)) end)
stub(shell, "aliases", function() return { cp = "copy" } end)
expect(capture(stub, "alias"))
:matches { ok = true, output = "cp:copy\n", error = "" }
expect(pagedTabulate):called_with_matching({ "cp:copy" })
end)
it("sets an alias", function()
local setAlias = stub(shell, "setAlias")
capture(stub, "alias test Hello")
expect(setAlias):called_with("test", "Hello")
end)
it("clears an alias", function()
local clearAlias = stub(shell, "clearAlias")
capture(stub, "alias test")
expect(clearAlias):called_with("test")
end)
end)

View File

@ -1,19 +1,18 @@
local capture = require "test_helpers".capture_program
describe("The cd program", function()
it("cd into a directory", function()
shell.run("cd /rom/programs")
expect(shell.dir()):eq("rom/programs")
it("changes into a directory", function()
local setDir = stub(shell, "setDir")
capture(stub, "cd /rom/programs")
expect(setDir):called_with("rom/programs")
end)
it("cd into a not existing directory", function()
it("does not move into a non-existent directory", function()
expect(capture(stub, "cd /rom/nothing"))
:matches { ok = true, output = "Not a directory\n", error = "" }
end)
it("displays the usage with no arguments", function()
it("displays the usage when given no arguments", function()
expect(capture(stub, "cd"))
:matches { ok = true, output = "Usage: cd <path>\n", error = "" }
end)

View File

@ -0,0 +1,13 @@
local capture = require "test_helpers".capture_program
describe("The clear program", function()
it("clears the screen", function()
local clear = stub(term, "clear")
local setCursorPos = stub(term, "setCursorPos")
capture(stub, "clear")
expect(clear):called(1)
expect(setCursorPos):called_with(1, 1)
end)
end)

View File

@ -0,0 +1,20 @@
local capture = require "test_helpers".capture_program
describe("The commands program", function()
it("displays an error without the commands api", function()
stub(_G, "commands", nil)
expect(capture(stub, "/rom/programs/command/commands.lua"))
:matches { ok = true, output = "", error = "Requires a Command Computer.\n" }
end)
it("lists commands", function()
local pagedTabulate = stub(textutils, "pagedTabulate", function(x) print(table.unpack(x)) end)
stub(_G, "commands", {
list = function() return { "computercraft" } end
})
expect(capture(stub, "/rom/programs/command/commands.lua"))
:matches { ok = true, output = "Available commands:\ncomputercraft\n", error = "" }
expect(pagedTabulate):called_with_matching({ "computercraft" })
end)
end)

View File

@ -0,0 +1,33 @@
local capture = require "test_helpers".capture_program
describe("The exec program", function()
it("displays an error without the commands api", function()
stub(_G, "commands", nil)
expect(capture(stub, "/rom/programs/command/exec.lua"))
:matches { ok = true, output = "", error = "Requires a Command Computer.\n" }
end)
it("displays its usage when given no argument", function()
stub(_G, "commands", {})
expect(capture(stub, "/rom/programs/command/exec.lua"))
:matches { ok = true, output = "", error = "Usage: exec <command>\n" }
end)
it("runs a command", function()
stub(_G, "commands", {
exec = function() return true, {"Hello World!"} end
})
expect(capture(stub, "/rom/programs/command/exec.lua computercraft"))
:matches { ok = true, output = "Success\nHello World!\n", error = "" }
end)
it("reports command failures", function()
stub(_G,"commands",{
exec = function() return false, {"Hello World!"} end
})
expect(capture(stub, "/rom/programs/command/exec.lua computercraft"))
:matches { ok = true, output = "Hello World!\n", error = "Failed\n" }
end)
end)

View File

@ -0,0 +1,16 @@
local capture = require "test_helpers".capture_program
describe("The drive program", function()
it("run the program", function()
local getFreeSpace = stub(fs, "getFreeSpace", function() return 1234e4 end)
expect(capture(stub, "drive"))
:matches { ok = true, output = "hdd (12.3MB remaining)\n", error = "" }
expect(getFreeSpace):called(1):called_with("")
end)
it("fails on a non-existent path", function()
expect(capture(stub, "drive /rom/nothing"))
:matches { ok = true, output = "No such path\n", error = "" }
end)
end)

View File

@ -1,10 +1,9 @@
local capture = require "test_helpers".capture_program
local testFile = require "test_helpers".testFile
describe("The edit program", function()
it("displays its usage when given no argument", function()
multishell = nil
it("displays its usage when given no argument", function()
expect(capture(stub, "edit"))
:matches { ok = true, output = "Usage: edit <path>\n", error = "" }
end)

View File

@ -0,0 +1,13 @@
local capture = require "test_helpers".capture_program
describe("The eject program", function()
it("displays its usage when given no argument", function()
expect(capture(stub, "eject"))
:matches { ok = true, output = "Usage: eject <drive>\n", error = "" }
end)
it("fails when trying to eject a non-drive", function()
expect(capture(stub, "eject /rom"))
:matches { ok = true, output = "Nothing in /rom drive\n", error = "" }
end)
end)

View File

@ -0,0 +1,9 @@
local capture = require "test_helpers".capture_program
describe("The exit program", function()
it("exits the shell", function()
local exit = stub(shell, "exit")
expect(capture(stub, "exit")):matches { ok = true, combined = "" }
expect(exit):called(1)
end)
end)

View File

@ -0,0 +1,8 @@
local capture = require "test_helpers".capture_program
describe("The paint program", function()
it("displays its usage when given no arguments", function()
expect(capture(stub, "paint"))
:matches { ok = true, output = "Usage: paint <path>\n", error = "" }
end)
end)

View File

@ -0,0 +1,13 @@
local capture = require "test_helpers".capture_program
describe("The dj program", function()
it("displays its usage when given too many arguments", function()
expect(capture(stub, "dj a b c"))
:matches { ok = true, output = "Usages:\ndj play\ndj play <drive>\ndj stop\n", error = "" }
end)
it("fails when no disks are present", function()
expect(capture(stub, "dj"))
:matches { ok = true, output = "No Music Discs in attached disk drives\n", error = "" }
end)
end)

View File

@ -0,0 +1,10 @@
local capture = require "test_helpers".capture_program
describe("The hello program", function()
it("says hello", function()
local slowPrint = stub(textutils, "slowPrint", function(...) return print(...) end)
expect(capture(stub, "hello"))
:matches { ok = true, output = "Hello World!\n", error = "" }
expect(slowPrint):called(1)
end)
end)

View File

@ -0,0 +1,23 @@
local capture = require "test_helpers".capture_program
describe("The gps program", function()
it("displays its usage when given no arguments", function()
expect(capture(stub, "gps"))
:matches { ok = true, output = "Usages:\ngps host\ngps host <x> <y> <z>\ngps locate\n", error = "" }
end)
it("fails on a pocket computer", function()
stub(_G, "pocket", {})
expect(capture(stub, "gps host"))
:matches { ok = true, output = "GPS Hosts must be stationary\n", error = "" }
end)
it("can locate the computer", function()
local locate = stub(gps, "locate", function() print("Some debugging information.") end)
expect(capture(stub, "gps locate"))
:matches { ok = true, output = "Some debugging information.\n", error = "" }
expect(locate):called_with(2, true)
end)
end)

View File

@ -0,0 +1,8 @@
local capture = require "test_helpers".capture_program
describe("The help program", function()
it("errors when there is no such help file", function()
expect(capture(stub, "help nothing"))
:matches { ok = true, output = "No help available\n", error = "" }
end)
end)

View File

@ -0,0 +1,34 @@
local capture = require "test_helpers".capture_program
describe("The label program", function()
it("displays its usage when given no arguments", function()
expect(capture(stub, "label"))
:matches { ok = true, output = "Usages:\nlabel get\nlabel get <drive>\nlabel set <text>\nlabel set <drive> <text>\nlabel clear\nlabel clear <drive>\n", error = "" }
end)
describe("displays the computer's label", function()
it("when it is not labelled", function()
stub(os, "getComputerLabel", function() return nil end)
expect(capture(stub, "label get"))
:matches { ok = true, output = "No Computer label\n", error = "" }
end)
it("when it is labelled", function()
stub(os, "getComputerLabel", function() return "Test" end)
expect(capture(stub, "label get"))
:matches { ok = true, output = "Computer label is \"Test\"\n", error = "" }
end)
end)
it("sets the computer's label", function()
local setComputerLabel = stub(os, "setComputerLabel")
capture(stub, "label set Test")
expect(setComputerLabel):called_with("Test")
end)
it("clears the computer's label", function()
local setComputerLabel = stub(os, "setComputerLabel")
capture(stub, "label clear")
expect(setComputerLabel):called_with(nil)
end)
end)

View File

@ -0,0 +1,22 @@
local capture = require "test_helpers".capture_program
describe("The list program", function()
it("lists files", function()
local pagedTabulate = stub(textutils, "pagedTabulate")
capture(stub, "list /rom")
expect(pagedTabulate):called_with_matching(
colors.green, { "apis", "autorun", "help", "modules", "programs" },
colors.white, { "motd.txt", "startup.lua" }
)
end)
it("fails on a non-existent directory", function()
expect(capture(stub, "list /rom/nothing"))
:matches { ok = true, output = "", error = "Not a directory\n" }
end)
it("fails on a file", function()
expect(capture(stub, "list /rom/startup.lua"))
:matches { ok = true, output = "", error = "Not a directory\n" }
end)
end)

View File

@ -0,0 +1,8 @@
local capture = require "test_helpers".capture_program
describe("The monitor program", function()
it("displays its usage when given no arguments", function()
expect(capture(stub, "monitor"))
:matches { ok = true, output = "Usage: monitor <name> <program> <arguments>\n", error = "" }
end)
end)

View File

@ -0,0 +1,8 @@
local capture = require "test_helpers".capture_program
describe("The peripherals program", function()
it("says when there are no peripherals", function()
expect(capture(stub, "peripherals" ))
:matches { ok = true, output = "Attached Peripherals:\nNone\n", error = "" }
end)
end)

View File

@ -0,0 +1,27 @@
local capture = require "test_helpers".capture_program
describe("The pocket equip program", function()
it("errors when not a pocket computer", function()
stub(_G, "pocket", nil)
expect(capture(stub, "/rom/programs/pocket/equip.lua"))
:matches { ok = true, output = "", error = "Requires a Pocket Computer\n" }
end)
it("can equip an upgrade", function()
stub(_G, "pocket", {
equipBack = function() return true end
})
expect(capture(stub, "/rom/programs/pocket/equip.lua"))
:matches { ok = true, output = "Item equipped\n", error = "" }
end)
it("handles when an upgrade cannot be equipped", function()
stub(_G, "pocket", {
equipBack = function() return false, "Cannot equip this item." end
})
expect(capture(stub, "/rom/programs/pocket/equip.lua"))
:matches { ok = true, output = "", error = "Cannot equip this item.\n" }
end)
end)

View File

@ -0,0 +1,27 @@
local capture = require "test_helpers".capture_program
describe("The pocket unequip program", function()
it("errors when not a pocket computer", function()
stub(_G, "pocket", nil)
expect(capture(stub, "/rom/programs/pocket/unequip.lua"))
:matches { ok = true, output = "", error = "Requires a Pocket Computer\n" }
end)
it("unequips an upgrade", function()
stub(_G, "pocket", {
unequipBack = function() return true end
})
expect(capture(stub, "/rom/programs/pocket/unequip.lua"))
:matches { ok = true, output = "Item unequipped\n", error = "" }
end)
it("handles when an upgrade cannot be equipped", function()
stub(_G, "pocket", {
unequipBack = function() return false, "Nothing to remove." end
})
expect(capture(stub, "/rom/programs/pocket/unequip.lua"))
:matches { ok = true, output = "", error = "Nothing to remove.\n" }
end)
end)

View File

@ -0,0 +1,14 @@
local capture = require "test_helpers".capture_program
describe("The programs program", function()
it("list programs", function()
local programs = stub(shell, "programs", function() return { "some", "programs" } end)
local pagedTabulate = stub(textutils, "pagedTabulate", function(x) print(table.unpack(x)) end)
expect(capture(stub, "/rom/programs/programs.lua"))
:matches { ok = true, output = "some programs\n", error = "" }
expect(programs):called_with(false)
expect(pagedTabulate):called_with_matching({ "some", "programs" })
end)
end)

View File

@ -0,0 +1,14 @@
local capture = require "test_helpers".capture_program
describe("The reboot program", function()
it("sleeps and then reboots", function()
local sleep = stub(_G, "sleep")
local reboot = stub(os, "reboot")
expect(capture(stub, "reboot"))
:matches { ok = true, output = "Goodbye\n", error = "" }
expect(sleep):called_with(1)
expect(reboot):called()
end)
end)

View File

@ -0,0 +1,8 @@
local capture = require "test_helpers".capture_program
describe("The redstone program", function()
it("displays its usage when given no arguments", function()
expect(capture(stub, "redstone"))
:matches { ok = true, output = "Usages:\nredstone probe\nredstone set <side> <value>\nredstone set <side> <color> <value>\nredstone pulse <side> <count> <period>\n", error = "" }
end)
end)

View File

@ -0,0 +1,15 @@
local capture = require "test_helpers".capture_program
describe("The shutdown program", function()
it("run the program", function()
local sleep = stub(_G, "sleep")
local shutdown = stub(os, "shutdown")
expect(capture(stub, "shutdown"))
:matches { ok = true, output = "Goodbye\n", error = "" }
expect(sleep):called_with(1)
expect(shutdown):called()
end)
end)

View File

@ -0,0 +1,69 @@
local capture = require "test_helpers".capture_program
describe("The craft program", function()
it("errors when not a turtle", function()
stub(_G, "turtle", nil)
expect(capture(stub, "/rom/programs/turtle/craft.lua"))
:matches { ok = true, output = "", error = "Requires a Turtle\n" }
end)
it("fails when turtle.craft() is unavailable", function()
stub(_G, "turtle", {})
expect(capture(stub, "/rom/programs/turtle/craft.lua"))
:matches { ok = true, output = "Requires a Crafty Turtle\n", error = "" }
end)
it("displays its usage when given no arguments", function()
stub(_G, "turtle", { craft = function() end })
expect(capture(stub, "/rom/programs/turtle/craft.lua"))
:matches { ok = true, output = "Usage: craft [number]\n", error = "" }
end)
it("crafts multiple items", function()
local item_count = 3
stub(_G, "turtle", {
craft = function()
item_count = 1
return true
end,
getItemCount = function() return item_count end,
getSelectedSlot = function() return 1 end,
})
expect(capture(stub, "/rom/programs/turtle/craft.lua 2"))
:matches { ok = true, output = "2 items crafted\n", error = "" }
end)
it("craft a single item", function()
local item_count = 2
stub(_G,"turtle",{
craft = function()
item_count = 1
return true
end,
getItemCount = function() return item_count end,
getSelectedSlot = function() return 1 end,
})
expect(capture(stub, "/rom/programs/turtle/craft.lua 1"))
:matches { ok = true, output = "1 item crafted\n", error = "" }
end)
it("crafts no items", function()
local item_count = 2
stub(_G,"turtle",{
craft = function()
item_count = 1
return false
end,
getItemCount = function() return item_count end,
getSelectedSlot = function() return 1 end,
})
expect(capture(stub, "/rom/programs/turtle/craft.lua 1"))
:matches { ok = true, output = "No items crafted\n", error = "" }
end)
end)

View File

@ -0,0 +1,89 @@
local capture = require "test_helpers".capture_program
describe("The turtle equip program", function()
it("errors when not a turtle", function()
stub(_G, "turtle", nil)
expect(capture(stub, "/rom/programs/turtle/equip.lua"))
:matches { ok = true, output = "", error = "Requires a Turtle\n" }
end)
it("displays its usage when given no arguments", function()
stub(_G, "turtle", {})
expect(capture(stub, "/rom/programs/turtle/equip.lua"))
:matches { ok = true, output = "Usage: equip <slot> <side>\n", error = "" }
end)
it("equip nothing", function()
stub(_G, "turtle", {
select = function() end,
getItemCount = function() return 0 end,
})
expect(capture(stub, "/rom/programs/turtle/equip.lua 1 left"))
:matches { ok = true, output = "Nothing to equip\n", error = "" }
expect(capture(stub, "/rom/programs/turtle/equip.lua 1 right"))
:matches { ok = true, output = "Nothing to equip\n", error = "" }
end)
it("swaps existing upgrades", function()
stub(_G,"turtle",{
select = function() end,
getItemCount = function() return 1 end,
equipLeft = function() return true end,
equipRight = function() return true end,
})
expect(capture(stub, "/rom/programs/turtle/equip.lua 1 left"))
:matches { ok = true, output = "Items swapped\n", error = "" }
expect(capture(stub, "/rom/programs/turtle/equip.lua 1 right"))
:matches { ok = true, output = "Items swapped\n", error = "" }
end)
describe("equips a new upgrade", function()
local function setup()
local item_count = 1
stub(_G,"turtle",{
select = function() end,
getItemCount = function() return item_count end,
equipLeft = function()
item_count = 0
return true
end,
equipRight = function()
item_count = 0
return true
end,
})
end
it("on the left", function()
setup()
expect(capture(stub, "/rom/programs/turtle/equip.lua 1 left"))
:matches { ok = true, output = "Item equipped\n", error = "" }
end)
it("on the right", function()
setup()
expect(capture(stub, "/rom/programs/turtle/equip.lua 1 right"))
:matches { ok = true, output = "Item equipped\n", error = "" }
end)
end)
it("handles when an upgrade cannot be equipped", function()
stub(_G,"turtle",{
select = function() end,
getItemCount = function() return 1 end,
equipLeft = function() return false end,
equipRight = function() return false end,
})
expect(capture(stub, "/rom/programs/turtle/equip.lua 1 left"))
:matches { ok = true, output = "Item not equippable\n", error = "" }
expect(capture(stub, "/rom/programs/turtle/equip.lua 1 right"))
:matches { ok = true, output = "Item not equippable\n", error = "" }
end)
end)

View File

@ -0,0 +1,62 @@
local capture = require "test_helpers".capture_program
describe("The refuel program", function()
local function setup_turtle(fuel_level, fuel_limit, item_count)
stub(_G, "turtle", {
getFuelLevel = function()
return fuel_level
end,
getItemCount = function()
return item_count
end,
refuel = function(nLimit)
item_count = item_count - nLimit
fuel_level = fuel_level + nLimit
end,
select = function()
end,
getFuelLimit = function()
return fuel_limit
end
})
end
it("errors when not a turtle", function()
stub(_G, "turtle", nil)
expect(capture(stub, "/rom/programs/turtle/refuel.lua"))
:matches { ok = true, output = "", error = "Requires a Turtle\n" }
end)
it("displays its usage when given too many argument", function()
setup_turtle(0, 5, 0)
expect(capture(stub, "/rom/programs/turtle/refuel.lua a b"))
:matches { ok = true, output = "Usage: refuel [number]\n", error = "" }
end)
it("requires a numeric argument", function()
setup_turtle(0, 0, 0)
expect(capture(stub, "/rom/programs/turtle/refuel.lua nothing"))
:matches { ok = true, output = "Invalid limit, expected a number or \"all\"\n", error = "" }
end)
it("refuels the turtle", function()
setup_turtle(0, 10, 5)
expect(capture(stub, "/rom/programs/turtle/refuel.lua 5"))
:matches { ok = true, output = "Fuel level is 5\n", error = "" }
end)
it("reports when the fuel limit is reached", function()
setup_turtle(0,5,5)
expect(capture(stub, "/rom/programs/turtle/refuel.lua 5"))
:matches { ok = true, output = "Fuel level is 5\nFuel limit reached\n", error = "" }
end)
it("reports when the fuel level is unlimited", function()
setup_turtle("unlimited",5,5)
expect(capture(stub, "/rom/programs/turtle/refuel.lua 5"))
:matches { ok = true, output = "Fuel level is unlimited\n", error = "" }
end)
end)

View File

@ -0,0 +1,69 @@
local capture = require "test_helpers".capture_program
describe("The turtle unequip program", function()
it("errors when not a turtle", function()
stub(_G, "turtle", nil)
expect(capture(stub, "/rom/programs/turtle/unequip.lua"))
:matches { ok = true, output = "", error = "Requires a Turtle\n" }
end)
it("displays its usage when given no arguments", function()
stub(_G, "turtle", {})
expect(capture(stub, "/rom/programs/turtle/unequip.lua"))
:matches { ok = true, output = "Usage: unequip <side>\n", error = "" }
end)
it("says when nothing was unequipped", function()
stub(_G,"turtle",{
select = function() end,
getItemCount = function() return 0 end,
equipRight = function() return true end,
equipLeft = function() return true end
})
expect(capture(stub, "/rom/programs/turtle/unequip.lua left"))
:matches { ok = true, output = "Nothing to unequip\n", error = "" }
expect(capture(stub, "/rom/programs/turtle/unequip.lua right"))
:matches { ok = true, output = "Nothing to unequip\n", error = "" }
end)
it("unequips a upgrade", function()
local item_count = 0
stub(_G,"turtle",{
select = function() end,
getItemCount = function() return item_count end,
equipRight = function()
item_count = 1
return true
end,
equipLeft = function()
item_count = 1
return true
end
})
expect(capture(stub, "/rom/programs/turtle/unequip.lua left"))
:matches { ok = true, output = "Item unequipped\n", error = "" }
item_count = 0
expect(capture(stub, "/rom/programs/turtle/unequip.lua right"))
:matches { ok = true, output = "Item unequipped\n", error = "" }
end)
it("fails when the turtle is full", function()
stub(_G,"turtle",{
select = function() end,
getItemCount = function() return 1 end,
equipRight = function() return true end,
equipLeft = function() return true end
})
expect(capture(stub, "/rom/programs/turtle/unequip.lua left"))
:matches { ok = true, output = "No space to unequip item\n", error = "" }
expect(capture(stub, "/rom/programs/turtle/unequip.lua right"))
:matches { ok = true, output = "No space to unequip item\n", error = "" }
end)
end)