1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-16 18:19:55 +00:00

Expose test methods to required libraries

This allows us to use stub everywhere, rather than having to pass it
around as an argument.
This commit is contained in:
Jonathan Coates 2023-01-24 18:27:30 +00:00
parent 83eddc6636
commit 7335a892b5
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
44 changed files with 158 additions and 154 deletions

View File

@ -213,7 +213,11 @@ DynamicNodeBuilder get(String name) {
void runs(String name, String uri, Executable executor) {
if (this.executor != null) throw new IllegalStateException(name + " is leaf node");
if (children.containsKey(name)) throw new IllegalStateException("Duplicate key for " + name);
if (children.containsKey(name)) {
var i = 1;
while (children.containsKey(name + i)) i++;
name = name + i;
}
children.put(name, new DynamicNodeBuilder(name, uri, executor));
}

View File

@ -182,8 +182,12 @@ end
-- @treturn string The formatted value
local function format(value)
-- TODO: Look into something like mbs's pretty printer.
local ok, res = pcall(textutils.serialise, value)
if ok then return res else return tostring(value) end
if type(value) == "string" and value:find("\n") then
return "<<<\n" .. value .. "\n>>>"
else
local ok, res = pcall(textutils.serialise, value)
if ok then return res else return tostring(value) end
end
end
local expect_mt = {}
@ -513,6 +517,7 @@ end
local function before_each(body)
check('it', 1, 'function', body)
if tests_locked then error("Cannot define before_each while running tests", 2) end
local n = before_each_fns.n + 1
before_each_fns[n], before_each_fns.n = body, n
@ -560,16 +565,11 @@ end
package.path = ("/%s/?.lua;/%s/?/init.lua;%s"):format(root_dir, root_dir, package.path)
do
-- Load in the tests from all our files
local env = setmetatable({}, { __index = _ENV })
local function set_env(tbl)
for k in pairs(env) do env[k] = nil end
for k, v in pairs(tbl) do env[k] = v end
end
-- When declaring tests, you shouldn't be able to use test methods
set_env { describe = describe, it = it, pending = pending, before_each = before_each }
-- Add our new functions to the current environment.
for k, v in pairs {
describe = describe, it = it, pending = pending, before_each = before_each,
expect = expect, fail = fail,
} do _ENV[k] = v end
local suffix = "_spec.lua"
local function run_in(sub_dir)
@ -578,7 +578,7 @@ do
if fs.isDir(file) then
run_in(file)
elseif file:sub(-#suffix) == suffix then
local fun, err = loadfile(file, nil, env)
local fun, err = loadfile(file, nil, _ENV)
if not fun then
do_test { name = file:sub(#root_dir + 2), error = { message = err } }
else
@ -591,8 +591,8 @@ do
run_in(root_dir)
-- When running tests, you shouldn't be able to declare new ones.
set_env { expect = expect, fail = fail, stub = stub }
-- Add stub later on, so its not available when running tests
_ENV.stub = stub
end
-- Error if we've found no tests

View File

@ -4,7 +4,7 @@ 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")
capture("bg")
expect(openTab):called_with("shell")
expect(switchTab):called(0)
end)

View File

@ -4,7 +4,7 @@ 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")
capture("fg")
expect(openTab):called_with("shell")
expect(switchTab):called_with(12)
end)

View File

@ -2,27 +2,27 @@ 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"))
expect(capture("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"))
expect(capture("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")
capture("alias test Hello")
expect(setAlias):called_with("test", "Hello")
end)
it("clears an alias", function()
local clearAlias = stub(shell, "clearAlias")
capture(stub, "alias test")
capture("alias test")
expect(clearAlias):called_with("test")
end)
end)

View File

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

View File

@ -5,7 +5,7 @@ describe("The clear program", function()
local clear = stub(term, "clear")
local setCursorPos = stub(term, "setCursorPos")
capture(stub, "clear")
capture("clear")
expect(clear):called(1)
expect(setCursorPos):called_with(1, 1)

View File

@ -3,7 +3,7 @@ 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"))
expect(capture("/rom/programs/command/commands.lua"))
:matches { ok = true, output = "", error = "Requires a Command Computer.\n" }
end)
@ -13,7 +13,7 @@ describe("The commands program", function()
list = function() return { "computercraft" } end,
})
expect(capture(stub, "/rom/programs/command/commands.lua"))
expect(capture("/rom/programs/command/commands.lua"))
:matches { ok = true, output = "Available commands:\ncomputercraft\n", error = "" }
expect(pagedTabulate):called_with_matching({ "computercraft" })
end)

View File

@ -3,13 +3,13 @@ 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"))
expect(capture("/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"))
expect(capture("/rom/programs/command/exec.lua"))
:matches { ok = true, output = "", error = "Usage: /rom/programs/command/exec.lua <command>\n" }
end)
@ -18,7 +18,7 @@ describe("The exec program", function()
exec = function() return true, { "Hello World!" } end,
})
expect(capture(stub, "/rom/programs/command/exec.lua computercraft"))
expect(capture("/rom/programs/command/exec.lua computercraft"))
:matches { ok = true, output = "Success\nHello World!\n", error = "" }
end)
@ -27,7 +27,7 @@ describe("The exec program", function()
exec = function() return false, { "Hello World!" } end,
})
expect(capture(stub, "/rom/programs/command/exec.lua computercraft"))
expect(capture("/rom/programs/command/exec.lua computercraft"))
:matches { ok = true, output = "Hello World!\n", error = "Failed\n" }
end)
end)

View File

@ -15,26 +15,26 @@ describe("The copy program", function()
end)
it("fails when copying a non-existent file", function()
expect(capture(stub, "copy nothing destination"))
expect(capture("copy nothing destination"))
:matches { ok = true, output = "", error = "No matching files\n" }
end)
it("fails when overwriting an existing file", function()
touch("/test-files/copy/c.txt")
expect(capture(stub, "copy /test-files/copy/c.txt /test-files/copy/c.txt"))
expect(capture("copy /test-files/copy/c.txt /test-files/copy/c.txt"))
:matches { ok = true, output = "", error = "Destination exists\n" }
end)
it("fails when copying into read-only locations", function()
touch("/test-files/copy/d.txt")
expect(capture(stub, "copy /test-files/copy/d.txt /rom/test.txt"))
expect(capture("copy /test-files/copy/d.txt /rom/test.txt"))
:matches { ok = true, output = "", error = "Destination is read-only\n" }
end)
it("displays the usage when given no arguments", function()
expect(capture(stub, "copy"))
expect(capture("copy"))
:matches { ok = true, output = "Usage: copy <source> <destination>\n", error = "" }
end)
end)

View File

@ -36,17 +36,17 @@ describe("The rm program", function()
end)
it("displays the usage with no arguments", function()
expect(capture(stub, "rm"))
expect(capture("rm"))
:matches { ok = true, output = "Usage: rm <paths>\n", error = "" }
end)
it("errors when trying to delete a read-only file", function()
expect(capture(stub, "rm /rom/startup.lua"))
expect(capture("rm /rom/startup.lua"))
:matches { ok = true, output = "", error = "Cannot delete read-only file /rom/startup.lua\n" }
end)
it("errors when trying to delete the root mount", function()
expect(capture(stub, "rm /")):matches {
expect(capture("rm /")):matches {
ok = true,
output = "To delete its contents run rm /*\n",
error = "Cannot delete mount /\n",
@ -54,7 +54,7 @@ describe("The rm program", function()
end)
it("errors when a glob fails to match", function()
expect(capture(stub, "rm", "never-existed"))
expect(capture("rm", "never-existed"))
:matches { ok = true, output = "", error = "never-existed: No matching files\n" }
end)
end)

View File

@ -4,13 +4,13 @@ describe("The drive program", function()
it("run the program", function()
local getFreeSpace = stub(fs, "getFreeSpace", function() return 1234e4 end)
expect(capture(stub, "drive"))
expect(capture("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"))
expect(capture("drive /rom/nothing"))
:matches { ok = true, output = "No such path\n", error = "" }
end)
end)

View File

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

View File

@ -2,12 +2,12 @@ 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"))
expect(capture("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"))
expect(capture("eject /rom"))
:matches { ok = true, output = "Nothing in /rom drive\n", error = "" }
end)
end)

View File

@ -3,7 +3,7 @@ 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(capture("exit")):matches { ok = true, combined = "" }
expect(exit):called(1)
end)
end)

View File

@ -2,7 +2,7 @@ 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"))
expect(capture("paint"))
:matches { ok = true, output = "Usage: paint <path>\n", error = "" }
end)
end)

View File

@ -2,12 +2,12 @@ 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"))
expect(capture("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"))
expect(capture("dj"))
:matches { ok = true, output = "No Music Discs in attached disk drives\n", error = "" }
end)
end)

View File

@ -3,7 +3,7 @@ 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"))
expect(capture("hello"))
:matches { ok = true, output = "Hello World!\n", error = "" }
expect(slowPrint):called(1)
end)

View File

@ -2,21 +2,21 @@ 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"))
expect(capture("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"))
expect(capture("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"))
expect(capture("gps locate"))
:matches { ok = true, output = "Some debugging information.\n", error = "" }
expect(locate):called_with(2, true)
end)

View File

@ -20,7 +20,7 @@ describe("The help program", function()
end
it("errors when there is no such help file", function()
expect(capture(stub, "help nothing"))
expect(capture("help nothing"))
:matches { ok = true, error = "No help available\n", output = "" }
end)

View File

@ -34,7 +34,7 @@ describe("The pastebin program", function()
it("downloads one file", function()
setup_request()
capture(stub, "pastebin", "get", "abcde", "testdown")
capture("pastebin", "get", "abcde", "testdown")
expect(fs.exists("/testdown")):eq(true)
end)
@ -42,7 +42,7 @@ describe("The pastebin program", function()
it("runs a program from the internet", function()
setup_request()
expect(capture(stub, "pastebin", "run", "abcde", "a", "b", "c"))
expect(capture("pastebin", "run", "abcde", "a", "b", "c"))
:matches { ok = true, output = "Connecting to pastebin.com... Success.\nHello a b c\n", error = "" }
end)
@ -52,21 +52,21 @@ describe("The pastebin program", function()
local file = fs.open("testup", "w")
file.close()
expect(capture(stub, "pastebin", "put", "testup"))
expect(capture("pastebin", "put", "testup"))
:matches { ok = true, output = "Connecting to pastebin.com... Success.\nUploaded as https://pastebin.com/abcde\nRun \"pastebin get abcde\" to download anywhere\n", error = "" }
end)
it("upload a not existing program to pastebin", function()
setup_request()
expect(capture(stub, "pastebin", "put", "nothing"))
expect(capture("pastebin", "put", "nothing"))
:matches { ok = true, output = "No such file\n", error = "" }
end)
it("displays its usage when given no arguments", function()
setup_request()
expect(capture(stub, "pastebin"))
expect(capture("pastebin"))
:matches { ok = true, output = "Usages:\npastebin put <filename>\npastebin get <code> <filename>\npastebin run <code> <arguments>\n", error = "" }
end)

View File

@ -23,7 +23,7 @@ describe("The wget program", function()
fs.delete("/example.com")
setup_request(default_contents)
capture(stub, "wget", "https://example.com")
capture("wget", "https://example.com")
expect(fs.exists("/example.com")):eq(true)
end)
@ -32,7 +32,7 @@ describe("The wget program", function()
fs.delete("/test-files/download")
setup_request(default_contents)
capture(stub, "wget", "https://example.com /test-files/download")
capture("wget", "https://example.com /test-files/download")
expect(fs.exists("/test-files/download")):eq(true)
end)
@ -41,7 +41,7 @@ describe("The wget program", function()
fs.delete("/test-files/download")
setup_request(nil)
capture(stub, "wget", "https://example.com", "/test-files/download")
capture("wget", "https://example.com", "/test-files/download")
expect(fs.exists("/test-files/download")):eq(true)
expect(fs.getSize("/test-files/download")):eq(0)
@ -50,7 +50,7 @@ describe("The wget program", function()
it("cannot save to rom", function()
setup_request(default_contents)
expect(capture(stub, "wget", "https://example.com", "/rom/a-file.txt")):matches {
expect(capture("wget", "https://example.com", "/rom/a-file.txt")):matches {
ok = true,
output = "Connecting to https://example.com... Success.\n",
error = "Cannot save file: /rom/a-file.txt: Access denied\n",
@ -60,14 +60,14 @@ describe("The wget program", function()
it("runs a program from the internet", function()
setup_request(default_contents)
expect(capture(stub, "wget", "run", "http://test.com", "a", "b", "c"))
expect(capture("wget", "run", "http://test.com", "a", "b", "c"))
:matches { ok = true, output = "Connecting to http://test.com... Success.\nHello a b c\n", error = "" }
end)
it("displays its usage when given no arguments", function()
setup_request(default_contents)
expect(capture(stub, "wget"))
expect(capture("wget"))
:matches { ok = true, output = "Usage:\nwget <url> [filename]\nwget run <url>\n", error = "" }
end)

View File

@ -5,7 +5,7 @@ describe("The id program", function()
it("displays computer id", function()
local id = os.getComputerID()
expect(capture(stub, "id"))
expect(capture("id"))
:matches { ok = true, output = "This is computer #" .. id .. "\n", error = "" }
end)
end)

View File

@ -2,33 +2,33 @@ 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"))
expect(capture("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"))
expect(capture("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"))
expect(capture("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")
capture("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")
capture("label clear")
expect(setComputerLabel):called_with(nil)
end)
end)

View File

@ -3,7 +3,7 @@ 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")
capture("list /rom")
expect(pagedTabulate):called_with_matching(
colors.green, { "apis", "autorun", "help", "modules", "programs" },
colors.white, { "motd.txt", "startup.lua" }
@ -11,12 +11,12 @@ describe("The list program", function()
end)
it("fails on a non-existent directory", function()
expect(capture(stub, "list /rom/nothing"))
expect(capture("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"))
expect(capture("list /rom/startup.lua"))
:matches { ok = true, output = "", error = "Not a directory\n" }
end)
end)

View File

@ -2,7 +2,7 @@ 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"))
expect(capture("monitor"))
:matches {
ok = true,
output =
@ -17,7 +17,7 @@ describe("The monitor program", function()
local r = 1
stub(peripheral, "call", function(s, f, t) r = t end)
stub(peripheral, "getType", function() return "monitor" end)
expect(capture(stub, "monitor", "scale", "left", "0.5"))
expect(capture("monitor", "scale", "left", "0.5"))
:matches { ok = true, output = "", error = "" }
expect(r):equals(0.5)
end)
@ -26,7 +26,7 @@ describe("The monitor program", function()
local r = 1
stub(peripheral, "call", function(s, f, t) r = t end)
stub(peripheral, "getType", function(side) return side == "left" and "monitor" or nil end)
expect(capture(stub, "monitor", "scale", "left"))
expect(capture("monitor", "scale", "left"))
:matches {
ok = true,
output =
@ -35,9 +35,9 @@ describe("The monitor program", function()
" monitor scale <name> <scale>\n",
error = "",
}
expect(capture(stub, "monitor", "scale", "top", "0.5"))
expect(capture("monitor", "scale", "top", "0.5"))
:matches { ok = true, output = "No monitor named top\n", error = "" }
expect(capture(stub, "monitor", "scale", "left", "aaa"))
expect(capture("monitor", "scale", "left", "aaa"))
:matches { ok = true, output = "Invalid scale: aaa\n", error = "" }
expect(r):equals(1)
end)

View File

@ -12,31 +12,31 @@ describe("The motd program", function()
file.close()
settings.set("motd.path", "/motd_check.txt")
expect(capture(stub, "motd"))
expect(capture("motd"))
:matches { ok = true, output = "Hello World!\n", error = "" }
end)
it("displays date-specific MOTD (1 Jan)", function()
setup_date(1, 1)
expect(capture(stub, "motd"))
expect(capture("motd"))
:matches { ok = true, output = "Happy new year!\n", error = "" }
end)
it("displays date-specific MOTD (28 Apr)", function()
setup_date(28, 4)
expect(capture(stub, "motd"))
expect(capture("motd"))
:matches { ok = true, output = "Ed Balls\n", error = "" }
end)
it("displays date-specific MOTD (31 Oct)", function()
setup_date(31, 10)
expect(capture(stub, "motd"))
expect(capture("motd"))
:matches { ok = true, output = "OOoooOOOoooo! Spooky!\n", error = "" }
end)
it("displays date-specific MOTD (24 Dec)", function()
setup_date(24, 12)
expect(capture(stub, "motd"))
expect(capture("motd"))
:matches { ok = true, output = "Merry X-mas!\n", error = "" }
end)
end)

View File

@ -21,7 +21,7 @@ describe("The move program", function()
touch("/test-files/move/a.txt")
fs.makeDir("/test-files/move/a")
expect(capture(stub, "move /test-files/move/a.txt /test-files/move/a"))
expect(capture("move /test-files/move/a.txt /test-files/move/a"))
:matches { ok = true }
expect(fs.exists("/test-files/move/a.txt")):eq(false)
@ -29,7 +29,7 @@ describe("The move program", function()
end)
it("fails when moving a file which doesn't exist", function()
expect(capture(stub, "move nothing destination"))
expect(capture("move nothing destination"))
:matches { ok = true, output = "", error = "No matching files\n" }
end)
@ -37,7 +37,7 @@ describe("The move program", function()
cleanup()
touch("/test-files/move/a.txt")
expect(capture(stub, "move /test-files/move/a.txt /test-files/move/a.txt"))
expect(capture("move /test-files/move/a.txt /test-files/move/a.txt"))
:matches { ok = true, output = "", error = "Destination exists\n" }
end)
@ -45,17 +45,17 @@ describe("The move program", function()
cleanup()
touch("/test-files/move/a.txt")
expect(capture(stub, "move /test-files/move/a.txt /rom/test.txt"))
expect(capture("move /test-files/move/a.txt /rom/test.txt"))
:matches { ok = true, output = "", error = "Destination is read-only\n" }
end)
it("fails when moving from read-only locations", function()
expect(capture(stub, "move /rom/startup.lua /test-files/move/not-exist.txt"))
expect(capture("move /rom/startup.lua /test-files/move/not-exist.txt"))
:matches { ok = true, output = "", error = "Cannot move read-only file /rom/startup.lua\n" }
end)
it("fails when moving mounts", function()
expect(capture(stub, "move /rom /test-files/move/rom"))
expect(capture("move /rom /test-files/move/rom"))
:matches { ok = true, output = "", error = "Cannot move mount /rom\n" }
end)
@ -63,12 +63,12 @@ describe("The move program", function()
cleanup()
touch("/test-files/move/a.txt")
touch("/test-files/move/b.txt")
expect(capture(stub, "move /test-files/move/*.txt /test-files/move/c.txt"))
expect(capture("move /test-files/move/*.txt /test-files/move/c.txt"))
:matches { ok = true, output = "", error = "Cannot overwrite file multiple times\n" }
end)
it("displays the usage with no arguments", function()
expect(capture(stub, "move"))
expect(capture("move"))
:matches { ok = true, output = "Usage: move <source> <destination>\n", error = "" }
end)
end)

View File

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

View File

@ -3,7 +3,7 @@ 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"))
expect(capture("/rom/programs/pocket/equip.lua"))
:matches { ok = true, output = "", error = "Requires a Pocket Computer\n" }
end)
@ -12,7 +12,7 @@ describe("The pocket equip program", function()
equipBack = function() return true end,
})
expect(capture(stub, "/rom/programs/pocket/equip.lua"))
expect(capture("/rom/programs/pocket/equip.lua"))
:matches { ok = true, output = "Item equipped\n", error = "" }
end)
@ -21,7 +21,7 @@ describe("The pocket equip program", function()
equipBack = function() return false, "Cannot equip this item." end,
})
expect(capture(stub, "/rom/programs/pocket/equip.lua"))
expect(capture("/rom/programs/pocket/equip.lua"))
:matches { ok = true, output = "", error = "Cannot equip this item.\n" }
end)
end)

View File

@ -3,7 +3,7 @@ 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"))
expect(capture("/rom/programs/pocket/unequip.lua"))
:matches { ok = true, output = "", error = "Requires a Pocket Computer\n" }
end)
@ -12,7 +12,7 @@ describe("The pocket unequip program", function()
unequipBack = function() return true end,
})
expect(capture(stub, "/rom/programs/pocket/unequip.lua"))
expect(capture("/rom/programs/pocket/unequip.lua"))
:matches { ok = true, output = "Item unequipped\n", error = "" }
end)
@ -21,7 +21,7 @@ describe("The pocket unequip program", function()
unequipBack = function() return false, "Nothing to remove." end,
})
expect(capture(stub, "/rom/programs/pocket/unequip.lua"))
expect(capture("/rom/programs/pocket/unequip.lua"))
:matches { ok = true, output = "", error = "Nothing to remove.\n" }
end)
end)

View File

@ -5,7 +5,7 @@ describe("The programs program", 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"))
expect(capture("/rom/programs/programs.lua"))
:matches { ok = true, output = "some programs\n", error = "" }
expect(programs):called_with(false)

View File

@ -5,7 +5,7 @@ describe("The reboot program", function()
local sleep = stub(_G, "sleep")
local reboot = stub(os, "reboot")
expect(capture(stub, "reboot"))
expect(capture("reboot"))
:matches { ok = true, output = "Goodbye\n", error = "" }
expect(sleep):called_with(1)

View File

@ -2,7 +2,7 @@ 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"))
expect(capture("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

@ -15,36 +15,36 @@ describe("The rename program", function()
end)
it("fails when renaming a file which doesn't exist", function()
expect(capture(stub, "rename nothing destination"))
expect(capture("rename nothing destination"))
:matches { ok = true, output = "", error = "No matching files\n" }
end)
it("fails when overwriting an existing file", function()
touch("/test-files/rename/c.txt")
expect(capture(stub, "rename /test-files/rename/c.txt /test-files/rename/c.txt"))
expect(capture("rename /test-files/rename/c.txt /test-files/rename/c.txt"))
:matches { ok = true, output = "", error = "Destination exists\n" }
end)
it("fails when renaming to read-only locations", function()
touch("/test-files/rename/d.txt")
expect(capture(stub, "rename /test-files/rename/d.txt /rom/test.txt"))
expect(capture("rename /test-files/rename/d.txt /rom/test.txt"))
:matches { ok = true, output = "", error = "Destination is read-only\n" }
end)
it("fails when renaming from read-only locations", function()
expect(capture(stub, "rename /rom/startup.lua /test-files/rename/d.txt"))
expect(capture("rename /rom/startup.lua /test-files/rename/d.txt"))
:matches { ok = true, output = "", error = "Source is read-only\n" }
end)
it("fails when renaming mounts", function()
expect(capture(stub, "rename /rom /test-files/rename/rom"))
expect(capture("rename /rom /test-files/rename/rom"))
:matches { ok = true, output = "", error = "Can't rename mounts\n" }
end)
it("displays the usage when given no arguments", function()
expect(capture(stub, "rename"))
expect(capture("rename"))
:matches { ok = true, output = "Usage: rename <source> <destination>\n", error = "" }
end)
end)

View File

@ -13,21 +13,21 @@ describe("The set program", function()
it("displays all settings", function()
setup()
expect(capture(stub, "set"))
expect(capture("set"))
:matches { ok = true, output = '"test" is "Hello World!"\n"test.defined" is 456\n', error = "" }
end)
it("displays a single setting", function()
setup()
expect(capture(stub, "set test"))
expect(capture("set test"))
:matches { ok = true, output = 'test is "Hello World!"\n', error = "" }
end)
it("displays a single setting with description", function()
setup()
expect(capture(stub, "set test"))
expect(capture("set test"))
:matches { ok = true, output = 'test is "Hello World!"\n', error = "" }
end)
@ -35,14 +35,14 @@ describe("The set program", function()
setup()
settings.set("test.defined", 123)
expect(capture(stub, "set test.defined"))
expect(capture("set test.defined"))
:matches { ok = true, output = 'test.defined is 123 (default is 456)\nA description\n', error = "" }
end)
it("set a setting", function()
setup()
expect(capture(stub, "set test Hello"))
expect(capture("set test Hello"))
:matches { ok = true, output = '"test" set to "Hello"\n', error = "" }
expect(settings.get("test")):eq("Hello")
@ -51,9 +51,9 @@ describe("The set program", function()
it("checks the type of a setting", function()
setup()
expect(capture(stub, "set test.defined Hello"))
expect(capture("set test.defined Hello"))
:matches { ok = true, output = "", error = '"Hello" is not a valid number.\n' }
expect(capture(stub, "set test.defined 456"))
expect(capture("set test.defined 456"))
:matches { ok = true, output = '"test.defined" set to 456\n', error = "" }
end)
end)

View File

@ -6,7 +6,7 @@ describe("The shutdown program", function()
local sleep = stub(_G, "sleep")
local shutdown = stub(os, "shutdown")
expect(capture(stub, "shutdown"))
expect(capture("shutdown"))
:matches { ok = true, output = "Goodbye\n", error = "" }
expect(sleep):called_with(1)

View File

@ -6,7 +6,7 @@ describe("The time program", function()
local time = textutils.formatTime(os.time())
local day = os.day()
expect(capture(stub, "time"))
expect(capture("time"))
:matches { ok = true, output = "The time is " .. time .. " on Day " .. day .. "\n", error = "" }
end)
end)

View File

@ -4,28 +4,28 @@ describe("The craft program", function()
it("errors when not a turtle", function()
stub(_G, "turtle", nil)
expect(capture(stub, "/rom/programs/turtle/craft.lua"))
expect(capture("/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"))
expect(capture("/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"))
expect(capture("/rom/programs/turtle/craft.lua"))
:matches { ok = true, output = "Usage: /rom/programs/turtle/craft.lua all|<number>\n", error = "" }
end)
it("displays its usage when given incorrect arguments", function()
stub(_G, "turtle", { craft = function() end })
expect(capture(stub, "/rom/programs/turtle/craft.lua a"))
expect(capture("/rom/programs/turtle/craft.lua a"))
:matches { ok = true, output = "Usage: /rom/programs/turtle/craft.lua all|<number>\n", error = "" }
end)
@ -40,7 +40,7 @@ describe("The craft program", function()
getSelectedSlot = function() return 1 end,
})
expect(capture(stub, "/rom/programs/turtle/craft.lua 2"))
expect(capture("/rom/programs/turtle/craft.lua 2"))
:matches { ok = true, output = "2 items crafted\n", error = "" }
end)
@ -55,7 +55,7 @@ describe("The craft program", function()
getSelectedSlot = function() return 1 end,
})
expect(capture(stub, "/rom/programs/turtle/craft.lua 1"))
expect(capture("/rom/programs/turtle/craft.lua 1"))
:matches { ok = true, output = "1 item crafted\n", error = "" }
end)
@ -70,7 +70,7 @@ describe("The craft program", function()
getSelectedSlot = function() return 1 end,
})
expect(capture(stub, "/rom/programs/turtle/craft.lua 1"))
expect(capture("/rom/programs/turtle/craft.lua 1"))
:matches { ok = true, output = "No items crafted\n", error = "" }
end)
@ -83,7 +83,7 @@ describe("The craft program", function()
getSelectedSlot = function() return 1 end,
})
expect(capture(stub, "/rom/programs/turtle/craft.lua all"))
expect(capture("/rom/programs/turtle/craft.lua all"))
:matches { ok = true, output = "17 items crafted\n", error = "" }
end)
end)

View File

@ -4,7 +4,7 @@ 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"))
expect(capture("/rom/programs/turtle/equip.lua"))
:matches { ok = true, output = "", error = "Requires a Turtle\n" }
end)
@ -12,7 +12,7 @@ describe("The turtle equip program", function()
it("displays its usage when given no arguments", function()
stub(_G, "turtle", {})
expect(capture(stub, "/rom/programs/turtle/equip.lua"))
expect(capture("/rom/programs/turtle/equip.lua"))
:matches { ok = true, output = "Usage: /rom/programs/turtle/equip.lua <slot> <side>\n", error = "" }
end)
@ -22,9 +22,9 @@ describe("The turtle equip program", function()
getItemCount = function() return 0 end,
})
expect(capture(stub, "/rom/programs/turtle/equip.lua 1 left"))
expect(capture("/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"))
expect(capture("/rom/programs/turtle/equip.lua 1 right"))
:matches { ok = true, output = "Nothing to equip\n", error = "" }
end)
@ -36,9 +36,9 @@ describe("The turtle equip program", function()
equipRight = function() return true end,
})
expect(capture(stub, "/rom/programs/turtle/equip.lua 1 left"))
expect(capture("/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"))
expect(capture("/rom/programs/turtle/equip.lua 1 right"))
:matches { ok = true, output = "Items swapped\n", error = "" }
end)
@ -61,13 +61,13 @@ describe("The turtle equip program", function()
it("on the left", function()
setup()
expect(capture(stub, "/rom/programs/turtle/equip.lua 1 left"))
expect(capture("/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"))
expect(capture("/rom/programs/turtle/equip.lua 1 right"))
:matches { ok = true, output = "Item equipped\n", error = "" }
end)
end)
@ -80,9 +80,9 @@ describe("The turtle equip program", function()
equipRight = function() return false end,
})
expect(capture(stub, "/rom/programs/turtle/equip.lua 1 left"))
expect(capture("/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"))
expect(capture("/rom/programs/turtle/equip.lua 1 right"))
:matches { ok = true, output = "Item not equippable\n", error = "" }
end)

View File

@ -24,39 +24,39 @@ describe("The refuel program", function()
it("errors when not a turtle", function()
stub(_G, "turtle", nil)
expect(capture(stub, "/rom/programs/turtle/refuel.lua"))
expect(capture("/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"))
expect(capture("/rom/programs/turtle/refuel.lua a b"))
:matches { ok = true, output = "Usage: /rom/programs/turtle/refuel.lua [number]\n", error = "" }
end)
it("requires a numeric argument", function()
setup_turtle(0, 0, 0)
expect(capture(stub, "/rom/programs/turtle/refuel.lua nothing"))
expect(capture("/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"))
expect(capture("/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"))
expect(capture("/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"))
expect(capture("/rom/programs/turtle/refuel.lua 5"))
:matches { ok = true, output = "Fuel level is unlimited\n", error = "" }
end)
end)

View File

@ -4,7 +4,7 @@ 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"))
expect(capture("/rom/programs/turtle/unequip.lua"))
:matches { ok = true, output = "", error = "Requires a Turtle\n" }
end)
@ -12,7 +12,7 @@ describe("The turtle unequip program", function()
it("displays its usage when given no arguments", function()
stub(_G, "turtle", {})
expect(capture(stub, "/rom/programs/turtle/unequip.lua"))
expect(capture("/rom/programs/turtle/unequip.lua"))
:matches { ok = true, output = "Usage: /rom/programs/turtle/unequip.lua <side>\n", error = "" }
end)
@ -24,9 +24,9 @@ describe("The turtle unequip program", function()
equipLeft = function() return true end,
})
expect(capture(stub, "/rom/programs/turtle/unequip.lua left"))
expect(capture("/rom/programs/turtle/unequip.lua left"))
:matches { ok = true, output = "Nothing to unequip\n", error = "" }
expect(capture(stub, "/rom/programs/turtle/unequip.lua right"))
expect(capture("/rom/programs/turtle/unequip.lua right"))
:matches { ok = true, output = "Nothing to unequip\n", error = "" }
end)
@ -45,10 +45,10 @@ describe("The turtle unequip program", function()
end,
})
expect(capture(stub, "/rom/programs/turtle/unequip.lua left"))
expect(capture("/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"))
expect(capture("/rom/programs/turtle/unequip.lua right"))
:matches { ok = true, output = "Item unequipped\n", error = "" }
end)
@ -60,9 +60,9 @@ describe("The turtle unequip program", function()
equipLeft = function() return true end,
})
expect(capture(stub, "/rom/programs/turtle/unequip.lua left"))
expect(capture("/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"))
expect(capture("/rom/programs/turtle/unequip.lua right"))
:matches { ok = true, output = "No space to unequip item\n", error = "" }
end)

View File

@ -3,22 +3,22 @@ local capture = require "test_helpers".capture_program
describe("The type program", function()
it("displays the usage with no arguments", function()
expect(capture(stub, "type"))
expect(capture("type"))
:matches { ok = true, output = "Usage: type <path>\n", error = "" }
end)
it("displays the output for a file", function()
expect(capture(stub, "type /rom/startup.lua"))
expect(capture("type /rom/startup.lua"))
:matches { ok = true, output = "file\n", error = "" }
end)
it("displays the output for a directory", function()
expect(capture(stub, "type /rom"))
expect(capture("type /rom"))
:matches { ok = true, output = "directory\n", error = "" }
end)
it("displays the output for a not existing path", function()
expect(capture(stub, "type /rom/nothing"))
expect(capture("type /rom/nothing"))
:matches { ok = true, output = "No such path\n", error = "" }
end)

View File

@ -5,7 +5,7 @@
-- @tparam string ... Arguments to this program.
-- @treturn { ok = boolean, output = string, error = string, combined = string }
-- Whether this program terminated successfully, and the various output streams.
local function capture_program(stub, program, ...)
local function capture_program(program, ...)
local output, error, combined = {}, {}, {}
local function out(stream, msg)