blob: 327350be09d6816df0c1275a0e3b3e81985a5987 [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 Moolenaar24c2e482017-01-29 15:45:12 +010023func Test_empty()
24 call assert_equal(1, empty(''))
25 call assert_equal(0, empty('a'))
26
27 call assert_equal(1, empty(0))
28 call assert_equal(1, empty(-0))
29 call assert_equal(0, empty(1))
30 call assert_equal(0, empty(-1))
31
Bram Moolenaar5feabe02020-01-30 18:24:53 +010032 if has('float')
33 call assert_equal(1, empty(0.0))
34 call assert_equal(1, empty(-0.0))
35 call assert_equal(0, empty(1.0))
36 call assert_equal(0, empty(-1.0))
37 call assert_equal(0, empty(1.0/0.0))
38 call assert_equal(0, empty(0.0/0.0))
39 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +010040
41 call assert_equal(1, empty([]))
42 call assert_equal(0, empty(['a']))
43
44 call assert_equal(1, empty({}))
45 call assert_equal(0, empty({'a':1}))
46
47 call assert_equal(1, empty(v:null))
48 call assert_equal(1, empty(v:none))
49 call assert_equal(1, empty(v:false))
50 call assert_equal(0, empty(v:true))
51
Bram Moolenaar41042f32017-03-09 12:09:32 +010052 if has('channel')
53 call assert_equal(1, empty(test_null_channel()))
54 endif
55 if has('job')
56 call assert_equal(1, empty(test_null_job()))
57 endif
58
Bram Moolenaar24c2e482017-01-29 15:45:12 +010059 call assert_equal(0, empty(function('Test_empty')))
Bram Moolenaar17aca702019-05-16 22:24:55 +020060 call assert_equal(0, empty(function('Test_empty', [0])))
Bram Moolenaar24c2e482017-01-29 15:45:12 +010061endfunc
62
63func Test_len()
64 call assert_equal(1, len(0))
65 call assert_equal(2, len(12))
66
67 call assert_equal(0, len(''))
68 call assert_equal(2, len('ab'))
69
70 call assert_equal(0, len([]))
71 call assert_equal(2, len([2, 1]))
72
73 call assert_equal(0, len({}))
74 call assert_equal(2, len({'a': 1, 'b': 2}))
75
76 call assert_fails('call len(v:none)', 'E701:')
77 call assert_fails('call len({-> 0})', 'E701:')
78endfunc
79
80func Test_max()
81 call assert_equal(0, max([]))
82 call assert_equal(2, max([2]))
83 call assert_equal(2, max([1, 2]))
84 call assert_equal(2, max([1, 2, v:null]))
85
86 call assert_equal(0, max({}))
87 call assert_equal(2, max({'a':1, 'b':2}))
88
89 call assert_fails('call max(1)', 'E712:')
90 call assert_fails('call max(v:none)', 'E712:')
91endfunc
92
93func Test_min()
94 call assert_equal(0, min([]))
95 call assert_equal(2, min([2]))
96 call assert_equal(1, min([1, 2]))
97 call assert_equal(0, min([1, 2, v:null]))
98
99 call assert_equal(0, min({}))
100 call assert_equal(1, min({'a':1, 'b':2}))
101
102 call assert_fails('call min(1)', 'E712:')
103 call assert_fails('call min(v:none)', 'E712:')
104endfunc
105
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200106func Test_strwidth()
107 for aw in ['single', 'double']
108 exe 'set ambiwidth=' . aw
109 call assert_equal(0, strwidth(''))
110 call assert_equal(1, strwidth("\t"))
111 call assert_equal(3, strwidth('Vim'))
112 call assert_equal(4, strwidth(1234))
113 call assert_equal(5, strwidth(-1234))
114
Bram Moolenaar30276f22019-01-24 17:59:39 +0100115 call assert_equal(2, strwidth('😉'))
116 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
117 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200118
119 call assert_fails('call strwidth({->0})', 'E729:')
120 call assert_fails('call strwidth([])', 'E730:')
121 call assert_fails('call strwidth({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100122 if has('float')
123 call assert_fails('call strwidth(1.2)', 'E806:')
124 endif
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200125 endfor
126
127 set ambiwidth&
128endfunc
129
Bram Moolenaar08243d22017-01-10 16:12:29 +0100130func Test_str2nr()
131 call assert_equal(0, str2nr(''))
132 call assert_equal(1, str2nr('1'))
133 call assert_equal(1, str2nr(' 1 '))
134
135 call assert_equal(1, str2nr('+1'))
136 call assert_equal(1, str2nr('+ 1'))
137 call assert_equal(1, str2nr(' + 1 '))
138
139 call assert_equal(-1, str2nr('-1'))
140 call assert_equal(-1, str2nr('- 1'))
141 call assert_equal(-1, str2nr(' - 1 '))
142
143 call assert_equal(123456789, str2nr('123456789'))
144 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100145
146 call assert_equal(5, str2nr('101', 2))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200147 call assert_equal(5, '0b101'->str2nr(2))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100148 call assert_equal(5, str2nr('0B101', 2))
149 call assert_equal(-5, str2nr('-101', 2))
150 call assert_equal(-5, str2nr('-0b101', 2))
151 call assert_equal(-5, str2nr('-0B101', 2))
152
153 call assert_equal(65, str2nr('101', 8))
154 call assert_equal(65, str2nr('0101', 8))
155 call assert_equal(-65, str2nr('-101', 8))
156 call assert_equal(-65, str2nr('-0101', 8))
157
158 call assert_equal(11259375, str2nr('abcdef', 16))
159 call assert_equal(11259375, str2nr('ABCDEF', 16))
160 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
161 call assert_equal(11259375, str2nr('0xabcdef', 16))
162 call assert_equal(11259375, str2nr('0Xabcdef', 16))
163 call assert_equal(11259375, str2nr('0XABCDEF', 16))
164 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
165
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200166 call assert_equal(1, str2nr("1'000'000", 10, 0))
167 call assert_equal(256, str2nr("1'0000'0000", 2, 1))
168 call assert_equal(262144, str2nr("1'000'000", 8, 1))
169 call assert_equal(1000000, str2nr("1'000'000", 10, 1))
Bram Moolenaarea8dcf82019-09-15 21:12:22 +0200170 call assert_equal(1000, str2nr("1'000''000", 10, 1))
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200171 call assert_equal(65536, str2nr("1'00'00", 16, 1))
172
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100173 call assert_equal(0, str2nr('0x10'))
174 call assert_equal(0, str2nr('0b10'))
175 call assert_equal(1, str2nr('12', 2))
176 call assert_equal(1, str2nr('18', 8))
177 call assert_equal(1, str2nr('1g', 16))
178
179 call assert_equal(0, str2nr(v:null))
180 call assert_equal(0, str2nr(v:none))
181
182 call assert_fails('call str2nr([])', 'E730:')
183 call assert_fails('call str2nr({->2})', 'E729:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100184 if has('float')
185 call assert_fails('call str2nr(1.2)', 'E806:')
186 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100187 call assert_fails('call str2nr(10, [])', 'E474:')
188endfunc
189
190func Test_strftime()
Bram Moolenaar10455d42019-11-21 15:36:18 +0100191 CheckFunction strftime
192
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100193 " Format of strftime() depends on system. We assume
194 " that basic formats tested here are available and
195 " identical on all systems which support strftime().
196 "
197 " The 2nd parameter of strftime() is a local time, so the output day
198 " of strftime() can be 17 or 18, depending on timezone.
199 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
200 "
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200201 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 +0100202
203 call assert_fails('call strftime([])', 'E730:')
204 call assert_fails('call strftime("%Y", [])', 'E745:')
Bram Moolenaardb517302019-06-18 22:53:24 +0200205
206 " Check that the time changes after we change the timezone
207 " Save previous timezone value, if any
208 if exists('$TZ')
209 let tz = $TZ
210 endif
211
212 " Force EST and then UTC, save the current hour (24-hour clock) for each
213 let $TZ = 'EST' | let est = strftime('%H')
214 let $TZ = 'UTC' | let utc = strftime('%H')
215
216 " Those hours should be two bytes long, and should not be the same; if they
217 " are, a tzset(3) call may have failed somewhere
218 call assert_equal(strlen(est), 2)
219 call assert_equal(strlen(utc), 2)
Bram Moolenaar87652a72019-06-18 23:07:37 +0200220 " TODO: this fails on MS-Windows
221 if has('unix')
222 call assert_notequal(est, utc)
223 endif
Bram Moolenaardb517302019-06-18 22:53:24 +0200224
225 " If we cached a timezone value, put it back, otherwise clear it
226 if exists('tz')
227 let $TZ = tz
228 else
229 unlet $TZ
230 endif
Bram Moolenaar10455d42019-11-21 15:36:18 +0100231endfunc
Bram Moolenaardb517302019-06-18 22:53:24 +0200232
Bram Moolenaar10455d42019-11-21 15:36:18 +0100233func Test_strptime()
234 CheckFunction strptime
235
236 if exists('$TZ')
237 let tz = $TZ
238 endif
239 let $TZ = 'UTC'
240
Bram Moolenaar9a838fe2019-12-06 12:45:01 +0100241 call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
Bram Moolenaar10455d42019-11-21 15:36:18 +0100242
243 call assert_fails('call strptime()', 'E119:')
244 call assert_fails('call strptime("xxx")', 'E119:')
245 call assert_equal(0, strptime("%Y", ''))
246 call assert_equal(0, strptime("%Y", "xxx"))
247
248 if exists('tz')
249 let $TZ = tz
250 else
251 unlet $TZ
252 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100253endfunc
254
Bram Moolenaardce1e892019-02-10 23:18:53 +0100255func Test_resolve_unix()
Bram Moolenaar26109902018-10-06 15:43:17 +0200256 if !has('unix')
257 return
258 endif
259
260 " Xlink1 -> Xlink2
261 " Xlink2 -> Xlink3
262 silent !ln -s -f Xlink2 Xlink1
263 silent !ln -s -f Xlink3 Xlink2
264 call assert_equal('Xlink3', resolve('Xlink1'))
265 call assert_equal('./Xlink3', resolve('./Xlink1'))
266 call assert_equal('Xlink3/', resolve('Xlink2/'))
267 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
268 "call assert_equal('Xlink3/', resolve('Xlink1/'))
269 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
270 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
271 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
272
273 " Test resolve() with a symlink cycle.
274 " Xlink1 -> Xlink2
275 " Xlink2 -> Xlink3
276 " Xlink3 -> Xlink1
277 silent !ln -s -f Xlink1 Xlink3
278 call assert_fails('call resolve("Xlink1")', 'E655:')
279 call assert_fails('call resolve("./Xlink1")', 'E655:')
280 call assert_fails('call resolve("Xlink2")', 'E655:')
281 call assert_fails('call resolve("Xlink3")', 'E655:')
282 call delete('Xlink1')
283 call delete('Xlink2')
284 call delete('Xlink3')
285
286 silent !ln -s -f Xdir//Xfile Xlink
287 call assert_equal('Xdir/Xfile', resolve('Xlink'))
288 call delete('Xlink')
289
290 silent !ln -s -f Xlink2/ Xlink1
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200291 call assert_equal('Xlink2', 'Xlink1'->resolve())
Bram Moolenaar26109902018-10-06 15:43:17 +0200292 call assert_equal('Xlink2/', resolve('Xlink1/'))
293 call delete('Xlink1')
294
295 silent !ln -s -f ./Xlink2 Xlink1
296 call assert_equal('Xlink2', resolve('Xlink1'))
297 call assert_equal('./Xlink2', resolve('./Xlink1'))
298 call delete('Xlink1')
299endfunc
300
Bram Moolenaardce1e892019-02-10 23:18:53 +0100301func s:normalize_fname(fname)
302 let ret = substitute(a:fname, '\', '/', 'g')
303 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200304 return ret->tolower()
Bram Moolenaardce1e892019-02-10 23:18:53 +0100305endfunc
306
307func Test_resolve_win32()
308 if !has('win32')
309 return
310 endif
311
312 " test for shortcut file
313 if executable('cscript')
314 new Xfile
315 wq
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200316 let lines =<< trim END
317 Set fs = CreateObject("Scripting.FileSystemObject")
318 Set ws = WScript.CreateObject("WScript.Shell")
319 Set shortcut = ws.CreateShortcut("Xlink.lnk")
320 shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")
321 shortcut.Save
322 END
323 call writefile(lines, 'link.vbs')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100324 silent !cscript link.vbs
325 call delete('link.vbs')
326 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
327 call delete('Xfile')
328
329 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
330 call delete('Xlink.lnk')
331 else
332 echomsg 'skipped test for shortcut file'
333 endif
334
335 " remove files
336 call delete('Xlink')
337 call delete('Xdir', 'd')
338 call delete('Xfile')
339
340 " test for symbolic link to a file
341 new Xfile
342 wq
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200343 call assert_equal('Xfile', resolve('Xfile'))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100344 silent !mklink Xlink Xfile
345 if !v:shell_error
346 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink')))
347 call delete('Xlink')
348 else
349 echomsg 'skipped test for symbolic link to a file'
350 endif
351 call delete('Xfile')
352
353 " test for junction to a directory
354 call mkdir('Xdir')
355 silent !mklink /J Xlink Xdir
356 if !v:shell_error
357 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
358
359 call delete('Xdir', 'd')
360
361 " test for junction already removed
362 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
363 call delete('Xlink')
364 else
365 echomsg 'skipped test for junction to a directory'
366 call delete('Xdir', 'd')
367 endif
368
369 " test for symbolic link to a directory
370 call mkdir('Xdir')
371 silent !mklink /D Xlink Xdir
372 if !v:shell_error
373 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
374
375 call delete('Xdir', 'd')
376
377 " test for symbolic link already removed
378 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
379 call delete('Xlink')
380 else
381 echomsg 'skipped test for symbolic link to a directory'
382 call delete('Xdir', 'd')
383 endif
384
385 " test for buffer name
386 new Xfile
387 wq
388 silent !mklink Xlink Xfile
389 if !v:shell_error
390 edit Xlink
391 call assert_equal('Xlink', bufname('%'))
392 call delete('Xlink')
393 bw!
394 else
395 echomsg 'skipped test for buffer name'
396 endif
397 call delete('Xfile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200398
399 " test for reparse point
400 call mkdir('Xdir')
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200401 call assert_equal('Xdir', resolve('Xdir'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200402 silent !mklink /D Xdirlink Xdir
403 if !v:shell_error
404 w Xdir/text.txt
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200405 call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200406 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
407 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200408 call delete('Xdirlink')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200409 else
410 echomsg 'skipped test for reparse point'
411 endif
412
413 call delete('Xdir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100414endfunc
415
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100416func Test_simplify()
417 call assert_equal('', simplify(''))
418 call assert_equal('/', simplify('/'))
419 call assert_equal('/', simplify('/.'))
420 call assert_equal('/', simplify('/..'))
421 call assert_equal('/...', simplify('/...'))
422 call assert_equal('./dir/file', simplify('./dir/file'))
423 call assert_equal('./dir/file', simplify('.///dir//file'))
424 call assert_equal('./dir/file', simplify('./dir/./file'))
425 call assert_equal('./file', simplify('./dir/../file'))
426 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
427 call assert_equal('./file', simplify('dir/.././file'))
428
429 call assert_fails('call simplify({->0})', 'E729:')
430 call assert_fails('call simplify([])', 'E730:')
431 call assert_fails('call simplify({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100432 if has('float')
433 call assert_fails('call simplify(1.2)', 'E806:')
434 endif
Bram Moolenaar08243d22017-01-10 16:12:29 +0100435endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100436
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200437func Test_pathshorten()
438 call assert_equal('', pathshorten(''))
439 call assert_equal('foo', pathshorten('foo'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200440 call assert_equal('/foo', '/foo'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200441 call assert_equal('f/', pathshorten('foo/'))
442 call assert_equal('f/bar', pathshorten('foo/bar'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200443 call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200444 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
445 call assert_equal('.f/bar', pathshorten('.foo/bar'))
446 call assert_equal('~f/bar', pathshorten('~foo/bar'))
447 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
448 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
449 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
450endfunc
451
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100452func Test_strpart()
453 call assert_equal('de', strpart('abcdefg', 3, 2))
454 call assert_equal('ab', strpart('abcdefg', -2, 4))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200455 call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100456 call assert_equal('fg', strpart('abcdefg', 5, 4))
457 call assert_equal('defg', strpart('abcdefg', 3))
458
Bram Moolenaar30276f22019-01-24 17:59:39 +0100459 call assert_equal('lép', strpart('éléphant', 2, 4))
460 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100461endfunc
462
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100463func Test_tolower()
464 call assert_equal("", tolower(""))
465
466 " Test with all printable ASCII characters.
467 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
468 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
469
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100470 " Test with a few uppercase diacritics.
471 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
472 call assert_equal("bḃḇ", tolower("BḂḆ"))
473 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
474 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
475 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
476 call assert_equal("fḟ ", tolower("FḞ "))
477 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
478 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
479 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
480 call assert_equal("jĵ", tolower("JĴ"))
481 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
482 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
483 call assert_equal("mḿṁ", tolower("MḾṀ"))
484 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
485 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
486 call assert_equal("pṕṗ", tolower("PṔṖ"))
487 call assert_equal("q", tolower("Q"))
488 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
489 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
490 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
491 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
492 call assert_equal("vṽ", tolower("VṼ"))
493 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
494 call assert_equal("xẋẍ", tolower("XẊẌ"))
495 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
496 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
497
498 " Test with a few lowercase diacritics, which should remain unchanged.
499 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
500 call assert_equal("bḃḇ", tolower("bḃḇ"))
501 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
502 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
503 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
504 call assert_equal("fḟ", tolower("fḟ"))
505 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
506 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
507 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
508 call assert_equal("jĵǰ", tolower("jĵǰ"))
509 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
510 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
511 call assert_equal("mḿṁ ", tolower("mḿṁ "))
512 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
513 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
514 call assert_equal("pṕṗ", tolower("pṕṗ"))
515 call assert_equal("q", tolower("q"))
516 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
517 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
518 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
519 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
520 call assert_equal("vṽ", tolower("vṽ"))
521 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
522 call assert_equal("ẋẍ", tolower("ẋẍ"))
523 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
524 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
525
526 " According to https://twitter.com/jifa/status/625776454479970304
527 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
528 " in length (2 to 3 bytes) when lowercased. So let's test them.
529 call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100530
531 " This call to tolower with invalid utf8 sequence used to cause access to
532 " invalid memory.
533 call tolower("\xC0\x80\xC0")
534 call tolower("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100535endfunc
536
537func Test_toupper()
538 call assert_equal("", toupper(""))
539
540 " Test with all printable ASCII characters.
541 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
542 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
543
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100544 " Test with a few lowercase diacritics.
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200545 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100546 call assert_equal("BḂḆ", toupper("bḃḇ"))
547 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
548 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
549 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
550 call assert_equal("FḞ", toupper("fḟ"))
551 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
552 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
553 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
554 call assert_equal("JĴǰ", toupper("jĵǰ"))
555 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
556 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
557 call assert_equal("MḾṀ ", toupper("mḿṁ "))
558 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
559 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
560 call assert_equal("PṔṖ", toupper("pṕṗ"))
561 call assert_equal("Q", toupper("q"))
562 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
563 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
564 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
565 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
566 call assert_equal("VṼ", toupper("vṽ"))
567 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
568 call assert_equal("ẊẌ", toupper("ẋẍ"))
569 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
570 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
571
572 " Test that uppercase diacritics, which should remain unchanged.
573 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
574 call assert_equal("BḂḆ", toupper("BḂḆ"))
575 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
576 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
577 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
578 call assert_equal("FḞ ", toupper("FḞ "))
579 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
580 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
581 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
582 call assert_equal("JĴ", toupper("JĴ"))
583 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
584 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
585 call assert_equal("MḾṀ", toupper("MḾṀ"))
586 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
587 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
588 call assert_equal("PṔṖ", toupper("PṔṖ"))
589 call assert_equal("Q", toupper("Q"))
590 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
591 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
592 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
593 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
594 call assert_equal("VṼ", toupper("VṼ"))
595 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
596 call assert_equal("XẊẌ", toupper("XẊẌ"))
597 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
598 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
599
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100600 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100601
602 " This call to toupper with invalid utf8 sequence used to cause access to
603 " invalid memory.
604 call toupper("\xC0\x80\xC0")
605 call toupper("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100606endfunc
607
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200608func Test_tr()
609 call assert_equal('foo', tr('bar', 'bar', 'foo'))
610 call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
611endfunc
612
Bram Moolenaare90858d2017-02-01 17:24:34 +0100613" Tests for the mode() function
614let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100615func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100616 let g:current_modes = mode(0) . '-' . mode(1)
617 return ''
618endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100619
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100620func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100621 new
622 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
623
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100624 " Only complete from the current buffer.
625 set complete=.
626
Bram Moolenaare90858d2017-02-01 17:24:34 +0100627 inoremap <F2> <C-R>=Save_mode()<CR>
628
629 normal! 3G
630 exe "normal i\<F2>\<Esc>"
631 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100632 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100633 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
634 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100635 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100636 exe "normal iBro\<C-P>\<F2>\<Esc>u"
637 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100638 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100639 exe "normal iBa\<C-X>\<F2>\<Esc>u"
640 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100641 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100642 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
643 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100644 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100645 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
646 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100647 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100648 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
649 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100650 " i_CTRL-X CTRL-L: Multiple matches
651 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
652 call assert_equal('i-ic', g:current_modes)
653 " i_CTRL-X CTRL-L: Single match
654 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
655 call assert_equal('i-ic', g:current_modes)
656 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100657 exe "normal iCom\<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 CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100660 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
661 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100662 " i_CTRL-X CTRL-L: No match
663 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
664 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100665
Bram Moolenaare971df32017-02-05 14:15:29 +0100666 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100667 exe "normal RBa\<C-P>\<F2>\<Esc>u"
668 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100669 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100670 exe "normal RBro\<C-P>\<F2>\<Esc>u"
671 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100672 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100673 exe "normal RBa\<C-X>\<F2>\<Esc>u"
674 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100675 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100676 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
677 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100678 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100679 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
680 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100681 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100682 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
683 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100684 " R_CTRL-X CTRL-L: Multiple matches
685 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
686 call assert_equal('R-Rc', g:current_modes)
687 " R_CTRL-X CTRL-L: Single match
688 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
689 call assert_equal('R-Rc', g:current_modes)
690 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100691 exe "normal RCom\<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 CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100694 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
695 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100696 " R_CTRL-X CTRL-L: No match
697 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
698 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100699
Bram Moolenaara1449832019-09-01 20:16:52 +0200700 call assert_equal('n', 0->mode())
701 call assert_equal('n', 1->mode())
Bram Moolenaare90858d2017-02-01 17:24:34 +0100702
Bram Moolenaar612cc382018-07-29 15:34:26 +0200703 " i_CTRL-O
704 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
705 call assert_equal("n-niI", g:current_modes)
706
707 " R_CTRL-O
708 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
709 call assert_equal("n-niR", g:current_modes)
710
711 " gR_CTRL-O
712 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
713 call assert_equal("n-niV", g:current_modes)
714
Bram Moolenaare90858d2017-02-01 17:24:34 +0100715 " How to test operator-pending mode?
716
717 call feedkeys("v", 'xt')
718 call assert_equal('v', mode())
719 call assert_equal('v', mode(1))
720 call feedkeys("\<Esc>V", 'xt')
721 call assert_equal('V', mode())
722 call assert_equal('V', mode(1))
723 call feedkeys("\<Esc>\<C-V>", 'xt')
724 call assert_equal("\<C-V>", mode())
725 call assert_equal("\<C-V>", mode(1))
726 call feedkeys("\<Esc>", 'xt')
727
728 call feedkeys("gh", 'xt')
729 call assert_equal('s', mode())
730 call assert_equal('s', mode(1))
731 call feedkeys("\<Esc>gH", 'xt')
732 call assert_equal('S', mode())
733 call assert_equal('S', mode(1))
734 call feedkeys("\<Esc>g\<C-H>", 'xt')
735 call assert_equal("\<C-S>", mode())
736 call assert_equal("\<C-S>", mode(1))
737 call feedkeys("\<Esc>", 'xt')
738
739 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
740 call assert_equal('c-c', g:current_modes)
741 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
742 call assert_equal('c-cv', g:current_modes)
743 " How to test Ex mode?
744
745 bwipe!
746 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100747 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100748endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100749
Bram Moolenaard2007022019-08-27 21:56:06 +0200750func Test_append()
751 enew!
752 split
753 call append(0, ["foo"])
754 split
755 only
756 undo
757endfunc
758
Bram Moolenaar79518e22017-02-17 16:31:35 +0100759func Test_getbufvar()
760 let bnr = bufnr('%')
761 let b:var_num = '1234'
762 let def_num = '5678'
763 call assert_equal('1234', getbufvar(bnr, 'var_num'))
764 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
765
766 let bd = getbufvar(bnr, '')
767 call assert_equal('1234', bd['var_num'])
768 call assert_true(exists("bd['changedtick']"))
769 call assert_equal(2, len(bd))
770
771 let bd2 = getbufvar(bnr, '', def_num)
772 call assert_equal(bd, bd2)
773
774 unlet b:var_num
775 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
776 call assert_equal('', getbufvar(bnr, 'var_num'))
777
778 let bd = getbufvar(bnr, '')
779 call assert_equal(1, len(bd))
780 let bd = getbufvar(bnr, '',def_num)
781 call assert_equal(1, len(bd))
782
Bram Moolenaar4520d442017-03-19 16:09:46 +0100783 call assert_equal('', getbufvar(9999, ''))
784 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100785 unlet def_num
786
Bram Moolenaar507647d2017-02-17 16:43:49 +0100787 call assert_equal(0, getbufvar(bnr, '&autoindent'))
788 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100789
790 " Open new window with forced option values
791 set fileformats=unix,dos
792 new ++ff=dos ++bin ++enc=iso-8859-2
793 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
794 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
795 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
796 close
797
798 set fileformats&
799endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100800
Bram Moolenaar41042f32017-03-09 12:09:32 +0100801func Test_last_buffer_nr()
802 call assert_equal(bufnr('$'), last_buffer_nr())
803endfunc
804
805func Test_stridx()
806 call assert_equal(-1, stridx('', 'l'))
807 call assert_equal(0, stridx('', ''))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200808 call assert_equal(0, 'hello'->stridx(''))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100809 call assert_equal(-1, stridx('hello', 'L'))
810 call assert_equal(2, stridx('hello', 'l', -1))
811 call assert_equal(2, stridx('hello', 'l', 0))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200812 call assert_equal(2, 'hello'->stridx('l', 1))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100813 call assert_equal(3, stridx('hello', 'l', 3))
814 call assert_equal(-1, stridx('hello', 'l', 4))
815 call assert_equal(-1, stridx('hello', 'l', 10))
816 call assert_equal(2, stridx('hello', 'll'))
817 call assert_equal(-1, stridx('hello', 'hello world'))
818endfunc
819
820func Test_strridx()
821 call assert_equal(-1, strridx('', 'l'))
822 call assert_equal(0, strridx('', ''))
823 call assert_equal(5, strridx('hello', ''))
824 call assert_equal(-1, strridx('hello', 'L'))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200825 call assert_equal(3, 'hello'->strridx('l'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100826 call assert_equal(3, strridx('hello', 'l', 10))
827 call assert_equal(3, strridx('hello', 'l', 3))
828 call assert_equal(2, strridx('hello', 'l', 2))
829 call assert_equal(-1, strridx('hello', 'l', 1))
830 call assert_equal(-1, strridx('hello', 'l', 0))
831 call assert_equal(-1, strridx('hello', 'l', -1))
832 call assert_equal(2, strridx('hello', 'll'))
833 call assert_equal(-1, strridx('hello', 'hello world'))
834endfunc
835
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200836func Test_match_func()
837 call assert_equal(4, match('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200838 call assert_equal(4, 'testing'->match('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200839 call assert_equal(-1, match('testing', 'ing', 5))
840 call assert_equal(-1, match('testing', 'ing', 8))
841 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
842 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
843endfunc
844
Bram Moolenaar41042f32017-03-09 12:09:32 +0100845func Test_matchend()
846 call assert_equal(7, matchend('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200847 call assert_equal(7, 'testing'->matchend('ing', 2))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100848 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200849 call assert_equal(-1, matchend('testing', 'ing', 8))
850 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
851 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
852endfunc
853
854func Test_matchlist()
855 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200856 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200857 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
858endfunc
859
860func Test_matchstr()
861 call assert_equal('ing', matchstr('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200862 call assert_equal('ing', 'testing'->matchstr('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200863 call assert_equal('', matchstr('testing', 'ing', 5))
864 call assert_equal('', matchstr('testing', 'ing', 8))
865 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
866 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
867endfunc
868
869func Test_matchstrpos()
870 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200871 call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200872 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
873 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
874 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
875 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100876endfunc
877
878func Test_nextnonblank_prevnonblank()
879 new
880insert
881This
882
883
884is
885
886a
887Test
888.
889 call assert_equal(0, nextnonblank(-1))
890 call assert_equal(0, nextnonblank(0))
891 call assert_equal(1, nextnonblank(1))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200892 call assert_equal(4, 2->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100893 call assert_equal(4, nextnonblank(3))
894 call assert_equal(4, nextnonblank(4))
895 call assert_equal(6, nextnonblank(5))
896 call assert_equal(6, nextnonblank(6))
897 call assert_equal(7, nextnonblank(7))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200898 call assert_equal(0, 8->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100899
900 call assert_equal(0, prevnonblank(-1))
901 call assert_equal(0, prevnonblank(0))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200902 call assert_equal(1, 1->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100903 call assert_equal(1, prevnonblank(2))
904 call assert_equal(1, prevnonblank(3))
905 call assert_equal(4, prevnonblank(4))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200906 call assert_equal(4, 5->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100907 call assert_equal(6, prevnonblank(6))
908 call assert_equal(7, prevnonblank(7))
909 call assert_equal(0, prevnonblank(8))
910 bw!
911endfunc
912
913func Test_byte2line_line2byte()
914 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200915 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +0100916 call setline(1, ['a', 'bc', 'd'])
917
918 set fileformat=unix
919 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
920 \ map(range(-1, 8), 'byte2line(v:val)'))
921 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
922 \ map(range(-1, 5), 'line2byte(v:val)'))
923
924 set fileformat=mac
925 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
Bram Moolenaar64b4d732019-08-22 22:18:17 +0200926 \ map(range(-1, 8), 'v:val->byte2line()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100927 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
Bram Moolenaar02b31112019-08-31 22:16:38 +0200928 \ map(range(-1, 5), 'v:val->line2byte()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100929
930 set fileformat=dos
931 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
932 \ map(range(-1, 11), 'byte2line(v:val)'))
933 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
934 \ map(range(-1, 5), 'line2byte(v:val)'))
935
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200936 bw!
937 set noendofline nofixendofline
938 normal a-
939 for ff in ["unix", "mac", "dos"]
940 let &fileformat = ff
941 call assert_equal(1, line2byte(1))
942 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
943 endfor
944
945 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +0100946 bw!
947endfunc
948
Bram Moolenaar64b4d732019-08-22 22:18:17 +0200949func Test_byteidx()
950 let a = '.é.' " one char of two bytes
951 call assert_equal(0, byteidx(a, 0))
952 call assert_equal(0, byteidxcomp(a, 0))
953 call assert_equal(1, byteidx(a, 1))
954 call assert_equal(1, byteidxcomp(a, 1))
955 call assert_equal(3, byteidx(a, 2))
956 call assert_equal(3, byteidxcomp(a, 2))
957 call assert_equal(4, byteidx(a, 3))
958 call assert_equal(4, byteidxcomp(a, 3))
959 call assert_equal(-1, byteidx(a, 4))
960 call assert_equal(-1, byteidxcomp(a, 4))
961
962 let b = '.é.' " normal e with composing char
963 call assert_equal(0, b->byteidx(0))
964 call assert_equal(1, b->byteidx(1))
965 call assert_equal(4, b->byteidx(2))
966 call assert_equal(5, b->byteidx(3))
967 call assert_equal(-1, b->byteidx(4))
968
969 call assert_equal(0, b->byteidxcomp(0))
970 call assert_equal(1, b->byteidxcomp(1))
971 call assert_equal(2, b->byteidxcomp(2))
972 call assert_equal(4, b->byteidxcomp(3))
973 call assert_equal(5, b->byteidxcomp(4))
974 call assert_equal(-1, b->byteidxcomp(5))
975endfunc
976
Bram Moolenaar41042f32017-03-09 12:09:32 +0100977func Test_count()
978 let l = ['a', 'a', 'A', 'b']
979 call assert_equal(2, count(l, 'a'))
980 call assert_equal(1, count(l, 'A'))
981 call assert_equal(1, count(l, 'b'))
982 call assert_equal(0, count(l, 'B'))
983
984 call assert_equal(2, count(l, 'a', 0))
985 call assert_equal(1, count(l, 'A', 0))
986 call assert_equal(1, count(l, 'b', 0))
987 call assert_equal(0, count(l, 'B', 0))
988
989 call assert_equal(3, count(l, 'a', 1))
990 call assert_equal(3, count(l, 'A', 1))
991 call assert_equal(1, count(l, 'b', 1))
992 call assert_equal(1, count(l, 'B', 1))
993 call assert_equal(0, count(l, 'c', 1))
994
995 call assert_equal(1, count(l, 'a', 0, 1))
996 call assert_equal(2, count(l, 'a', 1, 1))
997 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +0200998 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100999
1000 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
1001 call assert_equal(2, count(d, 'a'))
1002 call assert_equal(1, count(d, 'A'))
1003 call assert_equal(1, count(d, 'b'))
1004 call assert_equal(0, count(d, 'B'))
1005
1006 call assert_equal(2, count(d, 'a', 0))
1007 call assert_equal(1, count(d, 'A', 0))
1008 call assert_equal(1, count(d, 'b', 0))
1009 call assert_equal(0, count(d, 'B', 0))
1010
1011 call assert_equal(3, count(d, 'a', 1))
1012 call assert_equal(3, count(d, 'A', 1))
1013 call assert_equal(1, count(d, 'b', 1))
1014 call assert_equal(1, count(d, 'B', 1))
1015 call assert_equal(0, count(d, 'c', 1))
1016
1017 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +02001018
1019 call assert_equal(0, count("foo", "bar"))
1020 call assert_equal(1, count("foo", "oo"))
1021 call assert_equal(2, count("foo", "o"))
1022 call assert_equal(0, count("foo", "O"))
1023 call assert_equal(2, count("foo", "O", 1))
1024 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +01001025 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001026
1027 call assert_fails('call count(0, 0)', 'E712:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001028endfunc
1029
1030func Test_changenr()
1031 new Xchangenr
1032 call assert_equal(0, changenr())
1033 norm ifoo
1034 call assert_equal(1, changenr())
1035 set undolevels=10
1036 norm Sbar
1037 call assert_equal(2, changenr())
1038 undo
1039 call assert_equal(1, changenr())
1040 redo
1041 call assert_equal(2, changenr())
1042 bw!
1043 set undolevels&
1044endfunc
1045
1046func Test_filewritable()
1047 new Xfilewritable
1048 write!
1049 call assert_equal(1, filewritable('Xfilewritable'))
1050
1051 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1052 call assert_equal(0, filewritable('Xfilewritable'))
1053
1054 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001055 call assert_equal(1, 'Xfilewritable'->filewritable())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001056
1057 call assert_equal(0, filewritable('doesnotexist'))
1058
1059 call delete('Xfilewritable')
1060 bw!
1061endfunc
1062
Bram Moolenaar82956662018-10-06 15:18:45 +02001063func Test_Executable()
1064 if has('win32')
1065 call assert_equal(1, executable('notepad'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001066 call assert_equal(1, 'notepad.exe'->executable())
Bram Moolenaar82956662018-10-06 15:18:45 +02001067 call assert_equal(0, executable('notepad.exe.exe'))
1068 call assert_equal(0, executable('shell32.dll'))
1069 call assert_equal(0, executable('win.ini'))
1070 elseif has('unix')
Bram Moolenaara4208962019-08-24 20:50:19 +02001071 call assert_equal(1, 'cat'->executable())
Bram Moolenaara05a0d32018-10-07 18:43:05 +02001072 call assert_equal(0, executable('nodogshere'))
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001073
1074 " get "cat" path and remove the leading /
1075 let catcmd = exepath('cat')[1:]
1076 new
Bram Moolenaara4208962019-08-24 20:50:19 +02001077 " check that the relative path works in /
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001078 lcd /
1079 call assert_equal(1, executable(catcmd))
Bram Moolenaara4208962019-08-24 20:50:19 +02001080 call assert_equal('/' .. catcmd, catcmd->exepath())
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001081 bwipe
Bram Moolenaar82956662018-10-06 15:18:45 +02001082 endif
1083endfunc
1084
Bram Moolenaar86621892019-03-30 21:51:28 +01001085func Test_executable_longname()
1086 if !has('win32')
1087 return
1088 endif
1089
1090 let fname = 'X' . repeat('あ', 200) . '.bat'
1091 call writefile([], fname)
1092 call assert_equal(1, executable(fname))
1093 call delete(fname)
1094endfunc
1095
Bram Moolenaar41042f32017-03-09 12:09:32 +01001096func Test_hostname()
1097 let hostname_vim = hostname()
1098 if has('unix')
1099 let hostname_system = systemlist('uname -n')[0]
1100 call assert_equal(hostname_vim, hostname_system)
1101 endif
1102endfunc
1103
1104func Test_getpid()
1105 " getpid() always returns the same value within a vim instance.
1106 call assert_equal(getpid(), getpid())
1107 if has('unix')
1108 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1109 endif
1110endfunc
1111
1112func Test_hlexists()
1113 call assert_equal(0, hlexists('does_not_exist'))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001114 call assert_equal(0, 'Number'->hlexists())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001115 call assert_equal(0, highlight_exists('does_not_exist'))
1116 call assert_equal(0, highlight_exists('Number'))
1117 syntax on
1118 call assert_equal(0, hlexists('does_not_exist'))
1119 call assert_equal(1, hlexists('Number'))
1120 call assert_equal(0, highlight_exists('does_not_exist'))
1121 call assert_equal(1, highlight_exists('Number'))
1122 syntax off
1123endfunc
1124
1125func Test_col()
1126 new
1127 call setline(1, 'abcdef')
1128 norm gg4|mx6|mY2|
1129 call assert_equal(2, col('.'))
1130 call assert_equal(7, col('$'))
1131 call assert_equal(4, col("'x"))
1132 call assert_equal(6, col("'Y"))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001133 call assert_equal(2, [1, 2]->col())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001134 call assert_equal(7, col([1, '$']))
1135
1136 call assert_equal(0, col(''))
1137 call assert_equal(0, col('x'))
1138 call assert_equal(0, col([2, '$']))
1139 call assert_equal(0, col([1, 100]))
1140 call assert_equal(0, col([1]))
1141 bw!
1142endfunc
1143
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001144" Test for input()
1145func Test_input_func()
1146 " Test for prompt with multiple lines
1147 redir => v
1148 call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt')
1149 redir END
1150 call assert_equal("B", c)
1151 call assert_equal(['A', 'B', 'C'], split(v, "\n"))
1152
1153 " Test for default value
1154 call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt')
1155 call assert_equal('red', c)
1156
1157 " Test for completion at the input prompt
1158 func! Tcomplete(arglead, cmdline, pos)
1159 return "item1\nitem2\nitem3"
1160 endfunc
1161 call feedkeys(":let c = input('Q? ', '' , 'custom,Tcomplete')\<CR>"
1162 \ .. "\<C-A>\<CR>", 'xt')
1163 delfunc Tcomplete
1164 call assert_equal('item1 item2 item3', c)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001165
1166 call assert_fails("call input('F:', '', 'invalid')", 'E180:')
1167 call assert_fails("call input('F:', '', [])", 'E730:')
1168endfunc
1169
1170" Test for the inputdialog() function
1171func Test_inputdialog()
1172 CheckNotGui
1173
1174 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt')
1175 call assert_equal('xx', v)
1176 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt')
1177 call assert_equal('yy', v)
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001178endfunc
1179
1180" Test for inputlist()
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001181func Test_inputlist()
1182 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1183 call assert_equal(1, c)
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001184 call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001185 call assert_equal(2, c)
1186 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1187 call assert_equal(3, c)
1188
1189 call assert_fails('call inputlist("")', 'E686:')
1190endfunc
1191
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001192func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001193 if has('balloon_eval')
1194 " This won't do anything but must not crash either.
1195 call balloon_show('hi!')
Bram Moolenaar7d8ea0b2020-01-27 23:01:30 +01001196 if !has('gui_running')
1197 call balloon_show(range(3))
1198 endif
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001199 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001200endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001201
1202func Test_setbufvar_options()
1203 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1204 " window layout.
1205 call assert_equal(1, winnr('$'))
1206 split dummy_preview
1207 resize 2
1208 set winfixheight winfixwidth
1209 let prev_id = win_getid()
1210
1211 wincmd j
1212 let wh = winheight('.')
1213 let dummy_buf = bufnr('dummy_buf1', v:true)
1214 call setbufvar(dummy_buf, '&buftype', 'nofile')
1215 execute 'belowright vertical split #' . dummy_buf
1216 call assert_equal(wh, winheight('.'))
1217 let dum1_id = win_getid()
1218
1219 wincmd h
1220 let wh = winheight('.')
1221 let dummy_buf = bufnr('dummy_buf2', v:true)
Bram Moolenaar196b4662019-09-06 21:34:30 +02001222 eval 'nofile'->setbufvar(dummy_buf, '&buftype')
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001223 execute 'belowright vertical split #' . dummy_buf
1224 call assert_equal(wh, winheight('.'))
1225
1226 bwipe!
1227 call win_gotoid(prev_id)
1228 bwipe!
1229 call win_gotoid(dum1_id)
1230 bwipe!
1231endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001232
1233func Test_redo_in_nested_functions()
1234 nnoremap g. :set opfunc=Operator<CR>g@
1235 function Operator( type, ... )
1236 let @x = 'XXX'
1237 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1238 endfunction
1239
1240 function! Apply()
1241 5,6normal! .
1242 endfunction
1243
1244 new
1245 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1246 1normal g.i"
1247 call assert_equal('some "XXX" text', getline(1))
1248 3,4normal .
1249 call assert_equal('some "XXX" text', getline(3))
1250 call assert_equal('more "XXX" text', getline(4))
1251 call Apply()
1252 call assert_equal('some "XXX" text', getline(5))
1253 call assert_equal('more "XXX" text', getline(6))
1254 bwipe!
1255
1256 nunmap g.
1257 delfunc Operator
1258 delfunc Apply
1259endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02001260
1261func Test_shellescape()
1262 let save_shell = &shell
1263 set shell=bash
1264 call assert_equal("'text'", shellescape('text'))
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001265 call assert_equal("'te\"xt'", 'te"xt'->shellescape())
Bram Moolenaar20615522017-06-05 18:46:26 +02001266 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
1267
1268 call assert_equal("'te%xt'", shellescape("te%xt"))
1269 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
1270 call assert_equal("'te#xt'", shellescape("te#xt"))
1271 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
1272 call assert_equal("'te!xt'", shellescape("te!xt"))
1273 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1274
1275 call assert_equal("'te\nxt'", shellescape("te\nxt"))
1276 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1277 set shell=tcsh
1278 call assert_equal("'te\\!xt'", shellescape("te!xt"))
1279 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1280 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1281 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1282
1283 let &shell = save_shell
1284endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001285
1286func Test_trim()
1287 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001288 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 +01001289 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1290 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
1291 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
1292 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
1293 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
1294 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1295 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1296 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
1297 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
1298 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
1299 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
1300 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1301 call assert_equal("", trim("", ""))
1302 call assert_equal("a", trim("a", ""))
1303 call assert_equal("", trim("", "a"))
1304
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001305 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001306 call assert_equal("x", trim(chars . "x" . chars))
1307endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001308
1309" Test for reg_recording() and reg_executing()
1310func Test_reg_executing_and_recording()
1311 let s:reg_stat = ''
1312 func s:save_reg_stat()
1313 let s:reg_stat = reg_recording() . ':' . reg_executing()
1314 return ''
1315 endfunc
1316
1317 new
1318 call s:save_reg_stat()
1319 call assert_equal(':', s:reg_stat)
1320 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1321 call assert_equal('a:', s:reg_stat)
1322 call feedkeys("@a", 'xt')
1323 call assert_equal(':a', s:reg_stat)
1324 call feedkeys("qb@aq", 'xt')
1325 call assert_equal('b:a', s:reg_stat)
1326 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1327 call assert_equal('":', s:reg_stat)
1328
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001329 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001330 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001331 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1332 func TestFunc() abort
1333 normal! ia
1334 endfunc
1335 call feedkeys("@q", 'xt')
1336 call assert_equal(':q', s:reg_stat)
1337 delfunc TestFunc
1338
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001339 " getchar() command saves and restores reg_executing
1340 map W :call TestFunc()<CR>
1341 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001342 let g:typed = ''
1343 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001344 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001345 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001346 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001347 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001348 endfunc
1349 call feedkeys("@qy", 'xt')
1350 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001351 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001352 delfunc TestFunc
1353 unmap W
1354 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001355 unlet g:regs
1356
1357 " input() command saves and restores reg_executing
1358 map W :call TestFunc()<CR>
1359 let @q = "W"
1360 let g:typed = ''
1361 let g:regs = []
1362 func TestFunc() abort
1363 let g:regs += [reg_executing()]
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001364 let g:typed = '?'->input()
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001365 let g:regs += [reg_executing()]
1366 endfunc
1367 call feedkeys("@qy\<CR>", 'xt')
1368 call assert_equal("y", g:typed)
1369 call assert_equal(['q', 'q'], g:regs)
1370 delfunc TestFunc
1371 unmap W
1372 unlet g:typed
1373 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001374
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001375 bwipe!
1376 delfunc s:save_reg_stat
1377 unlet s:reg_stat
1378endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001379
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001380func Test_inputsecret()
1381 map W :call TestFunc()<CR>
1382 let @q = "W"
1383 let g:typed1 = ''
1384 let g:typed2 = ''
1385 let g:regs = []
1386 func TestFunc() abort
1387 let g:typed1 = '?'->inputsecret()
1388 let g:typed2 = inputsecret('password: ')
1389 endfunc
1390 call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1391 call assert_equal("something", g:typed1)
1392 call assert_equal("else", g:typed2)
1393 delfunc TestFunc
1394 unmap W
1395 unlet g:typed1
1396 unlet g:typed2
1397endfunc
1398
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001399func Test_getchar()
1400 call feedkeys('a', '')
1401 call assert_equal(char2nr('a'), getchar())
1402
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001403 call setline(1, 'xxxx')
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001404 call test_setmouse(1, 3)
1405 let v:mouse_win = 9
1406 let v:mouse_winid = 9
1407 let v:mouse_lnum = 9
1408 let v:mouse_col = 9
1409 call feedkeys("\<S-LeftMouse>", '')
1410 call assert_equal("\<S-LeftMouse>", getchar())
1411 call assert_equal(1, v:mouse_win)
1412 call assert_equal(win_getid(1), v:mouse_winid)
1413 call assert_equal(1, v:mouse_lnum)
1414 call assert_equal(3, v:mouse_col)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001415 enew!
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001416endfunc
1417
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001418func Test_libcall_libcallnr()
1419 if !has('libcall')
1420 return
1421 endif
1422
1423 if has('win32')
1424 let libc = 'msvcrt.dll'
1425 elseif has('mac')
1426 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001427 elseif executable('ldd')
1428 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1429 endif
1430 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001431 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001432 if has('linux')
1433 " There is not documented but regarding the 1st argument of glibc's
1434 " dlopen an empty string and nullptr are equivalent, so using an empty
1435 " string for the 1st argument of libcall allows to call functions.
1436 let libc = ''
1437 elseif has('sun')
1438 " Set the path to libc.so according to the architecture.
1439 let test_bits = system('file ' . GetVimProg())
1440 let test_arch = system('uname -p')
1441 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1442 let libc = '/usr/lib/sparcv9/libc.so'
1443 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1444 let libc = '/usr/lib/amd64/libc.so'
1445 else
1446 let libc = '/usr/lib/libc.so'
1447 endif
1448 else
1449 " Unfortunately skip this test until a good way is found.
1450 return
1451 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001452 endif
1453
1454 if has('win32')
Bram Moolenaar02b31112019-08-31 22:16:38 +02001455 call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001456 else
Bram Moolenaar02b31112019-08-31 22:16:38 +02001457 call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001458 endif
1459
1460 " If function returns NULL, libcall() should return an empty string.
1461 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1462
1463 " Test libcallnr() with string and integer argument.
Bram Moolenaar02b31112019-08-31 22:16:38 +02001464 call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1465 call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001466
1467 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1468 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1469
1470 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1471 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1472endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001473
1474sandbox function Fsandbox()
1475 normal ix
1476endfunc
1477
1478func Test_func_sandbox()
1479 sandbox let F = {-> 'hello'}
1480 call assert_equal('hello', F())
1481
Bram Moolenaara4208962019-08-24 20:50:19 +02001482 sandbox let F = {-> "normal ix\<Esc>"->execute()}
Bram Moolenaard90a1442018-07-15 20:24:31 +02001483 call assert_fails('call F()', 'E48:')
1484 unlet F
1485
1486 call assert_fails('call Fsandbox()', 'E48:')
1487 delfunc Fsandbox
1488endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001489
1490func EditAnotherFile()
1491 let word = expand('<cword>')
1492 edit Xfuncrange2
1493endfunc
1494
1495func Test_func_range_with_edit()
1496 " Define a function that edits another buffer, then call it with a range that
1497 " is invalid in that buffer.
1498 call writefile(['just one line'], 'Xfuncrange2')
1499 new
Bram Moolenaar196b4662019-09-06 21:34:30 +02001500 eval 10->range()->setline(1)
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001501 write Xfuncrange1
1502 call assert_fails('5,8call EditAnotherFile()', 'E16:')
1503
1504 call delete('Xfuncrange1')
1505 call delete('Xfuncrange2')
1506 bwipe!
1507endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001508
1509func Test_func_exists_on_reload()
1510 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1511 call assert_equal(0, exists('*ExistingFunction'))
1512 source Xfuncexists
Bram Moolenaara4208962019-08-24 20:50:19 +02001513 call assert_equal(1, '*ExistingFunction'->exists())
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001514 " Redefining a function when reloading a script is OK.
1515 source Xfuncexists
1516 call assert_equal(1, exists('*ExistingFunction'))
1517
1518 " But redefining in another script is not OK.
1519 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1520 call assert_fails('source Xfuncexists2', 'E122:')
1521
1522 delfunc ExistingFunction
1523 call assert_equal(0, exists('*ExistingFunction'))
1524 call writefile([
1525 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1526 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1527 \ ], 'Xfuncexists')
1528 call assert_fails('source Xfuncexists', 'E122:')
1529 call assert_equal(1, exists('*ExistingFunction'))
1530
1531 call delete('Xfuncexists2')
1532 call delete('Xfuncexists')
1533 delfunc ExistingFunction
1534endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01001535
1536" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1537func Test_confirm()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001538 CheckUnix
1539 CheckNotGui
Bram Moolenaar2e050092019-01-27 15:00:36 +01001540
1541 call feedkeys('o', 'L')
1542 let a = confirm('Press O to proceed')
1543 call assert_equal(1, a)
1544
1545 call feedkeys('y', 'L')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001546 let a = 'Are you sure?'->confirm("&Yes\n&No")
Bram Moolenaar2e050092019-01-27 15:00:36 +01001547 call assert_equal(1, a)
1548
1549 call feedkeys('n', 'L')
1550 let a = confirm('Are you sure?', "&Yes\n&No")
1551 call assert_equal(2, a)
1552
1553 " confirm() should return 0 when pressing CTRL-C.
1554 call feedkeys("\<C-c>", 'L')
1555 let a = confirm('Are you sure?', "&Yes\n&No")
1556 call assert_equal(0, a)
1557
1558 " <Esc> requires another character to avoid it being seen as the start of an
1559 " escape sequence. Zero should be harmless.
Bram Moolenaara4208962019-08-24 20:50:19 +02001560 eval "\<Esc>0"->feedkeys('L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01001561 let a = confirm('Are you sure?', "&Yes\n&No")
1562 call assert_equal(0, a)
1563
1564 " Default choice is returned when pressing <CR>.
1565 call feedkeys("\<CR>", 'L')
1566 let a = confirm('Are you sure?', "&Yes\n&No")
1567 call assert_equal(1, a)
1568
1569 call feedkeys("\<CR>", 'L')
1570 let a = confirm('Are you sure?', "&Yes\n&No", 2)
1571 call assert_equal(2, a)
1572
1573 call feedkeys("\<CR>", 'L')
1574 let a = confirm('Are you sure?', "&Yes\n&No", 0)
1575 call assert_equal(0, a)
1576
1577 " Test with the {type} 4th argument
1578 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
1579 call feedkeys('y', 'L')
1580 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
1581 call assert_equal(1, a)
1582 endfor
1583
1584 call assert_fails('call confirm([])', 'E730:')
1585 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
1586 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
1587 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
1588endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001589
1590func Test_platform_name()
1591 " The system matches at most only one name.
1592 let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
1593 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
1594
1595 " Is Unix?
1596 call assert_equal(has('beos'), has('beos') && has('unix'))
1597 call assert_equal(has('bsd'), has('bsd') && has('unix'))
1598 call assert_equal(has('hpux'), has('hpux') && has('unix'))
1599 call assert_equal(has('linux'), has('linux') && has('unix'))
1600 call assert_equal(has('mac'), has('mac') && has('unix'))
1601 call assert_equal(has('qnx'), has('qnx') && has('unix'))
1602 call assert_equal(has('sun'), has('sun') && has('unix'))
1603 call assert_equal(has('win32'), has('win32') && !has('unix'))
1604 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
1605
1606 if has('unix') && executable('uname')
1607 let uname = system('uname')
1608 call assert_equal(uname =~? 'BeOS', has('beos'))
Bram Moolenaara02e3f62019-02-07 21:27:14 +01001609 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
1610 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001611 call assert_equal(uname =~? 'HP-UX', has('hpux'))
1612 call assert_equal(uname =~? 'Linux', has('linux'))
1613 call assert_equal(uname =~? 'Darwin', has('mac'))
1614 call assert_equal(uname =~? 'QNX', has('qnx'))
1615 call assert_equal(uname =~? 'SunOS', has('sun'))
1616 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
1617 endif
1618endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001619
1620func Test_readdir()
1621 call mkdir('Xdir')
1622 call writefile([], 'Xdir/foo.txt')
1623 call writefile([], 'Xdir/bar.txt')
1624 call mkdir('Xdir/dir')
1625
1626 " All results
1627 let files = readdir('Xdir')
1628 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
1629
1630 " Only results containing "f"
Bram Moolenaara0d1fef2019-09-04 22:29:14 +02001631 let files = 'Xdir'->readdir({ x -> stridx(x, 'f') !=- 1 })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001632 call assert_equal(['foo.txt'], sort(files))
1633
1634 " Only .txt files
1635 let files = readdir('Xdir', { x -> x =~ '.txt$' })
1636 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1637
1638 " Only .txt files with string
1639 let files = readdir('Xdir', 'v:val =~ ".txt$"')
1640 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1641
1642 " Limit to 1 result.
1643 let l = []
1644 let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
1645 call assert_equal(1, len(files))
1646
Bram Moolenaar27da7de2019-09-03 17:13:37 +02001647 " Nested readdir() must not crash
1648 let files = readdir('Xdir', 'readdir("Xdir", "1") != []')
1649 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
1650
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001651 eval 'Xdir'->delete('rf')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001652endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02001653
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001654func Test_delete_rf()
1655 call mkdir('Xdir')
1656 call writefile([], 'Xdir/foo.txt')
1657 call writefile([], 'Xdir/bar.txt')
1658 call mkdir('Xdir/[a-1]') " issue #696
1659 call writefile([], 'Xdir/[a-1]/foo.txt')
1660 call writefile([], 'Xdir/[a-1]/bar.txt')
1661 call assert_true(filereadable('Xdir/foo.txt'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001662 call assert_true('Xdir/[a-1]/foo.txt'->filereadable())
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001663
1664 call assert_equal(0, delete('Xdir', 'rf'))
1665 call assert_false(filereadable('Xdir/foo.txt'))
1666 call assert_false(filereadable('Xdir/[a-1]/foo.txt'))
1667endfunc
1668
Bram Moolenaar17aca702019-05-16 22:24:55 +02001669func Test_call()
1670 call assert_equal(3, call('len', [123]))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001671 call assert_equal(3, 'len'->call([123]))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001672 call assert_fails("call call('len', 123)", 'E714:')
1673 call assert_equal(0, call('', []))
1674
1675 function Mylen() dict
1676 return len(self.data)
1677 endfunction
1678 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001679 eval mydict.len->call([], mydict)->assert_equal(4)
Bram Moolenaar17aca702019-05-16 22:24:55 +02001680 call assert_fails("call call('Mylen', [], 0)", 'E715:')
1681endfunc
1682
1683func Test_char2nr()
1684 call assert_equal(12354, char2nr('あ', 1))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001685 call assert_equal(120, 'x'->char2nr())
Bram Moolenaar17aca702019-05-16 22:24:55 +02001686endfunc
1687
1688func Test_eventhandler()
1689 call assert_equal(0, eventhandler())
1690endfunc
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001691
1692func Test_bufadd_bufload()
1693 call assert_equal(0, bufexists('someName'))
1694 let buf = bufadd('someName')
1695 call assert_notequal(0, buf)
1696 call assert_equal(1, bufexists('someName'))
1697 call assert_equal(0, getbufvar(buf, '&buflisted'))
1698 call assert_equal(0, bufloaded(buf))
1699 call bufload(buf)
1700 call assert_equal(1, bufloaded(buf))
1701 call assert_equal([''], getbufline(buf, 1, '$'))
1702
1703 let curbuf = bufnr('')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001704 eval ['some', 'text']->writefile('XotherName')
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001705 let buf = 'XotherName'->bufadd()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001706 call assert_notequal(0, buf)
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001707 eval 'XotherName'->bufexists()->assert_equal(1)
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001708 call assert_equal(0, getbufvar(buf, '&buflisted'))
1709 call assert_equal(0, bufloaded(buf))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001710 eval buf->bufload()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001711 call assert_equal(1, bufloaded(buf))
1712 call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
1713 call assert_equal(curbuf, bufnr(''))
1714
Bram Moolenaar892ae722019-06-30 20:33:01 +02001715 let buf1 = bufadd('')
1716 let buf2 = bufadd('')
1717 call assert_notequal(0, buf1)
1718 call assert_notequal(0, buf2)
1719 call assert_notequal(buf1, buf2)
1720 call assert_equal(1, bufexists(buf1))
1721 call assert_equal(1, bufexists(buf2))
1722 call assert_equal(0, bufloaded(buf1))
1723 exe 'bwipe ' .. buf1
1724 call assert_equal(0, bufexists(buf1))
1725 call assert_equal(1, bufexists(buf2))
1726 exe 'bwipe ' .. buf2
1727 call assert_equal(0, bufexists(buf2))
1728
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001729 bwipe someName
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001730 bwipe XotherName
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001731 call assert_equal(0, bufexists('someName'))
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001732 call delete('XotherName')
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001733endfunc
Bram Moolenaarc2585492019-09-22 21:29:53 +02001734
1735func Test_state()
1736 CheckRunVimInTerminal
1737
1738 let lines =<< trim END
1739 call setline(1, ['one', 'two', 'three'])
1740 map ;; gg
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001741 set complete=.
Bram Moolenaarc2585492019-09-22 21:29:53 +02001742 func RunTimer()
1743 call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
1744 endfunc
1745 au Filetype foobar let g:state = state()|let g:mode = mode()
1746 END
1747 call writefile(lines, 'XState')
1748 let buf = RunVimInTerminal('-S XState', #{rows: 6})
1749
1750 " Using a ":" command Vim is busy, thus "S" is returned
1751 call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
1752 call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
1753 call term_sendkeys(buf, ":\<CR>")
1754
1755 " Using a timer callback
1756 call term_sendkeys(buf, ":call RunTimer()\<CR>")
1757 call term_wait(buf, 50)
1758 let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"
1759 call term_sendkeys(buf, getstate)
1760 call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)
1761
1762 " Halfway a mapping
1763 call term_sendkeys(buf, ":call RunTimer()\<CR>;")
1764 call term_wait(buf, 50)
1765 call term_sendkeys(buf, ";")
1766 call term_sendkeys(buf, getstate)
1767 call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)
1768
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001769 " Insert mode completion (bit slower on Mac)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001770 call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001771 call term_wait(buf, 200)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001772 call term_sendkeys(buf, "\<Esc>")
1773 call term_sendkeys(buf, getstate)
1774 call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)
1775
1776 " Autocommand executing
1777 call term_sendkeys(buf, ":set filetype=foobar\<CR>")
1778 call term_wait(buf, 50)
1779 call term_sendkeys(buf, getstate)
1780 call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)
1781
1782 " Todo: "w" - waiting for ch_evalexpr()
1783
1784 " messages scrolled
1785 call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
1786 call term_wait(buf, 50)
1787 call term_sendkeys(buf, "\<CR>")
1788 call term_sendkeys(buf, getstate)
1789 call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)
1790
1791 call StopVimInTerminal(buf)
1792 call delete('XState')
1793endfunc
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001794
1795func Test_range()
1796 " destructuring
1797 let [x, y] = range(2)
1798 call assert_equal([0, 1], [x, y])
1799
1800 " index
1801 call assert_equal(4, range(1, 10)[3])
1802
1803 " add()
1804 call assert_equal([0, 1, 2, 3], add(range(3), 3))
1805 call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3)))
1806 call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3)))
1807
1808 " append()
1809 new
1810 call append('.', range(5))
1811 call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$'))
1812 bwipe!
1813
1814 " appendbufline()
1815 new
1816 call appendbufline(bufnr(''), '.', range(5))
1817 call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$'))
1818 bwipe!
1819
1820 " call()
1821 func TwoArgs(a, b)
1822 return [a:a, a:b]
1823 endfunc
1824 call assert_equal([0, 1], call('TwoArgs', range(2)))
1825
1826 " col()
1827 new
1828 call setline(1, ['foo', 'bar'])
1829 call assert_equal(2, col(range(1, 2)))
1830 bwipe!
1831
1832 " complete()
1833 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>"
1834 " complete_info()
1835 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>"
1836
1837 " copy()
1838 call assert_equal([1, 2, 3], copy(range(1, 3)))
1839
1840 " count()
1841 call assert_equal(0, count(range(0), 3))
1842 call assert_equal(0, count(range(2), 3))
1843 call assert_equal(1, count(range(5), 3))
1844
1845 " cursor()
1846 new
1847 call setline(1, ['aaa', 'bbb', 'ccc'])
1848 call cursor(range(1, 2))
1849 call assert_equal([2, 1], [col('.'), line('.')])
1850 bwipe!
1851
1852 " deepcopy()
1853 call assert_equal([1, 2, 3], deepcopy(range(1, 3)))
1854
1855 " empty()
1856 call assert_true(empty(range(0)))
1857 call assert_false(empty(range(2)))
1858
1859 " execute()
1860 new
1861 call setline(1, ['aaa', 'bbb', 'ccc'])
1862 call execute(range(3))
1863 call assert_equal(2, line('.'))
1864 bwipe!
1865
1866 " extend()
1867 call assert_equal([1, 2, 3, 4], extend([1], range(2, 4)))
1868 call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4)))
1869 call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4]))
1870
1871 " filter()
1872 call assert_equal([1, 3], filter(range(5), 'v:val % 2'))
1873
1874 " funcref()
1875 call assert_equal([0, 1], funcref('TwoArgs', range(2))())
1876
1877 " function()
1878 call assert_equal([0, 1], function('TwoArgs', range(2))())
1879
1880 " garbagecollect()
1881 let thelist = [1, range(2), 3]
1882 let otherlist = range(3)
1883 call test_garbagecollect_now()
1884
1885 " get()
1886 call assert_equal(4, get(range(1, 10), 3))
1887 call assert_equal(-1, get(range(1, 10), 42, -1))
1888
1889 " index()
1890 call assert_equal(1, index(range(1, 5), 2))
1891
1892 " inputlist()
Bram Moolenaar272ca952020-01-28 20:49:11 +01001893 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x')
1894 call assert_equal(1, result)
1895 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x')
1896 call assert_equal(1, result)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001897
1898 " insert()
1899 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42))
1900 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0))
1901 call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1))
1902 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4))
1903 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1))
1904 call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5))
1905
1906 " join()
1907 call assert_equal('0 1 2 3 4', join(range(5)))
1908
Bram Moolenaar272ca952020-01-28 20:49:11 +01001909 " json_encode()
1910 call assert_equal('[0,1,2,3]', json_encode(range(4)))
1911
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001912 " len()
1913 call assert_equal(0, len(range(0)))
1914 call assert_equal(2, len(range(2)))
1915 call assert_equal(5, len(range(0, 12, 3)))
1916 call assert_equal(4, len(range(3, 0, -1)))
1917
1918 " list2str()
1919 call assert_equal('ABC', list2str(range(65, 67)))
1920
1921 " lock()
1922 let thelist = range(5)
1923 lockvar thelist
1924
1925 " map()
1926 call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2'))
1927
1928 " match()
1929 call assert_equal(3, match(range(5), 3))
1930
1931 " matchaddpos()
1932 highlight MyGreenGroup ctermbg=green guibg=green
1933 call matchaddpos('MyGreenGroup', range(line('.'), line('.')))
1934
1935 " matchend()
1936 call assert_equal(4, matchend(range(5), '4'))
1937 call assert_equal(3, matchend(range(1, 5), '4'))
1938 call assert_equal(-1, matchend(range(1, 5), '42'))
1939
1940 " matchstrpos()
1941 call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4'))
1942 call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4'))
1943 call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42'))
1944
1945 " max() reverse()
1946 call assert_equal(0, max(range(0)))
1947 call assert_equal(0, max(range(10, 9)))
1948 call assert_equal(9, max(range(10)))
1949 call assert_equal(18, max(range(0, 20, 3)))
1950 call assert_equal(20, max(range(20, 0, -3)))
1951 call assert_equal(99999, max(range(100000)))
1952 call assert_equal(99999, max(range(99999, 0, -1)))
1953 call assert_equal(99999, max(reverse(range(100000))))
1954 call assert_equal(99999, max(reverse(range(99999, 0, -1))))
1955
1956 " min() reverse()
1957 call assert_equal(0, min(range(0)))
1958 call assert_equal(0, min(range(10, 9)))
1959 call assert_equal(5, min(range(5, 10)))
1960 call assert_equal(5, min(range(5, 10, 3)))
1961 call assert_equal(2, min(range(20, 0, -3)))
1962 call assert_equal(0, min(range(100000)))
1963 call assert_equal(0, min(range(99999, 0, -1)))
1964 call assert_equal(0, min(reverse(range(100000))))
1965 call assert_equal(0, min(reverse(range(99999, 0, -1))))
1966
1967 " remove()
1968 call assert_equal(1, remove(range(1, 10), 0))
1969 call assert_equal(2, remove(range(1, 10), 1))
1970 call assert_equal(9, remove(range(1, 10), 8))
1971 call assert_equal(10, remove(range(1, 10), 9))
1972 call assert_equal(10, remove(range(1, 10), -1))
1973 call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4))
1974
1975 " repeat()
1976 call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2))
1977 call assert_equal([0, 1, 2], repeat(range(3), 1))
1978 call assert_equal([], repeat(range(3), 0))
1979 call assert_equal([], repeat(range(5, 4), 2))
1980 call assert_equal([], repeat(range(5, 4), 0))
1981
1982 " reverse()
1983 call assert_equal([2, 1, 0], reverse(range(3)))
1984 call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1)))
1985 call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10)))
1986 call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20)))
1987 call assert_equal([16, 13, 10], reverse(range(10, 18, 3)))
1988 call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3)))
1989 call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3)))
1990 call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3)))
1991 call assert_equal([], reverse(range(0)))
1992
1993 " TODO: setpos()
1994 " new
1995 " call setline(1, repeat([''], bufnr('')))
1996 " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6))
1997 " call setpos('x', range(bufnr(''), bufnr('') + 3))
1998 " bwipe!
1999
2000 " setreg()
2001 call setreg('a', range(3))
2002 call assert_equal("0\n1\n2\n", getreg('a'))
2003
Bram Moolenaarb0992022020-01-30 14:55:42 +01002004 " settagstack()
2005 call settagstack(1, #{items : range(4)})
Bram Moolenaar94255df2020-02-05 20:10:33 +01002006
Bram Moolenaarb0992022020-01-30 14:55:42 +01002007 " sign_define()
2008 call assert_fails("call sign_define(range(5))", "E715:")
2009 call assert_fails("call sign_placelist(range(5))", "E715:")
2010
2011 " sign_undefine()
2012 call assert_fails("call sign_undefine(range(5))", "E908:")
2013
2014 " sign_unplacelist()
2015 call assert_fails("call sign_unplacelist(range(5))", "E715:")
2016
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002017 " sort()
2018 call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1)))
2019
Bram Moolenaarb0992022020-01-30 14:55:42 +01002020 " 'spellsuggest'
2021 func MySuggest()
2022 return range(3)
2023 endfunc
2024 set spell spellsuggest=expr:MySuggest()
2025 call assert_equal([], spellsuggest('baord', 3))
2026 set nospell spellsuggest&
2027
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002028 " string()
2029 call assert_equal('[0, 1, 2, 3, 4]', string(range(5)))
2030
Bram Moolenaarb0992022020-01-30 14:55:42 +01002031 " taglist() with 'tagfunc'
2032 func TagFunc(pattern, flags, info)
2033 return range(10)
2034 endfunc
2035 set tagfunc=TagFunc
2036 call assert_fails("call taglist('asdf')", 'E987:')
2037 set tagfunc=
Bram Moolenaar94255df2020-02-05 20:10:33 +01002038
Bram Moolenaarb0992022020-01-30 14:55:42 +01002039 " term_start()
Bram Moolenaar705724e2020-01-31 21:13:42 +01002040 if has('terminal') && has('termguicolors')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002041 call assert_fails('call term_start(range(3, 4))', 'E474:')
2042 let g:terminal_ansi_colors = range(16)
Bram Moolenaar94255df2020-02-05 20:10:33 +01002043 if has('win32')
2044 let cmd = "cmd /c dir"
2045 else
2046 let cmd = "ls"
2047 endif
2048 call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"})', 'E475:')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002049 unlet g:terminal_ansi_colors
2050 endif
2051
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002052 " type()
2053 call assert_equal(v:t_list, type(range(5)))
2054
2055 " uniq()
2056 call assert_equal([0, 1, 2, 3, 4], uniq(range(5)))
2057endfunc
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002058
2059func Test_echoraw()
2060 CheckScreendump
2061
2062 " Normally used for escape codes, but let's test with a CR.
2063 let lines =<< trim END
2064 call echoraw("hello\<CR>x")
2065 END
2066 call writefile(lines, 'XTest_echoraw')
2067 let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40})
2068 call VerifyScreenDump(buf, 'Test_functions_echoraw', {})
2069
2070 " clean up
2071 call StopVimInTerminal(buf)
2072 call delete('XTest_echoraw')
2073endfunc
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002074
2075" vim: shiftwidth=2 sts=2 expandtab