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