nextfile/tests/nextfile_spec.lua

87 lines
2.4 KiB
Lua
Raw Permalink Normal View History

describe("splitfilename", function()
2023-11-18 17:57:53 +00:00
local nextfile = require "nextfile"
it("should return the filename and extension of a filename", function()
local name, ext = nextfile.splitfilename("701.txt")
2023-11-18 17:57:53 +00:00
assert.are.equal("701", name)
2023-11-18 19:21:46 +00:00
assert.are.equal(".txt", ext)
2023-11-18 17:57:53 +00:00
end)
end)
2023-11-18 19:57:01 +00:00
describe("nextletter", function()
local nextfile = require "nextfile"
it("should return the next letter when the current letter is not 'z'", function()
local letter = nextfile.nextletter("q")
assert.are.equal("r", letter)
end)
it("should return 'za' when the current letter is 'z'", function()
local letter = nextfile.nextletter("z")
assert.are.equal("za", letter)
end)
end)
2023-11-18 12:46:28 +00:00
describe("nextfile", function()
2023-11-18 17:57:53 +00:00
local prevfile = require "prevfile"
2023-11-18 12:46:28 +00:00
local nextfile = require "nextfile"
2023-11-18 17:57:53 +00:00
insulate("prevfile.prevfile returns nil", function()
prevfile.prevfile = function() return nil end
it("should call prevfile.prevfile", function()
spy.on(prevfile, "prevfile")
test_directory = "/testdir"
nextfile.nextfile(test_directory)
assert.spy(prevfile.prevfile).was_called_with(test_directory)
end)
it("should return nil", function()
local file = nextfile.nextfile("/testdir")
assert.are.equal(nil, file)
end)
2023-11-18 12:46:28 +00:00
end)
2023-11-18 17:57:53 +00:00
2023-11-18 19:21:46 +00:00
insulate("prevfile.prevfile returns a numeric filename", function()
prevfile.prevfile = function() return "/testdir/701.txt" end
2023-11-18 19:57:01 +00:00
it("should return the result of incrementing the filename", function()
local file = nextfile.nextfile("/testdir")
assert.are.equal("/testdir/702.txt", file)
end)
end)
insulate("prevfile.prevfile returns a non-numeric filename", function()
2023-11-18 20:03:22 +00:00
it("should return the result of replacing the last character with the next letter of the alphabet", function()
prevfile.prevfile = function() return "/testdir/421b.txt" end
2023-11-18 19:57:01 +00:00
2023-11-18 20:03:22 +00:00
local file = nextfile.nextfile("/testdir")
assert.are.equal("/testdir/421c.txt", file)
end)
it("should return the result of replacing z with za when z is the last letter of the filename", function()
prevfile.prevfile = function() return "/testdir/421z.txt" end
2023-11-18 19:21:46 +00:00
2023-11-18 20:03:22 +00:00
local file = nextfile.nextfile("/testdir")
assert.are.equal("/testdir/421za.txt", file)
end)
it("should use the provided file extension if one is given", function()
prevfile.prevfile = function() return "/testdir/900.txt" end
local file = nextfile.nextfile("/testdir", "yaml")
assert.are.equal("/testdir/901.yaml", file)
end)
2023-11-18 19:21:46 +00:00
end)
2023-11-18 12:46:28 +00:00
end)