Skip to content

Commit 259f83b

Browse files
committed
test(treesitter): add test for treesitter wrap cursor commands
1 parent c7fc80a commit 259f83b

File tree

2 files changed

+92
-24
lines changed

2 files changed

+92
-24
lines changed

scripts/minimal_init.lua

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
-- Add current directory to 'runtimepath' to be able to use 'lua' files
22
vim.cmd([[let &rtp.=','.getcwd()]])
33

4-
-- Set up 'mini.test' only when calling headless Neovim (like with `make test`)
5-
if #vim.api.nvim_list_uis() == 0 then
6-
-- Add 'mini.nvim' to 'runtimepath' to be able to use 'mini.test'
7-
-- Assumed that 'mini.nvim' is stored in 'deps/mini.nvim'
8-
--
9-
local runtime_dependencies = {
10-
"deps/mini.nvim",
11-
"deps/nvim-treesitter",
12-
"deps/neotest",
13-
"deps/neotest-python",
14-
"deps/nvim-dap",
15-
"deps/nvim-dap-python",
16-
"deps/nvim-lspconfig",
17-
"deps/LuaSnip",
18-
}
19-
local runtime_path = vim.fn.join(runtime_dependencies, ",")
20-
vim.cmd("set rtp+=" .. runtime_path)
4+
local runtime_dependencies = {
5+
"deps/mini.nvim",
6+
"deps/nvim-treesitter",
7+
"deps/neotest",
8+
"deps/neotest-python",
9+
"deps/nvim-dap",
10+
"deps/nvim-dap-python",
11+
"deps/nvim-lspconfig",
12+
"deps/LuaSnip",
13+
}
14+
local runtime_path = vim.fn.join(runtime_dependencies, ",")
15+
vim.cmd("set rtp+=" .. runtime_path)
2116

22-
-- Set up 'mini.test'
23-
require("luasnip.extras.fmt")
24-
require("luasnip.nodes.absolute_indexer")
25-
require("nvim-treesitter.locals")
26-
require("mini.test").setup()
27-
require("mini.doc").setup()
28-
end
17+
-- Set up 'mini.test'
18+
require("luasnip.extras.fmt")
19+
require("luasnip.nodes.absolute_indexer")
20+
require("nvim-treesitter.locals")
21+
require("nvim-treesitter").setup()
22+
require("mini.test").setup()
23+
require("mini.doc").setup()
24+
require("nvim-treesitter.configs").setup({
25+
modules = {
26+
"highlight",
27+
},
28+
sync_install = false,
29+
auto_install = true,
30+
ignore_install = {},
31+
ensure_installed = {
32+
"python"
33+
},
34+
highlight = {
35+
enable = true
36+
}
37+
})

tests/test_treesitter.lua

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
-- Define helper aliases
2+
local new_set = MiniTest.new_set
3+
local expect, eq = MiniTest.expect, MiniTest.expect.equality
4+
5+
-- Create (but not start) child Neovim object
6+
local child = MiniTest.new_child_neovim()
7+
8+
-- Define main test set of this file
9+
local T = new_set({
10+
-- Register hooks
11+
hooks = {
12+
-- This will be executed before every (even nested) case
13+
pre_case = function()
14+
-- Restart child process with custom 'init.lua' script
15+
child.restart({ "-u", "scripts/minimal_init.lua" })
16+
-- Load tested plugin
17+
child.lua([[require('python').setup()]])
18+
end,
19+
-- This will be executed one after all tests from this set are finished
20+
post_once = child.stop,
21+
},
22+
})
23+
24+
25+
local get_lines = function() return child.api.nvim_buf_get_lines(0, 0, -1, true) end
26+
27+
T['wrap_cursor'] = MiniTest.new_set({
28+
hooks = {
29+
pre_case = function()
30+
child.cmd("e _not_existing_new_buffer.py")
31+
child.type_keys("cc", [["TEST"]], "<Esc>", "0")
32+
end,
33+
}
34+
})
35+
36+
T['wrap_cursor']['normal'] = function()
37+
child.cmd("Python treesitter wrap_cursor test(%s)")
38+
eq(get_lines(), {[[test("TEST")]]})
39+
end
40+
41+
T['wrap_cursor']['visual'] = function()
42+
child.type_keys("l", "v", "$")
43+
child.type_keys([[:Python treesitter wrap_cursor test(%s)<cr>]])
44+
eq(get_lines(), {[["test(TEST")]]})
45+
end
46+
47+
T['wrap_cursor']['visual_with_selection'] = function()
48+
child.type_keys("l", "v", "$")
49+
child.type_keys([[:Python treesitter wrap_cursor<cr>1<cr>]])
50+
eq(get_lines(), {[["print(TEST")]]})
51+
end
52+
53+
T['wrap_cursor']['with_selection'] = function()
54+
child.type_keys([[:Python treesitter wrap_cursor<cr>1<cr>]])
55+
eq(get_lines(), {[[print("TEST")]]})
56+
end
57+
58+
-- Return test set which will be collected and execute inside `MiniTest.run()`
59+
return T

0 commit comments

Comments
 (0)