From b8bd64913bc0ca36d94e8388ac1e0b9fefac6a00 Mon Sep 17 00:00:00 2001 From: Merith-TK Date: Mon, 22 Feb 2021 01:02:56 -0800 Subject: [PATCH] Add date-specific MOTDs (like Minecraft) (CCT#533) --- .../computercraft/lua/rom/programs/motd.lua | 29 +++++++++------ .../test-rom/spec/programs/motd_spec.lua | 36 +++++++++++++++++++ 2 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 src/test/resources/test-rom/spec/programs/motd_spec.lua diff --git a/src/main/resources/data/computercraft/lua/rom/programs/motd.lua b/src/main/resources/data/computercraft/lua/rom/programs/motd.lua index c8c75a40b..57ab7b916 100644 --- a/src/main/resources/data/computercraft/lua/rom/programs/motd.lua +++ b/src/main/resources/data/computercraft/lua/rom/programs/motd.lua @@ -1,15 +1,24 @@ -local tMotd = {} +local date = os.date("*t") +if date.month == 1 and date.day == 1 then + print("Happy new year!") +elseif date.month == 12 and date.day == 24 then + print("Merry X-mas!") +elseif date.month == 10 and date.day == 31 then + print("OOoooOOOoooo! Spooky!") +else + local tMotd = {} -for sPath in string.gmatch(settings.get("motd.path"), "[^:]+") do - if fs.exists(sPath) then - for sLine in io.lines(sPath) do - table.insert(tMotd, sLine) + for sPath in string.gmatch(settings.get("motd.path"), "[^:]+") do + if fs.exists(sPath) then + for sLine in io.lines(sPath) do + table.insert(tMotd, sLine) + end end end -end -if #tMotd == 0 then - print("missingno") -else - print(tMotd[math.random(1, #tMotd)]) + if #tMotd == 0 then + print("missingno") + else + print(tMotd[math.random(1, #tMotd)]) + 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..d426fb6e7 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/motd_spec.lua @@ -0,0 +1,36 @@ +local capture = require "test_helpers".capture_program + +describe("The motd program", function() + local function setup_date(month, day) + stub(os, "date", function() return { month = month, day = day } end) + end + + it("displays MOTD", function() + setup_date(0, 0) + local file = fs.open("/motd_check.txt", "w") + file.write("Hello World!") + file.close() + settings.set("motd.path", "/motd_check.txt") + + expect(capture(stub, "motd")) + :matches { ok = true, output = "Hello World!\n", error = "" } + end) + + it("displays date-specific MOTD (1/1)", function() + setup_date(1, 1) + expect(capture(stub, "motd")) + :matches { ok = true, output = "Happy new year!\n", error = "" } + end) + + it("displays date-specific MOTD (10/31)", function() + setup_date(10, 31) + expect(capture(stub, "motd")) + :matches { ok = true, output = "OOoooOOOoooo! Spooky!\n", error = "" } + end) + + it("displays date-specific MOTD (12/24)", function() + setup_date(12, 24) + expect(capture(stub, "motd")) + :matches { ok = true, output = "Merry X-mas!\n", error = "" } + end) +end)