blob: aad5a8ce9a908acaaf10f3beb6292d36b337f451 [file] [log] [blame]
Bram Moolenaar08243d22017-01-10 16:12:29 +01001" Tests for various functions.
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02002source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02003source check.vim
Bram Moolenaarc2585492019-09-22 21:29:53 +02004source term_util.vim
Bram Moolenaar4132eb52020-02-14 16:53:00 +01005source screendump.vim
Bram Moolenaar08243d22017-01-10 16:12:29 +01006
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +01007" Must be done first, since the alternate buffer must be unset.
8func Test_00_bufexists()
9 call assert_equal(0, bufexists('does_not_exist'))
10 call assert_equal(1, bufexists(bufnr('%')))
11 call assert_equal(0, bufexists(0))
12 new Xfoo
13 let bn = bufnr('%')
14 call assert_equal(1, bufexists(bn))
15 call assert_equal(1, bufexists('Xfoo'))
16 call assert_equal(1, bufexists(getcwd() . '/Xfoo'))
17 call assert_equal(1, bufexists(0))
18 bw
19 call assert_equal(0, bufexists(bn))
20 call assert_equal(0, bufexists('Xfoo'))
21endfunc
22
Bram Moolenaar79296512020-03-22 16:17:14 +010023func Test_has()
24 call assert_equal(1, has('eval'))
25 call assert_equal(1, has('eval', 1))
26
Bram Moolenaar0e05de42020-03-25 22:23:46 +010027 if has('unix')
28 call assert_equal(1, or(has('ttyin'), 1))
29 call assert_equal(0, and(has('ttyout'), 0))
30 call assert_equal(1, has('multi_byte_encoding'))
31 endif
Bram Moolenaar99fa7212020-04-26 15:59:55 +020032 call assert_equal(1, has('vcon', 1))
33 call assert_equal(1, has('mouse_gpm_enabled', 1))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010034
Bram Moolenaar79296512020-03-22 16:17:14 +010035 call assert_equal(0, has('nonexistent'))
36 call assert_equal(0, has('nonexistent', 1))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010037
38 " Will we ever have patch 9999?
39 let ver = 'patch-' .. v:version / 100 .. '.' .. v:version % 100 .. '.9999'
40 call assert_equal(0, has(ver))
Bram Moolenaar79296512020-03-22 16:17:14 +010041endfunc
42
Bram Moolenaar24c2e482017-01-29 15:45:12 +010043func Test_empty()
44 call assert_equal(1, empty(''))
45 call assert_equal(0, empty('a'))
46
47 call assert_equal(1, empty(0))
48 call assert_equal(1, empty(-0))
49 call assert_equal(0, empty(1))
50 call assert_equal(0, empty(-1))
51
Bram Moolenaar5feabe02020-01-30 18:24:53 +010052 if has('float')
53 call assert_equal(1, empty(0.0))
54 call assert_equal(1, empty(-0.0))
55 call assert_equal(0, empty(1.0))
56 call assert_equal(0, empty(-1.0))
57 call assert_equal(0, empty(1.0/0.0))
58 call assert_equal(0, empty(0.0/0.0))
59 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +010060
61 call assert_equal(1, empty([]))
62 call assert_equal(0, empty(['a']))
63
64 call assert_equal(1, empty({}))
65 call assert_equal(0, empty({'a':1}))
66
67 call assert_equal(1, empty(v:null))
68 call assert_equal(1, empty(v:none))
69 call assert_equal(1, empty(v:false))
70 call assert_equal(0, empty(v:true))
71
Bram Moolenaar41042f32017-03-09 12:09:32 +010072 if has('channel')
73 call assert_equal(1, empty(test_null_channel()))
74 endif
75 if has('job')
76 call assert_equal(1, empty(test_null_job()))
77 endif
78
Bram Moolenaar24c2e482017-01-29 15:45:12 +010079 call assert_equal(0, empty(function('Test_empty')))
Bram Moolenaar17aca702019-05-16 22:24:55 +020080 call assert_equal(0, empty(function('Test_empty', [0])))
Bram Moolenaar7c215c52020-02-29 13:43:27 +010081
82 call assert_fails("call empty(test_void())", 'E685:')
83 call assert_fails("call empty(test_unknown())", 'E685:')
Bram Moolenaar24c2e482017-01-29 15:45:12 +010084endfunc
85
Bram Moolenaardd589232020-02-29 17:38:12 +010086func Test_test_void()
87 call assert_fails('echo 1 == test_void()', 'E685:')
88 if has('float')
89 call assert_fails('echo 1.0 == test_void()', 'E685:')
90 endif
91 call assert_fails('let x = json_encode(test_void())', 'E685:')
92 call assert_fails('let x = copy(test_void())', 'E685:')
93 call assert_fails('let x = copy([test_void()])', 'E685:')
94endfunc
95
Bram Moolenaar24c2e482017-01-29 15:45:12 +010096func Test_len()
97 call assert_equal(1, len(0))
98 call assert_equal(2, len(12))
99
100 call assert_equal(0, len(''))
101 call assert_equal(2, len('ab'))
102
103 call assert_equal(0, len([]))
Bram Moolenaar08f41572020-04-20 16:50:00 +0200104 call assert_equal(0, len(test_null_list()))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100105 call assert_equal(2, len([2, 1]))
106
107 call assert_equal(0, len({}))
Bram Moolenaar08f41572020-04-20 16:50:00 +0200108 call assert_equal(0, len(test_null_dict()))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100109 call assert_equal(2, len({'a': 1, 'b': 2}))
110
111 call assert_fails('call len(v:none)', 'E701:')
112 call assert_fails('call len({-> 0})', 'E701:')
113endfunc
114
115func Test_max()
116 call assert_equal(0, max([]))
117 call assert_equal(2, max([2]))
118 call assert_equal(2, max([1, 2]))
119 call assert_equal(2, max([1, 2, v:null]))
120
121 call assert_equal(0, max({}))
122 call assert_equal(2, max({'a':1, 'b':2}))
123
124 call assert_fails('call max(1)', 'E712:')
125 call assert_fails('call max(v:none)', 'E712:')
126endfunc
127
128func Test_min()
129 call assert_equal(0, min([]))
130 call assert_equal(2, min([2]))
131 call assert_equal(1, min([1, 2]))
132 call assert_equal(0, min([1, 2, v:null]))
133
134 call assert_equal(0, min({}))
135 call assert_equal(1, min({'a':1, 'b':2}))
136
137 call assert_fails('call min(1)', 'E712:')
138 call assert_fails('call min(v:none)', 'E712:')
139endfunc
140
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200141func Test_strwidth()
142 for aw in ['single', 'double']
143 exe 'set ambiwidth=' . aw
144 call assert_equal(0, strwidth(''))
145 call assert_equal(1, strwidth("\t"))
146 call assert_equal(3, strwidth('Vim'))
147 call assert_equal(4, strwidth(1234))
148 call assert_equal(5, strwidth(-1234))
149
Bram Moolenaar30276f22019-01-24 17:59:39 +0100150 call assert_equal(2, strwidth('😉'))
151 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
152 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200153
154 call assert_fails('call strwidth({->0})', 'E729:')
155 call assert_fails('call strwidth([])', 'E730:')
156 call assert_fails('call strwidth({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100157 if has('float')
158 call assert_fails('call strwidth(1.2)', 'E806:')
159 endif
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200160 endfor
161
162 set ambiwidth&
163endfunc
164
Bram Moolenaar08243d22017-01-10 16:12:29 +0100165func Test_str2nr()
166 call assert_equal(0, str2nr(''))
167 call assert_equal(1, str2nr('1'))
168 call assert_equal(1, str2nr(' 1 '))
169
170 call assert_equal(1, str2nr('+1'))
171 call assert_equal(1, str2nr('+ 1'))
172 call assert_equal(1, str2nr(' + 1 '))
173
174 call assert_equal(-1, str2nr('-1'))
175 call assert_equal(-1, str2nr('- 1'))
176 call assert_equal(-1, str2nr(' - 1 '))
177
178 call assert_equal(123456789, str2nr('123456789'))
179 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100180
181 call assert_equal(5, str2nr('101', 2))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200182 call assert_equal(5, '0b101'->str2nr(2))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100183 call assert_equal(5, str2nr('0B101', 2))
184 call assert_equal(-5, str2nr('-101', 2))
185 call assert_equal(-5, str2nr('-0b101', 2))
186 call assert_equal(-5, str2nr('-0B101', 2))
187
188 call assert_equal(65, str2nr('101', 8))
189 call assert_equal(65, str2nr('0101', 8))
190 call assert_equal(-65, str2nr('-101', 8))
191 call assert_equal(-65, str2nr('-0101', 8))
Bram Moolenaarc17e66c2020-06-02 21:38:22 +0200192 call assert_equal(65, str2nr('0o101', 8))
193 call assert_equal(65, str2nr('0O0101', 8))
194 call assert_equal(-65, str2nr('-0O101', 8))
195 call assert_equal(-65, str2nr('-0o0101', 8))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100196
197 call assert_equal(11259375, str2nr('abcdef', 16))
198 call assert_equal(11259375, str2nr('ABCDEF', 16))
199 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
200 call assert_equal(11259375, str2nr('0xabcdef', 16))
201 call assert_equal(11259375, str2nr('0Xabcdef', 16))
202 call assert_equal(11259375, str2nr('0XABCDEF', 16))
203 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
204
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200205 call assert_equal(1, str2nr("1'000'000", 10, 0))
206 call assert_equal(256, str2nr("1'0000'0000", 2, 1))
207 call assert_equal(262144, str2nr("1'000'000", 8, 1))
208 call assert_equal(1000000, str2nr("1'000'000", 10, 1))
Bram Moolenaarea8dcf82019-09-15 21:12:22 +0200209 call assert_equal(1000, str2nr("1'000''000", 10, 1))
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200210 call assert_equal(65536, str2nr("1'00'00", 16, 1))
211
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100212 call assert_equal(0, str2nr('0x10'))
213 call assert_equal(0, str2nr('0b10'))
Bram Moolenaarc17e66c2020-06-02 21:38:22 +0200214 call assert_equal(0, str2nr('0o10'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100215 call assert_equal(1, str2nr('12', 2))
216 call assert_equal(1, str2nr('18', 8))
217 call assert_equal(1, str2nr('1g', 16))
218
219 call assert_equal(0, str2nr(v:null))
220 call assert_equal(0, str2nr(v:none))
221
222 call assert_fails('call str2nr([])', 'E730:')
223 call assert_fails('call str2nr({->2})', 'E729:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100224 if has('float')
225 call assert_fails('call str2nr(1.2)', 'E806:')
226 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100227 call assert_fails('call str2nr(10, [])', 'E474:')
228endfunc
229
230func Test_strftime()
Bram Moolenaar10455d42019-11-21 15:36:18 +0100231 CheckFunction strftime
232
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100233 " Format of strftime() depends on system. We assume
234 " that basic formats tested here are available and
235 " identical on all systems which support strftime().
236 "
237 " The 2nd parameter of strftime() is a local time, so the output day
238 " of strftime() can be 17 or 18, depending on timezone.
239 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
240 "
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200241 call assert_match('^\d\d\d\d-\(0\d\|1[012]\)-\([012]\d\|3[01]\) \([01]\d\|2[0-3]\):[0-5]\d:\([0-5]\d\|60\)$', '%Y-%m-%d %H:%M:%S'->strftime())
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100242
243 call assert_fails('call strftime([])', 'E730:')
244 call assert_fails('call strftime("%Y", [])', 'E745:')
Bram Moolenaardb517302019-06-18 22:53:24 +0200245
246 " Check that the time changes after we change the timezone
247 " Save previous timezone value, if any
248 if exists('$TZ')
249 let tz = $TZ
250 endif
251
252 " Force EST and then UTC, save the current hour (24-hour clock) for each
253 let $TZ = 'EST' | let est = strftime('%H')
254 let $TZ = 'UTC' | let utc = strftime('%H')
255
256 " Those hours should be two bytes long, and should not be the same; if they
257 " are, a tzset(3) call may have failed somewhere
258 call assert_equal(strlen(est), 2)
259 call assert_equal(strlen(utc), 2)
Bram Moolenaar87652a72019-06-18 23:07:37 +0200260 " TODO: this fails on MS-Windows
261 if has('unix')
262 call assert_notequal(est, utc)
263 endif
Bram Moolenaardb517302019-06-18 22:53:24 +0200264
265 " If we cached a timezone value, put it back, otherwise clear it
266 if exists('tz')
267 let $TZ = tz
268 else
269 unlet $TZ
270 endif
Bram Moolenaar10455d42019-11-21 15:36:18 +0100271endfunc
Bram Moolenaardb517302019-06-18 22:53:24 +0200272
Bram Moolenaar10455d42019-11-21 15:36:18 +0100273func Test_strptime()
274 CheckFunction strptime
275
276 if exists('$TZ')
277 let tz = $TZ
278 endif
279 let $TZ = 'UTC'
280
Bram Moolenaar9a838fe2019-12-06 12:45:01 +0100281 call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
Bram Moolenaar10455d42019-11-21 15:36:18 +0100282
Bram Moolenaarea1233f2020-06-10 16:54:13 +0200283 " Force DST and check that it's considered
284 let $TZ = 'WINTER0SUMMER,J1,J365'
285 call assert_equal(1484653763 - 3600, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
286
Bram Moolenaar10455d42019-11-21 15:36:18 +0100287 call assert_fails('call strptime()', 'E119:')
288 call assert_fails('call strptime("xxx")', 'E119:')
289 call assert_equal(0, strptime("%Y", ''))
290 call assert_equal(0, strptime("%Y", "xxx"))
291
292 if exists('tz')
293 let $TZ = tz
294 else
295 unlet $TZ
296 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100297endfunc
298
Bram Moolenaardce1e892019-02-10 23:18:53 +0100299func Test_resolve_unix()
Bram Moolenaar26109902018-10-06 15:43:17 +0200300 if !has('unix')
301 return
302 endif
303
304 " Xlink1 -> Xlink2
305 " Xlink2 -> Xlink3
306 silent !ln -s -f Xlink2 Xlink1
307 silent !ln -s -f Xlink3 Xlink2
308 call assert_equal('Xlink3', resolve('Xlink1'))
309 call assert_equal('./Xlink3', resolve('./Xlink1'))
310 call assert_equal('Xlink3/', resolve('Xlink2/'))
311 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
312 "call assert_equal('Xlink3/', resolve('Xlink1/'))
313 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
314 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
315 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
316
317 " Test resolve() with a symlink cycle.
318 " Xlink1 -> Xlink2
319 " Xlink2 -> Xlink3
320 " Xlink3 -> Xlink1
321 silent !ln -s -f Xlink1 Xlink3
322 call assert_fails('call resolve("Xlink1")', 'E655:')
323 call assert_fails('call resolve("./Xlink1")', 'E655:')
324 call assert_fails('call resolve("Xlink2")', 'E655:')
325 call assert_fails('call resolve("Xlink3")', 'E655:')
326 call delete('Xlink1')
327 call delete('Xlink2')
328 call delete('Xlink3')
329
330 silent !ln -s -f Xdir//Xfile Xlink
331 call assert_equal('Xdir/Xfile', resolve('Xlink'))
332 call delete('Xlink')
333
334 silent !ln -s -f Xlink2/ Xlink1
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200335 call assert_equal('Xlink2', 'Xlink1'->resolve())
Bram Moolenaar26109902018-10-06 15:43:17 +0200336 call assert_equal('Xlink2/', resolve('Xlink1/'))
337 call delete('Xlink1')
338
339 silent !ln -s -f ./Xlink2 Xlink1
340 call assert_equal('Xlink2', resolve('Xlink1'))
341 call assert_equal('./Xlink2', resolve('./Xlink1'))
342 call delete('Xlink1')
343endfunc
344
Bram Moolenaardce1e892019-02-10 23:18:53 +0100345func s:normalize_fname(fname)
346 let ret = substitute(a:fname, '\', '/', 'g')
347 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200348 return ret->tolower()
Bram Moolenaardce1e892019-02-10 23:18:53 +0100349endfunc
350
351func Test_resolve_win32()
352 if !has('win32')
353 return
354 endif
355
356 " test for shortcut file
357 if executable('cscript')
358 new Xfile
359 wq
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200360 let lines =<< trim END
361 Set fs = CreateObject("Scripting.FileSystemObject")
362 Set ws = WScript.CreateObject("WScript.Shell")
363 Set shortcut = ws.CreateShortcut("Xlink.lnk")
364 shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")
365 shortcut.Save
366 END
367 call writefile(lines, 'link.vbs')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100368 silent !cscript link.vbs
369 call delete('link.vbs')
370 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
371 call delete('Xfile')
372
373 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
374 call delete('Xlink.lnk')
375 else
376 echomsg 'skipped test for shortcut file'
377 endif
378
379 " remove files
380 call delete('Xlink')
381 call delete('Xdir', 'd')
382 call delete('Xfile')
383
384 " test for symbolic link to a file
385 new Xfile
386 wq
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200387 call assert_equal('Xfile', resolve('Xfile'))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100388 silent !mklink Xlink Xfile
389 if !v:shell_error
390 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink')))
391 call delete('Xlink')
392 else
393 echomsg 'skipped test for symbolic link to a file'
394 endif
395 call delete('Xfile')
396
397 " test for junction to a directory
398 call mkdir('Xdir')
399 silent !mklink /J Xlink Xdir
400 if !v:shell_error
401 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
402
403 call delete('Xdir', 'd')
404
405 " test for junction already removed
406 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
407 call delete('Xlink')
408 else
409 echomsg 'skipped test for junction to a directory'
410 call delete('Xdir', 'd')
411 endif
412
413 " test for symbolic link to a directory
414 call mkdir('Xdir')
415 silent !mklink /D Xlink Xdir
416 if !v:shell_error
417 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
418
419 call delete('Xdir', 'd')
420
421 " test for symbolic link already removed
422 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
423 call delete('Xlink')
424 else
425 echomsg 'skipped test for symbolic link to a directory'
426 call delete('Xdir', 'd')
427 endif
428
429 " test for buffer name
430 new Xfile
431 wq
432 silent !mklink Xlink Xfile
433 if !v:shell_error
434 edit Xlink
435 call assert_equal('Xlink', bufname('%'))
436 call delete('Xlink')
437 bw!
438 else
439 echomsg 'skipped test for buffer name'
440 endif
441 call delete('Xfile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200442
443 " test for reparse point
444 call mkdir('Xdir')
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200445 call assert_equal('Xdir', resolve('Xdir'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200446 silent !mklink /D Xdirlink Xdir
447 if !v:shell_error
448 w Xdir/text.txt
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200449 call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200450 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
451 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200452 call delete('Xdirlink')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200453 else
454 echomsg 'skipped test for reparse point'
455 endif
456
457 call delete('Xdir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100458endfunc
459
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100460func Test_simplify()
461 call assert_equal('', simplify(''))
462 call assert_equal('/', simplify('/'))
463 call assert_equal('/', simplify('/.'))
464 call assert_equal('/', simplify('/..'))
465 call assert_equal('/...', simplify('/...'))
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +0200466 call assert_equal('//path', simplify('//path'))
467 call assert_equal('/path', simplify('///path'))
468 call assert_equal('/path', simplify('////path'))
469
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200470 call assert_equal('./dir/file', './dir/file'->simplify())
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100471 call assert_equal('./dir/file', simplify('.///dir//file'))
472 call assert_equal('./dir/file', simplify('./dir/./file'))
473 call assert_equal('./file', simplify('./dir/../file'))
474 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
475 call assert_equal('./file', simplify('dir/.././file'))
476
477 call assert_fails('call simplify({->0})', 'E729:')
478 call assert_fails('call simplify([])', 'E730:')
479 call assert_fails('call simplify({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100480 if has('float')
481 call assert_fails('call simplify(1.2)', 'E806:')
482 endif
Bram Moolenaar08243d22017-01-10 16:12:29 +0100483endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100484
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200485func Test_pathshorten()
486 call assert_equal('', pathshorten(''))
487 call assert_equal('foo', pathshorten('foo'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200488 call assert_equal('/foo', '/foo'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200489 call assert_equal('f/', pathshorten('foo/'))
490 call assert_equal('f/bar', pathshorten('foo/bar'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200491 call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200492 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
493 call assert_equal('.f/bar', pathshorten('.foo/bar'))
494 call assert_equal('~f/bar', pathshorten('~foo/bar'))
495 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
496 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
497 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200498 call assert_fails('call pathshorten([])', 'E730:')
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200499endfunc
500
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100501func Test_strpart()
502 call assert_equal('de', strpart('abcdefg', 3, 2))
503 call assert_equal('ab', strpart('abcdefg', -2, 4))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200504 call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100505 call assert_equal('fg', strpart('abcdefg', 5, 4))
506 call assert_equal('defg', strpart('abcdefg', 3))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100507 call assert_equal('', strpart('abcdefg', 10))
508 call assert_fails("let s=strpart('abcdef', [])", 'E745:')
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100509
Bram Moolenaar30276f22019-01-24 17:59:39 +0100510 call assert_equal('lép', strpart('éléphant', 2, 4))
511 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100512endfunc
513
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100514func Test_tolower()
515 call assert_equal("", tolower(""))
516
517 " Test with all printable ASCII characters.
518 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
519 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
520
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100521 " Test with a few uppercase diacritics.
522 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
523 call assert_equal("bḃḇ", tolower("BḂḆ"))
524 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
525 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
526 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
527 call assert_equal("fḟ ", tolower("FḞ "))
528 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
529 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
530 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
531 call assert_equal("jĵ", tolower("JĴ"))
532 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
533 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
534 call assert_equal("mḿṁ", tolower("MḾṀ"))
535 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
536 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
537 call assert_equal("pṕṗ", tolower("PṔṖ"))
538 call assert_equal("q", tolower("Q"))
539 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
540 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
541 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
542 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
543 call assert_equal("vṽ", tolower("VṼ"))
544 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
545 call assert_equal("xẋẍ", tolower("XẊẌ"))
546 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
547 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
548
549 " Test with a few lowercase diacritics, which should remain unchanged.
550 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
551 call assert_equal("bḃḇ", tolower("bḃḇ"))
552 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
553 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
554 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
555 call assert_equal("fḟ", tolower("fḟ"))
556 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
557 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
558 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
559 call assert_equal("jĵǰ", tolower("jĵǰ"))
560 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
561 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
562 call assert_equal("mḿṁ ", tolower("mḿṁ "))
563 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
564 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
565 call assert_equal("pṕṗ", tolower("pṕṗ"))
566 call assert_equal("q", tolower("q"))
567 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
568 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
569 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
570 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
571 call assert_equal("vṽ", tolower("vṽ"))
572 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
573 call assert_equal("ẋẍ", tolower("ẋẍ"))
574 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
575 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
576
577 " According to https://twitter.com/jifa/status/625776454479970304
578 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
579 " in length (2 to 3 bytes) when lowercased. So let's test them.
580 call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100581
582 " This call to tolower with invalid utf8 sequence used to cause access to
583 " invalid memory.
584 call tolower("\xC0\x80\xC0")
585 call tolower("123\xC0\x80\xC0")
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200586
587 " Test in latin1 encoding
588 let save_enc = &encoding
589 set encoding=latin1
590 call assert_equal("abc", tolower("ABC"))
591 let &encoding = save_enc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100592endfunc
593
594func Test_toupper()
595 call assert_equal("", toupper(""))
596
597 " Test with all printable ASCII characters.
598 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
599 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
600
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100601 " Test with a few lowercase diacritics.
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200602 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100603 call assert_equal("BḂḆ", toupper("bḃḇ"))
604 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
605 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
606 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
607 call assert_equal("FḞ", toupper("fḟ"))
608 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
609 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
610 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
611 call assert_equal("JĴǰ", toupper("jĵǰ"))
612 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
613 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
614 call assert_equal("MḾṀ ", toupper("mḿṁ "))
615 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
616 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
617 call assert_equal("PṔṖ", toupper("pṕṗ"))
618 call assert_equal("Q", toupper("q"))
619 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
620 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
621 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
622 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
623 call assert_equal("VṼ", toupper("vṽ"))
624 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
625 call assert_equal("ẊẌ", toupper("ẋẍ"))
626 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
627 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
628
629 " Test that uppercase diacritics, which should remain unchanged.
630 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
631 call assert_equal("BḂḆ", toupper("BḂḆ"))
632 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
633 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
634 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
635 call assert_equal("FḞ ", toupper("FḞ "))
636 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
637 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
638 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
639 call assert_equal("JĴ", toupper("JĴ"))
640 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
641 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
642 call assert_equal("MḾṀ", toupper("MḾṀ"))
643 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
644 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
645 call assert_equal("PṔṖ", toupper("PṔṖ"))
646 call assert_equal("Q", toupper("Q"))
647 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
648 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
649 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
650 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
651 call assert_equal("VṼ", toupper("VṼ"))
652 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
653 call assert_equal("XẊẌ", toupper("XẊẌ"))
654 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
655 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
656
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100657 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100658
659 " This call to toupper with invalid utf8 sequence used to cause access to
660 " invalid memory.
661 call toupper("\xC0\x80\xC0")
662 call toupper("123\xC0\x80\xC0")
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200663
664 " Test in latin1 encoding
665 let save_enc = &encoding
666 set encoding=latin1
667 call assert_equal("ABC", toupper("abc"))
668 let &encoding = save_enc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100669endfunc
670
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200671func Test_tr()
672 call assert_equal('foo', tr('bar', 'bar', 'foo'))
673 call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100674 call assert_fails("let s=tr([], 'abc', 'def')", 'E730:')
675 call assert_fails("let s=tr('abc', [], 'def')", 'E730:')
676 call assert_fails("let s=tr('abc', 'abc', [])", 'E730:')
677 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
678 set encoding=latin1
679 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
680 call assert_equal('hEllO', tr('hello', 'eo', 'EO'))
681 call assert_equal('hello', tr('hello', 'xy', 'ab'))
682 set encoding=utf8
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200683endfunc
684
Bram Moolenaare90858d2017-02-01 17:24:34 +0100685" Tests for the mode() function
686let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100687func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100688 let g:current_modes = mode(0) . '-' . mode(1)
689 return ''
690endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100691
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200692" Test for the mode() function
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100693func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100694 new
695 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
696
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100697 " Only complete from the current buffer.
698 set complete=.
699
Bram Moolenaare90858d2017-02-01 17:24:34 +0100700 inoremap <F2> <C-R>=Save_mode()<CR>
701
702 normal! 3G
703 exe "normal i\<F2>\<Esc>"
704 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100705 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100706 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
707 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100708 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100709 exe "normal iBro\<C-P>\<F2>\<Esc>u"
710 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100711 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100712 exe "normal iBa\<C-X>\<F2>\<Esc>u"
713 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100714 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100715 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
716 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100717 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100718 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
719 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100720 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100721 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
722 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100723 " i_CTRL-X CTRL-L: Multiple matches
724 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
725 call assert_equal('i-ic', g:current_modes)
726 " i_CTRL-X CTRL-L: Single match
727 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
728 call assert_equal('i-ic', g:current_modes)
729 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100730 exe "normal iCom\<C-P>\<F2>\<Esc>u"
731 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100732 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100733 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
734 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100735 " i_CTRL-X CTRL-L: No match
736 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
737 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100738
Bram Moolenaare971df32017-02-05 14:15:29 +0100739 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100740 exe "normal RBa\<C-P>\<F2>\<Esc>u"
741 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100742 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100743 exe "normal RBro\<C-P>\<F2>\<Esc>u"
744 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100745 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100746 exe "normal RBa\<C-X>\<F2>\<Esc>u"
747 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100748 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100749 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
750 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100751 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100752 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
753 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100754 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100755 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
756 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100757 " R_CTRL-X CTRL-L: Multiple matches
758 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
759 call assert_equal('R-Rc', g:current_modes)
760 " R_CTRL-X CTRL-L: Single match
761 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
762 call assert_equal('R-Rc', g:current_modes)
763 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100764 exe "normal RCom\<C-P>\<F2>\<Esc>u"
765 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100766 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100767 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
768 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100769 " R_CTRL-X CTRL-L: No match
770 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
771 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100772
Bram Moolenaara1449832019-09-01 20:16:52 +0200773 call assert_equal('n', 0->mode())
774 call assert_equal('n', 1->mode())
Bram Moolenaare90858d2017-02-01 17:24:34 +0100775
Bram Moolenaar612cc382018-07-29 15:34:26 +0200776 " i_CTRL-O
777 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
778 call assert_equal("n-niI", g:current_modes)
779
780 " R_CTRL-O
781 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
782 call assert_equal("n-niR", g:current_modes)
783
784 " gR_CTRL-O
785 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
786 call assert_equal("n-niV", g:current_modes)
787
Bram Moolenaare90858d2017-02-01 17:24:34 +0100788 " How to test operator-pending mode?
789
790 call feedkeys("v", 'xt')
791 call assert_equal('v', mode())
792 call assert_equal('v', mode(1))
793 call feedkeys("\<Esc>V", 'xt')
794 call assert_equal('V', mode())
795 call assert_equal('V', mode(1))
796 call feedkeys("\<Esc>\<C-V>", 'xt')
797 call assert_equal("\<C-V>", mode())
798 call assert_equal("\<C-V>", mode(1))
799 call feedkeys("\<Esc>", 'xt')
800
801 call feedkeys("gh", 'xt')
802 call assert_equal('s', mode())
803 call assert_equal('s', mode(1))
804 call feedkeys("\<Esc>gH", 'xt')
805 call assert_equal('S', mode())
806 call assert_equal('S', mode(1))
807 call feedkeys("\<Esc>g\<C-H>", 'xt')
808 call assert_equal("\<C-S>", mode())
809 call assert_equal("\<C-S>", mode(1))
810 call feedkeys("\<Esc>", 'xt')
811
812 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
813 call assert_equal('c-c', g:current_modes)
814 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
815 call assert_equal('c-cv', g:current_modes)
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200816 call feedkeys("Qcall Save_mode()\<CR>vi\<CR>", 'xt')
817 call assert_equal('c-ce', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100818 " How to test Ex mode?
819
820 bwipe!
821 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100822 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100823endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100824
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200825" Test for append()
Bram Moolenaard2007022019-08-27 21:56:06 +0200826func Test_append()
827 enew!
828 split
829 call append(0, ["foo"])
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200830 call append(1, [])
831 call append(1, test_null_list())
832 call assert_equal(['foo', ''], getline(1, '$'))
Bram Moolenaard2007022019-08-27 21:56:06 +0200833 split
834 only
835 undo
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200836 undo
Bram Moolenaar08f41572020-04-20 16:50:00 +0200837
838 " Using $ instead of '$' must give an error
839 call assert_fails("call append($, 'foobar')", 'E116:')
Bram Moolenaard2007022019-08-27 21:56:06 +0200840endfunc
841
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200842" Test for setline()
843func Test_setline()
844 new
845 call setline(0, ["foo"])
846 call setline(0, [])
847 call setline(0, test_null_list())
848 call setline(1, ["bar"])
849 call setline(1, [])
850 call setline(1, test_null_list())
851 call setline(2, [])
852 call setline(2, test_null_list())
853 call setline(3, [])
854 call setline(3, test_null_list())
855 call setline(2, ["baz"])
856 call assert_equal(['bar', 'baz'], getline(1, '$'))
857 close!
858endfunc
859
Bram Moolenaar79518e22017-02-17 16:31:35 +0100860func Test_getbufvar()
861 let bnr = bufnr('%')
862 let b:var_num = '1234'
863 let def_num = '5678'
864 call assert_equal('1234', getbufvar(bnr, 'var_num'))
865 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
866
867 let bd = getbufvar(bnr, '')
868 call assert_equal('1234', bd['var_num'])
869 call assert_true(exists("bd['changedtick']"))
870 call assert_equal(2, len(bd))
871
872 let bd2 = getbufvar(bnr, '', def_num)
873 call assert_equal(bd, bd2)
874
875 unlet b:var_num
876 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
877 call assert_equal('', getbufvar(bnr, 'var_num'))
878
879 let bd = getbufvar(bnr, '')
880 call assert_equal(1, len(bd))
881 let bd = getbufvar(bnr, '',def_num)
882 call assert_equal(1, len(bd))
883
Bram Moolenaar4520d442017-03-19 16:09:46 +0100884 call assert_equal('', getbufvar(9999, ''))
885 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100886 unlet def_num
887
Bram Moolenaar507647d2017-02-17 16:43:49 +0100888 call assert_equal(0, getbufvar(bnr, '&autoindent'))
889 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100890
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100891 " Set and get a buffer-local variable
892 call setbufvar(bnr, 'bufvar_test', ['one', 'two'])
893 call assert_equal(['one', 'two'], getbufvar(bnr, 'bufvar_test'))
894
Bram Moolenaar79518e22017-02-17 16:31:35 +0100895 " Open new window with forced option values
896 set fileformats=unix,dos
897 new ++ff=dos ++bin ++enc=iso-8859-2
898 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
899 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
900 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
901 close
902
Bram Moolenaar52592752020-04-03 18:43:35 +0200903 " Get the b: dict.
904 let b:testvar = 'one'
905 new
906 let b:testvar = 'two'
907 let thebuf = bufnr()
908 wincmd w
909 call assert_equal('two', getbufvar(thebuf, 'testvar'))
910 call assert_equal('two', getbufvar(thebuf, '').testvar)
911 bwipe!
912
Bram Moolenaar79518e22017-02-17 16:31:35 +0100913 set fileformats&
914endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100915
Bram Moolenaar41042f32017-03-09 12:09:32 +0100916func Test_last_buffer_nr()
917 call assert_equal(bufnr('$'), last_buffer_nr())
918endfunc
919
920func Test_stridx()
921 call assert_equal(-1, stridx('', 'l'))
922 call assert_equal(0, stridx('', ''))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200923 call assert_equal(0, 'hello'->stridx(''))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100924 call assert_equal(-1, stridx('hello', 'L'))
925 call assert_equal(2, stridx('hello', 'l', -1))
926 call assert_equal(2, stridx('hello', 'l', 0))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200927 call assert_equal(2, 'hello'->stridx('l', 1))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100928 call assert_equal(3, stridx('hello', 'l', 3))
929 call assert_equal(-1, stridx('hello', 'l', 4))
930 call assert_equal(-1, stridx('hello', 'l', 10))
931 call assert_equal(2, stridx('hello', 'll'))
932 call assert_equal(-1, stridx('hello', 'hello world'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100933 call assert_fails("let n=stridx('hello', [])", 'E730:')
934 call assert_fails("let n=stridx([], 'l')", 'E730:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100935endfunc
936
937func Test_strridx()
938 call assert_equal(-1, strridx('', 'l'))
939 call assert_equal(0, strridx('', ''))
940 call assert_equal(5, strridx('hello', ''))
941 call assert_equal(-1, strridx('hello', 'L'))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200942 call assert_equal(3, 'hello'->strridx('l'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100943 call assert_equal(3, strridx('hello', 'l', 10))
944 call assert_equal(3, strridx('hello', 'l', 3))
945 call assert_equal(2, strridx('hello', 'l', 2))
946 call assert_equal(-1, strridx('hello', 'l', 1))
947 call assert_equal(-1, strridx('hello', 'l', 0))
948 call assert_equal(-1, strridx('hello', 'l', -1))
949 call assert_equal(2, strridx('hello', 'll'))
950 call assert_equal(-1, strridx('hello', 'hello world'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100951 call assert_fails("let n=strridx('hello', [])", 'E730:')
952 call assert_fails("let n=strridx([], 'l')", 'E730:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100953endfunc
954
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200955func Test_match_func()
956 call assert_equal(4, match('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200957 call assert_equal(4, 'testing'->match('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200958 call assert_equal(-1, match('testing', 'ing', 5))
959 call assert_equal(-1, match('testing', 'ing', 8))
960 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
961 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100962 call assert_fails("let x=match('vim', [])", 'E730:')
963 call assert_equal(3, match(['a', 'b', 'c', 'a'], 'a', 1))
964 call assert_equal(-1, match(['a', 'b', 'c', 'a'], 'a', 5))
965 call assert_equal(4, match('testing', 'ing', -1))
966 call assert_fails("let x=match('testing', 'ing', 0, [])", 'E745:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200967 call assert_equal(-1, match(test_null_list(), 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200968endfunc
969
Bram Moolenaar41042f32017-03-09 12:09:32 +0100970func Test_matchend()
971 call assert_equal(7, matchend('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200972 call assert_equal(7, 'testing'->matchend('ing', 2))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100973 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200974 call assert_equal(-1, matchend('testing', 'ing', 8))
975 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
976 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
977endfunc
978
979func Test_matchlist()
980 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200981 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200982 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
983endfunc
984
985func Test_matchstr()
986 call assert_equal('ing', matchstr('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200987 call assert_equal('ing', 'testing'->matchstr('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200988 call assert_equal('', matchstr('testing', 'ing', 5))
989 call assert_equal('', matchstr('testing', 'ing', 8))
990 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
991 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
992endfunc
993
994func Test_matchstrpos()
995 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200996 call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200997 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
998 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
999 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
1000 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001001 call assert_equal(['', -1, -1], matchstrpos(test_null_list(), '\a'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001002endfunc
1003
1004func Test_nextnonblank_prevnonblank()
1005 new
1006insert
1007This
1008
1009
1010is
1011
1012a
1013Test
1014.
1015 call assert_equal(0, nextnonblank(-1))
1016 call assert_equal(0, nextnonblank(0))
1017 call assert_equal(1, nextnonblank(1))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001018 call assert_equal(4, 2->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001019 call assert_equal(4, nextnonblank(3))
1020 call assert_equal(4, nextnonblank(4))
1021 call assert_equal(6, nextnonblank(5))
1022 call assert_equal(6, nextnonblank(6))
1023 call assert_equal(7, nextnonblank(7))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001024 call assert_equal(0, 8->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001025
1026 call assert_equal(0, prevnonblank(-1))
1027 call assert_equal(0, prevnonblank(0))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001028 call assert_equal(1, 1->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001029 call assert_equal(1, prevnonblank(2))
1030 call assert_equal(1, prevnonblank(3))
1031 call assert_equal(4, prevnonblank(4))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001032 call assert_equal(4, 5->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001033 call assert_equal(6, prevnonblank(6))
1034 call assert_equal(7, prevnonblank(7))
1035 call assert_equal(0, prevnonblank(8))
1036 bw!
1037endfunc
1038
1039func Test_byte2line_line2byte()
1040 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +02001041 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +01001042 call setline(1, ['a', 'bc', 'd'])
1043
1044 set fileformat=unix
1045 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
1046 \ map(range(-1, 8), 'byte2line(v:val)'))
1047 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
1048 \ map(range(-1, 5), 'line2byte(v:val)'))
1049
1050 set fileformat=mac
1051 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001052 \ map(range(-1, 8), 'v:val->byte2line()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001053 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
Bram Moolenaar02b31112019-08-31 22:16:38 +02001054 \ map(range(-1, 5), 'v:val->line2byte()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001055
1056 set fileformat=dos
1057 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
1058 \ map(range(-1, 11), 'byte2line(v:val)'))
1059 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
1060 \ map(range(-1, 5), 'line2byte(v:val)'))
1061
Bram Moolenaarc26f7c62018-08-20 22:53:04 +02001062 bw!
1063 set noendofline nofixendofline
1064 normal a-
1065 for ff in ["unix", "mac", "dos"]
1066 let &fileformat = ff
1067 call assert_equal(1, line2byte(1))
1068 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
1069 endfor
1070
1071 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +01001072 bw!
1073endfunc
1074
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001075" Test for byteidx() and byteidxcomp() functions
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001076func Test_byteidx()
1077 let a = '.é.' " one char of two bytes
1078 call assert_equal(0, byteidx(a, 0))
1079 call assert_equal(0, byteidxcomp(a, 0))
1080 call assert_equal(1, byteidx(a, 1))
1081 call assert_equal(1, byteidxcomp(a, 1))
1082 call assert_equal(3, byteidx(a, 2))
1083 call assert_equal(3, byteidxcomp(a, 2))
1084 call assert_equal(4, byteidx(a, 3))
1085 call assert_equal(4, byteidxcomp(a, 3))
1086 call assert_equal(-1, byteidx(a, 4))
1087 call assert_equal(-1, byteidxcomp(a, 4))
1088
1089 let b = '.é.' " normal e with composing char
1090 call assert_equal(0, b->byteidx(0))
1091 call assert_equal(1, b->byteidx(1))
1092 call assert_equal(4, b->byteidx(2))
1093 call assert_equal(5, b->byteidx(3))
1094 call assert_equal(-1, b->byteidx(4))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001095 call assert_fails("call byteidx([], 0)", 'E730:')
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001096
1097 call assert_equal(0, b->byteidxcomp(0))
1098 call assert_equal(1, b->byteidxcomp(1))
1099 call assert_equal(2, b->byteidxcomp(2))
1100 call assert_equal(4, b->byteidxcomp(3))
1101 call assert_equal(5, b->byteidxcomp(4))
1102 call assert_equal(-1, b->byteidxcomp(5))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001103 call assert_fails("call byteidxcomp([], 0)", 'E730:')
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001104endfunc
1105
Bram Moolenaar41042f32017-03-09 12:09:32 +01001106func Test_count()
1107 let l = ['a', 'a', 'A', 'b']
1108 call assert_equal(2, count(l, 'a'))
1109 call assert_equal(1, count(l, 'A'))
1110 call assert_equal(1, count(l, 'b'))
1111 call assert_equal(0, count(l, 'B'))
1112
1113 call assert_equal(2, count(l, 'a', 0))
1114 call assert_equal(1, count(l, 'A', 0))
1115 call assert_equal(1, count(l, 'b', 0))
1116 call assert_equal(0, count(l, 'B', 0))
1117
1118 call assert_equal(3, count(l, 'a', 1))
1119 call assert_equal(3, count(l, 'A', 1))
1120 call assert_equal(1, count(l, 'b', 1))
1121 call assert_equal(1, count(l, 'B', 1))
1122 call assert_equal(0, count(l, 'c', 1))
1123
1124 call assert_equal(1, count(l, 'a', 0, 1))
1125 call assert_equal(2, count(l, 'a', 1, 1))
1126 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02001127 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001128
1129 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
1130 call assert_equal(2, count(d, 'a'))
1131 call assert_equal(1, count(d, 'A'))
1132 call assert_equal(1, count(d, 'b'))
1133 call assert_equal(0, count(d, 'B'))
1134
1135 call assert_equal(2, count(d, 'a', 0))
1136 call assert_equal(1, count(d, 'A', 0))
1137 call assert_equal(1, count(d, 'b', 0))
1138 call assert_equal(0, count(d, 'B', 0))
1139
1140 call assert_equal(3, count(d, 'a', 1))
1141 call assert_equal(3, count(d, 'A', 1))
1142 call assert_equal(1, count(d, 'b', 1))
1143 call assert_equal(1, count(d, 'B', 1))
1144 call assert_equal(0, count(d, 'c', 1))
1145
1146 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +02001147
1148 call assert_equal(0, count("foo", "bar"))
1149 call assert_equal(1, count("foo", "oo"))
1150 call assert_equal(2, count("foo", "o"))
1151 call assert_equal(0, count("foo", "O"))
1152 call assert_equal(2, count("foo", "O", 1))
1153 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +01001154 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001155
1156 call assert_fails('call count(0, 0)', 'E712:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001157endfunc
1158
1159func Test_changenr()
1160 new Xchangenr
1161 call assert_equal(0, changenr())
1162 norm ifoo
1163 call assert_equal(1, changenr())
1164 set undolevels=10
1165 norm Sbar
1166 call assert_equal(2, changenr())
1167 undo
1168 call assert_equal(1, changenr())
1169 redo
1170 call assert_equal(2, changenr())
1171 bw!
1172 set undolevels&
1173endfunc
1174
1175func Test_filewritable()
1176 new Xfilewritable
1177 write!
1178 call assert_equal(1, filewritable('Xfilewritable'))
1179
1180 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1181 call assert_equal(0, filewritable('Xfilewritable'))
1182
1183 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001184 call assert_equal(1, 'Xfilewritable'->filewritable())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001185
1186 call assert_equal(0, filewritable('doesnotexist'))
1187
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +02001188 call mkdir('Xdir')
1189 call assert_equal(2, filewritable('Xdir'))
1190 call delete('Xdir', 'd')
1191
Bram Moolenaar41042f32017-03-09 12:09:32 +01001192 call delete('Xfilewritable')
1193 bw!
1194endfunc
1195
Bram Moolenaar82956662018-10-06 15:18:45 +02001196func Test_Executable()
1197 if has('win32')
1198 call assert_equal(1, executable('notepad'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001199 call assert_equal(1, 'notepad.exe'->executable())
Bram Moolenaar82956662018-10-06 15:18:45 +02001200 call assert_equal(0, executable('notepad.exe.exe'))
1201 call assert_equal(0, executable('shell32.dll'))
1202 call assert_equal(0, executable('win.ini'))
Bram Moolenaar95da1362020-05-30 18:37:55 +02001203
1204 " get "notepad" path and remove the leading drive and sep. (ex. 'C:\')
1205 let notepadcmd = exepath('notepad.exe')
1206 let driveroot = notepadcmd[:2]
1207 let notepadcmd = notepadcmd[3:]
1208 new
1209 " check that the relative path works in /
1210 execute 'lcd' driveroot
1211 call assert_equal(1, executable(notepadcmd))
1212 call assert_equal(driveroot .. notepadcmd, notepadcmd->exepath())
1213 bwipe
1214
1215 " create "notepad.bat"
1216 call mkdir('Xdir')
1217 let notepadbat = fnamemodify('Xdir/notepad.bat', ':p')
1218 call writefile([], notepadbat)
1219 new
1220 " check that the path and the pathext order is valid
1221 lcd Xdir
1222 let [pathext, $PATHEXT] = [$PATHEXT, '.com;.exe;.bat;.cmd']
1223 call assert_equal(notepadbat, exepath('notepad'))
1224 let $PATHEXT = pathext
1225 bwipe
1226 eval 'Xdir'->delete('rf')
Bram Moolenaar82956662018-10-06 15:18:45 +02001227 elseif has('unix')
Bram Moolenaara4208962019-08-24 20:50:19 +02001228 call assert_equal(1, 'cat'->executable())
Bram Moolenaara05a0d32018-10-07 18:43:05 +02001229 call assert_equal(0, executable('nodogshere'))
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001230
1231 " get "cat" path and remove the leading /
1232 let catcmd = exepath('cat')[1:]
1233 new
Bram Moolenaara4208962019-08-24 20:50:19 +02001234 " check that the relative path works in /
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001235 lcd /
1236 call assert_equal(1, executable(catcmd))
Bram Moolenaara4208962019-08-24 20:50:19 +02001237 call assert_equal('/' .. catcmd, catcmd->exepath())
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001238 bwipe
Bram Moolenaar82956662018-10-06 15:18:45 +02001239 endif
1240endfunc
1241
Bram Moolenaar86621892019-03-30 21:51:28 +01001242func Test_executable_longname()
1243 if !has('win32')
1244 return
1245 endif
1246
1247 let fname = 'X' . repeat('あ', 200) . '.bat'
1248 call writefile([], fname)
1249 call assert_equal(1, executable(fname))
1250 call delete(fname)
1251endfunc
1252
Bram Moolenaar41042f32017-03-09 12:09:32 +01001253func Test_hostname()
1254 let hostname_vim = hostname()
1255 if has('unix')
1256 let hostname_system = systemlist('uname -n')[0]
1257 call assert_equal(hostname_vim, hostname_system)
1258 endif
1259endfunc
1260
1261func Test_getpid()
1262 " getpid() always returns the same value within a vim instance.
1263 call assert_equal(getpid(), getpid())
1264 if has('unix')
1265 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1266 endif
1267endfunc
1268
1269func Test_hlexists()
1270 call assert_equal(0, hlexists('does_not_exist'))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001271 call assert_equal(0, 'Number'->hlexists())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001272 call assert_equal(0, highlight_exists('does_not_exist'))
1273 call assert_equal(0, highlight_exists('Number'))
1274 syntax on
1275 call assert_equal(0, hlexists('does_not_exist'))
1276 call assert_equal(1, hlexists('Number'))
1277 call assert_equal(0, highlight_exists('does_not_exist'))
1278 call assert_equal(1, highlight_exists('Number'))
1279 syntax off
1280endfunc
1281
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001282" Test for the col() function
Bram Moolenaar41042f32017-03-09 12:09:32 +01001283func Test_col()
1284 new
1285 call setline(1, 'abcdef')
1286 norm gg4|mx6|mY2|
1287 call assert_equal(2, col('.'))
1288 call assert_equal(7, col('$'))
Bram Moolenaar8b633132020-03-20 18:20:51 +01001289 call assert_equal(2, col('v'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001290 call assert_equal(4, col("'x"))
1291 call assert_equal(6, col("'Y"))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001292 call assert_equal(2, [1, 2]->col())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001293 call assert_equal(7, col([1, '$']))
1294
1295 call assert_equal(0, col(''))
1296 call assert_equal(0, col('x'))
1297 call assert_equal(0, col([2, '$']))
1298 call assert_equal(0, col([1, 100]))
1299 call assert_equal(0, col([1]))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001300 call assert_equal(0, col(test_null_list()))
1301 call assert_fails('let c = col({})', 'E731:')
Bram Moolenaar8b633132020-03-20 18:20:51 +01001302
1303 " test for getting the visual start column
1304 func T()
1305 let g:Vcol = col('v')
1306 return ''
1307 endfunc
1308 let g:Vcol = 0
1309 xmap <expr> <F2> T()
1310 exe "normal gg3|ve\<F2>"
1311 call assert_equal(3, g:Vcol)
1312 xunmap <F2>
1313 delfunc T
1314
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001315 " Test for the visual line start and end marks '< and '>
1316 call setline(1, ['one', 'one two', 'one two three'])
1317 "normal! ggVG
1318 call feedkeys("ggVG\<Esc>", 'xt')
1319 call assert_equal(1, col("'<"))
1320 call assert_equal(14, col("'>"))
1321 " Delete the last line of the visually selected region
1322 $d
1323 call assert_notequal(14, col("'>"))
1324
1325 " Test with 'virtualedit'
1326 set virtualedit=all
1327 call cursor(1, 10)
1328 call assert_equal(4, col('.'))
1329 set virtualedit&
1330
Bram Moolenaar41042f32017-03-09 12:09:32 +01001331 bw!
1332endfunc
1333
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001334" Test for input()
1335func Test_input_func()
1336 " Test for prompt with multiple lines
1337 redir => v
1338 call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt')
1339 redir END
1340 call assert_equal("B", c)
1341 call assert_equal(['A', 'B', 'C'], split(v, "\n"))
1342
1343 " Test for default value
1344 call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt')
1345 call assert_equal('red', c)
1346
1347 " Test for completion at the input prompt
1348 func! Tcomplete(arglead, cmdline, pos)
1349 return "item1\nitem2\nitem3"
1350 endfunc
1351 call feedkeys(":let c = input('Q? ', '' , 'custom,Tcomplete')\<CR>"
1352 \ .. "\<C-A>\<CR>", 'xt')
1353 delfunc Tcomplete
1354 call assert_equal('item1 item2 item3', c)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001355
1356 call assert_fails("call input('F:', '', 'invalid')", 'E180:')
1357 call assert_fails("call input('F:', '', [])", 'E730:')
1358endfunc
1359
1360" Test for the inputdialog() function
1361func Test_inputdialog()
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02001362 set timeout timeoutlen=10
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001363 if has('gui_running')
1364 call assert_fails('let v=inputdialog([], "xx")', 'E730:')
1365 call assert_fails('let v=inputdialog("Q", [])', 'E730:')
1366 else
1367 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt')
1368 call assert_equal('xx', v)
1369 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt')
1370 call assert_equal('yy', v)
1371 endif
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02001372 set timeout& timeoutlen&
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001373endfunc
1374
1375" Test for inputlist()
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001376func Test_inputlist()
1377 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1378 call assert_equal(1, c)
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001379 call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001380 call assert_equal(2, c)
1381 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1382 call assert_equal(3, c)
1383
Bram Moolenaareebd5552020-06-10 15:45:57 +02001384 " CR to cancel
1385 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<cr>", 'tx')
1386 call assert_equal(0, c)
1387
1388 " Esc to cancel
1389 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<Esc>", 'tx')
1390 call assert_equal(0, c)
1391
1392 " q to cancel
1393 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>q", 'tx')
1394 call assert_equal(0, c)
1395
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001396 " Use backspace to delete characters in the prompt
1397 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<BS>3\<BS>2\<cr>", 'tx')
1398 call assert_equal(2, c)
1399
1400 " Use mouse to make a selection
1401 call test_setmouse(&lines - 3, 2)
1402 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
1403 call assert_equal(1, c)
1404 " Mouse click outside of the list
1405 call test_setmouse(&lines - 6, 2)
1406 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
1407 call assert_equal(-2, c)
1408
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001409 call assert_fails('call inputlist("")', 'E686:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001410 call assert_fails('call inputlist(test_null_list())', 'E686:')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001411endfunc
1412
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001413func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001414 if has('balloon_eval')
1415 " This won't do anything but must not crash either.
1416 call balloon_show('hi!')
Bram Moolenaar7d8ea0b2020-01-27 23:01:30 +01001417 if !has('gui_running')
1418 call balloon_show(range(3))
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001419 call balloon_show([])
Bram Moolenaar7d8ea0b2020-01-27 23:01:30 +01001420 endif
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001421 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001422endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001423
1424func Test_setbufvar_options()
1425 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1426 " window layout.
1427 call assert_equal(1, winnr('$'))
1428 split dummy_preview
1429 resize 2
1430 set winfixheight winfixwidth
1431 let prev_id = win_getid()
1432
1433 wincmd j
1434 let wh = winheight('.')
1435 let dummy_buf = bufnr('dummy_buf1', v:true)
1436 call setbufvar(dummy_buf, '&buftype', 'nofile')
1437 execute 'belowright vertical split #' . dummy_buf
1438 call assert_equal(wh, winheight('.'))
1439 let dum1_id = win_getid()
1440
1441 wincmd h
1442 let wh = winheight('.')
1443 let dummy_buf = bufnr('dummy_buf2', v:true)
Bram Moolenaar196b4662019-09-06 21:34:30 +02001444 eval 'nofile'->setbufvar(dummy_buf, '&buftype')
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001445 execute 'belowright vertical split #' . dummy_buf
1446 call assert_equal(wh, winheight('.'))
1447
1448 bwipe!
1449 call win_gotoid(prev_id)
1450 bwipe!
1451 call win_gotoid(dum1_id)
1452 bwipe!
1453endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001454
1455func Test_redo_in_nested_functions()
1456 nnoremap g. :set opfunc=Operator<CR>g@
1457 function Operator( type, ... )
1458 let @x = 'XXX'
1459 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1460 endfunction
1461
1462 function! Apply()
1463 5,6normal! .
1464 endfunction
1465
1466 new
1467 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1468 1normal g.i"
1469 call assert_equal('some "XXX" text', getline(1))
1470 3,4normal .
1471 call assert_equal('some "XXX" text', getline(3))
1472 call assert_equal('more "XXX" text', getline(4))
1473 call Apply()
1474 call assert_equal('some "XXX" text', getline(5))
1475 call assert_equal('more "XXX" text', getline(6))
1476 bwipe!
1477
1478 nunmap g.
1479 delfunc Operator
1480 delfunc Apply
1481endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02001482
1483func Test_shellescape()
1484 let save_shell = &shell
1485 set shell=bash
1486 call assert_equal("'text'", shellescape('text'))
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001487 call assert_equal("'te\"xt'", 'te"xt'->shellescape())
Bram Moolenaar20615522017-06-05 18:46:26 +02001488 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
1489
1490 call assert_equal("'te%xt'", shellescape("te%xt"))
1491 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
1492 call assert_equal("'te#xt'", shellescape("te#xt"))
1493 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
1494 call assert_equal("'te!xt'", shellescape("te!xt"))
1495 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1496
1497 call assert_equal("'te\nxt'", shellescape("te\nxt"))
1498 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1499 set shell=tcsh
1500 call assert_equal("'te\\!xt'", shellescape("te!xt"))
1501 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1502 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1503 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1504
1505 let &shell = save_shell
1506endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001507
1508func Test_trim()
1509 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001510 call assert_equal("Testing", " \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B"->trim())
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001511 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1512 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
1513 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
1514 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
1515 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
1516 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1517 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1518 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
1519 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
1520 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
1521 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
1522 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1523 call assert_equal("", trim("", ""))
1524 call assert_equal("a", trim("a", ""))
1525 call assert_equal("", trim("", "a"))
1526
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001527 call assert_equal("vim", trim(" vim ", " ", 0))
1528 call assert_equal("vim ", trim(" vim ", " ", 1))
1529 call assert_equal(" vim", trim(" vim ", " ", 2))
1530 call assert_fails('eval trim(" vim ", " ", [])', 'E745:')
1531 call assert_fails('eval trim(" vim ", " ", -1)', 'E475:')
1532 call assert_fails('eval trim(" vim ", " ", 3)', 'E475:')
1533
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001534 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001535 call assert_equal("x", trim(chars . "x" . chars))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001536
1537 call assert_fails('let c=trim([])', 'E730:')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001538endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001539
1540" Test for reg_recording() and reg_executing()
1541func Test_reg_executing_and_recording()
1542 let s:reg_stat = ''
1543 func s:save_reg_stat()
1544 let s:reg_stat = reg_recording() . ':' . reg_executing()
1545 return ''
1546 endfunc
1547
1548 new
1549 call s:save_reg_stat()
1550 call assert_equal(':', s:reg_stat)
1551 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1552 call assert_equal('a:', s:reg_stat)
1553 call feedkeys("@a", 'xt')
1554 call assert_equal(':a', s:reg_stat)
1555 call feedkeys("qb@aq", 'xt')
1556 call assert_equal('b:a', s:reg_stat)
1557 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1558 call assert_equal('":', s:reg_stat)
1559
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001560 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001561 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001562 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1563 func TestFunc() abort
1564 normal! ia
1565 endfunc
1566 call feedkeys("@q", 'xt')
1567 call assert_equal(':q', s:reg_stat)
1568 delfunc TestFunc
1569
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001570 " getchar() command saves and restores reg_executing
1571 map W :call TestFunc()<CR>
1572 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001573 let g:typed = ''
1574 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001575 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001576 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001577 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001578 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001579 endfunc
1580 call feedkeys("@qy", 'xt')
1581 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001582 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001583 delfunc TestFunc
1584 unmap W
1585 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001586 unlet g:regs
1587
1588 " input() command saves and restores reg_executing
1589 map W :call TestFunc()<CR>
1590 let @q = "W"
1591 let g:typed = ''
1592 let g:regs = []
1593 func TestFunc() abort
1594 let g:regs += [reg_executing()]
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001595 let g:typed = '?'->input()
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001596 let g:regs += [reg_executing()]
1597 endfunc
1598 call feedkeys("@qy\<CR>", 'xt')
1599 call assert_equal("y", g:typed)
1600 call assert_equal(['q', 'q'], g:regs)
1601 delfunc TestFunc
1602 unmap W
1603 unlet g:typed
1604 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001605
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001606 bwipe!
1607 delfunc s:save_reg_stat
1608 unlet s:reg_stat
1609endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001610
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001611func Test_inputsecret()
1612 map W :call TestFunc()<CR>
1613 let @q = "W"
1614 let g:typed1 = ''
1615 let g:typed2 = ''
1616 let g:regs = []
1617 func TestFunc() abort
1618 let g:typed1 = '?'->inputsecret()
1619 let g:typed2 = inputsecret('password: ')
1620 endfunc
1621 call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1622 call assert_equal("something", g:typed1)
1623 call assert_equal("else", g:typed2)
1624 delfunc TestFunc
1625 unmap W
1626 unlet g:typed1
1627 unlet g:typed2
1628endfunc
1629
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001630func Test_getchar()
1631 call feedkeys('a', '')
1632 call assert_equal(char2nr('a'), getchar())
1633
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001634 call setline(1, 'xxxx')
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001635 call test_setmouse(1, 3)
1636 let v:mouse_win = 9
1637 let v:mouse_winid = 9
1638 let v:mouse_lnum = 9
1639 let v:mouse_col = 9
1640 call feedkeys("\<S-LeftMouse>", '')
1641 call assert_equal("\<S-LeftMouse>", getchar())
1642 call assert_equal(1, v:mouse_win)
1643 call assert_equal(win_getid(1), v:mouse_winid)
1644 call assert_equal(1, v:mouse_lnum)
1645 call assert_equal(3, v:mouse_col)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001646 enew!
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001647endfunc
1648
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001649func Test_libcall_libcallnr()
1650 if !has('libcall')
1651 return
1652 endif
1653
1654 if has('win32')
1655 let libc = 'msvcrt.dll'
1656 elseif has('mac')
1657 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001658 elseif executable('ldd')
1659 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1660 endif
1661 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001662 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001663 if has('linux')
1664 " There is not documented but regarding the 1st argument of glibc's
1665 " dlopen an empty string and nullptr are equivalent, so using an empty
1666 " string for the 1st argument of libcall allows to call functions.
1667 let libc = ''
1668 elseif has('sun')
1669 " Set the path to libc.so according to the architecture.
1670 let test_bits = system('file ' . GetVimProg())
1671 let test_arch = system('uname -p')
1672 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1673 let libc = '/usr/lib/sparcv9/libc.so'
1674 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1675 let libc = '/usr/lib/amd64/libc.so'
1676 else
1677 let libc = '/usr/lib/libc.so'
1678 endif
1679 else
1680 " Unfortunately skip this test until a good way is found.
1681 return
1682 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001683 endif
1684
1685 if has('win32')
Bram Moolenaar02b31112019-08-31 22:16:38 +02001686 call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001687 else
Bram Moolenaar02b31112019-08-31 22:16:38 +02001688 call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001689 endif
1690
1691 " If function returns NULL, libcall() should return an empty string.
1692 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1693
1694 " Test libcallnr() with string and integer argument.
Bram Moolenaar02b31112019-08-31 22:16:38 +02001695 call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1696 call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001697
1698 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1699 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1700
1701 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1702 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1703endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001704
1705sandbox function Fsandbox()
1706 normal ix
1707endfunc
1708
1709func Test_func_sandbox()
1710 sandbox let F = {-> 'hello'}
1711 call assert_equal('hello', F())
1712
Bram Moolenaara4208962019-08-24 20:50:19 +02001713 sandbox let F = {-> "normal ix\<Esc>"->execute()}
Bram Moolenaard90a1442018-07-15 20:24:31 +02001714 call assert_fails('call F()', 'E48:')
1715 unlet F
1716
1717 call assert_fails('call Fsandbox()', 'E48:')
1718 delfunc Fsandbox
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001719
1720 " From a sandbox try to set a predefined variable (which cannot be modified
1721 " from a sandbox)
1722 call assert_fails('sandbox let v:lnum = 10', 'E794:')
Bram Moolenaard90a1442018-07-15 20:24:31 +02001723endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001724
1725func EditAnotherFile()
1726 let word = expand('<cword>')
1727 edit Xfuncrange2
1728endfunc
1729
1730func Test_func_range_with_edit()
1731 " Define a function that edits another buffer, then call it with a range that
1732 " is invalid in that buffer.
1733 call writefile(['just one line'], 'Xfuncrange2')
1734 new
Bram Moolenaar196b4662019-09-06 21:34:30 +02001735 eval 10->range()->setline(1)
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001736 write Xfuncrange1
1737 call assert_fails('5,8call EditAnotherFile()', 'E16:')
1738
1739 call delete('Xfuncrange1')
1740 call delete('Xfuncrange2')
1741 bwipe!
1742endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001743
1744func Test_func_exists_on_reload()
1745 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1746 call assert_equal(0, exists('*ExistingFunction'))
1747 source Xfuncexists
Bram Moolenaara4208962019-08-24 20:50:19 +02001748 call assert_equal(1, '*ExistingFunction'->exists())
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001749 " Redefining a function when reloading a script is OK.
1750 source Xfuncexists
1751 call assert_equal(1, exists('*ExistingFunction'))
1752
1753 " But redefining in another script is not OK.
1754 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1755 call assert_fails('source Xfuncexists2', 'E122:')
1756
1757 delfunc ExistingFunction
1758 call assert_equal(0, exists('*ExistingFunction'))
1759 call writefile([
1760 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1761 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1762 \ ], 'Xfuncexists')
1763 call assert_fails('source Xfuncexists', 'E122:')
1764 call assert_equal(1, exists('*ExistingFunction'))
1765
1766 call delete('Xfuncexists2')
1767 call delete('Xfuncexists')
1768 delfunc ExistingFunction
1769endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01001770
1771" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1772func Test_confirm()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001773 CheckUnix
1774 CheckNotGui
Bram Moolenaar2e050092019-01-27 15:00:36 +01001775
1776 call feedkeys('o', 'L')
1777 let a = confirm('Press O to proceed')
1778 call assert_equal(1, a)
1779
1780 call feedkeys('y', 'L')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001781 let a = 'Are you sure?'->confirm("&Yes\n&No")
Bram Moolenaar2e050092019-01-27 15:00:36 +01001782 call assert_equal(1, a)
1783
1784 call feedkeys('n', 'L')
1785 let a = confirm('Are you sure?', "&Yes\n&No")
1786 call assert_equal(2, a)
1787
1788 " confirm() should return 0 when pressing CTRL-C.
Bram Moolenaar79296512020-03-22 16:17:14 +01001789 call feedkeys("\<C-C>", 'L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01001790 let a = confirm('Are you sure?', "&Yes\n&No")
1791 call assert_equal(0, a)
1792
1793 " <Esc> requires another character to avoid it being seen as the start of an
1794 " escape sequence. Zero should be harmless.
Bram Moolenaara4208962019-08-24 20:50:19 +02001795 eval "\<Esc>0"->feedkeys('L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01001796 let a = confirm('Are you sure?', "&Yes\n&No")
1797 call assert_equal(0, a)
1798
1799 " Default choice is returned when pressing <CR>.
1800 call feedkeys("\<CR>", 'L')
1801 let a = confirm('Are you sure?', "&Yes\n&No")
1802 call assert_equal(1, a)
1803
1804 call feedkeys("\<CR>", 'L')
1805 let a = confirm('Are you sure?', "&Yes\n&No", 2)
1806 call assert_equal(2, a)
1807
1808 call feedkeys("\<CR>", 'L')
1809 let a = confirm('Are you sure?', "&Yes\n&No", 0)
1810 call assert_equal(0, a)
1811
1812 " Test with the {type} 4th argument
1813 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
1814 call feedkeys('y', 'L')
1815 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
1816 call assert_equal(1, a)
1817 endfor
1818
1819 call assert_fails('call confirm([])', 'E730:')
1820 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
1821 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
1822 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
1823endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001824
1825func Test_platform_name()
1826 " The system matches at most only one name.
Bram Moolenaar041c7102020-05-30 18:14:57 +02001827 let names = ['amiga', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001828 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
1829
1830 " Is Unix?
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001831 call assert_equal(has('bsd'), has('bsd') && has('unix'))
1832 call assert_equal(has('hpux'), has('hpux') && has('unix'))
1833 call assert_equal(has('linux'), has('linux') && has('unix'))
1834 call assert_equal(has('mac'), has('mac') && has('unix'))
1835 call assert_equal(has('qnx'), has('qnx') && has('unix'))
1836 call assert_equal(has('sun'), has('sun') && has('unix'))
1837 call assert_equal(has('win32'), has('win32') && !has('unix'))
1838 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
1839
1840 if has('unix') && executable('uname')
1841 let uname = system('uname')
Bram Moolenaara02e3f62019-02-07 21:27:14 +01001842 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
1843 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001844 call assert_equal(uname =~? 'HP-UX', has('hpux'))
1845 call assert_equal(uname =~? 'Linux', has('linux'))
1846 call assert_equal(uname =~? 'Darwin', has('mac'))
1847 call assert_equal(uname =~? 'QNX', has('qnx'))
1848 call assert_equal(uname =~? 'SunOS', has('sun'))
1849 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
1850 endif
1851endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001852
1853func Test_readdir()
1854 call mkdir('Xdir')
1855 call writefile([], 'Xdir/foo.txt')
1856 call writefile([], 'Xdir/bar.txt')
1857 call mkdir('Xdir/dir')
1858
1859 " All results
1860 let files = readdir('Xdir')
1861 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
1862
1863 " Only results containing "f"
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001864 let files = 'Xdir'->readdir({ x -> stridx(x, 'f') != -1 })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001865 call assert_equal(['foo.txt'], sort(files))
1866
1867 " Only .txt files
1868 let files = readdir('Xdir', { x -> x =~ '.txt$' })
1869 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1870
1871 " Only .txt files with string
1872 let files = readdir('Xdir', 'v:val =~ ".txt$"')
1873 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1874
1875 " Limit to 1 result.
1876 let l = []
1877 let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
1878 call assert_equal(1, len(files))
1879
Bram Moolenaar27da7de2019-09-03 17:13:37 +02001880 " Nested readdir() must not crash
1881 let files = readdir('Xdir', 'readdir("Xdir", "1") != []')
1882 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
1883
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001884 eval 'Xdir'->delete('rf')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001885endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02001886
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001887func Test_readdirex()
1888 call mkdir('Xdir')
Bram Moolenaar441d60e2020-06-02 22:19:50 +02001889 call writefile(['foo'], 'Xdir/foo.txt')
1890 call writefile(['barbar'], 'Xdir/bar.txt')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001891 call mkdir('Xdir/dir')
1892
1893 " All results
1894 let files = readdirex('Xdir')->map({-> v:val.name})
1895 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
Bram Moolenaar441d60e2020-06-02 22:19:50 +02001896 let sizes = readdirex('Xdir')->map({-> v:val.size})
1897 call assert_equal([0, 4, 7], sort(sizes))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001898
1899 " Only results containing "f"
1900 let files = 'Xdir'->readdirex({ e -> stridx(e.name, 'f') != -1 })
1901 \ ->map({-> v:val.name})
1902 call assert_equal(['foo.txt'], sort(files))
1903
1904 " Only .txt files
1905 let files = readdirex('Xdir', { e -> e.name =~ '.txt$' })
1906 \ ->map({-> v:val.name})
1907 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1908
1909 " Only .txt files with string
1910 let files = readdirex('Xdir', 'v:val.name =~ ".txt$"')
1911 \ ->map({-> v:val.name})
1912 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1913
1914 " Limit to 1 result.
1915 let l = []
1916 let files = readdirex('Xdir', {e -> len(add(l, e.name)) == 2 ? -1 : 1})
1917 \ ->map({-> v:val.name})
1918 call assert_equal(1, len(files))
1919
1920 " Nested readdirex() must not crash
1921 let files = readdirex('Xdir', 'readdirex("Xdir", "1") != []')
1922 \ ->map({-> v:val.name})
1923 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
1924
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02001925 " report broken link correctly
Bram Moolenaarab540322020-06-10 15:55:36 +02001926 if has("unix")
1927 call writefile([], 'Xdir/abc.txt')
1928 call system("ln -s Xdir/abc.txt Xdir/link")
1929 call delete('Xdir/abc.txt')
1930 let files = readdirex('Xdir', 'readdirex("Xdir", "1") != []')
1931 \ ->map({-> v:val.name .. '_' .. v:val.type})
1932 call sort(files)->assert_equal(
1933 \ ['bar.txt_file', 'dir_dir', 'foo.txt_file', 'link_link'])
1934 endif
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02001935 eval 'Xdir'->delete('rf')
1936endfunc
1937
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001938func Test_delete_rf()
1939 call mkdir('Xdir')
1940 call writefile([], 'Xdir/foo.txt')
1941 call writefile([], 'Xdir/bar.txt')
1942 call mkdir('Xdir/[a-1]') " issue #696
1943 call writefile([], 'Xdir/[a-1]/foo.txt')
1944 call writefile([], 'Xdir/[a-1]/bar.txt')
1945 call assert_true(filereadable('Xdir/foo.txt'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001946 call assert_true('Xdir/[a-1]/foo.txt'->filereadable())
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001947
1948 call assert_equal(0, delete('Xdir', 'rf'))
1949 call assert_false(filereadable('Xdir/foo.txt'))
1950 call assert_false(filereadable('Xdir/[a-1]/foo.txt'))
1951endfunc
1952
Bram Moolenaar17aca702019-05-16 22:24:55 +02001953func Test_call()
1954 call assert_equal(3, call('len', [123]))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001955 call assert_equal(3, 'len'->call([123]))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001956 call assert_fails("call call('len', 123)", 'E714:')
1957 call assert_equal(0, call('', []))
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001958 call assert_equal(0, call('len', test_null_list()))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001959
1960 function Mylen() dict
1961 return len(self.data)
1962 endfunction
1963 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001964 eval mydict.len->call([], mydict)->assert_equal(4)
Bram Moolenaar17aca702019-05-16 22:24:55 +02001965 call assert_fails("call call('Mylen', [], 0)", 'E715:')
1966endfunc
1967
1968func Test_char2nr()
1969 call assert_equal(12354, char2nr('あ', 1))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001970 call assert_equal(120, 'x'->char2nr())
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001971 set encoding=latin1
1972 call assert_equal(120, 'x'->char2nr())
1973 set encoding=utf-8
Bram Moolenaar17aca702019-05-16 22:24:55 +02001974endfunc
1975
1976func Test_eventhandler()
1977 call assert_equal(0, eventhandler())
1978endfunc
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001979
1980func Test_bufadd_bufload()
1981 call assert_equal(0, bufexists('someName'))
1982 let buf = bufadd('someName')
1983 call assert_notequal(0, buf)
1984 call assert_equal(1, bufexists('someName'))
1985 call assert_equal(0, getbufvar(buf, '&buflisted'))
1986 call assert_equal(0, bufloaded(buf))
1987 call bufload(buf)
1988 call assert_equal(1, bufloaded(buf))
1989 call assert_equal([''], getbufline(buf, 1, '$'))
1990
1991 let curbuf = bufnr('')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001992 eval ['some', 'text']->writefile('XotherName')
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001993 let buf = 'XotherName'->bufadd()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001994 call assert_notequal(0, buf)
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001995 eval 'XotherName'->bufexists()->assert_equal(1)
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001996 call assert_equal(0, getbufvar(buf, '&buflisted'))
1997 call assert_equal(0, bufloaded(buf))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001998 eval buf->bufload()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001999 call assert_equal(1, bufloaded(buf))
2000 call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
2001 call assert_equal(curbuf, bufnr(''))
2002
Bram Moolenaar892ae722019-06-30 20:33:01 +02002003 let buf1 = bufadd('')
2004 let buf2 = bufadd('')
2005 call assert_notequal(0, buf1)
2006 call assert_notequal(0, buf2)
2007 call assert_notequal(buf1, buf2)
2008 call assert_equal(1, bufexists(buf1))
2009 call assert_equal(1, bufexists(buf2))
2010 call assert_equal(0, bufloaded(buf1))
2011 exe 'bwipe ' .. buf1
2012 call assert_equal(0, bufexists(buf1))
2013 call assert_equal(1, bufexists(buf2))
2014 exe 'bwipe ' .. buf2
2015 call assert_equal(0, bufexists(buf2))
2016
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002017 bwipe someName
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002018 bwipe XotherName
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002019 call assert_equal(0, bufexists('someName'))
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002020 call delete('XotherName')
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002021endfunc
Bram Moolenaarc2585492019-09-22 21:29:53 +02002022
2023func Test_state()
2024 CheckRunVimInTerminal
2025
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +01002026 let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"
2027
Bram Moolenaarc2585492019-09-22 21:29:53 +02002028 let lines =<< trim END
2029 call setline(1, ['one', 'two', 'three'])
2030 map ;; gg
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02002031 set complete=.
Bram Moolenaarc2585492019-09-22 21:29:53 +02002032 func RunTimer()
2033 call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
2034 endfunc
2035 au Filetype foobar let g:state = state()|let g:mode = mode()
2036 END
2037 call writefile(lines, 'XState')
2038 let buf = RunVimInTerminal('-S XState', #{rows: 6})
2039
2040 " Using a ":" command Vim is busy, thus "S" is returned
2041 call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
2042 call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
2043 call term_sendkeys(buf, ":\<CR>")
2044
2045 " Using a timer callback
2046 call term_sendkeys(buf, ":call RunTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002047 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002048 call term_sendkeys(buf, getstate)
2049 call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)
2050
2051 " Halfway a mapping
2052 call term_sendkeys(buf, ":call RunTimer()\<CR>;")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002053 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002054 call term_sendkeys(buf, ";")
2055 call term_sendkeys(buf, getstate)
2056 call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)
2057
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02002058 " Insert mode completion (bit slower on Mac)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002059 call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002060 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002061 call term_sendkeys(buf, "\<Esc>")
2062 call term_sendkeys(buf, getstate)
2063 call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)
2064
2065 " Autocommand executing
2066 call term_sendkeys(buf, ":set filetype=foobar\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002067 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002068 call term_sendkeys(buf, getstate)
2069 call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)
2070
2071 " Todo: "w" - waiting for ch_evalexpr()
2072
2073 " messages scrolled
2074 call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002075 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002076 call term_sendkeys(buf, "\<CR>")
2077 call term_sendkeys(buf, getstate)
2078 call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)
2079
2080 call StopVimInTerminal(buf)
2081 call delete('XState')
2082endfunc
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002083
2084func Test_range()
2085 " destructuring
2086 let [x, y] = range(2)
2087 call assert_equal([0, 1], [x, y])
2088
2089 " index
2090 call assert_equal(4, range(1, 10)[3])
2091
2092 " add()
2093 call assert_equal([0, 1, 2, 3], add(range(3), 3))
2094 call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3)))
2095 call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3)))
2096
2097 " append()
2098 new
2099 call append('.', range(5))
2100 call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$'))
2101 bwipe!
2102
2103 " appendbufline()
2104 new
2105 call appendbufline(bufnr(''), '.', range(5))
2106 call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$'))
2107 bwipe!
2108
2109 " call()
2110 func TwoArgs(a, b)
2111 return [a:a, a:b]
2112 endfunc
2113 call assert_equal([0, 1], call('TwoArgs', range(2)))
2114
2115 " col()
2116 new
2117 call setline(1, ['foo', 'bar'])
2118 call assert_equal(2, col(range(1, 2)))
2119 bwipe!
2120
2121 " complete()
2122 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>"
2123 " complete_info()
2124 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>"
2125
2126 " copy()
2127 call assert_equal([1, 2, 3], copy(range(1, 3)))
2128
2129 " count()
2130 call assert_equal(0, count(range(0), 3))
2131 call assert_equal(0, count(range(2), 3))
2132 call assert_equal(1, count(range(5), 3))
2133
2134 " cursor()
2135 new
2136 call setline(1, ['aaa', 'bbb', 'ccc'])
2137 call cursor(range(1, 2))
2138 call assert_equal([2, 1], [col('.'), line('.')])
2139 bwipe!
2140
2141 " deepcopy()
2142 call assert_equal([1, 2, 3], deepcopy(range(1, 3)))
2143
2144 " empty()
2145 call assert_true(empty(range(0)))
2146 call assert_false(empty(range(2)))
2147
2148 " execute()
2149 new
2150 call setline(1, ['aaa', 'bbb', 'ccc'])
2151 call execute(range(3))
2152 call assert_equal(2, line('.'))
2153 bwipe!
2154
2155 " extend()
2156 call assert_equal([1, 2, 3, 4], extend([1], range(2, 4)))
2157 call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4)))
2158 call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4]))
2159
2160 " filter()
2161 call assert_equal([1, 3], filter(range(5), 'v:val % 2'))
2162
2163 " funcref()
2164 call assert_equal([0, 1], funcref('TwoArgs', range(2))())
2165
2166 " function()
2167 call assert_equal([0, 1], function('TwoArgs', range(2))())
2168
2169 " garbagecollect()
2170 let thelist = [1, range(2), 3]
2171 let otherlist = range(3)
2172 call test_garbagecollect_now()
2173
2174 " get()
2175 call assert_equal(4, get(range(1, 10), 3))
2176 call assert_equal(-1, get(range(1, 10), 42, -1))
2177
2178 " index()
2179 call assert_equal(1, index(range(1, 5), 2))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002180 call assert_fails("echo index([1, 2], 1, [])", 'E745:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002181
2182 " inputlist()
Bram Moolenaar272ca952020-01-28 20:49:11 +01002183 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x')
2184 call assert_equal(1, result)
2185 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x')
2186 call assert_equal(1, result)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002187
2188 " insert()
2189 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42))
2190 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0))
2191 call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1))
2192 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4))
2193 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1))
2194 call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5))
2195
2196 " join()
2197 call assert_equal('0 1 2 3 4', join(range(5)))
2198
Bram Moolenaar272ca952020-01-28 20:49:11 +01002199 " json_encode()
2200 call assert_equal('[0,1,2,3]', json_encode(range(4)))
2201
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002202 " len()
2203 call assert_equal(0, len(range(0)))
2204 call assert_equal(2, len(range(2)))
2205 call assert_equal(5, len(range(0, 12, 3)))
2206 call assert_equal(4, len(range(3, 0, -1)))
2207
2208 " list2str()
2209 call assert_equal('ABC', list2str(range(65, 67)))
Bram Moolenaar08f41572020-04-20 16:50:00 +02002210 call assert_fails('let s = list2str(5)', 'E474:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002211
2212 " lock()
2213 let thelist = range(5)
2214 lockvar thelist
2215
2216 " map()
2217 call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2'))
2218
2219 " match()
2220 call assert_equal(3, match(range(5), 3))
2221
2222 " matchaddpos()
2223 highlight MyGreenGroup ctermbg=green guibg=green
2224 call matchaddpos('MyGreenGroup', range(line('.'), line('.')))
2225
2226 " matchend()
2227 call assert_equal(4, matchend(range(5), '4'))
2228 call assert_equal(3, matchend(range(1, 5), '4'))
2229 call assert_equal(-1, matchend(range(1, 5), '42'))
2230
2231 " matchstrpos()
2232 call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4'))
2233 call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4'))
2234 call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42'))
2235
2236 " max() reverse()
2237 call assert_equal(0, max(range(0)))
2238 call assert_equal(0, max(range(10, 9)))
2239 call assert_equal(9, max(range(10)))
2240 call assert_equal(18, max(range(0, 20, 3)))
2241 call assert_equal(20, max(range(20, 0, -3)))
2242 call assert_equal(99999, max(range(100000)))
2243 call assert_equal(99999, max(range(99999, 0, -1)))
2244 call assert_equal(99999, max(reverse(range(100000))))
2245 call assert_equal(99999, max(reverse(range(99999, 0, -1))))
2246
2247 " min() reverse()
2248 call assert_equal(0, min(range(0)))
2249 call assert_equal(0, min(range(10, 9)))
2250 call assert_equal(5, min(range(5, 10)))
2251 call assert_equal(5, min(range(5, 10, 3)))
2252 call assert_equal(2, min(range(20, 0, -3)))
2253 call assert_equal(0, min(range(100000)))
2254 call assert_equal(0, min(range(99999, 0, -1)))
2255 call assert_equal(0, min(reverse(range(100000))))
2256 call assert_equal(0, min(reverse(range(99999, 0, -1))))
2257
2258 " remove()
2259 call assert_equal(1, remove(range(1, 10), 0))
2260 call assert_equal(2, remove(range(1, 10), 1))
2261 call assert_equal(9, remove(range(1, 10), 8))
2262 call assert_equal(10, remove(range(1, 10), 9))
2263 call assert_equal(10, remove(range(1, 10), -1))
2264 call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4))
2265
2266 " repeat()
2267 call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2))
2268 call assert_equal([0, 1, 2], repeat(range(3), 1))
2269 call assert_equal([], repeat(range(3), 0))
2270 call assert_equal([], repeat(range(5, 4), 2))
2271 call assert_equal([], repeat(range(5, 4), 0))
2272
2273 " reverse()
2274 call assert_equal([2, 1, 0], reverse(range(3)))
2275 call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1)))
2276 call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10)))
2277 call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20)))
2278 call assert_equal([16, 13, 10], reverse(range(10, 18, 3)))
2279 call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3)))
2280 call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3)))
2281 call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3)))
2282 call assert_equal([], reverse(range(0)))
2283
2284 " TODO: setpos()
2285 " new
2286 " call setline(1, repeat([''], bufnr('')))
2287 " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6))
2288 " call setpos('x', range(bufnr(''), bufnr('') + 3))
2289 " bwipe!
2290
2291 " setreg()
2292 call setreg('a', range(3))
2293 call assert_equal("0\n1\n2\n", getreg('a'))
2294
Bram Moolenaarb0992022020-01-30 14:55:42 +01002295 " settagstack()
2296 call settagstack(1, #{items : range(4)})
Bram Moolenaar94255df2020-02-05 20:10:33 +01002297
Bram Moolenaarb0992022020-01-30 14:55:42 +01002298 " sign_define()
2299 call assert_fails("call sign_define(range(5))", "E715:")
2300 call assert_fails("call sign_placelist(range(5))", "E715:")
2301
2302 " sign_undefine()
2303 call assert_fails("call sign_undefine(range(5))", "E908:")
2304
2305 " sign_unplacelist()
2306 call assert_fails("call sign_unplacelist(range(5))", "E715:")
2307
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002308 " sort()
2309 call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1)))
2310
2311 " string()
2312 call assert_equal('[0, 1, 2, 3, 4]', string(range(5)))
2313
Bram Moolenaarb0992022020-01-30 14:55:42 +01002314 " taglist() with 'tagfunc'
2315 func TagFunc(pattern, flags, info)
2316 return range(10)
2317 endfunc
2318 set tagfunc=TagFunc
2319 call assert_fails("call taglist('asdf')", 'E987:')
2320 set tagfunc=
Bram Moolenaar94255df2020-02-05 20:10:33 +01002321
Bram Moolenaarb0992022020-01-30 14:55:42 +01002322 " term_start()
Bram Moolenaar705724e2020-01-31 21:13:42 +01002323 if has('terminal') && has('termguicolors')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002324 call assert_fails('call term_start(range(3, 4))', 'E474:')
2325 let g:terminal_ansi_colors = range(16)
Bram Moolenaar94255df2020-02-05 20:10:33 +01002326 if has('win32')
2327 let cmd = "cmd /c dir"
2328 else
2329 let cmd = "ls"
2330 endif
2331 call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"})', 'E475:')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002332 unlet g:terminal_ansi_colors
2333 endif
2334
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002335 " type()
2336 call assert_equal(v:t_list, type(range(5)))
2337
2338 " uniq()
2339 call assert_equal([0, 1, 2, 3, 4], uniq(range(5)))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002340
2341 " errors
2342 call assert_fails('let x=range(2, 8, 0)', 'E726:')
2343 call assert_fails('let x=range(3, 1)', 'E727:')
2344 call assert_fails('let x=range(1, 3, -2)', 'E727:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002345 call assert_fails('let x=range([])', 'E745:')
2346 call assert_fails('let x=range(1, [])', 'E745:')
2347 call assert_fails('let x=range(1, 4, [])', 'E745:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002348endfunc
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002349
2350func Test_echoraw()
2351 CheckScreendump
2352
2353 " Normally used for escape codes, but let's test with a CR.
2354 let lines =<< trim END
2355 call echoraw("hello\<CR>x")
2356 END
2357 call writefile(lines, 'XTest_echoraw')
2358 let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40})
2359 call VerifyScreenDump(buf, 'Test_functions_echoraw', {})
2360
2361 " clean up
2362 call StopVimInTerminal(buf)
2363 call delete('XTest_echoraw')
2364endfunc
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002365
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002366" Test for echo highlighting
2367func Test_echohl()
2368 echohl Search
2369 echo 'Vim'
2370 call assert_equal('Vim', Screenline(&lines))
2371 " TODO: How to check the highlight group used by echohl?
2372 " ScreenAttrs() returns all zeros.
2373 echohl None
2374endfunc
2375
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002376" Test for the eval() function
2377func Test_eval()
2378 call assert_fails("call eval('5 a')", 'E488:')
2379endfunc
2380
2381" Test for the nr2char() function
2382func Test_nr2char()
2383 set encoding=latin1
2384 call assert_equal('@', nr2char(64))
2385 set encoding=utf8
2386 call assert_equal('a', nr2char(97, 1))
2387 call assert_equal('a', nr2char(97, 0))
Bram Moolenaarf7271e82020-05-24 18:45:07 +02002388
2389 call assert_equal("\x80\xfc\b\xf4\x80\xfeX\x80\xfeX\x80\xfeX", eval('"\<M-' .. nr2char(0x100000) .. '>"'))
Bram Moolenaar19193712020-05-25 23:01:42 +02002390 call assert_equal("\x80\xfc\b\xfd\x80\xfeX\x80\xfeX\x80\xfeX\x80\xfeX\x80\xfeX", eval('"\<M-' .. nr2char(0x40000000) .. '>"'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002391endfunc
2392
2393" Test for screenattr(), screenchar() and screenchars() functions
2394func Test_screen_functions()
2395 call assert_equal(-1, screenattr(-1, -1))
2396 call assert_equal(-1, screenchar(-1, -1))
2397 call assert_equal([], screenchars(-1, -1))
2398endfunc
2399
Bram Moolenaar08f41572020-04-20 16:50:00 +02002400" Test for getcurpos() and setpos()
2401func Test_getcurpos_setpos()
2402 new
2403 call setline(1, ['012345678', '012345678'])
2404 normal gg6l
2405 let sp = getcurpos()
2406 normal 0
2407 call setpos('.', sp)
2408 normal jyl
2409 call assert_equal('6', @")
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002410 call assert_equal(-1, setpos('.', test_null_list()))
2411 call assert_equal(-1, setpos('.', {}))
Bram Moolenaar08f41572020-04-20 16:50:00 +02002412 close!
2413endfunc
2414
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002415" Test for glob()
2416func Test_glob()
2417 call assert_equal('', glob(test_null_string()))
2418 call assert_equal('', globpath(test_null_string(), test_null_string()))
2419endfunc
2420
2421" Test for browse()
2422func Test_browse()
2423 CheckFeature browse
2424 call assert_fails('call browse([], "open", "x", "a.c")', 'E745:')
2425endfunc
2426
2427" Test for browsedir()
2428func Test_browsedir()
2429 CheckFeature browse
2430 call assert_fails('call browsedir("open", [])', 'E730:')
2431endfunc
2432
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002433" vim: shiftwidth=2 sts=2 expandtab