Skip to content

Commit

Permalink
test: add tests for sorters
Browse files Browse the repository at this point in the history
  • Loading branch information
delphinus committed Nov 25, 2024
1 parent 4876b60 commit dbad3b5
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions lua/frecency/tests/sorter_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
local Default = require "frecency.sorter.default"
local Opened = require "frecency.sorter.opened"
local SameRepo = require "frecency.sorter.same_repo"

---@param text string
local function parse_text(text)
local entries = {}
for line in vim.gsplit(text, "\n", { plain = true, trimempty = true }) do
local part = vim.split(line, "%s+", { trimempty = true })
if #part == 2 then
table.insert(entries, { score = tonumber(part[1]), path = part[2] })
end
end
assert(#entries > 0)
return entries
end

local entries = [[
10 /path/to/project_A/style.css
20 /path/to/project_B/main.c
40 /path/to/project_C/lib/main.ts
60 /path/to/project_A/image.jpg
80 /path/to/project_B/Makefile
100 /path/to/project_A/index.html
]]

describe("frecency.sorter", function()
for _, c in ipairs {
{
M = Default,
name = "Default",
entries = [[
100 /path/to/project_A/index.html
80 /path/to/project_B/Makefile
60 /path/to/project_A/image.jpg
40 /path/to/project_C/lib/main.ts
20 /path/to/project_B/main.c
10 /path/to/project_A/style.css
]],
},
{
M = Opened,
name = "Opened",
entries = [[
80 /path/to/project_B/Makefile
60 /path/to/project_A/image.jpg
100 /path/to/project_A/index.html
40 /path/to/project_C/lib/main.ts
20 /path/to/project_B/main.c
10 /path/to/project_A/style.css
]],
},
{
M = SameRepo,
name = "SameRepo",
entries = [[
100 /path/to/project_A/index.html
80 /path/to/project_B/Makefile
60 /path/to/project_A/image.jpg
20 /path/to/project_B/main.c
10 /path/to/project_A/style.css
40 /path/to/project_C/lib/main.ts
]],
},
} do
it(("%s sorter returns valid entries"):format(c.name), function()
local originals = {
nvim_list_bufs = vim.api.nvim_list_bufs,
nvim_buf_get_name = vim.api.nvim_buf_get_name,
root = vim.fs.root,
}
vim.api.nvim_list_bufs = function()
return { 1, 2 }
end
vim.api.nvim_buf_get_name = function(bufnr)
return ({ "/path/to/project_A/image.jpg", "/path/to/project_B/Makefile" })[bufnr]
end
vim.fs.root = function(path, _)
local repo = path:match "(.*project_.)"
return repo
end
local sorter = c.M.new()
assert.are.same(parse_text(c.entries), sorter:sort(parse_text(entries)))
vim.api.nvim_list_bufs = originals.nvim_list_bufs
vim.api.nvim_buf_get_name = originals.nvim_buf_get_name
vim.fs.root = originals.root
end)
end
end)

0 comments on commit dbad3b5

Please sign in to comment.