From 04e963826107ea740cdfd70f5fdd10172fc8a083 Mon Sep 17 00:00:00 2001 From: The Magician Date: Sat, 18 Nov 2023 17:57:53 +0000 Subject: [PATCH] Add basename function to nextfile --- spec/nextfile_spec.lua | 48 +++++++++++++++++++++++++++++++++++++++--- src/nextfile.lua | 12 +++++++++-- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/spec/nextfile_spec.lua b/spec/nextfile_spec.lua index 48a2e4d..421a52b 100644 --- a/spec/nextfile_spec.lua +++ b/spec/nextfile_spec.lua @@ -1,7 +1,49 @@ -describe("nextfile", function() +describe("basename", function() local nextfile = require "nextfile" - it("returns true", function() - assert.is_true(nextfile.nextfile()) + it("should return the basename of an absolute filepath", function() + local name = nextfile.basename("/testdir/701.txt") + + assert.are.equal("701", name) + end) + + it("should return the basename of a relative filepath", function() + local name = nextfile.basename("../../foo/testdir/file.mp3") + + assert.are.equal("file", name) end) end) + +describe("nextfile", function() + local prevfile = require "prevfile" + local nextfile = require "nextfile" + + 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) + end) + +-- insulate("prevfile.prevfile returns a numeric filename", function() +-- prevfile.prevfile = function() return "/testdir/701.txt" end +-- +-- it("should return the result of incrementing the filename", function() +-- local file = nextfile.nextfile("/testdir") +-- +-- assert.are.equal("/testdir/702.txt", file) +-- end) +-- end) +end) diff --git a/src/nextfile.lua b/src/nextfile.lua index 1391628..e0f6735 100644 --- a/src/nextfile.lua +++ b/src/nextfile.lua @@ -1,7 +1,15 @@ +local prevfile = require "prevfile" + local nextfile = {} -function nextfile.nextfile() - return true +function nextfile.basename(path) + return path:match("^.*/(.*)%.%w*") + +end + +function nextfile.nextfile(directory) + prevfile.prevfile(directory) + return nil end return nextfile