blob: cc1ee8b6138c747c09d3bf1de3d365b807b9d55b [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:')
190endfunc
191
Bram Moolenaardce1e892019-02-10 23:18:53 +0100192func Test_resolve_unix()
Bram Moolenaar26109902018-10-06 15:43:17 +0200193 if !has('unix')
194 return
195 endif
196
197 " Xlink1 -> Xlink2
198 " Xlink2 -> Xlink3
199 silent !ln -s -f Xlink2 Xlink1
200 silent !ln -s -f Xlink3 Xlink2
201 call assert_equal('Xlink3', resolve('Xlink1'))
202 call assert_equal('./Xlink3', resolve('./Xlink1'))
203 call assert_equal('Xlink3/', resolve('Xlink2/'))
204 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
205 "call assert_equal('Xlink3/', resolve('Xlink1/'))
206 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
207 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
208 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
209
210 " Test resolve() with a symlink cycle.
211 " Xlink1 -> Xlink2
212 " Xlink2 -> Xlink3
213 " Xlink3 -> Xlink1
214 silent !ln -s -f Xlink1 Xlink3
215 call assert_fails('call resolve("Xlink1")', 'E655:')
216 call assert_fails('call resolve("./Xlink1")', 'E655:')
217 call assert_fails('call resolve("Xlink2")', 'E655:')
218 call assert_fails('call resolve("Xlink3")', 'E655:')
219 call delete('Xlink1')
220 call delete('Xlink2')
221 call delete('Xlink3')
222
223 silent !ln -s -f Xdir//Xfile Xlink
224 call assert_equal('Xdir/Xfile', resolve('Xlink'))
225 call delete('Xlink')
226
227 silent !ln -s -f Xlink2/ Xlink1
228 call assert_equal('Xlink2', resolve('Xlink1'))
229 call assert_equal('Xlink2/', resolve('Xlink1/'))
230 call delete('Xlink1')
231
232 silent !ln -s -f ./Xlink2 Xlink1
233 call assert_equal('Xlink2', resolve('Xlink1'))
234 call assert_equal('./Xlink2', resolve('./Xlink1'))
235 call delete('Xlink1')
236endfunc
237
Bram Moolenaardce1e892019-02-10 23:18:53 +0100238func s:normalize_fname(fname)
239 let ret = substitute(a:fname, '\', '/', 'g')
240 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200241 return tolower(ret)
Bram Moolenaardce1e892019-02-10 23:18:53 +0100242endfunc
243
244func Test_resolve_win32()
245 if !has('win32')
246 return
247 endif
248
249 " test for shortcut file
250 if executable('cscript')
251 new Xfile
252 wq
253 call writefile([
254 \ 'Set fs = CreateObject("Scripting.FileSystemObject")',
255 \ 'Set ws = WScript.CreateObject("WScript.Shell")',
256 \ 'Set shortcut = ws.CreateShortcut("Xlink.lnk")',
257 \ 'shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")',
258 \ 'shortcut.Save'
259 \], 'link.vbs')
260 silent !cscript link.vbs
261 call delete('link.vbs')
262 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
263 call delete('Xfile')
264
265 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
266 call delete('Xlink.lnk')
267 else
268 echomsg 'skipped test for shortcut file'
269 endif
270
271 " remove files
272 call delete('Xlink')
273 call delete('Xdir', 'd')
274 call delete('Xfile')
275
276 " test for symbolic link to a file
277 new Xfile
278 wq
279 silent !mklink Xlink Xfile
280 if !v:shell_error
281 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink')))
282 call delete('Xlink')
283 else
284 echomsg 'skipped test for symbolic link to a file'
285 endif
286 call delete('Xfile')
287
288 " test for junction to a directory
289 call mkdir('Xdir')
290 silent !mklink /J Xlink Xdir
291 if !v:shell_error
292 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
293
294 call delete('Xdir', 'd')
295
296 " test for junction already removed
297 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
298 call delete('Xlink')
299 else
300 echomsg 'skipped test for junction to a directory'
301 call delete('Xdir', 'd')
302 endif
303
304 " test for symbolic link to a directory
305 call mkdir('Xdir')
306 silent !mklink /D Xlink Xdir
307 if !v:shell_error
308 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
309
310 call delete('Xdir', 'd')
311
312 " test for symbolic link already removed
313 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
314 call delete('Xlink')
315 else
316 echomsg 'skipped test for symbolic link to a directory'
317 call delete('Xdir', 'd')
318 endif
319
320 " test for buffer name
321 new Xfile
322 wq
323 silent !mklink Xlink Xfile
324 if !v:shell_error
325 edit Xlink
326 call assert_equal('Xlink', bufname('%'))
327 call delete('Xlink')
328 bw!
329 else
330 echomsg 'skipped test for buffer name'
331 endif
332 call delete('Xfile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200333
334 " test for reparse point
335 call mkdir('Xdir')
336 silent !mklink /D Xdirlink Xdir
337 if !v:shell_error
338 w Xdir/text.txt
339 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
340 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
341 else
342 echomsg 'skipped test for reparse point'
343 endif
344
345 call delete('Xdir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100346endfunc
347
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100348func Test_simplify()
349 call assert_equal('', simplify(''))
350 call assert_equal('/', simplify('/'))
351 call assert_equal('/', simplify('/.'))
352 call assert_equal('/', simplify('/..'))
353 call assert_equal('/...', simplify('/...'))
354 call assert_equal('./dir/file', simplify('./dir/file'))
355 call assert_equal('./dir/file', simplify('.///dir//file'))
356 call assert_equal('./dir/file', simplify('./dir/./file'))
357 call assert_equal('./file', simplify('./dir/../file'))
358 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
359 call assert_equal('./file', simplify('dir/.././file'))
360
361 call assert_fails('call simplify({->0})', 'E729:')
362 call assert_fails('call simplify([])', 'E730:')
363 call assert_fails('call simplify({})', 'E731:')
364 call assert_fails('call simplify(1.2)', 'E806:')
Bram Moolenaar08243d22017-01-10 16:12:29 +0100365endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100366
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200367func Test_pathshorten()
368 call assert_equal('', pathshorten(''))
369 call assert_equal('foo', pathshorten('foo'))
370 call assert_equal('/foo', pathshorten('/foo'))
371 call assert_equal('f/', pathshorten('foo/'))
372 call assert_equal('f/bar', pathshorten('foo/bar'))
373 call assert_equal('f/b/foobar', pathshorten('foo/bar/foobar'))
374 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
375 call assert_equal('.f/bar', pathshorten('.foo/bar'))
376 call assert_equal('~f/bar', pathshorten('~foo/bar'))
377 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
378 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
379 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
380endfunc
381
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100382func Test_strpart()
383 call assert_equal('de', strpart('abcdefg', 3, 2))
384 call assert_equal('ab', strpart('abcdefg', -2, 4))
385 call assert_equal('abcdefg', strpart('abcdefg', -2))
386 call assert_equal('fg', strpart('abcdefg', 5, 4))
387 call assert_equal('defg', strpart('abcdefg', 3))
388
Bram Moolenaar30276f22019-01-24 17:59:39 +0100389 call assert_equal('lép', strpart('éléphant', 2, 4))
390 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100391endfunc
392
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100393func Test_tolower()
394 call assert_equal("", tolower(""))
395
396 " Test with all printable ASCII characters.
397 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
398 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
399
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100400 " Test with a few uppercase diacritics.
401 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
402 call assert_equal("bḃḇ", tolower("BḂḆ"))
403 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
404 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
405 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
406 call assert_equal("f ", tolower("F "))
407 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
408 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
409 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
410 call assert_equal("jĵ", tolower("JĴ"))
411 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
412 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
413 call assert_equal("mḿṁ", tolower("MḾṀ"))
414 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
415 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
416 call assert_equal("pṕṗ", tolower("PṔṖ"))
417 call assert_equal("q", tolower("Q"))
418 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
419 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
420 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
421 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
422 call assert_equal("v", tolower("V"))
423 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
424 call assert_equal("xẋẍ", tolower("XẊẌ"))
425 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
426 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
427
428 " Test with a few lowercase diacritics, which should remain unchanged.
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ìíîïĩīĭįǐỉ", 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ñńņňʼnṅṉ", tolower("nñńņňʼ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("ẋẍ", tolower("ẋẍ"))
453 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
454 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
455
456 " According to https://twitter.com/jifa/status/625776454479970304
457 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
458 " in length (2 to 3 bytes) when lowercased. So let's test them.
459 call assert_equal(" ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100460
461 " This call to tolower with invalid utf8 sequence used to cause access to
462 " invalid memory.
463 call tolower("\xC0\x80\xC0")
464 call tolower("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100465endfunc
466
467func Test_toupper()
468 call assert_equal("", toupper(""))
469
470 " Test with all printable ASCII characters.
471 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
472 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
473
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100474 " Test with a few lowercase diacritics.
475 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("aàáâãäåāăąǎǟǡả"))
476 call assert_equal("BḂḆ", toupper("bḃḇ"))
477 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
478 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
479 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
480 call assert_equal("F", toupper("f"))
481 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
482 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
483 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
484 call assert_equal("JĴǰ", toupper("jĵǰ"))
485 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
486 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
487 call assert_equal("MḾṀ ", toupper("mḿṁ "))
488 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
489 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
490 call assert_equal("PṔṖ", toupper("pṕṗ"))
491 call assert_equal("Q", toupper("q"))
492 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
493 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
494 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
495 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
496 call assert_equal("V", toupper("v"))
497 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
498 call assert_equal("ẊẌ", toupper("ẋẍ"))
499 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
500 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
501
502 " Test that uppercase diacritics, which should remain unchanged.
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ÑŃŅŇṄṈ", toupper("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("XẊẌ", toupper("XẊẌ"))
527 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
528 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
529
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100530 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100531
532 " This call to toupper with invalid utf8 sequence used to cause access to
533 " invalid memory.
534 call toupper("\xC0\x80\xC0")
535 call toupper("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100536endfunc
537
Bram Moolenaare90858d2017-02-01 17:24:34 +0100538" Tests for the mode() function
539let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100540func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100541 let g:current_modes = mode(0) . '-' . mode(1)
542 return ''
543endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100544
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100545func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100546 new
547 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
548
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100549 " Only complete from the current buffer.
550 set complete=.
551
Bram Moolenaare90858d2017-02-01 17:24:34 +0100552 inoremap <F2> <C-R>=Save_mode()<CR>
553
554 normal! 3G
555 exe "normal i\<F2>\<Esc>"
556 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100557 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100558 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
559 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100560 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100561 exe "normal iBro\<C-P>\<F2>\<Esc>u"
562 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100563 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100564 exe "normal iBa\<C-X>\<F2>\<Esc>u"
565 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100566 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100567 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
568 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100569 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100570 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
571 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100572 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100573 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
574 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100575 " i_CTRL-X CTRL-L: Multiple matches
576 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
577 call assert_equal('i-ic', g:current_modes)
578 " i_CTRL-X CTRL-L: Single match
579 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
580 call assert_equal('i-ic', g:current_modes)
581 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100582 exe "normal iCom\<C-P>\<F2>\<Esc>u"
583 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100584 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100585 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
586 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100587 " i_CTRL-X CTRL-L: No match
588 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
589 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100590
Bram Moolenaare971df32017-02-05 14:15:29 +0100591 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100592 exe "normal RBa\<C-P>\<F2>\<Esc>u"
593 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100594 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100595 exe "normal RBro\<C-P>\<F2>\<Esc>u"
596 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100597 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100598 exe "normal RBa\<C-X>\<F2>\<Esc>u"
599 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100600 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100601 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
602 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100603 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100604 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
605 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100606 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100607 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
608 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100609 " R_CTRL-X CTRL-L: Multiple matches
610 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
611 call assert_equal('R-Rc', g:current_modes)
612 " R_CTRL-X CTRL-L: Single match
613 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
614 call assert_equal('R-Rc', g:current_modes)
615 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100616 exe "normal RCom\<C-P>\<F2>\<Esc>u"
617 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100618 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100619 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
620 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100621 " R_CTRL-X CTRL-L: No match
622 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
623 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100624
625 call assert_equal('n', mode(0))
626 call assert_equal('n', mode(1))
627
Bram Moolenaar612cc382018-07-29 15:34:26 +0200628 " i_CTRL-O
629 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
630 call assert_equal("n-niI", g:current_modes)
631
632 " R_CTRL-O
633 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
634 call assert_equal("n-niR", g:current_modes)
635
636 " gR_CTRL-O
637 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
638 call assert_equal("n-niV", g:current_modes)
639
Bram Moolenaare90858d2017-02-01 17:24:34 +0100640 " How to test operator-pending mode?
641
642 call feedkeys("v", 'xt')
643 call assert_equal('v', mode())
644 call assert_equal('v', mode(1))
645 call feedkeys("\<Esc>V", 'xt')
646 call assert_equal('V', mode())
647 call assert_equal('V', mode(1))
648 call feedkeys("\<Esc>\<C-V>", 'xt')
649 call assert_equal("\<C-V>", mode())
650 call assert_equal("\<C-V>", mode(1))
651 call feedkeys("\<Esc>", 'xt')
652
653 call feedkeys("gh", 'xt')
654 call assert_equal('s', mode())
655 call assert_equal('s', mode(1))
656 call feedkeys("\<Esc>gH", 'xt')
657 call assert_equal('S', mode())
658 call assert_equal('S', mode(1))
659 call feedkeys("\<Esc>g\<C-H>", 'xt')
660 call assert_equal("\<C-S>", mode())
661 call assert_equal("\<C-S>", mode(1))
662 call feedkeys("\<Esc>", 'xt')
663
664 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
665 call assert_equal('c-c', g:current_modes)
666 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
667 call assert_equal('c-cv', g:current_modes)
668 " How to test Ex mode?
669
670 bwipe!
671 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100672 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100673endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100674
675func Test_getbufvar()
676 let bnr = bufnr('%')
677 let b:var_num = '1234'
678 let def_num = '5678'
679 call assert_equal('1234', getbufvar(bnr, 'var_num'))
680 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
681
682 let bd = getbufvar(bnr, '')
683 call assert_equal('1234', bd['var_num'])
684 call assert_true(exists("bd['changedtick']"))
685 call assert_equal(2, len(bd))
686
687 let bd2 = getbufvar(bnr, '', def_num)
688 call assert_equal(bd, bd2)
689
690 unlet b:var_num
691 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
692 call assert_equal('', getbufvar(bnr, 'var_num'))
693
694 let bd = getbufvar(bnr, '')
695 call assert_equal(1, len(bd))
696 let bd = getbufvar(bnr, '',def_num)
697 call assert_equal(1, len(bd))
698
Bram Moolenaar4520d442017-03-19 16:09:46 +0100699 call assert_equal('', getbufvar(9999, ''))
700 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100701 unlet def_num
702
Bram Moolenaar507647d2017-02-17 16:43:49 +0100703 call assert_equal(0, getbufvar(bnr, '&autoindent'))
704 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100705
706 " Open new window with forced option values
707 set fileformats=unix,dos
708 new ++ff=dos ++bin ++enc=iso-8859-2
709 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
710 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
711 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
712 close
713
714 set fileformats&
715endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100716
Bram Moolenaar41042f32017-03-09 12:09:32 +0100717func Test_last_buffer_nr()
718 call assert_equal(bufnr('$'), last_buffer_nr())
719endfunc
720
721func Test_stridx()
722 call assert_equal(-1, stridx('', 'l'))
723 call assert_equal(0, stridx('', ''))
724 call assert_equal(0, stridx('hello', ''))
725 call assert_equal(-1, stridx('hello', 'L'))
726 call assert_equal(2, stridx('hello', 'l', -1))
727 call assert_equal(2, stridx('hello', 'l', 0))
728 call assert_equal(2, stridx('hello', 'l', 1))
729 call assert_equal(3, stridx('hello', 'l', 3))
730 call assert_equal(-1, stridx('hello', 'l', 4))
731 call assert_equal(-1, stridx('hello', 'l', 10))
732 call assert_equal(2, stridx('hello', 'll'))
733 call assert_equal(-1, stridx('hello', 'hello world'))
734endfunc
735
736func Test_strridx()
737 call assert_equal(-1, strridx('', 'l'))
738 call assert_equal(0, strridx('', ''))
739 call assert_equal(5, strridx('hello', ''))
740 call assert_equal(-1, strridx('hello', 'L'))
741 call assert_equal(3, strridx('hello', 'l'))
742 call assert_equal(3, strridx('hello', 'l', 10))
743 call assert_equal(3, strridx('hello', 'l', 3))
744 call assert_equal(2, strridx('hello', 'l', 2))
745 call assert_equal(-1, strridx('hello', 'l', 1))
746 call assert_equal(-1, strridx('hello', 'l', 0))
747 call assert_equal(-1, strridx('hello', 'l', -1))
748 call assert_equal(2, strridx('hello', 'll'))
749 call assert_equal(-1, strridx('hello', 'hello world'))
750endfunc
751
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200752func Test_match_func()
753 call assert_equal(4, match('testing', 'ing'))
754 call assert_equal(4, match('testing', 'ing', 2))
755 call assert_equal(-1, match('testing', 'ing', 5))
756 call assert_equal(-1, match('testing', 'ing', 8))
757 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
758 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
759endfunc
760
Bram Moolenaar41042f32017-03-09 12:09:32 +0100761func Test_matchend()
762 call assert_equal(7, matchend('testing', 'ing'))
763 call assert_equal(7, matchend('testing', 'ing', 2))
764 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200765 call assert_equal(-1, matchend('testing', 'ing', 8))
766 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
767 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
768endfunc
769
770func Test_matchlist()
771 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
772 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
773 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
774endfunc
775
776func Test_matchstr()
777 call assert_equal('ing', matchstr('testing', 'ing'))
778 call assert_equal('ing', matchstr('testing', 'ing', 2))
779 call assert_equal('', matchstr('testing', 'ing', 5))
780 call assert_equal('', matchstr('testing', 'ing', 8))
781 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
782 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
783endfunc
784
785func Test_matchstrpos()
786 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
787 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2))
788 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
789 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
790 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
791 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100792endfunc
793
794func Test_nextnonblank_prevnonblank()
795 new
796insert
797This
798
799
800is
801
802a
803Test
804.
805 call assert_equal(0, nextnonblank(-1))
806 call assert_equal(0, nextnonblank(0))
807 call assert_equal(1, nextnonblank(1))
808 call assert_equal(4, nextnonblank(2))
809 call assert_equal(4, nextnonblank(3))
810 call assert_equal(4, nextnonblank(4))
811 call assert_equal(6, nextnonblank(5))
812 call assert_equal(6, nextnonblank(6))
813 call assert_equal(7, nextnonblank(7))
814 call assert_equal(0, nextnonblank(8))
815
816 call assert_equal(0, prevnonblank(-1))
817 call assert_equal(0, prevnonblank(0))
818 call assert_equal(1, prevnonblank(1))
819 call assert_equal(1, prevnonblank(2))
820 call assert_equal(1, prevnonblank(3))
821 call assert_equal(4, prevnonblank(4))
822 call assert_equal(4, prevnonblank(5))
823 call assert_equal(6, prevnonblank(6))
824 call assert_equal(7, prevnonblank(7))
825 call assert_equal(0, prevnonblank(8))
826 bw!
827endfunc
828
829func Test_byte2line_line2byte()
830 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200831 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +0100832 call setline(1, ['a', 'bc', 'd'])
833
834 set fileformat=unix
835 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
836 \ map(range(-1, 8), 'byte2line(v:val)'))
837 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
838 \ map(range(-1, 5), 'line2byte(v:val)'))
839
840 set fileformat=mac
841 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
842 \ map(range(-1, 8), 'byte2line(v:val)'))
843 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
844 \ map(range(-1, 5), 'line2byte(v:val)'))
845
846 set fileformat=dos
847 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
848 \ map(range(-1, 11), 'byte2line(v:val)'))
849 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
850 \ map(range(-1, 5), 'line2byte(v:val)'))
851
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200852 bw!
853 set noendofline nofixendofline
854 normal a-
855 for ff in ["unix", "mac", "dos"]
856 let &fileformat = ff
857 call assert_equal(1, line2byte(1))
858 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
859 endfor
860
861 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +0100862 bw!
863endfunc
864
865func Test_count()
866 let l = ['a', 'a', 'A', 'b']
867 call assert_equal(2, count(l, 'a'))
868 call assert_equal(1, count(l, 'A'))
869 call assert_equal(1, count(l, 'b'))
870 call assert_equal(0, count(l, 'B'))
871
872 call assert_equal(2, count(l, 'a', 0))
873 call assert_equal(1, count(l, 'A', 0))
874 call assert_equal(1, count(l, 'b', 0))
875 call assert_equal(0, count(l, 'B', 0))
876
877 call assert_equal(3, count(l, 'a', 1))
878 call assert_equal(3, count(l, 'A', 1))
879 call assert_equal(1, count(l, 'b', 1))
880 call assert_equal(1, count(l, 'B', 1))
881 call assert_equal(0, count(l, 'c', 1))
882
883 call assert_equal(1, count(l, 'a', 0, 1))
884 call assert_equal(2, count(l, 'a', 1, 1))
885 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +0200886 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100887
888 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
889 call assert_equal(2, count(d, 'a'))
890 call assert_equal(1, count(d, 'A'))
891 call assert_equal(1, count(d, 'b'))
892 call assert_equal(0, count(d, 'B'))
893
894 call assert_equal(2, count(d, 'a', 0))
895 call assert_equal(1, count(d, 'A', 0))
896 call assert_equal(1, count(d, 'b', 0))
897 call assert_equal(0, count(d, 'B', 0))
898
899 call assert_equal(3, count(d, 'a', 1))
900 call assert_equal(3, count(d, 'A', 1))
901 call assert_equal(1, count(d, 'b', 1))
902 call assert_equal(1, count(d, 'B', 1))
903 call assert_equal(0, count(d, 'c', 1))
904
905 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +0200906
907 call assert_equal(0, count("foo", "bar"))
908 call assert_equal(1, count("foo", "oo"))
909 call assert_equal(2, count("foo", "o"))
910 call assert_equal(0, count("foo", "O"))
911 call assert_equal(2, count("foo", "O", 1))
912 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +0100913 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +0200914
915 call assert_fails('call count(0, 0)', 'E712:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100916endfunc
917
918func Test_changenr()
919 new Xchangenr
920 call assert_equal(0, changenr())
921 norm ifoo
922 call assert_equal(1, changenr())
923 set undolevels=10
924 norm Sbar
925 call assert_equal(2, changenr())
926 undo
927 call assert_equal(1, changenr())
928 redo
929 call assert_equal(2, changenr())
930 bw!
931 set undolevels&
932endfunc
933
934func Test_filewritable()
935 new Xfilewritable
936 write!
937 call assert_equal(1, filewritable('Xfilewritable'))
938
939 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
940 call assert_equal(0, filewritable('Xfilewritable'))
941
942 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
943 call assert_equal(1, filewritable('Xfilewritable'))
944
945 call assert_equal(0, filewritable('doesnotexist'))
946
947 call delete('Xfilewritable')
948 bw!
949endfunc
950
Bram Moolenaar82956662018-10-06 15:18:45 +0200951func Test_Executable()
952 if has('win32')
953 call assert_equal(1, executable('notepad'))
954 call assert_equal(1, executable('notepad.exe'))
955 call assert_equal(0, executable('notepad.exe.exe'))
956 call assert_equal(0, executable('shell32.dll'))
957 call assert_equal(0, executable('win.ini'))
958 elseif has('unix')
959 call assert_equal(1, executable('cat'))
Bram Moolenaara05a0d32018-10-07 18:43:05 +0200960 call assert_equal(0, executable('nodogshere'))
Bram Moolenaar82956662018-10-06 15:18:45 +0200961 endif
962endfunc
963
Bram Moolenaar86621892019-03-30 21:51:28 +0100964func Test_executable_longname()
965 if !has('win32')
966 return
967 endif
968
969 let fname = 'X' . repeat('あ', 200) . '.bat'
970 call writefile([], fname)
971 call assert_equal(1, executable(fname))
972 call delete(fname)
973endfunc
974
Bram Moolenaar41042f32017-03-09 12:09:32 +0100975func Test_hostname()
976 let hostname_vim = hostname()
977 if has('unix')
978 let hostname_system = systemlist('uname -n')[0]
979 call assert_equal(hostname_vim, hostname_system)
980 endif
981endfunc
982
983func Test_getpid()
984 " getpid() always returns the same value within a vim instance.
985 call assert_equal(getpid(), getpid())
986 if has('unix')
987 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
988 endif
989endfunc
990
991func Test_hlexists()
992 call assert_equal(0, hlexists('does_not_exist'))
993 call assert_equal(0, hlexists('Number'))
994 call assert_equal(0, highlight_exists('does_not_exist'))
995 call assert_equal(0, highlight_exists('Number'))
996 syntax on
997 call assert_equal(0, hlexists('does_not_exist'))
998 call assert_equal(1, hlexists('Number'))
999 call assert_equal(0, highlight_exists('does_not_exist'))
1000 call assert_equal(1, highlight_exists('Number'))
1001 syntax off
1002endfunc
1003
1004func Test_col()
1005 new
1006 call setline(1, 'abcdef')
1007 norm gg4|mx6|mY2|
1008 call assert_equal(2, col('.'))
1009 call assert_equal(7, col('$'))
1010 call assert_equal(4, col("'x"))
1011 call assert_equal(6, col("'Y"))
1012 call assert_equal(2, col([1, 2]))
1013 call assert_equal(7, col([1, '$']))
1014
1015 call assert_equal(0, col(''))
1016 call assert_equal(0, col('x'))
1017 call assert_equal(0, col([2, '$']))
1018 call assert_equal(0, col([1, 100]))
1019 call assert_equal(0, col([1]))
1020 bw!
1021endfunc
1022
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001023func Test_inputlist()
1024 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1025 call assert_equal(1, c)
1026 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>2\<cr>", 'tx')
1027 call assert_equal(2, c)
1028 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1029 call assert_equal(3, c)
1030
1031 call assert_fails('call inputlist("")', 'E686:')
1032endfunc
1033
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001034func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001035 if has('balloon_eval')
1036 " This won't do anything but must not crash either.
1037 call balloon_show('hi!')
1038 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001039endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001040
1041func Test_setbufvar_options()
1042 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1043 " window layout.
1044 call assert_equal(1, winnr('$'))
1045 split dummy_preview
1046 resize 2
1047 set winfixheight winfixwidth
1048 let prev_id = win_getid()
1049
1050 wincmd j
1051 let wh = winheight('.')
1052 let dummy_buf = bufnr('dummy_buf1', v:true)
1053 call setbufvar(dummy_buf, '&buftype', 'nofile')
1054 execute 'belowright vertical split #' . dummy_buf
1055 call assert_equal(wh, winheight('.'))
1056 let dum1_id = win_getid()
1057
1058 wincmd h
1059 let wh = winheight('.')
1060 let dummy_buf = bufnr('dummy_buf2', v:true)
1061 call setbufvar(dummy_buf, '&buftype', 'nofile')
1062 execute 'belowright vertical split #' . dummy_buf
1063 call assert_equal(wh, winheight('.'))
1064
1065 bwipe!
1066 call win_gotoid(prev_id)
1067 bwipe!
1068 call win_gotoid(dum1_id)
1069 bwipe!
1070endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001071
1072func Test_redo_in_nested_functions()
1073 nnoremap g. :set opfunc=Operator<CR>g@
1074 function Operator( type, ... )
1075 let @x = 'XXX'
1076 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1077 endfunction
1078
1079 function! Apply()
1080 5,6normal! .
1081 endfunction
1082
1083 new
1084 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1085 1normal g.i"
1086 call assert_equal('some "XXX" text', getline(1))
1087 3,4normal .
1088 call assert_equal('some "XXX" text', getline(3))
1089 call assert_equal('more "XXX" text', getline(4))
1090 call Apply()
1091 call assert_equal('some "XXX" text', getline(5))
1092 call assert_equal('more "XXX" text', getline(6))
1093 bwipe!
1094
1095 nunmap g.
1096 delfunc Operator
1097 delfunc Apply
1098endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02001099
1100func Test_shellescape()
1101 let save_shell = &shell
1102 set shell=bash
1103 call assert_equal("'text'", shellescape('text'))
1104 call assert_equal("'te\"xt'", shellescape('te"xt'))
1105 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
1106
1107 call assert_equal("'te%xt'", shellescape("te%xt"))
1108 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
1109 call assert_equal("'te#xt'", shellescape("te#xt"))
1110 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
1111 call assert_equal("'te!xt'", shellescape("te!xt"))
1112 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1113
1114 call assert_equal("'te\nxt'", shellescape("te\nxt"))
1115 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1116 set shell=tcsh
1117 call assert_equal("'te\\!xt'", shellescape("te!xt"))
1118 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1119 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1120 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1121
1122 let &shell = save_shell
1123endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001124
1125func Test_trim()
1126 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
1127 call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B"))
1128 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1129 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
1130 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
1131 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
1132 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
1133 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1134 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1135 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
1136 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
1137 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
1138 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
1139 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1140 call assert_equal("", trim("", ""))
1141 call assert_equal("a", trim("a", ""))
1142 call assert_equal("", trim("", "a"))
1143
1144 let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '')
1145 call assert_equal("x", trim(chars . "x" . chars))
1146endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001147
1148" Test for reg_recording() and reg_executing()
1149func Test_reg_executing_and_recording()
1150 let s:reg_stat = ''
1151 func s:save_reg_stat()
1152 let s:reg_stat = reg_recording() . ':' . reg_executing()
1153 return ''
1154 endfunc
1155
1156 new
1157 call s:save_reg_stat()
1158 call assert_equal(':', s:reg_stat)
1159 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1160 call assert_equal('a:', s:reg_stat)
1161 call feedkeys("@a", 'xt')
1162 call assert_equal(':a', s:reg_stat)
1163 call feedkeys("qb@aq", 'xt')
1164 call assert_equal('b:a', s:reg_stat)
1165 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1166 call assert_equal('":', s:reg_stat)
1167
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001168 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001169 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001170 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1171 func TestFunc() abort
1172 normal! ia
1173 endfunc
1174 call feedkeys("@q", 'xt')
1175 call assert_equal(':q', s:reg_stat)
1176 delfunc TestFunc
1177
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001178 " getchar() command saves and restores reg_executing
1179 map W :call TestFunc()<CR>
1180 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001181 let g:typed = ''
1182 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001183 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001184 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001185 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001186 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001187 endfunc
1188 call feedkeys("@qy", 'xt')
1189 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001190 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001191 delfunc TestFunc
1192 unmap W
1193 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001194 unlet g:regs
1195
1196 " input() command saves and restores reg_executing
1197 map W :call TestFunc()<CR>
1198 let @q = "W"
1199 let g:typed = ''
1200 let g:regs = []
1201 func TestFunc() abort
1202 let g:regs += [reg_executing()]
1203 let g:typed = input('?')
1204 let g:regs += [reg_executing()]
1205 endfunc
1206 call feedkeys("@qy\<CR>", 'xt')
1207 call assert_equal("y", g:typed)
1208 call assert_equal(['q', 'q'], g:regs)
1209 delfunc TestFunc
1210 unmap W
1211 unlet g:typed
1212 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001213
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001214 bwipe!
1215 delfunc s:save_reg_stat
1216 unlet s:reg_stat
1217endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001218
1219func Test_libcall_libcallnr()
1220 if !has('libcall')
1221 return
1222 endif
1223
1224 if has('win32')
1225 let libc = 'msvcrt.dll'
1226 elseif has('mac')
1227 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001228 elseif executable('ldd')
1229 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1230 endif
1231 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001232 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001233 if has('linux')
1234 " There is not documented but regarding the 1st argument of glibc's
1235 " dlopen an empty string and nullptr are equivalent, so using an empty
1236 " string for the 1st argument of libcall allows to call functions.
1237 let libc = ''
1238 elseif has('sun')
1239 " Set the path to libc.so according to the architecture.
1240 let test_bits = system('file ' . GetVimProg())
1241 let test_arch = system('uname -p')
1242 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1243 let libc = '/usr/lib/sparcv9/libc.so'
1244 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1245 let libc = '/usr/lib/amd64/libc.so'
1246 else
1247 let libc = '/usr/lib/libc.so'
1248 endif
1249 else
1250 " Unfortunately skip this test until a good way is found.
1251 return
1252 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001253 endif
1254
1255 if has('win32')
1256 call assert_equal($USERPROFILE, libcall(libc, 'getenv', 'USERPROFILE'))
1257 else
1258 call assert_equal($HOME, libcall(libc, 'getenv', 'HOME'))
1259 endif
1260
1261 " If function returns NULL, libcall() should return an empty string.
1262 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1263
1264 " Test libcallnr() with string and integer argument.
1265 call assert_equal(4, libcallnr(libc, 'strlen', 'abcd'))
1266 call assert_equal(char2nr('A'), libcallnr(libc, 'toupper', char2nr('a')))
1267
1268 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1269 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1270
1271 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1272 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1273endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001274
1275sandbox function Fsandbox()
1276 normal ix
1277endfunc
1278
1279func Test_func_sandbox()
1280 sandbox let F = {-> 'hello'}
1281 call assert_equal('hello', F())
1282
1283 sandbox let F = {-> execute("normal ix\<Esc>")}
1284 call assert_fails('call F()', 'E48:')
1285 unlet F
1286
1287 call assert_fails('call Fsandbox()', 'E48:')
1288 delfunc Fsandbox
1289endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001290
1291func EditAnotherFile()
1292 let word = expand('<cword>')
1293 edit Xfuncrange2
1294endfunc
1295
1296func Test_func_range_with_edit()
1297 " Define a function that edits another buffer, then call it with a range that
1298 " is invalid in that buffer.
1299 call writefile(['just one line'], 'Xfuncrange2')
1300 new
1301 call setline(1, range(10))
1302 write Xfuncrange1
1303 call assert_fails('5,8call EditAnotherFile()', 'E16:')
1304
1305 call delete('Xfuncrange1')
1306 call delete('Xfuncrange2')
1307 bwipe!
1308endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001309
1310func Test_func_exists_on_reload()
1311 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1312 call assert_equal(0, exists('*ExistingFunction'))
1313 source Xfuncexists
1314 call assert_equal(1, exists('*ExistingFunction'))
1315 " Redefining a function when reloading a script is OK.
1316 source Xfuncexists
1317 call assert_equal(1, exists('*ExistingFunction'))
1318
1319 " But redefining in another script is not OK.
1320 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1321 call assert_fails('source Xfuncexists2', 'E122:')
1322
1323 delfunc ExistingFunction
1324 call assert_equal(0, exists('*ExistingFunction'))
1325 call writefile([
1326 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1327 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1328 \ ], 'Xfuncexists')
1329 call assert_fails('source Xfuncexists', 'E122:')
1330 call assert_equal(1, exists('*ExistingFunction'))
1331
1332 call delete('Xfuncexists2')
1333 call delete('Xfuncexists')
1334 delfunc ExistingFunction
1335endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01001336
1337" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1338func Test_confirm()
1339 if !has('unix') || has('gui_running')
1340 return
1341 endif
1342
1343 call feedkeys('o', 'L')
1344 let a = confirm('Press O to proceed')
1345 call assert_equal(1, a)
1346
1347 call feedkeys('y', 'L')
1348 let a = confirm('Are you sure?', "&Yes\n&No")
1349 call assert_equal(1, a)
1350
1351 call feedkeys('n', 'L')
1352 let a = confirm('Are you sure?', "&Yes\n&No")
1353 call assert_equal(2, a)
1354
1355 " confirm() should return 0 when pressing CTRL-C.
1356 call feedkeys("\<C-c>", 'L')
1357 let a = confirm('Are you sure?', "&Yes\n&No")
1358 call assert_equal(0, a)
1359
1360 " <Esc> requires another character to avoid it being seen as the start of an
1361 " escape sequence. Zero should be harmless.
1362 call feedkeys("\<Esc>0", 'L')
1363 let a = confirm('Are you sure?', "&Yes\n&No")
1364 call assert_equal(0, a)
1365
1366 " Default choice is returned when pressing <CR>.
1367 call feedkeys("\<CR>", 'L')
1368 let a = confirm('Are you sure?', "&Yes\n&No")
1369 call assert_equal(1, a)
1370
1371 call feedkeys("\<CR>", 'L')
1372 let a = confirm('Are you sure?', "&Yes\n&No", 2)
1373 call assert_equal(2, a)
1374
1375 call feedkeys("\<CR>", 'L')
1376 let a = confirm('Are you sure?', "&Yes\n&No", 0)
1377 call assert_equal(0, a)
1378
1379 " Test with the {type} 4th argument
1380 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
1381 call feedkeys('y', 'L')
1382 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
1383 call assert_equal(1, a)
1384 endfor
1385
1386 call assert_fails('call confirm([])', 'E730:')
1387 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
1388 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
1389 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
1390endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001391
1392func Test_platform_name()
1393 " The system matches at most only one name.
1394 let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
1395 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
1396
1397 " Is Unix?
1398 call assert_equal(has('beos'), has('beos') && has('unix'))
1399 call assert_equal(has('bsd'), has('bsd') && has('unix'))
1400 call assert_equal(has('hpux'), has('hpux') && has('unix'))
1401 call assert_equal(has('linux'), has('linux') && has('unix'))
1402 call assert_equal(has('mac'), has('mac') && has('unix'))
1403 call assert_equal(has('qnx'), has('qnx') && has('unix'))
1404 call assert_equal(has('sun'), has('sun') && has('unix'))
1405 call assert_equal(has('win32'), has('win32') && !has('unix'))
1406 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
1407
1408 if has('unix') && executable('uname')
1409 let uname = system('uname')
1410 call assert_equal(uname =~? 'BeOS', has('beos'))
Bram Moolenaara02e3f62019-02-07 21:27:14 +01001411 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
1412 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001413 call assert_equal(uname =~? 'HP-UX', has('hpux'))
1414 call assert_equal(uname =~? 'Linux', has('linux'))
1415 call assert_equal(uname =~? 'Darwin', has('mac'))
1416 call assert_equal(uname =~? 'QNX', has('qnx'))
1417 call assert_equal(uname =~? 'SunOS', has('sun'))
1418 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
1419 endif
1420endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001421
1422func Test_readdir()
1423 call mkdir('Xdir')
1424 call writefile([], 'Xdir/foo.txt')
1425 call writefile([], 'Xdir/bar.txt')
1426 call mkdir('Xdir/dir')
1427
1428 " All results
1429 let files = readdir('Xdir')
1430 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
1431
1432 " Only results containing "f"
1433 let files = readdir('Xdir', { x -> stridx(x, 'f') !=- 1 })
1434 call assert_equal(['foo.txt'], sort(files))
1435
1436 " Only .txt files
1437 let files = readdir('Xdir', { x -> x =~ '.txt$' })
1438 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1439
1440 " Only .txt files with string
1441 let files = readdir('Xdir', 'v:val =~ ".txt$"')
1442 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1443
1444 " Limit to 1 result.
1445 let l = []
1446 let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
1447 call assert_equal(1, len(files))
1448
1449 call delete('Xdir', 'rf')
1450endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02001451
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001452func Test_delete_rf()
1453 call mkdir('Xdir')
1454 call writefile([], 'Xdir/foo.txt')
1455 call writefile([], 'Xdir/bar.txt')
1456 call mkdir('Xdir/[a-1]') " issue #696
1457 call writefile([], 'Xdir/[a-1]/foo.txt')
1458 call writefile([], 'Xdir/[a-1]/bar.txt')
1459 call assert_true(filereadable('Xdir/foo.txt'))
1460 call assert_true(filereadable('Xdir/[a-1]/foo.txt'))
1461
1462 call assert_equal(0, delete('Xdir', 'rf'))
1463 call assert_false(filereadable('Xdir/foo.txt'))
1464 call assert_false(filereadable('Xdir/[a-1]/foo.txt'))
1465endfunc
1466
Bram Moolenaar17aca702019-05-16 22:24:55 +02001467func Test_call()
1468 call assert_equal(3, call('len', [123]))
1469 call assert_fails("call call('len', 123)", 'E714:')
1470 call assert_equal(0, call('', []))
1471
1472 function Mylen() dict
1473 return len(self.data)
1474 endfunction
1475 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
1476 call assert_fails("call call('Mylen', [], 0)", 'E715:')
1477endfunc
1478
1479func Test_char2nr()
1480 call assert_equal(12354, char2nr('あ', 1))
1481endfunc
1482
1483func Test_eventhandler()
1484 call assert_equal(0, eventhandler())
1485endfunc