From 691c1c3baee35c80e84ff76f6ab3ff0b5ec37fcf Mon Sep 17 00:00:00 2001 From: CPea Date: Sun, 24 Aug 2025 01:15:20 +0700 Subject: [PATCH] test: add input tests --- lua/input/init.lua | 2 +- tests/config_spec.lua | 2 +- tests/input_spec.lua | 64 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 tests/input_spec.lua diff --git a/lua/input/init.lua b/lua/input/init.lua index 2e55354..5eb69fa 100644 --- a/lua/input/init.lua +++ b/lua/input/init.lua @@ -54,7 +54,7 @@ local function input(opts, on_confirm) vim.fn.prompt_setinterrupt(bufnr, cancel) vim.api.nvim_win_call(winid, function() - vim.api.nvim_put({ default }, "", true, false) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { default }) vim.cmd.startinsert() end) vim.api.nvim_win_set_cursor(winid, { 1, #default }) diff --git a/tests/config_spec.lua b/tests/config_spec.lua index b8af4c1..cb7c207 100644 --- a/tests/config_spec.lua +++ b/tests/config_spec.lua @@ -3,7 +3,7 @@ local config = require "input.config" describe("Config options", function() it("could be indexed without options field", function() - assert.equal("", config.icon) + assert.equal(" ", config.icon) assert.equal("Input", config.default_prompt) end) end) diff --git a/tests/input_spec.lua b/tests/input_spec.lua new file mode 100644 index 0000000..154d796 --- /dev/null +++ b/tests/input_spec.lua @@ -0,0 +1,64 @@ +describe("Input", function() + local input = require "input" + input.setup() + + local function feedkeys(key) + vim.api.nvim_feedkeys(key, "m", true) + end + + local result = nil + + before_each(function() + result = nil + end) + + it("should accept new content with in insert mode", function() + vim.ui.input({ prompt = "Enter value:" }, function(content) + result = content + end) + feedkeys "new content" + feedkeys(vim.keycode "") + + assert.equal("new content", result) + end) + + it("should accept new content with in normal mode", function() + vim.ui.input({ prompt = "Enter value:" }, function(content) + result = content + end) + feedkeys(vim.keycode "") + feedkeys(vim.keycode "") + + assert.equal("new content", result) + end) + + it("should abort with in insert mode", function() + vim.ui.input({ prompt = "Enter value:" }, function(content) + result = content + end) + feedkeys "" + feedkeys "" + + assert.equal(nil, result) + end) + + it("should abort with q in normal mode", function() + vim.ui.input({ prompt = "Enter value:" }, function(content) + result = content + end) + feedkeys "" + feedkeys "q" + + assert.equal(nil, result) + end) + + it("should abort with Escape in normal mode", function() + vim.ui.input({ prompt = "Enter value:" }, function(content) + result = content + end) + feedkeys "" + feedkeys "" + + assert.equal(nil, result) + end) +end)