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