feat: add some tests and improve perf

This commit is contained in:
TJ DeVries
2020-09-18 00:11:19 -04:00
parent f3fc1ca8c1
commit d891ba4f2e
5 changed files with 183 additions and 7 deletions

View File

@@ -118,7 +118,7 @@ builtin.live_grep = function(opts)
return flatten { conf.vimgrep_arguments, prompt } return flatten { conf.vimgrep_arguments, prompt }
end, end,
opts.entry_maker or make_entry.gen_from_vimgrep(opts), opts.entry_maker or make_entry.gen_from_vimgrep(opts),
opts.max_results or 1000 opts.max_results
) )
pickers.new(opts, { pickers.new(opts, {

View File

@@ -656,9 +656,12 @@ end
-- TODO: We should consider adding `process_bulk` or `bulk_entry_manager` for things -- TODO: We should consider adding `process_bulk` or `bulk_entry_manager` for things
-- that we always know the items and can score quickly, so as to avoid drawing so much. -- that we always know the items and can score quickly, so as to avoid drawing so much.
pickers.entry_manager = function(max_results, set_entry) pickers.entry_manager = function(max_results, set_entry, info)
log.debug("Creating entry_manager...") log.debug("Creating entry_manager...")
info = info or {}
info.items_looped = 0
-- state contains list of -- state contains list of
-- { -- {
-- score = ... -- score = ...
@@ -669,11 +672,19 @@ pickers.entry_manager = function(max_results, set_entry)
set_entry = set_entry or function() end set_entry = set_entry or function() end
local worst_acceptable_score = math.huge
return setmetatable({ return setmetatable({
add_entry = function(self, score, entry) add_entry = function(self, score, entry)
score = score or 0 score = score or 0
if score >= worst_acceptable_score then
return
end
for index, item in ipairs(entry_state) do for index, item in ipairs(entry_state) do
info.items_looped = info.items_looped + 1
if item.score > score then if item.score > score then
return self:insert(index, { return self:insert(index, {
score = score, score = score,
@@ -683,7 +694,7 @@ pickers.entry_manager = function(max_results, set_entry)
-- Don't add results that are too bad. -- Don't add results that are too bad.
if index >= max_results then if index >= max_results then
return self return
end end
end end
@@ -701,16 +712,23 @@ pickers.entry_manager = function(max_results, set_entry)
-- To insert something, we place at the next available index (or specified index) -- To insert something, we place at the next available index (or specified index)
-- and then shift all the corresponding items one place. -- and then shift all the corresponding items one place.
local next_entry local next_entry, last_score
repeat repeat
next_entry = entry_state[index] next_entry = entry_state[index]
set_entry(index, entry.entry) set_entry(index, entry.entry)
entry_state[index] = entry entry_state[index] = entry
last_score = entry.score
index = index + 1 index = index + 1
entry = next_entry entry = next_entry
until not next_entry
until not next_entry or index > max_results
if index > max_results then
worst_acceptable_score = last_score
end
end, end,
num_results = function() num_results = function()

View File

@@ -0,0 +1,87 @@
require('plenary.reload').reload_module('telescope')
--[[
Goals:
1. Easily test a sorter and finder to make sure we get all the results we need.
--]]
local finders = require('telescope.finders')
local make_entry = require('telescope.make_entry')
local pickers = require('telescope.pickers')
local sorters = require('telescope.sorters')
local find_and_sort_test = function(prompt, f, s)
local info = {}
info.start = vim.loop.hrtime()
info.filtered = 0
info.added = 0
info.scoring_time = 0
info.set_entry = 0
local entry_manager = pickers.entry_manager(25, function()
info.set_entry = info.set_entry + 1
end, info)
local completed = false
local process_result = function(entry)
local score_start = vim.loop.hrtime()
local score = s:score(prompt, entry)
info.scoring_time = info.scoring_time + (vim.loop.hrtime() - score_start) / 1e9
-- Filter these out here.
if score == -1 then
info.filtered = info.filtered + 1
return
end
info.added = info.added + 1
entry_manager:add_entry(
s:score(prompt, entry),
entry
)
end
local process_complete = function()
info.finish = vim.loop.hrtime()
info.time = (info.finish - info.start) / 1e9
info.total = info.filtered + info.added
completed = true
end
f(prompt, process_result, process_complete)
-- Wait until we're done to return
vim.wait(5000, function() return completed end, 10)
return entry_manager, info
end
local cwd = vim.fn.expand("~/")
local finder = finders.new_oneshot_job(
{"fdfind"},
{
cwd = cwd,
entry_maker = make_entry.gen_from_file {cwd = cwd},
-- disable_devicons = true,
-- maximum_results = 1000,
}
)
local res, info = find_and_sort_test(
"pickers.lua",
finder,
sorters.get_generic_fuzzy_sorter()
)
print(vim.inspect(res:get_entry(1)))
print(vim.inspect(info))
-- No skip: 2,206,186
-- Ya skip: 2,133

View File

@@ -0,0 +1,71 @@
require('plenary.test_harness'):setup_busted()
local eq = function(a, b)
assert.are.same(a, b)
end
local resolve = require('telescope.config.resolve')
describe('telescope.config.resolve', function()
describe('win_option', function()
it('should resolve for percentages', function()
local height_config = 0.8
local opt = resolve.win_option(height_config)
eq(height_config, opt.preview)
eq(height_config, opt.prompt)
eq(height_config, opt.results)
end)
it('should resolve for percetnages with default', function()
local height_config = 0.8
local opt = resolve.win_option(nil, height_config)
eq(height_config, opt.preview)
eq(height_config, opt.prompt)
eq(height_config, opt.results)
end)
it('should resolve table values', function()
local table_val = {'a'}
local opt = resolve.win_option(nil, table_val)
eq(table_val, opt.preview)
eq(table_val, opt.prompt)
eq(table_val, opt.results)
end)
it('should allow overrides for different wins', function()
local prompt_override = {'a', prompt = 'b'}
local opt = resolve.win_option(prompt_override)
eq('a', opt.preview)
eq('a', opt.results)
eq('b', opt.prompt)
end)
it('should allow overrides for all wins', function()
local all_specified = {preview = 'a', prompt = 'b', results = 'c'}
local opt = resolve.win_option(all_specified)
eq('a', opt.preview)
eq('b', opt.prompt)
eq('c', opt.results)
end)
it('should allow some specified with a simple default', function()
local some_specified = {prompt = 'b', results = 'c'}
local opt = resolve.win_option(some_specified, 'a')
eq('a', opt.preview)
eq('b', opt.prompt)
eq('c', opt.results)
end)
end)
describe('resolve_height/width', function()
eq(10, resolve.resolve_height(0.1)(nil, 24, 100))
eq(2, resolve.resolve_width(0.1)(nil, 24, 100))
eq(10, resolve.resolve_width(10)(nil, 24, 100))
eq(24, resolve.resolve_width(50)(nil, 24, 100))
end)
end)

View File

@@ -10,7 +10,7 @@ local pickers = require('telescope.pickers')
local utils = require('telescope.utils') local utils = require('telescope.utils')
--[[ --[[
require("plenary.test_harness"):test_directory("busted", "./tests/") lua RELOAD('plenary'); require("plenary.test_harness"):test_directory("busted", "./tests/automated")
--]] --]]
describe('Picker', function() describe('Picker', function()
@@ -263,7 +263,7 @@ describe('Sorters', function()
assert(better_match < worse_match, "Final match should be stronger") assert(better_match < worse_match, "Final match should be stronger")
end) end)
it('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', 'exercises/generics/generics2.rs')