fix: crash in IvyLines

This commit is contained in:
Ade Attwood 2022-09-18 18:15:14 +01:00
parent 8d02c3a439
commit e9cdd40c74
4 changed files with 62 additions and 6 deletions

View file

@ -1,9 +1,13 @@
local mock = {
commands = {},
lines = {},
cursors = {},
}
mock.reset = function()
mock.commands = {}
mock.lines = {}
mock.cursors = {}
_G.vim = {
notify = function() end,
@ -23,6 +27,44 @@ mock.reset = function()
nvim_buf_set_option = function() end,
nvim_buf_set_var = function() end,
nvim_buf_set_keymap = function() end,
nvim_buf_delete = function() end,
nvim_buf_set_lines = function(buffer_number, state_index, end_index, _, items)
local new_lines = {}
for index = 1, state_index do
if mock.lines[buffer_number][index] == nil then
table.insert(new_lines, "")
else
table.insert(new_lines, mock.lines[buffer_number][index])
end
end
for index = 1, #items do
table.insert(new_lines, items[index])
end
if end_index ~= -1 then
error("Mock of nvim_buf_set_lines dose not support a end_index grater than -1 found " .. end_index)
end
mock.lines[buffer_number] = new_lines
end,
nvim_win_set_height = function() end,
nvim_win_set_cursor = function(window_number, position)
mock.cursors[window_number] = position
end,
nvim_buf_get_lines = function(buffer_number, start_index, end_index)
local lines = {}
for index = start_index, end_index do
table.insert(lines, mock.lines[buffer_number][index + 1])
end
if #lines == 0 then
return nil
end
return lines
end,
},
}
end

View file

@ -5,10 +5,19 @@ before_each(function()
vim_mock.reset()
end)
it("can initialize", function(t)
it("can initialize and destroy the window", function(t)
window.initialize()
if window.get_buffer() ~= 10 then
t.error("The windows buffer should be 10 found " .. window.get_buffer())
end
t.assert_equal(10, window.get_buffer())
t.assert_equal(10, window.buffer)
window.destroy()
t.assert_equal(nil, window.buffer)
end)
it("can set items", function(t)
window.initialize()
window.set_items { { content = "Line one" } }
t.assert_equal("Line one", window.get_current_selection())
end)

View file

@ -54,12 +54,12 @@ vim.api.nvim_create_user_command("IvyLines", function()
local score = libivy.ivy_match(input, line)
if score > -200 then
local prefix = string.rep(" ", 4 - #tostring(index)) .. index .. ": "
table.insert(list, { score, prefix .. line })
table.insert(list, { score = score, content = prefix .. line })
end
end
table.sort(list, function(a, b)
return a[1] < b[1]
return a.score < b.score
end)
return list

View file

@ -42,6 +42,11 @@ _G.it = function(name, callback)
error = function(message)
error(message, 2)
end,
assert_equal = function(expected, actual)
if expected ~= actual then
error("Failed to assert that '" .. expected .. "' matches '" .. actual .. "'", 2)
end
end,
}
call_hook "before_each"