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_matchend()
532  call assert_equal(7,  matchend('testing', 'ing'))
533  call assert_equal(7,  matchend('testing', 'ing', 2))
534  call assert_equal(-1, matchend('testing', 'ing', 5))
535endfunc
536
537func Test_nextnonblank_prevnonblank()
538  new
539insert
540This
541
542
543is
544
545a
546Test
547.
548  call assert_equal(0, nextnonblank(-1))
549  call assert_equal(0, nextnonblank(0))
550  call assert_equal(1, nextnonblank(1))
551  call assert_equal(4, nextnonblank(2))
552  call assert_equal(4, nextnonblank(3))
553  call assert_equal(4, nextnonblank(4))
554  call assert_equal(6, nextnonblank(5))
555  call assert_equal(6, nextnonblank(6))
556  call assert_equal(7, nextnonblank(7))
557  call assert_equal(0, nextnonblank(8))
558
559  call assert_equal(0, prevnonblank(-1))
560  call assert_equal(0, prevnonblank(0))
561  call assert_equal(1, prevnonblank(1))
562  call assert_equal(1, prevnonblank(2))
563  call assert_equal(1, prevnonblank(3))
564  call assert_equal(4, prevnonblank(4))
565  call assert_equal(4, prevnonblank(5))
566  call assert_equal(6, prevnonblank(6))
567  call assert_equal(7, prevnonblank(7))
568  call assert_equal(0, prevnonblank(8))
569  bw!
570endfunc
571
572func Test_byte2line_line2byte()
573  new
574  call setline(1, ['a', 'bc', 'd'])
575
576  set fileformat=unix
577  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
578  \                 map(range(-1, 8), 'byte2line(v:val)'))
579  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
580  \                 map(range(-1, 5), 'line2byte(v:val)'))
581
582  set fileformat=mac
583  call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
584  \                 map(range(-1, 8), 'byte2line(v:val)'))
585  call assert_equal([-1, -1, 1, 3, 6, 8, -1],
586  \                 map(range(-1, 5), 'line2byte(v:val)'))
587
588  set fileformat=dos
589  call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
590  \                 map(range(-1, 11), 'byte2line(v:val)'))
591  call assert_equal([-1, -1, 1, 4, 8, 11, -1],
592  \                 map(range(-1, 5), 'line2byte(v:val)'))
593
594  set fileformat&
595  bw!
596endfunc
597
598func Test_count()
599  let l = ['a', 'a', 'A', 'b']
600  call assert_equal(2, count(l, 'a'))
601  call assert_equal(1, count(l, 'A'))
602  call assert_equal(1, count(l, 'b'))
603  call assert_equal(0, count(l, 'B'))
604
605  call assert_equal(2, count(l, 'a', 0))
606  call assert_equal(1, count(l, 'A', 0))
607  call assert_equal(1, count(l, 'b', 0))
608  call assert_equal(0, count(l, 'B', 0))
609
610  call assert_equal(3, count(l, 'a', 1))
611  call assert_equal(3, count(l, 'A', 1))
612  call assert_equal(1, count(l, 'b', 1))
613  call assert_equal(1, count(l, 'B', 1))
614  call assert_equal(0, count(l, 'c', 1))
615
616  call assert_equal(1, count(l, 'a', 0, 1))
617  call assert_equal(2, count(l, 'a', 1, 1))
618  call assert_fails('call count(l, "a", 0, 10)', 'E684:')
619
620  let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
621  call assert_equal(2, count(d, 'a'))
622  call assert_equal(1, count(d, 'A'))
623  call assert_equal(1, count(d, 'b'))
624  call assert_equal(0, count(d, 'B'))
625
626  call assert_equal(2, count(d, 'a', 0))
627  call assert_equal(1, count(d, 'A', 0))
628  call assert_equal(1, count(d, 'b', 0))
629  call assert_equal(0, count(d, 'B', 0))
630
631  call assert_equal(3, count(d, 'a', 1))
632  call assert_equal(3, count(d, 'A', 1))
633  call assert_equal(1, count(d, 'b', 1))
634  call assert_equal(1, count(d, 'B', 1))
635  call assert_equal(0, count(d, 'c', 1))
636
637  call assert_fails('call count(d, "a", 0, 1)', 'E474:')
638
639  call assert_equal(0, count("foo", "bar"))
640  call assert_equal(1, count("foo", "oo"))
641  call assert_equal(2, count("foo", "o"))
642  call assert_equal(0, count("foo", "O"))
643  call assert_equal(2, count("foo", "O", 1))
644  call assert_equal(2, count("fooooo", "oo"))
645endfunc
646
647func Test_changenr()
648  new Xchangenr
649  call assert_equal(0, changenr())
650  norm ifoo
651  call assert_equal(1, changenr())
652  set undolevels=10
653  norm Sbar
654  call assert_equal(2, changenr())
655  undo
656  call assert_equal(1, changenr())
657  redo
658  call assert_equal(2, changenr())
659  bw!
660  set undolevels&
661endfunc
662
663func Test_filewritable()
664  new Xfilewritable
665  write!
666  call assert_equal(1, filewritable('Xfilewritable'))
667
668  call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
669  call assert_equal(0, filewritable('Xfilewritable'))
670
671  call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
672  call assert_equal(1, filewritable('Xfilewritable'))
673
674  call assert_equal(0, filewritable('doesnotexist'))
675
676  call delete('Xfilewritable')
677  bw!
678endfunc
679
680func Test_hostname()
681  let hostname_vim = hostname()
682  if has('unix')
683    let hostname_system = systemlist('uname -n')[0]
684    call assert_equal(hostname_vim, hostname_system)
685  endif
686endfunc
687
688func Test_getpid()
689  " getpid() always returns the same value within a vim instance.
690  call assert_equal(getpid(), getpid())
691  if has('unix')
692    call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
693  endif
694endfunc
695
696func Test_hlexists()
697  call assert_equal(0, hlexists('does_not_exist'))
698  call assert_equal(0, hlexists('Number'))
699  call assert_equal(0, highlight_exists('does_not_exist'))
700  call assert_equal(0, highlight_exists('Number'))
701  syntax on
702  call assert_equal(0, hlexists('does_not_exist'))
703  call assert_equal(1, hlexists('Number'))
704  call assert_equal(0, highlight_exists('does_not_exist'))
705  call assert_equal(1, highlight_exists('Number'))
706  syntax off
707endfunc
708
709func Test_col()
710  new
711  call setline(1, 'abcdef')
712  norm gg4|mx6|mY2|
713  call assert_equal(2, col('.'))
714  call assert_equal(7, col('$'))
715  call assert_equal(4, col("'x"))
716  call assert_equal(6, col("'Y"))
717  call assert_equal(2, col([1, 2]))
718  call assert_equal(7, col([1, '$']))
719
720  call assert_equal(0, col(''))
721  call assert_equal(0, col('x'))
722  call assert_equal(0, col([2, '$']))
723  call assert_equal(0, col([1, 100]))
724  call assert_equal(0, col([1]))
725  bw!
726endfunc
727
728func Test_balloon_show()
729  if has('balloon_eval')
730    " This won't do anything but must not crash either.
731    call balloon_show('hi!')
732  endif
733endfunc
734
735func Test_setbufvar_options()
736  " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
737  " window layout.
738  call assert_equal(1, winnr('$'))
739  split dummy_preview
740  resize 2
741  set winfixheight winfixwidth
742  let prev_id = win_getid()
743
744  wincmd j
745  let wh = winheight('.')
746  let dummy_buf = bufnr('dummy_buf1', v:true)
747  call setbufvar(dummy_buf, '&buftype', 'nofile')
748  execute 'belowright vertical split #' . dummy_buf
749  call assert_equal(wh, winheight('.'))
750  let dum1_id = win_getid()
751
752  wincmd h
753  let wh = winheight('.')
754  let dummy_buf = bufnr('dummy_buf2', v:true)
755  call setbufvar(dummy_buf, '&buftype', 'nofile')
756  execute 'belowright vertical split #' . dummy_buf
757  call assert_equal(wh, winheight('.'))
758
759  bwipe!
760  call win_gotoid(prev_id)
761  bwipe!
762  call win_gotoid(dum1_id)
763  bwipe!
764endfunc
765
766func Test_redo_in_nested_functions()
767  nnoremap g. :set opfunc=Operator<CR>g@
768  function Operator( type, ... )
769     let @x = 'XXX'
770     execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
771  endfunction
772
773  function! Apply()
774      5,6normal! .
775  endfunction
776
777  new
778  call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
779  1normal g.i"
780  call assert_equal('some "XXX" text', getline(1))
781  3,4normal .
782  call assert_equal('some "XXX" text', getline(3))
783  call assert_equal('more "XXX" text', getline(4))
784  call Apply()
785  call assert_equal('some "XXX" text', getline(5))
786  call assert_equal('more "XXX" text', getline(6))
787  bwipe!
788
789  nunmap g.
790  delfunc Operator
791  delfunc Apply
792endfunc
793
794func Test_shellescape()
795  let save_shell = &shell
796  set shell=bash
797  call assert_equal("'text'", shellescape('text'))
798  call assert_equal("'te\"xt'", shellescape('te"xt'))
799  call assert_equal("'te'\\''xt'", shellescape("te'xt"))
800
801  call assert_equal("'te%xt'", shellescape("te%xt"))
802  call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
803  call assert_equal("'te#xt'", shellescape("te#xt"))
804  call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
805  call assert_equal("'te!xt'", shellescape("te!xt"))
806  call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
807
808  call assert_equal("'te\nxt'", shellescape("te\nxt"))
809  call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
810  set shell=tcsh
811  call assert_equal("'te\\!xt'", shellescape("te!xt"))
812  call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
813  call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
814  call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
815
816  let &shell = save_shell
817endfunc
818