diff src/testdir/test_map_functions.vim @ 28674:38f7a132bba3 v8.2.4861

patch 8.2.4861: it is not easy to restore saved mappings Commit: https://github.com/vim/vim/commit/51d04d16f21e19d6eded98f9530d84089102f925 Author: Ernie Rael <errael@raelity.com> Date: Wed May 4 15:40:22 2022 +0100 patch 8.2.4861: it is not easy to restore saved mappings Problem: It is not easy to restore saved mappings. Solution: Make mapset() accept a dict argument. (Ernie Rael, closes https://github.com/vim/vim/issues/10295)
author Bram Moolenaar <Bram@vim.org>
date Wed, 04 May 2022 16:45:03 +0200
parents 9ae2c32841fb
children fea88e555652
line wrap: on
line diff
--- a/src/testdir/test_map_functions.vim
+++ b/src/testdir/test_map_functions.vim
@@ -19,13 +19,13 @@ func Test_maparg()
         \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
         \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
         \ 'lnum': lnum + 1, 
-	\ 'rhs': 'is<F4>foo', 'buffer': 0},
+	\ 'rhs': 'is<F4>foo', 'buffer': 0, 'abbr': 0},
 	\ maparg('foo<C-V>', '', 0, 1))
   call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar',
         \ 'lhsraw': 'bar', 'mode': 'v',
         \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1,
         \ 'lnum': lnum + 2,
-	\ 'rhs': 'isbar', 'buffer': 1},
+	\ 'rhs': 'isbar', 'buffer': 1, 'abbr': 0},
         \ 'bar'->maparg('', 0, 1))
   let lnum = expand('<sflnum>')
   map <buffer> <nowait> foo bar
@@ -33,7 +33,7 @@ func Test_maparg()
         \ 'lhsraw': 'foo', 'mode': ' ',
         \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1,
         \ 'lnum': lnum + 1, 'rhs': 'bar',
-	\ 'buffer': 1},
+	\ 'buffer': 1, 'abbr': 0},
         \ maparg('foo', '', 0, 1))
   let lnum = expand('<sflnum>')
   tmap baz foo
@@ -41,8 +41,17 @@ func Test_maparg()
         \ 'lhsraw': 'baz', 'mode': 't',
         \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
         \ 'lnum': lnum + 1, 'rhs': 'foo',
-	\ 'buffer': 0},
+	\ 'buffer': 0, 'abbr': 0},
         \ maparg('baz', 't', 0, 1))
+  let lnum = expand('<sflnum>')
+  iab A B
+  call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'A',
+        \ 'lhsraw': 'A', 'mode': 'i',
+        \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
+        \ 'lnum': lnum + 1, 'rhs': 'B',
+	\ 'buffer': 0, 'abbr': 1},
+        \ maparg('A', 'i', 1, 1))
+  iuna A
 
   map abc x<char-114>x
   call assert_equal("xrx", maparg('abc'))
@@ -250,10 +259,156 @@ func Test_mapset()
   bwipe!
 
   call assert_fails('call mapset([], v:false, {})', 'E730:')
-  call assert_fails('call mapset("i", 0, "")', 'E716:')
+  call assert_fails('call mapset("i", 0, "")', 'E715:')
   call assert_fails('call mapset("i", 0, {})', 'E460:')
 endfunc
 
+def Test_mapset_arg1_dir()
+  # This test is mostly about get_map_mode_string.
+  # Once the code gets past that, it's common with the 3 arg mapset.
+
+  # GetModes() return list of modes for 'XZ' lhs using maplist.
+  # There is one list item per mapping
+  def GetModes(abbr: bool = false): list<string>
+    return maplist(abbr)->filter((_, m) => m.lhs == 'XZ')
+                ->mapnew((_, m) => m.mode)
+  enddef
+
+  const unmap_cmds = [ 'unmap', 'unmap!', 'tunmap', 'lunmap' ]
+  def UnmapAll(lhs: string)
+    for cmd in unmap_cmds
+      try | execute(cmd .. ' ' .. lhs) | catch /E31/ | endtry
+    endfor
+  enddef
+
+  var tmap: dict<any>
+
+  # some mapset(mode, abbr, dict) tests using get_map_mode_str
+  map XZ x
+  tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy()
+  # this splits the mapping into 2 mappings
+  mapset('ox', false, tmap)
+  assert_equal(2, len(GetModes()))
+  mapset('o', false, tmap)
+  assert_equal(3, len(GetModes()))
+  # test that '' acts like ' ', and that the 3 mappings become 1
+  mapset('', false, tmap)
+  assert_equal([' '], GetModes())
+  # dict's mode/abbr are ignored
+  UnmapAll('XZ')
+  tmap.mode = '!'
+  tmap.abbr = true
+  mapset('o', false, tmap)
+  assert_equal(['o'], GetModes())
+
+  # test the 3 arg version handles bad mode string, dict not used
+  assert_fails("mapset('vi', false, {})", 'E1276:')
+
+
+  # get the abbreviations out of the way
+  abbreviate XZ ZX
+  tmap = maplist(true)->filter((_, m) => m.lhs == 'XZ')[0]->copy()
+
+  abclear
+  # 'ic' is the default ab command, shows up as '!'
+  tmap.mode = 'ic'
+  mapset(tmap)
+  assert_equal(['!'], GetModes(true))
+
+  abclear
+  tmap.mode = 'i'
+  mapset(tmap)
+  assert_equal(['i'], GetModes(true))
+
+  abclear
+  tmap.mode = 'c'
+  mapset(tmap)
+  assert_equal(['c'], GetModes(true))
+
+  abclear
+  tmap.mode = '!'
+  mapset(tmap)
+  assert_equal(['!'], GetModes(true))
+
+  assert_fails("mapset({mode: ' !', abbr: 1})", 'E1276:')
+  assert_fails("mapset({mode: 'cl', abbr: 1})", 'E1276:')
+  assert_fails("mapset({mode: 'in', abbr: 1})", 'E1276:')
+
+  # the map commands
+  map XZ x
+  tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy()
+
+  # try the combos
+  UnmapAll('XZ')
+  # 'nxso' is ' ', the unadorned :map
+  tmap.mode = 'nxso'
+  mapset(tmap)
+  assert_equal([' '], GetModes())
+
+  UnmapAll('XZ')
+  # 'ic' is '!'
+  tmap.mode = 'ic'
+  mapset(tmap)
+  assert_equal(['!'], GetModes())
+
+  UnmapAll('XZ')
+  # 'xs' is really 'v'
+  tmap.mode = 'xs'
+  mapset(tmap)
+  assert_equal(['v'], GetModes())
+
+  # try the individual modes
+  UnmapAll('XZ')
+  tmap.mode = 'n'
+  mapset(tmap)
+  assert_equal(['n'], GetModes())
+
+  UnmapAll('XZ')
+  tmap.mode = 'x'
+  mapset(tmap)
+  assert_equal(['x'], GetModes())
+
+  UnmapAll('XZ')
+  tmap.mode = 's'
+  mapset(tmap)
+  assert_equal(['s'], GetModes())
+
+  UnmapAll('XZ')
+  tmap.mode = 'o'
+  mapset(tmap)
+  assert_equal(['o'], GetModes())
+
+  UnmapAll('XZ')
+  tmap.mode = 'i'
+  mapset(tmap)
+  assert_equal(['i'], GetModes())
+
+  UnmapAll('XZ')
+  tmap.mode = 'c'
+  mapset(tmap)
+  assert_equal(['c'], GetModes())
+
+  UnmapAll('XZ')
+  tmap.mode = 't'
+  mapset(tmap)
+  assert_equal(['t'], GetModes())
+
+  UnmapAll('XZ')
+  tmap.mode = 'l'
+  mapset(tmap)
+  assert_equal(['l'], GetModes())
+
+  UnmapAll('XZ')
+
+  # get errors for modes that can't be in one mapping
+  assert_fails("mapset({mode: 'nxsoi', abbr: 0})", 'E1276:')
+  assert_fails("mapset({mode: ' !', abbr: 0})", 'E1276:')
+  assert_fails("mapset({mode: 'ix', abbr: 0})", 'E1276:')
+  assert_fails("mapset({mode: 'tl', abbr: 0})", 'E1276:')
+  assert_fails("mapset({mode: ' l', abbr: 0})", 'E1276:')
+  assert_fails("mapset({mode: ' t', abbr: 0})", 'E1276:')
+enddef
+
 func Check_ctrlb_map(d, check_alt)
   call assert_equal('<C-B>', a:d.lhs)
   if a:check_alt