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