robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
8.0 KiB287 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local clear = n.clear
6local eq = t.eq
7local eval = n.eval
8local exec = n.exec
9local exec_lua = n.exec_lua
10local expect = n.expect
11local feed = n.feed
12local fn = n.fn
13local api = n.api
14local source = n.source
15local command = n.command
16local exec_capture = n.exec_capture
17local pcall_err = t.pcall_err
18
19describe('maparg()', function()
20  before_each(clear)
21
22  local foo_bar_map_table = {
23    lhs = 'foo',
24    lhsraw = 'foo',
25    script = 0,
26    silent = 0,
27    rhs = 'bar',
28    expr = 0,
29    replace_keycodes = 0,
30    sid = 0,
31    scriptversion = 1,
32    buffer = 0,
33    nowait = 0,
34    mode = 'n',
35    mode_bits = 0x01,
36    abbr = 0,
37    noremap = 1,
38    lnum = 0,
39  }
40
41  it('returns a dict', function()
42    command('nnoremap foo bar')
43    eq('bar', fn.maparg('foo'))
44    eq(foo_bar_map_table, fn.maparg('foo', 'n', false, true))
45  end)
46
47  it('returns 1 for silent when <silent> is used', function()
48    command('nnoremap <silent> foo bar')
49    eq(1, fn.maparg('foo', 'n', false, true)['silent'])
50
51    command('nnoremap baz bat')
52    eq(0, fn.maparg('baz', 'n', false, true)['silent'])
53  end)
54
55  it('returns an empty string when no map is present', function()
56    eq('', fn.maparg('not a mapping'))
57  end)
58
59  it('returns an empty dict when no map is present and dict is requested', function()
60    eq({}, fn.maparg('not a mapping', 'n', false, true))
61  end)
62
63  it('returns the same value for noremap and <script>', function()
64    command('inoremap <script> hello world')
65    command('inoremap this that')
66    eq(
67      fn.maparg('hello', 'i', false, true)['noremap'],
68      fn.maparg('this', 'i', false, true)['noremap']
69    )
70  end)
71
72  it('returns a boolean for buffer', function()
73    -- Open enough windows to know we aren't on buffer number 1
74    command('new')
75    command('new')
76    command('new')
77    command('cnoremap <buffer> this that')
78    eq(1, fn.maparg('this', 'c', false, true)['buffer'])
79
80    -- Global will return 0 always
81    command('nnoremap other another')
82    eq(0, fn.maparg('other', 'n', false, true)['buffer'])
83  end)
84
85  it('returns script numbers', function()
86    source([[
87      function! s:maparg_test_function() abort
88        return 'testing'
89      endfunction
90
91      nnoremap fizz :call <SID>maparg_test_function()<CR>
92    ]])
93    eq(1, fn.maparg('fizz', 'n', false, true)['sid'])
94    eq('testing', api.nvim_call_function('<SNR>1_maparg_test_function', {}))
95  end)
96
97  it('works with <F12> and others', function()
98    source([[
99      let g:maparg_test_var = 0
100
101      nnoremap <F12> :let g:maparg_test_var = 1<CR>
102    ]])
103    eq(0, eval('g:maparg_test_var'))
104    source([[
105      call feedkeys("\<F12>")
106    ]])
107    eq(1, eval('g:maparg_test_var'))
108
109    eq(':let g:maparg_test_var = 1<CR>', fn.maparg('<F12>', 'n', false, true)['rhs'])
110  end)
111
112  it('works with <expr>', function()
113    source([[
114      let counter = 0
115      inoremap <expr> <C-L> ListItem()
116      inoremap <expr> <C-R> ListReset()
117
118      func ListItem()
119        let g:counter += 1
120        return g:counter . '. '
121      endfunc
122
123      func ListReset()
124        let g:counter = 0
125        return ''
126      endfunc
127
128      call feedkeys("i\<C-L>")
129    ]])
130    eq(1, eval('g:counter'))
131
132    local map_dict = fn.maparg('<C-L>', 'i', false, true)
133    eq(1, map_dict['expr'])
134    eq('i', map_dict['mode'])
135  end)
136
137  it('works with combining characters', function()
138    -- Using addacutes to make combining character better visible
139    local function ac(s)
140      local acute = '\204\129' -- U+0301 COMBINING ACUTE ACCENT
141      local ret = s:gsub('`', acute)
142      return ret
143    end
144    command(ac([[
145      nnoremap a  b`
146      nnoremap c` d
147      nnoremap e` f`
148    ]]))
149    eq(ac('b`'), fn.maparg(ac('a')))
150    eq(ac(''), fn.maparg(ac('c')))
151    eq(ac('d'), fn.maparg(ac('c`')))
152    eq(ac('f`'), fn.maparg(ac('e`')))
153
154    local function acmap(lhs, rhs)
155      return {
156        lhs = ac(lhs),
157        lhsraw = ac(lhs),
158        rhs = ac(rhs),
159
160        buffer = 0,
161        expr = 0,
162        replace_keycodes = 0,
163        mode = 'n',
164        mode_bits = 0x01,
165        abbr = 0,
166        noremap = 1,
167        nowait = 0,
168        script = 0,
169        sid = 0,
170        scriptversion = 1,
171        silent = 0,
172        lnum = 0,
173      }
174    end
175
176    eq({}, fn.maparg(ac('c'), 'n', 0, 1))
177    eq(acmap('a', 'b`'), fn.maparg(ac('a'), 'n', 0, 1))
178    eq(acmap('c`', 'd'), fn.maparg(ac('c`'), 'n', 0, 1))
179    eq(acmap('e`', 'f`'), fn.maparg(ac('e`'), 'n', 0, 1))
180  end)
181end)
182
183describe('mapset()', function()
184  before_each(clear)
185
186  it('can restore mapping with backslash in lhs', function()
187    api.nvim_set_keymap('n', '\\ab', 'a', {})
188    eq('\nn  \\ab           a', exec_capture('nmap \\ab'))
189    local mapargs = fn.maparg('\\ab', 'n', false, true)
190    api.nvim_set_keymap('n', '\\ab', 'b', {})
191    eq('\nn  \\ab           b', exec_capture('nmap \\ab'))
192    fn.mapset('n', false, mapargs)
193    eq('\nn  \\ab           a', exec_capture('nmap \\ab'))
194  end)
195
196  it('can restore mapping description from the dict returned by maparg()', function()
197    api.nvim_set_keymap('n', 'lhs', 'rhs', { desc = 'map description' })
198    eq('\nn  lhs           rhs\n                 map description', exec_capture('nmap lhs'))
199    local mapargs = fn.maparg('lhs', 'n', false, true)
200    api.nvim_set_keymap('n', 'lhs', 'rhs', { desc = 'MAP DESCRIPTION' })
201    eq('\nn  lhs           rhs\n                 MAP DESCRIPTION', exec_capture('nmap lhs'))
202    fn.mapset('n', false, mapargs)
203    eq('\nn  lhs           rhs\n                 map description', exec_capture('nmap lhs'))
204  end)
205
206  it('can restore "replace_keycodes" from the dict returned by maparg()', function()
207    api.nvim_set_keymap('i', 'foo', [['<l' .. 't>']], { expr = true, replace_keycodes = true })
208    feed('Afoo')
209    expect('<')
210    local mapargs = fn.maparg('foo', 'i', false, true)
211    api.nvim_set_keymap('i', 'foo', [['<l' .. 't>']], { expr = true })
212    feed('foo')
213    expect('<<lt>')
214    fn.mapset('i', false, mapargs)
215    feed('foo')
216    expect('<<lt><')
217  end)
218
219  it('replaces an abbreviation of the same lhs #20320', function()
220    command('inoreabbr foo bar')
221    eq('\ni  foo         * bar', exec_capture('iabbr foo'))
222    feed('ifoo ')
223    expect('bar ')
224    local mapargs = fn.maparg('foo', 'i', true, true)
225    command('inoreabbr foo BAR')
226    eq('\ni  foo         * BAR', exec_capture('iabbr foo'))
227    feed('foo ')
228    expect('bar BAR ')
229    fn.mapset('i', true, mapargs)
230    eq('\ni  foo         * bar', exec_capture('iabbr foo'))
231    feed('foo<Esc>')
232    expect('bar BAR bar')
233  end)
234
235  it('can restore Lua callback from the dict returned by maparg()', function()
236    eq(
237      0,
238      exec_lua([[
239      GlobalCount = 0
240      vim.api.nvim_set_keymap('n', 'asdf', '', {
241        callback = function() GlobalCount = GlobalCount + 1 end,
242      })
243      return GlobalCount
244    ]])
245    )
246    feed('asdf')
247    eq(1, exec_lua([[return GlobalCount]]))
248
249    exec_lua([[
250      _G.saved_asdf_map = vim.fn.maparg('asdf', 'n', false, true)
251      vim.api.nvim_set_keymap('n', 'asdf', '', {
252        callback = function() GlobalCount = GlobalCount + 10 end,
253      })
254    ]])
255    feed('asdf')
256    eq(11, exec_lua([[return GlobalCount]]))
257
258    exec_lua([[vim.fn.mapset('n', false, _G.saved_asdf_map)]])
259    feed('asdf')
260    eq(12, exec_lua([[return GlobalCount]]))
261
262    exec([[
263      let g:saved_asdf_map = maparg('asdf', 'n', v:false, v:true)
264      lua <<
265      vim.api.nvim_set_keymap('n', 'asdf', '', {
266        callback = function() GlobalCount = GlobalCount + 10 end,
267      })
268    ]])
269    feed('asdf')
270    eq(22, exec_lua([[return GlobalCount]]))
271
272    command([[call mapset('n', v:false, g:saved_asdf_map)]])
273    feed('asdf')
274    eq(23, exec_lua([[return GlobalCount]]))
275  end)
276
277  it('does not leak memory if lhs is missing', function()
278    eq(
279      'Vim:E460: Entries missing in mapset() dict argument',
280      pcall_err(exec_lua, [[vim.fn.mapset('n', false, {rhs = 'foo'})]])
281    )
282    eq(
283      'Vim:E460: Entries missing in mapset() dict argument',
284      pcall_err(exec_lua, [[vim.fn.mapset('n', false, {callback = function() end})]])
285    )
286  end)
287end)