diff --git a/src/test/resources/test-rom/mcfly.lua b/src/test/resources/test-rom/mcfly.lua index 02b4b26ed..38ebf9e83 100644 --- a/src/test/resources/test-rom/mcfly.lua +++ b/src/test/resources/test-rom/mcfly.lua @@ -49,6 +49,8 @@ local function push_state() term = term.current(), input = io.input(), output = io.output(), + dir = shell.dir(), + path = shell.path(), stubs = stubs, } end @@ -65,6 +67,8 @@ local function pop_state(state) term.redirect(state.term) io.input(state.input) io.output(state.output) + shell.setDir(state.dir) + shell.setPath(state.path) end local error_mt = { __tostring = function(self) return self.message end } diff --git a/src/test/resources/test-rom/spec/programs/cd_spec.lua b/src/test/resources/test-rom/spec/programs/cd_spec.lua new file mode 100644 index 000000000..2826b8007 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/cd_spec.lua @@ -0,0 +1,20 @@ +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") + end) + + it("cd into a not existing 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() + expect(capture(stub, "cd")) + :matches { ok = true, output = "Usage: cd \n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/edit_spec.lua b/src/test/resources/test-rom/spec/programs/edit_spec.lua new file mode 100644 index 000000000..dc8bcf9d0 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/edit_spec.lua @@ -0,0 +1,11 @@ +local capture = require "test_helpers".capture_program + +describe("The edit program", function() + + it("displays its usage when given no argument", function() + multishell = nil + + expect(capture(stub, "edit")) + :matches { ok = true, output = "Usage: edit \n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/http/pastebin_spec.lua b/src/test/resources/test-rom/spec/programs/http/pastebin_spec.lua new file mode 100644 index 000000000..eb55f53d4 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/http/pastebin_spec.lua @@ -0,0 +1,77 @@ +local capture = require "test_helpers".capture_program + +describe("The pastebin program", function() + local function setup_request() + stub(_G, "http", { + checkURL = function() + return true + end, + get = function() + return { + readAll = function() + return [[print("Hello", ...)]] + end, + close = function() + end, + getResponseHeaders = function() + local tHeader = {} + tHeader["Content-Type"] = "text/plain; charset=utf-8" + return tHeader + end + } + end, + post = function() + return { + readAll = function() + return "https://pastebin.com/abcde" + end, + close = function() + end, + } + end + }) + end + + it("downloads one file", function() + setup_request() + capture(stub, "pastebin", "get", "abcde", "testdown") + + expect(fs.exists("/testdown")):eq(true) + end) + + it("runs a program from the internet", function() + setup_request() + + expect(capture(stub, "pastebin", "run", "abcde", "a", "b", "c")) + :matches { ok = true, output = "Connecting to pastebin.com... Success.\nHello a b c\n", error = "" } + end) + + it("upload a program to pastebin", function() + setup_request() + + local file = fs.open( "testup", "w" ) + file.close() + + expect(capture(stub, "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" )) + :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")) + :matches { ok = true, output = "Usages:\npastebin put \npastebin get \npastebin run \n", error = "" } + end) + + it("can be completed", function() + local complete = shell.getCompletionInfo()["rom/programs/http/pastebin.lua"].fnComplete + expect(complete(shell, 1, "", {})):same { "put ", "get ", "run " } + end) +end) diff --git a/src/test/resources/test-rom/spec/http/wget_spec.lua b/src/test/resources/test-rom/spec/programs/http/wget_spec.lua similarity index 100% rename from src/test/resources/test-rom/spec/http/wget_spec.lua rename to src/test/resources/test-rom/spec/programs/http/wget_spec.lua diff --git a/src/test/resources/test-rom/spec/programs/id_spec.lua b/src/test/resources/test-rom/spec/programs/id_spec.lua new file mode 100644 index 000000000..a4e4657d3 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/id_spec.lua @@ -0,0 +1,11 @@ +local capture = require "test_helpers".capture_program + +describe("The id program", function() + + it("displays computer id", function() + local id = os.getComputerID() + + expect(capture(stub, "id")) + :matches { ok = true, output = "This is computer #"..id.."\n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/motd_spec.lua b/src/test/resources/test-rom/spec/programs/motd_spec.lua new file mode 100644 index 000000000..052cd8fa4 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/motd_spec.lua @@ -0,0 +1,14 @@ +local capture = require "test_helpers".capture_program + +describe("The motd program", function() + + it("displays MODT", function() + local file = fs.open("/modt_check.txt","w") + file.write("Hello World!") + file.close() + settings.set("motd.path","/modt_check.txt") + + expect(capture(stub, "motd")) + :matches { ok = true, output = "Hello World!\n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/move_spec.lua b/src/test/resources/test-rom/spec/programs/move_spec.lua new file mode 100644 index 000000000..de1a36569 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/move_spec.lua @@ -0,0 +1,26 @@ +local capture = require "test_helpers".capture_program + +describe("The move program", function() + local function touch(file) + io.open(file, "w"):close() + end + + it("move a file", function() + touch("/test-files/move/a.txt") + + shell.run("move /test-files/move/a.txt /test-files/move/b.txt") + + expect(fs.exists("/test-files/move/a.txt")):eq(false) + expect(fs.exists("/test-files/move/b.txt")):eq(true) + end) + + it("try to move a not existing file", function() + expect(capture(stub, "move nothing destination")) + :matches { ok = true, output = "", error = "No matching files\n" } + end) + + it("displays the usage with no arguments", function() + expect(capture(stub, "move")) + :matches { ok = true, output = "Usage: mv \n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/set_spec.lua b/src/test/resources/test-rom/spec/programs/set_spec.lua new file mode 100644 index 000000000..dc81dcfe3 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/set_spec.lua @@ -0,0 +1,29 @@ +local capture = require "test_helpers".capture_program + +describe("The set program", function() + + it("displays all settings", function() + settings.clear() + settings.set("Test","Hello World!") + settings.set("123",456) + + expect(capture(stub, "set")) + :matches { ok = true, output = '"123" is 456\n"Test" is "Hello World!"\n', error = "" } + end) + + it("displays a single settings", function() + settings.clear() + settings.set("Test","Hello World!") + settings.set("123",456) + + expect(capture(stub, "set Test")) + :matches { ok = true, output = '"Test" is "Hello World!"\n', error = "" } + end) + + it("set a setting", function() + expect(capture(stub, "set Test Hello")) + :matches { ok = true, output = '"Test" set to "Hello"\n', error = "" } + + expect(settings.get("Test")):eq("Hello") + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/shell_spec.lua b/src/test/resources/test-rom/spec/programs/shell_spec.lua index 9b9dc7188..cb35d70f6 100644 --- a/src/test/resources/test-rom/spec/programs/shell_spec.lua +++ b/src/test/resources/test-rom/spec/programs/shell_spec.lua @@ -27,6 +27,10 @@ describe("The shell", function() shell.setDir(shell.dir()) expect.error(shell.setDir, nil):eq("bad argument #1 (expected string, got nil)") end) + + it("not existing directory", function() + expect.error(shell.setDir, "/rom/nothing"):eq("Not a directory") + end) end) describe("shell.setPath", function() diff --git a/src/test/resources/test-rom/spec/programs/time_spec.lua b/src/test/resources/test-rom/spec/programs/time_spec.lua new file mode 100644 index 000000000..8ec10044e --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/time_spec.lua @@ -0,0 +1,12 @@ +local capture = require "test_helpers".capture_program + +describe("The time program", function() + + it("displays time", function() + local time = textutils.formatTime(os.time()) + local day = os.day() + + expect(capture(stub, "time")) + :matches { ok = true, output = "The time is "..time.." on Day "..day.."\n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/type_spec.lua b/src/test/resources/test-rom/spec/programs/type_spec.lua new file mode 100644 index 000000000..78009c746 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/type_spec.lua @@ -0,0 +1,26 @@ +local capture = require "test_helpers".capture_program + +describe("The type program", function() + + it("displays the usage with no arguments", function() + expect(capture(stub, "type")) + :matches { ok = true, output = "Usage: type \n", error = "" } + end) + + it("displays the output for a file", function() + expect(capture(stub, "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")) + :matches { ok = true, output = "directory\n", error = "" } + end) + + it("displays the output for a not existing path", function() + expect(capture(stub, "type /rom/nothing")) + :matches { ok = true, output = "No such path\n", error = "" } + end) + +end) +