1" Tests for various functions.
2source shared.vim
3source check.vim
4source term_util.vim
5source screendump.vim
6
7" 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
23func Test_has()
24  call assert_equal(1, has('eval'))
25  call assert_equal(1, has('eval', 1))
26
27  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
32
33  call assert_equal(0, has('nonexistent'))
34  call assert_equal(0, has('nonexistent', 1))
35
36  " Will we ever have patch 9999?
37  let ver = 'patch-' .. v:version / 100 .. '.' .. v:version % 100 .. '.9999'
38  call assert_equal(0, has(ver))
39endfunc
40
41func Test_empty()
42  call assert_equal(1, empty(''))
43  call assert_equal(0, empty('a'))
44
45  call assert_equal(1, empty(0))
46  call assert_equal(1, empty(-0))
47  call assert_equal(0, empty(1))
48  call assert_equal(0, empty(-1))
49
50  if has('float')
51    call assert_equal(1, empty(0.0))
52    call assert_equal(1, empty(-0.0))
53    call assert_equal(0, empty(1.0))
54    call assert_equal(0, empty(-1.0))
55    call assert_equal(0, empty(1.0/0.0))
56    call assert_equal(0, empty(0.0/0.0))
57  endif
58
59  call assert_equal(1, empty([]))
60  call assert_equal(0, empty(['a']))
61
62  call assert_equal(1, empty({}))
63  call assert_equal(0, empty({'a':1}))
64
65  call assert_equal(1, empty(v:null))
66  call assert_equal(1, empty(v:none))
67  call assert_equal(1, empty(v:false))
68  call assert_equal(0, empty(v:true))
69
70  if has('channel')
71    call assert_equal(1, empty(test_null_channel()))
72  endif
73  if has('job')
74    call assert_equal(1, empty(test_null_job()))
75  endif
76
77  call assert_equal(0, empty(function('Test_empty')))
78  call assert_equal(0, empty(function('Test_empty', [0])))
79
80  call assert_fails("call empty(test_void())", 'E685:')
81  call assert_fails("call empty(test_unknown())", 'E685:')
82endfunc
83
84func Test_test_void()
85  call assert_fails('echo 1 == test_void()', 'E685:')
86  if has('float')
87    call assert_fails('echo 1.0 == test_void()', 'E685:')
88  endif
89  call assert_fails('let x = json_encode(test_void())', 'E685:')
90  call assert_fails('let x = copy(test_void())', 'E685:')
91  call assert_fails('let x = copy([test_void()])', 'E685:')
92endfunc
93
94func Test_len()
95  call assert_equal(1, len(0))
96  call assert_equal(2, len(12))
97
98  call assert_equal(0, len(''))
99  call assert_equal(2, len('ab'))
100
101  call assert_equal(0, len([]))
102  call assert_equal(2, len([2, 1]))
103
104  call assert_equal(0, len({}))
105  call assert_equal(2, len({'a': 1, 'b': 2}))
106
107  call assert_fails('call len(v:none)', 'E701:')
108  call assert_fails('call len({-> 0})', 'E701:')
109endfunc
110
111func Test_max()
112  call assert_equal(0, max([]))
113  call assert_equal(2, max([2]))
114  call assert_equal(2, max([1, 2]))
115  call assert_equal(2, max([1, 2, v:null]))
116
117  call assert_equal(0, max({}))
118  call assert_equal(2, max({'a':1, 'b':2}))
119
120  call assert_fails('call max(1)', 'E712:')
121  call assert_fails('call max(v:none)', 'E712:')
122endfunc
123
124func Test_min()
125  call assert_equal(0, min([]))
126  call assert_equal(2, min([2]))
127  call assert_equal(1, min([1, 2]))
128  call assert_equal(0, min([1, 2, v:null]))
129
130  call assert_equal(0, min({}))
131  call assert_equal(1, min({'a':1, 'b':2}))
132
133  call assert_fails('call min(1)', 'E712:')
134  call assert_fails('call min(v:none)', 'E712:')
135endfunc
136
137func Test_strwidth()
138  for aw in ['single', 'double']
139    exe 'set ambiwidth=' . aw
140    call assert_equal(0, strwidth(''))
141    call assert_equal(1, strwidth("\t"))
142    call assert_equal(3, strwidth('Vim'))
143    call assert_equal(4, strwidth(1234))
144    call assert_equal(5, strwidth(-1234))
145
146    call assert_equal(2, strwidth('��'))
147    call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
148    call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
149
150    call assert_fails('call strwidth({->0})', 'E729:')
151    call assert_fails('call strwidth([])', 'E730:')
152    call assert_fails('call strwidth({})', 'E731:')
153    if has('float')
154      call assert_fails('call strwidth(1.2)', 'E806:')
155    endif
156  endfor
157
158  set ambiwidth&
159endfunc
160
161func Test_str2nr()
162  call assert_equal(0, str2nr(''))
163  call assert_equal(1, str2nr('1'))
164  call assert_equal(1, str2nr(' 1 '))
165
166  call assert_equal(1, str2nr('+1'))
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(123456789, str2nr('123456789'))
175  call assert_equal(-123456789, str2nr('-123456789'))
176
177  call assert_equal(5, str2nr('101', 2))
178  call assert_equal(5, '0b101'->str2nr(2))
179  call assert_equal(5, str2nr('0B101', 2))
180  call assert_equal(-5, str2nr('-101', 2))
181  call assert_equal(-5, str2nr('-0b101', 2))
182  call assert_equal(-5, str2nr('-0B101', 2))
183
184  call assert_equal(65, str2nr('101', 8))
185  call assert_equal(65, str2nr('0101', 8))
186  call assert_equal(-65, str2nr('-101', 8))
187  call assert_equal(-65, str2nr('-0101', 8))
188
189  call assert_equal(11259375, str2nr('abcdef', 16))
190  call assert_equal(11259375, str2nr('ABCDEF', 16))
191  call assert_equal(-11259375, str2nr('-ABCDEF', 16))
192  call assert_equal(11259375, str2nr('0xabcdef', 16))
193  call assert_equal(11259375, str2nr('0Xabcdef', 16))
194  call assert_equal(11259375, str2nr('0XABCDEF', 16))
195  call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
196
197  call assert_equal(1, str2nr("1'000'000", 10, 0))
198  call assert_equal(256, str2nr("1'0000'0000", 2, 1))
199  call assert_equal(262144, str2nr("1'000'000", 8, 1))
200  call assert_equal(1000000, str2nr("1'000'000", 10, 1))
201  call assert_equal(1000, str2nr("1'000''000", 10, 1))
202  call assert_equal(65536, str2nr("1'00'00", 16, 1))
203
204  call assert_equal(0, str2nr('0x10'))
205  call assert_equal(0, str2nr('0b10'))
206  call assert_equal(1, str2nr('12', 2))
207  call assert_equal(1, str2nr('18', 8))
208  call assert_equal(1, str2nr('1g', 16))
209
210  call assert_equal(0, str2nr(v:null))
211  call assert_equal(0, str2nr(v:none))
212
213  call assert_fails('call str2nr([])', 'E730:')
214  call assert_fails('call str2nr({->2})', 'E729:')
215  if has('float')
216    call assert_fails('call str2nr(1.2)', 'E806:')
217  endif
218  call assert_fails('call str2nr(10, [])', 'E474:')
219endfunc
220
221func Test_strftime()
222  CheckFunction strftime
223
224  " Format of strftime() depends on system. We assume
225  " that basic formats tested here are available and
226  " identical on all systems which support strftime().
227  "
228  " The 2nd parameter of strftime() is a local time, so the output day
229  " of strftime() can be 17 or 18, depending on timezone.
230  call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
231  "
232  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())
233
234  call assert_fails('call strftime([])', 'E730:')
235  call assert_fails('call strftime("%Y", [])', 'E745:')
236
237  " Check that the time changes after we change the timezone
238  " Save previous timezone value, if any
239  if exists('$TZ')
240    let tz = $TZ
241  endif
242
243  " Force EST and then UTC, save the current hour (24-hour clock) for each
244  let $TZ = 'EST' | let est = strftime('%H')
245  let $TZ = 'UTC' | let utc = strftime('%H')
246
247  " Those hours should be two bytes long, and should not be the same; if they
248  " are, a tzset(3) call may have failed somewhere
249  call assert_equal(strlen(est), 2)
250  call assert_equal(strlen(utc), 2)
251  " TODO: this fails on MS-Windows
252  if has('unix')
253    call assert_notequal(est, utc)
254  endif
255
256  " If we cached a timezone value, put it back, otherwise clear it
257  if exists('tz')
258    let $TZ = tz
259  else
260    unlet $TZ
261  endif
262endfunc
263
264func Test_strptime()
265  CheckFunction strptime
266
267  if exists('$TZ')
268    let tz = $TZ
269  endif
270  let $TZ = 'UTC'
271
272  call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
273
274  call assert_fails('call strptime()', 'E119:')
275  call assert_fails('call strptime("xxx")', 'E119:')
276  call assert_equal(0, strptime("%Y", ''))
277  call assert_equal(0, strptime("%Y", "xxx"))
278
279  if exists('tz')
280    let $TZ = tz
281  else
282    unlet $TZ
283  endif
284endfunc
285
286func Test_resolve_unix()
287  if !has('unix')
288    return
289  endif
290
291  " Xlink1 -> Xlink2
292  " Xlink2 -> Xlink3
293  silent !ln -s -f Xlink2 Xlink1
294  silent !ln -s -f Xlink3 Xlink2
295  call assert_equal('Xlink3', resolve('Xlink1'))
296  call assert_equal('./Xlink3', resolve('./Xlink1'))
297  call assert_equal('Xlink3/', resolve('Xlink2/'))
298  " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
299  "call assert_equal('Xlink3/', resolve('Xlink1/'))
300  "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
301  "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
302  call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
303
304  " Test resolve() with a symlink cycle.
305  " Xlink1 -> Xlink2
306  " Xlink2 -> Xlink3
307  " Xlink3 -> Xlink1
308  silent !ln -s -f Xlink1 Xlink3
309  call assert_fails('call resolve("Xlink1")',   'E655:')
310  call assert_fails('call resolve("./Xlink1")', 'E655:')
311  call assert_fails('call resolve("Xlink2")',   'E655:')
312  call assert_fails('call resolve("Xlink3")',   'E655:')
313  call delete('Xlink1')
314  call delete('Xlink2')
315  call delete('Xlink3')
316
317  silent !ln -s -f Xdir//Xfile Xlink
318  call assert_equal('Xdir/Xfile', resolve('Xlink'))
319  call delete('Xlink')
320
321  silent !ln -s -f Xlink2/ Xlink1
322  call assert_equal('Xlink2', 'Xlink1'->resolve())
323  call assert_equal('Xlink2/', resolve('Xlink1/'))
324  call delete('Xlink1')
325
326  silent !ln -s -f ./Xlink2 Xlink1
327  call assert_equal('Xlink2', resolve('Xlink1'))
328  call assert_equal('./Xlink2', resolve('./Xlink1'))
329  call delete('Xlink1')
330endfunc
331
332func s:normalize_fname(fname)
333  let ret = substitute(a:fname, '\', '/', 'g')
334  let ret = substitute(ret, '//', '/', 'g')
335  return ret->tolower()
336endfunc
337
338func Test_resolve_win32()
339  if !has('win32')
340    return
341  endif
342
343  " test for shortcut file
344  if executable('cscript')
345    new Xfile
346    wq
347    let lines =<< trim END
348	Set fs = CreateObject("Scripting.FileSystemObject")
349	Set ws = WScript.CreateObject("WScript.Shell")
350	Set shortcut = ws.CreateShortcut("Xlink.lnk")
351	shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")
352	shortcut.Save
353    END
354    call writefile(lines, 'link.vbs')
355    silent !cscript link.vbs
356    call delete('link.vbs')
357    call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
358    call delete('Xfile')
359
360    call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
361    call delete('Xlink.lnk')
362  else
363    echomsg 'skipped test for shortcut file'
364  endif
365
366  " remove files
367  call delete('Xlink')
368  call delete('Xdir', 'd')
369  call delete('Xfile')
370
371  " test for symbolic link to a file
372  new Xfile
373  wq
374  call assert_equal('Xfile', resolve('Xfile'))
375  silent !mklink Xlink Xfile
376  if !v:shell_error
377    call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink')))
378    call delete('Xlink')
379  else
380    echomsg 'skipped test for symbolic link to a file'
381  endif
382  call delete('Xfile')
383
384  " test for junction to a directory
385  call mkdir('Xdir')
386  silent !mklink /J Xlink Xdir
387  if !v:shell_error
388    call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
389
390    call delete('Xdir', 'd')
391
392    " test for junction already removed
393    call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
394    call delete('Xlink')
395  else
396    echomsg 'skipped test for junction to a directory'
397    call delete('Xdir', 'd')
398  endif
399
400  " test for symbolic link to a directory
401  call mkdir('Xdir')
402  silent !mklink /D Xlink Xdir
403  if !v:shell_error
404    call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
405
406    call delete('Xdir', 'd')
407
408    " test for symbolic link already removed
409    call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
410    call delete('Xlink')
411  else
412    echomsg 'skipped test for symbolic link to a directory'
413    call delete('Xdir', 'd')
414  endif
415
416  " test for buffer name
417  new Xfile
418  wq
419  silent !mklink Xlink Xfile
420  if !v:shell_error
421    edit Xlink
422    call assert_equal('Xlink', bufname('%'))
423    call delete('Xlink')
424    bw!
425  else
426    echomsg 'skipped test for buffer name'
427  endif
428  call delete('Xfile')
429
430  " test for reparse point
431  call mkdir('Xdir')
432  call assert_equal('Xdir', resolve('Xdir'))
433  silent !mklink /D Xdirlink Xdir
434  if !v:shell_error
435    w Xdir/text.txt
436    call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
437    call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
438    call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
439    call delete('Xdirlink')
440  else
441    echomsg 'skipped test for reparse point'
442  endif
443
444  call delete('Xdir', 'rf')
445endfunc
446
447func Test_simplify()
448  call assert_equal('',            simplify(''))
449  call assert_equal('/',           simplify('/'))
450  call assert_equal('/',           simplify('/.'))
451  call assert_equal('/',           simplify('/..'))
452  call assert_equal('/...',        simplify('/...'))
453  call assert_equal('./dir/file',  './dir/file'->simplify())
454  call assert_equal('./dir/file',  simplify('.///dir//file'))
455  call assert_equal('./dir/file',  simplify('./dir/./file'))
456  call assert_equal('./file',      simplify('./dir/../file'))
457  call assert_equal('../dir/file', simplify('dir/../../dir/file'))
458  call assert_equal('./file',      simplify('dir/.././file'))
459
460  call assert_fails('call simplify({->0})', 'E729:')
461  call assert_fails('call simplify([])', 'E730:')
462  call assert_fails('call simplify({})', 'E731:')
463  if has('float')
464    call assert_fails('call simplify(1.2)', 'E806:')
465  endif
466endfunc
467
468func Test_pathshorten()
469  call assert_equal('', pathshorten(''))
470  call assert_equal('foo', pathshorten('foo'))
471  call assert_equal('/foo', '/foo'->pathshorten())
472  call assert_equal('f/', pathshorten('foo/'))
473  call assert_equal('f/bar', pathshorten('foo/bar'))
474  call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
475  call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
476  call assert_equal('.f/bar', pathshorten('.foo/bar'))
477  call assert_equal('~f/bar', pathshorten('~foo/bar'))
478  call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
479  call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
480  call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
481endfunc
482
483func Test_strpart()
484  call assert_equal('de', strpart('abcdefg', 3, 2))
485  call assert_equal('ab', strpart('abcdefg', -2, 4))
486  call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
487  call assert_equal('fg', strpart('abcdefg', 5, 4))
488  call assert_equal('defg', strpart('abcdefg', 3))
489  call assert_equal('', strpart('abcdefg', 10))
490  call assert_fails("let s=strpart('abcdef', [])", 'E745:')
491
492  call assert_equal('lép', strpart('éléphant', 2, 4))
493  call assert_equal('léphant', strpart('éléphant', 2))
494endfunc
495
496func Test_tolower()
497  call assert_equal("", tolower(""))
498
499  " Test with all printable ASCII characters.
500  call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
501          \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
502
503  " Test with a few uppercase diacritics.
504  call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
505  call assert_equal("bḃḇ", tolower("BḂḆ"))
506  call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
507  call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
508  call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
509  call assert_equal("fḟ ", tolower("FḞ "))
510  call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
511  call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
512  call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
513  call assert_equal("jĵ", tolower("JĴ"))
514  call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
515  call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
516  call assert_equal("mḿṁ", tolower("MḾṀ"))
517  call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
518  call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
519  call assert_equal("pṕṗ", tolower("PṔṖ"))
520  call assert_equal("q", tolower("Q"))
521  call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
522  call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
523  call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
524  call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
525  call assert_equal("vṽ", tolower("VṼ"))
526  call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
527  call assert_equal("xẋẍ", tolower("XẊẌ"))
528  call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
529  call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
530
531  " Test with a few lowercase diacritics, which should remain unchanged.
532  call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
533  call assert_equal("bḃḇ", tolower("bḃḇ"))
534  call assert_equal("cçćĉċč", tolower("cçćĉċč"))
535  call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
536  call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
537  call assert_equal("fḟ", tolower("fḟ"))
538  call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
539  call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
540  call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
541  call assert_equal("jĵǰ", tolower("jĵǰ"))
542  call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
543  call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
544  call assert_equal("mḿṁ ", tolower("mḿṁ "))
545  call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
546  call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
547  call assert_equal("pṕṗ", tolower("pṕṗ"))
548  call assert_equal("q", tolower("q"))
549  call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
550  call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
551  call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
552  call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
553  call assert_equal("vṽ", tolower("vṽ"))
554  call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
555  call assert_equal("ẋẍ", tolower("ẋẍ"))
556  call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
557  call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
558
559  " According to https://twitter.com/jifa/status/625776454479970304
560  " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
561  " in length (2 to 3 bytes) when lowercased. So let's test them.
562  call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
563
564  " This call to tolower with invalid utf8 sequence used to cause access to
565  " invalid memory.
566  call tolower("\xC0\x80\xC0")
567  call tolower("123\xC0\x80\xC0")
568endfunc
569
570func Test_toupper()
571  call assert_equal("", toupper(""))
572
573  " Test with all printable ASCII characters.
574  call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
575          \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
576
577  " Test with a few lowercase diacritics.
578  call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
579  call assert_equal("BḂḆ", toupper("bḃḇ"))
580  call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
581  call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
582  call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
583  call assert_equal("FḞ", toupper("fḟ"))
584  call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
585  call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
586  call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
587  call assert_equal("JĴǰ", toupper("jĵǰ"))
588  call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
589  call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
590  call assert_equal("MḾṀ ", toupper("mḿṁ "))
591  call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
592  call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
593  call assert_equal("PṔṖ", toupper("pṕṗ"))
594  call assert_equal("Q", toupper("q"))
595  call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
596  call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
597  call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
598  call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
599  call assert_equal("VṼ", toupper("vṽ"))
600  call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
601  call assert_equal("ẊẌ", toupper("ẋẍ"))
602  call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
603  call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
604
605  " Test that uppercase diacritics, which should remain unchanged.
606  call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
607  call assert_equal("BḂḆ", toupper("BḂḆ"))
608  call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
609  call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
610  call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
611  call assert_equal("FḞ ", toupper("FḞ "))
612  call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
613  call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
614  call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
615  call assert_equal("JĴ", toupper("JĴ"))
616  call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
617  call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
618  call assert_equal("MḾṀ", toupper("MḾṀ"))
619  call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
620  call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
621  call assert_equal("PṔṖ", toupper("PṔṖ"))
622  call assert_equal("Q", toupper("Q"))
623  call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
624  call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
625  call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
626  call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
627  call assert_equal("VṼ", toupper("VṼ"))
628  call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
629  call assert_equal("XẊẌ", toupper("XẊẌ"))
630  call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
631  call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
632
633  call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
634
635  " This call to toupper with invalid utf8 sequence used to cause access to
636  " invalid memory.
637  call toupper("\xC0\x80\xC0")
638  call toupper("123\xC0\x80\xC0")
639endfunc
640
641func Test_tr()
642  call assert_equal('foo', tr('bar', 'bar', 'foo'))
643  call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
644  call assert_fails("let s=tr([], 'abc', 'def')", 'E730:')
645  call assert_fails("let s=tr('abc', [], 'def')", 'E730:')
646  call assert_fails("let s=tr('abc', 'abc', [])", 'E730:')
647  call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
648  set encoding=latin1
649  call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
650  call assert_equal('hEllO', tr('hello', 'eo', 'EO'))
651  call assert_equal('hello', tr('hello', 'xy', 'ab'))
652  set encoding=utf8
653endfunc
654
655" Tests for the mode() function
656let current_modes = ''
657func Save_mode()
658  let g:current_modes = mode(0) . '-' . mode(1)
659  return ''
660endfunc
661
662" Test for the mode() function
663func Test_mode()
664  new
665  call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
666
667  " Only complete from the current buffer.
668  set complete=.
669
670  inoremap <F2> <C-R>=Save_mode()<CR>
671
672  normal! 3G
673  exe "normal i\<F2>\<Esc>"
674  call assert_equal('i-i', g:current_modes)
675  " i_CTRL-P: Multiple matches
676  exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
677  call assert_equal('i-ic', g:current_modes)
678  " i_CTRL-P: Single match
679  exe "normal iBro\<C-P>\<F2>\<Esc>u"
680  call assert_equal('i-ic', g:current_modes)
681  " i_CTRL-X
682  exe "normal iBa\<C-X>\<F2>\<Esc>u"
683  call assert_equal('i-ix', g:current_modes)
684  " i_CTRL-X CTRL-P: Multiple matches
685  exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
686  call assert_equal('i-ic', g:current_modes)
687  " i_CTRL-X CTRL-P: Single match
688  exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
689  call assert_equal('i-ic', g:current_modes)
690  " i_CTRL-X CTRL-P + CTRL-P: Single match
691  exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
692  call assert_equal('i-ic', g:current_modes)
693  " i_CTRL-X CTRL-L: Multiple matches
694  exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
695  call assert_equal('i-ic', g:current_modes)
696  " i_CTRL-X CTRL-L: Single match
697  exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
698  call assert_equal('i-ic', g:current_modes)
699  " i_CTRL-P: No match
700  exe "normal iCom\<C-P>\<F2>\<Esc>u"
701  call assert_equal('i-ic', g:current_modes)
702  " i_CTRL-X CTRL-P: No match
703  exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
704  call assert_equal('i-ic', g:current_modes)
705  " i_CTRL-X CTRL-L: No match
706  exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
707  call assert_equal('i-ic', g:current_modes)
708
709  " R_CTRL-P: Multiple matches
710  exe "normal RBa\<C-P>\<F2>\<Esc>u"
711  call assert_equal('R-Rc', g:current_modes)
712  " R_CTRL-P: Single match
713  exe "normal RBro\<C-P>\<F2>\<Esc>u"
714  call assert_equal('R-Rc', g:current_modes)
715  " R_CTRL-X
716  exe "normal RBa\<C-X>\<F2>\<Esc>u"
717  call assert_equal('R-Rx', g:current_modes)
718  " R_CTRL-X CTRL-P: Multiple matches
719  exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
720  call assert_equal('R-Rc', g:current_modes)
721  " R_CTRL-X CTRL-P: Single match
722  exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
723  call assert_equal('R-Rc', g:current_modes)
724  " R_CTRL-X CTRL-P + CTRL-P: Single match
725  exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
726  call assert_equal('R-Rc', g:current_modes)
727  " R_CTRL-X CTRL-L: Multiple matches
728  exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
729  call assert_equal('R-Rc', g:current_modes)
730  " R_CTRL-X CTRL-L: Single match
731  exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
732  call assert_equal('R-Rc', g:current_modes)
733  " R_CTRL-P: No match
734  exe "normal RCom\<C-P>\<F2>\<Esc>u"
735  call assert_equal('R-Rc', g:current_modes)
736  " R_CTRL-X CTRL-P: No match
737  exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
738  call assert_equal('R-Rc', g:current_modes)
739  " R_CTRL-X CTRL-L: No match
740  exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
741  call assert_equal('R-Rc', g:current_modes)
742
743  call assert_equal('n', 0->mode())
744  call assert_equal('n', 1->mode())
745
746  " i_CTRL-O
747  exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
748  call assert_equal("n-niI", g:current_modes)
749
750  " R_CTRL-O
751  exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
752  call assert_equal("n-niR", g:current_modes)
753
754  " gR_CTRL-O
755  exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
756  call assert_equal("n-niV", g:current_modes)
757
758  " How to test operator-pending mode?
759
760  call feedkeys("v", 'xt')
761  call assert_equal('v', mode())
762  call assert_equal('v', mode(1))
763  call feedkeys("\<Esc>V", 'xt')
764  call assert_equal('V', mode())
765  call assert_equal('V', mode(1))
766  call feedkeys("\<Esc>\<C-V>", 'xt')
767  call assert_equal("\<C-V>", mode())
768  call assert_equal("\<C-V>", mode(1))
769  call feedkeys("\<Esc>", 'xt')
770
771  call feedkeys("gh", 'xt')
772  call assert_equal('s', mode())
773  call assert_equal('s', mode(1))
774  call feedkeys("\<Esc>gH", 'xt')
775  call assert_equal('S', mode())
776  call assert_equal('S', mode(1))
777  call feedkeys("\<Esc>g\<C-H>", 'xt')
778  call assert_equal("\<C-S>", mode())
779  call assert_equal("\<C-S>", mode(1))
780  call feedkeys("\<Esc>", 'xt')
781
782  call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
783  call assert_equal('c-c', g:current_modes)
784  call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
785  call assert_equal('c-cv', g:current_modes)
786  call feedkeys("Qcall Save_mode()\<CR>vi\<CR>", 'xt')
787  call assert_equal('c-ce', g:current_modes)
788  " How to test Ex mode?
789
790  bwipe!
791  iunmap <F2>
792  set complete&
793endfunc
794
795func Test_append()
796  enew!
797  split
798  call append(0, ["foo"])
799  split
800  only
801  undo
802endfunc
803
804func Test_getbufvar()
805  let bnr = bufnr('%')
806  let b:var_num = '1234'
807  let def_num = '5678'
808  call assert_equal('1234', getbufvar(bnr, 'var_num'))
809  call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
810
811  let bd = getbufvar(bnr, '')
812  call assert_equal('1234', bd['var_num'])
813  call assert_true(exists("bd['changedtick']"))
814  call assert_equal(2, len(bd))
815
816  let bd2 = getbufvar(bnr, '', def_num)
817  call assert_equal(bd, bd2)
818
819  unlet b:var_num
820  call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
821  call assert_equal('', getbufvar(bnr, 'var_num'))
822
823  let bd = getbufvar(bnr, '')
824  call assert_equal(1, len(bd))
825  let bd = getbufvar(bnr, '',def_num)
826  call assert_equal(1, len(bd))
827
828  call assert_equal('', getbufvar(9999, ''))
829  call assert_equal(def_num, getbufvar(9999, '', def_num))
830  unlet def_num
831
832  call assert_equal(0, getbufvar(bnr, '&autoindent'))
833  call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
834
835  " Set and get a buffer-local variable
836  call setbufvar(bnr, 'bufvar_test', ['one', 'two'])
837  call assert_equal(['one', 'two'], getbufvar(bnr, 'bufvar_test'))
838
839  " Open new window with forced option values
840  set fileformats=unix,dos
841  new ++ff=dos ++bin ++enc=iso-8859-2
842  call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
843  call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
844  call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
845  close
846
847  " Get the b: dict.
848  let b:testvar = 'one'
849  new
850  let b:testvar = 'two'
851  let thebuf = bufnr()
852  wincmd w
853  call assert_equal('two', getbufvar(thebuf, 'testvar'))
854  call assert_equal('two', getbufvar(thebuf, '').testvar)
855  bwipe!
856
857  set fileformats&
858endfunc
859
860func Test_last_buffer_nr()
861  call assert_equal(bufnr('$'), last_buffer_nr())
862endfunc
863
864func Test_stridx()
865  call assert_equal(-1, stridx('', 'l'))
866  call assert_equal(0,  stridx('', ''))
867  call assert_equal(0,  'hello'->stridx(''))
868  call assert_equal(-1, stridx('hello', 'L'))
869  call assert_equal(2,  stridx('hello', 'l', -1))
870  call assert_equal(2,  stridx('hello', 'l', 0))
871  call assert_equal(2,  'hello'->stridx('l', 1))
872  call assert_equal(3,  stridx('hello', 'l', 3))
873  call assert_equal(-1, stridx('hello', 'l', 4))
874  call assert_equal(-1, stridx('hello', 'l', 10))
875  call assert_equal(2,  stridx('hello', 'll'))
876  call assert_equal(-1, stridx('hello', 'hello world'))
877  call assert_fails("let n=stridx('hello', [])", 'E730:')
878  call assert_fails("let n=stridx([], 'l')", 'E730:')
879endfunc
880
881func Test_strridx()
882  call assert_equal(-1, strridx('', 'l'))
883  call assert_equal(0,  strridx('', ''))
884  call assert_equal(5,  strridx('hello', ''))
885  call assert_equal(-1, strridx('hello', 'L'))
886  call assert_equal(3,  'hello'->strridx('l'))
887  call assert_equal(3,  strridx('hello', 'l', 10))
888  call assert_equal(3,  strridx('hello', 'l', 3))
889  call assert_equal(2,  strridx('hello', 'l', 2))
890  call assert_equal(-1, strridx('hello', 'l', 1))
891  call assert_equal(-1, strridx('hello', 'l', 0))
892  call assert_equal(-1, strridx('hello', 'l', -1))
893  call assert_equal(2,  strridx('hello', 'll'))
894  call assert_equal(-1, strridx('hello', 'hello world'))
895  call assert_fails("let n=strridx('hello', [])", 'E730:')
896  call assert_fails("let n=strridx([], 'l')", 'E730:')
897endfunc
898
899func Test_match_func()
900  call assert_equal(4,  match('testing', 'ing'))
901  call assert_equal(4,  'testing'->match('ing', 2))
902  call assert_equal(-1, match('testing', 'ing', 5))
903  call assert_equal(-1, match('testing', 'ing', 8))
904  call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
905  call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
906  call assert_fails("let x=match('vim', [])", 'E730:')
907  call assert_equal(3, match(['a', 'b', 'c', 'a'], 'a', 1))
908  call assert_equal(-1, match(['a', 'b', 'c', 'a'], 'a', 5))
909  call assert_equal(4,  match('testing', 'ing', -1))
910  call assert_fails("let x=match('testing', 'ing', 0, [])", 'E745:')
911endfunc
912
913func Test_matchend()
914  call assert_equal(7,  matchend('testing', 'ing'))
915  call assert_equal(7,  'testing'->matchend('ing', 2))
916  call assert_equal(-1, matchend('testing', 'ing', 5))
917  call assert_equal(-1, matchend('testing', 'ing', 8))
918  call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
919  call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
920endfunc
921
922func Test_matchlist()
923  call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''],  matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
924  call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''],  'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
925  call assert_equal([],  matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
926endfunc
927
928func Test_matchstr()
929  call assert_equal('ing',  matchstr('testing', 'ing'))
930  call assert_equal('ing',  'testing'->matchstr('ing', 2))
931  call assert_equal('', matchstr('testing', 'ing', 5))
932  call assert_equal('', matchstr('testing', 'ing', 8))
933  call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
934  call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
935endfunc
936
937func Test_matchstrpos()
938  call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
939  call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
940  call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
941  call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
942  call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
943  call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
944endfunc
945
946func Test_nextnonblank_prevnonblank()
947  new
948insert
949This
950
951
952is
953
954a
955Test
956.
957  call assert_equal(0, nextnonblank(-1))
958  call assert_equal(0, nextnonblank(0))
959  call assert_equal(1, nextnonblank(1))
960  call assert_equal(4, 2->nextnonblank())
961  call assert_equal(4, nextnonblank(3))
962  call assert_equal(4, nextnonblank(4))
963  call assert_equal(6, nextnonblank(5))
964  call assert_equal(6, nextnonblank(6))
965  call assert_equal(7, nextnonblank(7))
966  call assert_equal(0, 8->nextnonblank())
967
968  call assert_equal(0, prevnonblank(-1))
969  call assert_equal(0, prevnonblank(0))
970  call assert_equal(1, 1->prevnonblank())
971  call assert_equal(1, prevnonblank(2))
972  call assert_equal(1, prevnonblank(3))
973  call assert_equal(4, prevnonblank(4))
974  call assert_equal(4, 5->prevnonblank())
975  call assert_equal(6, prevnonblank(6))
976  call assert_equal(7, prevnonblank(7))
977  call assert_equal(0, prevnonblank(8))
978  bw!
979endfunc
980
981func Test_byte2line_line2byte()
982  new
983  set endofline
984  call setline(1, ['a', 'bc', 'd'])
985
986  set fileformat=unix
987  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
988  \                 map(range(-1, 8), 'byte2line(v:val)'))
989  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
990  \                 map(range(-1, 5), 'line2byte(v:val)'))
991
992  set fileformat=mac
993  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
994  \                 map(range(-1, 8), 'v:val->byte2line()'))
995  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
996  \                 map(range(-1, 5), 'v:val->line2byte()'))
997
998  set fileformat=dos
999  call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
1000  \                 map(range(-1, 11), 'byte2line(v:val)'))
1001  call assert_equal([-1, -1, 1, 4, 8, 11, -1],
1002  \                 map(range(-1, 5), 'line2byte(v:val)'))
1003
1004  bw!
1005  set noendofline nofixendofline
1006  normal a-
1007  for ff in ["unix", "mac", "dos"]
1008    let &fileformat = ff
1009    call assert_equal(1, line2byte(1))
1010    call assert_equal(2, line2byte(2))  " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
1011  endfor
1012
1013  set endofline& fixendofline& fileformat&
1014  bw!
1015endfunc
1016
1017" Test for byteidx() and byteidxcomp() functions
1018func Test_byteidx()
1019  let a = '.é.' " one char of two bytes
1020  call assert_equal(0, byteidx(a, 0))
1021  call assert_equal(0, byteidxcomp(a, 0))
1022  call assert_equal(1, byteidx(a, 1))
1023  call assert_equal(1, byteidxcomp(a, 1))
1024  call assert_equal(3, byteidx(a, 2))
1025  call assert_equal(3, byteidxcomp(a, 2))
1026  call assert_equal(4, byteidx(a, 3))
1027  call assert_equal(4, byteidxcomp(a, 3))
1028  call assert_equal(-1, byteidx(a, 4))
1029  call assert_equal(-1, byteidxcomp(a, 4))
1030
1031  let b = '.é.' " normal e with composing char
1032  call assert_equal(0, b->byteidx(0))
1033  call assert_equal(1, b->byteidx(1))
1034  call assert_equal(4, b->byteidx(2))
1035  call assert_equal(5, b->byteidx(3))
1036  call assert_equal(-1, b->byteidx(4))
1037  call assert_fails("call byteidx([], 0)", 'E730:')
1038
1039  call assert_equal(0, b->byteidxcomp(0))
1040  call assert_equal(1, b->byteidxcomp(1))
1041  call assert_equal(2, b->byteidxcomp(2))
1042  call assert_equal(4, b->byteidxcomp(3))
1043  call assert_equal(5, b->byteidxcomp(4))
1044  call assert_equal(-1, b->byteidxcomp(5))
1045  call assert_fails("call byteidxcomp([], 0)", 'E730:')
1046endfunc
1047
1048func Test_count()
1049  let l = ['a', 'a', 'A', 'b']
1050  call assert_equal(2, count(l, 'a'))
1051  call assert_equal(1, count(l, 'A'))
1052  call assert_equal(1, count(l, 'b'))
1053  call assert_equal(0, count(l, 'B'))
1054
1055  call assert_equal(2, count(l, 'a', 0))
1056  call assert_equal(1, count(l, 'A', 0))
1057  call assert_equal(1, count(l, 'b', 0))
1058  call assert_equal(0, count(l, 'B', 0))
1059
1060  call assert_equal(3, count(l, 'a', 1))
1061  call assert_equal(3, count(l, 'A', 1))
1062  call assert_equal(1, count(l, 'b', 1))
1063  call assert_equal(1, count(l, 'B', 1))
1064  call assert_equal(0, count(l, 'c', 1))
1065
1066  call assert_equal(1, count(l, 'a', 0, 1))
1067  call assert_equal(2, count(l, 'a', 1, 1))
1068  call assert_fails('call count(l, "a", 0, 10)', 'E684:')
1069  call assert_fails('call count(l, "a", [])', 'E745:')
1070
1071  let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
1072  call assert_equal(2, count(d, 'a'))
1073  call assert_equal(1, count(d, 'A'))
1074  call assert_equal(1, count(d, 'b'))
1075  call assert_equal(0, count(d, 'B'))
1076
1077  call assert_equal(2, count(d, 'a', 0))
1078  call assert_equal(1, count(d, 'A', 0))
1079  call assert_equal(1, count(d, 'b', 0))
1080  call assert_equal(0, count(d, 'B', 0))
1081
1082  call assert_equal(3, count(d, 'a', 1))
1083  call assert_equal(3, count(d, 'A', 1))
1084  call assert_equal(1, count(d, 'b', 1))
1085  call assert_equal(1, count(d, 'B', 1))
1086  call assert_equal(0, count(d, 'c', 1))
1087
1088  call assert_fails('call count(d, "a", 0, 1)', 'E474:')
1089
1090  call assert_equal(0, count("foo", "bar"))
1091  call assert_equal(1, count("foo", "oo"))
1092  call assert_equal(2, count("foo", "o"))
1093  call assert_equal(0, count("foo", "O"))
1094  call assert_equal(2, count("foo", "O", 1))
1095  call assert_equal(2, count("fooooo", "oo"))
1096  call assert_equal(0, count("foo", ""))
1097
1098  call assert_fails('call count(0, 0)', 'E712:')
1099endfunc
1100
1101func Test_changenr()
1102  new Xchangenr
1103  call assert_equal(0, changenr())
1104  norm ifoo
1105  call assert_equal(1, changenr())
1106  set undolevels=10
1107  norm Sbar
1108  call assert_equal(2, changenr())
1109  undo
1110  call assert_equal(1, changenr())
1111  redo
1112  call assert_equal(2, changenr())
1113  bw!
1114  set undolevels&
1115endfunc
1116
1117func Test_filewritable()
1118  new Xfilewritable
1119  write!
1120  call assert_equal(1, filewritable('Xfilewritable'))
1121
1122  call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1123  call assert_equal(0, filewritable('Xfilewritable'))
1124
1125  call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
1126  call assert_equal(1, 'Xfilewritable'->filewritable())
1127
1128  call assert_equal(0, filewritable('doesnotexist'))
1129
1130  call delete('Xfilewritable')
1131  bw!
1132endfunc
1133
1134func Test_Executable()
1135  if has('win32')
1136    call assert_equal(1, executable('notepad'))
1137    call assert_equal(1, 'notepad.exe'->executable())
1138    call assert_equal(0, executable('notepad.exe.exe'))
1139    call assert_equal(0, executable('shell32.dll'))
1140    call assert_equal(0, executable('win.ini'))
1141  elseif has('unix')
1142    call assert_equal(1, 'cat'->executable())
1143    call assert_equal(0, executable('nodogshere'))
1144
1145    " get "cat" path and remove the leading /
1146    let catcmd = exepath('cat')[1:]
1147    new
1148    " check that the relative path works in /
1149    lcd /
1150    call assert_equal(1, executable(catcmd))
1151    call assert_equal('/' .. catcmd, catcmd->exepath())
1152    bwipe
1153  endif
1154endfunc
1155
1156func Test_executable_longname()
1157  if !has('win32')
1158    return
1159  endif
1160
1161  let fname = 'X' . repeat('あ', 200) . '.bat'
1162  call writefile([], fname)
1163  call assert_equal(1, executable(fname))
1164  call delete(fname)
1165endfunc
1166
1167func Test_hostname()
1168  let hostname_vim = hostname()
1169  if has('unix')
1170    let hostname_system = systemlist('uname -n')[0]
1171    call assert_equal(hostname_vim, hostname_system)
1172  endif
1173endfunc
1174
1175func Test_getpid()
1176  " getpid() always returns the same value within a vim instance.
1177  call assert_equal(getpid(), getpid())
1178  if has('unix')
1179    call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1180  endif
1181endfunc
1182
1183func Test_hlexists()
1184  call assert_equal(0, hlexists('does_not_exist'))
1185  call assert_equal(0, 'Number'->hlexists())
1186  call assert_equal(0, highlight_exists('does_not_exist'))
1187  call assert_equal(0, highlight_exists('Number'))
1188  syntax on
1189  call assert_equal(0, hlexists('does_not_exist'))
1190  call assert_equal(1, hlexists('Number'))
1191  call assert_equal(0, highlight_exists('does_not_exist'))
1192  call assert_equal(1, highlight_exists('Number'))
1193  syntax off
1194endfunc
1195
1196func Test_col()
1197  new
1198  call setline(1, 'abcdef')
1199  norm gg4|mx6|mY2|
1200  call assert_equal(2, col('.'))
1201  call assert_equal(7, col('$'))
1202  call assert_equal(2, col('v'))
1203  call assert_equal(4, col("'x"))
1204  call assert_equal(6, col("'Y"))
1205  call assert_equal(2, [1, 2]->col())
1206  call assert_equal(7, col([1, '$']))
1207
1208  call assert_equal(0, col(''))
1209  call assert_equal(0, col('x'))
1210  call assert_equal(0, col([2, '$']))
1211  call assert_equal(0, col([1, 100]))
1212  call assert_equal(0, col([1]))
1213
1214  " test for getting the visual start column
1215  func T()
1216    let g:Vcol = col('v')
1217    return ''
1218  endfunc
1219  let g:Vcol = 0
1220  xmap <expr> <F2> T()
1221  exe "normal gg3|ve\<F2>"
1222  call assert_equal(3, g:Vcol)
1223  xunmap <F2>
1224  delfunc T
1225
1226  " Test for the visual line start and end marks '< and '>
1227  call setline(1, ['one', 'one two', 'one two three'])
1228  "normal! ggVG
1229  call feedkeys("ggVG\<Esc>", 'xt')
1230  call assert_equal(1, col("'<"))
1231  call assert_equal(14, col("'>"))
1232  " Delete the last line of the visually selected region
1233  $d
1234  call assert_notequal(14, col("'>"))
1235
1236  " Test with 'virtualedit'
1237  set virtualedit=all
1238  call cursor(1, 10)
1239  call assert_equal(4, col('.'))
1240  set virtualedit&
1241
1242  bw!
1243endfunc
1244
1245" Test for input()
1246func Test_input_func()
1247  " Test for prompt with multiple lines
1248  redir => v
1249  call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt')
1250  redir END
1251  call assert_equal("B", c)
1252  call assert_equal(['A', 'B', 'C'], split(v, "\n"))
1253
1254  " Test for default value
1255  call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt')
1256  call assert_equal('red', c)
1257
1258  " Test for completion at the input prompt
1259  func! Tcomplete(arglead, cmdline, pos)
1260    return "item1\nitem2\nitem3"
1261  endfunc
1262  call feedkeys(":let c = input('Q? ', '' , 'custom,Tcomplete')\<CR>"
1263        \ .. "\<C-A>\<CR>", 'xt')
1264  delfunc Tcomplete
1265  call assert_equal('item1 item2 item3', c)
1266
1267  call assert_fails("call input('F:', '', 'invalid')", 'E180:')
1268  call assert_fails("call input('F:', '', [])", 'E730:')
1269endfunc
1270
1271" Test for the inputdialog() function
1272func Test_inputdialog()
1273  CheckNotGui
1274
1275  call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt')
1276  call assert_equal('xx', v)
1277  call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt')
1278  call assert_equal('yy', v)
1279endfunc
1280
1281" Test for inputlist()
1282func Test_inputlist()
1283  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1284  call assert_equal(1, c)
1285  call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
1286  call assert_equal(2, c)
1287  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1288  call assert_equal(3, c)
1289
1290  " Use backspace to delete characters in the prompt
1291  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<BS>3\<BS>2\<cr>", 'tx')
1292  call assert_equal(2, c)
1293
1294  " Use mouse to make a selection
1295  call test_setmouse(&lines - 3, 2)
1296  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
1297  call assert_equal(1, c)
1298  " Mouse click outside of the list
1299  call test_setmouse(&lines - 6, 2)
1300  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
1301  call assert_equal(-2, c)
1302
1303  call assert_fails('call inputlist("")', 'E686:')
1304endfunc
1305
1306func Test_balloon_show()
1307  if has('balloon_eval')
1308    " This won't do anything but must not crash either.
1309    call balloon_show('hi!')
1310    if !has('gui_running')
1311      call balloon_show(range(3))
1312    endif
1313  endif
1314endfunc
1315
1316func Test_setbufvar_options()
1317  " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1318  " window layout.
1319  call assert_equal(1, winnr('$'))
1320  split dummy_preview
1321  resize 2
1322  set winfixheight winfixwidth
1323  let prev_id = win_getid()
1324
1325  wincmd j
1326  let wh = winheight('.')
1327  let dummy_buf = bufnr('dummy_buf1', v:true)
1328  call setbufvar(dummy_buf, '&buftype', 'nofile')
1329  execute 'belowright vertical split #' . dummy_buf
1330  call assert_equal(wh, winheight('.'))
1331  let dum1_id = win_getid()
1332
1333  wincmd h
1334  let wh = winheight('.')
1335  let dummy_buf = bufnr('dummy_buf2', v:true)
1336  eval 'nofile'->setbufvar(dummy_buf, '&buftype')
1337  execute 'belowright vertical split #' . dummy_buf
1338  call assert_equal(wh, winheight('.'))
1339
1340  bwipe!
1341  call win_gotoid(prev_id)
1342  bwipe!
1343  call win_gotoid(dum1_id)
1344  bwipe!
1345endfunc
1346
1347func Test_redo_in_nested_functions()
1348  nnoremap g. :set opfunc=Operator<CR>g@
1349  function Operator( type, ... )
1350     let @x = 'XXX'
1351     execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1352  endfunction
1353
1354  function! Apply()
1355      5,6normal! .
1356  endfunction
1357
1358  new
1359  call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1360  1normal g.i"
1361  call assert_equal('some "XXX" text', getline(1))
1362  3,4normal .
1363  call assert_equal('some "XXX" text', getline(3))
1364  call assert_equal('more "XXX" text', getline(4))
1365  call Apply()
1366  call assert_equal('some "XXX" text', getline(5))
1367  call assert_equal('more "XXX" text', getline(6))
1368  bwipe!
1369
1370  nunmap g.
1371  delfunc Operator
1372  delfunc Apply
1373endfunc
1374
1375func Test_shellescape()
1376  let save_shell = &shell
1377  set shell=bash
1378  call assert_equal("'text'", shellescape('text'))
1379  call assert_equal("'te\"xt'", 'te"xt'->shellescape())
1380  call assert_equal("'te'\\''xt'", shellescape("te'xt"))
1381
1382  call assert_equal("'te%xt'", shellescape("te%xt"))
1383  call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
1384  call assert_equal("'te#xt'", shellescape("te#xt"))
1385  call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
1386  call assert_equal("'te!xt'", shellescape("te!xt"))
1387  call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1388
1389  call assert_equal("'te\nxt'", shellescape("te\nxt"))
1390  call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1391  set shell=tcsh
1392  call assert_equal("'te\\!xt'", shellescape("te!xt"))
1393  call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1394  call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1395  call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1396
1397  let &shell = save_shell
1398endfunc
1399
1400func Test_trim()
1401  call assert_equal("Testing", trim("  \t\r\r\x0BTesting  \t\n\r\n\t\x0B\x0B"))
1402  call assert_equal("Testing", "  \t  \r\r\n\n\x0BTesting  \t\n\r\n\t\x0B\x0B"->trim())
1403  call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1404  call assert_equal("wRE    \tSERVEzyww", trim("wRE    \tSERVEzyww"))
1405  call assert_equal("abcd\t     xxxx   tail", trim(" \tabcd\t     xxxx   tail"))
1406  call assert_equal("\tabcd\t     xxxx   tail", trim(" \tabcd\t     xxxx   tail", " "))
1407  call assert_equal(" \tabcd\t     xxxx   tail", trim(" \tabcd\t     xxxx   tail", "abx"))
1408  call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1409  call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1410  call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r   你好您R E SER V E早好你你    \t  \x0B", ))
1411  call assert_equal("您R E SER V E早好你你    \t  \x0B", trim("    你好您R E SER V E早好你你    \t  \x0B", " 你好"))
1412  call assert_equal("您R E SER V E早好你你    \t  \x0B", trim("    tteesstttt你好您R E SER V E早好你你    \t  \x0B ttestt", " 你好tes"))
1413  call assert_equal("您R E SER V E早好你你    \t  \x0B", trim("    tteesstttt你好您R E SER V E早好你你    \t  \x0B ttestt", "   你你你好好好tttsses"))
1414  call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1415  call assert_equal("", trim("", ""))
1416  call assert_equal("a", trim("a", ""))
1417  call assert_equal("", trim("", "a"))
1418
1419  let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
1420  call assert_equal("x", trim(chars . "x" . chars))
1421
1422  call assert_fails('let c=trim([])', 'E730:')
1423endfunc
1424
1425" Test for reg_recording() and reg_executing()
1426func Test_reg_executing_and_recording()
1427  let s:reg_stat = ''
1428  func s:save_reg_stat()
1429    let s:reg_stat = reg_recording() . ':' . reg_executing()
1430    return ''
1431  endfunc
1432
1433  new
1434  call s:save_reg_stat()
1435  call assert_equal(':', s:reg_stat)
1436  call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1437  call assert_equal('a:', s:reg_stat)
1438  call feedkeys("@a", 'xt')
1439  call assert_equal(':a', s:reg_stat)
1440  call feedkeys("qb@aq", 'xt')
1441  call assert_equal('b:a', s:reg_stat)
1442  call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1443  call assert_equal('":', s:reg_stat)
1444
1445  " :normal command saves and restores reg_executing
1446  let s:reg_stat = ''
1447  let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1448  func TestFunc() abort
1449    normal! ia
1450  endfunc
1451  call feedkeys("@q", 'xt')
1452  call assert_equal(':q', s:reg_stat)
1453  delfunc TestFunc
1454
1455  " getchar() command saves and restores reg_executing
1456  map W :call TestFunc()<CR>
1457  let @q = "W"
1458  let g:typed = ''
1459  let g:regs = []
1460  func TestFunc() abort
1461    let g:regs += [reg_executing()]
1462    let g:typed = getchar(0)
1463    let g:regs += [reg_executing()]
1464  endfunc
1465  call feedkeys("@qy", 'xt')
1466  call assert_equal(char2nr("y"), g:typed)
1467  call assert_equal(['q', 'q'], g:regs)
1468  delfunc TestFunc
1469  unmap W
1470  unlet g:typed
1471  unlet g:regs
1472
1473  " input() command saves and restores reg_executing
1474  map W :call TestFunc()<CR>
1475  let @q = "W"
1476  let g:typed = ''
1477  let g:regs = []
1478  func TestFunc() abort
1479    let g:regs += [reg_executing()]
1480    let g:typed = '?'->input()
1481    let g:regs += [reg_executing()]
1482  endfunc
1483  call feedkeys("@qy\<CR>", 'xt')
1484  call assert_equal("y", g:typed)
1485  call assert_equal(['q', 'q'], g:regs)
1486  delfunc TestFunc
1487  unmap W
1488  unlet g:typed
1489  unlet g:regs
1490
1491  bwipe!
1492  delfunc s:save_reg_stat
1493  unlet s:reg_stat
1494endfunc
1495
1496func Test_inputsecret()
1497  map W :call TestFunc()<CR>
1498  let @q = "W"
1499  let g:typed1 = ''
1500  let g:typed2 = ''
1501  let g:regs = []
1502  func TestFunc() abort
1503    let g:typed1 = '?'->inputsecret()
1504    let g:typed2 = inputsecret('password: ')
1505  endfunc
1506  call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1507  call assert_equal("something", g:typed1)
1508  call assert_equal("else", g:typed2)
1509  delfunc TestFunc
1510  unmap W
1511  unlet g:typed1
1512  unlet g:typed2
1513endfunc
1514
1515func Test_getchar()
1516  call feedkeys('a', '')
1517  call assert_equal(char2nr('a'), getchar())
1518
1519  call setline(1, 'xxxx')
1520  call test_setmouse(1, 3)
1521  let v:mouse_win = 9
1522  let v:mouse_winid = 9
1523  let v:mouse_lnum = 9
1524  let v:mouse_col = 9
1525  call feedkeys("\<S-LeftMouse>", '')
1526  call assert_equal("\<S-LeftMouse>", getchar())
1527  call assert_equal(1, v:mouse_win)
1528  call assert_equal(win_getid(1), v:mouse_winid)
1529  call assert_equal(1, v:mouse_lnum)
1530  call assert_equal(3, v:mouse_col)
1531  enew!
1532endfunc
1533
1534func Test_libcall_libcallnr()
1535  if !has('libcall')
1536    return
1537  endif
1538
1539  if has('win32')
1540    let libc = 'msvcrt.dll'
1541  elseif has('mac')
1542    let libc = 'libSystem.B.dylib'
1543  elseif executable('ldd')
1544    let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1545  endif
1546  if get(l:, 'libc', '') ==# ''
1547    " On Unix, libc.so can be in various places.
1548    if has('linux')
1549      " There is not documented but regarding the 1st argument of glibc's
1550      " dlopen an empty string and nullptr are equivalent, so using an empty
1551      " string for the 1st argument of libcall allows to call functions.
1552      let libc = ''
1553    elseif has('sun')
1554      " Set the path to libc.so according to the architecture.
1555      let test_bits = system('file ' . GetVimProg())
1556      let test_arch = system('uname -p')
1557      if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1558        let libc = '/usr/lib/sparcv9/libc.so'
1559      elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1560        let libc = '/usr/lib/amd64/libc.so'
1561      else
1562        let libc = '/usr/lib/libc.so'
1563      endif
1564    else
1565      " Unfortunately skip this test until a good way is found.
1566      return
1567    endif
1568  endif
1569
1570  if has('win32')
1571    call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
1572  else
1573    call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
1574  endif
1575
1576  " If function returns NULL, libcall() should return an empty string.
1577  call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1578
1579  " Test libcallnr() with string and integer argument.
1580  call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1581  call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
1582
1583  call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1584  call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1585
1586  call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1587  call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1588endfunc
1589
1590sandbox function Fsandbox()
1591  normal ix
1592endfunc
1593
1594func Test_func_sandbox()
1595  sandbox let F = {-> 'hello'}
1596  call assert_equal('hello', F())
1597
1598  sandbox let F = {-> "normal ix\<Esc>"->execute()}
1599  call assert_fails('call F()', 'E48:')
1600  unlet F
1601
1602  call assert_fails('call Fsandbox()', 'E48:')
1603  delfunc Fsandbox
1604
1605  " From a sandbox try to set a predefined variable (which cannot be modified
1606  " from a sandbox)
1607  call assert_fails('sandbox let v:lnum = 10', 'E794:')
1608endfunc
1609
1610func EditAnotherFile()
1611  let word = expand('<cword>')
1612  edit Xfuncrange2
1613endfunc
1614
1615func Test_func_range_with_edit()
1616  " Define a function that edits another buffer, then call it with a range that
1617  " is invalid in that buffer.
1618  call writefile(['just one line'], 'Xfuncrange2')
1619  new
1620  eval 10->range()->setline(1)
1621  write Xfuncrange1
1622  call assert_fails('5,8call EditAnotherFile()', 'E16:')
1623
1624  call delete('Xfuncrange1')
1625  call delete('Xfuncrange2')
1626  bwipe!
1627endfunc
1628
1629func Test_func_exists_on_reload()
1630  call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1631  call assert_equal(0, exists('*ExistingFunction'))
1632  source Xfuncexists
1633  call assert_equal(1, '*ExistingFunction'->exists())
1634  " Redefining a function when reloading a script is OK.
1635  source Xfuncexists
1636  call assert_equal(1, exists('*ExistingFunction'))
1637
1638  " But redefining in another script is not OK.
1639  call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1640  call assert_fails('source Xfuncexists2', 'E122:')
1641
1642  delfunc ExistingFunction
1643  call assert_equal(0, exists('*ExistingFunction'))
1644  call writefile([
1645	\ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1646	\ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1647	\ ], 'Xfuncexists')
1648  call assert_fails('source Xfuncexists', 'E122:')
1649  call assert_equal(1, exists('*ExistingFunction'))
1650
1651  call delete('Xfuncexists2')
1652  call delete('Xfuncexists')
1653  delfunc ExistingFunction
1654endfunc
1655
1656" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1657func Test_confirm()
1658  CheckUnix
1659  CheckNotGui
1660
1661  call feedkeys('o', 'L')
1662  let a = confirm('Press O to proceed')
1663  call assert_equal(1, a)
1664
1665  call feedkeys('y', 'L')
1666  let a = 'Are you sure?'->confirm("&Yes\n&No")
1667  call assert_equal(1, a)
1668
1669  call feedkeys('n', 'L')
1670  let a = confirm('Are you sure?', "&Yes\n&No")
1671  call assert_equal(2, a)
1672
1673  " confirm() should return 0 when pressing CTRL-C.
1674  call feedkeys("\<C-C>", 'L')
1675  let a = confirm('Are you sure?', "&Yes\n&No")
1676  call assert_equal(0, a)
1677
1678  " <Esc> requires another character to avoid it being seen as the start of an
1679  " escape sequence.  Zero should be harmless.
1680  eval "\<Esc>0"->feedkeys('L')
1681  let a = confirm('Are you sure?', "&Yes\n&No")
1682  call assert_equal(0, a)
1683
1684  " Default choice is returned when pressing <CR>.
1685  call feedkeys("\<CR>", 'L')
1686  let a = confirm('Are you sure?', "&Yes\n&No")
1687  call assert_equal(1, a)
1688
1689  call feedkeys("\<CR>", 'L')
1690  let a = confirm('Are you sure?', "&Yes\n&No", 2)
1691  call assert_equal(2, a)
1692
1693  call feedkeys("\<CR>", 'L')
1694  let a = confirm('Are you sure?', "&Yes\n&No", 0)
1695  call assert_equal(0, a)
1696
1697  " Test with the {type} 4th argument
1698  for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
1699    call feedkeys('y', 'L')
1700    let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
1701    call assert_equal(1, a)
1702  endfor
1703
1704  call assert_fails('call confirm([])', 'E730:')
1705  call assert_fails('call confirm("Are you sure?", [])', 'E730:')
1706  call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
1707  call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
1708endfunc
1709
1710func Test_platform_name()
1711  " The system matches at most only one name.
1712  let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
1713  call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
1714
1715  " Is Unix?
1716  call assert_equal(has('beos'), has('beos') && has('unix'))
1717  call assert_equal(has('bsd'), has('bsd') && has('unix'))
1718  call assert_equal(has('hpux'), has('hpux') && has('unix'))
1719  call assert_equal(has('linux'), has('linux') && has('unix'))
1720  call assert_equal(has('mac'), has('mac') && has('unix'))
1721  call assert_equal(has('qnx'), has('qnx') && has('unix'))
1722  call assert_equal(has('sun'), has('sun') && has('unix'))
1723  call assert_equal(has('win32'), has('win32') && !has('unix'))
1724  call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
1725
1726  if has('unix') && executable('uname')
1727    let uname = system('uname')
1728    call assert_equal(uname =~? 'BeOS', has('beos'))
1729    " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
1730    call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
1731    call assert_equal(uname =~? 'HP-UX', has('hpux'))
1732    call assert_equal(uname =~? 'Linux', has('linux'))
1733    call assert_equal(uname =~? 'Darwin', has('mac'))
1734    call assert_equal(uname =~? 'QNX', has('qnx'))
1735    call assert_equal(uname =~? 'SunOS', has('sun'))
1736    call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
1737  endif
1738endfunc
1739
1740func Test_readdir()
1741  call mkdir('Xdir')
1742  call writefile([], 'Xdir/foo.txt')
1743  call writefile([], 'Xdir/bar.txt')
1744  call mkdir('Xdir/dir')
1745
1746  " All results
1747  let files = readdir('Xdir')
1748  call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
1749
1750  " Only results containing "f"
1751  let files = 'Xdir'->readdir({ x -> stridx(x, 'f') !=- 1 })
1752  call assert_equal(['foo.txt'], sort(files))
1753
1754  " Only .txt files
1755  let files = readdir('Xdir', { x -> x =~ '.txt$' })
1756  call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1757
1758  " Only .txt files with string
1759  let files = readdir('Xdir', 'v:val =~ ".txt$"')
1760  call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1761
1762  " Limit to 1 result.
1763  let l = []
1764  let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
1765  call assert_equal(1, len(files))
1766
1767  " Nested readdir() must not crash
1768  let files = readdir('Xdir', 'readdir("Xdir", "1") != []')
1769  call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
1770
1771  eval 'Xdir'->delete('rf')
1772endfunc
1773
1774func Test_delete_rf()
1775  call mkdir('Xdir')
1776  call writefile([], 'Xdir/foo.txt')
1777  call writefile([], 'Xdir/bar.txt')
1778  call mkdir('Xdir/[a-1]')  " issue #696
1779  call writefile([], 'Xdir/[a-1]/foo.txt')
1780  call writefile([], 'Xdir/[a-1]/bar.txt')
1781  call assert_true(filereadable('Xdir/foo.txt'))
1782  call assert_true('Xdir/[a-1]/foo.txt'->filereadable())
1783
1784  call assert_equal(0, delete('Xdir', 'rf'))
1785  call assert_false(filereadable('Xdir/foo.txt'))
1786  call assert_false(filereadable('Xdir/[a-1]/foo.txt'))
1787endfunc
1788
1789func Test_call()
1790  call assert_equal(3, call('len', [123]))
1791  call assert_equal(3, 'len'->call([123]))
1792  call assert_fails("call call('len', 123)", 'E714:')
1793  call assert_equal(0, call('', []))
1794
1795  function Mylen() dict
1796     return len(self.data)
1797  endfunction
1798  let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
1799  eval mydict.len->call([], mydict)->assert_equal(4)
1800  call assert_fails("call call('Mylen', [], 0)", 'E715:')
1801endfunc
1802
1803func Test_char2nr()
1804  call assert_equal(12354, char2nr('あ', 1))
1805  call assert_equal(120, 'x'->char2nr())
1806  set encoding=latin1
1807  call assert_equal(120, 'x'->char2nr())
1808  set encoding=utf-8
1809endfunc
1810
1811func Test_eventhandler()
1812  call assert_equal(0, eventhandler())
1813endfunc
1814
1815func Test_bufadd_bufload()
1816  call assert_equal(0, bufexists('someName'))
1817  let buf = bufadd('someName')
1818  call assert_notequal(0, buf)
1819  call assert_equal(1, bufexists('someName'))
1820  call assert_equal(0, getbufvar(buf, '&buflisted'))
1821  call assert_equal(0, bufloaded(buf))
1822  call bufload(buf)
1823  call assert_equal(1, bufloaded(buf))
1824  call assert_equal([''], getbufline(buf, 1, '$'))
1825
1826  let curbuf = bufnr('')
1827  eval ['some', 'text']->writefile('XotherName')
1828  let buf = 'XotherName'->bufadd()
1829  call assert_notequal(0, buf)
1830  eval 'XotherName'->bufexists()->assert_equal(1)
1831  call assert_equal(0, getbufvar(buf, '&buflisted'))
1832  call assert_equal(0, bufloaded(buf))
1833  eval buf->bufload()
1834  call assert_equal(1, bufloaded(buf))
1835  call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
1836  call assert_equal(curbuf, bufnr(''))
1837
1838  let buf1 = bufadd('')
1839  let buf2 = bufadd('')
1840  call assert_notequal(0, buf1)
1841  call assert_notequal(0, buf2)
1842  call assert_notequal(buf1, buf2)
1843  call assert_equal(1, bufexists(buf1))
1844  call assert_equal(1, bufexists(buf2))
1845  call assert_equal(0, bufloaded(buf1))
1846  exe 'bwipe ' .. buf1
1847  call assert_equal(0, bufexists(buf1))
1848  call assert_equal(1, bufexists(buf2))
1849  exe 'bwipe ' .. buf2
1850  call assert_equal(0, bufexists(buf2))
1851
1852  bwipe someName
1853  bwipe XotherName
1854  call assert_equal(0, bufexists('someName'))
1855  call delete('XotherName')
1856endfunc
1857
1858func Test_state()
1859  CheckRunVimInTerminal
1860
1861  let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"
1862
1863  let lines =<< trim END
1864	call setline(1, ['one', 'two', 'three'])
1865	map ;; gg
1866	set complete=.
1867	func RunTimer()
1868	  call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
1869	endfunc
1870	au Filetype foobar let g:state = state()|let g:mode = mode()
1871  END
1872  call writefile(lines, 'XState')
1873  let buf = RunVimInTerminal('-S XState', #{rows: 6})
1874
1875  " Using a ":" command Vim is busy, thus "S" is returned
1876  call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
1877  call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
1878  call term_sendkeys(buf, ":\<CR>")
1879
1880  " Using a timer callback
1881  call term_sendkeys(buf, ":call RunTimer()\<CR>")
1882  call TermWait(buf, 25)
1883  call term_sendkeys(buf, getstate)
1884  call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)
1885
1886  " Halfway a mapping
1887  call term_sendkeys(buf, ":call RunTimer()\<CR>;")
1888  call TermWait(buf, 25)
1889  call term_sendkeys(buf, ";")
1890  call term_sendkeys(buf, getstate)
1891  call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)
1892
1893  " Insert mode completion (bit slower on Mac)
1894  call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
1895  call TermWait(buf, 25)
1896  call term_sendkeys(buf, "\<Esc>")
1897  call term_sendkeys(buf, getstate)
1898  call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)
1899
1900  " Autocommand executing
1901  call term_sendkeys(buf, ":set filetype=foobar\<CR>")
1902  call TermWait(buf, 25)
1903  call term_sendkeys(buf, getstate)
1904  call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)
1905
1906  " Todo: "w" - waiting for ch_evalexpr()
1907
1908  " messages scrolled
1909  call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
1910  call TermWait(buf, 25)
1911  call term_sendkeys(buf, "\<CR>")
1912  call term_sendkeys(buf, getstate)
1913  call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)
1914
1915  call StopVimInTerminal(buf)
1916  call delete('XState')
1917endfunc
1918
1919func Test_range()
1920  " destructuring
1921  let [x, y] = range(2)
1922  call assert_equal([0, 1], [x, y])
1923
1924  " index
1925  call assert_equal(4, range(1, 10)[3])
1926
1927  " add()
1928  call assert_equal([0, 1, 2, 3], add(range(3), 3))
1929  call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3)))
1930  call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3)))
1931
1932  " append()
1933  new
1934  call append('.', range(5))
1935  call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$'))
1936  bwipe!
1937
1938  " appendbufline()
1939  new
1940  call appendbufline(bufnr(''), '.', range(5))
1941  call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$'))
1942  bwipe!
1943
1944  " call()
1945  func TwoArgs(a, b)
1946    return [a:a, a:b]
1947  endfunc
1948  call assert_equal([0, 1], call('TwoArgs', range(2)))
1949
1950  " col()
1951  new
1952  call setline(1, ['foo', 'bar'])
1953  call assert_equal(2, col(range(1, 2)))
1954  bwipe!
1955
1956  " complete()
1957  execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>"
1958  " complete_info()
1959  execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>"
1960  call assert_fails('call complete(1, ["a"])', 'E785:')
1961
1962  " copy()
1963  call assert_equal([1, 2, 3], copy(range(1, 3)))
1964
1965  " count()
1966  call assert_equal(0, count(range(0), 3))
1967  call assert_equal(0, count(range(2), 3))
1968  call assert_equal(1, count(range(5), 3))
1969
1970  " cursor()
1971  new
1972  call setline(1, ['aaa', 'bbb', 'ccc'])
1973  call cursor(range(1, 2))
1974  call assert_equal([2, 1], [col('.'), line('.')])
1975  bwipe!
1976
1977  " deepcopy()
1978  call assert_equal([1, 2, 3], deepcopy(range(1, 3)))
1979
1980  " empty()
1981  call assert_true(empty(range(0)))
1982  call assert_false(empty(range(2)))
1983
1984  " execute()
1985  new
1986  call setline(1, ['aaa', 'bbb', 'ccc'])
1987  call execute(range(3))
1988  call assert_equal(2, line('.'))
1989  bwipe!
1990
1991  " extend()
1992  call assert_equal([1, 2, 3, 4], extend([1], range(2, 4)))
1993  call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4)))
1994  call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4]))
1995
1996  " filter()
1997  call assert_equal([1, 3], filter(range(5), 'v:val % 2'))
1998
1999  " funcref()
2000  call assert_equal([0, 1], funcref('TwoArgs', range(2))())
2001
2002  " function()
2003  call assert_equal([0, 1], function('TwoArgs', range(2))())
2004
2005  " garbagecollect()
2006  let thelist = [1, range(2), 3]
2007  let otherlist = range(3)
2008  call test_garbagecollect_now()
2009
2010  " get()
2011  call assert_equal(4, get(range(1, 10), 3))
2012  call assert_equal(-1, get(range(1, 10), 42, -1))
2013
2014  " index()
2015  call assert_equal(1, index(range(1, 5), 2))
2016  call assert_fails("echo index([1, 2], 1, [])", 'E745:')
2017
2018  " inputlist()
2019  call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x')
2020  call assert_equal(1, result)
2021  call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x')
2022  call assert_equal(1, result)
2023
2024  " insert()
2025  call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42))
2026  call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0))
2027  call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1))
2028  call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4))
2029  call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1))
2030  call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5))
2031
2032  " join()
2033  call assert_equal('0 1 2 3 4', join(range(5)))
2034
2035  " json_encode()
2036  call assert_equal('[0,1,2,3]', json_encode(range(4)))
2037
2038  " len()
2039  call assert_equal(0, len(range(0)))
2040  call assert_equal(2, len(range(2)))
2041  call assert_equal(5, len(range(0, 12, 3)))
2042  call assert_equal(4, len(range(3, 0, -1)))
2043
2044  " list2str()
2045  call assert_equal('ABC', list2str(range(65, 67)))
2046
2047  " lock()
2048  let thelist = range(5)
2049  lockvar thelist
2050
2051  " map()
2052  call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2'))
2053
2054  " match()
2055  call assert_equal(3, match(range(5), 3))
2056
2057  " matchaddpos()
2058  highlight MyGreenGroup ctermbg=green guibg=green
2059  call matchaddpos('MyGreenGroup', range(line('.'), line('.')))
2060
2061  " matchend()
2062  call assert_equal(4, matchend(range(5), '4'))
2063  call assert_equal(3, matchend(range(1, 5), '4'))
2064  call assert_equal(-1, matchend(range(1, 5), '42'))
2065
2066  " matchstrpos()
2067  call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4'))
2068  call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4'))
2069  call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42'))
2070
2071  " max() reverse()
2072  call assert_equal(0, max(range(0)))
2073  call assert_equal(0, max(range(10, 9)))
2074  call assert_equal(9, max(range(10)))
2075  call assert_equal(18, max(range(0, 20, 3)))
2076  call assert_equal(20, max(range(20, 0, -3)))
2077  call assert_equal(99999, max(range(100000)))
2078  call assert_equal(99999, max(range(99999, 0, -1)))
2079  call assert_equal(99999, max(reverse(range(100000))))
2080  call assert_equal(99999, max(reverse(range(99999, 0, -1))))
2081
2082  " min() reverse()
2083  call assert_equal(0, min(range(0)))
2084  call assert_equal(0, min(range(10, 9)))
2085  call assert_equal(5, min(range(5, 10)))
2086  call assert_equal(5, min(range(5, 10, 3)))
2087  call assert_equal(2, min(range(20, 0, -3)))
2088  call assert_equal(0, min(range(100000)))
2089  call assert_equal(0, min(range(99999, 0, -1)))
2090  call assert_equal(0, min(reverse(range(100000))))
2091  call assert_equal(0, min(reverse(range(99999, 0, -1))))
2092
2093  " remove()
2094  call assert_equal(1, remove(range(1, 10), 0))
2095  call assert_equal(2, remove(range(1, 10), 1))
2096  call assert_equal(9, remove(range(1, 10), 8))
2097  call assert_equal(10, remove(range(1, 10), 9))
2098  call assert_equal(10, remove(range(1, 10), -1))
2099  call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4))
2100
2101  " repeat()
2102  call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2))
2103  call assert_equal([0, 1, 2], repeat(range(3), 1))
2104  call assert_equal([], repeat(range(3), 0))
2105  call assert_equal([], repeat(range(5, 4), 2))
2106  call assert_equal([], repeat(range(5, 4), 0))
2107
2108  " reverse()
2109  call assert_equal([2, 1, 0], reverse(range(3)))
2110  call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1)))
2111  call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10)))
2112  call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20)))
2113  call assert_equal([16, 13, 10], reverse(range(10, 18, 3)))
2114  call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3)))
2115  call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3)))
2116  call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3)))
2117  call assert_equal([], reverse(range(0)))
2118
2119  " TODO: setpos()
2120  " new
2121  " call setline(1, repeat([''], bufnr('')))
2122  " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6))
2123  " call setpos('x', range(bufnr(''), bufnr('') + 3))
2124  " bwipe!
2125
2126  " setreg()
2127  call setreg('a', range(3))
2128  call assert_equal("0\n1\n2\n", getreg('a'))
2129
2130  " settagstack()
2131  call settagstack(1, #{items : range(4)})
2132
2133  " sign_define()
2134  call assert_fails("call sign_define(range(5))", "E715:")
2135  call assert_fails("call sign_placelist(range(5))", "E715:")
2136
2137  " sign_undefine()
2138  call assert_fails("call sign_undefine(range(5))", "E908:")
2139
2140  " sign_unplacelist()
2141  call assert_fails("call sign_unplacelist(range(5))", "E715:")
2142
2143  " sort()
2144  call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1)))
2145
2146  " string()
2147  call assert_equal('[0, 1, 2, 3, 4]', string(range(5)))
2148
2149  " taglist() with 'tagfunc'
2150  func TagFunc(pattern, flags, info)
2151    return range(10)
2152  endfunc
2153  set tagfunc=TagFunc
2154  call assert_fails("call taglist('asdf')", 'E987:')
2155  set tagfunc=
2156
2157  " term_start()
2158  if has('terminal') && has('termguicolors')
2159    call assert_fails('call term_start(range(3, 4))', 'E474:')
2160    let g:terminal_ansi_colors = range(16)
2161    if has('win32')
2162      let cmd = "cmd /c dir"
2163    else
2164      let cmd = "ls"
2165    endif
2166    call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"})', 'E475:')
2167    unlet g:terminal_ansi_colors
2168  endif
2169
2170  " type()
2171  call assert_equal(v:t_list, type(range(5)))
2172
2173  " uniq()
2174  call assert_equal([0, 1, 2, 3, 4], uniq(range(5)))
2175
2176  " errors
2177  call assert_fails('let x=range(2, 8, 0)', 'E726:')
2178  call assert_fails('let x=range(3, 1)', 'E727:')
2179  call assert_fails('let x=range(1, 3, -2)', 'E727:')
2180endfunc
2181
2182func Test_echoraw()
2183  CheckScreendump
2184
2185  " Normally used for escape codes, but let's test with a CR.
2186  let lines =<< trim END
2187    call echoraw("hello\<CR>x")
2188  END
2189  call writefile(lines, 'XTest_echoraw')
2190  let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40})
2191  call VerifyScreenDump(buf, 'Test_functions_echoraw', {})
2192
2193  " clean up
2194  call StopVimInTerminal(buf)
2195  call delete('XTest_echoraw')
2196endfunc
2197
2198" Test for the eval() function
2199func Test_eval()
2200  call assert_fails("call eval('5 a')", 'E488:')
2201endfunc
2202
2203" Test for the nr2char() function
2204func Test_nr2char()
2205  set encoding=latin1
2206  call assert_equal('@', nr2char(64))
2207  set encoding=utf8
2208  call assert_equal('a', nr2char(97, 1))
2209  call assert_equal('a', nr2char(97, 0))
2210endfunc
2211
2212" Test for screenattr(), screenchar() and screenchars() functions
2213func Test_screen_functions()
2214  call assert_equal(-1, screenattr(-1, -1))
2215  call assert_equal(-1, screenchar(-1, -1))
2216  call assert_equal([], screenchars(-1, -1))
2217endfunc
2218
2219" vim: shiftwidth=2 sts=2 expandtab
2220