Skip to content

Commit 4c0ea88

Browse files
authored
Merge pull request #7 from fidgetingbits/lua-tests
2 parents 027f188 + 1be17b8 commit 4c0ea88

File tree

15 files changed

+238
-18
lines changed

15 files changed

+238
-18
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: "Neovim Lua Tests"
2+
description: "Set up Neovim Lua environment and run Busted tests"
3+
runs:
4+
using: "composite"
5+
steps:
6+
- uses: leafo/gh-actions-lua@v9
7+
with:
8+
luaVersion: "luajit-2.1.0-beta3"
9+
- uses: leafo/gh-actions-luarocks@v4
10+
- shell: bash
11+
run: |
12+
luarocks install busted
13+
luarocks install luafilesystem
14+
- shell: bash
15+
run: |
16+
cd cursorless.nvim
17+
busted --run unit

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ jobs:
5656
if: runner.os == 'Linux'
5757
env:
5858
NEOVIM_PATH: ${{ steps.vim.outputs.executable }}
59+
- uses: ./.github/actions/test-neovim-lua/
60+
if: runner.os == 'Linux' && matrix.app_version == 'stable'
5961
- name: Create vscode dist that can be installed locally
6062
run: pnpm -F @cursorless/cursorless-vscode populate-dist --local-install
6163
if: runner.os == 'Linux' && matrix.app_version == 'stable'

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
"Lua.diagnostics.globals": ["vim", "talon"],
3131
"[lua]": {
3232
"editor.defaultFormatter": "JohnnyMorganz.stylua"
33+
},
34+
"files.associations": {
35+
".busted": "lua"
3336
}
3437
}

.vscode/tasks.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,14 @@
276276
// NOTE: We don't have a way on Windows atm due to command with argument inside Run() not working
277277
// so we need to show logs outside of vscode (see #2454)
278278
},
279+
{
280+
"label": "Neovim: Launch neovim (lua test)",
281+
"type": "shell",
282+
"command": "busted --run unit",
283+
"options": {
284+
"cwd": "cursorless.nvim"
285+
}
286+
},
279287

280288
// cursorless.org
281289
{

cursorless.nvim/.busted

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
return {
2+
_all = {
3+
lua = './test/nvim-shim.sh',
4+
output = "TAP",
5+
["defer-print"] = false,
6+
},
7+
unit = {
8+
ROOT = {'./test/unit/'},
9+
},
10+
}

cursorless.nvim/lua/cursorless/cursorless.lua

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,14 @@ function M.buffer_get_selection()
4949
end
5050

5151
-- https://github.com/nvim-treesitter/nvim-treesitter/blob/master/lua/nvim-treesitter/ts_utils.lua#L278
52-
-- luacheck:ignore 631
53-
-- https://github.com/nvim-treesitter/nvim-treesitter-textobjects/blob/master/lua/nvim-treesitter/textobjects/select.lua#L114
54-
-- as an example if you put that in a vim buffer and do the following you can do a selection:
55-
-- :w c:\work\tmp\test.lua
56-
-- :so %
57-
-- :lua select_range(5, 12, 5, 30)
58-
-- for example it will highlight the last function name (nvim_win_set_cursor).
59-
-- another example is :tmap <c-b> <Cmd>lua require("talon.cursorless").select_range(4, 0, 4, 38)<Cr>
52+
-- If you have a buffer with the line: "hello world"
53+
-- :lua require("cursorless.cursorless").select_range(1, 2, 1, 4)
54+
-- will highlight "llo"
6055
-- NOTE: works for any mode (n,i,v,nt) except in t mode
6156
function M.select_range(start_line, start_col, end_line, end_col)
62-
vim.cmd([[normal! :noh]])
57+
vim.cmd([[silent! normal! :noh]])
6358
vim.api.nvim_win_set_cursor(0, { start_line, start_col })
64-
vim.cmd([[normal v]])
59+
vim.cmd([[silent! normal v]])
6560
vim.api.nvim_win_set_cursor(0, { end_line, end_col })
6661
end
6762

cursorless.nvim/lua/cursorless/utils.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ end
5858
-- https://www.baeldung.com/linux/vim-paste-text
5959
-- e.g. run in command mode :imap <c-b> <Cmd>lua require('cursorless.utils').paste()<Cr>
6060
function M.paste()
61-
vim.cmd([[normal! "+p]])
61+
vim.cmd([[silent! normal! "+p]])
6262
end
6363

6464
return M

cursorless.nvim/test/helpers.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- This file gets linked into plugin/helpers.lua of busted nvim config
2+
-- Functions that are exposed to all tests
3+
4+
function _G.get_selected_text()
5+
local _, ls, cs = unpack(vim.fn.getpos("v"))
6+
local _, le, ce = unpack(vim.fn.getpos("."))
7+
return vim.api.nvim_buf_get_text(0, ls - 1, cs - 1, le - 1, ce, {})
8+
end
9+
10+
function _G.convert_table_entries(tbl, func)
11+
local mapped = {}
12+
for k, v in pairs(tbl) do
13+
mapped[k] = func(v)
14+
end
15+
return mapped
16+
end

cursorless.nvim/test/nvim-shim.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
if ! [[ "${PWD}" == *"cursorless.nvim" ]]; then
5+
echo "ERROR: This script must be run from inside cursorless.nvim/ directory"
6+
exit 1
7+
fi
8+
9+
test_folder=$(mktemp -d "${TMPDIR-/tmp}"/cursorless-busted-test-XXXXX)
10+
export XDG_CONFIG_HOME="${test_folder}/xdg/config/"
11+
export XDG_STATE_HOME="${test_folder}/xdg/local/state/"
12+
export XDG_DATA_HOME="${test_folder}/xdg/local/share/"
13+
dependency_folder="${XDG_DATA_HOME}/nvim/site/pack/testing/start/"
14+
plugin_folder="${XDG_CONFIG_HOME}/nvim/plugin/"
15+
16+
mkdir -p "${plugin_folder}" "${XDG_STATE_HOME}" "${dependency_folder}"
17+
ln -sf "${PWD}" "${dependency_folder}/cursorless.nvim"
18+
19+
# Link in standalone helper functions we want all tests to be able to call
20+
ln -sf "${PWD}/test/helpers.lua" "${plugin_folder}/helpers.lua"
21+
22+
# shellcheck disable=SC2068
23+
command nvim --cmd 'set loadplugins' -l $@
24+
exit_code=$?
25+
26+
rm -rf "${test_folder}"
27+
28+
exit $exit_code
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
describe("", function()
2+
local cursorless = require("cursorless.cursorless")
3+
4+
describe("window_get_visible_lines() ->", function()
5+
it("can read one visible line", function()
6+
local pos = vim.api.nvim_win_get_cursor(0)[2]
7+
local line = vim.api.nvim_get_current_line()
8+
local nline = line:sub(0, pos) .. "hello" .. line:sub(pos + 1)
9+
vim.api.nvim_set_current_line(nline)
10+
11+
local visible = cursorless.window_get_visible_lines()
12+
assert(table.concat(visible) == table.concat({ 1, 1 }))
13+
end)
14+
15+
it("can read all lines visible on the window", function()
16+
local maxlines = vim.api.nvim_win_get_height(0)
17+
local lines = {}
18+
for _ = 1, (maxlines + 1) do
19+
table.insert(lines, "hello ")
20+
end
21+
vim.api.nvim_buf_set_lines(0, 0, -1, false, lines)
22+
local visible = cursorless.window_get_visible_lines()
23+
assert(table.concat(visible) == table.concat({ 1, maxlines }))
24+
end)
25+
end)
26+
describe("select_range() ->", function()
27+
it("selects the specified range", function()
28+
local lines = "hello world"
29+
vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(lines, "\n"))
30+
cursorless.select_range(1, 2, 1, 4)
31+
32+
assert(table.concat(_G.get_selected_text()) == "llo")
33+
end)
34+
end)
35+
describe("buffer_get_selection() ->", function()
36+
it(
37+
"can get the forward selection in a format expected by cursorless",
38+
function()
39+
local lines = "hello world"
40+
vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(lines, "\n"))
41+
cursorless.select_range(1, 2, 1, 4)
42+
assert(
43+
table.concat(
44+
_G.convert_table_entries(
45+
cursorless.buffer_get_selection(),
46+
tostring
47+
),
48+
", "
49+
)
50+
== table.concat(
51+
_G.convert_table_entries({ 1, 3, 1, 5, false }, tostring),
52+
", "
53+
)
54+
)
55+
end
56+
)
57+
it(
58+
"can get the backward selection in a format expected by cursorless",
59+
function()
60+
local lines = "hello world"
61+
vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(lines, "\n"))
62+
cursorless.select_range(1, 4, 1, 2)
63+
assert(
64+
table.concat(
65+
_G.convert_table_entries(
66+
cursorless.buffer_get_selection(),
67+
tostring
68+
),
69+
", "
70+
)
71+
== table.concat(
72+
_G.convert_table_entries({ 1, 3, 1, 5, true }, tostring),
73+
", "
74+
)
75+
)
76+
end
77+
)
78+
end)
79+
end)

0 commit comments

Comments
 (0)