-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
Copy pathvim_spec.lua
5530 lines (5139 loc) · 190 KB
/
vim_spec.lua
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')
local uv = vim.uv
local fmt = string.format
local dedent = t.dedent
local assert_alive = n.assert_alive
local NIL = vim.NIL
local clear, eq, neq = n.clear, t.eq, t.neq
local command = n.command
local command_output = n.api.nvim_command_output
local exec = n.exec
local exec_capture = n.exec_capture
local eval = n.eval
local expect = n.expect
local fn = n.fn
local api = n.api
local matches = t.matches
local pesc = vim.pesc
local mkdir_p = n.mkdir_p
local ok, nvim_async, feed = t.ok, n.nvim_async, n.feed
local async_meths = n.async_meths
local is_os = t.is_os
local parse_context = n.parse_context
local request = n.request
local rmdir = n.rmdir
local source = n.source
local next_msg = n.next_msg
local tmpname = t.tmpname
local write_file = t.write_file
local exec_lua = n.exec_lua
local exc_exec = n.exc_exec
local insert = n.insert
local skip = t.skip
local pcall_err = t.pcall_err
local format_string = require('test.format_string').format_string
local intchar2lua = t.intchar2lua
local mergedicts_copy = t.mergedicts_copy
local endswith = vim.endswith
describe('API', function()
before_each(clear)
it('validates requests', function()
-- RPC
matches('Invalid method: bogus$', pcall_err(request, 'bogus'))
matches('Invalid method: … の り 。…$', pcall_err(request, '… の り 。…'))
matches('Invalid method: <empty>$', pcall_err(request, ''))
-- Non-RPC: rpcrequest(v:servername) uses internal channel.
matches(
'Invalid method: … の り 。…$',
pcall_err(
request,
'nvim_eval',
[=[rpcrequest(sockconnect('pipe', v:servername, {'rpc':1}), '… の り 。…')]=]
)
)
matches(
'Invalid method: bogus$',
pcall_err(
request,
'nvim_eval',
[=[rpcrequest(sockconnect('pipe', v:servername, {'rpc':1}), 'bogus')]=]
)
)
-- XXX: This must be the last one, else next one will fail:
-- "Packer instance already working. Use another Packer ..."
matches("can't serialize object of type .$", pcall_err(request, nil))
end)
it('handles errors in async requests', function()
local error_types = api.nvim_get_api_info()[2].error_types
nvim_async('bogus')
eq({
'notification',
'nvim_error_event',
{ error_types.Exception.id, 'Invalid method: bogus' },
}, next_msg())
-- error didn't close channel.
assert_alive()
end)
it('failed async request emits nvim_error_event', function()
local error_types = api.nvim_get_api_info()[2].error_types
async_meths.nvim_command('bogus')
eq({
'notification',
'nvim_error_event',
{ error_types.Exception.id, 'Vim:E492: Not an editor command: bogus' },
}, next_msg())
-- error didn't close channel.
assert_alive()
end)
it('input is processed first if followed immediately by non-fast events', function()
api.nvim_set_current_line('ab')
async_meths.nvim_input('x')
async_meths.nvim_exec_lua('_G.res1 = vim.api.nvim_get_current_line()', {})
async_meths.nvim_exec_lua('_G.res2 = vim.api.nvim_get_current_line()', {})
eq({ 'b', 'b' }, exec_lua('return { _G.res1, _G.res2 }'))
-- Also test with getchar()
async_meths.nvim_command('let g:getchar = 1 | call getchar() | let g:getchar = 0')
eq(1, api.nvim_get_var('getchar'))
async_meths.nvim_input('x')
async_meths.nvim_exec_lua('_G.res1 = vim.g.getchar', {})
async_meths.nvim_exec_lua('_G.res2 = vim.g.getchar', {})
eq({ 0, 0 }, exec_lua('return { _G.res1, _G.res2 }'))
end)
it('does not set CA_COMMAND_BUSY #7254', function()
command('split')
command('autocmd WinEnter * startinsert')
command('wincmd w')
eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
end)
describe('nvim_exec2', function()
it('always returns table', function()
-- In built version this results into `vim.empty_dict()`
eq({}, api.nvim_exec2('echo "Hello"', {}))
eq({}, api.nvim_exec2('echo "Hello"', { output = false }))
eq({ output = 'Hello' }, api.nvim_exec2('echo "Hello"', { output = true }))
end)
it('default options', function()
-- Should be equivalent to { output = false }
api.nvim_exec2("let x0 = 'a'", {})
eq('a', api.nvim_get_var('x0'))
end)
it('one-line input', function()
api.nvim_exec2("let x1 = 'a'", { output = false })
eq('a', api.nvim_get_var('x1'))
end)
it(':verbose set {option}?', function()
api.nvim_exec2('set nowrap', { output = false })
eq(
{ output = 'nowrap\n\tLast set from anonymous :source line 1' },
api.nvim_exec2('verbose set wrap?', { output = true })
)
-- Using script var to force creation of a script item
api.nvim_exec2(
[[
let s:a = 1
set nowrap
]],
{ output = false }
)
eq(
{ output = 'nowrap\n\tLast set from anonymous :source (script id 1) line 2' },
api.nvim_exec2('verbose set wrap?', { output = true })
)
end)
it('multiline input', function()
-- Heredoc + empty lines.
api.nvim_exec2("let x2 = 'a'\n", { output = false })
eq('a', api.nvim_get_var('x2'))
api.nvim_exec2('lua <<EOF\n\n\n\ny=3\n\n\nEOF', { output = false })
eq(3, api.nvim_eval("luaeval('y')"))
eq({}, api.nvim_exec2('lua <<EOF\ny=3\nEOF', { output = false }))
eq(3, api.nvim_eval("luaeval('y')"))
-- Multiple statements
api.nvim_exec2('let x1=1\nlet x2=2\nlet x3=3\n', { output = false })
eq(1, api.nvim_eval('x1'))
eq(2, api.nvim_eval('x2'))
eq(3, api.nvim_eval('x3'))
-- Functions
api.nvim_exec2('function Foo()\ncall setline(1,["xxx"])\nendfunction', { output = false })
eq('', api.nvim_get_current_line())
api.nvim_exec2('call Foo()', { output = false })
eq('xxx', api.nvim_get_current_line())
-- Autocmds
api.nvim_exec2('autocmd BufAdd * :let x1 = "Hello"', { output = false })
command('new foo')
eq('Hello', request('nvim_eval', 'g:x1'))
-- Line continuations
api.nvim_exec2(
[[
let abc = #{
\ a: 1,
"\ b: 2,
\ c: 3
\ }]],
{ output = false }
)
eq({ a = 1, c = 3 }, request('nvim_eval', 'g:abc'))
-- try no spaces before continuations to catch off-by-one error
api.nvim_exec2('let ab = #{\n\\a: 98,\n"\\ b: 2\n\\}', { output = false })
eq({ a = 98 }, request('nvim_eval', 'g:ab'))
-- Script scope (s:)
eq(
{ output = 'ahoy! script-scoped varrrrr' },
api.nvim_exec2(
[[
let s:pirate = 'script-scoped varrrrr'
function! s:avast_ye_hades(s) abort
return a:s .. ' ' .. s:pirate
endfunction
echo <sid>avast_ye_hades('ahoy!')
]],
{ output = true }
)
)
eq(
{ output = "{'output': 'ahoy! script-scoped varrrrr'}" },
api.nvim_exec2(
[[
let s:pirate = 'script-scoped varrrrr'
function! Avast_ye_hades(s) abort
return a:s .. ' ' .. s:pirate
endfunction
echo nvim_exec2('echo Avast_ye_hades(''ahoy!'')', {'output': v:true})
]],
{ output = true }
)
)
matches(
'Vim%(echo%):E121: Undefined variable: s:pirate$',
pcall_err(
request,
'nvim_exec2',
[[
let s:pirate = 'script-scoped varrrrr'
call nvim_exec2('echo s:pirate', {'output': v:true})
]],
{ output = false }
)
)
-- Script items are created only on script var access
eq(
{ output = '1\n0' },
api.nvim_exec2(
[[
echo expand("<SID>")->empty()
let s:a = 123
echo expand("<SID>")->empty()
]],
{ output = true }
)
)
eq(
{ output = '1\n0' },
api.nvim_exec2(
[[
echo expand("<SID>")->empty()
function s:a() abort
endfunction
echo expand("<SID>")->empty()
]],
{ output = true }
)
)
end)
it('non-ASCII input', function()
api.nvim_exec2(
[=[
new
exe "normal! i ax \n Ax "
:%s/ax/--a1234--/g | :%s/Ax/--A1234--/g
]=],
{ output = false }
)
command('1')
eq(' --a1234-- ', api.nvim_get_current_line())
command('2')
eq(' --A1234-- ', api.nvim_get_current_line())
api.nvim_exec2(
[[
new
call setline(1,['xxx'])
call feedkeys('r')
call feedkeys('ñ', 'xt')
]],
{ output = false }
)
eq('ñxx', api.nvim_get_current_line())
end)
it('can use :finish', function()
api.nvim_exec2('let g:var = 123\nfinish\nlet g:var = 456', {})
eq(123, api.nvim_get_var('var'))
end)
it('execution error', function()
eq(
'nvim_exec2(), line 1: Vim:E492: Not an editor command: bogus_command',
pcall_err(request, 'nvim_exec2', 'bogus_command', {})
)
eq('', api.nvim_eval('v:errmsg')) -- v:errmsg was not updated.
eq('', eval('v:exception'))
eq(
'nvim_exec2(), line 1: Vim(buffer):E86: Buffer 23487 does not exist',
pcall_err(request, 'nvim_exec2', 'buffer 23487', {})
)
eq('', eval('v:errmsg')) -- v:errmsg was not updated.
eq('', eval('v:exception'))
end)
it('recursion', function()
local fname = tmpname()
write_file(fname, 'let x1 = "set from :source file"\n')
-- nvim_exec2
-- :source
-- nvim_exec2
request('nvim_exec2', [[
let x2 = substitute('foo','o','X','g')
let x4 = 'should be overwritten'
call nvim_exec2("source ]] .. fname .. [[\nlet x3 = substitute('foo','foo','set by recursive nvim_exec2','g')\nlet x5='overwritten'\nlet x4=x5\n", {'output': v:false})
]], { output = false })
eq('set from :source file', request('nvim_get_var', 'x1'))
eq('fXX', request('nvim_get_var', 'x2'))
eq('set by recursive nvim_exec2', request('nvim_get_var', 'x3'))
eq('overwritten', request('nvim_get_var', 'x4'))
eq('overwritten', request('nvim_get_var', 'x5'))
os.remove(fname)
end)
it('traceback', function()
local fname = tmpname()
write_file(fname, 'echo "hello"\n')
local sourcing_fname = tmpname()
write_file(sourcing_fname, 'call nvim_exec2("source ' .. fname .. '", {"output": v:false})\n')
api.nvim_exec2('set verbose=2', { output = false })
local traceback_output = dedent([[
sourcing "nvim_exec2()"
line 1: sourcing "nvim_exec2() called at nvim_exec2():1"
line 1: sourcing "%s"
line 1: sourcing "nvim_exec2() called at %s:1"
line 1: sourcing "%s"
hello
finished sourcing %s
continuing in nvim_exec2() called at %s:1
finished sourcing nvim_exec2() called at %s:1
continuing in %s
finished sourcing %s
continuing in nvim_exec2() called at nvim_exec2():1
finished sourcing nvim_exec2() called at nvim_exec2():1
continuing in nvim_exec2()
finished sourcing nvim_exec2()]]):format(
sourcing_fname,
sourcing_fname,
fname,
fname,
sourcing_fname,
sourcing_fname,
sourcing_fname,
sourcing_fname
)
eq(
{ output = traceback_output },
api.nvim_exec2(
'call nvim_exec2("source ' .. sourcing_fname .. '", {"output": v:false})',
{ output = true }
)
)
os.remove(fname)
os.remove(sourcing_fname)
end)
it('returns output', function()
eq(
{ output = 'this is spinal tap' },
api.nvim_exec2('lua <<EOF\n\n\nprint("this is spinal tap")\n\n\nEOF', { output = true })
)
eq({ output = '' }, api.nvim_exec2('echo', { output = true }))
eq({ output = 'foo 42' }, api.nvim_exec2('echo "foo" 42', { output = true }))
end)
it('displays messages when opts.output=false', function()
local screen = Screen.new(40, 8)
api.nvim_exec2("echo 'hello'", { output = false })
screen:expect {
grid = [[
^ |
{1:~ }|*6
hello |
]],
}
end)
it("doesn't display messages when output=true", function()
local screen = Screen.new(40, 6)
api.nvim_exec2("echo 'hello'", { output = true })
screen:expect {
grid = [[
^ |
{1:~ }|*4
|
]],
}
exec([[
func Print()
call nvim_exec2('echo "hello"', { 'output': v:true })
endfunc
]])
feed([[:echon 1 | call Print() | echon 5<CR>]])
screen:expect {
grid = [[
^ |
{1:~ }|*4
15 |
]],
}
end)
it('errors properly when command too recursive', function()
exec_lua([[
_G.success = false
vim.api.nvim_create_user_command('Test', function()
vim.api.nvim_exec2('Test', {})
_G.success = true
end, {})
]])
pcall_err(command, 'Test')
assert_alive()
eq(false, exec_lua('return _G.success'))
end)
end)
describe('nvim_command', function()
it('works', function()
local fname = tmpname()
command('new')
command('edit ' .. fname)
command('normal itesting\napi')
command('w')
local f = assert(io.open(fname))
if is_os('win') then
eq('testing\r\napi\r\n', f:read('*a'))
else
eq('testing\napi\n', f:read('*a'))
end
f:close()
os.remove(fname)
end)
it('Vimscript validation error: fails with specific error', function()
local status, rv = pcall(command, 'bogus_command')
eq(false, status) -- nvim_command() failed.
eq('E492:', string.match(rv, 'E%d*:')) -- Vimscript error was returned.
eq('', api.nvim_eval('v:errmsg')) -- v:errmsg was not updated.
eq('', eval('v:exception'))
end)
it('Vimscript execution error: fails with specific error', function()
local status, rv = pcall(command, 'buffer 23487')
eq(false, status) -- nvim_command() failed.
eq('E86: Buffer 23487 does not exist', string.match(rv, 'E%d*:.*'))
eq('', eval('v:errmsg')) -- v:errmsg was not updated.
eq('', eval('v:exception'))
end)
it('gives E493 instead of prompting on backwards range', function()
command('split')
eq(
'Vim(windo):E493: Backwards range given: 2,1windo echo',
pcall_err(command, '2,1windo echo')
)
end)
end)
describe('nvim_command_output', function()
it('does not induce hit-enter prompt', function()
api.nvim_ui_attach(80, 20, {})
-- Induce a hit-enter prompt use nvim_input (non-blocking).
command('set cmdheight=1')
api.nvim_input([[:echo "hi\nhi2"<CR>]])
-- Verify hit-enter prompt.
eq({ mode = 'r', blocking = true }, api.nvim_get_mode())
api.nvim_input([[<C-c>]])
-- Verify NO hit-enter prompt.
command_output([[echo "hi\nhi2"]])
eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
end)
it('captures command output', function()
eq('this is\nspinal tap', command_output([[echo "this is\nspinal tap"]]))
eq('no line ending!', command_output([[echon "no line ending!"]]))
end)
it('captures empty command output', function()
eq('', command_output('echo'))
end)
it('captures single-char command output', function()
eq('x', command_output('echo "x"'))
end)
it('captures multiple commands', function()
eq('foo\n 1 %a "[No Name]" line 1', command_output('echo "foo" | ls'))
end)
it('captures nested execute()', function()
eq(
'\nnested1\nnested2\n 1 %a "[No Name]" line 1',
command_output([[echo execute('echo "nested1\nnested2"') | ls]])
)
end)
it('captures nested nvim_command_output()', function()
eq(
'nested1\nnested2\n 1 %a "[No Name]" line 1',
command_output([[echo nvim_command_output('echo "nested1\nnested2"') | ls]])
)
end)
it('returns shell |:!| output', function()
local win_lf = is_os('win') and '\r' or ''
eq(':!echo foo\r\n\nfoo' .. win_lf .. '\n', command_output([[!echo foo]]))
end)
it('Vimscript validation error: fails with specific error', function()
local status, rv = pcall(command_output, 'bogus commannnd')
eq(false, status) -- nvim_command_output() failed.
eq('E492: Not an editor command: bogus commannnd', string.match(rv, 'E%d*:.*'))
eq('', eval('v:errmsg')) -- v:errmsg was not updated.
-- Verify NO hit-enter prompt.
eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
end)
it('Vimscript execution error: fails with specific error', function()
local status, rv = pcall(command_output, 'buffer 42')
eq(false, status) -- nvim_command_output() failed.
eq('E86: Buffer 42 does not exist', string.match(rv, 'E%d*:.*'))
eq('', eval('v:errmsg')) -- v:errmsg was not updated.
-- Verify NO hit-enter prompt.
eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
end)
it('does not cause heap buffer overflow with large output', function()
eq(eval('string(range(1000000))'), command_output('echo range(1000000)'))
end)
end)
describe('nvim_eval', function()
it('works', function()
command('let g:v1 = "a"')
command('let g:v2 = [1, 2, {"v3": 3}]')
eq({ v1 = 'a', v2 = { 1, 2, { v3 = 3 } } }, api.nvim_eval('g:'))
end)
it('handles NULL-initialized strings correctly', function()
eq(1, api.nvim_eval("matcharg(1) == ['', '']"))
eq({ '', '' }, api.nvim_eval('matcharg(1)'))
end)
it('works under deprecated name', function()
eq(2, request('vim_eval', '1+1'))
end)
it('Vimscript error: returns error details, does NOT update v:errmsg', function()
eq('Vim:E121: Undefined variable: bogus', pcall_err(request, 'nvim_eval', 'bogus expression'))
eq('', eval('v:errmsg')) -- v:errmsg was not updated.
end)
it('can return Lua function to Lua code', function()
eq(
[["a string with \"double quotes\" and 'single quotes'"]],
exec_lua([=[
local fun = vim.api.nvim_eval([[luaeval('string.format')]])
return fun('%q', [[a string with "double quotes" and 'single quotes']])
]=])
)
end)
end)
describe('nvim_call_function', function()
it('works', function()
api.nvim_call_function('setqflist', { { { filename = 'something', lnum = 17 } }, 'r' })
eq(17, api.nvim_call_function('getqflist', {})[1].lnum)
eq(17, api.nvim_call_function('eval', { 17 }))
eq('foo', api.nvim_call_function('simplify', { 'this/./is//redundant/../../../foo' }))
end)
it('Vimscript validation error: returns specific error, does NOT update v:errmsg', function()
eq(
'Vim:E117: Unknown function: bogus function',
pcall_err(request, 'nvim_call_function', 'bogus function', { 'arg1' })
)
eq(
'Vim:E119: Not enough arguments for function: atan',
pcall_err(request, 'nvim_call_function', 'atan', {})
)
eq('', eval('v:exception'))
eq('', eval('v:errmsg')) -- v:errmsg was not updated.
end)
it('Vimscript error: returns error details, does NOT update v:errmsg', function()
eq(
'Vim:E808: Number or Float required',
pcall_err(request, 'nvim_call_function', 'atan', { 'foo' })
)
eq(
'Vim:Invalid channel stream "xxx"',
pcall_err(request, 'nvim_call_function', 'chanclose', { 999, 'xxx' })
)
eq(
'Vim:E900: Invalid channel id',
pcall_err(request, 'nvim_call_function', 'chansend', { 999, 'foo' })
)
eq('', eval('v:exception'))
eq('', eval('v:errmsg')) -- v:errmsg was not updated.
end)
it('Vimscript exception: returns exception details, does NOT update v:errmsg', function()
source([[
function! Foo() abort
throw 'wtf'
endfunction
]])
eq('function Foo, line 1: wtf', pcall_err(request, 'nvim_call_function', 'Foo', {}))
eq('', eval('v:exception'))
eq('', eval('v:errmsg')) -- v:errmsg was not updated.
end)
it('validation', function()
-- stylua: ignore
local too_many_args = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' }
source([[
function! Foo(...) abort
echo a:000
endfunction
]])
-- E740
eq(
'Function called with too many arguments',
pcall_err(request, 'nvim_call_function', 'Foo', too_many_args)
)
end)
it('can return Lua function to Lua code', function()
eq(
[["a string with \"double quotes\" and 'single quotes'"]],
exec_lua([=[
local fun = vim.api.nvim_call_function('luaeval', { 'string.format' })
return fun('%q', [[a string with "double quotes" and 'single quotes']])
]=])
)
end)
end)
describe('nvim_call_dict_function', function()
it('invokes Vimscript dict function', function()
source([[
function! F(name) dict
return self.greeting.', '.a:name.'!'
endfunction
let g:test_dict_fn = { 'greeting':'Hello', 'F':function('F') }
let g:test_dict_fn2 = { 'greeting':'Hi' }
function g:test_dict_fn2.F2(name)
return self.greeting.', '.a:name.' ...'
endfunction
]])
-- :help Dictionary-function
eq('Hello, World!', api.nvim_call_dict_function('g:test_dict_fn', 'F', { 'World' }))
-- Funcref is sent as NIL over RPC.
eq({ greeting = 'Hello', F = NIL }, api.nvim_get_var('test_dict_fn'))
-- :help numbered-function
eq('Hi, Moon ...', api.nvim_call_dict_function('g:test_dict_fn2', 'F2', { 'Moon' }))
-- Funcref is sent as NIL over RPC.
eq({ greeting = 'Hi', F2 = NIL }, api.nvim_get_var('test_dict_fn2'))
-- Function specified via RPC dict.
source('function! G() dict\n return "@".(self.result)."@"\nendfunction')
eq('@it works@', api.nvim_call_dict_function({ result = 'it works', G = 'G' }, 'G', {}))
end)
it('validation', function()
command('let g:d={"baz":"zub","meep":[]}')
eq(
'Not found: bogus',
pcall_err(request, 'nvim_call_dict_function', 'g:d', 'bogus', { 1, 2 })
)
eq(
'Not a function: baz',
pcall_err(request, 'nvim_call_dict_function', 'g:d', 'baz', { 1, 2 })
)
eq(
'Not a function: meep',
pcall_err(request, 'nvim_call_dict_function', 'g:d', 'meep', { 1, 2 })
)
eq(
'Vim:E117: Unknown function: f',
pcall_err(request, 'nvim_call_dict_function', { f = '' }, 'f', { 1, 2 })
)
eq(
'Not a function: f',
pcall_err(request, 'nvim_call_dict_function', "{ 'f': '' }", 'f', { 1, 2 })
)
eq(
'dict argument type must be String or Dict',
pcall_err(request, 'nvim_call_dict_function', 42, 'f', { 1, 2 })
)
eq(
'Vim:E121: Undefined variable: foo',
pcall_err(request, 'nvim_call_dict_function', 'foo', 'f', { 1, 2 })
)
eq('dict not found', pcall_err(request, 'nvim_call_dict_function', '42', 'f', { 1, 2 }))
eq(
'Invalid (empty) function name',
pcall_err(request, 'nvim_call_dict_function', "{ 'f': '' }", '', { 1, 2 })
)
end)
end)
describe('nvim_set_current_dir', function()
local start_dir
before_each(function()
fn.mkdir('Xtestdir')
start_dir = fn.getcwd()
end)
after_each(function()
n.rmdir('Xtestdir')
end)
it('works', function()
api.nvim_set_current_dir('Xtestdir')
eq(start_dir .. n.get_pathsep() .. 'Xtestdir', fn.getcwd())
end)
it('sets previous directory', function()
api.nvim_set_current_dir('Xtestdir')
command('cd -')
eq(start_dir, fn.getcwd())
end)
end)
describe('nvim_exec_lua', function()
it('works', function()
api.nvim_exec_lua('vim.api.nvim_set_var("test", 3)', {})
eq(3, api.nvim_get_var('test'))
eq(17, api.nvim_exec_lua('a, b = ...\nreturn a + b', { 10, 7 }))
eq(NIL, api.nvim_exec_lua('function xx(a,b)\nreturn a..b\nend', {}))
eq('xy', api.nvim_exec_lua('return xx(...)', { 'x', 'y' }))
-- Deprecated name: nvim_execute_lua.
eq('xy', api.nvim_execute_lua('return xx(...)', { 'x', 'y' }))
end)
it('reports errors', function()
eq(
[[Error loading lua: [string "<nvim>"]:0: '=' expected near '+']],
pcall_err(api.nvim_exec_lua, 'a+*b', {})
)
eq(
[[Error loading lua: [string "<nvim>"]:0: unexpected symbol near '1']],
pcall_err(api.nvim_exec_lua, '1+2', {})
)
eq(
[[Error loading lua: [string "<nvim>"]:0: unexpected symbol]],
pcall_err(api.nvim_exec_lua, 'aa=bb\0', {})
)
eq(
[[attempt to call global 'bork' (a nil value)]],
pcall_err(api.nvim_exec_lua, 'bork()', {})
)
eq('did\nthe\nfail', pcall_err(api.nvim_exec_lua, 'error("did\\nthe\\nfail")', {}))
end)
it('uses native float values', function()
eq(2.5, api.nvim_exec_lua('return select(1, ...)', { 2.5 }))
eq('2.5', api.nvim_exec_lua('return vim.inspect(...)', { 2.5 }))
-- "special" float values are still accepted as return values.
eq(2.5, api.nvim_exec_lua("return vim.api.nvim_eval('2.5')", {}))
eq(
'{\n [false] = 2.5,\n [true] = 3\n}',
api.nvim_exec_lua("return vim.inspect(vim.api.nvim_eval('2.5'))", {})
)
end)
end)
describe('nvim_input', function()
it('Vimscript error: does NOT fail, updates v:errmsg', function()
local status, _ = pcall(api.nvim_input, ':call bogus_fn()<CR>')
local v_errnum = string.match(api.nvim_eval('v:errmsg'), 'E%d*:')
eq(true, status) -- nvim_input() did not fail.
eq('E117:', v_errnum) -- v:errmsg was updated.
end)
it('does not crash even if trans_special result is largest #11788, #12287', function()
command("call nvim_input('<M-'.nr2char(0x40000000).'>')")
eq(1, eval('1'))
end)
end)
describe('nvim_paste', function()
it('validation', function()
eq("Invalid 'phase': -2", pcall_err(request, 'nvim_paste', 'foo', true, -2))
eq("Invalid 'phase': 4", pcall_err(request, 'nvim_paste', 'foo', true, 4))
end)
local function run_streamed_paste_tests()
it('stream: multiple chunks form one undo-block', function()
api.nvim_paste('1/chunk 1 (start)\n', true, 1)
api.nvim_paste('1/chunk 2 (end)\n', true, 3)
local expected1 = [[
1/chunk 1 (start)
1/chunk 2 (end)
]]
expect(expected1)
api.nvim_paste('2/chunk 1 (start)\n', true, 1)
api.nvim_paste('2/chunk 2\n', true, 2)
expect([[
1/chunk 1 (start)
1/chunk 2 (end)
2/chunk 1 (start)
2/chunk 2
]])
api.nvim_paste('2/chunk 3\n', true, 2)
api.nvim_paste('2/chunk 4 (end)\n', true, 3)
expect([[
1/chunk 1 (start)
1/chunk 2 (end)
2/chunk 1 (start)
2/chunk 2
2/chunk 3
2/chunk 4 (end)
]])
feed('u') -- Undo.
expect(expected1)
end)
it("stream: multiple chunks sets correct '[ mark", function()
-- Pastes single chunk
api.nvim_paste('aaaaaa\n', true, -1)
eq({ 0, 1, 1, 0 }, fn.getpos("'["))
-- Pastes an empty chunk
api.nvim_paste('', true, -1)
eq({ 0, 2, 1, 0 }, fn.getpos("'["))
-- Pastes some chunks on empty line
api.nvim_paste('1/chunk 1 (start)\n', true, 1)
eq({ 0, 2, 1, 0 }, fn.getpos("'["))
api.nvim_paste('1/chunk 2\n', true, 2)
eq({ 0, 2, 1, 0 }, fn.getpos("'["))
api.nvim_paste('1/chunk 3 (end)\n', true, 3)
eq({ 0, 2, 1, 0 }, fn.getpos("'["))
-- Pastes some chunks on non-empty line
api.nvim_paste('aaaaaa', true, -1)
eq({ 0, 5, 1, 0 }, fn.getpos("'["))
api.nvim_paste('bbbbbb', true, 1)
eq({ 0, 5, 7, 0 }, fn.getpos("'["))
api.nvim_paste('cccccc', true, 2)
eq({ 0, 5, 7, 0 }, fn.getpos("'["))
api.nvim_paste('dddddd\n', true, 3)
eq({ 0, 5, 7, 0 }, fn.getpos("'["))
-- Pastes some empty chunks between non-empty chunks
api.nvim_paste('', true, 1)
eq({ 0, 5, 7, 0 }, fn.getpos("'["))
api.nvim_paste('a', true, 2)
eq({ 0, 6, 1, 0 }, fn.getpos("'["))
api.nvim_paste('', true, 2)
eq({ 0, 6, 1, 0 }, fn.getpos("'["))
api.nvim_paste('a', true, 3)
eq({ 0, 6, 1, 0 }, fn.getpos("'["))
end)
it('stream: Insert mode', function()
-- If nvim_paste() calls :undojoin without making any changes, this makes it an error.
feed('afoo<Esc>u')
feed('i')
api.nvim_paste('aaaaaa', false, 1)
api.nvim_paste('bbbbbb', false, 2)
api.nvim_paste('cccccc', false, 2)
api.nvim_paste('dddddd', false, 3)
expect('aaaaaabbbbbbccccccdddddd')
feed('<Esc>u')
expect('')
end)
describe('stream: Normal mode', function()
describe('on empty line', function()
before_each(function()
-- If nvim_paste() calls :undojoin without making any changes, this makes it an error.
feed('afoo<Esc>u')
end)
after_each(function()
feed('u')
expect('')
end)
it('pasting one line', function()
api.nvim_paste('aaaaaa', false, 1)
api.nvim_paste('bbbbbb', false, 2)
api.nvim_paste('cccccc', false, 2)
api.nvim_paste('dddddd', false, 3)
expect('aaaaaabbbbbbccccccdddddd')
end)
it('pasting multiple lines', function()
api.nvim_paste('aaaaaa\n', false, 1)
api.nvim_paste('bbbbbb\n', false, 2)
api.nvim_paste('cccccc\n', false, 2)
api.nvim_paste('dddddd', false, 3)
expect([[
aaaaaa
bbbbbb
cccccc
dddddd]])
end)
end)
describe('not at the end of a line', function()
before_each(function()
feed('i||<Esc>')
-- If nvim_paste() calls :undojoin without making any changes, this makes it an error.
feed('afoo<Esc>u')
feed('0')
end)
after_each(function()
feed('u')
expect('||')
end)
it('pasting one line', function()
api.nvim_paste('aaaaaa', false, 1)
api.nvim_paste('bbbbbb', false, 2)
api.nvim_paste('cccccc', false, 2)
api.nvim_paste('dddddd', false, 3)
expect('|aaaaaabbbbbbccccccdddddd|')
end)
it('pasting multiple lines', function()
api.nvim_paste('aaaaaa\n', false, 1)
api.nvim_paste('bbbbbb\n', false, 2)
api.nvim_paste('cccccc\n', false, 2)
api.nvim_paste('dddddd', false, 3)
expect([[
|aaaaaa
bbbbbb
cccccc
dddddd|]])
end)
end)
describe('at the end of a line', function()
before_each(function()
feed('i||<Esc>')
-- If nvim_paste() calls :undojoin without making any changes, this makes it an error.
feed('afoo<Esc>u')
feed('2|')
end)
after_each(function()
feed('u')
expect('||')
end)
it('pasting one line', function()
api.nvim_paste('aaaaaa', false, 1)
api.nvim_paste('bbbbbb', false, 2)
api.nvim_paste('cccccc', false, 2)
api.nvim_paste('dddddd', false, 3)
expect('||aaaaaabbbbbbccccccdddddd')
end)
it('pasting multiple lines', function()
api.nvim_paste('aaaaaa\n', false, 1)
api.nvim_paste('bbbbbb\n', false, 2)
api.nvim_paste('cccccc\n', false, 2)
api.nvim_paste('dddddd', false, 3)
expect([[
||aaaaaa
bbbbbb
cccccc
dddddd]])
end)
end)
end)
describe('stream: Visual mode', function()
describe('neither end at the end of a line', function()
before_each(function()
feed('i|xxx<CR>xxx|<Esc>')
-- If nvim_paste() calls :undojoin without making any changes, this makes it an error.
feed('afoo<Esc>u')
feed('3|vhk')
end)
after_each(function()
feed('u')