xref: /vim-8.2.3635/src/testdir/test_maparg.vim (revision 591cec83)
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        \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'lnum': lnum + 1,
21	\ 'simplified': 1, 'rhs': 'is<F4>foo', 'buffer': 0},
22	\ maparg('foo<C-V>', '', 0, 1))
23  call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar', 'mode': 'v',
24        \ 'nowait': 0, 'expr': 1, 'sid': sid, 'lnum': lnum + 2,
25	\ 'simplified': 0, 'rhs': 'isbar', 'buffer': 1},
26        \ 'bar'->maparg('', 0, 1))
27  let lnum = expand('<sflnum>')
28  map <buffer> <nowait> foo bar
29  call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo', 'mode': ' ',
30        \ 'nowait': 1, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, 'rhs': 'bar',
31	\ 'simplified': 0, 'buffer': 1},
32        \ maparg('foo', '', 0, 1))
33  let lnum = expand('<sflnum>')
34  tmap baz foo
35  call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz', 'mode': 't',
36        \ 'nowait': 0, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, 'rhs': 'foo',
37	\ 'simplified': 0, 'buffer': 0},
38        \ maparg('baz', 't', 0, 1))
39
40  map abc x<char-114>x
41  call assert_equal("xrx", maparg('abc'))
42  map abc y<S-char-114>y
43  call assert_equal("yRy", maparg('abc'))
44
45  omap { w
46  let d = maparg('{', 'o', 0, 1)
47  call assert_equal(['{', 'w', 'o'], [d.lhs, d.rhs, d.mode])
48  ounmap {
49
50  lmap { w
51  let d = maparg('{', 'l', 0, 1)
52  call assert_equal(['{', 'w', 'l'], [d.lhs, d.rhs, d.mode])
53  lunmap {
54
55  nmap { w
56  let d = maparg('{', 'n', 0, 1)
57  call assert_equal(['{', 'w', 'n'], [d.lhs, d.rhs, d.mode])
58  nunmap {
59
60  xmap { w
61  let d = maparg('{', 'x', 0, 1)
62  call assert_equal(['{', 'w', 'x'], [d.lhs, d.rhs, d.mode])
63  xunmap {
64
65  smap { w
66  let d = maparg('{', 's', 0, 1)
67  call assert_equal(['{', 'w', 's'], [d.lhs, d.rhs, d.mode])
68  sunmap {
69
70  map abc <Nop>
71  call assert_equal("<Nop>", maparg('abc'))
72  unmap abc
73
74  call feedkeys(":abbr esc \<C-V>\<C-V>\<C-V>\<C-V>\<C-V>\<Esc>\<CR>", "xt")
75  let d = maparg('esc', 'i', 1, 1)
76  call assert_equal(['esc', "\<C-V>\<C-V>\<Esc>", '!'], [d.lhs, d.rhs, d.mode])
77  abclear
78endfunc
79
80func Test_mapcheck()
81  call assert_equal('', mapcheck('a'))
82  call assert_equal('', mapcheck('abc'))
83  call assert_equal('', mapcheck('ax'))
84  call assert_equal('', mapcheck('b'))
85
86  map a something
87  call assert_equal('something', mapcheck('a'))
88  call assert_equal('something', mapcheck('a', 'n'))
89  call assert_equal('', mapcheck('a', 'c'))
90  call assert_equal('', mapcheck('a', 'i'))
91  call assert_equal('something', 'abc'->mapcheck())
92  call assert_equal('something', 'ax'->mapcheck())
93  call assert_equal('', mapcheck('b'))
94  unmap a
95
96  map ab foobar
97  call assert_equal('foobar', mapcheck('a'))
98  call assert_equal('foobar', mapcheck('abc'))
99  call assert_equal('', mapcheck('ax'))
100  call assert_equal('', mapcheck('b'))
101  unmap ab
102
103  map abc barfoo
104  call assert_equal('barfoo', mapcheck('a'))
105  call assert_equal('barfoo', mapcheck('a', 'n', 0))
106  call assert_equal('', mapcheck('a', 'n', 1))
107  call assert_equal('barfoo', mapcheck('abc'))
108  call assert_equal('', mapcheck('ax'))
109  call assert_equal('', mapcheck('b'))
110  unmap abc
111
112  abbr ab abbrev
113  call assert_equal('abbrev', mapcheck('a', 'i', 1))
114  call assert_equal('', mapcheck('a', 'n', 1))
115  call assert_equal('', mapcheck('a', 'i', 0))
116  unabbr ab
117endfunc
118
119func Test_range_map()
120  new
121  " Outside of the range, minimum
122  inoremap <Char-0x1040> a
123  execute "normal a\u1040\<Esc>"
124  " Inside of the range, minimum
125  inoremap <Char-0x103f> b
126  execute "normal a\u103f\<Esc>"
127  " Inside of the range, maximum
128  inoremap <Char-0xf03f> c
129  execute "normal a\uf03f\<Esc>"
130  " Outside of the range, maximum
131  inoremap <Char-0xf040> d
132  execute "normal a\uf040\<Esc>"
133  call assert_equal("abcd", getline(1))
134endfunc
135
136func One_mapset_test(keys)
137  exe 'nnoremap ' .. a:keys .. ' original<CR>'
138  let orig = maparg(a:keys, 'n', 0, 1)
139  call assert_equal(a:keys, orig.lhs)
140  call assert_equal('original<CR>', orig.rhs)
141  call assert_equal('n', orig.mode)
142
143  exe 'nunmap ' .. a:keys
144  let d = maparg(a:keys, 'n', 0, 1)
145  call assert_equal({}, d)
146
147  call mapset('n', 0, orig)
148  let d = maparg(a:keys, 'n', 0, 1)
149  call assert_equal(a:keys, d.lhs)
150  call assert_equal('original<CR>', d.rhs)
151  call assert_equal('n', d.mode)
152
153  exe 'nunmap ' .. a:keys
154endfunc
155
156func Test_mapset()
157  call One_mapset_test('K')
158  call One_mapset_test('<F3>')
159
160  " Check <> key conversion
161  new
162  inoremap K one<Left>x
163  call feedkeys("iK\<Esc>", 'xt')
164  call assert_equal('onxe', getline(1))
165
166  let orig = maparg('K', 'i', 0, 1)
167  call assert_equal('K', orig.lhs)
168  call assert_equal('one<Left>x', orig.rhs)
169  call assert_equal('i', orig.mode)
170
171  iunmap K
172  let d = maparg('K', 'i', 0, 1)
173  call assert_equal({}, d)
174
175  call mapset('i', 0, orig)
176  call feedkeys("SK\<Esc>", 'xt')
177  call assert_equal('onxe', getline(1))
178
179  iunmap K
180
181  " Test literal <CR> using a backslash
182  let cpo_save = &cpo
183  set cpo-=B
184  inoremap K one\<CR>two
185  call feedkeys("SK\<Esc>", 'xt')
186  call assert_equal('one<CR>two', getline(1))
187
188  let orig = maparg('K', 'i', 0, 1)
189  call assert_equal('K', orig.lhs)
190  call assert_equal('one\<CR>two', orig.rhs)
191  call assert_equal('i', orig.mode)
192
193  iunmap K
194  let d = maparg('K', 'i', 0, 1)
195  call assert_equal({}, d)
196
197  call mapset('i', 0, orig)
198  call feedkeys("SK\<Esc>", 'xt')
199  call assert_equal('one<CR>two', getline(1))
200
201  iunmap K
202  let &cpo = cpo_save
203
204  " Test literal <CR> using CTRL-V
205  inoremap K one<CR>two
206  call feedkeys("SK\<Esc>", 'xt')
207  call assert_equal('one<CR>two', getline(1))
208
209  let orig = maparg('K', 'i', 0, 1)
210  call assert_equal('K', orig.lhs)
211  call assert_equal("one\x16<CR>two", orig.rhs)
212  call assert_equal('i', orig.mode)
213
214  iunmap K
215  let d = maparg('K', 'i', 0, 1)
216  call assert_equal({}, d)
217
218  call mapset('i', 0, orig)
219  call feedkeys("SK\<Esc>", 'xt')
220  call assert_equal('one<CR>two', getline(1))
221
222  iunmap K
223  let &cpo = cpo_save
224
225  bwipe!
226endfunc
227
228" vim: shiftwidth=2 sts=2 expandtab
229