xref: /vim-8.2.3635/src/testdir/test_maparg.vim (revision 92c098d1)
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
84func Test_mapcheck()
85  call assert_equal('', mapcheck('a'))
86  call assert_equal('', mapcheck('abc'))
87  call assert_equal('', mapcheck('ax'))
88  call assert_equal('', mapcheck('b'))
89
90  map a something
91  call assert_equal('something', mapcheck('a'))
92  call assert_equal('something', mapcheck('a', 'n'))
93  call assert_equal('', mapcheck('a', 'c'))
94  call assert_equal('', mapcheck('a', 'i'))
95  call assert_equal('something', 'abc'->mapcheck())
96  call assert_equal('something', 'ax'->mapcheck())
97  call assert_equal('', mapcheck('b'))
98  unmap a
99
100  map ab foobar
101  call assert_equal('foobar', mapcheck('a'))
102  call assert_equal('foobar', mapcheck('abc'))
103  call assert_equal('', mapcheck('ax'))
104  call assert_equal('', mapcheck('b'))
105  unmap ab
106
107  map abc barfoo
108  call assert_equal('barfoo', mapcheck('a'))
109  call assert_equal('barfoo', mapcheck('a', 'n', 0))
110  call assert_equal('', mapcheck('a', 'n', 1))
111  call assert_equal('barfoo', mapcheck('abc'))
112  call assert_equal('', mapcheck('ax'))
113  call assert_equal('', mapcheck('b'))
114  unmap abc
115
116  abbr ab abbrev
117  call assert_equal('abbrev', mapcheck('a', 'i', 1))
118  call assert_equal('', mapcheck('a', 'n', 1))
119  call assert_equal('', mapcheck('a', 'i', 0))
120  unabbr ab
121endfunc
122
123func Test_range_map()
124  new
125  " Outside of the range, minimum
126  inoremap <Char-0x1040> a
127  execute "normal a\u1040\<Esc>"
128  " Inside of the range, minimum
129  inoremap <Char-0x103f> b
130  execute "normal a\u103f\<Esc>"
131  " Inside of the range, maximum
132  inoremap <Char-0xf03f> c
133  execute "normal a\uf03f\<Esc>"
134  " Outside of the range, maximum
135  inoremap <Char-0xf040> d
136  execute "normal a\uf040\<Esc>"
137  call assert_equal("abcd", getline(1))
138endfunc
139
140func One_mapset_test(keys)
141  exe 'nnoremap ' .. a:keys .. ' original<CR>'
142  let orig = maparg(a:keys, 'n', 0, 1)
143  call assert_equal(a:keys, orig.lhs)
144  call assert_equal('original<CR>', orig.rhs)
145  call assert_equal('n', orig.mode)
146
147  exe 'nunmap ' .. a:keys
148  let d = maparg(a:keys, 'n', 0, 1)
149  call assert_equal({}, d)
150
151  call mapset('n', 0, orig)
152  let d = maparg(a:keys, 'n', 0, 1)
153  call assert_equal(a:keys, d.lhs)
154  call assert_equal('original<CR>', d.rhs)
155  call assert_equal('n', d.mode)
156
157  exe 'nunmap ' .. a:keys
158endfunc
159
160func Test_mapset()
161  call One_mapset_test('K')
162  call One_mapset_test('<F3>')
163
164  " Check <> key conversion
165  new
166  inoremap K one<Left>x
167  call feedkeys("iK\<Esc>", 'xt')
168  call assert_equal('onxe', getline(1))
169
170  let orig = maparg('K', 'i', 0, 1)
171  call assert_equal('K', orig.lhs)
172  call assert_equal('one<Left>x', orig.rhs)
173  call assert_equal('i', orig.mode)
174
175  iunmap K
176  let d = maparg('K', 'i', 0, 1)
177  call assert_equal({}, d)
178
179  call mapset('i', 0, orig)
180  call feedkeys("SK\<Esc>", 'xt')
181  call assert_equal('onxe', getline(1))
182
183  iunmap K
184
185  " Test literal <CR> using a backslash
186  let cpo_save = &cpo
187  set cpo-=B
188  inoremap K one\<CR>two
189  call feedkeys("SK\<Esc>", 'xt')
190  call assert_equal('one<CR>two', getline(1))
191
192  let orig = maparg('K', 'i', 0, 1)
193  call assert_equal('K', orig.lhs)
194  call assert_equal('one\<CR>two', orig.rhs)
195  call assert_equal('i', orig.mode)
196
197  iunmap K
198  let d = maparg('K', 'i', 0, 1)
199  call assert_equal({}, d)
200
201  call mapset('i', 0, orig)
202  call feedkeys("SK\<Esc>", 'xt')
203  call assert_equal('one<CR>two', getline(1))
204
205  iunmap K
206
207  " Test literal <CR> using CTRL-V
208  inoremap K one<CR>two
209  call feedkeys("SK\<Esc>", 'xt')
210  call assert_equal('one<CR>two', getline(1))
211
212  let orig = maparg('K', 'i', 0, 1)
213  call assert_equal('K', orig.lhs)
214  call assert_equal("one\x16<CR>two", orig.rhs)
215  call assert_equal('i', orig.mode)
216
217  iunmap K
218  let d = maparg('K', 'i', 0, 1)
219  call assert_equal({}, d)
220
221  call mapset('i', 0, orig)
222  call feedkeys("SK\<Esc>", 'xt')
223  call assert_equal('one<CR>two', getline(1))
224
225  iunmap K
226  let &cpo = cpo_save
227  bwipe!
228endfunc
229
230func Check_ctrlb_map(d, check_alt)
231  call assert_equal('<C-B>', a:d.lhs)
232  if a:check_alt
233    call assert_equal("\x80\xfc\x04B", a:d.lhsraw)
234    call assert_equal("\x02", a:d.lhsrawalt)
235  else
236    call assert_equal("\x02", a:d.lhsraw)
237  endif
238endfunc
239
240func Test_map_restore()
241  " Test restoring map with alternate keycode
242  nmap <C-B> back
243  let d = maparg('<C-B>', 'n', 0, 1)
244  call Check_ctrlb_map(d, 1)
245  let dsimp = maparg("\x02", 'n', 0, 1)
246  call Check_ctrlb_map(dsimp, 0)
247  nunmap <C-B>
248  call mapset('n', 0, d)
249  let d = maparg('<C-B>', 'n', 0, 1)
250  call Check_ctrlb_map(d, 1)
251  let dsimp = maparg("\x02", 'n', 0, 1)
252  call Check_ctrlb_map(dsimp, 0)
253
254  nunmap <C-B>
255
256endfunc
257
258" vim: shiftwidth=2 sts=2 expandtab
259