blob: 800c2751b7193e2010e216152033ba42c30d25fe [file] [log] [blame]
Maxim Kimaa68f8a2025-03-28 19:17:53 +01001" Test for the comment package
2
Maxim Kim51ff18e2025-03-19 20:54:12 +01003source check.vim
4source term_util.vim
5
6func Test_basic_comment()
7 CheckScreendump
8 let lines =<< trim END
9 vim9script
10
11 def Hello()
12 echo "Hello"
13 enddef
14 END
15
16 let input_file = "test_basic_comment_input.vim"
17 call writefile(lines, input_file, "D")
18
19 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
20
21 call term_sendkeys(buf, "gcc")
22 call term_sendkeys(buf, "2jgcip")
23 let output_file = "comment_basic_test.vim"
24 call term_sendkeys(buf, $":w {output_file}\<CR>")
25 defer delete(output_file)
26
27 call StopVimInTerminal(buf)
28
29 let result = readfile(output_file)
30
31 call assert_equal(["# vim9script", "", "# def Hello()", '# echo "Hello"', "# enddef"], result)
32endfunc
33
Maxim Kim51ff18e2025-03-19 20:54:12 +010034func Test_basic_uncomment()
35 CheckScreendump
36 let lines =<< trim END
37 vim9script
38
39 # def Hello()
40 # echo "Hello"
41 # enddef
42 END
43
44 let input_file = "test_basic_uncomment_input.vim"
45 call writefile(lines, input_file, "D")
46
47 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
48
49 call term_sendkeys(buf, "gcc")
50 call term_sendkeys(buf, "2jgcip")
51 let output_file = "uncomment_basic_test.vim"
52 call term_sendkeys(buf, $":w {output_file}\<CR>")
53 defer delete(output_file)
54
55 call StopVimInTerminal(buf)
56
57 let result = readfile(output_file)
58
Maxim Kim9712a252025-03-21 18:02:56 +010059 call assert_equal(["# vim9script", "", "def Hello()", ' echo "Hello"', "enddef"], result)
Maxim Kim51ff18e2025-03-19 20:54:12 +010060endfunc
61
Maxim Kimdd776dd2025-04-22 20:11:05 +020062func Test_caseinsensitive_uncomment()
63 CheckScreendump
64 let lines =<< trim END
65 rem echo "Hello"
66 END
67
68 let input_file = "test_caseinsensitive_uncomment_input.bat"
69 call writefile(lines, input_file, "D")
70
71 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
72
73 call term_sendkeys(buf, "gcc")
74 let output_file = "comment_testinsensitive_uncomment_test.bat"
75 call term_sendkeys(buf, $":w {output_file}\<CR>")
76 defer delete(output_file)
77
78 call StopVimInTerminal(buf)
79
80 let result = readfile(output_file)
81
82 call assert_equal(['echo "Hello"'], result)
83endfunc
84
Maxim Kim51ff18e2025-03-19 20:54:12 +010085func Test_bothends_comment()
86 CheckScreendump
87 let lines =<< trim END
88 int main() {}
89 END
90
91 let input_file = "test_bothends_comment_input.c"
92 call writefile(lines, input_file, "D")
93
94 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
95
96 call term_sendkeys(buf, "gcc")
97 let output_file = "comment_bothends_test.c"
98 call term_sendkeys(buf, $":w {output_file}\<CR>")
99 defer delete(output_file)
100
101 call StopVimInTerminal(buf)
102
103 let result = readfile(output_file)
104
105 call assert_equal(["/* int main() {} */"], result)
106endfunc
107
108func Test_bothends_uncomment()
109 CheckScreendump
110 let lines =<< trim END
111 /* int main() { */
112 /* return 0; */
113 /* } */
114 END
115
116 let input_file = "test_bothends_uncomment_input.c"
117 call writefile(lines, input_file, "D")
118
119 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
120
121 call term_sendkeys(buf, "gcip")
122 let output_file = "uncomment_bothends_test.c"
123 call term_sendkeys(buf, $":w {output_file}\<CR>")
124 defer delete(output_file)
125
126 call StopVimInTerminal(buf)
127
128 let result = readfile(output_file)
129
130 call assert_equal(["int main() {", " return 0;", "}"], result)
131endfunc
132
Maxim Kim51ff18e2025-03-19 20:54:12 +0100133func Test_mixed_comment()
134 CheckScreendump
135 let lines =<< trim END
136 for x in range(10):
137 # print(x)
138 # print(x*x)
139 END
140
141 let input_file = "test_mixed_comment_input.py"
142 call writefile(lines, input_file, "D")
143
144 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
145
146 call term_sendkeys(buf, "gcG")
147 let output_file = "comment_mixed_test.py"
148 call term_sendkeys(buf, $":w {output_file}\<CR>")
149 defer delete(output_file)
150
151 call StopVimInTerminal(buf)
152
153 let result = readfile(output_file)
154
155 call assert_equal(["# for x in range(10):", "# # print(x)", "# # print(x*x)"], result)
156endfunc
157
158func Test_mixed_comment2()
159 CheckScreendump
160 let lines =<< trim END
161 # for x in range(10):
162 print(x)
163 # print(x*x)
164 END
165
166 let input_file = "test_mixed_comment_input2.py"
167 call writefile(lines, input_file, "D")
168
169 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
170
171 call term_sendkeys(buf, "gcG")
172 let output_file = "comment_mixed_test2.py"
173 call term_sendkeys(buf, $":w {output_file}\<CR>")
174 defer delete(output_file)
175
176 call StopVimInTerminal(buf)
177
178 let result = readfile(output_file)
179
180 call assert_equal(["# # for x in range(10):", "# print(x)", "# # print(x*x)"], result)
181endfunc
182
183func Test_mixed_indent_comment()
184 CheckScreendump
185 let lines = ["int main() {", "\tif 1 {", "\t return 0;", "\t}", " return 1;", "}"]
186
187 let input_file = "test_mixed_indent_comment_input.c"
188 call writefile(lines, input_file, "D")
189
190 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
191
192 call term_sendkeys(buf, "gcip")
193 let output_file = "comment_mixed_indent_test.c"
194 call term_sendkeys(buf, $":w {output_file}\<CR>")
195 defer delete(output_file)
196
197 call StopVimInTerminal(buf)
198
199 let result = readfile(output_file)
200
201 call assert_equal(["/* int main() { */", "\t/* if 1 { */", "\t /* return 0; */", "\t/* } */", " /* return 1; */", "/* } */"], result)
202endfunc
Maxim Kim9712a252025-03-21 18:02:56 +0100203
Maxim Kimaa68f8a2025-03-28 19:17:53 +0100204func Test_buffer_first_col_comment()
205 CheckScreendump
206 let lines =<< trim END
207 def Hello():
208 print("Hello")
209 pass
210 END
211
212 let input_file = "test_first_col_comment_input.py"
213 call writefile(lines, input_file, "D")
214
215 let buf = RunVimInTerminal('-c "packadd comment" -c "let b:comment_first_col=1" ' .. input_file, {})
216
217 call term_sendkeys(buf, "jgcc")
218 let output_file = "comment_first_col_test.py"
219 call term_sendkeys(buf, $":w {output_file}\<CR>")
220 defer delete(output_file)
221
222 call StopVimInTerminal(buf)
223
224 let result = readfile(output_file)
225
226 call assert_equal(["def Hello():", '# print("Hello")', " pass"], result)
227endfunc
228
229func Test_global_first_col_comment()
230 CheckScreendump
231 let lines =<< trim END
232 def Hello():
233 print("Hello")
234 pass
235 END
236
237 let input_file = "test_first_col_comment_input.py"
238 call writefile(lines, input_file, "D")
239
240 let buf = RunVimInTerminal('-c "packadd comment" -c "let g:comment_first_col=1" ' .. input_file, {})
241
242 call term_sendkeys(buf, "jgcj")
243 let output_file = "comment_first_col_test.py"
244 call term_sendkeys(buf, $":w {output_file}\<CR>")
245 defer delete(output_file)
246
247 call StopVimInTerminal(buf)
248
249 let result = readfile(output_file)
250
251 call assert_equal(["def Hello():", '# print("Hello")', "# pass"], result)
252endfunc
253
Maxim Kim9712a252025-03-21 18:02:56 +0100254func Test_textobj_icomment()
255 CheckScreendump
256 let lines =<< trim END
257 for x in range(10):
258 print(x) # printing stuff
259 # print(x*x)
260 #print(x*x*x)
261 print(x*x*x*x) # printing stuff
262 print(x*x*x*x*x) # printing stuff
263 # print(x*x)
264 #print(x*x*x)
265
266 print(x*x*x*x*x)
267 END
268
269 let input_file = "test_textobj_icomment_input.py"
270 call writefile(lines, input_file, "D")
271
272 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
273
274 call term_sendkeys(buf, "dic..")
275 let output_file = "comment_textobj_icomment.py"
276 call term_sendkeys(buf, $":w {output_file}\<CR>")
277 defer delete(output_file)
278
279 call StopVimInTerminal(buf)
280
281 let result = readfile(output_file)
282
283 call assert_equal(["for x in range(10):", " print(x) ", " print(x*x*x*x) ", " print(x*x*x*x*x) ", "", " print(x*x*x*x*x)"], result)
284endfunc
285
286func Test_textobj_icomment2()
287 CheckScreendump
288 let lines =<< trim END
289 #include <stdio.h>
290
291 int main() {
292 printf("hello"); /* hello world */ printf(" world\n");
293 /* if 1 {
294 return 1;
295 }*/
296
297 return 0;
298 }
299 END
300
301 let input_file = "test_textobj_icomment2_input.c"
302 call writefile(lines, input_file, "D")
303
304 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
305
306 call term_sendkeys(buf, "dic..")
307 let output_file = "comment_textobj_icomment2.c"
308 call term_sendkeys(buf, $":w {output_file}\<CR>")
309 defer delete(output_file)
310
311 call StopVimInTerminal(buf)
312
313 let result = readfile(output_file)
314
315 call assert_equal(["#include <stdio.h>", "", "int main() {", " printf(\"hello\"); printf(\" world\\n\");", " ", "", " return 0;", "}"], result)
316endfunc
317
318func Test_textobj_icomment3()
319 CheckScreendump
320 let lines =<< trim END
321 #include <stdio.h>
322
323 int main() {
324 printf("hello");/*hello world*/printf(" world\n");
325 return 0;
326 }
327 END
328
329 let input_file = "test_textobj_icomment3_input.c"
330 call writefile(lines, input_file, "D")
331
332 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
333
334 call term_sendkeys(buf, "jjjdic")
335 let output_file = "comment_textobj_icomment3.c"
336 call term_sendkeys(buf, $":w {output_file}\<CR>")
337 defer delete(output_file)
338
339 call StopVimInTerminal(buf)
340
341 let result = readfile(output_file)
342
343 call assert_equal(["#include <stdio.h>", "", "int main() {", " printf(\"hello\");printf(\" world\\n\");", " return 0;", "}"], result)
344endfunc
345
346func Test_textobj_acomment()
347 CheckScreendump
348 let lines =<< trim END
349 for x in range(10):
350 print(x) # printing stuff
351 # print(x*x)
352 #print(x*x*x)
353 print(x*x*x*x) # printing stuff
354 print(x*x*x*x*x) # printing stuff
355 # print(x*x)
356 #print(x*x*x)
357
358 print(x*x*x*x*x)
359 END
360
361 let input_file = "test_textobj_acomment_input.py"
362 call writefile(lines, input_file, "D")
363
364 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
365
366 call term_sendkeys(buf, "dac..")
367 let output_file = "comment_textobj_acomment.py"
368 call term_sendkeys(buf, $":w {output_file}\<CR>")
369 defer delete(output_file)
370
371 call StopVimInTerminal(buf)
372
373 let result = readfile(output_file)
374
375 call assert_equal(["for x in range(10):", " print(x)", " print(x*x*x*x)", " print(x*x*x*x*x)", "", " print(x*x*x*x*x)"], result)
376endfunc
377
378func Test_textobj_acomment2()
379 CheckScreendump
380 let lines =<< trim END
381 #include <stdio.h>
382
383 int main() {
384 printf("hello"); /* hello world */ printf(" world\n");
385 /* if 1 {
386 return 1;
387 }*/
388
389 return 0;
390 }
391 END
392
393 let input_file = "test_textobj_acomment2_input.c"
394 call writefile(lines, input_file, "D")
395
396 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
397
398 call term_sendkeys(buf, "dac.")
399 let output_file = "comment_textobj_acomment2.c"
400 call term_sendkeys(buf, $":w {output_file}\<CR>")
401 defer delete(output_file)
402
403 call StopVimInTerminal(buf)
404
405 let result = readfile(output_file)
406
407 call assert_equal(["#include <stdio.h>", "", "int main() {", " printf(\"hello\");printf(\" world\\n\");", " return 0;", "}"], result)
408endfunc
409
410func Test_textobj_acomment3()
411 CheckScreendump
412 let lines =<< trim END
413 #include <stdio.h>
414
415 int main() {
416 printf("hello");/*hello world*/printf(" world\n");
417 return 0;
418 }
419 END
420
421 let input_file = "test_textobj_acomment3_input.c"
422 call writefile(lines, input_file, "D")
423
424 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
425
426 call term_sendkeys(buf, "jjjdac")
427 let output_file = "comment_textobj_acomment3.c"
428 call term_sendkeys(buf, $":w {output_file}\<CR>")
429 defer delete(output_file)
430
431 call StopVimInTerminal(buf)
432
433 let result = readfile(output_file)
434
435 call assert_equal(["#include <stdio.h>", "", "int main() {", " printf(\"hello\");printf(\" world\\n\");", " return 0;", "}"], result)
436endfunc
437
438func Test_textobj_firstline_comment()
439 CheckScreendump
440 let lines =<< trim END
441 /*#include <stdio.h>*/
442
443 int main() {}
444 END
445
446 let input_file = "test_textobj_firstlinecomment_input.c"
447 call writefile(lines, input_file, "D")
448
449 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
450
451 call term_sendkeys(buf, "dac")
452 let output_file = "comment_textobj_firstline_comment.c"
453 call term_sendkeys(buf, $":w {output_file}\<CR>")
454 defer delete(output_file)
455
456 call StopVimInTerminal(buf)
457
458 let result = readfile(output_file)
459
460 call assert_equal(["int main() {}"], result)
461endfunc
462
463func Test_textobj_noleading_space_comment()
464 CheckScreendump
465 let lines =<< trim END
466 int main() {// main start
467 }/* main end */
468 END
469
470 let input_file = "test_textobj_noleading_space_input.c"
471 call writefile(lines, input_file, "D")
472
473 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
474
475 call term_sendkeys(buf, "dacdic")
476 let output_file = "comment_textobj_noleading_space_comment.c"
477 call term_sendkeys(buf, $":w {output_file}\<CR>")
478 defer delete(output_file)
479
480 call StopVimInTerminal(buf)
481
482 let result = readfile(output_file)
483
484 call assert_equal(["int main() {", "}"], result)
485endfunc
486
487func Test_textobj_noleading_space_comment2()
488 CheckScreendump
489 let lines =<< trim END
490 int main() {// main start
491 } /* main end */
492 END
493
494 let input_file = "test_textobj_noleading_space_input2.c"
495 call writefile(lines, input_file, "D")
496
497 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
498
499 call term_sendkeys(buf, "dac.")
500 let output_file = "comment_textobj_noleading_space_comment2.c"
501 call term_sendkeys(buf, $":w {output_file}\<CR>")
502 defer delete(output_file)
503
504 call StopVimInTerminal(buf)
505
506 let result = readfile(output_file)
507
508 call assert_equal(["int main() {", "}"], result)
509endfunc
510
Maxim Kima5807612025-03-30 14:55:26 +0200511func Test_textobj_trailing_spaces_comment()
512 CheckScreendump
513 let lines = ['# print("hello") ', '# print("world") ', "#", 'print("!")']
514
515 let input_file = "test_textobj_trailing_spaces_input.py"
516 call writefile(lines, input_file, "D")
517
518 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
519
520 call term_sendkeys(buf, "jdac")
521 let output_file = "comment_textobj_trailing_spaces_comment.py"
522 call term_sendkeys(buf, $":w {output_file}\<CR>")
523 defer delete(output_file)
524
525 call StopVimInTerminal(buf)
526
527 let result = readfile(output_file)
528
529 call assert_equal(['print("!")'], result)
530endfunc
531
532func Test_textobj_trailing_spaces_last_comment()
533 CheckScreendump
534 let lines = ['# print("hello") ', '# print("world") ', "#", '', '']
535
536 let input_file = "test_textobj_trailing_spaces_last_input.py"
537 call writefile(lines, input_file, "D")
538
539 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
540
541 call term_sendkeys(buf, "jdac")
542 let output_file = "comment_textobj_trailing_spaces_last_comment.py"
543 call term_sendkeys(buf, $":w {output_file}\<CR>")
544 defer delete(output_file)
545
546 call StopVimInTerminal(buf)
547
548 let result = readfile(output_file)
549
550 call assert_equal([], result)
551endfunc
552
553func Test_textobj_last_line_empty_comment()
554 CheckScreendump
555 let lines =<< trim END
556 # print("hello")
557 #
558 #
559 END
560
561 let input_file = "test_textobj_last_line_empty_input.py"
562 call writefile(lines, input_file, "D")
563
564 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
565
566 call term_sendkeys(buf, "dac")
567 let output_file = "comment_textobj_last_line_empty_comment.py"
568 call term_sendkeys(buf, $":w {output_file}\<CR>")
569 defer delete(output_file)
570
571 call StopVimInTerminal(buf)
572
573 let result = readfile(output_file)
574
575 call assert_equal([], result)
576endfunc
Maxim Kimdd776dd2025-04-22 20:11:05 +0200577
Maxim Kim9712a252025-03-21 18:02:56 +0100578func Test_textobj_cursor_on_leading_space_comment()
579 CheckScreendump
580 let lines =<< trim END
581 int main() {
582 // multilple comments
583 // cursor is between them
584 }
585 END
586
587 let input_file = "test_textobj_cursor_on_leading_space_comment_input.c"
588 call writefile(lines, input_file, "D")
589
590 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
591
592 call term_sendkeys(buf, "jjdac")
593 let output_file = "comment_textobj_cursor_on_leading_space_comment.c"
594 call term_sendkeys(buf, $":w {output_file}\<CR>")
595 defer delete(output_file)
596
597 call StopVimInTerminal(buf)
598
599 let result = readfile(output_file)
600
Maxim Kim08283b22025-03-26 19:04:20 +0100601 call assert_equal(["int main() {", "}"], result)
Maxim Kim9712a252025-03-21 18:02:56 +0100602endfunc
603
604func Test_textobj_conseq_comment()
605 CheckScreendump
606 let lines =<< trim END
607 int main() {
608 printf("hello"); // hello
609 // world
610 printf("world");
611 }
612 END
613
614 let input_file = "test_textobj_conseq_comment_input.c"
615 call writefile(lines, input_file, "D")
616
617 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
618
619 call term_sendkeys(buf, "dac")
620 let output_file = "comment_textobj_conseq_comment.c"
621 call term_sendkeys(buf, $":w {output_file}\<CR>")
622 defer delete(output_file)
623
624 call StopVimInTerminal(buf)
625
626 let result = readfile(output_file)
627
628 call assert_equal(["int main() {", " printf(\"hello\");", " printf(\"world\");", "}"], result)
629endfunc
630
631func Test_textobj_conseq_comment2()
632 CheckScreendump
633 let lines =<< trim END
634 int main() {
635 printf("hello"); // hello
636
637 // world
638 printf("world");
639 }
640 END
641
642 let input_file = "test_textobj_conseq_comment_input2.c"
643 call writefile(lines, input_file, "D")
644
645 let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {})
646
647 call term_sendkeys(buf, "dac")
648 let output_file = "comment_textobj_conseq_comment2.c"
649 call term_sendkeys(buf, $":w {output_file}\<CR>")
650 defer delete(output_file)
651
652 call StopVimInTerminal(buf)
653
654 let result = readfile(output_file)
655
656 call assert_equal(["int main() {", " printf(\"hello\");", "", " // world", " printf(\"world\");", "}"], result)
657endfunc
Maxim Kim08283b22025-03-26 19:04:20 +0100658
659func Test_inline_comment()
660 CheckScreendump
661 let lines =<< trim END
662 echo "Hello" This should be a comment
663 END
664
665 let input_file = "test_inline_comment_input.vim"
666 call writefile(lines, input_file, "D")
667
668 let buf = RunVimInTerminal('-c "packadd comment" -c "nnoremap <expr> gC comment#Toggle()..''$''" ' .. input_file, {})
669
670 call term_sendkeys(buf, "fTgC")
671
672 let output_file = "comment_inline_test.vim"
673 call term_sendkeys(buf, $":w {output_file}\<CR>")
674 defer delete(output_file)
675
676 call StopVimInTerminal(buf)
677
678 let result = readfile(output_file)
679 call assert_equal(['echo "Hello" " This should be a comment'], result)
680endfunc
681
682func Test_inline_uncomment()
683 CheckScreendump
684 let lines =<< trim END
685 echo "Hello" " This should be a comment
686 END
687
688 let input_file = "test_inline_uncomment_input.vim"
689 call writefile(lines, input_file, "D")
690
691 let buf = RunVimInTerminal('-c "packadd comment" -c "nnoremap <expr> gC comment#Toggle()..''$''" ' .. input_file, {})
692
693 call term_sendkeys(buf, '$F"gC')
694
695 let output_file = "uncomment_inline_test.vim"
696 call term_sendkeys(buf, $":w {output_file}\<CR>")
697 defer delete(output_file)
698
699 call StopVimInTerminal(buf)
700
701 let result = readfile(output_file)
702 call assert_equal(['echo "Hello" This should be a comment'], result)
703endfunc
Maxim Kim0e59e672025-04-12 11:34:08 +0200704
705func Test_textobj_selection_exclusive_inline_comment()
706 CheckScreendump
707 let lines =<< trim END
708 print("Hello") # selection exclusive
709 END
710
711 let input_file = "test_selection_exclusive_inline_comment_input.py"
712 call writefile(lines, input_file, "D")
713
714 let buf = RunVimInTerminal('-c "set selection=exclusive" -c "packadd comment" ' .. input_file, {})
715
716 call term_sendkeys(buf, "dac")
717
718 let output_file = "test_selection_exclusive_inline_comment.py"
719 call term_sendkeys(buf, $":w {output_file}\<CR>")
720 defer delete(output_file)
721
722 call StopVimInTerminal(buf)
723
724 let result = readfile(output_file)
725 call assert_equal(['print("Hello")'], result)
726endfunc