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