blob: e7b81daf4bb80cb84263d27c1052865453149478 [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 Moolenaar8c5a2782019-08-07 23:07:07 +02003source check.vim
Bram Moolenaarc2585492019-09-22 21:29:53 +02004source term_util.vim
Bram Moolenaar08243d22017-01-10 16:12:29 +01005
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +01006" Must be done first, since the alternate buffer must be unset.
7func Test_00_bufexists()
8 call assert_equal(0, bufexists('does_not_exist'))
9 call assert_equal(1, bufexists(bufnr('%')))
10 call assert_equal(0, bufexists(0))
11 new Xfoo
12 let bn = bufnr('%')
13 call assert_equal(1, bufexists(bn))
14 call assert_equal(1, bufexists('Xfoo'))
15 call assert_equal(1, bufexists(getcwd() . '/Xfoo'))
16 call assert_equal(1, bufexists(0))
17 bw
18 call assert_equal(0, bufexists(bn))
19 call assert_equal(0, bufexists('Xfoo'))
20endfunc
21
Bram Moolenaar24c2e482017-01-29 15:45:12 +010022func Test_empty()
23 call assert_equal(1, empty(''))
24 call assert_equal(0, empty('a'))
25
26 call assert_equal(1, empty(0))
27 call assert_equal(1, empty(-0))
28 call assert_equal(0, empty(1))
29 call assert_equal(0, empty(-1))
30
31 call assert_equal(1, empty(0.0))
32 call assert_equal(1, empty(-0.0))
33 call assert_equal(0, empty(1.0))
34 call assert_equal(0, empty(-1.0))
35 call assert_equal(0, empty(1.0/0.0))
36 call assert_equal(0, empty(0.0/0.0))
37
38 call assert_equal(1, empty([]))
39 call assert_equal(0, empty(['a']))
40
41 call assert_equal(1, empty({}))
42 call assert_equal(0, empty({'a':1}))
43
44 call assert_equal(1, empty(v:null))
45 call assert_equal(1, empty(v:none))
46 call assert_equal(1, empty(v:false))
47 call assert_equal(0, empty(v:true))
48
Bram Moolenaar41042f32017-03-09 12:09:32 +010049 if has('channel')
50 call assert_equal(1, empty(test_null_channel()))
51 endif
52 if has('job')
53 call assert_equal(1, empty(test_null_job()))
54 endif
55
Bram Moolenaar24c2e482017-01-29 15:45:12 +010056 call assert_equal(0, empty(function('Test_empty')))
Bram Moolenaar17aca702019-05-16 22:24:55 +020057 call assert_equal(0, empty(function('Test_empty', [0])))
Bram Moolenaar24c2e482017-01-29 15:45:12 +010058endfunc
59
60func Test_len()
61 call assert_equal(1, len(0))
62 call assert_equal(2, len(12))
63
64 call assert_equal(0, len(''))
65 call assert_equal(2, len('ab'))
66
67 call assert_equal(0, len([]))
68 call assert_equal(2, len([2, 1]))
69
70 call assert_equal(0, len({}))
71 call assert_equal(2, len({'a': 1, 'b': 2}))
72
73 call assert_fails('call len(v:none)', 'E701:')
74 call assert_fails('call len({-> 0})', 'E701:')
75endfunc
76
77func Test_max()
78 call assert_equal(0, max([]))
79 call assert_equal(2, max([2]))
80 call assert_equal(2, max([1, 2]))
81 call assert_equal(2, max([1, 2, v:null]))
82
83 call assert_equal(0, max({}))
84 call assert_equal(2, max({'a':1, 'b':2}))
85
86 call assert_fails('call max(1)', 'E712:')
87 call assert_fails('call max(v:none)', 'E712:')
88endfunc
89
90func Test_min()
91 call assert_equal(0, min([]))
92 call assert_equal(2, min([2]))
93 call assert_equal(1, min([1, 2]))
94 call assert_equal(0, min([1, 2, v:null]))
95
96 call assert_equal(0, min({}))
97 call assert_equal(1, min({'a':1, 'b':2}))
98
99 call assert_fails('call min(1)', 'E712:')
100 call assert_fails('call min(v:none)', 'E712:')
101endfunc
102
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200103func Test_strwidth()
104 for aw in ['single', 'double']
105 exe 'set ambiwidth=' . aw
106 call assert_equal(0, strwidth(''))
107 call assert_equal(1, strwidth("\t"))
108 call assert_equal(3, strwidth('Vim'))
109 call assert_equal(4, strwidth(1234))
110 call assert_equal(5, strwidth(-1234))
111
Bram Moolenaar30276f22019-01-24 17:59:39 +0100112 call assert_equal(2, strwidth('😉'))
113 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
114 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200115
116 call assert_fails('call strwidth({->0})', 'E729:')
117 call assert_fails('call strwidth([])', 'E730:')
118 call assert_fails('call strwidth({})', 'E731:')
119 call assert_fails('call strwidth(1.2)', 'E806:')
120 endfor
121
122 set ambiwidth&
123endfunc
124
Bram Moolenaar08243d22017-01-10 16:12:29 +0100125func Test_str2nr()
126 call assert_equal(0, str2nr(''))
127 call assert_equal(1, str2nr('1'))
128 call assert_equal(1, str2nr(' 1 '))
129
130 call assert_equal(1, str2nr('+1'))
131 call assert_equal(1, str2nr('+ 1'))
132 call assert_equal(1, str2nr(' + 1 '))
133
134 call assert_equal(-1, str2nr('-1'))
135 call assert_equal(-1, str2nr('- 1'))
136 call assert_equal(-1, str2nr(' - 1 '))
137
138 call assert_equal(123456789, str2nr('123456789'))
139 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100140
141 call assert_equal(5, str2nr('101', 2))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200142 call assert_equal(5, '0b101'->str2nr(2))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100143 call assert_equal(5, str2nr('0B101', 2))
144 call assert_equal(-5, str2nr('-101', 2))
145 call assert_equal(-5, str2nr('-0b101', 2))
146 call assert_equal(-5, str2nr('-0B101', 2))
147
148 call assert_equal(65, str2nr('101', 8))
149 call assert_equal(65, str2nr('0101', 8))
150 call assert_equal(-65, str2nr('-101', 8))
151 call assert_equal(-65, str2nr('-0101', 8))
152
153 call assert_equal(11259375, str2nr('abcdef', 16))
154 call assert_equal(11259375, str2nr('ABCDEF', 16))
155 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
156 call assert_equal(11259375, str2nr('0xabcdef', 16))
157 call assert_equal(11259375, str2nr('0Xabcdef', 16))
158 call assert_equal(11259375, str2nr('0XABCDEF', 16))
159 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
160
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200161 call assert_equal(1, str2nr("1'000'000", 10, 0))
162 call assert_equal(256, str2nr("1'0000'0000", 2, 1))
163 call assert_equal(262144, str2nr("1'000'000", 8, 1))
164 call assert_equal(1000000, str2nr("1'000'000", 10, 1))
Bram Moolenaarea8dcf82019-09-15 21:12:22 +0200165 call assert_equal(1000, str2nr("1'000''000", 10, 1))
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200166 call assert_equal(65536, str2nr("1'00'00", 16, 1))
167
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100168 call assert_equal(0, str2nr('0x10'))
169 call assert_equal(0, str2nr('0b10'))
170 call assert_equal(1, str2nr('12', 2))
171 call assert_equal(1, str2nr('18', 8))
172 call assert_equal(1, str2nr('1g', 16))
173
174 call assert_equal(0, str2nr(v:null))
175 call assert_equal(0, str2nr(v:none))
176
177 call assert_fails('call str2nr([])', 'E730:')
178 call assert_fails('call str2nr({->2})', 'E729:')
179 call assert_fails('call str2nr(1.2)', 'E806:')
180 call assert_fails('call str2nr(10, [])', 'E474:')
181endfunc
182
183func Test_strftime()
184 if !exists('*strftime')
185 return
186 endif
187 " Format of strftime() depends on system. We assume
188 " that basic formats tested here are available and
189 " identical on all systems which support strftime().
190 "
191 " The 2nd parameter of strftime() is a local time, so the output day
192 " of strftime() can be 17 or 18, depending on timezone.
193 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
194 "
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200195 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\)$', '%Y-%m-%d %H:%M:%S'->strftime())
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100196
197 call assert_fails('call strftime([])', 'E730:')
198 call assert_fails('call strftime("%Y", [])', 'E745:')
Bram Moolenaardb517302019-06-18 22:53:24 +0200199
200 " Check that the time changes after we change the timezone
201 " Save previous timezone value, if any
202 if exists('$TZ')
203 let tz = $TZ
204 endif
205
206 " Force EST and then UTC, save the current hour (24-hour clock) for each
207 let $TZ = 'EST' | let est = strftime('%H')
208 let $TZ = 'UTC' | let utc = strftime('%H')
209
210 " Those hours should be two bytes long, and should not be the same; if they
211 " are, a tzset(3) call may have failed somewhere
212 call assert_equal(strlen(est), 2)
213 call assert_equal(strlen(utc), 2)
Bram Moolenaar87652a72019-06-18 23:07:37 +0200214 " TODO: this fails on MS-Windows
215 if has('unix')
216 call assert_notequal(est, utc)
217 endif
Bram Moolenaardb517302019-06-18 22:53:24 +0200218
219 " If we cached a timezone value, put it back, otherwise clear it
220 if exists('tz')
221 let $TZ = tz
222 else
223 unlet $TZ
224 endif
225
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100226endfunc
227
Bram Moolenaardce1e892019-02-10 23:18:53 +0100228func Test_resolve_unix()
Bram Moolenaar26109902018-10-06 15:43:17 +0200229 if !has('unix')
230 return
231 endif
232
233 " Xlink1 -> Xlink2
234 " Xlink2 -> Xlink3
235 silent !ln -s -f Xlink2 Xlink1
236 silent !ln -s -f Xlink3 Xlink2
237 call assert_equal('Xlink3', resolve('Xlink1'))
238 call assert_equal('./Xlink3', resolve('./Xlink1'))
239 call assert_equal('Xlink3/', resolve('Xlink2/'))
240 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
241 "call assert_equal('Xlink3/', resolve('Xlink1/'))
242 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
243 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
244 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
245
246 " Test resolve() with a symlink cycle.
247 " Xlink1 -> Xlink2
248 " Xlink2 -> Xlink3
249 " Xlink3 -> Xlink1
250 silent !ln -s -f Xlink1 Xlink3
251 call assert_fails('call resolve("Xlink1")', 'E655:')
252 call assert_fails('call resolve("./Xlink1")', 'E655:')
253 call assert_fails('call resolve("Xlink2")', 'E655:')
254 call assert_fails('call resolve("Xlink3")', 'E655:')
255 call delete('Xlink1')
256 call delete('Xlink2')
257 call delete('Xlink3')
258
259 silent !ln -s -f Xdir//Xfile Xlink
260 call assert_equal('Xdir/Xfile', resolve('Xlink'))
261 call delete('Xlink')
262
263 silent !ln -s -f Xlink2/ Xlink1
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200264 call assert_equal('Xlink2', 'Xlink1'->resolve())
Bram Moolenaar26109902018-10-06 15:43:17 +0200265 call assert_equal('Xlink2/', resolve('Xlink1/'))
266 call delete('Xlink1')
267
268 silent !ln -s -f ./Xlink2 Xlink1
269 call assert_equal('Xlink2', resolve('Xlink1'))
270 call assert_equal('./Xlink2', resolve('./Xlink1'))
271 call delete('Xlink1')
272endfunc
273
Bram Moolenaardce1e892019-02-10 23:18:53 +0100274func s:normalize_fname(fname)
275 let ret = substitute(a:fname, '\', '/', 'g')
276 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200277 return ret->tolower()
Bram Moolenaardce1e892019-02-10 23:18:53 +0100278endfunc
279
280func Test_resolve_win32()
281 if !has('win32')
282 return
283 endif
284
285 " test for shortcut file
286 if executable('cscript')
287 new Xfile
288 wq
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200289 let lines =<< trim END
290 Set fs = CreateObject("Scripting.FileSystemObject")
291 Set ws = WScript.CreateObject("WScript.Shell")
292 Set shortcut = ws.CreateShortcut("Xlink.lnk")
293 shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")
294 shortcut.Save
295 END
296 call writefile(lines, 'link.vbs')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100297 silent !cscript link.vbs
298 call delete('link.vbs')
299 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
300 call delete('Xfile')
301
302 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk')))
303 call delete('Xlink.lnk')
304 else
305 echomsg 'skipped test for shortcut file'
306 endif
307
308 " remove files
309 call delete('Xlink')
310 call delete('Xdir', 'd')
311 call delete('Xfile')
312
313 " test for symbolic link to a file
314 new Xfile
315 wq
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200316 call assert_equal('Xfile', resolve('Xfile'))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100317 silent !mklink Xlink Xfile
318 if !v:shell_error
319 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink')))
320 call delete('Xlink')
321 else
322 echomsg 'skipped test for symbolic link to a file'
323 endif
324 call delete('Xfile')
325
326 " test for junction to a directory
327 call mkdir('Xdir')
328 silent !mklink /J Xlink Xdir
329 if !v:shell_error
330 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
331
332 call delete('Xdir', 'd')
333
334 " test for junction already removed
335 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
336 call delete('Xlink')
337 else
338 echomsg 'skipped test for junction to a directory'
339 call delete('Xdir', 'd')
340 endif
341
342 " test for symbolic link to a directory
343 call mkdir('Xdir')
344 silent !mklink /D Xlink Xdir
345 if !v:shell_error
346 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
347
348 call delete('Xdir', 'd')
349
350 " test for symbolic link already removed
351 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
352 call delete('Xlink')
353 else
354 echomsg 'skipped test for symbolic link to a directory'
355 call delete('Xdir', 'd')
356 endif
357
358 " test for buffer name
359 new Xfile
360 wq
361 silent !mklink Xlink Xfile
362 if !v:shell_error
363 edit Xlink
364 call assert_equal('Xlink', bufname('%'))
365 call delete('Xlink')
366 bw!
367 else
368 echomsg 'skipped test for buffer name'
369 endif
370 call delete('Xfile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200371
372 " test for reparse point
373 call mkdir('Xdir')
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200374 call assert_equal('Xdir', resolve('Xdir'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200375 silent !mklink /D Xdirlink Xdir
376 if !v:shell_error
377 w Xdir/text.txt
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200378 call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200379 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
380 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200381 call delete('Xdirlink')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200382 else
383 echomsg 'skipped test for reparse point'
384 endif
385
386 call delete('Xdir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100387endfunc
388
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100389func Test_simplify()
390 call assert_equal('', simplify(''))
391 call assert_equal('/', simplify('/'))
392 call assert_equal('/', simplify('/.'))
393 call assert_equal('/', simplify('/..'))
394 call assert_equal('/...', simplify('/...'))
395 call assert_equal('./dir/file', simplify('./dir/file'))
396 call assert_equal('./dir/file', simplify('.///dir//file'))
397 call assert_equal('./dir/file', simplify('./dir/./file'))
398 call assert_equal('./file', simplify('./dir/../file'))
399 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
400 call assert_equal('./file', simplify('dir/.././file'))
401
402 call assert_fails('call simplify({->0})', 'E729:')
403 call assert_fails('call simplify([])', 'E730:')
404 call assert_fails('call simplify({})', 'E731:')
405 call assert_fails('call simplify(1.2)', 'E806:')
Bram Moolenaar08243d22017-01-10 16:12:29 +0100406endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100407
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200408func Test_pathshorten()
409 call assert_equal('', pathshorten(''))
410 call assert_equal('foo', pathshorten('foo'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200411 call assert_equal('/foo', '/foo'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200412 call assert_equal('f/', pathshorten('foo/'))
413 call assert_equal('f/bar', pathshorten('foo/bar'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200414 call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200415 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
416 call assert_equal('.f/bar', pathshorten('.foo/bar'))
417 call assert_equal('~f/bar', pathshorten('~foo/bar'))
418 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
419 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
420 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
421endfunc
422
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100423func Test_strpart()
424 call assert_equal('de', strpart('abcdefg', 3, 2))
425 call assert_equal('ab', strpart('abcdefg', -2, 4))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200426 call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100427 call assert_equal('fg', strpart('abcdefg', 5, 4))
428 call assert_equal('defg', strpart('abcdefg', 3))
429
Bram Moolenaar30276f22019-01-24 17:59:39 +0100430 call assert_equal('lép', strpart('éléphant', 2, 4))
431 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100432endfunc
433
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100434func Test_tolower()
435 call assert_equal("", tolower(""))
436
437 " Test with all printable ASCII characters.
438 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
439 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
440
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100441 " Test with a few uppercase diacritics.
442 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
443 call assert_equal("bḃḇ", tolower("BḂḆ"))
444 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
445 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
446 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
447 call assert_equal("fḟ ", tolower("FḞ "))
448 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
449 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
450 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
451 call assert_equal("jĵ", tolower("JĴ"))
452 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
453 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
454 call assert_equal("mḿṁ", tolower("MḾṀ"))
455 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
456 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
457 call assert_equal("pṕṗ", tolower("PṔṖ"))
458 call assert_equal("q", tolower("Q"))
459 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
460 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
461 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
462 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
463 call assert_equal("vṽ", tolower("VṼ"))
464 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
465 call assert_equal("xẋẍ", tolower("XẊẌ"))
466 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
467 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
468
469 " Test with a few lowercase diacritics, which should remain unchanged.
470 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
471 call assert_equal("bḃḇ", tolower("bḃḇ"))
472 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
473 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
474 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
475 call assert_equal("fḟ", tolower("fḟ"))
476 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
477 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
478 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
479 call assert_equal("jĵǰ", tolower("jĵǰ"))
480 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
481 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
482 call assert_equal("mḿṁ ", tolower("mḿṁ "))
483 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
484 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
485 call assert_equal("pṕṗ", tolower("pṕṗ"))
486 call assert_equal("q", tolower("q"))
487 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
488 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
489 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
490 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
491 call assert_equal("vṽ", tolower("vṽ"))
492 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
493 call assert_equal("ẋẍ", tolower("ẋẍ"))
494 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
495 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
496
497 " According to https://twitter.com/jifa/status/625776454479970304
498 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
499 " in length (2 to 3 bytes) when lowercased. So let's test them.
500 call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100501
502 " This call to tolower with invalid utf8 sequence used to cause access to
503 " invalid memory.
504 call tolower("\xC0\x80\xC0")
505 call tolower("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100506endfunc
507
508func Test_toupper()
509 call assert_equal("", toupper(""))
510
511 " Test with all printable ASCII characters.
512 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
513 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
514
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100515 " Test with a few lowercase diacritics.
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200516 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100517 call assert_equal("BḂḆ", toupper("bḃḇ"))
518 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
519 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
520 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
521 call assert_equal("FḞ", toupper("fḟ"))
522 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
523 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
524 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
525 call assert_equal("JĴǰ", toupper("jĵǰ"))
526 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
527 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
528 call assert_equal("MḾṀ ", toupper("mḿṁ "))
529 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
530 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
531 call assert_equal("PṔṖ", toupper("pṕṗ"))
532 call assert_equal("Q", toupper("q"))
533 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
534 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
535 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
536 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
537 call assert_equal("VṼ", toupper("vṽ"))
538 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
539 call assert_equal("ẊẌ", toupper("ẋẍ"))
540 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
541 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
542
543 " Test that uppercase diacritics, which should remain unchanged.
544 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
545 call assert_equal("BḂḆ", toupper("BḂḆ"))
546 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
547 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
548 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
549 call assert_equal("FḞ ", toupper("FḞ "))
550 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
551 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
552 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
553 call assert_equal("JĴ", toupper("JĴ"))
554 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
555 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
556 call assert_equal("MḾṀ", toupper("MḾṀ"))
557 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
558 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
559 call assert_equal("PṔṖ", toupper("PṔṖ"))
560 call assert_equal("Q", toupper("Q"))
561 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
562 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
563 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
564 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
565 call assert_equal("VṼ", toupper("VṼ"))
566 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
567 call assert_equal("XẊẌ", toupper("XẊẌ"))
568 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
569 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
570
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100571 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100572
573 " This call to toupper with invalid utf8 sequence used to cause access to
574 " invalid memory.
575 call toupper("\xC0\x80\xC0")
576 call toupper("123\xC0\x80\xC0")
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100577endfunc
578
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200579func Test_tr()
580 call assert_equal('foo', tr('bar', 'bar', 'foo'))
581 call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
582endfunc
583
Bram Moolenaare90858d2017-02-01 17:24:34 +0100584" Tests for the mode() function
585let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100586func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100587 let g:current_modes = mode(0) . '-' . mode(1)
588 return ''
589endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100590
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100591func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100592 new
593 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
594
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100595 " Only complete from the current buffer.
596 set complete=.
597
Bram Moolenaare90858d2017-02-01 17:24:34 +0100598 inoremap <F2> <C-R>=Save_mode()<CR>
599
600 normal! 3G
601 exe "normal i\<F2>\<Esc>"
602 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100603 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100604 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
605 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100606 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100607 exe "normal iBro\<C-P>\<F2>\<Esc>u"
608 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100609 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100610 exe "normal iBa\<C-X>\<F2>\<Esc>u"
611 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100612 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100613 exe "normal iBa\<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-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100616 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
617 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100618 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100619 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
620 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100621 " i_CTRL-X CTRL-L: Multiple matches
622 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
623 call assert_equal('i-ic', g:current_modes)
624 " i_CTRL-X CTRL-L: Single match
625 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
626 call assert_equal('i-ic', g:current_modes)
627 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100628 exe "normal iCom\<C-P>\<F2>\<Esc>u"
629 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100630 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100631 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
632 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100633 " i_CTRL-X CTRL-L: No match
634 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
635 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100636
Bram Moolenaare971df32017-02-05 14:15:29 +0100637 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100638 exe "normal RBa\<C-P>\<F2>\<Esc>u"
639 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100640 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100641 exe "normal RBro\<C-P>\<F2>\<Esc>u"
642 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100643 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100644 exe "normal RBa\<C-X>\<F2>\<Esc>u"
645 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100646 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100647 exe "normal RBa\<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-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100650 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
651 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100652 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100653 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
654 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100655 " R_CTRL-X CTRL-L: Multiple matches
656 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
657 call assert_equal('R-Rc', g:current_modes)
658 " R_CTRL-X CTRL-L: Single match
659 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
660 call assert_equal('R-Rc', g:current_modes)
661 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100662 exe "normal RCom\<C-P>\<F2>\<Esc>u"
663 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100664 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100665 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
666 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100667 " R_CTRL-X CTRL-L: No match
668 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
669 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100670
Bram Moolenaara1449832019-09-01 20:16:52 +0200671 call assert_equal('n', 0->mode())
672 call assert_equal('n', 1->mode())
Bram Moolenaare90858d2017-02-01 17:24:34 +0100673
Bram Moolenaar612cc382018-07-29 15:34:26 +0200674 " i_CTRL-O
675 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
676 call assert_equal("n-niI", g:current_modes)
677
678 " R_CTRL-O
679 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
680 call assert_equal("n-niR", g:current_modes)
681
682 " gR_CTRL-O
683 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
684 call assert_equal("n-niV", g:current_modes)
685
Bram Moolenaare90858d2017-02-01 17:24:34 +0100686 " How to test operator-pending mode?
687
688 call feedkeys("v", 'xt')
689 call assert_equal('v', mode())
690 call assert_equal('v', mode(1))
691 call feedkeys("\<Esc>V", 'xt')
692 call assert_equal('V', mode())
693 call assert_equal('V', mode(1))
694 call feedkeys("\<Esc>\<C-V>", 'xt')
695 call assert_equal("\<C-V>", mode())
696 call assert_equal("\<C-V>", mode(1))
697 call feedkeys("\<Esc>", 'xt')
698
699 call feedkeys("gh", 'xt')
700 call assert_equal('s', mode())
701 call assert_equal('s', mode(1))
702 call feedkeys("\<Esc>gH", 'xt')
703 call assert_equal('S', mode())
704 call assert_equal('S', mode(1))
705 call feedkeys("\<Esc>g\<C-H>", 'xt')
706 call assert_equal("\<C-S>", mode())
707 call assert_equal("\<C-S>", mode(1))
708 call feedkeys("\<Esc>", 'xt')
709
710 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
711 call assert_equal('c-c', g:current_modes)
712 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
713 call assert_equal('c-cv', g:current_modes)
714 " How to test Ex mode?
715
716 bwipe!
717 iunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100718 set complete&
Bram Moolenaare90858d2017-02-01 17:24:34 +0100719endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100720
Bram Moolenaard2007022019-08-27 21:56:06 +0200721func Test_append()
722 enew!
723 split
724 call append(0, ["foo"])
725 split
726 only
727 undo
728endfunc
729
Bram Moolenaar79518e22017-02-17 16:31:35 +0100730func Test_getbufvar()
731 let bnr = bufnr('%')
732 let b:var_num = '1234'
733 let def_num = '5678'
734 call assert_equal('1234', getbufvar(bnr, 'var_num'))
735 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
736
737 let bd = getbufvar(bnr, '')
738 call assert_equal('1234', bd['var_num'])
739 call assert_true(exists("bd['changedtick']"))
740 call assert_equal(2, len(bd))
741
742 let bd2 = getbufvar(bnr, '', def_num)
743 call assert_equal(bd, bd2)
744
745 unlet b:var_num
746 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
747 call assert_equal('', getbufvar(bnr, 'var_num'))
748
749 let bd = getbufvar(bnr, '')
750 call assert_equal(1, len(bd))
751 let bd = getbufvar(bnr, '',def_num)
752 call assert_equal(1, len(bd))
753
Bram Moolenaar4520d442017-03-19 16:09:46 +0100754 call assert_equal('', getbufvar(9999, ''))
755 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100756 unlet def_num
757
Bram Moolenaar507647d2017-02-17 16:43:49 +0100758 call assert_equal(0, getbufvar(bnr, '&autoindent'))
759 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +0100760
761 " Open new window with forced option values
762 set fileformats=unix,dos
763 new ++ff=dos ++bin ++enc=iso-8859-2
764 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
765 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
766 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
767 close
768
769 set fileformats&
770endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +0100771
Bram Moolenaar41042f32017-03-09 12:09:32 +0100772func Test_last_buffer_nr()
773 call assert_equal(bufnr('$'), last_buffer_nr())
774endfunc
775
776func Test_stridx()
777 call assert_equal(-1, stridx('', 'l'))
778 call assert_equal(0, stridx('', ''))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200779 call assert_equal(0, 'hello'->stridx(''))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100780 call assert_equal(-1, stridx('hello', 'L'))
781 call assert_equal(2, stridx('hello', 'l', -1))
782 call assert_equal(2, stridx('hello', 'l', 0))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200783 call assert_equal(2, 'hello'->stridx('l', 1))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100784 call assert_equal(3, stridx('hello', 'l', 3))
785 call assert_equal(-1, stridx('hello', 'l', 4))
786 call assert_equal(-1, stridx('hello', 'l', 10))
787 call assert_equal(2, stridx('hello', 'll'))
788 call assert_equal(-1, stridx('hello', 'hello world'))
789endfunc
790
791func Test_strridx()
792 call assert_equal(-1, strridx('', 'l'))
793 call assert_equal(0, strridx('', ''))
794 call assert_equal(5, strridx('hello', ''))
795 call assert_equal(-1, strridx('hello', 'L'))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200796 call assert_equal(3, 'hello'->strridx('l'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100797 call assert_equal(3, strridx('hello', 'l', 10))
798 call assert_equal(3, strridx('hello', 'l', 3))
799 call assert_equal(2, strridx('hello', 'l', 2))
800 call assert_equal(-1, strridx('hello', 'l', 1))
801 call assert_equal(-1, strridx('hello', 'l', 0))
802 call assert_equal(-1, strridx('hello', 'l', -1))
803 call assert_equal(2, strridx('hello', 'll'))
804 call assert_equal(-1, strridx('hello', 'hello world'))
805endfunc
806
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200807func Test_match_func()
808 call assert_equal(4, match('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200809 call assert_equal(4, 'testing'->match('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200810 call assert_equal(-1, match('testing', 'ing', 5))
811 call assert_equal(-1, match('testing', 'ing', 8))
812 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
813 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
814endfunc
815
Bram Moolenaar41042f32017-03-09 12:09:32 +0100816func Test_matchend()
817 call assert_equal(7, matchend('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200818 call assert_equal(7, 'testing'->matchend('ing', 2))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100819 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200820 call assert_equal(-1, matchend('testing', 'ing', 8))
821 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
822 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
823endfunc
824
825func Test_matchlist()
826 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200827 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200828 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
829endfunc
830
831func Test_matchstr()
832 call assert_equal('ing', matchstr('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200833 call assert_equal('ing', 'testing'->matchstr('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200834 call assert_equal('', matchstr('testing', 'ing', 5))
835 call assert_equal('', matchstr('testing', 'ing', 8))
836 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
837 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
838endfunc
839
840func Test_matchstrpos()
841 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +0200842 call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +0200843 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
844 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
845 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
846 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100847endfunc
848
849func Test_nextnonblank_prevnonblank()
850 new
851insert
852This
853
854
855is
856
857a
858Test
859.
860 call assert_equal(0, nextnonblank(-1))
861 call assert_equal(0, nextnonblank(0))
862 call assert_equal(1, nextnonblank(1))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200863 call assert_equal(4, 2->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100864 call assert_equal(4, nextnonblank(3))
865 call assert_equal(4, nextnonblank(4))
866 call assert_equal(6, nextnonblank(5))
867 call assert_equal(6, nextnonblank(6))
868 call assert_equal(7, nextnonblank(7))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200869 call assert_equal(0, 8->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100870
871 call assert_equal(0, prevnonblank(-1))
872 call assert_equal(0, prevnonblank(0))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200873 call assert_equal(1, 1->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100874 call assert_equal(1, prevnonblank(2))
875 call assert_equal(1, prevnonblank(3))
876 call assert_equal(4, prevnonblank(4))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200877 call assert_equal(4, 5->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +0100878 call assert_equal(6, prevnonblank(6))
879 call assert_equal(7, prevnonblank(7))
880 call assert_equal(0, prevnonblank(8))
881 bw!
882endfunc
883
884func Test_byte2line_line2byte()
885 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200886 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +0100887 call setline(1, ['a', 'bc', 'd'])
888
889 set fileformat=unix
890 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
891 \ map(range(-1, 8), 'byte2line(v:val)'))
892 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
893 \ map(range(-1, 5), 'line2byte(v:val)'))
894
895 set fileformat=mac
896 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
Bram Moolenaar64b4d732019-08-22 22:18:17 +0200897 \ map(range(-1, 8), 'v:val->byte2line()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100898 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
Bram Moolenaar02b31112019-08-31 22:16:38 +0200899 \ map(range(-1, 5), 'v:val->line2byte()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +0100900
901 set fileformat=dos
902 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
903 \ map(range(-1, 11), 'byte2line(v:val)'))
904 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
905 \ map(range(-1, 5), 'line2byte(v:val)'))
906
Bram Moolenaarc26f7c62018-08-20 22:53:04 +0200907 bw!
908 set noendofline nofixendofline
909 normal a-
910 for ff in ["unix", "mac", "dos"]
911 let &fileformat = ff
912 call assert_equal(1, line2byte(1))
913 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
914 endfor
915
916 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +0100917 bw!
918endfunc
919
Bram Moolenaar64b4d732019-08-22 22:18:17 +0200920func Test_byteidx()
921 let a = '.é.' " one char of two bytes
922 call assert_equal(0, byteidx(a, 0))
923 call assert_equal(0, byteidxcomp(a, 0))
924 call assert_equal(1, byteidx(a, 1))
925 call assert_equal(1, byteidxcomp(a, 1))
926 call assert_equal(3, byteidx(a, 2))
927 call assert_equal(3, byteidxcomp(a, 2))
928 call assert_equal(4, byteidx(a, 3))
929 call assert_equal(4, byteidxcomp(a, 3))
930 call assert_equal(-1, byteidx(a, 4))
931 call assert_equal(-1, byteidxcomp(a, 4))
932
933 let b = '.é.' " normal e with composing char
934 call assert_equal(0, b->byteidx(0))
935 call assert_equal(1, b->byteidx(1))
936 call assert_equal(4, b->byteidx(2))
937 call assert_equal(5, b->byteidx(3))
938 call assert_equal(-1, b->byteidx(4))
939
940 call assert_equal(0, b->byteidxcomp(0))
941 call assert_equal(1, b->byteidxcomp(1))
942 call assert_equal(2, b->byteidxcomp(2))
943 call assert_equal(4, b->byteidxcomp(3))
944 call assert_equal(5, b->byteidxcomp(4))
945 call assert_equal(-1, b->byteidxcomp(5))
946endfunc
947
Bram Moolenaar41042f32017-03-09 12:09:32 +0100948func Test_count()
949 let l = ['a', 'a', 'A', 'b']
950 call assert_equal(2, count(l, 'a'))
951 call assert_equal(1, count(l, 'A'))
952 call assert_equal(1, count(l, 'b'))
953 call assert_equal(0, count(l, 'B'))
954
955 call assert_equal(2, count(l, 'a', 0))
956 call assert_equal(1, count(l, 'A', 0))
957 call assert_equal(1, count(l, 'b', 0))
958 call assert_equal(0, count(l, 'B', 0))
959
960 call assert_equal(3, count(l, 'a', 1))
961 call assert_equal(3, count(l, 'A', 1))
962 call assert_equal(1, count(l, 'b', 1))
963 call assert_equal(1, count(l, 'B', 1))
964 call assert_equal(0, count(l, 'c', 1))
965
966 call assert_equal(1, count(l, 'a', 0, 1))
967 call assert_equal(2, count(l, 'a', 1, 1))
968 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +0200969 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100970
971 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
972 call assert_equal(2, count(d, 'a'))
973 call assert_equal(1, count(d, 'A'))
974 call assert_equal(1, count(d, 'b'))
975 call assert_equal(0, count(d, 'B'))
976
977 call assert_equal(2, count(d, 'a', 0))
978 call assert_equal(1, count(d, 'A', 0))
979 call assert_equal(1, count(d, 'b', 0))
980 call assert_equal(0, count(d, 'B', 0))
981
982 call assert_equal(3, count(d, 'a', 1))
983 call assert_equal(3, count(d, 'A', 1))
984 call assert_equal(1, count(d, 'b', 1))
985 call assert_equal(1, count(d, 'B', 1))
986 call assert_equal(0, count(d, 'c', 1))
987
988 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +0200989
990 call assert_equal(0, count("foo", "bar"))
991 call assert_equal(1, count("foo", "oo"))
992 call assert_equal(2, count("foo", "o"))
993 call assert_equal(0, count("foo", "O"))
994 call assert_equal(2, count("foo", "O", 1))
995 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +0100996 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +0200997
998 call assert_fails('call count(0, 0)', 'E712:')
Bram Moolenaar41042f32017-03-09 12:09:32 +0100999endfunc
1000
1001func Test_changenr()
1002 new Xchangenr
1003 call assert_equal(0, changenr())
1004 norm ifoo
1005 call assert_equal(1, changenr())
1006 set undolevels=10
1007 norm Sbar
1008 call assert_equal(2, changenr())
1009 undo
1010 call assert_equal(1, changenr())
1011 redo
1012 call assert_equal(2, changenr())
1013 bw!
1014 set undolevels&
1015endfunc
1016
1017func Test_filewritable()
1018 new Xfilewritable
1019 write!
1020 call assert_equal(1, filewritable('Xfilewritable'))
1021
1022 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1023 call assert_equal(0, filewritable('Xfilewritable'))
1024
1025 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001026 call assert_equal(1, 'Xfilewritable'->filewritable())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001027
1028 call assert_equal(0, filewritable('doesnotexist'))
1029
1030 call delete('Xfilewritable')
1031 bw!
1032endfunc
1033
Bram Moolenaar82956662018-10-06 15:18:45 +02001034func Test_Executable()
1035 if has('win32')
1036 call assert_equal(1, executable('notepad'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001037 call assert_equal(1, 'notepad.exe'->executable())
Bram Moolenaar82956662018-10-06 15:18:45 +02001038 call assert_equal(0, executable('notepad.exe.exe'))
1039 call assert_equal(0, executable('shell32.dll'))
1040 call assert_equal(0, executable('win.ini'))
1041 elseif has('unix')
Bram Moolenaara4208962019-08-24 20:50:19 +02001042 call assert_equal(1, 'cat'->executable())
Bram Moolenaara05a0d32018-10-07 18:43:05 +02001043 call assert_equal(0, executable('nodogshere'))
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001044
1045 " get "cat" path and remove the leading /
1046 let catcmd = exepath('cat')[1:]
1047 new
Bram Moolenaara4208962019-08-24 20:50:19 +02001048 " check that the relative path works in /
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001049 lcd /
1050 call assert_equal(1, executable(catcmd))
Bram Moolenaara4208962019-08-24 20:50:19 +02001051 call assert_equal('/' .. catcmd, catcmd->exepath())
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001052 bwipe
Bram Moolenaar82956662018-10-06 15:18:45 +02001053 endif
1054endfunc
1055
Bram Moolenaar86621892019-03-30 21:51:28 +01001056func Test_executable_longname()
1057 if !has('win32')
1058 return
1059 endif
1060
1061 let fname = 'X' . repeat('あ', 200) . '.bat'
1062 call writefile([], fname)
1063 call assert_equal(1, executable(fname))
1064 call delete(fname)
1065endfunc
1066
Bram Moolenaar41042f32017-03-09 12:09:32 +01001067func Test_hostname()
1068 let hostname_vim = hostname()
1069 if has('unix')
1070 let hostname_system = systemlist('uname -n')[0]
1071 call assert_equal(hostname_vim, hostname_system)
1072 endif
1073endfunc
1074
1075func Test_getpid()
1076 " getpid() always returns the same value within a vim instance.
1077 call assert_equal(getpid(), getpid())
1078 if has('unix')
1079 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1080 endif
1081endfunc
1082
1083func Test_hlexists()
1084 call assert_equal(0, hlexists('does_not_exist'))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001085 call assert_equal(0, 'Number'->hlexists())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001086 call assert_equal(0, highlight_exists('does_not_exist'))
1087 call assert_equal(0, highlight_exists('Number'))
1088 syntax on
1089 call assert_equal(0, hlexists('does_not_exist'))
1090 call assert_equal(1, hlexists('Number'))
1091 call assert_equal(0, highlight_exists('does_not_exist'))
1092 call assert_equal(1, highlight_exists('Number'))
1093 syntax off
1094endfunc
1095
1096func Test_col()
1097 new
1098 call setline(1, 'abcdef')
1099 norm gg4|mx6|mY2|
1100 call assert_equal(2, col('.'))
1101 call assert_equal(7, col('$'))
1102 call assert_equal(4, col("'x"))
1103 call assert_equal(6, col("'Y"))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001104 call assert_equal(2, [1, 2]->col())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001105 call assert_equal(7, col([1, '$']))
1106
1107 call assert_equal(0, col(''))
1108 call assert_equal(0, col('x'))
1109 call assert_equal(0, col([2, '$']))
1110 call assert_equal(0, col([1, 100]))
1111 call assert_equal(0, col([1]))
1112 bw!
1113endfunc
1114
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001115func Test_inputlist()
1116 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1117 call assert_equal(1, c)
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001118 call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001119 call assert_equal(2, c)
1120 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1121 call assert_equal(3, c)
1122
1123 call assert_fails('call inputlist("")', 'E686:')
1124endfunc
1125
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001126func Test_balloon_show()
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001127 if has('balloon_eval')
1128 " This won't do anything but must not crash either.
1129 call balloon_show('hi!')
1130 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001131endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001132
1133func Test_setbufvar_options()
1134 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1135 " window layout.
1136 call assert_equal(1, winnr('$'))
1137 split dummy_preview
1138 resize 2
1139 set winfixheight winfixwidth
1140 let prev_id = win_getid()
1141
1142 wincmd j
1143 let wh = winheight('.')
1144 let dummy_buf = bufnr('dummy_buf1', v:true)
1145 call setbufvar(dummy_buf, '&buftype', 'nofile')
1146 execute 'belowright vertical split #' . dummy_buf
1147 call assert_equal(wh, winheight('.'))
1148 let dum1_id = win_getid()
1149
1150 wincmd h
1151 let wh = winheight('.')
1152 let dummy_buf = bufnr('dummy_buf2', v:true)
Bram Moolenaar196b4662019-09-06 21:34:30 +02001153 eval 'nofile'->setbufvar(dummy_buf, '&buftype')
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001154 execute 'belowright vertical split #' . dummy_buf
1155 call assert_equal(wh, winheight('.'))
1156
1157 bwipe!
1158 call win_gotoid(prev_id)
1159 bwipe!
1160 call win_gotoid(dum1_id)
1161 bwipe!
1162endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001163
1164func Test_redo_in_nested_functions()
1165 nnoremap g. :set opfunc=Operator<CR>g@
1166 function Operator( type, ... )
1167 let @x = 'XXX'
1168 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1169 endfunction
1170
1171 function! Apply()
1172 5,6normal! .
1173 endfunction
1174
1175 new
1176 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1177 1normal g.i"
1178 call assert_equal('some "XXX" text', getline(1))
1179 3,4normal .
1180 call assert_equal('some "XXX" text', getline(3))
1181 call assert_equal('more "XXX" text', getline(4))
1182 call Apply()
1183 call assert_equal('some "XXX" text', getline(5))
1184 call assert_equal('more "XXX" text', getline(6))
1185 bwipe!
1186
1187 nunmap g.
1188 delfunc Operator
1189 delfunc Apply
1190endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02001191
1192func Test_shellescape()
1193 let save_shell = &shell
1194 set shell=bash
1195 call assert_equal("'text'", shellescape('text'))
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001196 call assert_equal("'te\"xt'", 'te"xt'->shellescape())
Bram Moolenaar20615522017-06-05 18:46:26 +02001197 call assert_equal("'te'\\''xt'", shellescape("te'xt"))
1198
1199 call assert_equal("'te%xt'", shellescape("te%xt"))
1200 call assert_equal("'te\\%xt'", shellescape("te%xt", 1))
1201 call assert_equal("'te#xt'", shellescape("te#xt"))
1202 call assert_equal("'te\\#xt'", shellescape("te#xt", 1))
1203 call assert_equal("'te!xt'", shellescape("te!xt"))
1204 call assert_equal("'te\\!xt'", shellescape("te!xt", 1))
1205
1206 call assert_equal("'te\nxt'", shellescape("te\nxt"))
1207 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1))
1208 set shell=tcsh
1209 call assert_equal("'te\\!xt'", shellescape("te!xt"))
1210 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1))
1211 call assert_equal("'te\\\nxt'", shellescape("te\nxt"))
1212 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1))
1213
1214 let &shell = save_shell
1215endfunc
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001216
1217func Test_trim()
1218 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001219 call assert_equal("Testing", " \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B"->trim())
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001220 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1221 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
1222 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
1223 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
1224 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
1225 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
1226 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
1227 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
1228 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
1229 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
1230 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
1231 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1232 call assert_equal("", trim("", ""))
1233 call assert_equal("a", trim("a", ""))
1234 call assert_equal("", trim("", "a"))
1235
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001236 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001237 call assert_equal("x", trim(chars . "x" . chars))
1238endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001239
1240" Test for reg_recording() and reg_executing()
1241func Test_reg_executing_and_recording()
1242 let s:reg_stat = ''
1243 func s:save_reg_stat()
1244 let s:reg_stat = reg_recording() . ':' . reg_executing()
1245 return ''
1246 endfunc
1247
1248 new
1249 call s:save_reg_stat()
1250 call assert_equal(':', s:reg_stat)
1251 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1252 call assert_equal('a:', s:reg_stat)
1253 call feedkeys("@a", 'xt')
1254 call assert_equal(':a', s:reg_stat)
1255 call feedkeys("qb@aq", 'xt')
1256 call assert_equal('b:a', s:reg_stat)
1257 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1258 call assert_equal('":', s:reg_stat)
1259
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001260 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001261 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001262 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1263 func TestFunc() abort
1264 normal! ia
1265 endfunc
1266 call feedkeys("@q", 'xt')
1267 call assert_equal(':q', s:reg_stat)
1268 delfunc TestFunc
1269
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001270 " getchar() command saves and restores reg_executing
1271 map W :call TestFunc()<CR>
1272 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001273 let g:typed = ''
1274 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001275 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001276 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001277 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001278 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001279 endfunc
1280 call feedkeys("@qy", 'xt')
1281 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001282 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001283 delfunc TestFunc
1284 unmap W
1285 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001286 unlet g:regs
1287
1288 " input() command saves and restores reg_executing
1289 map W :call TestFunc()<CR>
1290 let @q = "W"
1291 let g:typed = ''
1292 let g:regs = []
1293 func TestFunc() abort
1294 let g:regs += [reg_executing()]
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001295 let g:typed = '?'->input()
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001296 let g:regs += [reg_executing()]
1297 endfunc
1298 call feedkeys("@qy\<CR>", 'xt')
1299 call assert_equal("y", g:typed)
1300 call assert_equal(['q', 'q'], g:regs)
1301 delfunc TestFunc
1302 unmap W
1303 unlet g:typed
1304 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001305
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001306 bwipe!
1307 delfunc s:save_reg_stat
1308 unlet s:reg_stat
1309endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001310
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001311func Test_inputsecret()
1312 map W :call TestFunc()<CR>
1313 let @q = "W"
1314 let g:typed1 = ''
1315 let g:typed2 = ''
1316 let g:regs = []
1317 func TestFunc() abort
1318 let g:typed1 = '?'->inputsecret()
1319 let g:typed2 = inputsecret('password: ')
1320 endfunc
1321 call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1322 call assert_equal("something", g:typed1)
1323 call assert_equal("else", g:typed2)
1324 delfunc TestFunc
1325 unmap W
1326 unlet g:typed1
1327 unlet g:typed2
1328endfunc
1329
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001330func Test_getchar()
1331 call feedkeys('a', '')
1332 call assert_equal(char2nr('a'), getchar())
1333
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001334 call setline(1, 'xxxx')
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001335 call test_setmouse(1, 3)
1336 let v:mouse_win = 9
1337 let v:mouse_winid = 9
1338 let v:mouse_lnum = 9
1339 let v:mouse_col = 9
1340 call feedkeys("\<S-LeftMouse>", '')
1341 call assert_equal("\<S-LeftMouse>", getchar())
1342 call assert_equal(1, v:mouse_win)
1343 call assert_equal(win_getid(1), v:mouse_winid)
1344 call assert_equal(1, v:mouse_lnum)
1345 call assert_equal(3, v:mouse_col)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001346 enew!
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001347endfunc
1348
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001349func Test_libcall_libcallnr()
1350 if !has('libcall')
1351 return
1352 endif
1353
1354 if has('win32')
1355 let libc = 'msvcrt.dll'
1356 elseif has('mac')
1357 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001358 elseif executable('ldd')
1359 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1360 endif
1361 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001362 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001363 if has('linux')
1364 " There is not documented but regarding the 1st argument of glibc's
1365 " dlopen an empty string and nullptr are equivalent, so using an empty
1366 " string for the 1st argument of libcall allows to call functions.
1367 let libc = ''
1368 elseif has('sun')
1369 " Set the path to libc.so according to the architecture.
1370 let test_bits = system('file ' . GetVimProg())
1371 let test_arch = system('uname -p')
1372 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1373 let libc = '/usr/lib/sparcv9/libc.so'
1374 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1375 let libc = '/usr/lib/amd64/libc.so'
1376 else
1377 let libc = '/usr/lib/libc.so'
1378 endif
1379 else
1380 " Unfortunately skip this test until a good way is found.
1381 return
1382 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001383 endif
1384
1385 if has('win32')
Bram Moolenaar02b31112019-08-31 22:16:38 +02001386 call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001387 else
Bram Moolenaar02b31112019-08-31 22:16:38 +02001388 call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001389 endif
1390
1391 " If function returns NULL, libcall() should return an empty string.
1392 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1393
1394 " Test libcallnr() with string and integer argument.
Bram Moolenaar02b31112019-08-31 22:16:38 +02001395 call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1396 call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001397
1398 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:')
1399 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:')
1400
1401 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:')
1402 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:')
1403endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001404
1405sandbox function Fsandbox()
1406 normal ix
1407endfunc
1408
1409func Test_func_sandbox()
1410 sandbox let F = {-> 'hello'}
1411 call assert_equal('hello', F())
1412
Bram Moolenaara4208962019-08-24 20:50:19 +02001413 sandbox let F = {-> "normal ix\<Esc>"->execute()}
Bram Moolenaard90a1442018-07-15 20:24:31 +02001414 call assert_fails('call F()', 'E48:')
1415 unlet F
1416
1417 call assert_fails('call Fsandbox()', 'E48:')
1418 delfunc Fsandbox
1419endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001420
1421func EditAnotherFile()
1422 let word = expand('<cword>')
1423 edit Xfuncrange2
1424endfunc
1425
1426func Test_func_range_with_edit()
1427 " Define a function that edits another buffer, then call it with a range that
1428 " is invalid in that buffer.
1429 call writefile(['just one line'], 'Xfuncrange2')
1430 new
Bram Moolenaar196b4662019-09-06 21:34:30 +02001431 eval 10->range()->setline(1)
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001432 write Xfuncrange1
1433 call assert_fails('5,8call EditAnotherFile()', 'E16:')
1434
1435 call delete('Xfuncrange1')
1436 call delete('Xfuncrange2')
1437 bwipe!
1438endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001439
1440func Test_func_exists_on_reload()
1441 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1442 call assert_equal(0, exists('*ExistingFunction'))
1443 source Xfuncexists
Bram Moolenaara4208962019-08-24 20:50:19 +02001444 call assert_equal(1, '*ExistingFunction'->exists())
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001445 " Redefining a function when reloading a script is OK.
1446 source Xfuncexists
1447 call assert_equal(1, exists('*ExistingFunction'))
1448
1449 " But redefining in another script is not OK.
1450 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1451 call assert_fails('source Xfuncexists2', 'E122:')
1452
1453 delfunc ExistingFunction
1454 call assert_equal(0, exists('*ExistingFunction'))
1455 call writefile([
1456 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1457 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1458 \ ], 'Xfuncexists')
1459 call assert_fails('source Xfuncexists', 'E122:')
1460 call assert_equal(1, exists('*ExistingFunction'))
1461
1462 call delete('Xfuncexists2')
1463 call delete('Xfuncexists')
1464 delfunc ExistingFunction
1465endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01001466
1467" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1468func Test_confirm()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001469 CheckUnix
1470 CheckNotGui
Bram Moolenaar2e050092019-01-27 15:00:36 +01001471
1472 call feedkeys('o', 'L')
1473 let a = confirm('Press O to proceed')
1474 call assert_equal(1, a)
1475
1476 call feedkeys('y', 'L')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001477 let a = 'Are you sure?'->confirm("&Yes\n&No")
Bram Moolenaar2e050092019-01-27 15:00:36 +01001478 call assert_equal(1, a)
1479
1480 call feedkeys('n', 'L')
1481 let a = confirm('Are you sure?', "&Yes\n&No")
1482 call assert_equal(2, a)
1483
1484 " confirm() should return 0 when pressing CTRL-C.
1485 call feedkeys("\<C-c>", 'L')
1486 let a = confirm('Are you sure?', "&Yes\n&No")
1487 call assert_equal(0, a)
1488
1489 " <Esc> requires another character to avoid it being seen as the start of an
1490 " escape sequence. Zero should be harmless.
Bram Moolenaara4208962019-08-24 20:50:19 +02001491 eval "\<Esc>0"->feedkeys('L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01001492 let a = confirm('Are you sure?', "&Yes\n&No")
1493 call assert_equal(0, a)
1494
1495 " Default choice is returned when pressing <CR>.
1496 call feedkeys("\<CR>", 'L')
1497 let a = confirm('Are you sure?', "&Yes\n&No")
1498 call assert_equal(1, a)
1499
1500 call feedkeys("\<CR>", 'L')
1501 let a = confirm('Are you sure?', "&Yes\n&No", 2)
1502 call assert_equal(2, a)
1503
1504 call feedkeys("\<CR>", 'L')
1505 let a = confirm('Are you sure?', "&Yes\n&No", 0)
1506 call assert_equal(0, a)
1507
1508 " Test with the {type} 4th argument
1509 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
1510 call feedkeys('y', 'L')
1511 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
1512 call assert_equal(1, a)
1513 endfor
1514
1515 call assert_fails('call confirm([])', 'E730:')
1516 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
1517 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
1518 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
1519endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001520
1521func Test_platform_name()
1522 " The system matches at most only one name.
1523 let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
1524 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
1525
1526 " Is Unix?
1527 call assert_equal(has('beos'), has('beos') && has('unix'))
1528 call assert_equal(has('bsd'), has('bsd') && has('unix'))
1529 call assert_equal(has('hpux'), has('hpux') && has('unix'))
1530 call assert_equal(has('linux'), has('linux') && has('unix'))
1531 call assert_equal(has('mac'), has('mac') && has('unix'))
1532 call assert_equal(has('qnx'), has('qnx') && has('unix'))
1533 call assert_equal(has('sun'), has('sun') && has('unix'))
1534 call assert_equal(has('win32'), has('win32') && !has('unix'))
1535 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
1536
1537 if has('unix') && executable('uname')
1538 let uname = system('uname')
1539 call assert_equal(uname =~? 'BeOS', has('beos'))
Bram Moolenaara02e3f62019-02-07 21:27:14 +01001540 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
1541 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001542 call assert_equal(uname =~? 'HP-UX', has('hpux'))
1543 call assert_equal(uname =~? 'Linux', has('linux'))
1544 call assert_equal(uname =~? 'Darwin', has('mac'))
1545 call assert_equal(uname =~? 'QNX', has('qnx'))
1546 call assert_equal(uname =~? 'SunOS', has('sun'))
1547 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
1548 endif
1549endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001550
1551func Test_readdir()
1552 call mkdir('Xdir')
1553 call writefile([], 'Xdir/foo.txt')
1554 call writefile([], 'Xdir/bar.txt')
1555 call mkdir('Xdir/dir')
1556
1557 " All results
1558 let files = readdir('Xdir')
1559 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
1560
1561 " Only results containing "f"
Bram Moolenaara0d1fef2019-09-04 22:29:14 +02001562 let files = 'Xdir'->readdir({ x -> stridx(x, 'f') !=- 1 })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001563 call assert_equal(['foo.txt'], sort(files))
1564
1565 " Only .txt files
1566 let files = readdir('Xdir', { x -> x =~ '.txt$' })
1567 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1568
1569 " Only .txt files with string
1570 let files = readdir('Xdir', 'v:val =~ ".txt$"')
1571 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
1572
1573 " Limit to 1 result.
1574 let l = []
1575 let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
1576 call assert_equal(1, len(files))
1577
Bram Moolenaar27da7de2019-09-03 17:13:37 +02001578 " Nested readdir() must not crash
1579 let files = readdir('Xdir', 'readdir("Xdir", "1") != []')
1580 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
1581
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001582 eval 'Xdir'->delete('rf')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02001583endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02001584
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001585func Test_delete_rf()
1586 call mkdir('Xdir')
1587 call writefile([], 'Xdir/foo.txt')
1588 call writefile([], 'Xdir/bar.txt')
1589 call mkdir('Xdir/[a-1]') " issue #696
1590 call writefile([], 'Xdir/[a-1]/foo.txt')
1591 call writefile([], 'Xdir/[a-1]/bar.txt')
1592 call assert_true(filereadable('Xdir/foo.txt'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001593 call assert_true('Xdir/[a-1]/foo.txt'->filereadable())
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02001594
1595 call assert_equal(0, delete('Xdir', 'rf'))
1596 call assert_false(filereadable('Xdir/foo.txt'))
1597 call assert_false(filereadable('Xdir/[a-1]/foo.txt'))
1598endfunc
1599
Bram Moolenaar17aca702019-05-16 22:24:55 +02001600func Test_call()
1601 call assert_equal(3, call('len', [123]))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001602 call assert_equal(3, 'len'->call([123]))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001603 call assert_fails("call call('len', 123)", 'E714:')
1604 call assert_equal(0, call('', []))
1605
1606 function Mylen() dict
1607 return len(self.data)
1608 endfunction
1609 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001610 eval mydict.len->call([], mydict)->assert_equal(4)
Bram Moolenaar17aca702019-05-16 22:24:55 +02001611 call assert_fails("call call('Mylen', [], 0)", 'E715:')
1612endfunc
1613
1614func Test_char2nr()
1615 call assert_equal(12354, char2nr('あ', 1))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001616 call assert_equal(120, 'x'->char2nr())
Bram Moolenaar17aca702019-05-16 22:24:55 +02001617endfunc
1618
1619func Test_eventhandler()
1620 call assert_equal(0, eventhandler())
1621endfunc
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001622
1623func Test_bufadd_bufload()
1624 call assert_equal(0, bufexists('someName'))
1625 let buf = bufadd('someName')
1626 call assert_notequal(0, buf)
1627 call assert_equal(1, bufexists('someName'))
1628 call assert_equal(0, getbufvar(buf, '&buflisted'))
1629 call assert_equal(0, bufloaded(buf))
1630 call bufload(buf)
1631 call assert_equal(1, bufloaded(buf))
1632 call assert_equal([''], getbufline(buf, 1, '$'))
1633
1634 let curbuf = bufnr('')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001635 eval ['some', 'text']->writefile('XotherName')
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001636 let buf = 'XotherName'->bufadd()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001637 call assert_notequal(0, buf)
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001638 eval 'XotherName'->bufexists()->assert_equal(1)
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001639 call assert_equal(0, getbufvar(buf, '&buflisted'))
1640 call assert_equal(0, bufloaded(buf))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001641 eval buf->bufload()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001642 call assert_equal(1, bufloaded(buf))
1643 call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
1644 call assert_equal(curbuf, bufnr(''))
1645
Bram Moolenaar892ae722019-06-30 20:33:01 +02001646 let buf1 = bufadd('')
1647 let buf2 = bufadd('')
1648 call assert_notequal(0, buf1)
1649 call assert_notequal(0, buf2)
1650 call assert_notequal(buf1, buf2)
1651 call assert_equal(1, bufexists(buf1))
1652 call assert_equal(1, bufexists(buf2))
1653 call assert_equal(0, bufloaded(buf1))
1654 exe 'bwipe ' .. buf1
1655 call assert_equal(0, bufexists(buf1))
1656 call assert_equal(1, bufexists(buf2))
1657 exe 'bwipe ' .. buf2
1658 call assert_equal(0, bufexists(buf2))
1659
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001660 bwipe someName
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001661 bwipe XotherName
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001662 call assert_equal(0, bufexists('someName'))
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001663 call delete('XotherName')
Bram Moolenaar15e248e2019-06-30 20:21:37 +02001664endfunc
Bram Moolenaarc2585492019-09-22 21:29:53 +02001665
1666func Test_state()
1667 CheckRunVimInTerminal
1668
1669 let lines =<< trim END
1670 call setline(1, ['one', 'two', 'three'])
1671 map ;; gg
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001672 set complete=.
Bram Moolenaarc2585492019-09-22 21:29:53 +02001673 func RunTimer()
1674 call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
1675 endfunc
1676 au Filetype foobar let g:state = state()|let g:mode = mode()
1677 END
1678 call writefile(lines, 'XState')
1679 let buf = RunVimInTerminal('-S XState', #{rows: 6})
1680
1681 " Using a ":" command Vim is busy, thus "S" is returned
1682 call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
1683 call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
1684 call term_sendkeys(buf, ":\<CR>")
1685
1686 " Using a timer callback
1687 call term_sendkeys(buf, ":call RunTimer()\<CR>")
1688 call term_wait(buf, 50)
1689 let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"
1690 call term_sendkeys(buf, getstate)
1691 call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)
1692
1693 " Halfway a mapping
1694 call term_sendkeys(buf, ":call RunTimer()\<CR>;")
1695 call term_wait(buf, 50)
1696 call term_sendkeys(buf, ";")
1697 call term_sendkeys(buf, getstate)
1698 call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)
1699
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001700 " Insert mode completion (bit slower on Mac)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001701 call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02001702 call term_wait(buf, 200)
Bram Moolenaarc2585492019-09-22 21:29:53 +02001703 call term_sendkeys(buf, "\<Esc>")
1704 call term_sendkeys(buf, getstate)
1705 call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)
1706
1707 " Autocommand executing
1708 call term_sendkeys(buf, ":set filetype=foobar\<CR>")
1709 call term_wait(buf, 50)
1710 call term_sendkeys(buf, getstate)
1711 call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)
1712
1713 " Todo: "w" - waiting for ch_evalexpr()
1714
1715 " messages scrolled
1716 call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
1717 call term_wait(buf, 50)
1718 call term_sendkeys(buf, "\<CR>")
1719 call term_sendkeys(buf, getstate)
1720 call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)
1721
1722 call StopVimInTerminal(buf)
1723 call delete('XState')
1724endfunc