test: more tests for nvim_{set,del}_keymap with abbreviation (#23970)

This commit is contained in:
zeertzjq 2023-06-10 10:44:31 +08:00 committed by GitHub
parent 7154f0c986
commit b6d2f49b45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 10 deletions

View File

@ -1487,7 +1487,7 @@ nvim_set_keymap({mode}, {lhs}, {rhs}, {*opts}) *nvim_set_keymap()*
Parameters: ~
• {mode} Mode short-name (map command prefix: "n", "i", "v", "x", …) or
"!" for |:map!|, or empty string for |:map|. "ia", "ca" or
"!a" for abbreviation in insert mode, cmdline mode, or both,
"!a" for abbreviation in Insert mode, Cmdline mode, or both,
respectively
• {lhs} Left-hand-side |{lhs}| of the mapping.
• {rhs} Right-hand-side |{rhs}| of the mapping.

View File

@ -82,7 +82,7 @@ The following new APIs or features were added.
• |vim.system()| for running system commands.
• |nvim_set_keymap()| now supports abbreviations.
• |nvim_set_keymap()| and |nvim_del_keymap()| now support abbreviations.
==============================================================================
CHANGED FEATURES *news-changed*

View File

@ -1420,7 +1420,7 @@ ArrayOf(Dictionary) nvim_get_keymap(String mode)
/// @param channel_id
/// @param mode Mode short-name (map command prefix: "n", "i", "v", "x", …)
/// or "!" for |:map!|, or empty string for |:map|.
/// "ia", "ca" or "!a" for abbreviation in insert mode, cmdline mode, or both, respectively
/// "ia", "ca" or "!a" for abbreviation in Insert mode, Cmdline mode, or both, respectively
/// @param lhs Left-hand-side |{lhs}| of the mapping.
/// @param rhs Right-hand-side |{rhs}| of the mapping.
/// @param opts Optional parameters map: Accepts all |:map-arguments| as keys except |<buffer>|,

View File

@ -400,6 +400,9 @@ describe('nvim_set_keymap, nvim_del_keymap', function()
-- maparg(), which does not accept "!" (though it returns "!" in its output
-- if getting a mapping set with |:map!|).
local function normalize_mapmode(mode, generate_expected)
if mode:sub(-1) == 'a' then
mode = mode:sub(1, -2)
end
if not generate_expected and mode == '!' then
-- Cannot retrieve mapmode-ic mappings with "!", but can with "i" or "c".
mode = 'i'
@ -435,7 +438,7 @@ describe('nvim_set_keymap, nvim_del_keymap', function()
-- Gets a maparg() dict from Nvim, if one exists.
local function get_mapargs(mode, lhs)
local mapargs = funcs.maparg(lhs, normalize_mapmode(mode), false, true)
local mapargs = funcs.maparg(lhs, normalize_mapmode(mode), mode:sub(-1) == 'a', true)
-- drop "lhsraw" and "lhsrawalt" which are hard to check
mapargs.lhsraw = nil
mapargs.lhsrawalt = nil
@ -744,7 +747,7 @@ describe('nvim_set_keymap, nvim_del_keymap', function()
end)
-- Perform exhaustive tests of basic functionality
local mapmodes = {'n', 'v', 'x', 's', 'o', '!', 'i', 'l', 'c', 't', ''}
local mapmodes = {'n', 'v', 'x', 's', 'o', '!', 'i', 'l', 'c', 't', '', 'ia', 'ca', '!a'}
for _, mapmode in ipairs(mapmodes) do
it('can set/unset normal mappings in mapmode '..mapmode, function()
meths.set_keymap(mapmode, 'lhs', 'rhs', {})
@ -773,11 +776,9 @@ describe('nvim_set_keymap, nvim_del_keymap', function()
-- remove some map arguments that are harder to test, or were already tested
optnames = {'nowait', 'silent', 'expr', 'noremap'}
for _, mapmode in ipairs(mapmodes) do
local printable_mode = normalize_mapmode(mapmode)
-- Test with single mappings
for _, maparg in ipairs(optnames) do
it('can set/unset '..printable_mode..'-mappings with maparg: '..maparg,
it('can set/unset '..mapmode..'-mappings with maparg: '..maparg,
function()
meths.set_keymap(mapmode, 'lhs', 'rhs', {[maparg] = true})
eq(generate_mapargs(mapmode, 'lhs', 'rhs', {[maparg] = true}),
@ -785,7 +786,7 @@ describe('nvim_set_keymap, nvim_del_keymap', function()
meths.del_keymap(mapmode, 'lhs')
eq({}, get_mapargs(mapmode, 'lhs'))
end)
it ('can set/unset '..printable_mode..'-mode mappings with maparg '..
it ('can set/unset '..mapmode..'-mode mappings with maparg '..
maparg..', whose value is false', function()
meths.set_keymap(mapmode, 'lhs', 'rhs', {[maparg] = false})
eq(generate_mapargs(mapmode, 'lhs', 'rhs'),
@ -798,7 +799,7 @@ describe('nvim_set_keymap, nvim_del_keymap', function()
-- Test with triplets of mappings, one of which is false
for i = 1, (#optnames - 2) do
local opt1, opt2, opt3 = optnames[i], optnames[i + 1], optnames[i + 2]
it('can set/unset '..printable_mode..'-mode mappings with mapargs '..
it('can set/unset '..mapmode..'-mode mappings with mapargs '..
opt1..', '..opt2..', '..opt3, function()
local opts = {[opt1] = true, [opt2] = false, [opt3] = true}
meths.set_keymap(mapmode, 'lhs', 'rhs', opts)