test_excmd.vim 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. " Tests for various Ex commands.
  2. source check.vim
  3. source shared.vim
  4. source term_util.vim
  5. source screendump.vim
  6. func Test_ex_delete()
  7. new
  8. call setline(1, ['a', 'b', 'c'])
  9. 2
  10. " :dl is :delete with the "l" flag, not :dlist
  11. .dl
  12. call assert_equal(['a', 'c'], getline(1, 2))
  13. endfunc
  14. func Test_range_error()
  15. call assert_fails(':.echo 1', 'E481:')
  16. call assert_fails(':$echo 1', 'E481:')
  17. call assert_fails(':1,2echo 1', 'E481:')
  18. call assert_fails(':+1echo 1', 'E481:')
  19. call assert_fails(':/1/echo 1', 'E481:')
  20. call assert_fails(':\/echo 1', 'E481:')
  21. normal vv
  22. call assert_fails(":'<,'>echo 1", 'E481:')
  23. call assert_fails(":\\xcenter", 'E10:')
  24. endfunc
  25. func Test_buffers_lastused()
  26. edit bufc " oldest
  27. sleep 1200m
  28. edit bufa " middle
  29. sleep 1200m
  30. edit bufb " newest
  31. enew
  32. let ls = split(execute('buffers t', 'silent!'), '\n')
  33. let bufs = []
  34. for line in ls
  35. let bufs += [split(line, '"\s*')[1:2]]
  36. endfor
  37. let names = []
  38. for buf in bufs
  39. if buf[0] !=# '[No Name]'
  40. let names += [buf[0]]
  41. endif
  42. endfor
  43. call assert_equal(['bufb', 'bufa', 'bufc'], names)
  44. call assert_match('[0-2] seconds\= ago', bufs[1][1])
  45. bwipeout bufa
  46. bwipeout bufb
  47. bwipeout bufc
  48. endfunc
  49. " Test for the :copy command
  50. func Test_copy()
  51. new
  52. call setline(1, ['L1', 'L2', 'L3', 'L4'])
  53. " copy lines in a range to inside the range
  54. 1,3copy 2
  55. call assert_equal(['L1', 'L2', 'L1', 'L2', 'L3', 'L3', 'L4'], getline(1, 7))
  56. " Specifying a count before using : to run an ex-command
  57. exe "normal! gg4:yank\<CR>"
  58. call assert_equal("L1\nL2\nL1\nL2\n", @")
  59. close!
  60. endfunc
  61. " Test for the :file command
  62. func Test_file_cmd()
  63. call assert_fails('3file', 'E474:')
  64. call assert_fails('0,0file', 'E474:')
  65. call assert_fails('0file abc', 'E474:')
  66. if !has('win32')
  67. " Change the name of the buffer to the same name
  68. new Xfile1
  69. file Xfile1
  70. call assert_equal('Xfile1', @%)
  71. call assert_equal('Xfile1', @#)
  72. bw!
  73. endif
  74. endfunc
  75. " Test for the :drop command
  76. func Test_drop_cmd()
  77. call writefile(['L1', 'L2'], 'Xdropfile', 'D')
  78. " Test for reusing the current buffer
  79. enew | only
  80. let expected_nr = bufnr()
  81. drop Xdropfile
  82. call assert_equal(expected_nr, bufnr())
  83. call assert_equal('L2', getline(2))
  84. " Test for switching to an existing window
  85. below new
  86. drop Xdropfile
  87. call assert_equal(1, winnr())
  88. " Test for splitting the current window (set nohidden)
  89. enew | only
  90. set modified
  91. drop Xdropfile
  92. call assert_equal(2, winnr('$'))
  93. " Not splitting the current window even if modified (set hidden)
  94. set hidden
  95. enew | only
  96. set modified
  97. drop Xdropfile
  98. call assert_equal(1, winnr('$'))
  99. " Check for setting the argument list
  100. call assert_equal(['Xdropfile'], argv())
  101. enew | only!
  102. endfunc
  103. " Test for the :append command
  104. func Test_append_cmd()
  105. new
  106. call setline(1, [' L1'])
  107. call feedkeys(":append\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
  108. call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
  109. %delete _
  110. " append after a specific line
  111. call setline(1, [' L1', ' L2', ' L3'])
  112. call feedkeys(":2append\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
  113. call assert_equal([' L1', ' L2', ' L4', ' L5', ' L3'], getline(1, '$'))
  114. %delete _
  115. " append with toggling 'autoindent'
  116. call setline(1, [' L1'])
  117. call feedkeys(":append!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
  118. call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
  119. call assert_false(&autoindent)
  120. %delete _
  121. " append with 'autoindent' set and toggling 'autoindent'
  122. set autoindent
  123. call setline(1, [' L1'])
  124. call feedkeys(":append!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
  125. call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
  126. call assert_true(&autoindent)
  127. set autoindent&
  128. close!
  129. endfunc
  130. func Test_append_cmd_empty_buf()
  131. CheckRunVimInTerminal
  132. let lines =<< trim END
  133. func Timer(timer)
  134. append
  135. aaaaa
  136. bbbbb
  137. .
  138. endfunc
  139. call timer_start(10, 'Timer')
  140. END
  141. call writefile(lines, 'Xtest_append_cmd_empty_buf')
  142. let buf = RunVimInTerminal('-S Xtest_append_cmd_empty_buf', {'rows': 6})
  143. call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
  144. call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
  145. " clean up
  146. call StopVimInTerminal(buf)
  147. call delete('Xtest_append_cmd_empty_buf')
  148. endfunc
  149. " Test for the :insert command
  150. func Test_insert_cmd()
  151. new
  152. call setline(1, [' L1'])
  153. call feedkeys(":insert\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
  154. call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
  155. %delete _
  156. " insert before a specific line
  157. call setline(1, [' L1', ' L2', ' L3'])
  158. call feedkeys(":2insert\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
  159. call assert_equal([' L1', ' L4', ' L5', ' L2', ' L3'], getline(1, '$'))
  160. %delete _
  161. " insert with toggling 'autoindent'
  162. call setline(1, [' L1'])
  163. call feedkeys(":insert!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
  164. call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
  165. call assert_false(&autoindent)
  166. %delete _
  167. " insert with 'autoindent' set and toggling 'autoindent'
  168. set autoindent
  169. call setline(1, [' L1'])
  170. call feedkeys(":insert!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
  171. call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
  172. call assert_true(&autoindent)
  173. set autoindent&
  174. close!
  175. endfunc
  176. func Test_insert_cmd_empty_buf()
  177. CheckRunVimInTerminal
  178. let lines =<< trim END
  179. func Timer(timer)
  180. insert
  181. aaaaa
  182. bbbbb
  183. .
  184. endfunc
  185. call timer_start(10, 'Timer')
  186. END
  187. call writefile(lines, 'Xtest_insert_cmd_empty_buf')
  188. let buf = RunVimInTerminal('-S Xtest_insert_cmd_empty_buf', {'rows': 6})
  189. call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
  190. call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
  191. " clean up
  192. call StopVimInTerminal(buf)
  193. call delete('Xtest_insert_cmd_empty_buf')
  194. endfunc
  195. " Test for the :change command
  196. func Test_change_cmd()
  197. new
  198. call setline(1, [' L1', 'L2', 'L3'])
  199. call feedkeys(":change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
  200. call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
  201. %delete _
  202. " change a specific line
  203. call setline(1, [' L1', ' L2', ' L3'])
  204. call feedkeys(":2change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
  205. call assert_equal([' L1', ' L4', ' L5', ' L3'], getline(1, '$'))
  206. %delete _
  207. " change with toggling 'autoindent'
  208. call setline(1, [' L1', 'L2', 'L3'])
  209. call feedkeys(":change!\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
  210. call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
  211. call assert_false(&autoindent)
  212. %delete _
  213. " change with 'autoindent' set and toggling 'autoindent'
  214. set autoindent
  215. call setline(1, [' L1', 'L2', 'L3'])
  216. call feedkeys(":change!\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
  217. call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
  218. call assert_true(&autoindent)
  219. set autoindent&
  220. close!
  221. endfunc
  222. " Test for the :language command
  223. func Test_language_cmd()
  224. CheckFeature multi_lang
  225. call assert_fails('language ctype non_existing_lang', 'E197:')
  226. call assert_fails('language time non_existing_lang', 'E197:')
  227. endfunc
  228. " Test for the :confirm command dialog
  229. func Test_confirm_cmd()
  230. CheckNotGui
  231. CheckRunVimInTerminal
  232. call writefile(['foo1'], 'Xfoo')
  233. call writefile(['bar1'], 'Xbar')
  234. " Test for saving all the modified buffers
  235. let lines =<< trim END
  236. set nomore
  237. new Xfoo
  238. call setline(1, 'foo2')
  239. new Xbar
  240. call setline(1, 'bar2')
  241. wincmd b
  242. END
  243. call writefile(lines, 'Xscript')
  244. let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
  245. call term_sendkeys(buf, ":confirm qall\n")
  246. call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
  247. call term_sendkeys(buf, "A")
  248. call StopVimInTerminal(buf)
  249. call assert_equal(['foo2'], readfile('Xfoo'))
  250. call assert_equal(['bar2'], readfile('Xbar'))
  251. " Test for discarding all the changes to modified buffers
  252. let lines =<< trim END
  253. set nomore
  254. new Xfoo
  255. call setline(1, 'foo3')
  256. new Xbar
  257. call setline(1, 'bar3')
  258. wincmd b
  259. END
  260. call writefile(lines, 'Xscript')
  261. let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
  262. call term_sendkeys(buf, ":confirm qall\n")
  263. call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
  264. call term_sendkeys(buf, "D")
  265. call StopVimInTerminal(buf)
  266. call assert_equal(['foo2'], readfile('Xfoo'))
  267. call assert_equal(['bar2'], readfile('Xbar'))
  268. " Test for saving and discarding changes to some buffers
  269. let lines =<< trim END
  270. set nomore
  271. new Xfoo
  272. call setline(1, 'foo4')
  273. new Xbar
  274. call setline(1, 'bar4')
  275. wincmd b
  276. END
  277. call writefile(lines, 'Xscript')
  278. let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
  279. call term_sendkeys(buf, ":confirm qall\n")
  280. call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
  281. call term_sendkeys(buf, "N")
  282. call WaitForAssert({-> assert_match('\[Y\]es, (N)o, (C)ancel: ', term_getline(buf, 20))}, 1000)
  283. call term_sendkeys(buf, "Y")
  284. call StopVimInTerminal(buf)
  285. call assert_equal(['foo4'], readfile('Xfoo'))
  286. call assert_equal(['bar2'], readfile('Xbar'))
  287. call delete('Xscript')
  288. call delete('Xfoo')
  289. call delete('Xbar')
  290. endfunc
  291. func Test_confirm_cmd_cancel()
  292. CheckNotGui
  293. CheckRunVimInTerminal
  294. " Test for closing a window with a modified buffer
  295. let lines =<< trim END
  296. set nomore
  297. new
  298. call setline(1, 'abc')
  299. END
  300. call writefile(lines, 'Xscript')
  301. let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
  302. call term_sendkeys(buf, ":confirm close\n")
  303. call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
  304. \ term_getline(buf, 20))}, 1000)
  305. call term_sendkeys(buf, "C")
  306. call WaitForAssert({-> assert_equal('', term_getline(buf, 20))}, 1000)
  307. call term_sendkeys(buf, ":confirm close\n")
  308. call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
  309. \ term_getline(buf, 20))}, 1000)
  310. call term_sendkeys(buf, "N")
  311. call WaitForAssert({-> assert_match('^ *0,0-1 All$',
  312. \ term_getline(buf, 20))}, 1000)
  313. call StopVimInTerminal(buf)
  314. call delete('Xscript')
  315. endfunc
  316. " The ":confirm" prompt was sometimes used with the terminal in cooked mode.
  317. " This test verifies that a "\<CR>" character is NOT required to respond to a
  318. " prompt from the ":conf q" and ":conf wq" commands.
  319. func Test_confirm_q_wq()
  320. CheckNotGui
  321. CheckRunVimInTerminal
  322. call writefile(['foo'], 'Xfoo')
  323. let lines =<< trim END
  324. set hidden nomore
  325. call setline(1, 'abc')
  326. edit Xfoo
  327. END
  328. call writefile(lines, 'Xscript')
  329. let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
  330. call term_sendkeys(buf, ":confirm q\n")
  331. call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
  332. \ term_getline(buf, 20))}, 1000)
  333. call term_sendkeys(buf, 'C')
  334. call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
  335. \ term_getline(buf, 20))}, 1000)
  336. call term_sendkeys(buf, ":edit Xfoo\n")
  337. call term_sendkeys(buf, ":confirm wq\n")
  338. call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
  339. \ term_getline(buf, 20))}, 1000)
  340. call term_sendkeys(buf, 'C')
  341. call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
  342. \ term_getline(buf, 20))}, 1000)
  343. call StopVimInTerminal(buf)
  344. call delete('Xscript')
  345. call delete('Xfoo')
  346. endfunc
  347. func Test_confirm_write_ro()
  348. CheckNotGui
  349. CheckRunVimInTerminal
  350. call writefile(['foo'], 'Xconfirm_write_ro')
  351. let lines =<< trim END
  352. set nobackup ff=unix cmdheight=2
  353. edit Xconfirm_write_ro
  354. norm Abar
  355. END
  356. call writefile(lines, 'Xscript')
  357. let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
  358. " Try to write with 'ro' option.
  359. call term_sendkeys(buf, ":set ro | confirm w\n")
  360. call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
  361. \ term_getline(buf, 18))}, 1000)
  362. call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
  363. \ term_getline(buf, 19))}, 1000)
  364. call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
  365. call term_sendkeys(buf, 'N')
  366. call WaitForAssert({-> assert_match('^ *$', term_getline(buf, 19))}, 1000)
  367. call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
  368. call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
  369. call term_sendkeys(buf, ":confirm w\n")
  370. call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
  371. \ term_getline(buf, 18))}, 1000)
  372. call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
  373. \ term_getline(buf, 19))}, 1000)
  374. call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
  375. call term_sendkeys(buf, 'Y')
  376. call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 7B written$',
  377. \ term_getline(buf, 19))}, 1000)
  378. call assert_equal(['foobar'], readfile('Xconfirm_write_ro'))
  379. " Try to write with read-only file permissions.
  380. call setfperm('Xconfirm_write_ro', 'r--r--r--')
  381. call term_sendkeys(buf, ":set noro | undo | confirm w\n")
  382. call WaitForAssert({-> assert_match("^File permissions of \"Xconfirm_write_ro\" are read-only\. *$",
  383. \ term_getline(buf, 17))}, 1000)
  384. call WaitForAssert({-> assert_match('^It may still be possible to write it\. *$',
  385. \ term_getline(buf, 18))}, 1000)
  386. call WaitForAssert({-> assert_match('^Do you wish to try? *$', term_getline(buf, 19))}, 1000)
  387. call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
  388. call term_sendkeys(buf, 'Y')
  389. call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 4B written$',
  390. \ term_getline(buf, 19))}, 1000)
  391. call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
  392. call StopVimInTerminal(buf)
  393. call delete('Xscript')
  394. call delete('Xconfirm_write_ro')
  395. endfunc
  396. func Test_confirm_write_partial_file()
  397. CheckNotGui
  398. CheckRunVimInTerminal
  399. call writefile(['a', 'b', 'c', 'd'], 'Xwrite_partial')
  400. call writefile(['set nobackup ff=unix cmdheight=2',
  401. \ 'edit Xwrite_partial'], 'Xscript')
  402. let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
  403. call term_sendkeys(buf, ":confirm 2,3w\n")
  404. call WaitForAssert({-> assert_match('^Write partial file? *$',
  405. \ term_getline(buf, 19))}, 1000)
  406. call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
  407. \ term_getline(buf, 20))}, 1000)
  408. call term_sendkeys(buf, 'N')
  409. call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
  410. call assert_equal(['a', 'b', 'c', 'd'], readfile('Xwrite_partial'))
  411. call delete('Xwrite_partial')
  412. call term_sendkeys(buf, ":confirm 2,3w\n")
  413. call WaitForAssert({-> assert_match('^Write partial file? *$',
  414. \ term_getline(buf, 19))}, 1000)
  415. call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
  416. \ term_getline(buf, 20))}, 1000)
  417. call term_sendkeys(buf, 'Y')
  418. call WaitForAssert({-> assert_match('^"Xwrite_partial" \[New\] 2L, 4B written *$',
  419. \ term_getline(buf, 19))}, 1000)
  420. call WaitForAssert({-> assert_match('^Press ENTER or type command to continue *$',
  421. \ term_getline(buf, 20))}, 1000)
  422. call assert_equal(['b', 'c'], readfile('Xwrite_partial'))
  423. call StopVimInTerminal(buf)
  424. call delete('Xwrite_partial')
  425. call delete('Xscript')
  426. endfunc
  427. " Test for the :print command
  428. func Test_print_cmd()
  429. call assert_fails('print', 'E749:')
  430. endfunc
  431. " Test for the :winsize command
  432. func Test_winsize_cmd()
  433. call assert_fails('winsize 1', 'E465:')
  434. call assert_fails('winsize 1 x', 'E465:')
  435. call assert_fails('win_getid(1)', 'E475: Invalid argument: _getid(1)')
  436. " Actually changing the window size would be flaky.
  437. endfunc
  438. " Test for the :redir command
  439. " NOTE: if you run tests as root this will fail. Don't run tests as root!
  440. func Test_redir_cmd()
  441. call assert_fails('redir @@', 'E475:')
  442. call assert_fails('redir abc', 'E475:')
  443. call assert_fails('redir => 1abc', 'E474:')
  444. call assert_fails('redir => a b', 'E488:')
  445. call assert_fails('redir => abc[1]', 'E121:')
  446. let b = 0zFF
  447. call assert_fails('redir =>> b', 'E734:')
  448. unlet b
  449. if has('unix')
  450. " Redirecting to a directory name
  451. call mkdir('Xdir')
  452. call assert_fails('redir > Xdir', 'E17:')
  453. call delete('Xdir', 'd')
  454. endif
  455. " Test for redirecting to a register
  456. redir @q> | echon 'clean ' | redir END
  457. redir @q>> | echon 'water' | redir END
  458. call assert_equal('clean water', @q)
  459. " Test for redirecting to a variable
  460. redir => color | echon 'blue ' | redir END
  461. redir =>> color | echon 'sky' | redir END
  462. call assert_equal('blue sky', color)
  463. endfunc
  464. func Test_redir_cmd_readonly()
  465. CheckNotRoot
  466. " Redirecting to a read-only file
  467. call writefile([], 'Xfile')
  468. call setfperm('Xfile', 'r--r--r--')
  469. call assert_fails('redir! > Xfile', 'E190:')
  470. call delete('Xfile')
  471. endfunc
  472. " Test for the :filetype command
  473. func Test_filetype_cmd()
  474. call assert_fails('filetype abc', 'E475:')
  475. endfunc
  476. " Test for the :mode command
  477. func Test_mode_cmd()
  478. call assert_fails('mode abc', 'E359:')
  479. endfunc
  480. " Test for the :sleep command
  481. func Test_sleep_cmd()
  482. call assert_fails('sleep x', 'E475:')
  483. endfunc
  484. " Test for the :read command
  485. func Test_read_cmd()
  486. call writefile(['one'], 'Xfile')
  487. new
  488. call assert_fails('read', 'E32:')
  489. edit Xfile
  490. read
  491. call assert_equal(['one', 'one'], getline(1, '$'))
  492. close!
  493. new
  494. read Xfile
  495. call assert_equal(['', 'one'], getline(1, '$'))
  496. call deletebufline('', 1, '$')
  497. call feedkeys("Qr Xfile\<CR>visual\<CR>", 'xt')
  498. call assert_equal(['one'], getline(1, '$'))
  499. close!
  500. call delete('Xfile')
  501. endfunc
  502. " Test for running Ex commands when text is locked.
  503. " <C-\>e in the command line is used to lock the text
  504. func Test_run_excmd_with_text_locked()
  505. " :quit
  506. let cmd = ":\<C-\>eexecute('quit')\<CR>\<C-C>"
  507. call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
  508. " :qall
  509. let cmd = ":\<C-\>eexecute('qall')\<CR>\<C-C>"
  510. call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
  511. " :exit
  512. let cmd = ":\<C-\>eexecute('exit')\<CR>\<C-C>"
  513. call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
  514. " :close - should be ignored
  515. new
  516. let cmd = ":\<C-\>eexecute('close')\<CR>\<C-C>"
  517. call assert_equal(2, winnr('$'))
  518. close
  519. call assert_fails("call feedkeys(\":\<C-R>=execute('bnext')\<CR>\", 'xt')", 'E565:')
  520. " :tabfirst
  521. tabnew
  522. call assert_fails("call feedkeys(\":\<C-R>=execute('tabfirst')\<CR>\", 'xt')", 'E565:')
  523. tabclose
  524. endfunc
  525. " Test for the :verbose command
  526. func Test_verbose_cmd()
  527. set verbose=3
  528. call assert_match(' verbose=1\n\s*Last set from ', execute('verbose set vbs'), "\n")
  529. call assert_equal([' verbose=0'], split(execute('0verbose set vbs'), "\n"))
  530. set verbose=0
  531. call assert_match(' verbose=4\n\s*Last set from .*\n verbose=0',
  532. \ execute("4verbose set verbose | set verbose"))
  533. endfunc
  534. " Test for the :delete command and the related abbreviated commands
  535. func Test_excmd_delete()
  536. new
  537. call setline(1, ['foo', "\tbar"])
  538. call assert_equal(['^Ibar$'], split(execute('dl'), "\n"))
  539. call setline(1, ['foo', "\tbar"])
  540. call assert_equal(['^Ibar$'], split(execute('dell'), "\n"))
  541. call setline(1, ['foo', "\tbar"])
  542. call assert_equal(['^Ibar$'], split(execute('delel'), "\n"))
  543. call setline(1, ['foo', "\tbar"])
  544. call assert_equal(['^Ibar$'], split(execute('deletl'), "\n"))
  545. call setline(1, ['foo', "\tbar"])
  546. call assert_equal(['^Ibar$'], split(execute('deletel'), "\n"))
  547. call setline(1, ['foo', "\tbar"])
  548. call assert_equal([' bar'], split(execute('dp'), "\n"))
  549. call setline(1, ['foo', "\tbar"])
  550. call assert_equal([' bar'], split(execute('dep'), "\n"))
  551. call setline(1, ['foo', "\tbar"])
  552. call assert_equal([' bar'], split(execute('delp'), "\n"))
  553. call setline(1, ['foo', "\tbar"])
  554. call assert_equal([' bar'], split(execute('delep'), "\n"))
  555. call setline(1, ['foo', "\tbar"])
  556. call assert_equal([' bar'], split(execute('deletp'), "\n"))
  557. call setline(1, ['foo', "\tbar"])
  558. call assert_equal([' bar'], split(execute('deletep'), "\n"))
  559. close!
  560. endfunc
  561. " Test for commands that are blocked in a sandbox
  562. func Sandbox_tests()
  563. call assert_fails("call histadd(':', 'ls')", 'E48:')
  564. call assert_fails("call mkdir('Xdir')", 'E48:')
  565. call assert_fails("call rename('a', 'b')", 'E48:')
  566. call assert_fails("call setbufvar(1, 'myvar', 1)", 'E48:')
  567. call assert_fails("call settabvar(1, 'myvar', 1)", 'E48:')
  568. call assert_fails("call settabwinvar(1, 1, 'myvar', 1)", 'E48:')
  569. call assert_fails("call setwinvar(1, 'myvar', 1)", 'E48:')
  570. call assert_fails("call timer_start(100, '')", 'E48:')
  571. if has('channel')
  572. call assert_fails("call prompt_setcallback(1, '')", 'E48:')
  573. call assert_fails("call prompt_setinterrupt(1, '')", 'E48:')
  574. call assert_fails("call prompt_setprompt(1, '')", 'E48:')
  575. endif
  576. call assert_fails("let $TESTVAR=1", 'E48:')
  577. call assert_fails("call feedkeys('ivim')", 'E48:')
  578. call assert_fails("source! Xfile", 'E48:')
  579. call assert_fails("call delete('Xfile')", 'E48:')
  580. call assert_fails("call writefile([], 'Xfile')", 'E48:')
  581. call assert_fails('!ls', 'E48:')
  582. " call assert_fails('shell', 'E48:')
  583. call assert_fails('stop', 'E48:')
  584. call assert_fails('exe "normal \<C-Z>"', 'E48:')
  585. " set insertmode
  586. " call assert_fails('call feedkeys("\<C-Z>", "xt")', 'E48:')
  587. " set insertmode&
  588. call assert_fails('suspend', 'E48:')
  589. call assert_fails('call system("ls")', 'E48:')
  590. call assert_fails('call systemlist("ls")', 'E48:')
  591. if has('clientserver')
  592. call assert_fails('let s=remote_expr("gvim", "2+2")', 'E48:')
  593. if !has('win32')
  594. " remote_foreground() doesn't throw an error message on MS-Windows
  595. call assert_fails('call remote_foreground("gvim")', 'E48:')
  596. endif
  597. call assert_fails('let s=remote_peek("gvim")', 'E48:')
  598. call assert_fails('let s=remote_read("gvim")', 'E48:')
  599. call assert_fails('let s=remote_send("gvim", "abc")', 'E48:')
  600. call assert_fails('let s=server2client("gvim", "abc")', 'E48:')
  601. endif
  602. if has('terminal')
  603. call assert_fails('terminal', 'E48:')
  604. call assert_fails('call term_start("vim")', 'E48:')
  605. call assert_fails('call term_dumpwrite(1, "Xfile")', 'E48:')
  606. endif
  607. if has('channel')
  608. call assert_fails("call ch_logfile('chlog')", 'E48:')
  609. call assert_fails("call ch_open('localhost:8765')", 'E48:')
  610. endif
  611. if has('job')
  612. call assert_fails("call job_start('vim')", 'E48:')
  613. endif
  614. if has('unix') && has('libcall')
  615. call assert_fails("echo libcall('libc.so', 'getenv', 'HOME')", 'E48:')
  616. endif
  617. if has('unix')
  618. call assert_fails('cd `pwd`', 'E48:')
  619. endif
  620. " some options cannot be changed in a sandbox
  621. call assert_fails('set exrc', 'E48:')
  622. call assert_fails('set cdpath', 'E48:')
  623. if has('xim') && has('gui_gtk')
  624. call assert_fails('set imstyle', 'E48:')
  625. endif
  626. endfunc
  627. func Test_sandbox()
  628. sandbox call Sandbox_tests()
  629. endfunc
  630. func Test_command_not_implemented_E319()
  631. if !has('mzscheme')
  632. call assert_fails('mzscheme', 'E319:')
  633. endif
  634. endfunc
  635. func Test_not_break_expression_register()
  636. call setreg('=', '1+1')
  637. if 0
  638. put =1
  639. endif
  640. call assert_equal('1+1', getreg('=', 1))
  641. endfunc
  642. func Test_address_line_overflow()
  643. if !has('nvim') && v:sizeoflong < 8
  644. throw 'Skipped: only works with 64 bit long ints'
  645. endif
  646. new
  647. call setline(1, range(100))
  648. call assert_fails('|.44444444444444444444444', 'E1247:')
  649. call assert_fails('|.9223372036854775806', 'E1247:')
  650. call assert_fails('.44444444444444444444444d', 'E1247:')
  651. call assert_equal(range(100)->map('string(v:val)'), getline(1, '$'))
  652. $
  653. yank 77777777777777777777
  654. call assert_equal("99\n", @")
  655. bwipe!
  656. endfunc
  657. " This was leaving the cursor in line zero
  658. func Test_using_zero_in_range()
  659. new
  660. norm o00
  661. silent! 0;s/\%')
  662. bwipe!
  663. endfunc
  664. " Test :write after changing name with :file and loading it with :edit
  665. func Test_write_after_rename()
  666. call writefile(['text'], 'Xfile')
  667. enew
  668. file Xfile
  669. call assert_fails('write', 'E13: File exists (add ! to override)')
  670. " works OK after ":edit"
  671. edit
  672. write
  673. call delete('Xfile')
  674. bwipe!
  675. endfunc
  676. " catch address lines overflow
  677. func Test_ex_address_range_overflow()
  678. call assert_fails(':--+foobar', 'E492:')
  679. endfunc
  680. func Test_drop_modified_file()
  681. CheckScreendump
  682. let lines =<< trim END
  683. call setline(1, 'The quick brown fox jumped over the lazy dogs')
  684. END
  685. call writefile([''], 'Xdrop_modified.txt', 'D')
  686. call writefile(lines, 'Xtest_drop_modified', 'D')
  687. let buf = RunVimInTerminal('-S Xtest_drop_modified Xdrop_modified.txt', {'rows': 10,'columns': 40})
  688. call term_sendkeys(buf, ":drop Xdrop_modified.txt\<CR>")
  689. call VerifyScreenDump(buf, 'Test_drop_modified_1', {})
  690. " clean up
  691. call StopVimInTerminal(buf)
  692. endfunc
  693. " vim: shiftwidth=2 sts=2 expandtab