Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 1 | " Tests for the undo tree. |
| 2 | " Since this script is sourced we need to explicitly break changes up in |
| 3 | " undo-able pieces. Do that by setting 'undolevels'. |
| 4 | " Also tests :earlier and :later. |
| 5 | |
| 6 | func Test_undotree() |
Bram Moolenaar | 80eaddd | 2017-11-11 23:37:08 +0100 | [diff] [blame] | 7 | new |
| 8 | |
| 9 | normal! Aabc |
Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 10 | set ul=100 |
Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 11 | let d = undotree() |
Bram Moolenaar | 80eaddd | 2017-11-11 23:37:08 +0100 | [diff] [blame] | 12 | call assert_equal(1, d.seq_last) |
| 13 | call assert_equal(1, d.seq_cur) |
| 14 | call assert_equal(0, d.save_last) |
| 15 | call assert_equal(0, d.save_cur) |
| 16 | call assert_equal(1, len(d.entries)) |
| 17 | call assert_equal(1, d.entries[0].newhead) |
| 18 | call assert_equal(1, d.entries[0].seq) |
| 19 | call assert_true(d.entries[0].time <= d.time_cur) |
| 20 | |
| 21 | normal! Adef |
| 22 | set ul=100 |
| 23 | let d = undotree() |
| 24 | call assert_equal(2, d.seq_last) |
| 25 | call assert_equal(2, d.seq_cur) |
| 26 | call assert_equal(0, d.save_last) |
| 27 | call assert_equal(0, d.save_cur) |
| 28 | call assert_equal(2, len(d.entries)) |
| 29 | call assert_equal(1, d.entries[0].seq) |
| 30 | call assert_equal(1, d.entries[1].newhead) |
| 31 | call assert_equal(2, d.entries[1].seq) |
| 32 | call assert_true(d.entries[1].time <= d.time_cur) |
| 33 | |
| 34 | undo |
| 35 | set ul=100 |
| 36 | let d = undotree() |
| 37 | call assert_equal(2, d.seq_last) |
| 38 | call assert_equal(1, d.seq_cur) |
| 39 | call assert_equal(0, d.save_last) |
| 40 | call assert_equal(0, d.save_cur) |
| 41 | call assert_equal(2, len(d.entries)) |
| 42 | call assert_equal(1, d.entries[0].seq) |
| 43 | call assert_equal(1, d.entries[1].curhead) |
| 44 | call assert_equal(1, d.entries[1].newhead) |
| 45 | call assert_equal(2, d.entries[1].seq) |
| 46 | call assert_true(d.entries[1].time == d.time_cur) |
| 47 | |
| 48 | normal! Aghi |
| 49 | set ul=100 |
| 50 | let d = undotree() |
| 51 | call assert_equal(3, d.seq_last) |
| 52 | call assert_equal(3, d.seq_cur) |
| 53 | call assert_equal(0, d.save_last) |
| 54 | call assert_equal(0, d.save_cur) |
| 55 | call assert_equal(2, len(d.entries)) |
| 56 | call assert_equal(1, d.entries[0].seq) |
| 57 | call assert_equal(2, d.entries[1].alt[0].seq) |
| 58 | call assert_equal(1, d.entries[1].newhead) |
| 59 | call assert_equal(3, d.entries[1].seq) |
| 60 | call assert_true(d.entries[1].time <= d.time_cur) |
| 61 | |
| 62 | undo |
| 63 | set ul=100 |
| 64 | let d = undotree() |
| 65 | call assert_equal(3, d.seq_last) |
| 66 | call assert_equal(1, d.seq_cur) |
| 67 | call assert_equal(0, d.save_last) |
| 68 | call assert_equal(0, d.save_cur) |
| 69 | call assert_equal(2, len(d.entries)) |
| 70 | call assert_equal(1, d.entries[0].seq) |
| 71 | call assert_equal(2, d.entries[1].alt[0].seq) |
| 72 | call assert_equal(1, d.entries[1].curhead) |
| 73 | call assert_equal(1, d.entries[1].newhead) |
| 74 | call assert_equal(3, d.entries[1].seq) |
| 75 | call assert_true(d.entries[1].time == d.time_cur) |
Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 76 | |
| 77 | w! Xtest |
Bram Moolenaar | 80eaddd | 2017-11-11 23:37:08 +0100 | [diff] [blame] | 78 | let d = undotree() |
| 79 | call assert_equal(1, d.save_cur) |
| 80 | call assert_equal(1, d.save_last) |
Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 81 | call delete('Xtest') |
Bram Moolenaar | 80eaddd | 2017-11-11 23:37:08 +0100 | [diff] [blame] | 82 | bwipe! Xtest |
Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 83 | endfunc |
| 84 | |
| 85 | func FillBuffer() |
| 86 | for i in range(1,13) |
| 87 | put=i |
Bram Moolenaar | e5fa111 | 2018-05-26 18:46:30 +0200 | [diff] [blame] | 88 | " Set 'undolevels' to split undo. |
Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 89 | exe "setg ul=" . &g:ul |
| 90 | endfor |
| 91 | endfunc |
| 92 | |
| 93 | func Test_global_local_undolevels() |
| 94 | new one |
| 95 | set undolevels=5 |
| 96 | call FillBuffer() |
| 97 | " will only undo the last 5 changes, end up with 13 - (5 + 1) = 7 lines |
| 98 | earlier 10 |
| 99 | call assert_equal(5, &g:undolevels) |
| 100 | call assert_equal(-123456, &l:undolevels) |
| 101 | call assert_equal('7', getline('$')) |
| 102 | |
| 103 | new two |
| 104 | setlocal undolevels=2 |
| 105 | call FillBuffer() |
| 106 | " will only undo the last 2 changes, end up with 13 - (2 + 1) = 10 lines |
| 107 | earlier 10 |
| 108 | call assert_equal(5, &g:undolevels) |
| 109 | call assert_equal(2, &l:undolevels) |
| 110 | call assert_equal('10', getline('$')) |
| 111 | |
| 112 | setlocal ul=10 |
| 113 | call assert_equal(5, &g:undolevels) |
| 114 | call assert_equal(10, &l:undolevels) |
| 115 | |
| 116 | " Setting local value in "two" must not change local value in "one" |
| 117 | wincmd p |
| 118 | call assert_equal(5, &g:undolevels) |
| 119 | call assert_equal(-123456, &l:undolevels) |
| 120 | |
| 121 | new three |
| 122 | setglobal ul=50 |
| 123 | call assert_equal(50, &g:undolevels) |
| 124 | call assert_equal(-123456, &l:undolevels) |
| 125 | |
Bram Moolenaar | 1363a30 | 2020-04-12 13:50:26 +0200 | [diff] [blame] | 126 | " Resetting the local 'undolevels' value to use the global value |
| 127 | setlocal undolevels=5 |
| 128 | setlocal undolevels< |
| 129 | call assert_equal(-123456, &l:undolevels) |
| 130 | |
Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 131 | " Drop created windows |
| 132 | set ul& |
| 133 | new |
| 134 | only! |
| 135 | endfunc |
| 136 | |
| 137 | func BackOne(expected) |
| 138 | call feedkeys('g-', 'xt') |
| 139 | call assert_equal(a:expected, getline(1)) |
| 140 | endfunc |
| 141 | |
| 142 | func Test_undo_del_chars() |
| 143 | " Setup a buffer without creating undo entries |
| 144 | new |
| 145 | set ul=-1 |
| 146 | call setline(1, ['123-456']) |
| 147 | set ul=100 |
| 148 | 1 |
| 149 | call test_settime(100) |
| 150 | |
| 151 | " Delete three characters and undo with g- |
| 152 | call feedkeys('x', 'xt') |
| 153 | call feedkeys('x', 'xt') |
| 154 | call feedkeys('x', 'xt') |
| 155 | call assert_equal('-456', getline(1)) |
| 156 | call BackOne('3-456') |
| 157 | call BackOne('23-456') |
| 158 | call BackOne('123-456') |
| 159 | call assert_fails("BackOne('123-456')") |
| 160 | |
| 161 | :" Delete three other characters and go back in time with g- |
| 162 | call feedkeys('$x', 'xt') |
| 163 | call feedkeys('x', 'xt') |
| 164 | call feedkeys('x', 'xt') |
| 165 | call assert_equal('123-', getline(1)) |
| 166 | call test_settime(101) |
| 167 | |
| 168 | call BackOne('123-4') |
| 169 | call BackOne('123-45') |
| 170 | " skips '123-456' because it's older |
| 171 | call BackOne('-456') |
| 172 | call BackOne('3-456') |
| 173 | call BackOne('23-456') |
| 174 | call BackOne('123-456') |
| 175 | call assert_fails("BackOne('123-456')") |
| 176 | normal 10g+ |
| 177 | call assert_equal('123-', getline(1)) |
| 178 | |
| 179 | :" Jump two seconds and go some seconds forward and backward |
| 180 | call test_settime(103) |
| 181 | call feedkeys("Aa\<Esc>", 'xt') |
| 182 | call feedkeys("Ab\<Esc>", 'xt') |
| 183 | call feedkeys("Ac\<Esc>", 'xt') |
| 184 | call assert_equal('123-abc', getline(1)) |
| 185 | earlier 1s |
| 186 | call assert_equal('123-', getline(1)) |
| 187 | earlier 3s |
| 188 | call assert_equal('123-456', getline(1)) |
| 189 | later 1s |
| 190 | call assert_equal('123-', getline(1)) |
| 191 | later 1h |
| 192 | call assert_equal('123-abc', getline(1)) |
| 193 | |
| 194 | close! |
| 195 | endfunc |
| 196 | |
Bram Moolenaar | c628fdc | 2016-08-31 20:33:27 +0200 | [diff] [blame] | 197 | func Test_undolist() |
| 198 | new |
| 199 | set ul=100 |
| 200 | |
Bram Moolenaar | e5fa111 | 2018-05-26 18:46:30 +0200 | [diff] [blame] | 201 | let a = execute('undolist') |
Bram Moolenaar | c628fdc | 2016-08-31 20:33:27 +0200 | [diff] [blame] | 202 | call assert_equal("\nNothing to undo", a) |
| 203 | |
| 204 | " 1 leaf (2 changes). |
| 205 | call feedkeys('achange1', 'xt') |
| 206 | call feedkeys('achange2', 'xt') |
Bram Moolenaar | e5fa111 | 2018-05-26 18:46:30 +0200 | [diff] [blame] | 207 | let a = execute('undolist') |
Bram Moolenaar | c628fdc | 2016-08-31 20:33:27 +0200 | [diff] [blame] | 208 | call assert_match("^\nnumber changes when *saved\n *2 *2 .*$", a) |
| 209 | |
| 210 | " 2 leaves. |
| 211 | call feedkeys('u', 'xt') |
| 212 | call feedkeys('achange3\<Esc>', 'xt') |
Bram Moolenaar | e5fa111 | 2018-05-26 18:46:30 +0200 | [diff] [blame] | 213 | let a = execute('undolist') |
Bram Moolenaar | c628fdc | 2016-08-31 20:33:27 +0200 | [diff] [blame] | 214 | call assert_match("^\nnumber changes when *saved\n *2 *2 *.*\n *3 *2 .*$", a) |
| 215 | close! |
| 216 | endfunc |
| 217 | |
| 218 | func Test_U_command() |
| 219 | new |
| 220 | set ul=100 |
| 221 | call feedkeys("achange1\<Esc>", 'xt') |
| 222 | call feedkeys("achange2\<Esc>", 'xt') |
| 223 | norm! U |
| 224 | call assert_equal('', getline(1)) |
| 225 | norm! U |
| 226 | call assert_equal('change1change2', getline(1)) |
| 227 | close! |
| 228 | endfunc |
| 229 | |
Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 230 | func Test_undojoin() |
| 231 | new |
| 232 | call feedkeys("Goaaaa\<Esc>", 'xt') |
| 233 | call feedkeys("obbbb\<Esc>", 'xt') |
| 234 | call assert_equal(['aaaa', 'bbbb'], getline(2, '$')) |
| 235 | call feedkeys("u", 'xt') |
| 236 | call assert_equal(['aaaa'], getline(2, '$')) |
| 237 | call feedkeys("obbbb\<Esc>", 'xt') |
| 238 | undojoin |
| 239 | " Note: next change must not be as if typed |
| 240 | call feedkeys("occcc\<Esc>", 'x') |
| 241 | call assert_equal(['aaaa', 'bbbb', 'cccc'], getline(2, '$')) |
| 242 | call feedkeys("u", 'xt') |
| 243 | call assert_equal(['aaaa'], getline(2, '$')) |
Bram Moolenaar | 5e4e1b1 | 2017-01-17 22:09:45 +0100 | [diff] [blame] | 244 | bwipe! |
| 245 | endfunc |
| 246 | |
| 247 | func Test_undojoin_redo() |
| 248 | new |
| 249 | call setline(1, ['first line', 'second line']) |
| 250 | call feedkeys("ixx\<Esc>", 'xt') |
| 251 | call feedkeys(":undojoin | redo\<CR>", 'xt') |
| 252 | call assert_equal('xxfirst line', getline(1)) |
| 253 | call assert_equal('second line', getline(2)) |
| 254 | bwipe! |
Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 255 | endfunc |
| 256 | |
Bram Moolenaar | 559b9c6 | 2019-12-15 18:09:19 +0100 | [diff] [blame] | 257 | " undojoin not allowed after undo |
| 258 | func Test_undojoin_after_undo() |
| 259 | new |
| 260 | call feedkeys("ixx\<Esc>u", 'xt') |
| 261 | call assert_fails(':undojoin', 'E790:') |
| 262 | bwipe! |
| 263 | endfunc |
| 264 | |
| 265 | " undojoin is a noop when no change yet, or when 'undolevels' is negative |
| 266 | func Test_undojoin_noop() |
| 267 | new |
| 268 | call feedkeys(":undojoin\<CR>", 'xt') |
| 269 | call assert_equal([''], getline(1, '$')) |
| 270 | setlocal undolevels=-1 |
| 271 | call feedkeys("ixx\<Esc>u", 'xt') |
| 272 | call feedkeys(":undojoin\<CR>", 'xt') |
| 273 | call assert_equal(['xx'], getline(1, '$')) |
| 274 | bwipe! |
| 275 | endfunc |
| 276 | |
Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 277 | func Test_undo_write() |
Bram Moolenaar | 5842a74 | 2017-11-04 22:36:53 +0100 | [diff] [blame] | 278 | call delete('Xtest') |
Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 279 | split Xtest |
| 280 | call feedkeys("ione one one\<Esc>", 'xt') |
| 281 | w! |
| 282 | call feedkeys("otwo\<Esc>", 'xt') |
| 283 | call feedkeys("otwo\<Esc>", 'xt') |
| 284 | w |
| 285 | call feedkeys("othree\<Esc>", 'xt') |
| 286 | call assert_equal(['one one one', 'two', 'two', 'three'], getline(1, '$')) |
| 287 | earlier 1f |
| 288 | call assert_equal(['one one one', 'two', 'two'], getline(1, '$')) |
| 289 | earlier 1f |
| 290 | call assert_equal(['one one one'], getline(1, '$')) |
| 291 | earlier 1f |
| 292 | call assert_equal([''], getline(1, '$')) |
| 293 | later 1f |
| 294 | call assert_equal(['one one one'], getline(1, '$')) |
| 295 | later 1f |
| 296 | call assert_equal(['one one one', 'two', 'two'], getline(1, '$')) |
| 297 | later 1f |
| 298 | call assert_equal(['one one one', 'two', 'two', 'three'], getline(1, '$')) |
| 299 | |
| 300 | close! |
| 301 | call delete('Xtest') |
| 302 | bwipe! Xtest |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 303 | |
| 304 | call assert_fails('earlier xyz', 'E475:') |
Bram Moolenaar | 170b10b | 2016-07-29 16:15:27 +0200 | [diff] [blame] | 305 | endfunc |
| 306 | |
| 307 | func Test_insert_expr() |
| 308 | new |
| 309 | " calling setline() triggers undo sync |
| 310 | call feedkeys("oa\<Esc>", 'xt') |
| 311 | call feedkeys("ob\<Esc>", 'xt') |
| 312 | set ul=100 |
| 313 | call feedkeys("o1\<Esc>a2\<C-R>=setline('.','1234')\<CR>\<CR>\<Esc>", 'x') |
| 314 | call assert_equal(['a', 'b', '120', '34'], getline(2, '$')) |
| 315 | call feedkeys("u", 'x') |
| 316 | call assert_equal(['a', 'b', '12'], getline(2, '$')) |
| 317 | call feedkeys("u", 'x') |
| 318 | call assert_equal(['a', 'b'], getline(2, '$')) |
| 319 | |
| 320 | call feedkeys("oc\<Esc>", 'xt') |
| 321 | set ul=100 |
| 322 | call feedkeys("o1\<Esc>a2\<C-R>=setline('.','1234')\<CR>\<CR>\<Esc>", 'x') |
| 323 | call assert_equal(['a', 'b', 'c', '120', '34'], getline(2, '$')) |
| 324 | call feedkeys("u", 'x') |
| 325 | call assert_equal(['a', 'b', 'c', '12'], getline(2, '$')) |
| 326 | |
| 327 | call feedkeys("od\<Esc>", 'xt') |
| 328 | set ul=100 |
| 329 | call feedkeys("o1\<Esc>a2\<C-R>=string(123)\<CR>\<Esc>", 'x') |
| 330 | call assert_equal(['a', 'b', 'c', '12', 'd', '12123'], getline(2, '$')) |
| 331 | call feedkeys("u", 'x') |
| 332 | call assert_equal(['a', 'b', 'c', '12', 'd'], getline(2, '$')) |
| 333 | |
| 334 | close! |
| 335 | endfunc |
Bram Moolenaar | cbd4de4 | 2017-01-07 16:14:57 +0100 | [diff] [blame] | 336 | |
| 337 | func Test_undofile_earlier() |
K.Takata | 0500e87 | 2022-09-08 12:28:02 +0100 | [diff] [blame] | 338 | if has('win32') |
| 339 | " FIXME: This test is flaky on MS-Windows. |
| 340 | let g:test_is_flaky = 1 |
| 341 | endif |
| 342 | |
Bram Moolenaar | cbd4de4 | 2017-01-07 16:14:57 +0100 | [diff] [blame] | 343 | " Issue #1254 |
| 344 | " create undofile with timestamps older than Vim startup time. |
| 345 | let t0 = localtime() - 43200 |
| 346 | call test_settime(t0) |
Bram Moolenaar | cce293f | 2022-08-15 17:28:27 +0100 | [diff] [blame] | 347 | new XfileEarlier |
Bram Moolenaar | cbd4de4 | 2017-01-07 16:14:57 +0100 | [diff] [blame] | 348 | call feedkeys("ione\<Esc>", 'xt') |
| 349 | set ul=100 |
| 350 | call test_settime(t0 + 1) |
| 351 | call feedkeys("otwo\<Esc>", 'xt') |
| 352 | set ul=100 |
| 353 | call test_settime(t0 + 2) |
| 354 | call feedkeys("othree\<Esc>", 'xt') |
| 355 | set ul=100 |
| 356 | w |
| 357 | wundo Xundofile |
| 358 | bwipe! |
| 359 | " restore normal timestamps. |
| 360 | call test_settime(0) |
Bram Moolenaar | cce293f | 2022-08-15 17:28:27 +0100 | [diff] [blame] | 361 | new XfileEarlier |
Bram Moolenaar | cbd4de4 | 2017-01-07 16:14:57 +0100 | [diff] [blame] | 362 | rundo Xundofile |
| 363 | earlier 1d |
| 364 | call assert_equal('', getline(1)) |
| 365 | bwipe! |
Bram Moolenaar | cce293f | 2022-08-15 17:28:27 +0100 | [diff] [blame] | 366 | call delete('XfileEarlier') |
Bram Moolenaar | cbd4de4 | 2017-01-07 16:14:57 +0100 | [diff] [blame] | 367 | call delete('Xundofile') |
| 368 | endfunc |
Bram Moolenaar | 15993ce | 2017-10-26 20:21:44 +0200 | [diff] [blame] | 369 | |
Bram Moolenaar | 559b9c6 | 2019-12-15 18:09:19 +0100 | [diff] [blame] | 370 | func Test_wundo_errors() |
| 371 | new |
| 372 | call setline(1, 'hello') |
| 373 | call assert_fails('wundo! Xdoesnotexist/Xundofile', 'E828:') |
| 374 | bwipe! |
| 375 | endfunc |
| 376 | |
| 377 | " Check that reading a truncated undo file doesn't hang. |
Bram Moolenaar | fb06d76 | 2019-08-04 18:55:35 +0200 | [diff] [blame] | 378 | func Test_undofile_truncated() |
| 379 | new |
| 380 | call setline(1, 'hello') |
| 381 | set ul=100 |
| 382 | wundo Xundofile |
| 383 | let contents = readfile('Xundofile', 'B') |
| 384 | |
| 385 | " try several sizes |
| 386 | for size in range(20, 500, 33) |
Bram Moolenaar | 5b148ef | 2022-10-15 21:35:56 +0100 | [diff] [blame] | 387 | call writefile(contents[0:size], 'Xundofile', 'D') |
Bram Moolenaar | fb06d76 | 2019-08-04 18:55:35 +0200 | [diff] [blame] | 388 | call assert_fails('rundo Xundofile', 'E825:') |
| 389 | endfor |
| 390 | |
| 391 | bwipe! |
Bram Moolenaar | fb06d76 | 2019-08-04 18:55:35 +0200 | [diff] [blame] | 392 | endfunc |
| 393 | |
Bram Moolenaar | 559b9c6 | 2019-12-15 18:09:19 +0100 | [diff] [blame] | 394 | func Test_rundo_errors() |
| 395 | call assert_fails('rundo XfileDoesNotExist', 'E822:') |
| 396 | |
Bram Moolenaar | 5b148ef | 2022-10-15 21:35:56 +0100 | [diff] [blame] | 397 | call writefile(['abc'], 'Xundofile', 'D') |
Bram Moolenaar | 559b9c6 | 2019-12-15 18:09:19 +0100 | [diff] [blame] | 398 | call assert_fails('rundo Xundofile', 'E823:') |
Bram Moolenaar | 559b9c6 | 2019-12-15 18:09:19 +0100 | [diff] [blame] | 399 | endfunc |
| 400 | |
Bram Moolenaar | 55b419b | 2020-10-04 19:56:39 +0200 | [diff] [blame] | 401 | func Test_undofile_next() |
| 402 | set undofile |
| 403 | new Xfoo.txt |
| 404 | execute "norm ix\<c-g>uy\<c-g>uz\<Esc>" |
| 405 | write |
| 406 | bwipe |
| 407 | |
| 408 | next Xfoo.txt |
| 409 | call assert_equal('xyz', getline(1)) |
| 410 | silent undo |
| 411 | call assert_equal('xy', getline(1)) |
| 412 | silent undo |
| 413 | call assert_equal('x', getline(1)) |
| 414 | bwipe! |
| 415 | |
| 416 | call delete('Xfoo.txt') |
| 417 | call delete('.Xfoo.txt.un~') |
| 418 | set undofile& |
| 419 | endfunc |
| 420 | |
Bram Moolenaar | 15993ce | 2017-10-26 20:21:44 +0200 | [diff] [blame] | 421 | " Test for undo working properly when executing commands from a register. |
| 422 | " Also test this in an empty buffer. |
| 423 | func Test_cmd_in_reg_undo() |
| 424 | enew! |
Bram Moolenaar | e5fa111 | 2018-05-26 18:46:30 +0200 | [diff] [blame] | 425 | let @a = "Ox\<Esc>jAy\<Esc>kdd" |
Bram Moolenaar | 15993ce | 2017-10-26 20:21:44 +0200 | [diff] [blame] | 426 | edit +/^$ test_undo.vim |
| 427 | normal @au |
| 428 | call assert_equal(0, &modified) |
| 429 | return |
| 430 | new |
| 431 | normal @au |
| 432 | call assert_equal(0, &modified) |
| 433 | only! |
Bram Moolenaar | e5fa111 | 2018-05-26 18:46:30 +0200 | [diff] [blame] | 434 | let @a = '' |
Bram Moolenaar | 15993ce | 2017-10-26 20:21:44 +0200 | [diff] [blame] | 435 | endfunc |
Bram Moolenaar | 95dbcbe | 2018-01-27 21:01:34 +0100 | [diff] [blame] | 436 | |
| 437 | " This used to cause an illegal memory access |
| 438 | func Test_undo_append() |
| 439 | new |
| 440 | call feedkeys("axx\<Esc>v", 'xt') |
| 441 | undo |
| 442 | norm o |
| 443 | quit |
| 444 | endfunc |
Bram Moolenaar | ce46d93 | 2018-01-30 22:46:06 +0100 | [diff] [blame] | 445 | |
| 446 | func Test_undo_0() |
| 447 | new |
| 448 | set ul=100 |
| 449 | normal i1 |
| 450 | undo |
| 451 | normal i2 |
| 452 | undo |
| 453 | normal i3 |
| 454 | |
| 455 | undo 0 |
| 456 | let d = undotree() |
| 457 | call assert_equal('', getline(1)) |
| 458 | call assert_equal(0, d.seq_cur) |
| 459 | |
| 460 | redo |
| 461 | let d = undotree() |
| 462 | call assert_equal('3', getline(1)) |
| 463 | call assert_equal(3, d.seq_cur) |
| 464 | |
| 465 | undo 2 |
| 466 | undo 0 |
| 467 | let d = undotree() |
| 468 | call assert_equal('', getline(1)) |
| 469 | call assert_equal(0, d.seq_cur) |
| 470 | |
| 471 | redo |
| 472 | let d = undotree() |
| 473 | call assert_equal('2', getline(1)) |
| 474 | call assert_equal(2, d.seq_cur) |
| 475 | |
| 476 | undo 1 |
| 477 | undo 0 |
| 478 | let d = undotree() |
| 479 | call assert_equal('', getline(1)) |
| 480 | call assert_equal(0, d.seq_cur) |
| 481 | |
| 482 | redo |
| 483 | let d = undotree() |
| 484 | call assert_equal('1', getline(1)) |
| 485 | call assert_equal(1, d.seq_cur) |
| 486 | |
| 487 | bwipe! |
| 488 | endfunc |
Bram Moolenaar | f12519d | 2018-02-06 22:52:49 +0100 | [diff] [blame] | 489 | |
Bram Moolenaar | 559b9c6 | 2019-12-15 18:09:19 +0100 | [diff] [blame] | 490 | " undo or redo are noop if there is nothing to undo or redo |
| 491 | func Test_undo_redo_noop() |
| 492 | new |
| 493 | call assert_fails('undo 2', 'E830:') |
| 494 | |
| 495 | message clear |
| 496 | undo |
| 497 | let messages = split(execute('message'), "\n") |
| 498 | call assert_equal('Already at oldest change', messages[-1]) |
| 499 | |
| 500 | message clear |
| 501 | redo |
| 502 | let messages = split(execute('message'), "\n") |
| 503 | call assert_equal('Already at newest change', messages[-1]) |
| 504 | |
| 505 | bwipe! |
| 506 | endfunc |
| 507 | |
Bram Moolenaar | f12519d | 2018-02-06 22:52:49 +0100 | [diff] [blame] | 508 | func Test_redo_empty_line() |
| 509 | new |
| 510 | exe "norm\x16r\x160" |
| 511 | exe "norm." |
| 512 | bwipe! |
| 513 | endfunc |
Bram Moolenaar | e5fa111 | 2018-05-26 18:46:30 +0200 | [diff] [blame] | 514 | |
| 515 | funct Test_undofile() |
| 516 | " Test undofile() without setting 'undodir'. |
| 517 | if has('persistent_undo') |
| 518 | call assert_equal(fnamemodify('.Xundofoo.un~', ':p'), undofile('Xundofoo')) |
| 519 | else |
| 520 | call assert_equal('', undofile('Xundofoo')) |
| 521 | endif |
| 522 | call assert_equal('', undofile('')) |
| 523 | |
| 524 | " Test undofile() with 'undodir' set to to an existing directory. |
| 525 | call mkdir('Xundodir') |
| 526 | set undodir=Xundodir |
| 527 | let cwd = getcwd() |
| 528 | if has('win32') |
| 529 | " Replace windows drive such as C:... into C%... |
Bram Moolenaar | 56242f2 | 2018-12-14 15:48:48 +0100 | [diff] [blame] | 530 | let cwd = substitute(cwd, '^\([a-zA-Z]\):', '\1%', 'g') |
Bram Moolenaar | e5fa111 | 2018-05-26 18:46:30 +0200 | [diff] [blame] | 531 | endif |
| 532 | let cwd = substitute(cwd . '/Xundofoo', '/', '%', 'g') |
| 533 | if has('persistent_undo') |
| 534 | call assert_equal('Xundodir/' . cwd, undofile('Xundofoo')) |
| 535 | else |
| 536 | call assert_equal('', undofile('Xundofoo')) |
| 537 | endif |
| 538 | call assert_equal('', undofile('')) |
| 539 | call delete('Xundodir', 'd') |
| 540 | |
| 541 | " Test undofile() with 'undodir' set to a non-existing directory. |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 542 | call assert_equal('', 'Xundofoo'->undofile()) |
Bram Moolenaar | e5fa111 | 2018-05-26 18:46:30 +0200 | [diff] [blame] | 543 | |
Bram Moolenaar | 077ff43 | 2019-10-28 00:42:21 +0100 | [diff] [blame] | 544 | if !has('win32') && isdirectory('/tmp') |
Bram Moolenaar | e9ebc9a | 2019-05-19 15:27:14 +0200 | [diff] [blame] | 545 | set undodir=/tmp |
Bram Moolenaar | 2b39d80 | 2019-05-19 16:38:56 +0200 | [diff] [blame] | 546 | if has('osx') |
| 547 | call assert_equal('/tmp/%private%tmp%file', undofile('///tmp/file')) |
| 548 | else |
| 549 | call assert_equal('/tmp/%tmp%file', undofile('///tmp/file')) |
| 550 | endif |
Bram Moolenaar | e9ebc9a | 2019-05-19 15:27:14 +0200 | [diff] [blame] | 551 | endif |
| 552 | |
Bram Moolenaar | e5fa111 | 2018-05-26 18:46:30 +0200 | [diff] [blame] | 553 | set undodir& |
| 554 | endfunc |
Bram Moolenaar | 3e2d1c8 | 2019-12-14 20:35:01 +0100 | [diff] [blame] | 555 | |
| 556 | " Tests for the undo file |
| 557 | " Explicitly break changes up in undo-able pieces by setting 'undolevels'. |
| 558 | func Test_undofile_2() |
| 559 | set undolevels=100 undofile |
| 560 | edit Xtestfile |
| 561 | call append(0, 'this is one line') |
| 562 | call cursor(1, 1) |
| 563 | |
| 564 | " first a simple one-line change. |
| 565 | set undolevels=100 |
| 566 | s/one/ONE/ |
| 567 | set undolevels=100 |
| 568 | write |
| 569 | bwipe! |
| 570 | edit Xtestfile |
| 571 | undo |
| 572 | call assert_equal('this is one line', getline(1)) |
| 573 | |
| 574 | " change in original file fails check |
| 575 | set noundofile |
| 576 | edit! Xtestfile |
| 577 | s/line/Line/ |
| 578 | write |
| 579 | set undofile |
| 580 | bwipe! |
| 581 | edit Xtestfile |
| 582 | undo |
| 583 | call assert_equal('this is ONE Line', getline(1)) |
| 584 | |
| 585 | " add 10 lines, delete 6 lines, undo 3 |
| 586 | set undofile |
Bram Moolenaar | b8bd2e6 | 2021-08-21 17:13:14 +0200 | [diff] [blame] | 587 | call setbufline('%', 1, ['one', 'two', 'three', 'four', 'five', 'six', |
Bram Moolenaar | 3e2d1c8 | 2019-12-14 20:35:01 +0100 | [diff] [blame] | 588 | \ 'seven', 'eight', 'nine', 'ten']) |
| 589 | set undolevels=100 |
| 590 | normal 3Gdd |
| 591 | set undolevels=100 |
| 592 | normal dd |
| 593 | set undolevels=100 |
| 594 | normal dd |
| 595 | set undolevels=100 |
| 596 | normal dd |
| 597 | set undolevels=100 |
| 598 | normal dd |
| 599 | set undolevels=100 |
| 600 | normal dd |
| 601 | set undolevels=100 |
| 602 | write |
| 603 | bwipe! |
| 604 | edit Xtestfile |
| 605 | normal uuu |
| 606 | call assert_equal(['one', 'two', 'six', 'seven', 'eight', 'nine', 'ten'], |
| 607 | \ getline(1, '$')) |
| 608 | |
| 609 | " Test that reading the undofiles when setting undofile works |
| 610 | set noundofile undolevels=0 |
| 611 | exe "normal i\n" |
| 612 | undo |
| 613 | edit! Xtestfile |
| 614 | set undofile undolevels=100 |
| 615 | normal uuuuuu |
| 616 | call assert_equal(['one', 'two', 'three', 'four', 'five', 'six', 'seven', |
| 617 | \ 'eight', 'nine', 'ten'], getline(1, '$')) |
| 618 | |
| 619 | bwipe! |
| 620 | call delete('Xtestfile') |
| 621 | let ufile = has('vms') ? '_un_Xtestfile' : '.Xtestfile.un~' |
| 622 | call delete(ufile) |
| 623 | set undofile& undolevels& |
| 624 | endfunc |
| 625 | |
| 626 | " Test 'undofile' using a file encrypted with 'zip' crypt method |
| 627 | func Test_undofile_cryptmethod_zip() |
| 628 | edit Xtestfile |
| 629 | set undofile cryptmethod=zip |
| 630 | call append(0, ['monday', 'tuesday', 'wednesday', 'thursday', 'friday']) |
| 631 | call cursor(5, 1) |
| 632 | |
| 633 | set undolevels=100 |
| 634 | normal kkkdd |
| 635 | set undolevels=100 |
| 636 | normal dd |
| 637 | set undolevels=100 |
| 638 | normal dd |
| 639 | set undolevels=100 |
| 640 | " encrypt the file using key 'foobar' |
| 641 | call feedkeys("foobar\nfoobar\n") |
| 642 | X |
| 643 | write! |
| 644 | bwipe! |
| 645 | |
| 646 | call feedkeys("foobar\n") |
| 647 | edit Xtestfile |
| 648 | set key= |
| 649 | normal uu |
| 650 | call assert_equal(['monday', 'wednesday', 'thursday', 'friday', ''], |
| 651 | \ getline(1, '$')) |
| 652 | |
| 653 | bwipe! |
| 654 | call delete('Xtestfile') |
| 655 | let ufile = has('vms') ? '_un_Xtestfile' : '.Xtestfile.un~' |
| 656 | call delete(ufile) |
| 657 | set undofile& undolevels& cryptmethod& |
| 658 | endfunc |
| 659 | |
| 660 | " Test 'undofile' using a file encrypted with 'blowfish' crypt method |
| 661 | func Test_undofile_cryptmethod_blowfish() |
| 662 | edit Xtestfile |
| 663 | set undofile cryptmethod=blowfish |
| 664 | call append(0, ['jan', 'feb', 'mar', 'apr', 'jun']) |
| 665 | call cursor(5, 1) |
| 666 | |
| 667 | set undolevels=100 |
| 668 | exe 'normal kk0ifoo ' |
| 669 | set undolevels=100 |
| 670 | normal dd |
| 671 | set undolevels=100 |
| 672 | exe 'normal ibar ' |
| 673 | set undolevels=100 |
| 674 | " encrypt the file using key 'foobar' |
| 675 | call feedkeys("foobar\nfoobar\n") |
| 676 | X |
| 677 | write! |
| 678 | bwipe! |
| 679 | |
| 680 | call feedkeys("foobar\n") |
| 681 | edit Xtestfile |
| 682 | set key= |
| 683 | call search('bar') |
| 684 | call assert_equal('bar apr', getline('.')) |
| 685 | undo |
| 686 | call assert_equal('apr', getline('.')) |
| 687 | undo |
| 688 | call assert_equal('foo mar', getline('.')) |
| 689 | undo |
| 690 | call assert_equal('mar', getline('.')) |
| 691 | |
| 692 | bwipe! |
| 693 | call delete('Xtestfile') |
| 694 | let ufile = has('vms') ? '_un_Xtestfile' : '.Xtestfile.un~' |
| 695 | call delete(ufile) |
| 696 | set undofile& undolevels& cryptmethod& |
| 697 | endfunc |
| 698 | |
| 699 | " Test 'undofile' using a file encrypted with 'blowfish2' crypt method |
| 700 | func Test_undofile_cryptmethod_blowfish2() |
| 701 | edit Xtestfile |
| 702 | set undofile cryptmethod=blowfish2 |
| 703 | call append(0, ['jan', 'feb', 'mar', 'apr', 'jun']) |
| 704 | call cursor(5, 1) |
| 705 | |
| 706 | set undolevels=100 |
| 707 | exe 'normal kk0ifoo ' |
| 708 | set undolevels=100 |
| 709 | normal dd |
| 710 | set undolevels=100 |
| 711 | exe 'normal ibar ' |
| 712 | set undolevels=100 |
| 713 | " encrypt the file using key 'foo2bar' |
| 714 | call feedkeys("foo2bar\nfoo2bar\n") |
| 715 | X |
| 716 | write! |
| 717 | bwipe! |
| 718 | |
| 719 | call feedkeys("foo2bar\n") |
| 720 | edit Xtestfile |
| 721 | set key= |
| 722 | call search('bar') |
| 723 | call assert_equal('bar apr', getline('.')) |
| 724 | normal u |
| 725 | call assert_equal('apr', getline('.')) |
| 726 | normal u |
| 727 | call assert_equal('foo mar', getline('.')) |
| 728 | normal u |
| 729 | call assert_equal('mar', getline('.')) |
| 730 | |
| 731 | bwipe! |
| 732 | call delete('Xtestfile') |
| 733 | let ufile = has('vms') ? '_un_Xtestfile' : '.Xtestfile.un~' |
| 734 | call delete(ufile) |
| 735 | set undofile& undolevels& cryptmethod& |
| 736 | endfunc |
| 737 | |
Bram Moolenaar | f4fcedc | 2021-03-15 18:36:20 +0100 | [diff] [blame] | 738 | " Test for redoing with incrementing numbered registers |
| 739 | func Test_redo_repeat_numbered_register() |
| 740 | new |
| 741 | for [i, v] in [[1, 'one'], [2, 'two'], [3, 'three'], |
| 742 | \ [4, 'four'], [5, 'five'], [6, 'six'], |
| 743 | \ [7, 'seven'], [8, 'eight'], [9, 'nine']] |
| 744 | exe 'let @' .. i .. '="' .. v .. '\n"' |
| 745 | endfor |
| 746 | call feedkeys('"1p.........', 'xt') |
| 747 | call assert_equal(['', 'one', 'two', 'three', 'four', 'five', 'six', |
| 748 | \ 'seven', 'eight', 'nine', 'nine'], getline(1, '$')) |
| 749 | bwipe! |
| 750 | endfunc |
| 751 | |
Bram Moolenaar | 1f448d9 | 2021-03-22 19:37:06 +0100 | [diff] [blame] | 752 | " Test for redo in insert mode using CTRL-O with multibyte characters |
| 753 | func Test_redo_multibyte_in_insert_mode() |
| 754 | new |
| 755 | call feedkeys("a\<C-K>ft", 'xt') |
| 756 | call feedkeys("uiHe\<C-O>.llo", 'xt') |
| 757 | call assert_equal("He\ufb05llo", getline(1)) |
| 758 | bwipe! |
| 759 | endfunc |
| 760 | |
LemonBoy | 82444ce | 2022-05-12 15:39:31 +0100 | [diff] [blame] | 761 | func Test_undo_mark() |
| 762 | new |
| 763 | " The undo is applied to the only line. |
| 764 | call setline(1, 'hello') |
| 765 | call feedkeys("ggyiw$p", 'xt') |
| 766 | undo |
| 767 | call assert_equal([0, 1, 1, 0], getpos("'[")) |
| 768 | call assert_equal([0, 1, 1, 0], getpos("']")) |
| 769 | " The undo removes the last line. |
| 770 | call feedkeys("Goaaaa\<Esc>", 'xt') |
| 771 | call feedkeys("obbbb\<Esc>", 'xt') |
| 772 | undo |
| 773 | call assert_equal([0, 2, 1, 0], getpos("'[")) |
| 774 | call assert_equal([0, 2, 1, 0], getpos("']")) |
| 775 | bwipe! |
| 776 | endfunc |
| 777 | |
Bram Moolenaar | 3e2d1c8 | 2019-12-14 20:35:01 +0100 | [diff] [blame] | 778 | " vim: shiftwidth=2 sts=2 expandtab |