xref: /vim-8.2.3635/src/testdir/test_maparg.vim (revision 4c295027)
1" Tests for maparg(), mapcheck() and mapset().
2" Also test utf8 map with a 0x80 byte.
3" Also test mapcheck()
4
5func s:SID()
6  return str2nr(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$'))
7endfunc
8
9func Test_maparg()
10  new
11  set cpo-=<
12  set encoding=utf8
13  " Test maparg() with a string result
14  let sid = s:SID()
15  let lnum = expand('<sflnum>')
16  map foo<C-V> is<F4>foo
17  vnoremap <script> <buffer> <expr> <silent> bar isbar
18  call assert_equal("is<F4>foo", maparg('foo<C-V>'))
19  call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo<C-V>',
20        \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
21        \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'lnum': lnum + 1,
22	\ 'rhs': 'is<F4>foo', 'buffer': 0},
23	\ maparg('foo<C-V>', '', 0, 1))
24  call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar',
25        \ 'lhsraw': 'bar', 'mode': 'v',
26        \ 'nowait': 0, 'expr': 1, 'sid': sid, 'lnum': lnum + 2,
27	\ 'rhs': 'isbar', 'buffer': 1},
28        \ 'bar'->maparg('', 0, 1))
29  let lnum = expand('<sflnum>')
30  map <buffer> <nowait> foo bar
31  call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo',
32        \ 'lhsraw': 'foo', 'mode': ' ',
33        \ 'nowait': 1, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, 'rhs': 'bar',
34	\ 'buffer': 1},
35        \ maparg('foo', '', 0, 1))
36  let lnum = expand('<sflnum>')
37  tmap baz foo
38  call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz',
39        \ 'lhsraw': 'baz', 'mode': 't',
40        \ 'nowait': 0, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, 'rhs': 'foo',
41	\ 'buffer': 0},
42        \ maparg('baz', 't', 0, 1))
43
44  map abc x<char-114>x
45  call assert_equal("xrx", maparg('abc'))
46  map abc y<S-char-114>y
47  call assert_equal("yRy", maparg('abc'))
48
49  omap { w
50  let d = maparg('{', 'o', 0, 1)
51  call assert_equal(['{', 'w', 'o'], [d.lhs, d.rhs, d.mode])
52  ounmap {
53
54  lmap { w
55  let d = maparg('{', 'l', 0, 1)
56  call assert_equal(['{', 'w', 'l'], [d.lhs, d.rhs, d.mode])
57  lunmap {
58
59  nmap { w
60  let d = maparg('{', 'n', 0, 1)
61  call assert_equal(['{', 'w', 'n'], [d.lhs, d.rhs, d.mode])
62  nunmap {
63
64  xmap { w
65  let d = maparg('{', 'x', 0, 1)
66  call assert_equal(['{', 'w', 'x'], [d.lhs, d.rhs, d.mode])
67  xunmap {
68
69  smap { w
70  let d = maparg('{', 's', 0, 1)
71  call assert_equal(['{', 'w', 's'], [d.lhs, d.rhs, d.mode])
72  sunmap {
73
74  map abc <Nop>
75  call assert_equal("<Nop>", maparg('abc'))
76  unmap abc
77
78  call feedkeys(":abbr esc \<C-V>\<C-V>\<C-V>\<C-V>\<C-V>\<Esc>\<CR>", "xt")
79  let d = maparg('esc', 'i', 1, 1)
80  call assert_equal(['esc', "\<C-V>\<C-V>\<Esc>", '!'], [d.lhs, d.rhs, d.mode])
81  abclear
82endfunc
83
84def Test_vim9_maparg()
85  nmap { w
86  var one: string = maparg('{')
87  assert_equal('w', one)
88  var two: string = maparg('{', 'n')
89  assert_equal('w', two)
90  var three: string = maparg('{', 'n', 0)
91  assert_equal('w', three)
92  var four: dict<any> = maparg('{', 'n', 0, 1)
93  assert_equal(['{', 'w', 'n'], [four.lhs, four.rhs, four.mode])
94  nunmap {
95enddef
96
97func Test_mapcheck()
98  call assert_equal('', mapcheck('a'))
99  call assert_equal('', mapcheck('abc'))
100  call assert_equal('', mapcheck('ax'))
101  call assert_equal('', mapcheck('b'))
102
103  map a something
104  call assert_equal('something', mapcheck('a'))
105  call assert_equal('something', mapcheck('a', 'n'))
106  call assert_equal('', mapcheck('a', 'c'))
107  call assert_equal('', mapcheck('a', 'i'))
108  call assert_equal('something', 'abc'->mapcheck())
109  call assert_equal('something', 'ax'->mapcheck())
110  call assert_equal('', mapcheck('b'))
111  unmap a
112
113  map ab foobar
114  call assert_equal('foobar', mapcheck('a'))
115  call assert_equal('foobar', mapcheck('abc'))
116  call assert_equal('', mapcheck('ax'))
117  call assert_equal('', mapcheck('b'))
118  unmap ab
119
120  map abc barfoo
121  call assert_equal('barfoo', mapcheck('a'))
122  call assert_equal('barfoo', mapcheck('a', 'n', 0))
123  call assert_equal('', mapcheck('a', 'n', 1))
124  call assert_equal('barfoo', mapcheck('abc'))
125  call assert_equal('', mapcheck('ax'))
126  call assert_equal('', mapcheck('b'))
127  unmap abc
128
129  abbr ab abbrev
130  call assert_equal('abbrev', mapcheck('a', 'i', 1))
131  call assert_equal('', mapcheck('a', 'n', 1))
132  call assert_equal('', mapcheck('a', 'i', 0))
133  unabbr ab
134endfunc
135
136func Test_range_map()
137  new
138  " Outside of the range, minimum
139  inoremap <Char-0x1040> a
140  execute "normal a\u1040\<Esc>"
141  " Inside of the range, minimum
142  inoremap <Char-0x103f> b
143  execute "normal a\u103f\<Esc>"
144  " Inside of the range, maximum
145  inoremap <Char-0xf03f> c
146  execute "normal a\uf03f\<Esc>"
147  " Outside of the range, maximum
148  inoremap <Char-0xf040> d
149  execute "normal a\uf040\<Esc>"
150  call assert_equal("abcd", getline(1))
151endfunc
152
153func One_mapset_test(keys)
154  exe 'nnoremap ' .. a:keys .. ' original<CR>'
155  let orig = maparg(a:keys, 'n', 0, 1)
156  call assert_equal(a:keys, orig.lhs)
157  call assert_equal('original<CR>', orig.rhs)
158  call assert_equal('n', orig.mode)
159
160  exe 'nunmap ' .. a:keys
161  let d = maparg(a:keys, 'n', 0, 1)
162  call assert_equal({}, d)
163
164  call mapset('n', 0, orig)
165  let d = maparg(a:keys, 'n', 0, 1)
166  call assert_equal(a:keys, d.lhs)
167  call assert_equal('original<CR>', d.rhs)
168  call assert_equal('n', d.mode)
169
170  exe 'nunmap ' .. a:keys
171endfunc
172
173func Test_mapset()
174  call One_mapset_test('K')
175  call One_mapset_test('<F3>')
176
177  " Check <> key conversion
178  new
179  inoremap K one<Left>x
180  call feedkeys("iK\<Esc>", 'xt')
181  call assert_equal('onxe', getline(1))
182
183  let orig = maparg('K', 'i', 0, 1)
184  call assert_equal('K', orig.lhs)
185  call assert_equal('one<Left>x', orig.rhs)
186  call assert_equal('i', orig.mode)
187
188  iunmap K
189  let d = maparg('K', 'i', 0, 1)
190  call assert_equal({}, d)
191
192  call mapset('i', 0, orig)
193  call feedkeys("SK\<Esc>", 'xt')
194  call assert_equal('onxe', getline(1))
195
196  iunmap K
197
198  " Test literal <CR> using a backslash
199  let cpo_save = &cpo
200  set cpo-=B
201  inoremap K one\<CR>two
202  call feedkeys("SK\<Esc>", 'xt')
203  call assert_equal('one<CR>two', getline(1))
204
205  let orig = maparg('K', 'i', 0, 1)
206  call assert_equal('K', orig.lhs)
207  call assert_equal('one\<CR>two', orig.rhs)
208  call assert_equal('i', orig.mode)
209
210  iunmap K
211  let d = maparg('K', 'i', 0, 1)
212  call assert_equal({}, d)
213
214  call mapset('i', 0, orig)
215  call feedkeys("SK\<Esc>", 'xt')
216  call assert_equal('one<CR>two', getline(1))
217
218  iunmap K
219
220  " Test literal <CR> using CTRL-V
221  inoremap K one<CR>two
222  call feedkeys("SK\<Esc>", 'xt')
223  call assert_equal('one<CR>two', getline(1))
224
225  let orig = maparg('K', 'i', 0, 1)
226  call assert_equal('K', orig.lhs)
227  call assert_equal("one\x16<CR>two", orig.rhs)
228  call assert_equal('i', orig.mode)
229
230  iunmap K
231  let d = maparg('K', 'i', 0, 1)
232  call assert_equal({}, d)
233
234  call mapset('i', 0, orig)
235  call feedkeys("SK\<Esc>", 'xt')
236  call assert_equal('one<CR>two', getline(1))
237
238  iunmap K
239  let &cpo = cpo_save
240  bwipe!
241
242  call assert_fails('call mapset([], v:false, {})', 'E730:')
243  call assert_fails('call mapset("i", 0, "")', 'E716:')
244  call assert_fails('call mapset("i", 0, {})', 'E460:')
245endfunc
246
247func Check_ctrlb_map(d, check_alt)
248  call assert_equal('<C-B>', a:d.lhs)
249  if a:check_alt
250    call assert_equal("\x80\xfc\x04B", a:d.lhsraw)
251    call assert_equal("\x02", a:d.lhsrawalt)
252  else
253    call assert_equal("\x02", a:d.lhsraw)
254  endif
255endfunc
256
257func Test_map_local()
258  nmap a global
259  nmap <buffer>a local
260
261  let prev_map_list = split(execute('nmap a'), "\n")
262  call assert_match('n\s*a\s*@local', prev_map_list[0])
263  call assert_match('n\s*a\s*global', prev_map_list[1])
264
265  let mapping = maparg('a', 'n', 0, 1)
266  call assert_equal(1, mapping.buffer)
267  let mapping.rhs = 'new_local'
268  call mapset('n', 0, mapping)
269
270  " Check that the global mapping is left untouched.
271  let map_list = split(execute('nmap a'), "\n")
272  call assert_match('n\s*a\s*@new_local', map_list[0])
273  call assert_match('n\s*a\s*global', map_list[1])
274
275  nunmap a
276endfunc
277
278func Test_map_restore()
279  " Test restoring map with alternate keycode
280  nmap <C-B> back
281  let d = maparg('<C-B>', 'n', 0, 1)
282  call Check_ctrlb_map(d, 1)
283  let dsimp = maparg("\x02", 'n', 0, 1)
284  call Check_ctrlb_map(dsimp, 0)
285  nunmap <C-B>
286  call mapset('n', 0, d)
287  let d = maparg('<C-B>', 'n', 0, 1)
288  call Check_ctrlb_map(d, 1)
289  let dsimp = maparg("\x02", 'n', 0, 1)
290  call Check_ctrlb_map(dsimp, 0)
291
292  nunmap <C-B>
293
294endfunc
295
296" vim: shiftwidth=2 sts=2 expandtab
297