blob: 81e057adcae14f6c1ced3a0cff252493cdd52cbb [file] [log] [blame]
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02001" Tests for maparg(), mapcheck() and mapset().
Bram Moolenaar292eff02017-07-11 21:46:28 +02002" Also test utf8 map with a 0x80 byte.
Bram Moolenaara1449832019-09-01 20:16:52 +02003" Also test mapcheck()
Bram Moolenaar292eff02017-07-11 21:46:28 +02004
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02005func s:SID()
Bram Moolenaar292eff02017-07-11 21:46:28 +02006 return str2nr(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$'))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02007endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +02008
Bram Moolenaarc94c1462020-05-22 20:01:06 +02009func Test_maparg()
Bram Moolenaar292eff02017-07-11 21:46:28 +020010 new
11 set cpo-=<
12 set encoding=utf8
13 " Test maparg() with a string result
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020014 let sid = s:SID()
15 let lnum = expand('<sflnum>')
Bram Moolenaar292eff02017-07-11 21:46:28 +020016 map foo<C-V> is<F4>foo
17 vnoremap <script> <buffer> <expr> <silent> bar isbar
Bram Moolenaar292eff02017-07-11 21:46:28 +020018 call assert_equal("is<F4>foo", maparg('foo<C-V>'))
Bram Moolenaar2da0f0c2020-04-01 19:22:12 +020019 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo<C-V>',
Bram Moolenaar9c652532020-05-24 13:10:18 +020020 \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020021 \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'lnum': lnum + 1,
Bram Moolenaar9c652532020-05-24 13:10:18 +020022 \ 'rhs': 'is<F4>foo', 'buffer': 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 Moolenaarf29c1c62018-09-10 21:05:02 +020026 \ 'nowait': 0, 'expr': 1, 'sid': sid, 'lnum': lnum + 2,
Bram Moolenaar9c652532020-05-24 13:10:18 +020027 \ 'rhs': 'isbar', 'buffer': 1},
Bram Moolenaara1449832019-09-01 20:16:52 +020028 \ 'bar'->maparg('', 0, 1))
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020029 let lnum = expand('<sflnum>')
Bram Moolenaar292eff02017-07-11 21:46:28 +020030 map <buffer> <nowait> foo bar
Bram Moolenaar9c652532020-05-24 13:10:18 +020031 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo',
32 \ 'lhsraw': 'foo', 'mode': ' ',
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020033 \ 'nowait': 1, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, 'rhs': 'bar',
Bram Moolenaar9c652532020-05-24 13:10:18 +020034 \ 'buffer': 1},
Bram Moolenaar292eff02017-07-11 21:46:28 +020035 \ maparg('foo', '', 0, 1))
Bram Moolenaar14371ed2019-07-27 21:05:21 +020036 let lnum = expand('<sflnum>')
37 tmap baz foo
Bram Moolenaar9c652532020-05-24 13:10:18 +020038 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz',
39 \ 'lhsraw': 'baz', 'mode': 't',
Bram Moolenaar14371ed2019-07-27 21:05:21 +020040 \ 'nowait': 0, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, 'rhs': 'foo',
Bram Moolenaar9c652532020-05-24 13:10:18 +020041 \ 'buffer': 0},
Bram Moolenaar14371ed2019-07-27 21:05:21 +020042 \ maparg('baz', 't', 0, 1))
Bram Moolenaar292eff02017-07-11 21:46:28 +020043
44 map abc x<char-114>x
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020045 call assert_equal("xrx", maparg('abc'))
Bram Moolenaar292eff02017-07-11 21:46:28 +020046 map abc y<S-char-114>y
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020047 call assert_equal("yRy", maparg('abc'))
48
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +010049 omap { w
50 let d = maparg('{', 'o', 0, 1)
51 call assert_equal(['{', 'w', 'o'], [d.lhs, d.rhs, d.mode])
52 ounmap {
53
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +010054 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
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020074 map abc <Nop>
75 call assert_equal("<Nop>", maparg('abc'))
76 unmap abc
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +010077
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
Bram Moolenaar4c9243f2020-05-22 13:10:44 +020082endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +020083
Bram Moolenaara1449832019-09-01 20:16:52 +020084func 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
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200123func Test_range_map()
Bram Moolenaar292eff02017-07-11 21:46:28 +0200124 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))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200138endfunc
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>')
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200163
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
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200206
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
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200227 bwipe!
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200228endfunc
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100229
Bram Moolenaar9c652532020-05-24 13:10:18 +0200230func 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
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100258" vim: shiftwidth=2 sts=2 expandtab