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