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  call assert_fails('call count("a", "a")', 'E712:')
639endfunc
640
641func Test_changenr()
642  new Xchangenr
643  call assert_equal(0, changenr())
644  norm ifoo
645  call assert_equal(1, changenr())
646  set undolevels=10
647  norm Sbar
648  call assert_equal(2, changenr())
649  undo
650  call assert_equal(1, changenr())
651  redo
652  call assert_equal(2, changenr())
653  bw!
654  set undolevels&
655endfunc
656
657func Test_filewritable()
658  new Xfilewritable
659  write!
660  call assert_equal(1, filewritable('Xfilewritable'))
661
662  call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
663  call assert_equal(0, filewritable('Xfilewritable'))
664
665  call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
666  call assert_equal(1, filewritable('Xfilewritable'))
667
668  call assert_equal(0, filewritable('doesnotexist'))
669
670  call delete('Xfilewritable')
671  bw!
672endfunc
673
674func Test_hostname()
675  let hostname_vim = hostname()
676  if has('unix')
677    let hostname_system = systemlist('uname -n')[0]
678    call assert_equal(hostname_vim, hostname_system)
679  endif
680endfunc
681
682func Test_getpid()
683  " getpid() always returns the same value within a vim instance.
684  call assert_equal(getpid(), getpid())
685  if has('unix')
686    call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
687  endif
688endfunc
689
690func Test_hlexists()
691  call assert_equal(0, hlexists('does_not_exist'))
692  call assert_equal(0, hlexists('Number'))
693  call assert_equal(0, highlight_exists('does_not_exist'))
694  call assert_equal(0, highlight_exists('Number'))
695  syntax on
696  call assert_equal(0, hlexists('does_not_exist'))
697  call assert_equal(1, hlexists('Number'))
698  call assert_equal(0, highlight_exists('does_not_exist'))
699  call assert_equal(1, highlight_exists('Number'))
700  syntax off
701endfunc
702
703func Test_col()
704  new
705  call setline(1, 'abcdef')
706  norm gg4|mx6|mY2|
707  call assert_equal(2, col('.'))
708  call assert_equal(7, col('$'))
709  call assert_equal(4, col("'x"))
710  call assert_equal(6, col("'Y"))
711  call assert_equal(2, col([1, 2]))
712  call assert_equal(7, col([1, '$']))
713
714  call assert_equal(0, col(''))
715  call assert_equal(0, col('x'))
716  call assert_equal(0, col([2, '$']))
717  call assert_equal(0, col([1, 100]))
718  call assert_equal(0, col([1]))
719  bw!
720endfunc
721
722func Test_balloon_show()
723  if has('balloon_eval')
724    " This won't do anything but must not crash either.
725    call balloon_show('hi!')
726  endif
727endfunc
728
729func Test_setbufvar_options()
730  " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
731  " window layout.
732  call assert_equal(1, winnr('$'))
733  split dummy_preview
734  resize 2
735  set winfixheight winfixwidth
736  let prev_id = win_getid()
737
738  wincmd j
739  let wh = winheight('.')
740  let dummy_buf = bufnr('dummy_buf1', v:true)
741  call setbufvar(dummy_buf, '&buftype', 'nofile')
742  execute 'belowright vertical split #' . dummy_buf
743  call assert_equal(wh, winheight('.'))
744  let dum1_id = win_getid()
745
746  wincmd h
747  let wh = winheight('.')
748  let dummy_buf = bufnr('dummy_buf2', v:true)
749  call setbufvar(dummy_buf, '&buftype', 'nofile')
750  execute 'belowright vertical split #' . dummy_buf
751  call assert_equal(wh, winheight('.'))
752
753  bwipe!
754  call win_gotoid(prev_id)
755  bwipe!
756  call win_gotoid(dum1_id)
757  bwipe!
758endfunc
759
760func Test_redo_in_nested_functions()
761  nnoremap g. :set opfunc=Operator<CR>g@
762  function Operator( type, ... )
763     let @x = 'XXX'
764     execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
765  endfunction
766
767  function! Apply()
768      5,6normal! .
769  endfunction
770
771  new
772  call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
773  1normal g.i"
774  call assert_equal('some "XXX" text', getline(1))
775  3,4normal .
776  call assert_equal('some "XXX" text', getline(3))
777  call assert_equal('more "XXX" text', getline(4))
778  call Apply()
779  call assert_equal('some "XXX" text', getline(5))
780  call assert_equal('more "XXX" text', getline(6))
781  bwipe!
782
783  nunmap g.
784  delfunc Operator
785  delfunc Apply
786endfunc
787