@@ -461,6 +461,22 @@ func Test_list_mappings()
461461 call assert_equal ([' i <S-/> * ShiftSlash' ], execute (' imap' )- >trim ()- >split (" \n " ))
462462 iunmap <S-/>
463463 call assert_equal ([' No mapping found' ], execute (' imap' )- >trim ()- >split (" \n " ))
464+
465+ " List global, buffer local and script local mappings
466+ nmap ,f /^\k\+ (<CR>
467+ nmap <buffer> ,f /^\k\+ (<CR>
468+ nmap <script> ,fs /^\k\+ (<CR>
469+ call assert_equal ([' n ,f @/^\k\+ (<CR>' ,
470+ \ ' n ,fs & /^\k\+ (<CR>' ,
471+ \ ' n ,f /^\k\+ (<CR>' ],
472+ \ execute (' nmap ,f' )- >trim ()- >split (" \n " ))
473+
474+ " List <Nop> mapping
475+ nmap ,n <Nop>
476+ call assert_equal ([' n ,n <Nop>' ],
477+ \ execute (' nmap ,n' )- >trim ()- >split (" \n " ))
478+
479+ nmapclear
464480endfunc
465481
466482func Test_expr_map_restore_cursor ()
@@ -518,6 +534,15 @@ func Test_map_error()
518534
519535 call assert_fails (' mapclear abc' , ' E474:' )
520536 call assert_fails (' abclear abc' , ' E474:' )
537+ call assert_fails (' abbr $xyz abc' , ' E474:' )
538+
539+ " space character in an abbreviation
540+ call assert_fails (' abbr ab<space> ABC' , ' E474:' )
541+
542+ " invalid <expr> map
543+ map <expr> ,f abc
544+ call assert_fails (' normal ,f' , ' E121:' )
545+ unmap <expr> ,f
521546endfunc
522547
523548" Test for <special> key mapping
@@ -544,11 +569,66 @@ endfunc
544569" Test for hasmapto()
545570func Test_hasmapto ()
546571 call assert_equal (0 , hasmapto (' /^\k\+ (' ))
572+ map ,f /^\k\+ (<CR>
573+ call assert_equal (1 , hasmapto (' /^\k\+ (' ))
574+ unmap ,f
575+
576+ " Insert mode mapping
577+ call assert_equal (0 , hasmapto (' /^\k\+ (' , ' i' ))
578+ imap ,f /^\k\+ (<CR>
579+ call assert_equal (1 , hasmapto (' /^\k\+ (' , ' i' ))
580+ iunmap ,f
581+
582+ " Normal mode mapping
547583 call assert_equal (0 , hasmapto (' /^\k\+ (' , ' n' ))
548584 nmap ,f /^\k\+ (<CR>
549585 call assert_equal (1 , hasmapto (' /^\k\+ (' ))
550586 call assert_equal (1 , hasmapto (' /^\k\+ (' , ' n' ))
587+ nunmap ,f
588+
589+ " Visual and Select mode mapping
551590 call assert_equal (0 , hasmapto (' /^\k\+ (' , ' v' ))
591+ call assert_equal (0 , hasmapto (' /^\k\+ (' , ' x' ))
592+ call assert_equal (0 , hasmapto (' /^\k\+ (' , ' s' ))
593+ vmap ,f /^\k\+ (<CR>
594+ call assert_equal (1 , hasmapto (' /^\k\+ (' , ' v' ))
595+ call assert_equal (1 , hasmapto (' /^\k\+ (' , ' x' ))
596+ call assert_equal (1 , hasmapto (' /^\k\+ (' , ' s' ))
597+ vunmap ,f
598+
599+ " Visual mode mapping
600+ call assert_equal (0 , hasmapto (' /^\k\+ (' , ' x' ))
601+ xmap ,f /^\k\+ (<CR>
602+ call assert_equal (1 , hasmapto (' /^\k\+ (' , ' v' ))
603+ call assert_equal (1 , hasmapto (' /^\k\+ (' , ' x' ))
604+ call assert_equal (0 , hasmapto (' /^\k\+ (' , ' s' ))
605+ xunmap ,f
606+
607+ " Select mode mapping
608+ call assert_equal (0 , hasmapto (' /^\k\+ (' , ' s' ))
609+ smap ,f /^\k\+ (<CR>
610+ call assert_equal (1 , hasmapto (' /^\k\+ (' , ' v' ))
611+ call assert_equal (0 , hasmapto (' /^\k\+ (' , ' x' ))
612+ call assert_equal (1 , hasmapto (' /^\k\+ (' , ' s' ))
613+ sunmap ,f
614+
615+ " Operator-pending mode mapping
616+ call assert_equal (0 , hasmapto (' /^\k\+ (' , ' o' ))
617+ omap ,f /^\k\+ (<CR>
618+ call assert_equal (1 , hasmapto (' /^\k\+ (' , ' o' ))
619+ ounmap ,f
620+
621+ " Language mapping
622+ call assert_equal (0 , hasmapto (' /^\k\+ (' , ' l' ))
623+ lmap ,f /^\k\+ (<CR>
624+ call assert_equal (1 , hasmapto (' /^\k\+ (' , ' l' ))
625+ lunmap ,f
626+
627+ " Cmdline mode mapping
628+ call assert_equal (0 , hasmapto (' /^\k\+ (' , ' c' ))
629+ cmap ,f /^\k\+ (<CR>
630+ call assert_equal (1 , hasmapto (' /^\k\+ (' , ' c' ))
631+ cunmap ,f
552632
553633 call assert_equal (0 , hasmapto (' /^\k\+ (' , ' n' , 1 ))
554634endfunc
@@ -560,8 +640,176 @@ func Test_mapcomplete()
560640 \ getcompletion (' ' , ' mapping' ))
561641 call assert_equal ([], getcompletion (' ,d' , ' mapping' ))
562642
643+ call feedkeys (" :unmap <buf\<C-A>\<C-B> \" \<CR> " , ' tx' )
644+ call assert_equal (' "unmap <buffer>' , @: )
645+
646+ call feedkeys (" :unabbr <buf\<C-A>\<C-B> \" \<CR> " , ' tx' )
647+ call assert_equal (' "unabbr <buffer>' , @: )
648+
563649 call feedkeys (" :abbr! \<C-A>\<C-B> \" \<CR> " , ' tx' )
564- call assert_match (" abbr! \x01 " , @: )
650+ call assert_equal (" \" abbr! \x01 " , @: )
651+
652+ " Multiple matches for a map
653+ nmap ,f /H<CR>
654+ omap ,f /H<CR>
655+ call feedkeys (" :map ,\<C-A>\<C-B> \" \<CR> " , ' tx' )
656+ call assert_equal (' "map ,f' , @: )
657+ mapclear
658+ endfunc
659+
660+ " Test for <expr> in abbreviation
661+ func Test_expr_abbr ()
662+ new
663+ iabbr <expr> teh " the"
664+ call feedkeys (" iteh " , " tx" )
665+ call assert_equal (' the ' , getline (1 ))
666+ iabclear
667+ call setline (1 , ' ' )
668+
669+ " invalid <expr> abbreviation
670+ abbr <expr> hte GetAbbr ()
671+ call assert_fails (' normal ihte ' , ' E117:' )
672+ call assert_equal (' ' , getline (1 ))
673+ unabbr <expr> hte
674+
675+ close !
676+ endfunc
677+
678+ " Test for storing mappings in different modes in a vimrc file
679+ func Test_mkvimrc_mapmodes ()
680+ map a1 /a1
681+ nmap a2 /a2
682+ vmap a3 /a3
683+ smap a4 /a4
684+ xmap a5 /a5
685+ omap a6 /a6
686+ map! a7 /a7
687+ imap a8 /a8
688+ lmap a9 /a9
689+ cmap a10 /a10
690+ tmap a11 /a11
691+ " Normal + Visual map
692+ map a12 /a12
693+ sunmap a12
694+ ounmap a12
695+ " Normal + Selectmode map
696+ map a13 /a13
697+ xunmap a13
698+ ounmap a13
699+ " Normal + OpPending map
700+ map a14 /a14
701+ vunmap a14
702+ " Visual + Selectmode map
703+ map a15 /a15
704+ nunmap a15
705+ ounmap a15
706+ " Visual + OpPending map
707+ map a16 /a16
708+ nunmap a16
709+ sunmap a16
710+ " Selectmode + OpPending map
711+ map a17 /a17
712+ nunmap a17
713+ xunmap a17
714+ " Normal + Visual + Selectmode map
715+ map a18 /a18
716+ ounmap a18
717+ " Normal + Visual + OpPending map
718+ map a19 /a19
719+ sunmap a19
720+ " Normal + Selectmode + OpPending map
721+ map a20 /a20
722+ xunmap a20
723+ " Visual + Selectmode + OpPending map
724+ map a21 /a21
725+ nunmap a21
726+ " Mapping to Nop
727+ map a22 <Nop>
728+ " Script local mapping
729+ map <script> a23 /a23
730+
731+ " Newline in {lhs} and {rhs} of a map
732+ exe " map a24\<C-V>\<C-J> ia24\<C-V>\<C-J> <Esc>"
733+
734+ " Abbreviation
735+ abbr a25 A25
736+ cabbr a26 A26
737+ iabbr a27 A27
738+
739+ mkvimrc ! Xvimrc
740+ let l = readfile (' Xvimrc' )
741+ call assert_equal ([' map a1 /a1' ], filter (copy (l ), ' v:val =~ " a1 "' ))
742+ call assert_equal ([' nmap a2 /a2' ], filter (copy (l ), ' v:val =~ " a2 "' ))
743+ call assert_equal ([' vmap a3 /a3' ], filter (copy (l ), ' v:val =~ " a3 "' ))
744+ call assert_equal ([' smap a4 /a4' ], filter (copy (l ), ' v:val =~ " a4 "' ))
745+ call assert_equal ([' xmap a5 /a5' ], filter (copy (l ), ' v:val =~ " a5 "' ))
746+ call assert_equal ([' omap a6 /a6' ], filter (copy (l ), ' v:val =~ " a6 "' ))
747+ call assert_equal ([' map! a7 /a7' ], filter (copy (l ), ' v:val =~ " a7 "' ))
748+ call assert_equal ([' imap a8 /a8' ], filter (copy (l ), ' v:val =~ " a8 "' ))
749+ call assert_equal ([' lmap a9 /a9' ], filter (copy (l ), ' v:val =~ " a9 "' ))
750+ call assert_equal ([' cmap a10 /a10' ], filter (copy (l ), ' v:val =~ " a10 "' ))
751+ call assert_equal ([' tmap a11 /a11' ], filter (copy (l ), ' v:val =~ " a11 "' ))
752+ call assert_equal ([' nmap a12 /a12' , ' xmap a12 /a12' ],
753+ \ filter (copy (l ), ' v:val =~ " a12 "' ))
754+ call assert_equal ([' nmap a13 /a13' , ' smap a13 /a13' ],
755+ \ filter (copy (l ), ' v:val =~ " a13 "' ))
756+ call assert_equal ([' nmap a14 /a14' , ' omap a14 /a14' ],
757+ \ filter (copy (l ), ' v:val =~ " a14 "' ))
758+ call assert_equal ([' vmap a15 /a15' ], filter (copy (l ), ' v:val =~ " a15 "' ))
759+ call assert_equal ([' xmap a16 /a16' , ' omap a16 /a16' ],
760+ \ filter (copy (l ), ' v:val =~ " a16 "' ))
761+ call assert_equal ([' smap a17 /a17' , ' omap a17 /a17' ],
762+ \ filter (copy (l ), ' v:val =~ " a17 "' ))
763+ call assert_equal ([' nmap a18 /a18' , ' vmap a18 /a18' ],
764+ \ filter (copy (l ), ' v:val =~ " a18 "' ))
765+ call assert_equal ([' nmap a19 /a19' , ' xmap a19 /a19' , ' omap a19 /a19' ],
766+ \ filter (copy (l ), ' v:val =~ " a19 "' ))
767+ call assert_equal ([' nmap a20 /a20' , ' smap a20 /a20' , ' omap a20 /a20' ],
768+ \ filter (copy (l ), ' v:val =~ " a20 "' ))
769+ call assert_equal ([' vmap a21 /a21' , ' omap a21 /a21' ],
770+ \ filter (copy (l ), ' v:val =~ " a21 "' ))
771+ call assert_equal ([' map a22 <Nop>' ], filter (copy (l ), ' v:val =~ " a22 "' ))
772+ call assert_equal ([], filter (copy (l ), ' v:val =~ " a23 "' ))
773+ call assert_equal ([" map a24<NL> ia24<NL>\x16 \e " ],
774+ \ filter (copy (l ), ' v:val =~ " a24"' ))
775+
776+ call assert_equal ([' abbr a25 A25' ], filter (copy (l ), ' v:val =~ " a25 "' ))
777+ call assert_equal ([' cabbr a26 A26' ], filter (copy (l ), ' v:val =~ " a26 "' ))
778+ call assert_equal ([' iabbr a27 A27' ], filter (copy (l ), ' v:val =~ " a27 "' ))
779+ call delete (' Xvimrc' )
780+
781+ mapclear
782+ nmapclear
783+ vmapclear
784+ xmapclear
785+ smapclear
786+ omapclear
787+ imapclear
788+ lmapclear
789+ cmapclear
790+ tmapclear
791+ endfunc
792+
793+ " Test for recursive mapping ('maxmapdepth')
794+ func Test_map_recursive ()
795+ map x y
796+ map y x
797+ call assert_fails (' normal x' , ' E223:' )
798+ unmap x
799+ unmap y
800+ endfunc
801+
802+ " Test for removing an abbreviation using {rhs} and with space after {lhs}
803+ func Test_abbr_remove ()
804+ abbr foo bar
805+ let d = maparg (' foo' , ' i' , 1 , 1 )
806+ call assert_equal ([' foo' , ' bar' , ' !' ], [d .lhs, d .rhs, d .mode ])
807+ unabbr bar
808+ call assert_equal ({}, maparg (' foo' , ' i' , 1 , 1 ))
809+
810+ abbr foo bar
811+ unabbr foo<space><tab>
812+ call assert_equal ({}, maparg (' foo' , ' i' , 1 , 1 ))
565813endfunc
566814
567815" vim: shiftwidth = 2 sts = 2 expandtab
0 commit comments