eval_spec.lua 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. -- Test for various eval features.
  2. local t = require('test.testutil')
  3. local n = require('test.functional.testnvim')()
  4. local assert_alive = n.assert_alive
  5. local feed, insert, source = n.feed, n.insert, n.source
  6. local clear, command, expect = n.clear, n.command, n.expect
  7. local eq, eval, write_file = t.eq, n.eval, t.write_file
  8. local poke_eventloop = n.poke_eventloop
  9. local exc_exec = n.exc_exec
  10. local dedent = t.dedent
  11. describe('eval', function()
  12. setup(function()
  13. write_file(
  14. 'test_eval_setup.vim',
  15. [[
  16. set noswapfile
  17. lang C
  18. fun AppendRegContents(reg)
  19. call AppendRegParts(a:reg, getregtype(a:reg), getreg(a:reg), string(getreg(a:reg, 0, 1)), getreg(a:reg, 1), string(getreg(a:reg, 1, 1)))
  20. endfun
  21. fun AppendRegParts(reg, type, cont, strcont, cont1, strcont1)
  22. call append('$', printf('%s: type %s; value: %s (%s), expr: %s (%s)', a:reg, a:type, a:cont, a:strcont, a:cont1, a:strcont1))
  23. endfun
  24. command -nargs=? AR :call AppendRegContents(<q-args>)
  25. fun SetReg(...)
  26. call call('setreg', a:000)
  27. call append('$', printf('{{{2 setreg(%s)', string(a:000)[1:-2]))
  28. call AppendRegContents(a:1)
  29. if a:1 isnot# '='
  30. execute "silent normal! Go==\n==\e\"".a:1."P"
  31. endif
  32. endfun
  33. ]]
  34. )
  35. end)
  36. before_each(clear)
  37. teardown(function()
  38. os.remove('test_eval_setup.vim')
  39. end)
  40. it(':let', function()
  41. command('so test_eval_setup.vim')
  42. command([[let @" = 'abc']])
  43. command('AR "')
  44. command([[let @" = "abc\n"]])
  45. source('AR "')
  46. command([[let @" = "abc\<C-m>"]])
  47. command('AR "')
  48. command([[let @= = '"abc"']])
  49. command('AR =')
  50. expect([[
  51. ": type v; value: abc (['abc']), expr: abc (['abc'])
  52. ": type V; value: abc]] .. "\000 (['abc']), expr: abc\000" .. [[ (['abc'])
  53. ": type V; value: abc]] .. "\r\000 (['abc\r']), expr: abc\r\000 (['abc\r" .. [['])
  54. =: type v; value: abc (['abc']), expr: "abc" (['"abc"'])]])
  55. end)
  56. it('basic setreg() tests', function()
  57. command('so test_eval_setup.vim')
  58. insert('{{{1 Basic setreg tests')
  59. command([[call SetReg('a', 'abcA', 'c')]])
  60. command([[call SetReg('b', 'abcB', 'v')]])
  61. command([[call SetReg('c', 'abcC', 'l')]])
  62. command([[call SetReg('d', 'abcD', 'V')]])
  63. command([[call SetReg('e', 'abcE', 'b')]])
  64. command([[call SetReg('f', 'abcF', "\<C-v>")]])
  65. command([[call SetReg('g', 'abcG', 'b10')]])
  66. command([[call SetReg('h', 'abcH', "\<C-v>10")]])
  67. command([[call SetReg('I', 'abcI')]])
  68. feed('Go{{{1 Appending single lines with setreg()<esc>')
  69. poke_eventloop()
  70. command([[call SetReg('A', 'abcAc', 'c')]])
  71. command([[call SetReg('A', 'abcAl', 'l')]])
  72. command([[call SetReg('A', 'abcAc2','c')]])
  73. command([[call SetReg('b', 'abcBc', 'ca')]])
  74. command([[call SetReg('b', 'abcBb', 'ba')]])
  75. command([[call SetReg('b', 'abcBc2','ca')]])
  76. command([[call SetReg('b', 'abcBb2','b50a')]])
  77. command([[call SetReg('C', 'abcCl', 'l')]])
  78. command([[call SetReg('C', 'abcCc', 'c')]])
  79. command([[call SetReg('D', 'abcDb', 'b')]])
  80. command([[call SetReg('E', 'abcEb', 'b')]])
  81. command([[call SetReg('E', 'abcEl', 'l')]])
  82. command([[call SetReg('F', 'abcFc', 'c')]])
  83. expect([[
  84. {{{1 Basic setreg tests
  85. {{{2 setreg('a', 'abcA', 'c')
  86. a: type v; value: abcA (['abcA']), expr: abcA (['abcA'])
  87. ==
  88. =abcA=
  89. {{{2 setreg('b', 'abcB', 'v')
  90. b: type v; value: abcB (['abcB']), expr: abcB (['abcB'])
  91. ==
  92. =abcB=
  93. {{{2 setreg('c', 'abcC', 'l')
  94. c: type V; value: abcC]] .. "\000 (['abcC']), expr: abcC\000" .. [[ (['abcC'])
  95. ==
  96. abcC
  97. ==
  98. {{{2 setreg('d', 'abcD', 'V')
  99. d: type V; value: abcD]] .. "\000 (['abcD']), expr: abcD\000" .. [[ (['abcD'])
  100. ==
  101. abcD
  102. ==
  103. {{{2 setreg('e', 'abcE', 'b')
  104. e: type ]] .. '\022' .. [[4; value: abcE (['abcE']), expr: abcE (['abcE'])
  105. ==
  106. =abcE=
  107. {{{2 setreg('f', 'abcF', ']] .. '\022' .. [[')
  108. f: type ]] .. '\022' .. [[4; value: abcF (['abcF']), expr: abcF (['abcF'])
  109. ==
  110. =abcF=
  111. {{{2 setreg('g', 'abcG', 'b10')
  112. g: type ]] .. '\022' .. [[10; value: abcG (['abcG']), expr: abcG (['abcG'])
  113. ==
  114. =abcG =
  115. {{{2 setreg('h', 'abcH', ']] .. '\022' .. [[10')
  116. h: type ]] .. '\022' .. [[10; value: abcH (['abcH']), expr: abcH (['abcH'])
  117. ==
  118. =abcH =
  119. {{{2 setreg('I', 'abcI')
  120. I: type v; value: abcI (['abcI']), expr: abcI (['abcI'])
  121. ==
  122. =abcI=
  123. {{{1 Appending single lines with setreg()
  124. {{{2 setreg('A', 'abcAc', 'c')
  125. A: type v; value: abcAabcAc (['abcAabcAc']), expr: abcAabcAc (['abcAabcAc'])
  126. ==
  127. =abcAabcAc=
  128. {{{2 setreg('A', 'abcAl', 'l')
  129. A: type V; value: abcAabcAcabcAl]] .. "\000 (['abcAabcAcabcAl']), expr: abcAabcAcabcAl\000" .. [[ (['abcAabcAcabcAl'])
  130. ==
  131. abcAabcAcabcAl
  132. ==
  133. {{{2 setreg('A', 'abcAc2', 'c')
  134. A: type v; value: abcAabcAcabcAl]] .. "\000abcAc2 (['abcAabcAcabcAl', 'abcAc2']), expr: abcAabcAcabcAl\000" .. [[abcAc2 (['abcAabcAcabcAl', 'abcAc2'])
  135. ==
  136. =abcAabcAcabcAl
  137. abcAc2=
  138. {{{2 setreg('b', 'abcBc', 'ca')
  139. b: type v; value: abcBabcBc (['abcBabcBc']), expr: abcBabcBc (['abcBabcBc'])
  140. ==
  141. =abcBabcBc=
  142. {{{2 setreg('b', 'abcBb', 'ba')
  143. b: type ]] .. '\022' .. [[5; value: abcBabcBcabcBb (['abcBabcBcabcBb']), expr: abcBabcBcabcBb (['abcBabcBcabcBb'])
  144. ==
  145. =abcBabcBcabcBb=
  146. {{{2 setreg('b', 'abcBc2', 'ca')
  147. b: type v; value: abcBabcBcabcBb]] .. "\000abcBc2 (['abcBabcBcabcBb', 'abcBc2']), expr: abcBabcBcabcBb\000" .. [[abcBc2 (['abcBabcBcabcBb', 'abcBc2'])
  148. ==
  149. =abcBabcBcabcBb
  150. abcBc2=
  151. {{{2 setreg('b', 'abcBb2', 'b50a')
  152. b: type ]] .. "\02250; value: abcBabcBcabcBb\000abcBc2abcBb2 (['abcBabcBcabcBb', 'abcBc2abcBb2']), expr: abcBabcBcabcBb\000" .. [[abcBc2abcBb2 (['abcBabcBcabcBb', 'abcBc2abcBb2'])
  153. ==
  154. =abcBabcBcabcBb =
  155. abcBc2abcBb2
  156. {{{2 setreg('C', 'abcCl', 'l')
  157. C: type V; value: abcC]] .. "\000abcCl\000 (['abcC', 'abcCl']), expr: abcC\000abcCl\000" .. [[ (['abcC', 'abcCl'])
  158. ==
  159. abcC
  160. abcCl
  161. ==
  162. {{{2 setreg('C', 'abcCc', 'c')
  163. C: type v; value: abcC]] .. "\000abcCl\000abcCc (['abcC', 'abcCl', 'abcCc']), expr: abcC\000abcCl\000" .. [[abcCc (['abcC', 'abcCl', 'abcCc'])
  164. ==
  165. =abcC
  166. abcCl
  167. abcCc=
  168. {{{2 setreg('D', 'abcDb', 'b')
  169. D: type ]] .. "\0225; value: abcD\000abcDb (['abcD', 'abcDb']), expr: abcD\000" .. [[abcDb (['abcD', 'abcDb'])
  170. ==
  171. =abcD =
  172. abcDb
  173. {{{2 setreg('E', 'abcEb', 'b')
  174. E: type ]] .. "\0225; value: abcE\000abcEb (['abcE', 'abcEb']), expr: abcE\000" .. [[abcEb (['abcE', 'abcEb'])
  175. ==
  176. =abcE =
  177. abcEb
  178. {{{2 setreg('E', 'abcEl', 'l')
  179. E: type V; value: abcE]] .. "\000abcEb\000abcEl\000 (['abcE', 'abcEb', 'abcEl']), expr: abcE\000abcEb\000abcEl\000" .. [[ (['abcE', 'abcEb', 'abcEl'])
  180. ==
  181. abcE
  182. abcEb
  183. abcEl
  184. ==
  185. {{{2 setreg('F', 'abcFc', 'c')
  186. F: type v; value: abcF]] .. "\000abcFc (['abcF', 'abcFc']), expr: abcF\000" .. [[abcFc (['abcF', 'abcFc'])
  187. ==
  188. =abcF
  189. abcFc=]])
  190. end)
  191. it('appending NL with setreg()', function()
  192. command('so test_eval_setup.vim')
  193. command([[call setreg('a', 'abcA2', 'c')]])
  194. command([[call setreg('b', 'abcB2', 'v')]])
  195. command([[call setreg('c', 'abcC2', 'l')]])
  196. command([[call setreg('d', 'abcD2', 'V')]])
  197. command([[call setreg('e', 'abcE2', 'b')]])
  198. command([[call setreg('f', 'abcF2', "\<C-v>")]])
  199. -- These registers where set like this in the old test_eval.in but never
  200. -- copied to the output buffer with SetReg(). They do not appear in
  201. -- test_eval.ok. Therefore they are commented out.
  202. --execute([[call setreg('g', 'abcG2', 'b10')]])
  203. --execute([[call setreg('h', 'abcH2', "\<C-v>10")]])
  204. --execute([[call setreg('I', 'abcI2')]])
  205. command([[call SetReg('A', "\n")]])
  206. command([[call SetReg('B', "\n", 'c')]])
  207. command([[call SetReg('C', "\n")]])
  208. command([[call SetReg('D', "\n", 'l')]])
  209. command([[call SetReg('E', "\n")]])
  210. command([[call SetReg('F', "\n", 'b')]])
  211. expect([[
  212. {{{2 setreg('A', ']] .. '\000' .. [[')
  213. A: type V; value: abcA2]] .. "\000 (['abcA2']), expr: abcA2\000" .. [[ (['abcA2'])
  214. ==
  215. abcA2
  216. ==
  217. {{{2 setreg('B', ']] .. '\000' .. [[', 'c')
  218. B: type v; value: abcB2]] .. "\000 (['abcB2', '']), expr: abcB2\000" .. [[ (['abcB2', ''])
  219. ==
  220. =abcB2
  221. =
  222. {{{2 setreg('C', ']] .. '\000' .. [[')
  223. C: type V; value: abcC2]] .. "\000\000 (['abcC2', '']), expr: abcC2\000\000" .. [[ (['abcC2', ''])
  224. ==
  225. abcC2
  226. ==
  227. {{{2 setreg('D', ']] .. '\000' .. [[', 'l')
  228. D: type V; value: abcD2]] .. "\000\000 (['abcD2', '']), expr: abcD2\000\000" .. [[ (['abcD2', ''])
  229. ==
  230. abcD2
  231. ==
  232. {{{2 setreg('E', ']] .. '\000' .. [[')
  233. E: type V; value: abcE2]] .. "\000\000 (['abcE2', '']), expr: abcE2\000\000" .. [[ (['abcE2', ''])
  234. ==
  235. abcE2
  236. ==
  237. {{{2 setreg('F', ']] .. '\000' .. [[', 'b')
  238. F: type ]] .. "\0220; value: abcF2\000 (['abcF2', '']), expr: abcF2\000" .. [[ (['abcF2', ''])
  239. ==
  240. =abcF2=
  241. ]])
  242. end)
  243. it('setting and appending list with setreg()', function()
  244. command('so test_eval_setup.vim')
  245. command([[$put ='{{{1 Setting lists with setreg()']])
  246. command([=[call SetReg('a', ['abcA3'], 'c')]=])
  247. command([=[call SetReg('b', ['abcB3'], 'l')]=])
  248. command([=[call SetReg('c', ['abcC3'], 'b')]=])
  249. command([=[call SetReg('d', ['abcD3'])]=])
  250. command([=[call SetReg('e', [1, 2, 'abc', 3])]=])
  251. command([=[call SetReg('f', [1, 2, 3])]=])
  252. command([[$put ='{{{1 Appending lists with setreg()']])
  253. command([=[call SetReg('A', ['abcA3c'], 'c')]=])
  254. command([=[call SetReg('b', ['abcB3l'], 'la')]=])
  255. command([=[call SetReg('C', ['abcC3b'], 'lb')]=])
  256. command([=[call SetReg('D', ['abcD32'])]=])
  257. command([=[call SetReg('A', ['abcA32'])]=])
  258. command([=[call SetReg('B', ['abcB3c'], 'c')]=])
  259. command([=[call SetReg('C', ['abcC3l'], 'l')]=])
  260. command([=[call SetReg('D', ['abcD3b'], 'b')]=])
  261. expect([[
  262. {{{1 Setting lists with setreg()
  263. {{{2 setreg('a', ['abcA3'], 'c')
  264. a: type v; value: abcA3 (['abcA3']), expr: abcA3 (['abcA3'])
  265. ==
  266. =abcA3=
  267. {{{2 setreg('b', ['abcB3'], 'l')
  268. b: type V; value: abcB3]] .. "\000 (['abcB3']), expr: abcB3\000" .. [[ (['abcB3'])
  269. ==
  270. abcB3
  271. ==
  272. {{{2 setreg('c', ['abcC3'], 'b')
  273. c: type ]] .. '\022' .. [[5; value: abcC3 (['abcC3']), expr: abcC3 (['abcC3'])
  274. ==
  275. =abcC3=
  276. {{{2 setreg('d', ['abcD3'])
  277. d: type V; value: abcD3]] .. "\000 (['abcD3']), expr: abcD3\000" .. [[ (['abcD3'])
  278. ==
  279. abcD3
  280. ==
  281. {{{2 setreg('e', [1, 2, 'abc', 3])
  282. e: type V; value: 1]] .. "\0002\000abc\0003\000 (['1', '2', 'abc', '3']), expr: 1\0002\000abc\0003\000" .. [[ (['1', '2', 'abc', '3'])
  283. ==
  284. 1
  285. 2
  286. abc
  287. 3
  288. ==
  289. {{{2 setreg('f', [1, 2, 3])
  290. f: type V; value: 1]] .. "\0002\0003\000 (['1', '2', '3']), expr: 1\0002\0003\000" .. [[ (['1', '2', '3'])
  291. ==
  292. 1
  293. 2
  294. 3
  295. ==
  296. {{{1 Appending lists with setreg()
  297. {{{2 setreg('A', ['abcA3c'], 'c')
  298. A: type v; value: abcA3]] .. "\000abcA3c (['abcA3', 'abcA3c']), expr: abcA3\000" .. [[abcA3c (['abcA3', 'abcA3c'])
  299. ==
  300. =abcA3
  301. abcA3c=
  302. {{{2 setreg('b', ['abcB3l'], 'la')
  303. b: type V; value: abcB3]] .. "\000abcB3l\000 (['abcB3', 'abcB3l']), expr: abcB3\000abcB3l\000" .. [[ (['abcB3', 'abcB3l'])
  304. ==
  305. abcB3
  306. abcB3l
  307. ==
  308. {{{2 setreg('C', ['abcC3b'], 'lb')
  309. C: type ]] .. "\0226; value: abcC3\000abcC3b (['abcC3', 'abcC3b']), expr: abcC3\000" .. [[abcC3b (['abcC3', 'abcC3b'])
  310. ==
  311. =abcC3 =
  312. abcC3b
  313. {{{2 setreg('D', ['abcD32'])
  314. D: type V; value: abcD3]] .. "\000abcD32\000 (['abcD3', 'abcD32']), expr: abcD3\000abcD32\000" .. [[ (['abcD3', 'abcD32'])
  315. ==
  316. abcD3
  317. abcD32
  318. ==
  319. {{{2 setreg('A', ['abcA32'])
  320. A: type V; value: abcA3]] .. "\000abcA3c\000abcA32\000 (['abcA3', 'abcA3c', 'abcA32']), expr: abcA3\000abcA3c\000abcA32\000" .. [[ (['abcA3', 'abcA3c', 'abcA32'])
  321. ==
  322. abcA3
  323. abcA3c
  324. abcA32
  325. ==
  326. {{{2 setreg('B', ['abcB3c'], 'c')
  327. B: type v; value: abcB3]] .. "\000abcB3l\000abcB3c (['abcB3', 'abcB3l', 'abcB3c']), expr: abcB3\000abcB3l\000" .. [[abcB3c (['abcB3', 'abcB3l', 'abcB3c'])
  328. ==
  329. =abcB3
  330. abcB3l
  331. abcB3c=
  332. {{{2 setreg('C', ['abcC3l'], 'l')
  333. C: type V; value: abcC3]] .. "\000abcC3b\000abcC3l\000 (['abcC3', 'abcC3b', 'abcC3l']), expr: abcC3\000abcC3b\000abcC3l\000" .. [[ (['abcC3', 'abcC3b', 'abcC3l'])
  334. ==
  335. abcC3
  336. abcC3b
  337. abcC3l
  338. ==
  339. {{{2 setreg('D', ['abcD3b'], 'b')
  340. D: type ]] .. "\0226; value: abcD3\000abcD32\000abcD3b (['abcD3', 'abcD32', 'abcD3b']), expr: abcD3\000abcD32\000" .. [[abcD3b (['abcD3', 'abcD32', 'abcD3b'])
  341. ==
  342. =abcD3 =
  343. abcD32
  344. abcD3b]])
  345. -- From now on we delete the buffer contents after each expect() to make
  346. -- the next expect() easier to write. This is necessary because null
  347. -- bytes on a line by itself don't play well together with the dedent
  348. -- function used in expect().
  349. command('%delete')
  350. command([[$put ='{{{1 Appending lists with NL with setreg()']])
  351. command([=[call SetReg('A', ["\n", 'abcA3l2'], 'l')]=])
  352. expect(
  353. '\n'
  354. .. '{{{1 Appending lists with NL with setreg()\n'
  355. .. "{{{2 setreg('A', ['\000', 'abcA3l2'], 'l')\n"
  356. .. "A: type V; value: abcA3\000abcA3c\000abcA32\000\000\000abcA3l2\000 (['abcA3', 'abcA3c', 'abcA32', '\000', 'abcA3l2']), expr: abcA3\000abcA3c\000abcA32\000\000\000abcA3l2\000 (['abcA3', 'abcA3c', 'abcA32', '\000', 'abcA3l2'])\n"
  357. .. '==\n'
  358. .. 'abcA3\n'
  359. .. 'abcA3c\n'
  360. .. 'abcA32\n'
  361. .. '\000\n'
  362. .. 'abcA3l2\n'
  363. .. '=='
  364. )
  365. command('%delete')
  366. command([=[call SetReg('B', ["\n", 'abcB3c2'], 'c')]=])
  367. expect(
  368. '\n'
  369. .. "{{{2 setreg('B', ['\000', 'abcB3c2'], 'c')\n"
  370. .. "B: type v; value: abcB3\000abcB3l\000abcB3c\000\000\000abcB3c2 (['abcB3', 'abcB3l', 'abcB3c', '\000', 'abcB3c2']), expr: abcB3\000abcB3l\000abcB3c\000\000\000abcB3c2 (['abcB3', 'abcB3l', 'abcB3c', '\000', 'abcB3c2'])\n"
  371. .. '==\n'
  372. .. '=abcB3\n'
  373. .. 'abcB3l\n'
  374. .. 'abcB3c\n'
  375. .. '\000\n'
  376. .. 'abcB3c2='
  377. )
  378. command('%delete')
  379. command([=[call SetReg('C', ["\n", 'abcC3b2'], 'b')]=])
  380. expect(
  381. '\n'
  382. .. "{{{2 setreg('C', ['\000', 'abcC3b2'], 'b')\n"
  383. .. "C: type \0227; value: abcC3\000abcC3b\000abcC3l\000\000\000abcC3b2 (['abcC3', 'abcC3b', 'abcC3l', '\000', 'abcC3b2']), expr: abcC3\000abcC3b\000abcC3l\000\000\000abcC3b2 (['abcC3', 'abcC3b', 'abcC3l', '\000', 'abcC3b2'])\n"
  384. .. '==\n'
  385. .. '=abcC3 =\n'
  386. .. ' abcC3b\n'
  387. .. ' abcC3l\n'
  388. .. ' \000\n'
  389. .. ' abcC3b2'
  390. )
  391. command('%delete')
  392. command([=[call SetReg('D', ["\n", 'abcD3b50'],'b50')]=])
  393. expect(
  394. '\n'
  395. .. "{{{2 setreg('D', ['\000', 'abcD3b50'], 'b50')\n"
  396. .. "D: type \02250; value: abcD3\000abcD32\000abcD3b\000\000\000abcD3b50 (['abcD3', 'abcD32', 'abcD3b', '\000', 'abcD3b50']), expr: abcD3\000abcD32\000abcD3b\000\000\000abcD3b50 (['abcD3', 'abcD32', 'abcD3b', '\000', 'abcD3b50'])\n"
  397. .. '==\n'
  398. .. '=abcD3 =\n'
  399. .. ' abcD32\n'
  400. .. ' abcD3b\n'
  401. .. ' \000\n'
  402. .. ' abcD3b50'
  403. )
  404. end)
  405. -- The tests for setting lists with NLs are split into separate it() blocks
  406. -- to make the expect() calls easier to write. Otherwise the null byte can
  407. -- make trouble on a line on its own.
  408. it('setting lists with NLs with setreg(), part 1', function()
  409. command('so test_eval_setup.vim')
  410. command(
  411. [=[call SetReg('a', ['abcA4-0', "\n", "abcA4-2\n", "\nabcA4-3", "abcA4-4\nabcA4-4-2"])]=]
  412. )
  413. expect(
  414. '\n'
  415. .. "{{{2 setreg('a', ['abcA4-0', '\000', 'abcA4-2\000', '\000abcA4-3', 'abcA4-4\000abcA4-4-2'])\n"
  416. .. "a: type V; value: abcA4-0\000\000\000abcA4-2\000\000\000abcA4-3\000abcA4-4\000abcA4-4-2\000 (['abcA4-0', '\000', 'abcA4-2\000', '\000abcA4-3', 'abcA4-4\000abcA4-4-2']), expr: abcA4-0\000\000\000abcA4-2\000\000\000abcA4-3\000abcA4-4\000abcA4-4-2\000 (['abcA4-0', '\000', 'abcA4-2\000', '\000abcA4-3', 'abcA4-4\000abcA4-4-2'])\n"
  417. .. '==\n'
  418. .. 'abcA4-0\n'
  419. .. '\000\n'
  420. .. 'abcA4-2\000\n'
  421. .. '\000abcA4-3\n'
  422. .. 'abcA4-4\000abcA4-4-2\n'
  423. .. '=='
  424. )
  425. end)
  426. it('setting lists with NLs with setreg(), part 2', function()
  427. command('so test_eval_setup.vim')
  428. command(
  429. [=[call SetReg('b', ['abcB4c-0', "\n", "abcB4c-2\n", "\nabcB4c-3", "abcB4c-4\nabcB4c-4-2"], 'c')]=]
  430. )
  431. expect(
  432. '\n'
  433. .. "{{{2 setreg('b', ['abcB4c-0', '\000', 'abcB4c-2\000', '\000abcB4c-3', 'abcB4c-4\000abcB4c-4-2'], 'c')\n"
  434. .. "b: type v; value: abcB4c-0\000\000\000abcB4c-2\000\000\000abcB4c-3\000abcB4c-4\000abcB4c-4-2 (['abcB4c-0', '\000', 'abcB4c-2\000', '\000abcB4c-3', 'abcB4c-4\000abcB4c-4-2']), expr: abcB4c-0\000\000\000abcB4c-2\000\000\000abcB4c-3\000abcB4c-4\000abcB4c-4-2 (['abcB4c-0', '\000', 'abcB4c-2\000', '\000abcB4c-3', 'abcB4c-4\000abcB4c-4-2'])\n"
  435. .. '==\n'
  436. .. '=abcB4c-0\n'
  437. .. '\000\n'
  438. .. 'abcB4c-2\000\n'
  439. .. '\000abcB4c-3\n'
  440. .. 'abcB4c-4\000abcB4c-4-2='
  441. )
  442. end)
  443. it('setting lists with NLs with setreg(), part 3', function()
  444. command('so test_eval_setup.vim')
  445. command(
  446. [=[call SetReg('c', ['abcC4l-0', "\n", "abcC4l-2\n", "\nabcC4l-3", "abcC4l-4\nabcC4l-4-2"], 'l')]=]
  447. )
  448. expect(
  449. '\n'
  450. .. "{{{2 setreg('c', ['abcC4l-0', '\000', 'abcC4l-2\000', '\000abcC4l-3', 'abcC4l-4\000abcC4l-4-2'], 'l')\n"
  451. .. "c: type V; value: abcC4l-0\000\000\000abcC4l-2\000\000\000abcC4l-3\000abcC4l-4\000abcC4l-4-2\000 (['abcC4l-0', '\000', 'abcC4l-2\000', '\000abcC4l-3', 'abcC4l-4\000abcC4l-4-2']), expr: abcC4l-0\000\000\000abcC4l-2\000\000\000abcC4l-3\000abcC4l-4\000abcC4l-4-2\000 (['abcC4l-0', '\000', 'abcC4l-2\000', '\000abcC4l-3', 'abcC4l-4\000abcC4l-4-2'])\n"
  452. .. '==\n'
  453. .. 'abcC4l-0\n'
  454. .. '\000\n'
  455. .. 'abcC4l-2\000\n'
  456. .. '\000abcC4l-3\n'
  457. .. 'abcC4l-4\000abcC4l-4-2\n'
  458. .. '=='
  459. )
  460. end)
  461. it('setting lists with NLs with setreg(), part 4', function()
  462. command('so test_eval_setup.vim')
  463. command(
  464. [=[call SetReg('d', ['abcD4b-0', "\n", "abcD4b-2\n", "\nabcD4b-3", "abcD4b-4\nabcD4b-4-2"], 'b')]=]
  465. )
  466. expect(
  467. '\n'
  468. .. "{{{2 setreg('d', ['abcD4b-0', '\000', 'abcD4b-2\000', '\000abcD4b-3', 'abcD4b-4\000abcD4b-4-2'], 'b')\n"
  469. .. "d: type \02219; value: abcD4b-0\000\000\000abcD4b-2\000\000\000abcD4b-3\000abcD4b-4\000abcD4b-4-2 (['abcD4b-0', '\000', 'abcD4b-2\000', '\000abcD4b-3', 'abcD4b-4\000abcD4b-4-2']), expr: abcD4b-0\000\000\000abcD4b-2\000\000\000abcD4b-3\000abcD4b-4\000abcD4b-4-2 (['abcD4b-0', '\000', 'abcD4b-2\000', '\000abcD4b-3', 'abcD4b-4\000abcD4b-4-2'])\n"
  470. .. '==\n'
  471. .. '=abcD4b-0 =\n'
  472. .. ' \000\n'
  473. .. ' abcD4b-2\000\n'
  474. .. ' \000abcD4b-3\n'
  475. .. ' abcD4b-4\000abcD4b-4-2'
  476. )
  477. end)
  478. it('setting lists with NLs with setreg(), part 5', function()
  479. command('so test_eval_setup.vim')
  480. command(
  481. [=[call SetReg('e', ['abcE4b10-0', "\n", "abcE4b10-2\n", "\nabcE4b10-3", "abcE4b10-4\nabcE4b10-4-2"], 'b10')]=]
  482. )
  483. expect(
  484. '\n'
  485. .. "{{{2 setreg('e', ['abcE4b10-0', '\000', 'abcE4b10-2\000', '\000abcE4b10-3', 'abcE4b10-4\000abcE4b10-4-2'], 'b10')\n"
  486. .. "e: type \02210; value: abcE4b10-0\000\000\000abcE4b10-2\000\000\000abcE4b10-3\000abcE4b10-4\000abcE4b10-4-2 (['abcE4b10-0', '\000', 'abcE4b10-2\000', '\000abcE4b10-3', 'abcE4b10-4\000abcE4b10-4-2']), expr: abcE4b10-0\000\000\000abcE4b10-2\000\000\000abcE4b10-3\000abcE4b10-4\000abcE4b10-4-2 (['abcE4b10-0', '\000', 'abcE4b10-2\000', '\000abcE4b10-3', 'abcE4b10-4\000abcE4b10-4-2'])\n"
  487. .. '==\n'
  488. .. '=abcE4b10-0=\n'
  489. .. ' \000\n'
  490. .. ' abcE4b10-2\000\n'
  491. .. ' \000abcE4b10-3\n'
  492. .. ' abcE4b10-4\000abcE4b10-4-2'
  493. )
  494. end)
  495. it('getreg("a",1,1) returns a valid list when "a is unset', function()
  496. -- Precondition: "a is actually unset and "0 is nonempty
  497. eq('', eval("getregtype('a')"))
  498. eq('', eval("getreg('a')"))
  499. command("call setreg('0','text')")
  500. -- This used to return a NULL list
  501. -- which setreg didn't handle
  502. command("let x = getreg('a',1,1)")
  503. command("call setreg('0',x)")
  504. -- nvim didn't crash and "0 was emptied
  505. assert_alive()
  506. eq({}, eval("getreg('0',1,1)"))
  507. -- x is a mutable list
  508. command('let y = x')
  509. eq({}, eval('y'))
  510. command("call add(x, 'item')")
  511. eq({ 'item' }, eval('y'))
  512. end)
  513. it('sets the unnamed register when the "u" option is passed to setreg', function()
  514. command("call setreg('a','a reg', 'cu')")
  515. eq('a reg', eval('@"'))
  516. command("call setreg('b','b reg', 'cu')")
  517. eq('b reg', eval('@"'))
  518. command("call setreg('c','c reg', 'c')")
  519. eq('b reg', eval('@"'))
  520. end)
  521. it('search and expressions', function()
  522. command('so test_eval_setup.vim')
  523. command([=[call SetReg('/', ['abc/'])]=])
  524. command([=[call SetReg('/', ["abc/\n"])]=])
  525. command([=[call SetReg('=', ['"abc/"'])]=])
  526. command([=[call SetReg('=', ["\"abc/\n\""])]=])
  527. expect(
  528. [[
  529. {{{2 setreg('/', ['abc/'])
  530. /: type v; value: abc/ (['abc/']), expr: abc/ (['abc/'])
  531. ==
  532. =abc/=
  533. {{{2 setreg('/', ['abc/]]
  534. .. '\000'
  535. .. [['])
  536. /: type v; value: abc/]]
  537. .. "\000 (['abc/\000']), expr: abc/\000 (['abc/\000"
  538. .. [['])
  539. ==
  540. =abc/]]
  541. .. '\000'
  542. .. [[=
  543. {{{2 setreg('=', ['"abc/"'])
  544. =: type v; value: abc/ (['abc/']), expr: "abc/" (['"abc/"'])
  545. {{{2 setreg('=', ['"abc/]]
  546. .. '\000'
  547. .. [["'])
  548. =: type v; value: abc/]]
  549. .. "\000 (['abc/\000"
  550. .. [[']), expr: "abc/]]
  551. .. '\000'
  552. .. [[" (['"abc/]]
  553. .. '\000'
  554. .. [["'])]]
  555. )
  556. end)
  557. describe('system clipboard', function()
  558. before_each(function()
  559. command('let &runtimepath = "test/functional/fixtures,".&runtimepath')
  560. command('call getreg("*")') -- force load of provider
  561. end)
  562. it('works', function()
  563. insert([[
  564. Some first line (this text was at the top of the old test_eval.in).
  565. Note: system clipboard is saved, changed and restored.
  566. clipboard contents
  567. something else]])
  568. command('so test_eval_setup.vim')
  569. -- Save and restore system clipboard.
  570. command("let _clipreg = ['*', getreg('*'), getregtype('*')]")
  571. command('let _clipopt = &cb')
  572. command("let &cb='unnamed'")
  573. command('5y')
  574. command('AR *')
  575. command('tabdo :windo :echo "hi"')
  576. command('6y')
  577. command('AR *')
  578. command('let &cb=_clipopt')
  579. command("call call('setreg', _clipreg)")
  580. expect(
  581. [[
  582. Some first line (this text was at the top of the old test_eval.in).
  583. Note: system clipboard is saved, changed and restored.
  584. clipboard contents
  585. something else
  586. *: type V; value: clipboard contents]]
  587. .. '\00'
  588. .. [[ (['clipboard contents']), expr: clipboard contents]]
  589. .. '\00'
  590. .. [[ (['clipboard contents'])
  591. *: type V; value: something else]]
  592. .. '\00'
  593. .. [[ (['something else']), expr: something else]]
  594. .. '\00'
  595. .. [[ (['something else'])]]
  596. )
  597. end)
  598. end)
  599. it('errors', function()
  600. source([[
  601. fun ErrExe(str)
  602. call append('$', 'Executing '.a:str)
  603. try
  604. execute a:str
  605. catch
  606. $put =v:exception
  607. endtry
  608. endfun]])
  609. command([[call ErrExe('call setreg()')]])
  610. command([[call ErrExe('call setreg(1)')]])
  611. command([[call ErrExe('call setreg(1, 2, 3, 4)')]])
  612. command([=[call ErrExe('call setreg([], 2)')]=])
  613. command([=[call ErrExe('call setreg(1, 2, [])')]=])
  614. command([=[call ErrExe('call setreg("/", ["1", "2"])')]=])
  615. command([=[call ErrExe('call setreg("=", ["1", "2"])')]=])
  616. command([=[call ErrExe('call setreg(1, ["", "", [], ""])')]=])
  617. expect([[
  618. Executing call setreg()
  619. Vim(call):E119: Not enough arguments for function: setreg
  620. Executing call setreg(1)
  621. Vim(call):E119: Not enough arguments for function: setreg
  622. Executing call setreg(1, 2, 3, 4)
  623. Vim(call):E118: Too many arguments for function: setreg
  624. Executing call setreg([], 2)
  625. Vim(call):E730: Using a List as a String
  626. Executing call setreg(1, 2, [])
  627. Vim(call):E730: Using a List as a String
  628. Executing call setreg("/", ["1", "2"])
  629. Vim(call):E883: Search pattern and expression register may not contain two or more lines
  630. Executing call setreg("=", ["1", "2"])
  631. Vim(call):E883: Search pattern and expression register may not contain two or more lines
  632. Executing call setreg(1, ["", "", [], ""])
  633. Vim(call):E730: Using a List as a String]])
  634. end)
  635. it('function name not starting with a capital', function()
  636. eq(
  637. 'Vim(function):E128: Function name must start with a capital or "s:": g:test()\\nendfunction',
  638. exc_exec(dedent([[
  639. function! g:test()
  640. endfunction]]))
  641. )
  642. end)
  643. it('Function name followed by #', function()
  644. eq(
  645. 'Vim(function):E128: Function name must start with a capital or "s:": test2() "#\\nendfunction',
  646. exc_exec(dedent([[
  647. function! test2() "#
  648. endfunction]]))
  649. )
  650. end)
  651. it('function name includes a colon', function()
  652. eq(
  653. 'Vim(function):E884: Function name cannot contain a colon: b:test()\\nendfunction',
  654. exc_exec(dedent([[
  655. function! b:test()
  656. endfunction]]))
  657. )
  658. end)
  659. it('function name starting with/without "g:", buffer-local funcref', function()
  660. command([[
  661. function! g:Foo(n)
  662. $put ='called Foo(' . a:n . ')'
  663. endfunction
  664. ]])
  665. command("let b:my_func = function('Foo')")
  666. command('call b:my_func(1)')
  667. command('echo g:Foo(2)')
  668. command('echo Foo(3)')
  669. expect([[
  670. called Foo(1)
  671. called Foo(2)
  672. called Foo(3)]])
  673. end)
  674. it('script-local function used in Funcref must exist', function()
  675. source([[
  676. " Vim script used in test_eval.in. Needed for script-local function.
  677. func s:Testje()
  678. return "foo"
  679. endfunc
  680. let Bar = function('s:Testje')
  681. $put ='s:Testje exists: ' . exists('s:Testje')
  682. $put ='func s:Testje exists: ' . exists('*s:Testje')
  683. $put ='Bar exists: ' . exists('Bar')
  684. $put ='func Bar exists: ' . exists('*Bar')
  685. ]])
  686. expect([[
  687. s:Testje exists: 0
  688. func s:Testje exists: 1
  689. Bar exists: 1
  690. func Bar exists: 1]])
  691. end)
  692. it("using $ instead of '$' must give an error", function()
  693. eq(
  694. 'Vim(call):E116: Invalid arguments for function append',
  695. exc_exec('call append($, "foobar")')
  696. )
  697. end)
  698. it('getcurpos/setpos', function()
  699. insert([[
  700. 012345678
  701. 012345678
  702. start:]])
  703. command('/^012345678')
  704. feed('6l')
  705. poke_eventloop()
  706. command('let sp = getcurpos()')
  707. feed('0')
  708. poke_eventloop()
  709. command("call setpos('.', sp)")
  710. feed('jyl')
  711. poke_eventloop()
  712. command('$put')
  713. expect([[
  714. 012345678
  715. 012345678
  716. start:
  717. 6]])
  718. end)
  719. it('substring and variable name', function()
  720. command("let str = 'abcdef'")
  721. command('let n = 3')
  722. eq('def', eval('str[n:]'))
  723. eq('abcd', eval('str[:n]'))
  724. eq('d', eval('str[n:n]'))
  725. command('unlet n')
  726. command('let nn = 3')
  727. eq('def', eval('str[nn:]'))
  728. eq('abcd', eval('str[:nn]'))
  729. eq('d', eval('str[nn:nn]'))
  730. command('unlet nn')
  731. command('let b:nn = 4')
  732. eq('ef', eval('str[b:nn:]'))
  733. eq('abcde', eval('str[:b:nn]'))
  734. eq('e', eval('str[b:nn:b:nn]'))
  735. end)
  736. end)