1" Tests for various functions.
2source shared.vim
3source check.vim
4
5" Must be done first, since the alternate buffer must be unset.
6func Test_00_bufexists()
7  call assert_equal(0, bufexists('does_not_exist'))
8  call assert_equal(1, bufexists(bufnr('%')))
9  call assert_equal(0, bufexists(0))
10  new Xfoo
11  let bn = bufnr('%')
12  call assert_equal(1, bufexists(bn))
13  call assert_equal(1, bufexists('Xfoo'))
14  call assert_equal(1, bufexists(getcwd() . '/Xfoo'))
15  call assert_equal(1, bufexists(0))
16  bw
17  call assert_equal(0, bufexists(bn))
18  call assert_equal(0, bufexists('Xfoo'))
19endfunc
20
21func Test_empty()
22  call assert_equal(1, empty(''))
23  call assert_equal(0, empty('a'))
24
25  call assert_equal(1, empty(0))
26  call assert_equal(1, empty(-0))
27  call assert_equal(0, empty(1))
28  call assert_equal(0, empty(-1))
29
30  call assert_equal(1, empty(0.0))
31  call assert_equal(1, empty(-0.0))
32  call assert_equal(0, empty(1.0))
33  call assert_equal(0, empty(-1.0))
34  call assert_equal(0, empty(1.0/0.0))
35  call assert_equal(0, empty(0.0/0.0))
36
37  call assert_equal(1, empty([]))
38  call assert_equal(0, empty(['a']))
39
40  call assert_equal(1, empty({}))
41  call assert_equal(0, empty({'a':1}))
42
43  call assert_equal(1, empty(v:null))
44  call assert_equal(1, empty(v:none))
45  call assert_equal(1, empty(v:false))
46  call assert_equal(0, empty(v:true))
47
48  if has('channel')
49    call assert_equal(1, empty(test_null_channel()))
50  endif
51  if has('job')
52    call assert_equal(1, empty(test_null_job()))
53  endif
54
55  call assert_equal(0, empty(function('Test_empty')))
56  call assert_equal(0, empty(function('Test_empty', [0])))
57endfunc
58
59func Test_len()
60  call assert_equal(1, len(0))
61  call assert_equal(2, len(12))
62
63  call assert_equal(0, len(''))
64  call assert_equal(2, len('ab'))
65
66  call assert_equal(0, len([]))
67  call assert_equal(2, len([2, 1]))
68
69  call assert_equal(0, len({}))
70  call assert_equal(2, len({'a': 1, 'b': 2}))
71
72  call assert_fails('call len(v:none)', 'E701:')
73  call assert_fails('call len({-> 0})', 'E701:')
74endfunc
75
76func Test_max()
77  call assert_equal(0, max([]))
78  call assert_equal(2, max([2]))
79  call assert_equal(2, max([1, 2]))
80  call assert_equal(2, max([1, 2, v:null]))
81
82  call assert_equal(0, max({}))
83  call assert_equal(2, max({'a':1, 'b':2}))
84
85  call assert_fails('call max(1)', 'E712:')
86  call assert_fails('call max(v:none)', 'E712:')
87endfunc
88
89func Test_min()
90  call assert_equal(0, min([]))
91  call assert_equal(2, min([2]))
92  call assert_equal(1, min([1, 2]))
93  call assert_equal(0, min([1, 2, v:null]))
94
95  call assert_equal(0, min({}))
96  call assert_equal(1, min({'a':1, 'b':2}))
97
98  call assert_fails('call min(1)', 'E712:')
99  call assert_fails('call min(v:none)', 'E712:')
100endfunc
101
102func Test_strwidth()
103  for aw in ['single', 'double']
104    exe 'set ambiwidth=' . aw
105    call assert_equal(0, strwidth(''))
106    call assert_equal(1, strwidth("\t"))
107    call assert_equal(3, strwidth('Vim'))
108    call assert_equal(4, strwidth(1234))
109    call assert_equal(5, strwidth(-1234))
110
111    call assert_equal(2, strwidth('��'))
112    call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
113    call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
114
115    call assert_fails('call strwidth({->0})', 'E729:')
116    call assert_fails('call strwidth([])', 'E730:')
117    call assert_fails('call strwidth({})', 'E731:')
118    call assert_fails('call strwidth(1.2)', 'E806:')
119  endfor
120
121  set ambiwidth&
122endfunc
123
124func Test_str2nr()
125  call assert_equal(0, str2nr(''))
126  call assert_equal(1, str2nr('1'))
127  call assert_equal(1, str2nr(' 1 '))
128
129  call assert_equal(1, str2nr('+1'))
130  call assert_equal(1, str2nr('+ 1'))
131  call assert_equal(1, str2nr(' + 1 '))
132
133  call assert_equal(-1, str2nr('-1'))
134  call assert_equal(-1, str2nr('- 1'))
135  call assert_equal(-1, str2nr(' - 1 '))
136
137  call assert_equal(123456789, str2nr('123456789'))
138  call assert_equal(-123456789, str2nr('-123456789'))
139
140  call assert_equal(5, str2nr('101', 2))
141  call assert_equal(5, '0b101'->str2nr(2))
142  call assert_equal(5, str2nr('0B101', 2))
143  call assert_equal(-5, str2nr('-101', 2))
144  call assert_equal(-5, str2nr('-0b101', 2))
145  call assert_equal(-5, str2nr('-0B101', 2))
146
147  call assert_equal(65, str2nr('101', 8))
148  call assert_equal(65, str2nr('0101', 8))
149  call assert_equal(-65, str2nr('-101', 8))
150  call assert_equal(-65, str2nr('-0101', 8))
151
152  call assert_equal(11259375, str2nr('abcdef', 16))
153  call assert_equal(11259375, str2nr('ABCDEF', 16))
154  call assert_equal(-11259375, str2nr('-ABCDEF', 16))
155  call assert_equal(11259375, str2nr('0xabcdef', 16))
156  call assert_equal(11259375, str2nr('0Xabcdef', 16))
157  call assert_equal(11259375, str2nr('0XABCDEF', 16))
158  call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
159
160  call assert_equal(1, str2nr("1'000'000", 10, 0))
161  call assert_equal(256, str2nr("1'0000'0000", 2, 1))
162  call assert_equal(262144, str2nr("1'000'000", 8, 1))
163  call assert_equal(1000000, str2nr("1'000'000", 10, 1))
164  call assert_equal(1000, str2nr("1'000''000", 10, 1))
165  call assert_equal(65536, str2nr("1'00'00", 16, 1))
166
167  call assert_equal(0, str2nr('0x10'))
168  call assert_equal(0, str2nr('0b10'))
169  call assert_equal(1, str2nr('12', 2))
170  call assert_equal(1, str2nr('18', 8))
171  call assert_equal(1, str2nr('1g', 16))
172
173  call assert_equal(0, str2nr(v:null))
174  call assert_equal(0, str2nr(v:none))
175
176  call assert_fails('call str2nr([])', 'E730:')
177  call assert_fails('call str2nr({->2})', 'E729:')
178  call assert_fails('call str2nr(1.2)', 'E806:')
179  call assert_fails('call str2nr(10, [])', 'E474:')
180endfunc
181
182func Test_strftime()
183  if !exists('*strftime')
184    return
185  endif
186  " Format of strftime() depends on system. We assume
187  " that basic formats tested here are available and
188  " identical on all systems which support strftime().
189  "
190  " The 2nd parameter of strftime() is a local time, so the output day
191  " of strftime() can be 17 or 18, depending on timezone.
192  call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
193  "
194  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())
195
196  call assert_fails('call strftime([])', 'E730:')
197  call assert_fails('call strftime("%Y", [])', 'E745:')
198
199  " Check that the time changes after we change the timezone
200  " Save previous timezone value, if any
201  if exists('$TZ')
202    let tz = $TZ
203  endif
204
205  " Force EST and then UTC, save the current hour (24-hour clock) for each
206  let $TZ = 'EST' | let est = strftime('%H')
207  let $TZ = 'UTC' | let utc = strftime('%H')
208
209  " Those hours should be two bytes long, and should not be the same; if they
210  " are, a tzset(3) call may have failed somewhere
211  call assert_equal(strlen(est), 2)
212  call assert_equal(strlen(utc), 2)
213  " TODO: this fails on MS-Windows
214  if has('unix')
215    call assert_notequal(est, utc)
216  endif
217
218  " If we cached a timezone value, put it back, otherwise clear it
219  if exists('tz')
220    let $TZ = tz
221  else
222    unlet $TZ
223  endif
224
225endfunc
226
227func Test_resolve_unix()
228  if !has('unix')
229    return
230  endif
231
232  " Xlink1 -> Xlink2
233  " Xlink2 -> Xlink3
234  silent !ln -s -f Xlink2 Xlink1
235  silent !ln -s -f Xlink3 Xlink2
236  call assert_equal('Xlink3', resolve('Xlink1'))
237  call assert_equal('./Xlink3', resolve('./Xlink1'))
238  call assert_equal('Xlink3/', resolve('Xlink2/'))
239  " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
240  "call assert_equal('Xlink3/', resolve('Xlink1/'))
241  "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
242  "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
243  call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
244
245  " Test resolve() with a symlink cycle.
246  " Xlink1 -> Xlink2
247  " Xlink2 -> Xlink3
248  " Xlink3 -> Xlink1
249  silent !ln -s -f Xlink1 Xlink3
250  call assert_fails('call resolve("Xlink1")',   'E655:')
251  call assert_fails('call resolve("./Xlink1")', 'E655:')
252  call assert_fails('call resolve("Xlink2")',   'E655:')
253  call assert_fails('call resolve("Xlink3")',   'E655:')
254  call delete('Xlink1')
255  call delete('Xlink2')
256  call delete('Xlink3')
257
258  silent !ln -s -f Xdir//Xfile Xlink
259  call assert_equal('Xdir/Xfile', resolve('Xlink'))
260  call delete('Xlink')
261
262  silent !ln -s -f Xlink2/ Xlink1
263  call assert_equal('Xlink2', 'Xlink1'->resolve())
264  call assert_equal('Xlink2/', resolve('Xlink1/'))
265  call delete('Xlink1')
266
267  silent !ln -s -f ./Xlink2 Xlink1
268  call assert_equal('Xlink2', resolve('Xlink1'))
269  call assert_equal('./Xlink2', resolve('./Xlink1'))
270  call delete('Xlink1')
271endfunc
272
273func s:normalize_fname(fname)
274  let ret = substitute(a:fname, '\', '/', 'g')
275  let ret = substitute(ret, '//', '/', 'g')
276  return ret->tolower()
277endfunc
278
279func Test_resolve_win32()
280  if !has('win32')
281    return
282  endif
283
284  " test for shortcut file
285  if executable('cscript')
286    new Xfile
287    wq
288    let lines =<< trim END
289	Set fs = CreateObject("Scripting.FileSystemObject")
290	Set ws = WScript.CreateObject("WScript.Shell")
291	Set shortcut = ws.CreateShortcut("Xlink.lnk")
292	shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")
293	shortcut.Save
294    END
295    call writefile(lines, 'link.vbs')
296    silent !cscript link.vbs
297    call delete('link.vbs')
298    call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
299    call delete('Xfile')
300
301    call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
302    call delete('Xlink.lnk')
303  else
304    echomsg 'skipped test for shortcut file'
305  endif
306
307  " remove files
308  call delete('Xlink')
309  call delete('Xdir', 'd')
310  call delete('Xfile')
311
312  " test for symbolic link to a file
313  new Xfile
314  wq
315  call assert_equal('Xfile', resolve('Xfile'))
316  silent !mklink Xlink Xfile
317  if !v:shell_error
318    call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink')))
319    call delete('Xlink')
320  else
321    echomsg 'skipped test for symbolic link to a file'
322  endif
323  call delete('Xfile')
324
325  " test for junction to a directory
326  call mkdir('Xdir')
327  silent !mklink /J Xlink Xdir
328  if !v:shell_error
329    call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
330
331    call delete('Xdir', 'd')
332
333    " test for junction already removed
334    call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
335    call delete('Xlink')
336  else
337    echomsg 'skipped test for junction to a directory'
338    call delete('Xdir', 'd')
339  endif
340
341  " test for symbolic link to a directory
342  call mkdir('Xdir')
343  silent !mklink /D Xlink Xdir
344  if !v:shell_error
345    call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
346
347    call delete('Xdir', 'd')
348
349    " test for symbolic link already removed
350    call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
351    call delete('Xlink')
352  else
353    echomsg 'skipped test for symbolic link to a directory'
354    call delete('Xdir', 'd')
355  endif
356
357  " test for buffer name
358  new Xfile
359  wq
360  silent !mklink Xlink Xfile
361  if !v:shell_error
362    edit Xlink
363    call assert_equal('Xlink', bufname('%'))
364    call delete('Xlink')
365    bw!
366  else
367    echomsg 'skipped test for buffer name'
368  endif
369  call delete('Xfile')
370
371  " test for reparse point
372  call mkdir('Xdir')
373  call assert_equal('Xdir', resolve('Xdir'))
374  silent !mklink /D Xdirlink Xdir
375  if !v:shell_error
376    w Xdir/text.txt
377    call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
378    call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
379    call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
380    call delete('Xdirlink')
381  else
382    echomsg 'skipped test for reparse point'
383  endif
384
385  call delete('Xdir', 'rf')
386endfunc
387
388func Test_simplify()
389  call assert_equal('',            simplify(''))
390  call assert_equal('/',           simplify('/'))
391  call assert_equal('/',           simplify('/.'))
392  call assert_equal('/',           simplify('/..'))
393  call assert_equal('/...',        simplify('/...'))
394  call assert_equal('./dir/file',  simplify('./dir/file'))
395  call assert_equal('./dir/file',  simplify('.///dir//file'))
396  call assert_equal('./dir/file',  simplify('./dir/./file'))
397  call assert_equal('./file',      simplify('./dir/../file'))
398  call assert_equal('../dir/file', simplify('dir/../../dir/file'))
399  call assert_equal('./file',      simplify('dir/.././file'))
400
401  call assert_fails('call simplify({->0})', 'E729:')
402  call assert_fails('call simplify([])', 'E730:')
403  call assert_fails('call simplify({})', 'E731:')
404  call assert_fails('call simplify(1.2)', 'E806:')
405endfunc
406
407func Test_pathshorten()
408  call assert_equal('', pathshorten(''))
409  call assert_equal('foo', pathshorten('foo'))
410  call assert_equal('/foo', '/foo'->pathshorten())
411  call assert_equal('f/', pathshorten('foo/'))
412  call assert_equal('f/bar', pathshorten('foo/bar'))
413  call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
414  call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
415  call assert_equal('.f/bar', pathshorten('.foo/bar'))
416  call assert_equal('~f/bar', pathshorten('~foo/bar'))
417  call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
418  call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
419  call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
420endfunc
421
422func Test_strpart()
423  call assert_equal('de', strpart('abcdefg', 3, 2))
424  call assert_equal('ab', strpart('abcdefg', -2, 4))
425  call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
426  call assert_equal('fg', strpart('abcdefg', 5, 4))
427  call assert_equal('defg', strpart('abcdefg', 3))
428
429  call assert_equal('lép', strpart('éléphant', 2, 4))
430  call assert_equal('léphant', strpart('éléphant', 2))
431endfunc
432
433func Test_tolower()
434  call assert_equal("", tolower(""))
435
436  " Test with all printable ASCII characters.
437  call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
438          \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
439
440  " Test with a few uppercase diacritics.
441  call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
442  call assert_equal("bḃḇ", tolower("BḂḆ"))
443  call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
444  call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
445  call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
446  call assert_equal("fḟ ", tolower("FḞ "))
447  call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
448  call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
449  call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
450  call assert_equal("jĵ", tolower("JĴ"))
451  call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
452  call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
453  call assert_equal("mḿṁ", tolower("MḾṀ"))
454  call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
455  call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
456  call assert_equal("pṕṗ", tolower("PṔṖ"))
457  call assert_equal("q", tolower("Q"))
458  call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
459  call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
460  call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
461  call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
462  call assert_equal("vṽ", tolower("VṼ"))
463  call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
464  call assert_equal("xẋẍ", tolower("XẊẌ"))
465  call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
466  call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
467
468  " Test with a few lowercase diacritics, which should remain unchanged.
469  call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
470  call assert_equal("bḃḇ", tolower("bḃḇ"))
471  call assert_equal("cçćĉċč", tolower("cçćĉċč"))
472  call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
473  call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
474  call assert_equal("fḟ", tolower("fḟ"))
475  call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
476  call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
477  call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
478  call assert_equal("jĵǰ", tolower("jĵǰ"))
479  call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
480  call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
481  call assert_equal("mḿṁ ", tolower("mḿṁ "))
482  call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
483  call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
484  call assert_equal("pṕṗ", tolower("pṕṗ"))
485  call assert_equal("q", tolower("q"))
486  call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
487  call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
488  call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
489  call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
490  call assert_equal("vṽ", tolower("vṽ"))
491  call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
492  call assert_equal("ẋẍ", tolower("ẋẍ"))
493  call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
494  call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
495
496  " According to https://twitter.com/jifa/status/625776454479970304
497  " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
498  " in length (2 to 3 bytes) when lowercased. So let's test them.
499  call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
500
501  " This call to tolower with invalid utf8 sequence used to cause access to
502  " invalid memory.
503  call tolower("\xC0\x80\xC0")
504  call tolower("123\xC0\x80\xC0")
505endfunc
506
507func Test_toupper()
508  call assert_equal("", toupper(""))
509
510  " Test with all printable ASCII characters.
511  call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
512          \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
513
514  " Test with a few lowercase diacritics.
515  call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
516  call assert_equal("BḂḆ", toupper("bḃḇ"))
517  call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
518  call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
519  call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
520  call assert_equal("FḞ", toupper("fḟ"))
521  call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
522  call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
523  call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
524  call assert_equal("JĴǰ", toupper("jĵǰ"))
525  call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
526  call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
527  call assert_equal("MḾṀ ", toupper("mḿṁ "))
528  call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
529  call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
530  call assert_equal("PṔṖ", toupper("pṕṗ"))
531  call assert_equal("Q", toupper("q"))
532  call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
533  call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
534  call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
535  call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
536  call assert_equal("VṼ", toupper("vṽ"))
537  call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
538  call assert_equal("ẊẌ", toupper("ẋẍ"))
539  call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
540  call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
541
542  " Test that uppercase diacritics, which should remain unchanged.
543  call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
544  call assert_equal("BḂḆ", toupper("BḂḆ"))
545  call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
546  call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
547  call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
548  call assert_equal("FḞ ", toupper("FḞ "))
549  call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
550  call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
551  call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
552  call assert_equal("JĴ", toupper("JĴ"))
553  call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
554  call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
555  call assert_equal("MḾṀ", toupper("MḾṀ"))
556  call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
557  call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
558  call assert_equal("PṔṖ", toupper("PṔṖ"))
559  call assert_equal("Q", toupper("Q"))
560  call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
561  call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
562  call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
563  call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
564  call assert_equal("VṼ", toupper("VṼ"))
565  call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
566  call assert_equal("XẊẌ", toupper("XẊẌ"))
567  call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
568  call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
569
570  call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
571
572  " This call to toupper with invalid utf8 sequence used to cause access to
573  " invalid memory.
574  call toupper("\xC0\x80\xC0")
575  call toupper("123\xC0\x80\xC0")
576endfunc
577
578func Test_tr()
579  call assert_equal('foo', tr('bar', 'bar', 'foo'))
580  call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
581endfunc
582
583" Tests for the mode() function
584let current_modes = ''
585func Save_mode()
586  let g:current_modes = mode(0) . '-' . mode(1)
587  return ''
588endfunc
589
590func Test_mode()
591  new
592  call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
593
594  " Only complete from the current buffer.
595  set complete=.
596
597  inoremap <F2> <C-R>=Save_mode()<CR>
598
599  normal! 3G
600  exe "normal i\<F2>\<Esc>"
601  call assert_equal('i-i', g:current_modes)
602  " i_CTRL-P: Multiple matches
603  exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
604  call assert_equal('i-ic', g:current_modes)
605  " i_CTRL-P: Single match
606  exe "normal iBro\<C-P>\<F2>\<Esc>u"
607  call assert_equal('i-ic', g:current_modes)
608  " i_CTRL-X
609  exe "normal iBa\<C-X>\<F2>\<Esc>u"
610  call assert_equal('i-ix', g:current_modes)
611  " i_CTRL-X CTRL-P: Multiple matches
612  exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
613  call assert_equal('i-ic', g:current_modes)
614  " i_CTRL-X CTRL-P: Single match
615  exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
616  call assert_equal('i-ic', g:current_modes)
617  " i_CTRL-X CTRL-P + CTRL-P: Single match
618  exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
619  call assert_equal('i-ic', g:current_modes)
620  " i_CTRL-X CTRL-L: Multiple matches
621  exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
622  call assert_equal('i-ic', g:current_modes)
623  " i_CTRL-X CTRL-L: Single match
624  exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
625  call assert_equal('i-ic', g:current_modes)
626  " i_CTRL-P: No match
627  exe "normal iCom\<C-P>\<F2>\<Esc>u"
628  call assert_equal('i-ic', g:current_modes)
629  " i_CTRL-X CTRL-P: No match
630  exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
631  call assert_equal('i-ic', g:current_modes)
632  " i_CTRL-X CTRL-L: No match
633  exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
634  call assert_equal('i-ic', g:current_modes)
635
636  " R_CTRL-P: Multiple matches
637  exe "normal RBa\<C-P>\<F2>\<Esc>u"
638  call assert_equal('R-Rc', g:current_modes)
639  " R_CTRL-P: Single match
640  exe "normal RBro\<C-P>\<F2>\<Esc>u"
641  call assert_equal('R-Rc', g:current_modes)
642  " R_CTRL-X
643  exe "normal RBa\<C-X>\<F2>\<Esc>u"
644  call assert_equal('R-Rx', g:current_modes)
645  " R_CTRL-X CTRL-P: Multiple matches
646  exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
647  call assert_equal('R-Rc', g:current_modes)
648  " R_CTRL-X CTRL-P: Single match
649  exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
650  call assert_equal('R-Rc', g:current_modes)
651  " R_CTRL-X CTRL-P + CTRL-P: Single match
652  exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
653  call assert_equal('R-Rc', g:current_modes)
654  " R_CTRL-X CTRL-L: Multiple matches
655  exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
656  call assert_equal('R-Rc', g:current_modes)
657  " R_CTRL-X CTRL-L: Single match
658  exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
659  call assert_equal('R-Rc', g:current_modes)
660  " R_CTRL-P: No match
661  exe "normal RCom\<C-P>\<F2>\<Esc>u"
662  call assert_equal('R-Rc', g:current_modes)
663  " R_CTRL-X CTRL-P: No match
664  exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
665  call assert_equal('R-Rc', g:current_modes)
666  " R_CTRL-X CTRL-L: No match
667  exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
668  call assert_equal('R-Rc', g:current_modes)
669
670  call assert_equal('n', 0->mode())
671  call assert_equal('n', 1->mode())
672
673  " i_CTRL-O
674  exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
675  call assert_equal("n-niI", g:current_modes)
676
677  " R_CTRL-O
678  exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
679  call assert_equal("n-niR", g:current_modes)
680
681  " gR_CTRL-O
682  exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
683  call assert_equal("n-niV", g:current_modes)
684
685  " How to test operator-pending mode?
686
687  call feedkeys("v", 'xt')
688  call assert_equal('v', mode())
689  call assert_equal('v', mode(1))
690  call feedkeys("\<Esc>V", 'xt')
691  call assert_equal('V', mode())
692  call assert_equal('V', mode(1))
693  call feedkeys("\<Esc>\<C-V>", 'xt')
694  call assert_equal("\<C-V>", mode())
695  call assert_equal("\<C-V>", mode(1))
696  call feedkeys("\<Esc>", 'xt')
697
698  call feedkeys("gh", 'xt')
699  call assert_equal('s', mode())
700  call assert_equal('s', mode(1))
701  call feedkeys("\<Esc>gH", 'xt')
702  call assert_equal('S', mode())
703  call assert_equal('S', mode(1))
704  call feedkeys("\<Esc>g\<C-H>", 'xt')
705  call assert_equal("\<C-S>", mode())
706  call assert_equal("\<C-S>", mode(1))
707  call feedkeys("\<Esc>", 'xt')
708
709  call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
710  call assert_equal('c-c', g:current_modes)
711  call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
712  call assert_equal('c-cv', g:current_modes)
713  " How to test Ex mode?
714
715  bwipe!
716  iunmap <F2>
717  set complete&
718endfunc
719
720func Test_append()
721  enew!
722  split
723  call append(0, ["foo"])
724  split
725  only
726  undo
727endfunc
728
729func Test_getbufvar()
730  let bnr = bufnr('%')
731  let b:var_num = '1234'
732  let def_num = '5678'
733  call assert_equal('1234', getbufvar(bnr, 'var_num'))
734  call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
735
736  let bd = getbufvar(bnr, '')
737  call assert_equal('1234', bd['var_num'])
738  call assert_true(exists("bd['changedtick']"))
739  call assert_equal(2, len(bd))
740
741  let bd2 = getbufvar(bnr, '', def_num)
742  call assert_equal(bd, bd2)
743
744  unlet b:var_num
745  call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
746  call assert_equal('', getbufvar(bnr, 'var_num'))
747
748  let bd = getbufvar(bnr, '')
749  call assert_equal(1, len(bd))
750  let bd = getbufvar(bnr, '',def_num)
751  call assert_equal(1, len(bd))
752
753  call assert_equal('', getbufvar(9999, ''))
754  call assert_equal(def_num, getbufvar(9999, '', def_num))
755  unlet def_num
756
757  call assert_equal(0, getbufvar(bnr, '&autoindent'))
758  call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
759
760  " Open new window with forced option values
761  set fileformats=unix,dos
762  new ++ff=dos ++bin ++enc=iso-8859-2
763  call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
764  call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
765  call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
766  close
767
768  set fileformats&
769endfunc
770
771func Test_last_buffer_nr()
772  call assert_equal(bufnr('$'), last_buffer_nr())
773endfunc
774
775func Test_stridx()
776  call assert_equal(-1, stridx('', 'l'))
777  call assert_equal(0,  stridx('', ''))
778  call assert_equal(0,  'hello'->stridx(''))
779  call assert_equal(-1, stridx('hello', 'L'))
780  call assert_equal(2,  stridx('hello', 'l', -1))
781  call assert_equal(2,  stridx('hello', 'l', 0))
782  call assert_equal(2,  'hello'->stridx('l', 1))
783  call assert_equal(3,  stridx('hello', 'l', 3))
784  call assert_equal(-1, stridx('hello', 'l', 4))
785  call assert_equal(-1, stridx('hello', 'l', 10))
786  call assert_equal(2,  stridx('hello', 'll'))
787  call assert_equal(-1, stridx('hello', 'hello world'))
788endfunc
789
790func Test_strridx()
791  call assert_equal(-1, strridx('', 'l'))
792  call assert_equal(0,  strridx('', ''))
793  call assert_equal(5,  strridx('hello', ''))
794  call assert_equal(-1, strridx('hello', 'L'))
795  call assert_equal(3,  'hello'->strridx('l'))
796  call assert_equal(3,  strridx('hello', 'l', 10))
797  call assert_equal(3,  strridx('hello', 'l', 3))
798  call assert_equal(2,  strridx('hello', 'l', 2))
799  call assert_equal(-1, strridx('hello', 'l', 1))
800  call assert_equal(-1, strridx('hello', 'l', 0))
801  call assert_equal(-1, strridx('hello', 'l', -1))
802  call assert_equal(2,  strridx('hello', 'll'))
803  call assert_equal(-1, strridx('hello', 'hello world'))
804endfunc
805
806func Test_match_func()
807  call assert_equal(4,  match('testing', 'ing'))
808  call assert_equal(4,  'testing'->match('ing', 2))
809  call assert_equal(-1, match('testing', 'ing', 5))
810  call assert_equal(-1, match('testing', 'ing', 8))
811  call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
812  call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
813endfunc
814
815func Test_matchend()
816  call assert_equal(7,  matchend('testing', 'ing'))
817  call assert_equal(7,  'testing'->matchend('ing', 2))
818  call assert_equal(-1, matchend('testing', 'ing', 5))
819  call assert_equal(-1, matchend('testing', 'ing', 8))
820  call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
821  call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
822endfunc
823
824func Test_matchlist()
825  call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''],  matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
826  call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''],  'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
827  call assert_equal([],  matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
828endfunc
829
830func Test_matchstr()
831  call assert_equal('ing',  matchstr('testing', 'ing'))
832  call assert_equal('ing',  'testing'->matchstr('ing', 2))
833  call assert_equal('', matchstr('testing', 'ing', 5))
834  call assert_equal('', matchstr('testing', 'ing', 8))
835  call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
836  call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
837endfunc
838
839func Test_matchstrpos()
840  call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
841  call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
842  call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
843  call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
844  call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
845  call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
846endfunc
847
848func Test_nextnonblank_prevnonblank()
849  new
850insert
851This
852
853
854is
855
856a
857Test
858.
859  call assert_equal(0, nextnonblank(-1))
860  call assert_equal(0, nextnonblank(0))
861  call assert_equal(1, nextnonblank(1))
862  call assert_equal(4, 2->nextnonblank())
863  call assert_equal(4, nextnonblank(3))
864  call assert_equal(4, nextnonblank(4))
865  call assert_equal(6, nextnonblank(5))
866  call assert_equal(6, nextnonblank(6))
867  call assert_equal(7, nextnonblank(7))
868  call assert_equal(0, 8->nextnonblank())
869
870  call assert_equal(0, prevnonblank(-1))
871  call assert_equal(0, prevnonblank(0))
872  call assert_equal(1, 1->prevnonblank())
873  call assert_equal(1, prevnonblank(2))
874  call assert_equal(1, prevnonblank(3))
875  call assert_equal(4, prevnonblank(4))
876  call assert_equal(4, 5->prevnonblank())
877  call assert_equal(6, prevnonblank(6))
878  call assert_equal(7, prevnonblank(7))
879  call assert_equal(0, prevnonblank(8))
880  bw!
881endfunc
882
883func Test_byte2line_line2byte()
884  new
885  set endofline
886  call setline(1, ['a', 'bc', 'd'])
887
888  set fileformat=unix
889  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
890  \                 map(range(-1, 8), 'byte2line(v:val)'))
891  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
892  \                 map(range(-1, 5), 'line2byte(v:val)'))
893
894  set fileformat=mac
895  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
896  \                 map(range(-1, 8), 'v:val->byte2line()'))
897  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
898  \                 map(range(-1, 5), 'v:val->line2byte()'))
899
900  set fileformat=dos
901  call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
902  \                 map(range(-1, 11), 'byte2line(v:val)'))
903  call assert_equal([-1, -1, 1, 4, 8, 11, -1],
904  \                 map(range(-1, 5), 'line2byte(v:val)'))
905
906  bw!
907  set noendofline nofixendofline
908  normal a-
909  for ff in ["unix", "mac", "dos"]
910    let &fileformat = ff
911    call assert_equal(1, line2byte(1))
912    call assert_equal(2, line2byte(2))  " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
913  endfor
914
915  set endofline& fixendofline& fileformat&
916  bw!
917endfunc
918
919func Test_byteidx()
920  let a = '.é.' " one char of two bytes
921  call assert_equal(0, byteidx(a, 0))
922  call assert_equal(0, byteidxcomp(a, 0))
923  call assert_equal(1, byteidx(a, 1))
924  call assert_equal(1, byteidxcomp(a, 1))
925  call assert_equal(3, byteidx(a, 2))
926  call assert_equal(3, byteidxcomp(a, 2))
927  call assert_equal(4, byteidx(a, 3))
928  call assert_equal(4, byteidxcomp(a, 3))
929  call assert_equal(-1, byteidx(a, 4))
930  call assert_equal(-1, byteidxcomp(a, 4))
931
932  let b = '.é.' " normal e with composing char
933  call assert_equal(0, b->byteidx(0))
934  call assert_equal(1, b->byteidx(1))
935  call assert_equal(4, b->byteidx(2))
936  call assert_equal(5, b->byteidx(3))
937  call assert_equal(-1, b->byteidx(4))
938
939  call assert_equal(0, b->byteidxcomp(0))
940  call assert_equal(1, b->byteidxcomp(1))
941  call assert_equal(2, b->byteidxcomp(2))
942  call assert_equal(4, b->byteidxcomp(3))
943  call assert_equal(5, b->byteidxcomp(4))
944  call assert_equal(-1, b->byteidxcomp(5))
945endfunc
946
947func Test_count()
948  let l = ['a', 'a', 'A', 'b']
949  call assert_equal(2, count(l, 'a'))
950  call assert_equal(1, count(l, 'A'))
951  call assert_equal(1, count(l, 'b'))
952  call assert_equal(0, count(l, 'B'))
953
954  call assert_equal(2, count(l, 'a', 0))
955  call assert_equal(1, count(l, 'A', 0))
956  call assert_equal(1, count(l, 'b', 0))
957  call assert_equal(0, count(l, 'B', 0))
958
959  call assert_equal(3, count(l, 'a', 1))
960  call assert_equal(3, count(l, 'A', 1))
961  call assert_equal(1, count(l, 'b', 1))
962  call assert_equal(1, count(l, 'B', 1))
963  call assert_equal(0, count(l, 'c', 1))
964
965  call assert_equal(1, count(l, 'a', 0, 1))
966  call assert_equal(2, count(l, 'a', 1, 1))
967  call assert_fails('call count(l, "a", 0, 10)', 'E684:')
968  call assert_fails('call count(l, "a", [])', 'E745:')
969
970  let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
971  call assert_equal(2, count(d, 'a'))
972  call assert_equal(1, count(d, 'A'))
973  call assert_equal(1, count(d, 'b'))
974  call assert_equal(0, count(d, 'B'))
975
976  call assert_equal(2, count(d, 'a', 0))
977  call assert_equal(1, count(d, 'A', 0))
978  call assert_equal(1, count(d, 'b', 0))
979  call assert_equal(0, count(d, 'B', 0))
980
981  call assert_equal(3, count(d, 'a', 1))
982  call assert_equal(3, count(d, 'A', 1))
983  call assert_equal(1, count(d, 'b', 1))
984  call assert_equal(1, count(d, 'B', 1))
985  call assert_equal(0, count(d, 'c', 1))
986
987  call assert_fails('call count(d, "a", 0, 1)', 'E474:')
988
989  call assert_equal(0, count("foo", "bar"))
990  call assert_equal(1, count("foo", "oo"))
991  call assert_equal(2, count("foo", "o"))
992  call assert_equal(0, count("foo", "O"))
993  call assert_equal(2, count("foo", "O", 1))
994  call assert_equal(2, count("fooooo", "oo"))
995  call assert_equal(0, count("foo", ""))
996
997  call assert_fails('call count(0, 0)', 'E712:')
998endfunc
999
1000func Test_changenr()
1001  new Xchangenr
1002  call assert_equal(0, changenr())
1003  norm ifoo
1004  call assert_equal(1, changenr())
1005  set undolevels=10
1006  norm Sbar
1007  call assert_equal(2, changenr())
1008  undo
1009  call assert_equal(1, changenr())
1010  redo
1011  call assert_equal(2, changenr())
1012  bw!
1013  set undolevels&
1014endfunc
1015
1016func Test_filewritable()
1017  new Xfilewritable
1018  write!
1019  call assert_equal(1, filewritable('Xfilewritable'))
1020
1021  call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1022  call assert_equal(0, filewritable('Xfilewritable'))
1023
1024  call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
1025  call assert_equal(1, 'Xfilewritable'->filewritable())
1026
1027  call assert_equal(0, filewritable('doesnotexist'))
1028
1029  call delete('Xfilewritable')
1030  bw!
1031endfunc
1032
1033func Test_Executable()
1034  if has('win32')
1035    call assert_equal(1, executable('notepad'))
1036    call assert_equal(1, 'notepad.exe'->executable())
1037    call assert_equal(0, executable('notepad.exe.exe'))
1038    call assert_equal(0, executable('shell32.dll'))
1039    call assert_equal(0, executable('win.ini'))
1040  elseif has('unix')
1041    call assert_equal(1, 'cat'->executable())
1042    call assert_equal(0, executable('nodogshere'))
1043
1044    " get "cat" path and remove the leading /
1045    let catcmd = exepath('cat')[1:]
1046    new
1047    " check that the relative path works in /
1048    lcd /
1049    call assert_equal(1, executable(catcmd))
1050    call assert_equal('/' .. catcmd, catcmd->exepath())
1051    bwipe
1052  endif
1053endfunc
1054
1055func Test_executable_longname()
1056  if !has('win32')
1057    return
1058  endif
1059
1060  let fname = 'X' . repeat('あ', 200) . '.bat'
1061  call writefile([], fname)
1062  call assert_equal(1, executable(fname))
1063  call delete(fname)
1064endfunc
1065
1066func Test_hostname()
1067  let hostname_vim = hostname()
1068  if has('unix')
1069    let hostname_system = systemlist('uname -n')[0]
1070    call assert_equal(hostname_vim, hostname_system)
1071  endif
1072endfunc
1073
1074func Test_getpid()
1075  " getpid() always returns the same value within a vim instance.
1076  call assert_equal(getpid(), getpid())
1077  if has('unix')
1078    call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1079  endif
1080endfunc
1081
1082func Test_hlexists()
1083  call assert_equal(0, hlexists('does_not_exist'))
1084  call assert_equal(0, 'Number'->hlexists())
1085  call assert_equal(0, highlight_exists('does_not_exist'))
1086  call assert_equal(0, highlight_exists('Number'))
1087  syntax on
1088  call assert_equal(0, hlexists('does_not_exist'))
1089  call assert_equal(1, hlexists('Number'))
1090  call assert_equal(0, highlight_exists('does_not_exist'))
1091  call assert_equal(1, highlight_exists('Number'))
1092  syntax off
1093endfunc
1094
1095func Test_col()
1096  new
1097  call setline(1, 'abcdef')
1098  norm gg4|mx6|mY2|
1099  call assert_equal(2, col('.'))
1100  call assert_equal(7, col('$'))
1101  call assert_equal(4, col("'x"))
1102  call assert_equal(6, col("'Y"))
1103  call assert_equal(2, [1, 2]->col())
1104  call assert_equal(7, col([1, '$']))
1105
1106  call assert_equal(0, col(''))
1107  call assert_equal(0, col('x'))
1108  call assert_equal(0, col([2, '$']))
1109  call assert_equal(0, col([1, 100]))
1110  call assert_equal(0, col([1]))
1111  bw!
1112endfunc
1113
1114func Test_inputlist()
1115  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1116  call assert_equal(1, c)
1117  call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
1118  call assert_equal(2, c)
1119  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1120  call assert_equal(3, c)
1121
1122  call assert_fails('call inputlist("")', 'E686:')
1123endfunc
1124
1125func Test_balloon_show()
1126  if has('balloon_eval')
1127    " This won't do anything but must not crash either.
1128    call balloon_show('hi!')
1129  endif
1130endfunc
1131
1132func Test_setbufvar_options()
1133  " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1134  " window layout.
1135  call assert_equal(1, winnr('$'))
1136  split dummy_preview
1137  resize 2
1138  set winfixheight winfixwidth
1139  let prev_id = win_getid()
1140
1141  wincmd j
1142  let wh = winheight('.')
1143  let dummy_buf = bufnr('dummy_buf1', v:true)
1144  call setbufvar(dummy_buf, '&buftype', 'nofile')
1145  execute 'belowright vertical split #' . dummy_buf
1146  call assert_equal(wh, winheight('.'))
1147  let dum1_id = win_getid()
1148
1149  wincmd h
1150  let wh = winheight('.')
1151  let dummy_buf = bufnr('dummy_buf2', v:true)
1152  eval 'nofile'->setbufvar(dummy_buf, '&buftype')
1153  execute 'belowright vertical split #' . dummy_buf
1154  call assert_equal(wh, winheight('.'))
1155
1156  bwipe!
1157  call win_gotoid(prev_id)
1158  bwipe!
1159  call win_gotoid(dum1_id)
1160  bwipe!
1161endfunc
1162
1163func Test_redo_in_nested_functions()
1164  nnoremap g. :set opfunc=Operator<CR>g@
1165  function Operator( type, ... )
1166     let @x = 'XXX'
1167     execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1168  endfunction
1169
1170  function! Apply()
1171      5,6normal! .
1172  endfunction
1173
1174  new
1175  call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1176  1normal g.i"
1177  call assert_equal('some "XXX" text', getline(1))
1178  3,4normal .
1179  call assert_equal('some "XXX" text', getline(3))
1180  call assert_equal('more "XXX" text', getline(4))
1181  call Apply()
1182  call assert_equal('some "XXX" text', getline(5))
1183  call assert_equal('more "XXX" text', getline(6))
1184  bwipe!
1185
1186  nunmap g.
1187  delfunc Operator
1188  delfunc Apply
1189endfunc
1190
1191func Test_shellescape()
1192  let save_shell = &shell
1193  set shell=bash
1194  call assert_equal("'text'", shellescape('text'))
1195  call assert_equal("'te\"xt'", 'te"xt'->shellescape())
1196  call assert_equal("'te'\\''xt'", shellescape("te'xt"))
1197
1198  call assert_equal("'te%xt'", shellescape("te%xt"))
1199  call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
1200  call assert_equal("'te#xt'", shellescape("te#xt"))
1201  call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
1202  call assert_equal("'te!xt'", shellescape("te!xt"))
1203  call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1204
1205  call assert_equal("'te\nxt'", shellescape("te\nxt"))
1206  call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1207  set shell=tcsh
1208  call assert_equal("'te\\!xt'", shellescape("te!xt"))
1209  call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1210  call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1211  call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1212
1213  let &shell = save_shell
1214endfunc
1215
1216func Test_trim()
1217  call assert_equal("Testing", trim("  \t\r\r\x0BTesting  \t\n\r\n\t\x0B\x0B"))
1218  call assert_equal("Testing", "  \t  \r\r\n\n\x0BTesting  \t\n\r\n\t\x0B\x0B"->trim())
1219  call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1220  call assert_equal("wRE    \tSERVEzyww", trim("wRE    \tSERVEzyww"))
1221  call assert_equal("abcd\t     xxxx   tail", trim(" \tabcd\t     xxxx   tail"))
1222  call assert_equal("\tabcd\t     xxxx   tail", trim(" \tabcd\t     xxxx   tail", " "))
1223  call assert_equal(" \tabcd\t     xxxx   tail", trim(" \tabcd\t     xxxx   tail", "abx"))
1224  call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1225  call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1226  call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r   你好您R E SER V E早好你你    \t  \x0B", ))
1227  call assert_equal("您R E SER V E早好你你    \t  \x0B", trim("    你好您R E SER V E早好你你    \t  \x0B", " 你好"))
1228  call assert_equal("您R E SER V E早好你你    \t  \x0B", trim("    tteesstttt你好您R E SER V E早好你你    \t  \x0B ttestt", " 你好tes"))
1229  call assert_equal("您R E SER V E早好你你    \t  \x0B", trim("    tteesstttt你好您R E SER V E早好你你    \t  \x0B ttestt", "   你你你好好好tttsses"))
1230  call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1231  call assert_equal("", trim("", ""))
1232  call assert_equal("a", trim("a", ""))
1233  call assert_equal("", trim("", "a"))
1234
1235  let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
1236  call assert_equal("x", trim(chars . "x" . chars))
1237endfunc
1238
1239" Test for reg_recording() and reg_executing()
1240func Test_reg_executing_and_recording()
1241  let s:reg_stat = ''
1242  func s:save_reg_stat()
1243    let s:reg_stat = reg_recording() . ':' . reg_executing()
1244    return ''
1245  endfunc
1246
1247  new
1248  call s:save_reg_stat()
1249  call assert_equal(':', s:reg_stat)
1250  call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1251  call assert_equal('a:', s:reg_stat)
1252  call feedkeys("@a", 'xt')
1253  call assert_equal(':a', s:reg_stat)
1254  call feedkeys("qb@aq", 'xt')
1255  call assert_equal('b:a', s:reg_stat)
1256  call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1257  call assert_equal('":', s:reg_stat)
1258
1259  " :normal command saves and restores reg_executing
1260  let s:reg_stat = ''
1261  let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1262  func TestFunc() abort
1263    normal! ia
1264  endfunc
1265  call feedkeys("@q", 'xt')
1266  call assert_equal(':q', s:reg_stat)
1267  delfunc TestFunc
1268
1269  " getchar() command saves and restores reg_executing
1270  map W :call TestFunc()<CR>
1271  let @q = "W"
1272  let g:typed = ''
1273  let g:regs = []
1274  func TestFunc() abort
1275    let g:regs += [reg_executing()]
1276    let g:typed = getchar(0)
1277    let g:regs += [reg_executing()]
1278  endfunc
1279  call feedkeys("@qy", 'xt')
1280  call assert_equal(char2nr("y"), g:typed)
1281  call assert_equal(['q', 'q'], g:regs)
1282  delfunc TestFunc
1283  unmap W
1284  unlet g:typed
1285  unlet g:regs
1286
1287  " input() command saves and restores reg_executing
1288  map W :call TestFunc()<CR>
1289  let @q = "W"
1290  let g:typed = ''
1291  let g:regs = []
1292  func TestFunc() abort
1293    let g:regs += [reg_executing()]
1294    let g:typed = '?'->input()
1295    let g:regs += [reg_executing()]
1296  endfunc
1297  call feedkeys("@qy\<CR>", 'xt')
1298  call assert_equal("y", g:typed)
1299  call assert_equal(['q', 'q'], g:regs)
1300  delfunc TestFunc
1301  unmap W
1302  unlet g:typed
1303  unlet g:regs
1304
1305  bwipe!
1306  delfunc s:save_reg_stat
1307  unlet s:reg_stat
1308endfunc
1309
1310func Test_inputsecret()
1311  map W :call TestFunc()<CR>
1312  let @q = "W"
1313  let g:typed1 = ''
1314  let g:typed2 = ''
1315  let g:regs = []
1316  func TestFunc() abort
1317    let g:typed1 = '?'->inputsecret()
1318    let g:typed2 = inputsecret('password: ')
1319  endfunc
1320  call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1321  call assert_equal("something", g:typed1)
1322  call assert_equal("else", g:typed2)
1323  delfunc TestFunc
1324  unmap W
1325  unlet g:typed1
1326  unlet g:typed2
1327endfunc
1328
1329func Test_getchar()
1330  call feedkeys('a', '')
1331  call assert_equal(char2nr('a'), getchar())
1332
1333  call test_setmouse(1, 3)
1334  let v:mouse_win = 9
1335  let v:mouse_winid = 9
1336  let v:mouse_lnum = 9
1337  let v:mouse_col = 9
1338  call feedkeys("\<S-LeftMouse>", '')
1339  call assert_equal("\<S-LeftMouse>", getchar())
1340  call assert_equal(1, v:mouse_win)
1341  call assert_equal(win_getid(1), v:mouse_winid)
1342  call assert_equal(1, v:mouse_lnum)
1343  call assert_equal(3, v:mouse_col)
1344endfunc
1345
1346func Test_libcall_libcallnr()
1347  if !has('libcall')
1348    return
1349  endif
1350
1351  if has('win32')
1352    let libc = 'msvcrt.dll'
1353  elseif has('mac')
1354    let libc = 'libSystem.B.dylib'
1355  elseif executable('ldd')
1356    let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1357  endif
1358  if get(l:, 'libc', '') ==# ''
1359    " On Unix, libc.so can be in various places.
1360    if has('linux')
1361      " There is not documented but regarding the 1st argument of glibc's
1362      " dlopen an empty string and nullptr are equivalent, so using an empty
1363      " string for the 1st argument of libcall allows to call functions.
1364      let libc = ''
1365    elseif has('sun')
1366      " Set the path to libc.so according to the architecture.
1367      let test_bits = system('file ' . GetVimProg())
1368      let test_arch = system('uname -p')
1369      if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1370        let libc = '/usr/lib/sparcv9/libc.so'
1371      elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1372        let libc = '/usr/lib/amd64/libc.so'
1373      else
1374        let libc = '/usr/lib/libc.so'
1375      endif
1376    else
1377      " Unfortunately skip this test until a good way is found.
1378      return
1379    endif
1380  endif
1381
1382  if has('win32')
1383    call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
1384  else
1385    call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
1386  endif
1387
1388  " If function returns NULL, libcall() should return an empty string.
1389  call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1390
1391  " Test libcallnr() with string and integer argument.
1392  call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1393  call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
1394
1395  call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1396  call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1397
1398  call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1399  call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1400endfunc
1401
1402sandbox function Fsandbox()
1403  normal ix
1404endfunc
1405
1406func Test_func_sandbox()
1407  sandbox let F = {-> 'hello'}
1408  call assert_equal('hello', F())
1409
1410  sandbox let F = {-> "normal ix\<Esc>"->execute()}
1411  call assert_fails('call F()', 'E48:')
1412  unlet F
1413
1414  call assert_fails('call Fsandbox()', 'E48:')
1415  delfunc Fsandbox
1416endfunc
1417
1418func EditAnotherFile()
1419  let word = expand('<cword>')
1420  edit Xfuncrange2
1421endfunc
1422
1423func Test_func_range_with_edit()
1424  " Define a function that edits another buffer, then call it with a range that
1425  " is invalid in that buffer.
1426  call writefile(['just one line'], 'Xfuncrange2')
1427  new
1428  eval 10->range()->setline(1)
1429  write Xfuncrange1
1430  call assert_fails('5,8call EditAnotherFile()', 'E16:')
1431
1432  call delete('Xfuncrange1')
1433  call delete('Xfuncrange2')
1434  bwipe!
1435endfunc
1436
1437func Test_func_exists_on_reload()
1438  call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1439  call assert_equal(0, exists('*ExistingFunction'))
1440  source Xfuncexists
1441  call assert_equal(1, '*ExistingFunction'->exists())
1442  " Redefining a function when reloading a script is OK.
1443  source Xfuncexists
1444  call assert_equal(1, exists('*ExistingFunction'))
1445
1446  " But redefining in another script is not OK.
1447  call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1448  call assert_fails('source Xfuncexists2', 'E122:')
1449
1450  delfunc ExistingFunction
1451  call assert_equal(0, exists('*ExistingFunction'))
1452  call writefile([
1453	\ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1454	\ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1455	\ ], 'Xfuncexists')
1456  call assert_fails('source Xfuncexists', 'E122:')
1457  call assert_equal(1, exists('*ExistingFunction'))
1458
1459  call delete('Xfuncexists2')
1460  call delete('Xfuncexists')
1461  delfunc ExistingFunction
1462endfunc
1463
1464" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1465func Test_confirm()
1466  CheckUnix
1467  CheckNotGui
1468
1469  call feedkeys('o', 'L')
1470  let a = confirm('Press O to proceed')
1471  call assert_equal(1, a)
1472
1473  call feedkeys('y', 'L')
1474  let a = 'Are you sure?'->confirm("&Yes\n&No")
1475  call assert_equal(1, a)
1476
1477  call feedkeys('n', 'L')
1478  let a = confirm('Are you sure?', "&Yes\n&No")
1479  call assert_equal(2, a)
1480
1481  " confirm() should return 0 when pressing CTRL-C.
1482  call feedkeys("\<C-c>", 'L')
1483  let a = confirm('Are you sure?', "&Yes\n&No")
1484  call assert_equal(0, a)
1485
1486  " <Esc> requires another character to avoid it being seen as the start of an
1487  " escape sequence.  Zero should be harmless.
1488  eval "\<Esc>0"->feedkeys('L')
1489  let a = confirm('Are you sure?', "&Yes\n&No")
1490  call assert_equal(0, a)
1491
1492  " Default choice is returned when pressing <CR>.
1493  call feedkeys("\<CR>", 'L')
1494  let a = confirm('Are you sure?', "&Yes\n&No")
1495  call assert_equal(1, a)
1496
1497  call feedkeys("\<CR>", 'L')
1498  let a = confirm('Are you sure?', "&Yes\n&No", 2)
1499  call assert_equal(2, a)
1500
1501  call feedkeys("\<CR>", 'L')
1502  let a = confirm('Are you sure?', "&Yes\n&No", 0)
1503  call assert_equal(0, a)
1504
1505  " Test with the {type} 4th argument
1506  for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
1507    call feedkeys('y', 'L')
1508    let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
1509    call assert_equal(1, a)
1510  endfor
1511
1512  call assert_fails('call confirm([])', 'E730:')
1513  call assert_fails('call confirm("Are you sure?", [])', 'E730:')
1514  call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
1515  call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
1516endfunc
1517
1518func Test_platform_name()
1519  " The system matches at most only one name.
1520  let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
1521  call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
1522
1523  " Is Unix?
1524  call assert_equal(has('beos'), has('beos') && has('unix'))
1525  call assert_equal(has('bsd'), has('bsd') && has('unix'))
1526  call assert_equal(has('hpux'), has('hpux') && has('unix'))
1527  call assert_equal(has('linux'), has('linux') && has('unix'))
1528  call assert_equal(has('mac'), has('mac') && has('unix'))
1529  call assert_equal(has('qnx'), has('qnx') && has('unix'))
1530  call assert_equal(has('sun'), has('sun') && has('unix'))
1531  call assert_equal(has('win32'), has('win32') && !has('unix'))
1532  call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
1533
1534  if has('unix') && executable('uname')
1535    let uname = system('uname')
1536    call assert_equal(uname =~? 'BeOS', has('beos'))
1537    " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
1538    call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
1539    call assert_equal(uname =~? 'HP-UX', has('hpux'))
1540    call assert_equal(uname =~? 'Linux', has('linux'))
1541    call assert_equal(uname =~? 'Darwin', has('mac'))
1542    call assert_equal(uname =~? 'QNX', has('qnx'))
1543    call assert_equal(uname =~? 'SunOS', has('sun'))
1544    call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
1545  endif
1546endfunc
1547
1548func Test_readdir()
1549  call mkdir('Xdir')
1550  call writefile([], 'Xdir/foo.txt')
1551  call writefile([], 'Xdir/bar.txt')
1552  call mkdir('Xdir/dir')
1553
1554  " All results
1555  let files = readdir('Xdir')
1556  call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
1557
1558  " Only results containing "f"
1559  let files = 'Xdir'->readdir({ x -> stridx(x, 'f') !=- 1 })
1560  call assert_equal(['foo.txt'], sort(files))
1561
1562  " Only .txt files
1563  let files = readdir('Xdir', { x -> x =~ '.txt$' })
1564  call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1565
1566  " Only .txt files with string
1567  let files = readdir('Xdir', 'v:val =~ ".txt$"')
1568  call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1569
1570  " Limit to 1 result.
1571  let l = []
1572  let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
1573  call assert_equal(1, len(files))
1574
1575  " Nested readdir() must not crash
1576  let files = readdir('Xdir', 'readdir("Xdir", "1") != []')
1577  call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
1578
1579  eval 'Xdir'->delete('rf')
1580endfunc
1581
1582func Test_delete_rf()
1583  call mkdir('Xdir')
1584  call writefile([], 'Xdir/foo.txt')
1585  call writefile([], 'Xdir/bar.txt')
1586  call mkdir('Xdir/[a-1]')  " issue #696
1587  call writefile([], 'Xdir/[a-1]/foo.txt')
1588  call writefile([], 'Xdir/[a-1]/bar.txt')
1589  call assert_true(filereadable('Xdir/foo.txt'))
1590  call assert_true('Xdir/[a-1]/foo.txt'->filereadable())
1591
1592  call assert_equal(0, delete('Xdir', 'rf'))
1593  call assert_false(filereadable('Xdir/foo.txt'))
1594  call assert_false(filereadable('Xdir/[a-1]/foo.txt'))
1595endfunc
1596
1597func Test_call()
1598  call assert_equal(3, call('len', [123]))
1599  call assert_equal(3, 'len'->call([123]))
1600  call assert_fails("call call('len', 123)", 'E714:')
1601  call assert_equal(0, call('', []))
1602
1603  function Mylen() dict
1604     return len(self.data)
1605  endfunction
1606  let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
1607  eval mydict.len->call([], mydict)->assert_equal(4)
1608  call assert_fails("call call('Mylen', [], 0)", 'E715:')
1609endfunc
1610
1611func Test_char2nr()
1612  call assert_equal(12354, char2nr('あ', 1))
1613  call assert_equal(120, 'x'->char2nr())
1614endfunc
1615
1616func Test_eventhandler()
1617  call assert_equal(0, eventhandler())
1618endfunc
1619
1620func Test_bufadd_bufload()
1621  call assert_equal(0, bufexists('someName'))
1622  let buf = bufadd('someName')
1623  call assert_notequal(0, buf)
1624  call assert_equal(1, bufexists('someName'))
1625  call assert_equal(0, getbufvar(buf, '&buflisted'))
1626  call assert_equal(0, bufloaded(buf))
1627  call bufload(buf)
1628  call assert_equal(1, bufloaded(buf))
1629  call assert_equal([''], getbufline(buf, 1, '$'))
1630
1631  let curbuf = bufnr('')
1632  eval ['some', 'text']->writefile('XotherName')
1633  let buf = 'XotherName'->bufadd()
1634  call assert_notequal(0, buf)
1635  eval 'XotherName'->bufexists()->assert_equal(1)
1636  call assert_equal(0, getbufvar(buf, '&buflisted'))
1637  call assert_equal(0, bufloaded(buf))
1638  eval buf->bufload()
1639  call assert_equal(1, bufloaded(buf))
1640  call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
1641  call assert_equal(curbuf, bufnr(''))
1642
1643  let buf1 = bufadd('')
1644  let buf2 = bufadd('')
1645  call assert_notequal(0, buf1)
1646  call assert_notequal(0, buf2)
1647  call assert_notequal(buf1, buf2)
1648  call assert_equal(1, bufexists(buf1))
1649  call assert_equal(1, bufexists(buf2))
1650  call assert_equal(0, bufloaded(buf1))
1651  exe 'bwipe ' .. buf1
1652  call assert_equal(0, bufexists(buf1))
1653  call assert_equal(1, bufexists(buf2))
1654  exe 'bwipe ' .. buf2
1655  call assert_equal(0, bufexists(buf2))
1656
1657  bwipe someName
1658  bwipe XotherName
1659  call assert_equal(0, bufexists('someName'))
1660  call delete('XotherName')
1661endfunc
1662