From 9ffeff3dae491f12be927cb8ebd866da3db06191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20NICOLAS?= Date: Sun, 15 Dec 2024 14:52:58 +0100 Subject: [PATCH] Implement more elegant highlight timeout tests Use spies rather stubs that would be disabled afterwards to check for calls to nvim_buf_clear_namespace. We now don't have to run the tests where opts.highlight is true twice. --- tests/treewalker/acceptance_spec.lua | 57 +++++++++++++++------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/tests/treewalker/acceptance_spec.lua b/tests/treewalker/acceptance_spec.lua index bf25843..a53aee4 100644 --- a/tests/treewalker/acceptance_spec.lua +++ b/tests/treewalker/acceptance_spec.lua @@ -1,6 +1,6 @@ local util = require "treewalker.util" local load_fixture = require "tests.load_fixture" -local stub = require 'luassert.stub' +local spy = require 'luassert.spy' local assert = require "luassert" local treewalker = require 'treewalker' local ops = require 'treewalker.ops' @@ -18,19 +18,6 @@ local function assert_cursor_at(line, column, msg) assert.are.same({ line, column }, { current_line, current_column }, msg) end --- Count the number of calls made against the stub after a specified duration ----@param api unknown ----@param duration integer ----@return integer -local function calls_after(api, duration) - local tolerance = 5 - local calls = #api.calls - vim.wait(duration - tolerance) - assert.equal(0, #api.calls - calls) - vim.wait(tolerance * 2) - return #api.calls - calls -end - describe("Treewalker", function() describe("regular lua file: ", function() load_fixture("/lua.lua") @@ -92,34 +79,49 @@ describe("Treewalker", function() end) it("respects highlight config option", function() - local highlight_stub = stub(ops, "highlight") + spy.on(ops, "highlight") + local bufclear = spy.on(vim.api, "nvim_buf_clear_namespace") + + -- 'nvim_buf_clear_namespace' should be called times + -- within a 10ms tolerance window after ms + local function assert_bufclears_after(timeout, calls) + bufclear:clear() + vim.wait(timeout - 5) + assert.spy(bufclear).was.not_called() + vim.wait(10) + assert.spy(bufclear).was.called(calls) + end - treewalker.opts = {} + treewalker.setup({}) vim.fn.cursor(23, 5) treewalker.move_out() treewalker.move_down() treewalker.move_up() treewalker.move_in() - assert.equal(0, #highlight_stub.calls) + assert.spy(ops.highlight).was.not_called() - treewalker.opts.highlight = false + treewalker.setup({ highlight = false }) vim.fn.cursor(23, 5) treewalker.move_out() treewalker.move_down() treewalker.move_up() treewalker.move_in() - assert.equal(0, #highlight_stub.calls) + assert.spy(ops.highlight).was.not_called() - treewalker.opts.highlight = true + treewalker.setup({ highlight = false }) vim.fn.cursor(23, 5) treewalker.move_out() treewalker.move_down() treewalker.move_up() treewalker.move_in() - assert.equal(4, #highlight_stub.calls) + assert.spy(ops.highlight).was.not_called() - highlight_stub:revert() - local clear_stub = stub(vim.api, "nvim_buf_clear_namespace") + treewalker.setup({ highlight = true }) + vim.fn.cursor(23, 5) + treewalker.move_out() + treewalker.move_down() + treewalker.move_up() + treewalker.move_in() treewalker.opts.highlight = true vim.fn.cursor(23, 5) @@ -127,7 +129,8 @@ describe("Treewalker", function() treewalker.move_down() treewalker.move_up() treewalker.move_in() - assert.equal(4, calls_after(clear_stub, 250)) + assert.spy(ops.highlight).was.called(4) + assert_bufclears_after(250, 4) treewalker.opts.highlight_duration = 50 vim.fn.cursor(23, 5) @@ -135,7 +138,8 @@ describe("Treewalker", function() treewalker.move_down() treewalker.move_up() treewalker.move_in() - assert.equal(4, calls_after(clear_stub, 50)) + assert.spy(ops.highlight).was.called(8) + assert_bufclears_after(50, 4) treewalker.opts.highlight_duration = 500 vim.fn.cursor(23, 5) @@ -143,7 +147,8 @@ describe("Treewalker", function() treewalker.move_down() treewalker.move_up() treewalker.move_in() - assert.equal(4, calls_after(clear_stub, 500)) + assert.spy(ops.highlight).was.called(12) + assert_bufclears_after(500, 4) end) end)