eval_spec.lua 27 KB

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