blob: caf82ee80d576e79b88243b77ff6759eb354f3ea [file] [log] [blame]
Bram Moolenaardf7df592020-06-14 13:50:55 +02001" Test for the various 'cpoptions' (cpo) flags
Bram Moolenaarc9630d22020-06-13 13:20:48 +02002
3source check.vim
Bram Moolenaardf7df592020-06-14 13:50:55 +02004source shared.vim
Bram Moolenaarc9630d22020-06-13 13:20:48 +02005source view_util.vim
6
7" Test for the 'a' flag in 'cpo'. Reading a file should set the alternate
8" file name.
9func Test_cpo_a()
10 let save_cpo = &cpo
11 call writefile(['one'], 'Xfile')
12 " Wipe out all the buffers, so that the alternate file is empty
13 edit Xfoo | %bw
14 set cpo-=a
15 new
16 read Xfile
17 call assert_equal('', @#)
18 %d
19 set cpo+=a
20 read Xfile
21 call assert_equal('Xfile', @#)
22 close!
23 call delete('Xfile')
24 let &cpo = save_cpo
25endfunc
26
27" Test for the 'A' flag in 'cpo'. Writing a file should set the alternate
28" file name.
29func Test_cpo_A()
30 let save_cpo = &cpo
31 " Wipe out all the buffers, so that the alternate file is empty
32 edit Xfoo | %bw
33 set cpo-=A
34 new Xfile1
35 write Xfile2
36 call assert_equal('', @#)
37 %bw
38 call delete('Xfile2')
39 new Xfile1
40 set cpo+=A
41 write Xfile2
42 call assert_equal('Xfile2', @#)
43 close!
44 call delete('Xfile2')
45 let &cpo = save_cpo
46endfunc
47
48" Test for the 'b' flag in 'cpo'. "\|" at the end of a map command is
49" recognized as the end of the map.
50func Test_cpo_b()
51 let save_cpo = &cpo
52 set cpo+=b
53 nnoremap <F5> :pwd\<CR>\|let i = 1
54 call assert_equal(':pwd\<CR>\', maparg('<F5>'))
55 nunmap <F5>
56 exe "nnoremap <F5> :pwd\<C-V>|let i = 1"
57 call assert_equal(':pwd|let i = 1', maparg('<F5>'))
58 nunmap <F5>
59 set cpo-=b
60 nnoremap <F5> :pwd\<CR>\|let i = 1
61 call assert_equal(':pwd\<CR>|let i = 1', maparg('<F5>'))
62 let &cpo = save_cpo
63 nunmap <F5>
64endfunc
65
Bram Moolenaardf7df592020-06-14 13:50:55 +020066" Test for the 'B' flag in 'cpo'. A backslash in mappings, abbreviations, user
67" commands and menu commands has no special meaning.
68func Test_cpo_B()
69 let save_cpo = &cpo
70 new
71 set cpo-=B
72 iabbr <buffer> abc ab\<BS>d
73 exe "normal iabc "
74 call assert_equal('ab<BS>d ', getline(1))
75 %d
76 set cpo+=B
77 iabbr <buffer> abc ab\<BS>d
78 exe "normal iabc "
79 call assert_equal('abd ', getline(1))
80 close!
81 let &cpo = save_cpo
82endfunc
83
Bram Moolenaarc9630d22020-06-13 13:20:48 +020084" Test for the 'c' flag in 'cpo'.
85func Test_cpo_c()
86 let save_cpo = &cpo
87 set cpo+=c
88 new
89 call setline(1, ' abababababab')
90 exe "normal gg/abab\<CR>"
91 call assert_equal(3, searchcount().total)
92 set cpo-=c
93 exe "normal gg/abab\<CR>"
94 call assert_equal(5, searchcount().total)
95 close!
96 let &cpo = save_cpo
97endfunc
98
99" Test for the 'C' flag in 'cpo' (line continuation)
100func Test_cpo_C()
101 let save_cpo = &cpo
102 call writefile(['let l = [', '\ 1,', '\ 2]'], 'Xfile')
103 set cpo-=C
104 source Xfile
105 call assert_equal([1, 2], g:l)
106 set cpo+=C
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200107 call assert_fails('source Xfile', ['E697:', 'E10:'])
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200108 call delete('Xfile')
109 let &cpo = save_cpo
110endfunc
111
112" Test for the 'd' flag in 'cpo' (tags relative to the current file)
113func Test_cpo_d()
114 let save_cpo = &cpo
115 call mkdir('Xdir')
116 call writefile(["one\tXfile1\t/^one$/"], 'tags')
117 call writefile(["two\tXfile2\t/^two$/"], 'Xdir/tags')
118 set tags=./tags
119 set cpo-=d
120 edit Xdir/Xfile
121 call assert_equal('two', taglist('.*')[0].name)
122 set cpo+=d
123 call assert_equal('one', taglist('.*')[0].name)
124 %bw!
125 call delete('tags')
126 call delete('Xdir', 'rf')
127 set tags&
128 let &cpo = save_cpo
129endfunc
130
131" Test for the 'D' flag in 'cpo' (digraph after a r, f or t)
132func Test_cpo_D()
133 CheckFeature digraphs
134 let save_cpo = &cpo
135 new
136 set cpo-=D
137 call setline(1, 'abcdefgh|')
138 exe "norm! 1gg0f\<c-k>!!"
139 call assert_equal(9, col('.'))
140 set cpo+=D
141 exe "norm! 1gg0f\<c-k>!!"
142 call assert_equal(1, col('.'))
143 set cpo-=D
144 close!
145 let &cpo = save_cpo
146endfunc
147
148" Test for the 'e' flag in 'cpo'
149func Test_cpo_e()
150 let save_cpo = &cpo
151 let @a='let i = 45'
152 set cpo+=e
153 call feedkeys(":@a\<CR>", 'xt')
154 call assert_equal(45, i)
155 set cpo-=e
156 call feedkeys(":@a\<CR>6\<CR>", 'xt')
157 call assert_equal(456, i)
158 let &cpo = save_cpo
159endfunc
160
161" Test for the 'E' flag in 'cpo' with yank, change, delete, etc. operators
162func Test_cpo_E()
163 new
164 call setline(1, '')
165 set cpo+=E
166 " yank an empty line
167 call assert_beeps('normal "ayl')
168 " change an empty line
169 call assert_beeps('normal lcTa')
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200170 call assert_beeps('normal 0c0')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200171 " delete an empty line
172 call assert_beeps('normal D')
173 call assert_beeps('normal dl')
174 call assert_equal('', getline(1))
175 " change case of an empty line
176 call assert_beeps('normal gul')
177 call assert_beeps('normal gUl')
178 " replace a character
179 call assert_beeps('normal vrx')
180 " increment and decrement
181 call assert_beeps('exe "normal v\<C-A>"')
182 call assert_beeps('exe "normal v\<C-X>"')
183 set cpo-=E
184 close!
185endfunc
186
187" Test for the 'f' flag in 'cpo' (read in an empty buffer sets the file name)
188func Test_cpo_f()
189 let save_cpo = &cpo
190 new
191 set cpo-=f
192 read test_cpoptions.vim
193 call assert_equal('', @%)
194 %d
195 set cpo+=f
196 read test_cpoptions.vim
197 call assert_equal('test_cpoptions.vim', @%)
198 close!
199 let &cpo = save_cpo
200endfunc
201
202" Test for the 'F' flag in 'cpo' (write in an empty buffer sets the file name)
203func Test_cpo_F()
204 let save_cpo = &cpo
205 new
206 set cpo-=F
207 write Xfile
208 call assert_equal('', @%)
209 call delete('Xfile')
210 set cpo+=F
211 write Xfile
212 call assert_equal('Xfile', @%)
213 close!
214 call delete('Xfile')
215 let &cpo = save_cpo
216endfunc
217
218" Test for the 'g' flag in 'cpo' (jump to line 1 when re-editing a file)
219func Test_cpo_g()
220 let save_cpo = &cpo
221 new test_cpoptions.vim
222 set cpo-=g
223 normal 20G
224 edit
225 call assert_equal(20, line('.'))
226 set cpo+=g
227 edit
228 call assert_equal(1, line('.'))
229 close!
230 let &cpo = save_cpo
231endfunc
232
233" Test for inserting text in a line with only spaces ('H' flag in 'cpoptions')
234func Test_cpo_H()
235 let save_cpo = &cpo
236 new
237 set cpo-=H
238 call setline(1, ' ')
239 normal! Ia
240 call assert_equal(' a', getline(1))
241 set cpo+=H
242 call setline(1, ' ')
243 normal! Ia
244 call assert_equal(' a ', getline(1))
245 close!
246 let &cpo = save_cpo
247endfunc
248
Bram Moolenaardf7df592020-06-14 13:50:55 +0200249" TODO: Add a test for the 'i' flag in 'cpo'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200250" Interrupting the reading of a file will leave it modified.
Bram Moolenaardf7df592020-06-14 13:50:55 +0200251
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200252" Test for the 'I' flag in 'cpo' (deleting autoindent when using arrow keys)
253func Test_cpo_I()
254 let save_cpo = &cpo
255 new
256 setlocal autoindent
257 set cpo+=I
258 exe "normal i one\<CR>\<Up>"
259 call assert_equal(' ', getline(2))
260 set cpo-=I
261 %d
262 exe "normal i one\<CR>\<Up>"
263 call assert_equal('', getline(2))
264 close!
265 let &cpo = save_cpo
266endfunc
267
Bram Moolenaardf7df592020-06-14 13:50:55 +0200268" Test for the 'j' flag in 'cpo' is in the test_join.vim file.
269
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200270" Test for the 'J' flag in 'cpo' (two spaces after a sentence)
271func Test_cpo_J()
272 let save_cpo = &cpo
273 new
274 set cpo-=J
275 call setline(1, 'one. two! three? four."'' five.)]')
276 normal 0
277 for colnr in [6, 12, 19, 28, 34]
278 normal )
279 call assert_equal(colnr, col('.'))
280 endfor
281 for colnr in [28, 19, 12, 6, 1]
282 normal (
283 call assert_equal(colnr, col('.'))
284 endfor
285 set cpo+=J
286 normal 0
287 for colnr in [12, 28, 34]
288 normal )
289 call assert_equal(colnr, col('.'))
290 endfor
291 for colnr in [28, 12, 1]
292 normal (
293 call assert_equal(colnr, col('.'))
294 endfor
295 close!
296 let &cpo = save_cpo
297endfunc
298
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200299" TODO: Add a test for the 'k' flag in 'cpo'.
300" Disable the recognition of raw key codes in mappings, abbreviations, and the
301" "to" part of menu commands.
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200302
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200303" TODO: Add a test for the 'K' flag in 'cpo'.
304" Don't wait for a key code to complete when it is halfway a mapping.
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200305
306" Test for the 'l' flag in 'cpo' (backslash in a [] range)
307func Test_cpo_l()
308 let save_cpo = &cpo
309 new
310 call setline(1, ['', "a\tc" .. '\t'])
311 set cpo-=l
312 exe 'normal gg/[\t]' .. "\<CR>"
313 call assert_equal([2, 8], [col('.'), virtcol('.')])
314 set cpo+=l
315 exe 'normal gg/[\t]' .. "\<CR>"
316 call assert_equal([4, 10], [col('.'), virtcol('.')])
317 close!
318 let &cpo = save_cpo
319endfunc
320
321" Test for inserting tab in virtual replace mode ('L' flag in 'cpoptions')
322func Test_cpo_L()
323 let save_cpo = &cpo
324 new
325 set cpo-=L
326 call setline(1, 'abcdefghijklmnopqr')
327 exe "normal 0gR\<Tab>"
328 call assert_equal("\<Tab>ijklmnopqr", getline(1))
329 set cpo+=L
330 set list
331 call setline(1, 'abcdefghijklmnopqr')
332 exe "normal 0gR\<Tab>"
333 call assert_equal("\<Tab>cdefghijklmnopqr", getline(1))
334 set nolist
335 call setline(1, 'abcdefghijklmnopqr')
336 exe "normal 0gR\<Tab>"
337 call assert_equal("\<Tab>ijklmnopqr", getline(1))
338 close!
339 let &cpo = save_cpo
340endfunc
341
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200342" TODO: Add a test for the 'm' flag in 'cpo'.
343" When included, a showmatch will always wait half a second. When not
344" included, a showmatch will wait half a second or until a character is typed.
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200345
346" Test for the 'M' flag in 'cpo' (% with escape parenthesis)
347func Test_cpo_M()
348 let save_cpo = &cpo
349 new
350 call setline(1, ['( \( )', '\( ( \)'])
351
352 set cpo-=M
353 call cursor(1, 1)
354 normal %
355 call assert_equal(6, col('.'))
356 call cursor(1, 4)
357 call assert_beeps('normal %')
358 call cursor(2, 2)
359 normal %
360 call assert_equal(7, col('.'))
361 call cursor(2, 4)
362 call assert_beeps('normal %')
363
364 set cpo+=M
365 call cursor(1, 4)
366 normal %
367 call assert_equal(6, col('.'))
368 call cursor(1, 1)
369 call assert_beeps('normal %')
370 call cursor(2, 4)
371 normal %
372 call assert_equal(7, col('.'))
373 call cursor(2, 1)
374 call assert_beeps('normal %')
375
376 close!
377 let &cpo = save_cpo
378endfunc
379
380" Test for the 'n' flag in 'cpo' (using number column for wrapped lines)
381func Test_cpo_n()
382 let save_cpo = &cpo
383 new
384 call setline(1, repeat('a', &columns))
385 setlocal number
386 set cpo-=n
387 redraw!
388 call assert_equal(' aaaa', Screenline(2))
389 set cpo+=n
390 redraw!
391 call assert_equal('aaaa', Screenline(2))
392 close!
393 let &cpo = save_cpo
394endfunc
395
396" Test for the 'o' flag in 'cpo' (line offset to search command)
397func Test_cpo_o()
398 let save_cpo = &cpo
399 new
400 call setline(1, ['', 'one', 'two', 'three', 'one', 'two', 'three'])
401 set cpo-=o
402 exe "normal /one/+2\<CR>"
403 normal n
404 call assert_equal(7, line('.'))
405 set cpo+=o
406 exe "normal /one/+2\<CR>"
407 normal n
408 call assert_equal(5, line('.'))
409 close!
410 let &cpo = save_cpo
411endfunc
412
413" Test for the 'O' flag in 'cpo' (overwriting an existing file)
414func Test_cpo_O()
415 let save_cpo = &cpo
416 new Xfile
417 call setline(1, 'one')
418 call writefile(['two'], 'Xfile')
419 set cpo-=O
420 call assert_fails('write', 'E13:')
421 set cpo+=O
422 write
423 call assert_equal(['one'], readfile('Xfile'))
424 close!
425 call delete('Xfile')
426 let &cpo = save_cpo
427endfunc
428
Bram Moolenaardf7df592020-06-14 13:50:55 +0200429" Test for the 'p' flag in 'cpo' is in the test_lispwords.vim file.
430
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200431" Test for the 'P' flag in 'cpo' (appending to a file sets the current file
432" name)
433func Test_cpo_P()
434 let save_cpo = &cpo
435 call writefile([], 'Xfile')
436 new
437 call setline(1, 'one')
438 set cpo+=F
439 set cpo-=P
440 write >> Xfile
441 call assert_equal('', @%)
442 set cpo+=P
443 write >> Xfile
444 call assert_equal('Xfile', @%)
445 close!
446 call delete('Xfile')
447 let &cpo = save_cpo
448endfunc
449
450" Test for the 'q' flag in 'cpo' (joining multiple lines)
451func Test_cpo_q()
452 let save_cpo = &cpo
453 new
454 call setline(1, ['one', 'two', 'three', 'four', 'five'])
455 set cpo-=q
456 normal gg4J
457 call assert_equal(14, col('.'))
458 %d
459 call setline(1, ['one', 'two', 'three', 'four', 'five'])
460 set cpo+=q
461 normal gg4J
462 call assert_equal(4, col('.'))
463 close!
464 let &cpo = save_cpo
465endfunc
466
467" Test for the 'r' flag in 'cpo' (redo command with a search motion)
468func Test_cpo_r()
469 let save_cpo = &cpo
470 new
471 call setline(1, repeat(['one two three four'], 2))
472 set cpo-=r
473 exe "normal ggc/two\<CR>abc "
474 let @/ = 'three'
475 normal 2G.
476 call assert_equal('abc two three four', getline(2))
477 %d
478 call setline(1, repeat(['one two three four'], 2))
479 set cpo+=r
480 exe "normal ggc/two\<CR>abc "
481 let @/ = 'three'
482 normal 2G.
483 call assert_equal('abc three four', getline(2))
484 close!
485 let &cpo = save_cpo
486endfunc
487
488" Test for the 'R' flag in 'cpo' (clear marks after a filter command)
489func Test_cpo_R()
490 CheckUnix
491 let save_cpo = &cpo
492 new
493 call setline(1, ['three', 'one', 'two'])
494 set cpo-=R
495 3mark r
496 %!sort
497 call assert_equal(3, line("'r"))
498 %d
499 call setline(1, ['three', 'one', 'two'])
500 set cpo+=R
501 3mark r
502 %!sort
503 call assert_equal(0, line("'r"))
504 close!
505 let &cpo = save_cpo
506endfunc
507
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200508" TODO: Add a test for the 's' flag in 'cpo'.
509" Set buffer options when entering the buffer for the first time. If not
510" present the options are set when the buffer is created.
Bram Moolenaardf7df592020-06-14 13:50:55 +0200511
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200512" Test for the 'S' flag in 'cpo' (copying buffer options)
513func Test_cpo_S()
514 let save_cpo = &cpo
515 new Xfile1
516 set noautoindent
517 new Xfile2
518 set cpo-=S
519 set autoindent
520 wincmd p
521 call assert_equal(0, &autoindent)
522 wincmd p
523 call assert_equal(1, &autoindent)
524 set cpo+=S
525 wincmd p
526 call assert_equal(1, &autoindent)
527 set noautoindent
528 wincmd p
529 call assert_equal(0, &autoindent)
530 wincmd t
531 close!
532 close!
533 let &cpo = save_cpo
534endfunc
535
Bram Moolenaardf7df592020-06-14 13:50:55 +0200536" Test for the 't' flag in 'cpo' is in the test_tagjump.vim file.
537
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200538" Test for the 'u' flag in 'cpo' (Vi-compatible undo)
539func Test_cpo_u()
540 let save_cpo = &cpo
541 new
542 set cpo-=u
543 exe "normal iabc\<C-G>udef\<C-G>ughi"
544 normal uu
545 call assert_equal('abc', getline(1))
546 %d
547 set cpo+=u
548 exe "normal iabc\<C-G>udef\<C-G>ughi"
549 normal uu
550 call assert_equal('abcdefghi', getline(1))
551 close!
552 let &cpo = save_cpo
553endfunc
554
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200555" TODO: Add a test for the 'v' flag in 'cpo'.
556" Backspaced characters remain visible on the screen in Insert mode.
Bram Moolenaardf7df592020-06-14 13:50:55 +0200557
558" Test for the 'w' flag in 'cpo' ('cw' on a blank character changes only one
559" character)
560func Test_cpo_w()
561 let save_cpo = &cpo
562 new
563 set cpo+=w
564 call setline(1, 'here are some words')
565 norm! 1gg0elcwZZZ
566 call assert_equal('hereZZZ are some words', getline('.'))
567 norm! 1gg2elcWYYY
568 call assert_equal('hereZZZ areYYY some words', getline('.'))
569 set cpo-=w
570 call setline(1, 'here are some words')
571 norm! 1gg0elcwZZZ
572 call assert_equal('hereZZZare some words', getline('.'))
573 norm! 1gg2elcWYYY
574 call assert_equal('hereZZZare someYYYwords', getline('.'))
575 close!
576 let &cpo = save_cpo
577endfunc
578
579" Test for the 'W' flag in 'cpo' is in the test_writefile.vim file
580
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200581" Test for the 'x' flag in 'cpo' (Esc on command-line executes command)
582func Test_cpo_x()
583 let save_cpo = &cpo
584 set cpo-=x
585 let i = 1
586 call feedkeys(":let i=10\<Esc>", 'xt')
587 call assert_equal(1, i)
588 set cpo+=x
589 call feedkeys(":let i=10\<Esc>", 'xt')
590 call assert_equal(10, i)
591 let &cpo = save_cpo
592endfunc
593
594" Test for the 'X' flag in 'cpo' ('R' with a count)
595func Test_cpo_X()
596 let save_cpo = &cpo
597 new
598 call setline(1, 'aaaaaa')
599 set cpo-=X
600 normal gg4Rx
601 call assert_equal('xxxxaa', getline(1))
602 normal ggRy
603 normal 4.
604 call assert_equal('yyyyaa', getline(1))
605 call setline(1, 'aaaaaa')
606 set cpo+=X
607 normal gg4Rx
608 call assert_equal('xxxxaaaaa', getline(1))
609 normal ggRy
610 normal 4.
611 call assert_equal('yyyyxxxaaaaa', getline(1))
612 close!
613 let &cpo = save_cpo
614endfunc
615
616" Test for the 'y' flag in 'cpo' (repeating a yank command)
617func Test_cpo_y()
618 let save_cpo = &cpo
619 new
620 call setline(1, ['one', 'two'])
621 set cpo-=y
622 normal ggyy
623 normal 2G.
624 call assert_equal("one\n", @")
625 %d
626 call setline(1, ['one', 'two'])
627 set cpo+=y
628 normal ggyy
629 normal 2G.
630 call assert_equal("two\n", @")
631 close!
632 let &cpo = save_cpo
633endfunc
634
635" Test for the 'Z' flag in 'cpo' (write! resets 'readonly')
636func Test_cpo_Z()
637 let save_cpo = &cpo
638 call writefile([], 'Xfile')
639 new Xfile
640 setlocal readonly
641 set cpo-=Z
642 write!
643 call assert_equal(0, &readonly)
644 set cpo+=Z
645 setlocal readonly
646 write!
647 call assert_equal(1, &readonly)
648 close!
649 call delete('Xfile')
650 let &cpo = save_cpo
651endfunc
652
Bram Moolenaardf7df592020-06-14 13:50:55 +0200653" Test for the '!' flag in 'cpo' is in the test_normal.vim file
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200654
655" Test for displaying dollar when changing text ('$' flag in 'cpoptions')
656func Test_cpo_dollar()
657 new
658 let g:Line = ''
659 func SaveFirstLine()
660 let g:Line = Screenline(1)
661 return ''
662 endfunc
663 inoremap <expr> <buffer> <F2> SaveFirstLine()
664 call test_override('redraw_flag', 1)
665 set cpo+=$
666 call setline(1, 'one two three')
667 redraw!
668 exe "normal c2w\<F2>vim"
669 call assert_equal('one tw$ three', g:Line)
670 call assert_equal('vim three', getline(1))
671 set cpo-=$
672 call test_override('ALL', 0)
673 delfunc SaveFirstLine
674 %bw!
675endfunc
676
Bram Moolenaardf7df592020-06-14 13:50:55 +0200677" Test for the '%' flag in 'cpo' (parenthesis matching inside strings)
678func Test_cpo_percent()
679 let save_cpo = &cpo
680 new
681 call setline(1, ' if (strcmp("ab)cd(", s))')
682 set cpo-=%
683 normal 8|%
684 call assert_equal(28, col('.'))
685 normal 15|%
686 call assert_equal(27, col('.'))
687 normal 27|%
688 call assert_equal(15, col('.'))
689 call assert_beeps("normal 19|%")
690 call assert_beeps("normal 22|%")
691 set cpo+=%
692 normal 8|%
693 call assert_equal(28, col('.'))
694 normal 15|%
695 call assert_equal(19, col('.'))
696 normal 27|%
697 call assert_equal(22, col('.'))
698 normal 19|%
699 call assert_equal(15, col('.'))
700 normal 22|%
701 call assert_equal(27, col('.'))
702 close!
703 let &cpo = save_cpo
704endfunc
705
706" Test for cursor movement with '-' in 'cpoptions'
707func Test_cpo_minus()
708 new
709 call setline(1, ['foo', 'bar', 'baz'])
710 let save_cpo = &cpo
711 set cpo+=-
712 call assert_beeps('normal 10j')
713 call assert_equal(1, line('.'))
714 normal G
715 call assert_beeps('normal 10k')
716 call assert_equal(3, line('.'))
717 call assert_fails(10, 'E16:')
718 close!
719 let &cpo = save_cpo
720endfunc
721
722" Test for the '+' flag in 'cpo' ('write file' command resets the 'modified'
723" flag)
724func Test_cpo_plus()
725 let save_cpo = &cpo
726 call writefile([], 'Xfile')
727 new Xfile
728 call setline(1, 'foo')
729 write X1
730 call assert_equal(1, &modified)
731 set cpo+=+
732 write X2
733 call assert_equal(0, &modified)
734 close!
735 call delete('Xfile')
736 call delete('X1')
737 call delete('X2')
738 let &cpo = save_cpo
739endfunc
740
741" Test for the '*' flag in 'cpo' (':*' is same as ':@')
742func Test_cpo_star()
743 let save_cpo = &cpo
744 let x = 0
745 new
746 set cpo-=*
747 let @a = 'let x += 1'
748 call assert_fails('*a', 'E20:')
749 set cpo+=*
750 *a
751 call assert_equal(1, x)
752 close!
753 let &cpo = save_cpo
754endfunc
755
756" Test for the '<' flag in 'cpo' is in the test_mapping.vim file
757
758" Test for the '>' flag in 'cpo' (use a new line when appending to a register)
759func Test_cpo_gt()
760 let save_cpo = &cpo
761 new
762 call setline(1, 'one two')
763 set cpo-=>
764 let @r = ''
765 normal gg"Rye
766 normal "Rye
767 call assert_equal("oneone", @r)
768 set cpo+=>
769 let @r = ''
770 normal gg"Rye
771 normal "Rye
772 call assert_equal("\none\none", @r)
773 close!
774 let &cpo = save_cpo
775endfunc
776
777" Test for the ';' flag in 'cpo'
778" Test for t,f,F,T movement commands and 'cpo-;' setting
779func Test_cpo_semicolon()
780 let save_cpo = &cpo
781 new
782 call append(0, ["aaa two three four", " zzz", "yyy ",
783 \ "bbb yee yoo four", "ccc two three four",
784 \ "ddd yee yoo four"])
785 set cpo-=;
786 1
787 normal! 0tt;D
788 2
789 normal! 0fz;D
790 3
791 normal! $Fy;D
792 4
793 normal! $Ty;D
794 set cpo+=;
795 5
796 normal! 0tt;;D
797 6
798 normal! $Ty;;D
799
800 call assert_equal('aaa two', getline(1))
801 call assert_equal(' z', getline(2))
802 call assert_equal('y', getline(3))
803 call assert_equal('bbb y', getline(4))
804 call assert_equal('ccc', getline(5))
805 call assert_equal('ddd yee y', getline(6))
806 close!
807 let &cpo = save_cpo
808endfunc
809
810" Test for the '#' flag in 'cpo' (count before 'D', 'o' and 'O' operators)
811func Test_cpo_hash()
812 let save_cpo = &cpo
813 new
814 set cpo-=#
815 call setline(1, ['one', 'two', 'three'])
816 normal gg2D
817 call assert_equal(['three'], getline(1, '$'))
818 normal gg2ofour
819 call assert_equal(['three', 'four', 'four'], getline(1, '$'))
820 normal gg2Otwo
821 call assert_equal(['two', 'two', 'three', 'four', 'four'], getline(1, '$'))
822 %d
823 set cpo+=#
824 call setline(1, ['one', 'two', 'three'])
825 normal gg2D
826 call assert_equal(['', 'two', 'three'], getline(1, '$'))
827 normal gg2oone
828 call assert_equal(['', 'one', 'two', 'three'], getline(1, '$'))
829 normal gg2Ozero
830 call assert_equal(['zero', '', 'one', 'two', 'three'], getline(1, '$'))
831 close!
832 let &cpo = save_cpo
833endfunc
834
835" Test for the '&' flag in 'cpo'. The swap file is kept when a buffer is still
836" loaded and ':preserve' is used.
837func Test_cpo_ampersand()
838 call writefile(['one'], 'Xfile')
839 let after =<< trim [CODE]
840 set cpo+=&
841 preserve
842 qall
843 [CODE]
844 if RunVim([], after, 'Xfile')
845 call assert_equal(1, filereadable('.Xfile.swp'))
846 call delete('.Xfile.swp')
847 endif
848 call delete('Xfile')
849endfunc
850
851" Test for the '\' flag in 'cpo' (backslash in a [] range in a search pattern)
852func Test_cpo_backslash()
853 let save_cpo = &cpo
854 new
855 call setline(1, ['', " \\-string"])
856 set cpo-=\
857 exe 'normal gg/[ \-]' .. "\<CR>n"
858 call assert_equal(3, col('.'))
859 set cpo+=\
860 exe 'normal gg/[ \-]' .. "\<CR>n"
861 call assert_equal(2, col('.'))
862 close!
863 let &cpo = save_cpo
864endfunc
865
866" Test for the '/' flag in 'cpo' is in the test_substitute.vim file
867
868" Test for the '{' flag in 'cpo' (the "{" and "}" commands stop at a {
869" character at the start of a line)
870func Test_cpo_brace()
871 let save_cpo = &cpo
872 new
873 call setline(1, ['', '{', ' int i;', '}', ''])
874 set cpo-={
875 normal gg}
876 call assert_equal(5, line('.'))
877 normal G{
878 call assert_equal(1, line('.'))
879 set cpo+={
880 normal gg}
881 call assert_equal(2, line('.'))
882 normal G{
883 call assert_equal(2, line('.'))
884 close!
885 let &cpo = save_cpo
886endfunc
887
888" Test for the '.' flag in 'cpo' (:cd command fails if the current buffer is
889" modified)
890func Test_cpo_dot()
891 let save_cpo = &cpo
892 new Xfoo
893 call setline(1, 'foo')
894 let save_dir = getcwd()
895 set cpo+=.
896
897 " :cd should fail when buffer is modified and 'cpo' contains dot.
898 call assert_fails('cd ..', 'E747:')
899 call assert_equal(save_dir, getcwd())
900
901 " :cd with exclamation mark should succeed.
902 cd! ..
903 call assert_notequal(save_dir, getcwd())
904
905 " :cd should succeed when buffer has been written.
906 w!
907 exe 'cd ' .. fnameescape(save_dir)
908 call assert_equal(save_dir, getcwd())
909
910 call delete('Xfoo')
911 set cpo&
912 close!
913 let &cpo = save_cpo
914endfunc
915
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200916" vim: shiftwidth=2 sts=2 expandtab