forked from nvim-treesitter/nvim-treesitter-textobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.lua
More file actions
173 lines (158 loc) · 6.78 KB
/
common.lua
File metadata and controls
173 lines (158 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
local M = {}
local assert = require('luassert')
local Path = require('plenary.path')
-- Test in all possible col position
-- f, F, t, T
-- ; , repeat
-- count repeat
function M.run_builtin_find_test(file, spec)
assert.are.same(1, vim.fn.filereadable(file), string.format('File "%s" not readable', file))
-- load reference file
vim.cmd(string.format('edit %s', file))
vim.api.nvim_win_set_cursor(0, { spec.row, 0 })
local line = vim.api.nvim_get_current_line()
local num_cols = #line
for col = 0, num_cols - 1 do
for _, cmd in pairs({ 'f', 'F', 't', 'T' }) do
for _, repeat_cmd in pairs({ ';', ',' }) do
-- Get ground truth using vim's built-in search and repeat (normal mode)
vim.api.nvim_win_set_cursor(0, { spec.row, col })
local gt_cols = {}
vim.cmd([[normal! ]] .. cmd .. spec.char)
gt_cols[#gt_cols + 1] = vim.fn.col('.')
vim.cmd([[normal! ]] .. repeat_cmd)
gt_cols[#gt_cols + 1] = vim.fn.col('.')
vim.cmd([[normal! 2]] .. repeat_cmd)
gt_cols[#gt_cols + 1] = vim.fn.col('.')
vim.cmd([[normal! l]] .. repeat_cmd)
gt_cols[#gt_cols + 1] = vim.fn.col('.')
vim.cmd([[normal! h]] .. repeat_cmd)
gt_cols[#gt_cols + 1] = vim.fn.col('.')
vim.cmd([[normal! 2]] .. cmd .. spec.char)
gt_cols[#gt_cols + 1] = vim.fn.col('.')
-- test using tstextobj repeatable_move.lua (normal mode)
vim.api.nvim_win_set_cursor(0, { spec.row, col })
local ts_cols = {}
vim.cmd([[normal ]] .. cmd .. spec.char)
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
ts_cols[#ts_cols + 1] = vim.fn.col('.')
vim.cmd([[normal ]] .. repeat_cmd)
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
ts_cols[#ts_cols + 1] = vim.fn.col('.')
vim.cmd([[normal 2]] .. repeat_cmd)
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
ts_cols[#ts_cols + 1] = vim.fn.col('.')
vim.cmd([[normal l]] .. repeat_cmd)
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
ts_cols[#ts_cols + 1] = vim.fn.col('.')
vim.cmd([[normal h]] .. repeat_cmd)
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
ts_cols[#ts_cols + 1] = vim.fn.col('.')
vim.cmd([[normal 2]] .. cmd .. spec.char)
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
ts_cols[#ts_cols + 1] = vim.fn.col('.')
assert.are.same(
gt_cols,
ts_cols,
string.format(
"Command %s with repeat %s works differently than vim's built-in find, col: %d",
cmd,
repeat_cmd,
col
)
)
-- Get ground truth using vim's built-in search and repeat (operator-pending mode)
vim.api.nvim_win_set_cursor(0, { spec.row, col })
local gt_regs = {}
vim.fn.setreg('0', '')
vim.cmd([[normal! y]] .. cmd .. spec.char)
gt_regs[#gt_regs + 1] = vim.fn.getreg('0')
vim.fn.setreg('0', '')
vim.cmd([[normal! y]] .. repeat_cmd)
gt_regs[#gt_regs + 1] = vim.fn.getreg('0')
vim.fn.setreg('0', '')
vim.cmd([[normal! y2]] .. repeat_cmd)
gt_regs[#gt_regs + 1] = vim.fn.getreg('0')
vim.fn.setreg('0', '')
vim.cmd([[normal! l]] .. repeat_cmd)
vim.fn.setreg('0', '')
vim.cmd([[normal! y2]] .. repeat_cmd)
gt_regs[#gt_regs + 1] = vim.fn.getreg('0')
vim.fn.setreg('0', '')
vim.cmd([[normal! h]] .. repeat_cmd)
vim.fn.setreg('0', '')
vim.cmd([[normal! y2]] .. repeat_cmd)
gt_regs[#gt_regs + 1] = vim.fn.getreg('0')
vim.fn.setreg('0', '')
vim.cmd([[normal! 2y]] .. cmd .. spec.char)
gt_regs[#gt_regs + 1] = vim.fn.getreg('0')
-- test using tstextobj repeatable_move.lua (operator-pending mode)
vim.api.nvim_win_set_cursor(0, { spec.row, col })
local ts_regs = {}
vim.fn.setreg('0', '')
vim.cmd([[normal y]] .. cmd .. spec.char)
ts_regs[#ts_regs + 1] = vim.fn.getreg('0')
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
vim.fn.setreg('0', '')
vim.cmd([[normal y]] .. repeat_cmd)
ts_regs[#ts_regs + 1] = vim.fn.getreg('0')
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
vim.fn.setreg('0', '')
vim.cmd([[normal y2]] .. repeat_cmd)
ts_regs[#ts_regs + 1] = vim.fn.getreg('0')
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
vim.fn.setreg('0', '')
vim.cmd([[normal l]] .. repeat_cmd)
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
vim.fn.setreg('0', '')
vim.cmd([[normal y2]] .. repeat_cmd)
ts_regs[#ts_regs + 1] = vim.fn.getreg('0')
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
vim.fn.setreg('0', '')
vim.cmd([[normal h]] .. repeat_cmd)
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
vim.fn.setreg('0', '')
vim.cmd([[normal y2]] .. repeat_cmd)
ts_regs[#ts_regs + 1] = vim.fn.getreg('0')
assert.are.same(spec.row, vim.fn.line('.'), "Command shouldn't move cursor over rows")
vim.fn.setreg('0', '')
vim.cmd([[normal 2y]] .. cmd .. spec.char)
ts_regs[#ts_regs + 1] = vim.fn.getreg('0')
assert.are.same(
gt_regs,
ts_regs,
string.format(
"Command %s with repeat %s works differently than vim's built-in find, col: %d",
cmd,
repeat_cmd,
col
)
)
end
end
end
-- clear any changes to avoid 'No write since last change (add ! to override)'
vim.cmd('edit!')
end
local Runner = {}
Runner.__index = Runner
-- Helper to avoid boilerplate when defining tests
-- @param it the "it" function that busted defines globally in spec files
-- @param base_dir all other paths will be resolved relative to this directory
-- @param buf_opts buffer options passed to set_buf_indent_opts
function Runner:new(it, base_dir, buf_opts)
local runner = {}
runner.it = it
runner.base_dir = Path:new(base_dir)
runner.buf_opts = buf_opts
return setmetatable(runner, self)
end
function Runner:builtin_find(file, spec, title)
title = title and title or tostring(spec.row)
self.it(string.format('%s[%s]', file, title), function()
local path = self.base_dir / file
M.run_builtin_find_test(path.filename, spec)
end)
end
M.Runner = Runner
return M