blob: 7462da856d42d11c6d52a65b7413112131f8b100 [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
32
Bram Moolenaar79296512020-03-22 16:17:14 +010033 call assert_equal(0, has('nonexistent'))
34 call assert_equal(0, has('nonexistent', 1))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010035
36 " Will we ever have patch 9999?
37 let ver = 'patch-' .. v:version / 100 .. '.' .. v:version % 100 .. '.9999'
38 call assert_equal(0, has(ver))
Bram Moolenaar79296512020-03-22 16:17:14 +010039endfunc
40
Bram Moolenaar24c2e482017-01-29 15:45:12 +010041func Test_empty()
42 call assert_equal(1, empty(''))
43 call assert_equal(0, empty('a'))
44
45 call assert_equal(1, empty(0))
46 call assert_equal(1, empty(-0))
47 call assert_equal(0, empty(1))
48 call assert_equal(0, empty(-1))
49
Bram Moolenaar5feabe02020-01-30 18:24:53 +010050 if has('float')
51 call assert_equal(1, empty(0.0))
52 call assert_equal(1, empty(-0.0))
53 call assert_equal(0, empty(1.0))
54 call assert_equal(0, empty(-1.0))
55 call assert_equal(0, empty(1.0/0.0))
56 call assert_equal(0, empty(0.0/0.0))
57 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +010058
59 call assert_equal(1, empty([]))
60 call assert_equal(0, empty(['a']))
61
62 call assert_equal(1, empty({}))
63 call assert_equal(0, empty({'a':1}))
64
65 call assert_equal(1, empty(v:null))
66 call assert_equal(1, empty(v:none))
67 call assert_equal(1, empty(v:false))
68 call assert_equal(0, empty(v:true))
69
Bram Moolenaar41042f32017-03-09 12:09:32 +010070 if has('channel')
71 call assert_equal(1, empty(test_null_channel()))
72 endif
73 if has('job')
74 call assert_equal(1, empty(test_null_job()))
75 endif
76
Bram Moolenaar24c2e482017-01-29 15:45:12 +010077 call assert_equal(0, empty(function('Test_empty')))
Bram Moolenaar17aca702019-05-16 22:24:55 +020078 call assert_equal(0, empty(function('Test_empty', [0])))
Bram Moolenaar7c215c52020-02-29 13:43:27 +010079
80 call assert_fails("call empty(test_void())", 'E685:')
81 call assert_fails("call empty(test_unknown())", 'E685:')
Bram Moolenaar24c2e482017-01-29 15:45:12 +010082endfunc
83
Bram Moolenaardd589232020-02-29 17:38:12 +010084func Test_test_void()
85 call assert_fails('echo 1 == test_void()', 'E685:')
86 if has('float')
87 call assert_fails('echo 1.0 == test_void()', 'E685:')
88 endif
89 call assert_fails('let x = json_encode(test_void())', 'E685:')
90 call assert_fails('let x = copy(test_void())', 'E685:')
91 call assert_fails('let x = copy([test_void()])', 'E685:')
92endfunc
93
Bram Moolenaar24c2e482017-01-29 15:45:12 +010094func Test_len()
95 call assert_equal(1, len(0))
96 call assert_equal(2, len(12))
97
98 call assert_equal(0, len(''))
99 call assert_equal(2, len('ab'))
100
101 call assert_equal(0, len([]))
102 call assert_equal(2, len([2, 1]))
103
104 call assert_equal(0, len({}))
105 call assert_equal(2, len({'a': 1, 'b': 2}))
106
107 call assert_fails('call len(v:none)', 'E701:')
108 call assert_fails('call len({-> 0})', 'E701:')
109endfunc
110
111func Test_max()
112 call assert_equal(0, max([]))
113 call assert_equal(2, max([2]))
114 call assert_equal(2, max([1, 2]))
115 call assert_equal(2, max([1, 2, v:null]))
116
117 call assert_equal(0, max({}))
118 call assert_equal(2, max({'a':1, 'b':2}))
119
120 call assert_fails('call max(1)', 'E712:')
121 call assert_fails('call max(v:none)', 'E712:')
122endfunc
123
124func Test_min()
125 call assert_equal(0, min([]))
126 call assert_equal(2, min([2]))
127 call assert_equal(1, min([1, 2]))
128 call assert_equal(0, min([1, 2, v:null]))
129
130 call assert_equal(0, min({}))
131 call assert_equal(1, min({'a':1, 'b':2}))
132
133 call assert_fails('call min(1)', 'E712:')
134 call assert_fails('call min(v:none)', 'E712:')
135endfunc
136
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200137func Test_strwidth()
138 for aw in ['single', 'double']
139 exe 'set ambiwidth=' . aw
140 call assert_equal(0, strwidth(''))
141 call assert_equal(1, strwidth("\t"))
142 call assert_equal(3, strwidth('Vim'))
143 call assert_equal(4, strwidth(1234))
144 call assert_equal(5, strwidth(-1234))
145
Bram Moolenaar30276f22019-01-24 17:59:39 +0100146 call assert_equal(2, strwidth('😉'))
147 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
148 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200149
150 call assert_fails('call strwidth({->0})', 'E729:')
151 call assert_fails('call strwidth([])', 'E730:')
152 call assert_fails('call strwidth({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100153 if has('float')
154 call assert_fails('call strwidth(1.2)', 'E806:')
155 endif
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200156 endfor
157
158 set ambiwidth&
159endfunc
160
Bram Moolenaar08243d22017-01-10 16:12:29 +0100161func Test_str2nr()
162 call assert_equal(0, str2nr(''))
163 call assert_equal(1, str2nr('1'))
164 call assert_equal(1, str2nr(' 1 '))
165
166 call assert_equal(1, str2nr('+1'))
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(123456789, str2nr('123456789'))
175 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100176
177 call assert_equal(5, str2nr('101', 2))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200178 call assert_equal(5, '0b101'->str2nr(2))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100179 call assert_equal(5, str2nr('0B101', 2))
180 call assert_equal(-5, str2nr('-101', 2))
181 call assert_equal(-5, str2nr('-0b101', 2))
182 call assert_equal(-5, str2nr('-0B101', 2))
183
184 call assert_equal(65, str2nr('101', 8))
185 call assert_equal(65, str2nr('0101', 8))
186 call assert_equal(-65, str2nr('-101', 8))
187 call assert_equal(-65, str2nr('-0101', 8))
188
189 call assert_equal(11259375, str2nr('abcdef', 16))
190 call assert_equal(11259375, str2nr('ABCDEF', 16))
191 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
192 call assert_equal(11259375, str2nr('0xabcdef', 16))
193 call assert_equal(11259375, str2nr('0Xabcdef', 16))
194 call assert_equal(11259375, str2nr('0XABCDEF', 16))
195 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
196
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200197 call assert_equal(1, str2nr("1'000'000", 10, 0))
198 call assert_equal(256, str2nr("1'0000'0000", 2, 1))
199 call assert_equal(262144, str2nr("1'000'000", 8, 1))
200 call assert_equal(1000000, str2nr("1'000'000", 10, 1))
Bram Moolenaarea8dcf82019-09-15 21:12:22 +0200201 call assert_equal(1000, str2nr("1'000''000", 10, 1))
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200202 call assert_equal(65536, str2nr("1'00'00", 16, 1))
203
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100204 call assert_equal(0, str2nr('0x10'))
205 call assert_equal(0, str2nr('0b10'))
206 call assert_equal(1, str2nr('12', 2))
207 call assert_equal(1, str2nr('18', 8))
208 call assert_equal(1, str2nr('1g', 16))
209
210 call assert_equal(0, str2nr(v:null))
211 call assert_equal(0, str2nr(v:none))
212
213 call assert_fails('call str2nr([])', 'E730:')
214 call assert_fails('call str2nr({->2})', 'E729:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100215 if has('float')
216 call assert_fails('call str2nr(1.2)', 'E806:')
217 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100218 call assert_fails('call str2nr(10, [])', 'E474:')
219endfunc
220
221func Test_strftime()
Bram Moolenaar10455d42019-11-21 15:36:18 +0100222 CheckFunction strftime
223
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100224 " Format of strftime() depends on system. We assume
225 " that basic formats tested here are available and
226 " identical on all systems which support strftime().
227 "
228 " The 2nd parameter of strftime() is a local time, so the output day
229 " of strftime() can be 17 or 18, depending on timezone.
230 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
231 "
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200232 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 +0100233
234 call assert_fails('call strftime([])', 'E730:')
235 call assert_fails('call strftime("%Y", [])', 'E745:')
Bram Moolenaardb517302019-06-18 22:53:24 +0200236
237 " Check that the time changes after we change the timezone
238 " Save previous timezone value, if any
239 if exists('$TZ')
240 let tz = $TZ
241 endif
242
243 " Force EST and then UTC, save the current hour (24-hour clock) for each
244 let $TZ = 'EST' | let est = strftime('%H')
245 let $TZ = 'UTC' | let utc = strftime('%H')
246
247 " Those hours should be two bytes long, and should not be the same; if they
248 " are, a tzset(3) call may have failed somewhere
249 call assert_equal(strlen(est), 2)
250 call assert_equal(strlen(utc), 2)
Bram Moolenaar87652a72019-06-18 23:07:37 +0200251 " TODO: this fails on MS-Windows
252 if has('unix')
253 call assert_notequal(est, utc)
254 endif
Bram Moolenaardb517302019-06-18 22:53:24 +0200255
256 " If we cached a timezone value, put it back, otherwise clear it
257 if exists('tz')
258 let $TZ = tz
259 else
260 unlet $TZ
261 endif
Bram Moolenaar10455d42019-11-21 15:36:18 +0100262endfunc
Bram Moolenaardb517302019-06-18 22:53:24 +0200263
Bram Moolenaar10455d42019-11-21 15:36:18 +0100264func Test_strptime()
265 CheckFunction strptime
266
267 if exists('$TZ')
268 let tz = $TZ
269 endif
270 let $TZ = 'UTC'
271
Bram Moolenaar9a838fe2019-12-06 12:45:01 +0100272 call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
Bram Moolenaar10455d42019-11-21 15:36:18 +0100273
274 call assert_fails('call strptime()', 'E119:')
275 call assert_fails('call strptime("xxx")', 'E119:')
276 call assert_equal(0, strptime("%Y", ''))
277 call assert_equal(0, strptime("%Y", "xxx"))
278
279 if exists('tz')
280 let $TZ = tz
281 else
282 unlet $TZ
283 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100284endfunc
285
Bram Moolenaardce1e892019-02-10 23:18:53 +0100286func Test_resolve_unix()
Bram Moolenaar26109902018-10-06 15:43:17 +0200287 if !has('unix')
288 return
289 endif
290
291 " Xlink1 -> Xlink2
292 " Xlink2 -> Xlink3
293 silent !ln -s -f Xlink2 Xlink1
294 silent !ln -s -f Xlink3 Xlink2
295 call assert_equal('Xlink3', resolve('Xlink1'))
296 call assert_equal('./Xlink3', resolve('./Xlink1'))
297 call assert_equal('Xlink3/', resolve('Xlink2/'))
298 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
299 "call assert_equal('Xlink3/', resolve('Xlink1/'))
300 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
301 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
302 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
303
304 " Test resolve() with a symlink cycle.
305 " Xlink1 -> Xlink2
306 " Xlink2 -> Xlink3
307 " Xlink3 -> Xlink1
308 silent !ln -s -f Xlink1 Xlink3
309 call assert_fails('call resolve("Xlink1")', 'E655:')
310 call assert_fails('call resolve("./Xlink1")', 'E655:')
311 call assert_fails('call resolve("Xlink2")', 'E655:')
312 call assert_fails('call resolve("Xlink3")', 'E655:')
313 call delete('Xlink1')
314 call delete('Xlink2')
315 call delete('Xlink3')
316
317 silent !ln -s -f Xdir//Xfile Xlink
318 call assert_equal('Xdir/Xfile', resolve('Xlink'))
319 call delete('Xlink')
320
321 silent !ln -s -f Xlink2/ Xlink1
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200322 call assert_equal('Xlink2', 'Xlink1'->resolve())
Bram Moolenaar26109902018-10-06 15:43:17 +0200323 call assert_equal('Xlink2/', resolve('Xlink1/'))
324 call delete('Xlink1')
325
326 silent !ln -s -f ./Xlink2 Xlink1
327 call assert_equal('Xlink2', resolve('Xlink1'))
328 call assert_equal('./Xlink2', resolve('./Xlink1'))
329 call delete('Xlink1')
330endfunc
331
Bram Moolenaardce1e892019-02-10 23:18:53 +0100332func s:normalize_fname(fname)
333 let ret = substitute(a:fname, '\', '/', 'g')
334 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200335 return ret->tolower()
Bram Moolenaardce1e892019-02-10 23:18:53 +0100336endfunc
337
338func Test_resolve_win32()
339 if !has('win32')
340 return
341 endif
342
343 " test for shortcut file
344 if executable('cscript')
345 new Xfile
346 wq
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200347 let lines =<< trim END
348 Set fs = CreateObject("Scripting.FileSystemObject")
349 Set ws = WScript.CreateObject("WScript.Shell")
350 Set shortcut = ws.CreateShortcut("Xlink.lnk")
351 shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")
352 shortcut.Save
353 END
354 call writefile(lines, 'link.vbs')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100355 silent !cscript link.vbs
356 call delete('link.vbs')
357 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
358 call delete('Xfile')
359
360 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
361 call delete('Xlink.lnk')
362 else
363 echomsg 'skipped test for shortcut file'
364 endif
365
366 " remove files
367 call delete('Xlink')
368 call delete('Xdir', 'd')
369 call delete('Xfile')
370
371 " test for symbolic link to a file
372 new Xfile
373 wq
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200374 call assert_equal('Xfile', resolve('Xfile'))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100375 silent !mklink Xlink Xfile
376 if !v:shell_error
377 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink')))
378 call delete('Xlink')
379 else
380 echomsg 'skipped test for symbolic link to a file'
381 endif
382 call delete('Xfile')
383
384 " test for junction to a directory
385 call mkdir('Xdir')
386 silent !mklink /J Xlink Xdir
387 if !v:shell_error
388 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
389
390 call delete('Xdir', 'd')
391
392 " test for junction already removed
393 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
394 call delete('Xlink')
395 else
396 echomsg 'skipped test for junction to a directory'
397 call delete('Xdir', 'd')
398 endif
399
400 " test for symbolic link to a directory
401 call mkdir('Xdir')
402 silent !mklink /D Xlink Xdir
403 if !v:shell_error
404 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
405
406 call delete('Xdir', 'd')
407
408 " test for symbolic link already removed
409 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
410 call delete('Xlink')
411 else
412 echomsg 'skipped test for symbolic link to a directory'
413 call delete('Xdir', 'd')
414 endif
415
416 " test for buffer name
417 new Xfile
418 wq
419 silent !mklink Xlink Xfile
420 if !v:shell_error
421 edit Xlink
422 call assert_equal('Xlink', bufname('%'))
423 call delete('Xlink')
424 bw!
425 else
426 echomsg 'skipped test for buffer name'
427 endif
428 call delete('Xfile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200429
430 " test for reparse point
431 call mkdir('Xdir')
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200432 call assert_equal('Xdir', resolve('Xdir'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200433 silent !mklink /D Xdirlink Xdir
434 if !v:shell_error
435 w Xdir/text.txt
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200436 call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200437 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
438 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200439 call delete('Xdirlink')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200440 else
441 echomsg 'skipped test for reparse point'
442 endif
443
444 call delete('Xdir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100445endfunc
446
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100447func Test_simplify()
448 call assert_equal('', simplify(''))
449 call assert_equal('/', simplify('/'))
450 call assert_equal('/', simplify('/.'))
451 call assert_equal('/', simplify('/..'))
452 call assert_equal('/...', simplify('/...'))
453 call assert_equal('./dir/file', simplify('./dir/file'))
454 call assert_equal('./dir/file', simplify('.///dir//file'))
455 call assert_equal('./dir/file', simplify('./dir/./file'))
456 call assert_equal('./file', simplify('./dir/../file'))
457 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
458 call assert_equal('./file', simplify('dir/.././file'))
459
460 call assert_fails('call simplify({->0})', 'E729:')
461 call assert_fails('call simplify([])', 'E730:')
462 call assert_fails('call simplify({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100463 if has('float')
464 call assert_fails('call simplify(1.2)', 'E806:')
465 endif
Bram Moolenaar08243d22017-01-10 16:12:29 +0100466endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100467
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200468func Test_pathshorten()
469 call assert_equal('', pathshorten(''))
470 call assert_equal('foo', pathshorten('foo'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200471 call assert_equal('/foo', '/foo'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200472 call assert_equal('f/', pathshorten('foo/'))
473 call assert_equal('f/bar', pathshorten('foo/bar'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200474 call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200475 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
476 call assert_equal('.f/bar', pathshorten('.foo/bar'))
477 call assert_equal('~f/bar', pathshorten('~foo/bar'))
478 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
479 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
480 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
481endfunc
482
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100483func Test_strpart()
484 call assert_equal('de', strpart('abcdefg', 3, 2))
485 call assert_equal('ab', strpart('abcdefg', -2, 4))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200486 call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100487 call assert_equal('fg', strpart('abcdefg', 5, 4))
488 call assert_equal('defg', strpart('abcdefg', 3))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100489 call assert_equal('', strpart('abcdefg', 10))
490 call assert_fails("let s=strpart('abcdef', [])", 'E745:')
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100491
Bram Moolenaar30276f22019-01-24 17:59:39 +0100492 call assert_equal('lép', strpart('éléphant', 2, 4))
493 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100494endfunc
495
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100496func Test_tolower()
497 call assert_equal("", tolower(""))
498
499 " Test with all printable ASCII characters.
500 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
501 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
502
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100503 " Test with a few uppercase diacritics.
504 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
505 call assert_equal("bḃḇ", tolower("BḂḆ"))
506 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
507 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
508 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
509 call assert_equal("fḟ ", tolower("FḞ "))
510 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
511 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
512 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
513 call assert_equal("jĵ", tolower("JĴ"))
514 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
515 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
516 call assert_equal("mḿṁ", tolower("MḾṀ"))
517 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
518 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
519 call assert_equal("pṕṗ", tolower("PṔṖ"))
520 call assert_equal("q", tolower("Q"))
521 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
522 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
523 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
524 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
525 call assert_equal("vṽ", tolower("VṼ"))
526 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
527 call assert_equal("xẋẍ", tolower("XẊẌ"))
528 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
529 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
530
531 " Test with a few lowercase diacritics, which should remain unchanged.
532 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
533 call assert_equal("bḃḇ", tolower("bḃḇ"))
534 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
535 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
536 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
537 call assert_equal("fḟ", tolower("fḟ"))
538 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
539 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
540 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
541 call assert_equal("jĵǰ", tolower("jĵǰ"))
542 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
543 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
544 call assert_equal("mḿṁ ", tolower("mḿṁ "))
545 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
546 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
547 call assert_equal("pṕṗ", tolower("pṕṗ"))
548 call assert_equal("q", tolower("q"))
549 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
550 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
551 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
552 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
553 call assert_equal("vṽ", tolower("vṽ"))
554 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
555 call assert_equal("ẋẍ", tolower("ẋẍ"))
556 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
557 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
558
559 " According to https://twitter.com/jifa/status/625776454479970304
560 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
561 " in length (2 to 3 bytes) when lowercased. So let's test them.
562 call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100563
564 " This call to tolower with invalid utf8 sequence used to cause access to
565 " invalid memory.
566 call tolower("\xC0\x80\xC0")
567 call tolower("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100568endfunc
569
570func Test_toupper()
571 call assert_equal("", toupper(""))
572
573 " Test with all printable ASCII characters.
574 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
575 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
576
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100577 " Test with a few lowercase diacritics.
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200578 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100579 call assert_equal("BḂḆ", toupper("bḃḇ"))
580 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
581 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
582 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
583 call assert_equal("FḞ", toupper("fḟ"))
584 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
585 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
586 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
587 call assert_equal("JĴǰ", toupper("jĵǰ"))
588 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
589 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
590 call assert_equal("MḾṀ ", toupper("mḿṁ "))
591 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
592 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
593 call assert_equal("PṔṖ", toupper("pṕṗ"))
594 call assert_equal("Q", toupper("q"))
595 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
596 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
597 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
598 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
599 call assert_equal("VṼ", toupper("vṽ"))
600 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
601 call assert_equal("ẊẌ", toupper("ẋẍ"))
602 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
603 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
604
605 " Test that uppercase diacritics, which should remain unchanged.
606 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
607 call assert_equal("BḂḆ", toupper("BḂḆ"))
608 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
609 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
610 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
611 call assert_equal("FḞ ", toupper("FḞ "))
612 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
613 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
614 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
615 call assert_equal("JĴ", toupper("JĴ"))
616 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
617 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
618 call assert_equal("MḾṀ", toupper("MḾṀ"))
619 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
620 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
621 call assert_equal("PṔṖ", toupper("PṔṖ"))
622 call assert_equal("Q", toupper("Q"))
623 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
624 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
625 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
626 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
627 call assert_equal("VṼ", toupper("VṼ"))
628 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
629 call assert_equal("XẊẌ", toupper("XẊẌ"))
630 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
631 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
632
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100633 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100634
635 " This call to toupper with invalid utf8 sequence used to cause access to
636 " invalid memory.
637 call toupper("\xC0\x80\xC0")
638 call toupper("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100639endfunc
640
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200641func Test_tr()
642 call assert_equal('foo', tr('bar', 'bar', 'foo'))
643 call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100644 call assert_fails("let s=tr([], 'abc', 'def')", 'E730:')
645 call assert_fails("let s=tr('abc', [], 'def')", 'E730:')
646 call assert_fails("let s=tr('abc', 'abc', [])", 'E730:')
647 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
648 set encoding=latin1
649 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
650 call assert_equal('hEllO', tr('hello', 'eo', 'EO'))
651 call assert_equal('hello', tr('hello', 'xy', 'ab'))
652 set encoding=utf8
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200653endfunc
654
Bram Moolenaare90858d2017-02-01 17:24:34 +0100655" Tests for the mode() function
656let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100657func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100658 let g:current_modes = mode(0) . '-' . mode(1)
659 return ''
660endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100661
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100662func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100663 new
664 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
665
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100666 " Only complete from the current buffer.
667 set complete=.
668
Bram Moolenaare90858d2017-02-01 17:24:34 +0100669 inoremap <F2> <C-R>=Save_mode()<CR>
670
671 normal! 3G
672 exe "normal i\<F2>\<Esc>"
673 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100674 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100675 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
676 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100677 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100678 exe "normal iBro\<C-P>\<F2>\<Esc>u"
679 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100680 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100681 exe "normal iBa\<C-X>\<F2>\<Esc>u"
682 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100683 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100684 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
685 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100686 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100687 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
688 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100689 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100690 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
691 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100692 " i_CTRL-X CTRL-L: Multiple matches
693 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
694 call assert_equal('i-ic', g:current_modes)
695 " i_CTRL-X CTRL-L: Single match
696 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
697 call assert_equal('i-ic', g:current_modes)
698 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100699 exe "normal iCom\<C-P>\<F2>\<Esc>u"
700 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100701 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100702 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
703 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100704 " i_CTRL-X CTRL-L: No match
705 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
706 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100707
Bram Moolenaare971df32017-02-05 14:15:29 +0100708 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100709 exe "normal RBa\<C-P>\<F2>\<Esc>u"
710 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100711 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100712 exe "normal RBro\<C-P>\<F2>\<Esc>u"
713 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100714 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100715 exe "normal RBa\<C-X>\<F2>\<Esc>u"
716 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100717 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100718 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
719 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100720 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100721 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
722 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100723 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100724 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
725 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100726 " R_CTRL-X CTRL-L: Multiple matches
727 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
728 call assert_equal('R-Rc', g:current_modes)
729 " R_CTRL-X CTRL-L: Single match
730 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
731 call assert_equal('R-Rc', g:current_modes)
732 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100733 exe "normal RCom\<C-P>\<F2>\<Esc>u"
734 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100735 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100736 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
737 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100738 " R_CTRL-X CTRL-L: No match
739 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
740 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100741
Bram Moolenaara1449832019-09-01 20:16:52 +0200742 call assert_equal('n', 0->mode())
743 call assert_equal('n', 1->mode())
Bram Moolenaare90858d2017-02-01 17:24:34 +0100744
Bram Moolenaar612cc382018-07-29 15:34:26 +0200745 " i_CTRL-O
746 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
747 call assert_equal("n-niI", g:current_modes)
748
749 " R_CTRL-O
750 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
751 call assert_equal("n-niR", g:current_modes)
752
753 " gR_CTRL-O
754 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
755 call assert_equal("n-niV", g:current_modes)
756
Bram Moolenaare90858d2017-02-01 17:24:34 +0100757 " How to test operator-pending mode?
758
759 call feedkeys("v", 'xt')
760 call assert_equal('v', mode())
761 call assert_equal('v', mode(1))
762 call feedkeys("\<Esc>V", 'xt')
763 call assert_equal('V', mode())
764 call assert_equal('V', mode(1))
765 call feedkeys("\<Esc>\<C-V>", 'xt')
766 call assert_equal("\<C-V>", mode())
767 call assert_equal("\<C-V>", mode(1))
768 call feedkeys("\<Esc>", 'xt')
769
770 call feedkeys("gh", 'xt')
771 call assert_equal('s', mode())
772 call assert_equal('s', mode(1))
773 call feedkeys("\<Esc>gH", 'xt')
774 call assert_equal('S', mode())
775 call assert_equal('S', mode(1))
776 call feedkeys("\<Esc>g\<C-H>", 'xt')
777 call assert_equal("\<C-S>", mode())
778 call assert_equal("\<C-S>", mode(1))
779 call feedkeys("\<Esc>", 'xt')
780
781 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
782 call assert_equal('c-c', g:current_modes)
783 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
784 call assert_equal('c-cv', g:current_modes)
785 " How to test Ex mode?
786
787 bwipe!
788 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100789 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100790endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100791
Bram Moolenaard2007022019-08-27 21:56:06 +0200792func Test_append()
793 enew!
794 split
795 call append(0, ["foo"])
796 split
797 only
798 undo
799endfunc
800
Bram Moolenaar79518e22017-02-17 16:31:35 +0100801func Test_getbufvar()
802 let bnr = bufnr('%')
803 let b:var_num = '1234'
804 let def_num = '5678'
805 call assert_equal('1234', getbufvar(bnr, 'var_num'))
806 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
807
808 let bd = getbufvar(bnr, '')
809 call assert_equal('1234', bd['var_num'])
810 call assert_true(exists("bd['changedtick']"))
811 call assert_equal(2, len(bd))
812
813 let bd2 = getbufvar(bnr, '', def_num)
814 call assert_equal(bd, bd2)
815
816 unlet b:var_num
817 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
818 call assert_equal('', getbufvar(bnr, 'var_num'))
819
820 let bd = getbufvar(bnr, '')
821 call assert_equal(1, len(bd))
822 let bd = getbufvar(bnr, '',def_num)
823 call assert_equal(1, len(bd))
824
Bram Moolenaar4520d442017-03-19 16:09:46 +0100825 call assert_equal('', getbufvar(9999, ''))
826 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100827 unlet def_num
828
Bram Moolenaar507647d2017-02-17 16:43:49 +0100829 call assert_equal(0, getbufvar(bnr, '&autoindent'))
830 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100831
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100832 " Set and get a buffer-local variable
833 call setbufvar(bnr, 'bufvar_test', ['one', 'two'])
834 call assert_equal(['one', 'two'], getbufvar(bnr, 'bufvar_test'))
835
Bram Moolenaar79518e22017-02-17 16:31:35 +0100836 " Open new window with forced option values
837 set fileformats=unix,dos
838 new ++ff=dos ++bin ++enc=iso-8859-2
839 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
840 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
841 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
842 close
843
844 set fileformats&
845endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100846
Bram Moolenaar41042f32017-03-09 12:09:32 +0100847func Test_last_buffer_nr()
848 call assert_equal(bufnr('$'), last_buffer_nr())
849endfunc
850
851func Test_stridx()
852 call assert_equal(-1, stridx('', 'l'))
853 call assert_equal(0, stridx('', ''))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200854 call assert_equal(0, 'hello'->stridx(''))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100855 call assert_equal(-1, stridx('hello', 'L'))
856 call assert_equal(2, stridx('hello', 'l', -1))
857 call assert_equal(2, stridx('hello', 'l', 0))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200858 call assert_equal(2, 'hello'->stridx('l', 1))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100859 call assert_equal(3, stridx('hello', 'l', 3))
860 call assert_equal(-1, stridx('hello', 'l', 4))
861 call assert_equal(-1, stridx('hello', 'l', 10))
862 call assert_equal(2, stridx('hello', 'll'))
863 call assert_equal(-1, stridx('hello', 'hello world'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100864 call assert_fails("let n=stridx('hello', [])", 'E730:')
865 call assert_fails("let n=stridx([], 'l')", 'E730:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100866endfunc
867
868func Test_strridx()
869 call assert_equal(-1, strridx('', 'l'))
870 call assert_equal(0, strridx('', ''))
871 call assert_equal(5, strridx('hello', ''))
872 call assert_equal(-1, strridx('hello', 'L'))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200873 call assert_equal(3, 'hello'->strridx('l'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100874 call assert_equal(3, strridx('hello', 'l', 10))
875 call assert_equal(3, strridx('hello', 'l', 3))
876 call assert_equal(2, strridx('hello', 'l', 2))
877 call assert_equal(-1, strridx('hello', 'l', 1))
878 call assert_equal(-1, strridx('hello', 'l', 0))
879 call assert_equal(-1, strridx('hello', 'l', -1))
880 call assert_equal(2, strridx('hello', 'll'))
881 call assert_equal(-1, strridx('hello', 'hello world'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100882 call assert_fails("let n=strridx('hello', [])", 'E730:')
883 call assert_fails("let n=strridx([], 'l')", 'E730:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100884endfunc
885
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200886func Test_match_func()
887 call assert_equal(4, match('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200888 call assert_equal(4, 'testing'->match('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200889 call assert_equal(-1, match('testing', 'ing', 5))
890 call assert_equal(-1, match('testing', 'ing', 8))
891 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
892 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100893 call assert_fails("let x=match('vim', [])", 'E730:')
894 call assert_equal(3, match(['a', 'b', 'c', 'a'], 'a', 1))
895 call assert_equal(-1, match(['a', 'b', 'c', 'a'], 'a', 5))
896 call assert_equal(4, match('testing', 'ing', -1))
897 call assert_fails("let x=match('testing', 'ing', 0, [])", 'E745:')
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200898endfunc
899
Bram Moolenaar41042f32017-03-09 12:09:32 +0100900func Test_matchend()
901 call assert_equal(7, matchend('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200902 call assert_equal(7, 'testing'->matchend('ing', 2))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100903 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200904 call assert_equal(-1, matchend('testing', 'ing', 8))
905 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
906 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
907endfunc
908
909func Test_matchlist()
910 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200911 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200912 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
913endfunc
914
915func Test_matchstr()
916 call assert_equal('ing', matchstr('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200917 call assert_equal('ing', 'testing'->matchstr('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200918 call assert_equal('', matchstr('testing', 'ing', 5))
919 call assert_equal('', matchstr('testing', 'ing', 8))
920 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
921 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
922endfunc
923
924func Test_matchstrpos()
925 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200926 call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200927 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
928 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
929 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
930 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100931endfunc
932
933func Test_nextnonblank_prevnonblank()
934 new
935insert
936This
937
938
939is
940
941a
942Test
943.
944 call assert_equal(0, nextnonblank(-1))
945 call assert_equal(0, nextnonblank(0))
946 call assert_equal(1, nextnonblank(1))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200947 call assert_equal(4, 2->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100948 call assert_equal(4, nextnonblank(3))
949 call assert_equal(4, nextnonblank(4))
950 call assert_equal(6, nextnonblank(5))
951 call assert_equal(6, nextnonblank(6))
952 call assert_equal(7, nextnonblank(7))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200953 call assert_equal(0, 8->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100954
955 call assert_equal(0, prevnonblank(-1))
956 call assert_equal(0, prevnonblank(0))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200957 call assert_equal(1, 1->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100958 call assert_equal(1, prevnonblank(2))
959 call assert_equal(1, prevnonblank(3))
960 call assert_equal(4, prevnonblank(4))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200961 call assert_equal(4, 5->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100962 call assert_equal(6, prevnonblank(6))
963 call assert_equal(7, prevnonblank(7))
964 call assert_equal(0, prevnonblank(8))
965 bw!
966endfunc
967
968func Test_byte2line_line2byte()
969 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200970 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +0100971 call setline(1, ['a', 'bc', 'd'])
972
973 set fileformat=unix
974 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
975 \ map(range(-1, 8), 'byte2line(v:val)'))
976 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
977 \ map(range(-1, 5), 'line2byte(v:val)'))
978
979 set fileformat=mac
980 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
Bram Moolenaar64b4d732019-08-22 22:18:17 +0200981 \ map(range(-1, 8), 'v:val->byte2line()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100982 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
Bram Moolenaar02b31112019-08-31 22:16:38 +0200983 \ map(range(-1, 5), 'v:val->line2byte()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100984
985 set fileformat=dos
986 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
987 \ map(range(-1, 11), 'byte2line(v:val)'))
988 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
989 \ map(range(-1, 5), 'line2byte(v:val)'))
990
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200991 bw!
992 set noendofline nofixendofline
993 normal a-
994 for ff in ["unix", "mac", "dos"]
995 let &fileformat = ff
996 call assert_equal(1, line2byte(1))
997 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
998 endfor
999
1000 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +01001001 bw!
1002endfunc
1003
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001004" Test for byteidx() and byteidxcomp() functions
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001005func Test_byteidx()
1006 let a = '.é.' " one char of two bytes
1007 call assert_equal(0, byteidx(a, 0))
1008 call assert_equal(0, byteidxcomp(a, 0))
1009 call assert_equal(1, byteidx(a, 1))
1010 call assert_equal(1, byteidxcomp(a, 1))
1011 call assert_equal(3, byteidx(a, 2))
1012 call assert_equal(3, byteidxcomp(a, 2))
1013 call assert_equal(4, byteidx(a, 3))
1014 call assert_equal(4, byteidxcomp(a, 3))
1015 call assert_equal(-1, byteidx(a, 4))
1016 call assert_equal(-1, byteidxcomp(a, 4))
1017
1018 let b = '.é.' " normal e with composing char
1019 call assert_equal(0, b->byteidx(0))
1020 call assert_equal(1, b->byteidx(1))
1021 call assert_equal(4, b->byteidx(2))
1022 call assert_equal(5, b->byteidx(3))
1023 call assert_equal(-1, b->byteidx(4))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001024 call assert_fails("call byteidx([], 0)", 'E730:')
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001025
1026 call assert_equal(0, b->byteidxcomp(0))
1027 call assert_equal(1, b->byteidxcomp(1))
1028 call assert_equal(2, b->byteidxcomp(2))
1029 call assert_equal(4, b->byteidxcomp(3))
1030 call assert_equal(5, b->byteidxcomp(4))
1031 call assert_equal(-1, b->byteidxcomp(5))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001032 call assert_fails("call byteidxcomp([], 0)", 'E730:')
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001033endfunc
1034
Bram Moolenaar41042f32017-03-09 12:09:32 +01001035func Test_count()
1036 let l = ['a', 'a', 'A', 'b']
1037 call assert_equal(2, count(l, 'a'))
1038 call assert_equal(1, count(l, 'A'))
1039 call assert_equal(1, count(l, 'b'))
1040 call assert_equal(0, count(l, 'B'))
1041
1042 call assert_equal(2, count(l, 'a', 0))
1043 call assert_equal(1, count(l, 'A', 0))
1044 call assert_equal(1, count(l, 'b', 0))
1045 call assert_equal(0, count(l, 'B', 0))
1046
1047 call assert_equal(3, count(l, 'a', 1))
1048 call assert_equal(3, count(l, 'A', 1))
1049 call assert_equal(1, count(l, 'b', 1))
1050 call assert_equal(1, count(l, 'B', 1))
1051 call assert_equal(0, count(l, 'c', 1))
1052
1053 call assert_equal(1, count(l, 'a', 0, 1))
1054 call assert_equal(2, count(l, 'a', 1, 1))
1055 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02001056 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001057
1058 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
1059 call assert_equal(2, count(d, 'a'))
1060 call assert_equal(1, count(d, 'A'))
1061 call assert_equal(1, count(d, 'b'))
1062 call assert_equal(0, count(d, 'B'))
1063
1064 call assert_equal(2, count(d, 'a', 0))
1065 call assert_equal(1, count(d, 'A', 0))
1066 call assert_equal(1, count(d, 'b', 0))
1067 call assert_equal(0, count(d, 'B', 0))
1068
1069 call assert_equal(3, count(d, 'a', 1))
1070 call assert_equal(3, count(d, 'A', 1))
1071 call assert_equal(1, count(d, 'b', 1))
1072 call assert_equal(1, count(d, 'B', 1))
1073 call assert_equal(0, count(d, 'c', 1))
1074
1075 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +02001076
1077 call assert_equal(0, count("foo", "bar"))
1078 call assert_equal(1, count("foo", "oo"))
1079 call assert_equal(2, count("foo", "o"))
1080 call assert_equal(0, count("foo", "O"))
1081 call assert_equal(2, count("foo", "O", 1))
1082 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +01001083 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001084
1085 call assert_fails('call count(0, 0)', 'E712:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001086endfunc
1087
1088func Test_changenr()
1089 new Xchangenr
1090 call assert_equal(0, changenr())
1091 norm ifoo
1092 call assert_equal(1, changenr())
1093 set undolevels=10
1094 norm Sbar
1095 call assert_equal(2, changenr())
1096 undo
1097 call assert_equal(1, changenr())
1098 redo
1099 call assert_equal(2, changenr())
1100 bw!
1101 set undolevels&
1102endfunc
1103
1104func Test_filewritable()
1105 new Xfilewritable
1106 write!
1107 call assert_equal(1, filewritable('Xfilewritable'))
1108
1109 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1110 call assert_equal(0, filewritable('Xfilewritable'))
1111
1112 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001113 call assert_equal(1, 'Xfilewritable'->filewritable())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001114
1115 call assert_equal(0, filewritable('doesnotexist'))
1116
1117 call delete('Xfilewritable')
1118 bw!
1119endfunc
1120
Bram Moolenaar82956662018-10-06 15:18:45 +02001121func Test_Executable()
1122 if has('win32')
1123 call assert_equal(1, executable('notepad'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001124 call assert_equal(1, 'notepad.exe'->executable())
Bram Moolenaar82956662018-10-06 15:18:45 +02001125 call assert_equal(0, executable('notepad.exe.exe'))
1126 call assert_equal(0, executable('shell32.dll'))
1127 call assert_equal(0, executable('win.ini'))
1128 elseif has('unix')
Bram Moolenaara4208962019-08-24 20:50:19 +02001129 call assert_equal(1, 'cat'->executable())
Bram Moolenaara05a0d32018-10-07 18:43:05 +02001130 call assert_equal(0, executable('nodogshere'))
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001131
1132 " get "cat" path and remove the leading /
1133 let catcmd = exepath('cat')[1:]
1134 new
Bram Moolenaara4208962019-08-24 20:50:19 +02001135 " check that the relative path works in /
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001136 lcd /
1137 call assert_equal(1, executable(catcmd))
Bram Moolenaara4208962019-08-24 20:50:19 +02001138 call assert_equal('/' .. catcmd, catcmd->exepath())
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001139 bwipe
Bram Moolenaar82956662018-10-06 15:18:45 +02001140 endif
1141endfunc
1142
Bram Moolenaar86621892019-03-30 21:51:28 +01001143func Test_executable_longname()
1144 if !has('win32')
1145 return
1146 endif
1147
1148 let fname = 'X' . repeat('あ', 200) . '.bat'
1149 call writefile([], fname)
1150 call assert_equal(1, executable(fname))
1151 call delete(fname)
1152endfunc
1153
Bram Moolenaar41042f32017-03-09 12:09:32 +01001154func Test_hostname()
1155 let hostname_vim = hostname()
1156 if has('unix')
1157 let hostname_system = systemlist('uname -n')[0]
1158 call assert_equal(hostname_vim, hostname_system)
1159 endif
1160endfunc
1161
1162func Test_getpid()
1163 " getpid() always returns the same value within a vim instance.
1164 call assert_equal(getpid(), getpid())
1165 if has('unix')
1166 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1167 endif
1168endfunc
1169
1170func Test_hlexists()
1171 call assert_equal(0, hlexists('does_not_exist'))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001172 call assert_equal(0, 'Number'->hlexists())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001173 call assert_equal(0, highlight_exists('does_not_exist'))
1174 call assert_equal(0, highlight_exists('Number'))
1175 syntax on
1176 call assert_equal(0, hlexists('does_not_exist'))
1177 call assert_equal(1, hlexists('Number'))
1178 call assert_equal(0, highlight_exists('does_not_exist'))
1179 call assert_equal(1, highlight_exists('Number'))
1180 syntax off
1181endfunc
1182
1183func Test_col()
1184 new
1185 call setline(1, 'abcdef')
1186 norm gg4|mx6|mY2|
1187 call assert_equal(2, col('.'))
1188 call assert_equal(7, col('$'))
Bram Moolenaar8b633132020-03-20 18:20:51 +01001189 call assert_equal(2, col('v'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001190 call assert_equal(4, col("'x"))
1191 call assert_equal(6, col("'Y"))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001192 call assert_equal(2, [1, 2]->col())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001193 call assert_equal(7, col([1, '$']))
1194
1195 call assert_equal(0, col(''))
1196 call assert_equal(0, col('x'))
1197 call assert_equal(0, col([2, '$']))
1198 call assert_equal(0, col([1, 100]))
1199 call assert_equal(0, col([1]))
Bram Moolenaar8b633132020-03-20 18:20:51 +01001200
1201 " test for getting the visual start column
1202 func T()
1203 let g:Vcol = col('v')
1204 return ''
1205 endfunc
1206 let g:Vcol = 0
1207 xmap <expr> <F2> T()
1208 exe "normal gg3|ve\<F2>"
1209 call assert_equal(3, g:Vcol)
1210 xunmap <F2>
1211 delfunc T
1212
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001213 " Test for the visual line start and end marks '< and '>
1214 call setline(1, ['one', 'one two', 'one two three'])
1215 "normal! ggVG
1216 call feedkeys("ggVG\<Esc>", 'xt')
1217 call assert_equal(1, col("'<"))
1218 call assert_equal(14, col("'>"))
1219 " Delete the last line of the visually selected region
1220 $d
1221 call assert_notequal(14, col("'>"))
1222
1223 " Test with 'virtualedit'
1224 set virtualedit=all
1225 call cursor(1, 10)
1226 call assert_equal(4, col('.'))
1227 set virtualedit&
1228
Bram Moolenaar41042f32017-03-09 12:09:32 +01001229 bw!
1230endfunc
1231
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001232" Test for input()
1233func Test_input_func()
1234 " Test for prompt with multiple lines
1235 redir => v
1236 call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt')
1237 redir END
1238 call assert_equal("B", c)
1239 call assert_equal(['A', 'B', 'C'], split(v, "\n"))
1240
1241 " Test for default value
1242 call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt')
1243 call assert_equal('red', c)
1244
1245 " Test for completion at the input prompt
1246 func! Tcomplete(arglead, cmdline, pos)
1247 return "item1\nitem2\nitem3"
1248 endfunc
1249 call feedkeys(":let c = input('Q? ', '' , 'custom,Tcomplete')\<CR>"
1250 \ .. "\<C-A>\<CR>", 'xt')
1251 delfunc Tcomplete
1252 call assert_equal('item1 item2 item3', c)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001253
1254 call assert_fails("call input('F:', '', 'invalid')", 'E180:')
1255 call assert_fails("call input('F:', '', [])", 'E730:')
1256endfunc
1257
1258" Test for the inputdialog() function
1259func Test_inputdialog()
1260 CheckNotGui
1261
1262 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt')
1263 call assert_equal('xx', v)
1264 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt')
1265 call assert_equal('yy', v)
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001266endfunc
1267
1268" Test for inputlist()
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001269func Test_inputlist()
1270 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1271 call assert_equal(1, c)
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001272 call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001273 call assert_equal(2, c)
1274 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1275 call assert_equal(3, c)
1276
1277 call assert_fails('call inputlist("")', 'E686:')
1278endfunc
1279
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001280func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001281 if has('balloon_eval')
1282 " This won't do anything but must not crash either.
1283 call balloon_show('hi!')
Bram Moolenaar7d8ea0b2020-01-27 23:01:30 +01001284 if !has('gui_running')
1285 call balloon_show(range(3))
1286 endif
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001287 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001288endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001289
1290func Test_setbufvar_options()
1291 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1292 " window layout.
1293 call assert_equal(1, winnr('$'))
1294 split dummy_preview
1295 resize 2
1296 set winfixheight winfixwidth
1297 let prev_id = win_getid()
1298
1299 wincmd j
1300 let wh = winheight('.')
1301 let dummy_buf = bufnr('dummy_buf1', v:true)
1302 call setbufvar(dummy_buf, '&buftype', 'nofile')
1303 execute 'belowright vertical split #' . dummy_buf
1304 call assert_equal(wh, winheight('.'))
1305 let dum1_id = win_getid()
1306
1307 wincmd h
1308 let wh = winheight('.')
1309 let dummy_buf = bufnr('dummy_buf2', v:true)
Bram Moolenaar196b4662019-09-06 21:34:30 +02001310 eval 'nofile'->setbufvar(dummy_buf, '&buftype')
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001311 execute 'belowright vertical split #' . dummy_buf
1312 call assert_equal(wh, winheight('.'))
1313
1314 bwipe!
1315 call win_gotoid(prev_id)
1316 bwipe!
1317 call win_gotoid(dum1_id)
1318 bwipe!
1319endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001320
1321func Test_redo_in_nested_functions()
1322 nnoremap g. :set opfunc=Operator<CR>g@
1323 function Operator( type, ... )
1324 let @x = 'XXX'
1325 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1326 endfunction
1327
1328 function! Apply()
1329 5,6normal! .
1330 endfunction
1331
1332 new
1333 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1334 1normal g.i"
1335 call assert_equal('some "XXX" text', getline(1))
1336 3,4normal .
1337 call assert_equal('some "XXX" text', getline(3))
1338 call assert_equal('more "XXX" text', getline(4))
1339 call Apply()
1340 call assert_equal('some "XXX" text', getline(5))
1341 call assert_equal('more "XXX" text', getline(6))
1342 bwipe!
1343
1344 nunmap g.
1345 delfunc Operator
1346 delfunc Apply
1347endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02001348
1349func Test_shellescape()
1350 let save_shell = &shell
1351 set shell=bash
1352 call assert_equal("'text'", shellescape('text'))
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001353 call assert_equal("'te\"xt'", 'te"xt'->shellescape())
Bram Moolenaar20615522017-06-05 18:46:26 +02001354 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
1355
1356 call assert_equal("'te%xt'", shellescape("te%xt"))
1357 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
1358 call assert_equal("'te#xt'", shellescape("te#xt"))
1359 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
1360 call assert_equal("'te!xt'", shellescape("te!xt"))
1361 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1362
1363 call assert_equal("'te\nxt'", shellescape("te\nxt"))
1364 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1365 set shell=tcsh
1366 call assert_equal("'te\\!xt'", shellescape("te!xt"))
1367 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1368 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1369 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1370
1371 let &shell = save_shell
1372endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001373
1374func Test_trim()
1375 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001376 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 +01001377 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1378 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
1379 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
1380 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
1381 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
1382 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1383 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1384 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
1385 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
1386 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
1387 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
1388 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1389 call assert_equal("", trim("", ""))
1390 call assert_equal("a", trim("a", ""))
1391 call assert_equal("", trim("", "a"))
1392
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001393 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001394 call assert_equal("x", trim(chars . "x" . chars))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001395
1396 call assert_fails('let c=trim([])', 'E730:')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001397endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001398
1399" Test for reg_recording() and reg_executing()
1400func Test_reg_executing_and_recording()
1401 let s:reg_stat = ''
1402 func s:save_reg_stat()
1403 let s:reg_stat = reg_recording() . ':' . reg_executing()
1404 return ''
1405 endfunc
1406
1407 new
1408 call s:save_reg_stat()
1409 call assert_equal(':', s:reg_stat)
1410 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1411 call assert_equal('a:', s:reg_stat)
1412 call feedkeys("@a", 'xt')
1413 call assert_equal(':a', s:reg_stat)
1414 call feedkeys("qb@aq", 'xt')
1415 call assert_equal('b:a', s:reg_stat)
1416 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1417 call assert_equal('":', s:reg_stat)
1418
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001419 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001420 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001421 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1422 func TestFunc() abort
1423 normal! ia
1424 endfunc
1425 call feedkeys("@q", 'xt')
1426 call assert_equal(':q', s:reg_stat)
1427 delfunc TestFunc
1428
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001429 " getchar() command saves and restores reg_executing
1430 map W :call TestFunc()<CR>
1431 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001432 let g:typed = ''
1433 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001434 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001435 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001436 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001437 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001438 endfunc
1439 call feedkeys("@qy", 'xt')
1440 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001441 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001442 delfunc TestFunc
1443 unmap W
1444 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001445 unlet g:regs
1446
1447 " input() command saves and restores reg_executing
1448 map W :call TestFunc()<CR>
1449 let @q = "W"
1450 let g:typed = ''
1451 let g:regs = []
1452 func TestFunc() abort
1453 let g:regs += [reg_executing()]
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001454 let g:typed = '?'->input()
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001455 let g:regs += [reg_executing()]
1456 endfunc
1457 call feedkeys("@qy\<CR>", 'xt')
1458 call assert_equal("y", g:typed)
1459 call assert_equal(['q', 'q'], g:regs)
1460 delfunc TestFunc
1461 unmap W
1462 unlet g:typed
1463 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001464
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001465 bwipe!
1466 delfunc s:save_reg_stat
1467 unlet s:reg_stat
1468endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001469
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001470func Test_inputsecret()
1471 map W :call TestFunc()<CR>
1472 let @q = "W"
1473 let g:typed1 = ''
1474 let g:typed2 = ''
1475 let g:regs = []
1476 func TestFunc() abort
1477 let g:typed1 = '?'->inputsecret()
1478 let g:typed2 = inputsecret('password: ')
1479 endfunc
1480 call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1481 call assert_equal("something", g:typed1)
1482 call assert_equal("else", g:typed2)
1483 delfunc TestFunc
1484 unmap W
1485 unlet g:typed1
1486 unlet g:typed2
1487endfunc
1488
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001489func Test_getchar()
1490 call feedkeys('a', '')
1491 call assert_equal(char2nr('a'), getchar())
1492
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001493 call setline(1, 'xxxx')
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001494 call test_setmouse(1, 3)
1495 let v:mouse_win = 9
1496 let v:mouse_winid = 9
1497 let v:mouse_lnum = 9
1498 let v:mouse_col = 9
1499 call feedkeys("\<S-LeftMouse>", '')
1500 call assert_equal("\<S-LeftMouse>", getchar())
1501 call assert_equal(1, v:mouse_win)
1502 call assert_equal(win_getid(1), v:mouse_winid)
1503 call assert_equal(1, v:mouse_lnum)
1504 call assert_equal(3, v:mouse_col)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001505 enew!
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001506endfunc
1507
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001508func Test_libcall_libcallnr()
1509 if !has('libcall')
1510 return
1511 endif
1512
1513 if has('win32')
1514 let libc = 'msvcrt.dll'
1515 elseif has('mac')
1516 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001517 elseif executable('ldd')
1518 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1519 endif
1520 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001521 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001522 if has('linux')
1523 " There is not documented but regarding the 1st argument of glibc's
1524 " dlopen an empty string and nullptr are equivalent, so using an empty
1525 " string for the 1st argument of libcall allows to call functions.
1526 let libc = ''
1527 elseif has('sun')
1528 " Set the path to libc.so according to the architecture.
1529 let test_bits = system('file ' . GetVimProg())
1530 let test_arch = system('uname -p')
1531 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1532 let libc = '/usr/lib/sparcv9/libc.so'
1533 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1534 let libc = '/usr/lib/amd64/libc.so'
1535 else
1536 let libc = '/usr/lib/libc.so'
1537 endif
1538 else
1539 " Unfortunately skip this test until a good way is found.
1540 return
1541 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001542 endif
1543
1544 if has('win32')
Bram Moolenaar02b31112019-08-31 22:16:38 +02001545 call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001546 else
Bram Moolenaar02b31112019-08-31 22:16:38 +02001547 call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001548 endif
1549
1550 " If function returns NULL, libcall() should return an empty string.
1551 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1552
1553 " Test libcallnr() with string and integer argument.
Bram Moolenaar02b31112019-08-31 22:16:38 +02001554 call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1555 call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001556
1557 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1558 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1559
1560 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1561 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1562endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001563
1564sandbox function Fsandbox()
1565 normal ix
1566endfunc
1567
1568func Test_func_sandbox()
1569 sandbox let F = {-> 'hello'}
1570 call assert_equal('hello', F())
1571
Bram Moolenaara4208962019-08-24 20:50:19 +02001572 sandbox let F = {-> "normal ix\<Esc>"->execute()}
Bram Moolenaard90a1442018-07-15 20:24:31 +02001573 call assert_fails('call F()', 'E48:')
1574 unlet F
1575
1576 call assert_fails('call Fsandbox()', 'E48:')
1577 delfunc Fsandbox
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001578
1579 " From a sandbox try to set a predefined variable (which cannot be modified
1580 " from a sandbox)
1581 call assert_fails('sandbox let v:lnum = 10', 'E794:')
Bram Moolenaard90a1442018-07-15 20:24:31 +02001582endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001583
1584func EditAnotherFile()
1585 let word = expand('<cword>')
1586 edit Xfuncrange2
1587endfunc
1588
1589func Test_func_range_with_edit()
1590 " Define a function that edits another buffer, then call it with a range that
1591 " is invalid in that buffer.
1592 call writefile(['just one line'], 'Xfuncrange2')
1593 new
Bram Moolenaar196b4662019-09-06 21:34:30 +02001594 eval 10->range()->setline(1)
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001595 write Xfuncrange1
1596 call assert_fails('5,8call EditAnotherFile()', 'E16:')
1597
1598 call delete('Xfuncrange1')
1599 call delete('Xfuncrange2')
1600 bwipe!
1601endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001602
1603func Test_func_exists_on_reload()
1604 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1605 call assert_equal(0, exists('*ExistingFunction'))
1606 source Xfuncexists
Bram Moolenaara4208962019-08-24 20:50:19 +02001607 call assert_equal(1, '*ExistingFunction'->exists())
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001608 " Redefining a function when reloading a script is OK.
1609 source Xfuncexists
1610 call assert_equal(1, exists('*ExistingFunction'))
1611
1612 " But redefining in another script is not OK.
1613 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1614 call assert_fails('source Xfuncexists2', 'E122:')
1615
1616 delfunc ExistingFunction
1617 call assert_equal(0, exists('*ExistingFunction'))
1618 call writefile([
1619 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1620 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1621 \ ], 'Xfuncexists')
1622 call assert_fails('source Xfuncexists', 'E122:')
1623 call assert_equal(1, exists('*ExistingFunction'))
1624
1625 call delete('Xfuncexists2')
1626 call delete('Xfuncexists')
1627 delfunc ExistingFunction
1628endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01001629
1630" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1631func Test_confirm()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001632 CheckUnix
1633 CheckNotGui
Bram Moolenaar2e050092019-01-27 15:00:36 +01001634
1635 call feedkeys('o', 'L')
1636 let a = confirm('Press O to proceed')
1637 call assert_equal(1, a)
1638
1639 call feedkeys('y', 'L')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001640 let a = 'Are you sure?'->confirm("&Yes\n&No")
Bram Moolenaar2e050092019-01-27 15:00:36 +01001641 call assert_equal(1, a)
1642
1643 call feedkeys('n', 'L')
1644 let a = confirm('Are you sure?', "&Yes\n&No")
1645 call assert_equal(2, a)
1646
1647 " confirm() should return 0 when pressing CTRL-C.
Bram Moolenaar79296512020-03-22 16:17:14 +01001648 call feedkeys("\<C-C>", 'L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01001649 let a = confirm('Are you sure?', "&Yes\n&No")
1650 call assert_equal(0, a)
1651
1652 " <Esc> requires another character to avoid it being seen as the start of an
1653 " escape sequence. Zero should be harmless.
Bram Moolenaara4208962019-08-24 20:50:19 +02001654 eval "\<Esc>0"->feedkeys('L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01001655 let a = confirm('Are you sure?', "&Yes\n&No")
1656 call assert_equal(0, a)
1657
1658 " Default choice is returned when pressing <CR>.
1659 call feedkeys("\<CR>", 'L')
1660 let a = confirm('Are you sure?', "&Yes\n&No")
1661 call assert_equal(1, a)
1662
1663 call feedkeys("\<CR>", 'L')
1664 let a = confirm('Are you sure?', "&Yes\n&No", 2)
1665 call assert_equal(2, a)
1666
1667 call feedkeys("\<CR>", 'L')
1668 let a = confirm('Are you sure?', "&Yes\n&No", 0)
1669 call assert_equal(0, a)
1670
1671 " Test with the {type} 4th argument
1672 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
1673 call feedkeys('y', 'L')
1674 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
1675 call assert_equal(1, a)
1676 endfor
1677
1678 call assert_fails('call confirm([])', 'E730:')
1679 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
1680 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
1681 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
1682endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001683
1684func Test_platform_name()
1685 " The system matches at most only one name.
1686 let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
1687 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
1688
1689 " Is Unix?
1690 call assert_equal(has('beos'), has('beos') && has('unix'))
1691 call assert_equal(has('bsd'), has('bsd') && has('unix'))
1692 call assert_equal(has('hpux'), has('hpux') && has('unix'))
1693 call assert_equal(has('linux'), has('linux') && has('unix'))
1694 call assert_equal(has('mac'), has('mac') && has('unix'))
1695 call assert_equal(has('qnx'), has('qnx') && has('unix'))
1696 call assert_equal(has('sun'), has('sun') && has('unix'))
1697 call assert_equal(has('win32'), has('win32') && !has('unix'))
1698 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
1699
1700 if has('unix') && executable('uname')
1701 let uname = system('uname')
1702 call assert_equal(uname =~? 'BeOS', has('beos'))
Bram Moolenaara02e3f62019-02-07 21:27:14 +01001703 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
1704 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001705 call assert_equal(uname =~? 'HP-UX', has('hpux'))
1706 call assert_equal(uname =~? 'Linux', has('linux'))
1707 call assert_equal(uname =~? 'Darwin', has('mac'))
1708 call assert_equal(uname =~? 'QNX', has('qnx'))
1709 call assert_equal(uname =~? 'SunOS', has('sun'))
1710 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
1711 endif
1712endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001713
1714func Test_readdir()
1715 call mkdir('Xdir')
1716 call writefile([], 'Xdir/foo.txt')
1717 call writefile([], 'Xdir/bar.txt')
1718 call mkdir('Xdir/dir')
1719
1720 " All results
1721 let files = readdir('Xdir')
1722 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
1723
1724 " Only results containing "f"
Bram Moolenaara0d1fef2019-09-04 22:29:14 +02001725 let files = 'Xdir'->readdir({ x -> stridx(x, 'f') !=- 1 })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001726 call assert_equal(['foo.txt'], sort(files))
1727
1728 " Only .txt files
1729 let files = readdir('Xdir', { x -> x =~ '.txt$' })
1730 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1731
1732 " Only .txt files with string
1733 let files = readdir('Xdir', 'v:val =~ ".txt$"')
1734 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1735
1736 " Limit to 1 result.
1737 let l = []
1738 let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
1739 call assert_equal(1, len(files))
1740
Bram Moolenaar27da7de2019-09-03 17:13:37 +02001741 " Nested readdir() must not crash
1742 let files = readdir('Xdir', 'readdir("Xdir", "1") != []')
1743 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
1744
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001745 eval 'Xdir'->delete('rf')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001746endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02001747
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001748func Test_delete_rf()
1749 call mkdir('Xdir')
1750 call writefile([], 'Xdir/foo.txt')
1751 call writefile([], 'Xdir/bar.txt')
1752 call mkdir('Xdir/[a-1]') " issue #696
1753 call writefile([], 'Xdir/[a-1]/foo.txt')
1754 call writefile([], 'Xdir/[a-1]/bar.txt')
1755 call assert_true(filereadable('Xdir/foo.txt'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001756 call assert_true('Xdir/[a-1]/foo.txt'->filereadable())
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001757
1758 call assert_equal(0, delete('Xdir', 'rf'))
1759 call assert_false(filereadable('Xdir/foo.txt'))
1760 call assert_false(filereadable('Xdir/[a-1]/foo.txt'))
1761endfunc
1762
Bram Moolenaar17aca702019-05-16 22:24:55 +02001763func Test_call()
1764 call assert_equal(3, call('len', [123]))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001765 call assert_equal(3, 'len'->call([123]))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001766 call assert_fails("call call('len', 123)", 'E714:')
1767 call assert_equal(0, call('', []))
1768
1769 function Mylen() dict
1770 return len(self.data)
1771 endfunction
1772 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001773 eval mydict.len->call([], mydict)->assert_equal(4)
Bram Moolenaar17aca702019-05-16 22:24:55 +02001774 call assert_fails("call call('Mylen', [], 0)", 'E715:')
1775endfunc
1776
1777func Test_char2nr()
1778 call assert_equal(12354, char2nr('あ', 1))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001779 call assert_equal(120, 'x'->char2nr())
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001780 set encoding=latin1
1781 call assert_equal(120, 'x'->char2nr())
1782 set encoding=utf-8
Bram Moolenaar17aca702019-05-16 22:24:55 +02001783endfunc
1784
1785func Test_eventhandler()
1786 call assert_equal(0, eventhandler())
1787endfunc
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001788
1789func Test_bufadd_bufload()
1790 call assert_equal(0, bufexists('someName'))
1791 let buf = bufadd('someName')
1792 call assert_notequal(0, buf)
1793 call assert_equal(1, bufexists('someName'))
1794 call assert_equal(0, getbufvar(buf, '&buflisted'))
1795 call assert_equal(0, bufloaded(buf))
1796 call bufload(buf)
1797 call assert_equal(1, bufloaded(buf))
1798 call assert_equal([''], getbufline(buf, 1, '$'))
1799
1800 let curbuf = bufnr('')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001801 eval ['some', 'text']->writefile('XotherName')
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001802 let buf = 'XotherName'->bufadd()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001803 call assert_notequal(0, buf)
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001804 eval 'XotherName'->bufexists()->assert_equal(1)
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001805 call assert_equal(0, getbufvar(buf, '&buflisted'))
1806 call assert_equal(0, bufloaded(buf))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001807 eval buf->bufload()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001808 call assert_equal(1, bufloaded(buf))
1809 call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
1810 call assert_equal(curbuf, bufnr(''))
1811
Bram Moolenaar892ae722019-06-30 20:33:01 +02001812 let buf1 = bufadd('')
1813 let buf2 = bufadd('')
1814 call assert_notequal(0, buf1)
1815 call assert_notequal(0, buf2)
1816 call assert_notequal(buf1, buf2)
1817 call assert_equal(1, bufexists(buf1))
1818 call assert_equal(1, bufexists(buf2))
1819 call assert_equal(0, bufloaded(buf1))
1820 exe 'bwipe ' .. buf1
1821 call assert_equal(0, bufexists(buf1))
1822 call assert_equal(1, bufexists(buf2))
1823 exe 'bwipe ' .. buf2
1824 call assert_equal(0, bufexists(buf2))
1825
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001826 bwipe someName
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001827 bwipe XotherName
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001828 call assert_equal(0, bufexists('someName'))
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001829 call delete('XotherName')
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001830endfunc
Bram Moolenaarc2585492019-09-22 21:29:53 +02001831
1832func Test_state()
1833 CheckRunVimInTerminal
1834
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +01001835 " In the first run try a short wait time. If the test fails retry with a
1836 " longer wait time.
1837 if g:run_nr == 1
1838 let wait_time = 50
1839 elseif g:run_nr == 2
1840 let wait_time = 200
1841 else
1842 let wait_time = 500
1843 endif
1844 let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"
1845
Bram Moolenaarc2585492019-09-22 21:29:53 +02001846 let lines =<< trim END
1847 call setline(1, ['one', 'two', 'three'])
1848 map ;; gg
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001849 set complete=.
Bram Moolenaarc2585492019-09-22 21:29:53 +02001850 func RunTimer()
1851 call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
1852 endfunc
1853 au Filetype foobar let g:state = state()|let g:mode = mode()
1854 END
1855 call writefile(lines, 'XState')
1856 let buf = RunVimInTerminal('-S XState', #{rows: 6})
1857
1858 " Using a ":" command Vim is busy, thus "S" is returned
1859 call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
1860 call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
1861 call term_sendkeys(buf, ":\<CR>")
1862
1863 " Using a timer callback
1864 call term_sendkeys(buf, ":call RunTimer()\<CR>")
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +01001865 call term_wait(buf, wait_time)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001866 call term_sendkeys(buf, getstate)
1867 call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)
1868
1869 " Halfway a mapping
1870 call term_sendkeys(buf, ":call RunTimer()\<CR>;")
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +01001871 call term_wait(buf, wait_time)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001872 call term_sendkeys(buf, ";")
1873 call term_sendkeys(buf, getstate)
1874 call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)
1875
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001876 " Insert mode completion (bit slower on Mac)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001877 call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +01001878 call term_wait(buf, wait_time)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001879 call term_sendkeys(buf, "\<Esc>")
1880 call term_sendkeys(buf, getstate)
1881 call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)
1882
1883 " Autocommand executing
1884 call term_sendkeys(buf, ":set filetype=foobar\<CR>")
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +01001885 call term_wait(buf, wait_time)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001886 call term_sendkeys(buf, getstate)
1887 call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)
1888
1889 " Todo: "w" - waiting for ch_evalexpr()
1890
1891 " messages scrolled
1892 call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +01001893 call term_wait(buf, wait_time)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001894 call term_sendkeys(buf, "\<CR>")
1895 call term_sendkeys(buf, getstate)
1896 call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)
1897
1898 call StopVimInTerminal(buf)
1899 call delete('XState')
1900endfunc
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001901
1902func Test_range()
1903 " destructuring
1904 let [x, y] = range(2)
1905 call assert_equal([0, 1], [x, y])
1906
1907 " index
1908 call assert_equal(4, range(1, 10)[3])
1909
1910 " add()
1911 call assert_equal([0, 1, 2, 3], add(range(3), 3))
1912 call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3)))
1913 call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3)))
1914
1915 " append()
1916 new
1917 call append('.', range(5))
1918 call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$'))
1919 bwipe!
1920
1921 " appendbufline()
1922 new
1923 call appendbufline(bufnr(''), '.', range(5))
1924 call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$'))
1925 bwipe!
1926
1927 " call()
1928 func TwoArgs(a, b)
1929 return [a:a, a:b]
1930 endfunc
1931 call assert_equal([0, 1], call('TwoArgs', range(2)))
1932
1933 " col()
1934 new
1935 call setline(1, ['foo', 'bar'])
1936 call assert_equal(2, col(range(1, 2)))
1937 bwipe!
1938
1939 " complete()
1940 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>"
1941 " complete_info()
1942 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>"
1943
1944 " copy()
1945 call assert_equal([1, 2, 3], copy(range(1, 3)))
1946
1947 " count()
1948 call assert_equal(0, count(range(0), 3))
1949 call assert_equal(0, count(range(2), 3))
1950 call assert_equal(1, count(range(5), 3))
1951
1952 " cursor()
1953 new
1954 call setline(1, ['aaa', 'bbb', 'ccc'])
1955 call cursor(range(1, 2))
1956 call assert_equal([2, 1], [col('.'), line('.')])
1957 bwipe!
1958
1959 " deepcopy()
1960 call assert_equal([1, 2, 3], deepcopy(range(1, 3)))
1961
1962 " empty()
1963 call assert_true(empty(range(0)))
1964 call assert_false(empty(range(2)))
1965
1966 " execute()
1967 new
1968 call setline(1, ['aaa', 'bbb', 'ccc'])
1969 call execute(range(3))
1970 call assert_equal(2, line('.'))
1971 bwipe!
1972
1973 " extend()
1974 call assert_equal([1, 2, 3, 4], extend([1], range(2, 4)))
1975 call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4)))
1976 call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4]))
1977
1978 " filter()
1979 call assert_equal([1, 3], filter(range(5), 'v:val % 2'))
1980
1981 " funcref()
1982 call assert_equal([0, 1], funcref('TwoArgs', range(2))())
1983
1984 " function()
1985 call assert_equal([0, 1], function('TwoArgs', range(2))())
1986
1987 " garbagecollect()
1988 let thelist = [1, range(2), 3]
1989 let otherlist = range(3)
1990 call test_garbagecollect_now()
1991
1992 " get()
1993 call assert_equal(4, get(range(1, 10), 3))
1994 call assert_equal(-1, get(range(1, 10), 42, -1))
1995
1996 " index()
1997 call assert_equal(1, index(range(1, 5), 2))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001998 call assert_fails("echo index([1, 2], 1, [])", 'E745:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001999
2000 " inputlist()
Bram Moolenaar272ca952020-01-28 20:49:11 +01002001 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x')
2002 call assert_equal(1, result)
2003 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x')
2004 call assert_equal(1, result)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002005
2006 " insert()
2007 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42))
2008 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0))
2009 call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1))
2010 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4))
2011 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1))
2012 call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5))
2013
2014 " join()
2015 call assert_equal('0 1 2 3 4', join(range(5)))
2016
Bram Moolenaar272ca952020-01-28 20:49:11 +01002017 " json_encode()
2018 call assert_equal('[0,1,2,3]', json_encode(range(4)))
2019
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002020 " len()
2021 call assert_equal(0, len(range(0)))
2022 call assert_equal(2, len(range(2)))
2023 call assert_equal(5, len(range(0, 12, 3)))
2024 call assert_equal(4, len(range(3, 0, -1)))
2025
2026 " list2str()
2027 call assert_equal('ABC', list2str(range(65, 67)))
2028
2029 " lock()
2030 let thelist = range(5)
2031 lockvar thelist
2032
2033 " map()
2034 call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2'))
2035
2036 " match()
2037 call assert_equal(3, match(range(5), 3))
2038
2039 " matchaddpos()
2040 highlight MyGreenGroup ctermbg=green guibg=green
2041 call matchaddpos('MyGreenGroup', range(line('.'), line('.')))
2042
2043 " matchend()
2044 call assert_equal(4, matchend(range(5), '4'))
2045 call assert_equal(3, matchend(range(1, 5), '4'))
2046 call assert_equal(-1, matchend(range(1, 5), '42'))
2047
2048 " matchstrpos()
2049 call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4'))
2050 call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4'))
2051 call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42'))
2052
2053 " max() reverse()
2054 call assert_equal(0, max(range(0)))
2055 call assert_equal(0, max(range(10, 9)))
2056 call assert_equal(9, max(range(10)))
2057 call assert_equal(18, max(range(0, 20, 3)))
2058 call assert_equal(20, max(range(20, 0, -3)))
2059 call assert_equal(99999, max(range(100000)))
2060 call assert_equal(99999, max(range(99999, 0, -1)))
2061 call assert_equal(99999, max(reverse(range(100000))))
2062 call assert_equal(99999, max(reverse(range(99999, 0, -1))))
2063
2064 " min() reverse()
2065 call assert_equal(0, min(range(0)))
2066 call assert_equal(0, min(range(10, 9)))
2067 call assert_equal(5, min(range(5, 10)))
2068 call assert_equal(5, min(range(5, 10, 3)))
2069 call assert_equal(2, min(range(20, 0, -3)))
2070 call assert_equal(0, min(range(100000)))
2071 call assert_equal(0, min(range(99999, 0, -1)))
2072 call assert_equal(0, min(reverse(range(100000))))
2073 call assert_equal(0, min(reverse(range(99999, 0, -1))))
2074
2075 " remove()
2076 call assert_equal(1, remove(range(1, 10), 0))
2077 call assert_equal(2, remove(range(1, 10), 1))
2078 call assert_equal(9, remove(range(1, 10), 8))
2079 call assert_equal(10, remove(range(1, 10), 9))
2080 call assert_equal(10, remove(range(1, 10), -1))
2081 call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4))
2082
2083 " repeat()
2084 call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2))
2085 call assert_equal([0, 1, 2], repeat(range(3), 1))
2086 call assert_equal([], repeat(range(3), 0))
2087 call assert_equal([], repeat(range(5, 4), 2))
2088 call assert_equal([], repeat(range(5, 4), 0))
2089
2090 " reverse()
2091 call assert_equal([2, 1, 0], reverse(range(3)))
2092 call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1)))
2093 call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10)))
2094 call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20)))
2095 call assert_equal([16, 13, 10], reverse(range(10, 18, 3)))
2096 call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3)))
2097 call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3)))
2098 call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3)))
2099 call assert_equal([], reverse(range(0)))
2100
2101 " TODO: setpos()
2102 " new
2103 " call setline(1, repeat([''], bufnr('')))
2104 " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6))
2105 " call setpos('x', range(bufnr(''), bufnr('') + 3))
2106 " bwipe!
2107
2108 " setreg()
2109 call setreg('a', range(3))
2110 call assert_equal("0\n1\n2\n", getreg('a'))
2111
Bram Moolenaarb0992022020-01-30 14:55:42 +01002112 " settagstack()
2113 call settagstack(1, #{items : range(4)})
Bram Moolenaar94255df2020-02-05 20:10:33 +01002114
Bram Moolenaarb0992022020-01-30 14:55:42 +01002115 " sign_define()
2116 call assert_fails("call sign_define(range(5))", "E715:")
2117 call assert_fails("call sign_placelist(range(5))", "E715:")
2118
2119 " sign_undefine()
2120 call assert_fails("call sign_undefine(range(5))", "E908:")
2121
2122 " sign_unplacelist()
2123 call assert_fails("call sign_unplacelist(range(5))", "E715:")
2124
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002125 " sort()
2126 call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1)))
2127
2128 " string()
2129 call assert_equal('[0, 1, 2, 3, 4]', string(range(5)))
2130
Bram Moolenaarb0992022020-01-30 14:55:42 +01002131 " taglist() with 'tagfunc'
2132 func TagFunc(pattern, flags, info)
2133 return range(10)
2134 endfunc
2135 set tagfunc=TagFunc
2136 call assert_fails("call taglist('asdf')", 'E987:')
2137 set tagfunc=
Bram Moolenaar94255df2020-02-05 20:10:33 +01002138
Bram Moolenaarb0992022020-01-30 14:55:42 +01002139 " term_start()
Bram Moolenaar705724e2020-01-31 21:13:42 +01002140 if has('terminal') && has('termguicolors')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002141 call assert_fails('call term_start(range(3, 4))', 'E474:')
2142 let g:terminal_ansi_colors = range(16)
Bram Moolenaar94255df2020-02-05 20:10:33 +01002143 if has('win32')
2144 let cmd = "cmd /c dir"
2145 else
2146 let cmd = "ls"
2147 endif
2148 call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"})', 'E475:')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002149 unlet g:terminal_ansi_colors
2150 endif
2151
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002152 " type()
2153 call assert_equal(v:t_list, type(range(5)))
2154
2155 " uniq()
2156 call assert_equal([0, 1, 2, 3, 4], uniq(range(5)))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002157
2158 " errors
2159 call assert_fails('let x=range(2, 8, 0)', 'E726:')
2160 call assert_fails('let x=range(3, 1)', 'E727:')
2161 call assert_fails('let x=range(1, 3, -2)', 'E727:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002162endfunc
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002163
2164func Test_echoraw()
2165 CheckScreendump
2166
2167 " Normally used for escape codes, but let's test with a CR.
2168 let lines =<< trim END
2169 call echoraw("hello\<CR>x")
2170 END
2171 call writefile(lines, 'XTest_echoraw')
2172 let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40})
2173 call VerifyScreenDump(buf, 'Test_functions_echoraw', {})
2174
2175 " clean up
2176 call StopVimInTerminal(buf)
2177 call delete('XTest_echoraw')
2178endfunc
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002179
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002180" Test for the eval() function
2181func Test_eval()
2182 call assert_fails("call eval('5 a')", 'E488:')
2183endfunc
2184
2185" Test for the nr2char() function
2186func Test_nr2char()
2187 set encoding=latin1
2188 call assert_equal('@', nr2char(64))
2189 set encoding=utf8
2190 call assert_equal('a', nr2char(97, 1))
2191 call assert_equal('a', nr2char(97, 0))
2192endfunc
2193
2194" Test for screenattr(), screenchar() and screenchars() functions
2195func Test_screen_functions()
2196 call assert_equal(-1, screenattr(-1, -1))
2197 call assert_equal(-1, screenchar(-1, -1))
2198 call assert_equal([], screenchars(-1, -1))
2199endfunc
2200
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002201" vim: shiftwidth=2 sts=2 expandtab