blob: b25d98861608cdeb4fbc87ddc6ebf401e601bc72 [file] [log] [blame]
Bram Moolenaar08243d22017-01-10 16:12:29 +01001" Tests for various functions.
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02002source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02003source check.vim
Bram Moolenaarc2585492019-09-22 21:29:53 +02004source term_util.vim
Bram Moolenaar4132eb52020-02-14 16:53:00 +01005source screendump.vim
Bram Moolenaar08243d22017-01-10 16:12:29 +01006
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +01007" Must be done first, since the alternate buffer must be unset.
8func Test_00_bufexists()
9 call assert_equal(0, bufexists('does_not_exist'))
10 call assert_equal(1, bufexists(bufnr('%')))
11 call assert_equal(0, bufexists(0))
12 new Xfoo
13 let bn = bufnr('%')
14 call assert_equal(1, bufexists(bn))
15 call assert_equal(1, bufexists('Xfoo'))
16 call assert_equal(1, bufexists(getcwd() . '/Xfoo'))
17 call assert_equal(1, bufexists(0))
18 bw
19 call assert_equal(0, bufexists(bn))
20 call assert_equal(0, bufexists('Xfoo'))
21endfunc
22
Bram Moolenaar79296512020-03-22 16:17:14 +010023func Test_has()
24 call assert_equal(1, has('eval'))
25 call assert_equal(1, has('eval', 1))
26
Bram Moolenaar0e05de42020-03-25 22:23:46 +010027 if has('unix')
28 call assert_equal(1, or(has('ttyin'), 1))
29 call assert_equal(0, and(has('ttyout'), 0))
30 call assert_equal(1, has('multi_byte_encoding'))
31 endif
Bram Moolenaar99fa7212020-04-26 15:59:55 +020032 call assert_equal(1, has('vcon', 1))
33 call assert_equal(1, has('mouse_gpm_enabled', 1))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010034
Bram Moolenaar79296512020-03-22 16:17:14 +010035 call assert_equal(0, has('nonexistent'))
36 call assert_equal(0, has('nonexistent', 1))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010037
38 " Will we ever have patch 9999?
39 let ver = 'patch-' .. v:version / 100 .. '.' .. v:version % 100 .. '.9999'
40 call assert_equal(0, has(ver))
Bram Moolenaar79296512020-03-22 16:17:14 +010041endfunc
42
Bram Moolenaar24c2e482017-01-29 15:45:12 +010043func Test_empty()
44 call assert_equal(1, empty(''))
45 call assert_equal(0, empty('a'))
46
47 call assert_equal(1, empty(0))
48 call assert_equal(1, empty(-0))
49 call assert_equal(0, empty(1))
50 call assert_equal(0, empty(-1))
51
Bram Moolenaar5feabe02020-01-30 18:24:53 +010052 if has('float')
53 call assert_equal(1, empty(0.0))
54 call assert_equal(1, empty(-0.0))
55 call assert_equal(0, empty(1.0))
56 call assert_equal(0, empty(-1.0))
57 call assert_equal(0, empty(1.0/0.0))
58 call assert_equal(0, empty(0.0/0.0))
59 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +010060
61 call assert_equal(1, empty([]))
62 call assert_equal(0, empty(['a']))
63
64 call assert_equal(1, empty({}))
65 call assert_equal(0, empty({'a':1}))
66
67 call assert_equal(1, empty(v:null))
68 call assert_equal(1, empty(v:none))
69 call assert_equal(1, empty(v:false))
70 call assert_equal(0, empty(v:true))
71
Bram Moolenaar41042f32017-03-09 12:09:32 +010072 if has('channel')
73 call assert_equal(1, empty(test_null_channel()))
74 endif
75 if has('job')
76 call assert_equal(1, empty(test_null_job()))
77 endif
78
Bram Moolenaar24c2e482017-01-29 15:45:12 +010079 call assert_equal(0, empty(function('Test_empty')))
Bram Moolenaar17aca702019-05-16 22:24:55 +020080 call assert_equal(0, empty(function('Test_empty', [0])))
Bram Moolenaar7c215c52020-02-29 13:43:27 +010081
82 call assert_fails("call empty(test_void())", 'E685:')
83 call assert_fails("call empty(test_unknown())", 'E685:')
Bram Moolenaar24c2e482017-01-29 15:45:12 +010084endfunc
85
Bram Moolenaardd589232020-02-29 17:38:12 +010086func Test_test_void()
87 call assert_fails('echo 1 == test_void()', 'E685:')
88 if has('float')
89 call assert_fails('echo 1.0 == test_void()', 'E685:')
90 endif
91 call assert_fails('let x = json_encode(test_void())', 'E685:')
92 call assert_fails('let x = copy(test_void())', 'E685:')
93 call assert_fails('let x = copy([test_void()])', 'E685:')
94endfunc
95
Bram Moolenaar24c2e482017-01-29 15:45:12 +010096func Test_len()
97 call assert_equal(1, len(0))
98 call assert_equal(2, len(12))
99
100 call assert_equal(0, len(''))
101 call assert_equal(2, len('ab'))
102
103 call assert_equal(0, len([]))
Bram Moolenaar08f41572020-04-20 16:50:00 +0200104 call assert_equal(0, len(test_null_list()))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100105 call assert_equal(2, len([2, 1]))
106
107 call assert_equal(0, len({}))
Bram Moolenaar08f41572020-04-20 16:50:00 +0200108 call assert_equal(0, len(test_null_dict()))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100109 call assert_equal(2, len({'a': 1, 'b': 2}))
110
111 call assert_fails('call len(v:none)', 'E701:')
112 call assert_fails('call len({-> 0})', 'E701:')
113endfunc
114
115func Test_max()
116 call assert_equal(0, max([]))
117 call assert_equal(2, max([2]))
118 call assert_equal(2, max([1, 2]))
119 call assert_equal(2, max([1, 2, v:null]))
120
121 call assert_equal(0, max({}))
122 call assert_equal(2, max({'a':1, 'b':2}))
123
124 call assert_fails('call max(1)', 'E712:')
125 call assert_fails('call max(v:none)', 'E712:')
126endfunc
127
128func Test_min()
129 call assert_equal(0, min([]))
130 call assert_equal(2, min([2]))
131 call assert_equal(1, min([1, 2]))
132 call assert_equal(0, min([1, 2, v:null]))
133
134 call assert_equal(0, min({}))
135 call assert_equal(1, min({'a':1, 'b':2}))
136
137 call assert_fails('call min(1)', 'E712:')
138 call assert_fails('call min(v:none)', 'E712:')
139endfunc
140
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200141func Test_strwidth()
142 for aw in ['single', 'double']
143 exe 'set ambiwidth=' . aw
144 call assert_equal(0, strwidth(''))
145 call assert_equal(1, strwidth("\t"))
146 call assert_equal(3, strwidth('Vim'))
147 call assert_equal(4, strwidth(1234))
148 call assert_equal(5, strwidth(-1234))
149
Bram Moolenaar30276f22019-01-24 17:59:39 +0100150 call assert_equal(2, strwidth('😉'))
151 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
152 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200153
154 call assert_fails('call strwidth({->0})', 'E729:')
155 call assert_fails('call strwidth([])', 'E730:')
156 call assert_fails('call strwidth({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100157 if has('float')
158 call assert_fails('call strwidth(1.2)', 'E806:')
159 endif
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200160 endfor
161
162 set ambiwidth&
163endfunc
164
Bram Moolenaar08243d22017-01-10 16:12:29 +0100165func Test_str2nr()
166 call assert_equal(0, str2nr(''))
167 call assert_equal(1, str2nr('1'))
168 call assert_equal(1, str2nr(' 1 '))
169
170 call assert_equal(1, str2nr('+1'))
171 call assert_equal(1, str2nr('+ 1'))
172 call assert_equal(1, str2nr(' + 1 '))
173
174 call assert_equal(-1, str2nr('-1'))
175 call assert_equal(-1, str2nr('- 1'))
176 call assert_equal(-1, str2nr(' - 1 '))
177
178 call assert_equal(123456789, str2nr('123456789'))
179 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100180
181 call assert_equal(5, str2nr('101', 2))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200182 call assert_equal(5, '0b101'->str2nr(2))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100183 call assert_equal(5, str2nr('0B101', 2))
184 call assert_equal(-5, str2nr('-101', 2))
185 call assert_equal(-5, str2nr('-0b101', 2))
186 call assert_equal(-5, str2nr('-0B101', 2))
187
188 call assert_equal(65, str2nr('101', 8))
189 call assert_equal(65, str2nr('0101', 8))
190 call assert_equal(-65, str2nr('-101', 8))
191 call assert_equal(-65, str2nr('-0101', 8))
192
193 call assert_equal(11259375, str2nr('abcdef', 16))
194 call assert_equal(11259375, str2nr('ABCDEF', 16))
195 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
196 call assert_equal(11259375, str2nr('0xabcdef', 16))
197 call assert_equal(11259375, str2nr('0Xabcdef', 16))
198 call assert_equal(11259375, str2nr('0XABCDEF', 16))
199 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
200
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200201 call assert_equal(1, str2nr("1'000'000", 10, 0))
202 call assert_equal(256, str2nr("1'0000'0000", 2, 1))
203 call assert_equal(262144, str2nr("1'000'000", 8, 1))
204 call assert_equal(1000000, str2nr("1'000'000", 10, 1))
Bram Moolenaarea8dcf82019-09-15 21:12:22 +0200205 call assert_equal(1000, str2nr("1'000''000", 10, 1))
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200206 call assert_equal(65536, str2nr("1'00'00", 16, 1))
207
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100208 call assert_equal(0, str2nr('0x10'))
209 call assert_equal(0, str2nr('0b10'))
210 call assert_equal(1, str2nr('12', 2))
211 call assert_equal(1, str2nr('18', 8))
212 call assert_equal(1, str2nr('1g', 16))
213
214 call assert_equal(0, str2nr(v:null))
215 call assert_equal(0, str2nr(v:none))
216
217 call assert_fails('call str2nr([])', 'E730:')
218 call assert_fails('call str2nr({->2})', 'E729:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100219 if has('float')
220 call assert_fails('call str2nr(1.2)', 'E806:')
221 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100222 call assert_fails('call str2nr(10, [])', 'E474:')
223endfunc
224
225func Test_strftime()
Bram Moolenaar10455d42019-11-21 15:36:18 +0100226 CheckFunction strftime
227
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100228 " Format of strftime() depends on system. We assume
229 " that basic formats tested here are available and
230 " identical on all systems which support strftime().
231 "
232 " The 2nd parameter of strftime() is a local time, so the output day
233 " of strftime() can be 17 or 18, depending on timezone.
234 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
235 "
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200236 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 +0100237
238 call assert_fails('call strftime([])', 'E730:')
239 call assert_fails('call strftime("%Y", [])', 'E745:')
Bram Moolenaardb517302019-06-18 22:53:24 +0200240
241 " Check that the time changes after we change the timezone
242 " Save previous timezone value, if any
243 if exists('$TZ')
244 let tz = $TZ
245 endif
246
247 " Force EST and then UTC, save the current hour (24-hour clock) for each
248 let $TZ = 'EST' | let est = strftime('%H')
249 let $TZ = 'UTC' | let utc = strftime('%H')
250
251 " Those hours should be two bytes long, and should not be the same; if they
252 " are, a tzset(3) call may have failed somewhere
253 call assert_equal(strlen(est), 2)
254 call assert_equal(strlen(utc), 2)
Bram Moolenaar87652a72019-06-18 23:07:37 +0200255 " TODO: this fails on MS-Windows
256 if has('unix')
257 call assert_notequal(est, utc)
258 endif
Bram Moolenaardb517302019-06-18 22:53:24 +0200259
260 " If we cached a timezone value, put it back, otherwise clear it
261 if exists('tz')
262 let $TZ = tz
263 else
264 unlet $TZ
265 endif
Bram Moolenaar10455d42019-11-21 15:36:18 +0100266endfunc
Bram Moolenaardb517302019-06-18 22:53:24 +0200267
Bram Moolenaar10455d42019-11-21 15:36:18 +0100268func Test_strptime()
269 CheckFunction strptime
270
271 if exists('$TZ')
272 let tz = $TZ
273 endif
274 let $TZ = 'UTC'
275
Bram Moolenaar9a838fe2019-12-06 12:45:01 +0100276 call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
Bram Moolenaar10455d42019-11-21 15:36:18 +0100277
278 call assert_fails('call strptime()', 'E119:')
279 call assert_fails('call strptime("xxx")', 'E119:')
280 call assert_equal(0, strptime("%Y", ''))
281 call assert_equal(0, strptime("%Y", "xxx"))
282
283 if exists('tz')
284 let $TZ = tz
285 else
286 unlet $TZ
287 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100288endfunc
289
Bram Moolenaardce1e892019-02-10 23:18:53 +0100290func Test_resolve_unix()
Bram Moolenaar26109902018-10-06 15:43:17 +0200291 if !has('unix')
292 return
293 endif
294
295 " Xlink1 -> Xlink2
296 " Xlink2 -> Xlink3
297 silent !ln -s -f Xlink2 Xlink1
298 silent !ln -s -f Xlink3 Xlink2
299 call assert_equal('Xlink3', resolve('Xlink1'))
300 call assert_equal('./Xlink3', resolve('./Xlink1'))
301 call assert_equal('Xlink3/', resolve('Xlink2/'))
302 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
303 "call assert_equal('Xlink3/', resolve('Xlink1/'))
304 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
305 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
306 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
307
308 " Test resolve() with a symlink cycle.
309 " Xlink1 -> Xlink2
310 " Xlink2 -> Xlink3
311 " Xlink3 -> Xlink1
312 silent !ln -s -f Xlink1 Xlink3
313 call assert_fails('call resolve("Xlink1")', 'E655:')
314 call assert_fails('call resolve("./Xlink1")', 'E655:')
315 call assert_fails('call resolve("Xlink2")', 'E655:')
316 call assert_fails('call resolve("Xlink3")', 'E655:')
317 call delete('Xlink1')
318 call delete('Xlink2')
319 call delete('Xlink3')
320
321 silent !ln -s -f Xdir//Xfile Xlink
322 call assert_equal('Xdir/Xfile', resolve('Xlink'))
323 call delete('Xlink')
324
325 silent !ln -s -f Xlink2/ Xlink1
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200326 call assert_equal('Xlink2', 'Xlink1'->resolve())
Bram Moolenaar26109902018-10-06 15:43:17 +0200327 call assert_equal('Xlink2/', resolve('Xlink1/'))
328 call delete('Xlink1')
329
330 silent !ln -s -f ./Xlink2 Xlink1
331 call assert_equal('Xlink2', resolve('Xlink1'))
332 call assert_equal('./Xlink2', resolve('./Xlink1'))
333 call delete('Xlink1')
334endfunc
335
Bram Moolenaardce1e892019-02-10 23:18:53 +0100336func s:normalize_fname(fname)
337 let ret = substitute(a:fname, '\', '/', 'g')
338 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200339 return ret->tolower()
Bram Moolenaardce1e892019-02-10 23:18:53 +0100340endfunc
341
342func Test_resolve_win32()
343 if !has('win32')
344 return
345 endif
346
347 " test for shortcut file
348 if executable('cscript')
349 new Xfile
350 wq
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200351 let lines =<< trim END
352 Set fs = CreateObject("Scripting.FileSystemObject")
353 Set ws = WScript.CreateObject("WScript.Shell")
354 Set shortcut = ws.CreateShortcut("Xlink.lnk")
355 shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")
356 shortcut.Save
357 END
358 call writefile(lines, 'link.vbs')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100359 silent !cscript link.vbs
360 call delete('link.vbs')
361 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
362 call delete('Xfile')
363
364 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
365 call delete('Xlink.lnk')
366 else
367 echomsg 'skipped test for shortcut file'
368 endif
369
370 " remove files
371 call delete('Xlink')
372 call delete('Xdir', 'd')
373 call delete('Xfile')
374
375 " test for symbolic link to a file
376 new Xfile
377 wq
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200378 call assert_equal('Xfile', resolve('Xfile'))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100379 silent !mklink Xlink Xfile
380 if !v:shell_error
381 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink')))
382 call delete('Xlink')
383 else
384 echomsg 'skipped test for symbolic link to a file'
385 endif
386 call delete('Xfile')
387
388 " test for junction to a directory
389 call mkdir('Xdir')
390 silent !mklink /J Xlink Xdir
391 if !v:shell_error
392 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
393
394 call delete('Xdir', 'd')
395
396 " test for junction already removed
397 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
398 call delete('Xlink')
399 else
400 echomsg 'skipped test for junction to a directory'
401 call delete('Xdir', 'd')
402 endif
403
404 " test for symbolic link to a directory
405 call mkdir('Xdir')
406 silent !mklink /D Xlink Xdir
407 if !v:shell_error
408 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
409
410 call delete('Xdir', 'd')
411
412 " test for symbolic link already removed
413 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
414 call delete('Xlink')
415 else
416 echomsg 'skipped test for symbolic link to a directory'
417 call delete('Xdir', 'd')
418 endif
419
420 " test for buffer name
421 new Xfile
422 wq
423 silent !mklink Xlink Xfile
424 if !v:shell_error
425 edit Xlink
426 call assert_equal('Xlink', bufname('%'))
427 call delete('Xlink')
428 bw!
429 else
430 echomsg 'skipped test for buffer name'
431 endif
432 call delete('Xfile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200433
434 " test for reparse point
435 call mkdir('Xdir')
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200436 call assert_equal('Xdir', resolve('Xdir'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200437 silent !mklink /D Xdirlink Xdir
438 if !v:shell_error
439 w Xdir/text.txt
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200440 call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200441 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
442 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200443 call delete('Xdirlink')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200444 else
445 echomsg 'skipped test for reparse point'
446 endif
447
448 call delete('Xdir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100449endfunc
450
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100451func Test_simplify()
452 call assert_equal('', simplify(''))
453 call assert_equal('/', simplify('/'))
454 call assert_equal('/', simplify('/.'))
455 call assert_equal('/', simplify('/..'))
456 call assert_equal('/...', simplify('/...'))
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200457 call assert_equal('./dir/file', './dir/file'->simplify())
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100458 call assert_equal('./dir/file', simplify('.///dir//file'))
459 call assert_equal('./dir/file', simplify('./dir/./file'))
460 call assert_equal('./file', simplify('./dir/../file'))
461 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
462 call assert_equal('./file', simplify('dir/.././file'))
463
464 call assert_fails('call simplify({->0})', 'E729:')
465 call assert_fails('call simplify([])', 'E730:')
466 call assert_fails('call simplify({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100467 if has('float')
468 call assert_fails('call simplify(1.2)', 'E806:')
469 endif
Bram Moolenaar08243d22017-01-10 16:12:29 +0100470endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100471
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200472func Test_pathshorten()
473 call assert_equal('', pathshorten(''))
474 call assert_equal('foo', pathshorten('foo'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200475 call assert_equal('/foo', '/foo'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200476 call assert_equal('f/', pathshorten('foo/'))
477 call assert_equal('f/bar', pathshorten('foo/bar'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200478 call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200479 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
480 call assert_equal('.f/bar', pathshorten('.foo/bar'))
481 call assert_equal('~f/bar', pathshorten('~foo/bar'))
482 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
483 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
484 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200485 call assert_fails('call pathshorten([])', 'E730:')
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200486endfunc
487
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100488func Test_strpart()
489 call assert_equal('de', strpart('abcdefg', 3, 2))
490 call assert_equal('ab', strpart('abcdefg', -2, 4))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200491 call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100492 call assert_equal('fg', strpart('abcdefg', 5, 4))
493 call assert_equal('defg', strpart('abcdefg', 3))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100494 call assert_equal('', strpart('abcdefg', 10))
495 call assert_fails("let s=strpart('abcdef', [])", 'E745:')
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100496
Bram Moolenaar30276f22019-01-24 17:59:39 +0100497 call assert_equal('lép', strpart('éléphant', 2, 4))
498 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100499endfunc
500
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100501func Test_tolower()
502 call assert_equal("", tolower(""))
503
504 " Test with all printable ASCII characters.
505 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
506 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
507
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100508 " Test with a few uppercase diacritics.
509 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
510 call assert_equal("bḃḇ", tolower("BḂḆ"))
511 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
512 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
513 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
514 call assert_equal("fḟ ", tolower("FḞ "))
515 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
516 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
517 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
518 call assert_equal("jĵ", tolower("JĴ"))
519 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
520 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
521 call assert_equal("mḿṁ", tolower("MḾṀ"))
522 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
523 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
524 call assert_equal("pṕṗ", tolower("PṔṖ"))
525 call assert_equal("q", tolower("Q"))
526 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
527 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
528 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
529 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
530 call assert_equal("vṽ", tolower("VṼ"))
531 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
532 call assert_equal("xẋẍ", tolower("XẊẌ"))
533 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
534 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
535
536 " Test with a few lowercase diacritics, which should remain unchanged.
537 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
538 call assert_equal("bḃḇ", tolower("bḃḇ"))
539 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
540 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
541 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
542 call assert_equal("fḟ", tolower("fḟ"))
543 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
544 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
545 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
546 call assert_equal("jĵǰ", tolower("jĵǰ"))
547 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
548 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
549 call assert_equal("mḿṁ ", tolower("mḿṁ "))
550 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
551 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
552 call assert_equal("pṕṗ", tolower("pṕṗ"))
553 call assert_equal("q", tolower("q"))
554 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
555 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
556 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
557 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
558 call assert_equal("vṽ", tolower("vṽ"))
559 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
560 call assert_equal("ẋẍ", tolower("ẋẍ"))
561 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
562 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
563
564 " According to https://twitter.com/jifa/status/625776454479970304
565 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
566 " in length (2 to 3 bytes) when lowercased. So let's test them.
567 call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100568
569 " This call to tolower with invalid utf8 sequence used to cause access to
570 " invalid memory.
571 call tolower("\xC0\x80\xC0")
572 call tolower("123\xC0\x80\xC0")
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200573
574 " Test in latin1 encoding
575 let save_enc = &encoding
576 set encoding=latin1
577 call assert_equal("abc", tolower("ABC"))
578 let &encoding = save_enc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100579endfunc
580
581func Test_toupper()
582 call assert_equal("", toupper(""))
583
584 " Test with all printable ASCII characters.
585 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
586 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
587
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100588 " Test with a few lowercase diacritics.
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200589 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100590 call assert_equal("BḂḆ", toupper("bḃḇ"))
591 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
592 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
593 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
594 call assert_equal("FḞ", toupper("fḟ"))
595 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
596 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
597 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
598 call assert_equal("JĴǰ", toupper("jĵǰ"))
599 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
600 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
601 call assert_equal("MḾṀ ", toupper("mḿṁ "))
602 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
603 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
604 call assert_equal("PṔṖ", toupper("pṕṗ"))
605 call assert_equal("Q", toupper("q"))
606 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
607 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
608 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
609 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
610 call assert_equal("VṼ", toupper("vṽ"))
611 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
612 call assert_equal("ẊẌ", toupper("ẋẍ"))
613 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
614 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
615
616 " Test that uppercase diacritics, which should remain unchanged.
617 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
618 call assert_equal("BḂḆ", toupper("BḂḆ"))
619 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
620 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
621 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
622 call assert_equal("FḞ ", toupper("FḞ "))
623 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
624 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
625 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
626 call assert_equal("JĴ", toupper("JĴ"))
627 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
628 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
629 call assert_equal("MḾṀ", toupper("MḾṀ"))
630 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
631 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
632 call assert_equal("PṔṖ", toupper("PṔṖ"))
633 call assert_equal("Q", toupper("Q"))
634 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
635 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
636 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
637 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
638 call assert_equal("VṼ", toupper("VṼ"))
639 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
640 call assert_equal("XẊẌ", toupper("XẊẌ"))
641 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
642 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
643
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100644 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100645
646 " This call to toupper with invalid utf8 sequence used to cause access to
647 " invalid memory.
648 call toupper("\xC0\x80\xC0")
649 call toupper("123\xC0\x80\xC0")
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200650
651 " Test in latin1 encoding
652 let save_enc = &encoding
653 set encoding=latin1
654 call assert_equal("ABC", toupper("abc"))
655 let &encoding = save_enc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100656endfunc
657
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200658func Test_tr()
659 call assert_equal('foo', tr('bar', 'bar', 'foo'))
660 call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100661 call assert_fails("let s=tr([], 'abc', 'def')", 'E730:')
662 call assert_fails("let s=tr('abc', [], 'def')", 'E730:')
663 call assert_fails("let s=tr('abc', 'abc', [])", 'E730:')
664 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
665 set encoding=latin1
666 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
667 call assert_equal('hEllO', tr('hello', 'eo', 'EO'))
668 call assert_equal('hello', tr('hello', 'xy', 'ab'))
669 set encoding=utf8
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200670endfunc
671
Bram Moolenaare90858d2017-02-01 17:24:34 +0100672" Tests for the mode() function
673let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100674func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100675 let g:current_modes = mode(0) . '-' . mode(1)
676 return ''
677endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100678
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200679" Test for the mode() function
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100680func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100681 new
682 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
683
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100684 " Only complete from the current buffer.
685 set complete=.
686
Bram Moolenaare90858d2017-02-01 17:24:34 +0100687 inoremap <F2> <C-R>=Save_mode()<CR>
688
689 normal! 3G
690 exe "normal i\<F2>\<Esc>"
691 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100692 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100693 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
694 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100695 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100696 exe "normal iBro\<C-P>\<F2>\<Esc>u"
697 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100698 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100699 exe "normal iBa\<C-X>\<F2>\<Esc>u"
700 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100701 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100702 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
703 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100704 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100705 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
706 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100707 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100708 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
709 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100710 " i_CTRL-X CTRL-L: Multiple matches
711 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
712 call assert_equal('i-ic', g:current_modes)
713 " i_CTRL-X CTRL-L: Single match
714 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
715 call assert_equal('i-ic', g:current_modes)
716 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100717 exe "normal iCom\<C-P>\<F2>\<Esc>u"
718 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100719 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100720 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
721 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100722 " i_CTRL-X CTRL-L: No match
723 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
724 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100725
Bram Moolenaare971df32017-02-05 14:15:29 +0100726 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100727 exe "normal RBa\<C-P>\<F2>\<Esc>u"
728 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100729 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100730 exe "normal RBro\<C-P>\<F2>\<Esc>u"
731 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100732 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100733 exe "normal RBa\<C-X>\<F2>\<Esc>u"
734 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100735 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100736 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
737 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100738 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100739 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
740 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100741 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100742 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
743 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100744 " R_CTRL-X CTRL-L: Multiple matches
745 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
746 call assert_equal('R-Rc', g:current_modes)
747 " R_CTRL-X CTRL-L: Single match
748 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
749 call assert_equal('R-Rc', g:current_modes)
750 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100751 exe "normal RCom\<C-P>\<F2>\<Esc>u"
752 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100753 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100754 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
755 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100756 " R_CTRL-X CTRL-L: No match
757 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
758 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100759
Bram Moolenaara1449832019-09-01 20:16:52 +0200760 call assert_equal('n', 0->mode())
761 call assert_equal('n', 1->mode())
Bram Moolenaare90858d2017-02-01 17:24:34 +0100762
Bram Moolenaar612cc382018-07-29 15:34:26 +0200763 " i_CTRL-O
764 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
765 call assert_equal("n-niI", g:current_modes)
766
767 " R_CTRL-O
768 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
769 call assert_equal("n-niR", g:current_modes)
770
771 " gR_CTRL-O
772 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
773 call assert_equal("n-niV", g:current_modes)
774
Bram Moolenaare90858d2017-02-01 17:24:34 +0100775 " How to test operator-pending mode?
776
777 call feedkeys("v", 'xt')
778 call assert_equal('v', mode())
779 call assert_equal('v', mode(1))
780 call feedkeys("\<Esc>V", 'xt')
781 call assert_equal('V', mode())
782 call assert_equal('V', mode(1))
783 call feedkeys("\<Esc>\<C-V>", 'xt')
784 call assert_equal("\<C-V>", mode())
785 call assert_equal("\<C-V>", mode(1))
786 call feedkeys("\<Esc>", 'xt')
787
788 call feedkeys("gh", 'xt')
789 call assert_equal('s', mode())
790 call assert_equal('s', mode(1))
791 call feedkeys("\<Esc>gH", 'xt')
792 call assert_equal('S', mode())
793 call assert_equal('S', mode(1))
794 call feedkeys("\<Esc>g\<C-H>", 'xt')
795 call assert_equal("\<C-S>", mode())
796 call assert_equal("\<C-S>", mode(1))
797 call feedkeys("\<Esc>", 'xt')
798
799 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
800 call assert_equal('c-c', g:current_modes)
801 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
802 call assert_equal('c-cv', g:current_modes)
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200803 call feedkeys("Qcall Save_mode()\<CR>vi\<CR>", 'xt')
804 call assert_equal('c-ce', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100805 " How to test Ex mode?
806
807 bwipe!
808 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100809 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100810endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100811
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200812" Test for append()
Bram Moolenaard2007022019-08-27 21:56:06 +0200813func Test_append()
814 enew!
815 split
816 call append(0, ["foo"])
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200817 call append(1, [])
818 call append(1, test_null_list())
819 call assert_equal(['foo', ''], getline(1, '$'))
Bram Moolenaard2007022019-08-27 21:56:06 +0200820 split
821 only
822 undo
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200823 undo
Bram Moolenaar08f41572020-04-20 16:50:00 +0200824
825 " Using $ instead of '$' must give an error
826 call assert_fails("call append($, 'foobar')", 'E116:')
Bram Moolenaard2007022019-08-27 21:56:06 +0200827endfunc
828
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200829" Test for setline()
830func Test_setline()
831 new
832 call setline(0, ["foo"])
833 call setline(0, [])
834 call setline(0, test_null_list())
835 call setline(1, ["bar"])
836 call setline(1, [])
837 call setline(1, test_null_list())
838 call setline(2, [])
839 call setline(2, test_null_list())
840 call setline(3, [])
841 call setline(3, test_null_list())
842 call setline(2, ["baz"])
843 call assert_equal(['bar', 'baz'], getline(1, '$'))
844 close!
845endfunc
846
Bram Moolenaar79518e22017-02-17 16:31:35 +0100847func Test_getbufvar()
848 let bnr = bufnr('%')
849 let b:var_num = '1234'
850 let def_num = '5678'
851 call assert_equal('1234', getbufvar(bnr, 'var_num'))
852 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
853
854 let bd = getbufvar(bnr, '')
855 call assert_equal('1234', bd['var_num'])
856 call assert_true(exists("bd['changedtick']"))
857 call assert_equal(2, len(bd))
858
859 let bd2 = getbufvar(bnr, '', def_num)
860 call assert_equal(bd, bd2)
861
862 unlet b:var_num
863 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
864 call assert_equal('', getbufvar(bnr, 'var_num'))
865
866 let bd = getbufvar(bnr, '')
867 call assert_equal(1, len(bd))
868 let bd = getbufvar(bnr, '',def_num)
869 call assert_equal(1, len(bd))
870
Bram Moolenaar4520d442017-03-19 16:09:46 +0100871 call assert_equal('', getbufvar(9999, ''))
872 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100873 unlet def_num
874
Bram Moolenaar507647d2017-02-17 16:43:49 +0100875 call assert_equal(0, getbufvar(bnr, '&autoindent'))
876 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100877
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100878 " Set and get a buffer-local variable
879 call setbufvar(bnr, 'bufvar_test', ['one', 'two'])
880 call assert_equal(['one', 'two'], getbufvar(bnr, 'bufvar_test'))
881
Bram Moolenaar79518e22017-02-17 16:31:35 +0100882 " Open new window with forced option values
883 set fileformats=unix,dos
884 new ++ff=dos ++bin ++enc=iso-8859-2
885 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
886 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
887 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
888 close
889
Bram Moolenaar52592752020-04-03 18:43:35 +0200890 " Get the b: dict.
891 let b:testvar = 'one'
892 new
893 let b:testvar = 'two'
894 let thebuf = bufnr()
895 wincmd w
896 call assert_equal('two', getbufvar(thebuf, 'testvar'))
897 call assert_equal('two', getbufvar(thebuf, '').testvar)
898 bwipe!
899
Bram Moolenaar79518e22017-02-17 16:31:35 +0100900 set fileformats&
901endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100902
Bram Moolenaar41042f32017-03-09 12:09:32 +0100903func Test_last_buffer_nr()
904 call assert_equal(bufnr('$'), last_buffer_nr())
905endfunc
906
907func Test_stridx()
908 call assert_equal(-1, stridx('', 'l'))
909 call assert_equal(0, stridx('', ''))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200910 call assert_equal(0, 'hello'->stridx(''))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100911 call assert_equal(-1, stridx('hello', 'L'))
912 call assert_equal(2, stridx('hello', 'l', -1))
913 call assert_equal(2, stridx('hello', 'l', 0))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200914 call assert_equal(2, 'hello'->stridx('l', 1))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100915 call assert_equal(3, stridx('hello', 'l', 3))
916 call assert_equal(-1, stridx('hello', 'l', 4))
917 call assert_equal(-1, stridx('hello', 'l', 10))
918 call assert_equal(2, stridx('hello', 'll'))
919 call assert_equal(-1, stridx('hello', 'hello world'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100920 call assert_fails("let n=stridx('hello', [])", 'E730:')
921 call assert_fails("let n=stridx([], 'l')", 'E730:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100922endfunc
923
924func Test_strridx()
925 call assert_equal(-1, strridx('', 'l'))
926 call assert_equal(0, strridx('', ''))
927 call assert_equal(5, strridx('hello', ''))
928 call assert_equal(-1, strridx('hello', 'L'))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200929 call assert_equal(3, 'hello'->strridx('l'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100930 call assert_equal(3, strridx('hello', 'l', 10))
931 call assert_equal(3, strridx('hello', 'l', 3))
932 call assert_equal(2, strridx('hello', 'l', 2))
933 call assert_equal(-1, strridx('hello', 'l', 1))
934 call assert_equal(-1, strridx('hello', 'l', 0))
935 call assert_equal(-1, strridx('hello', 'l', -1))
936 call assert_equal(2, strridx('hello', 'll'))
937 call assert_equal(-1, strridx('hello', 'hello world'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100938 call assert_fails("let n=strridx('hello', [])", 'E730:')
939 call assert_fails("let n=strridx([], 'l')", 'E730:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100940endfunc
941
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200942func Test_match_func()
943 call assert_equal(4, match('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200944 call assert_equal(4, 'testing'->match('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200945 call assert_equal(-1, match('testing', 'ing', 5))
946 call assert_equal(-1, match('testing', 'ing', 8))
947 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
948 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100949 call assert_fails("let x=match('vim', [])", 'E730:')
950 call assert_equal(3, match(['a', 'b', 'c', 'a'], 'a', 1))
951 call assert_equal(-1, match(['a', 'b', 'c', 'a'], 'a', 5))
952 call assert_equal(4, match('testing', 'ing', -1))
953 call assert_fails("let x=match('testing', 'ing', 0, [])", 'E745:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200954 call assert_equal(-1, match(test_null_list(), 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200955endfunc
956
Bram Moolenaar41042f32017-03-09 12:09:32 +0100957func Test_matchend()
958 call assert_equal(7, matchend('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200959 call assert_equal(7, 'testing'->matchend('ing', 2))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100960 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200961 call assert_equal(-1, matchend('testing', 'ing', 8))
962 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
963 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
964endfunc
965
966func Test_matchlist()
967 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200968 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200969 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
970endfunc
971
972func Test_matchstr()
973 call assert_equal('ing', matchstr('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200974 call assert_equal('ing', 'testing'->matchstr('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200975 call assert_equal('', matchstr('testing', 'ing', 5))
976 call assert_equal('', matchstr('testing', 'ing', 8))
977 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
978 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
979endfunc
980
981func Test_matchstrpos()
982 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200983 call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200984 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
985 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
986 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
987 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200988 call assert_equal(['', -1, -1], matchstrpos(test_null_list(), '\a'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100989endfunc
990
991func Test_nextnonblank_prevnonblank()
992 new
993insert
994This
995
996
997is
998
999a
1000Test
1001.
1002 call assert_equal(0, nextnonblank(-1))
1003 call assert_equal(0, nextnonblank(0))
1004 call assert_equal(1, nextnonblank(1))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001005 call assert_equal(4, 2->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001006 call assert_equal(4, nextnonblank(3))
1007 call assert_equal(4, nextnonblank(4))
1008 call assert_equal(6, nextnonblank(5))
1009 call assert_equal(6, nextnonblank(6))
1010 call assert_equal(7, nextnonblank(7))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001011 call assert_equal(0, 8->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001012
1013 call assert_equal(0, prevnonblank(-1))
1014 call assert_equal(0, prevnonblank(0))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001015 call assert_equal(1, 1->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001016 call assert_equal(1, prevnonblank(2))
1017 call assert_equal(1, prevnonblank(3))
1018 call assert_equal(4, prevnonblank(4))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001019 call assert_equal(4, 5->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001020 call assert_equal(6, prevnonblank(6))
1021 call assert_equal(7, prevnonblank(7))
1022 call assert_equal(0, prevnonblank(8))
1023 bw!
1024endfunc
1025
1026func Test_byte2line_line2byte()
1027 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +02001028 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +01001029 call setline(1, ['a', 'bc', 'd'])
1030
1031 set fileformat=unix
1032 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
1033 \ map(range(-1, 8), 'byte2line(v:val)'))
1034 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
1035 \ map(range(-1, 5), 'line2byte(v:val)'))
1036
1037 set fileformat=mac
1038 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001039 \ map(range(-1, 8), 'v:val->byte2line()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001040 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
Bram Moolenaar02b31112019-08-31 22:16:38 +02001041 \ map(range(-1, 5), 'v:val->line2byte()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001042
1043 set fileformat=dos
1044 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
1045 \ map(range(-1, 11), 'byte2line(v:val)'))
1046 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
1047 \ map(range(-1, 5), 'line2byte(v:val)'))
1048
Bram Moolenaarc26f7c62018-08-20 22:53:04 +02001049 bw!
1050 set noendofline nofixendofline
1051 normal a-
1052 for ff in ["unix", "mac", "dos"]
1053 let &fileformat = ff
1054 call assert_equal(1, line2byte(1))
1055 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
1056 endfor
1057
1058 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +01001059 bw!
1060endfunc
1061
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001062" Test for byteidx() and byteidxcomp() functions
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001063func Test_byteidx()
1064 let a = '.é.' " one char of two bytes
1065 call assert_equal(0, byteidx(a, 0))
1066 call assert_equal(0, byteidxcomp(a, 0))
1067 call assert_equal(1, byteidx(a, 1))
1068 call assert_equal(1, byteidxcomp(a, 1))
1069 call assert_equal(3, byteidx(a, 2))
1070 call assert_equal(3, byteidxcomp(a, 2))
1071 call assert_equal(4, byteidx(a, 3))
1072 call assert_equal(4, byteidxcomp(a, 3))
1073 call assert_equal(-1, byteidx(a, 4))
1074 call assert_equal(-1, byteidxcomp(a, 4))
1075
1076 let b = '.é.' " normal e with composing char
1077 call assert_equal(0, b->byteidx(0))
1078 call assert_equal(1, b->byteidx(1))
1079 call assert_equal(4, b->byteidx(2))
1080 call assert_equal(5, b->byteidx(3))
1081 call assert_equal(-1, b->byteidx(4))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001082 call assert_fails("call byteidx([], 0)", 'E730:')
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001083
1084 call assert_equal(0, b->byteidxcomp(0))
1085 call assert_equal(1, b->byteidxcomp(1))
1086 call assert_equal(2, b->byteidxcomp(2))
1087 call assert_equal(4, b->byteidxcomp(3))
1088 call assert_equal(5, b->byteidxcomp(4))
1089 call assert_equal(-1, b->byteidxcomp(5))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001090 call assert_fails("call byteidxcomp([], 0)", 'E730:')
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001091endfunc
1092
Bram Moolenaar41042f32017-03-09 12:09:32 +01001093func Test_count()
1094 let l = ['a', 'a', 'A', 'b']
1095 call assert_equal(2, count(l, 'a'))
1096 call assert_equal(1, count(l, 'A'))
1097 call assert_equal(1, count(l, 'b'))
1098 call assert_equal(0, count(l, 'B'))
1099
1100 call assert_equal(2, count(l, 'a', 0))
1101 call assert_equal(1, count(l, 'A', 0))
1102 call assert_equal(1, count(l, 'b', 0))
1103 call assert_equal(0, count(l, 'B', 0))
1104
1105 call assert_equal(3, count(l, 'a', 1))
1106 call assert_equal(3, count(l, 'A', 1))
1107 call assert_equal(1, count(l, 'b', 1))
1108 call assert_equal(1, count(l, 'B', 1))
1109 call assert_equal(0, count(l, 'c', 1))
1110
1111 call assert_equal(1, count(l, 'a', 0, 1))
1112 call assert_equal(2, count(l, 'a', 1, 1))
1113 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02001114 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001115
1116 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
1117 call assert_equal(2, count(d, 'a'))
1118 call assert_equal(1, count(d, 'A'))
1119 call assert_equal(1, count(d, 'b'))
1120 call assert_equal(0, count(d, 'B'))
1121
1122 call assert_equal(2, count(d, 'a', 0))
1123 call assert_equal(1, count(d, 'A', 0))
1124 call assert_equal(1, count(d, 'b', 0))
1125 call assert_equal(0, count(d, 'B', 0))
1126
1127 call assert_equal(3, count(d, 'a', 1))
1128 call assert_equal(3, count(d, 'A', 1))
1129 call assert_equal(1, count(d, 'b', 1))
1130 call assert_equal(1, count(d, 'B', 1))
1131 call assert_equal(0, count(d, 'c', 1))
1132
1133 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +02001134
1135 call assert_equal(0, count("foo", "bar"))
1136 call assert_equal(1, count("foo", "oo"))
1137 call assert_equal(2, count("foo", "o"))
1138 call assert_equal(0, count("foo", "O"))
1139 call assert_equal(2, count("foo", "O", 1))
1140 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +01001141 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001142
1143 call assert_fails('call count(0, 0)', 'E712:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001144endfunc
1145
1146func Test_changenr()
1147 new Xchangenr
1148 call assert_equal(0, changenr())
1149 norm ifoo
1150 call assert_equal(1, changenr())
1151 set undolevels=10
1152 norm Sbar
1153 call assert_equal(2, changenr())
1154 undo
1155 call assert_equal(1, changenr())
1156 redo
1157 call assert_equal(2, changenr())
1158 bw!
1159 set undolevels&
1160endfunc
1161
1162func Test_filewritable()
1163 new Xfilewritable
1164 write!
1165 call assert_equal(1, filewritable('Xfilewritable'))
1166
1167 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1168 call assert_equal(0, filewritable('Xfilewritable'))
1169
1170 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001171 call assert_equal(1, 'Xfilewritable'->filewritable())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001172
1173 call assert_equal(0, filewritable('doesnotexist'))
1174
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +02001175 call mkdir('Xdir')
1176 call assert_equal(2, filewritable('Xdir'))
1177 call delete('Xdir', 'd')
1178
Bram Moolenaar41042f32017-03-09 12:09:32 +01001179 call delete('Xfilewritable')
1180 bw!
1181endfunc
1182
Bram Moolenaar82956662018-10-06 15:18:45 +02001183func Test_Executable()
1184 if has('win32')
1185 call assert_equal(1, executable('notepad'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001186 call assert_equal(1, 'notepad.exe'->executable())
Bram Moolenaar82956662018-10-06 15:18:45 +02001187 call assert_equal(0, executable('notepad.exe.exe'))
1188 call assert_equal(0, executable('shell32.dll'))
1189 call assert_equal(0, executable('win.ini'))
Bram Moolenaar95da1362020-05-30 18:37:55 +02001190
1191 " get "notepad" path and remove the leading drive and sep. (ex. 'C:\')
1192 let notepadcmd = exepath('notepad.exe')
1193 let driveroot = notepadcmd[:2]
1194 let notepadcmd = notepadcmd[3:]
1195 new
1196 " check that the relative path works in /
1197 execute 'lcd' driveroot
1198 call assert_equal(1, executable(notepadcmd))
1199 call assert_equal(driveroot .. notepadcmd, notepadcmd->exepath())
1200 bwipe
1201
1202 " create "notepad.bat"
1203 call mkdir('Xdir')
1204 let notepadbat = fnamemodify('Xdir/notepad.bat', ':p')
1205 call writefile([], notepadbat)
1206 new
1207 " check that the path and the pathext order is valid
1208 lcd Xdir
1209 let [pathext, $PATHEXT] = [$PATHEXT, '.com;.exe;.bat;.cmd']
1210 call assert_equal(notepadbat, exepath('notepad'))
1211 let $PATHEXT = pathext
1212 bwipe
1213 eval 'Xdir'->delete('rf')
Bram Moolenaar82956662018-10-06 15:18:45 +02001214 elseif has('unix')
Bram Moolenaara4208962019-08-24 20:50:19 +02001215 call assert_equal(1, 'cat'->executable())
Bram Moolenaara05a0d32018-10-07 18:43:05 +02001216 call assert_equal(0, executable('nodogshere'))
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001217
1218 " get "cat" path and remove the leading /
1219 let catcmd = exepath('cat')[1:]
1220 new
Bram Moolenaara4208962019-08-24 20:50:19 +02001221 " check that the relative path works in /
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001222 lcd /
1223 call assert_equal(1, executable(catcmd))
Bram Moolenaara4208962019-08-24 20:50:19 +02001224 call assert_equal('/' .. catcmd, catcmd->exepath())
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001225 bwipe
Bram Moolenaar82956662018-10-06 15:18:45 +02001226 endif
1227endfunc
1228
Bram Moolenaar86621892019-03-30 21:51:28 +01001229func Test_executable_longname()
1230 if !has('win32')
1231 return
1232 endif
1233
1234 let fname = 'X' . repeat('あ', 200) . '.bat'
1235 call writefile([], fname)
1236 call assert_equal(1, executable(fname))
1237 call delete(fname)
1238endfunc
1239
Bram Moolenaar41042f32017-03-09 12:09:32 +01001240func Test_hostname()
1241 let hostname_vim = hostname()
1242 if has('unix')
1243 let hostname_system = systemlist('uname -n')[0]
1244 call assert_equal(hostname_vim, hostname_system)
1245 endif
1246endfunc
1247
1248func Test_getpid()
1249 " getpid() always returns the same value within a vim instance.
1250 call assert_equal(getpid(), getpid())
1251 if has('unix')
1252 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1253 endif
1254endfunc
1255
1256func Test_hlexists()
1257 call assert_equal(0, hlexists('does_not_exist'))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001258 call assert_equal(0, 'Number'->hlexists())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001259 call assert_equal(0, highlight_exists('does_not_exist'))
1260 call assert_equal(0, highlight_exists('Number'))
1261 syntax on
1262 call assert_equal(0, hlexists('does_not_exist'))
1263 call assert_equal(1, hlexists('Number'))
1264 call assert_equal(0, highlight_exists('does_not_exist'))
1265 call assert_equal(1, highlight_exists('Number'))
1266 syntax off
1267endfunc
1268
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001269" Test for the col() function
Bram Moolenaar41042f32017-03-09 12:09:32 +01001270func Test_col()
1271 new
1272 call setline(1, 'abcdef')
1273 norm gg4|mx6|mY2|
1274 call assert_equal(2, col('.'))
1275 call assert_equal(7, col('$'))
Bram Moolenaar8b633132020-03-20 18:20:51 +01001276 call assert_equal(2, col('v'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001277 call assert_equal(4, col("'x"))
1278 call assert_equal(6, col("'Y"))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001279 call assert_equal(2, [1, 2]->col())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001280 call assert_equal(7, col([1, '$']))
1281
1282 call assert_equal(0, col(''))
1283 call assert_equal(0, col('x'))
1284 call assert_equal(0, col([2, '$']))
1285 call assert_equal(0, col([1, 100]))
1286 call assert_equal(0, col([1]))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001287 call assert_equal(0, col(test_null_list()))
1288 call assert_fails('let c = col({})', 'E731:')
Bram Moolenaar8b633132020-03-20 18:20:51 +01001289
1290 " test for getting the visual start column
1291 func T()
1292 let g:Vcol = col('v')
1293 return ''
1294 endfunc
1295 let g:Vcol = 0
1296 xmap <expr> <F2> T()
1297 exe "normal gg3|ve\<F2>"
1298 call assert_equal(3, g:Vcol)
1299 xunmap <F2>
1300 delfunc T
1301
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001302 " Test for the visual line start and end marks '< and '>
1303 call setline(1, ['one', 'one two', 'one two three'])
1304 "normal! ggVG
1305 call feedkeys("ggVG\<Esc>", 'xt')
1306 call assert_equal(1, col("'<"))
1307 call assert_equal(14, col("'>"))
1308 " Delete the last line of the visually selected region
1309 $d
1310 call assert_notequal(14, col("'>"))
1311
1312 " Test with 'virtualedit'
1313 set virtualedit=all
1314 call cursor(1, 10)
1315 call assert_equal(4, col('.'))
1316 set virtualedit&
1317
Bram Moolenaar41042f32017-03-09 12:09:32 +01001318 bw!
1319endfunc
1320
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001321" Test for input()
1322func Test_input_func()
1323 " Test for prompt with multiple lines
1324 redir => v
1325 call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt')
1326 redir END
1327 call assert_equal("B", c)
1328 call assert_equal(['A', 'B', 'C'], split(v, "\n"))
1329
1330 " Test for default value
1331 call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt')
1332 call assert_equal('red', c)
1333
1334 " Test for completion at the input prompt
1335 func! Tcomplete(arglead, cmdline, pos)
1336 return "item1\nitem2\nitem3"
1337 endfunc
1338 call feedkeys(":let c = input('Q? ', '' , 'custom,Tcomplete')\<CR>"
1339 \ .. "\<C-A>\<CR>", 'xt')
1340 delfunc Tcomplete
1341 call assert_equal('item1 item2 item3', c)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001342
1343 call assert_fails("call input('F:', '', 'invalid')", 'E180:')
1344 call assert_fails("call input('F:', '', [])", 'E730:')
1345endfunc
1346
1347" Test for the inputdialog() function
1348func Test_inputdialog()
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001349 if has('gui_running')
1350 call assert_fails('let v=inputdialog([], "xx")', 'E730:')
1351 call assert_fails('let v=inputdialog("Q", [])', 'E730:')
1352 else
1353 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt')
1354 call assert_equal('xx', v)
1355 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt')
1356 call assert_equal('yy', v)
1357 endif
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001358endfunc
1359
1360" Test for inputlist()
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001361func Test_inputlist()
1362 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1363 call assert_equal(1, c)
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001364 call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001365 call assert_equal(2, c)
1366 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1367 call assert_equal(3, c)
1368
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001369 " Use backspace to delete characters in the prompt
1370 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<BS>3\<BS>2\<cr>", 'tx')
1371 call assert_equal(2, c)
1372
1373 " Use mouse to make a selection
1374 call test_setmouse(&lines - 3, 2)
1375 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
1376 call assert_equal(1, c)
1377 " Mouse click outside of the list
1378 call test_setmouse(&lines - 6, 2)
1379 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
1380 call assert_equal(-2, c)
1381
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001382 call assert_fails('call inputlist("")', 'E686:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001383 call assert_fails('call inputlist(test_null_list())', 'E686:')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001384endfunc
1385
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001386func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001387 if has('balloon_eval')
1388 " This won't do anything but must not crash either.
1389 call balloon_show('hi!')
Bram Moolenaar7d8ea0b2020-01-27 23:01:30 +01001390 if !has('gui_running')
1391 call balloon_show(range(3))
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001392 call balloon_show([])
Bram Moolenaar7d8ea0b2020-01-27 23:01:30 +01001393 endif
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001394 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001395endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001396
1397func Test_setbufvar_options()
1398 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1399 " window layout.
1400 call assert_equal(1, winnr('$'))
1401 split dummy_preview
1402 resize 2
1403 set winfixheight winfixwidth
1404 let prev_id = win_getid()
1405
1406 wincmd j
1407 let wh = winheight('.')
1408 let dummy_buf = bufnr('dummy_buf1', v:true)
1409 call setbufvar(dummy_buf, '&buftype', 'nofile')
1410 execute 'belowright vertical split #' . dummy_buf
1411 call assert_equal(wh, winheight('.'))
1412 let dum1_id = win_getid()
1413
1414 wincmd h
1415 let wh = winheight('.')
1416 let dummy_buf = bufnr('dummy_buf2', v:true)
Bram Moolenaar196b4662019-09-06 21:34:30 +02001417 eval 'nofile'->setbufvar(dummy_buf, '&buftype')
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001418 execute 'belowright vertical split #' . dummy_buf
1419 call assert_equal(wh, winheight('.'))
1420
1421 bwipe!
1422 call win_gotoid(prev_id)
1423 bwipe!
1424 call win_gotoid(dum1_id)
1425 bwipe!
1426endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001427
1428func Test_redo_in_nested_functions()
1429 nnoremap g. :set opfunc=Operator<CR>g@
1430 function Operator( type, ... )
1431 let @x = 'XXX'
1432 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1433 endfunction
1434
1435 function! Apply()
1436 5,6normal! .
1437 endfunction
1438
1439 new
1440 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1441 1normal g.i"
1442 call assert_equal('some "XXX" text', getline(1))
1443 3,4normal .
1444 call assert_equal('some "XXX" text', getline(3))
1445 call assert_equal('more "XXX" text', getline(4))
1446 call Apply()
1447 call assert_equal('some "XXX" text', getline(5))
1448 call assert_equal('more "XXX" text', getline(6))
1449 bwipe!
1450
1451 nunmap g.
1452 delfunc Operator
1453 delfunc Apply
1454endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02001455
1456func Test_shellescape()
1457 let save_shell = &shell
1458 set shell=bash
1459 call assert_equal("'text'", shellescape('text'))
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001460 call assert_equal("'te\"xt'", 'te"xt'->shellescape())
Bram Moolenaar20615522017-06-05 18:46:26 +02001461 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
1462
1463 call assert_equal("'te%xt'", shellescape("te%xt"))
1464 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
1465 call assert_equal("'te#xt'", shellescape("te#xt"))
1466 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
1467 call assert_equal("'te!xt'", shellescape("te!xt"))
1468 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1469
1470 call assert_equal("'te\nxt'", shellescape("te\nxt"))
1471 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1472 set shell=tcsh
1473 call assert_equal("'te\\!xt'", shellescape("te!xt"))
1474 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1475 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1476 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1477
1478 let &shell = save_shell
1479endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001480
1481func Test_trim()
1482 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001483 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 +01001484 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1485 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
1486 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
1487 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
1488 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
1489 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1490 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1491 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
1492 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
1493 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
1494 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
1495 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1496 call assert_equal("", trim("", ""))
1497 call assert_equal("a", trim("a", ""))
1498 call assert_equal("", trim("", "a"))
1499
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001500 call assert_equal("vim", trim(" vim ", " ", 0))
1501 call assert_equal("vim ", trim(" vim ", " ", 1))
1502 call assert_equal(" vim", trim(" vim ", " ", 2))
1503 call assert_fails('eval trim(" vim ", " ", [])', 'E745:')
1504 call assert_fails('eval trim(" vim ", " ", -1)', 'E475:')
1505 call assert_fails('eval trim(" vim ", " ", 3)', 'E475:')
1506
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001507 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001508 call assert_equal("x", trim(chars . "x" . chars))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001509
1510 call assert_fails('let c=trim([])', 'E730:')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001511endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001512
1513" Test for reg_recording() and reg_executing()
1514func Test_reg_executing_and_recording()
1515 let s:reg_stat = ''
1516 func s:save_reg_stat()
1517 let s:reg_stat = reg_recording() . ':' . reg_executing()
1518 return ''
1519 endfunc
1520
1521 new
1522 call s:save_reg_stat()
1523 call assert_equal(':', s:reg_stat)
1524 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1525 call assert_equal('a:', s:reg_stat)
1526 call feedkeys("@a", 'xt')
1527 call assert_equal(':a', s:reg_stat)
1528 call feedkeys("qb@aq", 'xt')
1529 call assert_equal('b:a', s:reg_stat)
1530 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1531 call assert_equal('":', s:reg_stat)
1532
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001533 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001534 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001535 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1536 func TestFunc() abort
1537 normal! ia
1538 endfunc
1539 call feedkeys("@q", 'xt')
1540 call assert_equal(':q', s:reg_stat)
1541 delfunc TestFunc
1542
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001543 " getchar() command saves and restores reg_executing
1544 map W :call TestFunc()<CR>
1545 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001546 let g:typed = ''
1547 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001548 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001549 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001550 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001551 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001552 endfunc
1553 call feedkeys("@qy", 'xt')
1554 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001555 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001556 delfunc TestFunc
1557 unmap W
1558 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001559 unlet g:regs
1560
1561 " input() command saves and restores reg_executing
1562 map W :call TestFunc()<CR>
1563 let @q = "W"
1564 let g:typed = ''
1565 let g:regs = []
1566 func TestFunc() abort
1567 let g:regs += [reg_executing()]
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001568 let g:typed = '?'->input()
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001569 let g:regs += [reg_executing()]
1570 endfunc
1571 call feedkeys("@qy\<CR>", 'xt')
1572 call assert_equal("y", g:typed)
1573 call assert_equal(['q', 'q'], g:regs)
1574 delfunc TestFunc
1575 unmap W
1576 unlet g:typed
1577 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001578
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001579 bwipe!
1580 delfunc s:save_reg_stat
1581 unlet s:reg_stat
1582endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001583
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001584func Test_inputsecret()
1585 map W :call TestFunc()<CR>
1586 let @q = "W"
1587 let g:typed1 = ''
1588 let g:typed2 = ''
1589 let g:regs = []
1590 func TestFunc() abort
1591 let g:typed1 = '?'->inputsecret()
1592 let g:typed2 = inputsecret('password: ')
1593 endfunc
1594 call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1595 call assert_equal("something", g:typed1)
1596 call assert_equal("else", g:typed2)
1597 delfunc TestFunc
1598 unmap W
1599 unlet g:typed1
1600 unlet g:typed2
1601endfunc
1602
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001603func Test_getchar()
1604 call feedkeys('a', '')
1605 call assert_equal(char2nr('a'), getchar())
1606
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001607 call setline(1, 'xxxx')
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001608 call test_setmouse(1, 3)
1609 let v:mouse_win = 9
1610 let v:mouse_winid = 9
1611 let v:mouse_lnum = 9
1612 let v:mouse_col = 9
1613 call feedkeys("\<S-LeftMouse>", '')
1614 call assert_equal("\<S-LeftMouse>", getchar())
1615 call assert_equal(1, v:mouse_win)
1616 call assert_equal(win_getid(1), v:mouse_winid)
1617 call assert_equal(1, v:mouse_lnum)
1618 call assert_equal(3, v:mouse_col)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001619 enew!
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001620endfunc
1621
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001622func Test_libcall_libcallnr()
1623 if !has('libcall')
1624 return
1625 endif
1626
1627 if has('win32')
1628 let libc = 'msvcrt.dll'
1629 elseif has('mac')
1630 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001631 elseif executable('ldd')
1632 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1633 endif
1634 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001635 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001636 if has('linux')
1637 " There is not documented but regarding the 1st argument of glibc's
1638 " dlopen an empty string and nullptr are equivalent, so using an empty
1639 " string for the 1st argument of libcall allows to call functions.
1640 let libc = ''
1641 elseif has('sun')
1642 " Set the path to libc.so according to the architecture.
1643 let test_bits = system('file ' . GetVimProg())
1644 let test_arch = system('uname -p')
1645 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1646 let libc = '/usr/lib/sparcv9/libc.so'
1647 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1648 let libc = '/usr/lib/amd64/libc.so'
1649 else
1650 let libc = '/usr/lib/libc.so'
1651 endif
1652 else
1653 " Unfortunately skip this test until a good way is found.
1654 return
1655 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001656 endif
1657
1658 if has('win32')
Bram Moolenaar02b31112019-08-31 22:16:38 +02001659 call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001660 else
Bram Moolenaar02b31112019-08-31 22:16:38 +02001661 call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001662 endif
1663
1664 " If function returns NULL, libcall() should return an empty string.
1665 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1666
1667 " Test libcallnr() with string and integer argument.
Bram Moolenaar02b31112019-08-31 22:16:38 +02001668 call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1669 call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001670
1671 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1672 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1673
1674 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1675 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1676endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001677
1678sandbox function Fsandbox()
1679 normal ix
1680endfunc
1681
1682func Test_func_sandbox()
1683 sandbox let F = {-> 'hello'}
1684 call assert_equal('hello', F())
1685
Bram Moolenaara4208962019-08-24 20:50:19 +02001686 sandbox let F = {-> "normal ix\<Esc>"->execute()}
Bram Moolenaard90a1442018-07-15 20:24:31 +02001687 call assert_fails('call F()', 'E48:')
1688 unlet F
1689
1690 call assert_fails('call Fsandbox()', 'E48:')
1691 delfunc Fsandbox
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001692
1693 " From a sandbox try to set a predefined variable (which cannot be modified
1694 " from a sandbox)
1695 call assert_fails('sandbox let v:lnum = 10', 'E794:')
Bram Moolenaard90a1442018-07-15 20:24:31 +02001696endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001697
1698func EditAnotherFile()
1699 let word = expand('<cword>')
1700 edit Xfuncrange2
1701endfunc
1702
1703func Test_func_range_with_edit()
1704 " Define a function that edits another buffer, then call it with a range that
1705 " is invalid in that buffer.
1706 call writefile(['just one line'], 'Xfuncrange2')
1707 new
Bram Moolenaar196b4662019-09-06 21:34:30 +02001708 eval 10->range()->setline(1)
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001709 write Xfuncrange1
1710 call assert_fails('5,8call EditAnotherFile()', 'E16:')
1711
1712 call delete('Xfuncrange1')
1713 call delete('Xfuncrange2')
1714 bwipe!
1715endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001716
1717func Test_func_exists_on_reload()
1718 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1719 call assert_equal(0, exists('*ExistingFunction'))
1720 source Xfuncexists
Bram Moolenaara4208962019-08-24 20:50:19 +02001721 call assert_equal(1, '*ExistingFunction'->exists())
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001722 " Redefining a function when reloading a script is OK.
1723 source Xfuncexists
1724 call assert_equal(1, exists('*ExistingFunction'))
1725
1726 " But redefining in another script is not OK.
1727 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1728 call assert_fails('source Xfuncexists2', 'E122:')
1729
1730 delfunc ExistingFunction
1731 call assert_equal(0, exists('*ExistingFunction'))
1732 call writefile([
1733 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1734 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1735 \ ], 'Xfuncexists')
1736 call assert_fails('source Xfuncexists', 'E122:')
1737 call assert_equal(1, exists('*ExistingFunction'))
1738
1739 call delete('Xfuncexists2')
1740 call delete('Xfuncexists')
1741 delfunc ExistingFunction
1742endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01001743
1744" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1745func Test_confirm()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001746 CheckUnix
1747 CheckNotGui
Bram Moolenaar2e050092019-01-27 15:00:36 +01001748
1749 call feedkeys('o', 'L')
1750 let a = confirm('Press O to proceed')
1751 call assert_equal(1, a)
1752
1753 call feedkeys('y', 'L')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001754 let a = 'Are you sure?'->confirm("&Yes\n&No")
Bram Moolenaar2e050092019-01-27 15:00:36 +01001755 call assert_equal(1, a)
1756
1757 call feedkeys('n', 'L')
1758 let a = confirm('Are you sure?', "&Yes\n&No")
1759 call assert_equal(2, a)
1760
1761 " confirm() should return 0 when pressing CTRL-C.
Bram Moolenaar79296512020-03-22 16:17:14 +01001762 call feedkeys("\<C-C>", 'L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01001763 let a = confirm('Are you sure?', "&Yes\n&No")
1764 call assert_equal(0, a)
1765
1766 " <Esc> requires another character to avoid it being seen as the start of an
1767 " escape sequence. Zero should be harmless.
Bram Moolenaara4208962019-08-24 20:50:19 +02001768 eval "\<Esc>0"->feedkeys('L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01001769 let a = confirm('Are you sure?', "&Yes\n&No")
1770 call assert_equal(0, a)
1771
1772 " Default choice is returned when pressing <CR>.
1773 call feedkeys("\<CR>", 'L')
1774 let a = confirm('Are you sure?', "&Yes\n&No")
1775 call assert_equal(1, a)
1776
1777 call feedkeys("\<CR>", 'L')
1778 let a = confirm('Are you sure?', "&Yes\n&No", 2)
1779 call assert_equal(2, a)
1780
1781 call feedkeys("\<CR>", 'L')
1782 let a = confirm('Are you sure?', "&Yes\n&No", 0)
1783 call assert_equal(0, a)
1784
1785 " Test with the {type} 4th argument
1786 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
1787 call feedkeys('y', 'L')
1788 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
1789 call assert_equal(1, a)
1790 endfor
1791
1792 call assert_fails('call confirm([])', 'E730:')
1793 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
1794 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
1795 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
1796endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001797
1798func Test_platform_name()
1799 " The system matches at most only one name.
Bram Moolenaar041c7102020-05-30 18:14:57 +02001800 let names = ['amiga', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001801 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
1802
1803 " Is Unix?
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001804 call assert_equal(has('bsd'), has('bsd') && has('unix'))
1805 call assert_equal(has('hpux'), has('hpux') && has('unix'))
1806 call assert_equal(has('linux'), has('linux') && has('unix'))
1807 call assert_equal(has('mac'), has('mac') && has('unix'))
1808 call assert_equal(has('qnx'), has('qnx') && has('unix'))
1809 call assert_equal(has('sun'), has('sun') && has('unix'))
1810 call assert_equal(has('win32'), has('win32') && !has('unix'))
1811 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
1812
1813 if has('unix') && executable('uname')
1814 let uname = system('uname')
Bram Moolenaara02e3f62019-02-07 21:27:14 +01001815 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
1816 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001817 call assert_equal(uname =~? 'HP-UX', has('hpux'))
1818 call assert_equal(uname =~? 'Linux', has('linux'))
1819 call assert_equal(uname =~? 'Darwin', has('mac'))
1820 call assert_equal(uname =~? 'QNX', has('qnx'))
1821 call assert_equal(uname =~? 'SunOS', has('sun'))
1822 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
1823 endif
1824endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001825
1826func Test_readdir()
1827 call mkdir('Xdir')
1828 call writefile([], 'Xdir/foo.txt')
1829 call writefile([], 'Xdir/bar.txt')
1830 call mkdir('Xdir/dir')
1831
1832 " All results
1833 let files = readdir('Xdir')
1834 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
1835
1836 " Only results containing "f"
Bram Moolenaara0d1fef2019-09-04 22:29:14 +02001837 let files = 'Xdir'->readdir({ x -> stridx(x, 'f') !=- 1 })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001838 call assert_equal(['foo.txt'], sort(files))
1839
1840 " Only .txt files
1841 let files = readdir('Xdir', { x -> x =~ '.txt$' })
1842 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1843
1844 " Only .txt files with string
1845 let files = readdir('Xdir', 'v:val =~ ".txt$"')
1846 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1847
1848 " Limit to 1 result.
1849 let l = []
1850 let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
1851 call assert_equal(1, len(files))
1852
Bram Moolenaar27da7de2019-09-03 17:13:37 +02001853 " Nested readdir() must not crash
1854 let files = readdir('Xdir', 'readdir("Xdir", "1") != []')
1855 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
1856
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001857 eval 'Xdir'->delete('rf')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001858endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02001859
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001860func Test_delete_rf()
1861 call mkdir('Xdir')
1862 call writefile([], 'Xdir/foo.txt')
1863 call writefile([], 'Xdir/bar.txt')
1864 call mkdir('Xdir/[a-1]') " issue #696
1865 call writefile([], 'Xdir/[a-1]/foo.txt')
1866 call writefile([], 'Xdir/[a-1]/bar.txt')
1867 call assert_true(filereadable('Xdir/foo.txt'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001868 call assert_true('Xdir/[a-1]/foo.txt'->filereadable())
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001869
1870 call assert_equal(0, delete('Xdir', 'rf'))
1871 call assert_false(filereadable('Xdir/foo.txt'))
1872 call assert_false(filereadable('Xdir/[a-1]/foo.txt'))
1873endfunc
1874
Bram Moolenaar17aca702019-05-16 22:24:55 +02001875func Test_call()
1876 call assert_equal(3, call('len', [123]))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001877 call assert_equal(3, 'len'->call([123]))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001878 call assert_fails("call call('len', 123)", 'E714:')
1879 call assert_equal(0, call('', []))
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001880 call assert_equal(0, call('len', test_null_list()))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001881
1882 function Mylen() dict
1883 return len(self.data)
1884 endfunction
1885 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001886 eval mydict.len->call([], mydict)->assert_equal(4)
Bram Moolenaar17aca702019-05-16 22:24:55 +02001887 call assert_fails("call call('Mylen', [], 0)", 'E715:')
1888endfunc
1889
1890func Test_char2nr()
1891 call assert_equal(12354, char2nr('あ', 1))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001892 call assert_equal(120, 'x'->char2nr())
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001893 set encoding=latin1
1894 call assert_equal(120, 'x'->char2nr())
1895 set encoding=utf-8
Bram Moolenaar17aca702019-05-16 22:24:55 +02001896endfunc
1897
1898func Test_eventhandler()
1899 call assert_equal(0, eventhandler())
1900endfunc
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001901
1902func Test_bufadd_bufload()
1903 call assert_equal(0, bufexists('someName'))
1904 let buf = bufadd('someName')
1905 call assert_notequal(0, buf)
1906 call assert_equal(1, bufexists('someName'))
1907 call assert_equal(0, getbufvar(buf, '&buflisted'))
1908 call assert_equal(0, bufloaded(buf))
1909 call bufload(buf)
1910 call assert_equal(1, bufloaded(buf))
1911 call assert_equal([''], getbufline(buf, 1, '$'))
1912
1913 let curbuf = bufnr('')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001914 eval ['some', 'text']->writefile('XotherName')
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001915 let buf = 'XotherName'->bufadd()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001916 call assert_notequal(0, buf)
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001917 eval 'XotherName'->bufexists()->assert_equal(1)
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001918 call assert_equal(0, getbufvar(buf, '&buflisted'))
1919 call assert_equal(0, bufloaded(buf))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001920 eval buf->bufload()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001921 call assert_equal(1, bufloaded(buf))
1922 call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
1923 call assert_equal(curbuf, bufnr(''))
1924
Bram Moolenaar892ae722019-06-30 20:33:01 +02001925 let buf1 = bufadd('')
1926 let buf2 = bufadd('')
1927 call assert_notequal(0, buf1)
1928 call assert_notequal(0, buf2)
1929 call assert_notequal(buf1, buf2)
1930 call assert_equal(1, bufexists(buf1))
1931 call assert_equal(1, bufexists(buf2))
1932 call assert_equal(0, bufloaded(buf1))
1933 exe 'bwipe ' .. buf1
1934 call assert_equal(0, bufexists(buf1))
1935 call assert_equal(1, bufexists(buf2))
1936 exe 'bwipe ' .. buf2
1937 call assert_equal(0, bufexists(buf2))
1938
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001939 bwipe someName
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001940 bwipe XotherName
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001941 call assert_equal(0, bufexists('someName'))
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001942 call delete('XotherName')
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001943endfunc
Bram Moolenaarc2585492019-09-22 21:29:53 +02001944
1945func Test_state()
1946 CheckRunVimInTerminal
1947
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +01001948 let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"
1949
Bram Moolenaarc2585492019-09-22 21:29:53 +02001950 let lines =<< trim END
1951 call setline(1, ['one', 'two', 'three'])
1952 map ;; gg
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001953 set complete=.
Bram Moolenaarc2585492019-09-22 21:29:53 +02001954 func RunTimer()
1955 call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
1956 endfunc
1957 au Filetype foobar let g:state = state()|let g:mode = mode()
1958 END
1959 call writefile(lines, 'XState')
1960 let buf = RunVimInTerminal('-S XState', #{rows: 6})
1961
1962 " Using a ":" command Vim is busy, thus "S" is returned
1963 call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
1964 call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
1965 call term_sendkeys(buf, ":\<CR>")
1966
1967 " Using a timer callback
1968 call term_sendkeys(buf, ":call RunTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001969 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001970 call term_sendkeys(buf, getstate)
1971 call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)
1972
1973 " Halfway a mapping
1974 call term_sendkeys(buf, ":call RunTimer()\<CR>;")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001975 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001976 call term_sendkeys(buf, ";")
1977 call term_sendkeys(buf, getstate)
1978 call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)
1979
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001980 " Insert mode completion (bit slower on Mac)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001981 call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001982 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001983 call term_sendkeys(buf, "\<Esc>")
1984 call term_sendkeys(buf, getstate)
1985 call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)
1986
1987 " Autocommand executing
1988 call term_sendkeys(buf, ":set filetype=foobar\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001989 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001990 call term_sendkeys(buf, getstate)
1991 call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)
1992
1993 " Todo: "w" - waiting for ch_evalexpr()
1994
1995 " messages scrolled
1996 call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001997 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001998 call term_sendkeys(buf, "\<CR>")
1999 call term_sendkeys(buf, getstate)
2000 call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)
2001
2002 call StopVimInTerminal(buf)
2003 call delete('XState')
2004endfunc
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002005
2006func Test_range()
2007 " destructuring
2008 let [x, y] = range(2)
2009 call assert_equal([0, 1], [x, y])
2010
2011 " index
2012 call assert_equal(4, range(1, 10)[3])
2013
2014 " add()
2015 call assert_equal([0, 1, 2, 3], add(range(3), 3))
2016 call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3)))
2017 call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3)))
2018
2019 " append()
2020 new
2021 call append('.', range(5))
2022 call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$'))
2023 bwipe!
2024
2025 " appendbufline()
2026 new
2027 call appendbufline(bufnr(''), '.', range(5))
2028 call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$'))
2029 bwipe!
2030
2031 " call()
2032 func TwoArgs(a, b)
2033 return [a:a, a:b]
2034 endfunc
2035 call assert_equal([0, 1], call('TwoArgs', range(2)))
2036
2037 " col()
2038 new
2039 call setline(1, ['foo', 'bar'])
2040 call assert_equal(2, col(range(1, 2)))
2041 bwipe!
2042
2043 " complete()
2044 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>"
2045 " complete_info()
2046 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>"
2047
2048 " copy()
2049 call assert_equal([1, 2, 3], copy(range(1, 3)))
2050
2051 " count()
2052 call assert_equal(0, count(range(0), 3))
2053 call assert_equal(0, count(range(2), 3))
2054 call assert_equal(1, count(range(5), 3))
2055
2056 " cursor()
2057 new
2058 call setline(1, ['aaa', 'bbb', 'ccc'])
2059 call cursor(range(1, 2))
2060 call assert_equal([2, 1], [col('.'), line('.')])
2061 bwipe!
2062
2063 " deepcopy()
2064 call assert_equal([1, 2, 3], deepcopy(range(1, 3)))
2065
2066 " empty()
2067 call assert_true(empty(range(0)))
2068 call assert_false(empty(range(2)))
2069
2070 " execute()
2071 new
2072 call setline(1, ['aaa', 'bbb', 'ccc'])
2073 call execute(range(3))
2074 call assert_equal(2, line('.'))
2075 bwipe!
2076
2077 " extend()
2078 call assert_equal([1, 2, 3, 4], extend([1], range(2, 4)))
2079 call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4)))
2080 call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4]))
2081
2082 " filter()
2083 call assert_equal([1, 3], filter(range(5), 'v:val % 2'))
2084
2085 " funcref()
2086 call assert_equal([0, 1], funcref('TwoArgs', range(2))())
2087
2088 " function()
2089 call assert_equal([0, 1], function('TwoArgs', range(2))())
2090
2091 " garbagecollect()
2092 let thelist = [1, range(2), 3]
2093 let otherlist = range(3)
2094 call test_garbagecollect_now()
2095
2096 " get()
2097 call assert_equal(4, get(range(1, 10), 3))
2098 call assert_equal(-1, get(range(1, 10), 42, -1))
2099
2100 " index()
2101 call assert_equal(1, index(range(1, 5), 2))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002102 call assert_fails("echo index([1, 2], 1, [])", 'E745:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002103
2104 " inputlist()
Bram Moolenaar272ca952020-01-28 20:49:11 +01002105 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x')
2106 call assert_equal(1, result)
2107 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x')
2108 call assert_equal(1, result)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002109
2110 " insert()
2111 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42))
2112 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0))
2113 call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1))
2114 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4))
2115 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1))
2116 call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5))
2117
2118 " join()
2119 call assert_equal('0 1 2 3 4', join(range(5)))
2120
Bram Moolenaar272ca952020-01-28 20:49:11 +01002121 " json_encode()
2122 call assert_equal('[0,1,2,3]', json_encode(range(4)))
2123
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002124 " len()
2125 call assert_equal(0, len(range(0)))
2126 call assert_equal(2, len(range(2)))
2127 call assert_equal(5, len(range(0, 12, 3)))
2128 call assert_equal(4, len(range(3, 0, -1)))
2129
2130 " list2str()
2131 call assert_equal('ABC', list2str(range(65, 67)))
Bram Moolenaar08f41572020-04-20 16:50:00 +02002132 call assert_fails('let s = list2str(5)', 'E474:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002133
2134 " lock()
2135 let thelist = range(5)
2136 lockvar thelist
2137
2138 " map()
2139 call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2'))
2140
2141 " match()
2142 call assert_equal(3, match(range(5), 3))
2143
2144 " matchaddpos()
2145 highlight MyGreenGroup ctermbg=green guibg=green
2146 call matchaddpos('MyGreenGroup', range(line('.'), line('.')))
2147
2148 " matchend()
2149 call assert_equal(4, matchend(range(5), '4'))
2150 call assert_equal(3, matchend(range(1, 5), '4'))
2151 call assert_equal(-1, matchend(range(1, 5), '42'))
2152
2153 " matchstrpos()
2154 call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4'))
2155 call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4'))
2156 call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42'))
2157
2158 " max() reverse()
2159 call assert_equal(0, max(range(0)))
2160 call assert_equal(0, max(range(10, 9)))
2161 call assert_equal(9, max(range(10)))
2162 call assert_equal(18, max(range(0, 20, 3)))
2163 call assert_equal(20, max(range(20, 0, -3)))
2164 call assert_equal(99999, max(range(100000)))
2165 call assert_equal(99999, max(range(99999, 0, -1)))
2166 call assert_equal(99999, max(reverse(range(100000))))
2167 call assert_equal(99999, max(reverse(range(99999, 0, -1))))
2168
2169 " min() reverse()
2170 call assert_equal(0, min(range(0)))
2171 call assert_equal(0, min(range(10, 9)))
2172 call assert_equal(5, min(range(5, 10)))
2173 call assert_equal(5, min(range(5, 10, 3)))
2174 call assert_equal(2, min(range(20, 0, -3)))
2175 call assert_equal(0, min(range(100000)))
2176 call assert_equal(0, min(range(99999, 0, -1)))
2177 call assert_equal(0, min(reverse(range(100000))))
2178 call assert_equal(0, min(reverse(range(99999, 0, -1))))
2179
2180 " remove()
2181 call assert_equal(1, remove(range(1, 10), 0))
2182 call assert_equal(2, remove(range(1, 10), 1))
2183 call assert_equal(9, remove(range(1, 10), 8))
2184 call assert_equal(10, remove(range(1, 10), 9))
2185 call assert_equal(10, remove(range(1, 10), -1))
2186 call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4))
2187
2188 " repeat()
2189 call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2))
2190 call assert_equal([0, 1, 2], repeat(range(3), 1))
2191 call assert_equal([], repeat(range(3), 0))
2192 call assert_equal([], repeat(range(5, 4), 2))
2193 call assert_equal([], repeat(range(5, 4), 0))
2194
2195 " reverse()
2196 call assert_equal([2, 1, 0], reverse(range(3)))
2197 call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1)))
2198 call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10)))
2199 call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20)))
2200 call assert_equal([16, 13, 10], reverse(range(10, 18, 3)))
2201 call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3)))
2202 call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3)))
2203 call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3)))
2204 call assert_equal([], reverse(range(0)))
2205
2206 " TODO: setpos()
2207 " new
2208 " call setline(1, repeat([''], bufnr('')))
2209 " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6))
2210 " call setpos('x', range(bufnr(''), bufnr('') + 3))
2211 " bwipe!
2212
2213 " setreg()
2214 call setreg('a', range(3))
2215 call assert_equal("0\n1\n2\n", getreg('a'))
2216
Bram Moolenaarb0992022020-01-30 14:55:42 +01002217 " settagstack()
2218 call settagstack(1, #{items : range(4)})
Bram Moolenaar94255df2020-02-05 20:10:33 +01002219
Bram Moolenaarb0992022020-01-30 14:55:42 +01002220 " sign_define()
2221 call assert_fails("call sign_define(range(5))", "E715:")
2222 call assert_fails("call sign_placelist(range(5))", "E715:")
2223
2224 " sign_undefine()
2225 call assert_fails("call sign_undefine(range(5))", "E908:")
2226
2227 " sign_unplacelist()
2228 call assert_fails("call sign_unplacelist(range(5))", "E715:")
2229
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002230 " sort()
2231 call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1)))
2232
2233 " string()
2234 call assert_equal('[0, 1, 2, 3, 4]', string(range(5)))
2235
Bram Moolenaarb0992022020-01-30 14:55:42 +01002236 " taglist() with 'tagfunc'
2237 func TagFunc(pattern, flags, info)
2238 return range(10)
2239 endfunc
2240 set tagfunc=TagFunc
2241 call assert_fails("call taglist('asdf')", 'E987:')
2242 set tagfunc=
Bram Moolenaar94255df2020-02-05 20:10:33 +01002243
Bram Moolenaarb0992022020-01-30 14:55:42 +01002244 " term_start()
Bram Moolenaar705724e2020-01-31 21:13:42 +01002245 if has('terminal') && has('termguicolors')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002246 call assert_fails('call term_start(range(3, 4))', 'E474:')
2247 let g:terminal_ansi_colors = range(16)
Bram Moolenaar94255df2020-02-05 20:10:33 +01002248 if has('win32')
2249 let cmd = "cmd /c dir"
2250 else
2251 let cmd = "ls"
2252 endif
2253 call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"})', 'E475:')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002254 unlet g:terminal_ansi_colors
2255 endif
2256
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002257 " type()
2258 call assert_equal(v:t_list, type(range(5)))
2259
2260 " uniq()
2261 call assert_equal([0, 1, 2, 3, 4], uniq(range(5)))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002262
2263 " errors
2264 call assert_fails('let x=range(2, 8, 0)', 'E726:')
2265 call assert_fails('let x=range(3, 1)', 'E727:')
2266 call assert_fails('let x=range(1, 3, -2)', 'E727:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002267 call assert_fails('let x=range([])', 'E745:')
2268 call assert_fails('let x=range(1, [])', 'E745:')
2269 call assert_fails('let x=range(1, 4, [])', 'E745:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002270endfunc
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002271
2272func Test_echoraw()
2273 CheckScreendump
2274
2275 " Normally used for escape codes, but let's test with a CR.
2276 let lines =<< trim END
2277 call echoraw("hello\<CR>x")
2278 END
2279 call writefile(lines, 'XTest_echoraw')
2280 let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40})
2281 call VerifyScreenDump(buf, 'Test_functions_echoraw', {})
2282
2283 " clean up
2284 call StopVimInTerminal(buf)
2285 call delete('XTest_echoraw')
2286endfunc
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002287
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002288" Test for echo highlighting
2289func Test_echohl()
2290 echohl Search
2291 echo 'Vim'
2292 call assert_equal('Vim', Screenline(&lines))
2293 " TODO: How to check the highlight group used by echohl?
2294 " ScreenAttrs() returns all zeros.
2295 echohl None
2296endfunc
2297
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002298" Test for the eval() function
2299func Test_eval()
2300 call assert_fails("call eval('5 a')", 'E488:')
2301endfunc
2302
2303" Test for the nr2char() function
2304func Test_nr2char()
2305 set encoding=latin1
2306 call assert_equal('@', nr2char(64))
2307 set encoding=utf8
2308 call assert_equal('a', nr2char(97, 1))
2309 call assert_equal('a', nr2char(97, 0))
Bram Moolenaarf7271e82020-05-24 18:45:07 +02002310
2311 call assert_equal("\x80\xfc\b\xf4\x80\xfeX\x80\xfeX\x80\xfeX", eval('"\<M-' .. nr2char(0x100000) .. '>"'))
Bram Moolenaar19193712020-05-25 23:01:42 +02002312 call assert_equal("\x80\xfc\b\xfd\x80\xfeX\x80\xfeX\x80\xfeX\x80\xfeX\x80\xfeX", eval('"\<M-' .. nr2char(0x40000000) .. '>"'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002313endfunc
2314
2315" Test for screenattr(), screenchar() and screenchars() functions
2316func Test_screen_functions()
2317 call assert_equal(-1, screenattr(-1, -1))
2318 call assert_equal(-1, screenchar(-1, -1))
2319 call assert_equal([], screenchars(-1, -1))
2320endfunc
2321
Bram Moolenaar08f41572020-04-20 16:50:00 +02002322" Test for getcurpos() and setpos()
2323func Test_getcurpos_setpos()
2324 new
2325 call setline(1, ['012345678', '012345678'])
2326 normal gg6l
2327 let sp = getcurpos()
2328 normal 0
2329 call setpos('.', sp)
2330 normal jyl
2331 call assert_equal('6', @")
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002332 call assert_equal(-1, setpos('.', test_null_list()))
2333 call assert_equal(-1, setpos('.', {}))
Bram Moolenaar08f41572020-04-20 16:50:00 +02002334 close!
2335endfunc
2336
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002337" Test for glob()
2338func Test_glob()
2339 call assert_equal('', glob(test_null_string()))
2340 call assert_equal('', globpath(test_null_string(), test_null_string()))
2341endfunc
2342
2343" Test for browse()
2344func Test_browse()
2345 CheckFeature browse
2346 call assert_fails('call browse([], "open", "x", "a.c")', 'E745:')
2347endfunc
2348
2349" Test for browsedir()
2350func Test_browsedir()
2351 CheckFeature browse
2352 call assert_fails('call browsedir("open", [])', 'E730:')
2353endfunc
2354
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002355" vim: shiftwidth=2 sts=2 expandtab