Add prevfile function

This commit is contained in:
The Magician 2023-11-18 17:23:38 +00:00
parent 8ea961ff93
commit d802a6e25f
2 changed files with 40 additions and 1 deletions

View File

@ -39,7 +39,7 @@ describe("ls", function()
prevfile.ls(test_directory) prevfile.ls(test_directory)
assert.stub(lfs.dir).was_called_with(test_directory) assert.spy(lfs.dir).was_called_with(test_directory)
end) end)
end) end)
@ -73,3 +73,36 @@ describe("ls", function()
end) end)
end) end)
end) end)
describe("prevfile", function()
local prevfile = require "prevfile"
insulate("if ls returns nil", function()
prevfile.ls = function() return nil end
it("should return nil", function()
local file = prevfile.prevfile()
assert.are.same(nil, file)
end)
end)
insulate("if ls returns files", function()
prevfile.ls = function() return {"a.txt", "b.txt", "c.txt", "z.txt"} end
it("should call prevfile.ls to get files", function()
spy.on(prevfile, "ls")
test_directory = "/testdir"
prevfile.prevfile(test_directory)
assert.spy(prevfile.ls).was_called_with(test_directory)
end)
it("should return the last file in the list", function()
local file = prevfile.prevfile("/testdir")
assert.are.same("z.txt", file)
end)
end)
end)

View File

@ -27,4 +27,10 @@ function prevfile.ls(directory)
return files return files
end end
function prevfile.prevfile(directory)
local files = prevfile.ls(directory)
if files == nil then return nil end
return files[#files]
end
return prevfile return prevfile