robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
28.5 KiB745 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 = n.feed
8local eq = t.eq
9local pcall_err = t.pcall_err
10local expect = n.expect
11local eval = n.eval
12local fn = n.fn
13local insert = n.insert
14local write_file = t.write_file
15local command = n.command
16
17describe('mappings with <Cmd>', function()
18  local screen
19  local tmpfile = 'X_ex_cmds_cmd_map'
20
21  local function cmdmap(lhs, rhs)
22    command('noremap ' .. lhs .. ' <Cmd>' .. rhs .. '<cr>')
23    command('noremap! ' .. lhs .. ' <Cmd>' .. rhs .. '<cr>')
24  end
25
26  before_each(function()
27    clear()
28    screen = Screen.new(65, 8)
29    screen:set_default_attr_ids({
30      [1] = { bold = true, foreground = Screen.colors.Blue1 },
31      [2] = { foreground = Screen.colors.Grey100, background = Screen.colors.Red },
32      [3] = { bold = true, foreground = Screen.colors.SeaGreen4 },
33      [4] = { bold = true },
34      [5] = { foreground = Screen.colors.Black, background = Screen.colors.LightGrey },
35      [6] = { foreground = Screen.colors.Blue1 },
36      [7] = { bold = true, reverse = true },
37      [8] = { background = Screen.colors.WebGray },
38      [9] = { background = Screen.colors.LightMagenta },
39      [10] = { foreground = Screen.colors.Red },
40    })
41
42    cmdmap('<F3>', 'let m = mode(1)')
43    cmdmap('<F4>', 'normal! ww')
44    cmdmap('<F5>', 'normal! "ay')
45    cmdmap('<F6>', 'throw "very error"')
46    command([[
47        function! TextObj()
48            if mode() !=# "v"
49                normal! v
50            end
51            call cursor(1,3)
52            normal! o
53            call cursor(2,4)
54        endfunction]])
55    cmdmap('<F7>', 'call TextObj()')
56    insert([[
57        some short lines
58        of test text]])
59    feed('gg')
60    cmdmap('<F8>', 'startinsert')
61    cmdmap('<F9>', 'stopinsert')
62    command('abbr foo <Cmd>let g:y = 17<cr>bar')
63  end)
64
65  after_each(function()
66    os.remove(tmpfile)
67  end)
68
69  it('can be displayed', function()
70    command('map <F3>')
71    screen:expect([[
72      ^some short lines                                                 |
73      of test text                                                     |
74      {1:~                                                                }|*5
75         {6:<F3>}        {6:*} {6:<Cmd>}let m = mode(1){6:<CR>}                        |
76    ]])
77  end)
78
79  it('handles invalid mappings', function()
80    command('let x = 0')
81    command('noremap <F3> <Cmd><Cmd>let x = 1<cr>')
82    feed('<F3>')
83    screen:expect([[
84      ^some short lines                                                 |
85      of test text                                                     |
86      {1:~                                                                }|*5
87      {2:E1136: <Cmd> mapping must end with <CR> before second <Cmd>}      |
88    ]])
89
90    command('noremap <F3> <Cmd>let x = 3')
91    feed('<F3>')
92    screen:expect([[
93      ^some short lines                                                 |
94      of test text                                                     |
95      {1:~                                                                }|*5
96      {2:E1255: <Cmd> mapping must end with <CR>}                          |
97    ]])
98    eq(0, eval('x'))
99  end)
100
101  it('allows special keys and modifiers', function()
102    command('noremap <F3> <Cmd>normal! <Down><CR>')
103    feed('<F3>')
104    screen:expect([[
105      some short lines                                                 |
106      ^of test text                                                     |
107      {1:~                                                                }|*5
108                                                                       |
109    ]])
110
111    command('noremap <F3> <Cmd>normal! <C-Right><CR>')
112    feed('<F3>')
113    screen:expect([[
114      some short lines                                                 |
115      of ^test text                                                     |
116      {1:~                                                                }|*5
117                                                                       |
118    ]])
119  end)
120
121  it('handles string containing K_SPECIAL (0x80) bytes correctly', function()
122    command([[noremap <F3> <Cmd>let g:str = 'foo…bar'<CR>]])
123    feed('<F3>')
124    eq('foo…bar', eval('g:str'))
125    local str = eval([["foo\<D-…>bar"]])
126    command([[noremap <F3> <Cmd>let g:str = ']] .. str .. [['<CR>]])
127    feed('<F3>')
128    eq(str, eval('g:str'))
129    command([[noremap <F3> <Cmd>let g:str = 'foo<D-…>bar'<CR>]])
130    feed('<F3>')
131    eq(str, eval('g:str'))
132  end)
133
134  it('works in various modes and sees correct `mode()` value', function()
135    -- normal mode
136    feed('<F3>')
137    eq('n', eval('m'))
138
139    -- visual mode
140    feed('v<F3>')
141    eq('v', eval('m'))
142    -- didn't leave visual mode
143    eq('v', eval('mode(1)'))
144    feed('<esc>')
145    eq('n', eval('mode(1)'))
146
147    -- visual mapping in select mode
148    feed('gh<F3>')
149    eq('v', eval('m'))
150    -- didn't leave select mode
151    eq('s', eval('mode(1)'))
152    feed('<esc>')
153    eq('n', eval('mode(1)'))
154
155    -- select mode mapping
156    command('snoremap <F3> <Cmd>let m = mode(1)<cr>')
157    feed('gh<F3>')
158    eq('s', eval('m'))
159    -- didn't leave select mode
160    eq('s', eval('mode(1)'))
161    feed('<esc>')
162    eq('n', eval('mode(1)'))
163
164    -- operator-pending mode
165    feed('d<F3>')
166    eq('no', eval('m'))
167    -- did leave operator-pending mode
168    eq('n', eval('mode(1)'))
169
170    --insert mode
171    feed('i<F3>')
172    eq('i', eval('m'))
173    eq('i', eval('mode(1)'))
174
175    -- replace mode
176    feed('<Ins><F3>')
177    eq('R', eval('m'))
178    eq('R', eval('mode(1)'))
179    feed('<esc>')
180    eq('n', eval('mode(1)'))
181
182    -- virtual replace mode
183    feed('gR<F3>')
184    eq('Rv', eval('m'))
185    eq('Rv', eval('mode(1)'))
186    feed('<esc>')
187    eq('n', eval('mode(1)'))
188
189    -- langmap works, but is not distinguished in mode(1)
190    feed(':set iminsert=1<cr>i<F3>')
191    eq('i', eval('m'))
192    eq('i', eval('mode(1)'))
193    feed('<esc>')
194    eq('n', eval('mode(1)'))
195
196    feed(':<F3>')
197    eq('c', eval('m'))
198    eq('c', eval('mode(1)'))
199    feed('<esc>')
200    eq('n', eval('mode(1)'))
201
202    -- terminal mode
203    command('tnoremap <F3> <Cmd>let m = mode(1)<cr>')
204    command('split | terminal')
205    feed('i')
206    eq('t', eval('mode(1)'))
207    feed('<F3>')
208    eq('t', eval('m'))
209    eq('t', eval('mode(1)'))
210  end)
211
212  it('works in normal mode', function()
213    cmdmap('<F2>', 'let s = [mode(1), v:count, v:register]')
214
215    -- check v:count and v:register works
216    feed('<F2>')
217    eq({ 'n', 0, '"' }, eval('s'))
218    feed('7<F2>')
219    eq({ 'n', 7, '"' }, eval('s'))
220    feed('"e<F2>')
221    eq({ 'n', 0, 'e' }, eval('s'))
222    feed('5"k<F2>')
223    eq({ 'n', 5, 'k' }, eval('s'))
224    feed('"+2<F2>')
225    eq({ 'n', 2, '+' }, eval('s'))
226
227    -- text object enters visual mode
228    feed('<F7>')
229    screen:expect([[
230      so{5:me short lines}                                                 |
231      {5:of ^t}est text                                                     |
232      {1:~                                                                }|*5
233      {4:-- VISUAL --}                                                     |
234    ]])
235    feed('<esc>')
236
237    -- startinsert
238    feed('<F8>')
239    eq('i', eval('mode(1)'))
240    feed('<esc>')
241
242    eq('n', eval('mode(1)'))
243    cmdmap(',a', 'call feedkeys("aalpha") \\| let g:a = getline(2)')
244    cmdmap(',b', 'call feedkeys("abeta", "x") \\| let g:b = getline(2)')
245
246    feed(',a<F3>')
247    screen:expect([[
248      some short lines                                                 |
249      of alpha^test text                                                |
250      {1:~                                                                }|*5
251      {4:-- INSERT --}                                                     |
252    ]])
253    -- feedkeys were not executed immediately
254    eq({ 'n', 'of test text' }, eval('[m,a]'))
255    eq('i', eval('mode(1)'))
256    feed('<esc>')
257
258    feed(',b<F3>')
259    screen:expect([[
260      some short lines                                                 |
261      of alphabet^atest text                                            |
262      {1:~                                                                }|*5
263                                                                       |
264    ]])
265    -- feedkeys(..., 'x') was executed immediately, but insert mode gets aborted
266    eq({ 'n', 'of alphabetatest text' }, eval('[m,b]'))
267    eq('n', eval('mode(1)'))
268  end)
269
270  it('works in :normal command', function()
271    command('noremap ,x <Cmd>call append(1, "xx")\\| call append(1, "aa")<cr>')
272    command('noremap ,f <Cmd>nosuchcommand<cr>')
273    command('noremap ,e <Cmd>throw "very error"\\| call append(1, "yy")<cr>')
274    command('noremap ,m <Cmd>echoerr "The message."\\| call append(1, "zz")<cr>')
275    command(
276      'noremap ,w <Cmd>for i in range(5)\\|if i==1\\|echoerr "Err"\\|endif\\|call append(1, i)\\|endfor<cr>'
277    )
278
279    feed(':normal ,x<cr>')
280    screen:expect([[
281      ^some short lines                                                 |
282      aa                                                               |
283      xx                                                               |
284      of test text                                                     |
285      {1:~                                                                }|*3
286      :normal ,x                                                       |
287    ]])
288
289    eq('Vim:E492: Not an editor command: nosuchcommand', pcall_err(command, 'normal ,f'))
290    eq('very error', pcall_err(command, 'normal ,e'))
291    eq('Vim(echoerr):The message.', pcall_err(command, 'normal ,m'))
292    feed('w')
293    screen:expect([[
294      some ^short lines                                                 |
295      aa                                                               |
296      xx                                                               |
297      of test text                                                     |
298      {1:~                                                                }|*3
299      :normal ,x                                                       |
300    ]])
301
302    command(':%d')
303    eq('Vim(echoerr):Err', pcall_err(command, 'normal ,w'))
304    screen:expect([[
305      ^                                                                 |
306      0                                                                |
307      {1:~                                                                }|*5
308      --No lines in buffer--                                           |
309    ]])
310
311    command(':%d')
312    feed(':normal ,w<cr>')
313    screen:expect([[
314      ^                                                                 |
315      4                                                                |
316      3                                                                |
317      2                                                                |
318      1                                                                |
319      0                                                                |
320      {1:~                                                                }|
321      {2:Err}                                                              |
322    ]])
323  end)
324
325  it('works in visual mode', function()
326    -- can extend visual mode
327    feed('v<F4>')
328    screen:expect([[
329      {5:some short ^l}ines                                                 |
330      of test text                                                     |
331      {1:~                                                                }|*5
332      {4:-- VISUAL --}                                                     |
333    ]])
334    eq('v', fn.mode(1))
335
336    -- can invoke operator, ending visual mode
337    feed('<F5>')
338    eq('n', fn.mode(1))
339    eq({ 'some short l' }, fn.getreg('a', 1, 1))
340
341    -- error doesn't interrupt visual mode
342    feed('ggvw<F6>')
343    screen:expect([[
344      {5:some s}hort lines                                                 |
345      of test text                                                     |
346      {1:~                                                                }|*2
347      {7:                                                                 }|
348      {2:Error in :}                                                       |
349      {2:E605: Exception not caught: very error}                           |
350      {3:Press ENTER or type command to continue}^                          |
351    ]])
352    feed('<cr>')
353    eq('E605: Exception not caught: very error', eval('v:errmsg'))
354    -- still in visual mode, <cr> was consumed by the error prompt
355    screen:expect([[
356      {5:some ^s}hort lines                                                 |
357      of test text                                                     |
358      {1:~                                                                }|*5
359      {4:-- VISUAL --}                                                     |
360    ]])
361    eq('v', fn.mode(1))
362    feed('<F7>')
363    screen:expect([[
364      so{5:me short lines}                                                 |
365      {5:of ^t}est text                                                     |
366      {1:~                                                                }|*5
367      {4:-- VISUAL --}                                                     |
368    ]])
369    eq('v', fn.mode(1))
370
371    -- startinsert gives "-- (insert) VISUAL --" mode
372    feed('<F8>')
373    screen:expect([[
374      so{5:me short lines}                                                 |
375      {5:of ^t}est text                                                     |
376      {1:~                                                                }|*5
377      {4:-- (insert) VISUAL --}                                            |
378    ]])
379    eq('v', eval('mode(1)'))
380    feed('<esc>')
381    eq('i', eval('mode(1)'))
382  end)
383
384  it('works in select mode', function()
385    command('snoremap <F1> <cmd>throw "very error"<cr>')
386    command('snoremap <F2> <cmd>normal! <c-g>"by<cr>')
387    -- can extend select mode
388    feed('gh<F4>')
389    screen:expect([[
390      {5:some short ^l}ines                                                 |
391      of test text                                                     |
392      {1:~                                                                }|*5
393      {4:-- SELECT --}                                                     |
394    ]])
395    eq('s', fn.mode(1))
396
397    -- visual mapping in select mode restart select mode after operator
398    feed('<F5>')
399    eq('s', fn.mode(1))
400    eq({ 'some short l' }, fn.getreg('a', 1, 1))
401
402    -- select mode mapping works, and does not restart select mode
403    feed('<F2>')
404    eq('n', fn.mode(1))
405    eq({ 'some short l' }, fn.getreg('b', 1, 1))
406
407    -- error doesn't interrupt temporary visual mode
408    feed('<esc>ggvw<c-g><F6>')
409    screen:expect([[
410      {5:some s}hort lines                                                 |
411      of test text                                                     |
412      {1:~                                                                }|*2
413      {7:                                                                 }|
414      {2:Error in :}                                                       |
415      {2:E605: Exception not caught: very error}                           |
416      {3:Press ENTER or type command to continue}^                          |
417    ]])
418    feed('<cr>')
419    eq('E605: Exception not caught: very error', eval('v:errmsg'))
420    -- still in visual mode, <cr> was consumed by the error prompt
421    screen:expect([[
422      {5:some ^s}hort lines                                                 |
423      of test text                                                     |
424      {1:~                                                                }|*5
425      {4:-- VISUAL --}                                                     |
426    ]])
427    -- quirk: restoration of select mode is not performed
428    eq('v', fn.mode(1))
429
430    -- error doesn't interrupt select mode
431    feed('<esc>ggvw<c-g><F1>')
432    screen:expect([[
433      {5:some s}hort lines                                                 |
434      of test text                                                     |
435      {1:~                                                                }|*2
436      {7:                                                                 }|
437      {2:Error in :}                                                       |
438      {2:E605: Exception not caught: very error}                           |
439      {3:Press ENTER or type command to continue}^                          |
440    ]])
441    feed('<cr>')
442    eq('E605: Exception not caught: very error', eval('v:errmsg'))
443    -- still in select mode, <cr> was consumed by the error prompt
444    screen:expect([[
445      {5:some ^s}hort lines                                                 |
446      of test text                                                     |
447      {1:~                                                                }|*5
448      {4:-- SELECT --}                                                     |
449    ]])
450    -- quirk: restoration of select mode is not performed
451    eq('s', fn.mode(1))
452
453    feed('<F7>')
454    screen:expect([[
455      so{5:me short lines}                                                 |
456      {5:of ^t}est text                                                     |
457      {1:~                                                                }|*5
458      {4:-- SELECT --}                                                     |
459    ]])
460    eq('s', fn.mode(1))
461
462    -- startinsert gives "-- SELECT (insert) --" mode
463    feed('<F8>')
464    screen:expect([[
465      so{5:me short lines}                                                 |
466      {5:of ^t}est text                                                     |
467      {1:~                                                                }|*5
468      {4:-- (insert) SELECT --}                                            |
469    ]])
470    eq('s', eval('mode(1)'))
471    feed('<esc>')
472    eq('i', eval('mode(1)'))
473  end)
474
475  it('works in operator-pending mode', function()
476    feed('d<F4>')
477    expect([[
478        lines
479        of test text]])
480    eq({ 'some short ' }, fn.getreg('"', 1, 1))
481    feed('.')
482    expect([[
483        test text]])
484    eq({ 'lines', 'of ' }, fn.getreg('"', 1, 1))
485    feed('uu')
486    expect([[
487        some short lines
488        of test text]])
489
490    -- error aborts operator-pending, operator not performed
491    feed('d<F6>')
492    screen:expect([[
493      some short lines                                                 |
494      of test text                                                     |
495      {1:~                                                                }|*2
496      {7:                                                                 }|
497      {2:Error in :}                                                       |
498      {2:E605: Exception not caught: very error}                           |
499      {3:Press ENTER or type command to continue}^                          |
500    ]])
501    feed('<cr>')
502    eq('E605: Exception not caught: very error', eval('v:errmsg'))
503    expect([[
504        some short lines
505        of test text]])
506
507    feed('"bd<F7>')
508    expect([[
509        soest text]])
510    eq({ 'me short lines', 'of t' }, fn.getreg('b', 1, 1))
511
512    -- startinsert aborts operator
513    feed('d<F8>')
514    eq('i', eval('mode(1)'))
515    expect([[
516        soest text]])
517  end)
518
519  it('works in insert mode', function()
520    -- works the same as <c-o>w<c-o>w
521    feed('iindeed <F4>little ')
522    screen:expect([[
523      indeed some short little ^lines                                   |
524      of test text                                                     |
525      {1:~                                                                }|*5
526      {4:-- INSERT --}                                                     |
527    ]])
528
529    feed('<F6>')
530    screen:expect([[
531      indeed some short little lines                                   |
532      of test text                                                     |
533      {1:~                                                                }|*2
534      {7:                                                                 }|
535      {2:Error in :}                                                       |
536      {2:E605: Exception not caught: very error}                           |
537      {3:Press ENTER or type command to continue}^                          |
538    ]])
539
540    feed('<cr>')
541    eq('E605: Exception not caught: very error', eval('v:errmsg'))
542    -- still in insert
543    screen:expect([[
544      indeed some short little ^lines                                   |
545      of test text                                                     |
546      {1:~                                                                }|*5
547      {4:-- INSERT --}                                                     |
548    ]])
549    eq('i', eval('mode(1)'))
550
551    -- When entering visual mode from InsertEnter autocmd, an async event, or
552    -- a <cmd> mapping, vim ends up in undocumented "INSERT VISUAL" mode. If a
553    -- vim patch decides to disable this mode, this test is expected to fail.
554    feed('<F7>stuff ')
555    screen:expect([[
556      in{5:deed some short little lines}                                   |
557      {5:of stuff ^t}est text                                               |
558      {1:~                                                                }|*5
559      {4:-- INSERT VISUAL --}                                              |
560    ]])
561    expect([[
562      indeed some short little lines
563      of stuff test text]])
564
565    feed('<F5>')
566    eq({ 'deed some short little lines', 'of stuff t' }, fn.getreg('a', 1, 1))
567
568    -- still in insert
569    screen:expect([[
570      in^deed some short little lines                                   |
571      of stuff test text                                               |
572      {1:~                                                                }|*5
573      {4:-- INSERT --}                                                     |
574    ]])
575    eq('i', eval('mode(1)'))
576
577    -- also works as part of abbreviation
578    feed(' foo ')
579    screen:expect([[
580      in bar ^deed some short little lines                              |
581      of stuff test text                                               |
582      {1:~                                                                }|*5
583      {4:-- INSERT --}                                                     |
584    ]])
585    eq(17, eval('g:y'))
586
587    -- :startinsert does nothing
588    feed('<F8>')
589    eq('i', eval('mode(1)'))
590
591    -- :stopinsert works
592    feed('<F9>')
593    eq('n', eval('mode(1)'))
594  end)
595
596  it('works in insert completion (Ctrl-X) mode', function()
597    feed('os<c-x><c-n>')
598    screen:expect([[
599      some short lines                                                 |
600      some^                                                             |
601      {8:some           }                                                  |
602      {9:short          }{1:                                                  }|
603      {1:~                                                                }|*3
604      {4:-- Keyword Local completion (^N^P) }{3:match 1 of 2}                  |
605    ]])
606
607    feed('<f3>')
608    eq('ic', eval('m'))
609
610    -- ensure a redraw, this would have moved the cursor
611    -- instead if CTRL-X mode was left.
612    feed('<up>')
613    screen:expect([[
614      some short lines                                                 |
615      some^                                                             |
616      {9:some           }                                                  |
617      {9:short          }{1:                                                  }|
618      {1:~                                                                }|*3
619      {4:-- Keyword Local completion (^N^P) }{10:Back at original}              |
620    ]])
621  end)
622
623  it('works in cmdline mode', function()
624    feed(':text<F3>')
625    eq('c', eval('m'))
626    -- didn't leave cmdline mode
627    eq('c', eval('mode(1)'))
628    feed('<cr>')
629    eq('n', eval('mode(1)'))
630    screen:expect([[
631      ^some short lines                                                 |
632      of test text                                                     |
633      {1:~                                                                }|*5
634      {2:E492: Not an editor command: text}                                |
635    ]])
636
637    feed(':echo 2<F6>')
638    screen:expect([[
639      some short lines                                                 |
640      of test text                                                     |
641      {1:~                                                                }|
642      {7:                                                                 }|
643      :echo 2                                                          |
644      {2:Error in :}                                                       |
645      {2:E605: Exception not caught: very error}                           |
646      :echo 2^                                                          |
647    ]])
648    eq('E605: Exception not caught: very error', eval('v:errmsg'))
649    -- didn't leave cmdline mode
650    eq('c', eval('mode(1)'))
651    feed('+2<cr>')
652    screen:expect([[
653      some short lines                                                 |
654      of test text                                                     |
655      {7:                                                                 }|
656      :echo 2                                                          |
657      {2:Error in :}                                                       |
658      {2:E605: Exception not caught: very error}                           |
659      4                                                                |
660      {3:Press ENTER or type command to continue}^                          |
661    ]])
662    -- however, message scrolling may cause extra CR prompt
663    -- This is consistent with output from async events.
664    feed('<cr>')
665    screen:expect([[
666      ^some short lines                                                 |
667      of test text                                                     |
668      {1:~                                                                }|*5
669                                                                       |
670    ]])
671    eq('n', eval('mode(1)'))
672
673    feed(':let g:x = 3<F4>')
674    screen:expect([[
675      some short lines                                                 |
676      of test text                                                     |
677      {1:~                                                                }|*5
678      :let g:x = 3^                                                     |
679    ]])
680    feed('+2<cr>')
681    -- cursor was moved in the background
682    screen:expect([[
683      some short ^lines                                                 |
684      of test text                                                     |
685      {1:~                                                                }|*5
686      :let g:x = 3+2                                                   |
687    ]])
688    eq(5, eval('g:x'))
689
690    feed(':let g:y = 7<F8>')
691    screen:expect([[
692      some short lines                                                 |
693      of test text                                                     |
694      {1:~                                                                }|*5
695      :let g:y = 7^                                                     |
696    ]])
697    eq('c', eval('mode(1)'))
698    feed('+2<cr>')
699    -- startinsert takes effect after leaving cmdline mode
700    screen:expect([[
701      some short ^lines                                                 |
702      of test text                                                     |
703      {1:~                                                                }|*5
704      {4:-- INSERT --}                                                     |
705    ]])
706    eq('i', eval('mode(1)'))
707    eq(9, eval('g:y'))
708  end)
709
710  it("doesn't crash when invoking cmdline mode recursively #8859", function()
711    cmdmap('<F2>', 'norm! :foo')
712    feed(':bar')
713    screen:expect([[
714      some short lines                                                 |
715      of test text                                                     |
716      {1:~                                                                }|*5
717      :bar^                                                             |
718    ]])
719
720    feed('<f2>x')
721    screen:expect([[
722      some short lines                                                 |
723      of test text                                                     |
724      {1:~                                                                }|*5
725      :barx^                                                            |
726    ]])
727  end)
728
729  it('works with <SID> mappings', function()
730    command('new!')
731    write_file(
732      tmpfile,
733      [[
734      map <f2> <Cmd>call <SID>do_it()<Cr>
735      function! s:do_it()
736        let g:x = 10
737      endfunction
738    ]]
739    )
740    command('source ' .. tmpfile)
741    feed('<f2>')
742    eq('', eval('v:errmsg'))
743    eq(10, eval('g:x'))
744  end)
745end)