test_usercommands.vim 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. " Tests for user defined commands
  2. " Test for <mods> in user defined commands
  3. function Test_cmdmods()
  4. let g:mods = ''
  5. command! -nargs=* MyCmd let g:mods = '<mods>'
  6. MyCmd
  7. call assert_equal('', g:mods)
  8. aboveleft MyCmd
  9. call assert_equal('aboveleft', g:mods)
  10. abo MyCmd
  11. call assert_equal('aboveleft', g:mods)
  12. belowright MyCmd
  13. call assert_equal('belowright', g:mods)
  14. bel MyCmd
  15. call assert_equal('belowright', g:mods)
  16. botright MyCmd
  17. call assert_equal('botright', g:mods)
  18. bo MyCmd
  19. call assert_equal('botright', g:mods)
  20. browse MyCmd
  21. call assert_equal('browse', g:mods)
  22. bro MyCmd
  23. call assert_equal('browse', g:mods)
  24. confirm MyCmd
  25. call assert_equal('confirm', g:mods)
  26. conf MyCmd
  27. call assert_equal('confirm', g:mods)
  28. hide MyCmd
  29. call assert_equal('hide', g:mods)
  30. hid MyCmd
  31. call assert_equal('hide', g:mods)
  32. keepalt MyCmd
  33. call assert_equal('keepalt', g:mods)
  34. keepa MyCmd
  35. call assert_equal('keepalt', g:mods)
  36. keepjumps MyCmd
  37. call assert_equal('keepjumps', g:mods)
  38. keepj MyCmd
  39. call assert_equal('keepjumps', g:mods)
  40. keepmarks MyCmd
  41. call assert_equal('keepmarks', g:mods)
  42. kee MyCmd
  43. call assert_equal('keepmarks', g:mods)
  44. keeppatterns MyCmd
  45. call assert_equal('keeppatterns', g:mods)
  46. keepp MyCmd
  47. call assert_equal('keeppatterns', g:mods)
  48. leftabove MyCmd " results in :aboveleft
  49. call assert_equal('aboveleft', g:mods)
  50. lefta MyCmd
  51. call assert_equal('aboveleft', g:mods)
  52. lockmarks MyCmd
  53. call assert_equal('lockmarks', g:mods)
  54. loc MyCmd
  55. call assert_equal('lockmarks', g:mods)
  56. noautocmd MyCmd
  57. call assert_equal('noautocmd', g:mods)
  58. noa MyCmd
  59. call assert_equal('noautocmd', g:mods)
  60. noswapfile MyCmd
  61. call assert_equal('noswapfile', g:mods)
  62. nos MyCmd
  63. call assert_equal('noswapfile', g:mods)
  64. rightbelow MyCmd " results in :belowright
  65. call assert_equal('belowright', g:mods)
  66. rightb MyCmd
  67. call assert_equal('belowright', g:mods)
  68. " sandbox MyCmd
  69. silent MyCmd
  70. call assert_equal('silent', g:mods)
  71. sil MyCmd
  72. call assert_equal('silent', g:mods)
  73. silent! MyCmd
  74. call assert_equal('silent!', g:mods)
  75. sil! MyCmd
  76. call assert_equal('silent!', g:mods)
  77. tab MyCmd
  78. call assert_equal('tab', g:mods)
  79. 0tab MyCmd
  80. call assert_equal('0tab', g:mods)
  81. tab split
  82. tab MyCmd
  83. call assert_equal('tab', g:mods)
  84. 1tab MyCmd
  85. call assert_equal('1tab', g:mods)
  86. tabprev
  87. tab MyCmd
  88. call assert_equal('tab', g:mods)
  89. 2tab MyCmd
  90. call assert_equal('2tab', g:mods)
  91. 2tabclose
  92. topleft MyCmd
  93. call assert_equal('topleft', g:mods)
  94. to MyCmd
  95. call assert_equal('topleft', g:mods)
  96. unsilent MyCmd
  97. call assert_equal('unsilent', g:mods)
  98. uns MyCmd
  99. call assert_equal('unsilent', g:mods)
  100. verbose MyCmd
  101. call assert_equal('verbose', g:mods)
  102. verb MyCmd
  103. call assert_equal('verbose', g:mods)
  104. 0verbose MyCmd
  105. call assert_equal('0verbose', g:mods)
  106. 3verbose MyCmd
  107. call assert_equal('3verbose', g:mods)
  108. 999verbose MyCmd
  109. call assert_equal('999verbose', g:mods)
  110. vertical MyCmd
  111. call assert_equal('vertical', g:mods)
  112. vert MyCmd
  113. call assert_equal('vertical', g:mods)
  114. horizontal MyCmd
  115. call assert_equal('horizontal', g:mods)
  116. hor MyCmd
  117. call assert_equal('horizontal', g:mods)
  118. aboveleft belowright botright browse confirm hide keepalt keepjumps
  119. \ keepmarks keeppatterns lockmarks noautocmd noswapfile silent
  120. \ tab topleft unsilent verbose vertical MyCmd
  121. call assert_equal('browse confirm hide keepalt keepjumps ' .
  122. \ 'keepmarks keeppatterns lockmarks noswapfile unsilent noautocmd ' .
  123. \ 'silent verbose aboveleft belowright botright tab topleft vertical',
  124. \ g:mods)
  125. let g:mods = ''
  126. command! -nargs=* MyQCmd let g:mods .= '<q-mods> '
  127. vertical MyQCmd
  128. call assert_equal('"vertical" ', g:mods)
  129. delcommand MyCmd
  130. delcommand MyQCmd
  131. unlet g:mods
  132. endfunction
  133. func SaveCmdArgs(...)
  134. let g:args = a:000
  135. endfunc
  136. func Test_f_args()
  137. command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
  138. TestFArgs
  139. call assert_equal([], g:args)
  140. TestFArgs one two three
  141. call assert_equal(['one', 'two', 'three'], g:args)
  142. TestFArgs one\\two three
  143. call assert_equal(['one\two', 'three'], g:args)
  144. TestFArgs one\ two three
  145. call assert_equal(['one two', 'three'], g:args)
  146. TestFArgs one\"two three
  147. call assert_equal(['one\"two', 'three'], g:args)
  148. delcommand TestFArgs
  149. endfunc
  150. func Test_q_args()
  151. command -nargs=* TestQArgs call SaveCmdArgs(<q-args>)
  152. TestQArgs
  153. call assert_equal([''], g:args)
  154. TestQArgs one two three
  155. call assert_equal(['one two three'], g:args)
  156. TestQArgs one\\two three
  157. call assert_equal(['one\\two three'], g:args)
  158. TestQArgs one\ two three
  159. call assert_equal(['one\ two three'], g:args)
  160. TestQArgs one\"two three
  161. call assert_equal(['one\"two three'], g:args)
  162. delcommand TestQArgs
  163. endfunc
  164. func Test_reg_arg()
  165. command -nargs=* -reg TestRegArg call SaveCmdArgs("<reg>", "<register>")
  166. TestRegArg
  167. call assert_equal(['', ''], g:args)
  168. TestRegArg x
  169. call assert_equal(['x', 'x'], g:args)
  170. delcommand TestRegArg
  171. endfunc
  172. func Test_no_arg()
  173. command -nargs=* TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>")
  174. TestNoArg
  175. call assert_equal(['', '<>', '<x>', '<'], g:args)
  176. TestNoArg one
  177. call assert_equal(['one', '<>', '<x>', '<'], g:args)
  178. delcommand TestNoArg
  179. endfunc
  180. func Test_range_arg()
  181. command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>)
  182. new
  183. call setline(1, range(100))
  184. let lnum = line('.')
  185. TestRangeArg
  186. call assert_equal([0, lnum, lnum], g:args)
  187. 99TestRangeArg
  188. call assert_equal([1, 99, 99], g:args)
  189. 88,99TestRangeArg
  190. call assert_equal([2, 88, 99], g:args)
  191. call assert_fails('102TestRangeArg', 'E16:')
  192. bwipe!
  193. delcommand TestRangeArg
  194. endfunc
  195. func Test_Ambiguous()
  196. command Doit let g:didit = 'yes'
  197. command Dothat let g:didthat = 'also'
  198. call assert_fails('Do', 'E464:')
  199. Doit
  200. call assert_equal('yes', g:didit)
  201. Dothat
  202. call assert_equal('also', g:didthat)
  203. unlet g:didit
  204. unlet g:didthat
  205. delcommand Doit
  206. Do
  207. call assert_equal('also', g:didthat)
  208. delcommand Dothat
  209. " Nvim removed the ":Ni!" easter egg in 87e107d92.
  210. call assert_fails("\x4ei\041", 'E492: Not an editor command: Ni!')
  211. endfunc
  212. func Test_redefine_on_reload()
  213. call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists')
  214. call assert_equal(0, exists(':ExistingCommand'))
  215. source Xcommandexists
  216. call assert_equal(2, exists(':ExistingCommand'))
  217. " Redefining a command when reloading a script is OK.
  218. source Xcommandexists
  219. call assert_equal(2, exists(':ExistingCommand'))
  220. " But redefining in another script is not OK.
  221. call writefile(['command ExistingCommand echo "yes"'], 'Xcommandexists2')
  222. call assert_fails('source Xcommandexists2', 'E174:')
  223. call delete('Xcommandexists2')
  224. " And defining twice in one script is not OK.
  225. delcommand ExistingCommand
  226. call assert_equal(0, exists(':ExistingCommand'))
  227. call writefile([
  228. \ 'command ExistingCommand echo "yes"',
  229. \ 'command ExistingCommand echo "no"',
  230. \ ], 'Xcommandexists')
  231. call assert_fails('source Xcommandexists', 'E174:')
  232. call assert_equal(2, exists(':ExistingCommand'))
  233. call delete('Xcommandexists')
  234. delcommand ExistingCommand
  235. endfunc
  236. func Test_CmdUndefined()
  237. call assert_fails('Doit', 'E492:')
  238. au CmdUndefined Doit :command Doit let g:didit = 'yes'
  239. Doit
  240. call assert_equal('yes', g:didit)
  241. delcommand Doit
  242. call assert_fails('Dothat', 'E492:')
  243. au CmdUndefined * let g:didnot = 'yes'
  244. call assert_fails('Dothat', 'E492:')
  245. call assert_equal('yes', g:didnot)
  246. endfunc
  247. func Test_CmdErrors()
  248. call assert_fails('com! docmd :', 'E183:')
  249. call assert_fails('com! \<Tab> :', 'E182:')
  250. call assert_fails('com! _ :', 'E182:')
  251. call assert_fails('com! - DoCmd :', 'E175:')
  252. call assert_fails('com! -xxx DoCmd :', 'E181:')
  253. call assert_fails('com! -addr DoCmd :', 'E179:')
  254. call assert_fails('com! -addr=asdf DoCmd :', 'E180:')
  255. call assert_fails('com! -complete DoCmd :', 'E179:')
  256. call assert_fails('com! -complete=xxx DoCmd :', 'E180:')
  257. call assert_fails('com! -complete=custom DoCmd :', 'E467:')
  258. call assert_fails('com! -complete=customlist DoCmd :', 'E467:')
  259. " call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:')
  260. call assert_fails('com! -complete=file DoCmd :', 'E1208:')
  261. call assert_fails('com! -nargs=0 -complete=file DoCmd :', 'E1208:')
  262. call assert_fails('com! -nargs=x DoCmd :', 'E176:')
  263. call assert_fails('com! -count=1 -count=2 DoCmd :', 'E177:')
  264. call assert_fails('com! -count=x DoCmd :', 'E178:')
  265. call assert_fails('com! -range=x DoCmd :', 'E178:')
  266. com! -nargs=0 DoCmd :
  267. call assert_fails('DoCmd x', 'E488:')
  268. com! -nargs=1 DoCmd :
  269. call assert_fails('DoCmd', 'E471:')
  270. com! -nargs=+ DoCmd :
  271. call assert_fails('DoCmd', 'E471:')
  272. call assert_fails('com DoCmd :', 'E174:')
  273. comclear
  274. call assert_fails('delcom DoCmd', 'E184:')
  275. " These used to leak memory
  276. call assert_fails('com! -complete=custom,CustomComplete _ :', 'E182:')
  277. call assert_fails('com! -complete=custom,CustomComplete docmd :', 'E183:')
  278. call assert_fails('com! -complete=custom,CustomComplete -xxx DoCmd :', 'E181:')
  279. endfunc
  280. func CustomComplete(A, L, P)
  281. return "January\nFebruary\nMars\n"
  282. endfunc
  283. func CustomCompleteList(A, L, P)
  284. return [ "Monday", "Tuesday", "Wednesday", {}, v:_null_string]
  285. endfunc
  286. func Test_CmdCompletion()
  287. call feedkeys(":com -\<C-A>\<C-B>\"\<CR>", 'tx')
  288. call assert_equal('"com -addr bang bar buffer complete count keepscript nargs range register', @:)
  289. call feedkeys(":com -nargs=0 -\<C-A>\<C-B>\"\<CR>", 'tx')
  290. call assert_equal('"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register', @:)
  291. call feedkeys(":com -nargs=\<C-A>\<C-B>\"\<CR>", 'tx')
  292. call assert_equal('"com -nargs=* + 0 1 ?', @:)
  293. call feedkeys(":com -addr=\<C-A>\<C-B>\"\<CR>", 'tx')
  294. call assert_equal('"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows', @:)
  295. call feedkeys(":com -complete=co\<C-A>\<C-B>\"\<CR>", 'tx')
  296. call assert_equal('"com -complete=color command compiler', @:)
  297. " try completion for unsupported argument values
  298. call feedkeys(":com -newarg=\<Tab>\<C-B>\"\<CR>", 'tx')
  299. call assert_equal("\"com -newarg=\t", @:)
  300. " command completion after the name in a user defined command
  301. call feedkeys(":com MyCmd chist\<Tab>\<C-B>\"\<CR>", 'tx')
  302. call assert_equal("\"com MyCmd chistory", @:)
  303. command! DoCmd1 :
  304. command! DoCmd2 :
  305. call feedkeys(":com \<C-A>\<C-B>\"\<CR>", 'tx')
  306. call assert_equal('"com DoCmd1 DoCmd2', @:)
  307. call feedkeys(":DoC\<C-A>\<C-B>\"\<CR>", 'tx')
  308. call assert_equal('"DoCmd1 DoCmd2', @:)
  309. call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
  310. call assert_equal('"delcom DoCmd1 DoCmd2', @:)
  311. " try argument completion for a command without completion
  312. call feedkeys(":DoCmd1 \<Tab>\<C-B>\"\<CR>", 'tx')
  313. call assert_equal("\"DoCmd1 \t", @:)
  314. delcom DoCmd1
  315. call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
  316. call assert_equal('"delcom DoCmd2', @:)
  317. call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
  318. call assert_equal('"com DoCmd2', @:)
  319. delcom DoCmd2
  320. call feedkeys(":delcom DoC\<C-A>\<C-B>\"\<CR>", 'tx')
  321. call assert_equal('"delcom DoC', @:)
  322. call feedkeys(":com DoC\<C-A>\<C-B>\"\<CR>", 'tx')
  323. call assert_equal('"com DoC', @:)
  324. " com! -nargs=1 -complete=behave DoCmd :
  325. " call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
  326. " call assert_equal('"DoCmd mswin xterm', @:)
  327. " Test for file name completion
  328. com! -nargs=1 -complete=file DoCmd :
  329. call feedkeys(":DoCmd READM\<Tab>\<C-B>\"\<CR>", 'tx')
  330. call assert_equal('"DoCmd README.txt', @:)
  331. " Test for buffer name completion
  332. com! -nargs=1 -complete=buffer DoCmd :
  333. let bnum = bufadd('BufForUserCmd')
  334. call setbufvar(bnum, '&buflisted', 1)
  335. call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
  336. call assert_equal('"DoCmd BufForUserCmd', @:)
  337. bwipe BufForUserCmd
  338. call feedkeys(":DoCmd BufFor\<Tab>\<C-B>\"\<CR>", 'tx')
  339. call assert_equal('"DoCmd BufFor', @:)
  340. com! -nargs=* -complete=custom,CustomComplete DoCmd :
  341. call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
  342. call assert_equal('"DoCmd January February Mars', @:)
  343. com! -nargs=? -complete=customlist,CustomCompleteList DoCmd :
  344. call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx')
  345. call assert_equal('"DoCmd Monday Tuesday Wednesday', @:)
  346. com! -nargs=+ -complete=custom,CustomCompleteList DoCmd :
  347. call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E730:')
  348. com! -nargs=+ -complete=customlist,CustomComp DoCmd :
  349. call assert_fails("call feedkeys(':DoCmd \<C-D>', 'tx')", 'E117:')
  350. " custom completion without a function
  351. com! -nargs=? -complete=custom, DoCmd
  352. call assert_beeps("call feedkeys(':DoCmd \t', 'tx')")
  353. " custom completion failure with the wrong function
  354. com! -nargs=? -complete=custom,min DoCmd
  355. call assert_fails("call feedkeys(':DoCmd \t', 'tx')", 'E118:')
  356. " custom completion for a pattern with a backslash
  357. let g:ArgLead = ''
  358. func! CustCompl(A, L, P)
  359. let g:ArgLead = a:A
  360. return ['one', 'two', 'three']
  361. endfunc
  362. com! -nargs=? -complete=customlist,CustCompl DoCmd
  363. call feedkeys(":DoCmd a\\\t", 'xt')
  364. call assert_equal('a\', g:ArgLead)
  365. delfunc CustCompl
  366. delcom DoCmd
  367. endfunc
  368. func CallExecute(A, L, P)
  369. " Drop first '\n'
  370. return execute('echo "hi"')[1:]
  371. endfunc
  372. func Test_use_execute_in_completion()
  373. command! -nargs=* -complete=custom,CallExecute DoExec :
  374. call feedkeys(":DoExec \<C-A>\<C-B>\"\<CR>", 'tx')
  375. call assert_equal('"DoExec hi', @:)
  376. delcommand DoExec
  377. endfunc
  378. func Test_addr_all()
  379. command! -addr=lines DoSomething let g:a1 = <line1> | let g:a2 = <line2>
  380. %DoSomething
  381. call assert_equal(1, g:a1)
  382. call assert_equal(line('$'), g:a2)
  383. command! -addr=arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2>
  384. args one two three
  385. %DoSomething
  386. call assert_equal(1, g:a1)
  387. call assert_equal(3, g:a2)
  388. command! -addr=buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
  389. %DoSomething
  390. for low in range(1, bufnr('$'))
  391. if buflisted(low)
  392. break
  393. endif
  394. endfor
  395. call assert_equal(low, g:a1)
  396. call assert_equal(bufnr('$'), g:a2)
  397. command! -addr=loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2>
  398. %DoSomething
  399. for low in range(1, bufnr('$'))
  400. if bufloaded(low)
  401. break
  402. endif
  403. endfor
  404. call assert_equal(low, g:a1)
  405. for up in range(bufnr('$'), 1, -1)
  406. if bufloaded(up)
  407. break
  408. endif
  409. endfor
  410. call assert_equal(up, g:a2)
  411. command! -addr=windows DoSomething let g:a1 = <line1> | let g:a2 = <line2>
  412. new
  413. %DoSomething
  414. call assert_equal(1, g:a1)
  415. call assert_equal(winnr('$'), g:a2)
  416. bwipe
  417. command! -addr=tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2>
  418. tabnew
  419. %DoSomething
  420. call assert_equal(1, g:a1)
  421. call assert_equal(len(gettabinfo()), g:a2)
  422. bwipe
  423. command! -addr=other DoSomething let g:a1 = <line1> | let g:a2 = <line2>
  424. DoSomething
  425. call assert_equal(line('.'), g:a1)
  426. call assert_equal(line('.'), g:a2)
  427. %DoSomething
  428. call assert_equal(1, g:a1)
  429. call assert_equal(line('$'), g:a2)
  430. delcommand DoSomething
  431. endfunc
  432. func Test_command_list()
  433. command! DoCmd :
  434. call assert_equal("\n Name Args Address Complete Definition"
  435. \ .. "\n DoCmd 0 :",
  436. \ execute('command DoCmd'))
  437. " Test with various -range= and -count= argument values.
  438. command! -range DoCmd :
  439. call assert_equal("\n Name Args Address Complete Definition"
  440. \ .. "\n DoCmd 0 . :",
  441. \ execute('command DoCmd'))
  442. command! -range=% DoCmd :
  443. call assert_equal("\n Name Args Address Complete Definition"
  444. \ .. "\n DoCmd 0 % :",
  445. \ execute('command! DoCmd'))
  446. command! -range=2 DoCmd :
  447. call assert_equal("\n Name Args Address Complete Definition"
  448. \ .. "\n DoCmd 0 2 :",
  449. \ execute('command DoCmd'))
  450. command! -count=2 DoCmd :
  451. call assert_equal("\n Name Args Address Complete Definition"
  452. \ .. "\n DoCmd 0 2c ? :",
  453. \ execute('command DoCmd'))
  454. " Test with various -addr= argument values.
  455. command! -addr=lines DoCmd :
  456. call assert_equal("\n Name Args Address Complete Definition"
  457. \ .. "\n DoCmd 0 . :",
  458. \ execute('command DoCmd'))
  459. command! -addr=arguments DoCmd :
  460. call assert_equal("\n Name Args Address Complete Definition"
  461. \ .. "\n DoCmd 0 . arg :",
  462. \ execute('command DoCmd'))
  463. command! -addr=buffers DoCmd :
  464. call assert_equal("\n Name Args Address Complete Definition"
  465. \ .. "\n DoCmd 0 . buf :",
  466. \ execute('command DoCmd'))
  467. command! -addr=loaded_buffers DoCmd :
  468. call assert_equal("\n Name Args Address Complete Definition"
  469. \ .. "\n DoCmd 0 . load :",
  470. \ execute('command DoCmd'))
  471. command! -addr=windows DoCmd :
  472. call assert_equal("\n Name Args Address Complete Definition"
  473. \ .. "\n DoCmd 0 . win :",
  474. \ execute('command DoCmd'))
  475. command! -addr=tabs DoCmd :
  476. call assert_equal("\n Name Args Address Complete Definition"
  477. \ .. "\n DoCmd 0 . tab :",
  478. \ execute('command DoCmd'))
  479. command! -addr=other DoCmd :
  480. call assert_equal("\n Name Args Address Complete Definition"
  481. \ .. "\n DoCmd 0 . ? :",
  482. \ execute('command DoCmd'))
  483. " Test with various -complete= argument values (non-exhaustive list)
  484. command! -nargs=1 -complete=arglist DoCmd :
  485. call assert_equal("\n Name Args Address Complete Definition"
  486. \ .. "\n DoCmd 1 arglist :",
  487. \ execute('command DoCmd'))
  488. command! -nargs=* -complete=augroup DoCmd :
  489. call assert_equal("\n Name Args Address Complete Definition"
  490. \ .. "\n DoCmd * augroup :",
  491. \ execute('command DoCmd'))
  492. command! -nargs=? -complete=custom,CustomComplete DoCmd :
  493. call assert_equal("\n Name Args Address Complete Definition"
  494. \ .. "\n DoCmd ? custom :",
  495. \ execute('command DoCmd'))
  496. command! -nargs=+ -complete=customlist,CustomComplete DoCmd :
  497. call assert_equal("\n Name Args Address Complete Definition"
  498. \ .. "\n DoCmd + customlist :",
  499. \ execute('command DoCmd'))
  500. " Test with various -narg= argument values.
  501. command! -nargs=0 DoCmd :
  502. call assert_equal("\n Name Args Address Complete Definition"
  503. \ .. "\n DoCmd 0 :",
  504. \ execute('command DoCmd'))
  505. command! -nargs=1 DoCmd :
  506. call assert_equal("\n Name Args Address Complete Definition"
  507. \ .. "\n DoCmd 1 :",
  508. \ execute('command DoCmd'))
  509. command! -nargs=* DoCmd :
  510. call assert_equal("\n Name Args Address Complete Definition"
  511. \ .. "\n DoCmd * :",
  512. \ execute('command DoCmd'))
  513. command! -nargs=? DoCmd :
  514. call assert_equal("\n Name Args Address Complete Definition"
  515. \ .. "\n DoCmd ? :",
  516. \ execute('command DoCmd'))
  517. command! -nargs=+ DoCmd :
  518. call assert_equal("\n Name Args Address Complete Definition"
  519. \ .. "\n DoCmd + :",
  520. \ execute('command DoCmd'))
  521. " Test with other arguments.
  522. command! -bang DoCmd :
  523. call assert_equal("\n Name Args Address Complete Definition"
  524. \ .. "\n! DoCmd 0 :",
  525. \ execute('command DoCmd'))
  526. command! -bar DoCmd :
  527. call assert_equal("\n Name Args Address Complete Definition"
  528. \ .. "\n| DoCmd 0 :",
  529. \ execute('command DoCmd'))
  530. command! -register DoCmd :
  531. call assert_equal("\n Name Args Address Complete Definition"
  532. \ .. "\n\" DoCmd 0 :",
  533. \ execute('command DoCmd'))
  534. command! -buffer DoCmd :
  535. call assert_equal("\n Name Args Address Complete Definition"
  536. \ .. "\nb DoCmd 0 :"
  537. \ .. "\n\" DoCmd 0 :",
  538. \ execute('command DoCmd'))
  539. comclear
  540. " Test with many args.
  541. command! -bang -bar -register -buffer -nargs=+ -complete=environment -addr=windows -count=3 DoCmd :
  542. call assert_equal("\n Name Args Address Complete Definition"
  543. \ .. "\n!\"b|DoCmd + 3c win environment :",
  544. \ execute('command DoCmd'))
  545. comclear
  546. " Test with special characters in command definition.
  547. command! DoCmd :<cr><tab><c-d>
  548. call assert_equal("\n Name Args Address Complete Definition"
  549. \ .. "\n DoCmd 0 :<CR><Tab><C-D>",
  550. \ execute('command DoCmd'))
  551. " Test output in verbose mode.
  552. command! DoCmd :
  553. call assert_match("^\n"
  554. \ .. " Name Args Address Complete Definition\n"
  555. \ .. " DoCmd 0 :\n"
  556. \ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$",
  557. \ execute('verbose command DoCmd'))
  558. comclear
  559. call assert_equal("\nNo user-defined commands found", execute(':command Xxx'))
  560. call assert_equal("\nNo user-defined commands found", execute('command'))
  561. endfunc
  562. " Test for a custom user completion returning the wrong value type
  563. func Test_usercmd_custom()
  564. func T1(a, c, p)
  565. return "a\nb\n"
  566. endfunc
  567. command -nargs=* -complete=customlist,T1 TCmd1
  568. call feedkeys(":TCmd1 \<C-A>\<C-B>\"\<CR>", 'xt')
  569. call assert_equal('"TCmd1 ', @:)
  570. delcommand TCmd1
  571. delfunc T1
  572. func T2(a, c, p)
  573. return {}
  574. endfunc
  575. command -nargs=* -complete=customlist,T2 TCmd2
  576. call feedkeys(":TCmd2 \<C-A>\<C-B>\"\<CR>", 'xt')
  577. call assert_equal('"TCmd2 ', @:)
  578. delcommand TCmd2
  579. delfunc T2
  580. endfunc
  581. func Test_delcommand_buffer()
  582. command Global echo 'global'
  583. command -buffer OneBuffer echo 'one'
  584. new
  585. command -buffer TwoBuffer echo 'two'
  586. call assert_equal(0, exists(':OneBuffer'))
  587. call assert_equal(2, exists(':Global'))
  588. call assert_equal(2, exists(':TwoBuffer'))
  589. delcommand -buffer TwoBuffer
  590. call assert_equal(0, exists(':TwoBuffer'))
  591. call assert_fails('delcommand -buffer Global', 'E1237:')
  592. call assert_fails('delcommand -buffer OneBuffer', 'E1237:')
  593. bwipe!
  594. call assert_equal(2, exists(':OneBuffer'))
  595. delcommand -buffer OneBuffer
  596. call assert_equal(0, exists(':OneBuffer'))
  597. call assert_fails('delcommand -buffer Global', 'E1237:')
  598. delcommand Global
  599. call assert_equal(0, exists(':Global'))
  600. endfunc
  601. func DefCmd(name)
  602. if len(a:name) > 30
  603. return
  604. endif
  605. exe 'command ' .. a:name .. ' call DefCmd("' .. a:name .. 'x")'
  606. echo a:name
  607. exe a:name
  608. endfunc
  609. func Test_recursive_define()
  610. call DefCmd('Command')
  611. let name = 'Command'
  612. while len(name) < 30
  613. exe 'delcommand ' .. name
  614. let name ..= 'x'
  615. endwhile
  616. endfunc
  617. " Test for using buffer-local ambiguous user-defined commands
  618. func Test_buflocal_ambiguous_usercmd()
  619. new
  620. command -buffer -nargs=1 -complete=sign TestCmd1 echo "Hello"
  621. command -buffer -nargs=1 -complete=sign TestCmd2 echo "World"
  622. call assert_fails("call feedkeys(':TestCmd\<CR>', 'xt')", 'E464:')
  623. call feedkeys(":TestCmd \<Tab>\<C-B>\"\<CR>", 'xt')
  624. call assert_equal('"TestCmd ', @:)
  625. delcommand TestCmd1
  626. delcommand TestCmd2
  627. bw!
  628. endfunc
  629. " Test for using buffer-local user command from cmdwin.
  630. func Test_buflocal_usercmd_cmdwin()
  631. new
  632. command -buffer TestCmd edit Test
  633. " This used to crash Vim
  634. call assert_fails("norm q::TestCmd\<CR>", 'E11:')
  635. bw!
  636. endfunc
  637. " Test for using a multibyte character in a user command
  638. func Test_multibyte_in_usercmd()
  639. command SubJapanesePeriodToDot exe "%s/\u3002/./g"
  640. new
  641. call setline(1, "Hello\u3002")
  642. SubJapanesePeriodToDot
  643. call assert_equal('Hello.', getline(1))
  644. bw!
  645. delcommand SubJapanesePeriodToDot
  646. endfunc
  647. " vim: shiftwidth=2 sts=2 expandtab