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