diff --git a/lua/frecency/tests/sorter_spec.lua b/lua/frecency/tests/sorter_spec.lua new file mode 100644 index 0000000..2f38c0e --- /dev/null +++ b/lua/frecency/tests/sorter_spec.lua @@ -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)