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("Ⱥ Ⱦ"))
271endfunc
272
273func Test_toupper()
274  call assert_equal("", toupper(""))
275
276  " Test with all printable ASCII characters.
277  call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
278          \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
279
280  if !has('multi_byte')
281    return
282  endif
283
284  " Test with a few lowercase diacritics.
285  call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("aàáâãäåāăąǎǟǡả"))
286  call assert_equal("BḂḆ", toupper("bḃḇ"))
287  call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
288  call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
289  call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
290  call assert_equal("FḞ", toupper("fḟ"))
291  call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
292  call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
293  call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
294  call assert_equal("JĴǰ", toupper("jĵǰ"))
295  call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
296  call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
297  call assert_equal("MḾṀ ", toupper("mḿṁ "))
298  call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
299  call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
300  call assert_equal("PṔṖ", toupper("pṕṗ"))
301  call assert_equal("Q", toupper("q"))
302  call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
303  call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
304  call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
305  call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
306  call assert_equal("VṼ", toupper("vṽ"))
307  call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
308  call assert_equal("ẊẌ", toupper("ẋẍ"))
309  call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
310  call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
311
312  " Test that uppercase diacritics, which should remain unchanged.
313  call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
314  call assert_equal("BḂḆ", toupper("BḂḆ"))
315  call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
316  call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
317  call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
318  call assert_equal("FḞ ", toupper("FḞ "))
319  call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
320  call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
321  call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
322  call assert_equal("JĴ", toupper("JĴ"))
323  call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
324  call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
325  call assert_equal("MḾṀ", toupper("MḾṀ"))
326  call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
327  call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
328  call assert_equal("PṔṖ", toupper("PṔṖ"))
329  call assert_equal("Q", toupper("Q"))
330  call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
331  call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
332  call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
333  call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
334  call assert_equal("VṼ", toupper("VṼ"))
335  call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
336  call assert_equal("XẊẌ", toupper("XẊẌ"))
337  call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
338  call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
339
340  call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
341endfunc
342
343" Tests for the mode() function
344let current_modes = ''
345func Save_mode()
346  let g:current_modes = mode(0) . '-' . mode(1)
347  return ''
348endfunc
349
350func Test_mode()
351  new
352  call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
353
354  " Only complete from the current buffer.
355  set complete=.
356
357  inoremap <F2> <C-R>=Save_mode()<CR>
358
359  normal! 3G
360  exe "normal i\<F2>\<Esc>"
361  call assert_equal('i-i', g:current_modes)
362  " i_CTRL-P: Multiple matches
363  exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
364  call assert_equal('i-ic', g:current_modes)
365  " i_CTRL-P: Single match
366  exe "normal iBro\<C-P>\<F2>\<Esc>u"
367  call assert_equal('i-ic', g:current_modes)
368  " i_CTRL-X
369  exe "normal iBa\<C-X>\<F2>\<Esc>u"
370  call assert_equal('i-ix', g:current_modes)
371  " i_CTRL-X CTRL-P: Multiple matches
372  exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
373  call assert_equal('i-ic', g:current_modes)
374  " i_CTRL-X CTRL-P: Single match
375  exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
376  call assert_equal('i-ic', g:current_modes)
377  " i_CTRL-X CTRL-P + CTRL-P: Single match
378  exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
379  call assert_equal('i-ic', g:current_modes)
380  " i_CTRL-X CTRL-L: Multiple matches
381  exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
382  call assert_equal('i-ic', g:current_modes)
383  " i_CTRL-X CTRL-L: Single match
384  exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
385  call assert_equal('i-ic', g:current_modes)
386  " i_CTRL-P: No match
387  exe "normal iCom\<C-P>\<F2>\<Esc>u"
388  call assert_equal('i-ic', g:current_modes)
389  " i_CTRL-X CTRL-P: No match
390  exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
391  call assert_equal('i-ic', g:current_modes)
392  " i_CTRL-X CTRL-L: No match
393  exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
394  call assert_equal('i-ic', g:current_modes)
395
396  " R_CTRL-P: Multiple matches
397  exe "normal RBa\<C-P>\<F2>\<Esc>u"
398  call assert_equal('R-Rc', g:current_modes)
399  " R_CTRL-P: Single match
400  exe "normal RBro\<C-P>\<F2>\<Esc>u"
401  call assert_equal('R-Rc', g:current_modes)
402  " R_CTRL-X
403  exe "normal RBa\<C-X>\<F2>\<Esc>u"
404  call assert_equal('R-Rx', g:current_modes)
405  " R_CTRL-X CTRL-P: Multiple matches
406  exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
407  call assert_equal('R-Rc', g:current_modes)
408  " R_CTRL-X CTRL-P: Single match
409  exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
410  call assert_equal('R-Rc', g:current_modes)
411  " R_CTRL-X CTRL-P + CTRL-P: Single match
412  exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
413  call assert_equal('R-Rc', g:current_modes)
414  " R_CTRL-X CTRL-L: Multiple matches
415  exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
416  call assert_equal('R-Rc', g:current_modes)
417  " R_CTRL-X CTRL-L: Single match
418  exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
419  call assert_equal('R-Rc', g:current_modes)
420  " R_CTRL-P: No match
421  exe "normal RCom\<C-P>\<F2>\<Esc>u"
422  call assert_equal('R-Rc', g:current_modes)
423  " R_CTRL-X CTRL-P: No match
424  exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
425  call assert_equal('R-Rc', g:current_modes)
426  " R_CTRL-X CTRL-L: No match
427  exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
428  call assert_equal('R-Rc', g:current_modes)
429
430  call assert_equal('n', mode(0))
431  call assert_equal('n', mode(1))
432
433  " How to test operator-pending mode?
434
435  call feedkeys("v", 'xt')
436  call assert_equal('v', mode())
437  call assert_equal('v', mode(1))
438  call feedkeys("\<Esc>V", 'xt')
439  call assert_equal('V', mode())
440  call assert_equal('V', mode(1))
441  call feedkeys("\<Esc>\<C-V>", 'xt')
442  call assert_equal("\<C-V>", mode())
443  call assert_equal("\<C-V>", mode(1))
444  call feedkeys("\<Esc>", 'xt')
445
446  call feedkeys("gh", 'xt')
447  call assert_equal('s', mode())
448  call assert_equal('s', mode(1))
449  call feedkeys("\<Esc>gH", 'xt')
450  call assert_equal('S', mode())
451  call assert_equal('S', mode(1))
452  call feedkeys("\<Esc>g\<C-H>", 'xt')
453  call assert_equal("\<C-S>", mode())
454  call assert_equal("\<C-S>", mode(1))
455  call feedkeys("\<Esc>", 'xt')
456
457  call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
458  call assert_equal('c-c', g:current_modes)
459  call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
460  call assert_equal('c-cv', g:current_modes)
461  " How to test Ex mode?
462
463  bwipe!
464  iunmap <F2>
465  set complete&
466endfunc
467
468func Test_getbufvar()
469  let bnr = bufnr('%')
470  let b:var_num = '1234'
471  let def_num = '5678'
472  call assert_equal('1234', getbufvar(bnr, 'var_num'))
473  call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
474
475  let bd = getbufvar(bnr, '')
476  call assert_equal('1234', bd['var_num'])
477  call assert_true(exists("bd['changedtick']"))
478  call assert_equal(2, len(bd))
479
480  let bd2 = getbufvar(bnr, '', def_num)
481  call assert_equal(bd, bd2)
482
483  unlet b:var_num
484  call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
485  call assert_equal('', getbufvar(bnr, 'var_num'))
486
487  let bd = getbufvar(bnr, '')
488  call assert_equal(1, len(bd))
489  let bd = getbufvar(bnr, '',def_num)
490  call assert_equal(1, len(bd))
491
492  call assert_equal('', getbufvar(9999, ''))
493  call assert_equal(def_num, getbufvar(9999, '', def_num))
494  unlet def_num
495
496  call assert_equal(0, getbufvar(bnr, '&autoindent'))
497  call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
498
499  " Open new window with forced option values
500  set fileformats=unix,dos
501  new ++ff=dos ++bin ++enc=iso-8859-2
502  call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
503  call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
504  call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
505  close
506
507  set fileformats&
508endfunc
509
510func Test_last_buffer_nr()
511  call assert_equal(bufnr('$'), last_buffer_nr())
512endfunc
513
514func Test_stridx()
515  call assert_equal(-1, stridx('', 'l'))
516  call assert_equal(0,  stridx('', ''))
517  call assert_equal(0,  stridx('hello', ''))
518  call assert_equal(-1, stridx('hello', 'L'))
519  call assert_equal(2,  stridx('hello', 'l', -1))
520  call assert_equal(2,  stridx('hello', 'l', 0))
521  call assert_equal(2,  stridx('hello', 'l', 1))
522  call assert_equal(3,  stridx('hello', 'l', 3))
523  call assert_equal(-1, stridx('hello', 'l', 4))
524  call assert_equal(-1, stridx('hello', 'l', 10))
525  call assert_equal(2,  stridx('hello', 'll'))
526  call assert_equal(-1, stridx('hello', 'hello world'))
527endfunc
528
529func Test_strridx()
530  call assert_equal(-1, strridx('', 'l'))
531  call assert_equal(0,  strridx('', ''))
532  call assert_equal(5,  strridx('hello', ''))
533  call assert_equal(-1, strridx('hello', 'L'))
534  call assert_equal(3,  strridx('hello', 'l'))
535  call assert_equal(3,  strridx('hello', 'l', 10))
536  call assert_equal(3,  strridx('hello', 'l', 3))
537  call assert_equal(2,  strridx('hello', 'l', 2))
538  call assert_equal(-1, strridx('hello', 'l', 1))
539  call assert_equal(-1, strridx('hello', 'l', 0))
540  call assert_equal(-1, strridx('hello', 'l', -1))
541  call assert_equal(2,  strridx('hello', 'll'))
542  call assert_equal(-1, strridx('hello', 'hello world'))
543endfunc
544
545func Test_match_func()
546  call assert_equal(4,  match('testing', 'ing'))
547  call assert_equal(4,  match('testing', 'ing', 2))
548  call assert_equal(-1, match('testing', 'ing', 5))
549  call assert_equal(-1, match('testing', 'ing', 8))
550  call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
551  call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
552endfunc
553
554func Test_matchend()
555  call assert_equal(7,  matchend('testing', 'ing'))
556  call assert_equal(7,  matchend('testing', 'ing', 2))
557  call assert_equal(-1, matchend('testing', 'ing', 5))
558  call assert_equal(-1, matchend('testing', 'ing', 8))
559  call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
560  call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
561endfunc
562
563func Test_matchlist()
564  call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''],  matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
565  call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''],  matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
566  call assert_equal([],  matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
567endfunc
568
569func Test_matchstr()
570  call assert_equal('ing',  matchstr('testing', 'ing'))
571  call assert_equal('ing',  matchstr('testing', 'ing', 2))
572  call assert_equal('', matchstr('testing', 'ing', 5))
573  call assert_equal('', matchstr('testing', 'ing', 8))
574  call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
575  call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
576endfunc
577
578func Test_matchstrpos()
579  call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
580  call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2))
581  call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
582  call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
583  call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
584  call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
585endfunc
586
587func Test_nextnonblank_prevnonblank()
588  new
589insert
590This
591
592
593is
594
595a
596Test
597.
598  call assert_equal(0, nextnonblank(-1))
599  call assert_equal(0, nextnonblank(0))
600  call assert_equal(1, nextnonblank(1))
601  call assert_equal(4, nextnonblank(2))
602  call assert_equal(4, nextnonblank(3))
603  call assert_equal(4, nextnonblank(4))
604  call assert_equal(6, nextnonblank(5))
605  call assert_equal(6, nextnonblank(6))
606  call assert_equal(7, nextnonblank(7))
607  call assert_equal(0, nextnonblank(8))
608
609  call assert_equal(0, prevnonblank(-1))
610  call assert_equal(0, prevnonblank(0))
611  call assert_equal(1, prevnonblank(1))
612  call assert_equal(1, prevnonblank(2))
613  call assert_equal(1, prevnonblank(3))
614  call assert_equal(4, prevnonblank(4))
615  call assert_equal(4, prevnonblank(5))
616  call assert_equal(6, prevnonblank(6))
617  call assert_equal(7, prevnonblank(7))
618  call assert_equal(0, prevnonblank(8))
619  bw!
620endfunc
621
622func Test_byte2line_line2byte()
623  new
624  call setline(1, ['a', 'bc', 'd'])
625
626  set fileformat=unix
627  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
628  \                 map(range(-1, 8), 'byte2line(v:val)'))
629  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
630  \                 map(range(-1, 5), 'line2byte(v:val)'))
631
632  set fileformat=mac
633  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
634  \                 map(range(-1, 8), 'byte2line(v:val)'))
635  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
636  \                 map(range(-1, 5), 'line2byte(v:val)'))
637
638  set fileformat=dos
639  call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
640  \                 map(range(-1, 11), 'byte2line(v:val)'))
641  call assert_equal([-1, -1, 1, 4, 8, 11, -1],
642  \                 map(range(-1, 5), 'line2byte(v:val)'))
643
644  set fileformat&
645  bw!
646endfunc
647
648func Test_count()
649  let l = ['a', 'a', 'A', 'b']
650  call assert_equal(2, count(l, 'a'))
651  call assert_equal(1, count(l, 'A'))
652  call assert_equal(1, count(l, 'b'))
653  call assert_equal(0, count(l, 'B'))
654
655  call assert_equal(2, count(l, 'a', 0))
656  call assert_equal(1, count(l, 'A', 0))
657  call assert_equal(1, count(l, 'b', 0))
658  call assert_equal(0, count(l, 'B', 0))
659
660  call assert_equal(3, count(l, 'a', 1))
661  call assert_equal(3, count(l, 'A', 1))
662  call assert_equal(1, count(l, 'b', 1))
663  call assert_equal(1, count(l, 'B', 1))
664  call assert_equal(0, count(l, 'c', 1))
665
666  call assert_equal(1, count(l, 'a', 0, 1))
667  call assert_equal(2, count(l, 'a', 1, 1))
668  call assert_fails('call count(l, "a", 0, 10)', 'E684:')
669
670  let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
671  call assert_equal(2, count(d, 'a'))
672  call assert_equal(1, count(d, 'A'))
673  call assert_equal(1, count(d, 'b'))
674  call assert_equal(0, count(d, 'B'))
675
676  call assert_equal(2, count(d, 'a', 0))
677  call assert_equal(1, count(d, 'A', 0))
678  call assert_equal(1, count(d, 'b', 0))
679  call assert_equal(0, count(d, 'B', 0))
680
681  call assert_equal(3, count(d, 'a', 1))
682  call assert_equal(3, count(d, 'A', 1))
683  call assert_equal(1, count(d, 'b', 1))
684  call assert_equal(1, count(d, 'B', 1))
685  call assert_equal(0, count(d, 'c', 1))
686
687  call assert_fails('call count(d, "a", 0, 1)', 'E474:')
688
689  call assert_equal(0, count("foo", "bar"))
690  call assert_equal(1, count("foo", "oo"))
691  call assert_equal(2, count("foo", "o"))
692  call assert_equal(0, count("foo", "O"))
693  call assert_equal(2, count("foo", "O", 1))
694  call assert_equal(2, count("fooooo", "oo"))
695endfunc
696
697func Test_changenr()
698  new Xchangenr
699  call assert_equal(0, changenr())
700  norm ifoo
701  call assert_equal(1, changenr())
702  set undolevels=10
703  norm Sbar
704  call assert_equal(2, changenr())
705  undo
706  call assert_equal(1, changenr())
707  redo
708  call assert_equal(2, changenr())
709  bw!
710  set undolevels&
711endfunc
712
713func Test_filewritable()
714  new Xfilewritable
715  write!
716  call assert_equal(1, filewritable('Xfilewritable'))
717
718  call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
719  call assert_equal(0, filewritable('Xfilewritable'))
720
721  call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
722  call assert_equal(1, filewritable('Xfilewritable'))
723
724  call assert_equal(0, filewritable('doesnotexist'))
725
726  call delete('Xfilewritable')
727  bw!
728endfunc
729
730func Test_hostname()
731  let hostname_vim = hostname()
732  if has('unix')
733    let hostname_system = systemlist('uname -n')[0]
734    call assert_equal(hostname_vim, hostname_system)
735  endif
736endfunc
737
738func Test_getpid()
739  " getpid() always returns the same value within a vim instance.
740  call assert_equal(getpid(), getpid())
741  if has('unix')
742    call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
743  endif
744endfunc
745
746func Test_hlexists()
747  call assert_equal(0, hlexists('does_not_exist'))
748  call assert_equal(0, hlexists('Number'))
749  call assert_equal(0, highlight_exists('does_not_exist'))
750  call assert_equal(0, highlight_exists('Number'))
751  syntax on
752  call assert_equal(0, hlexists('does_not_exist'))
753  call assert_equal(1, hlexists('Number'))
754  call assert_equal(0, highlight_exists('does_not_exist'))
755  call assert_equal(1, highlight_exists('Number'))
756  syntax off
757endfunc
758
759func Test_col()
760  new
761  call setline(1, 'abcdef')
762  norm gg4|mx6|mY2|
763  call assert_equal(2, col('.'))
764  call assert_equal(7, col('$'))
765  call assert_equal(4, col("'x"))
766  call assert_equal(6, col("'Y"))
767  call assert_equal(2, col([1, 2]))
768  call assert_equal(7, col([1, '$']))
769
770  call assert_equal(0, col(''))
771  call assert_equal(0, col('x'))
772  call assert_equal(0, col([2, '$']))
773  call assert_equal(0, col([1, 100]))
774  call assert_equal(0, col([1]))
775  bw!
776endfunc
777
778func Test_balloon_show()
779  if has('balloon_eval')
780    " This won't do anything but must not crash either.
781    call balloon_show('hi!')
782  endif
783endfunc
784
785func Test_setbufvar_options()
786  " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
787  " window layout.
788  call assert_equal(1, winnr('$'))
789  split dummy_preview
790  resize 2
791  set winfixheight winfixwidth
792  let prev_id = win_getid()
793
794  wincmd j
795  let wh = winheight('.')
796  let dummy_buf = bufnr('dummy_buf1', v:true)
797  call setbufvar(dummy_buf, '&buftype', 'nofile')
798  execute 'belowright vertical split #' . dummy_buf
799  call assert_equal(wh, winheight('.'))
800  let dum1_id = win_getid()
801
802  wincmd h
803  let wh = winheight('.')
804  let dummy_buf = bufnr('dummy_buf2', v:true)
805  call setbufvar(dummy_buf, '&buftype', 'nofile')
806  execute 'belowright vertical split #' . dummy_buf
807  call assert_equal(wh, winheight('.'))
808
809  bwipe!
810  call win_gotoid(prev_id)
811  bwipe!
812  call win_gotoid(dum1_id)
813  bwipe!
814endfunc
815
816func Test_redo_in_nested_functions()
817  nnoremap g. :set opfunc=Operator<CR>g@
818  function Operator( type, ... )
819     let @x = 'XXX'
820     execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
821  endfunction
822
823  function! Apply()
824      5,6normal! .
825  endfunction
826
827  new
828  call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
829  1normal g.i"
830  call assert_equal('some "XXX" text', getline(1))
831  3,4normal .
832  call assert_equal('some "XXX" text', getline(3))
833  call assert_equal('more "XXX" text', getline(4))
834  call Apply()
835  call assert_equal('some "XXX" text', getline(5))
836  call assert_equal('more "XXX" text', getline(6))
837  bwipe!
838
839  nunmap g.
840  delfunc Operator
841  delfunc Apply
842endfunc
843
844func Test_shellescape()
845  let save_shell = &shell
846  set shell=bash
847  call assert_equal("'text'", shellescape('text'))
848  call assert_equal("'te\"xt'", shellescape('te"xt'))
849  call assert_equal("'te'\\''xt'", shellescape("te'xt"))
850
851  call assert_equal("'te%xt'", shellescape("te%xt"))
852  call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
853  call assert_equal("'te#xt'", shellescape("te#xt"))
854  call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
855  call assert_equal("'te!xt'", shellescape("te!xt"))
856  call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
857
858  call assert_equal("'te\nxt'", shellescape("te\nxt"))
859  call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
860  set shell=tcsh
861  call assert_equal("'te\\!xt'", shellescape("te!xt"))
862  call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
863  call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
864  call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
865
866  let &shell = save_shell
867endfunc
868