Skip to content

Commit 72c7c9b

Browse files
fidgetingbitsCedric Halbronn
authored andcommitted
initial lua test skeleton
1 parent d44187d commit 72c7c9b

File tree

6 files changed

+160
-10
lines changed

6 files changed

+160
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ packages/test-harness/testSubsetGrep.properties
4747
# cursorless-neovim
4848
cursorless.nvim/node/cursorless-neovim
4949
cursorless.nvim/node/test-harness
50+
cursorless.nvim/test/xdg/
5051

5152
# nix
5253
.direnv/

cursorless.nvim/.busted

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
return {
2+
_all = {
3+
lua = './test/nvim-shim.sh'
4+
},
5+
unit = {
6+
ROOT = {'./test/unit/'},
7+
},
8+
}

cursorless.nvim/lua/cursorless/cursorless.lua

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,9 @@ 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)
6257
vim.cmd([[normal! :noh]])

cursorless.nvim/test/nvim-shim.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
export XDG_CONFIG_HOME='test/xdg/config/'
5+
export XDG_STATE_HOME='test/xdg/local/state/'
6+
export XDG_DATA_HOME='test/xdg/local/share/'
7+
8+
# FIXME: For the flake.nix devshell I plan to eventually just install the unwrapped binary straight up so I can
9+
# reference it for these tests, but until then...
10+
function resolve_nvim_path() {
11+
nvim=$(type -p "nvim" | awk '{print $NF}')
12+
if file "${nvim}" | grep -q 'executable'; then
13+
echo "${nvim}"
14+
return
15+
fi
16+
17+
# On NixOS, this might still be a wrapper, so we need to make sure we find the real binary
18+
current_link="${nvim}"
19+
while [ -L "${current_link}" ]; do
20+
current_link=$(readlink "${current_link}")
21+
done
22+
nvim="${current_link}"
23+
24+
if file "${current_link}" | grep -q 'script'; then
25+
nvim=$(grep 'exec -a' "${current_link}" | cut -f4 -d" " | tr -d '"')
26+
fi
27+
echo "${nvim}"
28+
}
29+
30+
mkdir -p ${XDG_CONFIG_HOME} ${XDG_STATE_HOME} ${XDG_DATA_HOME}
31+
mkdir -p ${XDG_DATA_HOME}/nvim/site/pack/testing/start || true
32+
link_path="${XDG_DATA_HOME}/nvim/site/pack/testing/start/cursorless.nvim"
33+
if [ ! -e "${link_path}" ]; then
34+
# FIXME: Expects to be in cursorless.nvim, so we should force cd here?
35+
ln -sf "${PWD}" ${link_path}
36+
fi
37+
38+
NVIM=$(resolve_nvim_path)
39+
40+
# shellcheck disable=SC2068
41+
${NVIM} --cmd 'set loadplugins' -l $@
42+
exit_code=$?
43+
rm ${link_path} 2>/dev/null || true
44+
45+
exit $exit_code
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
describe("cursorless tests", function()
2+
local cursorless = require("cursorless.cursorless")
3+
4+
local get_selected_text = function()
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+
local function 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
17+
18+
describe("window_get_visible_lines()", function()
19+
before_each(function() end)
20+
21+
it("Can read one visible line", function()
22+
local pos = vim.api.nvim_win_get_cursor(0)[2]
23+
local line = vim.api.nvim_get_current_line()
24+
local nline = line:sub(0, pos) .. "hello" .. line:sub(pos + 1)
25+
vim.api.nvim_set_current_line(nline)
26+
27+
local visible = cursorless.window_get_visible_lines()
28+
assert(table.concat(visible) == table.concat({ 1, 1 }))
29+
end)
30+
31+
it("Can read all lines visible on the window", function()
32+
local maxlines = vim.api.nvim_win_get_height(0)
33+
local lines = {}
34+
for _ = 1, (maxlines + 1) do
35+
table.insert(lines, "hello ")
36+
end
37+
vim.api.nvim_buf_set_lines(0, 0, -1, false, lines)
38+
local visible = cursorless.window_get_visible_lines()
39+
assert(table.concat(visible) == table.concat({ 1, maxlines }))
40+
end)
41+
end)
42+
describe("select_range()", function()
43+
it("Selects the specified range", function()
44+
local lines = "hello world"
45+
vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(lines, "\n"))
46+
cursorless.select_range(1, 2, 1, 4)
47+
48+
assert(table.concat(get_selected_text()) == "llo")
49+
end)
50+
end)
51+
describe("buffer_get_selection()", function()
52+
it(
53+
"Can get the forward selection in a format expected by cursorless",
54+
function()
55+
local lines = "hello world"
56+
vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(lines, "\n"))
57+
cursorless.select_range(1, 2, 1, 4)
58+
assert(
59+
table.concat(
60+
convert_table_entries(cursorless.buffer_get_selection(), tostring),
61+
", "
62+
)
63+
== table.concat(
64+
convert_table_entries({ 1, 3, 1, 5, false }, tostring),
65+
", "
66+
)
67+
)
68+
end
69+
)
70+
it(
71+
"Can get the backward selection in a format expected by cursorless",
72+
function()
73+
local lines = "hello world"
74+
vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.split(lines, "\n"))
75+
cursorless.select_range(1, 4, 1, 2)
76+
print(
77+
table.concat(
78+
convert_table_entries(cursorless.buffer_get_selection(), tostring),
79+
", "
80+
)
81+
)
82+
assert(
83+
table.concat(
84+
convert_table_entries(cursorless.buffer_get_selection(), tostring),
85+
", "
86+
)
87+
== table.concat(
88+
convert_table_entries({ 1, 3, 1, 5, true }, tostring),
89+
", "
90+
)
91+
)
92+
end
93+
)
94+
end)
95+
end)

docs/contributing/cursorless-in-neovim.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Follow the steps in [CONTRIBUTING.md](./CONTRIBUTING.md#initial-setup).
1212

1313
Follow the installation steps in [cursorless.nvim](https://github.com/hands-free-vim/cursorless.nvim/tree/main#prerequisites).
1414

15-
Confirm that production cursorless.nvim is working in neovim, eg say `"take first paint"` in a nonempty document.
15+
Confirm that production cursorless.nvim is working in neovim, eg say `"take first paint"` in a non-empty document.
1616

1717
### 3. Add nvim executable path to your PATH
1818

@@ -40,7 +40,13 @@ debug mode. To do so you need to run the `workbench.action.debug.selectandstart`
4040

4141
The debug logs are written in `C:\path\to\cursorless\packages\cursorless-neovim\out\nvim_node.log`.
4242

43-
NOTE: This will spawn a standalone nvim instance that is independent of VSCode. Consequently after you're done debugging, you need to close nvim.
43+
NOTE: This will spawn a standalone nvim instance that is independent of VSCode. Consequently after you're done
44+
debugging, you need to close nvim.
45+
46+
### Running lua tests
47+
48+
Their are separate cursorless and lua tests. You can run the lua tests by entering the `cursorless.nvim` folder and
49+
running: `busted --run unit`. These tests currently only work on Linux.
4450

4551
## Sending pull requests
4652

0 commit comments

Comments
 (0)