Skip to content

Commit 476a613

Browse files
committed
patch 8.2.0531: various errors not tested
Problem: Various errors not tested. Solution: Add tests. (Yegappan Lakshmanan, closes #5895)
1 parent a65c288 commit 476a613

6 files changed

Lines changed: 357 additions & 160 deletions

File tree

src/testdir/test_search.vim

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,4 +1543,39 @@ func Test_searchforward_var()
15431543
close!
15441544
endfunc
15451545

1546+
" Test for invalid regular expressions
1547+
func Test_invalid_regexp()
1548+
set regexpengine=1
1549+
call assert_fails("call search(repeat('\\(.\\)', 10))", 'E51:')
1550+
call assert_fails("call search('a\\+*')", 'E61:')
1551+
call assert_fails("call search('x\\@#')", 'E59:')
1552+
call assert_fails("call search('\\_m')", 'E63:')
1553+
call assert_fails("call search('\\+')", 'E64:')
1554+
call assert_fails("call search('\\1')", 'E65:')
1555+
call assert_fails("call search('\\z\\(\\)')", 'E66:')
1556+
call assert_fails("call search('\\z2')", 'E67:')
1557+
call assert_fails("call search('\\zx')", 'E68:')
1558+
call assert_fails("call search('\\%[ab')", 'E69:')
1559+
call assert_fails("call search('\\%[]')", 'E70:')
1560+
call assert_fails("call search('\\%a')", 'E71:')
1561+
call assert_fails("call search('ab\\%[\\(cd\\)]')", 'E369:')
1562+
call assert_fails("call search('ab\\%[\\%(cd\\)]')", 'E369:')
1563+
set regexpengine=2
1564+
call assert_fails("call search('\\_')", 'E865:')
1565+
call assert_fails("call search('\\+')", 'E866:')
1566+
call assert_fails("call search('\\zx')", 'E867:')
1567+
call assert_fails("call search('\\%a')", 'E867:')
1568+
call assert_fails("call search('x\\@#')", 'E869:')
1569+
call assert_fails("call search(repeat('\\(.\\)', 10))", 'E872:')
1570+
call assert_fails("call search('\\_m')", 'E877:')
1571+
call assert_fails("call search('\\%(')", 'E53:')
1572+
call assert_fails("call search('\\(')", 'E54:')
1573+
call assert_fails("call search('\\)')", 'E55:')
1574+
call assert_fails("call search('\\z\\(\\)')", 'E66:')
1575+
call assert_fails("call search('\\%[ab')", 'E69:')
1576+
call assert_fails("call search('\\%9999999999999999999999999999v')", 'E951:')
1577+
set regexpengine&
1578+
call assert_fails("call search('\\%#=3ab')", 'E864:')
1579+
endfunc
1580+
15461581
" vim: shiftwidth=2 sts=2 expandtab

src/testdir/test_source.vim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,10 @@ func Test_source_autocmd_sfile()
8787
call delete('Xscript.vim')
8888
endfunc
8989

90+
func Test_source_error()
91+
call assert_fails('scriptencoding utf-8', 'E167:')
92+
call assert_fails('finish', 'E168:')
93+
call assert_fails('scriptversion 2', 'E984:')
94+
endfunc
95+
9096
" vim: shiftwidth=2 sts=2 expandtab

src/testdir/test_syntax.vim

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,64 @@ func Test_syntax_invalid_arg()
332332
call assert_fails('syntax sync x', 'E404:')
333333
call assert_fails('syntax keyword Abc a[', 'E789:')
334334
call assert_fails('syntax keyword Abc a[bc]d', 'E890:')
335+
336+
let caught_393 = 0
337+
try
338+
syntax keyword cMyItem grouphere G1
339+
catch /E393:/
340+
let caught_393 = 1
341+
endtry
342+
call assert_equal(1, caught_393)
343+
344+
let caught_394 = 0
345+
try
346+
syntax sync match Abc grouphere MyItem "abc"'
347+
catch /E394:/
348+
let caught_394 = 1
349+
endtry
350+
call assert_equal(1, caught_394)
351+
352+
" Test for too many \z\( and unmatched \z\(
353+
" Not able to use assert_fails() here because both E50:/E879: and E475:
354+
" messages are emitted.
355+
set regexpengine=1
356+
let caught_52 = 0
357+
try
358+
syntax region MyRegion start='\z\(' end='\*/'
359+
catch /E52:/
360+
let caught_52 = 1
361+
endtry
362+
call assert_equal(1, caught_52)
363+
364+
let caught_50 = 0
365+
try
366+
let cmd = "syntax region MyRegion start='"
367+
let cmd ..= repeat("\\z\\(.\\)", 10) .. "' end='\*/'"
368+
exe cmd
369+
catch /E50:/
370+
let caught_50 = 1
371+
endtry
372+
call assert_equal(1, caught_50)
373+
374+
set regexpengine=2
375+
let caught_54 = 0
376+
try
377+
syntax region MyRegion start='\z\(' end='\*/'
378+
catch /E54:/
379+
let caught_54 = 1
380+
endtry
381+
call assert_equal(1, caught_54)
382+
383+
let caught_879 = 0
384+
try
385+
let cmd = "syntax region MyRegion start='"
386+
let cmd ..= repeat("\\z\\(.\\)", 10) .. "' end='\*/'"
387+
exe cmd
388+
catch /E879:/
389+
let caught_879 = 1
390+
endtry
391+
call assert_equal(1, caught_879)
392+
set regexpengine&
335393
endfunc
336394

337395
func Test_syn_sync()

src/testdir/test_user_func.vim

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
" Also test that a builtin function cannot be replaced.
44
" Also test for regression when calling arbitrary expression.
55

6+
source check.vim
7+
source shared.vim
8+
69
func Table(title, ...)
710
let ret = a:title
811
let idx = 1
@@ -83,6 +86,7 @@ func Test_user_func()
8386
normal o[(one again
8487
call assert_equal('1. one again', getline('.'))
8588

89+
" Try to overwrite a function in the global (g:) scope
8690
call assert_equal(3, max([1, 2, 3]))
8791
call assert_fails("call extend(g:, {'max': function('min')})", 'E704')
8892
call assert_equal(3, max([1, 2, 3]))
@@ -175,4 +179,256 @@ func Test_function_list()
175179
call assert_fails("function Xabc", 'E123:')
176180
endfunc
177181

182+
" Test for <sfile>, <slnum> in a function
183+
func Test_sfile_in_function()
184+
func Xfunc()
185+
call assert_match('..Test_sfile_in_function\[5]..Xfunc', expand('<sfile>'))
186+
call assert_equal('2', expand('<slnum>'))
187+
endfunc
188+
call Xfunc()
189+
delfunc Xfunc
190+
endfunc
191+
192+
" Test trailing text after :endfunction {{{1
193+
func Test_endfunction_trailing()
194+
call assert_false(exists('*Xtest'))
195+
196+
exe "func Xtest()\necho 'hello'\nendfunc\nlet done = 'yes'"
197+
call assert_true(exists('*Xtest'))
198+
call assert_equal('yes', done)
199+
delfunc Xtest
200+
unlet done
201+
202+
exe "func Xtest()\necho 'hello'\nendfunc|let done = 'yes'"
203+
call assert_true(exists('*Xtest'))
204+
call assert_equal('yes', done)
205+
delfunc Xtest
206+
unlet done
207+
208+
" trailing line break
209+
exe "func Xtest()\necho 'hello'\nendfunc\n"
210+
call assert_true(exists('*Xtest'))
211+
delfunc Xtest
212+
213+
set verbose=1
214+
exe "func Xtest()\necho 'hello'\nendfunc \" garbage"
215+
call assert_notmatch('W22:', split(execute('1messages'), "\n")[0])
216+
call assert_true(exists('*Xtest'))
217+
delfunc Xtest
218+
219+
exe "func Xtest()\necho 'hello'\nendfunc garbage"
220+
call assert_match('W22:', split(execute('1messages'), "\n")[0])
221+
call assert_true(exists('*Xtest'))
222+
delfunc Xtest
223+
set verbose=0
224+
225+
function Foo()
226+
echo 'hello'
227+
endfunction | echo 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
228+
delfunc Foo
229+
endfunc
230+
231+
func Test_delfunction_force()
232+
delfunc! Xtest
233+
delfunc! Xtest
234+
func Xtest()
235+
echo 'nothing'
236+
endfunc
237+
delfunc! Xtest
238+
delfunc! Xtest
239+
240+
" Try deleting the current function
241+
call assert_fails('delfunc Test_delfunction_force', 'E131:')
242+
endfunc
243+
244+
func Test_function_defined_line()
245+
CheckNotGui
246+
247+
let lines =<< trim [CODE]
248+
" F1
249+
func F1()
250+
" F2
251+
func F2()
252+
"
253+
"
254+
"
255+
return
256+
endfunc
257+
" F3
258+
execute "func F3()\n\n\n\nreturn\nendfunc"
259+
" F4
260+
execute "func F4()\n
261+
\\n
262+
\\n
263+
\\n
264+
\return\n
265+
\endfunc"
266+
endfunc
267+
" F5
268+
execute "func F5()\n\n\n\nreturn\nendfunc"
269+
" F6
270+
execute "func F6()\n
271+
\\n
272+
\\n
273+
\\n
274+
\return\n
275+
\endfunc"
276+
call F1()
277+
verbose func F1
278+
verbose func F2
279+
verbose func F3
280+
verbose func F4
281+
verbose func F5
282+
verbose func F6
283+
qall!
284+
[CODE]
285+
286+
call writefile(lines, 'Xtest.vim')
287+
let res = system(GetVimCommandClean() .. ' -es -X -S Xtest.vim')
288+
call assert_equal(0, v:shell_error)
289+
290+
let m = matchstr(res, 'function F1()[^[:print:]]*[[:print:]]*')
291+
call assert_match(' line 2$', m)
292+
293+
let m = matchstr(res, 'function F2()[^[:print:]]*[[:print:]]*')
294+
call assert_match(' line 4$', m)
295+
296+
let m = matchstr(res, 'function F3()[^[:print:]]*[[:print:]]*')
297+
call assert_match(' line 11$', m)
298+
299+
let m = matchstr(res, 'function F4()[^[:print:]]*[[:print:]]*')
300+
call assert_match(' line 13$', m)
301+
302+
let m = matchstr(res, 'function F5()[^[:print:]]*[[:print:]]*')
303+
call assert_match(' line 21$', m)
304+
305+
let m = matchstr(res, 'function F6()[^[:print:]]*[[:print:]]*')
306+
call assert_match(' line 23$', m)
307+
308+
call delete('Xtest.vim')
309+
endfunc
310+
311+
" Test for defining a function reference in the global scope
312+
func Test_add_funcref_to_global_scope()
313+
let x = g:
314+
let caught_E862 = 0
315+
try
316+
func x.Xfunc()
317+
return 1
318+
endfunc
319+
catch /E862:/
320+
let caught_E862 = 1
321+
endtry
322+
call assert_equal(1, caught_E862)
323+
endfunc
324+
325+
func Test_funccall_garbage_collect()
326+
func Func(x, ...)
327+
call add(a:x, a:000)
328+
endfunc
329+
call Func([], [])
330+
" Must not crash cause by invalid freeing
331+
call test_garbagecollect_now()
332+
call assert_true(v:true)
333+
delfunc Func
334+
endfunc
335+
336+
" Test for script-local function
337+
func <SID>DoLast()
338+
call append(line('$'), "last line")
339+
endfunc
340+
341+
func s:DoNothing()
342+
call append(line('$'), "nothing line")
343+
endfunc
344+
345+
func Test_script_local_func()
346+
set nocp nomore viminfo+=nviminfo
347+
new
348+
nnoremap <buffer> _x :call <SID>DoNothing()<bar>call <SID>DoLast()<bar>delfunc <SID>DoNothing<bar>delfunc <SID>DoLast<cr>
349+
350+
normal _x
351+
call assert_equal('nothing line', getline(2))
352+
call assert_equal('last line', getline(3))
353+
close!
354+
355+
" Try to call a script local function in global scope
356+
let lines =<< trim [CODE]
357+
:call assert_fails('call s:Xfunc()', 'E81:')
358+
:call assert_fails('let x = call("<SID>Xfunc", [])', 'E120:')
359+
:call writefile(v:errors, 'Xresult')
360+
:qall
361+
362+
[CODE]
363+
call writefile(lines, 'Xscript')
364+
if RunVim([], [], '-s Xscript')
365+
call assert_equal([], readfile('Xresult'))
366+
endif
367+
call delete('Xresult')
368+
call delete('Xscript')
369+
endfunc
370+
371+
" Test for errors in defining new functions
372+
func Test_func_def_error()
373+
call assert_fails('func Xfunc abc ()', 'E124:')
374+
call assert_fails('func Xfunc(', 'E125:')
375+
call assert_fails('func xfunc()', 'E128:')
376+
377+
" Try to redefine a function that is in use
378+
let caught_E127 = 0
379+
try
380+
func! Test_func_def_error()
381+
endfunc
382+
catch /E127:/
383+
let caught_E127 = 1
384+
endtry
385+
call assert_equal(1, caught_E127)
386+
387+
" Try to define a function in a dict twice
388+
let d = {}
389+
let lines =<< trim END
390+
func d.F1()
391+
return 1
392+
endfunc
393+
END
394+
let l = join(lines, "\n") . "\n"
395+
exe l
396+
call assert_fails('exe l', 'E717:')
397+
398+
" Define an autoload function with an incorrect file name
399+
call writefile(['func foo#Bar()', 'return 1', 'endfunc'], 'Xscript')
400+
call assert_fails('source Xscript', 'E746:')
401+
call delete('Xscript')
402+
endfunc
403+
404+
" Test for deleting a function
405+
func Test_del_func()
406+
call assert_fails('delfunction Xabc', 'E130:')
407+
let d = {'a' : 10}
408+
call assert_fails('delfunc d.a', 'E718:')
409+
endfunc
410+
411+
" Test for calling return outside of a function
412+
func Test_return_outside_func()
413+
call writefile(['return 10'], 'Xscript')
414+
call assert_fails('source Xscript', 'E133:')
415+
call delete('Xscript')
416+
endfunc
417+
418+
" Test for errors in calling a function
419+
func Test_func_arg_error()
420+
" Too many arguments
421+
call assert_fails("call call('min', range(1,20))", 'E118:')
422+
call assert_fails("call call('min', range(1,21))", 'E699:')
423+
call assert_fails('echo min(0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,0,1)',
424+
\ 'E740:')
425+
426+
" Missing dict argument
427+
func Xfunc() dict
428+
return 1
429+
endfunc
430+
call assert_fails('call Xfunc()', 'E725:')
431+
delfunc Xfunc
432+
endfunc
433+
178434
" vim: shiftwidth=2 sts=2 expandtab

0 commit comments

Comments
 (0)