blob: f71717f73e0952788086a483e6b3cdfbed642216 [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')))
Bram Moolenaar17aca702019-05-16 22:24:55 +020055 call assert_equal(0, empty(function('Test_empty', [0])))
Bram Moolenaar24c2e482017-01-29 15:45:12 +010056endfunc
57
58func Test_len()
59 call assert_equal(1, len(0))
60 call assert_equal(2, len(12))
61
62 call assert_equal(0, len(''))
63 call assert_equal(2, len('ab'))
64
65 call assert_equal(0, len([]))
66 call assert_equal(2, len([2, 1]))
67
68 call assert_equal(0, len({}))
69 call assert_equal(2, len({'a': 1, 'b': 2}))
70
71 call assert_fails('call len(v:none)', 'E701:')
72 call assert_fails('call len({-> 0})', 'E701:')
73endfunc
74
75func Test_max()
76 call assert_equal(0, max([]))
77 call assert_equal(2, max([2]))
78 call assert_equal(2, max([1, 2]))
79 call assert_equal(2, max([1, 2, v:null]))
80
81 call assert_equal(0, max({}))
82 call assert_equal(2, max({'a':1, 'b':2}))
83
84 call assert_fails('call max(1)', 'E712:')
85 call assert_fails('call max(v:none)', 'E712:')
86endfunc
87
88func Test_min()
89 call assert_equal(0, min([]))
90 call assert_equal(2, min([2]))
91 call assert_equal(1, min([1, 2]))
92 call assert_equal(0, min([1, 2, v:null]))
93
94 call assert_equal(0, min({}))
95 call assert_equal(1, min({'a':1, 'b':2}))
96
97 call assert_fails('call min(1)', 'E712:')
98 call assert_fails('call min(v:none)', 'E712:')
99endfunc
100
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200101func Test_strwidth()
102 for aw in ['single', 'double']
103 exe 'set ambiwidth=' . aw
104 call assert_equal(0, strwidth(''))
105 call assert_equal(1, strwidth("\t"))
106 call assert_equal(3, strwidth('Vim'))
107 call assert_equal(4, strwidth(1234))
108 call assert_equal(5, strwidth(-1234))
109
Bram Moolenaar30276f22019-01-24 17:59:39 +0100110 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'))
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200113
114 call assert_fails('call strwidth({->0})', 'E729:')
115 call assert_fails('call strwidth([])', 'E730:')
116 call assert_fails('call strwidth({})', 'E731:')
117 call assert_fails('call strwidth(1.2)', 'E806:')
118 endfor
119
120 set ambiwidth&
121endfunc
122
Bram Moolenaar08243d22017-01-10 16:12:29 +0100123func Test_str2nr()
124 call assert_equal(0, str2nr(''))
125 call assert_equal(1, str2nr('1'))
126 call assert_equal(1, str2nr(' 1 '))
127
128 call assert_equal(1, str2nr('+1'))
129 call assert_equal(1, str2nr('+ 1'))
130 call assert_equal(1, str2nr(' + 1 '))
131
132 call assert_equal(-1, str2nr('-1'))
133 call assert_equal(-1, str2nr('- 1'))
134 call assert_equal(-1, str2nr(' - 1 '))
135
136 call assert_equal(123456789, str2nr('123456789'))
137 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100138
139 call assert_equal(5, str2nr('101', 2))
140 call assert_equal(5, str2nr('0b101', 2))
141 call assert_equal(5, str2nr('0B101', 2))
142 call assert_equal(-5, str2nr('-101', 2))
143 call assert_equal(-5, str2nr('-0b101', 2))
144 call assert_equal(-5, str2nr('-0B101', 2))
145
146 call assert_equal(65, str2nr('101', 8))
147 call assert_equal(65, str2nr('0101', 8))
148 call assert_equal(-65, str2nr('-101', 8))
149 call assert_equal(-65, str2nr('-0101', 8))
150
151 call assert_equal(11259375, str2nr('abcdef', 16))
152 call assert_equal(11259375, str2nr('ABCDEF', 16))
153 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
154 call assert_equal(11259375, str2nr('0xabcdef', 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
159 call assert_equal(0, str2nr('0x10'))
160 call assert_equal(0, str2nr('0b10'))
161 call assert_equal(1, str2nr('12', 2))
162 call assert_equal(1, str2nr('18', 8))
163 call assert_equal(1, str2nr('1g', 16))
164
165 call assert_equal(0, str2nr(v:null))
166 call assert_equal(0, str2nr(v:none))
167
168 call assert_fails('call str2nr([])', 'E730:')
169 call assert_fails('call str2nr({->2})', 'E729:')
170 call assert_fails('call str2nr(1.2)', 'E806:')
171 call assert_fails('call str2nr(10, [])', 'E474:')
172endfunc
173
174func Test_strftime()
175 if !exists('*strftime')
176 return
177 endif
178 " Format of strftime() depends on system. We assume
179 " that basic formats tested here are available and
180 " identical on all systems which support strftime().
181 "
182 " The 2nd parameter of strftime() is a local time, so the output day
183 " of strftime() can be 17 or 18, depending on timezone.
184 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
185 "
186 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'))
187
188 call assert_fails('call strftime([])', 'E730:')
189 call assert_fails('call strftime("%Y", [])', 'E745:')
Bram Moolenaardb517302019-06-18 22:53:24 +0200190
191 " Check that the time changes after we change the timezone
192 " Save previous timezone value, if any
193 if exists('$TZ')
194 let tz = $TZ
195 endif
196
197 " Force EST and then UTC, save the current hour (24-hour clock) for each
198 let $TZ = 'EST' | let est = strftime('%H')
199 let $TZ = 'UTC' | let utc = strftime('%H')
200
201 " Those hours should be two bytes long, and should not be the same; if they
202 " are, a tzset(3) call may have failed somewhere
203 call assert_equal(strlen(est), 2)
204 call assert_equal(strlen(utc), 2)
205 call assert_notequal(est, utc)
206
207 " If we cached a timezone value, put it back, otherwise clear it
208 if exists('tz')
209 let $TZ = tz
210 else
211 unlet $TZ
212 endif
213
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100214endfunc
215
Bram Moolenaardce1e892019-02-10 23:18:53 +0100216func Test_resolve_unix()
Bram Moolenaar26109902018-10-06 15:43:17 +0200217 if !has('unix')
218 return
219 endif
220
221 " Xlink1 -> Xlink2
222 " Xlink2 -> Xlink3
223 silent !ln -s -f Xlink2 Xlink1
224 silent !ln -s -f Xlink3 Xlink2
225 call assert_equal('Xlink3', resolve('Xlink1'))
226 call assert_equal('./Xlink3', resolve('./Xlink1'))
227 call assert_equal('Xlink3/', resolve('Xlink2/'))
228 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
229 "call assert_equal('Xlink3/', resolve('Xlink1/'))
230 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
231 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
232 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
233
234 " Test resolve() with a symlink cycle.
235 " Xlink1 -> Xlink2
236 " Xlink2 -> Xlink3
237 " Xlink3 -> Xlink1
238 silent !ln -s -f Xlink1 Xlink3
239 call assert_fails('call resolve("Xlink1")', 'E655:')
240 call assert_fails('call resolve("./Xlink1")', 'E655:')
241 call assert_fails('call resolve("Xlink2")', 'E655:')
242 call assert_fails('call resolve("Xlink3")', 'E655:')
243 call delete('Xlink1')
244 call delete('Xlink2')
245 call delete('Xlink3')
246
247 silent !ln -s -f Xdir//Xfile Xlink
248 call assert_equal('Xdir/Xfile', resolve('Xlink'))
249 call delete('Xlink')
250
251 silent !ln -s -f Xlink2/ Xlink1
252 call assert_equal('Xlink2', resolve('Xlink1'))
253 call assert_equal('Xlink2/', resolve('Xlink1/'))
254 call delete('Xlink1')
255
256 silent !ln -s -f ./Xlink2 Xlink1
257 call assert_equal('Xlink2', resolve('Xlink1'))
258 call assert_equal('./Xlink2', resolve('./Xlink1'))
259 call delete('Xlink1')
260endfunc
261
Bram Moolenaardce1e892019-02-10 23:18:53 +0100262func s:normalize_fname(fname)
263 let ret = substitute(a:fname, '\', '/', 'g')
264 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200265 return tolower(ret)
Bram Moolenaardce1e892019-02-10 23:18:53 +0100266endfunc
267
268func Test_resolve_win32()
269 if !has('win32')
270 return
271 endif
272
273 " test for shortcut file
274 if executable('cscript')
275 new Xfile
276 wq
277 call writefile([
278 \ 'Set fs = CreateObject("Scripting.FileSystemObject")',
279 \ 'Set ws = WScript.CreateObject("WScript.Shell")',
280 \ 'Set shortcut = ws.CreateShortcut("Xlink.lnk")',
281 \ 'shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")',
282 \ 'shortcut.Save'
283 \], 'link.vbs')
284 silent !cscript link.vbs
285 call delete('link.vbs')
286 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
287 call delete('Xfile')
288
289 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
290 call delete('Xlink.lnk')
291 else
292 echomsg 'skipped test for shortcut file'
293 endif
294
295 " remove files
296 call delete('Xlink')
297 call delete('Xdir', 'd')
298 call delete('Xfile')
299
300 " test for symbolic link to a file
301 new Xfile
302 wq
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200303 call assert_equal('Xfile', resolve('Xfile'))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100304 silent !mklink Xlink Xfile
305 if !v:shell_error
306 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink')))
307 call delete('Xlink')
308 else
309 echomsg 'skipped test for symbolic link to a file'
310 endif
311 call delete('Xfile')
312
313 " test for junction to a directory
314 call mkdir('Xdir')
315 silent !mklink /J Xlink Xdir
316 if !v:shell_error
317 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
318
319 call delete('Xdir', 'd')
320
321 " test for junction already removed
322 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
323 call delete('Xlink')
324 else
325 echomsg 'skipped test for junction to a directory'
326 call delete('Xdir', 'd')
327 endif
328
329 " test for symbolic link to a directory
330 call mkdir('Xdir')
331 silent !mklink /D Xlink Xdir
332 if !v:shell_error
333 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
334
335 call delete('Xdir', 'd')
336
337 " test for symbolic link already removed
338 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
339 call delete('Xlink')
340 else
341 echomsg 'skipped test for symbolic link to a directory'
342 call delete('Xdir', 'd')
343 endif
344
345 " test for buffer name
346 new Xfile
347 wq
348 silent !mklink Xlink Xfile
349 if !v:shell_error
350 edit Xlink
351 call assert_equal('Xlink', bufname('%'))
352 call delete('Xlink')
353 bw!
354 else
355 echomsg 'skipped test for buffer name'
356 endif
357 call delete('Xfile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200358
359 " test for reparse point
360 call mkdir('Xdir')
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200361 call assert_equal('Xdir', resolve('Xdir'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200362 silent !mklink /D Xdirlink Xdir
363 if !v:shell_error
364 w Xdir/text.txt
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200365 call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200366 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
367 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200368 call delete('Xdirlink')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200369 else
370 echomsg 'skipped test for reparse point'
371 endif
372
373 call delete('Xdir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100374endfunc
375
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100376func Test_simplify()
377 call assert_equal('', simplify(''))
378 call assert_equal('/', simplify('/'))
379 call assert_equal('/', simplify('/.'))
380 call assert_equal('/', simplify('/..'))
381 call assert_equal('/...', simplify('/...'))
382 call assert_equal('./dir/file', simplify('./dir/file'))
383 call assert_equal('./dir/file', simplify('.///dir//file'))
384 call assert_equal('./dir/file', simplify('./dir/./file'))
385 call assert_equal('./file', simplify('./dir/../file'))
386 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
387 call assert_equal('./file', simplify('dir/.././file'))
388
389 call assert_fails('call simplify({->0})', 'E729:')
390 call assert_fails('call simplify([])', 'E730:')
391 call assert_fails('call simplify({})', 'E731:')
392 call assert_fails('call simplify(1.2)', 'E806:')
Bram Moolenaar08243d22017-01-10 16:12:29 +0100393endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100394
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200395func Test_pathshorten()
396 call assert_equal('', pathshorten(''))
397 call assert_equal('foo', pathshorten('foo'))
398 call assert_equal('/foo', pathshorten('/foo'))
399 call assert_equal('f/', pathshorten('foo/'))
400 call assert_equal('f/bar', pathshorten('foo/bar'))
401 call assert_equal('f/b/foobar', pathshorten('foo/bar/foobar'))
402 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
403 call assert_equal('.f/bar', pathshorten('.foo/bar'))
404 call assert_equal('~f/bar', pathshorten('~foo/bar'))
405 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
406 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
407 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
408endfunc
409
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100410func Test_strpart()
411 call assert_equal('de', strpart('abcdefg', 3, 2))
412 call assert_equal('ab', strpart('abcdefg', -2, 4))
413 call assert_equal('abcdefg', strpart('abcdefg', -2))
414 call assert_equal('fg', strpart('abcdefg', 5, 4))
415 call assert_equal('defg', strpart('abcdefg', 3))
416
Bram Moolenaar30276f22019-01-24 17:59:39 +0100417 call assert_equal('lép', strpart('éléphant', 2, 4))
418 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100419endfunc
420
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100421func Test_tolower()
422 call assert_equal("", tolower(""))
423
424 " Test with all printable ASCII characters.
425 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
426 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
427
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100428 " Test with a few uppercase diacritics.
429 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
430 call assert_equal("bḃḇ", tolower("BḂḆ"))
431 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
432 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
433 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
434 call assert_equal("f ", tolower("F "))
435 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
436 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
437 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
438 call assert_equal("jĵ", tolower("JĴ"))
439 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
440 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
441 call assert_equal("mḿṁ", tolower("MḾṀ"))
442 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
443 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
444 call assert_equal("pṕṗ", tolower("PṔṖ"))
445 call assert_equal("q", tolower("Q"))
446 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
447 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
448 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
449 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
450 call assert_equal("v", tolower("V"))
451 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
452 call assert_equal("xẋẍ", tolower("XẊẌ"))
453 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
454 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
455
456 " Test with a few lowercase diacritics, which should remain unchanged.
457 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
458 call assert_equal("bḃḇ", tolower("bḃḇ"))
459 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
460 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
461 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
462 call assert_equal("fḟ", tolower("fḟ"))
463 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
464 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
465 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
466 call assert_equal("jĵǰ", tolower("jĵǰ"))
467 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
468 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
469 call assert_equal("mḿṁ ", tolower("mḿṁ "))
470 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
471 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
472 call assert_equal("pṕṗ", tolower("pṕṗ"))
473 call assert_equal("q", tolower("q"))
474 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
475 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
476 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
477 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
478 call assert_equal("vṽ", tolower("vṽ"))
479 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
480 call assert_equal("ẋẍ", tolower("ẋẍ"))
481 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
482 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
483
484 " According to https://twitter.com/jifa/status/625776454479970304
485 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
486 " in length (2 to 3 bytes) when lowercased. So let's test them.
487 call assert_equal(" ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100488
489 " This call to tolower with invalid utf8 sequence used to cause access to
490 " invalid memory.
491 call tolower("\xC0\x80\xC0")
492 call tolower("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100493endfunc
494
495func Test_toupper()
496 call assert_equal("", toupper(""))
497
498 " Test with all printable ASCII characters.
499 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
500 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
501
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100502 " Test with a few lowercase diacritics.
503 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("aàáâãäåāăąǎǟǡả"))
504 call assert_equal("BḂḆ", toupper("bḃḇ"))
505 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
506 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
507 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
508 call assert_equal("F", toupper("f"))
509 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
510 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
511 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
512 call assert_equal("JĴǰ", toupper("jĵǰ"))
513 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
514 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
515 call assert_equal("MḾṀ ", toupper("mḿṁ "))
516 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
517 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
518 call assert_equal("PṔṖ", toupper("pṕṗ"))
519 call assert_equal("Q", toupper("q"))
520 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
521 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
522 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
523 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
524 call assert_equal("V", toupper("v"))
525 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
526 call assert_equal("ẊẌ", toupper("ẋẍ"))
527 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
528 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
529
530 " Test that uppercase diacritics, which should remain unchanged.
531 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
532 call assert_equal("BḂḆ", toupper("BḂḆ"))
533 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
534 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
535 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
536 call assert_equal("FḞ ", toupper("FḞ "))
537 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
538 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
539 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
540 call assert_equal("JĴ", toupper("JĴ"))
541 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
542 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
543 call assert_equal("MḾṀ", toupper("MḾṀ"))
544 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
545 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
546 call assert_equal("PṔṖ", toupper("PṔṖ"))
547 call assert_equal("Q", toupper("Q"))
548 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
549 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
550 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
551 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
552 call assert_equal("VṼ", toupper("VṼ"))
553 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
554 call assert_equal("XẊẌ", toupper("XẊẌ"))
555 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
556 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
557
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100558 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100559
560 " This call to toupper with invalid utf8 sequence used to cause access to
561 " invalid memory.
562 call toupper("\xC0\x80\xC0")
563 call toupper("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100564endfunc
565
Bram Moolenaare90858d2017-02-01 17:24:34 +0100566" Tests for the mode() function
567let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100568func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100569 let g:current_modes = mode(0) . '-' . mode(1)
570 return ''
571endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100572
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100573func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100574 new
575 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
576
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100577 " Only complete from the current buffer.
578 set complete=.
579
Bram Moolenaare90858d2017-02-01 17:24:34 +0100580 inoremap <F2> <C-R>=Save_mode()<CR>
581
582 normal! 3G
583 exe "normal i\<F2>\<Esc>"
584 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100585 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100586 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
587 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100588 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100589 exe "normal iBro\<C-P>\<F2>\<Esc>u"
590 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100591 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100592 exe "normal iBa\<C-X>\<F2>\<Esc>u"
593 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100594 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100595 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
596 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100597 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100598 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
599 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100600 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100601 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
602 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100603 " i_CTRL-X CTRL-L: Multiple matches
604 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
605 call assert_equal('i-ic', g:current_modes)
606 " i_CTRL-X CTRL-L: Single match
607 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
608 call assert_equal('i-ic', g:current_modes)
609 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100610 exe "normal iCom\<C-P>\<F2>\<Esc>u"
611 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100612 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100613 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
614 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100615 " i_CTRL-X CTRL-L: No match
616 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
617 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100618
Bram Moolenaare971df32017-02-05 14:15:29 +0100619 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100620 exe "normal RBa\<C-P>\<F2>\<Esc>u"
621 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100622 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100623 exe "normal RBro\<C-P>\<F2>\<Esc>u"
624 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100625 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100626 exe "normal RBa\<C-X>\<F2>\<Esc>u"
627 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100628 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100629 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
630 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100631 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100632 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
633 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100634 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100635 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
636 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100637 " R_CTRL-X CTRL-L: Multiple matches
638 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
639 call assert_equal('R-Rc', g:current_modes)
640 " R_CTRL-X CTRL-L: Single match
641 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
642 call assert_equal('R-Rc', g:current_modes)
643 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100644 exe "normal RCom\<C-P>\<F2>\<Esc>u"
645 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100646 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100647 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
648 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100649 " R_CTRL-X CTRL-L: No match
650 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
651 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100652
653 call assert_equal('n', mode(0))
654 call assert_equal('n', mode(1))
655
Bram Moolenaar612cc382018-07-29 15:34:26 +0200656 " i_CTRL-O
657 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
658 call assert_equal("n-niI", g:current_modes)
659
660 " R_CTRL-O
661 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
662 call assert_equal("n-niR", g:current_modes)
663
664 " gR_CTRL-O
665 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
666 call assert_equal("n-niV", g:current_modes)
667
Bram Moolenaare90858d2017-02-01 17:24:34 +0100668 " How to test operator-pending mode?
669
670 call feedkeys("v", 'xt')
671 call assert_equal('v', mode())
672 call assert_equal('v', mode(1))
673 call feedkeys("\<Esc>V", 'xt')
674 call assert_equal('V', mode())
675 call assert_equal('V', mode(1))
676 call feedkeys("\<Esc>\<C-V>", 'xt')
677 call assert_equal("\<C-V>", mode())
678 call assert_equal("\<C-V>", mode(1))
679 call feedkeys("\<Esc>", 'xt')
680
681 call feedkeys("gh", 'xt')
682 call assert_equal('s', mode())
683 call assert_equal('s', mode(1))
684 call feedkeys("\<Esc>gH", 'xt')
685 call assert_equal('S', mode())
686 call assert_equal('S', mode(1))
687 call feedkeys("\<Esc>g\<C-H>", 'xt')
688 call assert_equal("\<C-S>", mode())
689 call assert_equal("\<C-S>", mode(1))
690 call feedkeys("\<Esc>", 'xt')
691
692 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
693 call assert_equal('c-c', g:current_modes)
694 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
695 call assert_equal('c-cv', g:current_modes)
696 " How to test Ex mode?
697
698 bwipe!
699 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100700 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100701endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100702
703func Test_getbufvar()
704 let bnr = bufnr('%')
705 let b:var_num = '1234'
706 let def_num = '5678'
707 call assert_equal('1234', getbufvar(bnr, 'var_num'))
708 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
709
710 let bd = getbufvar(bnr, '')
711 call assert_equal('1234', bd['var_num'])
712 call assert_true(exists("bd['changedtick']"))
713 call assert_equal(2, len(bd))
714
715 let bd2 = getbufvar(bnr, '', def_num)
716 call assert_equal(bd, bd2)
717
718 unlet b:var_num
719 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
720 call assert_equal('', getbufvar(bnr, 'var_num'))
721
722 let bd = getbufvar(bnr, '')
723 call assert_equal(1, len(bd))
724 let bd = getbufvar(bnr, '',def_num)
725 call assert_equal(1, len(bd))
726
Bram Moolenaar4520d442017-03-19 16:09:46 +0100727 call assert_equal('', getbufvar(9999, ''))
728 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100729 unlet def_num
730
Bram Moolenaar507647d2017-02-17 16:43:49 +0100731 call assert_equal(0, getbufvar(bnr, '&autoindent'))
732 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100733
734 " Open new window with forced option values
735 set fileformats=unix,dos
736 new ++ff=dos ++bin ++enc=iso-8859-2
737 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
738 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
739 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
740 close
741
742 set fileformats&
743endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100744
Bram Moolenaar41042f32017-03-09 12:09:32 +0100745func Test_last_buffer_nr()
746 call assert_equal(bufnr('$'), last_buffer_nr())
747endfunc
748
749func Test_stridx()
750 call assert_equal(-1, stridx('', 'l'))
751 call assert_equal(0, stridx('', ''))
752 call assert_equal(0, stridx('hello', ''))
753 call assert_equal(-1, stridx('hello', 'L'))
754 call assert_equal(2, stridx('hello', 'l', -1))
755 call assert_equal(2, stridx('hello', 'l', 0))
756 call assert_equal(2, stridx('hello', 'l', 1))
757 call assert_equal(3, stridx('hello', 'l', 3))
758 call assert_equal(-1, stridx('hello', 'l', 4))
759 call assert_equal(-1, stridx('hello', 'l', 10))
760 call assert_equal(2, stridx('hello', 'll'))
761 call assert_equal(-1, stridx('hello', 'hello world'))
762endfunc
763
764func Test_strridx()
765 call assert_equal(-1, strridx('', 'l'))
766 call assert_equal(0, strridx('', ''))
767 call assert_equal(5, strridx('hello', ''))
768 call assert_equal(-1, strridx('hello', 'L'))
769 call assert_equal(3, strridx('hello', 'l'))
770 call assert_equal(3, strridx('hello', 'l', 10))
771 call assert_equal(3, strridx('hello', 'l', 3))
772 call assert_equal(2, strridx('hello', 'l', 2))
773 call assert_equal(-1, strridx('hello', 'l', 1))
774 call assert_equal(-1, strridx('hello', 'l', 0))
775 call assert_equal(-1, strridx('hello', 'l', -1))
776 call assert_equal(2, strridx('hello', 'll'))
777 call assert_equal(-1, strridx('hello', 'hello world'))
778endfunc
779
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200780func Test_match_func()
781 call assert_equal(4, match('testing', 'ing'))
782 call assert_equal(4, match('testing', 'ing', 2))
783 call assert_equal(-1, match('testing', 'ing', 5))
784 call assert_equal(-1, match('testing', 'ing', 8))
785 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
786 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
787endfunc
788
Bram Moolenaar41042f32017-03-09 12:09:32 +0100789func Test_matchend()
790 call assert_equal(7, matchend('testing', 'ing'))
791 call assert_equal(7, matchend('testing', 'ing', 2))
792 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200793 call assert_equal(-1, matchend('testing', 'ing', 8))
794 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
795 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
796endfunc
797
798func Test_matchlist()
799 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
800 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
801 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
802endfunc
803
804func Test_matchstr()
805 call assert_equal('ing', matchstr('testing', 'ing'))
806 call assert_equal('ing', matchstr('testing', 'ing', 2))
807 call assert_equal('', matchstr('testing', 'ing', 5))
808 call assert_equal('', matchstr('testing', 'ing', 8))
809 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
810 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
811endfunc
812
813func Test_matchstrpos()
814 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
815 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2))
816 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
817 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
818 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
819 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100820endfunc
821
822func Test_nextnonblank_prevnonblank()
823 new
824insert
825This
826
827
828is
829
830a
831Test
832.
833 call assert_equal(0, nextnonblank(-1))
834 call assert_equal(0, nextnonblank(0))
835 call assert_equal(1, nextnonblank(1))
836 call assert_equal(4, nextnonblank(2))
837 call assert_equal(4, nextnonblank(3))
838 call assert_equal(4, nextnonblank(4))
839 call assert_equal(6, nextnonblank(5))
840 call assert_equal(6, nextnonblank(6))
841 call assert_equal(7, nextnonblank(7))
842 call assert_equal(0, nextnonblank(8))
843
844 call assert_equal(0, prevnonblank(-1))
845 call assert_equal(0, prevnonblank(0))
846 call assert_equal(1, prevnonblank(1))
847 call assert_equal(1, prevnonblank(2))
848 call assert_equal(1, prevnonblank(3))
849 call assert_equal(4, prevnonblank(4))
850 call assert_equal(4, prevnonblank(5))
851 call assert_equal(6, prevnonblank(6))
852 call assert_equal(7, prevnonblank(7))
853 call assert_equal(0, prevnonblank(8))
854 bw!
855endfunc
856
857func Test_byte2line_line2byte()
858 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200859 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +0100860 call setline(1, ['a', 'bc', 'd'])
861
862 set fileformat=unix
863 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
864 \ map(range(-1, 8), 'byte2line(v:val)'))
865 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
866 \ map(range(-1, 5), 'line2byte(v:val)'))
867
868 set fileformat=mac
869 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
870 \ map(range(-1, 8), 'byte2line(v:val)'))
871 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
872 \ map(range(-1, 5), 'line2byte(v:val)'))
873
874 set fileformat=dos
875 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
876 \ map(range(-1, 11), 'byte2line(v:val)'))
877 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
878 \ map(range(-1, 5), 'line2byte(v:val)'))
879
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200880 bw!
881 set noendofline nofixendofline
882 normal a-
883 for ff in ["unix", "mac", "dos"]
884 let &fileformat = ff
885 call assert_equal(1, line2byte(1))
886 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
887 endfor
888
889 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +0100890 bw!
891endfunc
892
893func Test_count()
894 let l = ['a', 'a', 'A', 'b']
895 call assert_equal(2, count(l, 'a'))
896 call assert_equal(1, count(l, 'A'))
897 call assert_equal(1, count(l, 'b'))
898 call assert_equal(0, count(l, 'B'))
899
900 call assert_equal(2, count(l, 'a', 0))
901 call assert_equal(1, count(l, 'A', 0))
902 call assert_equal(1, count(l, 'b', 0))
903 call assert_equal(0, count(l, 'B', 0))
904
905 call assert_equal(3, count(l, 'a', 1))
906 call assert_equal(3, count(l, 'A', 1))
907 call assert_equal(1, count(l, 'b', 1))
908 call assert_equal(1, count(l, 'B', 1))
909 call assert_equal(0, count(l, 'c', 1))
910
911 call assert_equal(1, count(l, 'a', 0, 1))
912 call assert_equal(2, count(l, 'a', 1, 1))
913 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +0200914 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100915
916 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
917 call assert_equal(2, count(d, 'a'))
918 call assert_equal(1, count(d, 'A'))
919 call assert_equal(1, count(d, 'b'))
920 call assert_equal(0, count(d, 'B'))
921
922 call assert_equal(2, count(d, 'a', 0))
923 call assert_equal(1, count(d, 'A', 0))
924 call assert_equal(1, count(d, 'b', 0))
925 call assert_equal(0, count(d, 'B', 0))
926
927 call assert_equal(3, count(d, 'a', 1))
928 call assert_equal(3, count(d, 'A', 1))
929 call assert_equal(1, count(d, 'b', 1))
930 call assert_equal(1, count(d, 'B', 1))
931 call assert_equal(0, count(d, 'c', 1))
932
933 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +0200934
935 call assert_equal(0, count("foo", "bar"))
936 call assert_equal(1, count("foo", "oo"))
937 call assert_equal(2, count("foo", "o"))
938 call assert_equal(0, count("foo", "O"))
939 call assert_equal(2, count("foo", "O", 1))
940 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +0100941 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +0200942
943 call assert_fails('call count(0, 0)', 'E712:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100944endfunc
945
946func Test_changenr()
947 new Xchangenr
948 call assert_equal(0, changenr())
949 norm ifoo
950 call assert_equal(1, changenr())
951 set undolevels=10
952 norm Sbar
953 call assert_equal(2, changenr())
954 undo
955 call assert_equal(1, changenr())
956 redo
957 call assert_equal(2, changenr())
958 bw!
959 set undolevels&
960endfunc
961
962func Test_filewritable()
963 new Xfilewritable
964 write!
965 call assert_equal(1, filewritable('Xfilewritable'))
966
967 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
968 call assert_equal(0, filewritable('Xfilewritable'))
969
970 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
971 call assert_equal(1, filewritable('Xfilewritable'))
972
973 call assert_equal(0, filewritable('doesnotexist'))
974
975 call delete('Xfilewritable')
976 bw!
977endfunc
978
Bram Moolenaar82956662018-10-06 15:18:45 +0200979func Test_Executable()
980 if has('win32')
981 call assert_equal(1, executable('notepad'))
982 call assert_equal(1, executable('notepad.exe'))
983 call assert_equal(0, executable('notepad.exe.exe'))
984 call assert_equal(0, executable('shell32.dll'))
985 call assert_equal(0, executable('win.ini'))
986 elseif has('unix')
987 call assert_equal(1, executable('cat'))
Bram Moolenaara05a0d32018-10-07 18:43:05 +0200988 call assert_equal(0, executable('nodogshere'))
Bram Moolenaar82956662018-10-06 15:18:45 +0200989 endif
990endfunc
991
Bram Moolenaar86621892019-03-30 21:51:28 +0100992func Test_executable_longname()
993 if !has('win32')
994 return
995 endif
996
997 let fname = 'X' . repeat('あ', 200) . '.bat'
998 call writefile([], fname)
999 call assert_equal(1, executable(fname))
1000 call delete(fname)
1001endfunc
1002
Bram Moolenaar41042f32017-03-09 12:09:32 +01001003func Test_hostname()
1004 let hostname_vim = hostname()
1005 if has('unix')
1006 let hostname_system = systemlist('uname -n')[0]
1007 call assert_equal(hostname_vim, hostname_system)
1008 endif
1009endfunc
1010
1011func Test_getpid()
1012 " getpid() always returns the same value within a vim instance.
1013 call assert_equal(getpid(), getpid())
1014 if has('unix')
1015 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1016 endif
1017endfunc
1018
1019func Test_hlexists()
1020 call assert_equal(0, hlexists('does_not_exist'))
1021 call assert_equal(0, hlexists('Number'))
1022 call assert_equal(0, highlight_exists('does_not_exist'))
1023 call assert_equal(0, highlight_exists('Number'))
1024 syntax on
1025 call assert_equal(0, hlexists('does_not_exist'))
1026 call assert_equal(1, hlexists('Number'))
1027 call assert_equal(0, highlight_exists('does_not_exist'))
1028 call assert_equal(1, highlight_exists('Number'))
1029 syntax off
1030endfunc
1031
1032func Test_col()
1033 new
1034 call setline(1, 'abcdef')
1035 norm gg4|mx6|mY2|
1036 call assert_equal(2, col('.'))
1037 call assert_equal(7, col('$'))
1038 call assert_equal(4, col("'x"))
1039 call assert_equal(6, col("'Y"))
1040 call assert_equal(2, col([1, 2]))
1041 call assert_equal(7, col([1, '$']))
1042
1043 call assert_equal(0, col(''))
1044 call assert_equal(0, col('x'))
1045 call assert_equal(0, col([2, '$']))
1046 call assert_equal(0, col([1, 100]))
1047 call assert_equal(0, col([1]))
1048 bw!
1049endfunc
1050
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001051func Test_inputlist()
1052 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1053 call assert_equal(1, c)
1054 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>2\<cr>", 'tx')
1055 call assert_equal(2, c)
1056 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1057 call assert_equal(3, c)
1058
1059 call assert_fails('call inputlist("")', 'E686:')
1060endfunc
1061
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001062func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001063 if has('balloon_eval')
1064 " This won't do anything but must not crash either.
1065 call balloon_show('hi!')
1066 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001067endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001068
1069func Test_setbufvar_options()
1070 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1071 " window layout.
1072 call assert_equal(1, winnr('$'))
1073 split dummy_preview
1074 resize 2
1075 set winfixheight winfixwidth
1076 let prev_id = win_getid()
1077
1078 wincmd j
1079 let wh = winheight('.')
1080 let dummy_buf = bufnr('dummy_buf1', v:true)
1081 call setbufvar(dummy_buf, '&buftype', 'nofile')
1082 execute 'belowright vertical split #' . dummy_buf
1083 call assert_equal(wh, winheight('.'))
1084 let dum1_id = win_getid()
1085
1086 wincmd h
1087 let wh = winheight('.')
1088 let dummy_buf = bufnr('dummy_buf2', v:true)
1089 call setbufvar(dummy_buf, '&buftype', 'nofile')
1090 execute 'belowright vertical split #' . dummy_buf
1091 call assert_equal(wh, winheight('.'))
1092
1093 bwipe!
1094 call win_gotoid(prev_id)
1095 bwipe!
1096 call win_gotoid(dum1_id)
1097 bwipe!
1098endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001099
1100func Test_redo_in_nested_functions()
1101 nnoremap g. :set opfunc=Operator<CR>g@
1102 function Operator( type, ... )
1103 let @x = 'XXX'
1104 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1105 endfunction
1106
1107 function! Apply()
1108 5,6normal! .
1109 endfunction
1110
1111 new
1112 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1113 1normal g.i"
1114 call assert_equal('some "XXX" text', getline(1))
1115 3,4normal .
1116 call assert_equal('some "XXX" text', getline(3))
1117 call assert_equal('more "XXX" text', getline(4))
1118 call Apply()
1119 call assert_equal('some "XXX" text', getline(5))
1120 call assert_equal('more "XXX" text', getline(6))
1121 bwipe!
1122
1123 nunmap g.
1124 delfunc Operator
1125 delfunc Apply
1126endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02001127
1128func Test_shellescape()
1129 let save_shell = &shell
1130 set shell=bash
1131 call assert_equal("'text'", shellescape('text'))
1132 call assert_equal("'te\"xt'", shellescape('te"xt'))
1133 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
1134
1135 call assert_equal("'te%xt'", shellescape("te%xt"))
1136 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
1137 call assert_equal("'te#xt'", shellescape("te#xt"))
1138 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
1139 call assert_equal("'te!xt'", shellescape("te!xt"))
1140 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1141
1142 call assert_equal("'te\nxt'", shellescape("te\nxt"))
1143 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1144 set shell=tcsh
1145 call assert_equal("'te\\!xt'", shellescape("te!xt"))
1146 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1147 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1148 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1149
1150 let &shell = save_shell
1151endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001152
1153func Test_trim()
1154 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
1155 call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B"))
1156 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1157 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
1158 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
1159 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
1160 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
1161 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1162 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1163 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
1164 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
1165 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
1166 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
1167 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1168 call assert_equal("", trim("", ""))
1169 call assert_equal("a", trim("a", ""))
1170 call assert_equal("", trim("", "a"))
1171
1172 let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '')
1173 call assert_equal("x", trim(chars . "x" . chars))
1174endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001175
1176" Test for reg_recording() and reg_executing()
1177func Test_reg_executing_and_recording()
1178 let s:reg_stat = ''
1179 func s:save_reg_stat()
1180 let s:reg_stat = reg_recording() . ':' . reg_executing()
1181 return ''
1182 endfunc
1183
1184 new
1185 call s:save_reg_stat()
1186 call assert_equal(':', s:reg_stat)
1187 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1188 call assert_equal('a:', s:reg_stat)
1189 call feedkeys("@a", 'xt')
1190 call assert_equal(':a', s:reg_stat)
1191 call feedkeys("qb@aq", 'xt')
1192 call assert_equal('b:a', s:reg_stat)
1193 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1194 call assert_equal('":', s:reg_stat)
1195
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001196 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001197 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001198 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1199 func TestFunc() abort
1200 normal! ia
1201 endfunc
1202 call feedkeys("@q", 'xt')
1203 call assert_equal(':q', s:reg_stat)
1204 delfunc TestFunc
1205
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001206 " getchar() command saves and restores reg_executing
1207 map W :call TestFunc()<CR>
1208 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001209 let g:typed = ''
1210 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001211 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001212 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001213 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001214 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001215 endfunc
1216 call feedkeys("@qy", 'xt')
1217 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001218 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001219 delfunc TestFunc
1220 unmap W
1221 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001222 unlet g:regs
1223
1224 " input() command saves and restores reg_executing
1225 map W :call TestFunc()<CR>
1226 let @q = "W"
1227 let g:typed = ''
1228 let g:regs = []
1229 func TestFunc() abort
1230 let g:regs += [reg_executing()]
1231 let g:typed = input('?')
1232 let g:regs += [reg_executing()]
1233 endfunc
1234 call feedkeys("@qy\<CR>", 'xt')
1235 call assert_equal("y", g:typed)
1236 call assert_equal(['q', 'q'], g:regs)
1237 delfunc TestFunc
1238 unmap W
1239 unlet g:typed
1240 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001241
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001242 bwipe!
1243 delfunc s:save_reg_stat
1244 unlet s:reg_stat
1245endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001246
1247func Test_libcall_libcallnr()
1248 if !has('libcall')
1249 return
1250 endif
1251
1252 if has('win32')
1253 let libc = 'msvcrt.dll'
1254 elseif has('mac')
1255 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001256 elseif executable('ldd')
1257 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1258 endif
1259 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001260 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001261 if has('linux')
1262 " There is not documented but regarding the 1st argument of glibc's
1263 " dlopen an empty string and nullptr are equivalent, so using an empty
1264 " string for the 1st argument of libcall allows to call functions.
1265 let libc = ''
1266 elseif has('sun')
1267 " Set the path to libc.so according to the architecture.
1268 let test_bits = system('file ' . GetVimProg())
1269 let test_arch = system('uname -p')
1270 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1271 let libc = '/usr/lib/sparcv9/libc.so'
1272 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1273 let libc = '/usr/lib/amd64/libc.so'
1274 else
1275 let libc = '/usr/lib/libc.so'
1276 endif
1277 else
1278 " Unfortunately skip this test until a good way is found.
1279 return
1280 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001281 endif
1282
1283 if has('win32')
1284 call assert_equal($USERPROFILE, libcall(libc, 'getenv', 'USERPROFILE'))
1285 else
1286 call assert_equal($HOME, libcall(libc, 'getenv', 'HOME'))
1287 endif
1288
1289 " If function returns NULL, libcall() should return an empty string.
1290 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1291
1292 " Test libcallnr() with string and integer argument.
1293 call assert_equal(4, libcallnr(libc, 'strlen', 'abcd'))
1294 call assert_equal(char2nr('A'), libcallnr(libc, 'toupper', char2nr('a')))
1295
1296 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1297 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1298
1299 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1300 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1301endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001302
1303sandbox function Fsandbox()
1304 normal ix
1305endfunc
1306
1307func Test_func_sandbox()
1308 sandbox let F = {-> 'hello'}
1309 call assert_equal('hello', F())
1310
1311 sandbox let F = {-> execute("normal ix\<Esc>")}
1312 call assert_fails('call F()', 'E48:')
1313 unlet F
1314
1315 call assert_fails('call Fsandbox()', 'E48:')
1316 delfunc Fsandbox
1317endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001318
1319func EditAnotherFile()
1320 let word = expand('<cword>')
1321 edit Xfuncrange2
1322endfunc
1323
1324func Test_func_range_with_edit()
1325 " Define a function that edits another buffer, then call it with a range that
1326 " is invalid in that buffer.
1327 call writefile(['just one line'], 'Xfuncrange2')
1328 new
1329 call setline(1, range(10))
1330 write Xfuncrange1
1331 call assert_fails('5,8call EditAnotherFile()', 'E16:')
1332
1333 call delete('Xfuncrange1')
1334 call delete('Xfuncrange2')
1335 bwipe!
1336endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001337
1338func Test_func_exists_on_reload()
1339 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1340 call assert_equal(0, exists('*ExistingFunction'))
1341 source Xfuncexists
1342 call assert_equal(1, exists('*ExistingFunction'))
1343 " Redefining a function when reloading a script is OK.
1344 source Xfuncexists
1345 call assert_equal(1, exists('*ExistingFunction'))
1346
1347 " But redefining in another script is not OK.
1348 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1349 call assert_fails('source Xfuncexists2', 'E122:')
1350
1351 delfunc ExistingFunction
1352 call assert_equal(0, exists('*ExistingFunction'))
1353 call writefile([
1354 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1355 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1356 \ ], 'Xfuncexists')
1357 call assert_fails('source Xfuncexists', 'E122:')
1358 call assert_equal(1, exists('*ExistingFunction'))
1359
1360 call delete('Xfuncexists2')
1361 call delete('Xfuncexists')
1362 delfunc ExistingFunction
1363endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01001364
1365" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1366func Test_confirm()
1367 if !has('unix') || has('gui_running')
1368 return
1369 endif
1370
1371 call feedkeys('o', 'L')
1372 let a = confirm('Press O to proceed')
1373 call assert_equal(1, a)
1374
1375 call feedkeys('y', 'L')
1376 let a = confirm('Are you sure?', "&Yes\n&No")
1377 call assert_equal(1, a)
1378
1379 call feedkeys('n', 'L')
1380 let a = confirm('Are you sure?', "&Yes\n&No")
1381 call assert_equal(2, a)
1382
1383 " confirm() should return 0 when pressing CTRL-C.
1384 call feedkeys("\<C-c>", 'L')
1385 let a = confirm('Are you sure?', "&Yes\n&No")
1386 call assert_equal(0, a)
1387
1388 " <Esc> requires another character to avoid it being seen as the start of an
1389 " escape sequence. Zero should be harmless.
1390 call feedkeys("\<Esc>0", 'L')
1391 let a = confirm('Are you sure?', "&Yes\n&No")
1392 call assert_equal(0, a)
1393
1394 " Default choice is returned when pressing <CR>.
1395 call feedkeys("\<CR>", 'L')
1396 let a = confirm('Are you sure?', "&Yes\n&No")
1397 call assert_equal(1, a)
1398
1399 call feedkeys("\<CR>", 'L')
1400 let a = confirm('Are you sure?', "&Yes\n&No", 2)
1401 call assert_equal(2, a)
1402
1403 call feedkeys("\<CR>", 'L')
1404 let a = confirm('Are you sure?', "&Yes\n&No", 0)
1405 call assert_equal(0, a)
1406
1407 " Test with the {type} 4th argument
1408 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
1409 call feedkeys('y', 'L')
1410 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
1411 call assert_equal(1, a)
1412 endfor
1413
1414 call assert_fails('call confirm([])', 'E730:')
1415 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
1416 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
1417 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
1418endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001419
1420func Test_platform_name()
1421 " The system matches at most only one name.
1422 let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
1423 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
1424
1425 " Is Unix?
1426 call assert_equal(has('beos'), has('beos') && has('unix'))
1427 call assert_equal(has('bsd'), has('bsd') && has('unix'))
1428 call assert_equal(has('hpux'), has('hpux') && has('unix'))
1429 call assert_equal(has('linux'), has('linux') && has('unix'))
1430 call assert_equal(has('mac'), has('mac') && has('unix'))
1431 call assert_equal(has('qnx'), has('qnx') && has('unix'))
1432 call assert_equal(has('sun'), has('sun') && has('unix'))
1433 call assert_equal(has('win32'), has('win32') && !has('unix'))
1434 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
1435
1436 if has('unix') && executable('uname')
1437 let uname = system('uname')
1438 call assert_equal(uname =~? 'BeOS', has('beos'))
Bram Moolenaara02e3f62019-02-07 21:27:14 +01001439 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
1440 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001441 call assert_equal(uname =~? 'HP-UX', has('hpux'))
1442 call assert_equal(uname =~? 'Linux', has('linux'))
1443 call assert_equal(uname =~? 'Darwin', has('mac'))
1444 call assert_equal(uname =~? 'QNX', has('qnx'))
1445 call assert_equal(uname =~? 'SunOS', has('sun'))
1446 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
1447 endif
1448endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001449
1450func Test_readdir()
1451 call mkdir('Xdir')
1452 call writefile([], 'Xdir/foo.txt')
1453 call writefile([], 'Xdir/bar.txt')
1454 call mkdir('Xdir/dir')
1455
1456 " All results
1457 let files = readdir('Xdir')
1458 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
1459
1460 " Only results containing "f"
1461 let files = readdir('Xdir', { x -> stridx(x, 'f') !=- 1 })
1462 call assert_equal(['foo.txt'], sort(files))
1463
1464 " Only .txt files
1465 let files = readdir('Xdir', { x -> x =~ '.txt$' })
1466 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1467
1468 " Only .txt files with string
1469 let files = readdir('Xdir', 'v:val =~ ".txt$"')
1470 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1471
1472 " Limit to 1 result.
1473 let l = []
1474 let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
1475 call assert_equal(1, len(files))
1476
1477 call delete('Xdir', 'rf')
1478endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02001479
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001480func Test_delete_rf()
1481 call mkdir('Xdir')
1482 call writefile([], 'Xdir/foo.txt')
1483 call writefile([], 'Xdir/bar.txt')
1484 call mkdir('Xdir/[a-1]') " issue #696
1485 call writefile([], 'Xdir/[a-1]/foo.txt')
1486 call writefile([], 'Xdir/[a-1]/bar.txt')
1487 call assert_true(filereadable('Xdir/foo.txt'))
1488 call assert_true(filereadable('Xdir/[a-1]/foo.txt'))
1489
1490 call assert_equal(0, delete('Xdir', 'rf'))
1491 call assert_false(filereadable('Xdir/foo.txt'))
1492 call assert_false(filereadable('Xdir/[a-1]/foo.txt'))
1493endfunc
1494
Bram Moolenaar17aca702019-05-16 22:24:55 +02001495func Test_call()
1496 call assert_equal(3, call('len', [123]))
1497 call assert_fails("call call('len', 123)", 'E714:')
1498 call assert_equal(0, call('', []))
1499
1500 function Mylen() dict
1501 return len(self.data)
1502 endfunction
1503 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
1504 call assert_fails("call call('Mylen', [], 0)", 'E715:')
1505endfunc
1506
1507func Test_char2nr()
1508 call assert_equal(12354, char2nr('あ', 1))
1509endfunc
1510
1511func Test_eventhandler()
1512 call assert_equal(0, eventhandler())
1513endfunc