feat: Add a test file
This commit is contained in:
2
Makefile
2
Makefile
@@ -1,2 +1,2 @@
|
|||||||
test:
|
test:
|
||||||
nvim --headless -c 'lua require("plenary.test_harness"):test_directory("busted", "./lua/tests/")'
|
nvim --headless -c 'lua require("plenary.test_harness"):test_directory("busted", "./lua/tests/automated/")'
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ describe('Picker', function()
|
|||||||
it('works with one entry', function()
|
it('works with one entry', function()
|
||||||
local manager = EntryManager:new(5, nil)
|
local manager = EntryManager:new(5, nil)
|
||||||
|
|
||||||
manager:add_entry(1, "hello")
|
manager:add_entry(nil, 1, "hello")
|
||||||
|
|
||||||
assert.are.same(1, manager:get_score(1))
|
assert.are.same(1, manager:get_score(1))
|
||||||
end)
|
end)
|
||||||
@@ -31,8 +31,8 @@ describe('Picker', function()
|
|||||||
it('works with two entries', function()
|
it('works with two entries', function()
|
||||||
local manager = EntryManager:new(5, nil)
|
local manager = EntryManager:new(5, nil)
|
||||||
|
|
||||||
manager:add_entry(1, "hello")
|
manager:add_entry(nil, 1, "hello")
|
||||||
manager:add_entry(2, "later")
|
manager:add_entry(nil, 2, "later")
|
||||||
|
|
||||||
assert.are.same("hello", manager:get_entry(1))
|
assert.are.same("hello", manager:get_entry(1))
|
||||||
assert.are.same("later", manager:get_entry(2))
|
assert.are.same("later", manager:get_entry(2))
|
||||||
@@ -43,7 +43,7 @@ describe('Picker', function()
|
|||||||
local manager = EntryManager:new(5, function() called_count = called_count + 1 end)
|
local manager = EntryManager:new(5, function() called_count = called_count + 1 end)
|
||||||
|
|
||||||
assert(called_count == 0)
|
assert(called_count == 0)
|
||||||
manager:add_entry(1, "hello")
|
manager:add_entry(nil, 1, "hello")
|
||||||
assert(called_count == 1)
|
assert(called_count == 1)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
@@ -52,16 +52,16 @@ describe('Picker', function()
|
|||||||
local manager = EntryManager:new(5, function() called_count = called_count + 1 end)
|
local manager = EntryManager:new(5, function() called_count = called_count + 1 end)
|
||||||
|
|
||||||
assert(called_count == 0)
|
assert(called_count == 0)
|
||||||
manager:add_entry(1, "hello")
|
manager:add_entry(nil, 1, "hello")
|
||||||
manager:add_entry(2, "world")
|
manager:add_entry(nil, 2, "world")
|
||||||
assert(called_count == 2)
|
assert(called_count == 2)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('correctly sorts lower scores', function()
|
it('correctly sorts lower scores', function()
|
||||||
local called_count = 0
|
local called_count = 0
|
||||||
local manager = EntryManager:new(5, function() called_count = called_count + 1 end)
|
local manager = EntryManager:new(5, function() called_count = called_count + 1 end)
|
||||||
manager:add_entry(5, "worse result")
|
manager:add_entry(nil, 5, "worse result")
|
||||||
manager:add_entry(2, "better result")
|
manager:add_entry(nil, 2, "better result")
|
||||||
|
|
||||||
assert.are.same("better result", manager:get_entry(1))
|
assert.are.same("better result", manager:get_entry(1))
|
||||||
assert.are.same("worse result", manager:get_entry(2))
|
assert.are.same("worse result", manager:get_entry(2))
|
||||||
@@ -75,8 +75,8 @@ describe('Picker', function()
|
|||||||
it('respects max results', function()
|
it('respects max results', function()
|
||||||
local called_count = 0
|
local called_count = 0
|
||||||
local manager = EntryManager:new(1, function() called_count = called_count + 1 end)
|
local manager = EntryManager:new(1, function() called_count = called_count + 1 end)
|
||||||
manager:add_entry(2, "better result")
|
manager:add_entry(nil, 2, "better result")
|
||||||
manager:add_entry(5, "worse result")
|
manager:add_entry(nil, 5, "worse result")
|
||||||
|
|
||||||
assert.are.same("better result", manager:get_entry(1))
|
assert.are.same("better result", manager:get_entry(1))
|
||||||
assert.are.same(1, called_count)
|
assert.are.same(1, called_count)
|
||||||
@@ -93,7 +93,7 @@ describe('Picker', function()
|
|||||||
local manager = EntryManager:new(5)
|
local manager = EntryManager:new(5)
|
||||||
|
|
||||||
local counts_executed = 0
|
local counts_executed = 0
|
||||||
manager:add_entry(1, setmetatable({}, {
|
manager:add_entry(nil, 1, setmetatable({}, {
|
||||||
__index = function(t, k)
|
__index = function(t, k)
|
||||||
local val = nil
|
local val = nil
|
||||||
if k == "ordinal" then
|
if k == "ordinal" then
|
||||||
@@ -211,9 +211,9 @@ describe('Sorters', function()
|
|||||||
it('sort matches well', function()
|
it('sort matches well', function()
|
||||||
local sorter = require('telescope.sorters').get_generic_fuzzy_sorter()
|
local sorter = require('telescope.sorters').get_generic_fuzzy_sorter()
|
||||||
|
|
||||||
local exact_match = sorter:score('hello', 'hello')
|
local exact_match = sorter:score('hello', {ordinal = 'hello'})
|
||||||
local no_match = sorter:score('abcdef', 'ghijkl')
|
local no_match = sorter:score('abcdef', {ordinal = 'ghijkl'})
|
||||||
local ok_match = sorter:score('abcdef', 'ab')
|
local ok_match = sorter:score('abcdef', {ordinal = 'ab'})
|
||||||
|
|
||||||
assert(exact_match < no_match, "exact match better than no match")
|
assert(exact_match < no_match, "exact match better than no match")
|
||||||
assert(exact_match < ok_match, "exact match better than ok match")
|
assert(exact_match < ok_match, "exact match better than ok match")
|
||||||
@@ -234,9 +234,9 @@ describe('Sorters', function()
|
|||||||
it('sort matches well', function()
|
it('sort matches well', function()
|
||||||
local sorter = require('telescope.sorters').get_fuzzy_file()
|
local sorter = require('telescope.sorters').get_fuzzy_file()
|
||||||
|
|
||||||
local exact_match = sorter:score('abcdef', 'abcdef')
|
local exact_match = sorter:score('abcdef', {ordinal = 'abcdef'})
|
||||||
local no_match = sorter:score('abcdef', 'ghijkl')
|
local no_match = sorter:score('abcdef', {ordinal = 'ghijkl'})
|
||||||
local ok_match = sorter:score('abcdef', 'ab')
|
local ok_match = sorter:score('abcdef', {ordinal = 'ab'})
|
||||||
|
|
||||||
assert(
|
assert(
|
||||||
exact_match < no_match,
|
exact_match < no_match,
|
||||||
@@ -252,8 +252,8 @@ describe('Sorters', function()
|
|||||||
it('sorts matches after last os sep better', function()
|
it('sorts matches after last os sep better', function()
|
||||||
local sorter = require('telescope.sorters').get_fuzzy_file()
|
local sorter = require('telescope.sorters').get_fuzzy_file()
|
||||||
|
|
||||||
local better_match = sorter:score('aaa', 'bbb/aaa')
|
local better_match = sorter:score('aaa', {ordinal = 'bbb/aaa'})
|
||||||
local worse_match = sorter:score('aaa', 'aaa/bbb')
|
local worse_match = sorter:score('aaa', {ordinal = 'aaa/bbb'})
|
||||||
|
|
||||||
assert(better_match < worse_match, "Final match should be stronger")
|
assert(better_match < worse_match, "Final match should be stronger")
|
||||||
end)
|
end)
|
||||||
@@ -261,12 +261,38 @@ describe('Sorters', function()
|
|||||||
pending('sorts multiple finds better', function()
|
pending('sorts multiple finds better', function()
|
||||||
local sorter = require('telescope.sorters').get_fuzzy_file()
|
local sorter = require('telescope.sorters').get_fuzzy_file()
|
||||||
|
|
||||||
local multi_match = sorter:score('generics', 'exercises/generics/generics2.rs')
|
local multi_match = sorter:score('generics', {ordinal = 'exercises/generics/generics2.rs'})
|
||||||
local one_match = sorter:score('abcdef', 'exercises/generics/README.md')
|
local one_match = sorter:score('abcdef', {ordinal = 'exercises/generics/README.md'})
|
||||||
|
|
||||||
assert(multi_match < one_match)
|
assert(multi_match < one_match)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('layout_strategies', function()
|
||||||
|
describe('center', function()
|
||||||
|
it('should handle large terminals', function()
|
||||||
|
-- TODO: This could call layout_strategies.center w/ some weird edge case.
|
||||||
|
-- and then assert stuff about the dimensions.
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- describe('file_finder', function()
|
||||||
|
-- COMPLETED = false
|
||||||
|
-- PASSED = false
|
||||||
|
|
||||||
|
-- require('tests.helpers').auto_find_files {
|
||||||
|
-- input = 'pickers.lua',
|
||||||
|
|
||||||
|
-- condition = function()
|
||||||
|
-- print(vim.api.nvim_buf_get_name(0))
|
||||||
|
-- return string.find(vim.api.nvim_buf_get_name(0), 'pickers.lua')
|
||||||
|
-- end,
|
||||||
|
-- }
|
||||||
|
|
||||||
|
-- print("WAIT:", vim.wait(5000, function() return COMPLETED end))
|
||||||
|
-- assert(PASSED)
|
||||||
|
-- end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
86
lua/tests/helpers.lua
Normal file
86
lua/tests/helpers.lua
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
local finders = require('telescope.finders')
|
||||||
|
local make_entry = require('telescope.make_entry')
|
||||||
|
local previewers = require('telescope.previewers')
|
||||||
|
local pickers = require('telescope.pickers')
|
||||||
|
local sorters = require('telescope.sorters')
|
||||||
|
|
||||||
|
local helpers = {}
|
||||||
|
|
||||||
|
-- TODO: We should do something with builtins to get those easily.
|
||||||
|
helpers.auto_find_files = function(opts)
|
||||||
|
opts = opts or {}
|
||||||
|
opts.prompt_prefix = ''
|
||||||
|
|
||||||
|
local find_command = opts.find_command
|
||||||
|
|
||||||
|
if not find_command then
|
||||||
|
if 1 == vim.fn.executable("fd") then
|
||||||
|
find_command = { 'fd', '--type', 'f' }
|
||||||
|
elseif 1 == vim.fn.executable("fdfind") then
|
||||||
|
find_command = { 'fdfind', '--type', 'f' }
|
||||||
|
elseif 1 == vim.fn.executable("rg") then
|
||||||
|
find_command = { 'rg', '--files' }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if opts.cwd then
|
||||||
|
opts.cwd = vim.fn.expand(opts.cwd)
|
||||||
|
end
|
||||||
|
|
||||||
|
opts.entry_maker = opts.entry_maker or make_entry.gen_from_file(opts)
|
||||||
|
|
||||||
|
local p = pickers.new(opts, {
|
||||||
|
prompt = 'Find Files',
|
||||||
|
finder = finders.new_oneshot_job(
|
||||||
|
find_command,
|
||||||
|
opts
|
||||||
|
),
|
||||||
|
previewer = previewers.cat.new(opts),
|
||||||
|
sorter = sorters.get_fuzzy_file(),
|
||||||
|
|
||||||
|
track = true,
|
||||||
|
})
|
||||||
|
|
||||||
|
local count = 0
|
||||||
|
p:register_completion_callback(function(s)
|
||||||
|
print(count, vim.inspect(s.stats, {
|
||||||
|
process = function(item)
|
||||||
|
if type(item) == 'string' and item:sub(1, 1) == '_' then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
return item
|
||||||
|
end,
|
||||||
|
}))
|
||||||
|
|
||||||
|
count = count + 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
local feed = function(text, feed_opts)
|
||||||
|
feed_opts = feed_opts or 'n'
|
||||||
|
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(text, true, false, true), feed_opts, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
p:register_completion_callback(coroutine.wrap(function()
|
||||||
|
local input = opts.input
|
||||||
|
|
||||||
|
for i = 1, #input do
|
||||||
|
feed(input:sub(i, i))
|
||||||
|
coroutine.yield()
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.wait(300, function() end)
|
||||||
|
feed("<CR>", '')
|
||||||
|
|
||||||
|
vim.defer_fn(function()
|
||||||
|
PASSED = opts.condition()
|
||||||
|
COMPLETED = true
|
||||||
|
end, 500)
|
||||||
|
|
||||||
|
coroutine.yield()
|
||||||
|
end))
|
||||||
|
|
||||||
|
p:find()
|
||||||
|
end
|
||||||
|
|
||||||
|
return helpers
|
||||||
@@ -1,15 +1,14 @@
|
|||||||
RELOAD('telescope')
|
RELOAD('telescope')
|
||||||
|
|
||||||
local actions = require('telescope.actions')
|
|
||||||
local finders = require('telescope.finders')
|
local finders = require('telescope.finders')
|
||||||
local make_entry = require('telescope.make_entry')
|
local make_entry = require('telescope.make_entry')
|
||||||
local previewers = require('telescope.previewers')
|
local previewers = require('telescope.previewers')
|
||||||
local pickers = require('telescope.pickers')
|
local pickers = require('telescope.pickers')
|
||||||
local sorters = require('telescope.sorters')
|
local sorters = require('telescope.sorters')
|
||||||
local utils = require('telescope.utils')
|
|
||||||
|
|
||||||
local find_files = function(opts)
|
local find_files = function(opts)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
|
opts.prompt_prefix = ''
|
||||||
|
|
||||||
local find_command = opts.find_command
|
local find_command = opts.find_command
|
||||||
|
|
||||||
@@ -56,8 +55,9 @@ local find_files = function(opts)
|
|||||||
count = count + 1
|
count = count + 1
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local feed = function(text)
|
local feed = function(text, feed_opts)
|
||||||
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(text, true, false, true), 'n', true)
|
feed_opts = feed_opts or 'n'
|
||||||
|
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(text, true, false, true), feed_opts, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
p:register_completion_callback(coroutine.wrap(function()
|
p:register_completion_callback(coroutine.wrap(function()
|
||||||
@@ -68,10 +68,9 @@ local find_files = function(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
vim.wait(300, function() end)
|
vim.wait(300, function() end)
|
||||||
|
feed("<CR>", '')
|
||||||
|
|
||||||
vim.cmd [[:q]]
|
coroutine.yield()
|
||||||
vim.cmd [[:Messages]]
|
|
||||||
vim.cmd [[stopinsert]]
|
|
||||||
end))
|
end))
|
||||||
|
|
||||||
p:find()
|
p:find()
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ local finders = require('telescope.finders')
|
|||||||
local make_entry = require('telescope.make_entry')
|
local make_entry = require('telescope.make_entry')
|
||||||
local pickers = require('telescope.pickers')
|
local pickers = require('telescope.pickers')
|
||||||
local sorters = require('telescope.sorters')
|
local sorters = require('telescope.sorters')
|
||||||
|
local EntryManager = require('telescope.entry_manager')
|
||||||
|
|
||||||
local find_and_sort_test = function(prompt, f, s)
|
local find_and_sort_test = function(prompt, f, s)
|
||||||
local info = {}
|
local info = {}
|
||||||
@@ -23,7 +24,7 @@ local find_and_sort_test = function(prompt, f, s)
|
|||||||
info.scoring_time = 0
|
info.scoring_time = 0
|
||||||
info.set_entry = 0
|
info.set_entry = 0
|
||||||
|
|
||||||
local entry_manager = pickers.entry_manager(25, function()
|
local entry_manager = EntryManager:new(25, function()
|
||||||
info.set_entry = info.set_entry + 1
|
info.set_entry = info.set_entry + 1
|
||||||
end, info)
|
end, info)
|
||||||
|
|
||||||
Reference in New Issue
Block a user