|
3 | 3 | " Also test that a builtin function cannot be replaced. |
4 | 4 | " Also test for regression when calling arbitrary expression. |
5 | 5 |
|
| 6 | +source check.vim |
| 7 | +source shared.vim |
| 8 | + |
6 | 9 | func Table(title, ...) |
7 | 10 | let ret = a:title |
8 | 11 | let idx = 1 |
@@ -83,6 +86,7 @@ func Test_user_func() |
83 | 86 | normal o[(one again |
84 | 87 | call assert_equal('1. one again', getline('.')) |
85 | 88 |
|
| 89 | + " Try to overwrite a function in the global (g:) scope |
86 | 90 | call assert_equal(3, max([1, 2, 3])) |
87 | 91 | call assert_fails("call extend(g:, {'max': function('min')})", 'E704') |
88 | 92 | call assert_equal(3, max([1, 2, 3])) |
@@ -175,4 +179,256 @@ func Test_function_list() |
175 | 179 | call assert_fails("function Xabc", 'E123:') |
176 | 180 | endfunc |
177 | 181 |
|
| 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 | + |
178 | 434 | " vim: shiftwidth=2 sts=2 expandtab |
0 commit comments