blob: 6f3b81a30c1868b57c601878636e1432398b4465 [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 Moolenaar7c215c52020-02-29 13:43:27 +010061
62 call assert_fails("call empty(test_void())", 'E685:')
63 call assert_fails("call empty(test_unknown())", 'E685:')
Bram Moolenaar24c2e482017-01-29 15:45:12 +010064endfunc
65
66func Test_len()
67 call assert_equal(1, len(0))
68 call assert_equal(2, len(12))
69
70 call assert_equal(0, len(''))
71 call assert_equal(2, len('ab'))
72
73 call assert_equal(0, len([]))
74 call assert_equal(2, len([2, 1]))
75
76 call assert_equal(0, len({}))
77 call assert_equal(2, len({'a': 1, 'b': 2}))
78
79 call assert_fails('call len(v:none)', 'E701:')
80 call assert_fails('call len({-> 0})', 'E701:')
81endfunc
82
83func Test_max()
84 call assert_equal(0, max([]))
85 call assert_equal(2, max([2]))
86 call assert_equal(2, max([1, 2]))
87 call assert_equal(2, max([1, 2, v:null]))
88
89 call assert_equal(0, max({}))
90 call assert_equal(2, max({'a':1, 'b':2}))
91
92 call assert_fails('call max(1)', 'E712:')
93 call assert_fails('call max(v:none)', 'E712:')
94endfunc
95
96func Test_min()
97 call assert_equal(0, min([]))
98 call assert_equal(2, min([2]))
99 call assert_equal(1, min([1, 2]))
100 call assert_equal(0, min([1, 2, v:null]))
101
102 call assert_equal(0, min({}))
103 call assert_equal(1, min({'a':1, 'b':2}))
104
105 call assert_fails('call min(1)', 'E712:')
106 call assert_fails('call min(v:none)', 'E712:')
107endfunc
108
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200109func Test_strwidth()
110 for aw in ['single', 'double']
111 exe 'set ambiwidth=' . aw
112 call assert_equal(0, strwidth(''))
113 call assert_equal(1, strwidth("\t"))
114 call assert_equal(3, strwidth('Vim'))
115 call assert_equal(4, strwidth(1234))
116 call assert_equal(5, strwidth(-1234))
117
Bram Moolenaar30276f22019-01-24 17:59:39 +0100118 call assert_equal(2, strwidth('😉'))
119 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
120 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200121
122 call assert_fails('call strwidth({->0})', 'E729:')
123 call assert_fails('call strwidth([])', 'E730:')
124 call assert_fails('call strwidth({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100125 if has('float')
126 call assert_fails('call strwidth(1.2)', 'E806:')
127 endif
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200128 endfor
129
130 set ambiwidth&
131endfunc
132
Bram Moolenaar08243d22017-01-10 16:12:29 +0100133func Test_str2nr()
134 call assert_equal(0, str2nr(''))
135 call assert_equal(1, str2nr('1'))
136 call assert_equal(1, str2nr(' 1 '))
137
138 call assert_equal(1, str2nr('+1'))
139 call assert_equal(1, str2nr('+ 1'))
140 call assert_equal(1, str2nr(' + 1 '))
141
142 call assert_equal(-1, str2nr('-1'))
143 call assert_equal(-1, str2nr('- 1'))
144 call assert_equal(-1, str2nr(' - 1 '))
145
146 call assert_equal(123456789, str2nr('123456789'))
147 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100148
149 call assert_equal(5, str2nr('101', 2))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200150 call assert_equal(5, '0b101'->str2nr(2))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100151 call assert_equal(5, str2nr('0B101', 2))
152 call assert_equal(-5, str2nr('-101', 2))
153 call assert_equal(-5, str2nr('-0b101', 2))
154 call assert_equal(-5, str2nr('-0B101', 2))
155
156 call assert_equal(65, str2nr('101', 8))
157 call assert_equal(65, str2nr('0101', 8))
158 call assert_equal(-65, str2nr('-101', 8))
159 call assert_equal(-65, str2nr('-0101', 8))
160
161 call assert_equal(11259375, str2nr('abcdef', 16))
162 call assert_equal(11259375, str2nr('ABCDEF', 16))
163 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
164 call assert_equal(11259375, str2nr('0xabcdef', 16))
165 call assert_equal(11259375, str2nr('0Xabcdef', 16))
166 call assert_equal(11259375, str2nr('0XABCDEF', 16))
167 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
168
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200169 call assert_equal(1, str2nr("1'000'000", 10, 0))
170 call assert_equal(256, str2nr("1'0000'0000", 2, 1))
171 call assert_equal(262144, str2nr("1'000'000", 8, 1))
172 call assert_equal(1000000, str2nr("1'000'000", 10, 1))
Bram Moolenaarea8dcf82019-09-15 21:12:22 +0200173 call assert_equal(1000, str2nr("1'000''000", 10, 1))
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200174 call assert_equal(65536, str2nr("1'00'00", 16, 1))
175
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100176 call assert_equal(0, str2nr('0x10'))
177 call assert_equal(0, str2nr('0b10'))
178 call assert_equal(1, str2nr('12', 2))
179 call assert_equal(1, str2nr('18', 8))
180 call assert_equal(1, str2nr('1g', 16))
181
182 call assert_equal(0, str2nr(v:null))
183 call assert_equal(0, str2nr(v:none))
184
185 call assert_fails('call str2nr([])', 'E730:')
186 call assert_fails('call str2nr({->2})', 'E729:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100187 if has('float')
188 call assert_fails('call str2nr(1.2)', 'E806:')
189 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100190 call assert_fails('call str2nr(10, [])', 'E474:')
191endfunc
192
193func Test_strftime()
Bram Moolenaar10455d42019-11-21 15:36:18 +0100194 CheckFunction strftime
195
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100196 " Format of strftime() depends on system. We assume
197 " that basic formats tested here are available and
198 " identical on all systems which support strftime().
199 "
200 " The 2nd parameter of strftime() is a local time, so the output day
201 " of strftime() can be 17 or 18, depending on timezone.
202 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
203 "
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200204 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 +0100205
206 call assert_fails('call strftime([])', 'E730:')
207 call assert_fails('call strftime("%Y", [])', 'E745:')
Bram Moolenaardb517302019-06-18 22:53:24 +0200208
209 " Check that the time changes after we change the timezone
210 " Save previous timezone value, if any
211 if exists('$TZ')
212 let tz = $TZ
213 endif
214
215 " Force EST and then UTC, save the current hour (24-hour clock) for each
216 let $TZ = 'EST' | let est = strftime('%H')
217 let $TZ = 'UTC' | let utc = strftime('%H')
218
219 " Those hours should be two bytes long, and should not be the same; if they
220 " are, a tzset(3) call may have failed somewhere
221 call assert_equal(strlen(est), 2)
222 call assert_equal(strlen(utc), 2)
Bram Moolenaar87652a72019-06-18 23:07:37 +0200223 " TODO: this fails on MS-Windows
224 if has('unix')
225 call assert_notequal(est, utc)
226 endif
Bram Moolenaardb517302019-06-18 22:53:24 +0200227
228 " If we cached a timezone value, put it back, otherwise clear it
229 if exists('tz')
230 let $TZ = tz
231 else
232 unlet $TZ
233 endif
Bram Moolenaar10455d42019-11-21 15:36:18 +0100234endfunc
Bram Moolenaardb517302019-06-18 22:53:24 +0200235
Bram Moolenaar10455d42019-11-21 15:36:18 +0100236func Test_strptime()
237 CheckFunction strptime
238
239 if exists('$TZ')
240 let tz = $TZ
241 endif
242 let $TZ = 'UTC'
243
Bram Moolenaar9a838fe2019-12-06 12:45:01 +0100244 call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
Bram Moolenaar10455d42019-11-21 15:36:18 +0100245
246 call assert_fails('call strptime()', 'E119:')
247 call assert_fails('call strptime("xxx")', 'E119:')
248 call assert_equal(0, strptime("%Y", ''))
249 call assert_equal(0, strptime("%Y", "xxx"))
250
251 if exists('tz')
252 let $TZ = tz
253 else
254 unlet $TZ
255 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100256endfunc
257
Bram Moolenaardce1e892019-02-10 23:18:53 +0100258func Test_resolve_unix()
Bram Moolenaar26109902018-10-06 15:43:17 +0200259 if !has('unix')
260 return
261 endif
262
263 " Xlink1 -> Xlink2
264 " Xlink2 -> Xlink3
265 silent !ln -s -f Xlink2 Xlink1
266 silent !ln -s -f Xlink3 Xlink2
267 call assert_equal('Xlink3', resolve('Xlink1'))
268 call assert_equal('./Xlink3', resolve('./Xlink1'))
269 call assert_equal('Xlink3/', resolve('Xlink2/'))
270 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
271 "call assert_equal('Xlink3/', resolve('Xlink1/'))
272 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
273 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
274 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
275
276 " Test resolve() with a symlink cycle.
277 " Xlink1 -> Xlink2
278 " Xlink2 -> Xlink3
279 " Xlink3 -> Xlink1
280 silent !ln -s -f Xlink1 Xlink3
281 call assert_fails('call resolve("Xlink1")', 'E655:')
282 call assert_fails('call resolve("./Xlink1")', 'E655:')
283 call assert_fails('call resolve("Xlink2")', 'E655:')
284 call assert_fails('call resolve("Xlink3")', 'E655:')
285 call delete('Xlink1')
286 call delete('Xlink2')
287 call delete('Xlink3')
288
289 silent !ln -s -f Xdir//Xfile Xlink
290 call assert_equal('Xdir/Xfile', resolve('Xlink'))
291 call delete('Xlink')
292
293 silent !ln -s -f Xlink2/ Xlink1
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200294 call assert_equal('Xlink2', 'Xlink1'->resolve())
Bram Moolenaar26109902018-10-06 15:43:17 +0200295 call assert_equal('Xlink2/', resolve('Xlink1/'))
296 call delete('Xlink1')
297
298 silent !ln -s -f ./Xlink2 Xlink1
299 call assert_equal('Xlink2', resolve('Xlink1'))
300 call assert_equal('./Xlink2', resolve('./Xlink1'))
301 call delete('Xlink1')
302endfunc
303
Bram Moolenaardce1e892019-02-10 23:18:53 +0100304func s:normalize_fname(fname)
305 let ret = substitute(a:fname, '\', '/', 'g')
306 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200307 return ret->tolower()
Bram Moolenaardce1e892019-02-10 23:18:53 +0100308endfunc
309
310func Test_resolve_win32()
311 if !has('win32')
312 return
313 endif
314
315 " test for shortcut file
316 if executable('cscript')
317 new Xfile
318 wq
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200319 let lines =<< trim END
320 Set fs = CreateObject("Scripting.FileSystemObject")
321 Set ws = WScript.CreateObject("WScript.Shell")
322 Set shortcut = ws.CreateShortcut("Xlink.lnk")
323 shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")
324 shortcut.Save
325 END
326 call writefile(lines, 'link.vbs')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100327 silent !cscript link.vbs
328 call delete('link.vbs')
329 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
330 call delete('Xfile')
331
332 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
333 call delete('Xlink.lnk')
334 else
335 echomsg 'skipped test for shortcut file'
336 endif
337
338 " remove files
339 call delete('Xlink')
340 call delete('Xdir', 'd')
341 call delete('Xfile')
342
343 " test for symbolic link to a file
344 new Xfile
345 wq
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200346 call assert_equal('Xfile', resolve('Xfile'))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100347 silent !mklink Xlink Xfile
348 if !v:shell_error
349 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink')))
350 call delete('Xlink')
351 else
352 echomsg 'skipped test for symbolic link to a file'
353 endif
354 call delete('Xfile')
355
356 " test for junction to a directory
357 call mkdir('Xdir')
358 silent !mklink /J Xlink Xdir
359 if !v:shell_error
360 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
361
362 call delete('Xdir', 'd')
363
364 " test for junction already removed
365 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
366 call delete('Xlink')
367 else
368 echomsg 'skipped test for junction to a directory'
369 call delete('Xdir', 'd')
370 endif
371
372 " test for symbolic link to a directory
373 call mkdir('Xdir')
374 silent !mklink /D Xlink Xdir
375 if !v:shell_error
376 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
377
378 call delete('Xdir', 'd')
379
380 " test for symbolic link already removed
381 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
382 call delete('Xlink')
383 else
384 echomsg 'skipped test for symbolic link to a directory'
385 call delete('Xdir', 'd')
386 endif
387
388 " test for buffer name
389 new Xfile
390 wq
391 silent !mklink Xlink Xfile
392 if !v:shell_error
393 edit Xlink
394 call assert_equal('Xlink', bufname('%'))
395 call delete('Xlink')
396 bw!
397 else
398 echomsg 'skipped test for buffer name'
399 endif
400 call delete('Xfile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200401
402 " test for reparse point
403 call mkdir('Xdir')
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200404 call assert_equal('Xdir', resolve('Xdir'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200405 silent !mklink /D Xdirlink Xdir
406 if !v:shell_error
407 w Xdir/text.txt
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200408 call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200409 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
410 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200411 call delete('Xdirlink')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200412 else
413 echomsg 'skipped test for reparse point'
414 endif
415
416 call delete('Xdir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100417endfunc
418
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100419func Test_simplify()
420 call assert_equal('', simplify(''))
421 call assert_equal('/', simplify('/'))
422 call assert_equal('/', simplify('/.'))
423 call assert_equal('/', simplify('/..'))
424 call assert_equal('/...', simplify('/...'))
425 call assert_equal('./dir/file', simplify('./dir/file'))
426 call assert_equal('./dir/file', simplify('.///dir//file'))
427 call assert_equal('./dir/file', simplify('./dir/./file'))
428 call assert_equal('./file', simplify('./dir/../file'))
429 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
430 call assert_equal('./file', simplify('dir/.././file'))
431
432 call assert_fails('call simplify({->0})', 'E729:')
433 call assert_fails('call simplify([])', 'E730:')
434 call assert_fails('call simplify({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100435 if has('float')
436 call assert_fails('call simplify(1.2)', 'E806:')
437 endif
Bram Moolenaar08243d22017-01-10 16:12:29 +0100438endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100439
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200440func Test_pathshorten()
441 call assert_equal('', pathshorten(''))
442 call assert_equal('foo', pathshorten('foo'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200443 call assert_equal('/foo', '/foo'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200444 call assert_equal('f/', pathshorten('foo/'))
445 call assert_equal('f/bar', pathshorten('foo/bar'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200446 call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200447 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
448 call assert_equal('.f/bar', pathshorten('.foo/bar'))
449 call assert_equal('~f/bar', pathshorten('~foo/bar'))
450 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
451 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
452 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
453endfunc
454
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100455func Test_strpart()
456 call assert_equal('de', strpart('abcdefg', 3, 2))
457 call assert_equal('ab', strpart('abcdefg', -2, 4))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200458 call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100459 call assert_equal('fg', strpart('abcdefg', 5, 4))
460 call assert_equal('defg', strpart('abcdefg', 3))
461
Bram Moolenaar30276f22019-01-24 17:59:39 +0100462 call assert_equal('lép', strpart('éléphant', 2, 4))
463 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100464endfunc
465
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100466func Test_tolower()
467 call assert_equal("", tolower(""))
468
469 " Test with all printable ASCII characters.
470 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
471 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
472
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100473 " Test with a few uppercase diacritics.
474 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
475 call assert_equal("bḃḇ", tolower("BḂḆ"))
476 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
477 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
478 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
479 call assert_equal("fḟ ", tolower("FḞ "))
480 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
481 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
482 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
483 call assert_equal("jĵ", tolower("JĴ"))
484 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
485 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
486 call assert_equal("mḿṁ", tolower("MḾṀ"))
487 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
488 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
489 call assert_equal("pṕṗ", tolower("PṔṖ"))
490 call assert_equal("q", tolower("Q"))
491 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
492 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
493 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
494 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
495 call assert_equal("vṽ", tolower("VṼ"))
496 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
497 call assert_equal("xẋẍ", tolower("XẊẌ"))
498 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
499 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
500
501 " Test with a few lowercase diacritics, which should remain unchanged.
502 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
503 call assert_equal("bḃḇ", tolower("bḃḇ"))
504 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
505 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
506 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
507 call assert_equal("fḟ", tolower("fḟ"))
508 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
509 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
510 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
511 call assert_equal("jĵǰ", tolower("jĵǰ"))
512 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
513 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
514 call assert_equal("mḿṁ ", tolower("mḿṁ "))
515 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
516 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
517 call assert_equal("pṕṗ", tolower("pṕṗ"))
518 call assert_equal("q", tolower("q"))
519 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
520 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
521 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
522 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
523 call assert_equal("vṽ", tolower("vṽ"))
524 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
525 call assert_equal("ẋẍ", tolower("ẋẍ"))
526 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
527 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
528
529 " According to https://twitter.com/jifa/status/625776454479970304
530 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
531 " in length (2 to 3 bytes) when lowercased. So let's test them.
532 call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100533
534 " This call to tolower with invalid utf8 sequence used to cause access to
535 " invalid memory.
536 call tolower("\xC0\x80\xC0")
537 call tolower("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100538endfunc
539
540func Test_toupper()
541 call assert_equal("", toupper(""))
542
543 " Test with all printable ASCII characters.
544 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
545 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
546
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100547 " Test with a few lowercase diacritics.
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200548 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100549 call assert_equal("BḂḆ", toupper("bḃḇ"))
550 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
551 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
552 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
553 call assert_equal("FḞ", toupper("fḟ"))
554 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
555 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
556 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
557 call assert_equal("JĴǰ", toupper("jĵǰ"))
558 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
559 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
560 call assert_equal("MḾṀ ", toupper("mḿṁ "))
561 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
562 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
563 call assert_equal("PṔṖ", toupper("pṕṗ"))
564 call assert_equal("Q", toupper("q"))
565 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
566 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
567 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
568 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
569 call assert_equal("VṼ", toupper("vṽ"))
570 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
571 call assert_equal("ẊẌ", toupper("ẋẍ"))
572 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
573 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
574
575 " Test that uppercase diacritics, which should remain unchanged.
576 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
577 call assert_equal("BḂḆ", toupper("BḂḆ"))
578 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
579 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
580 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
581 call assert_equal("FḞ ", toupper("FḞ "))
582 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
583 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
584 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
585 call assert_equal("JĴ", toupper("JĴ"))
586 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
587 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
588 call assert_equal("MḾṀ", toupper("MḾṀ"))
589 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
590 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
591 call assert_equal("PṔṖ", toupper("PṔṖ"))
592 call assert_equal("Q", toupper("Q"))
593 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
594 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
595 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
596 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
597 call assert_equal("VṼ", toupper("VṼ"))
598 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
599 call assert_equal("XẊẌ", toupper("XẊẌ"))
600 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
601 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
602
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100603 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100604
605 " This call to toupper with invalid utf8 sequence used to cause access to
606 " invalid memory.
607 call toupper("\xC0\x80\xC0")
608 call toupper("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100609endfunc
610
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200611func Test_tr()
612 call assert_equal('foo', tr('bar', 'bar', 'foo'))
613 call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
614endfunc
615
Bram Moolenaare90858d2017-02-01 17:24:34 +0100616" Tests for the mode() function
617let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100618func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100619 let g:current_modes = mode(0) . '-' . mode(1)
620 return ''
621endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100622
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100623func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100624 new
625 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
626
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100627 " Only complete from the current buffer.
628 set complete=.
629
Bram Moolenaare90858d2017-02-01 17:24:34 +0100630 inoremap <F2> <C-R>=Save_mode()<CR>
631
632 normal! 3G
633 exe "normal i\<F2>\<Esc>"
634 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100635 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100636 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
637 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100638 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100639 exe "normal iBro\<C-P>\<F2>\<Esc>u"
640 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100641 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100642 exe "normal iBa\<C-X>\<F2>\<Esc>u"
643 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100644 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100645 exe "normal iBa\<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: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100648 exe "normal iBro\<C-X>\<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-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100651 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
652 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100653 " i_CTRL-X CTRL-L: Multiple matches
654 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
655 call assert_equal('i-ic', g:current_modes)
656 " i_CTRL-X CTRL-L: Single match
657 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
658 call assert_equal('i-ic', g:current_modes)
659 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100660 exe "normal iCom\<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-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100663 exe "normal iCom\<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-L: No match
666 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
667 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100668
Bram Moolenaare971df32017-02-05 14:15:29 +0100669 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100670 exe "normal RBa\<C-P>\<F2>\<Esc>u"
671 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100672 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100673 exe "normal RBro\<C-P>\<F2>\<Esc>u"
674 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100675 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100676 exe "normal RBa\<C-X>\<F2>\<Esc>u"
677 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100678 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100679 exe "normal RBa\<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: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100682 exe "normal RBro\<C-X>\<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-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100685 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
686 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100687 " R_CTRL-X CTRL-L: Multiple matches
688 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
689 call assert_equal('R-Rc', g:current_modes)
690 " R_CTRL-X CTRL-L: Single match
691 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
692 call assert_equal('R-Rc', g:current_modes)
693 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100694 exe "normal RCom\<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-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100697 exe "normal RCom\<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-L: No match
700 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
701 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100702
Bram Moolenaara1449832019-09-01 20:16:52 +0200703 call assert_equal('n', 0->mode())
704 call assert_equal('n', 1->mode())
Bram Moolenaare90858d2017-02-01 17:24:34 +0100705
Bram Moolenaar612cc382018-07-29 15:34:26 +0200706 " i_CTRL-O
707 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
708 call assert_equal("n-niI", g:current_modes)
709
710 " R_CTRL-O
711 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
712 call assert_equal("n-niR", g:current_modes)
713
714 " gR_CTRL-O
715 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
716 call assert_equal("n-niV", g:current_modes)
717
Bram Moolenaare90858d2017-02-01 17:24:34 +0100718 " How to test operator-pending mode?
719
720 call feedkeys("v", 'xt')
721 call assert_equal('v', mode())
722 call assert_equal('v', mode(1))
723 call feedkeys("\<Esc>V", 'xt')
724 call assert_equal('V', mode())
725 call assert_equal('V', mode(1))
726 call feedkeys("\<Esc>\<C-V>", 'xt')
727 call assert_equal("\<C-V>", mode())
728 call assert_equal("\<C-V>", mode(1))
729 call feedkeys("\<Esc>", 'xt')
730
731 call feedkeys("gh", 'xt')
732 call assert_equal('s', mode())
733 call assert_equal('s', mode(1))
734 call feedkeys("\<Esc>gH", 'xt')
735 call assert_equal('S', mode())
736 call assert_equal('S', mode(1))
737 call feedkeys("\<Esc>g\<C-H>", 'xt')
738 call assert_equal("\<C-S>", mode())
739 call assert_equal("\<C-S>", mode(1))
740 call feedkeys("\<Esc>", 'xt')
741
742 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
743 call assert_equal('c-c', g:current_modes)
744 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
745 call assert_equal('c-cv', g:current_modes)
746 " How to test Ex mode?
747
748 bwipe!
749 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100750 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100751endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100752
Bram Moolenaard2007022019-08-27 21:56:06 +0200753func Test_append()
754 enew!
755 split
756 call append(0, ["foo"])
757 split
758 only
759 undo
760endfunc
761
Bram Moolenaar79518e22017-02-17 16:31:35 +0100762func Test_getbufvar()
763 let bnr = bufnr('%')
764 let b:var_num = '1234'
765 let def_num = '5678'
766 call assert_equal('1234', getbufvar(bnr, 'var_num'))
767 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
768
769 let bd = getbufvar(bnr, '')
770 call assert_equal('1234', bd['var_num'])
771 call assert_true(exists("bd['changedtick']"))
772 call assert_equal(2, len(bd))
773
774 let bd2 = getbufvar(bnr, '', def_num)
775 call assert_equal(bd, bd2)
776
777 unlet b:var_num
778 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
779 call assert_equal('', getbufvar(bnr, 'var_num'))
780
781 let bd = getbufvar(bnr, '')
782 call assert_equal(1, len(bd))
783 let bd = getbufvar(bnr, '',def_num)
784 call assert_equal(1, len(bd))
785
Bram Moolenaar4520d442017-03-19 16:09:46 +0100786 call assert_equal('', getbufvar(9999, ''))
787 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100788 unlet def_num
789
Bram Moolenaar507647d2017-02-17 16:43:49 +0100790 call assert_equal(0, getbufvar(bnr, '&autoindent'))
791 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100792
793 " Open new window with forced option values
794 set fileformats=unix,dos
795 new ++ff=dos ++bin ++enc=iso-8859-2
796 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
797 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
798 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
799 close
800
801 set fileformats&
802endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100803
Bram Moolenaar41042f32017-03-09 12:09:32 +0100804func Test_last_buffer_nr()
805 call assert_equal(bufnr('$'), last_buffer_nr())
806endfunc
807
808func Test_stridx()
809 call assert_equal(-1, stridx('', 'l'))
810 call assert_equal(0, stridx('', ''))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200811 call assert_equal(0, 'hello'->stridx(''))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100812 call assert_equal(-1, stridx('hello', 'L'))
813 call assert_equal(2, stridx('hello', 'l', -1))
814 call assert_equal(2, stridx('hello', 'l', 0))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200815 call assert_equal(2, 'hello'->stridx('l', 1))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100816 call assert_equal(3, stridx('hello', 'l', 3))
817 call assert_equal(-1, stridx('hello', 'l', 4))
818 call assert_equal(-1, stridx('hello', 'l', 10))
819 call assert_equal(2, stridx('hello', 'll'))
820 call assert_equal(-1, stridx('hello', 'hello world'))
821endfunc
822
823func Test_strridx()
824 call assert_equal(-1, strridx('', 'l'))
825 call assert_equal(0, strridx('', ''))
826 call assert_equal(5, strridx('hello', ''))
827 call assert_equal(-1, strridx('hello', 'L'))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200828 call assert_equal(3, 'hello'->strridx('l'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100829 call assert_equal(3, strridx('hello', 'l', 10))
830 call assert_equal(3, strridx('hello', 'l', 3))
831 call assert_equal(2, strridx('hello', 'l', 2))
832 call assert_equal(-1, strridx('hello', 'l', 1))
833 call assert_equal(-1, strridx('hello', 'l', 0))
834 call assert_equal(-1, strridx('hello', 'l', -1))
835 call assert_equal(2, strridx('hello', 'll'))
836 call assert_equal(-1, strridx('hello', 'hello world'))
837endfunc
838
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200839func Test_match_func()
840 call assert_equal(4, match('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200841 call assert_equal(4, 'testing'->match('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200842 call assert_equal(-1, match('testing', 'ing', 5))
843 call assert_equal(-1, match('testing', 'ing', 8))
844 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
845 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
846endfunc
847
Bram Moolenaar41042f32017-03-09 12:09:32 +0100848func Test_matchend()
849 call assert_equal(7, matchend('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200850 call assert_equal(7, 'testing'->matchend('ing', 2))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100851 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200852 call assert_equal(-1, matchend('testing', 'ing', 8))
853 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
854 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
855endfunc
856
857func Test_matchlist()
858 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200859 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200860 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
861endfunc
862
863func Test_matchstr()
864 call assert_equal('ing', matchstr('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200865 call assert_equal('ing', 'testing'->matchstr('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200866 call assert_equal('', matchstr('testing', 'ing', 5))
867 call assert_equal('', matchstr('testing', 'ing', 8))
868 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
869 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
870endfunc
871
872func Test_matchstrpos()
873 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200874 call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200875 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
876 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
877 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
878 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100879endfunc
880
881func Test_nextnonblank_prevnonblank()
882 new
883insert
884This
885
886
887is
888
889a
890Test
891.
892 call assert_equal(0, nextnonblank(-1))
893 call assert_equal(0, nextnonblank(0))
894 call assert_equal(1, nextnonblank(1))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200895 call assert_equal(4, 2->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100896 call assert_equal(4, nextnonblank(3))
897 call assert_equal(4, nextnonblank(4))
898 call assert_equal(6, nextnonblank(5))
899 call assert_equal(6, nextnonblank(6))
900 call assert_equal(7, nextnonblank(7))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200901 call assert_equal(0, 8->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100902
903 call assert_equal(0, prevnonblank(-1))
904 call assert_equal(0, prevnonblank(0))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200905 call assert_equal(1, 1->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100906 call assert_equal(1, prevnonblank(2))
907 call assert_equal(1, prevnonblank(3))
908 call assert_equal(4, prevnonblank(4))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200909 call assert_equal(4, 5->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100910 call assert_equal(6, prevnonblank(6))
911 call assert_equal(7, prevnonblank(7))
912 call assert_equal(0, prevnonblank(8))
913 bw!
914endfunc
915
916func Test_byte2line_line2byte()
917 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200918 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +0100919 call setline(1, ['a', 'bc', 'd'])
920
921 set fileformat=unix
922 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
923 \ map(range(-1, 8), 'byte2line(v:val)'))
924 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
925 \ map(range(-1, 5), 'line2byte(v:val)'))
926
927 set fileformat=mac
928 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
Bram Moolenaar64b4d732019-08-22 22:18:17 +0200929 \ map(range(-1, 8), 'v:val->byte2line()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100930 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
Bram Moolenaar02b31112019-08-31 22:16:38 +0200931 \ map(range(-1, 5), 'v:val->line2byte()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100932
933 set fileformat=dos
934 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
935 \ map(range(-1, 11), 'byte2line(v:val)'))
936 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
937 \ map(range(-1, 5), 'line2byte(v:val)'))
938
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200939 bw!
940 set noendofline nofixendofline
941 normal a-
942 for ff in ["unix", "mac", "dos"]
943 let &fileformat = ff
944 call assert_equal(1, line2byte(1))
945 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
946 endfor
947
948 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +0100949 bw!
950endfunc
951
Bram Moolenaar64b4d732019-08-22 22:18:17 +0200952func Test_byteidx()
953 let a = '.é.' " one char of two bytes
954 call assert_equal(0, byteidx(a, 0))
955 call assert_equal(0, byteidxcomp(a, 0))
956 call assert_equal(1, byteidx(a, 1))
957 call assert_equal(1, byteidxcomp(a, 1))
958 call assert_equal(3, byteidx(a, 2))
959 call assert_equal(3, byteidxcomp(a, 2))
960 call assert_equal(4, byteidx(a, 3))
961 call assert_equal(4, byteidxcomp(a, 3))
962 call assert_equal(-1, byteidx(a, 4))
963 call assert_equal(-1, byteidxcomp(a, 4))
964
965 let b = '.é.' " normal e with composing char
966 call assert_equal(0, b->byteidx(0))
967 call assert_equal(1, b->byteidx(1))
968 call assert_equal(4, b->byteidx(2))
969 call assert_equal(5, b->byteidx(3))
970 call assert_equal(-1, b->byteidx(4))
971
972 call assert_equal(0, b->byteidxcomp(0))
973 call assert_equal(1, b->byteidxcomp(1))
974 call assert_equal(2, b->byteidxcomp(2))
975 call assert_equal(4, b->byteidxcomp(3))
976 call assert_equal(5, b->byteidxcomp(4))
977 call assert_equal(-1, b->byteidxcomp(5))
978endfunc
979
Bram Moolenaar41042f32017-03-09 12:09:32 +0100980func Test_count()
981 let l = ['a', 'a', 'A', 'b']
982 call assert_equal(2, count(l, 'a'))
983 call assert_equal(1, count(l, 'A'))
984 call assert_equal(1, count(l, 'b'))
985 call assert_equal(0, count(l, 'B'))
986
987 call assert_equal(2, count(l, 'a', 0))
988 call assert_equal(1, count(l, 'A', 0))
989 call assert_equal(1, count(l, 'b', 0))
990 call assert_equal(0, count(l, 'B', 0))
991
992 call assert_equal(3, count(l, 'a', 1))
993 call assert_equal(3, count(l, 'A', 1))
994 call assert_equal(1, count(l, 'b', 1))
995 call assert_equal(1, count(l, 'B', 1))
996 call assert_equal(0, count(l, 'c', 1))
997
998 call assert_equal(1, count(l, 'a', 0, 1))
999 call assert_equal(2, count(l, 'a', 1, 1))
1000 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02001001 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001002
1003 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
1004 call assert_equal(2, count(d, 'a'))
1005 call assert_equal(1, count(d, 'A'))
1006 call assert_equal(1, count(d, 'b'))
1007 call assert_equal(0, count(d, 'B'))
1008
1009 call assert_equal(2, count(d, 'a', 0))
1010 call assert_equal(1, count(d, 'A', 0))
1011 call assert_equal(1, count(d, 'b', 0))
1012 call assert_equal(0, count(d, 'B', 0))
1013
1014 call assert_equal(3, count(d, 'a', 1))
1015 call assert_equal(3, count(d, 'A', 1))
1016 call assert_equal(1, count(d, 'b', 1))
1017 call assert_equal(1, count(d, 'B', 1))
1018 call assert_equal(0, count(d, 'c', 1))
1019
1020 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +02001021
1022 call assert_equal(0, count("foo", "bar"))
1023 call assert_equal(1, count("foo", "oo"))
1024 call assert_equal(2, count("foo", "o"))
1025 call assert_equal(0, count("foo", "O"))
1026 call assert_equal(2, count("foo", "O", 1))
1027 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +01001028 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001029
1030 call assert_fails('call count(0, 0)', 'E712:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001031endfunc
1032
1033func Test_changenr()
1034 new Xchangenr
1035 call assert_equal(0, changenr())
1036 norm ifoo
1037 call assert_equal(1, changenr())
1038 set undolevels=10
1039 norm Sbar
1040 call assert_equal(2, changenr())
1041 undo
1042 call assert_equal(1, changenr())
1043 redo
1044 call assert_equal(2, changenr())
1045 bw!
1046 set undolevels&
1047endfunc
1048
1049func Test_filewritable()
1050 new Xfilewritable
1051 write!
1052 call assert_equal(1, filewritable('Xfilewritable'))
1053
1054 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1055 call assert_equal(0, filewritable('Xfilewritable'))
1056
1057 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001058 call assert_equal(1, 'Xfilewritable'->filewritable())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001059
1060 call assert_equal(0, filewritable('doesnotexist'))
1061
1062 call delete('Xfilewritable')
1063 bw!
1064endfunc
1065
Bram Moolenaar82956662018-10-06 15:18:45 +02001066func Test_Executable()
1067 if has('win32')
1068 call assert_equal(1, executable('notepad'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001069 call assert_equal(1, 'notepad.exe'->executable())
Bram Moolenaar82956662018-10-06 15:18:45 +02001070 call assert_equal(0, executable('notepad.exe.exe'))
1071 call assert_equal(0, executable('shell32.dll'))
1072 call assert_equal(0, executable('win.ini'))
1073 elseif has('unix')
Bram Moolenaara4208962019-08-24 20:50:19 +02001074 call assert_equal(1, 'cat'->executable())
Bram Moolenaara05a0d32018-10-07 18:43:05 +02001075 call assert_equal(0, executable('nodogshere'))
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001076
1077 " get "cat" path and remove the leading /
1078 let catcmd = exepath('cat')[1:]
1079 new
Bram Moolenaara4208962019-08-24 20:50:19 +02001080 " check that the relative path works in /
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001081 lcd /
1082 call assert_equal(1, executable(catcmd))
Bram Moolenaara4208962019-08-24 20:50:19 +02001083 call assert_equal('/' .. catcmd, catcmd->exepath())
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001084 bwipe
Bram Moolenaar82956662018-10-06 15:18:45 +02001085 endif
1086endfunc
1087
Bram Moolenaar86621892019-03-30 21:51:28 +01001088func Test_executable_longname()
1089 if !has('win32')
1090 return
1091 endif
1092
1093 let fname = 'X' . repeat('あ', 200) . '.bat'
1094 call writefile([], fname)
1095 call assert_equal(1, executable(fname))
1096 call delete(fname)
1097endfunc
1098
Bram Moolenaar41042f32017-03-09 12:09:32 +01001099func Test_hostname()
1100 let hostname_vim = hostname()
1101 if has('unix')
1102 let hostname_system = systemlist('uname -n')[0]
1103 call assert_equal(hostname_vim, hostname_system)
1104 endif
1105endfunc
1106
1107func Test_getpid()
1108 " getpid() always returns the same value within a vim instance.
1109 call assert_equal(getpid(), getpid())
1110 if has('unix')
1111 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1112 endif
1113endfunc
1114
1115func Test_hlexists()
1116 call assert_equal(0, hlexists('does_not_exist'))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001117 call assert_equal(0, 'Number'->hlexists())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001118 call assert_equal(0, highlight_exists('does_not_exist'))
1119 call assert_equal(0, highlight_exists('Number'))
1120 syntax on
1121 call assert_equal(0, hlexists('does_not_exist'))
1122 call assert_equal(1, hlexists('Number'))
1123 call assert_equal(0, highlight_exists('does_not_exist'))
1124 call assert_equal(1, highlight_exists('Number'))
1125 syntax off
1126endfunc
1127
1128func Test_col()
1129 new
1130 call setline(1, 'abcdef')
1131 norm gg4|mx6|mY2|
1132 call assert_equal(2, col('.'))
1133 call assert_equal(7, col('$'))
1134 call assert_equal(4, col("'x"))
1135 call assert_equal(6, col("'Y"))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001136 call assert_equal(2, [1, 2]->col())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001137 call assert_equal(7, col([1, '$']))
1138
1139 call assert_equal(0, col(''))
1140 call assert_equal(0, col('x'))
1141 call assert_equal(0, col([2, '$']))
1142 call assert_equal(0, col([1, 100]))
1143 call assert_equal(0, col([1]))
1144 bw!
1145endfunc
1146
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001147" Test for input()
1148func Test_input_func()
1149 " Test for prompt with multiple lines
1150 redir => v
1151 call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt')
1152 redir END
1153 call assert_equal("B", c)
1154 call assert_equal(['A', 'B', 'C'], split(v, "\n"))
1155
1156 " Test for default value
1157 call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt')
1158 call assert_equal('red', c)
1159
1160 " Test for completion at the input prompt
1161 func! Tcomplete(arglead, cmdline, pos)
1162 return "item1\nitem2\nitem3"
1163 endfunc
1164 call feedkeys(":let c = input('Q? ', '' , 'custom,Tcomplete')\<CR>"
1165 \ .. "\<C-A>\<CR>", 'xt')
1166 delfunc Tcomplete
1167 call assert_equal('item1 item2 item3', c)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001168
1169 call assert_fails("call input('F:', '', 'invalid')", 'E180:')
1170 call assert_fails("call input('F:', '', [])", 'E730:')
1171endfunc
1172
1173" Test for the inputdialog() function
1174func Test_inputdialog()
1175 CheckNotGui
1176
1177 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt')
1178 call assert_equal('xx', v)
1179 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt')
1180 call assert_equal('yy', v)
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001181endfunc
1182
1183" Test for inputlist()
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001184func Test_inputlist()
1185 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1186 call assert_equal(1, c)
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001187 call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001188 call assert_equal(2, c)
1189 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1190 call assert_equal(3, c)
1191
1192 call assert_fails('call inputlist("")', 'E686:')
1193endfunc
1194
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001195func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001196 if has('balloon_eval')
1197 " This won't do anything but must not crash either.
1198 call balloon_show('hi!')
Bram Moolenaar7d8ea0b2020-01-27 23:01:30 +01001199 if !has('gui_running')
1200 call balloon_show(range(3))
1201 endif
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001202 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001203endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001204
1205func Test_setbufvar_options()
1206 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1207 " window layout.
1208 call assert_equal(1, winnr('$'))
1209 split dummy_preview
1210 resize 2
1211 set winfixheight winfixwidth
1212 let prev_id = win_getid()
1213
1214 wincmd j
1215 let wh = winheight('.')
1216 let dummy_buf = bufnr('dummy_buf1', v:true)
1217 call setbufvar(dummy_buf, '&buftype', 'nofile')
1218 execute 'belowright vertical split #' . dummy_buf
1219 call assert_equal(wh, winheight('.'))
1220 let dum1_id = win_getid()
1221
1222 wincmd h
1223 let wh = winheight('.')
1224 let dummy_buf = bufnr('dummy_buf2', v:true)
Bram Moolenaar196b4662019-09-06 21:34:30 +02001225 eval 'nofile'->setbufvar(dummy_buf, '&buftype')
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001226 execute 'belowright vertical split #' . dummy_buf
1227 call assert_equal(wh, winheight('.'))
1228
1229 bwipe!
1230 call win_gotoid(prev_id)
1231 bwipe!
1232 call win_gotoid(dum1_id)
1233 bwipe!
1234endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001235
1236func Test_redo_in_nested_functions()
1237 nnoremap g. :set opfunc=Operator<CR>g@
1238 function Operator( type, ... )
1239 let @x = 'XXX'
1240 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1241 endfunction
1242
1243 function! Apply()
1244 5,6normal! .
1245 endfunction
1246
1247 new
1248 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1249 1normal g.i"
1250 call assert_equal('some "XXX" text', getline(1))
1251 3,4normal .
1252 call assert_equal('some "XXX" text', getline(3))
1253 call assert_equal('more "XXX" text', getline(4))
1254 call Apply()
1255 call assert_equal('some "XXX" text', getline(5))
1256 call assert_equal('more "XXX" text', getline(6))
1257 bwipe!
1258
1259 nunmap g.
1260 delfunc Operator
1261 delfunc Apply
1262endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02001263
1264func Test_shellescape()
1265 let save_shell = &shell
1266 set shell=bash
1267 call assert_equal("'text'", shellescape('text'))
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001268 call assert_equal("'te\"xt'", 'te"xt'->shellescape())
Bram Moolenaar20615522017-06-05 18:46:26 +02001269 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
1270
1271 call assert_equal("'te%xt'", shellescape("te%xt"))
1272 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
1273 call assert_equal("'te#xt'", shellescape("te#xt"))
1274 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
1275 call assert_equal("'te!xt'", shellescape("te!xt"))
1276 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1277
1278 call assert_equal("'te\nxt'", shellescape("te\nxt"))
1279 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1280 set shell=tcsh
1281 call assert_equal("'te\\!xt'", shellescape("te!xt"))
1282 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1283 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1284 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1285
1286 let &shell = save_shell
1287endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001288
1289func Test_trim()
1290 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001291 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 +01001292 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1293 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
1294 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
1295 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
1296 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
1297 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1298 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1299 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
1300 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
1301 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
1302 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
1303 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1304 call assert_equal("", trim("", ""))
1305 call assert_equal("a", trim("a", ""))
1306 call assert_equal("", trim("", "a"))
1307
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001308 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001309 call assert_equal("x", trim(chars . "x" . chars))
1310endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001311
1312" Test for reg_recording() and reg_executing()
1313func Test_reg_executing_and_recording()
1314 let s:reg_stat = ''
1315 func s:save_reg_stat()
1316 let s:reg_stat = reg_recording() . ':' . reg_executing()
1317 return ''
1318 endfunc
1319
1320 new
1321 call s:save_reg_stat()
1322 call assert_equal(':', s:reg_stat)
1323 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1324 call assert_equal('a:', s:reg_stat)
1325 call feedkeys("@a", 'xt')
1326 call assert_equal(':a', s:reg_stat)
1327 call feedkeys("qb@aq", 'xt')
1328 call assert_equal('b:a', s:reg_stat)
1329 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1330 call assert_equal('":', s:reg_stat)
1331
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001332 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001333 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001334 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1335 func TestFunc() abort
1336 normal! ia
1337 endfunc
1338 call feedkeys("@q", 'xt')
1339 call assert_equal(':q', s:reg_stat)
1340 delfunc TestFunc
1341
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001342 " getchar() command saves and restores reg_executing
1343 map W :call TestFunc()<CR>
1344 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001345 let g:typed = ''
1346 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001347 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001348 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001349 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001350 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001351 endfunc
1352 call feedkeys("@qy", 'xt')
1353 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001354 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001355 delfunc TestFunc
1356 unmap W
1357 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001358 unlet g:regs
1359
1360 " input() command saves and restores reg_executing
1361 map W :call TestFunc()<CR>
1362 let @q = "W"
1363 let g:typed = ''
1364 let g:regs = []
1365 func TestFunc() abort
1366 let g:regs += [reg_executing()]
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001367 let g:typed = '?'->input()
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001368 let g:regs += [reg_executing()]
1369 endfunc
1370 call feedkeys("@qy\<CR>", 'xt')
1371 call assert_equal("y", g:typed)
1372 call assert_equal(['q', 'q'], g:regs)
1373 delfunc TestFunc
1374 unmap W
1375 unlet g:typed
1376 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001377
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001378 bwipe!
1379 delfunc s:save_reg_stat
1380 unlet s:reg_stat
1381endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001382
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001383func Test_inputsecret()
1384 map W :call TestFunc()<CR>
1385 let @q = "W"
1386 let g:typed1 = ''
1387 let g:typed2 = ''
1388 let g:regs = []
1389 func TestFunc() abort
1390 let g:typed1 = '?'->inputsecret()
1391 let g:typed2 = inputsecret('password: ')
1392 endfunc
1393 call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1394 call assert_equal("something", g:typed1)
1395 call assert_equal("else", g:typed2)
1396 delfunc TestFunc
1397 unmap W
1398 unlet g:typed1
1399 unlet g:typed2
1400endfunc
1401
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001402func Test_getchar()
1403 call feedkeys('a', '')
1404 call assert_equal(char2nr('a'), getchar())
1405
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001406 call setline(1, 'xxxx')
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001407 call test_setmouse(1, 3)
1408 let v:mouse_win = 9
1409 let v:mouse_winid = 9
1410 let v:mouse_lnum = 9
1411 let v:mouse_col = 9
1412 call feedkeys("\<S-LeftMouse>", '')
1413 call assert_equal("\<S-LeftMouse>", getchar())
1414 call assert_equal(1, v:mouse_win)
1415 call assert_equal(win_getid(1), v:mouse_winid)
1416 call assert_equal(1, v:mouse_lnum)
1417 call assert_equal(3, v:mouse_col)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001418 enew!
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001419endfunc
1420
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001421func Test_libcall_libcallnr()
1422 if !has('libcall')
1423 return
1424 endif
1425
1426 if has('win32')
1427 let libc = 'msvcrt.dll'
1428 elseif has('mac')
1429 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001430 elseif executable('ldd')
1431 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1432 endif
1433 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001434 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001435 if has('linux')
1436 " There is not documented but regarding the 1st argument of glibc's
1437 " dlopen an empty string and nullptr are equivalent, so using an empty
1438 " string for the 1st argument of libcall allows to call functions.
1439 let libc = ''
1440 elseif has('sun')
1441 " Set the path to libc.so according to the architecture.
1442 let test_bits = system('file ' . GetVimProg())
1443 let test_arch = system('uname -p')
1444 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1445 let libc = '/usr/lib/sparcv9/libc.so'
1446 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1447 let libc = '/usr/lib/amd64/libc.so'
1448 else
1449 let libc = '/usr/lib/libc.so'
1450 endif
1451 else
1452 " Unfortunately skip this test until a good way is found.
1453 return
1454 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001455 endif
1456
1457 if has('win32')
Bram Moolenaar02b31112019-08-31 22:16:38 +02001458 call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001459 else
Bram Moolenaar02b31112019-08-31 22:16:38 +02001460 call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001461 endif
1462
1463 " If function returns NULL, libcall() should return an empty string.
1464 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1465
1466 " Test libcallnr() with string and integer argument.
Bram Moolenaar02b31112019-08-31 22:16:38 +02001467 call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1468 call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001469
1470 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1471 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1472
1473 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1474 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1475endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001476
1477sandbox function Fsandbox()
1478 normal ix
1479endfunc
1480
1481func Test_func_sandbox()
1482 sandbox let F = {-> 'hello'}
1483 call assert_equal('hello', F())
1484
Bram Moolenaara4208962019-08-24 20:50:19 +02001485 sandbox let F = {-> "normal ix\<Esc>"->execute()}
Bram Moolenaard90a1442018-07-15 20:24:31 +02001486 call assert_fails('call F()', 'E48:')
1487 unlet F
1488
1489 call assert_fails('call Fsandbox()', 'E48:')
1490 delfunc Fsandbox
1491endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001492
1493func EditAnotherFile()
1494 let word = expand('<cword>')
1495 edit Xfuncrange2
1496endfunc
1497
1498func Test_func_range_with_edit()
1499 " Define a function that edits another buffer, then call it with a range that
1500 " is invalid in that buffer.
1501 call writefile(['just one line'], 'Xfuncrange2')
1502 new
Bram Moolenaar196b4662019-09-06 21:34:30 +02001503 eval 10->range()->setline(1)
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001504 write Xfuncrange1
1505 call assert_fails('5,8call EditAnotherFile()', 'E16:')
1506
1507 call delete('Xfuncrange1')
1508 call delete('Xfuncrange2')
1509 bwipe!
1510endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001511
1512func Test_func_exists_on_reload()
1513 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1514 call assert_equal(0, exists('*ExistingFunction'))
1515 source Xfuncexists
Bram Moolenaara4208962019-08-24 20:50:19 +02001516 call assert_equal(1, '*ExistingFunction'->exists())
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001517 " Redefining a function when reloading a script is OK.
1518 source Xfuncexists
1519 call assert_equal(1, exists('*ExistingFunction'))
1520
1521 " But redefining in another script is not OK.
1522 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1523 call assert_fails('source Xfuncexists2', 'E122:')
1524
1525 delfunc ExistingFunction
1526 call assert_equal(0, exists('*ExistingFunction'))
1527 call writefile([
1528 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1529 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1530 \ ], 'Xfuncexists')
1531 call assert_fails('source Xfuncexists', 'E122:')
1532 call assert_equal(1, exists('*ExistingFunction'))
1533
1534 call delete('Xfuncexists2')
1535 call delete('Xfuncexists')
1536 delfunc ExistingFunction
1537endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01001538
1539" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1540func Test_confirm()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001541 CheckUnix
1542 CheckNotGui
Bram Moolenaar2e050092019-01-27 15:00:36 +01001543
1544 call feedkeys('o', 'L')
1545 let a = confirm('Press O to proceed')
1546 call assert_equal(1, a)
1547
1548 call feedkeys('y', 'L')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001549 let a = 'Are you sure?'->confirm("&Yes\n&No")
Bram Moolenaar2e050092019-01-27 15:00:36 +01001550 call assert_equal(1, a)
1551
1552 call feedkeys('n', 'L')
1553 let a = confirm('Are you sure?', "&Yes\n&No")
1554 call assert_equal(2, a)
1555
1556 " confirm() should return 0 when pressing CTRL-C.
1557 call feedkeys("\<C-c>", 'L')
1558 let a = confirm('Are you sure?', "&Yes\n&No")
1559 call assert_equal(0, a)
1560
1561 " <Esc> requires another character to avoid it being seen as the start of an
1562 " escape sequence. Zero should be harmless.
Bram Moolenaara4208962019-08-24 20:50:19 +02001563 eval "\<Esc>0"->feedkeys('L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01001564 let a = confirm('Are you sure?', "&Yes\n&No")
1565 call assert_equal(0, a)
1566
1567 " Default choice is returned when pressing <CR>.
1568 call feedkeys("\<CR>", 'L')
1569 let a = confirm('Are you sure?', "&Yes\n&No")
1570 call assert_equal(1, a)
1571
1572 call feedkeys("\<CR>", 'L')
1573 let a = confirm('Are you sure?', "&Yes\n&No", 2)
1574 call assert_equal(2, a)
1575
1576 call feedkeys("\<CR>", 'L')
1577 let a = confirm('Are you sure?', "&Yes\n&No", 0)
1578 call assert_equal(0, a)
1579
1580 " Test with the {type} 4th argument
1581 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
1582 call feedkeys('y', 'L')
1583 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
1584 call assert_equal(1, a)
1585 endfor
1586
1587 call assert_fails('call confirm([])', 'E730:')
1588 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
1589 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
1590 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
1591endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001592
1593func Test_platform_name()
1594 " The system matches at most only one name.
1595 let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
1596 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
1597
1598 " Is Unix?
1599 call assert_equal(has('beos'), has('beos') && has('unix'))
1600 call assert_equal(has('bsd'), has('bsd') && has('unix'))
1601 call assert_equal(has('hpux'), has('hpux') && has('unix'))
1602 call assert_equal(has('linux'), has('linux') && has('unix'))
1603 call assert_equal(has('mac'), has('mac') && has('unix'))
1604 call assert_equal(has('qnx'), has('qnx') && has('unix'))
1605 call assert_equal(has('sun'), has('sun') && has('unix'))
1606 call assert_equal(has('win32'), has('win32') && !has('unix'))
1607 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
1608
1609 if has('unix') && executable('uname')
1610 let uname = system('uname')
1611 call assert_equal(uname =~? 'BeOS', has('beos'))
Bram Moolenaara02e3f62019-02-07 21:27:14 +01001612 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
1613 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001614 call assert_equal(uname =~? 'HP-UX', has('hpux'))
1615 call assert_equal(uname =~? 'Linux', has('linux'))
1616 call assert_equal(uname =~? 'Darwin', has('mac'))
1617 call assert_equal(uname =~? 'QNX', has('qnx'))
1618 call assert_equal(uname =~? 'SunOS', has('sun'))
1619 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
1620 endif
1621endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001622
1623func Test_readdir()
1624 call mkdir('Xdir')
1625 call writefile([], 'Xdir/foo.txt')
1626 call writefile([], 'Xdir/bar.txt')
1627 call mkdir('Xdir/dir')
1628
1629 " All results
1630 let files = readdir('Xdir')
1631 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
1632
1633 " Only results containing "f"
Bram Moolenaara0d1fef2019-09-04 22:29:14 +02001634 let files = 'Xdir'->readdir({ x -> stridx(x, 'f') !=- 1 })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001635 call assert_equal(['foo.txt'], sort(files))
1636
1637 " Only .txt files
1638 let files = readdir('Xdir', { x -> x =~ '.txt$' })
1639 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1640
1641 " Only .txt files with string
1642 let files = readdir('Xdir', 'v:val =~ ".txt$"')
1643 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1644
1645 " Limit to 1 result.
1646 let l = []
1647 let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
1648 call assert_equal(1, len(files))
1649
Bram Moolenaar27da7de2019-09-03 17:13:37 +02001650 " Nested readdir() must not crash
1651 let files = readdir('Xdir', 'readdir("Xdir", "1") != []')
1652 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
1653
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001654 eval 'Xdir'->delete('rf')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001655endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02001656
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001657func Test_delete_rf()
1658 call mkdir('Xdir')
1659 call writefile([], 'Xdir/foo.txt')
1660 call writefile([], 'Xdir/bar.txt')
1661 call mkdir('Xdir/[a-1]') " issue #696
1662 call writefile([], 'Xdir/[a-1]/foo.txt')
1663 call writefile([], 'Xdir/[a-1]/bar.txt')
1664 call assert_true(filereadable('Xdir/foo.txt'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001665 call assert_true('Xdir/[a-1]/foo.txt'->filereadable())
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001666
1667 call assert_equal(0, delete('Xdir', 'rf'))
1668 call assert_false(filereadable('Xdir/foo.txt'))
1669 call assert_false(filereadable('Xdir/[a-1]/foo.txt'))
1670endfunc
1671
Bram Moolenaar17aca702019-05-16 22:24:55 +02001672func Test_call()
1673 call assert_equal(3, call('len', [123]))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001674 call assert_equal(3, 'len'->call([123]))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001675 call assert_fails("call call('len', 123)", 'E714:')
1676 call assert_equal(0, call('', []))
1677
1678 function Mylen() dict
1679 return len(self.data)
1680 endfunction
1681 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001682 eval mydict.len->call([], mydict)->assert_equal(4)
Bram Moolenaar17aca702019-05-16 22:24:55 +02001683 call assert_fails("call call('Mylen', [], 0)", 'E715:')
1684endfunc
1685
1686func Test_char2nr()
1687 call assert_equal(12354, char2nr('あ', 1))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001688 call assert_equal(120, 'x'->char2nr())
Bram Moolenaar17aca702019-05-16 22:24:55 +02001689endfunc
1690
1691func Test_eventhandler()
1692 call assert_equal(0, eventhandler())
1693endfunc
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001694
1695func Test_bufadd_bufload()
1696 call assert_equal(0, bufexists('someName'))
1697 let buf = bufadd('someName')
1698 call assert_notequal(0, buf)
1699 call assert_equal(1, bufexists('someName'))
1700 call assert_equal(0, getbufvar(buf, '&buflisted'))
1701 call assert_equal(0, bufloaded(buf))
1702 call bufload(buf)
1703 call assert_equal(1, bufloaded(buf))
1704 call assert_equal([''], getbufline(buf, 1, '$'))
1705
1706 let curbuf = bufnr('')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001707 eval ['some', 'text']->writefile('XotherName')
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001708 let buf = 'XotherName'->bufadd()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001709 call assert_notequal(0, buf)
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001710 eval 'XotherName'->bufexists()->assert_equal(1)
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001711 call assert_equal(0, getbufvar(buf, '&buflisted'))
1712 call assert_equal(0, bufloaded(buf))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001713 eval buf->bufload()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001714 call assert_equal(1, bufloaded(buf))
1715 call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
1716 call assert_equal(curbuf, bufnr(''))
1717
Bram Moolenaar892ae722019-06-30 20:33:01 +02001718 let buf1 = bufadd('')
1719 let buf2 = bufadd('')
1720 call assert_notequal(0, buf1)
1721 call assert_notequal(0, buf2)
1722 call assert_notequal(buf1, buf2)
1723 call assert_equal(1, bufexists(buf1))
1724 call assert_equal(1, bufexists(buf2))
1725 call assert_equal(0, bufloaded(buf1))
1726 exe 'bwipe ' .. buf1
1727 call assert_equal(0, bufexists(buf1))
1728 call assert_equal(1, bufexists(buf2))
1729 exe 'bwipe ' .. buf2
1730 call assert_equal(0, bufexists(buf2))
1731
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001732 bwipe someName
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001733 bwipe XotherName
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001734 call assert_equal(0, bufexists('someName'))
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001735 call delete('XotherName')
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001736endfunc
Bram Moolenaarc2585492019-09-22 21:29:53 +02001737
1738func Test_state()
1739 CheckRunVimInTerminal
1740
1741 let lines =<< trim END
1742 call setline(1, ['one', 'two', 'three'])
1743 map ;; gg
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001744 set complete=.
Bram Moolenaarc2585492019-09-22 21:29:53 +02001745 func RunTimer()
1746 call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
1747 endfunc
1748 au Filetype foobar let g:state = state()|let g:mode = mode()
1749 END
1750 call writefile(lines, 'XState')
1751 let buf = RunVimInTerminal('-S XState', #{rows: 6})
1752
1753 " Using a ":" command Vim is busy, thus "S" is returned
1754 call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
1755 call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
1756 call term_sendkeys(buf, ":\<CR>")
1757
1758 " Using a timer callback
1759 call term_sendkeys(buf, ":call RunTimer()\<CR>")
1760 call term_wait(buf, 50)
1761 let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"
1762 call term_sendkeys(buf, getstate)
1763 call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)
1764
1765 " Halfway a mapping
1766 call term_sendkeys(buf, ":call RunTimer()\<CR>;")
1767 call term_wait(buf, 50)
1768 call term_sendkeys(buf, ";")
1769 call term_sendkeys(buf, getstate)
1770 call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)
1771
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001772 " Insert mode completion (bit slower on Mac)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001773 call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001774 call term_wait(buf, 200)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001775 call term_sendkeys(buf, "\<Esc>")
1776 call term_sendkeys(buf, getstate)
1777 call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)
1778
1779 " Autocommand executing
1780 call term_sendkeys(buf, ":set filetype=foobar\<CR>")
1781 call term_wait(buf, 50)
1782 call term_sendkeys(buf, getstate)
1783 call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)
1784
1785 " Todo: "w" - waiting for ch_evalexpr()
1786
1787 " messages scrolled
1788 call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
1789 call term_wait(buf, 50)
1790 call term_sendkeys(buf, "\<CR>")
1791 call term_sendkeys(buf, getstate)
1792 call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)
1793
1794 call StopVimInTerminal(buf)
1795 call delete('XState')
1796endfunc
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001797
1798func Test_range()
1799 " destructuring
1800 let [x, y] = range(2)
1801 call assert_equal([0, 1], [x, y])
1802
1803 " index
1804 call assert_equal(4, range(1, 10)[3])
1805
1806 " add()
1807 call assert_equal([0, 1, 2, 3], add(range(3), 3))
1808 call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3)))
1809 call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3)))
1810
1811 " append()
1812 new
1813 call append('.', range(5))
1814 call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$'))
1815 bwipe!
1816
1817 " appendbufline()
1818 new
1819 call appendbufline(bufnr(''), '.', range(5))
1820 call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$'))
1821 bwipe!
1822
1823 " call()
1824 func TwoArgs(a, b)
1825 return [a:a, a:b]
1826 endfunc
1827 call assert_equal([0, 1], call('TwoArgs', range(2)))
1828
1829 " col()
1830 new
1831 call setline(1, ['foo', 'bar'])
1832 call assert_equal(2, col(range(1, 2)))
1833 bwipe!
1834
1835 " complete()
1836 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>"
1837 " complete_info()
1838 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>"
1839
1840 " copy()
1841 call assert_equal([1, 2, 3], copy(range(1, 3)))
1842
1843 " count()
1844 call assert_equal(0, count(range(0), 3))
1845 call assert_equal(0, count(range(2), 3))
1846 call assert_equal(1, count(range(5), 3))
1847
1848 " cursor()
1849 new
1850 call setline(1, ['aaa', 'bbb', 'ccc'])
1851 call cursor(range(1, 2))
1852 call assert_equal([2, 1], [col('.'), line('.')])
1853 bwipe!
1854
1855 " deepcopy()
1856 call assert_equal([1, 2, 3], deepcopy(range(1, 3)))
1857
1858 " empty()
1859 call assert_true(empty(range(0)))
1860 call assert_false(empty(range(2)))
1861
1862 " execute()
1863 new
1864 call setline(1, ['aaa', 'bbb', 'ccc'])
1865 call execute(range(3))
1866 call assert_equal(2, line('.'))
1867 bwipe!
1868
1869 " extend()
1870 call assert_equal([1, 2, 3, 4], extend([1], range(2, 4)))
1871 call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4)))
1872 call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4]))
1873
1874 " filter()
1875 call assert_equal([1, 3], filter(range(5), 'v:val % 2'))
1876
1877 " funcref()
1878 call assert_equal([0, 1], funcref('TwoArgs', range(2))())
1879
1880 " function()
1881 call assert_equal([0, 1], function('TwoArgs', range(2))())
1882
1883 " garbagecollect()
1884 let thelist = [1, range(2), 3]
1885 let otherlist = range(3)
1886 call test_garbagecollect_now()
1887
1888 " get()
1889 call assert_equal(4, get(range(1, 10), 3))
1890 call assert_equal(-1, get(range(1, 10), 42, -1))
1891
1892 " index()
1893 call assert_equal(1, index(range(1, 5), 2))
1894
1895 " inputlist()
Bram Moolenaar272ca952020-01-28 20:49:11 +01001896 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x')
1897 call assert_equal(1, result)
1898 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x')
1899 call assert_equal(1, result)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001900
1901 " insert()
1902 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42))
1903 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0))
1904 call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1))
1905 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4))
1906 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1))
1907 call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5))
1908
1909 " join()
1910 call assert_equal('0 1 2 3 4', join(range(5)))
1911
Bram Moolenaar272ca952020-01-28 20:49:11 +01001912 " json_encode()
1913 call assert_equal('[0,1,2,3]', json_encode(range(4)))
1914
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001915 " len()
1916 call assert_equal(0, len(range(0)))
1917 call assert_equal(2, len(range(2)))
1918 call assert_equal(5, len(range(0, 12, 3)))
1919 call assert_equal(4, len(range(3, 0, -1)))
1920
1921 " list2str()
1922 call assert_equal('ABC', list2str(range(65, 67)))
1923
1924 " lock()
1925 let thelist = range(5)
1926 lockvar thelist
1927
1928 " map()
1929 call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2'))
1930
1931 " match()
1932 call assert_equal(3, match(range(5), 3))
1933
1934 " matchaddpos()
1935 highlight MyGreenGroup ctermbg=green guibg=green
1936 call matchaddpos('MyGreenGroup', range(line('.'), line('.')))
1937
1938 " matchend()
1939 call assert_equal(4, matchend(range(5), '4'))
1940 call assert_equal(3, matchend(range(1, 5), '4'))
1941 call assert_equal(-1, matchend(range(1, 5), '42'))
1942
1943 " matchstrpos()
1944 call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4'))
1945 call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4'))
1946 call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42'))
1947
1948 " max() reverse()
1949 call assert_equal(0, max(range(0)))
1950 call assert_equal(0, max(range(10, 9)))
1951 call assert_equal(9, max(range(10)))
1952 call assert_equal(18, max(range(0, 20, 3)))
1953 call assert_equal(20, max(range(20, 0, -3)))
1954 call assert_equal(99999, max(range(100000)))
1955 call assert_equal(99999, max(range(99999, 0, -1)))
1956 call assert_equal(99999, max(reverse(range(100000))))
1957 call assert_equal(99999, max(reverse(range(99999, 0, -1))))
1958
1959 " min() reverse()
1960 call assert_equal(0, min(range(0)))
1961 call assert_equal(0, min(range(10, 9)))
1962 call assert_equal(5, min(range(5, 10)))
1963 call assert_equal(5, min(range(5, 10, 3)))
1964 call assert_equal(2, min(range(20, 0, -3)))
1965 call assert_equal(0, min(range(100000)))
1966 call assert_equal(0, min(range(99999, 0, -1)))
1967 call assert_equal(0, min(reverse(range(100000))))
1968 call assert_equal(0, min(reverse(range(99999, 0, -1))))
1969
1970 " remove()
1971 call assert_equal(1, remove(range(1, 10), 0))
1972 call assert_equal(2, remove(range(1, 10), 1))
1973 call assert_equal(9, remove(range(1, 10), 8))
1974 call assert_equal(10, remove(range(1, 10), 9))
1975 call assert_equal(10, remove(range(1, 10), -1))
1976 call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4))
1977
1978 " repeat()
1979 call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2))
1980 call assert_equal([0, 1, 2], repeat(range(3), 1))
1981 call assert_equal([], repeat(range(3), 0))
1982 call assert_equal([], repeat(range(5, 4), 2))
1983 call assert_equal([], repeat(range(5, 4), 0))
1984
1985 " reverse()
1986 call assert_equal([2, 1, 0], reverse(range(3)))
1987 call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1)))
1988 call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10)))
1989 call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20)))
1990 call assert_equal([16, 13, 10], reverse(range(10, 18, 3)))
1991 call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3)))
1992 call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3)))
1993 call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3)))
1994 call assert_equal([], reverse(range(0)))
1995
1996 " TODO: setpos()
1997 " new
1998 " call setline(1, repeat([''], bufnr('')))
1999 " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6))
2000 " call setpos('x', range(bufnr(''), bufnr('') + 3))
2001 " bwipe!
2002
2003 " setreg()
2004 call setreg('a', range(3))
2005 call assert_equal("0\n1\n2\n", getreg('a'))
2006
Bram Moolenaarb0992022020-01-30 14:55:42 +01002007 " settagstack()
2008 call settagstack(1, #{items : range(4)})
Bram Moolenaar94255df2020-02-05 20:10:33 +01002009
Bram Moolenaarb0992022020-01-30 14:55:42 +01002010 " sign_define()
2011 call assert_fails("call sign_define(range(5))", "E715:")
2012 call assert_fails("call sign_placelist(range(5))", "E715:")
2013
2014 " sign_undefine()
2015 call assert_fails("call sign_undefine(range(5))", "E908:")
2016
2017 " sign_unplacelist()
2018 call assert_fails("call sign_unplacelist(range(5))", "E715:")
2019
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002020 " sort()
2021 call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1)))
2022
Bram Moolenaarb0992022020-01-30 14:55:42 +01002023 " 'spellsuggest'
2024 func MySuggest()
2025 return range(3)
2026 endfunc
2027 set spell spellsuggest=expr:MySuggest()
2028 call assert_equal([], spellsuggest('baord', 3))
2029 set nospell spellsuggest&
2030
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002031 " string()
2032 call assert_equal('[0, 1, 2, 3, 4]', string(range(5)))
2033
Bram Moolenaarb0992022020-01-30 14:55:42 +01002034 " taglist() with 'tagfunc'
2035 func TagFunc(pattern, flags, info)
2036 return range(10)
2037 endfunc
2038 set tagfunc=TagFunc
2039 call assert_fails("call taglist('asdf')", 'E987:')
2040 set tagfunc=
Bram Moolenaar94255df2020-02-05 20:10:33 +01002041
Bram Moolenaarb0992022020-01-30 14:55:42 +01002042 " term_start()
Bram Moolenaar705724e2020-01-31 21:13:42 +01002043 if has('terminal') && has('termguicolors')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002044 call assert_fails('call term_start(range(3, 4))', 'E474:')
2045 let g:terminal_ansi_colors = range(16)
Bram Moolenaar94255df2020-02-05 20:10:33 +01002046 if has('win32')
2047 let cmd = "cmd /c dir"
2048 else
2049 let cmd = "ls"
2050 endif
2051 call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"})', 'E475:')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002052 unlet g:terminal_ansi_colors
2053 endif
2054
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002055 " type()
2056 call assert_equal(v:t_list, type(range(5)))
2057
2058 " uniq()
2059 call assert_equal([0, 1, 2, 3, 4], uniq(range(5)))
2060endfunc
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002061
2062func Test_echoraw()
2063 CheckScreendump
2064
2065 " Normally used for escape codes, but let's test with a CR.
2066 let lines =<< trim END
2067 call echoraw("hello\<CR>x")
2068 END
2069 call writefile(lines, 'XTest_echoraw')
2070 let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40})
2071 call VerifyScreenDump(buf, 'Test_functions_echoraw', {})
2072
2073 " clean up
2074 call StopVimInTerminal(buf)
2075 call delete('XTest_echoraw')
2076endfunc
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002077
2078" vim: shiftwidth=2 sts=2 expandtab