Mercurial > vim
comparison src/testdir/test_normal.vim @ 26526:33d680d372aa v8.2.3792
patch 8.2.3792: setting *func options insufficiently tested
Commit: https://github.com/vim/vim/commit/04ef1fb13d200f770952e670357dddadb6210dd4
Author: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Sun Dec 12 20:08:05 2021 +0000
patch 8.2.3792: setting *func options insufficiently tested
Problem: Setting *func options insufficiently tested.
Solution: Impove tests. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/9337)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 12 Dec 2021 21:15:03 +0100 |
parents | 13ba00ef7687 |
children | 255bc9a08e58 |
comparison
equal
deleted
inserted
replaced
26525:508ea60b5118 | 26526:33d680d372aa |
---|---|
442 endfunc | 442 endfunc |
443 | 443 |
444 " Test for different ways of setting the 'operatorfunc' option | 444 " Test for different ways of setting the 'operatorfunc' option |
445 func Test_opfunc_callback() | 445 func Test_opfunc_callback() |
446 new | 446 new |
447 func MyopFunc(val, type) | 447 func OpFunc1(callnr, type) |
448 let g:OpFuncArgs = [a:val, a:type] | 448 let g:OpFunc1Args = [a:callnr, a:type] |
449 endfunc | 449 endfunc |
450 func OpFunc2(type) | |
451 let g:OpFunc2Args = [a:type] | |
452 endfunc | |
450 | 453 |
451 let lines =<< trim END | 454 let lines =<< trim END |
455 #" Test for using a function name | |
456 LET &opfunc = 'g:OpFunc2' | |
457 LET g:OpFunc2Args = [] | |
458 normal! g@l | |
459 call assert_equal(['char'], g:OpFunc2Args) | |
460 | |
452 #" Test for using a function() | 461 #" Test for using a function() |
453 set opfunc=function('g:MyopFunc',\ [10]) | 462 set opfunc=function('g:OpFunc1',\ [10]) |
454 LET g:OpFuncArgs = [] | 463 LET g:OpFunc1Args = [] |
455 normal! g@l | 464 normal! g@l |
456 call assert_equal([10, 'char'], g:OpFuncArgs) | 465 call assert_equal([10, 'char'], g:OpFunc1Args) |
457 | 466 |
458 #" Using a funcref variable to set 'operatorfunc' | 467 #" Using a funcref variable to set 'operatorfunc' |
459 VAR Fn = function('g:MyopFunc', [11]) | 468 VAR Fn = function('g:OpFunc1', [11]) |
460 LET &opfunc = Fn | 469 LET &opfunc = Fn |
461 LET g:OpFuncArgs = [] | 470 LET g:OpFunc1Args = [] |
462 normal! g@l | 471 normal! g@l |
463 call assert_equal([11, 'char'], g:OpFuncArgs) | 472 call assert_equal([11, 'char'], g:OpFunc1Args) |
464 | 473 |
465 #" Using a string(funcref_variable) to set 'operatorfunc' | 474 #" Using a string(funcref_variable) to set 'operatorfunc' |
466 LET Fn = function('g:MyopFunc', [12]) | 475 LET Fn = function('g:OpFunc1', [12]) |
467 LET &operatorfunc = string(Fn) | 476 LET &operatorfunc = string(Fn) |
468 LET g:OpFuncArgs = [] | 477 LET g:OpFunc1Args = [] |
469 normal! g@l | 478 normal! g@l |
470 call assert_equal([12, 'char'], g:OpFuncArgs) | 479 call assert_equal([12, 'char'], g:OpFunc1Args) |
471 | 480 |
472 #" Test for using a funcref() | 481 #" Test for using a funcref() |
473 set operatorfunc=funcref('g:MyopFunc',\ [13]) | 482 set operatorfunc=funcref('g:OpFunc1',\ [13]) |
474 LET g:OpFuncArgs = [] | 483 LET g:OpFunc1Args = [] |
475 normal! g@l | 484 normal! g@l |
476 call assert_equal([13, 'char'], g:OpFuncArgs) | 485 call assert_equal([13, 'char'], g:OpFunc1Args) |
477 | 486 |
478 #" Using a funcref variable to set 'operatorfunc' | 487 #" Using a funcref variable to set 'operatorfunc' |
479 LET Fn = funcref('g:MyopFunc', [14]) | 488 LET Fn = funcref('g:OpFunc1', [14]) |
480 LET &opfunc = Fn | 489 LET &opfunc = Fn |
481 LET g:OpFuncArgs = [] | 490 LET g:OpFunc1Args = [] |
482 normal! g@l | 491 normal! g@l |
483 call assert_equal([14, 'char'], g:OpFuncArgs) | 492 call assert_equal([14, 'char'], g:OpFunc1Args) |
484 | 493 |
485 #" Using a string(funcref_variable) to set 'operatorfunc' | 494 #" Using a string(funcref_variable) to set 'operatorfunc' |
486 LET Fn = funcref('g:MyopFunc', [15]) | 495 LET Fn = funcref('g:OpFunc1', [15]) |
487 LET &opfunc = string(Fn) | 496 LET &opfunc = string(Fn) |
488 LET g:OpFuncArgs = [] | 497 LET g:OpFunc1Args = [] |
489 normal! g@l | 498 normal! g@l |
490 call assert_equal([15, 'char'], g:OpFuncArgs) | 499 call assert_equal([15, 'char'], g:OpFunc1Args) |
491 | 500 |
492 #" Test for using a lambda function using set | 501 #" Test for using a lambda function using set |
493 VAR optval = "LSTART a LMIDDLE MyopFunc(16, a) LEND" | 502 VAR optval = "LSTART a LMIDDLE OpFunc1(16, a) LEND" |
494 LET optval = substitute(optval, ' ', '\\ ', 'g') | 503 LET optval = substitute(optval, ' ', '\\ ', 'g') |
495 exe "set opfunc=" .. optval | 504 exe "set opfunc=" .. optval |
496 LET g:OpFuncArgs = [] | 505 LET g:OpFunc1Args = [] |
497 normal! g@l | 506 normal! g@l |
498 call assert_equal([16, 'char'], g:OpFuncArgs) | 507 call assert_equal([16, 'char'], g:OpFunc1Args) |
499 | 508 |
500 #" Test for using a lambda function using LET | 509 #" Test for using a lambda function using LET |
501 LET &opfunc = LSTART a LMIDDLE MyopFunc(17, a) LEND | 510 LET &opfunc = LSTART a LMIDDLE OpFunc1(17, a) LEND |
502 LET g:OpFuncArgs = [] | 511 LET g:OpFunc1Args = [] |
503 normal! g@l | 512 normal! g@l |
504 call assert_equal([17, 'char'], g:OpFuncArgs) | 513 call assert_equal([17, 'char'], g:OpFunc1Args) |
505 | 514 |
506 #" Set 'operatorfunc' to a string(lambda expression) | 515 #" Set 'operatorfunc' to a string(lambda expression) |
507 LET &opfunc = 'LSTART a LMIDDLE MyopFunc(18, a) LEND' | 516 LET &opfunc = 'LSTART a LMIDDLE OpFunc1(18, a) LEND' |
508 LET g:OpFuncArgs = [] | 517 LET g:OpFunc1Args = [] |
509 normal! g@l | 518 normal! g@l |
510 call assert_equal([18, 'char'], g:OpFuncArgs) | 519 call assert_equal([18, 'char'], g:OpFunc1Args) |
511 | 520 |
512 #" Set 'operatorfunc' to a variable with a lambda expression | 521 #" Set 'operatorfunc' to a variable with a lambda expression |
513 VAR Lambda = LSTART a LMIDDLE MyopFunc(19, a) LEND | 522 VAR Lambda = LSTART a LMIDDLE OpFunc1(19, a) LEND |
514 LET &opfunc = Lambda | 523 LET &opfunc = Lambda |
515 LET g:OpFuncArgs = [] | 524 LET g:OpFunc1Args = [] |
516 normal! g@l | 525 normal! g@l |
517 call assert_equal([19, 'char'], g:OpFuncArgs) | 526 call assert_equal([19, 'char'], g:OpFunc1Args) |
518 | 527 |
519 #" Set 'operatorfunc' to a string(variable with a lambda expression) | 528 #" Set 'operatorfunc' to a string(variable with a lambda expression) |
520 LET Lambda = LSTART a LMIDDLE MyopFunc(20, a) LEND | 529 LET Lambda = LSTART a LMIDDLE OpFunc1(20, a) LEND |
521 LET &opfunc = string(Lambda) | 530 LET &opfunc = string(Lambda) |
522 LET g:OpFuncArgs = [] | 531 LET g:OpFunc1Args = [] |
523 normal! g@l | 532 normal! g@l |
524 call assert_equal([20, 'char'], g:OpFuncArgs) | 533 call assert_equal([20, 'char'], g:OpFunc1Args) |
525 | 534 |
526 #" Try to use 'operatorfunc' after the function is deleted | 535 #" Try to use 'operatorfunc' after the function is deleted |
527 func g:TmpOpFunc(type) | 536 func g:TmpOpFunc1(type) |
528 LET g:OpFuncArgs = [21, a:type] | 537 let g:TmpOpFunc1Args = [21, a:type] |
529 endfunc | 538 endfunc |
530 LET &opfunc = function('g:TmpOpFunc') | 539 LET &opfunc = function('g:TmpOpFunc1') |
531 delfunc g:TmpOpFunc | 540 delfunc g:TmpOpFunc1 |
532 call test_garbagecollect_now() | 541 call test_garbagecollect_now() |
533 LET g:OpFuncArgs = [] | 542 LET g:TmpOpFunc1Args = [] |
534 call assert_fails('normal! g@l', 'E117:') | 543 call assert_fails('normal! g@l', 'E117:') |
535 call assert_equal([], g:OpFuncArgs) | 544 call assert_equal([], g:TmpOpFunc1Args) |
536 | 545 |
537 #" Try to use a function with two arguments for 'operatorfunc' | 546 #" Try to use a function with two arguments for 'operatorfunc' |
538 func MyopFunc2(x, y) | 547 func g:TmpOpFunc2(x, y) |
539 LET g:OpFuncArgs = [a:x, a:y] | 548 let g:TmpOpFunc2Args = [a:x, a:y] |
540 endfunc | 549 endfunc |
541 set opfunc=MyopFunc2 | 550 set opfunc=TmpOpFunc2 |
542 LET g:OpFuncArgs = [] | 551 LET g:TmpOpFunc2Args = [] |
543 call assert_fails('normal! g@l', 'E119:') | 552 call assert_fails('normal! g@l', 'E119:') |
544 call assert_equal([], g:OpFuncArgs) | 553 call assert_equal([], g:TmpOpFunc2Args) |
554 delfunc TmpOpFunc2 | |
545 | 555 |
546 #" Try to use a lambda function with two arguments for 'operatorfunc' | 556 #" Try to use a lambda function with two arguments for 'operatorfunc' |
547 LET &opfunc = LSTART a, b LMIDDLE MyopFunc(22, b) LEND | 557 LET &opfunc = LSTART a, b LMIDDLE OpFunc1(22, b) LEND |
548 LET g:OpFuncArgs = [] | 558 LET g:OpFunc1Args = [] |
549 call assert_fails('normal! g@l', 'E119:') | 559 call assert_fails('normal! g@l', 'E119:') |
550 call assert_equal([], g:OpFuncArgs) | 560 call assert_equal([], g:OpFunc1Args) |
551 | 561 |
552 #" Test for clearing the 'operatorfunc' option | 562 #" Test for clearing the 'operatorfunc' option |
553 set opfunc='' | 563 set opfunc='' |
554 set opfunc& | 564 set opfunc& |
555 call assert_fails("set opfunc=function('abc')", "E700:") | 565 call assert_fails("set opfunc=function('abc')", "E700:") |
556 call assert_fails("set opfunc=funcref('abc')", "E700:") | 566 call assert_fails("set opfunc=funcref('abc')", "E700:") |
557 | 567 |
558 #" set 'operatorfunc' to a non-existing function | 568 #" set 'operatorfunc' to a non-existing function |
559 LET &opfunc = function('g:MyopFunc', [23]) | 569 LET &opfunc = function('g:OpFunc1', [23]) |
560 call assert_fails("set opfunc=function('NonExistingFunc')", 'E700:') | 570 call assert_fails("set opfunc=function('NonExistingFunc')", 'E700:') |
561 call assert_fails("LET &opfunc = function('NonExistingFunc')", 'E700:') | 571 call assert_fails("LET &opfunc = function('NonExistingFunc')", 'E700:') |
562 LET g:OpFuncArgs = [] | 572 LET g:OpFunc1Args = [] |
563 normal! g@l | 573 normal! g@l |
564 call assert_equal([23, 'char'], g:OpFuncArgs) | 574 call assert_equal([23, 'char'], g:OpFunc1Args) |
565 END | 575 END |
566 call CheckTransLegacySuccess(lines) | 576 call CheckTransLegacySuccess(lines) |
567 | 577 |
568 " Using Vim9 lambda expression in legacy context should fail | 578 " Using Vim9 lambda expression in legacy context should fail |
569 set opfunc=(a)\ =>\ MyopFunc(24,\ a) | 579 set opfunc=(a)\ =>\ OpFunc1(24,\ a) |
570 let g:OpFuncArgs = [] | 580 let g:OpFunc1Args = [] |
571 call assert_fails('normal! g@l', 'E117:') | 581 call assert_fails('normal! g@l', 'E117:') |
572 call assert_equal([], g:OpFuncArgs) | 582 call assert_equal([], g:OpFunc1Args) |
573 | 583 |
574 " set 'operatorfunc' to a partial with dict. This used to cause a crash. | 584 " set 'operatorfunc' to a partial with dict. This used to cause a crash. |
575 func SetOpFunc() | 585 func SetOpFunc() |
576 let operator = {'execute': function('OperatorExecute')} | 586 let operator = {'execute': function('OperatorExecute')} |
577 let &opfunc = operator.execute | 587 let &opfunc = operator.execute |
588 let lines =<< trim END | 598 let lines =<< trim END |
589 vim9script | 599 vim9script |
590 | 600 |
591 # Test for using a def function with opfunc | 601 # Test for using a def function with opfunc |
592 def g:Vim9opFunc(val: number, type: string): void | 602 def g:Vim9opFunc(val: number, type: string): void |
593 g:OpFuncArgs = [val, type] | 603 g:OpFunc1Args = [val, type] |
594 enddef | 604 enddef |
595 set opfunc=function('g:Vim9opFunc',\ [60]) | 605 set opfunc=function('g:Vim9opFunc',\ [60]) |
596 g:OpFuncArgs = [] | 606 g:OpFunc1Args = [] |
597 normal! g@l | 607 normal! g@l |
598 assert_equal([60, 'char'], g:OpFuncArgs) | 608 assert_equal([60, 'char'], g:OpFunc1Args) |
599 END | 609 END |
600 call CheckScriptSuccess(lines) | 610 call CheckScriptSuccess(lines) |
601 | 611 |
602 " cleanup | 612 " cleanup |
603 set opfunc& | 613 set opfunc& |
604 delfunc MyopFunc | 614 delfunc OpFunc1 |
605 delfunc MyopFunc2 | 615 delfunc OpFunc2 |
606 unlet g:OpFuncArgs | 616 unlet g:OpFunc1Args g:OpFunc2Args |
607 %bw! | 617 %bw! |
608 endfunc | 618 endfunc |
609 | 619 |
610 func Test_normal10_expand() | 620 func Test_normal10_expand() |
611 " Test for expand() | 621 " Test for expand() |