blob: 131db109e3da703760afab9142b65df970bcf37a [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'))
33 endif
Bram Moolenaar99fa7212020-04-26 15:59:55 +020034 call assert_equal(1, has('vcon', 1))
35 call assert_equal(1, has('mouse_gpm_enabled', 1))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010036
Bram Moolenaar79296512020-03-22 16:17:14 +010037 call assert_equal(0, has('nonexistent'))
38 call assert_equal(0, has('nonexistent', 1))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010039
40 " Will we ever have patch 9999?
41 let ver = 'patch-' .. v:version / 100 .. '.' .. v:version % 100 .. '.9999'
42 call assert_equal(0, has(ver))
Bram Moolenaarb0375d42022-06-30 11:03:39 +010043
44 " There actually isn't a patch 9.0.0, but this is more consistent.
45 call assert_equal(1, has('patch-9.0.0'))
Bram Moolenaar79296512020-03-22 16:17:14 +010046endfunc
47
Bram Moolenaar24c2e482017-01-29 15:45:12 +010048func Test_empty()
49 call assert_equal(1, empty(''))
50 call assert_equal(0, empty('a'))
51
52 call assert_equal(1, empty(0))
53 call assert_equal(1, empty(-0))
54 call assert_equal(0, empty(1))
55 call assert_equal(0, empty(-1))
56
Bram Moolenaar73e28dc2022-09-17 21:08:33 +010057 call assert_equal(1, empty(0.0))
58 call assert_equal(1, empty(-0.0))
59 call assert_equal(0, empty(1.0))
60 call assert_equal(0, empty(-1.0))
61 call assert_equal(0, empty(1.0/0.0))
62 call assert_equal(0, empty(0.0/0.0))
Bram Moolenaar24c2e482017-01-29 15:45:12 +010063
64 call assert_equal(1, empty([]))
65 call assert_equal(0, empty(['a']))
66
67 call assert_equal(1, empty({}))
68 call assert_equal(0, empty({'a':1}))
69
70 call assert_equal(1, empty(v:null))
71 call assert_equal(1, empty(v:none))
72 call assert_equal(1, empty(v:false))
73 call assert_equal(0, empty(v:true))
74
Bram Moolenaar41042f32017-03-09 12:09:32 +010075 if has('channel')
76 call assert_equal(1, empty(test_null_channel()))
77 endif
78 if has('job')
79 call assert_equal(1, empty(test_null_job()))
80 endif
81
Bram Moolenaar24c2e482017-01-29 15:45:12 +010082 call assert_equal(0, empty(function('Test_empty')))
Bram Moolenaar17aca702019-05-16 22:24:55 +020083 call assert_equal(0, empty(function('Test_empty', [0])))
Bram Moolenaar7c215c52020-02-29 13:43:27 +010084
85 call assert_fails("call empty(test_void())", 'E685:')
86 call assert_fails("call empty(test_unknown())", 'E685:')
Bram Moolenaar24c2e482017-01-29 15:45:12 +010087endfunc
88
Bram Moolenaardd589232020-02-29 17:38:12 +010089func Test_test_void()
Bram Moolenaar61a417b2021-06-15 22:54:28 +020090 call assert_fails('echo 1 == test_void()', 'E1031:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +010091 call assert_fails('echo 1.0 == test_void()', 'E1031:')
Bram Moolenaardd589232020-02-29 17:38:12 +010092 call assert_fails('let x = json_encode(test_void())', 'E685:')
93 call assert_fails('let x = copy(test_void())', 'E685:')
Bram Moolenaar61a417b2021-06-15 22:54:28 +020094 call assert_fails('let x = copy([test_void()])', 'E1031:')
Bram Moolenaardd589232020-02-29 17:38:12 +010095endfunc
96
Bram Moolenaar1840a7b2021-07-13 20:32:29 +020097func Test_islocked()
98 call assert_fails('call islocked(99)', 'E475:')
99 call assert_fails('call islocked("s: x")', 'E488:')
100endfunc
101
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100102func Test_len()
103 call assert_equal(1, len(0))
104 call assert_equal(2, len(12))
105
106 call assert_equal(0, len(''))
107 call assert_equal(2, len('ab'))
108
109 call assert_equal(0, len([]))
Bram Moolenaar08f41572020-04-20 16:50:00 +0200110 call assert_equal(0, len(test_null_list()))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100111 call assert_equal(2, len([2, 1]))
112
113 call assert_equal(0, len({}))
Bram Moolenaar08f41572020-04-20 16:50:00 +0200114 call assert_equal(0, len(test_null_dict()))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100115 call assert_equal(2, len({'a': 1, 'b': 2}))
116
117 call assert_fails('call len(v:none)', 'E701:')
118 call assert_fails('call len({-> 0})', 'E701:')
119endfunc
120
121func Test_max()
122 call assert_equal(0, max([]))
123 call assert_equal(2, max([2]))
124 call assert_equal(2, max([1, 2]))
125 call assert_equal(2, max([1, 2, v:null]))
126
127 call assert_equal(0, max({}))
128 call assert_equal(2, max({'a':1, 'b':2}))
129
130 call assert_fails('call max(1)', 'E712:')
131 call assert_fails('call max(v:none)', 'E712:')
Bram Moolenaarab65fc72021-02-04 22:07:16 +0100132
133 " check we only get one error
134 call assert_fails('call max([#{}, [1]])', ['E728:', 'E728:'])
135 call assert_fails('call max(#{a: {}, b: [1]})', ['E728:', 'E728:'])
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100136endfunc
137
138func Test_min()
139 call assert_equal(0, min([]))
140 call assert_equal(2, min([2]))
141 call assert_equal(1, min([1, 2]))
142 call assert_equal(0, min([1, 2, v:null]))
143
144 call assert_equal(0, min({}))
145 call assert_equal(1, min({'a':1, 'b':2}))
146
147 call assert_fails('call min(1)', 'E712:')
148 call assert_fails('call min(v:none)', 'E712:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200149 call assert_fails('call min([1, {}])', 'E728:')
Bram Moolenaarab65fc72021-02-04 22:07:16 +0100150
151 " check we only get one error
152 call assert_fails('call min([[1], #{}])', ['E745:', 'E745:'])
153 call assert_fails('call min(#{a: [1], b: #{}})', ['E745:', 'E745:'])
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100154endfunc
155
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200156func Test_strwidth()
157 for aw in ['single', 'double']
158 exe 'set ambiwidth=' . aw
159 call assert_equal(0, strwidth(''))
160 call assert_equal(1, strwidth("\t"))
161 call assert_equal(3, strwidth('Vim'))
162 call assert_equal(4, strwidth(1234))
163 call assert_equal(5, strwidth(-1234))
164
Bram Moolenaar30276f22019-01-24 17:59:39 +0100165 call assert_equal(2, strwidth('😉'))
166 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
167 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200168
169 call assert_fails('call strwidth({->0})', 'E729:')
170 call assert_fails('call strwidth([])', 'E730:')
171 call assert_fails('call strwidth({})', 'E731:')
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200172 endfor
173
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100174 call assert_equal(3, strwidth(1.2))
175 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 +0200176
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200177 set ambiwidth&
178endfunc
179
Bram Moolenaar08243d22017-01-10 16:12:29 +0100180func Test_str2nr()
181 call assert_equal(0, str2nr(''))
182 call assert_equal(1, str2nr('1'))
183 call assert_equal(1, str2nr(' 1 '))
184
185 call assert_equal(1, str2nr('+1'))
186 call assert_equal(1, str2nr('+ 1'))
187 call assert_equal(1, str2nr(' + 1 '))
188
189 call assert_equal(-1, str2nr('-1'))
190 call assert_equal(-1, str2nr('- 1'))
191 call assert_equal(-1, str2nr(' - 1 '))
192
193 call assert_equal(123456789, str2nr('123456789'))
194 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100195
196 call assert_equal(5, str2nr('101', 2))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200197 call assert_equal(5, '0b101'->str2nr(2))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100198 call assert_equal(5, str2nr('0B101', 2))
199 call assert_equal(-5, str2nr('-101', 2))
200 call assert_equal(-5, str2nr('-0b101', 2))
201 call assert_equal(-5, str2nr('-0B101', 2))
202
203 call assert_equal(65, str2nr('101', 8))
204 call assert_equal(65, str2nr('0101', 8))
205 call assert_equal(-65, str2nr('-101', 8))
206 call assert_equal(-65, str2nr('-0101', 8))
Bram Moolenaarc17e66c2020-06-02 21:38:22 +0200207 call assert_equal(65, str2nr('0o101', 8))
208 call assert_equal(65, str2nr('0O0101', 8))
209 call assert_equal(-65, str2nr('-0O101', 8))
210 call assert_equal(-65, str2nr('-0o0101', 8))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100211
212 call assert_equal(11259375, str2nr('abcdef', 16))
213 call assert_equal(11259375, str2nr('ABCDEF', 16))
214 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
215 call assert_equal(11259375, str2nr('0xabcdef', 16))
216 call assert_equal(11259375, str2nr('0Xabcdef', 16))
217 call assert_equal(11259375, str2nr('0XABCDEF', 16))
218 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
219
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200220 call assert_equal(1, str2nr("1'000'000", 10, 0))
221 call assert_equal(256, str2nr("1'0000'0000", 2, 1))
222 call assert_equal(262144, str2nr("1'000'000", 8, 1))
223 call assert_equal(1000000, str2nr("1'000'000", 10, 1))
Bram Moolenaarea8dcf82019-09-15 21:12:22 +0200224 call assert_equal(1000, str2nr("1'000''000", 10, 1))
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200225 call assert_equal(65536, str2nr("1'00'00", 16, 1))
226
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100227 call assert_equal(0, str2nr('0x10'))
228 call assert_equal(0, str2nr('0b10'))
Bram Moolenaarc17e66c2020-06-02 21:38:22 +0200229 call assert_equal(0, str2nr('0o10'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100230 call assert_equal(1, str2nr('12', 2))
231 call assert_equal(1, str2nr('18', 8))
232 call assert_equal(1, str2nr('1g', 16))
233
234 call assert_equal(0, str2nr(v:null))
235 call assert_equal(0, str2nr(v:none))
236
237 call assert_fails('call str2nr([])', 'E730:')
238 call assert_fails('call str2nr({->2})', 'E729:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100239 call assert_equal(1, str2nr(1.2))
240 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 +0200241 call assert_fails('call str2nr(10, [])', 'E745:')
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100242endfunc
243
244func Test_strftime()
Bram Moolenaar10455d42019-11-21 15:36:18 +0100245 CheckFunction strftime
246
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100247 " Format of strftime() depends on system. We assume
248 " that basic formats tested here are available and
249 " identical on all systems which support strftime().
250 "
251 " The 2nd parameter of strftime() is a local time, so the output day
252 " of strftime() can be 17 or 18, depending on timezone.
253 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
254 "
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200255 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 +0100256
257 call assert_fails('call strftime([])', 'E730:')
258 call assert_fails('call strftime("%Y", [])', 'E745:')
Bram Moolenaardb517302019-06-18 22:53:24 +0200259
260 " Check that the time changes after we change the timezone
261 " Save previous timezone value, if any
262 if exists('$TZ')
263 let tz = $TZ
264 endif
265
266 " Force EST and then UTC, save the current hour (24-hour clock) for each
267 let $TZ = 'EST' | let est = strftime('%H')
268 let $TZ = 'UTC' | let utc = strftime('%H')
269
270 " Those hours should be two bytes long, and should not be the same; if they
271 " are, a tzset(3) call may have failed somewhere
272 call assert_equal(strlen(est), 2)
273 call assert_equal(strlen(utc), 2)
Bram Moolenaar87652a72019-06-18 23:07:37 +0200274 " TODO: this fails on MS-Windows
275 if has('unix')
276 call assert_notequal(est, utc)
277 endif
Bram Moolenaardb517302019-06-18 22:53:24 +0200278
279 " If we cached a timezone value, put it back, otherwise clear it
280 if exists('tz')
281 let $TZ = tz
282 else
283 unlet $TZ
284 endif
Bram Moolenaar10455d42019-11-21 15:36:18 +0100285endfunc
Bram Moolenaardb517302019-06-18 22:53:24 +0200286
Bram Moolenaar10455d42019-11-21 15:36:18 +0100287func Test_strptime()
288 CheckFunction strptime
289
290 if exists('$TZ')
291 let tz = $TZ
292 endif
293 let $TZ = 'UTC'
294
Bram Moolenaar9a838fe2019-12-06 12:45:01 +0100295 call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
Bram Moolenaar10455d42019-11-21 15:36:18 +0100296
Bram Moolenaarea1233f2020-06-10 16:54:13 +0200297 " Force DST and check that it's considered
298 let $TZ = 'WINTER0SUMMER,J1,J365'
299 call assert_equal(1484653763 - 3600, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
300
Bram Moolenaar10455d42019-11-21 15:36:18 +0100301 call assert_fails('call strptime()', 'E119:')
302 call assert_fails('call strptime("xxx")', 'E119:')
303 call assert_equal(0, strptime("%Y", ''))
304 call assert_equal(0, strptime("%Y", "xxx"))
305
306 if exists('tz')
307 let $TZ = tz
308 else
309 unlet $TZ
310 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100311endfunc
312
Bram Moolenaardce1e892019-02-10 23:18:53 +0100313func Test_resolve_unix()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200314 CheckUnix
Bram Moolenaar26109902018-10-06 15:43:17 +0200315
316 " Xlink1 -> Xlink2
317 " Xlink2 -> Xlink3
318 silent !ln -s -f Xlink2 Xlink1
319 silent !ln -s -f Xlink3 Xlink2
320 call assert_equal('Xlink3', resolve('Xlink1'))
321 call assert_equal('./Xlink3', resolve('./Xlink1'))
322 call assert_equal('Xlink3/', resolve('Xlink2/'))
323 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
324 "call assert_equal('Xlink3/', resolve('Xlink1/'))
325 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
326 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
327 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
328
329 " Test resolve() with a symlink cycle.
330 " Xlink1 -> Xlink2
331 " Xlink2 -> Xlink3
332 " Xlink3 -> Xlink1
333 silent !ln -s -f Xlink1 Xlink3
334 call assert_fails('call resolve("Xlink1")', 'E655:')
335 call assert_fails('call resolve("./Xlink1")', 'E655:')
336 call assert_fails('call resolve("Xlink2")', 'E655:')
337 call assert_fails('call resolve("Xlink3")', 'E655:')
338 call delete('Xlink1')
339 call delete('Xlink2')
340 call delete('Xlink3')
341
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100342 silent !ln -s -f Xresolvedir//Xfile Xresolvelink
343 call assert_equal('Xresolvedir/Xfile', resolve('Xresolvelink'))
344 call delete('Xresolvelink')
Bram Moolenaar26109902018-10-06 15:43:17 +0200345
346 silent !ln -s -f Xlink2/ Xlink1
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200347 call assert_equal('Xlink2', 'Xlink1'->resolve())
Bram Moolenaar26109902018-10-06 15:43:17 +0200348 call assert_equal('Xlink2/', resolve('Xlink1/'))
349 call delete('Xlink1')
350
351 silent !ln -s -f ./Xlink2 Xlink1
352 call assert_equal('Xlink2', resolve('Xlink1'))
353 call assert_equal('./Xlink2', resolve('./Xlink1'))
354 call delete('Xlink1')
Bram Moolenaar50c4e9e2020-10-05 20:38:06 +0200355
356 call assert_equal('/', resolve('/'))
Bram Moolenaar26109902018-10-06 15:43:17 +0200357endfunc
358
Bram Moolenaardce1e892019-02-10 23:18:53 +0100359func s:normalize_fname(fname)
360 let ret = substitute(a:fname, '\', '/', 'g')
361 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200362 return ret->tolower()
Bram Moolenaardce1e892019-02-10 23:18:53 +0100363endfunc
364
365func Test_resolve_win32()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200366 CheckMSWindows
Bram Moolenaardce1e892019-02-10 23:18:53 +0100367
368 " test for shortcut file
369 if executable('cscript')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100370 new Xresfile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100371 wq
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200372 let lines =<< trim END
373 Set fs = CreateObject("Scripting.FileSystemObject")
374 Set ws = WScript.CreateObject("WScript.Shell")
375 Set shortcut = ws.CreateShortcut("Xlink.lnk")
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100376 shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xresfile")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200377 shortcut.Save
378 END
379 call writefile(lines, 'link.vbs')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100380 silent !cscript link.vbs
381 call delete('link.vbs')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100382 call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink.lnk')))
383 call delete('Xresfile')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100384
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100385 call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink.lnk')))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100386 call delete('Xlink.lnk')
387 else
388 echomsg 'skipped test for shortcut file'
389 endif
390
391 " remove files
392 call delete('Xlink')
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100393 call delete('Xdir', 'd')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100394 call delete('Xresfile')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100395
396 " test for symbolic link to a file
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100397 new Xresfile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100398 wq
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100399 call assert_equal('Xresfile', resolve('Xresfile'))
400 silent !mklink Xlink Xresfile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100401 if !v:shell_error
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100402 call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink')))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100403 call delete('Xlink')
404 else
405 echomsg 'skipped test for symbolic link to a file'
406 endif
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100407 call delete('Xresfile')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100408
409 " test for junction to a directory
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100410 call mkdir('Xdir')
411 silent !mklink /J Xlink Xdir
Bram Moolenaardce1e892019-02-10 23:18:53 +0100412 if !v:shell_error
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100413 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100414
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100415 call delete('Xdir', 'd')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100416
417 " test for junction already removed
418 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
419 call delete('Xlink')
420 else
421 echomsg 'skipped test for junction to a directory'
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100422 call delete('Xdir', 'd')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100423 endif
424
425 " test for symbolic link to a directory
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100426 call mkdir('Xdir')
427 silent !mklink /D Xlink Xdir
Bram Moolenaardce1e892019-02-10 23:18:53 +0100428 if !v:shell_error
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100429 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100430
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100431 call delete('Xdir', 'd')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100432
433 " test for symbolic link already removed
434 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
435 call delete('Xlink')
436 else
437 echomsg 'skipped test for symbolic link to a directory'
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100438 call delete('Xdir', 'd')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100439 endif
440
441 " test for buffer name
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100442 new Xbuffile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100443 wq
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100444 silent !mklink Xlink Xbuffile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100445 if !v:shell_error
446 edit Xlink
447 call assert_equal('Xlink', bufname('%'))
448 call delete('Xlink')
449 bw!
450 else
451 echomsg 'skipped test for buffer name'
452 endif
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100453 call delete('Xbuffile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200454
455 " test for reparse point
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100456 call mkdir('Xdir')
457 call assert_equal('Xdir', resolve('Xdir'))
458 silent !mklink /D Xdirlink Xdir
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200459 if !v:shell_error
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100460 w Xdir/text.txt
461 call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt'))
462 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
463 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink')))
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200464 call delete('Xdirlink')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200465 else
466 echomsg 'skipped test for reparse point'
467 endif
468
Bram Moolenaar15cae5c2022-08-29 22:51:38 +0100469 call delete('Xdir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100470endfunc
471
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100472func Test_simplify()
473 call assert_equal('', simplify(''))
474 call assert_equal('/', simplify('/'))
475 call assert_equal('/', simplify('/.'))
476 call assert_equal('/', simplify('/..'))
477 call assert_equal('/...', simplify('/...'))
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +0200478 call assert_equal('//path', simplify('//path'))
Bram Moolenaarc70222d2020-06-15 23:18:12 +0200479 if has('unix')
480 call assert_equal('/path', simplify('///path'))
481 call assert_equal('/path', simplify('////path'))
482 endif
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +0200483
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200484 call assert_equal('./dir/file', './dir/file'->simplify())
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100485 call assert_equal('./dir/file', simplify('.///dir//file'))
486 call assert_equal('./dir/file', simplify('./dir/./file'))
487 call assert_equal('./file', simplify('./dir/../file'))
488 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
489 call assert_equal('./file', simplify('dir/.././file'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200490 call assert_equal('../dir', simplify('./../dir'))
491 call assert_equal('..', simplify('../testdir/..'))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100492 call mkdir('Xsimpdir')
493 call assert_equal('.', simplify('Xsimpdir/../.'))
494 call delete('Xsimpdir', 'd')
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100495
496 call assert_fails('call simplify({->0})', 'E729:')
497 call assert_fails('call simplify([])', 'E730:')
498 call assert_fails('call simplify({})', 'E731:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100499 call assert_equal('1.2', simplify(1.2))
500 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 +0100501endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100502
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200503func Test_pathshorten()
504 call assert_equal('', pathshorten(''))
505 call assert_equal('foo', pathshorten('foo'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200506 call assert_equal('/foo', '/foo'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200507 call assert_equal('f/', pathshorten('foo/'))
508 call assert_equal('f/bar', pathshorten('foo/bar'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200509 call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200510 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
511 call assert_equal('.f/bar', pathshorten('.foo/bar'))
512 call assert_equal('~f/bar', pathshorten('~foo/bar'))
513 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
514 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
515 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200516 call assert_fails('call pathshorten([])', 'E730:')
Bram Moolenaar6a33ef02020-09-25 22:42:48 +0200517
518 " test pathshorten with optional variable to set preferred size of shortening
519 call assert_equal('', pathshorten('', 2))
520 call assert_equal('foo', pathshorten('foo', 2))
521 call assert_equal('/foo', pathshorten('/foo', 2))
522 call assert_equal('fo/', pathshorten('foo/', 2))
523 call assert_equal('fo/bar', pathshorten('foo/bar', 2))
524 call assert_equal('fo/ba/foobar', pathshorten('foo/bar/foobar', 2))
525 call assert_equal('/fo/ba/foobar', pathshorten('/foo/bar/foobar', 2))
526 call assert_equal('.fo/bar', pathshorten('.foo/bar', 2))
527 call assert_equal('~fo/bar', pathshorten('~foo/bar', 2))
528 call assert_equal('~.fo/bar', pathshorten('~.foo/bar', 2))
529 call assert_equal('.~fo/bar', pathshorten('.~foo/bar', 2))
530 call assert_equal('~/fo/bar', pathshorten('~/foo/bar', 2))
531 call assert_fails('call pathshorten([],2)', 'E730:')
532 call assert_notequal('~/fo/bar', pathshorten('~/foo/bar', 3))
533 call assert_equal('~/foo/bar', pathshorten('~/foo/bar', 3))
534 call assert_equal('~/f/bar', pathshorten('~/foo/bar', 0))
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200535endfunc
536
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100537func Test_strpart()
538 call assert_equal('de', strpart('abcdefg', 3, 2))
539 call assert_equal('ab', strpart('abcdefg', -2, 4))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200540 call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100541 call assert_equal('fg', strpart('abcdefg', 5, 4))
542 call assert_equal('defg', strpart('abcdefg', 3))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100543 call assert_equal('', strpart('abcdefg', 10))
544 call assert_fails("let s=strpart('abcdef', [])", 'E745:')
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100545
Bram Moolenaar30276f22019-01-24 17:59:39 +0100546 call assert_equal('lép', strpart('éléphant', 2, 4))
547 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaar6c53fca2020-08-23 17:34:46 +0200548
549 call assert_equal('é', strpart('éléphant', 0, 1, 1))
550 call assert_equal('ép', strpart('éléphant', 3, 2, v:true))
551 call assert_equal('ó', strpart('cómposed', 1, 1, 1))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100552endfunc
553
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100554func Test_tolower()
555 call assert_equal("", tolower(""))
556
557 " Test with all printable ASCII characters.
558 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
559 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
560
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100561 " Test with a few uppercase diacritics.
562 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
563 call assert_equal("bḃḇ", tolower("BḂḆ"))
564 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
565 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
566 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
567 call assert_equal("f ", tolower("F "))
568 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
569 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
570 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
571 call assert_equal("jĵ", tolower("JĴ"))
572 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
573 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
574 call assert_equal("mḿṁ", tolower("MḾṀ"))
575 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
576 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
577 call assert_equal("pṕṗ", tolower("PṔṖ"))
578 call assert_equal("q", tolower("Q"))
579 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
580 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
581 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
582 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
583 call assert_equal("v", tolower("V"))
584 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
585 call assert_equal("xẋẍ", tolower("XẊẌ"))
586 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
587 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
588
589 " Test with a few lowercase diacritics, which should remain unchanged.
590 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
591 call assert_equal("bḃḇ", tolower("bḃḇ"))
592 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
593 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
594 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
595 call assert_equal("fḟ", tolower("fḟ"))
596 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
597 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
598 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
599 call assert_equal("jĵǰ", tolower("jĵǰ"))
600 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
601 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
602 call assert_equal("mḿṁ ", tolower("mḿṁ "))
603 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
604 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
605 call assert_equal("pṕṗ", tolower("pṕṗ"))
606 call assert_equal("q", tolower("q"))
607 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
608 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
609 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
610 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
611 call assert_equal("vṽ", tolower("vṽ"))
612 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
613 call assert_equal("ẋẍ", tolower("ẋẍ"))
614 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
615 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
616
617 " According to https://twitter.com/jifa/status/625776454479970304
618 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
619 " in length (2 to 3 bytes) when lowercased. So let's test them.
620 call assert_equal(" ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100621
622 " This call to tolower with invalid utf8 sequence used to cause access to
623 " invalid memory.
624 call tolower("\xC0\x80\xC0")
625 call tolower("123\xC0\x80\xC0")
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200626
627 " Test in latin1 encoding
628 let save_enc = &encoding
629 set encoding=latin1
630 call assert_equal("abc", tolower("ABC"))
631 let &encoding = save_enc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100632endfunc
633
634func Test_toupper()
635 call assert_equal("", toupper(""))
636
637 " Test with all printable ASCII characters.
638 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
639 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
640
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100641 " Test with a few lowercase diacritics.
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200642 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100643 call assert_equal("BḂḆ", toupper("bḃḇ"))
644 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
645 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
646 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
647 call assert_equal("F", toupper("f"))
648 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
649 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
650 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
651 call assert_equal("JĴǰ", toupper("jĵǰ"))
652 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
653 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
654 call assert_equal("MḾṀ ", toupper("mḿṁ "))
655 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
656 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
657 call assert_equal("PṔṖ", toupper("pṕṗ"))
658 call assert_equal("Q", toupper("q"))
659 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
660 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
661 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
662 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
663 call assert_equal("V", toupper("v"))
664 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
665 call assert_equal("ẊẌ", toupper("ẋẍ"))
666 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
667 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
668
669 " Test that uppercase diacritics, which should remain unchanged.
670 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
671 call assert_equal("BḂḆ", toupper("BḂḆ"))
672 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
673 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
674 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
675 call assert_equal("FḞ ", toupper("FḞ "))
676 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
677 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
678 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
679 call assert_equal("JĴ", toupper("JĴ"))
680 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
681 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
682 call assert_equal("MḾṀ", toupper("MḾṀ"))
683 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
684 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
685 call assert_equal("PṔṖ", toupper("PṔṖ"))
686 call assert_equal("Q", toupper("Q"))
687 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
688 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
689 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
690 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
691 call assert_equal("VṼ", toupper("VṼ"))
692 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
693 call assert_equal("XẊẌ", toupper("XẊẌ"))
694 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
695 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
696
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100697 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100698
699 " This call to toupper with invalid utf8 sequence used to cause access to
700 " invalid memory.
701 call toupper("\xC0\x80\xC0")
702 call toupper("123\xC0\x80\xC0")
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200703
704 " Test in latin1 encoding
705 let save_enc = &encoding
706 set encoding=latin1
707 call assert_equal("ABC", toupper("abc"))
708 let &encoding = save_enc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100709endfunc
710
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200711func Test_tr()
712 call assert_equal('foo', tr('bar', 'bar', 'foo'))
713 call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100714 call assert_fails("let s=tr([], 'abc', 'def')", 'E730:')
715 call assert_fails("let s=tr('abc', [], 'def')", 'E730:')
716 call assert_fails("let s=tr('abc', 'abc', [])", 'E730:')
717 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
718 set encoding=latin1
719 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
720 call assert_equal('hEllO', tr('hello', 'eo', 'EO'))
721 call assert_equal('hello', tr('hello', 'xy', 'ab'))
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200722 call assert_fails('call tr("abc", "123", "₁₂")', 'E475:')
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100723 set encoding=utf8
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200724endfunc
725
Bram Moolenaare90858d2017-02-01 17:24:34 +0100726" Tests for the mode() function
727let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100728func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100729 let g:current_modes = mode(0) . '-' . mode(1)
730 return ''
731endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100732
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200733" Test for the mode() function
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100734func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100735 new
736 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
737
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100738 " Only complete from the current buffer.
739 set complete=.
740
Bram Moolenaare90858d2017-02-01 17:24:34 +0100741 inoremap <F2> <C-R>=Save_mode()<CR>
zeertzjqeaf3f362021-07-28 16:51:53 +0200742 xnoremap <F2> <Cmd>call Save_mode()<CR>
Bram Moolenaare90858d2017-02-01 17:24:34 +0100743
744 normal! 3G
745 exe "normal i\<F2>\<Esc>"
746 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100747 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100748 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
749 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100750 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100751 exe "normal iBro\<C-P>\<F2>\<Esc>u"
752 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100753 " i_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100754 exe "normal iBa\<C-X>\<F2>\<Esc>u"
755 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100756 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100757 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
758 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100759 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100760 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
761 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100762 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100763 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
764 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100765 " i_CTRL-X CTRL-L: Multiple matches
766 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
767 call assert_equal('i-ic', g:current_modes)
768 " i_CTRL-X CTRL-L: Single match
769 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
770 call assert_equal('i-ic', g:current_modes)
771 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100772 exe "normal iCom\<C-P>\<F2>\<Esc>u"
773 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100774 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100775 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
776 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100777 " i_CTRL-X CTRL-L: No match
778 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
779 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100780
zeertzjqcc8cd442021-10-03 15:19:14 +0100781 exe "normal R\<F2>\<Esc>"
782 call assert_equal('R-R', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100783 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100784 exe "normal RBa\<C-P>\<F2>\<Esc>u"
785 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100786 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100787 exe "normal RBro\<C-P>\<F2>\<Esc>u"
788 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100789 " R_CTRL-X
Bram Moolenaare90858d2017-02-01 17:24:34 +0100790 exe "normal RBa\<C-X>\<F2>\<Esc>u"
791 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100792 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100793 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
794 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100795 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100796 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
797 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100798 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100799 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
800 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100801 " R_CTRL-X CTRL-L: Multiple matches
802 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
803 call assert_equal('R-Rc', g:current_modes)
804 " R_CTRL-X CTRL-L: Single match
805 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
806 call assert_equal('R-Rc', g:current_modes)
807 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100808 exe "normal RCom\<C-P>\<F2>\<Esc>u"
809 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100810 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100811 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
812 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100813 " R_CTRL-X CTRL-L: No match
814 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
815 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100816
zeertzjqcc8cd442021-10-03 15:19:14 +0100817 exe "normal gR\<F2>\<Esc>"
818 call assert_equal('R-Rv', g:current_modes)
819 " gR_CTRL-P: Multiple matches
820 exe "normal gRBa\<C-P>\<F2>\<Esc>u"
821 call assert_equal('R-Rvc', g:current_modes)
822 " gR_CTRL-P: Single match
823 exe "normal gRBro\<C-P>\<F2>\<Esc>u"
824 call assert_equal('R-Rvc', g:current_modes)
825 " gR_CTRL-X
826 exe "normal gRBa\<C-X>\<F2>\<Esc>u"
827 call assert_equal('R-Rvx', g:current_modes)
828 " gR_CTRL-X CTRL-P: Multiple matches
829 exe "normal gRBa\<C-X>\<C-P>\<F2>\<Esc>u"
830 call assert_equal('R-Rvc', g:current_modes)
831 " gR_CTRL-X CTRL-P: Single match
832 exe "normal gRBro\<C-X>\<C-P>\<F2>\<Esc>u"
833 call assert_equal('R-Rvc', g:current_modes)
834 " gR_CTRL-X CTRL-P + CTRL-P: Single match
835 exe "normal gRBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
836 call assert_equal('R-Rvc', g:current_modes)
837 " gR_CTRL-X CTRL-L: Multiple matches
838 exe "normal gR\<C-X>\<C-L>\<F2>\<Esc>u"
839 call assert_equal('R-Rvc', g:current_modes)
840 " gR_CTRL-X CTRL-L: Single match
841 exe "normal gRBlu\<C-X>\<C-L>\<F2>\<Esc>u"
842 call assert_equal('R-Rvc', g:current_modes)
843 " gR_CTRL-P: No match
844 exe "normal gRCom\<C-P>\<F2>\<Esc>u"
845 call assert_equal('R-Rvc', g:current_modes)
846 " gR_CTRL-X CTRL-P: No match
847 exe "normal gRCom\<C-X>\<C-P>\<F2>\<Esc>u"
848 call assert_equal('R-Rvc', g:current_modes)
849 " gR_CTRL-X CTRL-L: No match
850 exe "normal gRabc\<C-X>\<C-L>\<F2>\<Esc>u"
851 call assert_equal('R-Rvc', g:current_modes)
852
Bram Moolenaara1449832019-09-01 20:16:52 +0200853 call assert_equal('n', 0->mode())
854 call assert_equal('n', 1->mode())
Bram Moolenaare90858d2017-02-01 17:24:34 +0100855
Bram Moolenaar612cc382018-07-29 15:34:26 +0200856 " i_CTRL-O
857 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
858 call assert_equal("n-niI", g:current_modes)
859
860 " R_CTRL-O
861 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
862 call assert_equal("n-niR", g:current_modes)
863
864 " gR_CTRL-O
865 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
866 call assert_equal("n-niV", g:current_modes)
867
Bram Moolenaare90858d2017-02-01 17:24:34 +0100868 " How to test operator-pending mode?
869
870 call feedkeys("v", 'xt')
871 call assert_equal('v', mode())
872 call assert_equal('v', mode(1))
873 call feedkeys("\<Esc>V", 'xt')
874 call assert_equal('V', mode())
875 call assert_equal('V', mode(1))
876 call feedkeys("\<Esc>\<C-V>", 'xt')
877 call assert_equal("\<C-V>", mode())
878 call assert_equal("\<C-V>", mode(1))
879 call feedkeys("\<Esc>", 'xt')
880
881 call feedkeys("gh", 'xt')
882 call assert_equal('s', mode())
883 call assert_equal('s', mode(1))
884 call feedkeys("\<Esc>gH", 'xt')
885 call assert_equal('S', mode())
886 call assert_equal('S', mode(1))
887 call feedkeys("\<Esc>g\<C-H>", 'xt')
888 call assert_equal("\<C-S>", mode())
889 call assert_equal("\<C-S>", mode(1))
890 call feedkeys("\<Esc>", 'xt')
891
zeertzjqeaf3f362021-07-28 16:51:53 +0200892 " v_CTRL-O
893 exe "normal gh\<C-O>\<F2>\<Esc>"
894 call assert_equal("v-vs", g:current_modes)
895 exe "normal gH\<C-O>\<F2>\<Esc>"
896 call assert_equal("V-Vs", g:current_modes)
897 exe "normal g\<C-H>\<C-O>\<F2>\<Esc>"
898 call assert_equal("\<C-V>-\<C-V>s", g:current_modes)
899
Bram Moolenaare90858d2017-02-01 17:24:34 +0100900 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
901 call assert_equal('c-c', g:current_modes)
902 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
903 call assert_equal('c-cv', g:current_modes)
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200904 call feedkeys("Qcall Save_mode()\<CR>vi\<CR>", 'xt')
905 call assert_equal('c-ce', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100906 " How to test Ex mode?
907
naohiro ono75c30e92021-10-19 11:15:41 +0100908 " Test mode in operatorfunc (it used to be Operator-pending).
909 set operatorfunc=OperatorFunc
910 function OperatorFunc(_)
911 call Save_mode()
912 endfunction
913 execute "normal! g@l\<Esc>"
914 call assert_equal('n-n', g:current_modes)
915 execute "normal! i\<C-o>g@l\<Esc>"
916 call assert_equal('n-niI', g:current_modes)
917 execute "normal! R\<C-o>g@l\<Esc>"
918 call assert_equal('n-niR', g:current_modes)
919 execute "normal! gR\<C-o>g@l\<Esc>"
920 call assert_equal('n-niV', g:current_modes)
921
Bram Moolenaar72406a42021-10-02 16:34:55 +0100922 if has('terminal')
923 term
924 call feedkeys("\<C-W>N", 'xt')
925 call assert_equal('n', mode())
926 call assert_equal('nt', mode(1))
927 call feedkeys("aexit\<CR>", 'xt')
928 endif
929
Bram Moolenaare90858d2017-02-01 17:24:34 +0100930 bwipe!
931 iunmap <F2>
zeertzjqeaf3f362021-07-28 16:51:53 +0200932 xunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100933 set complete&
naohiro ono75c30e92021-10-19 11:15:41 +0100934 set operatorfunc&
935 delfunction OperatorFunc
Bram Moolenaare90858d2017-02-01 17:24:34 +0100936endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100937
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200938" Test for append()
Bram Moolenaard2007022019-08-27 21:56:06 +0200939func Test_append()
940 enew!
941 split
Bram Moolenaarcd9c8d42022-11-05 23:46:43 +0000942 call assert_equal(0, append(1, []))
943 call assert_equal(0, append(1, test_null_list()))
944 call assert_equal(0, append(0, ["foo"]))
945 call assert_equal(0, append(1, []))
946 call assert_equal(0, append(1, test_null_list()))
947 call assert_equal(0, append(8, []))
948 call assert_equal(0, append(9, test_null_list()))
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200949 call assert_equal(['foo', ''], getline(1, '$'))
Bram Moolenaard2007022019-08-27 21:56:06 +0200950 split
951 only
952 undo
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200953 undo
Bram Moolenaar08f41572020-04-20 16:50:00 +0200954
955 " Using $ instead of '$' must give an error
956 call assert_fails("call append($, 'foobar')", 'E116:')
Bram Moolenaar801cd352022-10-10 16:08:16 +0100957
958 call assert_fails("call append({}, '')", ['E728:', 'E728:'])
Bram Moolenaard2007022019-08-27 21:56:06 +0200959endfunc
960
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200961" Test for setline()
962func Test_setline()
963 new
964 call setline(0, ["foo"])
965 call setline(0, [])
966 call setline(0, test_null_list())
967 call setline(1, ["bar"])
968 call setline(1, [])
969 call setline(1, test_null_list())
970 call setline(2, [])
971 call setline(2, test_null_list())
972 call setline(3, [])
973 call setline(3, test_null_list())
974 call setline(2, ["baz"])
975 call assert_equal(['bar', 'baz'], getline(1, '$'))
976 close!
977endfunc
978
Bram Moolenaar79518e22017-02-17 16:31:35 +0100979func Test_getbufvar()
980 let bnr = bufnr('%')
981 let b:var_num = '1234'
982 let def_num = '5678'
983 call assert_equal('1234', getbufvar(bnr, 'var_num'))
984 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
985
986 let bd = getbufvar(bnr, '')
987 call assert_equal('1234', bd['var_num'])
988 call assert_true(exists("bd['changedtick']"))
989 call assert_equal(2, len(bd))
990
991 let bd2 = getbufvar(bnr, '', def_num)
992 call assert_equal(bd, bd2)
993
994 unlet b:var_num
995 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
996 call assert_equal('', getbufvar(bnr, 'var_num'))
997
998 let bd = getbufvar(bnr, '')
999 call assert_equal(1, len(bd))
1000 let bd = getbufvar(bnr, '',def_num)
1001 call assert_equal(1, len(bd))
1002
Bram Moolenaar4520d442017-03-19 16:09:46 +01001003 call assert_equal('', getbufvar(9999, ''))
1004 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001005 unlet def_num
1006
Bram Moolenaar507647d2017-02-17 16:43:49 +01001007 call assert_equal(0, getbufvar(bnr, '&autoindent'))
1008 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001009
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001010 " Set and get a buffer-local variable
1011 call setbufvar(bnr, 'bufvar_test', ['one', 'two'])
1012 call assert_equal(['one', 'two'], getbufvar(bnr, 'bufvar_test'))
1013
Bram Moolenaar79518e22017-02-17 16:31:35 +01001014 " Open new window with forced option values
1015 set fileformats=unix,dos
1016 new ++ff=dos ++bin ++enc=iso-8859-2
1017 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
1018 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
1019 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
1020 close
1021
Bram Moolenaar52592752020-04-03 18:43:35 +02001022 " Get the b: dict.
1023 let b:testvar = 'one'
1024 new
1025 let b:testvar = 'two'
1026 let thebuf = bufnr()
1027 wincmd w
1028 call assert_equal('two', getbufvar(thebuf, 'testvar'))
1029 call assert_equal('two', getbufvar(thebuf, '').testvar)
1030 bwipe!
1031
Bram Moolenaar79518e22017-02-17 16:31:35 +01001032 set fileformats&
1033endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001034
Bram Moolenaar41042f32017-03-09 12:09:32 +01001035func Test_last_buffer_nr()
1036 call assert_equal(bufnr('$'), last_buffer_nr())
1037endfunc
1038
1039func Test_stridx()
1040 call assert_equal(-1, stridx('', 'l'))
1041 call assert_equal(0, stridx('', ''))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +02001042 call assert_equal(0, 'hello'->stridx(''))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001043 call assert_equal(-1, stridx('hello', 'L'))
1044 call assert_equal(2, stridx('hello', 'l', -1))
1045 call assert_equal(2, stridx('hello', 'l', 0))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +02001046 call assert_equal(2, 'hello'->stridx('l', 1))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001047 call assert_equal(3, stridx('hello', 'l', 3))
1048 call assert_equal(-1, stridx('hello', 'l', 4))
1049 call assert_equal(-1, stridx('hello', 'l', 10))
1050 call assert_equal(2, stridx('hello', 'll'))
1051 call assert_equal(-1, stridx('hello', 'hello world'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001052 call assert_fails("let n=stridx('hello', [])", 'E730:')
1053 call assert_fails("let n=stridx([], 'l')", 'E730:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001054endfunc
1055
1056func Test_strridx()
1057 call assert_equal(-1, strridx('', 'l'))
1058 call assert_equal(0, strridx('', ''))
1059 call assert_equal(5, strridx('hello', ''))
1060 call assert_equal(-1, strridx('hello', 'L'))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +02001061 call assert_equal(3, 'hello'->strridx('l'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001062 call assert_equal(3, strridx('hello', 'l', 10))
1063 call assert_equal(3, strridx('hello', 'l', 3))
1064 call assert_equal(2, strridx('hello', 'l', 2))
1065 call assert_equal(-1, strridx('hello', 'l', 1))
1066 call assert_equal(-1, strridx('hello', 'l', 0))
1067 call assert_equal(-1, strridx('hello', 'l', -1))
1068 call assert_equal(2, strridx('hello', 'll'))
1069 call assert_equal(-1, strridx('hello', 'hello world'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001070 call assert_fails("let n=strridx('hello', [])", 'E730:')
1071 call assert_fails("let n=strridx([], 'l')", 'E730:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001072endfunc
1073
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001074func Test_match_func()
1075 call assert_equal(4, match('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001076 call assert_equal(4, 'testing'->match('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001077 call assert_equal(-1, match('testing', 'ing', 5))
1078 call assert_equal(-1, match('testing', 'ing', 8))
1079 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
1080 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001081 call assert_fails("let x=match('vim', [])", 'E730:')
1082 call assert_equal(3, match(['a', 'b', 'c', 'a'], 'a', 1))
1083 call assert_equal(-1, match(['a', 'b', 'c', 'a'], 'a', 5))
1084 call assert_equal(4, match('testing', 'ing', -1))
1085 call assert_fails("let x=match('testing', 'ing', 0, [])", 'E745:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001086 call assert_equal(-1, match(test_null_list(), 2))
Bram Moolenaar531be472020-09-23 22:38:05 +02001087 call assert_equal(-1, match('abc', '\\%('))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001088endfunc
1089
Bram Moolenaar41042f32017-03-09 12:09:32 +01001090func Test_matchend()
1091 call assert_equal(7, matchend('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001092 call assert_equal(7, 'testing'->matchend('ing', 2))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001093 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001094 call assert_equal(-1, matchend('testing', 'ing', 8))
1095 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
1096 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
1097endfunc
1098
1099func Test_matchlist()
1100 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001101 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001102 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
1103endfunc
1104
1105func Test_matchstr()
1106 call assert_equal('ing', matchstr('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001107 call assert_equal('ing', 'testing'->matchstr('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001108 call assert_equal('', matchstr('testing', 'ing', 5))
1109 call assert_equal('', matchstr('testing', 'ing', 8))
1110 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
1111 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
1112endfunc
1113
1114func Test_matchstrpos()
1115 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001116 call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001117 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
1118 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
1119 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
1120 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001121 call assert_equal(['', -1, -1], matchstrpos(test_null_list(), '\a'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001122endfunc
1123
1124func Test_nextnonblank_prevnonblank()
1125 new
1126insert
1127This
1128
1129
1130is
1131
1132a
1133Test
1134.
1135 call assert_equal(0, nextnonblank(-1))
1136 call assert_equal(0, nextnonblank(0))
1137 call assert_equal(1, nextnonblank(1))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001138 call assert_equal(4, 2->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001139 call assert_equal(4, nextnonblank(3))
1140 call assert_equal(4, nextnonblank(4))
1141 call assert_equal(6, nextnonblank(5))
1142 call assert_equal(6, nextnonblank(6))
1143 call assert_equal(7, nextnonblank(7))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001144 call assert_equal(0, 8->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001145
1146 call assert_equal(0, prevnonblank(-1))
1147 call assert_equal(0, prevnonblank(0))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001148 call assert_equal(1, 1->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001149 call assert_equal(1, prevnonblank(2))
1150 call assert_equal(1, prevnonblank(3))
1151 call assert_equal(4, prevnonblank(4))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001152 call assert_equal(4, 5->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001153 call assert_equal(6, prevnonblank(6))
1154 call assert_equal(7, prevnonblank(7))
1155 call assert_equal(0, prevnonblank(8))
1156 bw!
1157endfunc
1158
1159func Test_byte2line_line2byte()
1160 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +02001161 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +01001162 call setline(1, ['a', 'bc', 'd'])
1163
1164 set fileformat=unix
1165 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
1166 \ map(range(-1, 8), 'byte2line(v:val)'))
1167 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
1168 \ map(range(-1, 5), 'line2byte(v:val)'))
1169
1170 set fileformat=mac
1171 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001172 \ map(range(-1, 8), 'v:val->byte2line()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001173 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
Bram Moolenaar02b31112019-08-31 22:16:38 +02001174 \ map(range(-1, 5), 'v:val->line2byte()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001175
1176 set fileformat=dos
1177 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
1178 \ map(range(-1, 11), 'byte2line(v:val)'))
1179 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
1180 \ map(range(-1, 5), 'line2byte(v:val)'))
1181
Bram Moolenaarc26f7c62018-08-20 22:53:04 +02001182 bw!
1183 set noendofline nofixendofline
1184 normal a-
1185 for ff in ["unix", "mac", "dos"]
1186 let &fileformat = ff
1187 call assert_equal(1, line2byte(1))
1188 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
1189 endfor
1190
1191 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +01001192 bw!
1193endfunc
1194
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001195" Test for byteidx() and byteidxcomp() functions
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001196func Test_byteidx()
1197 let a = '.é.' " one char of two bytes
1198 call assert_equal(0, byteidx(a, 0))
1199 call assert_equal(0, byteidxcomp(a, 0))
1200 call assert_equal(1, byteidx(a, 1))
1201 call assert_equal(1, byteidxcomp(a, 1))
1202 call assert_equal(3, byteidx(a, 2))
1203 call assert_equal(3, byteidxcomp(a, 2))
1204 call assert_equal(4, byteidx(a, 3))
1205 call assert_equal(4, byteidxcomp(a, 3))
1206 call assert_equal(-1, byteidx(a, 4))
1207 call assert_equal(-1, byteidxcomp(a, 4))
1208
1209 let b = '.é.' " normal e with composing char
1210 call assert_equal(0, b->byteidx(0))
1211 call assert_equal(1, b->byteidx(1))
1212 call assert_equal(4, b->byteidx(2))
1213 call assert_equal(5, b->byteidx(3))
1214 call assert_equal(-1, b->byteidx(4))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001215 call assert_fails("call byteidx([], 0)", 'E730:')
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001216
1217 call assert_equal(0, b->byteidxcomp(0))
1218 call assert_equal(1, b->byteidxcomp(1))
1219 call assert_equal(2, b->byteidxcomp(2))
1220 call assert_equal(4, b->byteidxcomp(3))
1221 call assert_equal(5, b->byteidxcomp(4))
1222 call assert_equal(-1, b->byteidxcomp(5))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001223 call assert_fails("call byteidxcomp([], 0)", 'E730:')
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001224endfunc
1225
Bram Moolenaar17793ef2020-12-28 12:56:58 +01001226" Test for charidx()
1227func Test_charidx()
1228 let a = 'xáb́y'
1229 call assert_equal(0, charidx(a, 0))
1230 call assert_equal(1, charidx(a, 3))
1231 call assert_equal(2, charidx(a, 4))
1232 call assert_equal(3, charidx(a, 7))
1233 call assert_equal(-1, charidx(a, 8))
Dominique Pelle6d37e8e2021-05-06 17:36:55 +02001234 call assert_equal(-1, charidx(a, -1))
Bram Moolenaar17793ef2020-12-28 12:56:58 +01001235 call assert_equal(-1, charidx('', 0))
Dominique Pelle6d37e8e2021-05-06 17:36:55 +02001236 call assert_equal(-1, charidx(test_null_string(), 0))
Bram Moolenaar17793ef2020-12-28 12:56:58 +01001237
1238 " count composing characters
1239 call assert_equal(0, charidx(a, 0, 1))
1240 call assert_equal(2, charidx(a, 2, 1))
1241 call assert_equal(3, charidx(a, 4, 1))
1242 call assert_equal(5, charidx(a, 7, 1))
1243 call assert_equal(-1, charidx(a, 8, 1))
1244 call assert_equal(-1, charidx('', 0, 1))
1245
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01001246 call assert_fails('let x = charidx([], 1)', 'E1174:')
1247 call assert_fails('let x = charidx("abc", [])', 'E1210:')
1248 call assert_fails('let x = charidx("abc", 1, [])', 'E1212:')
1249 call assert_fails('let x = charidx("abc", 1, -1)', 'E1212:')
1250 call assert_fails('let x = charidx("abc", 1, 2)', 'E1212:')
Bram Moolenaar17793ef2020-12-28 12:56:58 +01001251endfunc
1252
Bram Moolenaar41042f32017-03-09 12:09:32 +01001253func Test_count()
1254 let l = ['a', 'a', 'A', 'b']
1255 call assert_equal(2, count(l, 'a'))
1256 call assert_equal(1, count(l, 'A'))
1257 call assert_equal(1, count(l, 'b'))
1258 call assert_equal(0, count(l, 'B'))
1259
1260 call assert_equal(2, count(l, 'a', 0))
1261 call assert_equal(1, count(l, 'A', 0))
1262 call assert_equal(1, count(l, 'b', 0))
1263 call assert_equal(0, count(l, 'B', 0))
1264
1265 call assert_equal(3, count(l, 'a', 1))
1266 call assert_equal(3, count(l, 'A', 1))
1267 call assert_equal(1, count(l, 'b', 1))
1268 call assert_equal(1, count(l, 'B', 1))
1269 call assert_equal(0, count(l, 'c', 1))
1270
1271 call assert_equal(1, count(l, 'a', 0, 1))
1272 call assert_equal(2, count(l, 'a', 1, 1))
1273 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02001274 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001275
1276 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
1277 call assert_equal(2, count(d, 'a'))
1278 call assert_equal(1, count(d, 'A'))
1279 call assert_equal(1, count(d, 'b'))
1280 call assert_equal(0, count(d, 'B'))
1281
1282 call assert_equal(2, count(d, 'a', 0))
1283 call assert_equal(1, count(d, 'A', 0))
1284 call assert_equal(1, count(d, 'b', 0))
1285 call assert_equal(0, count(d, 'B', 0))
1286
1287 call assert_equal(3, count(d, 'a', 1))
1288 call assert_equal(3, count(d, 'A', 1))
1289 call assert_equal(1, count(d, 'b', 1))
1290 call assert_equal(1, count(d, 'B', 1))
1291 call assert_equal(0, count(d, 'c', 1))
1292
1293 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +02001294
1295 call assert_equal(0, count("foo", "bar"))
1296 call assert_equal(1, count("foo", "oo"))
1297 call assert_equal(2, count("foo", "o"))
1298 call assert_equal(0, count("foo", "O"))
1299 call assert_equal(2, count("foo", "O", 1))
1300 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +01001301 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001302
1303 call assert_fails('call count(0, 0)', 'E712:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001304endfunc
1305
1306func Test_changenr()
1307 new Xchangenr
1308 call assert_equal(0, changenr())
1309 norm ifoo
1310 call assert_equal(1, changenr())
1311 set undolevels=10
1312 norm Sbar
1313 call assert_equal(2, changenr())
1314 undo
1315 call assert_equal(1, changenr())
1316 redo
1317 call assert_equal(2, changenr())
1318 bw!
1319 set undolevels&
1320endfunc
1321
1322func Test_filewritable()
1323 new Xfilewritable
1324 write!
1325 call assert_equal(1, filewritable('Xfilewritable'))
1326
1327 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1328 call assert_equal(0, filewritable('Xfilewritable'))
1329
1330 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001331 call assert_equal(1, 'Xfilewritable'->filewritable())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001332
1333 call assert_equal(0, filewritable('doesnotexist'))
1334
Bram Moolenaar70e67252022-09-27 19:34:35 +01001335 call mkdir('Xwritedir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001336 call assert_equal(2, filewritable('Xwritedir'))
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +02001337
Bram Moolenaar41042f32017-03-09 12:09:32 +01001338 call delete('Xfilewritable')
1339 bw!
1340endfunc
1341
Bram Moolenaar82956662018-10-06 15:18:45 +02001342func Test_Executable()
1343 if has('win32')
1344 call assert_equal(1, executable('notepad'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001345 call assert_equal(1, 'notepad.exe'->executable())
Bram Moolenaar82956662018-10-06 15:18:45 +02001346 call assert_equal(0, executable('notepad.exe.exe'))
1347 call assert_equal(0, executable('shell32.dll'))
1348 call assert_equal(0, executable('win.ini'))
Bram Moolenaar95da1362020-05-30 18:37:55 +02001349
1350 " get "notepad" path and remove the leading drive and sep. (ex. 'C:\')
1351 let notepadcmd = exepath('notepad.exe')
1352 let driveroot = notepadcmd[:2]
1353 let notepadcmd = notepadcmd[3:]
1354 new
1355 " check that the relative path works in /
1356 execute 'lcd' driveroot
1357 call assert_equal(1, executable(notepadcmd))
1358 call assert_equal(driveroot .. notepadcmd, notepadcmd->exepath())
1359 bwipe
1360
1361 " create "notepad.bat"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001362 call mkdir('Xnotedir')
1363 let notepadbat = fnamemodify('Xnotedir/notepad.bat', ':p')
Bram Moolenaar95da1362020-05-30 18:37:55 +02001364 call writefile([], notepadbat)
1365 new
1366 " check that the path and the pathext order is valid
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001367 lcd Xnotedir
Bram Moolenaar95da1362020-05-30 18:37:55 +02001368 let [pathext, $PATHEXT] = [$PATHEXT, '.com;.exe;.bat;.cmd']
1369 call assert_equal(notepadbat, exepath('notepad'))
1370 let $PATHEXT = pathext
1371 bwipe
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001372 eval 'Xnotedir'->delete('rf')
Bram Moolenaar82956662018-10-06 15:18:45 +02001373 elseif has('unix')
Bram Moolenaara4208962019-08-24 20:50:19 +02001374 call assert_equal(1, 'cat'->executable())
Bram Moolenaara05a0d32018-10-07 18:43:05 +02001375 call assert_equal(0, executable('nodogshere'))
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001376
1377 " get "cat" path and remove the leading /
1378 let catcmd = exepath('cat')[1:]
1379 new
Bram Moolenaara4208962019-08-24 20:50:19 +02001380 " check that the relative path works in /
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001381 lcd /
1382 call assert_equal(1, executable(catcmd))
Bram Moolenaara3870832021-01-01 14:20:44 +01001383 let result = catcmd->exepath()
1384 " when using chroot looking for sbin/cat can return bin/cat, that is OK
1385 if catcmd =~ '\<sbin\>' && result =~ '\<bin\>'
1386 call assert_equal('/' .. substitute(catcmd, '\<sbin\>', 'bin', ''), result)
1387 else
Bram Moolenaarbf634a02021-07-31 17:20:04 +02001388 " /bin/cat and /usr/bin/cat may be hard linked, we could get either
1389 let result = substitute(result, '/usr/bin/cat', '/bin/cat', '')
1390 let catcmd = substitute(catcmd, 'usr/bin/cat', 'bin/cat', '')
Bram Moolenaara3870832021-01-01 14:20:44 +01001391 call assert_equal('/' .. catcmd, result)
1392 endif
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001393 bwipe
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001394 else
1395 throw 'Skipped: does not work on this platform'
Bram Moolenaar82956662018-10-06 15:18:45 +02001396 endif
1397endfunc
1398
LemonBoy40fd7e62022-05-05 20:18:16 +01001399func Test_executable_windows_store_apps()
1400 CheckMSWindows
1401
1402 " Windows Store apps install some 'decoy' .exe that require some careful
1403 " handling as they behave similarly to symlinks.
1404 let app_dir = expand("$LOCALAPPDATA\\Microsoft\\WindowsApps")
1405 if !isdirectory(app_dir)
1406 return
1407 endif
1408
1409 let save_path = $PATH
1410 let $PATH = app_dir
1411 " Ensure executable() finds all the app .exes
1412 for entry in readdir(app_dir)
1413 if entry =~ '\.exe$'
1414 call assert_true(executable(entry))
1415 endif
1416 endfor
1417
1418 let $PATH = save_path
1419endfunc
1420
Bram Moolenaar86621892019-03-30 21:51:28 +01001421func Test_executable_longname()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001422 CheckMSWindows
Bram Moolenaar86621892019-03-30 21:51:28 +01001423
Bram Moolenaarf637bce2020-11-23 18:14:56 +01001424 " Create a temporary .bat file with 205 characters in the name.
1425 " Maximum length of a filename (including the path) on MS-Windows is 259
1426 " characters.
1427 " See https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
1428 let len = 259 - getcwd()->len() - 6
1429 if len > 200
1430 let len = 200
1431 endif
1432
1433 let fname = 'X' . repeat('あ', len) . '.bat'
Bram Moolenaar86621892019-03-30 21:51:28 +01001434 call writefile([], fname)
1435 call assert_equal(1, executable(fname))
1436 call delete(fname)
1437endfunc
1438
Bram Moolenaar41042f32017-03-09 12:09:32 +01001439func Test_hostname()
1440 let hostname_vim = hostname()
1441 if has('unix')
1442 let hostname_system = systemlist('uname -n')[0]
1443 call assert_equal(hostname_vim, hostname_system)
1444 endif
1445endfunc
1446
1447func Test_getpid()
1448 " getpid() always returns the same value within a vim instance.
1449 call assert_equal(getpid(), getpid())
1450 if has('unix')
1451 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1452 endif
1453endfunc
1454
1455func Test_hlexists()
1456 call assert_equal(0, hlexists('does_not_exist'))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001457 call assert_equal(0, 'Number'->hlexists())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001458 call assert_equal(0, highlight_exists('does_not_exist'))
1459 call assert_equal(0, highlight_exists('Number'))
1460 syntax on
1461 call assert_equal(0, hlexists('does_not_exist'))
1462 call assert_equal(1, hlexists('Number'))
1463 call assert_equal(0, highlight_exists('does_not_exist'))
1464 call assert_equal(1, highlight_exists('Number'))
1465 syntax off
1466endfunc
1467
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001468" Test for the col() function
Bram Moolenaar41042f32017-03-09 12:09:32 +01001469func Test_col()
1470 new
1471 call setline(1, 'abcdef')
1472 norm gg4|mx6|mY2|
1473 call assert_equal(2, col('.'))
1474 call assert_equal(7, col('$'))
Bram Moolenaar8b633132020-03-20 18:20:51 +01001475 call assert_equal(2, col('v'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001476 call assert_equal(4, col("'x"))
1477 call assert_equal(6, col("'Y"))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001478 call assert_equal(2, [1, 2]->col())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001479 call assert_equal(7, col([1, '$']))
1480
1481 call assert_equal(0, col(''))
1482 call assert_equal(0, col('x'))
1483 call assert_equal(0, col([2, '$']))
1484 call assert_equal(0, col([1, 100]))
1485 call assert_equal(0, col([1]))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001486 call assert_equal(0, col(test_null_list()))
1487 call assert_fails('let c = col({})', 'E731:')
Bram Moolenaar8b633132020-03-20 18:20:51 +01001488
1489 " test for getting the visual start column
1490 func T()
1491 let g:Vcol = col('v')
1492 return ''
1493 endfunc
1494 let g:Vcol = 0
1495 xmap <expr> <F2> T()
1496 exe "normal gg3|ve\<F2>"
1497 call assert_equal(3, g:Vcol)
1498 xunmap <F2>
1499 delfunc T
1500
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001501 " Test for the visual line start and end marks '< and '>
1502 call setline(1, ['one', 'one two', 'one two three'])
1503 "normal! ggVG
1504 call feedkeys("ggVG\<Esc>", 'xt')
1505 call assert_equal(1, col("'<"))
1506 call assert_equal(14, col("'>"))
1507 " Delete the last line of the visually selected region
1508 $d
1509 call assert_notequal(14, col("'>"))
1510
1511 " Test with 'virtualedit'
1512 set virtualedit=all
1513 call cursor(1, 10)
1514 call assert_equal(4, col('.'))
1515 set virtualedit&
1516
Bram Moolenaar41042f32017-03-09 12:09:32 +01001517 bw!
1518endfunc
1519
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001520" Test for input()
1521func Test_input_func()
1522 " Test for prompt with multiple lines
1523 redir => v
1524 call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt')
1525 redir END
1526 call assert_equal("B", c)
1527 call assert_equal(['A', 'B', 'C'], split(v, "\n"))
1528
1529 " Test for default value
1530 call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt')
1531 call assert_equal('red', c)
1532
1533 " Test for completion at the input prompt
1534 func! Tcomplete(arglead, cmdline, pos)
1535 return "item1\nitem2\nitem3"
1536 endfunc
Bram Moolenaar9d489562020-07-30 20:08:50 +02001537 call feedkeys(":let c = input('Q? ', '', 'custom,Tcomplete')\<CR>"
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001538 \ .. "\<C-A>\<CR>", 'xt')
1539 delfunc Tcomplete
1540 call assert_equal('item1 item2 item3', c)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001541
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +01001542 " Test for using special characters as default input
Bram Moolenaar1f448d92021-03-22 19:37:06 +01001543 call feedkeys(":let c = input('name? ', \"x\\<BS>y\")\<CR>\<CR>", 'xt')
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +01001544 call assert_equal('y', c)
1545
zeertzjqe3a529b2022-06-05 19:01:37 +01001546 " Test for using text with composing characters as default input
1547 call feedkeys(":let c = input('name? ', \"ã̳\")\<CR>\<CR>", 'xt')
1548 call assert_equal('ã̳', c)
1549
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +01001550 " Test for using <CR> as default input
1551 call feedkeys(":let c = input('name? ', \"\\<CR>\")\<CR>x\<CR>", 'xt')
1552 call assert_equal(' x', c)
1553
Bram Moolenaar578fe942020-02-27 21:32:51 +01001554 call assert_fails("call input('F:', '', 'invalid')", 'E180:')
1555 call assert_fails("call input('F:', '', [])", 'E730:')
1556endfunc
1557
1558" Test for the inputdialog() function
1559func Test_inputdialog()
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02001560 set timeout timeoutlen=10
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001561 if has('gui_running')
1562 call assert_fails('let v=inputdialog([], "xx")', 'E730:')
1563 call assert_fails('let v=inputdialog("Q", [])', 'E730:')
1564 else
1565 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt')
1566 call assert_equal('xx', v)
1567 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt')
1568 call assert_equal('yy', v)
1569 endif
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02001570 set timeout& timeoutlen&
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001571endfunc
1572
1573" Test for inputlist()
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001574func Test_inputlist()
1575 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1576 call assert_equal(1, c)
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001577 call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001578 call assert_equal(2, c)
1579 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1580 call assert_equal(3, c)
1581
Bram Moolenaareebd5552020-06-10 15:45:57 +02001582 " CR to cancel
1583 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<cr>", 'tx')
1584 call assert_equal(0, c)
1585
1586 " Esc to cancel
1587 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<Esc>", 'tx')
1588 call assert_equal(0, c)
1589
1590 " q to cancel
1591 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>q", 'tx')
1592 call assert_equal(0, c)
1593
=?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?=5cf94572021-05-20 21:14:20 +02001594 " Cancel after inputting a number
1595 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>5q", 'tx')
1596 call assert_equal(0, c)
1597
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001598 " Use backspace to delete characters in the prompt
1599 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<BS>3\<BS>2\<cr>", 'tx')
1600 call assert_equal(2, c)
1601
1602 " Use mouse to make a selection
1603 call test_setmouse(&lines - 3, 2)
1604 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
1605 call assert_equal(1, c)
1606 " Mouse click outside of the list
1607 call test_setmouse(&lines - 6, 2)
1608 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
1609 call assert_equal(-2, c)
1610
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001611 call assert_fails('call inputlist("")', 'E686:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001612 call assert_fails('call inputlist(test_null_list())', 'E686:')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001613endfunc
1614
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001615func Test_balloon_show()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001616 CheckFeature balloon_eval
Bram Moolenaarb47bed22021-04-14 17:06:43 +02001617
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001618 " This won't do anything but must not crash either.
1619 call balloon_show('hi!')
1620 if !has('gui_running')
1621 call balloon_show(range(3))
1622 call balloon_show([])
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001623 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001624endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001625
1626func Test_setbufvar_options()
1627 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1628 " window layout.
1629 call assert_equal(1, winnr('$'))
1630 split dummy_preview
1631 resize 2
1632 set winfixheight winfixwidth
1633 let prev_id = win_getid()
1634
1635 wincmd j
Bram Moolenaarc05d1c02020-09-04 18:38:06 +02001636 let wh = winheight(0)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001637 let dummy_buf = bufnr('dummy_buf1', v:true)
1638 call setbufvar(dummy_buf, '&buftype', 'nofile')
1639 execute 'belowright vertical split #' . dummy_buf
Bram Moolenaarc05d1c02020-09-04 18:38:06 +02001640 call assert_equal(wh, winheight(0))
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001641 let dum1_id = win_getid()
1642
1643 wincmd h
Bram Moolenaarc05d1c02020-09-04 18:38:06 +02001644 let wh = winheight(0)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001645 let dummy_buf = bufnr('dummy_buf2', v:true)
Bram Moolenaar196b4662019-09-06 21:34:30 +02001646 eval 'nofile'->setbufvar(dummy_buf, '&buftype')
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001647 execute 'belowright vertical split #' . dummy_buf
Bram Moolenaarc05d1c02020-09-04 18:38:06 +02001648 call assert_equal(wh, winheight(0))
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001649
1650 bwipe!
1651 call win_gotoid(prev_id)
1652 bwipe!
1653 call win_gotoid(dum1_id)
1654 bwipe!
1655endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001656
Bram Moolenaardff97e62022-01-24 20:00:55 +00001657func Test_setbufvar_keep_window_title()
1658 CheckRunVimInTerminal
Bram Moolenaara6c09a72022-01-24 22:02:15 +00001659 if !has('title') || empty(&t_ts)
1660 throw "Skipped: can't get/set title"
1661 endif
Bram Moolenaardff97e62022-01-24 20:00:55 +00001662
1663 let lines =<< trim END
Bram Moolenaar14501122022-01-24 22:32:28 +00001664 set title
Bram Moolenaardff97e62022-01-24 20:00:55 +00001665 edit Xa.txt
1666 let g:buf = bufadd('Xb.txt')
1667 inoremap <F2> <C-R>=setbufvar(g:buf, '&autoindent', 1) ?? ''<CR>
1668 END
Bram Moolenaar70e67252022-09-27 19:34:35 +01001669 call writefile(lines, 'Xsetbufvar', 'D')
Bram Moolenaardff97e62022-01-24 20:00:55 +00001670 let buf = RunVimInTerminal('-S Xsetbufvar', {})
Bram Moolenaar3a8ad592022-01-24 22:18:24 +00001671 call WaitForAssert({-> assert_match('Xa.txt', term_gettitle(buf))}, 1000)
Bram Moolenaardff97e62022-01-24 20:00:55 +00001672
1673 call term_sendkeys(buf, "i\<F2>")
1674 call TermWait(buf)
1675 call term_sendkeys(buf, "\<Esc>")
1676 call TermWait(buf)
1677 call assert_match('Xa.txt', term_gettitle(buf))
1678
1679 call StopVimInTerminal(buf)
Bram Moolenaardff97e62022-01-24 20:00:55 +00001680endfunc
1681
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001682func Test_redo_in_nested_functions()
1683 nnoremap g. :set opfunc=Operator<CR>g@
1684 function Operator( type, ... )
1685 let @x = 'XXX'
1686 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1687 endfunction
1688
1689 function! Apply()
1690 5,6normal! .
1691 endfunction
1692
1693 new
1694 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1695 1normal g.i"
1696 call assert_equal('some "XXX" text', getline(1))
1697 3,4normal .
1698 call assert_equal('some "XXX" text', getline(3))
1699 call assert_equal('more "XXX" text', getline(4))
1700 call Apply()
1701 call assert_equal('some "XXX" text', getline(5))
1702 call assert_equal('more "XXX" text', getline(6))
1703 bwipe!
1704
1705 nunmap g.
1706 delfunc Operator
1707 delfunc Apply
1708endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02001709
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001710func Test_trim()
1711 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001712 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 +01001713 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1714 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
1715 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
1716 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
1717 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
1718 call assert_equal("RESERVE", trim("RESERVE", "你好"))
1719 call assert_equal("R E SER V E", trim("你好您R E SER V E早好你你", "你好"))
1720 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
1721 call assert_equal("R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
1722 call assert_equal("R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
1723 call assert_equal("R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
1724 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1725 call assert_equal("", trim("", ""))
1726 call assert_equal("a", trim("a", ""))
1727 call assert_equal("", trim("", "a"))
1728
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001729 call assert_equal("vim", trim(" vim ", " ", 0))
1730 call assert_equal("vim ", trim(" vim ", " ", 1))
1731 call assert_equal(" vim", trim(" vim ", " ", 2))
1732 call assert_fails('eval trim(" vim ", " ", [])', 'E745:')
1733 call assert_fails('eval trim(" vim ", " ", -1)', 'E475:')
1734 call assert_fails('eval trim(" vim ", " ", 3)', 'E475:')
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01001735 call assert_fails('eval trim(" vim ", 0)', 'E1174:')
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001736
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001737 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001738 call assert_equal("x", trim(chars . "x" . chars))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001739
1740 call assert_fails('let c=trim([])', 'E730:')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001741endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001742
1743" Test for reg_recording() and reg_executing()
1744func Test_reg_executing_and_recording()
1745 let s:reg_stat = ''
1746 func s:save_reg_stat()
1747 let s:reg_stat = reg_recording() . ':' . reg_executing()
1748 return ''
1749 endfunc
1750
1751 new
1752 call s:save_reg_stat()
1753 call assert_equal(':', s:reg_stat)
1754 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1755 call assert_equal('a:', s:reg_stat)
1756 call feedkeys("@a", 'xt')
1757 call assert_equal(':a', s:reg_stat)
1758 call feedkeys("qb@aq", 'xt')
1759 call assert_equal('b:a', s:reg_stat)
1760 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1761 call assert_equal('":', s:reg_stat)
1762
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001763 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001764 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001765 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1766 func TestFunc() abort
1767 normal! ia
1768 endfunc
1769 call feedkeys("@q", 'xt')
1770 call assert_equal(':q', s:reg_stat)
1771 delfunc TestFunc
1772
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001773 " getchar() command saves and restores reg_executing
1774 map W :call TestFunc()<CR>
1775 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001776 let g:typed = ''
1777 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001778 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001779 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001780 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001781 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001782 endfunc
1783 call feedkeys("@qy", 'xt')
1784 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001785 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001786 delfunc TestFunc
1787 unmap W
1788 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001789 unlet g:regs
1790
1791 " input() command saves and restores reg_executing
1792 map W :call TestFunc()<CR>
1793 let @q = "W"
1794 let g:typed = ''
1795 let g:regs = []
1796 func TestFunc() abort
1797 let g:regs += [reg_executing()]
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001798 let g:typed = '?'->input()
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001799 let g:regs += [reg_executing()]
1800 endfunc
1801 call feedkeys("@qy\<CR>", 'xt')
1802 call assert_equal("y", g:typed)
1803 call assert_equal(['q', 'q'], g:regs)
1804 delfunc TestFunc
1805 unmap W
1806 unlet g:typed
1807 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001808
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001809 bwipe!
1810 delfunc s:save_reg_stat
1811 unlet s:reg_stat
1812endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001813
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001814func Test_inputsecret()
1815 map W :call TestFunc()<CR>
1816 let @q = "W"
1817 let g:typed1 = ''
1818 let g:typed2 = ''
1819 let g:regs = []
1820 func TestFunc() abort
1821 let g:typed1 = '?'->inputsecret()
1822 let g:typed2 = inputsecret('password: ')
1823 endfunc
1824 call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1825 call assert_equal("something", g:typed1)
1826 call assert_equal("else", g:typed2)
1827 delfunc TestFunc
1828 unmap W
1829 unlet g:typed1
1830 unlet g:typed2
1831endfunc
1832
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001833func Test_getchar()
1834 call feedkeys('a', '')
1835 call assert_equal(char2nr('a'), getchar())
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02001836 call assert_equal(0, getchar(0))
1837 call assert_equal(0, getchar(1))
1838
1839 call feedkeys('a', '')
1840 call assert_equal('a', getcharstr())
1841 call assert_equal('', getcharstr(0))
1842 call assert_equal('', getcharstr(1))
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001843
zeertzjqad6c45f2022-02-20 19:05:10 +00001844 call feedkeys("\<M-F2>", '')
1845 call assert_equal("\<M-F2>", getchar(0))
1846 call assert_equal(0, getchar(0))
1847
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001848 call setline(1, 'xxxx')
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001849 call test_setmouse(1, 3)
1850 let v:mouse_win = 9
1851 let v:mouse_winid = 9
1852 let v:mouse_lnum = 9
1853 let v:mouse_col = 9
1854 call feedkeys("\<S-LeftMouse>", '')
1855 call assert_equal("\<S-LeftMouse>", getchar())
1856 call assert_equal(1, v:mouse_win)
1857 call assert_equal(win_getid(1), v:mouse_winid)
1858 call assert_equal(1, v:mouse_lnum)
1859 call assert_equal(3, v:mouse_col)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001860 enew!
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001861endfunc
1862
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001863func Test_libcall_libcallnr()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001864 CheckFeature libcall
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001865
1866 if has('win32')
1867 let libc = 'msvcrt.dll'
1868 elseif has('mac')
1869 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001870 elseif executable('ldd')
1871 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1872 endif
1873 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001874 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001875 if has('linux')
1876 " There is not documented but regarding the 1st argument of glibc's
1877 " dlopen an empty string and nullptr are equivalent, so using an empty
1878 " string for the 1st argument of libcall allows to call functions.
1879 let libc = ''
1880 elseif has('sun')
1881 " Set the path to libc.so according to the architecture.
1882 let test_bits = system('file ' . GetVimProg())
1883 let test_arch = system('uname -p')
1884 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1885 let libc = '/usr/lib/sparcv9/libc.so'
1886 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1887 let libc = '/usr/lib/amd64/libc.so'
1888 else
1889 let libc = '/usr/lib/libc.so'
1890 endif
1891 else
1892 " Unfortunately skip this test until a good way is found.
1893 return
1894 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001895 endif
1896
1897 if has('win32')
Bram Moolenaar02b31112019-08-31 22:16:38 +02001898 call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001899 else
Bram Moolenaar02b31112019-08-31 22:16:38 +02001900 call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001901 endif
1902
1903 " If function returns NULL, libcall() should return an empty string.
1904 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1905
1906 " Test libcallnr() with string and integer argument.
Bram Moolenaar02b31112019-08-31 22:16:38 +02001907 call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1908 call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001909
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001910 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", ['', 'E364:'])
1911 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", ['', 'E364:'])
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001912
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001913 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", ['', 'E364:'])
1914 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", ['', 'E364:'])
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001915endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001916
1917sandbox function Fsandbox()
1918 normal ix
1919endfunc
1920
1921func Test_func_sandbox()
1922 sandbox let F = {-> 'hello'}
1923 call assert_equal('hello', F())
1924
Bram Moolenaara4208962019-08-24 20:50:19 +02001925 sandbox let F = {-> "normal ix\<Esc>"->execute()}
Bram Moolenaard90a1442018-07-15 20:24:31 +02001926 call assert_fails('call F()', 'E48:')
1927 unlet F
1928
1929 call assert_fails('call Fsandbox()', 'E48:')
1930 delfunc Fsandbox
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001931
1932 " From a sandbox try to set a predefined variable (which cannot be modified
1933 " from a sandbox)
1934 call assert_fails('sandbox let v:lnum = 10', 'E794:')
Bram Moolenaard90a1442018-07-15 20:24:31 +02001935endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001936
1937func EditAnotherFile()
1938 let word = expand('<cword>')
1939 edit Xfuncrange2
1940endfunc
1941
1942func Test_func_range_with_edit()
1943 " Define a function that edits another buffer, then call it with a range that
1944 " is invalid in that buffer.
Bram Moolenaar70e67252022-09-27 19:34:35 +01001945 call writefile(['just one line'], 'Xfuncrange2', 'D')
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001946 new
Bram Moolenaar196b4662019-09-06 21:34:30 +02001947 eval 10->range()->setline(1)
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001948 write Xfuncrange1
1949 call assert_fails('5,8call EditAnotherFile()', 'E16:')
1950
1951 call delete('Xfuncrange1')
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001952 bwipe!
1953endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001954
1955func Test_func_exists_on_reload()
Bram Moolenaar70e67252022-09-27 19:34:35 +01001956 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists', 'D')
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001957 call assert_equal(0, exists('*ExistingFunction'))
1958 source Xfuncexists
Bram Moolenaara4208962019-08-24 20:50:19 +02001959 call assert_equal(1, '*ExistingFunction'->exists())
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001960 " Redefining a function when reloading a script is OK.
1961 source Xfuncexists
1962 call assert_equal(1, exists('*ExistingFunction'))
1963
1964 " But redefining in another script is not OK.
Bram Moolenaar70e67252022-09-27 19:34:35 +01001965 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2', 'D')
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001966 call assert_fails('source Xfuncexists2', 'E122:')
1967
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001968 " Defining a new function from the cmdline should fail if the function is
1969 " already defined
1970 call assert_fails('call feedkeys(":func ExistingFunction()\<CR>", "xt")', 'E122:')
1971
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001972 delfunc ExistingFunction
1973 call assert_equal(0, exists('*ExistingFunction'))
1974 call writefile([
1975 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1976 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1977 \ ], 'Xfuncexists')
1978 call assert_fails('source Xfuncexists', 'E122:')
1979 call assert_equal(1, exists('*ExistingFunction'))
1980
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001981 delfunc ExistingFunction
1982endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01001983
1984" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1985func Test_confirm()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001986 CheckUnix
1987 CheckNotGui
Bram Moolenaar2e050092019-01-27 15:00:36 +01001988
1989 call feedkeys('o', 'L')
1990 let a = confirm('Press O to proceed')
1991 call assert_equal(1, a)
1992
1993 call feedkeys('y', 'L')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001994 let a = 'Are you sure?'->confirm("&Yes\n&No")
Bram Moolenaar2e050092019-01-27 15:00:36 +01001995 call assert_equal(1, a)
1996
1997 call feedkeys('n', 'L')
1998 let a = confirm('Are you sure?', "&Yes\n&No")
1999 call assert_equal(2, a)
2000
2001 " confirm() should return 0 when pressing CTRL-C.
Bram Moolenaar79296512020-03-22 16:17:14 +01002002 call feedkeys("\<C-C>", 'L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01002003 let a = confirm('Are you sure?', "&Yes\n&No")
2004 call assert_equal(0, a)
2005
2006 " <Esc> requires another character to avoid it being seen as the start of an
2007 " escape sequence. Zero should be harmless.
Bram Moolenaara4208962019-08-24 20:50:19 +02002008 eval "\<Esc>0"->feedkeys('L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01002009 let a = confirm('Are you sure?', "&Yes\n&No")
2010 call assert_equal(0, a)
2011
2012 " Default choice is returned when pressing <CR>.
2013 call feedkeys("\<CR>", 'L')
2014 let a = confirm('Are you sure?', "&Yes\n&No")
2015 call assert_equal(1, a)
2016
2017 call feedkeys("\<CR>", 'L')
2018 let a = confirm('Are you sure?', "&Yes\n&No", 2)
2019 call assert_equal(2, a)
2020
2021 call feedkeys("\<CR>", 'L')
2022 let a = confirm('Are you sure?', "&Yes\n&No", 0)
2023 call assert_equal(0, a)
2024
2025 " Test with the {type} 4th argument
2026 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
2027 call feedkeys('y', 'L')
2028 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
2029 call assert_equal(1, a)
2030 endfor
2031
2032 call assert_fails('call confirm([])', 'E730:')
2033 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
2034 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
2035 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
2036endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002037
2038func Test_platform_name()
2039 " The system matches at most only one name.
Bram Moolenaar041c7102020-05-30 18:14:57 +02002040 let names = ['amiga', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002041 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
2042
2043 " Is Unix?
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002044 call assert_equal(has('bsd'), has('bsd') && has('unix'))
2045 call assert_equal(has('hpux'), has('hpux') && has('unix'))
2046 call assert_equal(has('linux'), has('linux') && has('unix'))
2047 call assert_equal(has('mac'), has('mac') && has('unix'))
2048 call assert_equal(has('qnx'), has('qnx') && has('unix'))
2049 call assert_equal(has('sun'), has('sun') && has('unix'))
2050 call assert_equal(has('win32'), has('win32') && !has('unix'))
2051 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
2052
2053 if has('unix') && executable('uname')
2054 let uname = system('uname')
Bram Moolenaara02e3f62019-02-07 21:27:14 +01002055 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
2056 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002057 call assert_equal(uname =~? 'HP-UX', has('hpux'))
2058 call assert_equal(uname =~? 'Linux', has('linux'))
2059 call assert_equal(uname =~? 'Darwin', has('mac'))
2060 call assert_equal(uname =~? 'QNX', has('qnx'))
2061 call assert_equal(uname =~? 'SunOS', has('sun'))
2062 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
2063 endif
2064endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002065
2066func Test_readdir()
Bram Moolenaar70e67252022-09-27 19:34:35 +01002067 call mkdir('Xreaddir', 'R')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002068 call writefile([], 'Xreaddir/foo.txt')
2069 call writefile([], 'Xreaddir/bar.txt')
2070 call mkdir('Xreaddir/dir')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002071
2072 " All results
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002073 let files = readdir('Xreaddir')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002074 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
2075
2076 " Only results containing "f"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002077 let files = 'Xreaddir'->readdir({ x -> stridx(x, 'f') != -1 })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002078 call assert_equal(['foo.txt'], sort(files))
2079
2080 " Only .txt files
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002081 let files = readdir('Xreaddir', { x -> x =~ '.txt$' })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002082 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
2083
2084 " Only .txt files with string
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002085 let files = readdir('Xreaddir', 'v:val =~ ".txt$"')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002086 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
2087
2088 " Limit to 1 result.
2089 let l = []
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002090 let files = readdir('Xreaddir', {x -> len(add(l, x)) == 2 ? -1 : 1})
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002091 call assert_equal(1, len(files))
2092
Bram Moolenaar27da7de2019-09-03 17:13:37 +02002093 " Nested readdir() must not crash
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002094 let files = readdir('Xreaddir', 'readdir("Xreaddir", "1") != []')
Bram Moolenaar27da7de2019-09-03 17:13:37 +02002095 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002096endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02002097
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002098func Test_readdirex()
Bram Moolenaar70e67252022-09-27 19:34:35 +01002099 call mkdir('Xexdir', 'R')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002100 call writefile(['foo'], 'Xexdir/foo.txt')
2101 call writefile(['barbar'], 'Xexdir/bar.txt')
2102 call mkdir('Xexdir/dir')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002103
2104 " All results
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002105 let files = readdirex('Xexdir')->map({-> v:val.name})
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002106 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002107 let sizes = readdirex('Xexdir')->map({-> v:val.size})
Bram Moolenaar441d60e2020-06-02 22:19:50 +02002108 call assert_equal([0, 4, 7], sort(sizes))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002109
2110 " Only results containing "f"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002111 let files = 'Xexdir'->readdirex({ e -> stridx(e.name, 'f') != -1 })
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002112 \ ->map({-> v:val.name})
2113 call assert_equal(['foo.txt'], sort(files))
2114
2115 " Only .txt files
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002116 let files = readdirex('Xexdir', { e -> e.name =~ '.txt$' })
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002117 \ ->map({-> v:val.name})
2118 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
2119
2120 " Only .txt files with string
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002121 let files = readdirex('Xexdir', 'v:val.name =~ ".txt$"')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002122 \ ->map({-> v:val.name})
2123 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
2124
2125 " Limit to 1 result.
2126 let l = []
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002127 let files = readdirex('Xexdir', {e -> len(add(l, e.name)) == 2 ? -1 : 1})
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002128 \ ->map({-> v:val.name})
2129 call assert_equal(1, len(files))
2130
2131 " Nested readdirex() must not crash
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002132 let files = readdirex('Xexdir', 'readdirex("Xexdir", "1") != []')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002133 \ ->map({-> v:val.name})
2134 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
2135
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002136 " report broken link correctly
Bram Moolenaarab540322020-06-10 15:55:36 +02002137 if has("unix")
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002138 call writefile([], 'Xexdir/abc.txt')
2139 call system("ln -s Xexdir/abc.txt Xexdir/link")
2140 call delete('Xexdir/abc.txt')
2141 let files = readdirex('Xexdir', 'readdirex("Xexdir", "1") != []')
Bram Moolenaarab540322020-06-10 15:55:36 +02002142 \ ->map({-> v:val.name .. '_' .. v:val.type})
2143 call sort(files)->assert_equal(
2144 \ ['bar.txt_file', 'dir_dir', 'foo.txt_file', 'link_link'])
2145 endif
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02002146
2147 call assert_fails('call readdirex("doesnotexist")', 'E484:')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002148endfunc
2149
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002150func Test_readdirex_sort()
2151 CheckUnix
2152 " Skip tests on Mac OS X and Cygwin (does not allow several files with different casing)
2153 if has("osxdarwin") || has("osx") || has("macunix") || has("win32unix")
2154 throw 'Skipped: Test_readdirex_sort on systems that do not allow this using the default filesystem'
2155 endif
2156 let _collate = v:collate
Bram Moolenaar70e67252022-09-27 19:34:35 +01002157 call mkdir('Xsortdir2', 'R')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002158 call writefile(['1'], 'Xsortdir2/README.txt')
2159 call writefile(['2'], 'Xsortdir2/Readme.txt')
2160 call writefile(['3'], 'Xsortdir2/readme.txt')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002161
2162 " 1) default
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002163 let files = readdirex('Xsortdir2')->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002164 let default = copy(files)
2165 call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort using default')
2166
2167 " 2) no sorting
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002168 let files = readdirex('Xsortdir2', 1, #{sort: 'none'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002169 let unsorted = copy(files)
2170 call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], sort(files), 'unsorted')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002171 call assert_fails("call readdirex('Xsortdir2', 1, #{slort: 'none'})", 'E857: Dictionary key "sort" required')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002172
2173 " 3) sort by case (same as default)
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002174 let files = readdirex('Xsortdir2', 1, #{sort: 'case'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002175 call assert_equal(default, files, 'sort by case')
2176
2177 " 4) sort by ignoring case
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002178 let files = readdirex('Xsortdir2', 1, #{sort: 'icase'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002179 call assert_equal(unsorted->sort('i'), files, 'sort by icase')
2180
2181 " 5) Default Collation
2182 let collate = v:collate
2183 lang collate C
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002184 let files = readdirex('Xsortdir2', 1, #{sort: 'collate'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002185 call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort by C collation')
2186
2187 " 6) Collation de_DE
2188 " Switch locale, this may not work on the CI system, if the locale isn't
2189 " available
2190 try
2191 lang collate de_DE
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002192 let files = readdirex('Xsortdir2', 1, #{sort: 'collate'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002193 call assert_equal(['readme.txt', 'Readme.txt', 'README.txt'], files, 'sort by de_DE collation')
2194 catch
2195 throw 'Skipped: de_DE collation is not available'
2196
2197 finally
2198 exe 'lang collate' collate
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002199 endtry
2200endfunc
2201
2202func Test_readdir_sort()
2203 " some more cases for testing sorting for readdirex
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002204 let dir = 'Xsortdir3'
Bram Moolenaar70e67252022-09-27 19:34:35 +01002205 call mkdir(dir, 'R')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002206 call writefile(['1'], dir .. '/README.txt')
2207 call writefile(['2'], dir .. '/Readm.txt')
2208 call writefile(['3'], dir .. '/read.txt')
2209 call writefile(['4'], dir .. '/Z.txt')
2210 call writefile(['5'], dir .. '/a.txt')
2211 call writefile(['6'], dir .. '/b.txt')
2212
2213 " 1) default
2214 let files = readdir(dir)
2215 let default = copy(files)
2216 call assert_equal(default->sort(), files, 'sort using default')
2217
2218 " 2) sort by case (same as default)
2219 let files = readdir(dir, '1', #{sort: 'case'})
2220 call assert_equal(default, files, 'sort using default')
2221
2222 " 3) sort by ignoring case
2223 let files = readdir(dir, '1', #{sort: 'icase'})
2224 call assert_equal(default->sort('i'), files, 'sort by ignoring case')
2225
Bram Moolenaare17f8812020-06-17 20:30:44 +02002226 " 4) collation
2227 let collate = v:collate
2228 lang collate C
2229 let files = readdir(dir, 1, #{sort: 'collate'})
2230 call assert_equal(default->sort(), files, 'sort by C collation')
2231 exe "lang collate" collate
2232
2233 " 5) Errors
Bram Moolenaard83392a2022-09-01 12:22:46 +01002234 call assert_fails('call readdir(dir, 1, 1)', 'E1206:')
Bram Moolenaare17f8812020-06-17 20:30:44 +02002235 call assert_fails('call readdir(dir, 1, #{sorta: 1})')
Bram Moolenaard83392a2022-09-01 12:22:46 +01002236 call assert_fails('call readdir(dir, 1, test_null_dict())', 'E1297:')
2237 call assert_fails('call readdirex(dir, 1, 1)', 'E1206:')
Bram Moolenaare17f8812020-06-17 20:30:44 +02002238 call assert_fails('call readdirex(dir, 1, #{sorta: 1})')
Bram Moolenaard83392a2022-09-01 12:22:46 +01002239 call assert_fails('call readdirex(dir, 1, test_null_dict())', 'E1297:')
Bram Moolenaare17f8812020-06-17 20:30:44 +02002240
2241 " 6) ignore other values in dict
2242 let files = readdir(dir, '1', #{sort: 'c'})
2243 call assert_equal(default, files, 'sort using default2')
2244
2245 " Cleanup
2246 exe "lang collate" collate
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002247endfunc
2248
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02002249func Test_delete_rf()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002250 call mkdir('Xrfdir')
2251 call writefile([], 'Xrfdir/foo.txt')
2252 call writefile([], 'Xrfdir/bar.txt')
2253 call mkdir('Xrfdir/[a-1]') " issue #696
2254 call writefile([], 'Xrfdir/[a-1]/foo.txt')
2255 call writefile([], 'Xrfdir/[a-1]/bar.txt')
2256 call assert_true(filereadable('Xrfdir/foo.txt'))
2257 call assert_true('Xrfdir/[a-1]/foo.txt'->filereadable())
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02002258
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002259 call assert_equal(0, delete('Xrfdir', 'rf'))
2260 call assert_false(filereadable('Xrfdir/foo.txt'))
2261 call assert_false(filereadable('Xrfdir/[a-1]/foo.txt'))
zeertzjq47870032022-04-05 15:31:01 +01002262
2263 if has('unix')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002264 call mkdir('Xrfdir/Xdir2', 'p')
2265 silent !chmod 555 Xrfdir
2266 call assert_equal(-1, delete('Xrfdir/Xdir2', 'rf'))
2267 call assert_equal(-1, delete('Xrfdir', 'rf'))
2268 silent !chmod 755 Xrfdir
2269 call assert_equal(0, delete('Xrfdir', 'rf'))
zeertzjq47870032022-04-05 15:31:01 +01002270 endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02002271endfunc
2272
Bram Moolenaar17aca702019-05-16 22:24:55 +02002273func Test_call()
2274 call assert_equal(3, call('len', [123]))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02002275 call assert_equal(3, 'len'->call([123]))
Bram Moolenaard83392a2022-09-01 12:22:46 +01002276 call assert_fails("call call('len', 123)", 'E1211:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02002277 call assert_equal(0, call('', []))
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002278 call assert_equal(0, call('len', test_null_list()))
Bram Moolenaar17aca702019-05-16 22:24:55 +02002279
2280 function Mylen() dict
2281 return len(self.data)
2282 endfunction
2283 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
Bram Moolenaar64b4d732019-08-22 22:18:17 +02002284 eval mydict.len->call([], mydict)->assert_equal(4)
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002285 call assert_fails("call call('Mylen', [], 0)", 'E1206:')
Bram Moolenaar67322bf2020-12-06 15:03:19 +01002286 call assert_fails('call foo', 'E107:')
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002287
Bram Moolenaar22db0d52021-06-12 12:16:55 +02002288 " These once caused a crash.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002289 call call(test_null_function(), [])
2290 call call(test_null_partial(), [])
Bram Moolenaar22db0d52021-06-12 12:16:55 +02002291 call assert_fails('call test_null_function()()', 'E1192:')
2292 call assert_fails('call test_null_partial()()', 'E117:')
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002293
2294 let lines =<< trim END
2295 let Time = 'localtime'
2296 call Time()
2297 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002298 call v9.CheckScriptFailure(lines, 'E1085:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02002299endfunc
2300
2301func Test_char2nr()
2302 call assert_equal(12354, char2nr('あ', 1))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02002303 call assert_equal(120, 'x'->char2nr())
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002304 set encoding=latin1
2305 call assert_equal(120, 'x'->char2nr())
2306 set encoding=utf-8
Bram Moolenaar17aca702019-05-16 22:24:55 +02002307endfunc
2308
Bram Moolenaar4e4473c2020-08-28 22:24:57 +02002309func Test_charclass()
2310 call assert_equal(0, charclass(' '))
2311 call assert_equal(1, charclass('.'))
2312 call assert_equal(2, charclass('x'))
2313 call assert_equal(3, charclass("\u203c"))
Christian Brabandt72463f82021-07-02 20:19:31 +02002314 " this used to crash vim
2315 call assert_equal(0, "xxx"[-1]->charclass())
Bram Moolenaar4e4473c2020-08-28 22:24:57 +02002316endfunc
2317
Bram Moolenaar17aca702019-05-16 22:24:55 +02002318func Test_eventhandler()
2319 call assert_equal(0, eventhandler())
2320endfunc
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002321
2322func Test_bufadd_bufload()
2323 call assert_equal(0, bufexists('someName'))
2324 let buf = bufadd('someName')
2325 call assert_notequal(0, buf)
2326 call assert_equal(1, bufexists('someName'))
2327 call assert_equal(0, getbufvar(buf, '&buflisted'))
2328 call assert_equal(0, bufloaded(buf))
2329 call bufload(buf)
2330 call assert_equal(1, bufloaded(buf))
2331 call assert_equal([''], getbufline(buf, 1, '$'))
2332
2333 let curbuf = bufnr('')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02002334 eval ['some', 'text']->writefile('XotherName')
Bram Moolenaar073e4b92019-08-18 23:01:56 +02002335 let buf = 'XotherName'->bufadd()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002336 call assert_notequal(0, buf)
Bram Moolenaar073e4b92019-08-18 23:01:56 +02002337 eval 'XotherName'->bufexists()->assert_equal(1)
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002338 call assert_equal(0, getbufvar(buf, '&buflisted'))
2339 call assert_equal(0, bufloaded(buf))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02002340 eval buf->bufload()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002341 call assert_equal(1, bufloaded(buf))
2342 call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
2343 call assert_equal(curbuf, bufnr(''))
2344
Bram Moolenaar892ae722019-06-30 20:33:01 +02002345 let buf1 = bufadd('')
2346 let buf2 = bufadd('')
2347 call assert_notequal(0, buf1)
2348 call assert_notequal(0, buf2)
2349 call assert_notequal(buf1, buf2)
2350 call assert_equal(1, bufexists(buf1))
2351 call assert_equal(1, bufexists(buf2))
2352 call assert_equal(0, bufloaded(buf1))
2353 exe 'bwipe ' .. buf1
2354 call assert_equal(0, bufexists(buf1))
2355 call assert_equal(1, bufexists(buf2))
2356 exe 'bwipe ' .. buf2
2357 call assert_equal(0, bufexists(buf2))
2358
zeertzjq93f72cc2022-08-26 15:34:52 +01002359 " When 'buftype' is "nofile" then bufload() does not read the file.
2360 " Other values too.
2361 for val in [['nofile', 0],
2362 \ ['nowrite', 1],
2363 \ ['acwrite', 1],
2364 \ ['quickfix', 0],
2365 \ ['help', 1],
2366 \ ['terminal', 0],
2367 \ ['prompt', 0],
2368 \ ['popup', 0],
2369 \ ]
2370 bwipe! XotherName
2371 let buf = bufadd('XotherName')
2372 call setbufvar(buf, '&bt', val[0])
2373 call bufload(buf)
2374 call assert_equal(val[1] ? ['some', 'text'] : [''], getbufline(buf, 1, '$'), val[0])
2375 endfor
Bram Moolenaarc3126192022-08-26 12:58:17 +01002376
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002377 bwipe someName
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002378 bwipe XotherName
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002379 call assert_equal(0, bufexists('someName'))
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002380 call delete('XotherName')
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002381endfunc
Bram Moolenaarc2585492019-09-22 21:29:53 +02002382
2383func Test_state()
2384 CheckRunVimInTerminal
2385
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +01002386 let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"
2387
Bram Moolenaarc2585492019-09-22 21:29:53 +02002388 let lines =<< trim END
2389 call setline(1, ['one', 'two', 'three'])
2390 map ;; gg
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02002391 set complete=.
Bram Moolenaarc2585492019-09-22 21:29:53 +02002392 func RunTimer()
2393 call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
2394 endfunc
2395 au Filetype foobar let g:state = state()|let g:mode = mode()
2396 END
2397 call writefile(lines, 'XState')
2398 let buf = RunVimInTerminal('-S XState', #{rows: 6})
2399
2400 " Using a ":" command Vim is busy, thus "S" is returned
2401 call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
2402 call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
2403 call term_sendkeys(buf, ":\<CR>")
2404
2405 " Using a timer callback
2406 call term_sendkeys(buf, ":call RunTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002407 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002408 call term_sendkeys(buf, getstate)
2409 call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)
2410
2411 " Halfway a mapping
2412 call term_sendkeys(buf, ":call RunTimer()\<CR>;")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002413 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002414 call term_sendkeys(buf, ";")
2415 call term_sendkeys(buf, getstate)
2416 call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)
2417
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02002418 " Insert mode completion (bit slower on Mac)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002419 call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002420 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002421 call term_sendkeys(buf, "\<Esc>")
2422 call term_sendkeys(buf, getstate)
2423 call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)
2424
2425 " Autocommand executing
2426 call term_sendkeys(buf, ":set filetype=foobar\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002427 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002428 call term_sendkeys(buf, getstate)
2429 call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)
2430
2431 " Todo: "w" - waiting for ch_evalexpr()
2432
2433 " messages scrolled
2434 call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002435 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002436 call term_sendkeys(buf, "\<CR>")
2437 call term_sendkeys(buf, getstate)
2438 call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)
2439
2440 call StopVimInTerminal(buf)
2441 call delete('XState')
2442endfunc
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002443
2444func Test_range()
2445 " destructuring
2446 let [x, y] = range(2)
2447 call assert_equal([0, 1], [x, y])
2448
2449 " index
2450 call assert_equal(4, range(1, 10)[3])
2451
2452 " add()
2453 call assert_equal([0, 1, 2, 3], add(range(3), 3))
2454 call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3)))
2455 call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3)))
2456
2457 " append()
2458 new
2459 call append('.', range(5))
2460 call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$'))
2461 bwipe!
2462
2463 " appendbufline()
2464 new
2465 call appendbufline(bufnr(''), '.', range(5))
2466 call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$'))
2467 bwipe!
2468
2469 " call()
2470 func TwoArgs(a, b)
2471 return [a:a, a:b]
2472 endfunc
2473 call assert_equal([0, 1], call('TwoArgs', range(2)))
2474
2475 " col()
2476 new
2477 call setline(1, ['foo', 'bar'])
2478 call assert_equal(2, col(range(1, 2)))
2479 bwipe!
2480
2481 " complete()
2482 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>"
2483 " complete_info()
2484 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>"
2485
2486 " copy()
2487 call assert_equal([1, 2, 3], copy(range(1, 3)))
2488
2489 " count()
2490 call assert_equal(0, count(range(0), 3))
2491 call assert_equal(0, count(range(2), 3))
2492 call assert_equal(1, count(range(5), 3))
2493
2494 " cursor()
2495 new
2496 call setline(1, ['aaa', 'bbb', 'ccc'])
2497 call cursor(range(1, 2))
2498 call assert_equal([2, 1], [col('.'), line('.')])
2499 bwipe!
2500
2501 " deepcopy()
2502 call assert_equal([1, 2, 3], deepcopy(range(1, 3)))
2503
2504 " empty()
2505 call assert_true(empty(range(0)))
2506 call assert_false(empty(range(2)))
2507
2508 " execute()
2509 new
2510 call setline(1, ['aaa', 'bbb', 'ccc'])
2511 call execute(range(3))
2512 call assert_equal(2, line('.'))
2513 bwipe!
2514
2515 " extend()
2516 call assert_equal([1, 2, 3, 4], extend([1], range(2, 4)))
2517 call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4)))
2518 call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4]))
2519
2520 " filter()
2521 call assert_equal([1, 3], filter(range(5), 'v:val % 2'))
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002522 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 +01002523
2524 " funcref()
2525 call assert_equal([0, 1], funcref('TwoArgs', range(2))())
2526
2527 " function()
2528 call assert_equal([0, 1], function('TwoArgs', range(2))())
2529
2530 " garbagecollect()
2531 let thelist = [1, range(2), 3]
2532 let otherlist = range(3)
2533 call test_garbagecollect_now()
2534
2535 " get()
2536 call assert_equal(4, get(range(1, 10), 3))
2537 call assert_equal(-1, get(range(1, 10), 42, -1))
2538
2539 " index()
2540 call assert_equal(1, index(range(1, 5), 2))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002541 call assert_fails("echo index([1, 2], 1, [])", 'E745:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002542
2543 " inputlist()
Bram Moolenaar272ca952020-01-28 20:49:11 +01002544 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x')
2545 call assert_equal(1, result)
2546 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x')
2547 call assert_equal(1, result)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002548
2549 " insert()
2550 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42))
2551 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0))
2552 call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1))
2553 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4))
2554 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1))
2555 call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5))
2556
2557 " join()
2558 call assert_equal('0 1 2 3 4', join(range(5)))
2559
Bram Moolenaar272ca952020-01-28 20:49:11 +01002560 " json_encode()
2561 call assert_equal('[0,1,2,3]', json_encode(range(4)))
2562
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002563 " len()
2564 call assert_equal(0, len(range(0)))
2565 call assert_equal(2, len(range(2)))
2566 call assert_equal(5, len(range(0, 12, 3)))
2567 call assert_equal(4, len(range(3, 0, -1)))
2568
2569 " list2str()
2570 call assert_equal('ABC', list2str(range(65, 67)))
Bram Moolenaard83392a2022-09-01 12:22:46 +01002571 call assert_fails('let s = list2str(5)', 'E1211:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002572
2573 " lock()
2574 let thelist = range(5)
2575 lockvar thelist
2576
2577 " map()
2578 call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2'))
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002579 call assert_equal([3, 5, 7, 9, 11], map(map(range(5), 'v:val * 2'), 'v:val + 3'))
2580 call assert_equal([2, 6], map(filter(range(5), 'v:val % 2'), 'v:val * 2'))
2581 call assert_equal([2, 4, 8], filter(map(range(5), 'v:val * 2'), 'v:val % 3'))
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002582
2583 " match()
2584 call assert_equal(3, match(range(5), 3))
2585
2586 " matchaddpos()
2587 highlight MyGreenGroup ctermbg=green guibg=green
2588 call matchaddpos('MyGreenGroup', range(line('.'), line('.')))
2589
2590 " matchend()
2591 call assert_equal(4, matchend(range(5), '4'))
2592 call assert_equal(3, matchend(range(1, 5), '4'))
2593 call assert_equal(-1, matchend(range(1, 5), '42'))
2594
2595 " matchstrpos()
2596 call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4'))
2597 call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4'))
2598 call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42'))
2599
2600 " max() reverse()
2601 call assert_equal(0, max(range(0)))
2602 call assert_equal(0, max(range(10, 9)))
2603 call assert_equal(9, max(range(10)))
2604 call assert_equal(18, max(range(0, 20, 3)))
2605 call assert_equal(20, max(range(20, 0, -3)))
2606 call assert_equal(99999, max(range(100000)))
2607 call assert_equal(99999, max(range(99999, 0, -1)))
2608 call assert_equal(99999, max(reverse(range(100000))))
2609 call assert_equal(99999, max(reverse(range(99999, 0, -1))))
2610
2611 " min() reverse()
2612 call assert_equal(0, min(range(0)))
2613 call assert_equal(0, min(range(10, 9)))
2614 call assert_equal(5, min(range(5, 10)))
2615 call assert_equal(5, min(range(5, 10, 3)))
2616 call assert_equal(2, min(range(20, 0, -3)))
2617 call assert_equal(0, min(range(100000)))
2618 call assert_equal(0, min(range(99999, 0, -1)))
2619 call assert_equal(0, min(reverse(range(100000))))
2620 call assert_equal(0, min(reverse(range(99999, 0, -1))))
2621
2622 " remove()
2623 call assert_equal(1, remove(range(1, 10), 0))
2624 call assert_equal(2, remove(range(1, 10), 1))
2625 call assert_equal(9, remove(range(1, 10), 8))
2626 call assert_equal(10, remove(range(1, 10), 9))
2627 call assert_equal(10, remove(range(1, 10), -1))
2628 call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4))
2629
2630 " repeat()
2631 call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2))
2632 call assert_equal([0, 1, 2], repeat(range(3), 1))
2633 call assert_equal([], repeat(range(3), 0))
2634 call assert_equal([], repeat(range(5, 4), 2))
2635 call assert_equal([], repeat(range(5, 4), 0))
2636
2637 " reverse()
2638 call assert_equal([2, 1, 0], reverse(range(3)))
2639 call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1)))
2640 call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10)))
2641 call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20)))
2642 call assert_equal([16, 13, 10], reverse(range(10, 18, 3)))
2643 call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3)))
2644 call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3)))
2645 call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3)))
2646 call assert_equal([], reverse(range(0)))
2647
2648 " TODO: setpos()
2649 " new
2650 " call setline(1, repeat([''], bufnr('')))
2651 " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6))
2652 " call setpos('x', range(bufnr(''), bufnr('') + 3))
2653 " bwipe!
2654
2655 " setreg()
2656 call setreg('a', range(3))
2657 call assert_equal("0\n1\n2\n", getreg('a'))
2658
Bram Moolenaarb0992022020-01-30 14:55:42 +01002659 " settagstack()
2660 call settagstack(1, #{items : range(4)})
Bram Moolenaar94255df2020-02-05 20:10:33 +01002661
Bram Moolenaarb0992022020-01-30 14:55:42 +01002662 " sign_define()
2663 call assert_fails("call sign_define(range(5))", "E715:")
2664 call assert_fails("call sign_placelist(range(5))", "E715:")
2665
2666 " sign_undefine()
2667 call assert_fails("call sign_undefine(range(5))", "E908:")
2668
2669 " sign_unplacelist()
2670 call assert_fails("call sign_unplacelist(range(5))", "E715:")
2671
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002672 " sort()
2673 call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1)))
2674
2675 " string()
2676 call assert_equal('[0, 1, 2, 3, 4]', string(range(5)))
2677
Bram Moolenaarb0992022020-01-30 14:55:42 +01002678 " taglist() with 'tagfunc'
2679 func TagFunc(pattern, flags, info)
2680 return range(10)
2681 endfunc
2682 set tagfunc=TagFunc
2683 call assert_fails("call taglist('asdf')", 'E987:')
2684 set tagfunc=
Bram Moolenaar94255df2020-02-05 20:10:33 +01002685
Bram Moolenaarb0992022020-01-30 14:55:42 +01002686 " term_start()
Bram Moolenaar705724e2020-01-31 21:13:42 +01002687 if has('terminal') && has('termguicolors')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002688 call assert_fails('call term_start(range(3, 4))', 'E474:')
2689 let g:terminal_ansi_colors = range(16)
Bram Moolenaar94255df2020-02-05 20:10:33 +01002690 if has('win32')
2691 let cmd = "cmd /c dir"
2692 else
2693 let cmd = "ls"
2694 endif
LemonBoyb2b3acb2022-05-20 10:10:34 +01002695 call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"'
2696 \ .. ', ansi_colors: range(16)})', 'E475:')
Bram Moolenaarb0855f52022-05-20 10:39:18 +01002697 unlet g:terminal_ansi_colors
Bram Moolenaarb0992022020-01-30 14:55:42 +01002698 endif
2699
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002700 " type()
2701 call assert_equal(v:t_list, type(range(5)))
2702
2703 " uniq()
2704 call assert_equal([0, 1, 2, 3, 4], uniq(range(5)))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002705
2706 " errors
2707 call assert_fails('let x=range(2, 8, 0)', 'E726:')
2708 call assert_fails('let x=range(3, 1)', 'E727:')
2709 call assert_fails('let x=range(1, 3, -2)', 'E727:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002710 call assert_fails('let x=range([])', 'E745:')
2711 call assert_fails('let x=range(1, [])', 'E745:')
2712 call assert_fails('let x=range(1, 4, [])', 'E745:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002713endfunc
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002714
Bram Moolenaarb3d83982022-01-27 19:59:47 +00002715func Test_garbagecollect_now_fails()
2716 let v:testing = 0
2717 call assert_fails('call test_garbagecollect_now()', 'E1142:')
2718 let v:testing = 1
2719endfunc
2720
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002721func Test_echoraw()
2722 CheckScreendump
2723
2724 " Normally used for escape codes, but let's test with a CR.
2725 let lines =<< trim END
2726 call echoraw("hello\<CR>x")
2727 END
2728 call writefile(lines, 'XTest_echoraw')
2729 let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40})
2730 call VerifyScreenDump(buf, 'Test_functions_echoraw', {})
2731
2732 " clean up
2733 call StopVimInTerminal(buf)
2734 call delete('XTest_echoraw')
2735endfunc
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002736
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002737" Test for echo highlighting
2738func Test_echohl()
2739 echohl Search
2740 echo 'Vim'
2741 call assert_equal('Vim', Screenline(&lines))
2742 " TODO: How to check the highlight group used by echohl?
2743 " ScreenAttrs() returns all zeros.
2744 echohl None
2745endfunc
2746
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002747" Test for the eval() function
2748func Test_eval()
2749 call assert_fails("call eval('5 a')", 'E488:')
2750endfunc
2751
zeertzjqcdc83932022-09-12 13:38:41 +01002752" Test for the keytrans() function
2753func Test_keytrans()
2754 call assert_equal('<Space>', keytrans(' '))
2755 call assert_equal('<lt>', keytrans('<'))
2756 call assert_equal('<lt>Tab>', keytrans('<Tab>'))
2757 call assert_equal('<Tab>', keytrans("\<Tab>"))
2758 call assert_equal('<C-V>', keytrans("\<C-V>"))
2759 call assert_equal('<BS>', keytrans("\<BS>"))
2760 call assert_equal('<Home>', keytrans("\<Home>"))
2761 call assert_equal('<C-Home>', keytrans("\<C-Home>"))
2762 call assert_equal('<M-Home>', keytrans("\<M-Home>"))
2763 call assert_equal('<C-Space>', keytrans("\<C-Space>"))
2764 call assert_equal('<M-Space>', keytrans("\<*M-Space>"))
2765 call assert_equal('<M-x>', "\<*M-x>"->keytrans())
2766 call assert_equal('<C-I>', "\<*C-I>"->keytrans())
2767 call assert_equal('<S-3>', "\<*S-3>"->keytrans())
2768 call assert_equal('π', 'π'->keytrans())
2769 call assert_equal('<M-π>', "\<M-π>"->keytrans())
2770 call assert_equal('ě', 'ě'->keytrans())
2771 call assert_equal('<M-ě>', "\<M-ě>"->keytrans())
2772 call assert_equal('', ''->keytrans())
2773 call assert_equal('', test_null_string()->keytrans())
2774 call assert_fails('call keytrans(1)', 'E1174:')
2775 call assert_fails('call keytrans()', 'E119:')
2776endfunc
2777
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002778" Test for the nr2char() function
2779func Test_nr2char()
2780 set encoding=latin1
2781 call assert_equal('@', nr2char(64))
2782 set encoding=utf8
2783 call assert_equal('a', nr2char(97, 1))
2784 call assert_equal('a', nr2char(97, 0))
Bram Moolenaarf7271e82020-05-24 18:45:07 +02002785
zeertzjqdb088872022-05-02 22:53:45 +01002786 call assert_equal("\x80\xfc\b" .. nr2char(0x100000), eval('"\<M-' .. nr2char(0x100000) .. '>"'))
2787 call assert_equal("\x80\xfc\b" .. nr2char(0x40000000), eval('"\<M-' .. nr2char(0x40000000) .. '>"'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002788endfunc
2789
2790" Test for screenattr(), screenchar() and screenchars() functions
2791func Test_screen_functions()
2792 call assert_equal(-1, screenattr(-1, -1))
2793 call assert_equal(-1, screenchar(-1, -1))
2794 call assert_equal([], screenchars(-1, -1))
2795endfunc
2796
Bram Moolenaar08f41572020-04-20 16:50:00 +02002797" Test for getcurpos() and setpos()
2798func Test_getcurpos_setpos()
2799 new
2800 call setline(1, ['012345678', '012345678'])
2801 normal gg6l
2802 let sp = getcurpos()
2803 normal 0
2804 call setpos('.', sp)
2805 normal jyl
2806 call assert_equal('6', @")
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002807 call assert_equal(-1, setpos('.', test_null_list()))
2808 call assert_equal(-1, setpos('.', {}))
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02002809
2810 let winid = win_getid()
2811 normal G$
2812 let pos = getcurpos()
2813 wincmd w
2814 call assert_equal(pos, getcurpos(winid))
2815
2816 wincmd w
Bram Moolenaar08f41572020-04-20 16:50:00 +02002817 close!
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02002818
2819 call assert_equal(getcurpos(), getcurpos(0))
2820 call assert_equal([0, 0, 0, 0, 0], getcurpos(-1))
2821 call assert_equal([0, 0, 0, 0, 0], getcurpos(1999))
Bram Moolenaar08f41572020-04-20 16:50:00 +02002822endfunc
2823
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00002824func Test_getmousepos()
2825 enew!
2826 call setline(1, "\t\t\t1234")
Bram Moolenaar533870a2022-03-13 15:52:44 +00002827 call test_setmouse(1, 1)
2828 call assert_equal(#{
2829 \ screenrow: 1,
2830 \ screencol: 1,
2831 \ winid: win_getid(),
2832 \ winrow: 1,
2833 \ wincol: 1,
2834 \ line: 1,
2835 \ column: 1,
2836 \ }, getmousepos())
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00002837 call test_setmouse(1, 25)
2838 call assert_equal(#{
2839 \ screenrow: 1,
2840 \ screencol: 25,
2841 \ winid: win_getid(),
2842 \ winrow: 1,
2843 \ wincol: 25,
2844 \ line: 1,
Bram Moolenaar533870a2022-03-13 15:52:44 +00002845 \ column: 4,
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00002846 \ }, getmousepos())
2847 call test_setmouse(1, 50)
2848 call assert_equal(#{
2849 \ screenrow: 1,
2850 \ screencol: 50,
2851 \ winid: win_getid(),
2852 \ winrow: 1,
2853 \ wincol: 50,
2854 \ line: 1,
Bram Moolenaar533870a2022-03-13 15:52:44 +00002855 \ column: 8,
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00002856 \ }, getmousepos())
Sean Dewar10792fe2022-03-15 09:46:54 +00002857
2858 " If the mouse is positioned past the last buffer line, "line" and "column"
2859 " should act like it's positioned on the last buffer line.
2860 call test_setmouse(2, 25)
2861 call assert_equal(#{
2862 \ screenrow: 2,
2863 \ screencol: 25,
2864 \ winid: win_getid(),
2865 \ winrow: 2,
2866 \ wincol: 25,
2867 \ line: 1,
2868 \ column: 4,
2869 \ }, getmousepos())
2870 call test_setmouse(2, 50)
2871 call assert_equal(#{
2872 \ screenrow: 2,
2873 \ screencol: 50,
2874 \ winid: win_getid(),
2875 \ winrow: 2,
2876 \ wincol: 50,
2877 \ line: 1,
2878 \ column: 8,
2879 \ }, getmousepos())
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00002880 bwipe!
2881endfunc
2882
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002883" Test for glob()
2884func Test_glob()
2885 call assert_equal('', glob(test_null_string()))
2886 call assert_equal('', globpath(test_null_string(), test_null_string()))
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002887 call assert_fails("let x = globpath(&rtp, 'syntax/c.vim', [])", 'E745:')
Bram Moolenaar1b04ce22020-08-21 22:46:11 +02002888
2889 call writefile([], 'Xglob1')
2890 call writefile([], 'XGLOB2')
2891 set wildignorecase
2892 " Sort output of glob() otherwise we end up with different
2893 " ordering depending on whether file system is case-sensitive.
2894 call assert_equal(['XGLOB2', 'Xglob1'], sort(glob('Xglob[12]', 0, 1)))
LemonBoya3157a42022-04-03 11:58:31 +01002895 " wildignorecase shall be applied even when the pattern contains no wildcards.
2896 call assert_equal('XGLOB2', glob('xglob2'))
Bram Moolenaar1b04ce22020-08-21 22:46:11 +02002897 set wildignorecase&
2898
2899 call delete('Xglob1')
2900 call delete('XGLOB2')
2901
2902 call assert_fails("call glob('*', 0, {})", 'E728:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002903endfunc
2904
2905" Test for browse()
2906func Test_browse()
2907 CheckFeature browse
2908 call assert_fails('call browse([], "open", "x", "a.c")', 'E745:')
2909endfunc
2910
2911" Test for browsedir()
2912func Test_browsedir()
2913 CheckFeature browse
2914 call assert_fails('call browsedir("open", [])', 'E730:')
2915endfunc
2916
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002917func HasDefault(msg = 'msg')
2918 return a:msg
2919endfunc
2920
2921func Test_default_arg_value()
2922 call assert_equal('msg', HasDefault())
2923endfunc
2924
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02002925" Test for gettext()
2926func Test_gettext()
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01002927 call assert_fails('call gettext(1)', 'E1174:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02002928endfunc
2929
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002930func Test_builtin_check()
2931 call assert_fails('let g:["trim"] = {x -> " " .. x}', 'E704:')
2932 call assert_fails('let g:.trim = {x -> " " .. x}', 'E704:')
Bram Moolenaarb54abee2021-06-02 11:49:23 +02002933 call assert_fails('let l:["trim"] = {x -> " " .. x}', 'E704:')
2934 call assert_fails('let l:.trim = {x -> " " .. x}', 'E704:')
2935 let lines =<< trim END
2936 vim9script
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002937 var trim = (x) => " " .. x
Bram Moolenaarb54abee2021-06-02 11:49:23 +02002938 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002939 call v9.CheckScriptFailure(lines, 'E704:')
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002940
2941 call assert_fails('call extend(g:, #{foo: { -> "foo" }})', 'E704:')
2942 let g:bar = 123
2943 call extend(g:, #{bar: { -> "foo" }}, "keep")
2944 call assert_fails('call extend(g:, #{bar: { -> "foo" }}, "force")', 'E704:')
zeertzjq91c75d12022-11-05 20:21:58 +00002945 unlet g:bar
2946
2947 call assert_fails('call extend(l:, #{foo: { -> "foo" }})', 'E704:')
2948 let bar = 123
2949 call extend(l:, #{bar: { -> "foo" }}, "keep")
2950 call assert_fails('call extend(l:, #{bar: { -> "foo" }}, "force")', 'E704:')
2951 unlet bar
2952
2953 call assert_fails('call extend(g:, #{foo: function("extend")})', 'E704:')
2954 let g:bar = 123
2955 call extend(g:, #{bar: function("extend")}, "keep")
2956 call assert_fails('call extend(g:, #{bar: function("extend")}, "force")', 'E704:')
2957 unlet g:bar
2958
2959 call assert_fails('call extend(l:, #{foo: function("extend")})', 'E704:')
2960 let bar = 123
2961 call extend(l:, #{bar: function("extend")}, "keep")
2962 call assert_fails('call extend(l:, #{bar: function("extend")}, "force")', 'E704:')
2963 unlet bar
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002964endfunc
2965
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00002966func Test_funcref_to_string()
2967 let Fn = funcref('g:Test_funcref_to_string')
2968 call assert_equal("function('g:Test_funcref_to_string')", string(Fn))
2969endfunc
2970
LemonBoydca1d402022-04-28 15:26:33 +01002971" Test for isabsolutepath()
2972func Test_isabsolutepath()
2973 call assert_false(isabsolutepath(''))
2974 call assert_false(isabsolutepath('.'))
2975 call assert_false(isabsolutepath('../Foo'))
2976 call assert_false(isabsolutepath('Foo/'))
2977 if has('win32')
2978 call assert_true(isabsolutepath('A:\'))
2979 call assert_true(isabsolutepath('A:\Foo'))
2980 call assert_true(isabsolutepath('A:/Foo'))
2981 call assert_false(isabsolutepath('A:Foo'))
2982 call assert_false(isabsolutepath('\Windows'))
2983 call assert_true(isabsolutepath('\\Server2\Share\Test\Foo.txt'))
2984 else
2985 call assert_true(isabsolutepath('/'))
2986 call assert_true(isabsolutepath('/usr/share/'))
2987 endif
2988endfunc
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002989
Yasuhiro Matsumoto05cf63e2022-05-03 11:02:28 +01002990" Test for exepath()
2991func Test_exepath()
2992 if has('win32')
2993 call assert_notequal(exepath('cmd'), '')
2994
2995 let oldNoDefaultCurrentDirectoryInExePath = $NoDefaultCurrentDirectoryInExePath
2996 call writefile(['@echo off', 'echo Evil'], 'vim-test-evil.bat')
2997 let $NoDefaultCurrentDirectoryInExePath = ''
2998 call assert_notequal(exepath("vim-test-evil.bat"), '')
2999 let $NoDefaultCurrentDirectoryInExePath = '1'
3000 call assert_equal(exepath("vim-test-evil.bat"), '')
3001 let $NoDefaultCurrentDirectoryInExePath = oldNoDefaultCurrentDirectoryInExePath
3002 call delete('vim-test-evil.bat')
3003 else
3004 call assert_notequal(exepath('sh'), '')
3005 endif
3006endfunc
3007
LemonBoy0f7a3e12022-05-26 12:10:37 +01003008" Test for virtcol()
3009func Test_virtcol()
3010 enew!
3011 call setline(1, "the\tquick\tbrown\tfox")
3012 norm! 4|
3013 call assert_equal(8, virtcol('.'))
3014 call assert_equal(8, virtcol('.', v:false))
3015 call assert_equal([4, 8], virtcol('.', v:true))
3016 bwipe!
3017endfunc
3018
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01003019" vim: shiftwidth=2 sts=2 expandtab