feat: Add a test file

This commit is contained in:
TJ DeVries
2020-10-08 23:48:29 -04:00
parent 8c0f2630a0
commit ad12bf03d1
5 changed files with 142 additions and 30 deletions

View File

@@ -1,2 +1,2 @@
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/")'

View File

@@ -23,7 +23,7 @@ describe('Picker', function()
it('works with one entry', function()
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))
end)
@@ -31,8 +31,8 @@ describe('Picker', function()
it('works with two entries', function()
local manager = EntryManager:new(5, nil)
manager:add_entry(1, "hello")
manager:add_entry(2, "later")
manager:add_entry(nil, 1, "hello")
manager:add_entry(nil, 2, "later")
assert.are.same("hello", manager:get_entry(1))
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)
assert(called_count == 0)
manager:add_entry(1, "hello")
manager:add_entry(nil, 1, "hello")
assert(called_count == 1)
end)
@@ -52,16 +52,16 @@ describe('Picker', function()
local manager = EntryManager:new(5, function() called_count = called_count + 1 end)
assert(called_count == 0)
manager:add_entry(1, "hello")
manager:add_entry(2, "world")
manager:add_entry(nil, 1, "hello")
manager:add_entry(nil, 2, "world")
assert(called_count == 2)
end)
it('correctly sorts lower scores', function()
local called_count = 0
local manager = EntryManager:new(5, function() called_count = called_count + 1 end)
manager:add_entry(5, "worse result")
manager:add_entry(2, "better result")
manager:add_entry(nil, 5, "worse result")
manager:add_entry(nil, 2, "better result")
assert.are.same("better result", manager:get_entry(1))
assert.are.same("worse result", manager:get_entry(2))
@@ -75,8 +75,8 @@ describe('Picker', function()
it('respects max results', function()
local called_count = 0
local manager = EntryManager:new(1, function() called_count = called_count + 1 end)
manager:add_entry(2, "better result")
manager:add_entry(5, "worse result")
manager:add_entry(nil, 2, "better result")
manager:add_entry(nil, 5, "worse result")
assert.are.same("better result", manager:get_entry(1))
assert.are.same(1, called_count)
@@ -93,7 +93,7 @@ describe('Picker', function()
local manager = EntryManager:new(5)
local counts_executed = 0
manager:add_entry(1, setmetatable({}, {
manager:add_entry(nil, 1, setmetatable({}, {
__index = function(t, k)
local val = nil
if k == "ordinal" then
@@ -211,9 +211,9 @@ describe('Sorters', function()
it('sort matches well', function()
local sorter = require('telescope.sorters').get_generic_fuzzy_sorter()
local exact_match = sorter:score('hello', 'hello')
local no_match = sorter:score('abcdef', 'ghijkl')
local ok_match = sorter:score('abcdef', 'ab')
local exact_match = sorter:score('hello', {ordinal = 'hello'})
local no_match = sorter:score('abcdef', {ordinal = 'ghijkl'})
local ok_match = sorter:score('abcdef', {ordinal = 'ab'})
assert(exact_match < no_match, "exact match better than no match")
assert(exact_match < ok_match, "exact match better than ok match")
@@ -234,9 +234,9 @@ describe('Sorters', function()
it('sort matches well', function()
local sorter = require('telescope.sorters').get_fuzzy_file()
local exact_match = sorter:score('abcdef', 'abcdef')
local no_match = sorter:score('abcdef', 'ghijkl')
local ok_match = sorter:score('abcdef', 'ab')
local exact_match = sorter:score('abcdef', {ordinal = 'abcdef'})
local no_match = sorter:score('abcdef', {ordinal = 'ghijkl'})
local ok_match = sorter:score('abcdef', {ordinal = 'ab'})
assert(
exact_match < no_match,
@@ -252,8 +252,8 @@ describe('Sorters', function()
it('sorts matches after last os sep better', function()
local sorter = require('telescope.sorters').get_fuzzy_file()
local better_match = sorter:score('aaa', 'bbb/aaa')
local worse_match = sorter:score('aaa', 'aaa/bbb')
local better_match = sorter:score('aaa', {ordinal = 'bbb/aaa'})
local worse_match = sorter:score('aaa', {ordinal = 'aaa/bbb'})
assert(better_match < worse_match, "Final match should be stronger")
end)
@@ -261,12 +261,38 @@ describe('Sorters', function()
pending('sorts multiple finds better', function()
local sorter = require('telescope.sorters').get_fuzzy_file()
local multi_match = sorter:score('generics', 'exercises/generics/generics2.rs')
local one_match = sorter:score('abcdef', 'exercises/generics/README.md')
local multi_match = sorter:score('generics', {ordinal = 'exercises/generics/generics2.rs'})
local one_match = sorter:score('abcdef', {ordinal = 'exercises/generics/README.md'})
assert(multi_match < one_match)
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)

86
lua/tests/helpers.lua Normal file
View 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

View File

@@ -1,15 +1,14 @@
RELOAD('telescope')
local actions = require('telescope.actions')
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 utils = require('telescope.utils')
local find_files = function(opts)
opts = opts or {}
opts.prompt_prefix = ''
local find_command = opts.find_command
@@ -56,8 +55,9 @@ local find_files = function(opts)
count = count + 1
end)
local feed = function(text)
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(text, true, false, true), 'n', true)
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()
@@ -68,10 +68,9 @@ local find_files = function(opts)
end
vim.wait(300, function() end)
feed("<CR>", '')
vim.cmd [[:q]]
vim.cmd [[:Messages]]
vim.cmd [[stopinsert]]
coroutine.yield()
end))
p:find()

View File

@@ -12,6 +12,7 @@ local finders = require('telescope.finders')
local make_entry = require('telescope.make_entry')
local pickers = require('telescope.pickers')
local sorters = require('telescope.sorters')
local EntryManager = require('telescope.entry_manager')
local find_and_sort_test = function(prompt, f, s)
local info = {}
@@ -23,7 +24,7 @@ local find_and_sort_test = function(prompt, f, s)
info.scoring_time = 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
end, info)