blob: 21c899048a1988a7cce85fac180551646b1ecb86 [file] [log] [blame]
Ernie Rael09661202022-04-25 14:40:44 +01001" Tests for maparg(), mapcheck(), mapset(), maplist()
Bram Moolenaar292eff02017-07-11 21:46:28 +02002" Also test utf8 map with a 0x80 byte.
Bram Moolenaar292eff02017-07-11 21:46:28 +02003
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02004func s:SID()
Bram Moolenaar292eff02017-07-11 21:46:28 +02005 return str2nr(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$'))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02006endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +02007
Bram Moolenaarc94c1462020-05-22 20:01:06 +02008func Test_maparg()
Bram Moolenaar292eff02017-07-11 21:46:28 +02009 new
10 set cpo-=<
11 set encoding=utf8
12 " Test maparg() with a string result
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020013 let sid = s:SID()
14 let lnum = expand('<sflnum>')
Bram Moolenaar292eff02017-07-11 21:46:28 +020015 map foo<C-V> is<F4>foo
16 vnoremap <script> <buffer> <expr> <silent> bar isbar
Bram Moolenaar292eff02017-07-11 21:46:28 +020017 call assert_equal("is<F4>foo", maparg('foo<C-V>'))
Bram Moolenaar2da0f0c2020-04-01 19:22:12 +020018 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo<C-V>',
Bram Moolenaar9c652532020-05-24 13:10:18 +020019 \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
Bram Moolenaara9528b32022-01-18 20:51:35 +000020 \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
21 \ 'lnum': lnum + 1,
Ernie Rael51d04d12022-05-04 15:40:22 +010022 \ 'rhs': 'is<F4>foo', 'buffer': 0, 'abbr': 0},
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020023 \ maparg('foo<C-V>', '', 0, 1))
Bram Moolenaar9c652532020-05-24 13:10:18 +020024 call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar',
25 \ 'lhsraw': 'bar', 'mode': 'v',
Bram Moolenaara9528b32022-01-18 20:51:35 +000026 \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1,
27 \ 'lnum': lnum + 2,
Ernie Rael51d04d12022-05-04 15:40:22 +010028 \ 'rhs': 'isbar', 'buffer': 1, 'abbr': 0},
Bram Moolenaara1449832019-09-01 20:16:52 +020029 \ 'bar'->maparg('', 0, 1))
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020030 let lnum = expand('<sflnum>')
Bram Moolenaar292eff02017-07-11 21:46:28 +020031 map <buffer> <nowait> foo bar
Bram Moolenaar9c652532020-05-24 13:10:18 +020032 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo',
33 \ 'lhsraw': 'foo', 'mode': ' ',
Bram Moolenaara9528b32022-01-18 20:51:35 +000034 \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1,
35 \ 'lnum': lnum + 1, 'rhs': 'bar',
Ernie Rael51d04d12022-05-04 15:40:22 +010036 \ 'buffer': 1, 'abbr': 0},
Bram Moolenaar292eff02017-07-11 21:46:28 +020037 \ maparg('foo', '', 0, 1))
Bram Moolenaar14371ed2019-07-27 21:05:21 +020038 let lnum = expand('<sflnum>')
39 tmap baz foo
Bram Moolenaar9c652532020-05-24 13:10:18 +020040 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz',
41 \ 'lhsraw': 'baz', 'mode': 't',
Bram Moolenaara9528b32022-01-18 20:51:35 +000042 \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
43 \ 'lnum': lnum + 1, 'rhs': 'foo',
Ernie Rael51d04d12022-05-04 15:40:22 +010044 \ 'buffer': 0, 'abbr': 0},
Bram Moolenaar14371ed2019-07-27 21:05:21 +020045 \ maparg('baz', 't', 0, 1))
Ernie Rael51d04d12022-05-04 15:40:22 +010046 let lnum = expand('<sflnum>')
47 iab A B
48 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'A',
49 \ 'lhsraw': 'A', 'mode': 'i',
50 \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
51 \ 'lnum': lnum + 1, 'rhs': 'B',
52 \ 'buffer': 0, 'abbr': 1},
53 \ maparg('A', 'i', 1, 1))
54 iuna A
Bram Moolenaar292eff02017-07-11 21:46:28 +020055
56 map abc x<char-114>x
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020057 call assert_equal("xrx", maparg('abc'))
Bram Moolenaar292eff02017-07-11 21:46:28 +020058 map abc y<S-char-114>y
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020059 call assert_equal("yRy", maparg('abc'))
60
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +010061 omap { w
62 let d = maparg('{', 'o', 0, 1)
63 call assert_equal(['{', 'w', 'o'], [d.lhs, d.rhs, d.mode])
64 ounmap {
65
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +010066 lmap { w
67 let d = maparg('{', 'l', 0, 1)
68 call assert_equal(['{', 'w', 'l'], [d.lhs, d.rhs, d.mode])
69 lunmap {
70
71 nmap { w
72 let d = maparg('{', 'n', 0, 1)
73 call assert_equal(['{', 'w', 'n'], [d.lhs, d.rhs, d.mode])
74 nunmap {
75
76 xmap { w
77 let d = maparg('{', 'x', 0, 1)
78 call assert_equal(['{', 'w', 'x'], [d.lhs, d.rhs, d.mode])
79 xunmap {
80
81 smap { w
82 let d = maparg('{', 's', 0, 1)
83 call assert_equal(['{', 'w', 's'], [d.lhs, d.rhs, d.mode])
84 sunmap {
85
zeertzjq2c8a7eb2022-04-26 21:36:21 +010086 map <C-I> foo
87 unmap <Tab>
88 " This used to cause a segfault
89 call maparg('<C-I>', '', 0, 1)
90 unmap <C-I>
91
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020092 map abc <Nop>
93 call assert_equal("<Nop>", maparg('abc'))
94 unmap abc
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +010095
96 call feedkeys(":abbr esc \<C-V>\<C-V>\<C-V>\<C-V>\<C-V>\<Esc>\<CR>", "xt")
97 let d = maparg('esc', 'i', 1, 1)
98 call assert_equal(['esc', "\<C-V>\<C-V>\<Esc>", '!'], [d.lhs, d.rhs, d.mode])
99 abclear
zeertzjq2c8a7eb2022-04-26 21:36:21 +0100100 unlet d
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200101endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +0200102
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200103def Test_vim9_maparg()
104 nmap { w
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200105 var one: string = maparg('{')
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200106 assert_equal('w', one)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200107 var two: string = maparg('{', 'n')
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200108 assert_equal('w', two)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200109 var three: string = maparg('{', 'n', 0)
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200110 assert_equal('w', three)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200111 var four: dict<any> = maparg('{', 'n', 0, 1)
Bram Moolenaard2c61702020-09-06 15:58:36 +0200112 assert_equal(['{', 'w', 'n'], [four.lhs, four.rhs, four.mode])
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200113 nunmap {
114enddef
115
Bram Moolenaara1449832019-09-01 20:16:52 +0200116func Test_mapcheck()
117 call assert_equal('', mapcheck('a'))
118 call assert_equal('', mapcheck('abc'))
119 call assert_equal('', mapcheck('ax'))
120 call assert_equal('', mapcheck('b'))
121
122 map a something
123 call assert_equal('something', mapcheck('a'))
124 call assert_equal('something', mapcheck('a', 'n'))
125 call assert_equal('', mapcheck('a', 'c'))
126 call assert_equal('', mapcheck('a', 'i'))
127 call assert_equal('something', 'abc'->mapcheck())
128 call assert_equal('something', 'ax'->mapcheck())
129 call assert_equal('', mapcheck('b'))
130 unmap a
131
132 map ab foobar
133 call assert_equal('foobar', mapcheck('a'))
134 call assert_equal('foobar', mapcheck('abc'))
135 call assert_equal('', mapcheck('ax'))
136 call assert_equal('', mapcheck('b'))
137 unmap ab
138
139 map abc barfoo
140 call assert_equal('barfoo', mapcheck('a'))
141 call assert_equal('barfoo', mapcheck('a', 'n', 0))
142 call assert_equal('', mapcheck('a', 'n', 1))
143 call assert_equal('barfoo', mapcheck('abc'))
144 call assert_equal('', mapcheck('ax'))
145 call assert_equal('', mapcheck('b'))
146 unmap abc
147
148 abbr ab abbrev
149 call assert_equal('abbrev', mapcheck('a', 'i', 1))
150 call assert_equal('', mapcheck('a', 'n', 1))
151 call assert_equal('', mapcheck('a', 'i', 0))
152 unabbr ab
153endfunc
154
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200155func Test_range_map()
Bram Moolenaar292eff02017-07-11 21:46:28 +0200156 new
157 " Outside of the range, minimum
158 inoremap <Char-0x1040> a
159 execute "normal a\u1040\<Esc>"
160 " Inside of the range, minimum
161 inoremap <Char-0x103f> b
162 execute "normal a\u103f\<Esc>"
163 " Inside of the range, maximum
164 inoremap <Char-0xf03f> c
165 execute "normal a\uf03f\<Esc>"
166 " Outside of the range, maximum
167 inoremap <Char-0xf040> d
168 execute "normal a\uf040\<Esc>"
169 call assert_equal("abcd", getline(1))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200170endfunc
171
172func One_mapset_test(keys)
173 exe 'nnoremap ' .. a:keys .. ' original<CR>'
174 let orig = maparg(a:keys, 'n', 0, 1)
175 call assert_equal(a:keys, orig.lhs)
176 call assert_equal('original<CR>', orig.rhs)
177 call assert_equal('n', orig.mode)
178
179 exe 'nunmap ' .. a:keys
180 let d = maparg(a:keys, 'n', 0, 1)
181 call assert_equal({}, d)
182
183 call mapset('n', 0, orig)
184 let d = maparg(a:keys, 'n', 0, 1)
185 call assert_equal(a:keys, d.lhs)
186 call assert_equal('original<CR>', d.rhs)
187 call assert_equal('n', d.mode)
188
189 exe 'nunmap ' .. a:keys
190endfunc
191
192func Test_mapset()
193 call One_mapset_test('K')
194 call One_mapset_test('<F3>')
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200195
196 " Check <> key conversion
197 new
198 inoremap K one<Left>x
199 call feedkeys("iK\<Esc>", 'xt')
200 call assert_equal('onxe', getline(1))
201
202 let orig = maparg('K', 'i', 0, 1)
203 call assert_equal('K', orig.lhs)
204 call assert_equal('one<Left>x', orig.rhs)
205 call assert_equal('i', orig.mode)
206
207 iunmap K
208 let d = maparg('K', 'i', 0, 1)
209 call assert_equal({}, d)
210
211 call mapset('i', 0, orig)
212 call feedkeys("SK\<Esc>", 'xt')
213 call assert_equal('onxe', getline(1))
214
215 iunmap K
216
217 " Test literal <CR> using a backslash
218 let cpo_save = &cpo
219 set cpo-=B
220 inoremap K one\<CR>two
221 call feedkeys("SK\<Esc>", 'xt')
222 call assert_equal('one<CR>two', getline(1))
223
224 let orig = maparg('K', 'i', 0, 1)
225 call assert_equal('K', orig.lhs)
226 call assert_equal('one\<CR>two', orig.rhs)
227 call assert_equal('i', orig.mode)
228
229 iunmap K
230 let d = maparg('K', 'i', 0, 1)
231 call assert_equal({}, d)
232
233 call mapset('i', 0, orig)
234 call feedkeys("SK\<Esc>", 'xt')
235 call assert_equal('one<CR>two', getline(1))
236
237 iunmap K
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200238
239 " Test literal <CR> using CTRL-V
240 inoremap K one<CR>two
241 call feedkeys("SK\<Esc>", 'xt')
242 call assert_equal('one<CR>two', getline(1))
243
244 let orig = maparg('K', 'i', 0, 1)
245 call assert_equal('K', orig.lhs)
246 call assert_equal("one\x16<CR>two", orig.rhs)
247 call assert_equal('i', orig.mode)
248
249 iunmap K
250 let d = maparg('K', 'i', 0, 1)
251 call assert_equal({}, d)
252
253 call mapset('i', 0, orig)
254 call feedkeys("SK\<Esc>", 'xt')
255 call assert_equal('one<CR>two', getline(1))
256
257 iunmap K
258 let &cpo = cpo_save
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200259 bwipe!
Bram Moolenaar1b912982020-09-29 21:45:41 +0200260
261 call assert_fails('call mapset([], v:false, {})', 'E730:')
Ernie Rael51d04d12022-05-04 15:40:22 +0100262 call assert_fails('call mapset("i", 0, "")', 'E715:')
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100263 call assert_fails('call mapset("i", 0, {})', 'E460:')
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200264endfunc
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100265
Ernie Rael51d04d12022-05-04 15:40:22 +0100266def Test_mapset_arg1_dir()
267 # This test is mostly about get_map_mode_string.
268 # Once the code gets past that, it's common with the 3 arg mapset.
269
270 # GetModes() return list of modes for 'XZ' lhs using maplist.
271 # There is one list item per mapping
272 def GetModes(abbr: bool = false): list<string>
273 return maplist(abbr)->filter((_, m) => m.lhs == 'XZ')
274 ->mapnew((_, m) => m.mode)
275 enddef
276
277 const unmap_cmds = [ 'unmap', 'unmap!', 'tunmap', 'lunmap' ]
278 def UnmapAll(lhs: string)
279 for cmd in unmap_cmds
280 try | execute(cmd .. ' ' .. lhs) | catch /E31/ | endtry
281 endfor
282 enddef
283
284 var tmap: dict<any>
285
286 # some mapset(mode, abbr, dict) tests using get_map_mode_str
287 map XZ x
288 tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy()
289 # this splits the mapping into 2 mappings
290 mapset('ox', false, tmap)
291 assert_equal(2, len(GetModes()))
292 mapset('o', false, tmap)
293 assert_equal(3, len(GetModes()))
294 # test that '' acts like ' ', and that the 3 mappings become 1
295 mapset('', false, tmap)
296 assert_equal([' '], GetModes())
297 # dict's mode/abbr are ignored
298 UnmapAll('XZ')
299 tmap.mode = '!'
300 tmap.abbr = true
301 mapset('o', false, tmap)
302 assert_equal(['o'], GetModes())
303
304 # test the 3 arg version handles bad mode string, dict not used
305 assert_fails("mapset('vi', false, {})", 'E1276:')
306
307
308 # get the abbreviations out of the way
309 abbreviate XZ ZX
310 tmap = maplist(true)->filter((_, m) => m.lhs == 'XZ')[0]->copy()
311
312 abclear
313 # 'ic' is the default ab command, shows up as '!'
314 tmap.mode = 'ic'
315 mapset(tmap)
316 assert_equal(['!'], GetModes(true))
317
318 abclear
319 tmap.mode = 'i'
320 mapset(tmap)
321 assert_equal(['i'], GetModes(true))
322
323 abclear
324 tmap.mode = 'c'
325 mapset(tmap)
326 assert_equal(['c'], GetModes(true))
327
328 abclear
329 tmap.mode = '!'
330 mapset(tmap)
331 assert_equal(['!'], GetModes(true))
332
333 assert_fails("mapset({mode: ' !', abbr: 1})", 'E1276:')
334 assert_fails("mapset({mode: 'cl', abbr: 1})", 'E1276:')
335 assert_fails("mapset({mode: 'in', abbr: 1})", 'E1276:')
336
337 # the map commands
338 map XZ x
339 tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy()
340
341 # try the combos
342 UnmapAll('XZ')
343 # 'nxso' is ' ', the unadorned :map
344 tmap.mode = 'nxso'
345 mapset(tmap)
346 assert_equal([' '], GetModes())
347
348 UnmapAll('XZ')
349 # 'ic' is '!'
350 tmap.mode = 'ic'
351 mapset(tmap)
352 assert_equal(['!'], GetModes())
353
354 UnmapAll('XZ')
355 # 'xs' is really 'v'
356 tmap.mode = 'xs'
357 mapset(tmap)
358 assert_equal(['v'], GetModes())
359
360 # try the individual modes
361 UnmapAll('XZ')
362 tmap.mode = 'n'
363 mapset(tmap)
364 assert_equal(['n'], GetModes())
365
366 UnmapAll('XZ')
367 tmap.mode = 'x'
368 mapset(tmap)
369 assert_equal(['x'], GetModes())
370
371 UnmapAll('XZ')
372 tmap.mode = 's'
373 mapset(tmap)
374 assert_equal(['s'], GetModes())
375
376 UnmapAll('XZ')
377 tmap.mode = 'o'
378 mapset(tmap)
379 assert_equal(['o'], GetModes())
380
381 UnmapAll('XZ')
382 tmap.mode = 'i'
383 mapset(tmap)
384 assert_equal(['i'], GetModes())
385
386 UnmapAll('XZ')
387 tmap.mode = 'c'
388 mapset(tmap)
389 assert_equal(['c'], GetModes())
390
391 UnmapAll('XZ')
392 tmap.mode = 't'
393 mapset(tmap)
394 assert_equal(['t'], GetModes())
395
396 UnmapAll('XZ')
397 tmap.mode = 'l'
398 mapset(tmap)
399 assert_equal(['l'], GetModes())
400
401 UnmapAll('XZ')
402
403 # get errors for modes that can't be in one mapping
404 assert_fails("mapset({mode: 'nxsoi', abbr: 0})", 'E1276:')
405 assert_fails("mapset({mode: ' !', abbr: 0})", 'E1276:')
406 assert_fails("mapset({mode: 'ix', abbr: 0})", 'E1276:')
407 assert_fails("mapset({mode: 'tl', abbr: 0})", 'E1276:')
408 assert_fails("mapset({mode: ' l', abbr: 0})", 'E1276:')
409 assert_fails("mapset({mode: ' t', abbr: 0})", 'E1276:')
410enddef
411
Bram Moolenaar9c652532020-05-24 13:10:18 +0200412func Check_ctrlb_map(d, check_alt)
413 call assert_equal('<C-B>', a:d.lhs)
414 if a:check_alt
415 call assert_equal("\x80\xfc\x04B", a:d.lhsraw)
416 call assert_equal("\x02", a:d.lhsrawalt)
417 else
418 call assert_equal("\x02", a:d.lhsraw)
419 endif
420endfunc
421
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +0200422func Test_map_local()
423 nmap a global
424 nmap <buffer>a local
425
426 let prev_map_list = split(execute('nmap a'), "\n")
427 call assert_match('n\s*a\s*@local', prev_map_list[0])
428 call assert_match('n\s*a\s*global', prev_map_list[1])
429
430 let mapping = maparg('a', 'n', 0, 1)
431 call assert_equal(1, mapping.buffer)
432 let mapping.rhs = 'new_local'
433 call mapset('n', 0, mapping)
434
435 " Check that the global mapping is left untouched.
436 let map_list = split(execute('nmap a'), "\n")
437 call assert_match('n\s*a\s*@new_local', map_list[0])
438 call assert_match('n\s*a\s*global', map_list[1])
439
440 nunmap a
441endfunc
442
Bram Moolenaar9c652532020-05-24 13:10:18 +0200443func Test_map_restore()
444 " Test restoring map with alternate keycode
445 nmap <C-B> back
446 let d = maparg('<C-B>', 'n', 0, 1)
447 call Check_ctrlb_map(d, 1)
448 let dsimp = maparg("\x02", 'n', 0, 1)
449 call Check_ctrlb_map(dsimp, 0)
450 nunmap <C-B>
451 call mapset('n', 0, d)
452 let d = maparg('<C-B>', 'n', 0, 1)
453 call Check_ctrlb_map(d, 1)
454 let dsimp = maparg("\x02", 'n', 0, 1)
455 call Check_ctrlb_map(dsimp, 0)
456
457 nunmap <C-B>
458
459endfunc
460
Ernie Rael09661202022-04-25 14:40:44 +0100461def Test_maplist()
Ernie Rael659c2402022-04-24 18:40:28 +0100462 new
Ernie Rael09661202022-04-25 14:40:44 +0100463 def ClearMappingsAbbreviations()
Ernie Rael659c2402022-04-24 18:40:28 +0100464 mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear
465 mapclear! | imapclear | lmapclear | cmapclear | tmapclear
466 mapclear <buffer> | nmapclear <buffer> | vmapclear <buffer>
467 xmapclear <buffer> | smapclear <buffer> | omapclear <buffer>
468 mapclear! <buffer> | imapclear <buffer> | lmapclear <buffer>
469 cmapclear <buffer> | tmapclear <buffer>
Ernie Rael09661202022-04-25 14:40:44 +0100470 abclear | abclear <buffer>
Ernie Rael659c2402022-04-24 18:40:28 +0100471 enddef
472
473 def AddMaps(new: list<string>, accum: list<string>)
474 if len(new) > 0 && new[0] != "No mapping found"
475 accum->extend(new)
476 endif
477 enddef
478
Ernie Rael09661202022-04-25 14:40:44 +0100479 ClearMappingsAbbreviations()
480 assert_equal(0, len(maplist()))
481 assert_equal(0, len(maplist(true)))
Ernie Rael659c2402022-04-24 18:40:28 +0100482
483 # Set up some mappings.
484 map dup bar
485 map <buffer> dup bufbar
486 map foo<C-V> is<F4>foo
487 vnoremap <script> <buffer> <expr> <silent> bar isbar
488 tmap baz foo
489 omap h w
490 lmap i w
491 nmap j w
492 xmap k w
493 smap l w
494 map abc <Nop>
495 nmap <M-j> x
496 nmap <M-Space> y
Ernie Rael09661202022-04-25 14:40:44 +0100497 # And abbreviations
498 abbreviate xy he
499 abbreviate xx she
500 abbreviate <buffer> x they
Ernie Rael659c2402022-04-24 18:40:28 +0100501
502 # Get a list of the mappings with the ':map' commands.
Ernie Rael09661202022-04-25 14:40:44 +0100503 # Check maplist() return a list of the same size.
504 assert_equal(13, len(maplist()))
505 assert_equal(3, len(maplist(true)))
506 assert_equal(13, len(maplist(false)))
Ernie Rael659c2402022-04-24 18:40:28 +0100507
508 # collect all the current maps using :map commands
509 var maps_command: list<string>
510 AddMaps(split(execute('map'), '\n'), maps_command)
511 AddMaps(split(execute('map!'), '\n'), maps_command)
512 AddMaps(split(execute('tmap'), '\n'), maps_command)
513 AddMaps(split(execute('lmap'), '\n'), maps_command)
514
Ernie Rael09661202022-04-25 14:40:44 +0100515 # Use maplist to get all the maps
516 var maps_maplist = maplist()
517 assert_equal(len(maps_command), len(maps_maplist))
Ernie Rael659c2402022-04-24 18:40:28 +0100518
519 # make sure all the mode-lhs are unique, no duplicates
520 var map_set: dict<number>
Ernie Rael09661202022-04-25 14:40:44 +0100521 for d in maps_maplist
Ernie Rael659c2402022-04-24 18:40:28 +0100522 map_set[d.mode .. "-" .. d.lhs .. "-" .. d.buffer] = 0
523 endfor
Ernie Rael09661202022-04-25 14:40:44 +0100524 assert_equal(len(maps_maplist), len(map_set))
Ernie Rael659c2402022-04-24 18:40:28 +0100525
Ernie Rael09661202022-04-25 14:40:44 +0100526 # For everything returned by maplist, should be the same as from maparg.
Ernie Rael659c2402022-04-24 18:40:28 +0100527 # Except for "map dup", bacause maparg returns the <buffer> version
Ernie Rael09661202022-04-25 14:40:44 +0100528 for d in maps_maplist
Ernie Rael659c2402022-04-24 18:40:28 +0100529 if d.lhs == 'dup' && d.buffer == 0
530 continue
531 endif
532 var d_maparg = maparg(d.lhs, d.mode, false, true)
533 assert_equal(d_maparg, d)
534 endfor
535
Ernie Rael09661202022-04-25 14:40:44 +0100536 # Check abbr matches maparg
537 for d in maplist(true)
538 # Note, d.mode is '!', but can't use that with maparg
539 var d_maparg = maparg(d.lhs, 'i', true, true)
540 assert_equal(d_maparg, d)
541 endfor
542
543 ClearMappingsAbbreviations()
544 assert_equal(0, len(maplist()))
545 assert_equal(0, len(maplist(true)))
Ernie Rael659c2402022-04-24 18:40:28 +0100546enddef
547
548
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100549" vim: shiftwidth=2 sts=2 expandtab