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    if has('multi_byte')
110      call assert_equal(2, strwidth('��'))
111      call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
112      call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
113    endif
114
115    call assert_fails('call strwidth({->0})', 'E729:')
116    call assert_fails('call strwidth([])', 'E730:')
117    call assert_fails('call strwidth({})', 'E731:')
118    call assert_fails('call strwidth(1.2)', 'E806:')
119  endfor
120
121  set ambiwidth&
122endfunc
123
124func Test_str2nr()
125  call assert_equal(0, str2nr(''))
126  call assert_equal(1, str2nr('1'))
127  call assert_equal(1, str2nr(' 1 '))
128
129  call assert_equal(1, str2nr('+1'))
130  call assert_equal(1, str2nr('+ 1'))
131  call assert_equal(1, str2nr(' + 1 '))
132
133  call assert_equal(-1, str2nr('-1'))
134  call assert_equal(-1, str2nr('- 1'))
135  call assert_equal(-1, str2nr(' - 1 '))
136
137  call assert_equal(123456789, str2nr('123456789'))
138  call assert_equal(-123456789, str2nr('-123456789'))
139
140  call assert_equal(5, str2nr('101', 2))
141  call assert_equal(5, str2nr('0b101', 2))
142  call assert_equal(5, str2nr('0B101', 2))
143  call assert_equal(-5, str2nr('-101', 2))
144  call assert_equal(-5, str2nr('-0b101', 2))
145  call assert_equal(-5, str2nr('-0B101', 2))
146
147  call assert_equal(65, str2nr('101', 8))
148  call assert_equal(65, str2nr('0101', 8))
149  call assert_equal(-65, str2nr('-101', 8))
150  call assert_equal(-65, str2nr('-0101', 8))
151
152  call assert_equal(11259375, str2nr('abcdef', 16))
153  call assert_equal(11259375, str2nr('ABCDEF', 16))
154  call assert_equal(-11259375, str2nr('-ABCDEF', 16))
155  call assert_equal(11259375, str2nr('0xabcdef', 16))
156  call assert_equal(11259375, str2nr('0Xabcdef', 16))
157  call assert_equal(11259375, str2nr('0XABCDEF', 16))
158  call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
159
160  call assert_equal(0, str2nr('0x10'))
161  call assert_equal(0, str2nr('0b10'))
162  call assert_equal(1, str2nr('12', 2))
163  call assert_equal(1, str2nr('18', 8))
164  call assert_equal(1, str2nr('1g', 16))
165
166  call assert_equal(0, str2nr(v:null))
167  call assert_equal(0, str2nr(v:none))
168
169  call assert_fails('call str2nr([])', 'E730:')
170  call assert_fails('call str2nr({->2})', 'E729:')
171  call assert_fails('call str2nr(1.2)', 'E806:')
172  call assert_fails('call str2nr(10, [])', 'E474:')
173endfunc
174
175func Test_strftime()
176  if !exists('*strftime')
177    return
178  endif
179  " Format of strftime() depends on system. We assume
180  " that basic formats tested here are available and
181  " identical on all systems which support strftime().
182  "
183  " The 2nd parameter of strftime() is a local time, so the output day
184  " of strftime() can be 17 or 18, depending on timezone.
185  call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
186  "
187  call assert_match('^\d\d\d\d-\(0\d\|1[012]\)-\([012]\d\|3[01]\) \([01]\d\|2[0-3]\):[0-5]\d:\([0-5]\d\|60\)$', strftime('%Y-%m-%d %H:%M:%S'))
188
189  call assert_fails('call strftime([])', 'E730:')
190  call assert_fails('call strftime("%Y", [])', 'E745:')
191endfunc
192
193func Test_resolve()
194  if !has('unix')
195    return
196  endif
197
198  " Xlink1 -> Xlink2
199  " Xlink2 -> Xlink3
200  silent !ln -s -f Xlink2 Xlink1
201  silent !ln -s -f Xlink3 Xlink2
202  call assert_equal('Xlink3', resolve('Xlink1'))
203  call assert_equal('./Xlink3', resolve('./Xlink1'))
204  call assert_equal('Xlink3/', resolve('Xlink2/'))
205  " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
206  "call assert_equal('Xlink3/', resolve('Xlink1/'))
207  "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
208  "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
209  call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
210
211  " Test resolve() with a symlink cycle.
212  " Xlink1 -> Xlink2
213  " Xlink2 -> Xlink3
214  " Xlink3 -> Xlink1
215  silent !ln -s -f Xlink1 Xlink3
216  call assert_fails('call resolve("Xlink1")',   'E655:')
217  call assert_fails('call resolve("./Xlink1")', 'E655:')
218  call assert_fails('call resolve("Xlink2")',   'E655:')
219  call assert_fails('call resolve("Xlink3")',   'E655:')
220  call delete('Xlink1')
221  call delete('Xlink2')
222  call delete('Xlink3')
223
224  silent !ln -s -f Xdir//Xfile Xlink
225  call assert_equal('Xdir/Xfile', resolve('Xlink'))
226  call delete('Xlink')
227
228  silent !ln -s -f Xlink2/ Xlink1
229  call assert_equal('Xlink2', resolve('Xlink1'))
230  call assert_equal('Xlink2/', resolve('Xlink1/'))
231  call delete('Xlink1')
232
233  silent !ln -s -f ./Xlink2 Xlink1
234  call assert_equal('Xlink2', resolve('Xlink1'))
235  call assert_equal('./Xlink2', resolve('./Xlink1'))
236  call delete('Xlink1')
237endfunc
238
239func Test_simplify()
240  call assert_equal('',            simplify(''))
241  call assert_equal('/',           simplify('/'))
242  call assert_equal('/',           simplify('/.'))
243  call assert_equal('/',           simplify('/..'))
244  call assert_equal('/...',        simplify('/...'))
245  call assert_equal('./dir/file',  simplify('./dir/file'))
246  call assert_equal('./dir/file',  simplify('.///dir//file'))
247  call assert_equal('./dir/file',  simplify('./dir/./file'))
248  call assert_equal('./file',      simplify('./dir/../file'))
249  call assert_equal('../dir/file', simplify('dir/../../dir/file'))
250  call assert_equal('./file',      simplify('dir/.././file'))
251
252  call assert_fails('call simplify({->0})', 'E729:')
253  call assert_fails('call simplify([])', 'E730:')
254  call assert_fails('call simplify({})', 'E731:')
255  call assert_fails('call simplify(1.2)', 'E806:')
256endfunc
257
258func Test_pathshorten()
259  call assert_equal('', pathshorten(''))
260  call assert_equal('foo', pathshorten('foo'))
261  call assert_equal('/foo', pathshorten('/foo'))
262  call assert_equal('f/', pathshorten('foo/'))
263  call assert_equal('f/bar', pathshorten('foo/bar'))
264  call assert_equal('f/b/foobar', pathshorten('foo/bar/foobar'))
265  call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
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'))
269  call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
270  call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
271endfunc
272
273func Test_strpart()
274  call assert_equal('de', strpart('abcdefg', 3, 2))
275  call assert_equal('ab', strpart('abcdefg', -2, 4))
276  call assert_equal('abcdefg', strpart('abcdefg', -2))
277  call assert_equal('fg', strpart('abcdefg', 5, 4))
278  call assert_equal('defg', strpart('abcdefg', 3))
279
280  if has('multi_byte')
281    call assert_equal('lép', strpart('éléphant', 2, 4))
282    call assert_equal('léphant', strpart('éléphant', 2))
283  endif
284endfunc
285
286func Test_tolower()
287  call assert_equal("", tolower(""))
288
289  " Test with all printable ASCII characters.
290  call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
291          \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
292
293  if !has('multi_byte')
294    return
295  endif
296
297  " Test with a few uppercase diacritics.
298  call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
299  call assert_equal("bḃḇ", tolower("BḂḆ"))
300  call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
301  call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
302  call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
303  call assert_equal("fḟ ", tolower("FḞ "))
304  call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
305  call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
306  call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
307  call assert_equal("jĵ", tolower("JĴ"))
308  call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
309  call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
310  call assert_equal("mḿṁ", tolower("MḾṀ"))
311  call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
312  call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
313  call assert_equal("pṕṗ", tolower("PṔṖ"))
314  call assert_equal("q", tolower("Q"))
315  call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
316  call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
317  call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
318  call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
319  call assert_equal("vṽ", tolower("VṼ"))
320  call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
321  call assert_equal("xẋẍ", tolower("XẊẌ"))
322  call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
323  call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
324
325  " Test with a few lowercase diacritics, which should remain unchanged.
326  call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
327  call assert_equal("bḃḇ", tolower("bḃḇ"))
328  call assert_equal("cçćĉċč", tolower("cçćĉċč"))
329  call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
330  call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
331  call assert_equal("fḟ", tolower("fḟ"))
332  call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
333  call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
334  call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
335  call assert_equal("jĵǰ", tolower("jĵǰ"))
336  call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
337  call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
338  call assert_equal("mḿṁ ", tolower("mḿṁ "))
339  call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
340  call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
341  call assert_equal("pṕṗ", tolower("pṕṗ"))
342  call assert_equal("q", tolower("q"))
343  call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
344  call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
345  call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
346  call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
347  call assert_equal("vṽ", tolower("vṽ"))
348  call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
349  call assert_equal("ẋẍ", tolower("ẋẍ"))
350  call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
351  call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
352
353  " According to https://twitter.com/jifa/status/625776454479970304
354  " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
355  " in length (2 to 3 bytes) when lowercased. So let's test them.
356  call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
357
358  " This call to tolower with invalid utf8 sequence used to cause access to
359  " invalid memory.
360  call tolower("\xC0\x80\xC0")
361  call tolower("123\xC0\x80\xC0")
362endfunc
363
364func Test_toupper()
365  call assert_equal("", toupper(""))
366
367  " Test with all printable ASCII characters.
368  call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
369          \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
370
371  if !has('multi_byte')
372    return
373  endif
374
375  " Test with a few lowercase diacritics.
376  call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("aàáâãäåāăąǎǟǡả"))
377  call assert_equal("BḂḆ", toupper("bḃḇ"))
378  call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
379  call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
380  call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
381  call assert_equal("FḞ", toupper("fḟ"))
382  call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
383  call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
384  call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
385  call assert_equal("JĴǰ", toupper("jĵǰ"))
386  call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
387  call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
388  call assert_equal("MḾṀ ", toupper("mḿṁ "))
389  call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
390  call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
391  call assert_equal("PṔṖ", toupper("pṕṗ"))
392  call assert_equal("Q", toupper("q"))
393  call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
394  call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
395  call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
396  call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
397  call assert_equal("VṼ", toupper("vṽ"))
398  call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
399  call assert_equal("ẊẌ", toupper("ẋẍ"))
400  call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
401  call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
402
403  " Test that uppercase diacritics, which should remain unchanged.
404  call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
405  call assert_equal("BḂḆ", toupper("BḂḆ"))
406  call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
407  call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
408  call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
409  call assert_equal("FḞ ", toupper("FḞ "))
410  call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
411  call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
412  call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
413  call assert_equal("JĴ", toupper("JĴ"))
414  call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
415  call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
416  call assert_equal("MḾṀ", toupper("MḾṀ"))
417  call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
418  call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
419  call assert_equal("PṔṖ", toupper("PṔṖ"))
420  call assert_equal("Q", toupper("Q"))
421  call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
422  call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
423  call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
424  call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
425  call assert_equal("VṼ", toupper("VṼ"))
426  call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
427  call assert_equal("XẊẌ", toupper("XẊẌ"))
428  call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
429  call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
430
431  call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
432
433  " This call to toupper with invalid utf8 sequence used to cause access to
434  " invalid memory.
435  call toupper("\xC0\x80\xC0")
436  call toupper("123\xC0\x80\xC0")
437endfunc
438
439" Tests for the mode() function
440let current_modes = ''
441func Save_mode()
442  let g:current_modes = mode(0) . '-' . mode(1)
443  return ''
444endfunc
445
446func Test_mode()
447  new
448  call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
449
450  " Only complete from the current buffer.
451  set complete=.
452
453  inoremap <F2> <C-R>=Save_mode()<CR>
454
455  normal! 3G
456  exe "normal i\<F2>\<Esc>"
457  call assert_equal('i-i', g:current_modes)
458  " i_CTRL-P: Multiple matches
459  exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
460  call assert_equal('i-ic', g:current_modes)
461  " i_CTRL-P: Single match
462  exe "normal iBro\<C-P>\<F2>\<Esc>u"
463  call assert_equal('i-ic', g:current_modes)
464  " i_CTRL-X
465  exe "normal iBa\<C-X>\<F2>\<Esc>u"
466  call assert_equal('i-ix', g:current_modes)
467  " i_CTRL-X CTRL-P: Multiple matches
468  exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
469  call assert_equal('i-ic', g:current_modes)
470  " i_CTRL-X CTRL-P: Single match
471  exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
472  call assert_equal('i-ic', g:current_modes)
473  " i_CTRL-X CTRL-P + CTRL-P: Single match
474  exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
475  call assert_equal('i-ic', g:current_modes)
476  " i_CTRL-X CTRL-L: Multiple matches
477  exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
478  call assert_equal('i-ic', g:current_modes)
479  " i_CTRL-X CTRL-L: Single match
480  exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
481  call assert_equal('i-ic', g:current_modes)
482  " i_CTRL-P: No match
483  exe "normal iCom\<C-P>\<F2>\<Esc>u"
484  call assert_equal('i-ic', g:current_modes)
485  " i_CTRL-X CTRL-P: No match
486  exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
487  call assert_equal('i-ic', g:current_modes)
488  " i_CTRL-X CTRL-L: No match
489  exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
490  call assert_equal('i-ic', g:current_modes)
491
492  " R_CTRL-P: Multiple matches
493  exe "normal RBa\<C-P>\<F2>\<Esc>u"
494  call assert_equal('R-Rc', g:current_modes)
495  " R_CTRL-P: Single match
496  exe "normal RBro\<C-P>\<F2>\<Esc>u"
497  call assert_equal('R-Rc', g:current_modes)
498  " R_CTRL-X
499  exe "normal RBa\<C-X>\<F2>\<Esc>u"
500  call assert_equal('R-Rx', g:current_modes)
501  " R_CTRL-X CTRL-P: Multiple matches
502  exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
503  call assert_equal('R-Rc', g:current_modes)
504  " R_CTRL-X CTRL-P: Single match
505  exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
506  call assert_equal('R-Rc', g:current_modes)
507  " R_CTRL-X CTRL-P + CTRL-P: Single match
508  exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
509  call assert_equal('R-Rc', g:current_modes)
510  " R_CTRL-X CTRL-L: Multiple matches
511  exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
512  call assert_equal('R-Rc', g:current_modes)
513  " R_CTRL-X CTRL-L: Single match
514  exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
515  call assert_equal('R-Rc', g:current_modes)
516  " R_CTRL-P: No match
517  exe "normal RCom\<C-P>\<F2>\<Esc>u"
518  call assert_equal('R-Rc', g:current_modes)
519  " R_CTRL-X CTRL-P: No match
520  exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
521  call assert_equal('R-Rc', g:current_modes)
522  " R_CTRL-X CTRL-L: No match
523  exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
524  call assert_equal('R-Rc', g:current_modes)
525
526  call assert_equal('n', mode(0))
527  call assert_equal('n', mode(1))
528
529  " i_CTRL-O
530  exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
531  call assert_equal("n-niI", g:current_modes)
532
533  " R_CTRL-O
534  exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
535  call assert_equal("n-niR", g:current_modes)
536
537  " gR_CTRL-O
538  exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
539  call assert_equal("n-niV", g:current_modes)
540
541  " How to test operator-pending mode?
542
543  call feedkeys("v", 'xt')
544  call assert_equal('v', mode())
545  call assert_equal('v', mode(1))
546  call feedkeys("\<Esc>V", 'xt')
547  call assert_equal('V', mode())
548  call assert_equal('V', mode(1))
549  call feedkeys("\<Esc>\<C-V>", 'xt')
550  call assert_equal("\<C-V>", mode())
551  call assert_equal("\<C-V>", mode(1))
552  call feedkeys("\<Esc>", 'xt')
553
554  call feedkeys("gh", 'xt')
555  call assert_equal('s', mode())
556  call assert_equal('s', mode(1))
557  call feedkeys("\<Esc>gH", 'xt')
558  call assert_equal('S', mode())
559  call assert_equal('S', mode(1))
560  call feedkeys("\<Esc>g\<C-H>", 'xt')
561  call assert_equal("\<C-S>", mode())
562  call assert_equal("\<C-S>", mode(1))
563  call feedkeys("\<Esc>", 'xt')
564
565  call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
566  call assert_equal('c-c', g:current_modes)
567  call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
568  call assert_equal('c-cv', g:current_modes)
569  " How to test Ex mode?
570
571  bwipe!
572  iunmap <F2>
573  set complete&
574endfunc
575
576func Test_getbufvar()
577  let bnr = bufnr('%')
578  let b:var_num = '1234'
579  let def_num = '5678'
580  call assert_equal('1234', getbufvar(bnr, 'var_num'))
581  call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
582
583  let bd = getbufvar(bnr, '')
584  call assert_equal('1234', bd['var_num'])
585  call assert_true(exists("bd['changedtick']"))
586  call assert_equal(2, len(bd))
587
588  let bd2 = getbufvar(bnr, '', def_num)
589  call assert_equal(bd, bd2)
590
591  unlet b:var_num
592  call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
593  call assert_equal('', getbufvar(bnr, 'var_num'))
594
595  let bd = getbufvar(bnr, '')
596  call assert_equal(1, len(bd))
597  let bd = getbufvar(bnr, '',def_num)
598  call assert_equal(1, len(bd))
599
600  call assert_equal('', getbufvar(9999, ''))
601  call assert_equal(def_num, getbufvar(9999, '', def_num))
602  unlet def_num
603
604  call assert_equal(0, getbufvar(bnr, '&autoindent'))
605  call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
606
607  " Open new window with forced option values
608  set fileformats=unix,dos
609  new ++ff=dos ++bin ++enc=iso-8859-2
610  call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
611  call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
612  call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
613  close
614
615  set fileformats&
616endfunc
617
618func Test_last_buffer_nr()
619  call assert_equal(bufnr('$'), last_buffer_nr())
620endfunc
621
622func Test_stridx()
623  call assert_equal(-1, stridx('', 'l'))
624  call assert_equal(0,  stridx('', ''))
625  call assert_equal(0,  stridx('hello', ''))
626  call assert_equal(-1, stridx('hello', 'L'))
627  call assert_equal(2,  stridx('hello', 'l', -1))
628  call assert_equal(2,  stridx('hello', 'l', 0))
629  call assert_equal(2,  stridx('hello', 'l', 1))
630  call assert_equal(3,  stridx('hello', 'l', 3))
631  call assert_equal(-1, stridx('hello', 'l', 4))
632  call assert_equal(-1, stridx('hello', 'l', 10))
633  call assert_equal(2,  stridx('hello', 'll'))
634  call assert_equal(-1, stridx('hello', 'hello world'))
635endfunc
636
637func Test_strridx()
638  call assert_equal(-1, strridx('', 'l'))
639  call assert_equal(0,  strridx('', ''))
640  call assert_equal(5,  strridx('hello', ''))
641  call assert_equal(-1, strridx('hello', 'L'))
642  call assert_equal(3,  strridx('hello', 'l'))
643  call assert_equal(3,  strridx('hello', 'l', 10))
644  call assert_equal(3,  strridx('hello', 'l', 3))
645  call assert_equal(2,  strridx('hello', 'l', 2))
646  call assert_equal(-1, strridx('hello', 'l', 1))
647  call assert_equal(-1, strridx('hello', 'l', 0))
648  call assert_equal(-1, strridx('hello', 'l', -1))
649  call assert_equal(2,  strridx('hello', 'll'))
650  call assert_equal(-1, strridx('hello', 'hello world'))
651endfunc
652
653func Test_match_func()
654  call assert_equal(4,  match('testing', 'ing'))
655  call assert_equal(4,  match('testing', 'ing', 2))
656  call assert_equal(-1, match('testing', 'ing', 5))
657  call assert_equal(-1, match('testing', 'ing', 8))
658  call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
659  call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
660endfunc
661
662func Test_matchend()
663  call assert_equal(7,  matchend('testing', 'ing'))
664  call assert_equal(7,  matchend('testing', 'ing', 2))
665  call assert_equal(-1, matchend('testing', 'ing', 5))
666  call assert_equal(-1, matchend('testing', 'ing', 8))
667  call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
668  call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
669endfunc
670
671func Test_matchlist()
672  call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''],  matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
673  call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''],  matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
674  call assert_equal([],  matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
675endfunc
676
677func Test_matchstr()
678  call assert_equal('ing',  matchstr('testing', 'ing'))
679  call assert_equal('ing',  matchstr('testing', 'ing', 2))
680  call assert_equal('', matchstr('testing', 'ing', 5))
681  call assert_equal('', matchstr('testing', 'ing', 8))
682  call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
683  call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
684endfunc
685
686func Test_matchstrpos()
687  call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
688  call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2))
689  call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
690  call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
691  call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
692  call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
693endfunc
694
695func Test_nextnonblank_prevnonblank()
696  new
697insert
698This
699
700
701is
702
703a
704Test
705.
706  call assert_equal(0, nextnonblank(-1))
707  call assert_equal(0, nextnonblank(0))
708  call assert_equal(1, nextnonblank(1))
709  call assert_equal(4, nextnonblank(2))
710  call assert_equal(4, nextnonblank(3))
711  call assert_equal(4, nextnonblank(4))
712  call assert_equal(6, nextnonblank(5))
713  call assert_equal(6, nextnonblank(6))
714  call assert_equal(7, nextnonblank(7))
715  call assert_equal(0, nextnonblank(8))
716
717  call assert_equal(0, prevnonblank(-1))
718  call assert_equal(0, prevnonblank(0))
719  call assert_equal(1, prevnonblank(1))
720  call assert_equal(1, prevnonblank(2))
721  call assert_equal(1, prevnonblank(3))
722  call assert_equal(4, prevnonblank(4))
723  call assert_equal(4, prevnonblank(5))
724  call assert_equal(6, prevnonblank(6))
725  call assert_equal(7, prevnonblank(7))
726  call assert_equal(0, prevnonblank(8))
727  bw!
728endfunc
729
730func Test_byte2line_line2byte()
731  new
732  set endofline
733  call setline(1, ['a', 'bc', 'd'])
734
735  set fileformat=unix
736  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
737  \                 map(range(-1, 8), 'byte2line(v:val)'))
738  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
739  \                 map(range(-1, 5), 'line2byte(v:val)'))
740
741  set fileformat=mac
742  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
743  \                 map(range(-1, 8), 'byte2line(v:val)'))
744  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
745  \                 map(range(-1, 5), 'line2byte(v:val)'))
746
747  set fileformat=dos
748  call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
749  \                 map(range(-1, 11), 'byte2line(v:val)'))
750  call assert_equal([-1, -1, 1, 4, 8, 11, -1],
751  \                 map(range(-1, 5), 'line2byte(v:val)'))
752
753  bw!
754  set noendofline nofixendofline
755  normal a-
756  for ff in ["unix", "mac", "dos"]
757    let &fileformat = ff
758    call assert_equal(1, line2byte(1))
759    call assert_equal(2, line2byte(2))  " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
760  endfor
761
762  set endofline& fixendofline& fileformat&
763  bw!
764endfunc
765
766func Test_count()
767  let l = ['a', 'a', 'A', 'b']
768  call assert_equal(2, count(l, 'a'))
769  call assert_equal(1, count(l, 'A'))
770  call assert_equal(1, count(l, 'b'))
771  call assert_equal(0, count(l, 'B'))
772
773  call assert_equal(2, count(l, 'a', 0))
774  call assert_equal(1, count(l, 'A', 0))
775  call assert_equal(1, count(l, 'b', 0))
776  call assert_equal(0, count(l, 'B', 0))
777
778  call assert_equal(3, count(l, 'a', 1))
779  call assert_equal(3, count(l, 'A', 1))
780  call assert_equal(1, count(l, 'b', 1))
781  call assert_equal(1, count(l, 'B', 1))
782  call assert_equal(0, count(l, 'c', 1))
783
784  call assert_equal(1, count(l, 'a', 0, 1))
785  call assert_equal(2, count(l, 'a', 1, 1))
786  call assert_fails('call count(l, "a", 0, 10)', 'E684:')
787
788  let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
789  call assert_equal(2, count(d, 'a'))
790  call assert_equal(1, count(d, 'A'))
791  call assert_equal(1, count(d, 'b'))
792  call assert_equal(0, count(d, 'B'))
793
794  call assert_equal(2, count(d, 'a', 0))
795  call assert_equal(1, count(d, 'A', 0))
796  call assert_equal(1, count(d, 'b', 0))
797  call assert_equal(0, count(d, 'B', 0))
798
799  call assert_equal(3, count(d, 'a', 1))
800  call assert_equal(3, count(d, 'A', 1))
801  call assert_equal(1, count(d, 'b', 1))
802  call assert_equal(1, count(d, 'B', 1))
803  call assert_equal(0, count(d, 'c', 1))
804
805  call assert_fails('call count(d, "a", 0, 1)', 'E474:')
806
807  call assert_equal(0, count("foo", "bar"))
808  call assert_equal(1, count("foo", "oo"))
809  call assert_equal(2, count("foo", "o"))
810  call assert_equal(0, count("foo", "O"))
811  call assert_equal(2, count("foo", "O", 1))
812  call assert_equal(2, count("fooooo", "oo"))
813  call assert_equal(0, count("foo", ""))
814endfunc
815
816func Test_changenr()
817  new Xchangenr
818  call assert_equal(0, changenr())
819  norm ifoo
820  call assert_equal(1, changenr())
821  set undolevels=10
822  norm Sbar
823  call assert_equal(2, changenr())
824  undo
825  call assert_equal(1, changenr())
826  redo
827  call assert_equal(2, changenr())
828  bw!
829  set undolevels&
830endfunc
831
832func Test_filewritable()
833  new Xfilewritable
834  write!
835  call assert_equal(1, filewritable('Xfilewritable'))
836
837  call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
838  call assert_equal(0, filewritable('Xfilewritable'))
839
840  call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
841  call assert_equal(1, filewritable('Xfilewritable'))
842
843  call assert_equal(0, filewritable('doesnotexist'))
844
845  call delete('Xfilewritable')
846  bw!
847endfunc
848
849func Test_Executable()
850  if has('win32')
851    call assert_equal(1, executable('notepad'))
852    call assert_equal(1, executable('notepad.exe'))
853    call assert_equal(0, executable('notepad.exe.exe'))
854    call assert_equal(0, executable('shell32.dll'))
855    call assert_equal(0, executable('win.ini'))
856  elseif has('unix')
857    call assert_equal(1, executable('cat'))
858    call assert_equal(0, executable('nodogshere'))
859  endif
860endfunc
861
862func Test_hostname()
863  let hostname_vim = hostname()
864  if has('unix')
865    let hostname_system = systemlist('uname -n')[0]
866    call assert_equal(hostname_vim, hostname_system)
867  endif
868endfunc
869
870func Test_getpid()
871  " getpid() always returns the same value within a vim instance.
872  call assert_equal(getpid(), getpid())
873  if has('unix')
874    call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
875  endif
876endfunc
877
878func Test_hlexists()
879  call assert_equal(0, hlexists('does_not_exist'))
880  call assert_equal(0, hlexists('Number'))
881  call assert_equal(0, highlight_exists('does_not_exist'))
882  call assert_equal(0, highlight_exists('Number'))
883  syntax on
884  call assert_equal(0, hlexists('does_not_exist'))
885  call assert_equal(1, hlexists('Number'))
886  call assert_equal(0, highlight_exists('does_not_exist'))
887  call assert_equal(1, highlight_exists('Number'))
888  syntax off
889endfunc
890
891func Test_col()
892  new
893  call setline(1, 'abcdef')
894  norm gg4|mx6|mY2|
895  call assert_equal(2, col('.'))
896  call assert_equal(7, col('$'))
897  call assert_equal(4, col("'x"))
898  call assert_equal(6, col("'Y"))
899  call assert_equal(2, col([1, 2]))
900  call assert_equal(7, col([1, '$']))
901
902  call assert_equal(0, col(''))
903  call assert_equal(0, col('x'))
904  call assert_equal(0, col([2, '$']))
905  call assert_equal(0, col([1, 100]))
906  call assert_equal(0, col([1]))
907  bw!
908endfunc
909
910func Test_inputlist()
911  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
912  call assert_equal(1, c)
913  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>2\<cr>", 'tx')
914  call assert_equal(2, c)
915  call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
916  call assert_equal(3, c)
917
918  call assert_fails('call inputlist("")', 'E686:')
919endfunc
920
921func Test_balloon_show()
922  if has('balloon_eval')
923    " This won't do anything but must not crash either.
924    call balloon_show('hi!')
925  endif
926endfunc
927
928func Test_setbufvar_options()
929  " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
930  " window layout.
931  call assert_equal(1, winnr('$'))
932  split dummy_preview
933  resize 2
934  set winfixheight winfixwidth
935  let prev_id = win_getid()
936
937  wincmd j
938  let wh = winheight('.')
939  let dummy_buf = bufnr('dummy_buf1', v:true)
940  call setbufvar(dummy_buf, '&buftype', 'nofile')
941  execute 'belowright vertical split #' . dummy_buf
942  call assert_equal(wh, winheight('.'))
943  let dum1_id = win_getid()
944
945  wincmd h
946  let wh = winheight('.')
947  let dummy_buf = bufnr('dummy_buf2', v:true)
948  call setbufvar(dummy_buf, '&buftype', 'nofile')
949  execute 'belowright vertical split #' . dummy_buf
950  call assert_equal(wh, winheight('.'))
951
952  bwipe!
953  call win_gotoid(prev_id)
954  bwipe!
955  call win_gotoid(dum1_id)
956  bwipe!
957endfunc
958
959func Test_redo_in_nested_functions()
960  nnoremap g. :set opfunc=Operator<CR>g@
961  function Operator( type, ... )
962     let @x = 'XXX'
963     execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
964  endfunction
965
966  function! Apply()
967      5,6normal! .
968  endfunction
969
970  new
971  call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
972  1normal g.i"
973  call assert_equal('some "XXX" text', getline(1))
974  3,4normal .
975  call assert_equal('some "XXX" text', getline(3))
976  call assert_equal('more "XXX" text', getline(4))
977  call Apply()
978  call assert_equal('some "XXX" text', getline(5))
979  call assert_equal('more "XXX" text', getline(6))
980  bwipe!
981
982  nunmap g.
983  delfunc Operator
984  delfunc Apply
985endfunc
986
987func Test_shellescape()
988  let save_shell = &shell
989  set shell=bash
990  call assert_equal("'text'", shellescape('text'))
991  call assert_equal("'te\"xt'", shellescape('te"xt'))
992  call assert_equal("'te'\\''xt'", shellescape("te'xt"))
993
994  call assert_equal("'te%xt'", shellescape("te%xt"))
995  call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
996  call assert_equal("'te#xt'", shellescape("te#xt"))
997  call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
998  call assert_equal("'te!xt'", shellescape("te!xt"))
999  call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1000
1001  call assert_equal("'te\nxt'", shellescape("te\nxt"))
1002  call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1003  set shell=tcsh
1004  call assert_equal("'te\\!xt'", shellescape("te!xt"))
1005  call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1006  call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1007  call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1008
1009  let &shell = save_shell
1010endfunc
1011
1012func Test_trim()
1013  call assert_equal("Testing", trim("  \t\r\r\x0BTesting  \t\n\r\n\t\x0B\x0B"))
1014  call assert_equal("Testing", trim("  \t  \r\r\n\n\x0BTesting  \t\n\r\n\t\x0B\x0B"))
1015  call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1016  call assert_equal("wRE    \tSERVEzyww", trim("wRE    \tSERVEzyww"))
1017  call assert_equal("abcd\t     xxxx   tail", trim(" \tabcd\t     xxxx   tail"))
1018  call assert_equal("\tabcd\t     xxxx   tail", trim(" \tabcd\t     xxxx   tail", " "))
1019  call assert_equal(" \tabcd\t     xxxx   tail", trim(" \tabcd\t     xxxx   tail", "abx"))
1020  call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1021  call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1022  call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r   你好您R E SER V E早好你你    \t  \x0B", ))
1023  call assert_equal("您R E SER V E早好你你    \t  \x0B", trim("    你好您R E SER V E早好你你    \t  \x0B", " 你好"))
1024  call assert_equal("您R E SER V E早好你你    \t  \x0B", trim("    tteesstttt你好您R E SER V E早好你你    \t  \x0B ttestt", " 你好tes"))
1025  call assert_equal("您R E SER V E早好你你    \t  \x0B", trim("    tteesstttt你好您R E SER V E早好你你    \t  \x0B ttestt", "   你你你好好好tttsses"))
1026  call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1027  call assert_equal("", trim("", ""))
1028  call assert_equal("a", trim("a", ""))
1029  call assert_equal("", trim("", "a"))
1030
1031  let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '')
1032  call assert_equal("x", trim(chars . "x" . chars))
1033endfunc
1034
1035" Test for reg_recording() and reg_executing()
1036func Test_reg_executing_and_recording()
1037  let s:reg_stat = ''
1038  func s:save_reg_stat()
1039    let s:reg_stat = reg_recording() . ':' . reg_executing()
1040    return ''
1041  endfunc
1042
1043  new
1044  call s:save_reg_stat()
1045  call assert_equal(':', s:reg_stat)
1046  call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1047  call assert_equal('a:', s:reg_stat)
1048  call feedkeys("@a", 'xt')
1049  call assert_equal(':a', s:reg_stat)
1050  call feedkeys("qb@aq", 'xt')
1051  call assert_equal('b:a', s:reg_stat)
1052  call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1053  call assert_equal('":', s:reg_stat)
1054
1055  bwipe!
1056  delfunc s:save_reg_stat
1057  unlet s:reg_stat
1058endfunc
1059
1060func Test_libcall_libcallnr()
1061  if !has('libcall')
1062    return
1063  endif
1064
1065  if has('win32')
1066    let libc = 'msvcrt.dll'
1067  elseif has('mac')
1068    let libc = 'libSystem.B.dylib'
1069  elseif system('uname -s') =~ 'SunOS'
1070    " Set the path to libc.so according to the architecture.
1071    let test_bits = system('file ' . GetVimProg())
1072    let test_arch = system('uname -p')
1073    if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1074      let libc = '/usr/lib/sparcv9/libc.so'
1075    elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1076      let libc = '/usr/lib/amd64/libc.so'
1077    else
1078      let libc = '/usr/lib/libc.so'
1079    endif
1080  else
1081    " On Unix, libc.so can be in various places.
1082    " Interestingly, using an empty string for the 1st argument of libcall
1083    " allows to call functions from libc which is not documented.
1084    let libc = ''
1085  endif
1086
1087  if has('win32')
1088    call assert_equal($USERPROFILE, libcall(libc, 'getenv', 'USERPROFILE'))
1089  else
1090    call assert_equal($HOME, libcall(libc, 'getenv', 'HOME'))
1091  endif
1092
1093  " If function returns NULL, libcall() should return an empty string.
1094  call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1095
1096  " Test libcallnr() with string and integer argument.
1097  call assert_equal(4, libcallnr(libc, 'strlen', 'abcd'))
1098  call assert_equal(char2nr('A'), libcallnr(libc, 'toupper', char2nr('a')))
1099
1100  call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1101  call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1102
1103  call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1104  call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1105endfunc
1106
1107sandbox function Fsandbox()
1108  normal ix
1109endfunc
1110
1111func Test_func_sandbox()
1112  sandbox let F = {-> 'hello'}
1113  call assert_equal('hello', F())
1114
1115  sandbox let F = {-> execute("normal ix\<Esc>")}
1116  call assert_fails('call F()', 'E48:')
1117  unlet F
1118
1119  call assert_fails('call Fsandbox()', 'E48:')
1120  delfunc Fsandbox
1121endfunc
1122