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