Skip to content

Commit d39ff55

Browse files
committed
fix(base): add tests for self:remove_suffix
1 parent 26fa1f2 commit d39ff55

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lua/pathlib/base.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,9 @@ end
356356
---@param suffix string # Remove this suffix from path.
357357
function Path:remove_suffix(suffix)
358358
local basename = self:basename()
359-
if basename:sub(-suffix:len()) == suffix then
360-
basename = basename:sub(1, -suffix:len() - 1)
359+
local suffix_length = suffix:len()
360+
if suffix_length > 0 and basename:sub(-suffix_length) == suffix then
361+
basename = basename:sub(1, -suffix_length - 1)
361362
end
362363
return self:with_basename(basename)
363364
end

spec/stem_suffix_spec.lua

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,36 @@ describe("Stem / Suffix Test", function()
159159
}
160160
for _, test in ipairs(test_table) do
161161
local a, suffix, b = unpack(test)
162-
it(string.format("%s - '%s' -> %s", a, suffix, b), function()
162+
it(string.format("add: %s - '%s' -> %s", a, suffix, b), function()
163163
assert.are_equal(Posix(b), Posix(a):add_suffix(suffix))
164164
assert.are_equal(Windows(b), Windows(a):add_suffix(suffix))
165165
end)
166166
end
167167
end)
168+
169+
describe("add_suffix", function()
170+
local test_table = { -- from, suffix, to
171+
{ "folder/foo.txt", ".png", "folder/foo.txt.png" },
172+
{ "folder/foo.txt", ".txt", "folder/foo.txt.txt" },
173+
{ "foo.txt", ".bak", "foo.txt.bak" },
174+
{ "foo.tar", ".zip", "foo.tar.zip" },
175+
{ "foo.tar.gz", ".zip", "foo.tar.gz.zip" },
176+
{ "foo.png", ".tar.gz", "foo.png.tar.gz" },
177+
{ ".bashrc", ".zshrc", ".bashrc.zshrc" },
178+
{ "", ".zshrc", ".zshrc" },
179+
{ "foo", ".zip", "foo.zip" },
180+
{ "foo.txt", "", "foo.txt" },
181+
{ "my.awesome.file.png", "", "my.awesome.file.png" },
182+
{ "my.awesome.file.", "", "my.awesome.file." },
183+
{ "my.awesome.file.png", ".txt", "my.awesome.file.png.txt" },
184+
{ "my.awesome.file..", ".png", "my.awesome.file...png" },
185+
}
186+
for _, test in ipairs(test_table) do
187+
local a, suffix, b = unpack(test)
188+
it(string.format("remove: %s - '%s' -> %s", b, suffix, a), function()
189+
assert.are_equal(Posix(a), Posix(b):remove_suffix(suffix))
190+
assert.are_equal(Windows(a), Windows(b):remove_suffix(suffix))
191+
end)
192+
end
193+
end)
168194
end)

0 commit comments

Comments
 (0)