test(utils): add unit test for transform_path (#3013)
This commit is contained in:
@@ -1,27 +1,42 @@
|
||||
local Path = require "plenary.path"
|
||||
local utils = require "telescope.utils"
|
||||
|
||||
local eq = assert.are.equal
|
||||
|
||||
describe("path_expand()", function()
|
||||
it("removes trailing /", function()
|
||||
assert.is.equal("/home/user", utils.path_expand "/home/user/")
|
||||
it("removes trailing os_sep", function()
|
||||
if utils.iswin then
|
||||
eq([[C:\Users\a\b]], utils.path_expand [[C:\Users\a\b\]])
|
||||
else
|
||||
eq("/home/user", utils.path_expand "/home/user/")
|
||||
end
|
||||
end)
|
||||
|
||||
it("works with /", function()
|
||||
assert.is.equal("/", utils.path_expand "/")
|
||||
it("works with root dir", function()
|
||||
if utils.iswin then
|
||||
eq([[C:\]], utils.path_expand [[C:\]])
|
||||
else
|
||||
eq("/", utils.path_expand "/")
|
||||
end
|
||||
end)
|
||||
|
||||
it("works with ~", function()
|
||||
assert.is.equal(vim.loop.os_homedir() .. "/src/foo", utils.path_expand "~/src/foo")
|
||||
eq(vim.loop.os_homedir() .. "/src/foo", utils.path_expand "~/src/foo")
|
||||
end)
|
||||
|
||||
it("handles duplicate /", function()
|
||||
assert.is.equal("/home/user", utils.path_expand "/home///user")
|
||||
it("handles duplicate os_sep", function()
|
||||
if utils.iswin then
|
||||
eq([[C:\Users\a]], utils.path_expand [[C:\\\Users\\a]])
|
||||
else
|
||||
eq("/home/user", utils.path_expand "/home///user")
|
||||
end
|
||||
end)
|
||||
|
||||
it("preserves fake whitespace characters and whitespace", function()
|
||||
local path_space = "/home/user/hello world"
|
||||
assert.is.equal(path_space, utils.path_expand(path_space))
|
||||
eq(path_space, utils.path_expand(path_space))
|
||||
local path_newline = [[/home/user/hello\nworld]]
|
||||
assert.is.equal(path_newline, utils.path_expand(path_newline))
|
||||
eq(path_newline, utils.path_expand(path_newline))
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -140,9 +155,106 @@ describe("__separates_file_path_location", function()
|
||||
it("separtates file path for " .. suite.input, function()
|
||||
local file, row, col = utils.__separate_file_path_location(suite.input)
|
||||
|
||||
assert.are.equal(file, suite.file)
|
||||
assert.are.equal(row, suite.row)
|
||||
assert.are.equal(col, suite.col)
|
||||
eq(file, suite.file)
|
||||
eq(row, suite.row)
|
||||
eq(col, suite.col)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
describe("transform_path", function()
|
||||
local cwd = (function()
|
||||
if utils.iswin then
|
||||
return [[C:\Users\user\projects\telescope.nvim]]
|
||||
else
|
||||
return "/home/user/projects/telescope.nvim"
|
||||
end
|
||||
end)()
|
||||
|
||||
local function new_relpath(unix_path)
|
||||
return Path:new(unpack(vim.split(unix_path, "/"))).filename
|
||||
end
|
||||
|
||||
local function assert_path(path_display, path, expect)
|
||||
local opts = { cwd = cwd, __length = 15 }
|
||||
if type(path_display) == "string" then
|
||||
opts.path_display = { path_display }
|
||||
eq(expect, utils.transform_path(opts, path))
|
||||
opts.path_display = { [path_display] = true }
|
||||
eq(expect, utils.transform_path(opts, path))
|
||||
elseif type(path_display) == "table" then
|
||||
opts.path_display = path_display
|
||||
eq(expect, utils.transform_path(opts, path))
|
||||
elseif path_display == nil then
|
||||
eq(expect, utils.transform_path(opts, path))
|
||||
end
|
||||
end
|
||||
|
||||
it("handles nil path", function()
|
||||
assert_path(nil, nil, "")
|
||||
end)
|
||||
|
||||
it("returns back uri", function()
|
||||
local uri = [[https://www.example.com/index.html]]
|
||||
assert_path(nil, uri, uri)
|
||||
end)
|
||||
|
||||
it("handles 'hidden' path_display", function()
|
||||
eq("", utils.transform_path({ cwd = cwd, path_display = "hidden" }, "foobar"))
|
||||
assert_path("hidden", "foobar", "")
|
||||
end)
|
||||
|
||||
it("returns relative path for default opts", function()
|
||||
local relative = Path:new { "lua", "telescope", "init.lua" }
|
||||
local absolute = Path:new { cwd, relative }
|
||||
assert_path(nil, absolute.filename, relative.filename)
|
||||
assert_path(nil, relative.filename, relative.filename)
|
||||
end)
|
||||
|
||||
it("handles 'tail' path_display", function()
|
||||
local path = new_relpath "lua/telescope/init.lua"
|
||||
assert_path("tail", path, "init.lua")
|
||||
end)
|
||||
|
||||
it("handles 'smart' path_display", function()
|
||||
local path1 = new_relpath "lua/telescope/init.lua"
|
||||
local path2 = new_relpath "lua/telescope/finders.lua"
|
||||
local path3 = new_relpath "lua/telescope/finders/async_job_finder.lua"
|
||||
local path4 = new_relpath "plugin/telescope.lua"
|
||||
|
||||
assert_path("smart", path1, path1)
|
||||
assert_path("smart", path2, new_relpath "../telescope/finders.lua")
|
||||
assert_path("smart", path3, new_relpath "../telescope/finders/async_job_finder.lua")
|
||||
assert_path("smart", path4, path4)
|
||||
end)
|
||||
|
||||
it("handles 'absolute' path_display", function()
|
||||
local relative = Path:new { "lua", "telescope", "init.lua" }
|
||||
local absolute = Path:new { cwd, relative }
|
||||
|
||||
-- TODO: feels like 'absolute' should turn relative paths to absolute
|
||||
-- assert_path("absolute", relative.filename, absolute.filename)
|
||||
assert_path("absolute", absolute.filename, absolute.filename)
|
||||
end)
|
||||
|
||||
it("handles default 'shorten' path_display", function()
|
||||
assert_path("shorten", new_relpath "lua/telescope/init.lua", new_relpath "l/t/init.lua")
|
||||
end)
|
||||
|
||||
it("handles 'shorten' with number", function()
|
||||
assert_path({ shorten = 2 }, new_relpath "lua/telescope/init.lua", new_relpath "lu/te/init.lua")
|
||||
end)
|
||||
|
||||
it("handles 'shorten' with option table", function()
|
||||
assert_path({ shorten = { len = 2 } }, new_relpath "lua/telescope/init.lua", new_relpath "lu/te/init.lua")
|
||||
assert_path(
|
||||
{ shorten = { len = 2, exclude = { 1, 3, -1 } } },
|
||||
new_relpath "lua/telescope/builtin/init.lua",
|
||||
new_relpath "lua/te/builtin/init.lua"
|
||||
)
|
||||
end)
|
||||
|
||||
it("handles default 'truncate' path_display", function()
|
||||
assert_path({ "truncate" }, new_relpath "lua/telescope/init.lua", new_relpath "…scope/init.lua")
|
||||
end)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user