blob: 79e282eff06cf58f50b721b821dd8b24b24fc0df [file] [log] [blame]
Bram Moolenaar209d3872017-11-16 21:52:51 +01001" Tests for the various 'formatoptions' settings
Bram Moolenaar396b7c72019-10-21 23:08:59 +02002
3source check.vim
4
Bram Moolenaar209d3872017-11-16 21:52:51 +01005func Test_text_format()
6 enew!
7
8 setl noai tw=2 fo=t
9 call append('$', [
10 \ '{',
11 \ ' ',
12 \ '',
13 \ '}'])
14 exe "normal /^{/+1\n0"
15 normal gRa b
16 let lnum = line('.')
17 call assert_equal([
18 \ 'a',
19 \ 'b'], getline(lnum - 1, lnum))
20
21 normal ggdG
22 setl ai tw=2 fo=tw
23 call append('$', [
24 \ '{',
25 \ 'a b ',
26 \ '',
27 \ 'a ',
28 \ '}'])
29 exe "normal /^{/+1\n0"
30 normal gqgqjjllab
31 let lnum = line('.')
32 call assert_equal([
33 \ 'a ',
34 \ 'b ',
35 \ '',
36 \ 'a ',
37 \ 'b'], getline(lnum - 4, lnum))
38
39 normal ggdG
40 setl tw=3 fo=t
41 call append('$', [
42 \ '{',
43 \ "a \<C-A>",
44 \ '}'])
45 exe "normal /^{/+1\n0"
46 exe "normal gqgqo\na \<C-V>\<C-A>"
47 let lnum = line('.')
48 call assert_equal([
49 \ 'a',
50 \ "\<C-A>",
51 \ '',
52 \ 'a',
53 \ "\<C-A>"], getline(lnum - 4, lnum))
54
55 normal ggdG
56 setl tw=2 fo=tcq1 comments=:#
57 call append('$', [
58 \ '{',
59 \ 'a b',
60 \ '#a b',
61 \ '}'])
62 exe "normal /^{/+1\n0"
63 exe "normal gqgqjgqgqo\na b\n#a b"
64 let lnum = line('.')
65 call assert_equal([
66 \ 'a b',
67 \ '#a b',
68 \ '',
69 \ 'a b',
70 \ '#a b'], getline(lnum - 4, lnum))
71
72 normal ggdG
73 setl tw=5 fo=tcn comments=:#
74 call append('$', [
75 \ '{',
76 \ ' 1 a',
77 \ '# 1 a',
78 \ '}'])
79 exe "normal /^{/+1\n0"
80 exe "normal A b\<Esc>jA b"
81 let lnum = line('.')
82 call assert_equal([
83 \ ' 1 a',
84 \ ' b',
85 \ '# 1 a',
86 \ '# b'], getline(lnum - 3, lnum))
87
88 normal ggdG
89 setl tw=5 fo=t2a si
90 call append('$', [
91 \ '{',
92 \ '',
93 \ ' x a',
94 \ ' b',
95 \ ' c',
96 \ '',
97 \ '}'])
98 exe "normal /^{/+3\n0"
99 exe "normal i \<Esc>A_"
100 let lnum = line('.')
101 call assert_equal([
102 \ '',
103 \ ' x a',
104 \ ' b_',
105 \ ' c',
106 \ ''], getline(lnum - 2, lnum + 2))
107
108 normal ggdG
109 setl tw=5 fo=qn comments=:#
110 call append('$', [
111 \ '{',
112 \ '# 1 a b',
113 \ '}'])
114 exe "normal /^{/+1\n5|"
115 normal gwap
116 call assert_equal(5, col('.'))
117 let lnum = line('.')
118 call assert_equal([
119 \ '# 1 a',
120 \ '# b'], getline(lnum, lnum + 1))
121
122 normal ggdG
123 setl tw=5 fo=q2 comments=:#
124 call append('$', [
125 \ '{',
126 \ '# x',
127 \ '# a b',
128 \ '}'])
129 exe "normal /^{/+1\n0"
130 normal gwap
131 let lnum = line('.')
132 call assert_equal([
133 \ '# x a',
134 \ '# b'], getline(lnum, lnum + 1))
135
136 normal ggdG
137 setl tw& fo=a
138 call append('$', [
139 \ '{',
140 \ ' 1aa',
141 \ ' 2bb',
142 \ '}'])
143 exe "normal /^{/+2\n0"
144 normal I^^
145 call assert_equal('{ 1aa ^^2bb }', getline('.'))
146
147 normal ggdG
148 setl tw=20 fo=an12wcq comments=s1:/*,mb:*,ex:*/
149 call append('$', [
150 \ '/* abc def ghi jkl ',
151 \ ' * mno pqr stu',
152 \ ' */'])
153 exe "normal /mno pqr/\n"
154 normal A vwx yz
155 let lnum = line('.')
156 call assert_equal([
157 \ ' * mno pqr stu ',
158 \ ' * vwx yz',
159 \ ' */'], getline(lnum - 1, lnum + 1))
160
161 normal ggdG
162 setl tw=12 fo=tqnc comments=:#
163 call setline('.', '# 1 xxxxx')
164 normal A foobar
165 call assert_equal([
166 \ '# 1 xxxxx',
167 \ '# foobar'], getline(1, 2))
168
Bram Moolenaarc3c31582019-01-11 22:15:05 +0100169 " Test the 'p' flag for 'formatoptions'
170 " First test without the flag: that it will break "Mr. Feynman" at the space
171 normal ggdG
172 setl tw=28 fo=tcq
173 call setline('.', 'Surely you''re joking, Mr. Feynman!')
174 normal gqq
175 call assert_equal([
176 \ 'Surely you''re joking, Mr.',
177 \ 'Feynman!'], getline(1, 2))
178 " Now test with the flag: that it will push the name with the title onto the
179 " next line
180 normal ggdG
181 setl fo+=p
182 call setline('.', 'Surely you''re joking, Mr. Feynman!')
183 normal gqq
184 call assert_equal([
185 \ 'Surely you''re joking,',
186 \ 'Mr. Feynman!'], getline(1, 2))
187 " Ensure that it will still break if two spaces are entered
188 normal ggdG
189 call setline('.', 'Surely you''re joking, Mr. Feynman!')
190 normal gqq
191 call assert_equal([
192 \ 'Surely you''re joking, Mr.',
193 \ 'Feynman!'], getline(1, 2))
194
Bram Moolenaar209d3872017-11-16 21:52:51 +0100195 setl ai& tw& fo& si& comments&
196 enew!
197endfunc
Bram Moolenaarcada7892018-01-31 19:30:24 +0100198
Bram Moolenaar6e371ec2021-12-12 14:16:39 +0000199func Test_format_c_comment()
200 new
201 setl ai cindent tw=40 et fo=croql
202 let text =<< trim END
203 var = 2345; // asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf
204 END
205 call setline(1, text)
206 normal gql
207 let expected =<< trim END
208 var = 2345; // asdf asdf asdf asdf asdf
209 // asdf asdf asdf asdf asdf
210 END
211 call assert_equal(expected, getline(1, '$'))
212
213 %del
214 let text =<< trim END
215 var = 2345; // asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf
216 END
217 call setline(1, text)
218 normal gql
219 let expected =<< trim END
220 var = 2345; // asdf asdf asdf asdf asdf
221 // asdf asdf asdf asdf asdf
222 // asdf asdf
223 END
224 call assert_equal(expected, getline(1, '$'))
225
Bram Moolenaar264d3dd2021-12-29 14:09:32 +0000226 %del
227 let text =<< trim END
228 #if 0 // This is another long end of
229 // line comment that
230 // wraps.
231 END
232 call setline(1, text)
233 normal gq2j
234 let expected =<< trim END
235 #if 0 // This is another long
236 // end of line comment
237 // that wraps.
238 END
239 call assert_equal(expected, getline(1, '$'))
240
zeertzjq51ab7c72022-02-08 12:58:37 +0000241 " Using either "o" or "O" repeats a line comment occupying a whole line.
242 %del
243 let text =<< trim END
244 nop;
245 // This is a comment
246 val = val;
247 END
248 call setline(1, text)
249 normal 2Go
250 let expected =<< trim END
251 nop;
252 // This is a comment
253 //
254 val = val;
255 END
256 call assert_equal(expected, getline(1, '$'))
257 normal 2GO
258 let expected =<< trim END
259 nop;
260 //
261 // This is a comment
262 //
263 val = val;
264 END
265 call assert_equal(expected, getline(1, '$'))
266
267 " Using "o" repeats a line comment after a statement, "O" does not.
Bram Moolenaar5ea5f372021-12-29 15:15:47 +0000268 %del
269 let text =<< trim END
270 nop;
271 val = val; // This is a comment
272 END
273 call setline(1, text)
274 normal 2Go
275 let expected =<< trim END
276 nop;
277 val = val; // This is a comment
278 //
279 END
280 call assert_equal(expected, getline(1, '$'))
Bram Moolenaar2bf875f2022-05-07 14:54:11 +0100281 3delete
282
283 " No comment repeated with a slash in 'formatoptions'
284 set fo+=/
285 normal 2Gox
286 let expected =<< trim END
287 nop;
288 val = val; // This is a comment
289 x
290 END
291 call assert_equal(expected, getline(1, '$'))
292 set fo-=/
Bram Moolenaar16556192022-04-11 17:36:37 +0100293
294 " using 'indentexpr' instead of 'cindent' does not repeat a comment
295 setl nocindent indentexpr=2
296 3delete
297 normal 2Gox
298 let expected =<< trim END
299 nop;
300 val = val; // This is a comment
301 x
302 END
303 call assert_equal(expected, getline(1, '$'))
304 setl cindent indentexpr=
305 3delete
306
Bram Moolenaar5ea5f372021-12-29 15:15:47 +0000307 normal 2GO
308 let expected =<< trim END
309 nop;
310
311 val = val; // This is a comment
Bram Moolenaar5ea5f372021-12-29 15:15:47 +0000312 END
313 call assert_equal(expected, getline(1, '$'))
314
Bram Moolenaarba263672021-12-29 18:09:13 +0000315 " Using "o" does not repeat a comment in a string
316 %del
317 let text =<< trim END
318 nop;
319 val = " // This is not a comment";
320 END
321 call setline(1, text)
322 normal 2Gox
323 let expected =<< trim END
324 nop;
325 val = " // This is not a comment";
326 x
327 END
328 call assert_equal(expected, getline(1, '$'))
329
Bram Moolenaar5d20fbf2021-12-29 16:05:31 +0000330 " Using CTRL-U after "o" fixes the indent
331 %del
332 let text =<< trim END
333 {
334 val = val; // This is a comment
335 END
336 call setline(1, text)
337 exe "normal! 2Go\<C-U>x\<Esc>"
338 let expected =<< trim END
339 {
340 val = val; // This is a comment
341 x
342 END
343 call assert_equal(expected, getline(1, '$'))
344
Bram Moolenaar6e371ec2021-12-12 14:16:39 +0000345 bwipe!
346endfunc
347
Bram Moolenaarcada7892018-01-31 19:30:24 +0100348" Tests for :right, :center and :left on text with embedded TAB.
349func Test_format_align()
350 enew!
351 set tw=65
352
353 " :left alignment
354 call append(0, [
355 \ " test for :left",
356 \ " a a",
357 \ " fa a",
358 \ " dfa a",
359 \ " sdfa a",
360 \ " asdfa a",
361 \ " xasdfa a",
362 \ "asxxdfa a",
363 \ ])
364 %left
365 call assert_equal([
366 \ "test for :left",
367 \ "a a",
368 \ "fa a",
369 \ "dfa a",
370 \ "sdfa a",
371 \ "asdfa a",
372 \ "xasdfa a",
373 \ "asxxdfa a",
374 \ ""
375 \ ], getline(1, '$'))
376 enew!
377
378 " :center alignment
379 call append(0, [
380 \ " test for :center",
381 \ " a a",
382 \ " fa afd asdf",
383 \ " dfa a",
384 \ " sdfa afd asdf",
385 \ " asdfa a",
386 \ " xasdfa asdfasdfasdfasdfasdf",
387 \ "asxxdfa a"
388 \ ])
389 %center
390 call assert_equal([
391 \ " test for :center",
392 \ " a a",
393 \ " fa afd asdf",
394 \ " dfa a",
395 \ " sdfa afd asdf",
396 \ " asdfa a",
397 \ " xasdfa asdfasdfasdfasdfasdf",
398 \ " asxxdfa a",
399 \ ""
400 \ ], getline(1, '$'))
401 enew!
402
403 " :right alignment
404 call append(0, [
405 \ " test for :right",
406 \ " a a",
407 \ " fa a",
408 \ " dfa a",
409 \ " sdfa a",
410 \ " asdfa a",
411 \ " xasdfa a",
412 \ " asxxdfa a",
413 \ " asxa;ofa a",
414 \ " asdfaqwer a",
415 \ " a ax",
416 \ " fa ax",
417 \ " dfa ax",
418 \ " sdfa ax",
419 \ " asdfa ax",
420 \ " xasdfa ax",
421 \ " asxxdfa ax",
422 \ " asxa;ofa ax",
423 \ " asdfaqwer ax",
424 \ " a axx",
425 \ " fa axx",
426 \ " dfa axx",
427 \ " sdfa axx",
428 \ " asdfa axx",
429 \ " xasdfa axx",
430 \ " asxxdfa axx",
431 \ " asxa;ofa axx",
432 \ " asdfaqwer axx",
433 \ " a axxx",
434 \ " fa axxx",
435 \ " dfa axxx",
436 \ " sdfa axxx",
437 \ " asdfa axxx",
438 \ " xasdfa axxx",
439 \ " asxxdfa axxx",
440 \ " asxa;ofa axxx",
441 \ " asdfaqwer axxx",
442 \ " a axxxo",
443 \ " fa axxxo",
444 \ " dfa axxxo",
445 \ " sdfa axxxo",
446 \ " asdfa axxxo",
447 \ " xasdfa axxxo",
448 \ " asxxdfa axxxo",
449 \ " asxa;ofa axxxo",
450 \ " asdfaqwer axxxo",
451 \ " a axxxoi",
452 \ " fa axxxoi",
453 \ " dfa axxxoi",
454 \ " sdfa axxxoi",
455 \ " asdfa axxxoi",
456 \ " xasdfa axxxoi",
457 \ " asxxdfa axxxoi",
458 \ " asxa;ofa axxxoi",
459 \ " asdfaqwer axxxoi",
460 \ " a axxxoik",
461 \ " fa axxxoik",
462 \ " dfa axxxoik",
463 \ " sdfa axxxoik",
464 \ " asdfa axxxoik",
465 \ " xasdfa axxxoik",
466 \ " asxxdfa axxxoik",
467 \ " asxa;ofa axxxoik",
468 \ " asdfaqwer axxxoik",
469 \ " a axxxoike",
470 \ " fa axxxoike",
471 \ " dfa axxxoike",
472 \ " sdfa axxxoike",
473 \ " asdfa axxxoike",
474 \ " xasdfa axxxoike",
475 \ " asxxdfa axxxoike",
476 \ " asxa;ofa axxxoike",
477 \ " asdfaqwer axxxoike",
478 \ " a axxxoikey",
479 \ " fa axxxoikey",
480 \ " dfa axxxoikey",
481 \ " sdfa axxxoikey",
482 \ " asdfa axxxoikey",
483 \ " xasdfa axxxoikey",
484 \ " asxxdfa axxxoikey",
485 \ " asxa;ofa axxxoikey",
486 \ " asdfaqwer axxxoikey",
487 \ ])
488 %right
489 call assert_equal([
490 \ "\t\t\t\t test for :right",
491 \ "\t\t\t\t a a",
492 \ "\t\t\t\t fa a",
493 \ "\t\t\t\t dfa a",
494 \ "\t\t\t\t sdfa a",
495 \ "\t\t\t\t asdfa a",
496 \ "\t\t\t\t xasdfa a",
497 \ "\t\t\t\t asxxdfa a",
498 \ "\t\t\t\t asxa;ofa a",
499 \ "\t\t\t\t asdfaqwer a",
500 \ "\t\t\t\t a ax",
501 \ "\t\t\t\t fa ax",
502 \ "\t\t\t\t dfa ax",
503 \ "\t\t\t\t sdfa ax",
504 \ "\t\t\t\t asdfa ax",
505 \ "\t\t\t\t xasdfa ax",
506 \ "\t\t\t\t asxxdfa ax",
507 \ "\t\t\t\t asxa;ofa ax",
508 \ "\t\t\t\t asdfaqwer ax",
509 \ "\t\t\t\t a axx",
510 \ "\t\t\t\t fa axx",
511 \ "\t\t\t\t dfa axx",
512 \ "\t\t\t\t sdfa axx",
513 \ "\t\t\t\t asdfa axx",
514 \ "\t\t\t\t xasdfa axx",
515 \ "\t\t\t\t asxxdfa axx",
516 \ "\t\t\t\t asxa;ofa axx",
517 \ "\t\t\t\t asdfaqwer axx",
518 \ "\t\t\t\t a axxx",
519 \ "\t\t\t\t fa axxx",
520 \ "\t\t\t\t dfa axxx",
521 \ "\t\t\t\t sdfa axxx",
522 \ "\t\t\t\t asdfa axxx",
523 \ "\t\t\t\t xasdfa axxx",
524 \ "\t\t\t\t asxxdfa axxx",
525 \ "\t\t\t\t asxa;ofa axxx",
526 \ "\t\t\t\t asdfaqwer axxx",
527 \ "\t\t\t\t a axxxo",
528 \ "\t\t\t\t fa axxxo",
529 \ "\t\t\t\t dfa axxxo",
530 \ "\t\t\t\t sdfa axxxo",
531 \ "\t\t\t\t asdfa axxxo",
532 \ "\t\t\t\t xasdfa axxxo",
533 \ "\t\t\t\t asxxdfa axxxo",
534 \ "\t\t\t\t asxa;ofa axxxo",
535 \ "\t\t\t\t asdfaqwer axxxo",
536 \ "\t\t\t\t a axxxoi",
537 \ "\t\t\t\t fa axxxoi",
538 \ "\t\t\t\t dfa axxxoi",
539 \ "\t\t\t\t sdfa axxxoi",
540 \ "\t\t\t\t asdfa axxxoi",
541 \ "\t\t\t\t xasdfa axxxoi",
542 \ "\t\t\t\t asxxdfa axxxoi",
543 \ "\t\t\t\t asxa;ofa axxxoi",
544 \ "\t\t\t\t asdfaqwer axxxoi",
545 \ "\t\t\t\t a axxxoik",
546 \ "\t\t\t\t fa axxxoik",
547 \ "\t\t\t\t dfa axxxoik",
548 \ "\t\t\t\t sdfa axxxoik",
549 \ "\t\t\t\t asdfa axxxoik",
550 \ "\t\t\t\t xasdfa axxxoik",
551 \ "\t\t\t\t asxxdfa axxxoik",
552 \ "\t\t\t\t asxa;ofa axxxoik",
553 \ "\t\t\t\t asdfaqwer axxxoik",
554 \ "\t\t\t\t a axxxoike",
555 \ "\t\t\t\t fa axxxoike",
556 \ "\t\t\t\t dfa axxxoike",
557 \ "\t\t\t\t sdfa axxxoike",
558 \ "\t\t\t\t asdfa axxxoike",
559 \ "\t\t\t\t xasdfa axxxoike",
560 \ "\t\t\t\t asxxdfa axxxoike",
561 \ "\t\t\t\t asxa;ofa axxxoike",
562 \ "\t\t\t\t asdfaqwer axxxoike",
563 \ "\t\t\t\t a axxxoikey",
564 \ "\t\t\t\t fa axxxoikey",
565 \ "\t\t\t\t dfa axxxoikey",
566 \ "\t\t\t\t sdfa axxxoikey",
567 \ "\t\t\t\t asdfa axxxoikey",
568 \ "\t\t\t\t xasdfa axxxoikey",
569 \ "\t\t\t\t asxxdfa axxxoikey",
570 \ "\t\t\t\t asxa;ofa axxxoikey",
571 \ "\t\t\t\t asdfaqwer axxxoikey",
572 \ ""
573 \ ], getline(1, '$'))
574 enew!
575
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100576 " align text with 'wrapmargin'
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100577 50vnew
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100578 call setline(1, ['Vim'])
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100579 setl textwidth=0
580 setl wrapmargin=30
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100581 right
582 call assert_equal("\t\t Vim", getline(1))
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100583 q!
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100584
Bram Moolenaarea3db912020-02-02 15:32:13 +0100585 " align text with 'rightleft'
586 if has('rightleft')
587 new
588 call setline(1, 'Vim')
589 setlocal rightleft
590 left 20
591 setlocal norightleft
592 call assert_equal("\t\t Vim", getline(1))
593 setlocal rightleft
594 right
595 setlocal norightleft
596 call assert_equal("Vim", getline(1))
597 close!
598 endif
599
Bram Moolenaarcada7892018-01-31 19:30:24 +0100600 set tw&
601endfunc
602
603" Test formatting a paragraph.
604func Test_format_para()
605 enew!
606 set fo+=tcroql tw=72
607
608 call append(0, [
609 \ "xxxxx xx xxxxxx ",
610 \ "xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx",
611 \ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx",
612 \ "xx xxxxxxx. xxxx xxxx.",
613 \ "",
614 \ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx",
615 \ "> xxxxxx xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx"
616 \ ])
617 exe "normal /xxxxxxxx$\<CR>"
618 normal 0gq6kk
619 call assert_equal([
620 \ "xxxxx xx xxxxxx xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx",
621 \ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx xx xxxxxxx.",
622 \ "xxxx xxxx.",
623 \ "",
624 \ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx xxxxxx",
625 \ "> xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx",
626 \ ""
627 \ ], getline(1, '$'))
628
629 set fo& tw&
630 enew!
631endfunc
632
633" Test undo after ":%s" and formatting.
634func Test_format_undo()
635 enew!
636 map gg :.,.+2s/^/x/<CR>kk:set tw=3<CR>gqq
637
638 call append(0, [
639 \ "aa aa aa aa",
640 \ "bb bb bb bb",
641 \ "cc cc cc cc"
642 \ ])
643 " undo/redo here to make the next undo only work on the following changes
644 exe "normal i\<C-G>u"
645 call cursor(1,1)
646 normal ggu
647 call assert_equal([
648 \ "aa aa aa aa",
649 \ "bb bb bb bb",
650 \ "cc cc cc cc",
651 \ ""
652 \ ], getline(1, '$'))
653
654 unmap gg
Bram Moolenaare1e714e2018-12-31 23:58:24 +0100655 set tw&
Bram Moolenaarcada7892018-01-31 19:30:24 +0100656 enew!
657endfunc
Bram Moolenaare1e714e2018-12-31 23:58:24 +0100658
659func Test_format_list_auto()
660 new
661 call setline(1, ['1. abc', '2. def', '3. ghi'])
662 set fo=tan ai bs=2
663 call feedkeys("3G0lli\<BS>\<BS>x\<Esc>", 'tx')
664 call assert_equal('2. defx ghi', getline(2))
665 bwipe!
666 set fo& ai& bs&
667endfunc
Bram Moolenaar396b7c72019-10-21 23:08:59 +0200668
669func Test_crash_github_issue_5095()
670 CheckFeature autocmd
671
672 " This used to segfault, see https://github.com/vim/vim/issues/5095
673 augroup testing
674 au BufNew x center
675 augroup END
676
677 next! x
678
679 bw
680 augroup testing
681 au!
682 augroup END
683 augroup! testing
684endfunc
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100685
686" Test for formatting multi-byte text with 'fo=t'
687func Test_tw_2_fo_t()
688 new
689 let t =<< trim END
690 {
691 XYZ
692 abc XYZ
693 }
694 END
695 call setline(1, t)
696 call cursor(2, 1)
697
698 set tw=2 fo=t
699 let t =<< trim END
700 XYZ
701 abc XYZ
702 END
703 exe "normal gqgqjgqgq"
704 exe "normal o\n" . join(t, "\n")
705
706 let expected =<< trim END
707 {
708 XYZ
709 abc
710 XYZ
711
712 XYZ
713 abc
714 XYZ
715 }
716 END
717 call assert_equal(expected, getline(1, '$'))
718
719 set tw& fo&
720 bwipe!
721endfunc
722
723" Test for formatting multi-byte text with 'fo=tm' and 'tw=1'
724func Test_tw_1_fo_tm()
725 new
726 let t =<< trim END
727 {
728
729 Xa
730 X a
731 XY
732 X Y
733 }
734 END
735 call setline(1, t)
736 call cursor(2, 1)
737
738 set tw=1 fo=tm
739 let t =<< trim END
740
741 Xa
742 X a
743 XY
744 X Y
745 END
746 exe "normal gqgqjgqgqjgqgqjgqgqjgqgq"
747 exe "normal o\n" . join(t, "\n")
748
749 let expected =<< trim END
750 {
751
752
753 a
754
755 a
756
757
758
759
760
761
762
763 a
764
765 a
766
767
768
769
770 }
771 END
772 call assert_equal(expected, getline(1, '$'))
773
774 set tw& fo&
775 bwipe!
776endfunc
777
778" Test for formatting multi-byte text with 'fo=tm' and 'tw=2'
779func Test_tw_2_fo_tm()
780 new
781 let t =<< trim END
782 {
783
784 a
785 a
786 XY
787
788 a
789 ab
790 abc
791 ab c
792 abXY
793 }
794 END
795 call setline(1, t)
796 call cursor(2, 1)
797
798 set tw=2 fo=tm
799 let t =<< trim END
800
801 a
802 a
803 XY
804
805 a
806 ab
807 abc
808 ab c
809 abXY
810 END
811 exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq"
812 exe "normal o\n" . join(t, "\n")
813
814 let expected =<< trim END
815 {
816
817
818 a
819
820 a
821
822
823
824
825 a
826
827 ab
828
829 abc
830
831 ab
832
833 c
834 ab
835
836
837
838
839
840 a
841
842 a
843
844
845
846
847 a
848
849 ab
850
851 abc
852
853 ab
854
855 c
856 ab
857
858
859 }
860 END
861 call assert_equal(expected, getline(1, '$'))
862
863 set tw& fo&
864 bwipe!
865endfunc
866
867" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'autoindent'.
868func Test_tw_2_fo_tm_ai()
869 new
870 let t =<< trim END
871 {
872
873 Xa
874 }
875 END
876 call setline(1, t)
877 call cursor(2, 1)
878
879 set ai tw=2 fo=tm
880 let t =<< trim END
881
882 Xa
883 END
884 exe "normal gqgqjgqgq"
885 exe "normal o\n" . join(t, "\n")
886
887 let expected =<< trim END
888 {
889
890
891 a
892
893
894
895 a
896 }
897 END
898 call assert_equal(expected, getline(1, '$'))
899
900 set tw& fo& ai&
901 bwipe!
902endfunc
903
904" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'noai'.
905func Test_tw_2_fo_tm_noai()
906 new
907 let t =<< trim END
908 {
909
910 a
911 }
912 END
913 call setline(1, t)
914 call cursor(2, 1)
915
916 set noai tw=2 fo=tm
917 exe "normal gqgqjgqgqo\n X\n Xa"
918
919 let expected =<< trim END
920 {
921
922
923 a
924
925
926
927 a
928 }
929 END
930 call assert_equal(expected, getline(1, '$'))
931
932 set tw& fo& ai&
933 bwipe!
934endfunc
935
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100936func Test_tw_2_fo_tm_replace()
937 new
938 let t =<< trim END
939 {
940
941 }
942 END
943 call setline(1, t)
944 call cursor(2, 1)
945
946 set tw=2 fo=tm
947 exe "normal RXa"
948
949 let expected =<< trim END
950 {
951
952 a
953 }
954 END
955 call assert_equal(expected, getline(1, '$'))
956
957 set tw& fo&
958 bwipe!
959endfunc
960
961" Test for 'matchpairs' with multibyte chars
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200962func Test_mps_multibyte()
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100963 new
964 let t =<< trim END
965 {
966 ‘ two three ’ four
967 }
968 END
969 call setline(1, t)
970 call cursor(2, 1)
971
972 exe "set mps+=\u2018:\u2019"
973 normal d%
974
975 let expected =<< trim END
976 {
977 four
978 }
979 END
980 call assert_equal(expected, getline(1, '$'))
981
982 set mps&
983 bwipe!
984endfunc
985
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200986" Test for 'matchpairs' in latin1 encoding
987func Test_mps_latin1()
988 new
989 let save_enc = &encoding
990 set encoding=latin1
991 call setline(1, 'abc(def)ghi')
992 normal %
993 call assert_equal(8, col('.'))
994 normal %
995 call assert_equal(4, col('.'))
996 call cursor(1, 6)
997 normal [(
998 call assert_equal(4, col('.'))
999 normal %
1000 call assert_equal(8, col('.'))
1001 call cursor(1, 6)
1002 normal ])
1003 call assert_equal(8, col('.'))
1004 normal %
1005 call assert_equal(4, col('.'))
1006 let &encoding = save_enc
1007 close!
1008endfunc
1009
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +02001010func Test_empty_matchpairs()
1011 split
1012 set matchpairs= showmatch
1013 call assert_nobeep('call feedkeys("ax\tx\t\<Esc>", "xt")')
1014 set matchpairs& noshowmatch
1015 bwipe!
1016endfunc
1017
Bram Moolenaar6fd367a2021-03-13 13:14:04 +01001018func Test_mps_error()
1019 let encoding_save = &encoding
1020
1021 for e in ['utf-8', 'latin1']
1022 exe 'set encoding=' .. e
1023
1024 call assert_fails('set mps=<:', 'E474:', e)
1025 call assert_fails('set mps=:>', 'E474:', e)
1026 call assert_fails('set mps=<>', 'E474:', e)
1027 call assert_fails('set mps=<:>_', 'E474:', e)
1028 endfor
1029
1030 let &encoding = encoding_save
1031endfunc
1032
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001033" Test for ra on multi-byte characters
1034func Test_ra_multibyte()
1035 new
1036 let t =<< trim END
1037 ra test
1038 abba
1039 aab
1040 END
1041 call setline(1, t)
1042 call cursor(1, 1)
1043
1044 normal jVjra
1045
1046 let expected =<< trim END
1047 ra test
1048 aaaa
1049 aaa
1050 END
1051 call assert_equal(expected, getline(1, '$'))
1052
1053 bwipe!
1054endfunc
1055
1056" Test for 'whichwrap' with multi-byte character
Bram Moolenaara48e78e2019-12-17 20:29:26 +01001057func Test_whichwrap_multi_byte()
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001058 new
1059 let t =<< trim END
1060 á
1061 x
1062 END
1063 call setline(1, t)
1064 call cursor(2, 1)
1065
1066 set whichwrap+=h
1067 normal dh
Bram Moolenaara48e78e2019-12-17 20:29:26 +01001068 set whichwrap&
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001069
1070 let expected =<< trim END
1071 áx
1072 END
1073 call assert_equal(expected, getline(1, '$'))
1074
1075 bwipe!
1076endfunc
1077
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001078" Test for 'a' and 'w' flags in 'formatoptions'
1079func Test_fo_a_w()
1080 new
1081 setlocal fo+=aw tw=10
1082 call feedkeys("iabc abc a abc\<Esc>k0weade", 'xt')
1083 call assert_equal(['abc abcde ', 'a abc'], getline(1, '$'))
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +02001084
Bram Moolenaar845e0ee2020-06-20 16:05:32 +02001085 " when a line ends with space, it is not broken up.
1086 %d
1087 call feedkeys("ione two to ", 'xt')
1088 call assert_equal('one two to ', getline(1))
1089
1090 " when a line ends with spaces and backspace is used in the next line, the
1091 " last space in the previous line should be removed.
1092 %d
1093 set backspace=indent,eol,start
1094 call setline(1, ['one ', 'two'])
1095 exe "normal 2Gi\<BS>"
1096 call assert_equal(['one two'], getline(1, '$'))
1097 set backspace&
1098
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +02001099 " Test for 'a', 'w' and '1' options.
1100 setlocal textwidth=0
1101 setlocal fo=1aw
1102 %d
1103 call setline(1, '. foo')
1104 normal 72ig
1105 call feedkeys('a uu uu uu', 'xt')
1106 call assert_equal('g uu uu ', getline(1)[-8:])
1107 call assert_equal(['uu. foo'], getline(2, '$'))
1108
Bram Moolenaard0a1dee2020-12-20 13:07:48 +01001109 " using backspace or "x" triggers reformat
1110 call setline(1, ['1 2 3 4 5 ', '6 7 8 9'])
1111 set tw=10
1112 set fo=taw
1113 set bs=indent,eol,start
1114 exe "normal 1G4la\<BS>\<BS>\<Esc>"
1115 call assert_equal(['1 2 4 5 6 ', '7 8 9'], getline(1, 2))
1116 exe "normal f4xx"
1117 call assert_equal(['1 2 5 6 7 ', '8 9'], getline(1, 2))
1118
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001119 " using "cw" leaves cursor in right spot
1120 call setline(1, ['Now we g whether that nation, or',
1121 \ 'any nation so conceived and,'])
1122 set fo=tcqa tw=35
1123 exe "normal 2G0cwx\<Esc>"
1124 call assert_equal(['Now we g whether that nation, or x', 'nation so conceived and,'], getline(1, 2))
1125
Bram Moolenaard0a1dee2020-12-20 13:07:48 +01001126 set tw=0
1127 set fo&
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001128 %bw!
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001129endfunc
1130
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +02001131" Test for formatting lines using gq in visual mode
1132func Test_visual_gq_format()
1133 new
1134 call setline(1, ['one two three four', 'five six', 'one two'])
1135 setl textwidth=10
1136 call feedkeys('ggv$jj', 'xt')
1137 redraw!
1138 normal gq
1139 %d
1140 call setline(1, ['one two three four', 'five six', 'one two'])
1141 normal G$
1142 call feedkeys('v0kk', 'xt')
1143 redraw!
1144 normal gq
1145 setl textwidth&
1146 close!
1147endfunc
1148
1149" Test for 'n' flag in 'formatoptions' to format numbered lists
1150func Test_fo_n()
1151 new
1152 setlocal autoindent
1153 setlocal textwidth=12
1154 setlocal fo=n
1155 call setline(1, [' 1) one two three four', ' 2) two'])
1156 normal gggqG
1157 call assert_equal([' 1) one two', ' three', ' four', ' 2) two'],
1158 \ getline(1, '$'))
1159 close!
1160endfunc
1161
1162" Test for 'formatlistpat' option
1163func Test_formatlistpat()
1164 new
1165 setlocal autoindent
1166 setlocal textwidth=10
1167 setlocal fo=n
1168 setlocal formatlistpat=^\\s*-\\s*
1169 call setline(1, [' - one two three', ' - two'])
1170 normal gggqG
1171 call assert_equal([' - one', ' two', ' three', ' - two'],
1172 \ getline(1, '$'))
1173 close!
1174endfunc
1175
1176" Test for the 'b' and 'v' flags in 'formatoptions'
1177" Text should wrap only if a space character is inserted at or before
1178" 'textwidth'
1179func Test_fo_b()
1180 new
1181 setlocal textwidth=20
1182
1183 setlocal formatoptions=t
1184 call setline(1, 'one two three four')
1185 call feedkeys('Amore', 'xt')
1186 call assert_equal(['one two three', 'fourmore'], getline(1, '$'))
1187
1188 setlocal formatoptions=bt
1189 %d
1190 call setline(1, 'one two three four')
1191 call feedkeys('Amore five', 'xt')
1192 call assert_equal(['one two three fourmore five'], getline(1, '$'))
1193
1194 setlocal formatoptions=bt
1195 %d
1196 call setline(1, 'one two three four')
1197 call feedkeys('A five', 'xt')
1198 call assert_equal(['one two three four', 'five'], getline(1, '$'))
1199
1200 setlocal formatoptions=vt
1201 %d
1202 call setline(1, 'one two three four')
1203 call feedkeys('Amore five', 'xt')
1204 call assert_equal(['one two three fourmore', 'five'], getline(1, '$'))
1205
1206 close!
1207endfunc
1208
1209" Test for the '1' flag in 'formatoptions'. Don't wrap text after a one letter
1210" word.
1211func Test_fo_1()
1212 new
1213 setlocal textwidth=20
1214
1215 setlocal formatoptions=t
1216 call setline(1, 'one two three four')
1217 call feedkeys('A a bird', 'xt')
1218 call assert_equal(['one two three four a', 'bird'], getline(1, '$'))
1219
1220 %d
1221 setlocal formatoptions=t1
1222 call setline(1, 'one two three four')
1223 call feedkeys('A a bird', 'xt')
1224 call assert_equal(['one two three four', 'a bird'], getline(1, '$'))
1225
1226 close!
1227endfunc
1228
1229" Test for 'l' flag in 'formatoptions'. When starting insert mode, if a line
1230" is longer than 'textwidth', then it is not broken.
1231func Test_fo_l()
1232 new
1233 setlocal textwidth=20
1234
1235 setlocal formatoptions=t
1236 call setline(1, 'one two three four five')
1237 call feedkeys('A six', 'xt')
1238 call assert_equal(['one two three four', 'five six'], getline(1, '$'))
1239
1240 %d
1241 setlocal formatoptions=tl
1242 call setline(1, 'one two three four five')
1243 call feedkeys('A six', 'xt')
1244 call assert_equal(['one two three four five six'], getline(1, '$'))
1245
1246 close!
1247endfunc
1248
1249" Test for the '2' flag in 'formatoptions'
1250func Test_fo_2()
1251 new
1252 setlocal autoindent
1253 setlocal formatoptions=t2
1254 setlocal textwidth=30
1255 call setline(1, ["\tfirst line of a paragraph.",
1256 \ "second line of the same paragraph.",
1257 \ "third line."])
1258 normal gggqG
1259 call assert_equal(["\tfirst line of a",
1260 \ "paragraph. second line of the",
1261 \ "same paragraph. third line."], getline(1, '$'))
1262 close!
1263endfunc
1264
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001265" vim: shiftwidth=2 sts=2 expandtab