blob: 96d7f31f475514318aedd56413140af4c880cccc [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
27 call assert_equal(0, has('nonexistent'))
28 call assert_equal(0, has('nonexistent', 1))
29endfunc
30
Bram Moolenaar24c2e482017-01-29 15:45:12 +010031func Test_empty()
32 call assert_equal(1, empty(''))
33 call assert_equal(0, empty('a'))
34
35 call assert_equal(1, empty(0))
36 call assert_equal(1, empty(-0))
37 call assert_equal(0, empty(1))
38 call assert_equal(0, empty(-1))
39
Bram Moolenaar5feabe02020-01-30 18:24:53 +010040 if has('float')
41 call assert_equal(1, empty(0.0))
42 call assert_equal(1, empty(-0.0))
43 call assert_equal(0, empty(1.0))
44 call assert_equal(0, empty(-1.0))
45 call assert_equal(0, empty(1.0/0.0))
46 call assert_equal(0, empty(0.0/0.0))
47 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +010048
49 call assert_equal(1, empty([]))
50 call assert_equal(0, empty(['a']))
51
52 call assert_equal(1, empty({}))
53 call assert_equal(0, empty({'a':1}))
54
55 call assert_equal(1, empty(v:null))
56 call assert_equal(1, empty(v:none))
57 call assert_equal(1, empty(v:false))
58 call assert_equal(0, empty(v:true))
59
Bram Moolenaar41042f32017-03-09 12:09:32 +010060 if has('channel')
61 call assert_equal(1, empty(test_null_channel()))
62 endif
63 if has('job')
64 call assert_equal(1, empty(test_null_job()))
65 endif
66
Bram Moolenaar24c2e482017-01-29 15:45:12 +010067 call assert_equal(0, empty(function('Test_empty')))
Bram Moolenaar17aca702019-05-16 22:24:55 +020068 call assert_equal(0, empty(function('Test_empty', [0])))
Bram Moolenaar7c215c52020-02-29 13:43:27 +010069
70 call assert_fails("call empty(test_void())", 'E685:')
71 call assert_fails("call empty(test_unknown())", 'E685:')
Bram Moolenaar24c2e482017-01-29 15:45:12 +010072endfunc
73
Bram Moolenaardd589232020-02-29 17:38:12 +010074func Test_test_void()
75 call assert_fails('echo 1 == test_void()', 'E685:')
76 if has('float')
77 call assert_fails('echo 1.0 == test_void()', 'E685:')
78 endif
79 call assert_fails('let x = json_encode(test_void())', 'E685:')
80 call assert_fails('let x = copy(test_void())', 'E685:')
81 call assert_fails('let x = copy([test_void()])', 'E685:')
82endfunc
83
Bram Moolenaar24c2e482017-01-29 15:45:12 +010084func Test_len()
85 call assert_equal(1, len(0))
86 call assert_equal(2, len(12))
87
88 call assert_equal(0, len(''))
89 call assert_equal(2, len('ab'))
90
91 call assert_equal(0, len([]))
92 call assert_equal(2, len([2, 1]))
93
94 call assert_equal(0, len({}))
95 call assert_equal(2, len({'a': 1, 'b': 2}))
96
97 call assert_fails('call len(v:none)', 'E701:')
98 call assert_fails('call len({-> 0})', 'E701:')
99endfunc
100
101func Test_max()
102 call assert_equal(0, max([]))
103 call assert_equal(2, max([2]))
104 call assert_equal(2, max([1, 2]))
105 call assert_equal(2, max([1, 2, v:null]))
106
107 call assert_equal(0, max({}))
108 call assert_equal(2, max({'a':1, 'b':2}))
109
110 call assert_fails('call max(1)', 'E712:')
111 call assert_fails('call max(v:none)', 'E712:')
112endfunc
113
114func Test_min()
115 call assert_equal(0, min([]))
116 call assert_equal(2, min([2]))
117 call assert_equal(1, min([1, 2]))
118 call assert_equal(0, min([1, 2, v:null]))
119
120 call assert_equal(0, min({}))
121 call assert_equal(1, min({'a':1, 'b':2}))
122
123 call assert_fails('call min(1)', 'E712:')
124 call assert_fails('call min(v:none)', 'E712:')
125endfunc
126
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200127func Test_strwidth()
128 for aw in ['single', 'double']
129 exe 'set ambiwidth=' . aw
130 call assert_equal(0, strwidth(''))
131 call assert_equal(1, strwidth("\t"))
132 call assert_equal(3, strwidth('Vim'))
133 call assert_equal(4, strwidth(1234))
134 call assert_equal(5, strwidth(-1234))
135
Bram Moolenaar30276f22019-01-24 17:59:39 +0100136 call assert_equal(2, strwidth('😉'))
137 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
138 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200139
140 call assert_fails('call strwidth({->0})', 'E729:')
141 call assert_fails('call strwidth([])', 'E730:')
142 call assert_fails('call strwidth({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100143 if has('float')
144 call assert_fails('call strwidth(1.2)', 'E806:')
145 endif
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200146 endfor
147
148 set ambiwidth&
149endfunc
150
Bram Moolenaar08243d22017-01-10 16:12:29 +0100151func Test_str2nr()
152 call assert_equal(0, str2nr(''))
153 call assert_equal(1, str2nr('1'))
154 call assert_equal(1, str2nr(' 1 '))
155
156 call assert_equal(1, str2nr('+1'))
157 call assert_equal(1, str2nr('+ 1'))
158 call assert_equal(1, str2nr(' + 1 '))
159
160 call assert_equal(-1, str2nr('-1'))
161 call assert_equal(-1, str2nr('- 1'))
162 call assert_equal(-1, str2nr(' - 1 '))
163
164 call assert_equal(123456789, str2nr('123456789'))
165 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100166
167 call assert_equal(5, str2nr('101', 2))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200168 call assert_equal(5, '0b101'->str2nr(2))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100169 call assert_equal(5, str2nr('0B101', 2))
170 call assert_equal(-5, str2nr('-101', 2))
171 call assert_equal(-5, str2nr('-0b101', 2))
172 call assert_equal(-5, str2nr('-0B101', 2))
173
174 call assert_equal(65, str2nr('101', 8))
175 call assert_equal(65, str2nr('0101', 8))
176 call assert_equal(-65, str2nr('-101', 8))
177 call assert_equal(-65, str2nr('-0101', 8))
178
179 call assert_equal(11259375, str2nr('abcdef', 16))
180 call assert_equal(11259375, str2nr('ABCDEF', 16))
181 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
182 call assert_equal(11259375, str2nr('0xabcdef', 16))
183 call assert_equal(11259375, str2nr('0Xabcdef', 16))
184 call assert_equal(11259375, str2nr('0XABCDEF', 16))
185 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
186
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200187 call assert_equal(1, str2nr("1'000'000", 10, 0))
188 call assert_equal(256, str2nr("1'0000'0000", 2, 1))
189 call assert_equal(262144, str2nr("1'000'000", 8, 1))
190 call assert_equal(1000000, str2nr("1'000'000", 10, 1))
Bram Moolenaarea8dcf82019-09-15 21:12:22 +0200191 call assert_equal(1000, str2nr("1'000''000", 10, 1))
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200192 call assert_equal(65536, str2nr("1'00'00", 16, 1))
193
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100194 call assert_equal(0, str2nr('0x10'))
195 call assert_equal(0, str2nr('0b10'))
196 call assert_equal(1, str2nr('12', 2))
197 call assert_equal(1, str2nr('18', 8))
198 call assert_equal(1, str2nr('1g', 16))
199
200 call assert_equal(0, str2nr(v:null))
201 call assert_equal(0, str2nr(v:none))
202
203 call assert_fails('call str2nr([])', 'E730:')
204 call assert_fails('call str2nr({->2})', 'E729:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100205 if has('float')
206 call assert_fails('call str2nr(1.2)', 'E806:')
207 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100208 call assert_fails('call str2nr(10, [])', 'E474:')
209endfunc
210
211func Test_strftime()
Bram Moolenaar10455d42019-11-21 15:36:18 +0100212 CheckFunction strftime
213
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100214 " Format of strftime() depends on system. We assume
215 " that basic formats tested here are available and
216 " identical on all systems which support strftime().
217 "
218 " The 2nd parameter of strftime() is a local time, so the output day
219 " of strftime() can be 17 or 18, depending on timezone.
220 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
221 "
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200222 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 +0100223
224 call assert_fails('call strftime([])', 'E730:')
225 call assert_fails('call strftime("%Y", [])', 'E745:')
Bram Moolenaardb517302019-06-18 22:53:24 +0200226
227 " Check that the time changes after we change the timezone
228 " Save previous timezone value, if any
229 if exists('$TZ')
230 let tz = $TZ
231 endif
232
233 " Force EST and then UTC, save the current hour (24-hour clock) for each
234 let $TZ = 'EST' | let est = strftime('%H')
235 let $TZ = 'UTC' | let utc = strftime('%H')
236
237 " Those hours should be two bytes long, and should not be the same; if they
238 " are, a tzset(3) call may have failed somewhere
239 call assert_equal(strlen(est), 2)
240 call assert_equal(strlen(utc), 2)
Bram Moolenaar87652a72019-06-18 23:07:37 +0200241 " TODO: this fails on MS-Windows
242 if has('unix')
243 call assert_notequal(est, utc)
244 endif
Bram Moolenaardb517302019-06-18 22:53:24 +0200245
246 " If we cached a timezone value, put it back, otherwise clear it
247 if exists('tz')
248 let $TZ = tz
249 else
250 unlet $TZ
251 endif
Bram Moolenaar10455d42019-11-21 15:36:18 +0100252endfunc
Bram Moolenaardb517302019-06-18 22:53:24 +0200253
Bram Moolenaar10455d42019-11-21 15:36:18 +0100254func Test_strptime()
255 CheckFunction strptime
256
257 if exists('$TZ')
258 let tz = $TZ
259 endif
260 let $TZ = 'UTC'
261
Bram Moolenaar9a838fe2019-12-06 12:45:01 +0100262 call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
Bram Moolenaar10455d42019-11-21 15:36:18 +0100263
264 call assert_fails('call strptime()', 'E119:')
265 call assert_fails('call strptime("xxx")', 'E119:')
266 call assert_equal(0, strptime("%Y", ''))
267 call assert_equal(0, strptime("%Y", "xxx"))
268
269 if exists('tz')
270 let $TZ = tz
271 else
272 unlet $TZ
273 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100274endfunc
275
Bram Moolenaardce1e892019-02-10 23:18:53 +0100276func Test_resolve_unix()
Bram Moolenaar26109902018-10-06 15:43:17 +0200277 if !has('unix')
278 return
279 endif
280
281 " Xlink1 -> Xlink2
282 " Xlink2 -> Xlink3
283 silent !ln -s -f Xlink2 Xlink1
284 silent !ln -s -f Xlink3 Xlink2
285 call assert_equal('Xlink3', resolve('Xlink1'))
286 call assert_equal('./Xlink3', resolve('./Xlink1'))
287 call assert_equal('Xlink3/', resolve('Xlink2/'))
288 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
289 "call assert_equal('Xlink3/', resolve('Xlink1/'))
290 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
291 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
292 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
293
294 " Test resolve() with a symlink cycle.
295 " Xlink1 -> Xlink2
296 " Xlink2 -> Xlink3
297 " Xlink3 -> Xlink1
298 silent !ln -s -f Xlink1 Xlink3
299 call assert_fails('call resolve("Xlink1")', 'E655:')
300 call assert_fails('call resolve("./Xlink1")', 'E655:')
301 call assert_fails('call resolve("Xlink2")', 'E655:')
302 call assert_fails('call resolve("Xlink3")', 'E655:')
303 call delete('Xlink1')
304 call delete('Xlink2')
305 call delete('Xlink3')
306
307 silent !ln -s -f Xdir//Xfile Xlink
308 call assert_equal('Xdir/Xfile', resolve('Xlink'))
309 call delete('Xlink')
310
311 silent !ln -s -f Xlink2/ Xlink1
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200312 call assert_equal('Xlink2', 'Xlink1'->resolve())
Bram Moolenaar26109902018-10-06 15:43:17 +0200313 call assert_equal('Xlink2/', resolve('Xlink1/'))
314 call delete('Xlink1')
315
316 silent !ln -s -f ./Xlink2 Xlink1
317 call assert_equal('Xlink2', resolve('Xlink1'))
318 call assert_equal('./Xlink2', resolve('./Xlink1'))
319 call delete('Xlink1')
320endfunc
321
Bram Moolenaardce1e892019-02-10 23:18:53 +0100322func s:normalize_fname(fname)
323 let ret = substitute(a:fname, '\', '/', 'g')
324 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200325 return ret->tolower()
Bram Moolenaardce1e892019-02-10 23:18:53 +0100326endfunc
327
328func Test_resolve_win32()
329 if !has('win32')
330 return
331 endif
332
333 " test for shortcut file
334 if executable('cscript')
335 new Xfile
336 wq
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200337 let lines =<< trim END
338 Set fs = CreateObject("Scripting.FileSystemObject")
339 Set ws = WScript.CreateObject("WScript.Shell")
340 Set shortcut = ws.CreateShortcut("Xlink.lnk")
341 shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")
342 shortcut.Save
343 END
344 call writefile(lines, 'link.vbs')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100345 silent !cscript link.vbs
346 call delete('link.vbs')
347 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
348 call delete('Xfile')
349
350 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
351 call delete('Xlink.lnk')
352 else
353 echomsg 'skipped test for shortcut file'
354 endif
355
356 " remove files
357 call delete('Xlink')
358 call delete('Xdir', 'd')
359 call delete('Xfile')
360
361 " test for symbolic link to a file
362 new Xfile
363 wq
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200364 call assert_equal('Xfile', resolve('Xfile'))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100365 silent !mklink Xlink Xfile
366 if !v:shell_error
367 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink')))
368 call delete('Xlink')
369 else
370 echomsg 'skipped test for symbolic link to a file'
371 endif
372 call delete('Xfile')
373
374 " test for junction to a directory
375 call mkdir('Xdir')
376 silent !mklink /J Xlink Xdir
377 if !v:shell_error
378 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
379
380 call delete('Xdir', 'd')
381
382 " test for junction already removed
383 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
384 call delete('Xlink')
385 else
386 echomsg 'skipped test for junction to a directory'
387 call delete('Xdir', 'd')
388 endif
389
390 " test for symbolic link to a directory
391 call mkdir('Xdir')
392 silent !mklink /D Xlink Xdir
393 if !v:shell_error
394 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
395
396 call delete('Xdir', 'd')
397
398 " test for symbolic link already removed
399 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
400 call delete('Xlink')
401 else
402 echomsg 'skipped test for symbolic link to a directory'
403 call delete('Xdir', 'd')
404 endif
405
406 " test for buffer name
407 new Xfile
408 wq
409 silent !mklink Xlink Xfile
410 if !v:shell_error
411 edit Xlink
412 call assert_equal('Xlink', bufname('%'))
413 call delete('Xlink')
414 bw!
415 else
416 echomsg 'skipped test for buffer name'
417 endif
418 call delete('Xfile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200419
420 " test for reparse point
421 call mkdir('Xdir')
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200422 call assert_equal('Xdir', resolve('Xdir'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200423 silent !mklink /D Xdirlink Xdir
424 if !v:shell_error
425 w Xdir/text.txt
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200426 call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200427 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
428 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200429 call delete('Xdirlink')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200430 else
431 echomsg 'skipped test for reparse point'
432 endif
433
434 call delete('Xdir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100435endfunc
436
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100437func Test_simplify()
438 call assert_equal('', simplify(''))
439 call assert_equal('/', simplify('/'))
440 call assert_equal('/', simplify('/.'))
441 call assert_equal('/', simplify('/..'))
442 call assert_equal('/...', simplify('/...'))
443 call assert_equal('./dir/file', simplify('./dir/file'))
444 call assert_equal('./dir/file', simplify('.///dir//file'))
445 call assert_equal('./dir/file', simplify('./dir/./file'))
446 call assert_equal('./file', simplify('./dir/../file'))
447 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
448 call assert_equal('./file', simplify('dir/.././file'))
449
450 call assert_fails('call simplify({->0})', 'E729:')
451 call assert_fails('call simplify([])', 'E730:')
452 call assert_fails('call simplify({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100453 if has('float')
454 call assert_fails('call simplify(1.2)', 'E806:')
455 endif
Bram Moolenaar08243d22017-01-10 16:12:29 +0100456endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100457
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200458func Test_pathshorten()
459 call assert_equal('', pathshorten(''))
460 call assert_equal('foo', pathshorten('foo'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200461 call assert_equal('/foo', '/foo'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200462 call assert_equal('f/', pathshorten('foo/'))
463 call assert_equal('f/bar', pathshorten('foo/bar'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200464 call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200465 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
466 call assert_equal('.f/bar', pathshorten('.foo/bar'))
467 call assert_equal('~f/bar', pathshorten('~foo/bar'))
468 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
469 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
470 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
471endfunc
472
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100473func Test_strpart()
474 call assert_equal('de', strpart('abcdefg', 3, 2))
475 call assert_equal('ab', strpart('abcdefg', -2, 4))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200476 call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100477 call assert_equal('fg', strpart('abcdefg', 5, 4))
478 call assert_equal('defg', strpart('abcdefg', 3))
479
Bram Moolenaar30276f22019-01-24 17:59:39 +0100480 call assert_equal('lép', strpart('éléphant', 2, 4))
481 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100482endfunc
483
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100484func Test_tolower()
485 call assert_equal("", tolower(""))
486
487 " Test with all printable ASCII characters.
488 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
489 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
490
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100491 " Test with a few uppercase diacritics.
492 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
493 call assert_equal("bḃḇ", tolower("BḂḆ"))
494 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
495 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
496 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
497 call assert_equal("fḟ ", tolower("FḞ "))
498 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
499 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
500 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
501 call assert_equal("jĵ", tolower("JĴ"))
502 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
503 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
504 call assert_equal("mḿṁ", tolower("MḾṀ"))
505 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
506 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
507 call assert_equal("pṕṗ", tolower("PṔṖ"))
508 call assert_equal("q", tolower("Q"))
509 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
510 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
511 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
512 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
513 call assert_equal("vṽ", tolower("VṼ"))
514 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
515 call assert_equal("xẋẍ", tolower("XẊẌ"))
516 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
517 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
518
519 " Test with a few lowercase diacritics, which should remain unchanged.
520 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
521 call assert_equal("bḃḇ", tolower("bḃḇ"))
522 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
523 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
524 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
525 call assert_equal("fḟ", tolower("fḟ"))
526 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
527 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
528 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
529 call assert_equal("jĵǰ", tolower("jĵǰ"))
530 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
531 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
532 call assert_equal("mḿṁ ", tolower("mḿṁ "))
533 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
534 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
535 call assert_equal("pṕṗ", tolower("pṕṗ"))
536 call assert_equal("q", tolower("q"))
537 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
538 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
539 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
540 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
541 call assert_equal("vṽ", tolower("vṽ"))
542 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
543 call assert_equal("ẋẍ", tolower("ẋẍ"))
544 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
545 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
546
547 " According to https://twitter.com/jifa/status/625776454479970304
548 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
549 " in length (2 to 3 bytes) when lowercased. So let's test them.
550 call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100551
552 " This call to tolower with invalid utf8 sequence used to cause access to
553 " invalid memory.
554 call tolower("\xC0\x80\xC0")
555 call tolower("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100556endfunc
557
558func Test_toupper()
559 call assert_equal("", toupper(""))
560
561 " Test with all printable ASCII characters.
562 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
563 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
564
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100565 " Test with a few lowercase diacritics.
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200566 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100567 call assert_equal("BḂḆ", toupper("bḃḇ"))
568 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
569 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
570 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
571 call assert_equal("FḞ", toupper("fḟ"))
572 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
573 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
574 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
575 call assert_equal("JĴǰ", toupper("jĵǰ"))
576 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
577 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
578 call assert_equal("MḾṀ ", toupper("mḿṁ "))
579 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
580 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
581 call assert_equal("PṔṖ", toupper("pṕṗ"))
582 call assert_equal("Q", toupper("q"))
583 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
584 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
585 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
586 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
587 call assert_equal("VṼ", toupper("vṽ"))
588 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
589 call assert_equal("ẊẌ", toupper("ẋẍ"))
590 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
591 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
592
593 " Test that uppercase diacritics, which should remain unchanged.
594 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
595 call assert_equal("BḂḆ", toupper("BḂḆ"))
596 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
597 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
598 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
599 call assert_equal("FḞ ", toupper("FḞ "))
600 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
601 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
602 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
603 call assert_equal("JĴ", toupper("JĴ"))
604 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
605 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
606 call assert_equal("MḾṀ", toupper("MḾṀ"))
607 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
608 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
609 call assert_equal("PṔṖ", toupper("PṔṖ"))
610 call assert_equal("Q", toupper("Q"))
611 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
612 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
613 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
614 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
615 call assert_equal("VṼ", toupper("VṼ"))
616 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
617 call assert_equal("XẊẌ", toupper("XẊẌ"))
618 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
619 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
620
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100621 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100622
623 " This call to toupper with invalid utf8 sequence used to cause access to
624 " invalid memory.
625 call toupper("\xC0\x80\xC0")
626 call toupper("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100627endfunc
628
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200629func Test_tr()
630 call assert_equal('foo', tr('bar', 'bar', 'foo'))
631 call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
632endfunc
633
Bram Moolenaare90858d2017-02-01 17:24:34 +0100634" Tests for the mode() function
635let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100636func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100637 let g:current_modes = mode(0) . '-' . mode(1)
638 return ''
639endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100640
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100641func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100642 new
643 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
644
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100645 " Only complete from the current buffer.
646 set complete=.
647
Bram Moolenaare90858d2017-02-01 17:24:34 +0100648 inoremap <F2> <C-R>=Save_mode()<CR>
649
650 normal! 3G
651 exe "normal i\<F2>\<Esc>"
652 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100653 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100654 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
655 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100656 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100657 exe "normal iBro\<C-P>\<F2>\<Esc>u"
658 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100659 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100660 exe "normal iBa\<C-X>\<F2>\<Esc>u"
661 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100662 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100663 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
664 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100665 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100666 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
667 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100668 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100669 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
670 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100671 " i_CTRL-X CTRL-L: Multiple matches
672 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
673 call assert_equal('i-ic', g:current_modes)
674 " i_CTRL-X CTRL-L: Single match
675 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
676 call assert_equal('i-ic', g:current_modes)
677 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100678 exe "normal iCom\<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 CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100681 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
682 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100683 " i_CTRL-X CTRL-L: No match
684 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
685 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100686
Bram Moolenaare971df32017-02-05 14:15:29 +0100687 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100688 exe "normal RBa\<C-P>\<F2>\<Esc>u"
689 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100690 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100691 exe "normal RBro\<C-P>\<F2>\<Esc>u"
692 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100693 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100694 exe "normal RBa\<C-X>\<F2>\<Esc>u"
695 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100696 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100697 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
698 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100699 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100700 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
701 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100702 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100703 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
704 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100705 " R_CTRL-X CTRL-L: Multiple matches
706 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
707 call assert_equal('R-Rc', g:current_modes)
708 " R_CTRL-X CTRL-L: Single match
709 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
710 call assert_equal('R-Rc', g:current_modes)
711 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100712 exe "normal RCom\<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 CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100715 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
716 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100717 " R_CTRL-X CTRL-L: No match
718 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
719 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100720
Bram Moolenaara1449832019-09-01 20:16:52 +0200721 call assert_equal('n', 0->mode())
722 call assert_equal('n', 1->mode())
Bram Moolenaare90858d2017-02-01 17:24:34 +0100723
Bram Moolenaar612cc382018-07-29 15:34:26 +0200724 " i_CTRL-O
725 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
726 call assert_equal("n-niI", g:current_modes)
727
728 " R_CTRL-O
729 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
730 call assert_equal("n-niR", g:current_modes)
731
732 " gR_CTRL-O
733 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
734 call assert_equal("n-niV", g:current_modes)
735
Bram Moolenaare90858d2017-02-01 17:24:34 +0100736 " How to test operator-pending mode?
737
738 call feedkeys("v", 'xt')
739 call assert_equal('v', mode())
740 call assert_equal('v', mode(1))
741 call feedkeys("\<Esc>V", 'xt')
742 call assert_equal('V', mode())
743 call assert_equal('V', mode(1))
744 call feedkeys("\<Esc>\<C-V>", 'xt')
745 call assert_equal("\<C-V>", mode())
746 call assert_equal("\<C-V>", mode(1))
747 call feedkeys("\<Esc>", 'xt')
748
749 call feedkeys("gh", 'xt')
750 call assert_equal('s', mode())
751 call assert_equal('s', mode(1))
752 call feedkeys("\<Esc>gH", 'xt')
753 call assert_equal('S', mode())
754 call assert_equal('S', mode(1))
755 call feedkeys("\<Esc>g\<C-H>", 'xt')
756 call assert_equal("\<C-S>", mode())
757 call assert_equal("\<C-S>", mode(1))
758 call feedkeys("\<Esc>", 'xt')
759
760 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
761 call assert_equal('c-c', g:current_modes)
762 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
763 call assert_equal('c-cv', g:current_modes)
764 " How to test Ex mode?
765
766 bwipe!
767 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100768 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100769endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100770
Bram Moolenaard2007022019-08-27 21:56:06 +0200771func Test_append()
772 enew!
773 split
774 call append(0, ["foo"])
775 split
776 only
777 undo
778endfunc
779
Bram Moolenaar79518e22017-02-17 16:31:35 +0100780func Test_getbufvar()
781 let bnr = bufnr('%')
782 let b:var_num = '1234'
783 let def_num = '5678'
784 call assert_equal('1234', getbufvar(bnr, 'var_num'))
785 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
786
787 let bd = getbufvar(bnr, '')
788 call assert_equal('1234', bd['var_num'])
789 call assert_true(exists("bd['changedtick']"))
790 call assert_equal(2, len(bd))
791
792 let bd2 = getbufvar(bnr, '', def_num)
793 call assert_equal(bd, bd2)
794
795 unlet b:var_num
796 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
797 call assert_equal('', getbufvar(bnr, 'var_num'))
798
799 let bd = getbufvar(bnr, '')
800 call assert_equal(1, len(bd))
801 let bd = getbufvar(bnr, '',def_num)
802 call assert_equal(1, len(bd))
803
Bram Moolenaar4520d442017-03-19 16:09:46 +0100804 call assert_equal('', getbufvar(9999, ''))
805 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100806 unlet def_num
807
Bram Moolenaar507647d2017-02-17 16:43:49 +0100808 call assert_equal(0, getbufvar(bnr, '&autoindent'))
809 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100810
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100811 " Set and get a buffer-local variable
812 call setbufvar(bnr, 'bufvar_test', ['one', 'two'])
813 call assert_equal(['one', 'two'], getbufvar(bnr, 'bufvar_test'))
814
Bram Moolenaar79518e22017-02-17 16:31:35 +0100815 " Open new window with forced option values
816 set fileformats=unix,dos
817 new ++ff=dos ++bin ++enc=iso-8859-2
818 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
819 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
820 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
821 close
822
823 set fileformats&
824endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100825
Bram Moolenaar41042f32017-03-09 12:09:32 +0100826func Test_last_buffer_nr()
827 call assert_equal(bufnr('$'), last_buffer_nr())
828endfunc
829
830func Test_stridx()
831 call assert_equal(-1, stridx('', 'l'))
832 call assert_equal(0, stridx('', ''))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200833 call assert_equal(0, 'hello'->stridx(''))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100834 call assert_equal(-1, stridx('hello', 'L'))
835 call assert_equal(2, stridx('hello', 'l', -1))
836 call assert_equal(2, stridx('hello', 'l', 0))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200837 call assert_equal(2, 'hello'->stridx('l', 1))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100838 call assert_equal(3, stridx('hello', 'l', 3))
839 call assert_equal(-1, stridx('hello', 'l', 4))
840 call assert_equal(-1, stridx('hello', 'l', 10))
841 call assert_equal(2, stridx('hello', 'll'))
842 call assert_equal(-1, stridx('hello', 'hello world'))
843endfunc
844
845func Test_strridx()
846 call assert_equal(-1, strridx('', 'l'))
847 call assert_equal(0, strridx('', ''))
848 call assert_equal(5, strridx('hello', ''))
849 call assert_equal(-1, strridx('hello', 'L'))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200850 call assert_equal(3, 'hello'->strridx('l'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100851 call assert_equal(3, strridx('hello', 'l', 10))
852 call assert_equal(3, strridx('hello', 'l', 3))
853 call assert_equal(2, strridx('hello', 'l', 2))
854 call assert_equal(-1, strridx('hello', 'l', 1))
855 call assert_equal(-1, strridx('hello', 'l', 0))
856 call assert_equal(-1, strridx('hello', 'l', -1))
857 call assert_equal(2, strridx('hello', 'll'))
858 call assert_equal(-1, strridx('hello', 'hello world'))
859endfunc
860
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200861func Test_match_func()
862 call assert_equal(4, match('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200863 call assert_equal(4, 'testing'->match('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200864 call assert_equal(-1, match('testing', 'ing', 5))
865 call assert_equal(-1, match('testing', 'ing', 8))
866 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
867 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
868endfunc
869
Bram Moolenaar41042f32017-03-09 12:09:32 +0100870func Test_matchend()
871 call assert_equal(7, matchend('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200872 call assert_equal(7, 'testing'->matchend('ing', 2))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100873 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200874 call assert_equal(-1, matchend('testing', 'ing', 8))
875 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
876 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
877endfunc
878
879func Test_matchlist()
880 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200881 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200882 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
883endfunc
884
885func Test_matchstr()
886 call assert_equal('ing', matchstr('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200887 call assert_equal('ing', 'testing'->matchstr('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200888 call assert_equal('', matchstr('testing', 'ing', 5))
889 call assert_equal('', matchstr('testing', 'ing', 8))
890 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
891 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
892endfunc
893
894func Test_matchstrpos()
895 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200896 call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200897 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
898 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
899 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
900 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100901endfunc
902
903func Test_nextnonblank_prevnonblank()
904 new
905insert
906This
907
908
909is
910
911a
912Test
913.
914 call assert_equal(0, nextnonblank(-1))
915 call assert_equal(0, nextnonblank(0))
916 call assert_equal(1, nextnonblank(1))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200917 call assert_equal(4, 2->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100918 call assert_equal(4, nextnonblank(3))
919 call assert_equal(4, nextnonblank(4))
920 call assert_equal(6, nextnonblank(5))
921 call assert_equal(6, nextnonblank(6))
922 call assert_equal(7, nextnonblank(7))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200923 call assert_equal(0, 8->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100924
925 call assert_equal(0, prevnonblank(-1))
926 call assert_equal(0, prevnonblank(0))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200927 call assert_equal(1, 1->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100928 call assert_equal(1, prevnonblank(2))
929 call assert_equal(1, prevnonblank(3))
930 call assert_equal(4, prevnonblank(4))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200931 call assert_equal(4, 5->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100932 call assert_equal(6, prevnonblank(6))
933 call assert_equal(7, prevnonblank(7))
934 call assert_equal(0, prevnonblank(8))
935 bw!
936endfunc
937
938func Test_byte2line_line2byte()
939 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200940 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +0100941 call setline(1, ['a', 'bc', 'd'])
942
943 set fileformat=unix
944 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
945 \ map(range(-1, 8), 'byte2line(v:val)'))
946 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
947 \ map(range(-1, 5), 'line2byte(v:val)'))
948
949 set fileformat=mac
950 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
Bram Moolenaar64b4d732019-08-22 22:18:17 +0200951 \ map(range(-1, 8), 'v:val->byte2line()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100952 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
Bram Moolenaar02b31112019-08-31 22:16:38 +0200953 \ map(range(-1, 5), 'v:val->line2byte()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100954
955 set fileformat=dos
956 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
957 \ map(range(-1, 11), 'byte2line(v:val)'))
958 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
959 \ map(range(-1, 5), 'line2byte(v:val)'))
960
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200961 bw!
962 set noendofline nofixendofline
963 normal a-
964 for ff in ["unix", "mac", "dos"]
965 let &fileformat = ff
966 call assert_equal(1, line2byte(1))
967 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
968 endfor
969
970 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +0100971 bw!
972endfunc
973
Bram Moolenaar64b4d732019-08-22 22:18:17 +0200974func Test_byteidx()
975 let a = '.é.' " one char of two bytes
976 call assert_equal(0, byteidx(a, 0))
977 call assert_equal(0, byteidxcomp(a, 0))
978 call assert_equal(1, byteidx(a, 1))
979 call assert_equal(1, byteidxcomp(a, 1))
980 call assert_equal(3, byteidx(a, 2))
981 call assert_equal(3, byteidxcomp(a, 2))
982 call assert_equal(4, byteidx(a, 3))
983 call assert_equal(4, byteidxcomp(a, 3))
984 call assert_equal(-1, byteidx(a, 4))
985 call assert_equal(-1, byteidxcomp(a, 4))
986
987 let b = '.é.' " normal e with composing char
988 call assert_equal(0, b->byteidx(0))
989 call assert_equal(1, b->byteidx(1))
990 call assert_equal(4, b->byteidx(2))
991 call assert_equal(5, b->byteidx(3))
992 call assert_equal(-1, b->byteidx(4))
993
994 call assert_equal(0, b->byteidxcomp(0))
995 call assert_equal(1, b->byteidxcomp(1))
996 call assert_equal(2, b->byteidxcomp(2))
997 call assert_equal(4, b->byteidxcomp(3))
998 call assert_equal(5, b->byteidxcomp(4))
999 call assert_equal(-1, b->byteidxcomp(5))
1000endfunc
1001
Bram Moolenaar41042f32017-03-09 12:09:32 +01001002func Test_count()
1003 let l = ['a', 'a', 'A', 'b']
1004 call assert_equal(2, count(l, 'a'))
1005 call assert_equal(1, count(l, 'A'))
1006 call assert_equal(1, count(l, 'b'))
1007 call assert_equal(0, count(l, 'B'))
1008
1009 call assert_equal(2, count(l, 'a', 0))
1010 call assert_equal(1, count(l, 'A', 0))
1011 call assert_equal(1, count(l, 'b', 0))
1012 call assert_equal(0, count(l, 'B', 0))
1013
1014 call assert_equal(3, count(l, 'a', 1))
1015 call assert_equal(3, count(l, 'A', 1))
1016 call assert_equal(1, count(l, 'b', 1))
1017 call assert_equal(1, count(l, 'B', 1))
1018 call assert_equal(0, count(l, 'c', 1))
1019
1020 call assert_equal(1, count(l, 'a', 0, 1))
1021 call assert_equal(2, count(l, 'a', 1, 1))
1022 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02001023 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001024
1025 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
1026 call assert_equal(2, count(d, 'a'))
1027 call assert_equal(1, count(d, 'A'))
1028 call assert_equal(1, count(d, 'b'))
1029 call assert_equal(0, count(d, 'B'))
1030
1031 call assert_equal(2, count(d, 'a', 0))
1032 call assert_equal(1, count(d, 'A', 0))
1033 call assert_equal(1, count(d, 'b', 0))
1034 call assert_equal(0, count(d, 'B', 0))
1035
1036 call assert_equal(3, count(d, 'a', 1))
1037 call assert_equal(3, count(d, 'A', 1))
1038 call assert_equal(1, count(d, 'b', 1))
1039 call assert_equal(1, count(d, 'B', 1))
1040 call assert_equal(0, count(d, 'c', 1))
1041
1042 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +02001043
1044 call assert_equal(0, count("foo", "bar"))
1045 call assert_equal(1, count("foo", "oo"))
1046 call assert_equal(2, count("foo", "o"))
1047 call assert_equal(0, count("foo", "O"))
1048 call assert_equal(2, count("foo", "O", 1))
1049 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +01001050 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001051
1052 call assert_fails('call count(0, 0)', 'E712:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001053endfunc
1054
1055func Test_changenr()
1056 new Xchangenr
1057 call assert_equal(0, changenr())
1058 norm ifoo
1059 call assert_equal(1, changenr())
1060 set undolevels=10
1061 norm Sbar
1062 call assert_equal(2, changenr())
1063 undo
1064 call assert_equal(1, changenr())
1065 redo
1066 call assert_equal(2, changenr())
1067 bw!
1068 set undolevels&
1069endfunc
1070
1071func Test_filewritable()
1072 new Xfilewritable
1073 write!
1074 call assert_equal(1, filewritable('Xfilewritable'))
1075
1076 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1077 call assert_equal(0, filewritable('Xfilewritable'))
1078
1079 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001080 call assert_equal(1, 'Xfilewritable'->filewritable())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001081
1082 call assert_equal(0, filewritable('doesnotexist'))
1083
1084 call delete('Xfilewritable')
1085 bw!
1086endfunc
1087
Bram Moolenaar82956662018-10-06 15:18:45 +02001088func Test_Executable()
1089 if has('win32')
1090 call assert_equal(1, executable('notepad'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001091 call assert_equal(1, 'notepad.exe'->executable())
Bram Moolenaar82956662018-10-06 15:18:45 +02001092 call assert_equal(0, executable('notepad.exe.exe'))
1093 call assert_equal(0, executable('shell32.dll'))
1094 call assert_equal(0, executable('win.ini'))
1095 elseif has('unix')
Bram Moolenaara4208962019-08-24 20:50:19 +02001096 call assert_equal(1, 'cat'->executable())
Bram Moolenaara05a0d32018-10-07 18:43:05 +02001097 call assert_equal(0, executable('nodogshere'))
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001098
1099 " get "cat" path and remove the leading /
1100 let catcmd = exepath('cat')[1:]
1101 new
Bram Moolenaara4208962019-08-24 20:50:19 +02001102 " check that the relative path works in /
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001103 lcd /
1104 call assert_equal(1, executable(catcmd))
Bram Moolenaara4208962019-08-24 20:50:19 +02001105 call assert_equal('/' .. catcmd, catcmd->exepath())
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001106 bwipe
Bram Moolenaar82956662018-10-06 15:18:45 +02001107 endif
1108endfunc
1109
Bram Moolenaar86621892019-03-30 21:51:28 +01001110func Test_executable_longname()
1111 if !has('win32')
1112 return
1113 endif
1114
1115 let fname = 'X' . repeat('あ', 200) . '.bat'
1116 call writefile([], fname)
1117 call assert_equal(1, executable(fname))
1118 call delete(fname)
1119endfunc
1120
Bram Moolenaar41042f32017-03-09 12:09:32 +01001121func Test_hostname()
1122 let hostname_vim = hostname()
1123 if has('unix')
1124 let hostname_system = systemlist('uname -n')[0]
1125 call assert_equal(hostname_vim, hostname_system)
1126 endif
1127endfunc
1128
1129func Test_getpid()
1130 " getpid() always returns the same value within a vim instance.
1131 call assert_equal(getpid(), getpid())
1132 if has('unix')
1133 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1134 endif
1135endfunc
1136
1137func Test_hlexists()
1138 call assert_equal(0, hlexists('does_not_exist'))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001139 call assert_equal(0, 'Number'->hlexists())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001140 call assert_equal(0, highlight_exists('does_not_exist'))
1141 call assert_equal(0, highlight_exists('Number'))
1142 syntax on
1143 call assert_equal(0, hlexists('does_not_exist'))
1144 call assert_equal(1, hlexists('Number'))
1145 call assert_equal(0, highlight_exists('does_not_exist'))
1146 call assert_equal(1, highlight_exists('Number'))
1147 syntax off
1148endfunc
1149
1150func Test_col()
1151 new
1152 call setline(1, 'abcdef')
1153 norm gg4|mx6|mY2|
1154 call assert_equal(2, col('.'))
1155 call assert_equal(7, col('$'))
Bram Moolenaar8b633132020-03-20 18:20:51 +01001156 call assert_equal(2, col('v'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001157 call assert_equal(4, col("'x"))
1158 call assert_equal(6, col("'Y"))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001159 call assert_equal(2, [1, 2]->col())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001160 call assert_equal(7, col([1, '$']))
1161
1162 call assert_equal(0, col(''))
1163 call assert_equal(0, col('x'))
1164 call assert_equal(0, col([2, '$']))
1165 call assert_equal(0, col([1, 100]))
1166 call assert_equal(0, col([1]))
Bram Moolenaar8b633132020-03-20 18:20:51 +01001167
1168 " test for getting the visual start column
1169 func T()
1170 let g:Vcol = col('v')
1171 return ''
1172 endfunc
1173 let g:Vcol = 0
1174 xmap <expr> <F2> T()
1175 exe "normal gg3|ve\<F2>"
1176 call assert_equal(3, g:Vcol)
1177 xunmap <F2>
1178 delfunc T
1179
Bram Moolenaar41042f32017-03-09 12:09:32 +01001180 bw!
1181endfunc
1182
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001183" Test for input()
1184func Test_input_func()
1185 " Test for prompt with multiple lines
1186 redir => v
1187 call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt')
1188 redir END
1189 call assert_equal("B", c)
1190 call assert_equal(['A', 'B', 'C'], split(v, "\n"))
1191
1192 " Test for default value
1193 call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt')
1194 call assert_equal('red', c)
1195
1196 " Test for completion at the input prompt
1197 func! Tcomplete(arglead, cmdline, pos)
1198 return "item1\nitem2\nitem3"
1199 endfunc
1200 call feedkeys(":let c = input('Q? ', '' , 'custom,Tcomplete')\<CR>"
1201 \ .. "\<C-A>\<CR>", 'xt')
1202 delfunc Tcomplete
1203 call assert_equal('item1 item2 item3', c)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001204
1205 call assert_fails("call input('F:', '', 'invalid')", 'E180:')
1206 call assert_fails("call input('F:', '', [])", 'E730:')
1207endfunc
1208
1209" Test for the inputdialog() function
1210func Test_inputdialog()
1211 CheckNotGui
1212
1213 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt')
1214 call assert_equal('xx', v)
1215 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt')
1216 call assert_equal('yy', v)
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001217endfunc
1218
1219" Test for inputlist()
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001220func Test_inputlist()
1221 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1222 call assert_equal(1, c)
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001223 call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001224 call assert_equal(2, c)
1225 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1226 call assert_equal(3, c)
1227
1228 call assert_fails('call inputlist("")', 'E686:')
1229endfunc
1230
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001231func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001232 if has('balloon_eval')
1233 " This won't do anything but must not crash either.
1234 call balloon_show('hi!')
Bram Moolenaar7d8ea0b2020-01-27 23:01:30 +01001235 if !has('gui_running')
1236 call balloon_show(range(3))
1237 endif
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001238 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001239endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001240
1241func Test_setbufvar_options()
1242 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1243 " window layout.
1244 call assert_equal(1, winnr('$'))
1245 split dummy_preview
1246 resize 2
1247 set winfixheight winfixwidth
1248 let prev_id = win_getid()
1249
1250 wincmd j
1251 let wh = winheight('.')
1252 let dummy_buf = bufnr('dummy_buf1', v:true)
1253 call setbufvar(dummy_buf, '&buftype', 'nofile')
1254 execute 'belowright vertical split #' . dummy_buf
1255 call assert_equal(wh, winheight('.'))
1256 let dum1_id = win_getid()
1257
1258 wincmd h
1259 let wh = winheight('.')
1260 let dummy_buf = bufnr('dummy_buf2', v:true)
Bram Moolenaar196b4662019-09-06 21:34:30 +02001261 eval 'nofile'->setbufvar(dummy_buf, '&buftype')
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001262 execute 'belowright vertical split #' . dummy_buf
1263 call assert_equal(wh, winheight('.'))
1264
1265 bwipe!
1266 call win_gotoid(prev_id)
1267 bwipe!
1268 call win_gotoid(dum1_id)
1269 bwipe!
1270endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001271
1272func Test_redo_in_nested_functions()
1273 nnoremap g. :set opfunc=Operator<CR>g@
1274 function Operator( type, ... )
1275 let @x = 'XXX'
1276 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1277 endfunction
1278
1279 function! Apply()
1280 5,6normal! .
1281 endfunction
1282
1283 new
1284 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1285 1normal g.i"
1286 call assert_equal('some "XXX" text', getline(1))
1287 3,4normal .
1288 call assert_equal('some "XXX" text', getline(3))
1289 call assert_equal('more "XXX" text', getline(4))
1290 call Apply()
1291 call assert_equal('some "XXX" text', getline(5))
1292 call assert_equal('more "XXX" text', getline(6))
1293 bwipe!
1294
1295 nunmap g.
1296 delfunc Operator
1297 delfunc Apply
1298endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02001299
1300func Test_shellescape()
1301 let save_shell = &shell
1302 set shell=bash
1303 call assert_equal("'text'", shellescape('text'))
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001304 call assert_equal("'te\"xt'", 'te"xt'->shellescape())
Bram Moolenaar20615522017-06-05 18:46:26 +02001305 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
1306
1307 call assert_equal("'te%xt'", shellescape("te%xt"))
1308 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
1309 call assert_equal("'te#xt'", shellescape("te#xt"))
1310 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
1311 call assert_equal("'te!xt'", shellescape("te!xt"))
1312 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1313
1314 call assert_equal("'te\nxt'", shellescape("te\nxt"))
1315 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1316 set shell=tcsh
1317 call assert_equal("'te\\!xt'", shellescape("te!xt"))
1318 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1319 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1320 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1321
1322 let &shell = save_shell
1323endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001324
1325func Test_trim()
1326 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001327 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 +01001328 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1329 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
1330 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
1331 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
1332 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
1333 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1334 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1335 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
1336 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
1337 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
1338 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
1339 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1340 call assert_equal("", trim("", ""))
1341 call assert_equal("a", trim("a", ""))
1342 call assert_equal("", trim("", "a"))
1343
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001344 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001345 call assert_equal("x", trim(chars . "x" . chars))
1346endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001347
1348" Test for reg_recording() and reg_executing()
1349func Test_reg_executing_and_recording()
1350 let s:reg_stat = ''
1351 func s:save_reg_stat()
1352 let s:reg_stat = reg_recording() . ':' . reg_executing()
1353 return ''
1354 endfunc
1355
1356 new
1357 call s:save_reg_stat()
1358 call assert_equal(':', s:reg_stat)
1359 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1360 call assert_equal('a:', s:reg_stat)
1361 call feedkeys("@a", 'xt')
1362 call assert_equal(':a', s:reg_stat)
1363 call feedkeys("qb@aq", 'xt')
1364 call assert_equal('b:a', s:reg_stat)
1365 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1366 call assert_equal('":', s:reg_stat)
1367
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001368 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001369 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001370 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1371 func TestFunc() abort
1372 normal! ia
1373 endfunc
1374 call feedkeys("@q", 'xt')
1375 call assert_equal(':q', s:reg_stat)
1376 delfunc TestFunc
1377
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001378 " getchar() command saves and restores reg_executing
1379 map W :call TestFunc()<CR>
1380 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001381 let g:typed = ''
1382 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001383 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001384 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001385 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001386 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001387 endfunc
1388 call feedkeys("@qy", 'xt')
1389 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001390 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001391 delfunc TestFunc
1392 unmap W
1393 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001394 unlet g:regs
1395
1396 " input() command saves and restores reg_executing
1397 map W :call TestFunc()<CR>
1398 let @q = "W"
1399 let g:typed = ''
1400 let g:regs = []
1401 func TestFunc() abort
1402 let g:regs += [reg_executing()]
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001403 let g:typed = '?'->input()
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001404 let g:regs += [reg_executing()]
1405 endfunc
1406 call feedkeys("@qy\<CR>", 'xt')
1407 call assert_equal("y", g:typed)
1408 call assert_equal(['q', 'q'], g:regs)
1409 delfunc TestFunc
1410 unmap W
1411 unlet g:typed
1412 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001413
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001414 bwipe!
1415 delfunc s:save_reg_stat
1416 unlet s:reg_stat
1417endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001418
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001419func Test_inputsecret()
1420 map W :call TestFunc()<CR>
1421 let @q = "W"
1422 let g:typed1 = ''
1423 let g:typed2 = ''
1424 let g:regs = []
1425 func TestFunc() abort
1426 let g:typed1 = '?'->inputsecret()
1427 let g:typed2 = inputsecret('password: ')
1428 endfunc
1429 call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1430 call assert_equal("something", g:typed1)
1431 call assert_equal("else", g:typed2)
1432 delfunc TestFunc
1433 unmap W
1434 unlet g:typed1
1435 unlet g:typed2
1436endfunc
1437
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001438func Test_getchar()
1439 call feedkeys('a', '')
1440 call assert_equal(char2nr('a'), getchar())
1441
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001442 call setline(1, 'xxxx')
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001443 call test_setmouse(1, 3)
1444 let v:mouse_win = 9
1445 let v:mouse_winid = 9
1446 let v:mouse_lnum = 9
1447 let v:mouse_col = 9
1448 call feedkeys("\<S-LeftMouse>", '')
1449 call assert_equal("\<S-LeftMouse>", getchar())
1450 call assert_equal(1, v:mouse_win)
1451 call assert_equal(win_getid(1), v:mouse_winid)
1452 call assert_equal(1, v:mouse_lnum)
1453 call assert_equal(3, v:mouse_col)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001454 enew!
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001455endfunc
1456
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001457func Test_libcall_libcallnr()
1458 if !has('libcall')
1459 return
1460 endif
1461
1462 if has('win32')
1463 let libc = 'msvcrt.dll'
1464 elseif has('mac')
1465 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001466 elseif executable('ldd')
1467 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1468 endif
1469 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001470 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001471 if has('linux')
1472 " There is not documented but regarding the 1st argument of glibc's
1473 " dlopen an empty string and nullptr are equivalent, so using an empty
1474 " string for the 1st argument of libcall allows to call functions.
1475 let libc = ''
1476 elseif has('sun')
1477 " Set the path to libc.so according to the architecture.
1478 let test_bits = system('file ' . GetVimProg())
1479 let test_arch = system('uname -p')
1480 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1481 let libc = '/usr/lib/sparcv9/libc.so'
1482 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1483 let libc = '/usr/lib/amd64/libc.so'
1484 else
1485 let libc = '/usr/lib/libc.so'
1486 endif
1487 else
1488 " Unfortunately skip this test until a good way is found.
1489 return
1490 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001491 endif
1492
1493 if has('win32')
Bram Moolenaar02b31112019-08-31 22:16:38 +02001494 call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001495 else
Bram Moolenaar02b31112019-08-31 22:16:38 +02001496 call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001497 endif
1498
1499 " If function returns NULL, libcall() should return an empty string.
1500 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1501
1502 " Test libcallnr() with string and integer argument.
Bram Moolenaar02b31112019-08-31 22:16:38 +02001503 call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1504 call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001505
1506 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1507 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1508
1509 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1510 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1511endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001512
1513sandbox function Fsandbox()
1514 normal ix
1515endfunc
1516
1517func Test_func_sandbox()
1518 sandbox let F = {-> 'hello'}
1519 call assert_equal('hello', F())
1520
Bram Moolenaara4208962019-08-24 20:50:19 +02001521 sandbox let F = {-> "normal ix\<Esc>"->execute()}
Bram Moolenaard90a1442018-07-15 20:24:31 +02001522 call assert_fails('call F()', 'E48:')
1523 unlet F
1524
1525 call assert_fails('call Fsandbox()', 'E48:')
1526 delfunc Fsandbox
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001527
1528 " From a sandbox try to set a predefined variable (which cannot be modified
1529 " from a sandbox)
1530 call assert_fails('sandbox let v:lnum = 10', 'E794:')
Bram Moolenaard90a1442018-07-15 20:24:31 +02001531endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001532
1533func EditAnotherFile()
1534 let word = expand('<cword>')
1535 edit Xfuncrange2
1536endfunc
1537
1538func Test_func_range_with_edit()
1539 " Define a function that edits another buffer, then call it with a range that
1540 " is invalid in that buffer.
1541 call writefile(['just one line'], 'Xfuncrange2')
1542 new
Bram Moolenaar196b4662019-09-06 21:34:30 +02001543 eval 10->range()->setline(1)
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001544 write Xfuncrange1
1545 call assert_fails('5,8call EditAnotherFile()', 'E16:')
1546
1547 call delete('Xfuncrange1')
1548 call delete('Xfuncrange2')
1549 bwipe!
1550endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001551
1552func Test_func_exists_on_reload()
1553 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1554 call assert_equal(0, exists('*ExistingFunction'))
1555 source Xfuncexists
Bram Moolenaara4208962019-08-24 20:50:19 +02001556 call assert_equal(1, '*ExistingFunction'->exists())
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001557 " Redefining a function when reloading a script is OK.
1558 source Xfuncexists
1559 call assert_equal(1, exists('*ExistingFunction'))
1560
1561 " But redefining in another script is not OK.
1562 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1563 call assert_fails('source Xfuncexists2', 'E122:')
1564
1565 delfunc ExistingFunction
1566 call assert_equal(0, exists('*ExistingFunction'))
1567 call writefile([
1568 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1569 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1570 \ ], 'Xfuncexists')
1571 call assert_fails('source Xfuncexists', 'E122:')
1572 call assert_equal(1, exists('*ExistingFunction'))
1573
1574 call delete('Xfuncexists2')
1575 call delete('Xfuncexists')
1576 delfunc ExistingFunction
1577endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01001578
1579" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1580func Test_confirm()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001581 CheckUnix
1582 CheckNotGui
Bram Moolenaar2e050092019-01-27 15:00:36 +01001583
1584 call feedkeys('o', 'L')
1585 let a = confirm('Press O to proceed')
1586 call assert_equal(1, a)
1587
1588 call feedkeys('y', 'L')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001589 let a = 'Are you sure?'->confirm("&Yes\n&No")
Bram Moolenaar2e050092019-01-27 15:00:36 +01001590 call assert_equal(1, a)
1591
1592 call feedkeys('n', 'L')
1593 let a = confirm('Are you sure?', "&Yes\n&No")
1594 call assert_equal(2, a)
1595
1596 " confirm() should return 0 when pressing CTRL-C.
Bram Moolenaar79296512020-03-22 16:17:14 +01001597 call feedkeys("\<C-C>", 'L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01001598 let a = confirm('Are you sure?', "&Yes\n&No")
1599 call assert_equal(0, a)
1600
1601 " <Esc> requires another character to avoid it being seen as the start of an
1602 " escape sequence. Zero should be harmless.
Bram Moolenaara4208962019-08-24 20:50:19 +02001603 eval "\<Esc>0"->feedkeys('L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01001604 let a = confirm('Are you sure?', "&Yes\n&No")
1605 call assert_equal(0, a)
1606
1607 " Default choice is returned when pressing <CR>.
1608 call feedkeys("\<CR>", 'L')
1609 let a = confirm('Are you sure?', "&Yes\n&No")
1610 call assert_equal(1, a)
1611
1612 call feedkeys("\<CR>", 'L')
1613 let a = confirm('Are you sure?', "&Yes\n&No", 2)
1614 call assert_equal(2, a)
1615
1616 call feedkeys("\<CR>", 'L')
1617 let a = confirm('Are you sure?', "&Yes\n&No", 0)
1618 call assert_equal(0, a)
1619
1620 " Test with the {type} 4th argument
1621 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
1622 call feedkeys('y', 'L')
1623 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
1624 call assert_equal(1, a)
1625 endfor
1626
1627 call assert_fails('call confirm([])', 'E730:')
1628 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
1629 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
1630 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
1631endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001632
1633func Test_platform_name()
1634 " The system matches at most only one name.
1635 let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
1636 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
1637
1638 " Is Unix?
1639 call assert_equal(has('beos'), has('beos') && has('unix'))
1640 call assert_equal(has('bsd'), has('bsd') && has('unix'))
1641 call assert_equal(has('hpux'), has('hpux') && has('unix'))
1642 call assert_equal(has('linux'), has('linux') && has('unix'))
1643 call assert_equal(has('mac'), has('mac') && has('unix'))
1644 call assert_equal(has('qnx'), has('qnx') && has('unix'))
1645 call assert_equal(has('sun'), has('sun') && has('unix'))
1646 call assert_equal(has('win32'), has('win32') && !has('unix'))
1647 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
1648
1649 if has('unix') && executable('uname')
1650 let uname = system('uname')
1651 call assert_equal(uname =~? 'BeOS', has('beos'))
Bram Moolenaara02e3f62019-02-07 21:27:14 +01001652 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
1653 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001654 call assert_equal(uname =~? 'HP-UX', has('hpux'))
1655 call assert_equal(uname =~? 'Linux', has('linux'))
1656 call assert_equal(uname =~? 'Darwin', has('mac'))
1657 call assert_equal(uname =~? 'QNX', has('qnx'))
1658 call assert_equal(uname =~? 'SunOS', has('sun'))
1659 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
1660 endif
1661endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001662
1663func Test_readdir()
1664 call mkdir('Xdir')
1665 call writefile([], 'Xdir/foo.txt')
1666 call writefile([], 'Xdir/bar.txt')
1667 call mkdir('Xdir/dir')
1668
1669 " All results
1670 let files = readdir('Xdir')
1671 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
1672
1673 " Only results containing "f"
Bram Moolenaara0d1fef2019-09-04 22:29:14 +02001674 let files = 'Xdir'->readdir({ x -> stridx(x, 'f') !=- 1 })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001675 call assert_equal(['foo.txt'], sort(files))
1676
1677 " Only .txt files
1678 let files = readdir('Xdir', { x -> x =~ '.txt$' })
1679 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1680
1681 " Only .txt files with string
1682 let files = readdir('Xdir', 'v:val =~ ".txt$"')
1683 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1684
1685 " Limit to 1 result.
1686 let l = []
1687 let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
1688 call assert_equal(1, len(files))
1689
Bram Moolenaar27da7de2019-09-03 17:13:37 +02001690 " Nested readdir() must not crash
1691 let files = readdir('Xdir', 'readdir("Xdir", "1") != []')
1692 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
1693
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001694 eval 'Xdir'->delete('rf')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001695endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02001696
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001697func Test_delete_rf()
1698 call mkdir('Xdir')
1699 call writefile([], 'Xdir/foo.txt')
1700 call writefile([], 'Xdir/bar.txt')
1701 call mkdir('Xdir/[a-1]') " issue #696
1702 call writefile([], 'Xdir/[a-1]/foo.txt')
1703 call writefile([], 'Xdir/[a-1]/bar.txt')
1704 call assert_true(filereadable('Xdir/foo.txt'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001705 call assert_true('Xdir/[a-1]/foo.txt'->filereadable())
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001706
1707 call assert_equal(0, delete('Xdir', 'rf'))
1708 call assert_false(filereadable('Xdir/foo.txt'))
1709 call assert_false(filereadable('Xdir/[a-1]/foo.txt'))
1710endfunc
1711
Bram Moolenaar17aca702019-05-16 22:24:55 +02001712func Test_call()
1713 call assert_equal(3, call('len', [123]))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001714 call assert_equal(3, 'len'->call([123]))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001715 call assert_fails("call call('len', 123)", 'E714:')
1716 call assert_equal(0, call('', []))
1717
1718 function Mylen() dict
1719 return len(self.data)
1720 endfunction
1721 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001722 eval mydict.len->call([], mydict)->assert_equal(4)
Bram Moolenaar17aca702019-05-16 22:24:55 +02001723 call assert_fails("call call('Mylen', [], 0)", 'E715:')
1724endfunc
1725
1726func Test_char2nr()
1727 call assert_equal(12354, char2nr('あ', 1))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001728 call assert_equal(120, 'x'->char2nr())
Bram Moolenaar17aca702019-05-16 22:24:55 +02001729endfunc
1730
1731func Test_eventhandler()
1732 call assert_equal(0, eventhandler())
1733endfunc
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001734
1735func Test_bufadd_bufload()
1736 call assert_equal(0, bufexists('someName'))
1737 let buf = bufadd('someName')
1738 call assert_notequal(0, buf)
1739 call assert_equal(1, bufexists('someName'))
1740 call assert_equal(0, getbufvar(buf, '&buflisted'))
1741 call assert_equal(0, bufloaded(buf))
1742 call bufload(buf)
1743 call assert_equal(1, bufloaded(buf))
1744 call assert_equal([''], getbufline(buf, 1, '$'))
1745
1746 let curbuf = bufnr('')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001747 eval ['some', 'text']->writefile('XotherName')
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001748 let buf = 'XotherName'->bufadd()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001749 call assert_notequal(0, buf)
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001750 eval 'XotherName'->bufexists()->assert_equal(1)
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001751 call assert_equal(0, getbufvar(buf, '&buflisted'))
1752 call assert_equal(0, bufloaded(buf))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001753 eval buf->bufload()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001754 call assert_equal(1, bufloaded(buf))
1755 call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
1756 call assert_equal(curbuf, bufnr(''))
1757
Bram Moolenaar892ae722019-06-30 20:33:01 +02001758 let buf1 = bufadd('')
1759 let buf2 = bufadd('')
1760 call assert_notequal(0, buf1)
1761 call assert_notequal(0, buf2)
1762 call assert_notequal(buf1, buf2)
1763 call assert_equal(1, bufexists(buf1))
1764 call assert_equal(1, bufexists(buf2))
1765 call assert_equal(0, bufloaded(buf1))
1766 exe 'bwipe ' .. buf1
1767 call assert_equal(0, bufexists(buf1))
1768 call assert_equal(1, bufexists(buf2))
1769 exe 'bwipe ' .. buf2
1770 call assert_equal(0, bufexists(buf2))
1771
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001772 bwipe someName
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001773 bwipe XotherName
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001774 call assert_equal(0, bufexists('someName'))
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001775 call delete('XotherName')
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001776endfunc
Bram Moolenaarc2585492019-09-22 21:29:53 +02001777
1778func Test_state()
1779 CheckRunVimInTerminal
1780
1781 let lines =<< trim END
1782 call setline(1, ['one', 'two', 'three'])
1783 map ;; gg
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001784 set complete=.
Bram Moolenaarc2585492019-09-22 21:29:53 +02001785 func RunTimer()
1786 call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
1787 endfunc
1788 au Filetype foobar let g:state = state()|let g:mode = mode()
1789 END
1790 call writefile(lines, 'XState')
1791 let buf = RunVimInTerminal('-S XState', #{rows: 6})
1792
1793 " Using a ":" command Vim is busy, thus "S" is returned
1794 call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
1795 call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
1796 call term_sendkeys(buf, ":\<CR>")
1797
1798 " Using a timer callback
1799 call term_sendkeys(buf, ":call RunTimer()\<CR>")
1800 call term_wait(buf, 50)
1801 let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"
1802 call term_sendkeys(buf, getstate)
1803 call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)
1804
1805 " Halfway a mapping
1806 call term_sendkeys(buf, ":call RunTimer()\<CR>;")
1807 call term_wait(buf, 50)
1808 call term_sendkeys(buf, ";")
1809 call term_sendkeys(buf, getstate)
1810 call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)
1811
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001812 " Insert mode completion (bit slower on Mac)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001813 call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001814 call term_wait(buf, 200)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001815 call term_sendkeys(buf, "\<Esc>")
1816 call term_sendkeys(buf, getstate)
1817 call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)
1818
1819 " Autocommand executing
1820 call term_sendkeys(buf, ":set filetype=foobar\<CR>")
1821 call term_wait(buf, 50)
1822 call term_sendkeys(buf, getstate)
1823 call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)
1824
1825 " Todo: "w" - waiting for ch_evalexpr()
1826
1827 " messages scrolled
1828 call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
1829 call term_wait(buf, 50)
1830 call term_sendkeys(buf, "\<CR>")
1831 call term_sendkeys(buf, getstate)
1832 call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)
1833
1834 call StopVimInTerminal(buf)
1835 call delete('XState')
1836endfunc
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001837
1838func Test_range()
1839 " destructuring
1840 let [x, y] = range(2)
1841 call assert_equal([0, 1], [x, y])
1842
1843 " index
1844 call assert_equal(4, range(1, 10)[3])
1845
1846 " add()
1847 call assert_equal([0, 1, 2, 3], add(range(3), 3))
1848 call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3)))
1849 call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3)))
1850
1851 " append()
1852 new
1853 call append('.', range(5))
1854 call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$'))
1855 bwipe!
1856
1857 " appendbufline()
1858 new
1859 call appendbufline(bufnr(''), '.', range(5))
1860 call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$'))
1861 bwipe!
1862
1863 " call()
1864 func TwoArgs(a, b)
1865 return [a:a, a:b]
1866 endfunc
1867 call assert_equal([0, 1], call('TwoArgs', range(2)))
1868
1869 " col()
1870 new
1871 call setline(1, ['foo', 'bar'])
1872 call assert_equal(2, col(range(1, 2)))
1873 bwipe!
1874
1875 " complete()
1876 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>"
1877 " complete_info()
1878 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>"
1879
1880 " copy()
1881 call assert_equal([1, 2, 3], copy(range(1, 3)))
1882
1883 " count()
1884 call assert_equal(0, count(range(0), 3))
1885 call assert_equal(0, count(range(2), 3))
1886 call assert_equal(1, count(range(5), 3))
1887
1888 " cursor()
1889 new
1890 call setline(1, ['aaa', 'bbb', 'ccc'])
1891 call cursor(range(1, 2))
1892 call assert_equal([2, 1], [col('.'), line('.')])
1893 bwipe!
1894
1895 " deepcopy()
1896 call assert_equal([1, 2, 3], deepcopy(range(1, 3)))
1897
1898 " empty()
1899 call assert_true(empty(range(0)))
1900 call assert_false(empty(range(2)))
1901
1902 " execute()
1903 new
1904 call setline(1, ['aaa', 'bbb', 'ccc'])
1905 call execute(range(3))
1906 call assert_equal(2, line('.'))
1907 bwipe!
1908
1909 " extend()
1910 call assert_equal([1, 2, 3, 4], extend([1], range(2, 4)))
1911 call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4)))
1912 call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4]))
1913
1914 " filter()
1915 call assert_equal([1, 3], filter(range(5), 'v:val % 2'))
1916
1917 " funcref()
1918 call assert_equal([0, 1], funcref('TwoArgs', range(2))())
1919
1920 " function()
1921 call assert_equal([0, 1], function('TwoArgs', range(2))())
1922
1923 " garbagecollect()
1924 let thelist = [1, range(2), 3]
1925 let otherlist = range(3)
1926 call test_garbagecollect_now()
1927
1928 " get()
1929 call assert_equal(4, get(range(1, 10), 3))
1930 call assert_equal(-1, get(range(1, 10), 42, -1))
1931
1932 " index()
1933 call assert_equal(1, index(range(1, 5), 2))
1934
1935 " inputlist()
Bram Moolenaar272ca952020-01-28 20:49:11 +01001936 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x')
1937 call assert_equal(1, result)
1938 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x')
1939 call assert_equal(1, result)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001940
1941 " insert()
1942 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42))
1943 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0))
1944 call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1))
1945 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4))
1946 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1))
1947 call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5))
1948
1949 " join()
1950 call assert_equal('0 1 2 3 4', join(range(5)))
1951
Bram Moolenaar272ca952020-01-28 20:49:11 +01001952 " json_encode()
1953 call assert_equal('[0,1,2,3]', json_encode(range(4)))
1954
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001955 " len()
1956 call assert_equal(0, len(range(0)))
1957 call assert_equal(2, len(range(2)))
1958 call assert_equal(5, len(range(0, 12, 3)))
1959 call assert_equal(4, len(range(3, 0, -1)))
1960
1961 " list2str()
1962 call assert_equal('ABC', list2str(range(65, 67)))
1963
1964 " lock()
1965 let thelist = range(5)
1966 lockvar thelist
1967
1968 " map()
1969 call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2'))
1970
1971 " match()
1972 call assert_equal(3, match(range(5), 3))
1973
1974 " matchaddpos()
1975 highlight MyGreenGroup ctermbg=green guibg=green
1976 call matchaddpos('MyGreenGroup', range(line('.'), line('.')))
1977
1978 " matchend()
1979 call assert_equal(4, matchend(range(5), '4'))
1980 call assert_equal(3, matchend(range(1, 5), '4'))
1981 call assert_equal(-1, matchend(range(1, 5), '42'))
1982
1983 " matchstrpos()
1984 call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4'))
1985 call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4'))
1986 call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42'))
1987
1988 " max() reverse()
1989 call assert_equal(0, max(range(0)))
1990 call assert_equal(0, max(range(10, 9)))
1991 call assert_equal(9, max(range(10)))
1992 call assert_equal(18, max(range(0, 20, 3)))
1993 call assert_equal(20, max(range(20, 0, -3)))
1994 call assert_equal(99999, max(range(100000)))
1995 call assert_equal(99999, max(range(99999, 0, -1)))
1996 call assert_equal(99999, max(reverse(range(100000))))
1997 call assert_equal(99999, max(reverse(range(99999, 0, -1))))
1998
1999 " min() reverse()
2000 call assert_equal(0, min(range(0)))
2001 call assert_equal(0, min(range(10, 9)))
2002 call assert_equal(5, min(range(5, 10)))
2003 call assert_equal(5, min(range(5, 10, 3)))
2004 call assert_equal(2, min(range(20, 0, -3)))
2005 call assert_equal(0, min(range(100000)))
2006 call assert_equal(0, min(range(99999, 0, -1)))
2007 call assert_equal(0, min(reverse(range(100000))))
2008 call assert_equal(0, min(reverse(range(99999, 0, -1))))
2009
2010 " remove()
2011 call assert_equal(1, remove(range(1, 10), 0))
2012 call assert_equal(2, remove(range(1, 10), 1))
2013 call assert_equal(9, remove(range(1, 10), 8))
2014 call assert_equal(10, remove(range(1, 10), 9))
2015 call assert_equal(10, remove(range(1, 10), -1))
2016 call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4))
2017
2018 " repeat()
2019 call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2))
2020 call assert_equal([0, 1, 2], repeat(range(3), 1))
2021 call assert_equal([], repeat(range(3), 0))
2022 call assert_equal([], repeat(range(5, 4), 2))
2023 call assert_equal([], repeat(range(5, 4), 0))
2024
2025 " reverse()
2026 call assert_equal([2, 1, 0], reverse(range(3)))
2027 call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1)))
2028 call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10)))
2029 call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20)))
2030 call assert_equal([16, 13, 10], reverse(range(10, 18, 3)))
2031 call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3)))
2032 call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3)))
2033 call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3)))
2034 call assert_equal([], reverse(range(0)))
2035
2036 " TODO: setpos()
2037 " new
2038 " call setline(1, repeat([''], bufnr('')))
2039 " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6))
2040 " call setpos('x', range(bufnr(''), bufnr('') + 3))
2041 " bwipe!
2042
2043 " setreg()
2044 call setreg('a', range(3))
2045 call assert_equal("0\n1\n2\n", getreg('a'))
2046
Bram Moolenaarb0992022020-01-30 14:55:42 +01002047 " settagstack()
2048 call settagstack(1, #{items : range(4)})
Bram Moolenaar94255df2020-02-05 20:10:33 +01002049
Bram Moolenaarb0992022020-01-30 14:55:42 +01002050 " sign_define()
2051 call assert_fails("call sign_define(range(5))", "E715:")
2052 call assert_fails("call sign_placelist(range(5))", "E715:")
2053
2054 " sign_undefine()
2055 call assert_fails("call sign_undefine(range(5))", "E908:")
2056
2057 " sign_unplacelist()
2058 call assert_fails("call sign_unplacelist(range(5))", "E715:")
2059
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002060 " sort()
2061 call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1)))
2062
2063 " string()
2064 call assert_equal('[0, 1, 2, 3, 4]', string(range(5)))
2065
Bram Moolenaarb0992022020-01-30 14:55:42 +01002066 " taglist() with 'tagfunc'
2067 func TagFunc(pattern, flags, info)
2068 return range(10)
2069 endfunc
2070 set tagfunc=TagFunc
2071 call assert_fails("call taglist('asdf')", 'E987:')
2072 set tagfunc=
Bram Moolenaar94255df2020-02-05 20:10:33 +01002073
Bram Moolenaarb0992022020-01-30 14:55:42 +01002074 " term_start()
Bram Moolenaar705724e2020-01-31 21:13:42 +01002075 if has('terminal') && has('termguicolors')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002076 call assert_fails('call term_start(range(3, 4))', 'E474:')
2077 let g:terminal_ansi_colors = range(16)
Bram Moolenaar94255df2020-02-05 20:10:33 +01002078 if has('win32')
2079 let cmd = "cmd /c dir"
2080 else
2081 let cmd = "ls"
2082 endif
2083 call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"})', 'E475:')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002084 unlet g:terminal_ansi_colors
2085 endif
2086
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002087 " type()
2088 call assert_equal(v:t_list, type(range(5)))
2089
2090 " uniq()
2091 call assert_equal([0, 1, 2, 3, 4], uniq(range(5)))
2092endfunc
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002093
2094func Test_echoraw()
2095 CheckScreendump
2096
2097 " Normally used for escape codes, but let's test with a CR.
2098 let lines =<< trim END
2099 call echoraw("hello\<CR>x")
2100 END
2101 call writefile(lines, 'XTest_echoraw')
2102 let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40})
2103 call VerifyScreenDump(buf, 'Test_functions_echoraw', {})
2104
2105 " clean up
2106 call StopVimInTerminal(buf)
2107 call delete('XTest_echoraw')
2108endfunc
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002109
2110" vim: shiftwidth=2 sts=2 expandtab