blob: 9173e8547c9600ed452b5820b9ea76e466918cc1 [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 Moolenaar08243d22017-01-10 16:12:29 +01005
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +01006" Must be done first, since the alternate buffer must be unset.
7func Test_00_bufexists()
8 call assert_equal(0, bufexists('does_not_exist'))
9 call assert_equal(1, bufexists(bufnr('%')))
10 call assert_equal(0, bufexists(0))
11 new Xfoo
12 let bn = bufnr('%')
13 call assert_equal(1, bufexists(bn))
14 call assert_equal(1, bufexists('Xfoo'))
15 call assert_equal(1, bufexists(getcwd() . '/Xfoo'))
16 call assert_equal(1, bufexists(0))
17 bw
18 call assert_equal(0, bufexists(bn))
19 call assert_equal(0, bufexists('Xfoo'))
20endfunc
21
Bram Moolenaar24c2e482017-01-29 15:45:12 +010022func Test_empty()
23 call assert_equal(1, empty(''))
24 call assert_equal(0, empty('a'))
25
26 call assert_equal(1, empty(0))
27 call assert_equal(1, empty(-0))
28 call assert_equal(0, empty(1))
29 call assert_equal(0, empty(-1))
30
Bram Moolenaar5feabe02020-01-30 18:24:53 +010031 if has('float')
32 call assert_equal(1, empty(0.0))
33 call assert_equal(1, empty(-0.0))
34 call assert_equal(0, empty(1.0))
35 call assert_equal(0, empty(-1.0))
36 call assert_equal(0, empty(1.0/0.0))
37 call assert_equal(0, empty(0.0/0.0))
38 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +010039
40 call assert_equal(1, empty([]))
41 call assert_equal(0, empty(['a']))
42
43 call assert_equal(1, empty({}))
44 call assert_equal(0, empty({'a':1}))
45
46 call assert_equal(1, empty(v:null))
47 call assert_equal(1, empty(v:none))
48 call assert_equal(1, empty(v:false))
49 call assert_equal(0, empty(v:true))
50
Bram Moolenaar41042f32017-03-09 12:09:32 +010051 if has('channel')
52 call assert_equal(1, empty(test_null_channel()))
53 endif
54 if has('job')
55 call assert_equal(1, empty(test_null_job()))
56 endif
57
Bram Moolenaar24c2e482017-01-29 15:45:12 +010058 call assert_equal(0, empty(function('Test_empty')))
Bram Moolenaar17aca702019-05-16 22:24:55 +020059 call assert_equal(0, empty(function('Test_empty', [0])))
Bram Moolenaar24c2e482017-01-29 15:45:12 +010060endfunc
61
62func Test_len()
63 call assert_equal(1, len(0))
64 call assert_equal(2, len(12))
65
66 call assert_equal(0, len(''))
67 call assert_equal(2, len('ab'))
68
69 call assert_equal(0, len([]))
70 call assert_equal(2, len([2, 1]))
71
72 call assert_equal(0, len({}))
73 call assert_equal(2, len({'a': 1, 'b': 2}))
74
75 call assert_fails('call len(v:none)', 'E701:')
76 call assert_fails('call len({-> 0})', 'E701:')
77endfunc
78
79func Test_max()
80 call assert_equal(0, max([]))
81 call assert_equal(2, max([2]))
82 call assert_equal(2, max([1, 2]))
83 call assert_equal(2, max([1, 2, v:null]))
84
85 call assert_equal(0, max({}))
86 call assert_equal(2, max({'a':1, 'b':2}))
87
88 call assert_fails('call max(1)', 'E712:')
89 call assert_fails('call max(v:none)', 'E712:')
90endfunc
91
92func Test_min()
93 call assert_equal(0, min([]))
94 call assert_equal(2, min([2]))
95 call assert_equal(1, min([1, 2]))
96 call assert_equal(0, min([1, 2, v:null]))
97
98 call assert_equal(0, min({}))
99 call assert_equal(1, min({'a':1, 'b':2}))
100
101 call assert_fails('call min(1)', 'E712:')
102 call assert_fails('call min(v:none)', 'E712:')
103endfunc
104
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200105func Test_strwidth()
106 for aw in ['single', 'double']
107 exe 'set ambiwidth=' . aw
108 call assert_equal(0, strwidth(''))
109 call assert_equal(1, strwidth("\t"))
110 call assert_equal(3, strwidth('Vim'))
111 call assert_equal(4, strwidth(1234))
112 call assert_equal(5, strwidth(-1234))
113
Bram Moolenaar30276f22019-01-24 17:59:39 +0100114 call assert_equal(2, strwidth('😉'))
115 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
116 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200117
118 call assert_fails('call strwidth({->0})', 'E729:')
119 call assert_fails('call strwidth([])', 'E730:')
120 call assert_fails('call strwidth({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100121 if has('float')
122 call assert_fails('call strwidth(1.2)', 'E806:')
123 endif
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200124 endfor
125
126 set ambiwidth&
127endfunc
128
Bram Moolenaar08243d22017-01-10 16:12:29 +0100129func Test_str2nr()
130 call assert_equal(0, str2nr(''))
131 call assert_equal(1, str2nr('1'))
132 call assert_equal(1, str2nr(' 1 '))
133
134 call assert_equal(1, str2nr('+1'))
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(123456789, str2nr('123456789'))
143 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100144
145 call assert_equal(5, str2nr('101', 2))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200146 call assert_equal(5, '0b101'->str2nr(2))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100147 call assert_equal(5, str2nr('0B101', 2))
148 call assert_equal(-5, str2nr('-101', 2))
149 call assert_equal(-5, str2nr('-0b101', 2))
150 call assert_equal(-5, str2nr('-0B101', 2))
151
152 call assert_equal(65, str2nr('101', 8))
153 call assert_equal(65, str2nr('0101', 8))
154 call assert_equal(-65, str2nr('-101', 8))
155 call assert_equal(-65, str2nr('-0101', 8))
156
157 call assert_equal(11259375, str2nr('abcdef', 16))
158 call assert_equal(11259375, str2nr('ABCDEF', 16))
159 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
160 call assert_equal(11259375, str2nr('0xabcdef', 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
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200165 call assert_equal(1, str2nr("1'000'000", 10, 0))
166 call assert_equal(256, str2nr("1'0000'0000", 2, 1))
167 call assert_equal(262144, str2nr("1'000'000", 8, 1))
168 call assert_equal(1000000, str2nr("1'000'000", 10, 1))
Bram Moolenaarea8dcf82019-09-15 21:12:22 +0200169 call assert_equal(1000, str2nr("1'000''000", 10, 1))
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200170 call assert_equal(65536, str2nr("1'00'00", 16, 1))
171
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100172 call assert_equal(0, str2nr('0x10'))
173 call assert_equal(0, str2nr('0b10'))
174 call assert_equal(1, str2nr('12', 2))
175 call assert_equal(1, str2nr('18', 8))
176 call assert_equal(1, str2nr('1g', 16))
177
178 call assert_equal(0, str2nr(v:null))
179 call assert_equal(0, str2nr(v:none))
180
181 call assert_fails('call str2nr([])', 'E730:')
182 call assert_fails('call str2nr({->2})', 'E729:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100183 if has('float')
184 call assert_fails('call str2nr(1.2)', 'E806:')
185 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100186 call assert_fails('call str2nr(10, [])', 'E474:')
187endfunc
188
189func Test_strftime()
Bram Moolenaar10455d42019-11-21 15:36:18 +0100190 CheckFunction strftime
191
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100192 " Format of strftime() depends on system. We assume
193 " that basic formats tested here are available and
194 " identical on all systems which support strftime().
195 "
196 " The 2nd parameter of strftime() is a local time, so the output day
197 " of strftime() can be 17 or 18, depending on timezone.
198 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
199 "
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200200 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 +0100201
202 call assert_fails('call strftime([])', 'E730:')
203 call assert_fails('call strftime("%Y", [])', 'E745:')
Bram Moolenaardb517302019-06-18 22:53:24 +0200204
205 " Check that the time changes after we change the timezone
206 " Save previous timezone value, if any
207 if exists('$TZ')
208 let tz = $TZ
209 endif
210
211 " Force EST and then UTC, save the current hour (24-hour clock) for each
212 let $TZ = 'EST' | let est = strftime('%H')
213 let $TZ = 'UTC' | let utc = strftime('%H')
214
215 " Those hours should be two bytes long, and should not be the same; if they
216 " are, a tzset(3) call may have failed somewhere
217 call assert_equal(strlen(est), 2)
218 call assert_equal(strlen(utc), 2)
Bram Moolenaar87652a72019-06-18 23:07:37 +0200219 " TODO: this fails on MS-Windows
220 if has('unix')
221 call assert_notequal(est, utc)
222 endif
Bram Moolenaardb517302019-06-18 22:53:24 +0200223
224 " If we cached a timezone value, put it back, otherwise clear it
225 if exists('tz')
226 let $TZ = tz
227 else
228 unlet $TZ
229 endif
Bram Moolenaar10455d42019-11-21 15:36:18 +0100230endfunc
Bram Moolenaardb517302019-06-18 22:53:24 +0200231
Bram Moolenaar10455d42019-11-21 15:36:18 +0100232func Test_strptime()
233 CheckFunction strptime
234
235 if exists('$TZ')
236 let tz = $TZ
237 endif
238 let $TZ = 'UTC'
239
Bram Moolenaar9a838fe2019-12-06 12:45:01 +0100240 call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
Bram Moolenaar10455d42019-11-21 15:36:18 +0100241
242 call assert_fails('call strptime()', 'E119:')
243 call assert_fails('call strptime("xxx")', 'E119:')
244 call assert_equal(0, strptime("%Y", ''))
245 call assert_equal(0, strptime("%Y", "xxx"))
246
247 if exists('tz')
248 let $TZ = tz
249 else
250 unlet $TZ
251 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100252endfunc
253
Bram Moolenaardce1e892019-02-10 23:18:53 +0100254func Test_resolve_unix()
Bram Moolenaar26109902018-10-06 15:43:17 +0200255 if !has('unix')
256 return
257 endif
258
259 " Xlink1 -> Xlink2
260 " Xlink2 -> Xlink3
261 silent !ln -s -f Xlink2 Xlink1
262 silent !ln -s -f Xlink3 Xlink2
263 call assert_equal('Xlink3', resolve('Xlink1'))
264 call assert_equal('./Xlink3', resolve('./Xlink1'))
265 call assert_equal('Xlink3/', resolve('Xlink2/'))
266 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
267 "call assert_equal('Xlink3/', resolve('Xlink1/'))
268 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
269 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
270 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
271
272 " Test resolve() with a symlink cycle.
273 " Xlink1 -> Xlink2
274 " Xlink2 -> Xlink3
275 " Xlink3 -> Xlink1
276 silent !ln -s -f Xlink1 Xlink3
277 call assert_fails('call resolve("Xlink1")', 'E655:')
278 call assert_fails('call resolve("./Xlink1")', 'E655:')
279 call assert_fails('call resolve("Xlink2")', 'E655:')
280 call assert_fails('call resolve("Xlink3")', 'E655:')
281 call delete('Xlink1')
282 call delete('Xlink2')
283 call delete('Xlink3')
284
285 silent !ln -s -f Xdir//Xfile Xlink
286 call assert_equal('Xdir/Xfile', resolve('Xlink'))
287 call delete('Xlink')
288
289 silent !ln -s -f Xlink2/ Xlink1
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200290 call assert_equal('Xlink2', 'Xlink1'->resolve())
Bram Moolenaar26109902018-10-06 15:43:17 +0200291 call assert_equal('Xlink2/', resolve('Xlink1/'))
292 call delete('Xlink1')
293
294 silent !ln -s -f ./Xlink2 Xlink1
295 call assert_equal('Xlink2', resolve('Xlink1'))
296 call assert_equal('./Xlink2', resolve('./Xlink1'))
297 call delete('Xlink1')
298endfunc
299
Bram Moolenaardce1e892019-02-10 23:18:53 +0100300func s:normalize_fname(fname)
301 let ret = substitute(a:fname, '\', '/', 'g')
302 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200303 return ret->tolower()
Bram Moolenaardce1e892019-02-10 23:18:53 +0100304endfunc
305
306func Test_resolve_win32()
307 if !has('win32')
308 return
309 endif
310
311 " test for shortcut file
312 if executable('cscript')
313 new Xfile
314 wq
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200315 let lines =<< trim END
316 Set fs = CreateObject("Scripting.FileSystemObject")
317 Set ws = WScript.CreateObject("WScript.Shell")
318 Set shortcut = ws.CreateShortcut("Xlink.lnk")
319 shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")
320 shortcut.Save
321 END
322 call writefile(lines, 'link.vbs')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100323 silent !cscript link.vbs
324 call delete('link.vbs')
325 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
326 call delete('Xfile')
327
328 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
329 call delete('Xlink.lnk')
330 else
331 echomsg 'skipped test for shortcut file'
332 endif
333
334 " remove files
335 call delete('Xlink')
336 call delete('Xdir', 'd')
337 call delete('Xfile')
338
339 " test for symbolic link to a file
340 new Xfile
341 wq
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200342 call assert_equal('Xfile', resolve('Xfile'))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100343 silent !mklink Xlink Xfile
344 if !v:shell_error
345 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink')))
346 call delete('Xlink')
347 else
348 echomsg 'skipped test for symbolic link to a file'
349 endif
350 call delete('Xfile')
351
352 " test for junction to a directory
353 call mkdir('Xdir')
354 silent !mklink /J Xlink Xdir
355 if !v:shell_error
356 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
357
358 call delete('Xdir', 'd')
359
360 " test for junction already removed
361 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
362 call delete('Xlink')
363 else
364 echomsg 'skipped test for junction to a directory'
365 call delete('Xdir', 'd')
366 endif
367
368 " test for symbolic link to a directory
369 call mkdir('Xdir')
370 silent !mklink /D Xlink Xdir
371 if !v:shell_error
372 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
373
374 call delete('Xdir', 'd')
375
376 " test for symbolic link already removed
377 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
378 call delete('Xlink')
379 else
380 echomsg 'skipped test for symbolic link to a directory'
381 call delete('Xdir', 'd')
382 endif
383
384 " test for buffer name
385 new Xfile
386 wq
387 silent !mklink Xlink Xfile
388 if !v:shell_error
389 edit Xlink
390 call assert_equal('Xlink', bufname('%'))
391 call delete('Xlink')
392 bw!
393 else
394 echomsg 'skipped test for buffer name'
395 endif
396 call delete('Xfile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200397
398 " test for reparse point
399 call mkdir('Xdir')
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200400 call assert_equal('Xdir', resolve('Xdir'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200401 silent !mklink /D Xdirlink Xdir
402 if !v:shell_error
403 w Xdir/text.txt
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200404 call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200405 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
406 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200407 call delete('Xdirlink')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200408 else
409 echomsg 'skipped test for reparse point'
410 endif
411
412 call delete('Xdir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100413endfunc
414
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100415func Test_simplify()
416 call assert_equal('', 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('./dir/file', simplify('./dir/file'))
422 call assert_equal('./dir/file', simplify('.///dir//file'))
423 call assert_equal('./dir/file', simplify('./dir/./file'))
424 call assert_equal('./file', simplify('./dir/../file'))
425 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
426 call assert_equal('./file', simplify('dir/.././file'))
427
428 call assert_fails('call simplify({->0})', 'E729:')
429 call assert_fails('call simplify([])', 'E730:')
430 call assert_fails('call simplify({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100431 if has('float')
432 call assert_fails('call simplify(1.2)', 'E806:')
433 endif
Bram Moolenaar08243d22017-01-10 16:12:29 +0100434endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100435
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200436func Test_pathshorten()
437 call assert_equal('', pathshorten(''))
438 call assert_equal('foo', pathshorten('foo'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200439 call assert_equal('/foo', '/foo'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200440 call assert_equal('f/', pathshorten('foo/'))
441 call assert_equal('f/bar', pathshorten('foo/bar'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200442 call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200443 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
444 call assert_equal('.f/bar', pathshorten('.foo/bar'))
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'))
449endfunc
450
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100451func Test_strpart()
452 call assert_equal('de', strpart('abcdefg', 3, 2))
453 call assert_equal('ab', strpart('abcdefg', -2, 4))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200454 call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100455 call assert_equal('fg', strpart('abcdefg', 5, 4))
456 call assert_equal('defg', strpart('abcdefg', 3))
457
Bram Moolenaar30276f22019-01-24 17:59:39 +0100458 call assert_equal('lép', strpart('éléphant', 2, 4))
459 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100460endfunc
461
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100462func Test_tolower()
463 call assert_equal("", tolower(""))
464
465 " Test with all printable ASCII characters.
466 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
467 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
468
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100469 " Test with a few uppercase diacritics.
470 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
471 call assert_equal("bḃḇ", tolower("BḂḆ"))
472 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
473 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
474 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
475 call assert_equal("fḟ ", tolower("FḞ "))
476 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
477 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
478 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
479 call assert_equal("jĵ", tolower("JĴ"))
480 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
481 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
482 call assert_equal("mḿṁ", tolower("MḾṀ"))
483 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
484 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
485 call assert_equal("pṕṗ", tolower("PṔṖ"))
486 call assert_equal("q", tolower("Q"))
487 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
488 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
489 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
490 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
491 call assert_equal("vṽ", tolower("VṼ"))
492 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
493 call assert_equal("xẋẍ", tolower("XẊẌ"))
494 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
495 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
496
497 " Test with a few lowercase diacritics, which should remain unchanged.
498 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
499 call assert_equal("bḃḇ", tolower("bḃḇ"))
500 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
501 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
502 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
503 call assert_equal("fḟ", tolower("fḟ"))
504 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
505 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
506 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
507 call assert_equal("jĵǰ", tolower("jĵǰ"))
508 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
509 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
510 call assert_equal("mḿṁ ", tolower("mḿṁ "))
511 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
512 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
513 call assert_equal("pṕṗ", tolower("pṕṗ"))
514 call assert_equal("q", tolower("q"))
515 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
516 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
517 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
518 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
519 call assert_equal("vṽ", tolower("vṽ"))
520 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
521 call assert_equal("ẋẍ", tolower("ẋẍ"))
522 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
523 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
524
525 " According to https://twitter.com/jifa/status/625776454479970304
526 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
527 " in length (2 to 3 bytes) when lowercased. So let's test them.
528 call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100529
530 " This call to tolower with invalid utf8 sequence used to cause access to
531 " invalid memory.
532 call tolower("\xC0\x80\xC0")
533 call tolower("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100534endfunc
535
536func Test_toupper()
537 call assert_equal("", toupper(""))
538
539 " Test with all printable ASCII characters.
540 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
541 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
542
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100543 " Test with a few lowercase diacritics.
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200544 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100545 call assert_equal("BḂḆ", toupper("bḃḇ"))
546 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
547 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
548 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
549 call assert_equal("FḞ", toupper("fḟ"))
550 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
551 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
552 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
553 call assert_equal("JĴǰ", toupper("jĵǰ"))
554 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
555 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
556 call assert_equal("MḾṀ ", toupper("mḿṁ "))
557 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
558 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
559 call assert_equal("PṔṖ", toupper("pṕṗ"))
560 call assert_equal("Q", toupper("q"))
561 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
562 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
563 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
564 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
565 call assert_equal("VṼ", toupper("vṽ"))
566 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
567 call assert_equal("ẊẌ", toupper("ẋẍ"))
568 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
569 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
570
571 " Test that uppercase diacritics, which should remain unchanged.
572 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
573 call assert_equal("BḂḆ", toupper("BḂḆ"))
574 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
575 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
576 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
577 call assert_equal("FḞ ", toupper("FḞ "))
578 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
579 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
580 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
581 call assert_equal("JĴ", toupper("JĴ"))
582 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
583 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
584 call assert_equal("MḾṀ", toupper("MḾṀ"))
585 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
586 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
587 call assert_equal("PṔṖ", toupper("PṔṖ"))
588 call assert_equal("Q", toupper("Q"))
589 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
590 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
591 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
592 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
593 call assert_equal("VṼ", toupper("VṼ"))
594 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
595 call assert_equal("XẊẌ", toupper("XẊẌ"))
596 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
597 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
598
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100599 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100600
601 " This call to toupper with invalid utf8 sequence used to cause access to
602 " invalid memory.
603 call toupper("\xC0\x80\xC0")
604 call toupper("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100605endfunc
606
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200607func Test_tr()
608 call assert_equal('foo', tr('bar', 'bar', 'foo'))
609 call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
610endfunc
611
Bram Moolenaare90858d2017-02-01 17:24:34 +0100612" Tests for the mode() function
613let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100614func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100615 let g:current_modes = mode(0) . '-' . mode(1)
616 return ''
617endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100618
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100619func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100620 new
621 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
622
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100623 " Only complete from the current buffer.
624 set complete=.
625
Bram Moolenaare90858d2017-02-01 17:24:34 +0100626 inoremap <F2> <C-R>=Save_mode()<CR>
627
628 normal! 3G
629 exe "normal i\<F2>\<Esc>"
630 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100631 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100632 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
633 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100634 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100635 exe "normal iBro\<C-P>\<F2>\<Esc>u"
636 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100637 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100638 exe "normal iBa\<C-X>\<F2>\<Esc>u"
639 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100640 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100641 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
642 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100643 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100644 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
645 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100646 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100647 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
648 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100649 " i_CTRL-X CTRL-L: Multiple matches
650 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
651 call assert_equal('i-ic', g:current_modes)
652 " i_CTRL-X CTRL-L: Single match
653 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
654 call assert_equal('i-ic', g:current_modes)
655 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100656 exe "normal iCom\<C-P>\<F2>\<Esc>u"
657 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100658 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100659 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
660 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100661 " i_CTRL-X CTRL-L: No match
662 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
663 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100664
Bram Moolenaare971df32017-02-05 14:15:29 +0100665 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100666 exe "normal RBa\<C-P>\<F2>\<Esc>u"
667 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100668 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100669 exe "normal RBro\<C-P>\<F2>\<Esc>u"
670 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100671 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100672 exe "normal RBa\<C-X>\<F2>\<Esc>u"
673 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100674 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100675 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
676 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100677 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100678 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
679 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100680 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100681 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
682 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100683 " R_CTRL-X CTRL-L: Multiple matches
684 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
685 call assert_equal('R-Rc', g:current_modes)
686 " R_CTRL-X CTRL-L: Single match
687 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
688 call assert_equal('R-Rc', g:current_modes)
689 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100690 exe "normal RCom\<C-P>\<F2>\<Esc>u"
691 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100692 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100693 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
694 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100695 " R_CTRL-X CTRL-L: No match
696 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
697 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100698
Bram Moolenaara1449832019-09-01 20:16:52 +0200699 call assert_equal('n', 0->mode())
700 call assert_equal('n', 1->mode())
Bram Moolenaare90858d2017-02-01 17:24:34 +0100701
Bram Moolenaar612cc382018-07-29 15:34:26 +0200702 " i_CTRL-O
703 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
704 call assert_equal("n-niI", g:current_modes)
705
706 " R_CTRL-O
707 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
708 call assert_equal("n-niR", g:current_modes)
709
710 " gR_CTRL-O
711 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
712 call assert_equal("n-niV", g:current_modes)
713
Bram Moolenaare90858d2017-02-01 17:24:34 +0100714 " How to test operator-pending mode?
715
716 call feedkeys("v", 'xt')
717 call assert_equal('v', mode())
718 call assert_equal('v', mode(1))
719 call feedkeys("\<Esc>V", 'xt')
720 call assert_equal('V', mode())
721 call assert_equal('V', mode(1))
722 call feedkeys("\<Esc>\<C-V>", 'xt')
723 call assert_equal("\<C-V>", mode())
724 call assert_equal("\<C-V>", mode(1))
725 call feedkeys("\<Esc>", 'xt')
726
727 call feedkeys("gh", 'xt')
728 call assert_equal('s', mode())
729 call assert_equal('s', mode(1))
730 call feedkeys("\<Esc>gH", 'xt')
731 call assert_equal('S', mode())
732 call assert_equal('S', mode(1))
733 call feedkeys("\<Esc>g\<C-H>", 'xt')
734 call assert_equal("\<C-S>", mode())
735 call assert_equal("\<C-S>", mode(1))
736 call feedkeys("\<Esc>", 'xt')
737
738 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
739 call assert_equal('c-c', g:current_modes)
740 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
741 call assert_equal('c-cv', g:current_modes)
742 " How to test Ex mode?
743
744 bwipe!
745 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100746 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100747endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100748
Bram Moolenaard2007022019-08-27 21:56:06 +0200749func Test_append()
750 enew!
751 split
752 call append(0, ["foo"])
753 split
754 only
755 undo
756endfunc
757
Bram Moolenaar79518e22017-02-17 16:31:35 +0100758func Test_getbufvar()
759 let bnr = bufnr('%')
760 let b:var_num = '1234'
761 let def_num = '5678'
762 call assert_equal('1234', getbufvar(bnr, 'var_num'))
763 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
764
765 let bd = getbufvar(bnr, '')
766 call assert_equal('1234', bd['var_num'])
767 call assert_true(exists("bd['changedtick']"))
768 call assert_equal(2, len(bd))
769
770 let bd2 = getbufvar(bnr, '', def_num)
771 call assert_equal(bd, bd2)
772
773 unlet b:var_num
774 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
775 call assert_equal('', getbufvar(bnr, 'var_num'))
776
777 let bd = getbufvar(bnr, '')
778 call assert_equal(1, len(bd))
779 let bd = getbufvar(bnr, '',def_num)
780 call assert_equal(1, len(bd))
781
Bram Moolenaar4520d442017-03-19 16:09:46 +0100782 call assert_equal('', getbufvar(9999, ''))
783 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100784 unlet def_num
785
Bram Moolenaar507647d2017-02-17 16:43:49 +0100786 call assert_equal(0, getbufvar(bnr, '&autoindent'))
787 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100788
789 " Open new window with forced option values
790 set fileformats=unix,dos
791 new ++ff=dos ++bin ++enc=iso-8859-2
792 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
793 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
794 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
795 close
796
797 set fileformats&
798endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100799
Bram Moolenaar41042f32017-03-09 12:09:32 +0100800func Test_last_buffer_nr()
801 call assert_equal(bufnr('$'), last_buffer_nr())
802endfunc
803
804func Test_stridx()
805 call assert_equal(-1, stridx('', 'l'))
806 call assert_equal(0, stridx('', ''))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200807 call assert_equal(0, 'hello'->stridx(''))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100808 call assert_equal(-1, stridx('hello', 'L'))
809 call assert_equal(2, stridx('hello', 'l', -1))
810 call assert_equal(2, stridx('hello', 'l', 0))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200811 call assert_equal(2, 'hello'->stridx('l', 1))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100812 call assert_equal(3, stridx('hello', 'l', 3))
813 call assert_equal(-1, stridx('hello', 'l', 4))
814 call assert_equal(-1, stridx('hello', 'l', 10))
815 call assert_equal(2, stridx('hello', 'll'))
816 call assert_equal(-1, stridx('hello', 'hello world'))
817endfunc
818
819func Test_strridx()
820 call assert_equal(-1, strridx('', 'l'))
821 call assert_equal(0, strridx('', ''))
822 call assert_equal(5, strridx('hello', ''))
823 call assert_equal(-1, strridx('hello', 'L'))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200824 call assert_equal(3, 'hello'->strridx('l'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100825 call assert_equal(3, strridx('hello', 'l', 10))
826 call assert_equal(3, strridx('hello', 'l', 3))
827 call assert_equal(2, strridx('hello', 'l', 2))
828 call assert_equal(-1, strridx('hello', 'l', 1))
829 call assert_equal(-1, strridx('hello', 'l', 0))
830 call assert_equal(-1, strridx('hello', 'l', -1))
831 call assert_equal(2, strridx('hello', 'll'))
832 call assert_equal(-1, strridx('hello', 'hello world'))
833endfunc
834
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200835func Test_match_func()
836 call assert_equal(4, match('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200837 call assert_equal(4, 'testing'->match('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200838 call assert_equal(-1, match('testing', 'ing', 5))
839 call assert_equal(-1, match('testing', 'ing', 8))
840 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
841 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
842endfunc
843
Bram Moolenaar41042f32017-03-09 12:09:32 +0100844func Test_matchend()
845 call assert_equal(7, matchend('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200846 call assert_equal(7, 'testing'->matchend('ing', 2))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100847 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200848 call assert_equal(-1, matchend('testing', 'ing', 8))
849 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
850 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
851endfunc
852
853func Test_matchlist()
854 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200855 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200856 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
857endfunc
858
859func Test_matchstr()
860 call assert_equal('ing', matchstr('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200861 call assert_equal('ing', 'testing'->matchstr('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200862 call assert_equal('', matchstr('testing', 'ing', 5))
863 call assert_equal('', matchstr('testing', 'ing', 8))
864 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
865 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
866endfunc
867
868func Test_matchstrpos()
869 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200870 call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200871 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
872 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
873 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
874 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100875endfunc
876
877func Test_nextnonblank_prevnonblank()
878 new
879insert
880This
881
882
883is
884
885a
886Test
887.
888 call assert_equal(0, nextnonblank(-1))
889 call assert_equal(0, nextnonblank(0))
890 call assert_equal(1, nextnonblank(1))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200891 call assert_equal(4, 2->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100892 call assert_equal(4, nextnonblank(3))
893 call assert_equal(4, nextnonblank(4))
894 call assert_equal(6, nextnonblank(5))
895 call assert_equal(6, nextnonblank(6))
896 call assert_equal(7, nextnonblank(7))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200897 call assert_equal(0, 8->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100898
899 call assert_equal(0, prevnonblank(-1))
900 call assert_equal(0, prevnonblank(0))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200901 call assert_equal(1, 1->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100902 call assert_equal(1, prevnonblank(2))
903 call assert_equal(1, prevnonblank(3))
904 call assert_equal(4, prevnonblank(4))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200905 call assert_equal(4, 5->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100906 call assert_equal(6, prevnonblank(6))
907 call assert_equal(7, prevnonblank(7))
908 call assert_equal(0, prevnonblank(8))
909 bw!
910endfunc
911
912func Test_byte2line_line2byte()
913 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200914 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +0100915 call setline(1, ['a', 'bc', 'd'])
916
917 set fileformat=unix
918 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
919 \ map(range(-1, 8), 'byte2line(v:val)'))
920 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
921 \ map(range(-1, 5), 'line2byte(v:val)'))
922
923 set fileformat=mac
924 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
Bram Moolenaar64b4d732019-08-22 22:18:17 +0200925 \ map(range(-1, 8), 'v:val->byte2line()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100926 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
Bram Moolenaar02b31112019-08-31 22:16:38 +0200927 \ map(range(-1, 5), 'v:val->line2byte()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100928
929 set fileformat=dos
930 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
931 \ map(range(-1, 11), 'byte2line(v:val)'))
932 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
933 \ map(range(-1, 5), 'line2byte(v:val)'))
934
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200935 bw!
936 set noendofline nofixendofline
937 normal a-
938 for ff in ["unix", "mac", "dos"]
939 let &fileformat = ff
940 call assert_equal(1, line2byte(1))
941 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
942 endfor
943
944 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +0100945 bw!
946endfunc
947
Bram Moolenaar64b4d732019-08-22 22:18:17 +0200948func Test_byteidx()
949 let a = '.é.' " one char of two bytes
950 call assert_equal(0, byteidx(a, 0))
951 call assert_equal(0, byteidxcomp(a, 0))
952 call assert_equal(1, byteidx(a, 1))
953 call assert_equal(1, byteidxcomp(a, 1))
954 call assert_equal(3, byteidx(a, 2))
955 call assert_equal(3, byteidxcomp(a, 2))
956 call assert_equal(4, byteidx(a, 3))
957 call assert_equal(4, byteidxcomp(a, 3))
958 call assert_equal(-1, byteidx(a, 4))
959 call assert_equal(-1, byteidxcomp(a, 4))
960
961 let b = '.é.' " normal e with composing char
962 call assert_equal(0, b->byteidx(0))
963 call assert_equal(1, b->byteidx(1))
964 call assert_equal(4, b->byteidx(2))
965 call assert_equal(5, b->byteidx(3))
966 call assert_equal(-1, b->byteidx(4))
967
968 call assert_equal(0, b->byteidxcomp(0))
969 call assert_equal(1, b->byteidxcomp(1))
970 call assert_equal(2, b->byteidxcomp(2))
971 call assert_equal(4, b->byteidxcomp(3))
972 call assert_equal(5, b->byteidxcomp(4))
973 call assert_equal(-1, b->byteidxcomp(5))
974endfunc
975
Bram Moolenaar41042f32017-03-09 12:09:32 +0100976func Test_count()
977 let l = ['a', 'a', 'A', 'b']
978 call assert_equal(2, count(l, 'a'))
979 call assert_equal(1, count(l, 'A'))
980 call assert_equal(1, count(l, 'b'))
981 call assert_equal(0, count(l, 'B'))
982
983 call assert_equal(2, count(l, 'a', 0))
984 call assert_equal(1, count(l, 'A', 0))
985 call assert_equal(1, count(l, 'b', 0))
986 call assert_equal(0, count(l, 'B', 0))
987
988 call assert_equal(3, count(l, 'a', 1))
989 call assert_equal(3, count(l, 'A', 1))
990 call assert_equal(1, count(l, 'b', 1))
991 call assert_equal(1, count(l, 'B', 1))
992 call assert_equal(0, count(l, 'c', 1))
993
994 call assert_equal(1, count(l, 'a', 0, 1))
995 call assert_equal(2, count(l, 'a', 1, 1))
996 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +0200997 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100998
999 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
1000 call assert_equal(2, count(d, 'a'))
1001 call assert_equal(1, count(d, 'A'))
1002 call assert_equal(1, count(d, 'b'))
1003 call assert_equal(0, count(d, 'B'))
1004
1005 call assert_equal(2, count(d, 'a', 0))
1006 call assert_equal(1, count(d, 'A', 0))
1007 call assert_equal(1, count(d, 'b', 0))
1008 call assert_equal(0, count(d, 'B', 0))
1009
1010 call assert_equal(3, count(d, 'a', 1))
1011 call assert_equal(3, count(d, 'A', 1))
1012 call assert_equal(1, count(d, 'b', 1))
1013 call assert_equal(1, count(d, 'B', 1))
1014 call assert_equal(0, count(d, 'c', 1))
1015
1016 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +02001017
1018 call assert_equal(0, count("foo", "bar"))
1019 call assert_equal(1, count("foo", "oo"))
1020 call assert_equal(2, count("foo", "o"))
1021 call assert_equal(0, count("foo", "O"))
1022 call assert_equal(2, count("foo", "O", 1))
1023 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +01001024 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001025
1026 call assert_fails('call count(0, 0)', 'E712:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001027endfunc
1028
1029func Test_changenr()
1030 new Xchangenr
1031 call assert_equal(0, changenr())
1032 norm ifoo
1033 call assert_equal(1, changenr())
1034 set undolevels=10
1035 norm Sbar
1036 call assert_equal(2, changenr())
1037 undo
1038 call assert_equal(1, changenr())
1039 redo
1040 call assert_equal(2, changenr())
1041 bw!
1042 set undolevels&
1043endfunc
1044
1045func Test_filewritable()
1046 new Xfilewritable
1047 write!
1048 call assert_equal(1, filewritable('Xfilewritable'))
1049
1050 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1051 call assert_equal(0, filewritable('Xfilewritable'))
1052
1053 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001054 call assert_equal(1, 'Xfilewritable'->filewritable())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001055
1056 call assert_equal(0, filewritable('doesnotexist'))
1057
1058 call delete('Xfilewritable')
1059 bw!
1060endfunc
1061
Bram Moolenaar82956662018-10-06 15:18:45 +02001062func Test_Executable()
1063 if has('win32')
1064 call assert_equal(1, executable('notepad'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001065 call assert_equal(1, 'notepad.exe'->executable())
Bram Moolenaar82956662018-10-06 15:18:45 +02001066 call assert_equal(0, executable('notepad.exe.exe'))
1067 call assert_equal(0, executable('shell32.dll'))
1068 call assert_equal(0, executable('win.ini'))
1069 elseif has('unix')
Bram Moolenaara4208962019-08-24 20:50:19 +02001070 call assert_equal(1, 'cat'->executable())
Bram Moolenaara05a0d32018-10-07 18:43:05 +02001071 call assert_equal(0, executable('nodogshere'))
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001072
1073 " get "cat" path and remove the leading /
1074 let catcmd = exepath('cat')[1:]
1075 new
Bram Moolenaara4208962019-08-24 20:50:19 +02001076 " check that the relative path works in /
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001077 lcd /
1078 call assert_equal(1, executable(catcmd))
Bram Moolenaara4208962019-08-24 20:50:19 +02001079 call assert_equal('/' .. catcmd, catcmd->exepath())
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001080 bwipe
Bram Moolenaar82956662018-10-06 15:18:45 +02001081 endif
1082endfunc
1083
Bram Moolenaar86621892019-03-30 21:51:28 +01001084func Test_executable_longname()
1085 if !has('win32')
1086 return
1087 endif
1088
1089 let fname = 'X' . repeat('あ', 200) . '.bat'
1090 call writefile([], fname)
1091 call assert_equal(1, executable(fname))
1092 call delete(fname)
1093endfunc
1094
Bram Moolenaar41042f32017-03-09 12:09:32 +01001095func Test_hostname()
1096 let hostname_vim = hostname()
1097 if has('unix')
1098 let hostname_system = systemlist('uname -n')[0]
1099 call assert_equal(hostname_vim, hostname_system)
1100 endif
1101endfunc
1102
1103func Test_getpid()
1104 " getpid() always returns the same value within a vim instance.
1105 call assert_equal(getpid(), getpid())
1106 if has('unix')
1107 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1108 endif
1109endfunc
1110
1111func Test_hlexists()
1112 call assert_equal(0, hlexists('does_not_exist'))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001113 call assert_equal(0, 'Number'->hlexists())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001114 call assert_equal(0, highlight_exists('does_not_exist'))
1115 call assert_equal(0, highlight_exists('Number'))
1116 syntax on
1117 call assert_equal(0, hlexists('does_not_exist'))
1118 call assert_equal(1, hlexists('Number'))
1119 call assert_equal(0, highlight_exists('does_not_exist'))
1120 call assert_equal(1, highlight_exists('Number'))
1121 syntax off
1122endfunc
1123
1124func Test_col()
1125 new
1126 call setline(1, 'abcdef')
1127 norm gg4|mx6|mY2|
1128 call assert_equal(2, col('.'))
1129 call assert_equal(7, col('$'))
1130 call assert_equal(4, col("'x"))
1131 call assert_equal(6, col("'Y"))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001132 call assert_equal(2, [1, 2]->col())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001133 call assert_equal(7, col([1, '$']))
1134
1135 call assert_equal(0, col(''))
1136 call assert_equal(0, col('x'))
1137 call assert_equal(0, col([2, '$']))
1138 call assert_equal(0, col([1, 100]))
1139 call assert_equal(0, col([1]))
1140 bw!
1141endfunc
1142
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001143func Test_inputlist()
1144 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1145 call assert_equal(1, c)
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001146 call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001147 call assert_equal(2, c)
1148 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1149 call assert_equal(3, c)
1150
1151 call assert_fails('call inputlist("")', 'E686:')
1152endfunc
1153
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001154func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001155 if has('balloon_eval')
1156 " This won't do anything but must not crash either.
1157 call balloon_show('hi!')
Bram Moolenaar7d8ea0b2020-01-27 23:01:30 +01001158 if !has('gui_running')
1159 call balloon_show(range(3))
1160 endif
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001161 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001162endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001163
1164func Test_setbufvar_options()
1165 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1166 " window layout.
1167 call assert_equal(1, winnr('$'))
1168 split dummy_preview
1169 resize 2
1170 set winfixheight winfixwidth
1171 let prev_id = win_getid()
1172
1173 wincmd j
1174 let wh = winheight('.')
1175 let dummy_buf = bufnr('dummy_buf1', v:true)
1176 call setbufvar(dummy_buf, '&buftype', 'nofile')
1177 execute 'belowright vertical split #' . dummy_buf
1178 call assert_equal(wh, winheight('.'))
1179 let dum1_id = win_getid()
1180
1181 wincmd h
1182 let wh = winheight('.')
1183 let dummy_buf = bufnr('dummy_buf2', v:true)
Bram Moolenaar196b4662019-09-06 21:34:30 +02001184 eval 'nofile'->setbufvar(dummy_buf, '&buftype')
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001185 execute 'belowright vertical split #' . dummy_buf
1186 call assert_equal(wh, winheight('.'))
1187
1188 bwipe!
1189 call win_gotoid(prev_id)
1190 bwipe!
1191 call win_gotoid(dum1_id)
1192 bwipe!
1193endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001194
1195func Test_redo_in_nested_functions()
1196 nnoremap g. :set opfunc=Operator<CR>g@
1197 function Operator( type, ... )
1198 let @x = 'XXX'
1199 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1200 endfunction
1201
1202 function! Apply()
1203 5,6normal! .
1204 endfunction
1205
1206 new
1207 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1208 1normal g.i"
1209 call assert_equal('some "XXX" text', getline(1))
1210 3,4normal .
1211 call assert_equal('some "XXX" text', getline(3))
1212 call assert_equal('more "XXX" text', getline(4))
1213 call Apply()
1214 call assert_equal('some "XXX" text', getline(5))
1215 call assert_equal('more "XXX" text', getline(6))
1216 bwipe!
1217
1218 nunmap g.
1219 delfunc Operator
1220 delfunc Apply
1221endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02001222
1223func Test_shellescape()
1224 let save_shell = &shell
1225 set shell=bash
1226 call assert_equal("'text'", shellescape('text'))
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001227 call assert_equal("'te\"xt'", 'te"xt'->shellescape())
Bram Moolenaar20615522017-06-05 18:46:26 +02001228 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
1229
1230 call assert_equal("'te%xt'", shellescape("te%xt"))
1231 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
1232 call assert_equal("'te#xt'", shellescape("te#xt"))
1233 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
1234 call assert_equal("'te!xt'", shellescape("te!xt"))
1235 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1236
1237 call assert_equal("'te\nxt'", shellescape("te\nxt"))
1238 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1239 set shell=tcsh
1240 call assert_equal("'te\\!xt'", shellescape("te!xt"))
1241 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1242 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1243 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1244
1245 let &shell = save_shell
1246endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001247
1248func Test_trim()
1249 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001250 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 +01001251 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1252 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
1253 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
1254 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
1255 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
1256 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1257 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1258 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
1259 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
1260 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
1261 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
1262 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1263 call assert_equal("", trim("", ""))
1264 call assert_equal("a", trim("a", ""))
1265 call assert_equal("", trim("", "a"))
1266
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001267 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001268 call assert_equal("x", trim(chars . "x" . chars))
1269endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001270
1271" Test for reg_recording() and reg_executing()
1272func Test_reg_executing_and_recording()
1273 let s:reg_stat = ''
1274 func s:save_reg_stat()
1275 let s:reg_stat = reg_recording() . ':' . reg_executing()
1276 return ''
1277 endfunc
1278
1279 new
1280 call s:save_reg_stat()
1281 call assert_equal(':', s:reg_stat)
1282 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1283 call assert_equal('a:', s:reg_stat)
1284 call feedkeys("@a", 'xt')
1285 call assert_equal(':a', s:reg_stat)
1286 call feedkeys("qb@aq", 'xt')
1287 call assert_equal('b:a', s:reg_stat)
1288 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1289 call assert_equal('":', s:reg_stat)
1290
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001291 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001292 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001293 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1294 func TestFunc() abort
1295 normal! ia
1296 endfunc
1297 call feedkeys("@q", 'xt')
1298 call assert_equal(':q', s:reg_stat)
1299 delfunc TestFunc
1300
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001301 " getchar() command saves and restores reg_executing
1302 map W :call TestFunc()<CR>
1303 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001304 let g:typed = ''
1305 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001306 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001307 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001308 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001309 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001310 endfunc
1311 call feedkeys("@qy", 'xt')
1312 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001313 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001314 delfunc TestFunc
1315 unmap W
1316 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001317 unlet g:regs
1318
1319 " input() command saves and restores reg_executing
1320 map W :call TestFunc()<CR>
1321 let @q = "W"
1322 let g:typed = ''
1323 let g:regs = []
1324 func TestFunc() abort
1325 let g:regs += [reg_executing()]
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001326 let g:typed = '?'->input()
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001327 let g:regs += [reg_executing()]
1328 endfunc
1329 call feedkeys("@qy\<CR>", 'xt')
1330 call assert_equal("y", g:typed)
1331 call assert_equal(['q', 'q'], g:regs)
1332 delfunc TestFunc
1333 unmap W
1334 unlet g:typed
1335 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001336
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001337 bwipe!
1338 delfunc s:save_reg_stat
1339 unlet s:reg_stat
1340endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001341
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001342func Test_inputsecret()
1343 map W :call TestFunc()<CR>
1344 let @q = "W"
1345 let g:typed1 = ''
1346 let g:typed2 = ''
1347 let g:regs = []
1348 func TestFunc() abort
1349 let g:typed1 = '?'->inputsecret()
1350 let g:typed2 = inputsecret('password: ')
1351 endfunc
1352 call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1353 call assert_equal("something", g:typed1)
1354 call assert_equal("else", g:typed2)
1355 delfunc TestFunc
1356 unmap W
1357 unlet g:typed1
1358 unlet g:typed2
1359endfunc
1360
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001361func Test_getchar()
1362 call feedkeys('a', '')
1363 call assert_equal(char2nr('a'), getchar())
1364
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001365 call setline(1, 'xxxx')
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001366 call test_setmouse(1, 3)
1367 let v:mouse_win = 9
1368 let v:mouse_winid = 9
1369 let v:mouse_lnum = 9
1370 let v:mouse_col = 9
1371 call feedkeys("\<S-LeftMouse>", '')
1372 call assert_equal("\<S-LeftMouse>", getchar())
1373 call assert_equal(1, v:mouse_win)
1374 call assert_equal(win_getid(1), v:mouse_winid)
1375 call assert_equal(1, v:mouse_lnum)
1376 call assert_equal(3, v:mouse_col)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001377 enew!
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001378endfunc
1379
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001380func Test_libcall_libcallnr()
1381 if !has('libcall')
1382 return
1383 endif
1384
1385 if has('win32')
1386 let libc = 'msvcrt.dll'
1387 elseif has('mac')
1388 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001389 elseif executable('ldd')
1390 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1391 endif
1392 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001393 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001394 if has('linux')
1395 " There is not documented but regarding the 1st argument of glibc's
1396 " dlopen an empty string and nullptr are equivalent, so using an empty
1397 " string for the 1st argument of libcall allows to call functions.
1398 let libc = ''
1399 elseif has('sun')
1400 " Set the path to libc.so according to the architecture.
1401 let test_bits = system('file ' . GetVimProg())
1402 let test_arch = system('uname -p')
1403 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1404 let libc = '/usr/lib/sparcv9/libc.so'
1405 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1406 let libc = '/usr/lib/amd64/libc.so'
1407 else
1408 let libc = '/usr/lib/libc.so'
1409 endif
1410 else
1411 " Unfortunately skip this test until a good way is found.
1412 return
1413 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001414 endif
1415
1416 if has('win32')
Bram Moolenaar02b31112019-08-31 22:16:38 +02001417 call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001418 else
Bram Moolenaar02b31112019-08-31 22:16:38 +02001419 call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001420 endif
1421
1422 " If function returns NULL, libcall() should return an empty string.
1423 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1424
1425 " Test libcallnr() with string and integer argument.
Bram Moolenaar02b31112019-08-31 22:16:38 +02001426 call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1427 call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001428
1429 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1430 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1431
1432 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1433 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1434endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001435
1436sandbox function Fsandbox()
1437 normal ix
1438endfunc
1439
1440func Test_func_sandbox()
1441 sandbox let F = {-> 'hello'}
1442 call assert_equal('hello', F())
1443
Bram Moolenaara4208962019-08-24 20:50:19 +02001444 sandbox let F = {-> "normal ix\<Esc>"->execute()}
Bram Moolenaard90a1442018-07-15 20:24:31 +02001445 call assert_fails('call F()', 'E48:')
1446 unlet F
1447
1448 call assert_fails('call Fsandbox()', 'E48:')
1449 delfunc Fsandbox
1450endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001451
1452func EditAnotherFile()
1453 let word = expand('<cword>')
1454 edit Xfuncrange2
1455endfunc
1456
1457func Test_func_range_with_edit()
1458 " Define a function that edits another buffer, then call it with a range that
1459 " is invalid in that buffer.
1460 call writefile(['just one line'], 'Xfuncrange2')
1461 new
Bram Moolenaar196b4662019-09-06 21:34:30 +02001462 eval 10->range()->setline(1)
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001463 write Xfuncrange1
1464 call assert_fails('5,8call EditAnotherFile()', 'E16:')
1465
1466 call delete('Xfuncrange1')
1467 call delete('Xfuncrange2')
1468 bwipe!
1469endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001470
1471func Test_func_exists_on_reload()
1472 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1473 call assert_equal(0, exists('*ExistingFunction'))
1474 source Xfuncexists
Bram Moolenaara4208962019-08-24 20:50:19 +02001475 call assert_equal(1, '*ExistingFunction'->exists())
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001476 " Redefining a function when reloading a script is OK.
1477 source Xfuncexists
1478 call assert_equal(1, exists('*ExistingFunction'))
1479
1480 " But redefining in another script is not OK.
1481 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1482 call assert_fails('source Xfuncexists2', 'E122:')
1483
1484 delfunc ExistingFunction
1485 call assert_equal(0, exists('*ExistingFunction'))
1486 call writefile([
1487 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1488 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1489 \ ], 'Xfuncexists')
1490 call assert_fails('source Xfuncexists', 'E122:')
1491 call assert_equal(1, exists('*ExistingFunction'))
1492
1493 call delete('Xfuncexists2')
1494 call delete('Xfuncexists')
1495 delfunc ExistingFunction
1496endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01001497
1498" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1499func Test_confirm()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001500 CheckUnix
1501 CheckNotGui
Bram Moolenaar2e050092019-01-27 15:00:36 +01001502
1503 call feedkeys('o', 'L')
1504 let a = confirm('Press O to proceed')
1505 call assert_equal(1, a)
1506
1507 call feedkeys('y', 'L')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001508 let a = 'Are you sure?'->confirm("&Yes\n&No")
Bram Moolenaar2e050092019-01-27 15:00:36 +01001509 call assert_equal(1, a)
1510
1511 call feedkeys('n', 'L')
1512 let a = confirm('Are you sure?', "&Yes\n&No")
1513 call assert_equal(2, a)
1514
1515 " confirm() should return 0 when pressing CTRL-C.
1516 call feedkeys("\<C-c>", 'L')
1517 let a = confirm('Are you sure?', "&Yes\n&No")
1518 call assert_equal(0, a)
1519
1520 " <Esc> requires another character to avoid it being seen as the start of an
1521 " escape sequence. Zero should be harmless.
Bram Moolenaara4208962019-08-24 20:50:19 +02001522 eval "\<Esc>0"->feedkeys('L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01001523 let a = confirm('Are you sure?', "&Yes\n&No")
1524 call assert_equal(0, a)
1525
1526 " Default choice is returned when pressing <CR>.
1527 call feedkeys("\<CR>", 'L')
1528 let a = confirm('Are you sure?', "&Yes\n&No")
1529 call assert_equal(1, a)
1530
1531 call feedkeys("\<CR>", 'L')
1532 let a = confirm('Are you sure?', "&Yes\n&No", 2)
1533 call assert_equal(2, a)
1534
1535 call feedkeys("\<CR>", 'L')
1536 let a = confirm('Are you sure?', "&Yes\n&No", 0)
1537 call assert_equal(0, a)
1538
1539 " Test with the {type} 4th argument
1540 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
1541 call feedkeys('y', 'L')
1542 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
1543 call assert_equal(1, a)
1544 endfor
1545
1546 call assert_fails('call confirm([])', 'E730:')
1547 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
1548 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
1549 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
1550endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001551
1552func Test_platform_name()
1553 " The system matches at most only one name.
1554 let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
1555 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
1556
1557 " Is Unix?
1558 call assert_equal(has('beos'), has('beos') && has('unix'))
1559 call assert_equal(has('bsd'), has('bsd') && has('unix'))
1560 call assert_equal(has('hpux'), has('hpux') && has('unix'))
1561 call assert_equal(has('linux'), has('linux') && has('unix'))
1562 call assert_equal(has('mac'), has('mac') && has('unix'))
1563 call assert_equal(has('qnx'), has('qnx') && has('unix'))
1564 call assert_equal(has('sun'), has('sun') && has('unix'))
1565 call assert_equal(has('win32'), has('win32') && !has('unix'))
1566 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
1567
1568 if has('unix') && executable('uname')
1569 let uname = system('uname')
1570 call assert_equal(uname =~? 'BeOS', has('beos'))
Bram Moolenaara02e3f62019-02-07 21:27:14 +01001571 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
1572 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001573 call assert_equal(uname =~? 'HP-UX', has('hpux'))
1574 call assert_equal(uname =~? 'Linux', has('linux'))
1575 call assert_equal(uname =~? 'Darwin', has('mac'))
1576 call assert_equal(uname =~? 'QNX', has('qnx'))
1577 call assert_equal(uname =~? 'SunOS', has('sun'))
1578 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
1579 endif
1580endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001581
1582func Test_readdir()
1583 call mkdir('Xdir')
1584 call writefile([], 'Xdir/foo.txt')
1585 call writefile([], 'Xdir/bar.txt')
1586 call mkdir('Xdir/dir')
1587
1588 " All results
1589 let files = readdir('Xdir')
1590 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
1591
1592 " Only results containing "f"
Bram Moolenaara0d1fef2019-09-04 22:29:14 +02001593 let files = 'Xdir'->readdir({ x -> stridx(x, 'f') !=- 1 })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001594 call assert_equal(['foo.txt'], sort(files))
1595
1596 " Only .txt files
1597 let files = readdir('Xdir', { x -> x =~ '.txt$' })
1598 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1599
1600 " Only .txt files with string
1601 let files = readdir('Xdir', 'v:val =~ ".txt$"')
1602 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1603
1604 " Limit to 1 result.
1605 let l = []
1606 let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
1607 call assert_equal(1, len(files))
1608
Bram Moolenaar27da7de2019-09-03 17:13:37 +02001609 " Nested readdir() must not crash
1610 let files = readdir('Xdir', 'readdir("Xdir", "1") != []')
1611 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
1612
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001613 eval 'Xdir'->delete('rf')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001614endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02001615
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001616func Test_delete_rf()
1617 call mkdir('Xdir')
1618 call writefile([], 'Xdir/foo.txt')
1619 call writefile([], 'Xdir/bar.txt')
1620 call mkdir('Xdir/[a-1]') " issue #696
1621 call writefile([], 'Xdir/[a-1]/foo.txt')
1622 call writefile([], 'Xdir/[a-1]/bar.txt')
1623 call assert_true(filereadable('Xdir/foo.txt'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001624 call assert_true('Xdir/[a-1]/foo.txt'->filereadable())
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001625
1626 call assert_equal(0, delete('Xdir', 'rf'))
1627 call assert_false(filereadable('Xdir/foo.txt'))
1628 call assert_false(filereadable('Xdir/[a-1]/foo.txt'))
1629endfunc
1630
Bram Moolenaar17aca702019-05-16 22:24:55 +02001631func Test_call()
1632 call assert_equal(3, call('len', [123]))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001633 call assert_equal(3, 'len'->call([123]))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001634 call assert_fails("call call('len', 123)", 'E714:')
1635 call assert_equal(0, call('', []))
1636
1637 function Mylen() dict
1638 return len(self.data)
1639 endfunction
1640 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001641 eval mydict.len->call([], mydict)->assert_equal(4)
Bram Moolenaar17aca702019-05-16 22:24:55 +02001642 call assert_fails("call call('Mylen', [], 0)", 'E715:')
1643endfunc
1644
1645func Test_char2nr()
1646 call assert_equal(12354, char2nr('あ', 1))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001647 call assert_equal(120, 'x'->char2nr())
Bram Moolenaar17aca702019-05-16 22:24:55 +02001648endfunc
1649
1650func Test_eventhandler()
1651 call assert_equal(0, eventhandler())
1652endfunc
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001653
1654func Test_bufadd_bufload()
1655 call assert_equal(0, bufexists('someName'))
1656 let buf = bufadd('someName')
1657 call assert_notequal(0, buf)
1658 call assert_equal(1, bufexists('someName'))
1659 call assert_equal(0, getbufvar(buf, '&buflisted'))
1660 call assert_equal(0, bufloaded(buf))
1661 call bufload(buf)
1662 call assert_equal(1, bufloaded(buf))
1663 call assert_equal([''], getbufline(buf, 1, '$'))
1664
1665 let curbuf = bufnr('')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001666 eval ['some', 'text']->writefile('XotherName')
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001667 let buf = 'XotherName'->bufadd()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001668 call assert_notequal(0, buf)
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001669 eval 'XotherName'->bufexists()->assert_equal(1)
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001670 call assert_equal(0, getbufvar(buf, '&buflisted'))
1671 call assert_equal(0, bufloaded(buf))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001672 eval buf->bufload()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001673 call assert_equal(1, bufloaded(buf))
1674 call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
1675 call assert_equal(curbuf, bufnr(''))
1676
Bram Moolenaar892ae722019-06-30 20:33:01 +02001677 let buf1 = bufadd('')
1678 let buf2 = bufadd('')
1679 call assert_notequal(0, buf1)
1680 call assert_notequal(0, buf2)
1681 call assert_notequal(buf1, buf2)
1682 call assert_equal(1, bufexists(buf1))
1683 call assert_equal(1, bufexists(buf2))
1684 call assert_equal(0, bufloaded(buf1))
1685 exe 'bwipe ' .. buf1
1686 call assert_equal(0, bufexists(buf1))
1687 call assert_equal(1, bufexists(buf2))
1688 exe 'bwipe ' .. buf2
1689 call assert_equal(0, bufexists(buf2))
1690
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001691 bwipe someName
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001692 bwipe XotherName
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001693 call assert_equal(0, bufexists('someName'))
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001694 call delete('XotherName')
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001695endfunc
Bram Moolenaarc2585492019-09-22 21:29:53 +02001696
1697func Test_state()
1698 CheckRunVimInTerminal
1699
1700 let lines =<< trim END
1701 call setline(1, ['one', 'two', 'three'])
1702 map ;; gg
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001703 set complete=.
Bram Moolenaarc2585492019-09-22 21:29:53 +02001704 func RunTimer()
1705 call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
1706 endfunc
1707 au Filetype foobar let g:state = state()|let g:mode = mode()
1708 END
1709 call writefile(lines, 'XState')
1710 let buf = RunVimInTerminal('-S XState', #{rows: 6})
1711
1712 " Using a ":" command Vim is busy, thus "S" is returned
1713 call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
1714 call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
1715 call term_sendkeys(buf, ":\<CR>")
1716
1717 " Using a timer callback
1718 call term_sendkeys(buf, ":call RunTimer()\<CR>")
1719 call term_wait(buf, 50)
1720 let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"
1721 call term_sendkeys(buf, getstate)
1722 call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)
1723
1724 " Halfway a mapping
1725 call term_sendkeys(buf, ":call RunTimer()\<CR>;")
1726 call term_wait(buf, 50)
1727 call term_sendkeys(buf, ";")
1728 call term_sendkeys(buf, getstate)
1729 call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)
1730
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001731 " Insert mode completion (bit slower on Mac)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001732 call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001733 call term_wait(buf, 200)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001734 call term_sendkeys(buf, "\<Esc>")
1735 call term_sendkeys(buf, getstate)
1736 call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)
1737
1738 " Autocommand executing
1739 call term_sendkeys(buf, ":set filetype=foobar\<CR>")
1740 call term_wait(buf, 50)
1741 call term_sendkeys(buf, getstate)
1742 call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)
1743
1744 " Todo: "w" - waiting for ch_evalexpr()
1745
1746 " messages scrolled
1747 call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
1748 call term_wait(buf, 50)
1749 call term_sendkeys(buf, "\<CR>")
1750 call term_sendkeys(buf, getstate)
1751 call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)
1752
1753 call StopVimInTerminal(buf)
1754 call delete('XState')
1755endfunc
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001756
1757func Test_range()
1758 " destructuring
1759 let [x, y] = range(2)
1760 call assert_equal([0, 1], [x, y])
1761
1762 " index
1763 call assert_equal(4, range(1, 10)[3])
1764
1765 " add()
1766 call assert_equal([0, 1, 2, 3], add(range(3), 3))
1767 call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3)))
1768 call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3)))
1769
1770 " append()
1771 new
1772 call append('.', range(5))
1773 call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$'))
1774 bwipe!
1775
1776 " appendbufline()
1777 new
1778 call appendbufline(bufnr(''), '.', range(5))
1779 call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$'))
1780 bwipe!
1781
1782 " call()
1783 func TwoArgs(a, b)
1784 return [a:a, a:b]
1785 endfunc
1786 call assert_equal([0, 1], call('TwoArgs', range(2)))
1787
1788 " col()
1789 new
1790 call setline(1, ['foo', 'bar'])
1791 call assert_equal(2, col(range(1, 2)))
1792 bwipe!
1793
1794 " complete()
1795 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>"
1796 " complete_info()
1797 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>"
1798
1799 " copy()
1800 call assert_equal([1, 2, 3], copy(range(1, 3)))
1801
1802 " count()
1803 call assert_equal(0, count(range(0), 3))
1804 call assert_equal(0, count(range(2), 3))
1805 call assert_equal(1, count(range(5), 3))
1806
1807 " cursor()
1808 new
1809 call setline(1, ['aaa', 'bbb', 'ccc'])
1810 call cursor(range(1, 2))
1811 call assert_equal([2, 1], [col('.'), line('.')])
1812 bwipe!
1813
1814 " deepcopy()
1815 call assert_equal([1, 2, 3], deepcopy(range(1, 3)))
1816
1817 " empty()
1818 call assert_true(empty(range(0)))
1819 call assert_false(empty(range(2)))
1820
1821 " execute()
1822 new
1823 call setline(1, ['aaa', 'bbb', 'ccc'])
1824 call execute(range(3))
1825 call assert_equal(2, line('.'))
1826 bwipe!
1827
1828 " extend()
1829 call assert_equal([1, 2, 3, 4], extend([1], range(2, 4)))
1830 call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4)))
1831 call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4]))
1832
1833 " filter()
1834 call assert_equal([1, 3], filter(range(5), 'v:val % 2'))
1835
1836 " funcref()
1837 call assert_equal([0, 1], funcref('TwoArgs', range(2))())
1838
1839 " function()
1840 call assert_equal([0, 1], function('TwoArgs', range(2))())
1841
1842 " garbagecollect()
1843 let thelist = [1, range(2), 3]
1844 let otherlist = range(3)
1845 call test_garbagecollect_now()
1846
1847 " get()
1848 call assert_equal(4, get(range(1, 10), 3))
1849 call assert_equal(-1, get(range(1, 10), 42, -1))
1850
1851 " index()
1852 call assert_equal(1, index(range(1, 5), 2))
1853
1854 " inputlist()
Bram Moolenaar272ca952020-01-28 20:49:11 +01001855 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x')
1856 call assert_equal(1, result)
1857 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x')
1858 call assert_equal(1, result)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001859
1860 " insert()
1861 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42))
1862 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0))
1863 call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1))
1864 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4))
1865 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1))
1866 call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5))
1867
1868 " join()
1869 call assert_equal('0 1 2 3 4', join(range(5)))
1870
Bram Moolenaar272ca952020-01-28 20:49:11 +01001871 " json_encode()
1872 call assert_equal('[0,1,2,3]', json_encode(range(4)))
1873
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001874 " len()
1875 call assert_equal(0, len(range(0)))
1876 call assert_equal(2, len(range(2)))
1877 call assert_equal(5, len(range(0, 12, 3)))
1878 call assert_equal(4, len(range(3, 0, -1)))
1879
1880 " list2str()
1881 call assert_equal('ABC', list2str(range(65, 67)))
1882
1883 " lock()
1884 let thelist = range(5)
1885 lockvar thelist
1886
1887 " map()
1888 call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2'))
1889
1890 " match()
1891 call assert_equal(3, match(range(5), 3))
1892
1893 " matchaddpos()
1894 highlight MyGreenGroup ctermbg=green guibg=green
1895 call matchaddpos('MyGreenGroup', range(line('.'), line('.')))
1896
1897 " matchend()
1898 call assert_equal(4, matchend(range(5), '4'))
1899 call assert_equal(3, matchend(range(1, 5), '4'))
1900 call assert_equal(-1, matchend(range(1, 5), '42'))
1901
1902 " matchstrpos()
1903 call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4'))
1904 call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4'))
1905 call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42'))
1906
1907 " max() reverse()
1908 call assert_equal(0, max(range(0)))
1909 call assert_equal(0, max(range(10, 9)))
1910 call assert_equal(9, max(range(10)))
1911 call assert_equal(18, max(range(0, 20, 3)))
1912 call assert_equal(20, max(range(20, 0, -3)))
1913 call assert_equal(99999, max(range(100000)))
1914 call assert_equal(99999, max(range(99999, 0, -1)))
1915 call assert_equal(99999, max(reverse(range(100000))))
1916 call assert_equal(99999, max(reverse(range(99999, 0, -1))))
1917
1918 " min() reverse()
1919 call assert_equal(0, min(range(0)))
1920 call assert_equal(0, min(range(10, 9)))
1921 call assert_equal(5, min(range(5, 10)))
1922 call assert_equal(5, min(range(5, 10, 3)))
1923 call assert_equal(2, min(range(20, 0, -3)))
1924 call assert_equal(0, min(range(100000)))
1925 call assert_equal(0, min(range(99999, 0, -1)))
1926 call assert_equal(0, min(reverse(range(100000))))
1927 call assert_equal(0, min(reverse(range(99999, 0, -1))))
1928
1929 " remove()
1930 call assert_equal(1, remove(range(1, 10), 0))
1931 call assert_equal(2, remove(range(1, 10), 1))
1932 call assert_equal(9, remove(range(1, 10), 8))
1933 call assert_equal(10, remove(range(1, 10), 9))
1934 call assert_equal(10, remove(range(1, 10), -1))
1935 call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4))
1936
1937 " repeat()
1938 call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2))
1939 call assert_equal([0, 1, 2], repeat(range(3), 1))
1940 call assert_equal([], repeat(range(3), 0))
1941 call assert_equal([], repeat(range(5, 4), 2))
1942 call assert_equal([], repeat(range(5, 4), 0))
1943
1944 " reverse()
1945 call assert_equal([2, 1, 0], reverse(range(3)))
1946 call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1)))
1947 call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10)))
1948 call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20)))
1949 call assert_equal([16, 13, 10], reverse(range(10, 18, 3)))
1950 call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3)))
1951 call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3)))
1952 call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3)))
1953 call assert_equal([], reverse(range(0)))
1954
1955 " TODO: setpos()
1956 " new
1957 " call setline(1, repeat([''], bufnr('')))
1958 " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6))
1959 " call setpos('x', range(bufnr(''), bufnr('') + 3))
1960 " bwipe!
1961
1962 " setreg()
1963 call setreg('a', range(3))
1964 call assert_equal("0\n1\n2\n", getreg('a'))
1965
Bram Moolenaarb0992022020-01-30 14:55:42 +01001966 " settagstack()
1967 call settagstack(1, #{items : range(4)})
Bram Moolenaar94255df2020-02-05 20:10:33 +01001968
Bram Moolenaarb0992022020-01-30 14:55:42 +01001969 " sign_define()
1970 call assert_fails("call sign_define(range(5))", "E715:")
1971 call assert_fails("call sign_placelist(range(5))", "E715:")
1972
1973 " sign_undefine()
1974 call assert_fails("call sign_undefine(range(5))", "E908:")
1975
1976 " sign_unplacelist()
1977 call assert_fails("call sign_unplacelist(range(5))", "E715:")
1978
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001979 " sort()
1980 call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1)))
1981
Bram Moolenaarb0992022020-01-30 14:55:42 +01001982 " 'spellsuggest'
1983 func MySuggest()
1984 return range(3)
1985 endfunc
1986 set spell spellsuggest=expr:MySuggest()
1987 call assert_equal([], spellsuggest('baord', 3))
1988 set nospell spellsuggest&
1989
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001990 " string()
1991 call assert_equal('[0, 1, 2, 3, 4]', string(range(5)))
1992
Bram Moolenaarb0992022020-01-30 14:55:42 +01001993 " taglist() with 'tagfunc'
1994 func TagFunc(pattern, flags, info)
1995 return range(10)
1996 endfunc
1997 set tagfunc=TagFunc
1998 call assert_fails("call taglist('asdf')", 'E987:')
1999 set tagfunc=
Bram Moolenaar94255df2020-02-05 20:10:33 +01002000
Bram Moolenaarb0992022020-01-30 14:55:42 +01002001 " term_start()
Bram Moolenaar705724e2020-01-31 21:13:42 +01002002 if has('terminal') && has('termguicolors')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002003 call assert_fails('call term_start(range(3, 4))', 'E474:')
2004 let g:terminal_ansi_colors = range(16)
Bram Moolenaar94255df2020-02-05 20:10:33 +01002005 if has('win32')
2006 let cmd = "cmd /c dir"
2007 else
2008 let cmd = "ls"
2009 endif
2010 call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"})', 'E475:')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002011 unlet g:terminal_ansi_colors
2012 endif
2013
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002014 " type()
2015 call assert_equal(v:t_list, type(range(5)))
2016
2017 " uniq()
2018 call assert_equal([0, 1, 2, 3, 4], uniq(range(5)))
2019endfunc