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