test_eval_stuff.vim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. " Tests for various eval things.
  2. source view_util.vim
  3. source shared.vim
  4. function s:foo() abort
  5. try
  6. return [] == 0
  7. catch
  8. return 1
  9. endtry
  10. endfunction
  11. func Test_catch_return_with_error()
  12. call assert_equal(1, s:foo())
  13. endfunc
  14. func Test_nocatch_restore_silent_emsg()
  15. silent! try
  16. throw 1
  17. catch
  18. endtry
  19. echoerr 'wrong again'
  20. call assert_equal('wrong again', ScreenLine(&lines))
  21. endfunc
  22. func Test_mkdir_p()
  23. call mkdir('Xmkdir/nested', 'p')
  24. call assert_true(isdirectory('Xmkdir/nested'))
  25. try
  26. " Trying to make existing directories doesn't error
  27. call mkdir('Xmkdir', 'p')
  28. call mkdir('Xmkdir/nested', 'p')
  29. catch /E739:/
  30. call assert_report('mkdir(..., "p") failed for an existing directory')
  31. endtry
  32. " 'p' doesn't suppress real errors
  33. call writefile([], 'Xfile')
  34. call assert_fails('call mkdir("Xfile", "p")', 'E739:')
  35. call delete('Xfile')
  36. call delete('Xmkdir', 'rf')
  37. call assert_equal(0, mkdir(v:_null_string))
  38. call assert_fails('call mkdir([])', 'E730:')
  39. call assert_fails('call mkdir("abc", [], [])', 'E745:')
  40. endfunc
  41. func DoMkdirDel(name)
  42. call mkdir(a:name, 'pD')
  43. call assert_true(isdirectory(a:name))
  44. endfunc
  45. func DoMkdirDelAddFile(name)
  46. call mkdir(a:name, 'pD')
  47. call assert_true(isdirectory(a:name))
  48. call writefile(['text'], a:name .. '/file')
  49. endfunc
  50. func DoMkdirDelRec(name)
  51. call mkdir(a:name, 'pR')
  52. call assert_true(isdirectory(a:name))
  53. endfunc
  54. func DoMkdirDelRecAddFile(name)
  55. call mkdir(a:name, 'pR')
  56. call assert_true(isdirectory(a:name))
  57. call writefile(['text'], a:name .. '/file')
  58. endfunc
  59. func Test_mkdir_defer_del()
  60. " Xtopdir/tmp is created thus deleted, not Xtopdir itself
  61. call mkdir('Xtopdir', 'R')
  62. call DoMkdirDel('Xtopdir/tmp')
  63. call assert_true(isdirectory('Xtopdir'))
  64. call assert_false(isdirectory('Xtopdir/tmp'))
  65. " Deletion fails because "tmp" contains "sub"
  66. call DoMkdirDel('Xtopdir/tmp/sub')
  67. call assert_true(isdirectory('Xtopdir'))
  68. call assert_true(isdirectory('Xtopdir/tmp'))
  69. call delete('Xtopdir/tmp', 'rf')
  70. " Deletion fails because "tmp" contains "file"
  71. call DoMkdirDelAddFile('Xtopdir/tmp')
  72. call assert_true(isdirectory('Xtopdir'))
  73. call assert_true(isdirectory('Xtopdir/tmp'))
  74. call assert_true(filereadable('Xtopdir/tmp/file'))
  75. call delete('Xtopdir/tmp', 'rf')
  76. " Xtopdir/tmp is created thus deleted, not Xtopdir itself
  77. call DoMkdirDelRec('Xtopdir/tmp')
  78. call assert_true(isdirectory('Xtopdir'))
  79. call assert_false(isdirectory('Xtopdir/tmp'))
  80. " Deletion works even though "tmp" contains "sub"
  81. call DoMkdirDelRec('Xtopdir/tmp/sub')
  82. call assert_true(isdirectory('Xtopdir'))
  83. call assert_false(isdirectory('Xtopdir/tmp'))
  84. " Deletion works even though "tmp" contains "file"
  85. call DoMkdirDelRecAddFile('Xtopdir/tmp')
  86. call assert_true(isdirectory('Xtopdir'))
  87. call assert_false(isdirectory('Xtopdir/tmp'))
  88. endfunc
  89. func Test_line_continuation()
  90. let array = [5,
  91. "\ ignore this
  92. \ 6,
  93. "\ more to ignore
  94. "\ more moreto ignore
  95. \ ]
  96. "\ and some more
  97. call assert_equal([5, 6], array)
  98. endfunc
  99. func Test_E963()
  100. " These commands used to cause an internal error prior to vim 8.1.0563
  101. let v_e = v:errors
  102. let v_o = v:oldfiles
  103. call assert_fails("let v:errors=''", 'E963:')
  104. call assert_equal(v_e, v:errors)
  105. call assert_fails("let v:oldfiles=''", 'E963:')
  106. call assert_equal(v_o, v:oldfiles)
  107. endfunc
  108. func Test_for_invalid()
  109. call assert_fails("for x in 99", 'E1098:')
  110. call assert_fails("for x in function('winnr')", 'E1098:')
  111. call assert_fails("for x in {'a': 9}", 'E1098:')
  112. if 0
  113. /1/5/2/s/\n
  114. endif
  115. redraw
  116. endfunc
  117. func Test_for_over_null_string()
  118. let save_enc = &enc
  119. " set enc=iso8859
  120. let cnt = 0
  121. for c in v:_null_string
  122. let cnt += 1
  123. endfor
  124. call assert_equal(0, cnt)
  125. let &enc = save_enc
  126. endfunc
  127. func Test_for_with_modifier()
  128. " this checks has_loop_cmd() works with a modifier
  129. let result = []
  130. horizontal for i in range(3)
  131. call extend(result, [i])
  132. endfor
  133. call assert_equal([0, 1, 2], result)
  134. endfunc
  135. func Test_for_invalid_line_count()
  136. let lines =<< trim END
  137. 111111111111111111111111 for line in ['one']
  138. endfor
  139. END
  140. call writefile(lines, 'XinvalidFor')
  141. " only test that this doesn't crash
  142. call RunVim([], [], '-u NONE -e -s -S XinvalidFor -c qa')
  143. call delete('XinvalidFor')
  144. endfunc
  145. func Test_readfile_binary()
  146. new
  147. call setline(1, ['one', 'two', 'three'])
  148. setlocal ff=dos
  149. silent write XReadfile_bin
  150. let lines = 'XReadfile_bin'->readfile()
  151. call assert_equal(['one', 'two', 'three'], lines)
  152. let lines = readfile('XReadfile_bin', '', 2)
  153. call assert_equal(['one', 'two'], lines)
  154. let lines = readfile('XReadfile_bin', 'b')
  155. call assert_equal(["one\r", "two\r", "three\r", ""], lines)
  156. let lines = readfile('XReadfile_bin', 'b', 2)
  157. call assert_equal(["one\r", "two\r"], lines)
  158. bwipe!
  159. call delete('XReadfile_bin')
  160. endfunc
  161. func Test_readfile_binary_empty()
  162. call writefile([], 'Xempty-file')
  163. " This used to compare uninitialized memory in Vim <= 8.2.4065
  164. call assert_equal([''], readfile('Xempty-file', 'b'))
  165. call delete('Xempty-file')
  166. endfunc
  167. func Test_readfile_bom()
  168. call writefile(["\ufeffFOO", "FOO\ufeffBAR"], 'XReadfile_bom')
  169. call assert_equal(['FOO', 'FOOBAR'], readfile('XReadfile_bom'))
  170. call delete('XReadfile_bom')
  171. endfunc
  172. func Test_readfile_max()
  173. call writefile(range(1, 4), 'XReadfile_max')
  174. call assert_equal(['1', '2'], readfile('XReadfile_max', '', 2))
  175. call assert_equal(['3', '4'], readfile('XReadfile_max', '', -2))
  176. call delete('XReadfile_max')
  177. endfunc
  178. func Test_let_errmsg()
  179. call assert_fails('let v:errmsg = []', 'E730:')
  180. let v:errmsg = ''
  181. call assert_fails('let v:errmsg = []', 'E730:')
  182. let v:errmsg = ''
  183. endfunc
  184. func Test_string_concatenation()
  185. call assert_equal('ab', 'a'.'b')
  186. call assert_equal('ab', 'a' .'b')
  187. call assert_equal('ab', 'a'. 'b')
  188. call assert_equal('ab', 'a' . 'b')
  189. call assert_equal('ab', 'a'..'b')
  190. call assert_equal('ab', 'a' ..'b')
  191. call assert_equal('ab', 'a'.. 'b')
  192. call assert_equal('ab', 'a' .. 'b')
  193. let a = 'a'
  194. let b = 'b'
  195. let a .= b
  196. call assert_equal('ab', a)
  197. let a = 'a'
  198. let a.=b
  199. call assert_equal('ab', a)
  200. let a = 'a'
  201. let a ..= b
  202. call assert_equal('ab', a)
  203. let a = 'a'
  204. let a..=b
  205. call assert_equal('ab', a)
  206. if has('float')
  207. let a = 'A'
  208. let b = 1.234
  209. call assert_equal('A1.234', a .. b)
  210. endif
  211. endfunc
  212. " Test fix for issue #4507
  213. func Test_skip_after_throw()
  214. try
  215. throw 'something'
  216. let x = wincol() || &ts
  217. catch /something/
  218. endtry
  219. endfunc
  220. " scriptversion 1
  221. func Test_string_concat_scriptversion1()
  222. call assert_true(has('vimscript-1'))
  223. let a = 'a'
  224. let b = 'b'
  225. echo a . b
  226. let a .= b
  227. let vers = 1.2.3
  228. call assert_equal('123', vers)
  229. if has('float')
  230. call assert_fails('let f = .5', 'E15:')
  231. endif
  232. endfunc
  233. " scriptversion 1
  234. func Test_vvar_scriptversion1()
  235. call assert_equal(15, 017)
  236. call assert_equal(15, 0o17)
  237. call assert_equal(15, 0O17)
  238. call assert_equal(18, 018)
  239. call assert_equal(511, 0o777)
  240. endfunc
  241. func Test_execute_cmd_with_null()
  242. call assert_fails('execute v:_null_list', 'E730:')
  243. call assert_fails('execute v:_null_dict', 'E731:')
  244. call assert_fails('execute v:_null_blob', 'E976:')
  245. execute v:_null_string
  246. " Nvim doesn't have null partials
  247. " call assert_fails('execute test_null_partial()', 'E729:')
  248. " Nvim doesn't have test_unknown()
  249. " call assert_fails('execute test_unknown()', 'E908:')
  250. if has('job')
  251. call assert_fails('execute test_null_job()', 'E908:')
  252. call assert_fails('execute test_null_channel()', 'E908:')
  253. endif
  254. endfunc
  255. func Test_number_max_min_size()
  256. " This will fail on systems without 64 bit number support or when not
  257. " configured correctly.
  258. call assert_equal(64, v:numbersize)
  259. call assert_true(v:numbermin < -9999999)
  260. call assert_true(v:numbermax > 9999999)
  261. endfunc
  262. func Assert_reg(name, type, value, valuestr, expr, exprstr)
  263. call assert_equal(a:type, getregtype(a:name))
  264. call assert_equal(a:value, getreg(a:name))
  265. call assert_equal(a:valuestr, string(getreg(a:name, 0, 1)))
  266. call assert_equal(a:expr, getreg(a:name, 1))
  267. call assert_equal(a:exprstr, string(getreg(a:name, 1, 1)))
  268. endfunc
  269. func Test_let_register()
  270. let @" = 'abc'
  271. call Assert_reg('"', 'v', "abc", "['abc']", "abc", "['abc']")
  272. let @" = "abc\n"
  273. call Assert_reg('"', 'V', "abc\n", "['abc']", "abc\n", "['abc']")
  274. let @" = "abc\<C-m>"
  275. call Assert_reg('"', 'V', "abc\r\n", "['abc\r']", "abc\r\n", "['abc\r']")
  276. let @= = '"abc"'
  277. call Assert_reg('=', 'v', "abc", "['abc']", '"abc"', "['\"abc\"']")
  278. endfunc
  279. func Assert_regput(name, result)
  280. new
  281. execute "silent normal! o==\n==\e\"" . a:name . "P"
  282. call assert_equal(a:result, getline(2, line('$')))
  283. bwipe!
  284. endfunc
  285. func Test_setreg_basic()
  286. call setreg('a', 'abcA', 'c')
  287. call Assert_reg('a', 'v', "abcA", "['abcA']", "abcA", "['abcA']")
  288. call Assert_regput('a', ['==', '=abcA='])
  289. call setreg('A', 'abcAc', 'c')
  290. call Assert_reg('A', 'v', "abcAabcAc", "['abcAabcAc']", "abcAabcAc", "['abcAabcAc']")
  291. call Assert_regput('a', ['==', '=abcAabcAc='])
  292. call setreg('A', 'abcAl', 'l')
  293. call Assert_reg('A', 'V', "abcAabcAcabcAl\n", "['abcAabcAcabcAl']", "abcAabcAcabcAl\n", "['abcAabcAcabcAl']")
  294. call Assert_regput('a', ['==', 'abcAabcAcabcAl', '=='])
  295. call setreg('A', 'abcAc2','c')
  296. call Assert_reg('A', 'v', "abcAabcAcabcAl\nabcAc2", "['abcAabcAcabcAl', 'abcAc2']", "abcAabcAcabcAl\nabcAc2", "['abcAabcAcabcAl', 'abcAc2']")
  297. call Assert_regput('a', ['==', '=abcAabcAcabcAl', 'abcAc2='])
  298. call setreg('b', 'abcB', 'v')
  299. call Assert_reg('b', 'v', "abcB", "['abcB']", "abcB", "['abcB']")
  300. call Assert_regput('b', ['==', '=abcB='])
  301. call setreg('b', 'abcBc', 'ca')
  302. call Assert_reg('b', 'v', "abcBabcBc", "['abcBabcBc']", "abcBabcBc", "['abcBabcBc']")
  303. call Assert_regput('b', ['==', '=abcBabcBc='])
  304. call setreg('b', 'abcBb', 'ba')
  305. call Assert_reg('b', "\<C-V>5", "abcBabcBcabcBb", "['abcBabcBcabcBb']", "abcBabcBcabcBb", "['abcBabcBcabcBb']")
  306. call Assert_regput('b', ['==', '=abcBabcBcabcBb='])
  307. call setreg('b', 'abcBc2','ca')
  308. call Assert_reg('b', "v", "abcBabcBcabcBb\nabcBc2", "['abcBabcBcabcBb', 'abcBc2']", "abcBabcBcabcBb\nabcBc2", "['abcBabcBcabcBb', 'abcBc2']")
  309. call Assert_regput('b', ['==', '=abcBabcBcabcBb', 'abcBc2='])
  310. call setreg('b', 'abcBb2','b50a')
  311. call Assert_reg('b', "\<C-V>50", "abcBabcBcabcBb\nabcBc2abcBb2", "['abcBabcBcabcBb', 'abcBc2abcBb2']", "abcBabcBcabcBb\nabcBc2abcBb2", "['abcBabcBcabcBb', 'abcBc2abcBb2']")
  312. call Assert_regput('b', ['==', '=abcBabcBcabcBb =', ' abcBc2abcBb2'])
  313. call setreg('c', 'abcC', 'l')
  314. call Assert_reg('c', 'V', "abcC\n", "['abcC']", "abcC\n", "['abcC']")
  315. call Assert_regput('c', ['==', 'abcC', '=='])
  316. call setreg('C', 'abcCl', 'l')
  317. call Assert_reg('C', 'V', "abcC\nabcCl\n", "['abcC', 'abcCl']", "abcC\nabcCl\n", "['abcC', 'abcCl']")
  318. call Assert_regput('c', ['==', 'abcC', 'abcCl', '=='])
  319. call setreg('C', 'abcCc', 'c')
  320. call Assert_reg('C', 'v', "abcC\nabcCl\nabcCc", "['abcC', 'abcCl', 'abcCc']", "abcC\nabcCl\nabcCc", "['abcC', 'abcCl', 'abcCc']")
  321. call Assert_regput('c', ['==', '=abcC', 'abcCl', 'abcCc='])
  322. call setreg('d', 'abcD', 'V')
  323. call Assert_reg('d', 'V', "abcD\n", "['abcD']", "abcD\n", "['abcD']")
  324. call Assert_regput('d', ['==', 'abcD', '=='])
  325. call setreg('D', 'abcDb', 'b')
  326. call Assert_reg('d', "\<C-V>5", "abcD\nabcDb", "['abcD', 'abcDb']", "abcD\nabcDb", "['abcD', 'abcDb']")
  327. call Assert_regput('d', ['==', '=abcD =', ' abcDb'])
  328. call setreg('e', 'abcE', 'b')
  329. call Assert_reg('e', "\<C-V>4", "abcE", "['abcE']", "abcE", "['abcE']")
  330. call Assert_regput('e', ['==', '=abcE='])
  331. call setreg('E', 'abcEb', 'b')
  332. call Assert_reg('E', "\<C-V>5", "abcE\nabcEb", "['abcE', 'abcEb']", "abcE\nabcEb", "['abcE', 'abcEb']")
  333. call Assert_regput('e', ['==', '=abcE =', ' abcEb'])
  334. call setreg('E', 'abcEl', 'l')
  335. call Assert_reg('E', "V", "abcE\nabcEb\nabcEl\n", "['abcE', 'abcEb', 'abcEl']", "abcE\nabcEb\nabcEl\n", "['abcE', 'abcEb', 'abcEl']")
  336. call Assert_regput('e', ['==', 'abcE', 'abcEb', 'abcEl', '=='])
  337. call setreg('f', 'abcF', "\<C-v>")
  338. call Assert_reg('f', "\<C-V>4", "abcF", "['abcF']", "abcF", "['abcF']")
  339. call Assert_regput('f', ['==', '=abcF='])
  340. call setreg('F', 'abcFc', 'c')
  341. call Assert_reg('F', "v", "abcF\nabcFc", "['abcF', 'abcFc']", "abcF\nabcFc", "['abcF', 'abcFc']")
  342. call Assert_regput('f', ['==', '=abcF', 'abcFc='])
  343. call setreg('g', 'abcG', 'b10')
  344. call Assert_reg('g', "\<C-V>10", "abcG", "['abcG']", "abcG", "['abcG']")
  345. call Assert_regput('g', ['==', '=abcG ='])
  346. call setreg('h', 'abcH', "\<C-v>10")
  347. call Assert_reg('h', "\<C-V>10", "abcH", "['abcH']", "abcH", "['abcH']")
  348. call Assert_regput('h', ['==', '=abcH ='])
  349. call setreg('I', 'abcI')
  350. call Assert_reg('I', "v", "abcI", "['abcI']", "abcI", "['abcI']")
  351. call Assert_regput('I', ['==', '=abcI='])
  352. " Error cases
  353. call assert_fails('call setreg()', 'E119:')
  354. call assert_fails('call setreg(1)', 'E119:')
  355. call assert_fails('call setreg(1, 2, 3, 4)', 'E118:')
  356. call assert_fails('call setreg([], 2)', 'E730:')
  357. call assert_fails('call setreg(1, 2, [])', 'E730:')
  358. call assert_fails('call setreg("/", ["1", "2"])', 'E883:')
  359. call assert_fails('call setreg("=", ["1", "2"])', 'E883:')
  360. call assert_fails('call setreg(1, ["", "", [], ""])', 'E730:')
  361. endfunc
  362. func Test_curly_assignment()
  363. let s:svar = 'svar'
  364. let g:gvar = 'gvar'
  365. let lname = 'gvar'
  366. let gname = 'gvar'
  367. let {'s:'.lname} = {'g:'.gname}
  368. call assert_equal('gvar', s:gvar)
  369. let s:gvar = ''
  370. let { 's:'.lname } = { 'g:'.gname }
  371. call assert_equal('gvar', s:gvar)
  372. let s:gvar = ''
  373. let { 's:' . lname } = { 'g:' . gname }
  374. call assert_equal('gvar', s:gvar)
  375. let s:gvar = ''
  376. let { 's:' .. lname } = { 'g:' .. gname }
  377. call assert_equal('gvar', s:gvar)
  378. unlet s:svar
  379. unlet s:gvar
  380. unlet g:gvar
  381. endfunc
  382. func Test_deep_recursion()
  383. " this was running out of stack
  384. call assert_fails("exe 'if ' .. repeat('(', 1002)", 'E1169: Expression too recursive: ((')
  385. endfunc
  386. " K_SPECIAL in the modified character used be escaped, which causes
  387. " double-escaping with feedkeys() or as the return value of an <expr> mapping,
  388. " and doesn't match what getchar() returns,
  389. func Test_modified_char_no_escape_special()
  390. nnoremap <M-…> <Cmd>let g:got_m_ellipsis += 1<CR>
  391. call feedkeys("\<M-…>", 't')
  392. call assert_equal("\<M-…>", getchar())
  393. let g:got_m_ellipsis = 0
  394. call feedkeys("\<M-…>", 'xt')
  395. call assert_equal(1, g:got_m_ellipsis)
  396. func Func()
  397. return "\<M-…>"
  398. endfunc
  399. nmap <expr> <F2> Func()
  400. call feedkeys("\<F2>", 'xt')
  401. call assert_equal(2, g:got_m_ellipsis)
  402. delfunc Func
  403. nunmap <F2>
  404. unlet g:got_m_ellipsis
  405. nunmap <M-…>
  406. endfunc
  407. func Test_eval_string_in_special_key()
  408. " this was using the '{' inside <> as the start of an interpolated string
  409. silent! echo 0{1-$"\<S--{>n|nö%
  410. endfunc
  411. " vim: shiftwidth=2 sts=2 expandtab