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