blob: 17f7c1905e19104d05e8044536b5c5c6bdfbd159 [file] [log] [blame]
Bram Moolenaar08243d22017-01-10 16:12:29 +01001" Tests for various functions.
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02002source shared.vim
Bram Moolenaar08243d22017-01-10 16:12:29 +01003
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +01004" Must be done first, since the alternate buffer must be unset.
5func Test_00_bufexists()
6 call assert_equal(0, bufexists('does_not_exist'))
7 call assert_equal(1, bufexists(bufnr('%')))
8 call assert_equal(0, bufexists(0))
9 new Xfoo
10 let bn = bufnr('%')
11 call assert_equal(1, bufexists(bn))
12 call assert_equal(1, bufexists('Xfoo'))
13 call assert_equal(1, bufexists(getcwd() . '/Xfoo'))
14 call assert_equal(1, bufexists(0))
15 bw
16 call assert_equal(0, bufexists(bn))
17 call assert_equal(0, bufexists('Xfoo'))
18endfunc
19
Bram Moolenaar24c2e482017-01-29 15:45:12 +010020func Test_empty()
21 call assert_equal(1, empty(''))
22 call assert_equal(0, empty('a'))
23
24 call assert_equal(1, empty(0))
25 call assert_equal(1, empty(-0))
26 call assert_equal(0, empty(1))
27 call assert_equal(0, empty(-1))
28
29 call assert_equal(1, empty(0.0))
30 call assert_equal(1, empty(-0.0))
31 call assert_equal(0, empty(1.0))
32 call assert_equal(0, empty(-1.0))
33 call assert_equal(0, empty(1.0/0.0))
34 call assert_equal(0, empty(0.0/0.0))
35
36 call assert_equal(1, empty([]))
37 call assert_equal(0, empty(['a']))
38
39 call assert_equal(1, empty({}))
40 call assert_equal(0, empty({'a':1}))
41
42 call assert_equal(1, empty(v:null))
43 call assert_equal(1, empty(v:none))
44 call assert_equal(1, empty(v:false))
45 call assert_equal(0, empty(v:true))
46
Bram Moolenaar41042f32017-03-09 12:09:32 +010047 if has('channel')
48 call assert_equal(1, empty(test_null_channel()))
49 endif
50 if has('job')
51 call assert_equal(1, empty(test_null_job()))
52 endif
53
Bram Moolenaar24c2e482017-01-29 15:45:12 +010054 call assert_equal(0, empty(function('Test_empty')))
55endfunc
56
57func Test_len()
58 call assert_equal(1, len(0))
59 call assert_equal(2, len(12))
60
61 call assert_equal(0, len(''))
62 call assert_equal(2, len('ab'))
63
64 call assert_equal(0, len([]))
65 call assert_equal(2, len([2, 1]))
66
67 call assert_equal(0, len({}))
68 call assert_equal(2, len({'a': 1, 'b': 2}))
69
70 call assert_fails('call len(v:none)', 'E701:')
71 call assert_fails('call len({-> 0})', 'E701:')
72endfunc
73
74func Test_max()
75 call assert_equal(0, max([]))
76 call assert_equal(2, max([2]))
77 call assert_equal(2, max([1, 2]))
78 call assert_equal(2, max([1, 2, v:null]))
79
80 call assert_equal(0, max({}))
81 call assert_equal(2, max({'a':1, 'b':2}))
82
83 call assert_fails('call max(1)', 'E712:')
84 call assert_fails('call max(v:none)', 'E712:')
85endfunc
86
87func Test_min()
88 call assert_equal(0, min([]))
89 call assert_equal(2, min([2]))
90 call assert_equal(1, min([1, 2]))
91 call assert_equal(0, min([1, 2, v:null]))
92
93 call assert_equal(0, min({}))
94 call assert_equal(1, min({'a':1, 'b':2}))
95
96 call assert_fails('call min(1)', 'E712:')
97 call assert_fails('call min(v:none)', 'E712:')
98endfunc
99
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200100func Test_strwidth()
101 for aw in ['single', 'double']
102 exe 'set ambiwidth=' . aw
103 call assert_equal(0, strwidth(''))
104 call assert_equal(1, strwidth("\t"))
105 call assert_equal(3, strwidth('Vim'))
106 call assert_equal(4, strwidth(1234))
107 call assert_equal(5, strwidth(-1234))
108
109 if has('multi_byte')
110 call assert_equal(2, strwidth('😉'))
111 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
112 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
113 endif
114
115 call assert_fails('call strwidth({->0})', 'E729:')
116 call assert_fails('call strwidth([])', 'E730:')
117 call assert_fails('call strwidth({})', 'E731:')
118 call assert_fails('call strwidth(1.2)', 'E806:')
119 endfor
120
121 set ambiwidth&
122endfunc
123
Bram Moolenaar08243d22017-01-10 16:12:29 +0100124func Test_str2nr()
125 call assert_equal(0, str2nr(''))
126 call assert_equal(1, str2nr('1'))
127 call assert_equal(1, str2nr(' 1 '))
128
129 call assert_equal(1, str2nr('+1'))
130 call assert_equal(1, str2nr('+ 1'))
131 call assert_equal(1, str2nr(' + 1 '))
132
133 call assert_equal(-1, str2nr('-1'))
134 call assert_equal(-1, str2nr('- 1'))
135 call assert_equal(-1, str2nr(' - 1 '))
136
137 call assert_equal(123456789, str2nr('123456789'))
138 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100139
140 call assert_equal(5, str2nr('101', 2))
141 call assert_equal(5, str2nr('0b101', 2))
142 call assert_equal(5, str2nr('0B101', 2))
143 call assert_equal(-5, str2nr('-101', 2))
144 call assert_equal(-5, str2nr('-0b101', 2))
145 call assert_equal(-5, str2nr('-0B101', 2))
146
147 call assert_equal(65, str2nr('101', 8))
148 call assert_equal(65, str2nr('0101', 8))
149 call assert_equal(-65, str2nr('-101', 8))
150 call assert_equal(-65, str2nr('-0101', 8))
151
152 call assert_equal(11259375, str2nr('abcdef', 16))
153 call assert_equal(11259375, str2nr('ABCDEF', 16))
154 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
155 call assert_equal(11259375, str2nr('0xabcdef', 16))
156 call assert_equal(11259375, str2nr('0Xabcdef', 16))
157 call assert_equal(11259375, str2nr('0XABCDEF', 16))
158 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
159
160 call assert_equal(0, str2nr('0x10'))
161 call assert_equal(0, str2nr('0b10'))
162 call assert_equal(1, str2nr('12', 2))
163 call assert_equal(1, str2nr('18', 8))
164 call assert_equal(1, str2nr('1g', 16))
165
166 call assert_equal(0, str2nr(v:null))
167 call assert_equal(0, str2nr(v:none))
168
169 call assert_fails('call str2nr([])', 'E730:')
170 call assert_fails('call str2nr({->2})', 'E729:')
171 call assert_fails('call str2nr(1.2)', 'E806:')
172 call assert_fails('call str2nr(10, [])', 'E474:')
173endfunc
174
175func Test_strftime()
176 if !exists('*strftime')
177 return
178 endif
179 " Format of strftime() depends on system. We assume
180 " that basic formats tested here are available and
181 " identical on all systems which support strftime().
182 "
183 " The 2nd parameter of strftime() is a local time, so the output day
184 " of strftime() can be 17 or 18, depending on timezone.
185 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
186 "
187 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'))
188
189 call assert_fails('call strftime([])', 'E730:')
190 call assert_fails('call strftime("%Y", [])', 'E745:')
191endfunc
192
193func Test_simplify()
194 call assert_equal('', simplify(''))
195 call assert_equal('/', simplify('/'))
196 call assert_equal('/', simplify('/.'))
197 call assert_equal('/', simplify('/..'))
198 call assert_equal('/...', simplify('/...'))
199 call assert_equal('./dir/file', simplify('./dir/file'))
200 call assert_equal('./dir/file', simplify('.///dir//file'))
201 call assert_equal('./dir/file', simplify('./dir/./file'))
202 call assert_equal('./file', simplify('./dir/../file'))
203 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
204 call assert_equal('./file', simplify('dir/.././file'))
205
206 call assert_fails('call simplify({->0})', 'E729:')
207 call assert_fails('call simplify([])', 'E730:')
208 call assert_fails('call simplify({})', 'E731:')
209 call assert_fails('call simplify(1.2)', 'E806:')
Bram Moolenaar08243d22017-01-10 16:12:29 +0100210endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100211
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200212func Test_pathshorten()
213 call assert_equal('', pathshorten(''))
214 call assert_equal('foo', pathshorten('foo'))
215 call assert_equal('/foo', pathshorten('/foo'))
216 call assert_equal('f/', pathshorten('foo/'))
217 call assert_equal('f/bar', pathshorten('foo/bar'))
218 call assert_equal('f/b/foobar', pathshorten('foo/bar/foobar'))
219 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
220 call assert_equal('.f/bar', pathshorten('.foo/bar'))
221 call assert_equal('~f/bar', pathshorten('~foo/bar'))
222 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
223 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
224 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
225endfunc
226
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100227func Test_strpart()
228 call assert_equal('de', strpart('abcdefg', 3, 2))
229 call assert_equal('ab', strpart('abcdefg', -2, 4))
230 call assert_equal('abcdefg', strpart('abcdefg', -2))
231 call assert_equal('fg', strpart('abcdefg', 5, 4))
232 call assert_equal('defg', strpart('abcdefg', 3))
233
234 if has('multi_byte')
235 call assert_equal('lép', strpart('éléphant', 2, 4))
236 call assert_equal('léphant', strpart('éléphant', 2))
237 endif
238endfunc
239
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100240func Test_tolower()
241 call assert_equal("", tolower(""))
242
243 " Test with all printable ASCII characters.
244 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
245 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
246
247 if !has('multi_byte')
248 return
249 endif
250
251 " Test with a few uppercase diacritics.
252 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
253 call assert_equal("bḃḇ", tolower("BḂḆ"))
254 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
255 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
256 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
257 call assert_equal("f ", tolower("F "))
258 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
259 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
260 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
261 call assert_equal("jĵ", tolower("JĴ"))
262 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
263 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
264 call assert_equal("mḿṁ", tolower("MḾṀ"))
265 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
266 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
267 call assert_equal("pṕṗ", tolower("PṔṖ"))
268 call assert_equal("q", tolower("Q"))
269 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
270 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
271 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
272 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
273 call assert_equal("v", tolower("V"))
274 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
275 call assert_equal("xẋẍ", tolower("XẊẌ"))
276 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
277 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
278
279 " Test with a few lowercase diacritics, which should remain unchanged.
280 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
281 call assert_equal("bḃḇ", tolower("bḃḇ"))
282 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
283 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
284 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
285 call assert_equal("fḟ", tolower("fḟ"))
286 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
287 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
288 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
289 call assert_equal("jĵǰ", tolower("jĵǰ"))
290 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
291 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
292 call assert_equal("mḿṁ ", tolower("mḿṁ "))
293 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
294 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
295 call assert_equal("pṕṗ", tolower("pṕṗ"))
296 call assert_equal("q", tolower("q"))
297 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
298 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
299 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
300 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
301 call assert_equal("vṽ", tolower("vṽ"))
302 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
303 call assert_equal("ẋẍ", tolower("ẋẍ"))
304 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
305 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
306
307 " According to https://twitter.com/jifa/status/625776454479970304
308 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
309 " in length (2 to 3 bytes) when lowercased. So let's test them.
310 call assert_equal(" ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100311
312 " This call to tolower with invalid utf8 sequence used to cause access to
313 " invalid memory.
314 call tolower("\xC0\x80\xC0")
315 call tolower("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100316endfunc
317
318func Test_toupper()
319 call assert_equal("", toupper(""))
320
321 " Test with all printable ASCII characters.
322 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
323 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
324
325 if !has('multi_byte')
326 return
327 endif
328
329 " Test with a few lowercase diacritics.
330 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("aàáâãäåāăąǎǟǡả"))
331 call assert_equal("BḂḆ", toupper("bḃḇ"))
332 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
333 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
334 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
335 call assert_equal("F", toupper("f"))
336 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
337 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
338 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
339 call assert_equal("JĴǰ", toupper("jĵǰ"))
340 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
341 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
342 call assert_equal("MḾṀ ", toupper("mḿṁ "))
343 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
344 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
345 call assert_equal("PṔṖ", toupper("pṕṗ"))
346 call assert_equal("Q", toupper("q"))
347 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
348 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
349 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
350 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
351 call assert_equal("V", toupper("v"))
352 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
353 call assert_equal("ẊẌ", toupper("ẋẍ"))
354 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
355 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
356
357 " Test that uppercase diacritics, which should remain unchanged.
358 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
359 call assert_equal("BḂḆ", toupper("BḂḆ"))
360 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
361 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
362 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
363 call assert_equal("FḞ ", toupper("FḞ "))
364 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
365 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
366 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
367 call assert_equal("JĴ", toupper("JĴ"))
368 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
369 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
370 call assert_equal("MḾṀ", toupper("MḾṀ"))
371 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
372 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
373 call assert_equal("PṔṖ", toupper("PṔṖ"))
374 call assert_equal("Q", toupper("Q"))
375 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
376 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
377 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
378 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
379 call assert_equal("VṼ", toupper("VṼ"))
380 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
381 call assert_equal("XẊẌ", toupper("XẊẌ"))
382 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
383 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
384
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100385 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100386
387 " This call to toupper with invalid utf8 sequence used to cause access to
388 " invalid memory.
389 call toupper("\xC0\x80\xC0")
390 call toupper("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100391endfunc
392
Bram Moolenaare90858d2017-02-01 17:24:34 +0100393" Tests for the mode() function
394let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100395func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100396 let g:current_modes = mode(0) . '-' . mode(1)
397 return ''
398endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100399
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100400func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100401 new
402 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
403
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100404 " Only complete from the current buffer.
405 set complete=.
406
Bram Moolenaare90858d2017-02-01 17:24:34 +0100407 inoremap <F2> <C-R>=Save_mode()<CR>
408
409 normal! 3G
410 exe "normal i\<F2>\<Esc>"
411 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100412 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100413 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
414 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100415 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100416 exe "normal iBro\<C-P>\<F2>\<Esc>u"
417 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100418 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100419 exe "normal iBa\<C-X>\<F2>\<Esc>u"
420 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100421 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100422 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
423 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100424 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100425 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
426 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100427 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100428 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
429 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100430 " i_CTRL-X CTRL-L: Multiple matches
431 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
432 call assert_equal('i-ic', g:current_modes)
433 " i_CTRL-X CTRL-L: Single match
434 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
435 call assert_equal('i-ic', g:current_modes)
436 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100437 exe "normal iCom\<C-P>\<F2>\<Esc>u"
438 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100439 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100440 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
441 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100442 " i_CTRL-X CTRL-L: No match
443 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
444 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100445
Bram Moolenaare971df32017-02-05 14:15:29 +0100446 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100447 exe "normal RBa\<C-P>\<F2>\<Esc>u"
448 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100449 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100450 exe "normal RBro\<C-P>\<F2>\<Esc>u"
451 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100452 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100453 exe "normal RBa\<C-X>\<F2>\<Esc>u"
454 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100455 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100456 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
457 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100458 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100459 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
460 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100461 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100462 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
463 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100464 " R_CTRL-X CTRL-L: Multiple matches
465 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
466 call assert_equal('R-Rc', g:current_modes)
467 " R_CTRL-X CTRL-L: Single match
468 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
469 call assert_equal('R-Rc', g:current_modes)
470 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100471 exe "normal RCom\<C-P>\<F2>\<Esc>u"
472 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100473 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100474 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
475 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100476 " R_CTRL-X CTRL-L: No match
477 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
478 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100479
480 call assert_equal('n', mode(0))
481 call assert_equal('n', mode(1))
482
Bram Moolenaar612cc382018-07-29 15:34:26 +0200483 " i_CTRL-O
484 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
485 call assert_equal("n-niI", g:current_modes)
486
487 " R_CTRL-O
488 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
489 call assert_equal("n-niR", g:current_modes)
490
491 " gR_CTRL-O
492 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
493 call assert_equal("n-niV", g:current_modes)
494
Bram Moolenaare90858d2017-02-01 17:24:34 +0100495 " How to test operator-pending mode?
496
497 call feedkeys("v", 'xt')
498 call assert_equal('v', mode())
499 call assert_equal('v', mode(1))
500 call feedkeys("\<Esc>V", 'xt')
501 call assert_equal('V', mode())
502 call assert_equal('V', mode(1))
503 call feedkeys("\<Esc>\<C-V>", 'xt')
504 call assert_equal("\<C-V>", mode())
505 call assert_equal("\<C-V>", mode(1))
506 call feedkeys("\<Esc>", 'xt')
507
508 call feedkeys("gh", 'xt')
509 call assert_equal('s', mode())
510 call assert_equal('s', mode(1))
511 call feedkeys("\<Esc>gH", 'xt')
512 call assert_equal('S', mode())
513 call assert_equal('S', mode(1))
514 call feedkeys("\<Esc>g\<C-H>", 'xt')
515 call assert_equal("\<C-S>", mode())
516 call assert_equal("\<C-S>", mode(1))
517 call feedkeys("\<Esc>", 'xt')
518
519 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
520 call assert_equal('c-c', g:current_modes)
521 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
522 call assert_equal('c-cv', g:current_modes)
523 " How to test Ex mode?
524
525 bwipe!
526 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100527 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100528endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100529
530func Test_getbufvar()
531 let bnr = bufnr('%')
532 let b:var_num = '1234'
533 let def_num = '5678'
534 call assert_equal('1234', getbufvar(bnr, 'var_num'))
535 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
536
537 let bd = getbufvar(bnr, '')
538 call assert_equal('1234', bd['var_num'])
539 call assert_true(exists("bd['changedtick']"))
540 call assert_equal(2, len(bd))
541
542 let bd2 = getbufvar(bnr, '', def_num)
543 call assert_equal(bd, bd2)
544
545 unlet b:var_num
546 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
547 call assert_equal('', getbufvar(bnr, 'var_num'))
548
549 let bd = getbufvar(bnr, '')
550 call assert_equal(1, len(bd))
551 let bd = getbufvar(bnr, '',def_num)
552 call assert_equal(1, len(bd))
553
Bram Moolenaar4520d442017-03-19 16:09:46 +0100554 call assert_equal('', getbufvar(9999, ''))
555 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100556 unlet def_num
557
Bram Moolenaar507647d2017-02-17 16:43:49 +0100558 call assert_equal(0, getbufvar(bnr, '&autoindent'))
559 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100560
561 " Open new window with forced option values
562 set fileformats=unix,dos
563 new ++ff=dos ++bin ++enc=iso-8859-2
564 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
565 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
566 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
567 close
568
569 set fileformats&
570endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100571
Bram Moolenaar41042f32017-03-09 12:09:32 +0100572func Test_last_buffer_nr()
573 call assert_equal(bufnr('$'), last_buffer_nr())
574endfunc
575
576func Test_stridx()
577 call assert_equal(-1, stridx('', 'l'))
578 call assert_equal(0, stridx('', ''))
579 call assert_equal(0, stridx('hello', ''))
580 call assert_equal(-1, stridx('hello', 'L'))
581 call assert_equal(2, stridx('hello', 'l', -1))
582 call assert_equal(2, stridx('hello', 'l', 0))
583 call assert_equal(2, stridx('hello', 'l', 1))
584 call assert_equal(3, stridx('hello', 'l', 3))
585 call assert_equal(-1, stridx('hello', 'l', 4))
586 call assert_equal(-1, stridx('hello', 'l', 10))
587 call assert_equal(2, stridx('hello', 'll'))
588 call assert_equal(-1, stridx('hello', 'hello world'))
589endfunc
590
591func Test_strridx()
592 call assert_equal(-1, strridx('', 'l'))
593 call assert_equal(0, strridx('', ''))
594 call assert_equal(5, strridx('hello', ''))
595 call assert_equal(-1, strridx('hello', 'L'))
596 call assert_equal(3, strridx('hello', 'l'))
597 call assert_equal(3, strridx('hello', 'l', 10))
598 call assert_equal(3, strridx('hello', 'l', 3))
599 call assert_equal(2, strridx('hello', 'l', 2))
600 call assert_equal(-1, strridx('hello', 'l', 1))
601 call assert_equal(-1, strridx('hello', 'l', 0))
602 call assert_equal(-1, strridx('hello', 'l', -1))
603 call assert_equal(2, strridx('hello', 'll'))
604 call assert_equal(-1, strridx('hello', 'hello world'))
605endfunc
606
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200607func Test_match_func()
608 call assert_equal(4, match('testing', 'ing'))
609 call assert_equal(4, match('testing', 'ing', 2))
610 call assert_equal(-1, match('testing', 'ing', 5))
611 call assert_equal(-1, match('testing', 'ing', 8))
612 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
613 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
614endfunc
615
Bram Moolenaar41042f32017-03-09 12:09:32 +0100616func Test_matchend()
617 call assert_equal(7, matchend('testing', 'ing'))
618 call assert_equal(7, matchend('testing', 'ing', 2))
619 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200620 call assert_equal(-1, matchend('testing', 'ing', 8))
621 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
622 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
623endfunc
624
625func Test_matchlist()
626 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
627 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
628 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
629endfunc
630
631func Test_matchstr()
632 call assert_equal('ing', matchstr('testing', 'ing'))
633 call assert_equal('ing', matchstr('testing', 'ing', 2))
634 call assert_equal('', matchstr('testing', 'ing', 5))
635 call assert_equal('', matchstr('testing', 'ing', 8))
636 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
637 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
638endfunc
639
640func Test_matchstrpos()
641 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
642 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2))
643 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
644 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
645 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
646 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100647endfunc
648
649func Test_nextnonblank_prevnonblank()
650 new
651insert
652This
653
654
655is
656
657a
658Test
659.
660 call assert_equal(0, nextnonblank(-1))
661 call assert_equal(0, nextnonblank(0))
662 call assert_equal(1, nextnonblank(1))
663 call assert_equal(4, nextnonblank(2))
664 call assert_equal(4, nextnonblank(3))
665 call assert_equal(4, nextnonblank(4))
666 call assert_equal(6, nextnonblank(5))
667 call assert_equal(6, nextnonblank(6))
668 call assert_equal(7, nextnonblank(7))
669 call assert_equal(0, nextnonblank(8))
670
671 call assert_equal(0, prevnonblank(-1))
672 call assert_equal(0, prevnonblank(0))
673 call assert_equal(1, prevnonblank(1))
674 call assert_equal(1, prevnonblank(2))
675 call assert_equal(1, prevnonblank(3))
676 call assert_equal(4, prevnonblank(4))
677 call assert_equal(4, prevnonblank(5))
678 call assert_equal(6, prevnonblank(6))
679 call assert_equal(7, prevnonblank(7))
680 call assert_equal(0, prevnonblank(8))
681 bw!
682endfunc
683
684func Test_byte2line_line2byte()
685 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200686 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +0100687 call setline(1, ['a', 'bc', 'd'])
688
689 set fileformat=unix
690 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
691 \ map(range(-1, 8), 'byte2line(v:val)'))
692 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
693 \ map(range(-1, 5), 'line2byte(v:val)'))
694
695 set fileformat=mac
696 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
697 \ map(range(-1, 8), 'byte2line(v:val)'))
698 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
699 \ map(range(-1, 5), 'line2byte(v:val)'))
700
701 set fileformat=dos
702 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
703 \ map(range(-1, 11), 'byte2line(v:val)'))
704 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
705 \ map(range(-1, 5), 'line2byte(v:val)'))
706
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200707 bw!
708 set noendofline nofixendofline
709 normal a-
710 for ff in ["unix", "mac", "dos"]
711 let &fileformat = ff
712 call assert_equal(1, line2byte(1))
713 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
714 endfor
715
716 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +0100717 bw!
718endfunc
719
720func Test_count()
721 let l = ['a', 'a', 'A', 'b']
722 call assert_equal(2, count(l, 'a'))
723 call assert_equal(1, count(l, 'A'))
724 call assert_equal(1, count(l, 'b'))
725 call assert_equal(0, count(l, 'B'))
726
727 call assert_equal(2, count(l, 'a', 0))
728 call assert_equal(1, count(l, 'A', 0))
729 call assert_equal(1, count(l, 'b', 0))
730 call assert_equal(0, count(l, 'B', 0))
731
732 call assert_equal(3, count(l, 'a', 1))
733 call assert_equal(3, count(l, 'A', 1))
734 call assert_equal(1, count(l, 'b', 1))
735 call assert_equal(1, count(l, 'B', 1))
736 call assert_equal(0, count(l, 'c', 1))
737
738 call assert_equal(1, count(l, 'a', 0, 1))
739 call assert_equal(2, count(l, 'a', 1, 1))
740 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
741
742 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
743 call assert_equal(2, count(d, 'a'))
744 call assert_equal(1, count(d, 'A'))
745 call assert_equal(1, count(d, 'b'))
746 call assert_equal(0, count(d, 'B'))
747
748 call assert_equal(2, count(d, 'a', 0))
749 call assert_equal(1, count(d, 'A', 0))
750 call assert_equal(1, count(d, 'b', 0))
751 call assert_equal(0, count(d, 'B', 0))
752
753 call assert_equal(3, count(d, 'a', 1))
754 call assert_equal(3, count(d, 'A', 1))
755 call assert_equal(1, count(d, 'b', 1))
756 call assert_equal(1, count(d, 'B', 1))
757 call assert_equal(0, count(d, 'c', 1))
758
759 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +0200760
761 call assert_equal(0, count("foo", "bar"))
762 call assert_equal(1, count("foo", "oo"))
763 call assert_equal(2, count("foo", "o"))
764 call assert_equal(0, count("foo", "O"))
765 call assert_equal(2, count("foo", "O", 1))
766 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +0100767 call assert_equal(0, count("foo", ""))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100768endfunc
769
770func Test_changenr()
771 new Xchangenr
772 call assert_equal(0, changenr())
773 norm ifoo
774 call assert_equal(1, changenr())
775 set undolevels=10
776 norm Sbar
777 call assert_equal(2, changenr())
778 undo
779 call assert_equal(1, changenr())
780 redo
781 call assert_equal(2, changenr())
782 bw!
783 set undolevels&
784endfunc
785
786func Test_filewritable()
787 new Xfilewritable
788 write!
789 call assert_equal(1, filewritable('Xfilewritable'))
790
791 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
792 call assert_equal(0, filewritable('Xfilewritable'))
793
794 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
795 call assert_equal(1, filewritable('Xfilewritable'))
796
797 call assert_equal(0, filewritable('doesnotexist'))
798
799 call delete('Xfilewritable')
800 bw!
801endfunc
802
Bram Moolenaar82956662018-10-06 15:18:45 +0200803func Test_Executable()
804 if has('win32')
805 call assert_equal(1, executable('notepad'))
806 call assert_equal(1, executable('notepad.exe'))
807 call assert_equal(0, executable('notepad.exe.exe'))
808 call assert_equal(0, executable('shell32.dll'))
809 call assert_equal(0, executable('win.ini'))
810 elseif has('unix')
811 call assert_equal(1, executable('cat'))
812 call assert_equal(0, executable('dog'))
813 endif
814endfunc
815
Bram Moolenaar41042f32017-03-09 12:09:32 +0100816func Test_hostname()
817 let hostname_vim = hostname()
818 if has('unix')
819 let hostname_system = systemlist('uname -n')[0]
820 call assert_equal(hostname_vim, hostname_system)
821 endif
822endfunc
823
824func Test_getpid()
825 " getpid() always returns the same value within a vim instance.
826 call assert_equal(getpid(), getpid())
827 if has('unix')
828 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
829 endif
830endfunc
831
832func Test_hlexists()
833 call assert_equal(0, hlexists('does_not_exist'))
834 call assert_equal(0, hlexists('Number'))
835 call assert_equal(0, highlight_exists('does_not_exist'))
836 call assert_equal(0, highlight_exists('Number'))
837 syntax on
838 call assert_equal(0, hlexists('does_not_exist'))
839 call assert_equal(1, hlexists('Number'))
840 call assert_equal(0, highlight_exists('does_not_exist'))
841 call assert_equal(1, highlight_exists('Number'))
842 syntax off
843endfunc
844
845func Test_col()
846 new
847 call setline(1, 'abcdef')
848 norm gg4|mx6|mY2|
849 call assert_equal(2, col('.'))
850 call assert_equal(7, col('$'))
851 call assert_equal(4, col("'x"))
852 call assert_equal(6, col("'Y"))
853 call assert_equal(2, col([1, 2]))
854 call assert_equal(7, col([1, '$']))
855
856 call assert_equal(0, col(''))
857 call assert_equal(0, col('x'))
858 call assert_equal(0, col([2, '$']))
859 call assert_equal(0, col([1, 100]))
860 call assert_equal(0, col([1]))
861 bw!
862endfunc
863
Bram Moolenaar947b39e2018-07-22 19:36:37 +0200864func Test_inputlist()
865 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
866 call assert_equal(1, c)
867 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>2\<cr>", 'tx')
868 call assert_equal(2, c)
869 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
870 call assert_equal(3, c)
871
872 call assert_fails('call inputlist("")', 'E686:')
873endfunc
874
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100875func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +0100876 if has('balloon_eval')
877 " This won't do anything but must not crash either.
878 call balloon_show('hi!')
879 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100880endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +0100881
882func Test_setbufvar_options()
883 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
884 " window layout.
885 call assert_equal(1, winnr('$'))
886 split dummy_preview
887 resize 2
888 set winfixheight winfixwidth
889 let prev_id = win_getid()
890
891 wincmd j
892 let wh = winheight('.')
893 let dummy_buf = bufnr('dummy_buf1', v:true)
894 call setbufvar(dummy_buf, '&buftype', 'nofile')
895 execute 'belowright vertical split #' . dummy_buf
896 call assert_equal(wh, winheight('.'))
897 let dum1_id = win_getid()
898
899 wincmd h
900 let wh = winheight('.')
901 let dummy_buf = bufnr('dummy_buf2', v:true)
902 call setbufvar(dummy_buf, '&buftype', 'nofile')
903 execute 'belowright vertical split #' . dummy_buf
904 call assert_equal(wh, winheight('.'))
905
906 bwipe!
907 call win_gotoid(prev_id)
908 bwipe!
909 call win_gotoid(dum1_id)
910 bwipe!
911endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +0200912
913func Test_redo_in_nested_functions()
914 nnoremap g. :set opfunc=Operator<CR>g@
915 function Operator( type, ... )
916 let @x = 'XXX'
917 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
918 endfunction
919
920 function! Apply()
921 5,6normal! .
922 endfunction
923
924 new
925 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
926 1normal g.i"
927 call assert_equal('some "XXX" text', getline(1))
928 3,4normal .
929 call assert_equal('some "XXX" text', getline(3))
930 call assert_equal('more "XXX" text', getline(4))
931 call Apply()
932 call assert_equal('some "XXX" text', getline(5))
933 call assert_equal('more "XXX" text', getline(6))
934 bwipe!
935
936 nunmap g.
937 delfunc Operator
938 delfunc Apply
939endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +0200940
941func Test_shellescape()
942 let save_shell = &shell
943 set shell=bash
944 call assert_equal("'text'", shellescape('text'))
945 call assert_equal("'te\"xt'", shellescape('te"xt'))
946 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
947
948 call assert_equal("'te%xt'", shellescape("te%xt"))
949 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
950 call assert_equal("'te#xt'", shellescape("te#xt"))
951 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
952 call assert_equal("'te!xt'", shellescape("te!xt"))
953 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
954
955 call assert_equal("'te\nxt'", shellescape("te\nxt"))
956 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
957 set shell=tcsh
958 call assert_equal("'te\\!xt'", shellescape("te!xt"))
959 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
960 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
961 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
962
963 let &shell = save_shell
964endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100965
966func Test_trim()
967 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
968 call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B"))
969 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
970 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
971 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
972 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
973 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
974 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
975 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
976 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
977 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
978 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
979 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
980 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
981 call assert_equal("", trim("", ""))
982 call assert_equal("a", trim("a", ""))
983 call assert_equal("", trim("", "a"))
984
985 let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '')
986 call assert_equal("x", trim(chars . "x" . chars))
987endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200988
989" Test for reg_recording() and reg_executing()
990func Test_reg_executing_and_recording()
991 let s:reg_stat = ''
992 func s:save_reg_stat()
993 let s:reg_stat = reg_recording() . ':' . reg_executing()
994 return ''
995 endfunc
996
997 new
998 call s:save_reg_stat()
999 call assert_equal(':', s:reg_stat)
1000 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1001 call assert_equal('a:', s:reg_stat)
1002 call feedkeys("@a", 'xt')
1003 call assert_equal(':a', s:reg_stat)
1004 call feedkeys("qb@aq", 'xt')
1005 call assert_equal('b:a', s:reg_stat)
1006 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1007 call assert_equal('":', s:reg_stat)
1008
1009 bwipe!
1010 delfunc s:save_reg_stat
1011 unlet s:reg_stat
1012endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001013
1014func Test_libcall_libcallnr()
1015 if !has('libcall')
1016 return
1017 endif
1018
1019 if has('win32')
1020 let libc = 'msvcrt.dll'
1021 elseif has('mac')
1022 let libc = 'libSystem.B.dylib'
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02001023 elseif system('uname -s') =~ 'SunOS'
1024 " Set the path to libc.so according to the architecture.
1025 let test_bits = system('file ' . GetVimProg())
1026 let test_arch = system('uname -p')
1027 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1028 let libc = '/usr/lib/sparcv9/libc.so'
1029 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1030 let libc = '/usr/lib/amd64/libc.so'
1031 else
1032 let libc = '/usr/lib/libc.so'
1033 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001034 else
1035 " On Unix, libc.so can be in various places.
1036 " Interestingly, using an empty string for the 1st argument of libcall
1037 " allows to call functions from libc which is not documented.
1038 let libc = ''
1039 endif
1040
1041 if has('win32')
1042 call assert_equal($USERPROFILE, libcall(libc, 'getenv', 'USERPROFILE'))
1043 else
1044 call assert_equal($HOME, libcall(libc, 'getenv', 'HOME'))
1045 endif
1046
1047 " If function returns NULL, libcall() should return an empty string.
1048 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1049
1050 " Test libcallnr() with string and integer argument.
1051 call assert_equal(4, libcallnr(libc, 'strlen', 'abcd'))
1052 call assert_equal(char2nr('A'), libcallnr(libc, 'toupper', char2nr('a')))
1053
1054 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1055 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1056
1057 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1058 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1059endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001060
1061sandbox function Fsandbox()
1062 normal ix
1063endfunc
1064
1065func Test_func_sandbox()
1066 sandbox let F = {-> 'hello'}
1067 call assert_equal('hello', F())
1068
1069 sandbox let F = {-> execute("normal ix\<Esc>")}
1070 call assert_fails('call F()', 'E48:')
1071 unlet F
1072
1073 call assert_fails('call Fsandbox()', 'E48:')
1074 delfunc Fsandbox
1075endfunc