ci: more tests (#359)

* more tests

* lint
This commit is contained in:
TJ DeVries
2020-12-23 13:31:05 -05:00
committed by GitHub
parent 4850c6df6d
commit 049602a2c5
5 changed files with 93 additions and 54 deletions

View File

@@ -30,17 +30,16 @@ tester.picker_feed = function(input, test_cases, debug)
end
end
vim.wait(100, function() end)
vim.wait(10, function() end)
local timer = vim.loop.new_timer()
timer:start(200, 0, vim.schedule_wrap(function()
timer:start(20, 0, vim.schedule_wrap(function()
if test_cases.post_close then
for k, v in ipairs(test_cases.post_close) do
io.stderr:write(vim.fn.json_encode({ case = k, expected = v[1], actual = v[2]() }))
io.stderr:write("\n")
end
end
vim.wait(10)
if debug then
return
@@ -48,7 +47,7 @@ tester.picker_feed = function(input, test_cases, debug)
vim.defer_fn(function()
vim.cmd [[qa!]]
end, 15)
end, 10)
end))
if not debug then
@@ -74,10 +73,25 @@ end
-- { "README.md", function() return "README.md" end },
-- },
-- }
local _VALID_KEYS = {
post_typed = true,
post_close = true,
}
tester.builtin_picker = function(key, input, test_cases, opts)
opts = opts or {}
local debug = opts.debug or false
for k, _ in pairs(test_cases) do
if not _VALID_KEYS[k] then
-- TODO: Make an error type for the json protocol.
io.stderr:write(vim.fn.json_encode({ case = k, expected = '<a valid key>', actual = k }))
io.stderr:write("\n")
vim.cmd [[qa!]]
end
end
opts.on_complete = {
tester.picker_feed(input, test_cases, debug)
}
@@ -119,32 +133,10 @@ tester.run_string = function(contents)
local tempname = vim.fn.tempname()
contents = [[
-- TODO: Add globals!
-- local tester = require('telescope.pickers._tests')
local tester = require('telescope.pickers._tests')
local tester = require('telescope.pickers._test')
local helper = require('telescope.pickers._test_helpers')
local get_picker = function()
local state = require('telescope.state')
return state.get_status(vim.api.nvim_get_current_buf()).picker
end
local get_results_bufnr = function()
local state = require('telescope.state')
return state.get_status(vim.api.nvim_get_current_buf()).results_bufnr
end
local GetFile = function() return vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":t") end
local GetPrompt = function() return vim.api.nvim_buf_get_lines(0, 0, -1, false)[1] end
local GetResults = function()
return vim.api.nvim_buf_get_lines(get_results_bufnr(), 0, -1, false)
end
local GetLastResult = function()
local results = GetResults()
return results[#results]
end
helper.make_globals()
]] .. contents
vim.fn.writefile(vim.split(contents, "\n"), tempname)
@@ -162,6 +154,4 @@ tester.run_file = function(filename)
assert.are.same(result_table.expected, result_table.actual)
end
return tester

View File

@@ -0,0 +1,50 @@
local test_helpers = {}
test_helpers.get_picker = function()
local state = require('telescope.state')
return state.get_status(vim.api.nvim_get_current_buf()).picker
end
test_helpers.get_results_bufnr = function()
local state = require('telescope.state')
return state.get_status(vim.api.nvim_get_current_buf()).results_bufnr
end
test_helpers.get_file = function()
return vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":t")
end
test_helpers.get_prompt = function()
return vim.api.nvim_buf_get_lines(0, 0, -1, false)[1]
end
test_helpers.get_results = function()
return vim.api.nvim_buf_get_lines(test_helpers.get_results_bufnr(), 0, -1, false)
end
test_helpers.get_last_result = function()
local results = test_helpers.get_results()
return results[#results]
end
test_helpers.get_selection = function()
local state = require('telescope.state')
return state.get_global_key('selected_entry')
end
test_helpers.get_selection_value = function()
return test_helpers.get_selection().value
end
test_helpers.make_globals = function()
GetFile = test_helpers.get_file -- luacheck: globals GetFile
GetPrompt = test_helpers.get_prompt -- luacheck: globals GetPrompt
GetResults = test_helpers.get_results -- luacheck: globals GetResults
GetLastResult = test_helpers.get_last_result -- luacheck: globals GetLastResult
GetSelection = test_helpers.get_selection -- luacheck: globals GetSelection
GetSelectionValue = test_helpers.get_selection_value -- luacheck: globals GetSelectionValue
end
return test_helpers

View File

@@ -1,12 +1,16 @@
require('plenary.reload').reload_module('telescope')
local tester = require('telescope.pickers._tests')
local tester = require('telescope.pickers._test')
describe('builtin.find_files', function()
it('should find the readme', function()
tester.run_file('find_files__readme')
end)
it('should be able to move selections', function()
tester.run_file('find_files__with_ctrl_n')
end)
it('should not display devicons when disabled', function()
tester.run_string [[
tester.builtin_picker('find_files', 'README.md', {
@@ -105,4 +109,14 @@ describe('builtin.find_files', function()
})
]]
end)
it('should be able to get the current selection', function()
tester.run_string [[
tester.builtin_picker('find_files', 'fixtures/file_abc', {
post_typed = {
{ 'lua/tests/fixtures/file_abc.txt', GetSelectionValue },
}
})
]]
end)
end)

View File

@@ -1,4 +1,4 @@
local tester = require('telescope.pickers._tests')
local tester = require('telescope.pickers._test')
tester.builtin_picker('find_files', 'README.md', {
post_close = {

View File

@@ -1,24 +1,9 @@
pcall(function() RELOAD('telescope') end)
local tester = require('telescope.pickers._test')
local helper = require('telescope.pickers._test_helpers')
local builtin = require('telescope.builtin')
local tester = require('telescope.pickers._tests')
local key = 'find_files'
local input = 'fixtures/file<c-p>'
local expected = 'lua/tests/fixtures/file_2.txt'
local get_actual = function()
return vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":.")
end
-- local on_complete_item = tester.picker_feed(input, expected, get_actual, true)
--
-- builtin[key] {
-- on_complete = { on_complete_item }
-- }
tester.builtin_picker('find_files', 'fixtures/file<c-p>', 'lua/tests/fixtures/file_2.txt', function()
return vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":.")
end, {
debug = false,
tester.builtin_picker('find_files', 'fixtures/file<c-p>', {
post_close = {
{ 'lua/tests/fixtures/file_2.txt', helper.get_file }
},
})