diff --git a/patchwork.md b/patchwork.md index 1fb580af8..4e8030962 100644 --- a/patchwork.md +++ b/patchwork.md @@ -275,3 +275,9 @@ matter? Nah.... Use blit to draw boxes, add colors.toBlit (#570) ``` + +``` +d13bd2cce8d102ad7f61f557e707d6fe3731bc37 + +use arg[0] in all usage printouts (#571) +``` diff --git a/src/main/resources/data/computercraft/lua/rom/programs/alias.lua b/src/main/resources/data/computercraft/lua/rom/programs/alias.lua index 79ebedc1f..c3471a723 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/alias.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/alias.lua @@ -1,6 +1,7 @@ local tArgs = { ... } if #tArgs > 2 then - print("Usage: alias ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/cd.lua b/src/main/resources/data/computercraft/lua/rom/programs/cd.lua index d1a3e86c1..f127ea88b 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/cd.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/cd.lua @@ -1,6 +1,7 @@ local tArgs = { ... } if #tArgs < 1 then - print("Usage: cd ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/command/exec.lua b/src/main/resources/data/computercraft/lua/rom/programs/command/exec.lua index 67c9446f9..1591b579d 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/command/exec.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/command/exec.lua @@ -4,7 +4,8 @@ if not commands then return end if #tArgs == 0 then - printError("Usage: exec ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + printError("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/copy.lua b/src/main/resources/data/computercraft/lua/rom/programs/copy.lua index 49d58a0b9..0977d3f8f 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/copy.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/copy.lua @@ -1,6 +1,7 @@ local tArgs = { ... } if #tArgs < 2 then - print("Usage: cp ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/delete.lua b/src/main/resources/data/computercraft/lua/rom/programs/delete.lua index 620cdd629..1d071619c 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/delete.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/delete.lua @@ -1,7 +1,8 @@ local args = table.pack(...) if args.n < 1 then - print("Usage: rm ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/edit.lua b/src/main/resources/data/computercraft/lua/rom/programs/edit.lua index 007f37a24..8f0af9356 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/edit.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/edit.lua @@ -1,7 +1,8 @@ -- Get file to edit local tArgs = { ... } if #tArgs == 0 then - print("Usage: edit ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/eject.lua b/src/main/resources/data/computercraft/lua/rom/programs/eject.lua index b15e2f20f..3deb9c2a2 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/eject.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/eject.lua @@ -1,7 +1,8 @@ -- Get arguments local tArgs = { ... } if #tArgs == 0 then - print("Usage: eject ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/fun/advanced/paint.lua b/src/main/resources/data/computercraft/lua/rom/programs/fun/advanced/paint.lua index b7c44526a..cb9de4153 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/fun/advanced/paint.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/fun/advanced/paint.lua @@ -34,7 +34,8 @@ end -- Determines if the file exists, and can be edited on this computer local tArgs = { ... } if #tArgs == 0 then - print("Usage: paint ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end local sPath = shell.resolve(tArgs[1]) diff --git a/src/main/resources/data/computercraft/lua/rom/programs/fun/dj.lua b/src/main/resources/data/computercraft/lua/rom/programs/fun/dj.lua index 322b4a887..48b4aa06c 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/fun/dj.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/fun/dj.lua @@ -1,10 +1,11 @@ local tArgs = { ... } local function printUsage() + local programName = arg[0] or fs.getName(shell.getRunningProgram()) print("Usages:") - print("dj play") - print("dj play ") - print("dj stop") + print(programName .. " play") + print(programName .. " play ") + print(programName .. " stop") end if #tArgs > 2 then diff --git a/src/main/resources/data/computercraft/lua/rom/programs/gps.lua b/src/main/resources/data/computercraft/lua/rom/programs/gps.lua index c0194443b..3e0590e9b 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/gps.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/gps.lua @@ -1,8 +1,9 @@ local function printUsage() + local programName = arg[0] or fs.getName(shell.getRunningProgram()) print("Usages:") - print("gps host") - print("gps host ") - print("gps locate") + print(programName .. " host") + print(programName .. " host ") + print(programName .. " locate") end local tArgs = { ... } diff --git a/src/main/resources/data/computercraft/lua/rom/programs/http/pastebin.lua b/src/main/resources/data/computercraft/lua/rom/programs/http/pastebin.lua index 15ad89f36..807ef603f 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/http/pastebin.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/http/pastebin.lua @@ -1,8 +1,9 @@ local function printUsage() + local programName = arg[0] or fs.getName(shell.getRunningProgram()) print("Usages:") - print("pastebin put ") - print("pastebin get ") - print("pastebin run ") + print(programName .. " put ") + print(programName .. " get ") + print(programName .. " run ") end local tArgs = { ... } diff --git a/src/main/resources/data/computercraft/lua/rom/programs/http/wget.lua b/src/main/resources/data/computercraft/lua/rom/programs/http/wget.lua index 7b5c7654e..ac5fc4fc0 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/http/wget.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/http/wget.lua @@ -1,7 +1,8 @@ local function printUsage() + local programName = arg[0] or fs.getName(shell.getRunningProgram()) print("Usage:") - print("wget [filename]") - print("wget run ") + print(programName .. " [filename]") + print(programName .. " run ") end local tArgs = { ... } diff --git a/src/main/resources/data/computercraft/lua/rom/programs/label.lua b/src/main/resources/data/computercraft/lua/rom/programs/label.lua index f857dac5c..8e413e09a 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/label.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/label.lua @@ -1,13 +1,15 @@ local function printUsage() + local programName = arg[0] or fs.getName(shell.getRunningProgram()) print("Usages:") - print("label get") - print("label get ") - print("label set ") - print("label set ") - print("label clear") - print("label clear ") + print(programName .. " get") + print(programName .. " get ") + print(programName .. " set ") + print(programName .. " set ") + print(programName .. " clear") + print(programName .. " clear ") end + local function checkDrive(sDrive) if peripheral.getType(sDrive) == "drive" then -- Check the disk exists diff --git a/src/main/resources/data/computercraft/lua/rom/programs/mkdir.lua b/src/main/resources/data/computercraft/lua/rom/programs/mkdir.lua index bbdd08002..4e1b8ac10 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/mkdir.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/mkdir.lua @@ -1,7 +1,8 @@ local tArgs = { ... } if #tArgs < 1 then - print("Usage: mkdir ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/monitor.lua b/src/main/resources/data/computercraft/lua/rom/programs/monitor.lua index 01c9d7949..e97558357 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/monitor.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/monitor.lua @@ -1,5 +1,6 @@ local function printUsage() - print("Usage: monitor ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/move.lua b/src/main/resources/data/computercraft/lua/rom/programs/move.lua index 254592200..3f68236d0 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/move.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/move.lua @@ -1,6 +1,7 @@ local tArgs = { ... } if #tArgs < 2 then - print("Usage: mv ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/rednet/chat.lua b/src/main/resources/data/computercraft/lua/rom/programs/rednet/chat.lua index 1bf5582b2..6585d0675 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/rednet/chat.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/rednet/chat.lua @@ -1,9 +1,10 @@ local tArgs = { ... } local function printUsage() + local programName = arg[0] or fs.getName(shell.getRunningProgram()) print("Usages:") - print("chat host ") - print("chat join ") + print(programName .. " host ") + print(programName .. " join ") end local sOpenedModem = nil diff --git a/src/main/resources/data/computercraft/lua/rom/programs/redstone.lua b/src/main/resources/data/computercraft/lua/rom/programs/redstone.lua index 65d6c5e1f..7c3165c1c 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/redstone.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/redstone.lua @@ -1,11 +1,12 @@ local tArgs = { ... } local function printUsage() + local programName = arg[0] or fs.getName(shell.getRunningProgram()) print("Usages:") - print("redstone probe") - print("redstone set ") - print("redstone set ") - print("redstone pulse ") + print(programName .. " probe") + print(programName .. " set ") + print(programName .. " set ") + print(programName .. " pulse ") end local sCommand = tArgs[1] diff --git a/src/main/resources/data/computercraft/lua/rom/programs/rename.lua b/src/main/resources/data/computercraft/lua/rom/programs/rename.lua index 8b491abcd..e627bb27f 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/rename.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/rename.lua @@ -1,6 +1,7 @@ local tArgs = { ... } if #tArgs < 2 then - print("Usage: rename ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/turtle/craft.lua b/src/main/resources/data/computercraft/lua/rom/programs/turtle/craft.lua index 65f93104b..34da46aea 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/turtle/craft.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/turtle/craft.lua @@ -1,5 +1,6 @@ if not turtle then - printError("Requires a Turtle") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " [number]") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/turtle/equip.lua b/src/main/resources/data/computercraft/lua/rom/programs/turtle/equip.lua index b69ef4c45..9f0d60735 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/turtle/equip.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/turtle/equip.lua @@ -5,7 +5,8 @@ end local tArgs = { ... } local function printUsage() - print("Usage: equip ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") end if #tArgs ~= 2 then diff --git a/src/main/resources/data/computercraft/lua/rom/programs/turtle/excavate.lua b/src/main/resources/data/computercraft/lua/rom/programs/turtle/excavate.lua index 9d4313eb1..1bb0488fe 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/turtle/excavate.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/turtle/excavate.lua @@ -5,7 +5,8 @@ end local tArgs = { ... } if #tArgs ~= 1 then - print("Usage: excavate ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/turtle/go.lua b/src/main/resources/data/computercraft/lua/rom/programs/turtle/go.lua index 9c6de67ce..5b6c658e2 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/turtle/go.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/turtle/go.lua @@ -5,7 +5,8 @@ end local tArgs = { ... } if #tArgs < 1 then - print("Usage: go ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/turtle/refuel.lua b/src/main/resources/data/computercraft/lua/rom/programs/turtle/refuel.lua index d9dd5b002..138cb9e47 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/turtle/refuel.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/turtle/refuel.lua @@ -6,7 +6,8 @@ end local tArgs = { ... } local nLimit = 1 if #tArgs > 1 then - print("Usage: refuel [number]") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " [number]") return elseif #tArgs > 0 then if tArgs[1] == "all" then diff --git a/src/main/resources/data/computercraft/lua/rom/programs/turtle/tunnel.lua b/src/main/resources/data/computercraft/lua/rom/programs/turtle/tunnel.lua index 34c0807d9..821965390 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/turtle/tunnel.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/turtle/tunnel.lua @@ -5,7 +5,8 @@ end local tArgs = { ... } if #tArgs ~= 1 then - print("Usage: tunnel ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/turtle/turn.lua b/src/main/resources/data/computercraft/lua/rom/programs/turtle/turn.lua index e3fbbcbd3..c4d6ceeb7 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/turtle/turn.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/turtle/turn.lua @@ -5,7 +5,8 @@ end local tArgs = { ... } if #tArgs < 1 then - print("Usage: turn ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/main/resources/data/computercraft/lua/rom/programs/turtle/unequip.lua b/src/main/resources/data/computercraft/lua/rom/programs/turtle/unequip.lua index 97501aa42..82a37c895 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/turtle/unequip.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/turtle/unequip.lua @@ -5,7 +5,8 @@ end local tArgs = { ... } local function printUsage() - print("Usage: unequip ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") end if #tArgs ~= 1 then diff --git a/src/main/resources/data/computercraft/lua/rom/programs/type.lua b/src/main/resources/data/computercraft/lua/rom/programs/type.lua index ccbaf0dd8..aa2ee54bb 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/type.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/type.lua @@ -1,6 +1,7 @@ local tArgs = { ... } if #tArgs < 1 then - print("Usage: type ") + local programName = arg[0] or fs.getName(shell.getRunningProgram()) + print("Usage: " .. programName .. " ") return end diff --git a/src/test/resources/test-rom/spec/programs/command/exec_spec.lua b/src/test/resources/test-rom/spec/programs/command/exec_spec.lua new file mode 100644 index 000000000..4e6b37b2c --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/command/exec_spec.lua @@ -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: /rom/programs/command/exec.lua \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) diff --git a/src/test/resources/test-rom/spec/programs/copy_spec.lua b/src/test/resources/test-rom/spec/programs/copy_spec.lua new file mode 100644 index 000000000..c18dd54c6 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/copy_spec.lua @@ -0,0 +1,40 @@ +local capture = require "test_helpers".capture_program + +describe("The copy program", function() + local function touch(file) + io.open(file, "w"):close() + end + + it("copies a file", function() + touch("/test-files/copy/a.txt") + + shell.run("copy /test-files/copy/a.txt /test-files/copy/b.txt") + + expect(fs.exists("/test-files/copy/a.txt")):eq(true) + expect(fs.exists("/test-files/copy/b.txt")):eq(true) + end) + + it("fails when copying a non-existent file", function() + expect(capture(stub, "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")) + :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")) + :matches { ok = true, output = "", error = "Destination is read-only\n" } + end) + + it("displays the usage when given no arguments", function() + expect(capture(stub, "copy")) + :matches { ok = true, output = "Usage: copy \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..7fa8a3857 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/move_spec.lua @@ -0,0 +1,74 @@ +local capture = require "test_helpers".capture_program + +describe("The move program", function() + local function cleanup() fs.delete("/test-files/move") end + local function touch(file) + io.open(file, "w"):close() + end + + it("move a file", function() + cleanup() + 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("moves a file to a directory", function() + cleanup() + 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")) + :matches { ok = true } + + expect(fs.exists("/test-files/move/a.txt")):eq(false) + expect(fs.exists("/test-files/move/a/a.txt")):eq(true) + end) + + it("fails when moving a file which doesn't exist", function() + expect(capture(stub, "move nothing destination")) + :matches { ok = true, output = "", error = "No matching files\n" } + end) + + it("fails when overwriting an existing file", function() + cleanup() + touch("/test-files/move/a.txt") + + expect(capture(stub, "move /test-files/move/a.txt /test-files/move/a.txt")) + :matches { ok = true, output = "", error = "Destination exists\n" } + end) + + it("fails when moving to read-only locations", function() + cleanup() + touch("/test-files/move/a.txt") + + expect(capture(stub, "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")) + :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")) + :matches { ok = true, output = "", error = "Cannot move mount /rom\n" } + end) + + it("fails when moving a file multiple times", 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")) + :matches { ok = true, output = "", error = "Cannot overwrite file multiple times\n" } + end) + + it("displays the usage with no arguments", function() + expect(capture(stub, "move")) + :matches { ok = true, output = "Usage: move \n", error = "" } + end) +end) diff --git a/src/test/resources/test-rom/spec/programs/turtle/craft_spec.lua b/src/test/resources/test-rom/spec/programs/turtle/craft_spec.lua new file mode 100644 index 000000000..95b953b38 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/turtle/craft_spec.lua @@ -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: /rom/programs/turtle/craft.lua [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) diff --git a/src/test/resources/test-rom/spec/programs/turtle/equip_spec.lua b/src/test/resources/test-rom/spec/programs/turtle/equip_spec.lua new file mode 100644 index 000000000..cc12aa367 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/turtle/equip_spec.lua @@ -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: /rom/programs/turtle/equip.lua \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) diff --git a/src/test/resources/test-rom/spec/programs/turtle/refuel_spec.lua b/src/test/resources/test-rom/spec/programs/turtle/refuel_spec.lua new file mode 100644 index 000000000..5ba397b72 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/turtle/refuel_spec.lua @@ -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: /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")) + :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) diff --git a/src/test/resources/test-rom/spec/programs/turtle/unequip_spec.lua b/src/test/resources/test-rom/spec/programs/turtle/unequip_spec.lua new file mode 100644 index 000000000..627d5952a --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/turtle/unequip_spec.lua @@ -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: /rom/programs/turtle/unequip.lua \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)