blob: d598966977c8c410b94c3fd5b99d35f05f9e6b71 [file] [log] [blame]
Bram Moolenaar08243d22017-01-10 16:12:29 +01001" Tests for various functions.
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002
Bram Moolenaarf1c118b2018-09-03 22:08:10 +02003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarc2585492019-09-22 21:29:53 +02005source term_util.vim
Bram Moolenaar4132eb52020-02-14 16:53:00 +01006source screendump.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00007import './vim9.vim' as v9
Bram Moolenaar08243d22017-01-10 16:12:29 +01008
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +01009" Must be done first, since the alternate buffer must be unset.
10func Test_00_bufexists()
11 call assert_equal(0, bufexists('does_not_exist'))
12 call assert_equal(1, bufexists(bufnr('%')))
13 call assert_equal(0, bufexists(0))
14 new Xfoo
15 let bn = bufnr('%')
16 call assert_equal(1, bufexists(bn))
17 call assert_equal(1, bufexists('Xfoo'))
18 call assert_equal(1, bufexists(getcwd() . '/Xfoo'))
19 call assert_equal(1, bufexists(0))
20 bw
21 call assert_equal(0, bufexists(bn))
22 call assert_equal(0, bufexists('Xfoo'))
23endfunc
24
Bram Moolenaar79296512020-03-22 16:17:14 +010025func Test_has()
26 call assert_equal(1, has('eval'))
27 call assert_equal(1, has('eval', 1))
28
Bram Moolenaar0e05de42020-03-25 22:23:46 +010029 if has('unix')
30 call assert_equal(1, or(has('ttyin'), 1))
31 call assert_equal(0, and(has('ttyout'), 0))
32 call assert_equal(1, has('multi_byte_encoding'))
Bram Moolenaar80adaa82023-07-07 18:57:40 +010033 call assert_equal(0, has(':tearoff'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010034 endif
Bram Moolenaar99fa7212020-04-26 15:59:55 +020035 call assert_equal(1, has('vcon', 1))
36 call assert_equal(1, has('mouse_gpm_enabled', 1))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010037
Bram Moolenaar80adaa82023-07-07 18:57:40 +010038 call assert_equal(has('gui_win32') && has('menu'), has(':tearoff'))
39
Bram Moolenaar79296512020-03-22 16:17:14 +010040 call assert_equal(0, has('nonexistent'))
41 call assert_equal(0, has('nonexistent', 1))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010042
43 " Will we ever have patch 9999?
44 let ver = 'patch-' .. v:version / 100 .. '.' .. v:version % 100 .. '.9999'
45 call assert_equal(0, has(ver))
Bram Moolenaarb0375d42022-06-30 11:03:39 +010046
47 " There actually isn't a patch 9.0.0, but this is more consistent.
48 call assert_equal(1, has('patch-9.0.0'))
Bram Moolenaar79296512020-03-22 16:17:14 +010049endfunc
50
Bram Moolenaar24c2e482017-01-29 15:45:12 +010051func Test_empty()
52 call assert_equal(1, empty(''))
53 call assert_equal(0, empty('a'))
54
55 call assert_equal(1, empty(0))
56 call assert_equal(1, empty(-0))
57 call assert_equal(0, empty(1))
58 call assert_equal(0, empty(-1))
59
Bram Moolenaar73e28dc2022-09-17 21:08:33 +010060 call assert_equal(1, empty(0.0))
61 call assert_equal(1, empty(-0.0))
62 call assert_equal(0, empty(1.0))
63 call assert_equal(0, empty(-1.0))
64 call assert_equal(0, empty(1.0/0.0))
65 call assert_equal(0, empty(0.0/0.0))
Bram Moolenaar24c2e482017-01-29 15:45:12 +010066
67 call assert_equal(1, empty([]))
68 call assert_equal(0, empty(['a']))
69
70 call assert_equal(1, empty({}))
71 call assert_equal(0, empty({'a':1}))
72
73 call assert_equal(1, empty(v:null))
74 call assert_equal(1, empty(v:none))
75 call assert_equal(1, empty(v:false))
76 call assert_equal(0, empty(v:true))
77
Bram Moolenaar41042f32017-03-09 12:09:32 +010078 if has('channel')
79 call assert_equal(1, empty(test_null_channel()))
80 endif
81 if has('job')
82 call assert_equal(1, empty(test_null_job()))
83 endif
84
Bram Moolenaar24c2e482017-01-29 15:45:12 +010085 call assert_equal(0, empty(function('Test_empty')))
Bram Moolenaar17aca702019-05-16 22:24:55 +020086 call assert_equal(0, empty(function('Test_empty', [0])))
Bram Moolenaar7c215c52020-02-29 13:43:27 +010087
Bram Moolenaar097c5372023-05-24 21:02:24 +010088 call assert_fails("call empty(test_void())", ['E340:', 'E685:'])
89 call assert_fails("call empty(test_unknown())", ['E340:', 'E685:'])
Bram Moolenaar24c2e482017-01-29 15:45:12 +010090endfunc
91
Bram Moolenaar80adaa82023-07-07 18:57:40 +010092func Test_err_teapot()
93 call assert_fails('call err_teapot()', "E418: I'm a teapot")
94 call assert_fails('call err_teapot(0)', "E418: I'm a teapot")
95 call assert_fails('call err_teapot(v:false)', "E418: I'm a teapot")
96
97 call assert_fails('call err_teapot("1")', "E503: Coffee is currently not available")
98 call assert_fails('call err_teapot(v:true)', "E503: Coffee is currently not available")
99 let expr = 1
100 call assert_fails('call err_teapot(expr)', "E503: Coffee is currently not available")
101endfunc
102
Bram Moolenaardd589232020-02-29 17:38:12 +0100103func Test_test_void()
Bram Moolenaar61a417b2021-06-15 22:54:28 +0200104 call assert_fails('echo 1 == test_void()', 'E1031:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100105 call assert_fails('echo 1.0 == test_void()', 'E1031:')
Bram Moolenaar097c5372023-05-24 21:02:24 +0100106 call assert_fails('let x = json_encode(test_void())', ['E340:', 'E685:'])
107 call assert_fails('let x = copy(test_void())', ['E340:', 'E685:'])
Bram Moolenaar61a417b2021-06-15 22:54:28 +0200108 call assert_fails('let x = copy([test_void()])', 'E1031:')
Bram Moolenaardd589232020-02-29 17:38:12 +0100109endfunc
110
Bram Moolenaar1840a7b2021-07-13 20:32:29 +0200111func Test_islocked()
112 call assert_fails('call islocked(99)', 'E475:')
113 call assert_fails('call islocked("s: x")', 'E488:')
114endfunc
115
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100116func Test_len()
117 call assert_equal(1, len(0))
118 call assert_equal(2, len(12))
119
120 call assert_equal(0, len(''))
121 call assert_equal(2, len('ab'))
122
123 call assert_equal(0, len([]))
Bram Moolenaar08f41572020-04-20 16:50:00 +0200124 call assert_equal(0, len(test_null_list()))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100125 call assert_equal(2, len([2, 1]))
126
127 call assert_equal(0, len({}))
Bram Moolenaar08f41572020-04-20 16:50:00 +0200128 call assert_equal(0, len(test_null_dict()))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100129 call assert_equal(2, len({'a': 1, 'b': 2}))
130
131 call assert_fails('call len(v:none)', 'E701:')
132 call assert_fails('call len({-> 0})', 'E701:')
133endfunc
134
135func Test_max()
136 call assert_equal(0, max([]))
137 call assert_equal(2, max([2]))
138 call assert_equal(2, max([1, 2]))
139 call assert_equal(2, max([1, 2, v:null]))
140
141 call assert_equal(0, max({}))
142 call assert_equal(2, max({'a':1, 'b':2}))
143
144 call assert_fails('call max(1)', 'E712:')
145 call assert_fails('call max(v:none)', 'E712:')
Bram Moolenaarab65fc72021-02-04 22:07:16 +0100146
147 " check we only get one error
148 call assert_fails('call max([#{}, [1]])', ['E728:', 'E728:'])
149 call assert_fails('call max(#{a: {}, b: [1]})', ['E728:', 'E728:'])
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100150endfunc
151
152func Test_min()
153 call assert_equal(0, min([]))
154 call assert_equal(2, min([2]))
155 call assert_equal(1, min([1, 2]))
156 call assert_equal(0, min([1, 2, v:null]))
157
158 call assert_equal(0, min({}))
159 call assert_equal(1, min({'a':1, 'b':2}))
160
161 call assert_fails('call min(1)', 'E712:')
162 call assert_fails('call min(v:none)', 'E712:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200163 call assert_fails('call min([1, {}])', 'E728:')
Bram Moolenaarab65fc72021-02-04 22:07:16 +0100164
165 " check we only get one error
166 call assert_fails('call min([[1], #{}])', ['E745:', 'E745:'])
167 call assert_fails('call min(#{a: [1], b: #{}})', ['E745:', 'E745:'])
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100168endfunc
169
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200170func Test_strwidth()
171 for aw in ['single', 'double']
172 exe 'set ambiwidth=' . aw
173 call assert_equal(0, strwidth(''))
174 call assert_equal(1, strwidth("\t"))
175 call assert_equal(3, strwidth('Vim'))
176 call assert_equal(4, strwidth(1234))
177 call assert_equal(5, strwidth(-1234))
178
Bram Moolenaar30276f22019-01-24 17:59:39 +0100179 call assert_equal(2, strwidth('😉'))
180 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
181 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200182
183 call assert_fails('call strwidth({->0})', 'E729:')
184 call assert_fails('call strwidth([])', 'E730:')
185 call assert_fails('call strwidth({})', 'E731:')
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200186 endfor
187
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100188 call assert_equal(3, strwidth(1.2))
189 call v9.CheckDefAndScriptFailure(['echo strwidth(1.2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1'])
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200190
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200191 set ambiwidth&
192endfunc
193
Bram Moolenaar08243d22017-01-10 16:12:29 +0100194func Test_str2nr()
195 call assert_equal(0, str2nr(''))
196 call assert_equal(1, str2nr('1'))
197 call assert_equal(1, str2nr(' 1 '))
198
199 call assert_equal(1, str2nr('+1'))
200 call assert_equal(1, str2nr('+ 1'))
201 call assert_equal(1, str2nr(' + 1 '))
202
203 call assert_equal(-1, str2nr('-1'))
204 call assert_equal(-1, str2nr('- 1'))
205 call assert_equal(-1, str2nr(' - 1 '))
206
207 call assert_equal(123456789, str2nr('123456789'))
208 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100209
210 call assert_equal(5, str2nr('101', 2))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200211 call assert_equal(5, '0b101'->str2nr(2))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100212 call assert_equal(5, str2nr('0B101', 2))
213 call assert_equal(-5, str2nr('-101', 2))
214 call assert_equal(-5, str2nr('-0b101', 2))
215 call assert_equal(-5, str2nr('-0B101', 2))
216
217 call assert_equal(65, str2nr('101', 8))
218 call assert_equal(65, str2nr('0101', 8))
219 call assert_equal(-65, str2nr('-101', 8))
220 call assert_equal(-65, str2nr('-0101', 8))
Bram Moolenaarc17e66c2020-06-02 21:38:22 +0200221 call assert_equal(65, str2nr('0o101', 8))
222 call assert_equal(65, str2nr('0O0101', 8))
223 call assert_equal(-65, str2nr('-0O101', 8))
224 call assert_equal(-65, str2nr('-0o0101', 8))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100225
226 call assert_equal(11259375, str2nr('abcdef', 16))
227 call assert_equal(11259375, str2nr('ABCDEF', 16))
228 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
229 call assert_equal(11259375, str2nr('0xabcdef', 16))
230 call assert_equal(11259375, str2nr('0Xabcdef', 16))
231 call assert_equal(11259375, str2nr('0XABCDEF', 16))
232 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
233
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200234 call assert_equal(1, str2nr("1'000'000", 10, 0))
235 call assert_equal(256, str2nr("1'0000'0000", 2, 1))
236 call assert_equal(262144, str2nr("1'000'000", 8, 1))
237 call assert_equal(1000000, str2nr("1'000'000", 10, 1))
Bram Moolenaarea8dcf82019-09-15 21:12:22 +0200238 call assert_equal(1000, str2nr("1'000''000", 10, 1))
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200239 call assert_equal(65536, str2nr("1'00'00", 16, 1))
240
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100241 call assert_equal(0, str2nr('0x10'))
242 call assert_equal(0, str2nr('0b10'))
Bram Moolenaarc17e66c2020-06-02 21:38:22 +0200243 call assert_equal(0, str2nr('0o10'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100244 call assert_equal(1, str2nr('12', 2))
245 call assert_equal(1, str2nr('18', 8))
246 call assert_equal(1, str2nr('1g', 16))
247
248 call assert_equal(0, str2nr(v:null))
249 call assert_equal(0, str2nr(v:none))
250
251 call assert_fails('call str2nr([])', 'E730:')
252 call assert_fails('call str2nr({->2})', 'E729:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100253 call assert_equal(1, str2nr(1.2))
254 call v9.CheckDefAndScriptFailure(['echo str2nr(1.2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1'])
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200255 call assert_fails('call str2nr(10, [])', 'E745:')
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100256endfunc
257
258func Test_strftime()
Bram Moolenaar10455d42019-11-21 15:36:18 +0100259 CheckFunction strftime
260
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100261 " Format of strftime() depends on system. We assume
262 " that basic formats tested here are available and
263 " identical on all systems which support strftime().
264 "
265 " The 2nd parameter of strftime() is a local time, so the output day
266 " of strftime() can be 17 or 18, depending on timezone.
267 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
268 "
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200269 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 +0100270
271 call assert_fails('call strftime([])', 'E730:')
272 call assert_fails('call strftime("%Y", [])', 'E745:')
Bram Moolenaardb517302019-06-18 22:53:24 +0200273
274 " Check that the time changes after we change the timezone
275 " Save previous timezone value, if any
276 if exists('$TZ')
277 let tz = $TZ
278 endif
279
280 " Force EST and then UTC, save the current hour (24-hour clock) for each
281 let $TZ = 'EST' | let est = strftime('%H')
282 let $TZ = 'UTC' | let utc = strftime('%H')
283
284 " Those hours should be two bytes long, and should not be the same; if they
285 " are, a tzset(3) call may have failed somewhere
286 call assert_equal(strlen(est), 2)
287 call assert_equal(strlen(utc), 2)
Bram Moolenaar87652a72019-06-18 23:07:37 +0200288 " TODO: this fails on MS-Windows
289 if has('unix')
290 call assert_notequal(est, utc)
291 endif
Bram Moolenaardb517302019-06-18 22:53:24 +0200292
293 " If we cached a timezone value, put it back, otherwise clear it
294 if exists('tz')
295 let $TZ = tz
296 else
297 unlet $TZ
298 endif
Bram Moolenaar10455d42019-11-21 15:36:18 +0100299endfunc
Bram Moolenaardb517302019-06-18 22:53:24 +0200300
Bram Moolenaar10455d42019-11-21 15:36:18 +0100301func Test_strptime()
302 CheckFunction strptime
Christian Brabandt983d8082023-09-10 19:06:09 +0200303 CheckNotBSD
Bram Moolenaar10455d42019-11-21 15:36:18 +0100304
305 if exists('$TZ')
306 let tz = $TZ
307 endif
308 let $TZ = 'UTC'
309
Bram Moolenaar9a838fe2019-12-06 12:45:01 +0100310 call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
Bram Moolenaar10455d42019-11-21 15:36:18 +0100311
Bram Moolenaarea1233f2020-06-10 16:54:13 +0200312 " Force DST and check that it's considered
313 let $TZ = 'WINTER0SUMMER,J1,J365'
314 call assert_equal(1484653763 - 3600, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
315
Bram Moolenaar10455d42019-11-21 15:36:18 +0100316 call assert_fails('call strptime()', 'E119:')
317 call assert_fails('call strptime("xxx")', 'E119:')
Christian Brabandte5f7cd02023-09-10 19:25:26 +0200318 " This fails on BSD 14 and returns
Christian Brabandt983d8082023-09-10 19:06:09 +0200319 " -2209078800 instead of 0
Bram Moolenaar10455d42019-11-21 15:36:18 +0100320 call assert_equal(0, strptime("%Y", ''))
321 call assert_equal(0, strptime("%Y", "xxx"))
322
323 if exists('tz')
324 let $TZ = tz
325 else
326 unlet $TZ
327 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100328endfunc
329
Bram Moolenaardce1e892019-02-10 23:18:53 +0100330func Test_resolve_unix()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200331 CheckUnix
Bram Moolenaar26109902018-10-06 15:43:17 +0200332
333 " Xlink1 -> Xlink2
334 " Xlink2 -> Xlink3
335 silent !ln -s -f Xlink2 Xlink1
336 silent !ln -s -f Xlink3 Xlink2
337 call assert_equal('Xlink3', resolve('Xlink1'))
338 call assert_equal('./Xlink3', resolve('./Xlink1'))
339 call assert_equal('Xlink3/', resolve('Xlink2/'))
340 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
341 "call assert_equal('Xlink3/', resolve('Xlink1/'))
342 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
343 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
344 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
345
346 " Test resolve() with a symlink cycle.
347 " Xlink1 -> Xlink2
348 " Xlink2 -> Xlink3
349 " Xlink3 -> Xlink1
350 silent !ln -s -f Xlink1 Xlink3
351 call assert_fails('call resolve("Xlink1")', 'E655:')
352 call assert_fails('call resolve("./Xlink1")', 'E655:')
353 call assert_fails('call resolve("Xlink2")', 'E655:')
354 call assert_fails('call resolve("Xlink3")', 'E655:')
355 call delete('Xlink1')
356 call delete('Xlink2')
357 call delete('Xlink3')
358
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100359 silent !ln -s -f Xresolvedir//Xfile Xresolvelink
360 call assert_equal('Xresolvedir/Xfile', resolve('Xresolvelink'))
361 call delete('Xresolvelink')
Bram Moolenaar26109902018-10-06 15:43:17 +0200362
363 silent !ln -s -f Xlink2/ Xlink1
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200364 call assert_equal('Xlink2', 'Xlink1'->resolve())
Bram Moolenaar26109902018-10-06 15:43:17 +0200365 call assert_equal('Xlink2/', resolve('Xlink1/'))
366 call delete('Xlink1')
367
368 silent !ln -s -f ./Xlink2 Xlink1
369 call assert_equal('Xlink2', resolve('Xlink1'))
370 call assert_equal('./Xlink2', resolve('./Xlink1'))
371 call delete('Xlink1')
Bram Moolenaar50c4e9e2020-10-05 20:38:06 +0200372
373 call assert_equal('/', resolve('/'))
Bram Moolenaar26109902018-10-06 15:43:17 +0200374endfunc
375
Bram Moolenaardce1e892019-02-10 23:18:53 +0100376func s:normalize_fname(fname)
377 let ret = substitute(a:fname, '\', '/', 'g')
378 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200379 return ret->tolower()
Bram Moolenaardce1e892019-02-10 23:18:53 +0100380endfunc
381
382func Test_resolve_win32()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200383 CheckMSWindows
Bram Moolenaardce1e892019-02-10 23:18:53 +0100384
385 " test for shortcut file
386 if executable('cscript')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100387 new Xresfile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100388 wq
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200389 let lines =<< trim END
390 Set fs = CreateObject("Scripting.FileSystemObject")
391 Set ws = WScript.CreateObject("WScript.Shell")
392 Set shortcut = ws.CreateShortcut("Xlink.lnk")
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100393 shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xresfile")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200394 shortcut.Save
395 END
396 call writefile(lines, 'link.vbs')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100397 silent !cscript link.vbs
398 call delete('link.vbs')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100399 call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink.lnk')))
400 call delete('Xresfile')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100401
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100402 call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink.lnk')))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100403 call delete('Xlink.lnk')
404 else
405 echomsg 'skipped test for shortcut file'
406 endif
407
408 " remove files
409 call delete('Xlink')
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100410 call delete('Xdir', 'd')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100411 call delete('Xresfile')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100412
413 " test for symbolic link to a file
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100414 new Xresfile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100415 wq
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100416 call assert_equal('Xresfile', resolve('Xresfile'))
417 silent !mklink Xlink Xresfile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100418 if !v:shell_error
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100419 call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink')))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100420 call delete('Xlink')
421 else
422 echomsg 'skipped test for symbolic link to a file'
423 endif
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100424 call delete('Xresfile')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100425
426 " test for junction to a directory
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100427 call mkdir('Xdir')
428 silent !mklink /J Xlink Xdir
Bram Moolenaardce1e892019-02-10 23:18:53 +0100429 if !v:shell_error
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100430 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100431
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100432 call delete('Xdir', 'd')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100433
434 " test for junction already removed
435 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
436 call delete('Xlink')
437 else
438 echomsg 'skipped test for junction to a directory'
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100439 call delete('Xdir', 'd')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100440 endif
441
442 " test for symbolic link to a directory
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100443 call mkdir('Xdir')
444 silent !mklink /D Xlink Xdir
Bram Moolenaardce1e892019-02-10 23:18:53 +0100445 if !v:shell_error
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100446 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100447
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100448 call delete('Xdir', 'd')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100449
450 " test for symbolic link already removed
451 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
452 call delete('Xlink')
453 else
454 echomsg 'skipped test for symbolic link to a directory'
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100455 call delete('Xdir', 'd')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100456 endif
457
458 " test for buffer name
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100459 new Xbuffile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100460 wq
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100461 silent !mklink Xlink Xbuffile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100462 if !v:shell_error
463 edit Xlink
464 call assert_equal('Xlink', bufname('%'))
465 call delete('Xlink')
466 bw!
467 else
468 echomsg 'skipped test for buffer name'
469 endif
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100470 call delete('Xbuffile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200471
472 " test for reparse point
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100473 call mkdir('Xdir')
474 call assert_equal('Xdir', resolve('Xdir'))
475 silent !mklink /D Xdirlink Xdir
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200476 if !v:shell_error
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100477 w Xdir/text.txt
478 call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
479 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
480 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200481 call delete('Xdirlink')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200482 else
483 echomsg 'skipped test for reparse point'
484 endif
485
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100486 call delete('Xdir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100487endfunc
488
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100489func Test_simplify()
490 call assert_equal('', simplify(''))
491 call assert_equal('/', simplify('/'))
492 call assert_equal('/', simplify('/.'))
493 call assert_equal('/', simplify('/..'))
494 call assert_equal('/...', simplify('/...'))
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +0200495 call assert_equal('//path', simplify('//path'))
Bram Moolenaarc70222d2020-06-15 23:18:12 +0200496 if has('unix')
497 call assert_equal('/path', simplify('///path'))
498 call assert_equal('/path', simplify('////path'))
499 endif
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +0200500
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200501 call assert_equal('./dir/file', './dir/file'->simplify())
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100502 call assert_equal('./dir/file', simplify('.///dir//file'))
503 call assert_equal('./dir/file', simplify('./dir/./file'))
504 call assert_equal('./file', simplify('./dir/../file'))
505 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
506 call assert_equal('./file', simplify('dir/.././file'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200507 call assert_equal('../dir', simplify('./../dir'))
508 call assert_equal('..', simplify('../testdir/..'))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100509 call mkdir('Xsimpdir')
510 call assert_equal('.', simplify('Xsimpdir/../.'))
511 call delete('Xsimpdir', 'd')
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100512
513 call assert_fails('call simplify({->0})', 'E729:')
514 call assert_fails('call simplify([])', 'E730:')
515 call assert_fails('call simplify({})', 'E731:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100516 call assert_equal('1.2', simplify(1.2))
517 call v9.CheckDefAndScriptFailure(['echo simplify(1.2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1'])
Bram Moolenaar08243d22017-01-10 16:12:29 +0100518endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100519
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200520func Test_pathshorten()
521 call assert_equal('', pathshorten(''))
522 call assert_equal('foo', pathshorten('foo'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200523 call assert_equal('/foo', '/foo'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200524 call assert_equal('f/', pathshorten('foo/'))
525 call assert_equal('f/bar', pathshorten('foo/bar'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200526 call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200527 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
528 call assert_equal('.f/bar', pathshorten('.foo/bar'))
529 call assert_equal('~f/bar', pathshorten('~foo/bar'))
530 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
531 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
532 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200533 call assert_fails('call pathshorten([])', 'E730:')
Bram Moolenaar6a33ef02020-09-25 22:42:48 +0200534
535 " test pathshorten with optional variable to set preferred size of shortening
536 call assert_equal('', pathshorten('', 2))
537 call assert_equal('foo', pathshorten('foo', 2))
538 call assert_equal('/foo', pathshorten('/foo', 2))
539 call assert_equal('fo/', pathshorten('foo/', 2))
540 call assert_equal('fo/bar', pathshorten('foo/bar', 2))
541 call assert_equal('fo/ba/foobar', pathshorten('foo/bar/foobar', 2))
542 call assert_equal('/fo/ba/foobar', pathshorten('/foo/bar/foobar', 2))
543 call assert_equal('.fo/bar', pathshorten('.foo/bar', 2))
544 call assert_equal('~fo/bar', pathshorten('~foo/bar', 2))
545 call assert_equal('~.fo/bar', pathshorten('~.foo/bar', 2))
546 call assert_equal('.~fo/bar', pathshorten('.~foo/bar', 2))
547 call assert_equal('~/fo/bar', pathshorten('~/foo/bar', 2))
548 call assert_fails('call pathshorten([],2)', 'E730:')
549 call assert_notequal('~/fo/bar', pathshorten('~/foo/bar', 3))
550 call assert_equal('~/foo/bar', pathshorten('~/foo/bar', 3))
551 call assert_equal('~/f/bar', pathshorten('~/foo/bar', 0))
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200552endfunc
553
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100554func Test_strpart()
555 call assert_equal('de', strpart('abcdefg', 3, 2))
556 call assert_equal('ab', strpart('abcdefg', -2, 4))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200557 call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100558 call assert_equal('fg', strpart('abcdefg', 5, 4))
559 call assert_equal('defg', strpart('abcdefg', 3))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100560 call assert_equal('', strpart('abcdefg', 10))
561 call assert_fails("let s=strpart('abcdef', [])", 'E745:')
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100562
Bram Moolenaar30276f22019-01-24 17:59:39 +0100563 call assert_equal('lép', strpart('éléphant', 2, 4))
564 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaar6c53fca2020-08-23 17:34:46 +0200565
566 call assert_equal('é', strpart('éléphant', 0, 1, 1))
567 call assert_equal('ép', strpart('éléphant', 3, 2, v:true))
568 call assert_equal('ó', strpart('cómposed', 1, 1, 1))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100569endfunc
570
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100571func Test_tolower()
572 call assert_equal("", tolower(""))
573
574 " Test with all printable ASCII characters.
575 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
576 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
577
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100578 " Test with a few uppercase diacritics.
579 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
580 call assert_equal("bḃḇ", tolower("BḂḆ"))
581 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
582 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
583 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
584 call assert_equal("fḟ ", tolower("FḞ "))
585 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
586 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
587 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
588 call assert_equal("jĵ", tolower("JĴ"))
589 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
590 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
591 call assert_equal("mḿṁ", tolower("MḾṀ"))
592 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
593 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
594 call assert_equal("pṕṗ", tolower("PṔṖ"))
595 call assert_equal("q", tolower("Q"))
596 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
597 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
598 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
599 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
600 call assert_equal("vṽ", tolower("VṼ"))
601 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
602 call assert_equal("xẋẍ", tolower("XẊẌ"))
603 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
604 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
605
606 " Test with a few lowercase diacritics, which should remain unchanged.
607 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
608 call assert_equal("bḃḇ", tolower("bḃḇ"))
609 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
610 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
611 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
612 call assert_equal("fḟ", tolower("fḟ"))
613 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
614 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
615 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
616 call assert_equal("jĵǰ", tolower("jĵǰ"))
617 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
618 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
619 call assert_equal("mḿṁ ", tolower("mḿṁ "))
620 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
621 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
622 call assert_equal("pṕṗ", tolower("pṕṗ"))
623 call assert_equal("q", tolower("q"))
624 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
625 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
626 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
627 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
628 call assert_equal("vṽ", tolower("vṽ"))
629 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
630 call assert_equal("ẋẍ", tolower("ẋẍ"))
631 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
632 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
633
634 " According to https://twitter.com/jifa/status/625776454479970304
635 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
636 " in length (2 to 3 bytes) when lowercased. So let's test them.
637 call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100638
639 " This call to tolower with invalid utf8 sequence used to cause access to
640 " invalid memory.
641 call tolower("\xC0\x80\xC0")
642 call tolower("123\xC0\x80\xC0")
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200643
644 " Test in latin1 encoding
645 let save_enc = &encoding
646 set encoding=latin1
647 call assert_equal("abc", tolower("ABC"))
648 let &encoding = save_enc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100649endfunc
650
651func Test_toupper()
652 call assert_equal("", toupper(""))
653
654 " Test with all printable ASCII characters.
655 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
656 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
657
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100658 " Test with a few lowercase diacritics.
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200659 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100660 call assert_equal("BḂḆ", toupper("bḃḇ"))
661 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
662 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
663 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
664 call assert_equal("FḞ", toupper("fḟ"))
665 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
666 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
667 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
668 call assert_equal("JĴǰ", toupper("jĵǰ"))
669 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
670 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
671 call assert_equal("MḾṀ ", toupper("mḿṁ "))
672 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
673 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
674 call assert_equal("PṔṖ", toupper("pṕṗ"))
675 call assert_equal("Q", toupper("q"))
676 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
677 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
678 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
679 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
680 call assert_equal("VṼ", toupper("vṽ"))
681 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
682 call assert_equal("ẊẌ", toupper("ẋẍ"))
683 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
684 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
685
686 " Test that uppercase diacritics, which should remain unchanged.
687 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
688 call assert_equal("BḂḆ", toupper("BḂḆ"))
689 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
690 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
691 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
692 call assert_equal("FḞ ", toupper("FḞ "))
693 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
694 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
695 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
696 call assert_equal("JĴ", toupper("JĴ"))
697 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
698 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
699 call assert_equal("MḾṀ", toupper("MḾṀ"))
700 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
701 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
702 call assert_equal("PṔṖ", toupper("PṔṖ"))
703 call assert_equal("Q", toupper("Q"))
704 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
705 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
706 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
707 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
708 call assert_equal("VṼ", toupper("VṼ"))
709 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
710 call assert_equal("XẊẌ", toupper("XẊẌ"))
711 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
712 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
713
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100714 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100715
716 " This call to toupper with invalid utf8 sequence used to cause access to
717 " invalid memory.
718 call toupper("\xC0\x80\xC0")
719 call toupper("123\xC0\x80\xC0")
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200720
721 " Test in latin1 encoding
722 let save_enc = &encoding
723 set encoding=latin1
724 call assert_equal("ABC", toupper("abc"))
725 let &encoding = save_enc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100726endfunc
727
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200728func Test_tr()
729 call assert_equal('foo', tr('bar', 'bar', 'foo'))
730 call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100731 call assert_fails("let s=tr([], 'abc', 'def')", 'E730:')
732 call assert_fails("let s=tr('abc', [], 'def')", 'E730:')
733 call assert_fails("let s=tr('abc', 'abc', [])", 'E730:')
734 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
735 set encoding=latin1
736 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
737 call assert_equal('hEllO', tr('hello', 'eo', 'EO'))
738 call assert_equal('hello', tr('hello', 'xy', 'ab'))
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200739 call assert_fails('call tr("abc", "123", "₁₂")', 'E475:')
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100740 set encoding=utf8
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200741endfunc
742
Bram Moolenaare90858d2017-02-01 17:24:34 +0100743" Tests for the mode() function
744let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100745func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100746 let g:current_modes = mode(0) . '-' . mode(1)
747 return ''
748endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100749
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200750" Test for the mode() function
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100751func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100752 new
753 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
754
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100755 " Only complete from the current buffer.
756 set complete=.
757
Bram Moolenaare90858d2017-02-01 17:24:34 +0100758 inoremap <F2> <C-R>=Save_mode()<CR>
zeertzjqeaf3f362021-07-28 16:51:53 +0200759 xnoremap <F2> <Cmd>call Save_mode()<CR>
Bram Moolenaare90858d2017-02-01 17:24:34 +0100760
761 normal! 3G
762 exe "normal i\<F2>\<Esc>"
763 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100764 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100765 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
766 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100767 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100768 exe "normal iBro\<C-P>\<F2>\<Esc>u"
769 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100770 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100771 exe "normal iBa\<C-X>\<F2>\<Esc>u"
772 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100773 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100774 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
775 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100776 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100777 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
778 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100779 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100780 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
781 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100782 " i_CTRL-X CTRL-L: Multiple matches
783 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
784 call assert_equal('i-ic', g:current_modes)
785 " i_CTRL-X CTRL-L: Single match
786 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
787 call assert_equal('i-ic', g:current_modes)
788 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100789 exe "normal iCom\<C-P>\<F2>\<Esc>u"
790 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100791 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100792 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
793 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100794 " i_CTRL-X CTRL-L: No match
795 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
796 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100797
zeertzjqcc8cd442021-10-03 15:19:14 +0100798 exe "normal R\<F2>\<Esc>"
799 call assert_equal('R-R', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100800 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100801 exe "normal RBa\<C-P>\<F2>\<Esc>u"
802 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100803 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100804 exe "normal RBro\<C-P>\<F2>\<Esc>u"
805 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100806 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100807 exe "normal RBa\<C-X>\<F2>\<Esc>u"
808 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100809 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100810 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
811 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100812 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100813 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
814 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100815 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100816 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
817 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100818 " R_CTRL-X CTRL-L: Multiple matches
819 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
820 call assert_equal('R-Rc', g:current_modes)
821 " R_CTRL-X CTRL-L: Single match
822 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
823 call assert_equal('R-Rc', g:current_modes)
824 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100825 exe "normal RCom\<C-P>\<F2>\<Esc>u"
826 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100827 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100828 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
829 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100830 " R_CTRL-X CTRL-L: No match
831 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
832 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100833
zeertzjqcc8cd442021-10-03 15:19:14 +0100834 exe "normal gR\<F2>\<Esc>"
835 call assert_equal('R-Rv', g:current_modes)
836 " gR_CTRL-P: Multiple matches
837 exe "normal gRBa\<C-P>\<F2>\<Esc>u"
838 call assert_equal('R-Rvc', g:current_modes)
839 " gR_CTRL-P: Single match
840 exe "normal gRBro\<C-P>\<F2>\<Esc>u"
841 call assert_equal('R-Rvc', g:current_modes)
842 " gR_CTRL-X
843 exe "normal gRBa\<C-X>\<F2>\<Esc>u"
844 call assert_equal('R-Rvx', g:current_modes)
845 " gR_CTRL-X CTRL-P: Multiple matches
846 exe "normal gRBa\<C-X>\<C-P>\<F2>\<Esc>u"
847 call assert_equal('R-Rvc', g:current_modes)
848 " gR_CTRL-X CTRL-P: Single match
849 exe "normal gRBro\<C-X>\<C-P>\<F2>\<Esc>u"
850 call assert_equal('R-Rvc', g:current_modes)
851 " gR_CTRL-X CTRL-P + CTRL-P: Single match
852 exe "normal gRBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
853 call assert_equal('R-Rvc', g:current_modes)
854 " gR_CTRL-X CTRL-L: Multiple matches
855 exe "normal gR\<C-X>\<C-L>\<F2>\<Esc>u"
856 call assert_equal('R-Rvc', g:current_modes)
857 " gR_CTRL-X CTRL-L: Single match
858 exe "normal gRBlu\<C-X>\<C-L>\<F2>\<Esc>u"
859 call assert_equal('R-Rvc', g:current_modes)
860 " gR_CTRL-P: No match
861 exe "normal gRCom\<C-P>\<F2>\<Esc>u"
862 call assert_equal('R-Rvc', g:current_modes)
863 " gR_CTRL-X CTRL-P: No match
864 exe "normal gRCom\<C-X>\<C-P>\<F2>\<Esc>u"
865 call assert_equal('R-Rvc', g:current_modes)
866 " gR_CTRL-X CTRL-L: No match
867 exe "normal gRabc\<C-X>\<C-L>\<F2>\<Esc>u"
868 call assert_equal('R-Rvc', g:current_modes)
869
Bram Moolenaara1449832019-09-01 20:16:52 +0200870 call assert_equal('n', 0->mode())
871 call assert_equal('n', 1->mode())
Bram Moolenaare90858d2017-02-01 17:24:34 +0100872
Bram Moolenaar612cc382018-07-29 15:34:26 +0200873 " i_CTRL-O
874 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
875 call assert_equal("n-niI", g:current_modes)
876
877 " R_CTRL-O
878 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
879 call assert_equal("n-niR", g:current_modes)
880
881 " gR_CTRL-O
882 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
883 call assert_equal("n-niV", g:current_modes)
884
Bram Moolenaare90858d2017-02-01 17:24:34 +0100885 " How to test operator-pending mode?
886
887 call feedkeys("v", 'xt')
888 call assert_equal('v', mode())
889 call assert_equal('v', mode(1))
890 call feedkeys("\<Esc>V", 'xt')
891 call assert_equal('V', mode())
892 call assert_equal('V', mode(1))
893 call feedkeys("\<Esc>\<C-V>", 'xt')
894 call assert_equal("\<C-V>", mode())
895 call assert_equal("\<C-V>", mode(1))
896 call feedkeys("\<Esc>", 'xt')
897
898 call feedkeys("gh", 'xt')
899 call assert_equal('s', mode())
900 call assert_equal('s', mode(1))
901 call feedkeys("\<Esc>gH", 'xt')
902 call assert_equal('S', mode())
903 call assert_equal('S', mode(1))
904 call feedkeys("\<Esc>g\<C-H>", 'xt')
905 call assert_equal("\<C-S>", mode())
906 call assert_equal("\<C-S>", mode(1))
907 call feedkeys("\<Esc>", 'xt')
908
zeertzjqeaf3f362021-07-28 16:51:53 +0200909 " v_CTRL-O
910 exe "normal gh\<C-O>\<F2>\<Esc>"
911 call assert_equal("v-vs", g:current_modes)
912 exe "normal gH\<C-O>\<F2>\<Esc>"
913 call assert_equal("V-Vs", g:current_modes)
914 exe "normal g\<C-H>\<C-O>\<F2>\<Esc>"
915 call assert_equal("\<C-V>-\<C-V>s", g:current_modes)
916
Bram Moolenaare90858d2017-02-01 17:24:34 +0100917 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
918 call assert_equal('c-c', g:current_modes)
919 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
920 call assert_equal('c-cv', g:current_modes)
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200921 call feedkeys("Qcall Save_mode()\<CR>vi\<CR>", 'xt')
922 call assert_equal('c-ce', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100923 " How to test Ex mode?
924
naohiro ono75c30e92021-10-19 11:15:41 +0100925 " Test mode in operatorfunc (it used to be Operator-pending).
926 set operatorfunc=OperatorFunc
927 function OperatorFunc(_)
928 call Save_mode()
929 endfunction
930 execute "normal! g@l\<Esc>"
931 call assert_equal('n-n', g:current_modes)
932 execute "normal! i\<C-o>g@l\<Esc>"
933 call assert_equal('n-niI', g:current_modes)
934 execute "normal! R\<C-o>g@l\<Esc>"
935 call assert_equal('n-niR', g:current_modes)
936 execute "normal! gR\<C-o>g@l\<Esc>"
937 call assert_equal('n-niV', g:current_modes)
938
Bram Moolenaar72406a42021-10-02 16:34:55 +0100939 if has('terminal')
940 term
h-east71ebf3b2023-09-03 17:12:55 +0200941 " Terminal-Job mode
942 call assert_equal('t', mode())
943 call assert_equal('t', mode(1))
944 call feedkeys("\<C-W>:echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
945 call assert_equal("c-ct", g:current_modes)
946 call feedkeys("\<Esc>", 'xt')
947
948 " Terminal-Normal mode
Bram Moolenaar72406a42021-10-02 16:34:55 +0100949 call feedkeys("\<C-W>N", 'xt')
950 call assert_equal('n', mode())
951 call assert_equal('nt', mode(1))
h-east71ebf3b2023-09-03 17:12:55 +0200952 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
953 call assert_equal("c-c", g:current_modes)
Bram Moolenaar72406a42021-10-02 16:34:55 +0100954 call feedkeys("aexit\<CR>", 'xt')
955 endif
956
Bram Moolenaare90858d2017-02-01 17:24:34 +0100957 bwipe!
958 iunmap <F2>
zeertzjqeaf3f362021-07-28 16:51:53 +0200959 xunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100960 set complete&
naohiro ono75c30e92021-10-19 11:15:41 +0100961 set operatorfunc&
962 delfunction OperatorFunc
Bram Moolenaare90858d2017-02-01 17:24:34 +0100963endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100964
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200965" Test for append()
Bram Moolenaard2007022019-08-27 21:56:06 +0200966func Test_append()
967 enew!
968 split
Bram Moolenaarcd9c8d42022-11-05 23:46:43 +0000969 call assert_equal(0, append(1, []))
970 call assert_equal(0, append(1, test_null_list()))
971 call assert_equal(0, append(0, ["foo"]))
972 call assert_equal(0, append(1, []))
973 call assert_equal(0, append(1, test_null_list()))
974 call assert_equal(0, append(8, []))
975 call assert_equal(0, append(9, test_null_list()))
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200976 call assert_equal(['foo', ''], getline(1, '$'))
Bram Moolenaard2007022019-08-27 21:56:06 +0200977 split
978 only
979 undo
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200980 undo
Bram Moolenaar08f41572020-04-20 16:50:00 +0200981
982 " Using $ instead of '$' must give an error
983 call assert_fails("call append($, 'foobar')", 'E116:')
Bram Moolenaar801cd352022-10-10 16:08:16 +0100984
985 call assert_fails("call append({}, '')", ['E728:', 'E728:'])
Bram Moolenaard2007022019-08-27 21:56:06 +0200986endfunc
987
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200988" Test for setline()
989func Test_setline()
990 new
991 call setline(0, ["foo"])
992 call setline(0, [])
993 call setline(0, test_null_list())
994 call setline(1, ["bar"])
995 call setline(1, [])
996 call setline(1, test_null_list())
997 call setline(2, [])
998 call setline(2, test_null_list())
999 call setline(3, [])
1000 call setline(3, test_null_list())
1001 call setline(2, ["baz"])
1002 call assert_equal(['bar', 'baz'], getline(1, '$'))
1003 close!
1004endfunc
1005
Bram Moolenaar79518e22017-02-17 16:31:35 +01001006func Test_getbufvar()
1007 let bnr = bufnr('%')
1008 let b:var_num = '1234'
1009 let def_num = '5678'
1010 call assert_equal('1234', getbufvar(bnr, 'var_num'))
1011 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
1012
1013 let bd = getbufvar(bnr, '')
1014 call assert_equal('1234', bd['var_num'])
1015 call assert_true(exists("bd['changedtick']"))
1016 call assert_equal(2, len(bd))
1017
1018 let bd2 = getbufvar(bnr, '', def_num)
1019 call assert_equal(bd, bd2)
1020
1021 unlet b:var_num
1022 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
1023 call assert_equal('', getbufvar(bnr, 'var_num'))
1024
1025 let bd = getbufvar(bnr, '')
1026 call assert_equal(1, len(bd))
1027 let bd = getbufvar(bnr, '',def_num)
1028 call assert_equal(1, len(bd))
1029
Bram Moolenaar4520d442017-03-19 16:09:46 +01001030 call assert_equal('', getbufvar(9999, ''))
1031 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001032 unlet def_num
1033
Bram Moolenaar507647d2017-02-17 16:43:49 +01001034 call assert_equal(0, getbufvar(bnr, '&autoindent'))
1035 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001036
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001037 " Set and get a buffer-local variable
1038 call setbufvar(bnr, 'bufvar_test', ['one', 'two'])
1039 call assert_equal(['one', 'two'], getbufvar(bnr, 'bufvar_test'))
1040
Bram Moolenaar79518e22017-02-17 16:31:35 +01001041 " Open new window with forced option values
1042 set fileformats=unix,dos
1043 new ++ff=dos ++bin ++enc=iso-8859-2
1044 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
1045 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
1046 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
1047 close
1048
Bram Moolenaar52592752020-04-03 18:43:35 +02001049 " Get the b: dict.
1050 let b:testvar = 'one'
1051 new
1052 let b:testvar = 'two'
1053 let thebuf = bufnr()
1054 wincmd w
1055 call assert_equal('two', getbufvar(thebuf, 'testvar'))
1056 call assert_equal('two', getbufvar(thebuf, '').testvar)
1057 bwipe!
1058
Bram Moolenaar79518e22017-02-17 16:31:35 +01001059 set fileformats&
1060endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001061
Bram Moolenaar41042f32017-03-09 12:09:32 +01001062func Test_last_buffer_nr()
1063 call assert_equal(bufnr('$'), last_buffer_nr())
1064endfunc
1065
1066func Test_stridx()
1067 call assert_equal(-1, stridx('', 'l'))
1068 call assert_equal(0, stridx('', ''))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +02001069 call assert_equal(0, 'hello'->stridx(''))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001070 call assert_equal(-1, stridx('hello', 'L'))
1071 call assert_equal(2, stridx('hello', 'l', -1))
1072 call assert_equal(2, stridx('hello', 'l', 0))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +02001073 call assert_equal(2, 'hello'->stridx('l', 1))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001074 call assert_equal(3, stridx('hello', 'l', 3))
1075 call assert_equal(-1, stridx('hello', 'l', 4))
1076 call assert_equal(-1, stridx('hello', 'l', 10))
1077 call assert_equal(2, stridx('hello', 'll'))
1078 call assert_equal(-1, stridx('hello', 'hello world'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001079 call assert_fails("let n=stridx('hello', [])", 'E730:')
1080 call assert_fails("let n=stridx([], 'l')", 'E730:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001081endfunc
1082
1083func Test_strridx()
1084 call assert_equal(-1, strridx('', 'l'))
1085 call assert_equal(0, strridx('', ''))
1086 call assert_equal(5, strridx('hello', ''))
1087 call assert_equal(-1, strridx('hello', 'L'))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +02001088 call assert_equal(3, 'hello'->strridx('l'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001089 call assert_equal(3, strridx('hello', 'l', 10))
1090 call assert_equal(3, strridx('hello', 'l', 3))
1091 call assert_equal(2, strridx('hello', 'l', 2))
1092 call assert_equal(-1, strridx('hello', 'l', 1))
1093 call assert_equal(-1, strridx('hello', 'l', 0))
1094 call assert_equal(-1, strridx('hello', 'l', -1))
1095 call assert_equal(2, strridx('hello', 'll'))
1096 call assert_equal(-1, strridx('hello', 'hello world'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001097 call assert_fails("let n=strridx('hello', [])", 'E730:')
1098 call assert_fails("let n=strridx([], 'l')", 'E730:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001099endfunc
1100
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001101func Test_match_func()
1102 call assert_equal(4, match('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001103 call assert_equal(4, 'testing'->match('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001104 call assert_equal(-1, match('testing', 'ing', 5))
1105 call assert_equal(-1, match('testing', 'ing', 8))
1106 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
1107 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001108 call assert_fails("let x=match('vim', [])", 'E730:')
1109 call assert_equal(3, match(['a', 'b', 'c', 'a'], 'a', 1))
1110 call assert_equal(-1, match(['a', 'b', 'c', 'a'], 'a', 5))
1111 call assert_equal(4, match('testing', 'ing', -1))
1112 call assert_fails("let x=match('testing', 'ing', 0, [])", 'E745:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001113 call assert_equal(-1, match(test_null_list(), 2))
Bram Moolenaar531be472020-09-23 22:38:05 +02001114 call assert_equal(-1, match('abc', '\\%('))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001115endfunc
1116
Bram Moolenaar41042f32017-03-09 12:09:32 +01001117func Test_matchend()
1118 call assert_equal(7, matchend('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001119 call assert_equal(7, 'testing'->matchend('ing', 2))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001120 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001121 call assert_equal(-1, matchend('testing', 'ing', 8))
1122 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
1123 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
1124endfunc
1125
1126func Test_matchlist()
1127 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001128 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001129 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
1130endfunc
1131
1132func Test_matchstr()
1133 call assert_equal('ing', matchstr('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001134 call assert_equal('ing', 'testing'->matchstr('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001135 call assert_equal('', matchstr('testing', 'ing', 5))
1136 call assert_equal('', matchstr('testing', 'ing', 8))
1137 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
1138 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
1139endfunc
1140
1141func Test_matchstrpos()
1142 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001143 call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001144 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
1145 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
1146 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
1147 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001148 call assert_equal(['', -1, -1], matchstrpos(test_null_list(), '\a'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001149endfunc
1150
1151func Test_nextnonblank_prevnonblank()
1152 new
1153insert
1154This
1155
1156
1157is
1158
1159a
1160Test
1161.
1162 call assert_equal(0, nextnonblank(-1))
1163 call assert_equal(0, nextnonblank(0))
1164 call assert_equal(1, nextnonblank(1))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001165 call assert_equal(4, 2->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001166 call assert_equal(4, nextnonblank(3))
1167 call assert_equal(4, nextnonblank(4))
1168 call assert_equal(6, nextnonblank(5))
1169 call assert_equal(6, nextnonblank(6))
1170 call assert_equal(7, nextnonblank(7))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001171 call assert_equal(0, 8->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001172
1173 call assert_equal(0, prevnonblank(-1))
1174 call assert_equal(0, prevnonblank(0))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001175 call assert_equal(1, 1->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001176 call assert_equal(1, prevnonblank(2))
1177 call assert_equal(1, prevnonblank(3))
1178 call assert_equal(4, prevnonblank(4))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001179 call assert_equal(4, 5->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001180 call assert_equal(6, prevnonblank(6))
1181 call assert_equal(7, prevnonblank(7))
1182 call assert_equal(0, prevnonblank(8))
1183 bw!
1184endfunc
1185
1186func Test_byte2line_line2byte()
1187 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +02001188 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +01001189 call setline(1, ['a', 'bc', 'd'])
1190
1191 set fileformat=unix
1192 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
1193 \ map(range(-1, 8), 'byte2line(v:val)'))
1194 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
1195 \ map(range(-1, 5), 'line2byte(v:val)'))
1196
1197 set fileformat=mac
1198 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001199 \ map(range(-1, 8), 'v:val->byte2line()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001200 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
Bram Moolenaar02b31112019-08-31 22:16:38 +02001201 \ map(range(-1, 5), 'v:val->line2byte()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001202
1203 set fileformat=dos
1204 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
1205 \ map(range(-1, 11), 'byte2line(v:val)'))
1206 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
1207 \ map(range(-1, 5), 'line2byte(v:val)'))
1208
Bram Moolenaarc26f7c62018-08-20 22:53:04 +02001209 bw!
1210 set noendofline nofixendofline
1211 normal a-
1212 for ff in ["unix", "mac", "dos"]
1213 let &fileformat = ff
1214 call assert_equal(1, line2byte(1))
1215 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
1216 endfor
1217
1218 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +01001219 bw!
1220endfunc
1221
Christian Brabandt67672ef2023-04-24 21:09:54 +01001222" Test for byteidx() using a character index
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001223func Test_byteidx()
1224 let a = '.é.' " one char of two bytes
1225 call assert_equal(0, byteidx(a, 0))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001226 call assert_equal(1, byteidx(a, 1))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001227 call assert_equal(3, byteidx(a, 2))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001228 call assert_equal(4, byteidx(a, 3))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001229 call assert_equal(-1, byteidx(a, 4))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001230
1231 let b = '.é.' " normal e with composing char
1232 call assert_equal(0, b->byteidx(0))
1233 call assert_equal(1, b->byteidx(1))
1234 call assert_equal(4, b->byteidx(2))
1235 call assert_equal(5, b->byteidx(3))
1236 call assert_equal(-1, b->byteidx(4))
1237
Christian Brabandt67672ef2023-04-24 21:09:54 +01001238 " string with multiple composing characters
1239 let str = '-ą́-ą́'
1240 call assert_equal(0, byteidx(str, 0))
1241 call assert_equal(1, byteidx(str, 1))
1242 call assert_equal(6, byteidx(str, 2))
1243 call assert_equal(7, byteidx(str, 3))
1244 call assert_equal(12, byteidx(str, 4))
1245 call assert_equal(-1, byteidx(str, 5))
1246
1247 " empty string
1248 call assert_equal(0, byteidx('', 0))
1249 call assert_equal(-1, byteidx('', 1))
1250
1251 " error cases
1252 call assert_fails("call byteidx([], 0)", 'E730:')
1253 call assert_fails("call byteidx('abc', [])", 'E745:')
Bram Moolenaare4098452023-05-07 18:53:49 +01001254 call assert_fails("call byteidx('abc', 0, {})", ['E728:', 'E728:'])
zeertzjq8cf51372023-05-08 15:31:38 +01001255 call assert_fails("call byteidx('abc', 0, -1)", ['E1023:', 'E1023:'])
Christian Brabandt67672ef2023-04-24 21:09:54 +01001256endfunc
1257
1258" Test for byteidxcomp() using a character index
1259func Test_byteidxcomp()
1260 let a = '.é.' " one char of two bytes
1261 call assert_equal(0, byteidxcomp(a, 0))
1262 call assert_equal(1, byteidxcomp(a, 1))
1263 call assert_equal(3, byteidxcomp(a, 2))
1264 call assert_equal(4, byteidxcomp(a, 3))
1265 call assert_equal(-1, byteidxcomp(a, 4))
1266
1267 let b = '.é.' " normal e with composing char
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001268 call assert_equal(0, b->byteidxcomp(0))
1269 call assert_equal(1, b->byteidxcomp(1))
1270 call assert_equal(2, b->byteidxcomp(2))
1271 call assert_equal(4, b->byteidxcomp(3))
1272 call assert_equal(5, b->byteidxcomp(4))
1273 call assert_equal(-1, b->byteidxcomp(5))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001274
1275 " string with multiple composing characters
1276 let str = '-ą́-ą́'
1277 call assert_equal(0, byteidxcomp(str, 0))
1278 call assert_equal(1, byteidxcomp(str, 1))
1279 call assert_equal(2, byteidxcomp(str, 2))
1280 call assert_equal(4, byteidxcomp(str, 3))
1281 call assert_equal(6, byteidxcomp(str, 4))
1282 call assert_equal(7, byteidxcomp(str, 5))
1283 call assert_equal(8, byteidxcomp(str, 6))
1284 call assert_equal(10, byteidxcomp(str, 7))
1285 call assert_equal(12, byteidxcomp(str, 8))
1286 call assert_equal(-1, byteidxcomp(str, 9))
1287
1288 " empty string
1289 call assert_equal(0, byteidxcomp('', 0))
1290 call assert_equal(-1, byteidxcomp('', 1))
1291
1292 " error cases
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001293 call assert_fails("call byteidxcomp([], 0)", 'E730:')
Christian Brabandt67672ef2023-04-24 21:09:54 +01001294 call assert_fails("call byteidxcomp('abc', [])", 'E745:')
Bram Moolenaare4098452023-05-07 18:53:49 +01001295 call assert_fails("call byteidxcomp('abc', 0, {})", ['E728:', 'E728:'])
zeertzjq8cf51372023-05-08 15:31:38 +01001296 call assert_fails("call byteidxcomp('abc', 0, -1)", ['E1023:', 'E1023:'])
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001297endfunc
1298
Christian Brabandt67672ef2023-04-24 21:09:54 +01001299" Test for byteidx() using a UTF-16 index
1300func Test_byteidx_from_utf16_index()
1301 " string with single byte characters
1302 let str = "abc"
1303 for i in range(3)
1304 call assert_equal(i, byteidx(str, i, v:true))
1305 endfor
1306 call assert_equal(3, byteidx(str, 3, v:true))
1307 call assert_equal(-1, byteidx(str, 4, v:true))
1308
1309 " string with two byte characters
1310 let str = "a©©b"
1311 call assert_equal(0, byteidx(str, 0, v:true))
1312 call assert_equal(1, byteidx(str, 1, v:true))
1313 call assert_equal(3, byteidx(str, 2, v:true))
1314 call assert_equal(5, byteidx(str, 3, v:true))
1315 call assert_equal(6, byteidx(str, 4, v:true))
1316 call assert_equal(-1, byteidx(str, 5, v:true))
1317
1318 " string with two byte characters
1319 let str = "a😊😊b"
1320 call assert_equal(0, byteidx(str, 0, v:true))
1321 call assert_equal(1, byteidx(str, 1, v:true))
1322 call assert_equal(1, byteidx(str, 2, v:true))
1323 call assert_equal(5, byteidx(str, 3, v:true))
1324 call assert_equal(5, byteidx(str, 4, v:true))
1325 call assert_equal(9, byteidx(str, 5, v:true))
1326 call assert_equal(10, byteidx(str, 6, v:true))
1327 call assert_equal(-1, byteidx(str, 7, v:true))
1328
1329 " string with composing characters
1330 let str = '-á-b́'
1331 call assert_equal(0, byteidx(str, 0, v:true))
1332 call assert_equal(1, byteidx(str, 1, v:true))
1333 call assert_equal(4, byteidx(str, 2, v:true))
1334 call assert_equal(5, byteidx(str, 3, v:true))
1335 call assert_equal(8, byteidx(str, 4, v:true))
1336 call assert_equal(-1, byteidx(str, 5, v:true))
1337
1338 " string with multiple composing characters
1339 let str = '-ą́-ą́'
1340 call assert_equal(0, byteidx(str, 0, v:true))
1341 call assert_equal(1, byteidx(str, 1, v:true))
1342 call assert_equal(6, byteidx(str, 2, v:true))
1343 call assert_equal(7, byteidx(str, 3, v:true))
1344 call assert_equal(12, byteidx(str, 4, v:true))
1345 call assert_equal(-1, byteidx(str, 5, v:true))
1346
1347 " empty string
1348 call assert_equal(0, byteidx('', 0, v:true))
1349 call assert_equal(-1, byteidx('', 1, v:true))
1350
1351 " error cases
1352 call assert_fails('call byteidx(str, 0, [])', 'E745:')
1353endfunc
1354
1355" Test for byteidxcomp() using a UTF-16 index
1356func Test_byteidxcomp_from_utf16_index()
1357 " string with single byte characters
1358 let str = "abc"
1359 for i in range(3)
1360 call assert_equal(i, byteidxcomp(str, i, v:true))
1361 endfor
1362 call assert_equal(3, byteidxcomp(str, 3, v:true))
1363 call assert_equal(-1, byteidxcomp(str, 4, v:true))
1364
1365 " string with two byte characters
1366 let str = "a©©b"
1367 call assert_equal(0, byteidxcomp(str, 0, v:true))
1368 call assert_equal(1, byteidxcomp(str, 1, v:true))
1369 call assert_equal(3, byteidxcomp(str, 2, v:true))
1370 call assert_equal(5, byteidxcomp(str, 3, v:true))
1371 call assert_equal(6, byteidxcomp(str, 4, v:true))
1372 call assert_equal(-1, byteidxcomp(str, 5, v:true))
1373
1374 " string with two byte characters
1375 let str = "a😊😊b"
1376 call assert_equal(0, byteidxcomp(str, 0, v:true))
1377 call assert_equal(1, byteidxcomp(str, 1, v:true))
1378 call assert_equal(1, byteidxcomp(str, 2, v:true))
1379 call assert_equal(5, byteidxcomp(str, 3, v:true))
1380 call assert_equal(5, byteidxcomp(str, 4, v:true))
1381 call assert_equal(9, byteidxcomp(str, 5, v:true))
1382 call assert_equal(10, byteidxcomp(str, 6, v:true))
1383 call assert_equal(-1, byteidxcomp(str, 7, v:true))
1384
1385 " string with composing characters
1386 let str = '-á-b́'
1387 call assert_equal(0, byteidxcomp(str, 0, v:true))
1388 call assert_equal(1, byteidxcomp(str, 1, v:true))
1389 call assert_equal(2, byteidxcomp(str, 2, v:true))
1390 call assert_equal(4, byteidxcomp(str, 3, v:true))
1391 call assert_equal(5, byteidxcomp(str, 4, v:true))
1392 call assert_equal(6, byteidxcomp(str, 5, v:true))
1393 call assert_equal(8, byteidxcomp(str, 6, v:true))
1394 call assert_equal(-1, byteidxcomp(str, 7, v:true))
1395 call assert_fails('call byteidxcomp(str, 0, [])', 'E745:')
1396
1397 " string with multiple composing characters
1398 let str = '-ą́-ą́'
1399 call assert_equal(0, byteidxcomp(str, 0, v:true))
1400 call assert_equal(1, byteidxcomp(str, 1, v:true))
1401 call assert_equal(2, byteidxcomp(str, 2, v:true))
1402 call assert_equal(4, byteidxcomp(str, 3, v:true))
1403 call assert_equal(6, byteidxcomp(str, 4, v:true))
1404 call assert_equal(7, byteidxcomp(str, 5, v:true))
1405 call assert_equal(8, byteidxcomp(str, 6, v:true))
1406 call assert_equal(10, byteidxcomp(str, 7, v:true))
1407 call assert_equal(12, byteidxcomp(str, 8, v:true))
1408 call assert_equal(-1, byteidxcomp(str, 9, v:true))
1409
1410 " empty string
1411 call assert_equal(0, byteidxcomp('', 0, v:true))
1412 call assert_equal(-1, byteidxcomp('', 1, v:true))
1413
1414 " error cases
1415 call assert_fails('call byteidxcomp(str, 0, [])', 'E745:')
1416endfunc
1417
1418" Test for charidx() using a byte index
Bram Moolenaar17793ef2020-12-28 12:56:58 +01001419func Test_charidx()
1420 let a = 'xáb́y'
1421 call assert_equal(0, charidx(a, 0))
1422 call assert_equal(1, charidx(a, 3))
1423 call assert_equal(2, charidx(a, 4))
1424 call assert_equal(3, charidx(a, 7))
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001425 call assert_equal(4, charidx(a, 8))
1426 call assert_equal(-1, charidx(a, 9))
Dominique Pelle6d37e8e2021-05-06 17:36:55 +02001427 call assert_equal(-1, charidx(a, -1))
Bram Moolenaar17793ef2020-12-28 12:56:58 +01001428
1429 " count composing characters
Christian Brabandt67672ef2023-04-24 21:09:54 +01001430 call assert_equal(0, a->charidx(0, 1))
1431 call assert_equal(2, a->charidx(2, 1))
1432 call assert_equal(3, a->charidx(4, 1))
1433 call assert_equal(5, a->charidx(7, 1))
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001434 call assert_equal(6, a->charidx(8, 1))
1435 call assert_equal(-1, a->charidx(9, 1))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001436
1437 " empty string
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001438 call assert_equal(0, charidx('', 0))
1439 call assert_equal(-1, charidx('', 1))
1440 call assert_equal(0, charidx('', 0, 1))
1441 call assert_equal(-1, charidx('', 1, 1))
Bram Moolenaar17793ef2020-12-28 12:56:58 +01001442
Christian Brabandt67672ef2023-04-24 21:09:54 +01001443 " error cases
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001444 call assert_equal(0, charidx(test_null_string(), 0))
1445 call assert_equal(-1, charidx(test_null_string(), 1))
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01001446 call assert_fails('let x = charidx([], 1)', 'E1174:')
1447 call assert_fails('let x = charidx("abc", [])', 'E1210:')
1448 call assert_fails('let x = charidx("abc", 1, [])', 'E1212:')
1449 call assert_fails('let x = charidx("abc", 1, -1)', 'E1212:')
1450 call assert_fails('let x = charidx("abc", 1, 2)', 'E1212:')
Bram Moolenaar17793ef2020-12-28 12:56:58 +01001451endfunc
1452
Christian Brabandt67672ef2023-04-24 21:09:54 +01001453" Test for charidx() using a UTF-16 index
1454func Test_charidx_from_utf16_index()
1455 " string with single byte characters
1456 let str = "abc"
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001457 for i in range(4)
Christian Brabandt67672ef2023-04-24 21:09:54 +01001458 call assert_equal(i, charidx(str, i, v:false, v:true))
1459 endfor
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001460 call assert_equal(-1, charidx(str, 4, v:false, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001461
1462 " string with two byte characters
1463 let str = "a©©b"
1464 call assert_equal(0, charidx(str, 0, v:false, v:true))
1465 call assert_equal(1, charidx(str, 1, v:false, v:true))
1466 call assert_equal(2, charidx(str, 2, v:false, v:true))
1467 call assert_equal(3, charidx(str, 3, v:false, v:true))
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001468 call assert_equal(4, charidx(str, 4, v:false, v:true))
1469 call assert_equal(-1, charidx(str, 5, v:false, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001470
1471 " string with four byte characters
1472 let str = "a😊😊b"
1473 call assert_equal(0, charidx(str, 0, v:false, v:true))
1474 call assert_equal(1, charidx(str, 1, v:false, v:true))
1475 call assert_equal(1, charidx(str, 2, v:false, v:true))
1476 call assert_equal(2, charidx(str, 3, v:false, v:true))
1477 call assert_equal(2, charidx(str, 4, v:false, v:true))
1478 call assert_equal(3, charidx(str, 5, v:false, v:true))
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001479 call assert_equal(4, charidx(str, 6, v:false, v:true))
1480 call assert_equal(-1, charidx(str, 7, v:false, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001481
1482 " string with composing characters
1483 let str = '-á-b́'
1484 for i in str->strcharlen()->range()
1485 call assert_equal(i, charidx(str, i, v:false, v:true))
1486 endfor
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001487 call assert_equal(4, charidx(str, 4, v:false, v:true))
1488 call assert_equal(-1, charidx(str, 5, v:false, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001489 for i in str->strchars()->range()
1490 call assert_equal(i, charidx(str, i, v:true, v:true))
1491 endfor
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001492 call assert_equal(6, charidx(str, 6, v:true, v:true))
1493 call assert_equal(-1, charidx(str, 7, v:true, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001494
1495 " string with multiple composing characters
1496 let str = '-ą́-ą́'
1497 for i in str->strcharlen()->range()
1498 call assert_equal(i, charidx(str, i, v:false, v:true))
1499 endfor
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001500 call assert_equal(4, charidx(str, 4, v:false, v:true))
1501 call assert_equal(-1, charidx(str, 5, v:false, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001502 for i in str->strchars()->range()
1503 call assert_equal(i, charidx(str, i, v:true, v:true))
1504 endfor
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001505 call assert_equal(8, charidx(str, 8, v:true, v:true))
1506 call assert_equal(-1, charidx(str, 9, v:true, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001507
1508 " empty string
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001509 call assert_equal(0, charidx('', 0, v:false, v:true))
1510 call assert_equal(-1, charidx('', 1, v:false, v:true))
1511 call assert_equal(0, charidx('', 0, v:true, v:true))
1512 call assert_equal(-1, charidx('', 1, v:true, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001513
1514 " error cases
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001515 call assert_equal(0, charidx('', 0, v:false, v:true))
1516 call assert_equal(-1, charidx('', 1, v:false, v:true))
1517 call assert_equal(0, charidx('', 0, v:true, v:true))
1518 call assert_equal(-1, charidx('', 1, v:true, v:true))
1519 call assert_equal(0, charidx(test_null_string(), 0, v:false, v:true))
1520 call assert_equal(-1, charidx(test_null_string(), 1, v:false, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001521 call assert_fails('let x = charidx("abc", 1, v:false, [])', 'E1212:')
1522 call assert_fails('let x = charidx("abc", 1, v:true, [])', 'E1212:')
1523endfunc
1524
1525" Test for utf16idx() using a byte index
1526func Test_utf16idx_from_byteidx()
1527 " UTF-16 index of a string with single byte characters
1528 let str = "abc"
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001529 for i in range(4)
Christian Brabandt67672ef2023-04-24 21:09:54 +01001530 call assert_equal(i, utf16idx(str, i))
1531 endfor
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001532 call assert_equal(-1, utf16idx(str, 4))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001533
1534 " UTF-16 index of a string with two byte characters
1535 let str = 'a©©b'
1536 call assert_equal(0, str->utf16idx(0))
1537 call assert_equal(1, str->utf16idx(1))
1538 call assert_equal(1, str->utf16idx(2))
1539 call assert_equal(2, str->utf16idx(3))
1540 call assert_equal(2, str->utf16idx(4))
1541 call assert_equal(3, str->utf16idx(5))
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001542 call assert_equal(4, str->utf16idx(6))
1543 call assert_equal(-1, str->utf16idx(7))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001544
1545 " UTF-16 index of a string with four byte characters
1546 let str = 'a😊😊b'
1547 call assert_equal(0, utf16idx(str, 0))
Yegappan Lakshmanan95707032023-06-14 13:10:15 +01001548 call assert_equal(1, utf16idx(str, 1))
1549 call assert_equal(1, utf16idx(str, 2))
1550 call assert_equal(1, utf16idx(str, 3))
1551 call assert_equal(1, utf16idx(str, 4))
1552 call assert_equal(3, utf16idx(str, 5))
1553 call assert_equal(3, utf16idx(str, 6))
1554 call assert_equal(3, utf16idx(str, 7))
1555 call assert_equal(3, utf16idx(str, 8))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001556 call assert_equal(5, utf16idx(str, 9))
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001557 call assert_equal(6, utf16idx(str, 10))
1558 call assert_equal(-1, utf16idx(str, 11))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001559
1560 " UTF-16 index of a string with composing characters
1561 let str = '-á-b́'
1562 call assert_equal(0, utf16idx(str, 0))
1563 call assert_equal(1, utf16idx(str, 1))
1564 call assert_equal(1, utf16idx(str, 2))
1565 call assert_equal(1, utf16idx(str, 3))
1566 call assert_equal(2, utf16idx(str, 4))
1567 call assert_equal(3, utf16idx(str, 5))
1568 call assert_equal(3, utf16idx(str, 6))
1569 call assert_equal(3, utf16idx(str, 7))
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001570 call assert_equal(4, utf16idx(str, 8))
1571 call assert_equal(-1, utf16idx(str, 9))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001572 call assert_equal(0, utf16idx(str, 0, v:true))
1573 call assert_equal(1, utf16idx(str, 1, v:true))
1574 call assert_equal(2, utf16idx(str, 2, v:true))
1575 call assert_equal(2, utf16idx(str, 3, v:true))
1576 call assert_equal(3, utf16idx(str, 4, v:true))
1577 call assert_equal(4, utf16idx(str, 5, v:true))
1578 call assert_equal(5, utf16idx(str, 6, v:true))
1579 call assert_equal(5, utf16idx(str, 7, v:true))
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001580 call assert_equal(6, utf16idx(str, 8, v:true))
1581 call assert_equal(-1, utf16idx(str, 9, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001582
1583 " string with multiple composing characters
1584 let str = '-ą́-ą́'
1585 call assert_equal(0, utf16idx(str, 0))
1586 call assert_equal(1, utf16idx(str, 1))
1587 call assert_equal(1, utf16idx(str, 2))
1588 call assert_equal(1, utf16idx(str, 3))
1589 call assert_equal(1, utf16idx(str, 4))
1590 call assert_equal(1, utf16idx(str, 5))
1591 call assert_equal(2, utf16idx(str, 6))
1592 call assert_equal(3, utf16idx(str, 7))
1593 call assert_equal(3, utf16idx(str, 8))
1594 call assert_equal(3, utf16idx(str, 9))
1595 call assert_equal(3, utf16idx(str, 10))
1596 call assert_equal(3, utf16idx(str, 11))
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001597 call assert_equal(4, utf16idx(str, 12))
1598 call assert_equal(-1, utf16idx(str, 13))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001599 call assert_equal(0, utf16idx(str, 0, v:true))
1600 call assert_equal(1, utf16idx(str, 1, v:true))
1601 call assert_equal(2, utf16idx(str, 2, v:true))
1602 call assert_equal(2, utf16idx(str, 3, v:true))
1603 call assert_equal(3, utf16idx(str, 4, v:true))
1604 call assert_equal(3, utf16idx(str, 5, v:true))
1605 call assert_equal(4, utf16idx(str, 6, v:true))
1606 call assert_equal(5, utf16idx(str, 7, v:true))
1607 call assert_equal(6, utf16idx(str, 8, v:true))
1608 call assert_equal(6, utf16idx(str, 9, v:true))
1609 call assert_equal(7, utf16idx(str, 10, v:true))
1610 call assert_equal(7, utf16idx(str, 11, v:true))
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001611 call assert_equal(8, utf16idx(str, 12, v:true))
1612 call assert_equal(-1, utf16idx(str, 13, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001613
1614 " empty string
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001615 call assert_equal(0, utf16idx('', 0))
1616 call assert_equal(-1, utf16idx('', 1))
1617 call assert_equal(0, utf16idx('', 0, v:true))
1618 call assert_equal(-1, utf16idx('', 1, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001619
1620 " error cases
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001621 call assert_equal(0, utf16idx("", 0))
1622 call assert_equal(-1, utf16idx("", 1))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001623 call assert_equal(-1, utf16idx("abc", -1))
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001624 call assert_equal(0, utf16idx(test_null_string(), 0))
1625 call assert_equal(-1, utf16idx(test_null_string(), 1))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001626 call assert_fails('let l = utf16idx([], 0)', 'E1174:')
1627 call assert_fails('let l = utf16idx("ab", [])', 'E1210:')
1628 call assert_fails('let l = utf16idx("ab", 0, [])', 'E1212:')
1629endfunc
1630
1631" Test for utf16idx() using a character index
1632func Test_utf16idx_from_charidx()
1633 let str = "abc"
1634 for i in str->strcharlen()->range()
1635 call assert_equal(i, utf16idx(str, i, v:false, v:true))
1636 endfor
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001637 call assert_equal(3, utf16idx(str, 3, v:false, v:true))
1638 call assert_equal(-1, utf16idx(str, 4, v:false, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001639
1640 " UTF-16 index of a string with two byte characters
1641 let str = "a©©b"
1642 for i in str->strcharlen()->range()
1643 call assert_equal(i, utf16idx(str, i, v:false, v:true))
1644 endfor
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001645 call assert_equal(4, utf16idx(str, 4, v:false, v:true))
1646 call assert_equal(-1, utf16idx(str, 5, v:false, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001647
1648 " UTF-16 index of a string with four byte characters
1649 let str = "a😊😊b"
1650 call assert_equal(0, utf16idx(str, 0, v:false, v:true))
Yegappan Lakshmanan95707032023-06-14 13:10:15 +01001651 call assert_equal(1, utf16idx(str, 1, v:false, v:true))
1652 call assert_equal(3, utf16idx(str, 2, v:false, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001653 call assert_equal(5, utf16idx(str, 3, v:false, v:true))
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001654 call assert_equal(6, utf16idx(str, 4, v:false, v:true))
1655 call assert_equal(-1, utf16idx(str, 5, v:false, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001656
1657 " UTF-16 index of a string with composing characters
1658 let str = '-á-b́'
1659 for i in str->strcharlen()->range()
1660 call assert_equal(i, utf16idx(str, i, v:false, v:true))
1661 endfor
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001662 call assert_equal(4, utf16idx(str, 4, v:false, v:true))
1663 call assert_equal(-1, utf16idx(str, 5, v:false, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001664 for i in str->strchars()->range()
1665 call assert_equal(i, utf16idx(str, i, v:true, v:true))
1666 endfor
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001667 call assert_equal(6, utf16idx(str, 6, v:true, v:true))
1668 call assert_equal(-1, utf16idx(str, 7, v:true, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001669
1670 " string with multiple composing characters
1671 let str = '-ą́-ą́'
1672 for i in str->strcharlen()->range()
1673 call assert_equal(i, utf16idx(str, i, v:false, v:true))
1674 endfor
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001675 call assert_equal(4, utf16idx(str, 4, v:false, v:true))
1676 call assert_equal(-1, utf16idx(str, 5, v:false, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001677 for i in str->strchars()->range()
1678 call assert_equal(i, utf16idx(str, i, v:true, v:true))
1679 endfor
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001680 call assert_equal(8, utf16idx(str, 8, v:true, v:true))
1681 call assert_equal(-1, utf16idx(str, 9, v:true, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001682
1683 " empty string
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001684 call assert_equal(0, utf16idx('', 0, v:false, v:true))
1685 call assert_equal(-1, utf16idx('', 1, v:false, v:true))
1686 call assert_equal(0, utf16idx('', 0, v:true, v:true))
1687 call assert_equal(-1, utf16idx('', 1, v:true, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001688
1689 " error cases
Yegappan Lakshmanan577922b2023-06-08 17:09:45 +01001690 call assert_equal(0, utf16idx(test_null_string(), 0, v:true, v:true))
1691 call assert_equal(-1, utf16idx(test_null_string(), 1, v:true, v:true))
Christian Brabandt67672ef2023-04-24 21:09:54 +01001692 call assert_fails('let l = utf16idx("ab", 0, v:false, [])', 'E1212:')
1693endfunc
1694
1695" Test for strutf16len()
1696func Test_strutf16len()
1697 call assert_equal(3, strutf16len('abc'))
1698 call assert_equal(3, 'abc'->strutf16len(v:true))
1699 call assert_equal(4, strutf16len('a©©b'))
1700 call assert_equal(4, strutf16len('a©©b', v:true))
1701 call assert_equal(6, strutf16len('a😊😊b'))
1702 call assert_equal(6, strutf16len('a😊😊b', v:true))
1703 call assert_equal(4, strutf16len('-á-b́'))
1704 call assert_equal(6, strutf16len('-á-b́', v:true))
1705 call assert_equal(4, strutf16len('-ą́-ą́'))
1706 call assert_equal(8, strutf16len('-ą́-ą́', v:true))
1707 call assert_equal(0, strutf16len(''))
1708
1709 " error cases
1710 call assert_fails('let l = strutf16len([])', 'E1174:')
1711 call assert_fails('let l = strutf16len("a", [])', 'E1212:')
1712 call assert_equal(0, strutf16len(test_null_string()))
1713endfunc
1714
Bram Moolenaar41042f32017-03-09 12:09:32 +01001715func Test_count()
1716 let l = ['a', 'a', 'A', 'b']
1717 call assert_equal(2, count(l, 'a'))
1718 call assert_equal(1, count(l, 'A'))
1719 call assert_equal(1, count(l, 'b'))
1720 call assert_equal(0, count(l, 'B'))
1721
1722 call assert_equal(2, count(l, 'a', 0))
1723 call assert_equal(1, count(l, 'A', 0))
1724 call assert_equal(1, count(l, 'b', 0))
1725 call assert_equal(0, count(l, 'B', 0))
1726
1727 call assert_equal(3, count(l, 'a', 1))
1728 call assert_equal(3, count(l, 'A', 1))
1729 call assert_equal(1, count(l, 'b', 1))
1730 call assert_equal(1, count(l, 'B', 1))
1731 call assert_equal(0, count(l, 'c', 1))
1732
1733 call assert_equal(1, count(l, 'a', 0, 1))
1734 call assert_equal(2, count(l, 'a', 1, 1))
1735 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02001736 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001737
1738 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
1739 call assert_equal(2, count(d, 'a'))
1740 call assert_equal(1, count(d, 'A'))
1741 call assert_equal(1, count(d, 'b'))
1742 call assert_equal(0, count(d, 'B'))
1743
1744 call assert_equal(2, count(d, 'a', 0))
1745 call assert_equal(1, count(d, 'A', 0))
1746 call assert_equal(1, count(d, 'b', 0))
1747 call assert_equal(0, count(d, 'B', 0))
1748
1749 call assert_equal(3, count(d, 'a', 1))
1750 call assert_equal(3, count(d, 'A', 1))
1751 call assert_equal(1, count(d, 'b', 1))
1752 call assert_equal(1, count(d, 'B', 1))
1753 call assert_equal(0, count(d, 'c', 1))
1754
1755 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +02001756
1757 call assert_equal(0, count("foo", "bar"))
1758 call assert_equal(1, count("foo", "oo"))
1759 call assert_equal(2, count("foo", "o"))
1760 call assert_equal(0, count("foo", "O"))
1761 call assert_equal(2, count("foo", "O", 1))
1762 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +01001763 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001764
zeertzjq4f389e72023-08-17 22:10:40 +02001765 call assert_fails('call count(0, 0)', 'E706:')
1766 call assert_fails('call count("", "", {})', ['E728:', 'E728:'])
Bram Moolenaar41042f32017-03-09 12:09:32 +01001767endfunc
1768
1769func Test_changenr()
1770 new Xchangenr
1771 call assert_equal(0, changenr())
1772 norm ifoo
1773 call assert_equal(1, changenr())
1774 set undolevels=10
1775 norm Sbar
1776 call assert_equal(2, changenr())
1777 undo
1778 call assert_equal(1, changenr())
1779 redo
1780 call assert_equal(2, changenr())
1781 bw!
1782 set undolevels&
1783endfunc
1784
1785func Test_filewritable()
1786 new Xfilewritable
1787 write!
1788 call assert_equal(1, filewritable('Xfilewritable'))
1789
1790 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1791 call assert_equal(0, filewritable('Xfilewritable'))
1792
1793 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001794 call assert_equal(1, 'Xfilewritable'->filewritable())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001795
1796 call assert_equal(0, filewritable('doesnotexist'))
1797
Bram Moolenaar70e67252022-09-27 19:34:35 +01001798 call mkdir('Xwritedir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001799 call assert_equal(2, filewritable('Xwritedir'))
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +02001800
Bram Moolenaar41042f32017-03-09 12:09:32 +01001801 call delete('Xfilewritable')
1802 bw!
1803endfunc
1804
Bram Moolenaar82956662018-10-06 15:18:45 +02001805func Test_Executable()
1806 if has('win32')
1807 call assert_equal(1, executable('notepad'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001808 call assert_equal(1, 'notepad.exe'->executable())
Bram Moolenaar82956662018-10-06 15:18:45 +02001809 call assert_equal(0, executable('notepad.exe.exe'))
1810 call assert_equal(0, executable('shell32.dll'))
1811 call assert_equal(0, executable('win.ini'))
Bram Moolenaar95da1362020-05-30 18:37:55 +02001812
1813 " get "notepad" path and remove the leading drive and sep. (ex. 'C:\')
1814 let notepadcmd = exepath('notepad.exe')
1815 let driveroot = notepadcmd[:2]
1816 let notepadcmd = notepadcmd[3:]
1817 new
1818 " check that the relative path works in /
1819 execute 'lcd' driveroot
1820 call assert_equal(1, executable(notepadcmd))
1821 call assert_equal(driveroot .. notepadcmd, notepadcmd->exepath())
1822 bwipe
1823
1824 " create "notepad.bat"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001825 call mkdir('Xnotedir')
1826 let notepadbat = fnamemodify('Xnotedir/notepad.bat', ':p')
Bram Moolenaar95da1362020-05-30 18:37:55 +02001827 call writefile([], notepadbat)
1828 new
1829 " check that the path and the pathext order is valid
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001830 lcd Xnotedir
Bram Moolenaar95da1362020-05-30 18:37:55 +02001831 let [pathext, $PATHEXT] = [$PATHEXT, '.com;.exe;.bat;.cmd']
1832 call assert_equal(notepadbat, exepath('notepad'))
1833 let $PATHEXT = pathext
AmberArrf5d0f542023-08-20 20:03:45 +02001834 " check for symbolic link
1835 execute 'silent !mklink np.bat "' .. notepadbat .. '"'
1836 call assert_equal(1, executable('./np.bat'))
1837 call assert_equal(1, executable('./np'))
Bram Moolenaar95da1362020-05-30 18:37:55 +02001838 bwipe
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001839 eval 'Xnotedir'->delete('rf')
Bram Moolenaar82956662018-10-06 15:18:45 +02001840 elseif has('unix')
Bram Moolenaara4208962019-08-24 20:50:19 +02001841 call assert_equal(1, 'cat'->executable())
Bram Moolenaara05a0d32018-10-07 18:43:05 +02001842 call assert_equal(0, executable('nodogshere'))
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001843
1844 " get "cat" path and remove the leading /
1845 let catcmd = exepath('cat')[1:]
1846 new
Bram Moolenaara4208962019-08-24 20:50:19 +02001847 " check that the relative path works in /
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001848 lcd /
1849 call assert_equal(1, executable(catcmd))
Bram Moolenaara3870832021-01-01 14:20:44 +01001850 let result = catcmd->exepath()
1851 " when using chroot looking for sbin/cat can return bin/cat, that is OK
1852 if catcmd =~ '\<sbin\>' && result =~ '\<bin\>'
1853 call assert_equal('/' .. substitute(catcmd, '\<sbin\>', 'bin', ''), result)
1854 else
Bram Moolenaarbf634a02021-07-31 17:20:04 +02001855 " /bin/cat and /usr/bin/cat may be hard linked, we could get either
1856 let result = substitute(result, '/usr/bin/cat', '/bin/cat', '')
1857 let catcmd = substitute(catcmd, 'usr/bin/cat', 'bin/cat', '')
Bram Moolenaara3870832021-01-01 14:20:44 +01001858 call assert_equal('/' .. catcmd, result)
1859 endif
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001860 bwipe
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001861 else
1862 throw 'Skipped: does not work on this platform'
Bram Moolenaar82956662018-10-06 15:18:45 +02001863 endif
1864endfunc
1865
LemonBoy40fd7e62022-05-05 20:18:16 +01001866func Test_executable_windows_store_apps()
1867 CheckMSWindows
1868
1869 " Windows Store apps install some 'decoy' .exe that require some careful
1870 " handling as they behave similarly to symlinks.
1871 let app_dir = expand("$LOCALAPPDATA\\Microsoft\\WindowsApps")
1872 if !isdirectory(app_dir)
1873 return
1874 endif
1875
1876 let save_path = $PATH
1877 let $PATH = app_dir
1878 " Ensure executable() finds all the app .exes
1879 for entry in readdir(app_dir)
1880 if entry =~ '\.exe$'
1881 call assert_true(executable(entry))
1882 endif
1883 endfor
1884
1885 let $PATH = save_path
1886endfunc
1887
Bram Moolenaar86621892019-03-30 21:51:28 +01001888func Test_executable_longname()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001889 CheckMSWindows
Bram Moolenaar86621892019-03-30 21:51:28 +01001890
Bram Moolenaarf637bce2020-11-23 18:14:56 +01001891 " Create a temporary .bat file with 205 characters in the name.
1892 " Maximum length of a filename (including the path) on MS-Windows is 259
1893 " characters.
1894 " See https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
1895 let len = 259 - getcwd()->len() - 6
1896 if len > 200
1897 let len = 200
1898 endif
1899
1900 let fname = 'X' . repeat('あ', len) . '.bat'
Bram Moolenaar86621892019-03-30 21:51:28 +01001901 call writefile([], fname)
1902 call assert_equal(1, executable(fname))
1903 call delete(fname)
1904endfunc
1905
Bram Moolenaar41042f32017-03-09 12:09:32 +01001906func Test_hostname()
1907 let hostname_vim = hostname()
1908 if has('unix')
1909 let hostname_system = systemlist('uname -n')[0]
1910 call assert_equal(hostname_vim, hostname_system)
1911 endif
1912endfunc
1913
1914func Test_getpid()
1915 " getpid() always returns the same value within a vim instance.
1916 call assert_equal(getpid(), getpid())
1917 if has('unix')
1918 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1919 endif
1920endfunc
1921
1922func Test_hlexists()
1923 call assert_equal(0, hlexists('does_not_exist'))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001924 call assert_equal(0, 'Number'->hlexists())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001925 call assert_equal(0, highlight_exists('does_not_exist'))
1926 call assert_equal(0, highlight_exists('Number'))
1927 syntax on
1928 call assert_equal(0, hlexists('does_not_exist'))
1929 call assert_equal(1, hlexists('Number'))
1930 call assert_equal(0, highlight_exists('does_not_exist'))
1931 call assert_equal(1, highlight_exists('Number'))
1932 syntax off
1933endfunc
1934
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001935" Test for the col() function
Bram Moolenaar41042f32017-03-09 12:09:32 +01001936func Test_col()
1937 new
1938 call setline(1, 'abcdef')
1939 norm gg4|mx6|mY2|
1940 call assert_equal(2, col('.'))
1941 call assert_equal(7, col('$'))
Bram Moolenaar8b633132020-03-20 18:20:51 +01001942 call assert_equal(2, col('v'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001943 call assert_equal(4, col("'x"))
1944 call assert_equal(6, col("'Y"))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001945 call assert_equal(2, [1, 2]->col())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001946 call assert_equal(7, col([1, '$']))
1947
1948 call assert_equal(0, col(''))
1949 call assert_equal(0, col('x'))
1950 call assert_equal(0, col([2, '$']))
1951 call assert_equal(0, col([1, 100]))
1952 call assert_equal(0, col([1]))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001953 call assert_equal(0, col(test_null_list()))
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +00001954 call assert_fails('let c = col({})', 'E1222:')
1955 call assert_fails('let c = col(".", [])', 'E1210:')
Bram Moolenaar8b633132020-03-20 18:20:51 +01001956
1957 " test for getting the visual start column
1958 func T()
1959 let g:Vcol = col('v')
1960 return ''
1961 endfunc
1962 let g:Vcol = 0
1963 xmap <expr> <F2> T()
1964 exe "normal gg3|ve\<F2>"
1965 call assert_equal(3, g:Vcol)
1966 xunmap <F2>
1967 delfunc T
1968
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001969 " Test for the visual line start and end marks '< and '>
1970 call setline(1, ['one', 'one two', 'one two three'])
1971 "normal! ggVG
1972 call feedkeys("ggVG\<Esc>", 'xt')
1973 call assert_equal(1, col("'<"))
1974 call assert_equal(14, col("'>"))
1975 " Delete the last line of the visually selected region
1976 $d
1977 call assert_notequal(14, col("'>"))
1978
1979 " Test with 'virtualedit'
1980 set virtualedit=all
1981 call cursor(1, 10)
1982 call assert_equal(4, col('.'))
1983 set virtualedit&
1984
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +00001985 " Test for getting the column number in another window
1986 let winid = win_getid()
1987 new
1988 call win_execute(winid, 'normal 1G$')
1989 call assert_equal(3, col('.', winid))
1990 call win_execute(winid, 'normal 2G')
1991 call assert_equal(8, col('$', winid))
1992 call assert_equal(0, col('.', 5001))
1993
Bram Moolenaar41042f32017-03-09 12:09:32 +01001994 bw!
1995endfunc
1996
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001997" Test for input()
1998func Test_input_func()
1999 " Test for prompt with multiple lines
2000 redir => v
2001 call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt')
2002 redir END
2003 call assert_equal("B", c)
2004 call assert_equal(['A', 'B', 'C'], split(v, "\n"))
2005
2006 " Test for default value
2007 call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt')
2008 call assert_equal('red', c)
2009
2010 " Test for completion at the input prompt
2011 func! Tcomplete(arglead, cmdline, pos)
2012 return "item1\nitem2\nitem3"
2013 endfunc
Bram Moolenaar9d489562020-07-30 20:08:50 +02002014 call feedkeys(":let c = input('Q? ', '', 'custom,Tcomplete')\<CR>"
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002015 \ .. "\<C-A>\<CR>", 'xt')
2016 delfunc Tcomplete
2017 call assert_equal('item1 item2 item3', c)
Bram Moolenaar578fe942020-02-27 21:32:51 +01002018
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +01002019 " Test for using special characters as default input
Bram Moolenaar1f448d92021-03-22 19:37:06 +01002020 call feedkeys(":let c = input('name? ', \"x\\<BS>y\")\<CR>\<CR>", 'xt')
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +01002021 call assert_equal('y', c)
2022
zeertzjqe3a529b2022-06-05 19:01:37 +01002023 " Test for using text with composing characters as default input
2024 call feedkeys(":let c = input('name? ', \"ã̳\")\<CR>\<CR>", 'xt')
2025 call assert_equal('ã̳', c)
2026
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +01002027 " Test for using <CR> as default input
2028 call feedkeys(":let c = input('name? ', \"\\<CR>\")\<CR>x\<CR>", 'xt')
2029 call assert_equal(' x', c)
2030
Bram Moolenaar578fe942020-02-27 21:32:51 +01002031 call assert_fails("call input('F:', '', 'invalid')", 'E180:')
2032 call assert_fails("call input('F:', '', [])", 'E730:')
2033endfunc
2034
2035" Test for the inputdialog() function
2036func Test_inputdialog()
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002037 set timeout timeoutlen=10
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002038 if has('gui_running')
2039 call assert_fails('let v=inputdialog([], "xx")', 'E730:')
2040 call assert_fails('let v=inputdialog("Q", [])', 'E730:')
2041 else
2042 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt')
2043 call assert_equal('xx', v)
2044 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt')
2045 call assert_equal('yy', v)
2046 endif
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002047 set timeout& timeoutlen&
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002048endfunc
2049
2050" Test for inputlist()
Bram Moolenaar947b39e2018-07-22 19:36:37 +02002051func Test_inputlist()
2052 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
2053 call assert_equal(1, c)
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02002054 call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02002055 call assert_equal(2, c)
2056 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
2057 call assert_equal(3, c)
2058
Bram Moolenaareebd5552020-06-10 15:45:57 +02002059 " CR to cancel
2060 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<cr>", 'tx')
2061 call assert_equal(0, c)
2062
2063 " Esc to cancel
2064 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<Esc>", 'tx')
2065 call assert_equal(0, c)
2066
2067 " q to cancel
2068 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>q", 'tx')
2069 call assert_equal(0, c)
2070
=?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?=5cf94572021-05-20 21:14:20 +02002071 " Cancel after inputting a number
2072 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>5q", 'tx')
2073 call assert_equal(0, c)
2074
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02002075 " Use backspace to delete characters in the prompt
2076 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<BS>3\<BS>2\<cr>", 'tx')
2077 call assert_equal(2, c)
2078
2079 " Use mouse to make a selection
2080 call test_setmouse(&lines - 3, 2)
2081 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
2082 call assert_equal(1, c)
2083 " Mouse click outside of the list
2084 call test_setmouse(&lines - 6, 2)
2085 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
2086 call assert_equal(-2, c)
2087
Bram Moolenaar947b39e2018-07-22 19:36:37 +02002088 call assert_fails('call inputlist("")', 'E686:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002089 call assert_fails('call inputlist(test_null_list())', 'E686:')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02002090endfunc
2091
Bram Moolenaar7bdcba02023-01-02 11:59:26 +00002092func Test_range_inputlist()
2093 " flush out any garbage left in the buffer
2094 while getchar(0)
2095 endwhile
2096
2097 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x')
2098 call assert_equal(1, result)
2099 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x')
2100 call assert_equal(1, result)
2101
2102 unlet result
2103endfunc
2104
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002105func Test_balloon_show()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002106 CheckFeature balloon_eval
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002107
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002108 " This won't do anything but must not crash either.
2109 call balloon_show('hi!')
2110 if !has('gui_running')
2111 call balloon_show(range(3))
2112 call balloon_show([])
Bram Moolenaara0107bd2017-03-02 22:48:01 +01002113 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01002114endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01002115
2116func Test_setbufvar_options()
2117 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
zeertzjq49f05242023-02-04 10:58:34 +00002118 " window layout and cursor position.
Bram Moolenaar2c90d512017-03-18 22:35:30 +01002119 call assert_equal(1, winnr('$'))
2120 split dummy_preview
2121 resize 2
2122 set winfixheight winfixwidth
2123 let prev_id = win_getid()
2124
2125 wincmd j
Bram Moolenaarc05d1c02020-09-04 18:38:06 +02002126 let wh = winheight(0)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01002127 let dummy_buf = bufnr('dummy_buf1', v:true)
2128 call setbufvar(dummy_buf, '&buftype', 'nofile')
2129 execute 'belowright vertical split #' . dummy_buf
Bram Moolenaarc05d1c02020-09-04 18:38:06 +02002130 call assert_equal(wh, winheight(0))
Bram Moolenaar2c90d512017-03-18 22:35:30 +01002131 let dum1_id = win_getid()
zeertzjq49f05242023-02-04 10:58:34 +00002132 call setline(1, 'foo')
2133 normal! V$
2134 call assert_equal(4, col('.'))
2135 call setbufvar('dummy_preview', '&buftype', 'nofile')
2136 call assert_equal(4, col('.'))
Bram Moolenaar2c90d512017-03-18 22:35:30 +01002137
2138 wincmd h
Bram Moolenaarc05d1c02020-09-04 18:38:06 +02002139 let wh = winheight(0)
zeertzjq49f05242023-02-04 10:58:34 +00002140 call setline(1, 'foo')
2141 normal! V$
2142 call assert_equal(4, col('.'))
Bram Moolenaar2c90d512017-03-18 22:35:30 +01002143 let dummy_buf = bufnr('dummy_buf2', v:true)
Bram Moolenaar196b4662019-09-06 21:34:30 +02002144 eval 'nofile'->setbufvar(dummy_buf, '&buftype')
zeertzjq49f05242023-02-04 10:58:34 +00002145 call assert_equal(4, col('.'))
Bram Moolenaar2c90d512017-03-18 22:35:30 +01002146 execute 'belowright vertical split #' . dummy_buf
Bram Moolenaarc05d1c02020-09-04 18:38:06 +02002147 call assert_equal(wh, winheight(0))
Bram Moolenaar2c90d512017-03-18 22:35:30 +01002148
2149 bwipe!
2150 call win_gotoid(prev_id)
2151 bwipe!
2152 call win_gotoid(dum1_id)
2153 bwipe!
2154endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02002155
Bram Moolenaardff97e62022-01-24 20:00:55 +00002156func Test_setbufvar_keep_window_title()
2157 CheckRunVimInTerminal
Bram Moolenaara6c09a72022-01-24 22:02:15 +00002158 if !has('title') || empty(&t_ts)
2159 throw "Skipped: can't get/set title"
2160 endif
Bram Moolenaardff97e62022-01-24 20:00:55 +00002161
2162 let lines =<< trim END
Bram Moolenaar14501122022-01-24 22:32:28 +00002163 set title
Bram Moolenaardff97e62022-01-24 20:00:55 +00002164 edit Xa.txt
2165 let g:buf = bufadd('Xb.txt')
2166 inoremap <F2> <C-R>=setbufvar(g:buf, '&autoindent', 1) ?? ''<CR>
2167 END
Bram Moolenaar70e67252022-09-27 19:34:35 +01002168 call writefile(lines, 'Xsetbufvar', 'D')
Bram Moolenaardff97e62022-01-24 20:00:55 +00002169 let buf = RunVimInTerminal('-S Xsetbufvar', {})
Bram Moolenaar3a8ad592022-01-24 22:18:24 +00002170 call WaitForAssert({-> assert_match('Xa.txt', term_gettitle(buf))}, 1000)
Bram Moolenaardff97e62022-01-24 20:00:55 +00002171
2172 call term_sendkeys(buf, "i\<F2>")
2173 call TermWait(buf)
2174 call term_sendkeys(buf, "\<Esc>")
2175 call TermWait(buf)
2176 call assert_match('Xa.txt', term_gettitle(buf))
2177
2178 call StopVimInTerminal(buf)
Bram Moolenaardff97e62022-01-24 20:00:55 +00002179endfunc
2180
Bram Moolenaard4863aa2017-04-07 19:50:12 +02002181func Test_redo_in_nested_functions()
2182 nnoremap g. :set opfunc=Operator<CR>g@
2183 function Operator( type, ... )
2184 let @x = 'XXX'
2185 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
2186 endfunction
2187
2188 function! Apply()
2189 5,6normal! .
2190 endfunction
2191
2192 new
2193 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
2194 1normal g.i"
2195 call assert_equal('some "XXX" text', getline(1))
2196 3,4normal .
2197 call assert_equal('some "XXX" text', getline(3))
2198 call assert_equal('more "XXX" text', getline(4))
2199 call Apply()
2200 call assert_equal('some "XXX" text', getline(5))
2201 call assert_equal('more "XXX" text', getline(6))
2202 bwipe!
2203
2204 nunmap g.
2205 delfunc Operator
2206 delfunc Apply
2207endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02002208
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01002209func Test_trim()
2210 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02002211 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 +01002212 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
2213 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
2214 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
2215 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
2216 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
2217 call assert_equal("RESERVE", trim("你RESERVE好", "你好"))
2218 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好"))
2219 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
2220 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
2221 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
2222 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
2223 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
2224 call assert_equal("", trim("", ""))
2225 call assert_equal("a", trim("a", ""))
2226 call assert_equal("", trim("", "a"))
2227
Bram Moolenaar2245ae12020-05-31 22:20:36 +02002228 call assert_equal("vim", trim(" vim ", " ", 0))
2229 call assert_equal("vim ", trim(" vim ", " ", 1))
2230 call assert_equal(" vim", trim(" vim ", " ", 2))
2231 call assert_fails('eval trim(" vim ", " ", [])', 'E745:')
2232 call assert_fails('eval trim(" vim ", " ", -1)', 'E475:')
2233 call assert_fails('eval trim(" vim ", " ", 3)', 'E475:')
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01002234 call assert_fails('eval trim(" vim ", 0)', 'E1174:')
Bram Moolenaar2245ae12020-05-31 22:20:36 +02002235
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02002236 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01002237 call assert_equal("x", trim(chars . "x" . chars))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002238
2239 call assert_fails('let c=trim([])', 'E730:')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01002240endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002241
2242" Test for reg_recording() and reg_executing()
2243func Test_reg_executing_and_recording()
2244 let s:reg_stat = ''
2245 func s:save_reg_stat()
2246 let s:reg_stat = reg_recording() . ':' . reg_executing()
2247 return ''
2248 endfunc
2249
2250 new
2251 call s:save_reg_stat()
2252 call assert_equal(':', s:reg_stat)
2253 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
2254 call assert_equal('a:', s:reg_stat)
2255 call feedkeys("@a", 'xt')
2256 call assert_equal(':a', s:reg_stat)
2257 call feedkeys("qb@aq", 'xt')
2258 call assert_equal('b:a', s:reg_stat)
2259 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
2260 call assert_equal('":', s:reg_stat)
2261
Bram Moolenaarcce713d2019-03-04 11:40:12 +01002262 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01002263 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01002264 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
2265 func TestFunc() abort
2266 normal! ia
2267 endfunc
2268 call feedkeys("@q", 'xt')
2269 call assert_equal(':q', s:reg_stat)
2270 delfunc TestFunc
2271
Bram Moolenaarf0fab302019-03-05 12:24:10 +01002272 " getchar() command saves and restores reg_executing
2273 map W :call TestFunc()<CR>
2274 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002275 let g:typed = ''
2276 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01002277 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002278 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01002279 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002280 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01002281 endfunc
2282 call feedkeys("@qy", 'xt')
2283 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002284 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01002285 delfunc TestFunc
2286 unmap W
2287 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002288 unlet g:regs
2289
2290 " input() command saves and restores reg_executing
2291 map W :call TestFunc()<CR>
2292 let @q = "W"
2293 let g:typed = ''
2294 let g:regs = []
2295 func TestFunc() abort
2296 let g:regs += [reg_executing()]
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02002297 let g:typed = '?'->input()
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002298 let g:regs += [reg_executing()]
2299 endfunc
2300 call feedkeys("@qy\<CR>", 'xt')
2301 call assert_equal("y", g:typed)
2302 call assert_equal(['q', 'q'], g:regs)
2303 delfunc TestFunc
2304 unmap W
2305 unlet g:typed
2306 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01002307
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02002308 bwipe!
2309 delfunc s:save_reg_stat
2310 unlet s:reg_stat
2311endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02002312
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02002313func Test_inputsecret()
2314 map W :call TestFunc()<CR>
2315 let @q = "W"
2316 let g:typed1 = ''
2317 let g:typed2 = ''
2318 let g:regs = []
2319 func TestFunc() abort
2320 let g:typed1 = '?'->inputsecret()
2321 let g:typed2 = inputsecret('password: ')
2322 endfunc
2323 call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
2324 call assert_equal("something", g:typed1)
2325 call assert_equal("else", g:typed2)
2326 delfunc TestFunc
2327 unmap W
2328 unlet g:typed1
2329 unlet g:typed2
2330endfunc
2331
Bram Moolenaar5d712e42019-09-03 23:37:01 +02002332func Test_getchar()
2333 call feedkeys('a', '')
2334 call assert_equal(char2nr('a'), getchar())
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02002335 call assert_equal(0, getchar(0))
2336 call assert_equal(0, getchar(1))
2337
2338 call feedkeys('a', '')
2339 call assert_equal('a', getcharstr())
2340 call assert_equal('', getcharstr(0))
2341 call assert_equal('', getcharstr(1))
Bram Moolenaar5d712e42019-09-03 23:37:01 +02002342
zeertzjqad6c45f2022-02-20 19:05:10 +00002343 call feedkeys("\<M-F2>", '')
2344 call assert_equal("\<M-F2>", getchar(0))
2345 call assert_equal(0, getchar(0))
2346
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002347 call setline(1, 'xxxx')
Bram Moolenaar5d712e42019-09-03 23:37:01 +02002348 call test_setmouse(1, 3)
2349 let v:mouse_win = 9
2350 let v:mouse_winid = 9
2351 let v:mouse_lnum = 9
2352 let v:mouse_col = 9
2353 call feedkeys("\<S-LeftMouse>", '')
2354 call assert_equal("\<S-LeftMouse>", getchar())
2355 call assert_equal(1, v:mouse_win)
2356 call assert_equal(win_getid(1), v:mouse_winid)
2357 call assert_equal(1, v:mouse_lnum)
2358 call assert_equal(3, v:mouse_col)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002359 enew!
Bram Moolenaar5d712e42019-09-03 23:37:01 +02002360endfunc
2361
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02002362func Test_libcall_libcallnr()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002363 CheckFeature libcall
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02002364
2365 if has('win32')
2366 let libc = 'msvcrt.dll'
2367 elseif has('mac')
2368 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002369 elseif executable('ldd')
2370 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
2371 endif
2372 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02002373 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002374 if has('linux')
2375 " There is not documented but regarding the 1st argument of glibc's
2376 " dlopen an empty string and nullptr are equivalent, so using an empty
2377 " string for the 1st argument of libcall allows to call functions.
2378 let libc = ''
2379 elseif has('sun')
2380 " Set the path to libc.so according to the architecture.
2381 let test_bits = system('file ' . GetVimProg())
2382 let test_arch = system('uname -p')
2383 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
2384 let libc = '/usr/lib/sparcv9/libc.so'
2385 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
2386 let libc = '/usr/lib/amd64/libc.so'
2387 else
2388 let libc = '/usr/lib/libc.so'
2389 endif
2390 else
2391 " Unfortunately skip this test until a good way is found.
2392 return
2393 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02002394 endif
2395
2396 if has('win32')
Bram Moolenaar02b31112019-08-31 22:16:38 +02002397 call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02002398 else
Bram Moolenaar02b31112019-08-31 22:16:38 +02002399 call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02002400 endif
2401
2402 " If function returns NULL, libcall() should return an empty string.
2403 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
2404
2405 " Test libcallnr() with string and integer argument.
Bram Moolenaar02b31112019-08-31 22:16:38 +02002406 call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
2407 call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02002408
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002409 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", ['', 'E364:'])
2410 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", ['', 'E364:'])
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02002411
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002412 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", ['', 'E364:'])
2413 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", ['', 'E364:'])
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02002414endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02002415
2416sandbox function Fsandbox()
2417 normal ix
2418endfunc
2419
2420func Test_func_sandbox()
2421 sandbox let F = {-> 'hello'}
2422 call assert_equal('hello', F())
2423
Bram Moolenaara4208962019-08-24 20:50:19 +02002424 sandbox let F = {-> "normal ix\<Esc>"->execute()}
Bram Moolenaard90a1442018-07-15 20:24:31 +02002425 call assert_fails('call F()', 'E48:')
2426 unlet F
2427
2428 call assert_fails('call Fsandbox()', 'E48:')
2429 delfunc Fsandbox
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01002430
2431 " From a sandbox try to set a predefined variable (which cannot be modified
2432 " from a sandbox)
2433 call assert_fails('sandbox let v:lnum = 10', 'E794:')
Bram Moolenaard90a1442018-07-15 20:24:31 +02002434endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01002435
2436func EditAnotherFile()
2437 let word = expand('<cword>')
2438 edit Xfuncrange2
2439endfunc
2440
2441func Test_func_range_with_edit()
2442 " Define a function that edits another buffer, then call it with a range that
2443 " is invalid in that buffer.
Bram Moolenaar70e67252022-09-27 19:34:35 +01002444 call writefile(['just one line'], 'Xfuncrange2', 'D')
Bram Moolenaar9e353b52018-11-04 23:39:38 +01002445 new
Bram Moolenaar196b4662019-09-06 21:34:30 +02002446 eval 10->range()->setline(1)
Bram Moolenaar9e353b52018-11-04 23:39:38 +01002447 write Xfuncrange1
2448 call assert_fails('5,8call EditAnotherFile()', 'E16:')
2449
2450 call delete('Xfuncrange1')
Bram Moolenaar9e353b52018-11-04 23:39:38 +01002451 bwipe!
2452endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002453
2454func Test_func_exists_on_reload()
Bram Moolenaar70e67252022-09-27 19:34:35 +01002455 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists', 'D')
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002456 call assert_equal(0, exists('*ExistingFunction'))
2457 source Xfuncexists
Bram Moolenaara4208962019-08-24 20:50:19 +02002458 call assert_equal(1, '*ExistingFunction'->exists())
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002459 " Redefining a function when reloading a script is OK.
2460 source Xfuncexists
2461 call assert_equal(1, exists('*ExistingFunction'))
2462
2463 " But redefining in another script is not OK.
Bram Moolenaar70e67252022-09-27 19:34:35 +01002464 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2', 'D')
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002465 call assert_fails('source Xfuncexists2', 'E122:')
2466
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02002467 " Defining a new function from the cmdline should fail if the function is
2468 " already defined
2469 call assert_fails('call feedkeys(":func ExistingFunction()\<CR>", "xt")', 'E122:')
2470
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002471 delfunc ExistingFunction
2472 call assert_equal(0, exists('*ExistingFunction'))
2473 call writefile([
2474 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
2475 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
2476 \ ], 'Xfuncexists')
2477 call assert_fails('source Xfuncexists', 'E122:')
2478 call assert_equal(1, exists('*ExistingFunction'))
2479
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002480 delfunc ExistingFunction
2481endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01002482
2483" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
2484func Test_confirm()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002485 CheckUnix
2486 CheckNotGui
Bram Moolenaar2e050092019-01-27 15:00:36 +01002487
2488 call feedkeys('o', 'L')
2489 let a = confirm('Press O to proceed')
2490 call assert_equal(1, a)
2491
2492 call feedkeys('y', 'L')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02002493 let a = 'Are you sure?'->confirm("&Yes\n&No")
Bram Moolenaar2e050092019-01-27 15:00:36 +01002494 call assert_equal(1, a)
2495
2496 call feedkeys('n', 'L')
2497 let a = confirm('Are you sure?', "&Yes\n&No")
2498 call assert_equal(2, a)
2499
2500 " confirm() should return 0 when pressing CTRL-C.
Bram Moolenaar79296512020-03-22 16:17:14 +01002501 call feedkeys("\<C-C>", 'L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01002502 let a = confirm('Are you sure?', "&Yes\n&No")
2503 call assert_equal(0, a)
2504
2505 " <Esc> requires another character to avoid it being seen as the start of an
2506 " escape sequence. Zero should be harmless.
Bram Moolenaara4208962019-08-24 20:50:19 +02002507 eval "\<Esc>0"->feedkeys('L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01002508 let a = confirm('Are you sure?', "&Yes\n&No")
2509 call assert_equal(0, a)
2510
2511 " Default choice is returned when pressing <CR>.
2512 call feedkeys("\<CR>", 'L')
2513 let a = confirm('Are you sure?', "&Yes\n&No")
2514 call assert_equal(1, a)
2515
2516 call feedkeys("\<CR>", 'L')
2517 let a = confirm('Are you sure?', "&Yes\n&No", 2)
2518 call assert_equal(2, a)
2519
2520 call feedkeys("\<CR>", 'L')
2521 let a = confirm('Are you sure?', "&Yes\n&No", 0)
2522 call assert_equal(0, a)
2523
2524 " Test with the {type} 4th argument
2525 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
2526 call feedkeys('y', 'L')
2527 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
2528 call assert_equal(1, a)
2529 endfor
2530
2531 call assert_fails('call confirm([])', 'E730:')
2532 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
2533 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
2534 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
2535endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002536
2537func Test_platform_name()
2538 " The system matches at most only one name.
Bram Moolenaar041c7102020-05-30 18:14:57 +02002539 let names = ['amiga', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002540 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
2541
2542 " Is Unix?
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002543 call assert_equal(has('bsd'), has('bsd') && has('unix'))
2544 call assert_equal(has('hpux'), has('hpux') && has('unix'))
2545 call assert_equal(has('linux'), has('linux') && has('unix'))
2546 call assert_equal(has('mac'), has('mac') && has('unix'))
2547 call assert_equal(has('qnx'), has('qnx') && has('unix'))
2548 call assert_equal(has('sun'), has('sun') && has('unix'))
2549 call assert_equal(has('win32'), has('win32') && !has('unix'))
2550 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
2551
2552 if has('unix') && executable('uname')
2553 let uname = system('uname')
Bram Moolenaara02e3f62019-02-07 21:27:14 +01002554 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
2555 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002556 call assert_equal(uname =~? 'HP-UX', has('hpux'))
2557 call assert_equal(uname =~? 'Linux', has('linux'))
2558 call assert_equal(uname =~? 'Darwin', has('mac'))
2559 call assert_equal(uname =~? 'QNX', has('qnx'))
2560 call assert_equal(uname =~? 'SunOS', has('sun'))
2561 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
2562 endif
2563endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002564
2565func Test_readdir()
Bram Moolenaar70e67252022-09-27 19:34:35 +01002566 call mkdir('Xreaddir', 'R')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002567 call writefile([], 'Xreaddir/foo.txt')
2568 call writefile([], 'Xreaddir/bar.txt')
2569 call mkdir('Xreaddir/dir')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002570
2571 " All results
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002572 let files = readdir('Xreaddir')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002573 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
2574
2575 " Only results containing "f"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002576 let files = 'Xreaddir'->readdir({ x -> stridx(x, 'f') != -1 })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002577 call assert_equal(['foo.txt'], sort(files))
2578
2579 " Only .txt files
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002580 let files = readdir('Xreaddir', { x -> x =~ '.txt$' })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002581 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
2582
2583 " Only .txt files with string
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002584 let files = readdir('Xreaddir', 'v:val =~ ".txt$"')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002585 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
2586
2587 " Limit to 1 result.
2588 let l = []
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002589 let files = readdir('Xreaddir', {x -> len(add(l, x)) == 2 ? -1 : 1})
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002590 call assert_equal(1, len(files))
2591
Bram Moolenaar27da7de2019-09-03 17:13:37 +02002592 " Nested readdir() must not crash
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002593 let files = readdir('Xreaddir', 'readdir("Xreaddir", "1") != []')
Bram Moolenaar27da7de2019-09-03 17:13:37 +02002594 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002595endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02002596
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002597func Test_readdirex()
Bram Moolenaar70e67252022-09-27 19:34:35 +01002598 call mkdir('Xexdir', 'R')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002599 call writefile(['foo'], 'Xexdir/foo.txt')
2600 call writefile(['barbar'], 'Xexdir/bar.txt')
2601 call mkdir('Xexdir/dir')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002602
2603 " All results
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002604 let files = readdirex('Xexdir')->map({-> v:val.name})
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002605 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002606 let sizes = readdirex('Xexdir')->map({-> v:val.size})
Bram Moolenaar441d60e2020-06-02 22:19:50 +02002607 call assert_equal([0, 4, 7], sort(sizes))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002608
2609 " Only results containing "f"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002610 let files = 'Xexdir'->readdirex({ e -> stridx(e.name, 'f') != -1 })
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002611 \ ->map({-> v:val.name})
2612 call assert_equal(['foo.txt'], sort(files))
2613
2614 " Only .txt files
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002615 let files = readdirex('Xexdir', { e -> e.name =~ '.txt$' })
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002616 \ ->map({-> v:val.name})
2617 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
2618
2619 " Only .txt files with string
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002620 let files = readdirex('Xexdir', 'v:val.name =~ ".txt$"')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002621 \ ->map({-> v:val.name})
2622 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
2623
2624 " Limit to 1 result.
2625 let l = []
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002626 let files = readdirex('Xexdir', {e -> len(add(l, e.name)) == 2 ? -1 : 1})
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002627 \ ->map({-> v:val.name})
2628 call assert_equal(1, len(files))
2629
2630 " Nested readdirex() must not crash
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002631 let files = readdirex('Xexdir', 'readdirex("Xexdir", "1") != []')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002632 \ ->map({-> v:val.name})
2633 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
2634
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002635 " report broken link correctly
Bram Moolenaarab540322020-06-10 15:55:36 +02002636 if has("unix")
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002637 call writefile([], 'Xexdir/abc.txt')
2638 call system("ln -s Xexdir/abc.txt Xexdir/link")
2639 call delete('Xexdir/abc.txt')
2640 let files = readdirex('Xexdir', 'readdirex("Xexdir", "1") != []')
Bram Moolenaarab540322020-06-10 15:55:36 +02002641 \ ->map({-> v:val.name .. '_' .. v:val.type})
2642 call sort(files)->assert_equal(
2643 \ ['bar.txt_file', 'dir_dir', 'foo.txt_file', 'link_link'])
2644 endif
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02002645
2646 call assert_fails('call readdirex("doesnotexist")', 'E484:')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002647endfunc
2648
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002649func Test_readdirex_sort()
2650 CheckUnix
2651 " Skip tests on Mac OS X and Cygwin (does not allow several files with different casing)
2652 if has("osxdarwin") || has("osx") || has("macunix") || has("win32unix")
2653 throw 'Skipped: Test_readdirex_sort on systems that do not allow this using the default filesystem'
2654 endif
2655 let _collate = v:collate
Bram Moolenaar70e67252022-09-27 19:34:35 +01002656 call mkdir('Xsortdir2', 'R')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002657 call writefile(['1'], 'Xsortdir2/README.txt')
2658 call writefile(['2'], 'Xsortdir2/Readme.txt')
2659 call writefile(['3'], 'Xsortdir2/readme.txt')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002660
2661 " 1) default
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002662 let files = readdirex('Xsortdir2')->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002663 let default = copy(files)
2664 call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort using default')
2665
2666 " 2) no sorting
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002667 let files = readdirex('Xsortdir2', 1, #{sort: 'none'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002668 let unsorted = copy(files)
2669 call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], sort(files), 'unsorted')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002670 call assert_fails("call readdirex('Xsortdir2', 1, #{slort: 'none'})", 'E857: Dictionary key "sort" required')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002671
2672 " 3) sort by case (same as default)
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002673 let files = readdirex('Xsortdir2', 1, #{sort: 'case'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002674 call assert_equal(default, files, 'sort by case')
2675
2676 " 4) sort by ignoring case
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002677 let files = readdirex('Xsortdir2', 1, #{sort: 'icase'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002678 call assert_equal(unsorted->sort('i'), files, 'sort by icase')
2679
2680 " 5) Default Collation
2681 let collate = v:collate
2682 lang collate C
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002683 let files = readdirex('Xsortdir2', 1, #{sort: 'collate'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002684 call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort by C collation')
2685
2686 " 6) Collation de_DE
2687 " Switch locale, this may not work on the CI system, if the locale isn't
2688 " available
2689 try
2690 lang collate de_DE
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002691 let files = readdirex('Xsortdir2', 1, #{sort: 'collate'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002692 call assert_equal(['readme.txt', 'Readme.txt', 'README.txt'], files, 'sort by de_DE collation')
2693 catch
2694 throw 'Skipped: de_DE collation is not available'
2695
2696 finally
2697 exe 'lang collate' collate
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002698 endtry
2699endfunc
2700
2701func Test_readdir_sort()
2702 " some more cases for testing sorting for readdirex
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002703 let dir = 'Xsortdir3'
Bram Moolenaar70e67252022-09-27 19:34:35 +01002704 call mkdir(dir, 'R')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002705 call writefile(['1'], dir .. '/README.txt')
2706 call writefile(['2'], dir .. '/Readm.txt')
2707 call writefile(['3'], dir .. '/read.txt')
2708 call writefile(['4'], dir .. '/Z.txt')
2709 call writefile(['5'], dir .. '/a.txt')
2710 call writefile(['6'], dir .. '/b.txt')
2711
2712 " 1) default
2713 let files = readdir(dir)
2714 let default = copy(files)
2715 call assert_equal(default->sort(), files, 'sort using default')
2716
2717 " 2) sort by case (same as default)
2718 let files = readdir(dir, '1', #{sort: 'case'})
2719 call assert_equal(default, files, 'sort using default')
2720
2721 " 3) sort by ignoring case
2722 let files = readdir(dir, '1', #{sort: 'icase'})
2723 call assert_equal(default->sort('i'), files, 'sort by ignoring case')
2724
Bram Moolenaare17f8812020-06-17 20:30:44 +02002725 " 4) collation
2726 let collate = v:collate
2727 lang collate C
2728 let files = readdir(dir, 1, #{sort: 'collate'})
2729 call assert_equal(default->sort(), files, 'sort by C collation')
2730 exe "lang collate" collate
2731
2732 " 5) Errors
Bram Moolenaard83392a2022-09-01 12:22:46 +01002733 call assert_fails('call readdir(dir, 1, 1)', 'E1206:')
Bram Moolenaare17f8812020-06-17 20:30:44 +02002734 call assert_fails('call readdir(dir, 1, #{sorta: 1})')
Bram Moolenaard83392a2022-09-01 12:22:46 +01002735 call assert_fails('call readdir(dir, 1, test_null_dict())', 'E1297:')
2736 call assert_fails('call readdirex(dir, 1, 1)', 'E1206:')
Bram Moolenaare17f8812020-06-17 20:30:44 +02002737 call assert_fails('call readdirex(dir, 1, #{sorta: 1})')
Bram Moolenaard83392a2022-09-01 12:22:46 +01002738 call assert_fails('call readdirex(dir, 1, test_null_dict())', 'E1297:')
Bram Moolenaare17f8812020-06-17 20:30:44 +02002739
2740 " 6) ignore other values in dict
2741 let files = readdir(dir, '1', #{sort: 'c'})
2742 call assert_equal(default, files, 'sort using default2')
2743
2744 " Cleanup
2745 exe "lang collate" collate
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002746endfunc
2747
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02002748func Test_delete_rf()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002749 call mkdir('Xrfdir')
2750 call writefile([], 'Xrfdir/foo.txt')
2751 call writefile([], 'Xrfdir/bar.txt')
2752 call mkdir('Xrfdir/[a-1]') " issue #696
2753 call writefile([], 'Xrfdir/[a-1]/foo.txt')
2754 call writefile([], 'Xrfdir/[a-1]/bar.txt')
2755 call assert_true(filereadable('Xrfdir/foo.txt'))
2756 call assert_true('Xrfdir/[a-1]/foo.txt'->filereadable())
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02002757
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002758 call assert_equal(0, delete('Xrfdir', 'rf'))
2759 call assert_false(filereadable('Xrfdir/foo.txt'))
2760 call assert_false(filereadable('Xrfdir/[a-1]/foo.txt'))
zeertzjq47870032022-04-05 15:31:01 +01002761
2762 if has('unix')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002763 call mkdir('Xrfdir/Xdir2', 'p')
2764 silent !chmod 555 Xrfdir
2765 call assert_equal(-1, delete('Xrfdir/Xdir2', 'rf'))
2766 call assert_equal(-1, delete('Xrfdir', 'rf'))
2767 silent !chmod 755 Xrfdir
2768 call assert_equal(0, delete('Xrfdir', 'rf'))
zeertzjq47870032022-04-05 15:31:01 +01002769 endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02002770endfunc
2771
Bram Moolenaar17aca702019-05-16 22:24:55 +02002772func Test_call()
2773 call assert_equal(3, call('len', [123]))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02002774 call assert_equal(3, 'len'->call([123]))
Bram Moolenaard83392a2022-09-01 12:22:46 +01002775 call assert_fails("call call('len', 123)", 'E1211:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02002776 call assert_equal(0, call('', []))
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002777 call assert_equal(0, call('len', test_null_list()))
Bram Moolenaar17aca702019-05-16 22:24:55 +02002778
2779 function Mylen() dict
2780 return len(self.data)
2781 endfunction
2782 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
Bram Moolenaar64b4d732019-08-22 22:18:17 +02002783 eval mydict.len->call([], mydict)->assert_equal(4)
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002784 call assert_fails("call call('Mylen', [], 0)", 'E1206:')
Bram Moolenaar67322bf2020-12-06 15:03:19 +01002785 call assert_fails('call foo', 'E107:')
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002786
Bram Moolenaar22db0d52021-06-12 12:16:55 +02002787 " These once caused a crash.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002788 call call(test_null_function(), [])
2789 call call(test_null_partial(), [])
Bram Moolenaar22db0d52021-06-12 12:16:55 +02002790 call assert_fails('call test_null_function()()', 'E1192:')
2791 call assert_fails('call test_null_partial()()', 'E117:')
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002792
2793 let lines =<< trim END
2794 let Time = 'localtime'
2795 call Time()
2796 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002797 call v9.CheckScriptFailure(lines, 'E1085:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02002798endfunc
2799
2800func Test_char2nr()
2801 call assert_equal(12354, char2nr('あ', 1))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02002802 call assert_equal(120, 'x'->char2nr())
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002803 set encoding=latin1
2804 call assert_equal(120, 'x'->char2nr())
2805 set encoding=utf-8
Bram Moolenaar17aca702019-05-16 22:24:55 +02002806endfunc
2807
Bram Moolenaar4e4473c2020-08-28 22:24:57 +02002808func Test_charclass()
2809 call assert_equal(0, charclass(' '))
2810 call assert_equal(1, charclass('.'))
2811 call assert_equal(2, charclass('x'))
2812 call assert_equal(3, charclass("\u203c"))
Christian Brabandt72463f82021-07-02 20:19:31 +02002813 " this used to crash vim
2814 call assert_equal(0, "xxx"[-1]->charclass())
Bram Moolenaar4e4473c2020-08-28 22:24:57 +02002815endfunc
2816
Bram Moolenaar17aca702019-05-16 22:24:55 +02002817func Test_eventhandler()
2818 call assert_equal(0, eventhandler())
2819endfunc
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002820
2821func Test_bufadd_bufload()
2822 call assert_equal(0, bufexists('someName'))
2823 let buf = bufadd('someName')
2824 call assert_notequal(0, buf)
2825 call assert_equal(1, bufexists('someName'))
2826 call assert_equal(0, getbufvar(buf, '&buflisted'))
2827 call assert_equal(0, bufloaded(buf))
2828 call bufload(buf)
2829 call assert_equal(1, bufloaded(buf))
2830 call assert_equal([''], getbufline(buf, 1, '$'))
2831
2832 let curbuf = bufnr('')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02002833 eval ['some', 'text']->writefile('XotherName')
Bram Moolenaar073e4b92019-08-18 23:01:56 +02002834 let buf = 'XotherName'->bufadd()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002835 call assert_notequal(0, buf)
Bram Moolenaar073e4b92019-08-18 23:01:56 +02002836 eval 'XotherName'->bufexists()->assert_equal(1)
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002837 call assert_equal(0, getbufvar(buf, '&buflisted'))
2838 call assert_equal(0, bufloaded(buf))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02002839 eval buf->bufload()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002840 call assert_equal(1, bufloaded(buf))
2841 call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
2842 call assert_equal(curbuf, bufnr(''))
2843
Bram Moolenaar892ae722019-06-30 20:33:01 +02002844 let buf1 = bufadd('')
2845 let buf2 = bufadd('')
2846 call assert_notequal(0, buf1)
2847 call assert_notequal(0, buf2)
2848 call assert_notequal(buf1, buf2)
2849 call assert_equal(1, bufexists(buf1))
2850 call assert_equal(1, bufexists(buf2))
2851 call assert_equal(0, bufloaded(buf1))
2852 exe 'bwipe ' .. buf1
2853 call assert_equal(0, bufexists(buf1))
2854 call assert_equal(1, bufexists(buf2))
2855 exe 'bwipe ' .. buf2
2856 call assert_equal(0, bufexists(buf2))
2857
zeertzjq93f72cc2022-08-26 15:34:52 +01002858 " When 'buftype' is "nofile" then bufload() does not read the file.
2859 " Other values too.
2860 for val in [['nofile', 0],
2861 \ ['nowrite', 1],
2862 \ ['acwrite', 1],
2863 \ ['quickfix', 0],
2864 \ ['help', 1],
2865 \ ['terminal', 0],
2866 \ ['prompt', 0],
2867 \ ['popup', 0],
2868 \ ]
2869 bwipe! XotherName
2870 let buf = bufadd('XotherName')
2871 call setbufvar(buf, '&bt', val[0])
2872 call bufload(buf)
2873 call assert_equal(val[1] ? ['some', 'text'] : [''], getbufline(buf, 1, '$'), val[0])
2874 endfor
Bram Moolenaarc3126192022-08-26 12:58:17 +01002875
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002876 bwipe someName
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002877 bwipe XotherName
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002878 call assert_equal(0, bufexists('someName'))
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002879 call delete('XotherName')
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002880endfunc
Bram Moolenaarc2585492019-09-22 21:29:53 +02002881
2882func Test_state()
2883 CheckRunVimInTerminal
2884
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +01002885 let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"
2886
Bram Moolenaarc2585492019-09-22 21:29:53 +02002887 let lines =<< trim END
2888 call setline(1, ['one', 'two', 'three'])
2889 map ;; gg
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02002890 set complete=.
Bram Moolenaarc2585492019-09-22 21:29:53 +02002891 func RunTimer()
2892 call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
2893 endfunc
2894 au Filetype foobar let g:state = state()|let g:mode = mode()
2895 END
2896 call writefile(lines, 'XState')
2897 let buf = RunVimInTerminal('-S XState', #{rows: 6})
2898
2899 " Using a ":" command Vim is busy, thus "S" is returned
2900 call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
2901 call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
2902 call term_sendkeys(buf, ":\<CR>")
2903
2904 " Using a timer callback
2905 call term_sendkeys(buf, ":call RunTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002906 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002907 call term_sendkeys(buf, getstate)
2908 call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)
2909
2910 " Halfway a mapping
2911 call term_sendkeys(buf, ":call RunTimer()\<CR>;")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002912 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002913 call term_sendkeys(buf, ";")
2914 call term_sendkeys(buf, getstate)
2915 call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)
2916
Christian Brabandtee17b6f2023-09-09 11:23:50 +02002917 " An operator is pending
zeertzjq8dabccd2023-08-22 21:22:24 +02002918 call term_sendkeys(buf, ":call RunTimer()\<CR>y")
2919 call TermWait(buf, 25)
2920 call term_sendkeys(buf, "y")
2921 call term_sendkeys(buf, getstate)
2922 call WaitForAssert({-> assert_match('state: oSc; mode: n', term_getline(buf, 6))}, 1000)
2923
2924 " A register was specified
2925 call term_sendkeys(buf, ":call RunTimer()\<CR>\"r")
2926 call TermWait(buf, 25)
2927 call term_sendkeys(buf, "yy")
2928 call term_sendkeys(buf, getstate)
2929 call WaitForAssert({-> assert_match('state: oSc; mode: n', term_getline(buf, 6))}, 1000)
2930
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02002931 " Insert mode completion (bit slower on Mac)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002932 call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002933 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002934 call term_sendkeys(buf, "\<Esc>")
2935 call term_sendkeys(buf, getstate)
2936 call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)
2937
2938 " Autocommand executing
2939 call term_sendkeys(buf, ":set filetype=foobar\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002940 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002941 call term_sendkeys(buf, getstate)
2942 call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)
2943
2944 " Todo: "w" - waiting for ch_evalexpr()
2945
2946 " messages scrolled
2947 call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002948 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002949 call term_sendkeys(buf, "\<CR>")
2950 call term_sendkeys(buf, getstate)
2951 call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)
2952
2953 call StopVimInTerminal(buf)
2954 call delete('XState')
2955endfunc
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002956
2957func Test_range()
2958 " destructuring
2959 let [x, y] = range(2)
2960 call assert_equal([0, 1], [x, y])
2961
2962 " index
2963 call assert_equal(4, range(1, 10)[3])
2964
2965 " add()
2966 call assert_equal([0, 1, 2, 3], add(range(3), 3))
2967 call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3)))
2968 call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3)))
2969
2970 " append()
2971 new
2972 call append('.', range(5))
2973 call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$'))
2974 bwipe!
2975
2976 " appendbufline()
2977 new
2978 call appendbufline(bufnr(''), '.', range(5))
2979 call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$'))
2980 bwipe!
2981
2982 " call()
2983 func TwoArgs(a, b)
2984 return [a:a, a:b]
2985 endfunc
2986 call assert_equal([0, 1], call('TwoArgs', range(2)))
2987
2988 " col()
2989 new
2990 call setline(1, ['foo', 'bar'])
2991 call assert_equal(2, col(range(1, 2)))
2992 bwipe!
2993
2994 " complete()
2995 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>"
2996 " complete_info()
2997 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>"
2998
2999 " copy()
3000 call assert_equal([1, 2, 3], copy(range(1, 3)))
3001
3002 " count()
3003 call assert_equal(0, count(range(0), 3))
3004 call assert_equal(0, count(range(2), 3))
3005 call assert_equal(1, count(range(5), 3))
3006
3007 " cursor()
3008 new
3009 call setline(1, ['aaa', 'bbb', 'ccc'])
3010 call cursor(range(1, 2))
3011 call assert_equal([2, 1], [col('.'), line('.')])
3012 bwipe!
3013
3014 " deepcopy()
3015 call assert_equal([1, 2, 3], deepcopy(range(1, 3)))
3016
3017 " empty()
3018 call assert_true(empty(range(0)))
3019 call assert_false(empty(range(2)))
3020
3021 " execute()
3022 new
3023 call setline(1, ['aaa', 'bbb', 'ccc'])
3024 call execute(range(3))
3025 call assert_equal(2, line('.'))
3026 bwipe!
3027
3028 " extend()
3029 call assert_equal([1, 2, 3, 4], extend([1], range(2, 4)))
3030 call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4)))
3031 call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4]))
3032
3033 " filter()
3034 call assert_equal([1, 3], filter(range(5), 'v:val % 2'))
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01003035 call assert_equal([1, 5, 7, 11, 13], filter(filter(range(15), 'v:val % 2'), 'v:val % 3'))
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003036
3037 " funcref()
3038 call assert_equal([0, 1], funcref('TwoArgs', range(2))())
3039
3040 " function()
3041 call assert_equal([0, 1], function('TwoArgs', range(2))())
3042
3043 " garbagecollect()
3044 let thelist = [1, range(2), 3]
3045 let otherlist = range(3)
3046 call test_garbagecollect_now()
3047
3048 " get()
3049 call assert_equal(4, get(range(1, 10), 3))
3050 call assert_equal(-1, get(range(1, 10), 42, -1))
3051
3052 " index()
3053 call assert_equal(1, index(range(1, 5), 2))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01003054 call assert_fails("echo index([1, 2], 1, [])", 'E745:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003055
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003056 " insert()
3057 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42))
3058 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0))
3059 call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1))
3060 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4))
3061 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1))
3062 call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5))
3063
3064 " join()
3065 call assert_equal('0 1 2 3 4', join(range(5)))
3066
Bram Moolenaar272ca952020-01-28 20:49:11 +01003067 " json_encode()
3068 call assert_equal('[0,1,2,3]', json_encode(range(4)))
3069
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003070 " len()
3071 call assert_equal(0, len(range(0)))
3072 call assert_equal(2, len(range(2)))
3073 call assert_equal(5, len(range(0, 12, 3)))
3074 call assert_equal(4, len(range(3, 0, -1)))
3075
3076 " list2str()
3077 call assert_equal('ABC', list2str(range(65, 67)))
Bram Moolenaard83392a2022-09-01 12:22:46 +01003078 call assert_fails('let s = list2str(5)', 'E1211:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003079
3080 " lock()
3081 let thelist = range(5)
3082 lockvar thelist
3083
3084 " map()
3085 call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2'))
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01003086 call assert_equal([3, 5, 7, 9, 11], map(map(range(5), 'v:val * 2'), 'v:val + 3'))
3087 call assert_equal([2, 6], map(filter(range(5), 'v:val % 2'), 'v:val * 2'))
3088 call assert_equal([2, 4, 8], filter(map(range(5), 'v:val * 2'), 'v:val % 3'))
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003089
3090 " match()
3091 call assert_equal(3, match(range(5), 3))
3092
3093 " matchaddpos()
3094 highlight MyGreenGroup ctermbg=green guibg=green
3095 call matchaddpos('MyGreenGroup', range(line('.'), line('.')))
3096
3097 " matchend()
3098 call assert_equal(4, matchend(range(5), '4'))
3099 call assert_equal(3, matchend(range(1, 5), '4'))
3100 call assert_equal(-1, matchend(range(1, 5), '42'))
3101
3102 " matchstrpos()
3103 call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4'))
3104 call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4'))
3105 call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42'))
3106
3107 " max() reverse()
3108 call assert_equal(0, max(range(0)))
3109 call assert_equal(0, max(range(10, 9)))
3110 call assert_equal(9, max(range(10)))
3111 call assert_equal(18, max(range(0, 20, 3)))
3112 call assert_equal(20, max(range(20, 0, -3)))
3113 call assert_equal(99999, max(range(100000)))
3114 call assert_equal(99999, max(range(99999, 0, -1)))
3115 call assert_equal(99999, max(reverse(range(100000))))
3116 call assert_equal(99999, max(reverse(range(99999, 0, -1))))
3117
3118 " min() reverse()
3119 call assert_equal(0, min(range(0)))
3120 call assert_equal(0, min(range(10, 9)))
3121 call assert_equal(5, min(range(5, 10)))
3122 call assert_equal(5, min(range(5, 10, 3)))
3123 call assert_equal(2, min(range(20, 0, -3)))
3124 call assert_equal(0, min(range(100000)))
3125 call assert_equal(0, min(range(99999, 0, -1)))
3126 call assert_equal(0, min(reverse(range(100000))))
3127 call assert_equal(0, min(reverse(range(99999, 0, -1))))
3128
3129 " remove()
3130 call assert_equal(1, remove(range(1, 10), 0))
3131 call assert_equal(2, remove(range(1, 10), 1))
3132 call assert_equal(9, remove(range(1, 10), 8))
3133 call assert_equal(10, remove(range(1, 10), 9))
3134 call assert_equal(10, remove(range(1, 10), -1))
3135 call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4))
3136
3137 " repeat()
3138 call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2))
3139 call assert_equal([0, 1, 2], repeat(range(3), 1))
3140 call assert_equal([], repeat(range(3), 0))
3141 call assert_equal([], repeat(range(5, 4), 2))
3142 call assert_equal([], repeat(range(5, 4), 0))
3143
3144 " reverse()
3145 call assert_equal([2, 1, 0], reverse(range(3)))
3146 call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1)))
3147 call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10)))
3148 call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20)))
3149 call assert_equal([16, 13, 10], reverse(range(10, 18, 3)))
3150 call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3)))
3151 call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3)))
3152 call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3)))
3153 call assert_equal([], reverse(range(0)))
3154
3155 " TODO: setpos()
3156 " new
3157 " call setline(1, repeat([''], bufnr('')))
3158 " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6))
3159 " call setpos('x', range(bufnr(''), bufnr('') + 3))
3160 " bwipe!
3161
3162 " setreg()
3163 call setreg('a', range(3))
3164 call assert_equal("0\n1\n2\n", getreg('a'))
3165
Bram Moolenaarb0992022020-01-30 14:55:42 +01003166 " settagstack()
3167 call settagstack(1, #{items : range(4)})
Bram Moolenaar94255df2020-02-05 20:10:33 +01003168
Bram Moolenaarb0992022020-01-30 14:55:42 +01003169 " sign_define()
3170 call assert_fails("call sign_define(range(5))", "E715:")
3171 call assert_fails("call sign_placelist(range(5))", "E715:")
3172
3173 " sign_undefine()
3174 call assert_fails("call sign_undefine(range(5))", "E908:")
3175
3176 " sign_unplacelist()
3177 call assert_fails("call sign_unplacelist(range(5))", "E715:")
3178
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003179 " sort()
3180 call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1)))
3181
3182 " string()
3183 call assert_equal('[0, 1, 2, 3, 4]', string(range(5)))
3184
Bram Moolenaarb0992022020-01-30 14:55:42 +01003185 " taglist() with 'tagfunc'
3186 func TagFunc(pattern, flags, info)
3187 return range(10)
3188 endfunc
3189 set tagfunc=TagFunc
3190 call assert_fails("call taglist('asdf')", 'E987:')
3191 set tagfunc=
Bram Moolenaar94255df2020-02-05 20:10:33 +01003192
Bram Moolenaarb0992022020-01-30 14:55:42 +01003193 " term_start()
Bram Moolenaar705724e2020-01-31 21:13:42 +01003194 if has('terminal') && has('termguicolors')
Bram Moolenaarb0992022020-01-30 14:55:42 +01003195 call assert_fails('call term_start(range(3, 4))', 'E474:')
3196 let g:terminal_ansi_colors = range(16)
Bram Moolenaar94255df2020-02-05 20:10:33 +01003197 if has('win32')
3198 let cmd = "cmd /c dir"
3199 else
3200 let cmd = "ls"
3201 endif
LemonBoyb2b3acb2022-05-20 10:10:34 +01003202 call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"'
3203 \ .. ', ansi_colors: range(16)})', 'E475:')
Bram Moolenaarb0855f52022-05-20 10:39:18 +01003204 unlet g:terminal_ansi_colors
Bram Moolenaarb0992022020-01-30 14:55:42 +01003205 endif
3206
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003207 " type()
3208 call assert_equal(v:t_list, type(range(5)))
3209
3210 " uniq()
3211 call assert_equal([0, 1, 2, 3, 4], uniq(range(5)))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01003212
3213 " errors
3214 call assert_fails('let x=range(2, 8, 0)', 'E726:')
3215 call assert_fails('let x=range(3, 1)', 'E727:')
3216 call assert_fails('let x=range(1, 3, -2)', 'E727:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02003217 call assert_fails('let x=range([])', 'E745:')
3218 call assert_fails('let x=range(1, [])', 'E745:')
3219 call assert_fails('let x=range(1, 4, [])', 'E745:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003220endfunc
Bram Moolenaar4132eb52020-02-14 16:53:00 +01003221
Bram Moolenaarb3d83982022-01-27 19:59:47 +00003222func Test_garbagecollect_now_fails()
3223 let v:testing = 0
3224 call assert_fails('call test_garbagecollect_now()', 'E1142:')
3225 let v:testing = 1
3226endfunc
3227
Bram Moolenaar4132eb52020-02-14 16:53:00 +01003228func Test_echoraw()
3229 CheckScreendump
3230
3231 " Normally used for escape codes, but let's test with a CR.
3232 let lines =<< trim END
3233 call echoraw("hello\<CR>x")
3234 END
3235 call writefile(lines, 'XTest_echoraw')
3236 let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40})
3237 call VerifyScreenDump(buf, 'Test_functions_echoraw', {})
3238
3239 " clean up
3240 call StopVimInTerminal(buf)
3241 call delete('XTest_echoraw')
3242endfunc
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01003243
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003244" Test for echo highlighting
3245func Test_echohl()
3246 echohl Search
3247 echo 'Vim'
3248 call assert_equal('Vim', Screenline(&lines))
3249 " TODO: How to check the highlight group used by echohl?
3250 " ScreenAttrs() returns all zeros.
3251 echohl None
3252endfunc
3253
Bram Moolenaar0e05de42020-03-25 22:23:46 +01003254" Test for the eval() function
3255func Test_eval()
3256 call assert_fails("call eval('5 a')", 'E488:')
3257endfunc
3258
zeertzjqcdc83932022-09-12 13:38:41 +01003259" Test for the keytrans() function
3260func Test_keytrans()
3261 call assert_equal('<Space>', keytrans(' '))
3262 call assert_equal('<lt>', keytrans('<'))
3263 call assert_equal('<lt>Tab>', keytrans('<Tab>'))
3264 call assert_equal('<Tab>', keytrans("\<Tab>"))
3265 call assert_equal('<C-V>', keytrans("\<C-V>"))
3266 call assert_equal('<BS>', keytrans("\<BS>"))
3267 call assert_equal('<Home>', keytrans("\<Home>"))
3268 call assert_equal('<C-Home>', keytrans("\<C-Home>"))
3269 call assert_equal('<M-Home>', keytrans("\<M-Home>"))
3270 call assert_equal('<C-Space>', keytrans("\<C-Space>"))
3271 call assert_equal('<M-Space>', keytrans("\<*M-Space>"))
3272 call assert_equal('<M-x>', "\<*M-x>"->keytrans())
3273 call assert_equal('<C-I>', "\<*C-I>"->keytrans())
3274 call assert_equal('<S-3>', "\<*S-3>"->keytrans())
3275 call assert_equal('π', 'π'->keytrans())
3276 call assert_equal('<M-π>', "\<M-π>"->keytrans())
3277 call assert_equal('ě', 'ě'->keytrans())
3278 call assert_equal('<M-ě>', "\<M-ě>"->keytrans())
3279 call assert_equal('', ''->keytrans())
3280 call assert_equal('', test_null_string()->keytrans())
3281 call assert_fails('call keytrans(1)', 'E1174:')
3282 call assert_fails('call keytrans()', 'E119:')
3283endfunc
3284
Bram Moolenaar0e05de42020-03-25 22:23:46 +01003285" Test for the nr2char() function
3286func Test_nr2char()
3287 set encoding=latin1
3288 call assert_equal('@', nr2char(64))
3289 set encoding=utf8
3290 call assert_equal('a', nr2char(97, 1))
3291 call assert_equal('a', nr2char(97, 0))
Bram Moolenaarf7271e82020-05-24 18:45:07 +02003292
zeertzjqdb088872022-05-02 22:53:45 +01003293 call assert_equal("\x80\xfc\b" .. nr2char(0x100000), eval('"\<M-' .. nr2char(0x100000) .. '>"'))
3294 call assert_equal("\x80\xfc\b" .. nr2char(0x40000000), eval('"\<M-' .. nr2char(0x40000000) .. '>"'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01003295endfunc
3296
3297" Test for screenattr(), screenchar() and screenchars() functions
3298func Test_screen_functions()
3299 call assert_equal(-1, screenattr(-1, -1))
3300 call assert_equal(-1, screenchar(-1, -1))
3301 call assert_equal([], screenchars(-1, -1))
zeertzjq47eec672023-06-01 20:26:55 +01003302
3303 " Run this in a separate Vim instance to avoid messing up.
3304 let after =<< trim [CODE]
3305 scriptencoding utf-8
3306 call setline(1, '口')
3307 redraw
3308 call assert_equal(0, screenattr(1, 1))
3309 call assert_equal(char2nr('口'), screenchar(1, 1))
3310 call assert_equal([char2nr('口')], screenchars(1, 1))
3311 call assert_equal('口', screenstring(1, 1))
3312 call writefile(v:errors, 'Xresult')
3313 qall!
3314 [CODE]
3315
3316 let encodings = ['utf-8', 'cp932', 'cp936', 'cp949', 'cp950']
3317 if !has('win32')
3318 let encodings += ['euc-jp']
3319 endif
3320 for enc in encodings
3321 let msg = 'enc=' .. enc
3322 if RunVim([], after, $'--clean --cmd "set encoding={enc}"')
3323 call assert_equal([], readfile('Xresult'), msg)
3324 endif
3325 call delete('Xresult')
3326 endfor
Bram Moolenaar0e05de42020-03-25 22:23:46 +01003327endfunc
3328
Bram Moolenaar08f41572020-04-20 16:50:00 +02003329" Test for getcurpos() and setpos()
3330func Test_getcurpos_setpos()
3331 new
3332 call setline(1, ['012345678', '012345678'])
3333 normal gg6l
3334 let sp = getcurpos()
3335 normal 0
3336 call setpos('.', sp)
3337 normal jyl
3338 call assert_equal('6', @")
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003339 call assert_equal(-1, setpos('.', test_null_list()))
3340 call assert_equal(-1, setpos('.', {}))
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02003341
3342 let winid = win_getid()
3343 normal G$
3344 let pos = getcurpos()
3345 wincmd w
3346 call assert_equal(pos, getcurpos(winid))
3347
3348 wincmd w
Bram Moolenaar08f41572020-04-20 16:50:00 +02003349 close!
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02003350
3351 call assert_equal(getcurpos(), getcurpos(0))
3352 call assert_equal([0, 0, 0, 0, 0], getcurpos(-1))
3353 call assert_equal([0, 0, 0, 0, 0], getcurpos(1999))
Bram Moolenaar08f41572020-04-20 16:50:00 +02003354endfunc
3355
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00003356func Test_getmousepos()
3357 enew!
3358 call setline(1, "\t\t\t1234")
Bram Moolenaar533870a2022-03-13 15:52:44 +00003359 call test_setmouse(1, 1)
3360 call assert_equal(#{
3361 \ screenrow: 1,
3362 \ screencol: 1,
3363 \ winid: win_getid(),
3364 \ winrow: 1,
3365 \ wincol: 1,
3366 \ line: 1,
3367 \ column: 1,
zeertzjqf5a94d52023-10-15 10:03:30 +02003368 \ coladd: 0,
Bram Moolenaar533870a2022-03-13 15:52:44 +00003369 \ }, getmousepos())
zeertzjqb583eda2023-10-14 11:32:28 +02003370 call test_setmouse(1, 2)
3371 call assert_equal(#{
3372 \ screenrow: 1,
3373 \ screencol: 2,
3374 \ winid: win_getid(),
3375 \ winrow: 1,
3376 \ wincol: 2,
3377 \ line: 1,
3378 \ column: 1,
zeertzjqf5a94d52023-10-15 10:03:30 +02003379 \ coladd: 1,
zeertzjqb583eda2023-10-14 11:32:28 +02003380 \ }, getmousepos())
3381 call test_setmouse(1, 8)
3382 call assert_equal(#{
3383 \ screenrow: 1,
3384 \ screencol: 8,
3385 \ winid: win_getid(),
3386 \ winrow: 1,
3387 \ wincol: 8,
3388 \ line: 1,
3389 \ column: 1,
zeertzjqf5a94d52023-10-15 10:03:30 +02003390 \ coladd: 7,
zeertzjqb583eda2023-10-14 11:32:28 +02003391 \ }, getmousepos())
3392 call test_setmouse(1, 9)
3393 call assert_equal(#{
3394 \ screenrow: 1,
3395 \ screencol: 9,
3396 \ winid: win_getid(),
3397 \ winrow: 1,
3398 \ wincol: 9,
3399 \ line: 1,
3400 \ column: 2,
zeertzjqf5a94d52023-10-15 10:03:30 +02003401 \ coladd: 0,
zeertzjqb583eda2023-10-14 11:32:28 +02003402 \ }, getmousepos())
3403 call test_setmouse(1, 12)
3404 call assert_equal(#{
3405 \ screenrow: 1,
3406 \ screencol: 12,
3407 \ winid: win_getid(),
3408 \ winrow: 1,
3409 \ wincol: 12,
3410 \ line: 1,
3411 \ column: 2,
zeertzjqf5a94d52023-10-15 10:03:30 +02003412 \ coladd: 3,
zeertzjqb583eda2023-10-14 11:32:28 +02003413 \ }, getmousepos())
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00003414 call test_setmouse(1, 25)
3415 call assert_equal(#{
3416 \ screenrow: 1,
3417 \ screencol: 25,
3418 \ winid: win_getid(),
3419 \ winrow: 1,
3420 \ wincol: 25,
3421 \ line: 1,
Bram Moolenaar533870a2022-03-13 15:52:44 +00003422 \ column: 4,
zeertzjqf5a94d52023-10-15 10:03:30 +02003423 \ coladd: 0,
3424 \ }, getmousepos())
3425 call test_setmouse(1, 28)
3426 call assert_equal(#{
3427 \ screenrow: 1,
3428 \ screencol: 28,
3429 \ winid: win_getid(),
3430 \ winrow: 1,
3431 \ wincol: 28,
3432 \ line: 1,
3433 \ column: 7,
3434 \ coladd: 0,
3435 \ }, getmousepos())
3436 call test_setmouse(1, 29)
3437 call assert_equal(#{
3438 \ screenrow: 1,
3439 \ screencol: 29,
3440 \ winid: win_getid(),
3441 \ winrow: 1,
3442 \ wincol: 29,
3443 \ line: 1,
3444 \ column: 8,
3445 \ coladd: 0,
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00003446 \ }, getmousepos())
3447 call test_setmouse(1, 50)
3448 call assert_equal(#{
3449 \ screenrow: 1,
3450 \ screencol: 50,
3451 \ winid: win_getid(),
3452 \ winrow: 1,
3453 \ wincol: 50,
3454 \ line: 1,
Bram Moolenaar533870a2022-03-13 15:52:44 +00003455 \ column: 8,
zeertzjqf5a94d52023-10-15 10:03:30 +02003456 \ coladd: 21,
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00003457 \ }, getmousepos())
Sean Dewar10792fe2022-03-15 09:46:54 +00003458
3459 " If the mouse is positioned past the last buffer line, "line" and "column"
3460 " should act like it's positioned on the last buffer line.
3461 call test_setmouse(2, 25)
3462 call assert_equal(#{
3463 \ screenrow: 2,
3464 \ screencol: 25,
3465 \ winid: win_getid(),
3466 \ winrow: 2,
3467 \ wincol: 25,
3468 \ line: 1,
3469 \ column: 4,
zeertzjqf5a94d52023-10-15 10:03:30 +02003470 \ coladd: 0,
Sean Dewar10792fe2022-03-15 09:46:54 +00003471 \ }, getmousepos())
3472 call test_setmouse(2, 50)
3473 call assert_equal(#{
3474 \ screenrow: 2,
3475 \ screencol: 50,
3476 \ winid: win_getid(),
3477 \ winrow: 2,
3478 \ wincol: 50,
3479 \ line: 1,
3480 \ column: 8,
zeertzjqf5a94d52023-10-15 10:03:30 +02003481 \ coladd: 21,
Sean Dewar10792fe2022-03-15 09:46:54 +00003482 \ }, getmousepos())
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00003483 bwipe!
3484endfunc
3485
Bram Moolenaar24dc19c2022-11-14 19:49:15 +00003486func Test_getmouseshape()
3487 CheckFeature mouseshape
3488
3489 call assert_equal('arrow', getmouseshape())
3490endfunc
3491
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003492" Test for glob()
3493func Test_glob()
3494 call assert_equal('', glob(test_null_string()))
3495 call assert_equal('', globpath(test_null_string(), test_null_string()))
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02003496 call assert_fails("let x = globpath(&rtp, 'syntax/c.vim', [])", 'E745:')
Bram Moolenaar1b04ce22020-08-21 22:46:11 +02003497
3498 call writefile([], 'Xglob1')
3499 call writefile([], 'XGLOB2')
3500 set wildignorecase
3501 " Sort output of glob() otherwise we end up with different
3502 " ordering depending on whether file system is case-sensitive.
3503 call assert_equal(['XGLOB2', 'Xglob1'], sort(glob('Xglob[12]', 0, 1)))
LemonBoya3157a42022-04-03 11:58:31 +01003504 " wildignorecase shall be applied even when the pattern contains no wildcards.
3505 call assert_equal('XGLOB2', glob('xglob2'))
Bram Moolenaar1b04ce22020-08-21 22:46:11 +02003506 set wildignorecase&
3507
3508 call delete('Xglob1')
3509 call delete('XGLOB2')
3510
3511 call assert_fails("call glob('*', 0, {})", 'E728:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02003512endfunc
3513
3514" Test for browse()
3515func Test_browse()
3516 CheckFeature browse
3517 call assert_fails('call browse([], "open", "x", "a.c")', 'E745:')
3518endfunc
3519
3520" Test for browsedir()
3521func Test_browsedir()
3522 CheckFeature browse
3523 call assert_fails('call browsedir("open", [])', 'E730:')
3524endfunc
3525
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003526func HasDefault(msg = 'msg')
3527 return a:msg
3528endfunc
3529
3530func Test_default_arg_value()
3531 call assert_equal('msg', HasDefault())
3532endfunc
3533
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003534" Test for gettext()
3535func Test_gettext()
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01003536 call assert_fails('call gettext(1)', 'E1174:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02003537endfunc
3538
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02003539func Test_builtin_check()
3540 call assert_fails('let g:["trim"] = {x -> " " .. x}', 'E704:')
3541 call assert_fails('let g:.trim = {x -> " " .. x}', 'E704:')
Bram Moolenaarb54abee2021-06-02 11:49:23 +02003542 call assert_fails('let l:["trim"] = {x -> " " .. x}', 'E704:')
3543 call assert_fails('let l:.trim = {x -> " " .. x}', 'E704:')
3544 let lines =<< trim END
3545 vim9script
Bram Moolenaar62b191c2022-02-12 20:34:50 +00003546 var trim = (x) => " " .. x
Bram Moolenaarb54abee2021-06-02 11:49:23 +02003547 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00003548 call v9.CheckScriptFailure(lines, 'E704:')
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02003549
3550 call assert_fails('call extend(g:, #{foo: { -> "foo" }})', 'E704:')
3551 let g:bar = 123
3552 call extend(g:, #{bar: { -> "foo" }}, "keep")
3553 call assert_fails('call extend(g:, #{bar: { -> "foo" }}, "force")', 'E704:')
zeertzjq91c75d12022-11-05 20:21:58 +00003554 unlet g:bar
3555
3556 call assert_fails('call extend(l:, #{foo: { -> "foo" }})', 'E704:')
3557 let bar = 123
3558 call extend(l:, #{bar: { -> "foo" }}, "keep")
3559 call assert_fails('call extend(l:, #{bar: { -> "foo" }}, "force")', 'E704:')
3560 unlet bar
3561
3562 call assert_fails('call extend(g:, #{foo: function("extend")})', 'E704:')
3563 let g:bar = 123
3564 call extend(g:, #{bar: function("extend")}, "keep")
3565 call assert_fails('call extend(g:, #{bar: function("extend")}, "force")', 'E704:')
3566 unlet g:bar
3567
3568 call assert_fails('call extend(l:, #{foo: function("extend")})', 'E704:')
3569 let bar = 123
3570 call extend(l:, #{bar: function("extend")}, "keep")
3571 call assert_fails('call extend(l:, #{bar: function("extend")}, "force")', 'E704:')
3572 unlet bar
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02003573endfunc
3574
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00003575func Test_funcref_to_string()
3576 let Fn = funcref('g:Test_funcref_to_string')
3577 call assert_equal("function('g:Test_funcref_to_string')", string(Fn))
3578endfunc
3579
LemonBoydca1d402022-04-28 15:26:33 +01003580" Test for isabsolutepath()
3581func Test_isabsolutepath()
3582 call assert_false(isabsolutepath(''))
3583 call assert_false(isabsolutepath('.'))
3584 call assert_false(isabsolutepath('../Foo'))
3585 call assert_false(isabsolutepath('Foo/'))
3586 if has('win32')
3587 call assert_true(isabsolutepath('A:\'))
3588 call assert_true(isabsolutepath('A:\Foo'))
3589 call assert_true(isabsolutepath('A:/Foo'))
3590 call assert_false(isabsolutepath('A:Foo'))
3591 call assert_false(isabsolutepath('\Windows'))
3592 call assert_true(isabsolutepath('\\Server2\Share\Test\Foo.txt'))
3593 else
3594 call assert_true(isabsolutepath('/'))
3595 call assert_true(isabsolutepath('/usr/share/'))
3596 endif
3597endfunc
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02003598
Yasuhiro Matsumoto05cf63e2022-05-03 11:02:28 +01003599" Test for exepath()
3600func Test_exepath()
3601 if has('win32')
3602 call assert_notequal(exepath('cmd'), '')
3603
3604 let oldNoDefaultCurrentDirectoryInExePath = $NoDefaultCurrentDirectoryInExePath
3605 call writefile(['@echo off', 'echo Evil'], 'vim-test-evil.bat')
3606 let $NoDefaultCurrentDirectoryInExePath = ''
3607 call assert_notequal(exepath("vim-test-evil.bat"), '')
3608 let $NoDefaultCurrentDirectoryInExePath = '1'
3609 call assert_equal(exepath("vim-test-evil.bat"), '')
3610 let $NoDefaultCurrentDirectoryInExePath = oldNoDefaultCurrentDirectoryInExePath
3611 call delete('vim-test-evil.bat')
3612 else
3613 call assert_notequal(exepath('sh'), '')
3614 endif
3615endfunc
3616
LemonBoy0f7a3e12022-05-26 12:10:37 +01003617" Test for virtcol()
3618func Test_virtcol()
zeertzjq825cf812023-08-17 22:55:25 +02003619 new
LemonBoy0f7a3e12022-05-26 12:10:37 +01003620 call setline(1, "the\tquick\tbrown\tfox")
3621 norm! 4|
3622 call assert_equal(8, virtcol('.'))
3623 call assert_equal(8, virtcol('.', v:false))
3624 call assert_equal([4, 8], virtcol('.', v:true))
zeertzjq825cf812023-08-17 22:55:25 +02003625
3626 let w = winwidth(0)
3627 call setline(2, repeat('a', w + 2))
3628 let win_nosbr = win_getid()
3629 split
3630 setlocal showbreak=!!
3631 let win_sbr = win_getid()
3632 call assert_equal([w, w], virtcol([2, w], v:true, win_nosbr))
3633 call assert_equal([w + 1, w + 1], virtcol([2, w + 1], v:true, win_nosbr))
3634 call assert_equal([w + 2, w + 2], virtcol([2, w + 2], v:true, win_nosbr))
3635 call assert_equal([w, w], virtcol([2, w], v:true, win_sbr))
3636 call assert_equal([w + 3, w + 3], virtcol([2, w + 1], v:true, win_sbr))
3637 call assert_equal([w + 4, w + 4], virtcol([2, w + 2], v:true, win_sbr))
3638 close
3639
3640 call assert_equal(0, virtcol(''))
3641 call assert_equal([0, 0], virtcol('', v:true))
3642 call assert_equal(0, virtcol('.', v:false, 5001))
3643 call assert_equal([0, 0], virtcol('.', v:true, 5001))
3644
LemonBoy0f7a3e12022-05-26 12:10:37 +01003645 bwipe!
3646endfunc
3647
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003648func Test_delfunc_while_listing()
3649 CheckRunVimInTerminal
3650
3651 let lines =<< trim END
3652 set nocompatible
3653 for i in range(1, 999)
3654 exe 'func ' .. 'MyFunc' .. i .. '()'
3655 endfunc
3656 endfor
3657 au CmdlineLeave : call timer_start(0, {-> execute('delfunc MyFunc622')})
3658 END
3659 call writefile(lines, 'Xfunctionclear', 'D')
3660 let buf = RunVimInTerminal('-S Xfunctionclear', {'rows': 12})
3661
3662 " This was using freed memory. The height of the terminal must be so that
3663 " the next function to be listed with "j" is the one that is deleted in the
3664 " timer callback, tricky!
3665 call term_sendkeys(buf, ":func /MyFunc\<CR>")
3666 call TermWait(buf, 50)
3667 call term_sendkeys(buf, "j")
3668 call TermWait(buf, 50)
3669 call term_sendkeys(buf, "\<CR>")
3670
3671 call StopVimInTerminal(buf)
3672endfunc
3673
Yegappan Lakshmanan03ff1c22023-05-06 14:08:21 +01003674" Test for the reverse() function with a string
3675func Test_string_reverse()
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +01003676 let lines =<< trim END
3677 call assert_equal('', reverse(test_null_string()))
3678 for [s1, s2] in [['', ''], ['a', 'a'], ['ab', 'ba'], ['abc', 'cba'],
3679 \ ['abcd', 'dcba'], ['«-«-»-»', '»-»-«-«'],
3680 \ ['🇦', '🇦'], ['🇦🇧', '🇧🇦'], ['🇦🇧🇨', '🇨🇧🇦'],
3681 \ ['🇦«🇧-🇨»🇩', '🇩»🇨-🇧«🇦']]
3682 call assert_equal(s2, reverse(s1))
3683 endfor
3684 END
3685 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan03ff1c22023-05-06 14:08:21 +01003686
3687 " test in latin1 encoding
3688 let save_enc = &encoding
3689 set encoding=latin1
3690 call assert_equal('dcba', reverse('abcd'))
3691 let &encoding = save_enc
3692endfunc
3693
Christian Brabandt4c6fe2e2023-09-02 19:30:03 +02003694func Test_fullcommand()
3695 " this used to crash vim
3696 call assert_equal('', fullcommand(10))
3697endfunc
3698
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02003699" Test for glob() with shell special patterns
3700func Test_glob_extended_bash()
3701 CheckExecutable bash
Ken Takata03ca4002023-09-28 21:59:58 +02003702 CheckNotMSWindows
3703 CheckNotMac " The default version of bash is old on macOS.
3704
Christian Brabandt9eb1ce52023-09-27 19:08:25 +02003705 let _shell = &shell
3706 set shell=bash
3707
3708 call mkdir('Xtestglob/foo/bar/src', 'p')
3709 call writefile([], 'Xtestglob/foo/bar/src/foo.sh')
3710 call writefile([], 'Xtestglob/foo/bar/src/foo.h')
3711 call writefile([], 'Xtestglob/foo/bar/src/foo.cpp')
3712
3713 " Sort output of glob() otherwise we end up with different
3714 " ordering depending on whether file system is case-sensitive.
3715 let expected = ['Xtestglob/foo/bar/src/foo.cpp', 'Xtestglob/foo/bar/src/foo.h']
3716 call assert_equal(expected, sort(glob('Xtestglob/**/foo.{h,cpp}', 0, 1)))
3717 call delete('Xtestglob', 'rf')
3718 let &shell=_shell
3719endfunc
3720
Ken Takata4a1ad552023-10-02 21:31:31 +02003721" Test for glob() with extended patterns (MS-Windows)
3722" Vim doesn't use 'shell' to expand wildcards on MS-Windows.
3723" Unlike bash, it doesn't support {,} expansion.
3724func Test_glob_extended_mswin()
3725 CheckMSWindows
3726
3727 call mkdir('Xtestglob/foo/bar/src', 'p')
3728 call writefile([], 'Xtestglob/foo/bar/src/foo.sh')
3729 call writefile([], 'Xtestglob/foo/bar/src/foo.h')
3730 call writefile([], 'Xtestglob/foo/bar/src/foo.cpp')
3731
3732 " Sort output of glob() otherwise we end up with different
3733 " ordering depending on whether file system is case-sensitive.
3734 let expected = ['Xtestglob/foo/bar/src/foo.cpp', 'Xtestglob/foo/bar/src/foo.h', 'Xtestglob/foo/bar/src/foo.sh']
3735 call assert_equal(expected, sort(glob('Xtestglob/**/foo.*', 0, 1)))
3736 call delete('Xtestglob', 'rf')
3737endfunc
3738
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01003739" vim: shiftwidth=2 sts=2 expandtab