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