Maxim Kim | 51ff18e | 2025-03-19 20:54:12 +0100 | [diff] [blame] | 1 | source check.vim |
| 2 | source term_util.vim |
| 3 | |
| 4 | func Test_basic_comment() |
| 5 | CheckScreendump |
| 6 | let lines =<< trim END |
| 7 | vim9script |
| 8 | |
| 9 | def Hello() |
| 10 | echo "Hello" |
| 11 | enddef |
| 12 | END |
| 13 | |
| 14 | let input_file = "test_basic_comment_input.vim" |
| 15 | call writefile(lines, input_file, "D") |
| 16 | |
| 17 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 18 | |
| 19 | call term_sendkeys(buf, "gcc") |
| 20 | call term_sendkeys(buf, "2jgcip") |
| 21 | let output_file = "comment_basic_test.vim" |
| 22 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 23 | defer delete(output_file) |
| 24 | |
| 25 | call StopVimInTerminal(buf) |
| 26 | |
| 27 | let result = readfile(output_file) |
| 28 | |
| 29 | call assert_equal(["# vim9script", "", "# def Hello()", '# echo "Hello"', "# enddef"], result) |
| 30 | endfunc |
| 31 | |
Maxim Kim | 51ff18e | 2025-03-19 20:54:12 +0100 | [diff] [blame] | 32 | func Test_basic_uncomment() |
| 33 | CheckScreendump |
| 34 | let lines =<< trim END |
| 35 | vim9script |
| 36 | |
| 37 | # def Hello() |
| 38 | # echo "Hello" |
| 39 | # enddef |
| 40 | END |
| 41 | |
| 42 | let input_file = "test_basic_uncomment_input.vim" |
| 43 | call writefile(lines, input_file, "D") |
| 44 | |
| 45 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 46 | |
| 47 | call term_sendkeys(buf, "gcc") |
| 48 | call term_sendkeys(buf, "2jgcip") |
| 49 | let output_file = "uncomment_basic_test.vim" |
| 50 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 51 | defer delete(output_file) |
| 52 | |
| 53 | call StopVimInTerminal(buf) |
| 54 | |
| 55 | let result = readfile(output_file) |
| 56 | |
Maxim Kim | 9712a25 | 2025-03-21 18:02:56 +0100 | [diff] [blame] | 57 | call assert_equal(["# vim9script", "", "def Hello()", ' echo "Hello"', "enddef"], result) |
Maxim Kim | 51ff18e | 2025-03-19 20:54:12 +0100 | [diff] [blame] | 58 | endfunc |
| 59 | |
| 60 | func Test_bothends_comment() |
| 61 | CheckScreendump |
| 62 | let lines =<< trim END |
| 63 | int main() {} |
| 64 | END |
| 65 | |
| 66 | let input_file = "test_bothends_comment_input.c" |
| 67 | call writefile(lines, input_file, "D") |
| 68 | |
| 69 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 70 | |
| 71 | call term_sendkeys(buf, "gcc") |
| 72 | let output_file = "comment_bothends_test.c" |
| 73 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 74 | defer delete(output_file) |
| 75 | |
| 76 | call StopVimInTerminal(buf) |
| 77 | |
| 78 | let result = readfile(output_file) |
| 79 | |
| 80 | call assert_equal(["/* int main() {} */"], result) |
| 81 | endfunc |
| 82 | |
| 83 | func Test_bothends_uncomment() |
| 84 | CheckScreendump |
| 85 | let lines =<< trim END |
| 86 | /* int main() { */ |
| 87 | /* return 0; */ |
| 88 | /* } */ |
| 89 | END |
| 90 | |
| 91 | let input_file = "test_bothends_uncomment_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, "gcip") |
| 97 | let output_file = "uncomment_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() {", " return 0;", "}"], result) |
| 106 | endfunc |
| 107 | |
| 108 | |
| 109 | func Test_mixed_comment() |
| 110 | CheckScreendump |
| 111 | let lines =<< trim END |
| 112 | for x in range(10): |
| 113 | # print(x) |
| 114 | # print(x*x) |
| 115 | END |
| 116 | |
| 117 | let input_file = "test_mixed_comment_input.py" |
| 118 | call writefile(lines, input_file, "D") |
| 119 | |
| 120 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 121 | |
| 122 | call term_sendkeys(buf, "gcG") |
| 123 | let output_file = "comment_mixed_test.py" |
| 124 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 125 | defer delete(output_file) |
| 126 | |
| 127 | call StopVimInTerminal(buf) |
| 128 | |
| 129 | let result = readfile(output_file) |
| 130 | |
| 131 | call assert_equal(["# for x in range(10):", "# # print(x)", "# # print(x*x)"], result) |
| 132 | endfunc |
| 133 | |
| 134 | func Test_mixed_comment2() |
| 135 | CheckScreendump |
| 136 | let lines =<< trim END |
| 137 | # for x in range(10): |
| 138 | print(x) |
| 139 | # print(x*x) |
| 140 | END |
| 141 | |
| 142 | let input_file = "test_mixed_comment_input2.py" |
| 143 | call writefile(lines, input_file, "D") |
| 144 | |
| 145 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 146 | |
| 147 | call term_sendkeys(buf, "gcG") |
| 148 | let output_file = "comment_mixed_test2.py" |
| 149 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 150 | defer delete(output_file) |
| 151 | |
| 152 | call StopVimInTerminal(buf) |
| 153 | |
| 154 | let result = readfile(output_file) |
| 155 | |
| 156 | call assert_equal(["# # for x in range(10):", "# print(x)", "# # print(x*x)"], result) |
| 157 | endfunc |
| 158 | |
| 159 | func Test_mixed_indent_comment() |
| 160 | CheckScreendump |
| 161 | let lines = ["int main() {", "\tif 1 {", "\t return 0;", "\t}", " return 1;", "}"] |
| 162 | |
| 163 | let input_file = "test_mixed_indent_comment_input.c" |
| 164 | call writefile(lines, input_file, "D") |
| 165 | |
| 166 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 167 | |
| 168 | call term_sendkeys(buf, "gcip") |
| 169 | let output_file = "comment_mixed_indent_test.c" |
| 170 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 171 | defer delete(output_file) |
| 172 | |
| 173 | call StopVimInTerminal(buf) |
| 174 | |
| 175 | let result = readfile(output_file) |
| 176 | |
| 177 | call assert_equal(["/* int main() { */", "\t/* if 1 { */", "\t /* return 0; */", "\t/* } */", " /* return 1; */", "/* } */"], result) |
| 178 | endfunc |
Maxim Kim | 9712a25 | 2025-03-21 18:02:56 +0100 | [diff] [blame] | 179 | |
| 180 | func Test_textobj_icomment() |
| 181 | CheckScreendump |
| 182 | let lines =<< trim END |
| 183 | for x in range(10): |
| 184 | print(x) # printing stuff |
| 185 | # print(x*x) |
| 186 | #print(x*x*x) |
| 187 | print(x*x*x*x) # printing stuff |
| 188 | print(x*x*x*x*x) # printing stuff |
| 189 | # print(x*x) |
| 190 | #print(x*x*x) |
| 191 | |
| 192 | print(x*x*x*x*x) |
| 193 | END |
| 194 | |
| 195 | let input_file = "test_textobj_icomment_input.py" |
| 196 | call writefile(lines, input_file, "D") |
| 197 | |
| 198 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 199 | |
| 200 | call term_sendkeys(buf, "dic..") |
| 201 | let output_file = "comment_textobj_icomment.py" |
| 202 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 203 | defer delete(output_file) |
| 204 | |
| 205 | call StopVimInTerminal(buf) |
| 206 | |
| 207 | let result = readfile(output_file) |
| 208 | |
| 209 | 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) |
| 210 | endfunc |
| 211 | |
| 212 | func Test_textobj_icomment2() |
| 213 | CheckScreendump |
| 214 | let lines =<< trim END |
| 215 | #include <stdio.h> |
| 216 | |
| 217 | int main() { |
| 218 | printf("hello"); /* hello world */ printf(" world\n"); |
| 219 | /* if 1 { |
| 220 | return 1; |
| 221 | }*/ |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | END |
| 226 | |
| 227 | let input_file = "test_textobj_icomment2_input.c" |
| 228 | call writefile(lines, input_file, "D") |
| 229 | |
| 230 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 231 | |
| 232 | call term_sendkeys(buf, "dic..") |
| 233 | let output_file = "comment_textobj_icomment2.c" |
| 234 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 235 | defer delete(output_file) |
| 236 | |
| 237 | call StopVimInTerminal(buf) |
| 238 | |
| 239 | let result = readfile(output_file) |
| 240 | |
| 241 | call assert_equal(["#include <stdio.h>", "", "int main() {", " printf(\"hello\"); printf(\" world\\n\");", " ", "", " return 0;", "}"], result) |
| 242 | endfunc |
| 243 | |
| 244 | func Test_textobj_icomment3() |
| 245 | CheckScreendump |
| 246 | let lines =<< trim END |
| 247 | #include <stdio.h> |
| 248 | |
| 249 | int main() { |
| 250 | printf("hello");/*hello world*/printf(" world\n"); |
| 251 | return 0; |
| 252 | } |
| 253 | END |
| 254 | |
| 255 | let input_file = "test_textobj_icomment3_input.c" |
| 256 | call writefile(lines, input_file, "D") |
| 257 | |
| 258 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 259 | |
| 260 | call term_sendkeys(buf, "jjjdic") |
| 261 | let output_file = "comment_textobj_icomment3.c" |
| 262 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 263 | defer delete(output_file) |
| 264 | |
| 265 | call StopVimInTerminal(buf) |
| 266 | |
| 267 | let result = readfile(output_file) |
| 268 | |
| 269 | call assert_equal(["#include <stdio.h>", "", "int main() {", " printf(\"hello\");printf(\" world\\n\");", " return 0;", "}"], result) |
| 270 | endfunc |
| 271 | |
| 272 | func Test_textobj_acomment() |
| 273 | CheckScreendump |
| 274 | let lines =<< trim END |
| 275 | for x in range(10): |
| 276 | print(x) # printing stuff |
| 277 | # print(x*x) |
| 278 | #print(x*x*x) |
| 279 | print(x*x*x*x) # printing stuff |
| 280 | print(x*x*x*x*x) # printing stuff |
| 281 | # print(x*x) |
| 282 | #print(x*x*x) |
| 283 | |
| 284 | print(x*x*x*x*x) |
| 285 | END |
| 286 | |
| 287 | let input_file = "test_textobj_acomment_input.py" |
| 288 | call writefile(lines, input_file, "D") |
| 289 | |
| 290 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 291 | |
| 292 | call term_sendkeys(buf, "dac..") |
| 293 | let output_file = "comment_textobj_acomment.py" |
| 294 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 295 | defer delete(output_file) |
| 296 | |
| 297 | call StopVimInTerminal(buf) |
| 298 | |
| 299 | let result = readfile(output_file) |
| 300 | |
| 301 | 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) |
| 302 | endfunc |
| 303 | |
| 304 | func Test_textobj_acomment2() |
| 305 | CheckScreendump |
| 306 | let lines =<< trim END |
| 307 | #include <stdio.h> |
| 308 | |
| 309 | int main() { |
| 310 | printf("hello"); /* hello world */ printf(" world\n"); |
| 311 | /* if 1 { |
| 312 | return 1; |
| 313 | }*/ |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | END |
| 318 | |
| 319 | let input_file = "test_textobj_acomment2_input.c" |
| 320 | call writefile(lines, input_file, "D") |
| 321 | |
| 322 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 323 | |
| 324 | call term_sendkeys(buf, "dac.") |
| 325 | let output_file = "comment_textobj_acomment2.c" |
| 326 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 327 | defer delete(output_file) |
| 328 | |
| 329 | call StopVimInTerminal(buf) |
| 330 | |
| 331 | let result = readfile(output_file) |
| 332 | |
| 333 | call assert_equal(["#include <stdio.h>", "", "int main() {", " printf(\"hello\");printf(\" world\\n\");", " return 0;", "}"], result) |
| 334 | endfunc |
| 335 | |
| 336 | func Test_textobj_acomment3() |
| 337 | CheckScreendump |
| 338 | let lines =<< trim END |
| 339 | #include <stdio.h> |
| 340 | |
| 341 | int main() { |
| 342 | printf("hello");/*hello world*/printf(" world\n"); |
| 343 | return 0; |
| 344 | } |
| 345 | END |
| 346 | |
| 347 | let input_file = "test_textobj_acomment3_input.c" |
| 348 | call writefile(lines, input_file, "D") |
| 349 | |
| 350 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 351 | |
| 352 | call term_sendkeys(buf, "jjjdac") |
| 353 | let output_file = "comment_textobj_acomment3.c" |
| 354 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 355 | defer delete(output_file) |
| 356 | |
| 357 | call StopVimInTerminal(buf) |
| 358 | |
| 359 | let result = readfile(output_file) |
| 360 | |
| 361 | call assert_equal(["#include <stdio.h>", "", "int main() {", " printf(\"hello\");printf(\" world\\n\");", " return 0;", "}"], result) |
| 362 | endfunc |
| 363 | |
| 364 | func Test_textobj_firstline_comment() |
| 365 | CheckScreendump |
| 366 | let lines =<< trim END |
| 367 | /*#include <stdio.h>*/ |
| 368 | |
| 369 | int main() {} |
| 370 | END |
| 371 | |
| 372 | let input_file = "test_textobj_firstlinecomment_input.c" |
| 373 | call writefile(lines, input_file, "D") |
| 374 | |
| 375 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 376 | |
| 377 | call term_sendkeys(buf, "dac") |
| 378 | let output_file = "comment_textobj_firstline_comment.c" |
| 379 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 380 | defer delete(output_file) |
| 381 | |
| 382 | call StopVimInTerminal(buf) |
| 383 | |
| 384 | let result = readfile(output_file) |
| 385 | |
| 386 | call assert_equal(["int main() {}"], result) |
| 387 | endfunc |
| 388 | |
| 389 | func Test_textobj_noleading_space_comment() |
| 390 | CheckScreendump |
| 391 | let lines =<< trim END |
| 392 | int main() {// main start |
| 393 | }/* main end */ |
| 394 | END |
| 395 | |
| 396 | let input_file = "test_textobj_noleading_space_input.c" |
| 397 | call writefile(lines, input_file, "D") |
| 398 | |
| 399 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 400 | |
| 401 | call term_sendkeys(buf, "dacdic") |
| 402 | let output_file = "comment_textobj_noleading_space_comment.c" |
| 403 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 404 | defer delete(output_file) |
| 405 | |
| 406 | call StopVimInTerminal(buf) |
| 407 | |
| 408 | let result = readfile(output_file) |
| 409 | |
| 410 | call assert_equal(["int main() {", "}"], result) |
| 411 | endfunc |
| 412 | |
| 413 | func Test_textobj_noleading_space_comment2() |
| 414 | CheckScreendump |
| 415 | let lines =<< trim END |
| 416 | int main() {// main start |
| 417 | } /* main end */ |
| 418 | END |
| 419 | |
| 420 | let input_file = "test_textobj_noleading_space_input2.c" |
| 421 | call writefile(lines, input_file, "D") |
| 422 | |
| 423 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 424 | |
| 425 | call term_sendkeys(buf, "dac.") |
| 426 | let output_file = "comment_textobj_noleading_space_comment2.c" |
| 427 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 428 | defer delete(output_file) |
| 429 | |
| 430 | call StopVimInTerminal(buf) |
| 431 | |
| 432 | let result = readfile(output_file) |
| 433 | |
| 434 | call assert_equal(["int main() {", "}"], result) |
| 435 | endfunc |
| 436 | |
| 437 | func Test_textobj_cursor_on_leading_space_comment() |
| 438 | CheckScreendump |
| 439 | let lines =<< trim END |
| 440 | int main() { |
| 441 | // multilple comments |
| 442 | // cursor is between them |
| 443 | } |
| 444 | END |
| 445 | |
| 446 | let input_file = "test_textobj_cursor_on_leading_space_comment_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, "jjdac") |
| 452 | let output_file = "comment_textobj_cursor_on_leading_space_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 | |
Maxim Kim | 08283b2 | 2025-03-26 19:04:20 +0100 | [diff] [blame] | 460 | call assert_equal(["int main() {", "}"], result) |
Maxim Kim | 9712a25 | 2025-03-21 18:02:56 +0100 | [diff] [blame] | 461 | endfunc |
| 462 | |
| 463 | func Test_textobj_conseq_comment() |
| 464 | CheckScreendump |
| 465 | let lines =<< trim END |
| 466 | int main() { |
| 467 | printf("hello"); // hello |
| 468 | // world |
| 469 | printf("world"); |
| 470 | } |
| 471 | END |
| 472 | |
| 473 | let input_file = "test_textobj_conseq_comment_input.c" |
| 474 | call writefile(lines, input_file, "D") |
| 475 | |
| 476 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 477 | |
| 478 | call term_sendkeys(buf, "dac") |
| 479 | let output_file = "comment_textobj_conseq_comment.c" |
| 480 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 481 | defer delete(output_file) |
| 482 | |
| 483 | call StopVimInTerminal(buf) |
| 484 | |
| 485 | let result = readfile(output_file) |
| 486 | |
| 487 | call assert_equal(["int main() {", " printf(\"hello\");", " printf(\"world\");", "}"], result) |
| 488 | endfunc |
| 489 | |
| 490 | func Test_textobj_conseq_comment2() |
| 491 | CheckScreendump |
| 492 | let lines =<< trim END |
| 493 | int main() { |
| 494 | printf("hello"); // hello |
| 495 | |
| 496 | // world |
| 497 | printf("world"); |
| 498 | } |
| 499 | END |
| 500 | |
| 501 | let input_file = "test_textobj_conseq_comment_input2.c" |
| 502 | call writefile(lines, input_file, "D") |
| 503 | |
| 504 | let buf = RunVimInTerminal('-c "packadd comment" ' .. input_file, {}) |
| 505 | |
| 506 | call term_sendkeys(buf, "dac") |
| 507 | let output_file = "comment_textobj_conseq_comment2.c" |
| 508 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 509 | defer delete(output_file) |
| 510 | |
| 511 | call StopVimInTerminal(buf) |
| 512 | |
| 513 | let result = readfile(output_file) |
| 514 | |
| 515 | call assert_equal(["int main() {", " printf(\"hello\");", "", " // world", " printf(\"world\");", "}"], result) |
| 516 | endfunc |
Maxim Kim | 08283b2 | 2025-03-26 19:04:20 +0100 | [diff] [blame] | 517 | |
| 518 | func Test_inline_comment() |
| 519 | CheckScreendump |
| 520 | let lines =<< trim END |
| 521 | echo "Hello" This should be a comment |
| 522 | END |
| 523 | |
| 524 | let input_file = "test_inline_comment_input.vim" |
| 525 | call writefile(lines, input_file, "D") |
| 526 | |
| 527 | let buf = RunVimInTerminal('-c "packadd comment" -c "nnoremap <expr> gC comment#Toggle()..''$''" ' .. input_file, {}) |
| 528 | |
| 529 | call term_sendkeys(buf, "fTgC") |
| 530 | |
| 531 | let output_file = "comment_inline_test.vim" |
| 532 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 533 | defer delete(output_file) |
| 534 | |
| 535 | call StopVimInTerminal(buf) |
| 536 | |
| 537 | let result = readfile(output_file) |
| 538 | call assert_equal(['echo "Hello" " This should be a comment'], result) |
| 539 | endfunc |
| 540 | |
| 541 | func Test_inline_uncomment() |
| 542 | CheckScreendump |
| 543 | let lines =<< trim END |
| 544 | echo "Hello" " This should be a comment |
| 545 | END |
| 546 | |
| 547 | let input_file = "test_inline_uncomment_input.vim" |
| 548 | call writefile(lines, input_file, "D") |
| 549 | |
| 550 | let buf = RunVimInTerminal('-c "packadd comment" -c "nnoremap <expr> gC comment#Toggle()..''$''" ' .. input_file, {}) |
| 551 | |
| 552 | call term_sendkeys(buf, '$F"gC') |
| 553 | |
| 554 | let output_file = "uncomment_inline_test.vim" |
| 555 | call term_sendkeys(buf, $":w {output_file}\<CR>") |
| 556 | defer delete(output_file) |
| 557 | |
| 558 | call StopVimInTerminal(buf) |
| 559 | |
| 560 | let result = readfile(output_file) |
| 561 | call assert_equal(['echo "Hello" This should be a comment'], result) |
| 562 | endfunc |