diff --git a/lua/ivy/vim_mock.lua b/lua/ivy/vim_mock.lua index 98f03fd..8f07472 100644 --- a/lua/ivy/vim_mock.lua +++ b/lua/ivy/vim_mock.lua @@ -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 diff --git a/lua/ivy/window_test.lua b/lua/ivy/window_test.lua index 75b9698..c42b380 100644 --- a/lua/ivy/window_test.lua +++ b/lua/ivy/window_test.lua @@ -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) diff --git a/plugin/ivy.lua b/plugin/ivy.lua index dcf5322..671d289 100644 --- a/plugin/ivy.lua +++ b/plugin/ivy.lua @@ -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 diff --git a/scripts/test.lua b/scripts/test.lua index 8b69506..0574c55 100644 --- a/scripts/test.lua +++ b/scripts/test.lua @@ -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"