diff src/testdir/test_maparg.vim @ 28592:d3c966c0cdf7 v8.2.4820

patch 8.2.4820: not simple programmatic way to find a specific mapping Commit: https://github.com/vim/vim/commit/659c240cf769925ff432b88df8719e7ace4629b0 Author: Ernie Rael <errael@raelity.com> Date: Sun Apr 24 18:40:28 2022 +0100 patch 8.2.4820: not simple programmatic way to find a specific mapping Problem: Not simple programmatic way to find a specific mapping. Solution: Add getmappings(). (Ernie Rael, closes https://github.com/vim/vim/issues/10273)
author Bram Moolenaar <Bram@vim.org>
date Sun, 24 Apr 2022 19:45:03 +0200
parents ea2b4cb4515b
children
line wrap: on
line diff
--- a/src/testdir/test_maparg.vim
+++ b/src/testdir/test_maparg.vim
@@ -297,4 +297,76 @@ func Test_map_restore()
 
 endfunc
 
+def Test_getmappings()
+  new
+  def ClearMaps()
+    mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear
+    mapclear!  | imapclear | lmapclear | cmapclear | tmapclear
+    mapclear <buffer> | nmapclear <buffer> | vmapclear <buffer>
+    xmapclear <buffer> | smapclear <buffer> | omapclear <buffer>
+    mapclear! <buffer> | imapclear <buffer> | lmapclear <buffer>
+    cmapclear <buffer> | tmapclear <buffer>
+  enddef
+
+  def AddMaps(new: list<string>, accum: list<string>)
+    if len(new) > 0 && new[0] != "No mapping found"
+      accum->extend(new)
+    endif
+  enddef
+
+  ClearMaps()
+  assert_equal(0, len(getmappings()))
+
+  # Set up some mappings.
+  map dup bar
+  map <buffer> dup bufbar
+  map foo<C-V> is<F4>foo
+  vnoremap <script> <buffer> <expr> <silent> bar isbar
+  tmap baz foo
+  omap h w
+  lmap i w
+  nmap j w
+  xmap k w
+  smap l w
+  map abc <Nop>
+  nmap <M-j> x
+  nmap <M-Space> y
+
+  # Get a list of the mappings with the ':map' commands.
+  # Check getmappings() return a list of the same size.
+  assert_equal(13, len(getmappings()))
+
+  # collect all the current maps using :map commands
+  var maps_command: list<string>
+  AddMaps(split(execute('map'), '\n'), maps_command)
+  AddMaps(split(execute('map!'), '\n'), maps_command)
+  AddMaps(split(execute('tmap'), '\n'), maps_command)
+  AddMaps(split(execute('lmap'), '\n'), maps_command)
+
+  # Use getmappings to get all the maps
+  var maps_getmappings = getmappings()
+  assert_equal(len(maps_command), len(maps_getmappings))
+
+  # make sure all the mode-lhs are unique, no duplicates
+  var map_set: dict<number>
+  for d in maps_getmappings
+    map_set[d.mode .. "-" .. d.lhs .. "-" .. d.buffer] = 0
+  endfor
+  assert_equal(len(maps_getmappings), len(map_set))
+
+  # For everything returned by getmappings, should be the same as from maparg.
+  # Except for "map dup", bacause maparg returns the <buffer> version
+  for d in maps_getmappings
+    if d.lhs == 'dup' && d.buffer == 0
+      continue
+    endif
+    var d_maparg = maparg(d.lhs, d.mode, false, true)
+    assert_equal(d_maparg, d)
+  endfor
+
+  ClearMaps()
+  assert_equal(0, len(getmappings()))
+enddef
+
+
 " vim: shiftwidth=2 sts=2 expandtab