robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
15.0 KiB460 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
6local clear = n.clear
7local feed, next_msg, eq = n.feed, n.next_msg, t.eq
8local command = n.command
9local expect = n.expect
10local curbuf_contents = n.curbuf_contents
11local api = n.api
12local exec_lua = n.exec_lua
13local write_file = t.write_file
14local fn = n.fn
15local eval = n.eval
16
17before_each(clear)
18
19describe('mappings', function()
20  local add_mapping = function(mapping, send)
21    local cmd = 'nnoremap '
22      .. mapping
23      .. " :call rpcnotify(1, 'mapped', '"
24      .. send:gsub('<', '<lt>')
25      .. "')<cr>"
26    command(cmd)
27  end
28
29  local check_mapping = function(mapping, expected)
30    feed(mapping)
31    eq({ 'notification', 'mapped', { expected } }, next_msg())
32  end
33
34  before_each(function()
35    add_mapping('<A-l>', '<A-l>')
36    add_mapping('<A-L>', '<A-L>')
37    add_mapping('<D-l>', '<D-l>')
38    add_mapping('<D-L>', '<D-L>')
39    add_mapping('<C-L>', '<C-L>')
40    add_mapping('<C-S-L>', '<C-S-L>')
41    add_mapping('<s-up>', '<s-up>')
42    add_mapping('<s-up>', '<s-up>')
43    add_mapping('<c-s-up>', '<c-s-up>')
44    add_mapping('<c-s-a-up>', '<c-s-a-up>')
45    add_mapping('<c-s-a-d-up>', '<c-s-a-d-up>')
46    add_mapping('<c-d-a>', '<c-d-a>')
47    add_mapping('<d-1>', '<d-1>')
48    add_mapping('<khome>', '<khome>')
49    add_mapping('<kup>', '<kup>')
50    add_mapping('<kpageup>', '<kpageup>')
51    add_mapping('<kleft>', '<kleft>')
52    add_mapping('<korigin>', '<korigin>')
53    add_mapping('<kright>', '<kright>')
54    add_mapping('<kend>', '<kend>')
55    add_mapping('<kdown>', '<kdown>')
56    add_mapping('<kpagedown>', '<kpagedown>')
57    add_mapping('<kinsert>', '<kinsert>')
58    add_mapping('<kdel>', '<kdel>')
59    add_mapping('<kdivide>', '<kdivide>')
60    add_mapping('<kmultiply>', '<kmultiply>')
61    add_mapping('<kminus>', '<kminus>')
62    add_mapping('<kplus>', '<kplus>')
63    add_mapping('<kenter>', '<kenter>')
64    add_mapping('<kcomma>', '<kcomma>')
65    add_mapping('<kequal>', '<kequal>')
66    add_mapping('<find>', '<find>')
67    add_mapping('<select>', '<select>')
68    add_mapping('<f38>', '<f38>')
69    add_mapping('<f63>', '<f63>')
70  end)
71
72  it('ok', function()
73    check_mapping('<A-l>', '<A-l>')
74    check_mapping('<A-L>', '<A-L>')
75    check_mapping('<A-S-l>', '<A-L>')
76    check_mapping('<A-S-L>', '<A-L>')
77    check_mapping('<D-l>', '<D-l>')
78    check_mapping('<D-L>', '<D-L>')
79    check_mapping('<D-S-l>', '<D-L>')
80    check_mapping('<D-S-L>', '<D-L>')
81    check_mapping('<C-l>', '<C-L>')
82    check_mapping('<C-L>', '<C-L>')
83    check_mapping('<C-S-l>', '<C-S-L>')
84    check_mapping('<C-S-L>', '<C-S-L>')
85    check_mapping('<s-up>', '<s-up>')
86    check_mapping('<c-s-up>', '<c-s-up>')
87    check_mapping('<s-c-up>', '<c-s-up>')
88    check_mapping('<c-s-a-up>', '<c-s-a-up>')
89    check_mapping('<s-c-a-up>', '<c-s-a-up>')
90    check_mapping('<c-a-s-up>', '<c-s-a-up>')
91    check_mapping('<s-a-c-up>', '<c-s-a-up>')
92    check_mapping('<a-c-s-up>', '<c-s-a-up>')
93    check_mapping('<a-s-c-up>', '<c-s-a-up>')
94    check_mapping('<c-s-a-d-up>', '<c-s-a-d-up>')
95    check_mapping('<s-a-d-c-up>', '<c-s-a-d-up>')
96    check_mapping('<d-s-a-c-up>', '<c-s-a-d-up>')
97    check_mapping('<c-d-a>', '<c-d-a>')
98    check_mapping('<d-c-a>', '<c-d-a>')
99    check_mapping('<d-1>', '<d-1>')
100    check_mapping('<khome>', '<khome>')
101    check_mapping('<KP7>', '<khome>')
102    check_mapping('<kup>', '<kup>')
103    check_mapping('<KP8>', '<kup>')
104    check_mapping('<kpageup>', '<kpageup>')
105    check_mapping('<KP9>', '<kpageup>')
106    check_mapping('<kleft>', '<kleft>')
107    check_mapping('<KP4>', '<kleft>')
108    check_mapping('<korigin>', '<korigin>')
109    check_mapping('<KP5>', '<korigin>')
110    check_mapping('<kright>', '<kright>')
111    check_mapping('<KP6>', '<kright>')
112    check_mapping('<kend>', '<kend>')
113    check_mapping('<KP1>', '<kend>')
114    check_mapping('<kdown>', '<kdown>')
115    check_mapping('<KP2>', '<kdown>')
116    check_mapping('<kpagedown>', '<kpagedown>')
117    check_mapping('<KP3>', '<kpagedown>')
118    check_mapping('<kinsert>', '<kinsert>')
119    check_mapping('<KP0>', '<kinsert>')
120    check_mapping('<kdel>', '<kdel>')
121    check_mapping('<KPPeriod>', '<kdel>')
122    check_mapping('<kdivide>', '<kdivide>')
123    check_mapping('<KPDiv>', '<kdivide>')
124    check_mapping('<kmultiply>', '<kmultiply>')
125    check_mapping('<KPMult>', '<kmultiply>')
126    check_mapping('<kminus>', '<kminus>')
127    check_mapping('<KPMinus>', '<kminus>')
128    check_mapping('<kplus>', '<kplus>')
129    check_mapping('<KPPlus>', '<kplus>')
130    check_mapping('<kenter>', '<kenter>')
131    check_mapping('<KPEnter>', '<kenter>')
132    check_mapping('<kcomma>', '<kcomma>')
133    check_mapping('<KPComma>', '<kcomma>')
134    check_mapping('<kequal>', '<kequal>')
135    check_mapping('<KPEquals>', '<kequal>')
136    check_mapping('<Find>', '<find>')
137    check_mapping('<Select>', '<select>')
138    check_mapping('<f38>', '<f38>')
139    check_mapping('<f63>', '<f63>')
140  end)
141
142  it('support meta + multibyte char mapping', function()
143    add_mapping('<m-ä>', '<m-ä>')
144    check_mapping('<m-ä>', '<m-ä>')
145  end)
146end)
147
148describe('input utf sequences that contain K_SPECIAL (0x80)', function()
149  it('ok', function()
150    feed('i…<esc>')
151    expect('…')
152  end)
153
154  it('can be mapped', function()
155    command('inoremap … E280A6')
156    feed('i…<esc>')
157    expect('E280A6')
158  end)
159end)
160
161describe('input utf sequences that contain CSI (0x9B)', function()
162  it('ok', function()
163    feed('iě<esc>')
164    expect('ě')
165  end)
166
167  it('can be mapped', function()
168    command('inoremap ě C49B')
169    feed('iě<esc>')
170    expect('C49B')
171  end)
172end)
173
174describe('input split utf sequences', function()
175  it('ok', function()
176    local str = '►'
177    feed('i' .. str:sub(1, 1))
178    vim.uv.sleep(10)
179    feed(str:sub(2, 3))
180    expect('►')
181  end)
182
183  it('can be mapped', function()
184    command('inoremap ► E296BA')
185    command('set ttimeoutlen=100') -- CI test runners may cause timeout otherwise
186    local str = '►'
187    feed('i' .. str:sub(1, 1))
188    vim.uv.sleep(10)
189    feed(str:sub(2, 3))
190    expect('E296BA')
191  end)
192end)
193
194describe('input pairs', function()
195  describe('<tab> / <c-i>', function()
196    it('ok', function()
197      feed('i<tab><c-i><esc>')
198      eq('\t\t', curbuf_contents())
199    end)
200
201    describe('can be mapped separately', function()
202      it('if <tab> is mapped after <c-i>', function()
203        command('inoremap <c-i> CTRL-I!')
204        command('inoremap <tab> TAB!')
205        feed('i<tab><c-i><esc>')
206        eq('TAB!CTRL-I!', curbuf_contents())
207      end)
208
209      it('if <tab> is mapped before <c-i>', function()
210        command('inoremap <tab> TAB!')
211        command('inoremap <c-i> CTRL-I!')
212        feed('i<tab><c-i><esc>')
213        eq('TAB!CTRL-I!', curbuf_contents())
214      end)
215    end)
216  end)
217
218  describe('<cr> / <c-m>', function()
219    it('ok', function()
220      feed('iunos<c-m>dos<cr>tres<esc>')
221      eq('unos\ndos\ntres', curbuf_contents())
222    end)
223
224    describe('can be mapped separately', function()
225      it('if <cr> is mapped after <c-m>', function()
226        command('inoremap <c-m> SNIPPET!')
227        command('inoremap <cr> , and then<cr>')
228        feed('iunos<c-m>dos<cr>tres<esc>')
229        eq('unosSNIPPET!dos, and then\ntres', curbuf_contents())
230      end)
231
232      it('if <cr> is mapped before <c-m>', function()
233        command('inoremap <cr> , and then<cr>')
234        command('inoremap <c-m> SNIPPET!')
235        feed('iunos<c-m>dos<cr>tres<esc>')
236        eq('unosSNIPPET!dos, and then\ntres', curbuf_contents())
237      end)
238    end)
239  end)
240
241  describe('<esc> / <c-[>', function()
242    it('ok', function()
243      feed('2adouble<c-[>asingle<esc>')
244      eq('doubledoublesingle', curbuf_contents())
245    end)
246
247    describe('can be mapped separately', function()
248      it('if <esc> is mapped after <c-[>', function()
249        command('inoremap <c-[> HALLOJ!')
250        command('inoremap <esc> ,<esc>')
251        feed('2adubbel<c-[>upp<esc>')
252        eq('dubbelHALLOJ!upp,dubbelHALLOJ!upp,', curbuf_contents())
253      end)
254
255      it('if <esc> is mapped before <c-[>', function()
256        command('inoremap <esc> ,<esc>')
257        command('inoremap <c-[> HALLOJ!')
258        feed('2adubbel<c-[>upp<esc>')
259        eq('dubbelHALLOJ!upp,dubbelHALLOJ!upp,', curbuf_contents())
260      end)
261    end)
262  end)
263end)
264
265it('Ctrl-6 is Ctrl-^ vim-patch:8.1.2333', function()
266  command('split aaa')
267  command('edit bbb')
268  feed('<C-6>')
269  eq('aaa', fn.bufname())
270end)
271
272it('c_CTRL-R_CTRL-R, i_CTRL-R_CTRL-R, i_CTRL-G_CTRL-K work properly vim-patch:8.1.2346', function()
273  command('set timeoutlen=10')
274
275  command([[let @a = 'aaa']])
276  feed([[:let x = '<C-R><C-R>a'<CR>]])
277  eq([[let x = 'aaa']], eval('@:'))
278
279  feed('a<C-R><C-R>a<Esc>')
280  expect('aaa')
281  command('bwipe!')
282
283  feed('axx<CR>yy<C-G><C-K>a<Esc>')
284  expect([[
285  axx
286  yy]])
287end)
288
289it('typing a simplifiable key at hit-enter prompt triggers mapping vim-patch:8.2.0839', function()
290  local screen = Screen.new(60, 8)
291  command([[nnoremap <C-6> <Cmd>echo 'hit ctrl-6'<CR>]])
292  feed(':ls<CR>')
293  screen:expect([[
294                                                                |
295    {1:~                                                           }|*3
296    {3:                                                            }|
297    :ls                                                         |
298      1 %a   "[No Name]"                    line 1              |
299    {6:Press ENTER or type command to continue}^                     |
300  ]])
301  feed('<C-6>')
302  screen:expect([[
303    ^                                                            |
304    {1:~                                                           }|*6
305    hit ctrl-6                                                  |
306  ]])
307end)
308
309it('mixing simplified and unsimplified keys can trigger mapping vim-patch:8.2.0916', function()
310  command('set timeoutlen=10')
311  command([[imap ' <C-W>]])
312  command('imap <C-W><C-A> c-a')
313  feed([[a'<C-A>]])
314  expect('c-a')
315end)
316
317it('unsimplified mapping works when there was a partial match vim-patch:8.2.4504', function()
318  command('set timeoutlen=10')
319  command('nnoremap <C-J> a')
320  command('nnoremap <NL> x')
321  command('nnoremap <C-J>x <Nop>')
322  fn.setline(1, 'x')
323  -- CTRL-J b should have trigger the <C-J> mapping and then insert "b"
324  feed('<C-J>b<Esc>')
325  expect('xb')
326end)
327
328describe('input non-printable chars', function()
329  after_each(function()
330    os.remove('Xtest-overwrite')
331  end)
332
333  it("doesn't crash when echoing them back", function()
334    write_file('Xtest-overwrite', [[foobar]])
335    local screen = Screen.new(60, 8)
336    command('set shortmess-=F')
337
338    command('e Xtest-overwrite')
339    screen:expect([[
340      ^foobar                                                      |
341      {1:~                                                           }|*6
342      "Xtest-overwrite" [noeol] 1L, 6B                            |
343    ]])
344
345    -- Wait for some time so that the timestamp changes.
346    vim.uv.sleep(10)
347    write_file('Xtest-overwrite', [[smurf]])
348    feed(':w<CR>')
349    screen:expect([[
350      foobar                                                      |
351      {1:~                                                           }|*3
352      {3:                                                            }|
353      "Xtest-overwrite"                                           |
354      {9:WARNING: The file has been changed since reading it!!!}      |
355      {6:Do you really want to write to it (y/n)?}^                    |
356    ]])
357
358    feed('u')
359    screen:expect([[
360      foobar                                                      |
361      {1:~                                                           }|*2
362      {3:                                                            }|
363      "Xtest-overwrite"                                           |
364      {9:WARNING: The file has been changed since reading it!!!}      |
365      {6:Do you really want to write to it (y/n)?}u                   |
366      {6:Do you really want to write to it (y/n)?}^                    |
367    ]])
368
369    feed('\005')
370    screen:expect([[
371      foobar                                                      |
372      {1:~                                                           }|
373      {3:                                                            }|
374      "Xtest-overwrite"                                           |
375      {9:WARNING: The file has been changed since reading it!!!}      |
376      {6:Do you really want to write to it (y/n)?}u                   |
377      {6:Do you really want to write to it (y/n)?}{18:^E}                  |
378      {6:Do you really want to write to it (y/n)?}^                    |
379    ]])
380
381    feed('n')
382    screen:expect([[
383      foobar                                                      |
384      {3:                                                            }|
385      "Xtest-overwrite"                                           |
386      {9:WARNING: The file has been changed since reading it!!!}      |
387      {6:Do you really want to write to it (y/n)?}u                   |
388      {6:Do you really want to write to it (y/n)?}{18:^E}                  |
389      {6:Do you really want to write to it (y/n)?}n                   |
390      {6:Press ENTER or type command to continue}^                     |
391    ]])
392
393    feed('<cr>')
394    screen:expect([[
395      ^foobar                                                      |
396      {1:~                                                           }|*6
397                                                                  |
398    ]])
399  end)
400end)
401
402describe('event processing and input', function()
403  it('not blocked by event bursts', function()
404    api.nvim_set_keymap(
405      '',
406      '<f2>',
407      "<cmd>lua vim.rpcnotify(1, 'stop') winning = true <cr>",
408      { noremap = true }
409    )
410
411    exec_lua [[
412      winning = false
413      burst = vim.schedule_wrap(function(tell)
414        if tell then
415          vim.rpcnotify(1, 'start')
416        end
417        -- Are we winning, son?
418        if not winning then
419          burst(false)
420        end
421      end)
422      burst(true)
423    ]]
424
425    eq({ 'notification', 'start', {} }, next_msg())
426    feed '<f2>'
427    eq({ 'notification', 'stop', {} }, next_msg())
428  end)
429end)
430
431describe('display is updated', function()
432  local screen
433  before_each(function()
434    screen = Screen.new(60, 8)
435  end)
436
437  it('in Insert mode after <Nop> mapping #17911', function()
438    command('imap <Plug>test <Nop>')
439    command('imap <F2> abc<CR><Plug>test')
440    feed('i<F2>')
441    screen:expect([[
442      abc                                                         |
443      ^                                                            |
444      {1:~                                                           }|*5
445      {5:-- INSERT --}                                                |
446    ]])
447  end)
448
449  it('in Insert mode after empty string <expr> mapping #17911', function()
450    command('imap <expr> <Plug>test ""')
451    command('imap <F2> abc<CR><Plug>test')
452    feed('i<F2>')
453    screen:expect([[
454      abc                                                         |
455      ^                                                            |
456      {1:~                                                           }|*5
457      {5:-- INSERT --}                                                |
458    ]])
459  end)
460end)