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