Skip to content

Commit

Permalink
fix(base): add tests for self:remove_suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
pysan3 committed Mar 28, 2024
1 parent 26fa1f2 commit d39ff55
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lua/pathlib/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,9 @@ end
---@param suffix string # Remove this suffix from path.
function Path:remove_suffix(suffix)
local basename = self:basename()
if basename:sub(-suffix:len()) == suffix then
basename = basename:sub(1, -suffix:len() - 1)
local suffix_length = suffix:len()
if suffix_length > 0 and basename:sub(-suffix_length) == suffix then
basename = basename:sub(1, -suffix_length - 1)
end
return self:with_basename(basename)
end
Expand Down
28 changes: 27 additions & 1 deletion spec/stem_suffix_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,36 @@ describe("Stem / Suffix Test", function()
}
for _, test in ipairs(test_table) do
local a, suffix, b = unpack(test)
it(string.format("%s - '%s' -> %s", a, suffix, b), function()
it(string.format("add: %s - '%s' -> %s", a, suffix, b), function()
assert.are_equal(Posix(b), Posix(a):add_suffix(suffix))
assert.are_equal(Windows(b), Windows(a):add_suffix(suffix))
end)
end
end)

describe("add_suffix", function()
local test_table = { -- from, suffix, to
{ "folder/foo.txt", ".png", "folder/foo.txt.png" },
{ "folder/foo.txt", ".txt", "folder/foo.txt.txt" },
{ "foo.txt", ".bak", "foo.txt.bak" },
{ "foo.tar", ".zip", "foo.tar.zip" },
{ "foo.tar.gz", ".zip", "foo.tar.gz.zip" },
{ "foo.png", ".tar.gz", "foo.png.tar.gz" },
{ ".bashrc", ".zshrc", ".bashrc.zshrc" },
{ "", ".zshrc", ".zshrc" },
{ "foo", ".zip", "foo.zip" },
{ "foo.txt", "", "foo.txt" },
{ "my.awesome.file.png", "", "my.awesome.file.png" },
{ "my.awesome.file.", "", "my.awesome.file." },
{ "my.awesome.file.png", ".txt", "my.awesome.file.png.txt" },
{ "my.awesome.file..", ".png", "my.awesome.file...png" },
}
for _, test in ipairs(test_table) do
local a, suffix, b = unpack(test)
it(string.format("remove: %s - '%s' -> %s", b, suffix, a), function()
assert.are_equal(Posix(a), Posix(b):remove_suffix(suffix))
assert.are_equal(Windows(a), Windows(b):remove_suffix(suffix))
end)
end
end)
end)

0 comments on commit d39ff55

Please sign in to comment.