blob: 1547447f7b2219c1c197986ac707238c18d9c50b [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 Moolenaar5feabe02020-01-30 18:24:53 +010057 if has('float')
58 call assert_equal(1, empty(0.0))
59 call assert_equal(1, empty(-0.0))
60 call assert_equal(0, empty(1.0))
61 call assert_equal(0, empty(-1.0))
62 call assert_equal(0, empty(1.0/0.0))
63 call assert_equal(0, empty(0.0/0.0))
64 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +010065
66 call assert_equal(1, empty([]))
67 call assert_equal(0, empty(['a']))
68
69 call assert_equal(1, empty({}))
70 call assert_equal(0, empty({'a':1}))
71
72 call assert_equal(1, empty(v:null))
73 call assert_equal(1, empty(v:none))
74 call assert_equal(1, empty(v:false))
75 call assert_equal(0, empty(v:true))
76
Bram Moolenaar41042f32017-03-09 12:09:32 +010077 if has('channel')
78 call assert_equal(1, empty(test_null_channel()))
79 endif
80 if has('job')
81 call assert_equal(1, empty(test_null_job()))
82 endif
83
Bram Moolenaar24c2e482017-01-29 15:45:12 +010084 call assert_equal(0, empty(function('Test_empty')))
Bram Moolenaar17aca702019-05-16 22:24:55 +020085 call assert_equal(0, empty(function('Test_empty', [0])))
Bram Moolenaar7c215c52020-02-29 13:43:27 +010086
87 call assert_fails("call empty(test_void())", 'E685:')
88 call assert_fails("call empty(test_unknown())", 'E685:')
Bram Moolenaar24c2e482017-01-29 15:45:12 +010089endfunc
90
Bram Moolenaardd589232020-02-29 17:38:12 +010091func Test_test_void()
Bram Moolenaar61a417b2021-06-15 22:54:28 +020092 call assert_fails('echo 1 == test_void()', 'E1031:')
Bram Moolenaardd589232020-02-29 17:38:12 +010093 if has('float')
Bram Moolenaar61a417b2021-06-15 22:54:28 +020094 call assert_fails('echo 1.0 == test_void()', 'E1031:')
Bram Moolenaardd589232020-02-29 17:38:12 +010095 endif
96 call assert_fails('let x = json_encode(test_void())', 'E685:')
97 call assert_fails('let x = copy(test_void())', 'E685:')
Bram Moolenaar61a417b2021-06-15 22:54:28 +020098 call assert_fails('let x = copy([test_void()])', 'E1031:')
Bram Moolenaardd589232020-02-29 17:38:12 +010099endfunc
100
Bram Moolenaar1840a7b2021-07-13 20:32:29 +0200101func Test_islocked()
102 call assert_fails('call islocked(99)', 'E475:')
103 call assert_fails('call islocked("s: x")', 'E488:')
104endfunc
105
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100106func Test_len()
107 call assert_equal(1, len(0))
108 call assert_equal(2, len(12))
109
110 call assert_equal(0, len(''))
111 call assert_equal(2, len('ab'))
112
113 call assert_equal(0, len([]))
Bram Moolenaar08f41572020-04-20 16:50:00 +0200114 call assert_equal(0, len(test_null_list()))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100115 call assert_equal(2, len([2, 1]))
116
117 call assert_equal(0, len({}))
Bram Moolenaar08f41572020-04-20 16:50:00 +0200118 call assert_equal(0, len(test_null_dict()))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100119 call assert_equal(2, len({'a': 1, 'b': 2}))
120
121 call assert_fails('call len(v:none)', 'E701:')
122 call assert_fails('call len({-> 0})', 'E701:')
123endfunc
124
125func Test_max()
126 call assert_equal(0, max([]))
127 call assert_equal(2, max([2]))
128 call assert_equal(2, max([1, 2]))
129 call assert_equal(2, max([1, 2, v:null]))
130
131 call assert_equal(0, max({}))
132 call assert_equal(2, max({'a':1, 'b':2}))
133
134 call assert_fails('call max(1)', 'E712:')
135 call assert_fails('call max(v:none)', 'E712:')
Bram Moolenaarab65fc72021-02-04 22:07:16 +0100136
137 " check we only get one error
138 call assert_fails('call max([#{}, [1]])', ['E728:', 'E728:'])
139 call assert_fails('call max(#{a: {}, b: [1]})', ['E728:', 'E728:'])
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100140endfunc
141
142func Test_min()
143 call assert_equal(0, min([]))
144 call assert_equal(2, min([2]))
145 call assert_equal(1, min([1, 2]))
146 call assert_equal(0, min([1, 2, v:null]))
147
148 call assert_equal(0, min({}))
149 call assert_equal(1, min({'a':1, 'b':2}))
150
151 call assert_fails('call min(1)', 'E712:')
152 call assert_fails('call min(v:none)', 'E712:')
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200153 call assert_fails('call min([1, {}])', 'E728:')
Bram Moolenaarab65fc72021-02-04 22:07:16 +0100154
155 " check we only get one error
156 call assert_fails('call min([[1], #{}])', ['E745:', 'E745:'])
157 call assert_fails('call min(#{a: [1], b: #{}})', ['E745:', 'E745:'])
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100158endfunc
159
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200160func Test_strwidth()
161 for aw in ['single', 'double']
162 exe 'set ambiwidth=' . aw
163 call assert_equal(0, strwidth(''))
164 call assert_equal(1, strwidth("\t"))
165 call assert_equal(3, strwidth('Vim'))
166 call assert_equal(4, strwidth(1234))
167 call assert_equal(5, strwidth(-1234))
168
Bram Moolenaar30276f22019-01-24 17:59:39 +0100169 call assert_equal(2, strwidth('😉'))
170 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde'))
171 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße'))
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200172
173 call assert_fails('call strwidth({->0})', 'E729:')
174 call assert_fails('call strwidth([])', 'E730:')
175 call assert_fails('call strwidth({})', 'E731:')
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200176 endfor
177
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200178 if has('float')
179 call assert_equal(3, strwidth(1.2))
Bram Moolenaar62aec932022-01-29 21:45:34 +0000180 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 +0200181 endif
182
Bram Moolenaar42ab17b2018-05-20 14:11:10 +0200183 set ambiwidth&
184endfunc
185
Bram Moolenaar08243d22017-01-10 16:12:29 +0100186func Test_str2nr()
187 call assert_equal(0, str2nr(''))
188 call assert_equal(1, str2nr('1'))
189 call assert_equal(1, str2nr(' 1 '))
190
191 call assert_equal(1, str2nr('+1'))
192 call assert_equal(1, str2nr('+ 1'))
193 call assert_equal(1, str2nr(' + 1 '))
194
195 call assert_equal(-1, str2nr('-1'))
196 call assert_equal(-1, str2nr('- 1'))
197 call assert_equal(-1, str2nr(' - 1 '))
198
199 call assert_equal(123456789, str2nr('123456789'))
200 call assert_equal(-123456789, str2nr('-123456789'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100201
202 call assert_equal(5, str2nr('101', 2))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200203 call assert_equal(5, '0b101'->str2nr(2))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100204 call assert_equal(5, str2nr('0B101', 2))
205 call assert_equal(-5, str2nr('-101', 2))
206 call assert_equal(-5, str2nr('-0b101', 2))
207 call assert_equal(-5, str2nr('-0B101', 2))
208
209 call assert_equal(65, str2nr('101', 8))
210 call assert_equal(65, str2nr('0101', 8))
211 call assert_equal(-65, str2nr('-101', 8))
212 call assert_equal(-65, str2nr('-0101', 8))
Bram Moolenaarc17e66c2020-06-02 21:38:22 +0200213 call assert_equal(65, str2nr('0o101', 8))
214 call assert_equal(65, str2nr('0O0101', 8))
215 call assert_equal(-65, str2nr('-0O101', 8))
216 call assert_equal(-65, str2nr('-0o0101', 8))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100217
218 call assert_equal(11259375, str2nr('abcdef', 16))
219 call assert_equal(11259375, str2nr('ABCDEF', 16))
220 call assert_equal(-11259375, str2nr('-ABCDEF', 16))
221 call assert_equal(11259375, str2nr('0xabcdef', 16))
222 call assert_equal(11259375, str2nr('0Xabcdef', 16))
223 call assert_equal(11259375, str2nr('0XABCDEF', 16))
224 call assert_equal(-11259375, str2nr('-0xABCDEF', 16))
225
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200226 call assert_equal(1, str2nr("1'000'000", 10, 0))
227 call assert_equal(256, str2nr("1'0000'0000", 2, 1))
228 call assert_equal(262144, str2nr("1'000'000", 8, 1))
229 call assert_equal(1000000, str2nr("1'000'000", 10, 1))
Bram Moolenaarea8dcf82019-09-15 21:12:22 +0200230 call assert_equal(1000, str2nr("1'000''000", 10, 1))
Bram Moolenaar60a8de22019-09-15 14:33:22 +0200231 call assert_equal(65536, str2nr("1'00'00", 16, 1))
232
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100233 call assert_equal(0, str2nr('0x10'))
234 call assert_equal(0, str2nr('0b10'))
Bram Moolenaarc17e66c2020-06-02 21:38:22 +0200235 call assert_equal(0, str2nr('0o10'))
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100236 call assert_equal(1, str2nr('12', 2))
237 call assert_equal(1, str2nr('18', 8))
238 call assert_equal(1, str2nr('1g', 16))
239
240 call assert_equal(0, str2nr(v:null))
241 call assert_equal(0, str2nr(v:none))
242
243 call assert_fails('call str2nr([])', 'E730:')
244 call assert_fails('call str2nr({->2})', 'E729:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100245 if has('float')
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200246 call assert_equal(1, str2nr(1.2))
Bram Moolenaar62aec932022-01-29 21:45:34 +0000247 call v9.CheckDefAndScriptFailure(['echo str2nr(1.2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1'])
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100248 endif
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200249 call assert_fails('call str2nr(10, [])', 'E745:')
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100250endfunc
251
252func Test_strftime()
Bram Moolenaar10455d42019-11-21 15:36:18 +0100253 CheckFunction strftime
254
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100255 " Format of strftime() depends on system. We assume
256 " that basic formats tested here are available and
257 " identical on all systems which support strftime().
258 "
259 " The 2nd parameter of strftime() is a local time, so the output day
260 " of strftime() can be 17 or 18, depending on timezone.
261 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512))
262 "
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200263 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 +0100264
265 call assert_fails('call strftime([])', 'E730:')
266 call assert_fails('call strftime("%Y", [])', 'E745:')
Bram Moolenaardb517302019-06-18 22:53:24 +0200267
268 " Check that the time changes after we change the timezone
269 " Save previous timezone value, if any
270 if exists('$TZ')
271 let tz = $TZ
272 endif
273
274 " Force EST and then UTC, save the current hour (24-hour clock) for each
275 let $TZ = 'EST' | let est = strftime('%H')
276 let $TZ = 'UTC' | let utc = strftime('%H')
277
278 " Those hours should be two bytes long, and should not be the same; if they
279 " are, a tzset(3) call may have failed somewhere
280 call assert_equal(strlen(est), 2)
281 call assert_equal(strlen(utc), 2)
Bram Moolenaar87652a72019-06-18 23:07:37 +0200282 " TODO: this fails on MS-Windows
283 if has('unix')
284 call assert_notequal(est, utc)
285 endif
Bram Moolenaardb517302019-06-18 22:53:24 +0200286
287 " If we cached a timezone value, put it back, otherwise clear it
288 if exists('tz')
289 let $TZ = tz
290 else
291 unlet $TZ
292 endif
Bram Moolenaar10455d42019-11-21 15:36:18 +0100293endfunc
Bram Moolenaardb517302019-06-18 22:53:24 +0200294
Bram Moolenaar10455d42019-11-21 15:36:18 +0100295func Test_strptime()
296 CheckFunction strptime
297
298 if exists('$TZ')
299 let tz = $TZ
300 endif
301 let $TZ = 'UTC'
302
Bram Moolenaar9a838fe2019-12-06 12:45:01 +0100303 call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
Bram Moolenaar10455d42019-11-21 15:36:18 +0100304
Bram Moolenaarea1233f2020-06-10 16:54:13 +0200305 " Force DST and check that it's considered
306 let $TZ = 'WINTER0SUMMER,J1,J365'
307 call assert_equal(1484653763 - 3600, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23'))
308
Bram Moolenaar10455d42019-11-21 15:36:18 +0100309 call assert_fails('call strptime()', 'E119:')
310 call assert_fails('call strptime("xxx")', 'E119:')
311 call assert_equal(0, strptime("%Y", ''))
312 call assert_equal(0, strptime("%Y", "xxx"))
313
314 if exists('tz')
315 let $TZ = tz
316 else
317 unlet $TZ
318 endif
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100319endfunc
320
Bram Moolenaardce1e892019-02-10 23:18:53 +0100321func Test_resolve_unix()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200322 CheckUnix
Bram Moolenaar26109902018-10-06 15:43:17 +0200323
324 " Xlink1 -> Xlink2
325 " Xlink2 -> Xlink3
326 silent !ln -s -f Xlink2 Xlink1
327 silent !ln -s -f Xlink3 Xlink2
328 call assert_equal('Xlink3', resolve('Xlink1'))
329 call assert_equal('./Xlink3', resolve('./Xlink1'))
330 call assert_equal('Xlink3/', resolve('Xlink2/'))
331 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?!
332 "call assert_equal('Xlink3/', resolve('Xlink1/'))
333 "call assert_equal('./Xlink3/', resolve('./Xlink1/'))
334 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/'))
335 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1'))
336
337 " Test resolve() with a symlink cycle.
338 " Xlink1 -> Xlink2
339 " Xlink2 -> Xlink3
340 " Xlink3 -> Xlink1
341 silent !ln -s -f Xlink1 Xlink3
342 call assert_fails('call resolve("Xlink1")', 'E655:')
343 call assert_fails('call resolve("./Xlink1")', 'E655:')
344 call assert_fails('call resolve("Xlink2")', 'E655:')
345 call assert_fails('call resolve("Xlink3")', 'E655:')
346 call delete('Xlink1')
347 call delete('Xlink2')
348 call delete('Xlink3')
349
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100350 silent !ln -s -f Xresolvedir//Xfile Xresolvelink
351 call assert_equal('Xresolvedir/Xfile', resolve('Xresolvelink'))
352 call delete('Xresolvelink')
Bram Moolenaar26109902018-10-06 15:43:17 +0200353
354 silent !ln -s -f Xlink2/ Xlink1
Bram Moolenaara0d1fef2019-09-04 22:29:14 +0200355 call assert_equal('Xlink2', 'Xlink1'->resolve())
Bram Moolenaar26109902018-10-06 15:43:17 +0200356 call assert_equal('Xlink2/', resolve('Xlink1/'))
357 call delete('Xlink1')
358
359 silent !ln -s -f ./Xlink2 Xlink1
360 call assert_equal('Xlink2', resolve('Xlink1'))
361 call assert_equal('./Xlink2', resolve('./Xlink1'))
362 call delete('Xlink1')
Bram Moolenaar50c4e9e2020-10-05 20:38:06 +0200363
364 call assert_equal('/', resolve('/'))
Bram Moolenaar26109902018-10-06 15:43:17 +0200365endfunc
366
Bram Moolenaardce1e892019-02-10 23:18:53 +0100367func s:normalize_fname(fname)
368 let ret = substitute(a:fname, '\', '/', 'g')
369 let ret = substitute(ret, '//', '/', 'g')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200370 return ret->tolower()
Bram Moolenaardce1e892019-02-10 23:18:53 +0100371endfunc
372
373func Test_resolve_win32()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200374 CheckMSWindows
Bram Moolenaardce1e892019-02-10 23:18:53 +0100375
376 " test for shortcut file
377 if executable('cscript')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100378 new Xresfile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100379 wq
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200380 let lines =<< trim END
381 Set fs = CreateObject("Scripting.FileSystemObject")
382 Set ws = WScript.CreateObject("WScript.Shell")
383 Set shortcut = ws.CreateShortcut("Xlink.lnk")
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100384 shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xresfile")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200385 shortcut.Save
386 END
387 call writefile(lines, 'link.vbs')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100388 silent !cscript link.vbs
389 call delete('link.vbs')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100390 call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink.lnk')))
391 call delete('Xresfile')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100392
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100393 call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink.lnk')))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100394 call delete('Xlink.lnk')
395 else
396 echomsg 'skipped test for shortcut file'
397 endif
398
399 " remove files
400 call delete('Xlink')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100401 call delete('Xresfile')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100402
403 " test for symbolic link to a file
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100404 new Xslinkfile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100405 wq
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100406 call assert_equal('Xslinkfile', resolve('Xslinkfile'))
407 silent !mklink Xlink Xslinkfile
Bram Moolenaardce1e892019-02-10 23:18:53 +0100408 if !v:shell_error
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100409 call assert_equal(s:normalize_fname(getcwd() . '\Xslinkfile'), s:normalize_fname(resolve('./Xlink')))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100410 call delete('Xlink')
411 else
412 echomsg 'skipped test for symbolic link to a file'
413 endif
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100414 call delete('Xslinkfile')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100415
416 " test for junction to a directory
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100417 call mkdir('Xjuncdir')
418 silent !mklink /J Xlink Xjuncdir
Bram Moolenaardce1e892019-02-10 23:18:53 +0100419 if !v:shell_error
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100420 call assert_equal(s:normalize_fname(getcwd() . '\Xjuncdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100421
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100422 call delete('Xjuncdir', 'd')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100423
424 " test for junction already removed
425 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
426 call delete('Xlink')
427 else
428 echomsg 'skipped test for junction to a directory'
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100429 call delete('Xjuncdir', 'd')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100430 endif
431
432 " test for symbolic link to a directory
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100433 call mkdir('Xjuncdir')
434 silent !mklink /D Xlink Xjuncdir
Bram Moolenaardce1e892019-02-10 23:18:53 +0100435 if !v:shell_error
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100436 call assert_equal(s:normalize_fname(getcwd() . '\Xjuncdir'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
Bram Moolenaardce1e892019-02-10 23:18:53 +0100437
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100438 call delete('Xjuncdir', 'd')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100439
440 " test for symbolic link already removed
441 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink')))
442 call delete('Xlink')
443 else
444 echomsg 'skipped test for symbolic link to a directory'
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100445 call delete('Xjuncdir', 'd')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100446 endif
447
448 " test for buffer name
449 new Xfile
450 wq
451 silent !mklink Xlink Xfile
452 if !v:shell_error
453 edit Xlink
454 call assert_equal('Xlink', bufname('%'))
455 call delete('Xlink')
456 bw!
457 else
458 echomsg 'skipped test for buffer name'
459 endif
460 call delete('Xfile')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200461
462 " test for reparse point
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100463 call mkdir('Xparsedir')
464 call assert_equal('Xdir', resolve('Xparsedir'))
465 silent !mklink /D Xdirlink Xparsedir
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200466 if !v:shell_error
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100467 w Xparsedir/text.txt
468 call assert_equal('Xparsedir/text.txt', resolve('Xparsedir/text.txt'))
469 call assert_equal(s:normalize_fname(getcwd() . '\Xparsedir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt')))
470 call assert_equal(s:normalize_fname(getcwd() . '\Xparsedir'), s:normalize_fname(resolve('Xdirlink')))
Bram Moolenaar4a792c82019-06-06 12:22:41 +0200471 call delete('Xdirlink')
Bram Moolenaar1bbebab2019-05-29 20:36:54 +0200472 else
473 echomsg 'skipped test for reparse point'
474 endif
475
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100476 call delete('Xparsedir', 'rf')
Bram Moolenaardce1e892019-02-10 23:18:53 +0100477endfunc
478
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100479func Test_simplify()
480 call assert_equal('', simplify(''))
481 call assert_equal('/', simplify('/'))
482 call assert_equal('/', simplify('/.'))
483 call assert_equal('/', simplify('/..'))
484 call assert_equal('/...', simplify('/...'))
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +0200485 call assert_equal('//path', simplify('//path'))
Bram Moolenaarc70222d2020-06-15 23:18:12 +0200486 if has('unix')
487 call assert_equal('/path', simplify('///path'))
488 call assert_equal('/path', simplify('////path'))
489 endif
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +0200490
Bram Moolenaar7035fd92020-04-08 20:03:52 +0200491 call assert_equal('./dir/file', './dir/file'->simplify())
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100492 call assert_equal('./dir/file', simplify('.///dir//file'))
493 call assert_equal('./dir/file', simplify('./dir/./file'))
494 call assert_equal('./file', simplify('./dir/../file'))
495 call assert_equal('../dir/file', simplify('dir/../../dir/file'))
496 call assert_equal('./file', simplify('dir/.././file'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200497 call assert_equal('../dir', simplify('./../dir'))
498 call assert_equal('..', simplify('../testdir/..'))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100499 call mkdir('Xsimpdir')
500 call assert_equal('.', simplify('Xsimpdir/../.'))
501 call delete('Xsimpdir', 'd')
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100502
503 call assert_fails('call simplify({->0})', 'E729:')
504 call assert_fails('call simplify([])', 'E730:')
505 call assert_fails('call simplify({})', 'E731:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100506 if has('float')
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200507 call assert_equal('1.2', simplify(1.2))
Bram Moolenaar62aec932022-01-29 21:45:34 +0000508 call v9.CheckDefAndScriptFailure(['echo simplify(1.2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1'])
Bram Moolenaar5feabe02020-01-30 18:24:53 +0100509 endif
Bram Moolenaar08243d22017-01-10 16:12:29 +0100510endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100511
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200512func Test_pathshorten()
513 call assert_equal('', pathshorten(''))
514 call assert_equal('foo', pathshorten('foo'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200515 call assert_equal('/foo', '/foo'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200516 call assert_equal('f/', pathshorten('foo/'))
517 call assert_equal('f/bar', pathshorten('foo/bar'))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +0200518 call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten())
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200519 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar'))
520 call assert_equal('.f/bar', pathshorten('.foo/bar'))
521 call assert_equal('~f/bar', pathshorten('~foo/bar'))
522 call assert_equal('~.f/bar', pathshorten('~.foo/bar'))
523 call assert_equal('.~f/bar', pathshorten('.~foo/bar'))
524 call assert_equal('~/f/bar', pathshorten('~/foo/bar'))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200525 call assert_fails('call pathshorten([])', 'E730:')
Bram Moolenaar6a33ef02020-09-25 22:42:48 +0200526
527 " test pathshorten with optional variable to set preferred size of shortening
528 call assert_equal('', pathshorten('', 2))
529 call assert_equal('foo', pathshorten('foo', 2))
530 call assert_equal('/foo', pathshorten('/foo', 2))
531 call assert_equal('fo/', pathshorten('foo/', 2))
532 call assert_equal('fo/bar', pathshorten('foo/bar', 2))
533 call assert_equal('fo/ba/foobar', pathshorten('foo/bar/foobar', 2))
534 call assert_equal('/fo/ba/foobar', pathshorten('/foo/bar/foobar', 2))
535 call assert_equal('.fo/bar', pathshorten('.foo/bar', 2))
536 call assert_equal('~fo/bar', pathshorten('~foo/bar', 2))
537 call assert_equal('~.fo/bar', pathshorten('~.foo/bar', 2))
538 call assert_equal('.~fo/bar', pathshorten('.~foo/bar', 2))
539 call assert_equal('~/fo/bar', pathshorten('~/foo/bar', 2))
540 call assert_fails('call pathshorten([],2)', 'E730:')
541 call assert_notequal('~/fo/bar', pathshorten('~/foo/bar', 3))
542 call assert_equal('~/foo/bar', pathshorten('~/foo/bar', 3))
543 call assert_equal('~/f/bar', pathshorten('~/foo/bar', 0))
Bram Moolenaarbfde0b42018-08-08 22:27:31 +0200544endfunc
545
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100546func Test_strpart()
547 call assert_equal('de', strpart('abcdefg', 3, 2))
548 call assert_equal('ab', strpart('abcdefg', -2, 4))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +0200549 call assert_equal('abcdefg', 'abcdefg'->strpart(-2))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100550 call assert_equal('fg', strpart('abcdefg', 5, 4))
551 call assert_equal('defg', strpart('abcdefg', 3))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100552 call assert_equal('', strpart('abcdefg', 10))
553 call assert_fails("let s=strpart('abcdef', [])", 'E745:')
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100554
Bram Moolenaar30276f22019-01-24 17:59:39 +0100555 call assert_equal('lép', strpart('éléphant', 2, 4))
556 call assert_equal('léphant', strpart('éléphant', 2))
Bram Moolenaar6c53fca2020-08-23 17:34:46 +0200557
558 call assert_equal('é', strpart('éléphant', 0, 1, 1))
559 call assert_equal('ép', strpart('éléphant', 3, 2, v:true))
560 call assert_equal('ó', strpart('cómposed', 1, 1, 1))
Bram Moolenaarc7d16dc2017-11-18 20:32:03 +0100561endfunc
562
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100563func Test_tolower()
564 call assert_equal("", tolower(""))
565
566 " Test with all printable ASCII characters.
567 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
568 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
569
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100570 " Test with a few uppercase diacritics.
571 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
572 call assert_equal("bḃḇ", tolower("BḂḆ"))
573 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ"))
574 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ"))
575 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ"))
576 call assert_equal("f ", tolower("F "))
577 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ"))
578 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ"))
579 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ"))
580 call assert_equal("jĵ", tolower("JĴ"))
581 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ"))
582 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ"))
583 call assert_equal("mḿṁ", tolower("MḾṀ"))
584 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ"))
585 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
586 call assert_equal("pṕṗ", tolower("PṔṖ"))
587 call assert_equal("q", tolower("Q"))
588 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ"))
589 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ"))
590 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ"))
591 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
592 call assert_equal("v", tolower("V"))
593 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ"))
594 call assert_equal("xẋẍ", tolower("XẊẌ"))
595 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ"))
596 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ"))
597
598 " Test with a few lowercase diacritics, which should remain unchanged.
599 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả"))
600 call assert_equal("bḃḇ", tolower("bḃḇ"))
601 call assert_equal("cçćĉċč", tolower("cçćĉċč"))
602 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ"))
603 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ"))
604 call assert_equal("fḟ", tolower("fḟ"))
605 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ"))
606 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ"))
607 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ"))
608 call assert_equal("jĵǰ", tolower("jĵǰ"))
609 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ"))
610 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ"))
611 call assert_equal("mḿṁ ", tolower("mḿṁ "))
612 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ"))
613 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ"))
614 call assert_equal("pṕṗ", tolower("pṕṗ"))
615 call assert_equal("q", tolower("q"))
616 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ"))
617 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ"))
618 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ"))
619 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ"))
620 call assert_equal("vṽ", tolower("vṽ"))
621 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ"))
622 call assert_equal("ẋẍ", tolower("ẋẍ"))
623 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ"))
624 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ"))
625
626 " According to https://twitter.com/jifa/status/625776454479970304
627 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase
628 " in length (2 to 3 bytes) when lowercased. So let's test them.
629 call assert_equal(" ", tolower("Ⱥ Ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100630
631 " This call to tolower with invalid utf8 sequence used to cause access to
632 " invalid memory.
633 call tolower("\xC0\x80\xC0")
634 call tolower("123\xC0\x80\xC0")
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200635
636 " Test in latin1 encoding
637 let save_enc = &encoding
638 set encoding=latin1
639 call assert_equal("abc", tolower("ABC"))
640 let &encoding = save_enc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100641endfunc
642
643func Test_toupper()
644 call assert_equal("", toupper(""))
645
646 " Test with all printable ASCII characters.
647 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~',
648 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'))
649
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100650 " Test with a few lowercase diacritics.
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200651 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper())
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100652 call assert_equal("BḂḆ", toupper("bḃḇ"))
653 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč"))
654 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ"))
655 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ"))
656 call assert_equal("F", toupper("f"))
657 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ"))
658 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ"))
659 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ"))
660 call assert_equal("JĴǰ", toupper("jĵǰ"))
661 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ"))
662 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ"))
663 call assert_equal("MḾṀ ", toupper("mḿṁ "))
664 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ"))
665 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ"))
666 call assert_equal("PṔṖ", toupper("pṕṗ"))
667 call assert_equal("Q", toupper("q"))
668 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ"))
669 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ"))
670 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ"))
671 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ"))
672 call assert_equal("V", toupper("v"))
673 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ"))
674 call assert_equal("ẊẌ", toupper("ẋẍ"))
675 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ"))
676 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ"))
677
678 " Test that uppercase diacritics, which should remain unchanged.
679 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ"))
680 call assert_equal("BḂḆ", toupper("BḂḆ"))
681 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ"))
682 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ"))
683 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ"))
684 call assert_equal("FḞ ", toupper("FḞ "))
685 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ"))
686 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ"))
687 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ"))
688 call assert_equal("JĴ", toupper("JĴ"))
689 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ"))
690 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ"))
691 call assert_equal("MḾṀ", toupper("MḾṀ"))
692 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ"))
693 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ"))
694 call assert_equal("PṔṖ", toupper("PṔṖ"))
695 call assert_equal("Q", toupper("Q"))
696 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ"))
697 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ"))
698 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ"))
699 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ"))
700 call assert_equal("VṼ", toupper("VṼ"))
701 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ"))
702 call assert_equal("XẊẌ", toupper("XẊẌ"))
703 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ"))
704 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ"))
705
Bram Moolenaar24c2e482017-01-29 15:45:12 +0100706 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ"))
Bram Moolenaare6640ad2017-12-22 21:06:56 +0100707
708 " This call to toupper with invalid utf8 sequence used to cause access to
709 " invalid memory.
710 call toupper("\xC0\x80\xC0")
711 call toupper("123\xC0\x80\xC0")
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +0200712
713 " Test in latin1 encoding
714 let save_enc = &encoding
715 set encoding=latin1
716 call assert_equal("ABC", toupper("abc"))
717 let &encoding = save_enc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100718endfunc
719
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200720func Test_tr()
721 call assert_equal('foo', tr('bar', 'bar', 'foo'))
722 call assert_equal('zxy', 'cab'->tr('abc', 'xyz'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100723 call assert_fails("let s=tr([], 'abc', 'def')", 'E730:')
724 call assert_fails("let s=tr('abc', [], 'def')", 'E730:')
725 call assert_fails("let s=tr('abc', 'abc', [])", 'E730:')
726 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
727 set encoding=latin1
728 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:')
729 call assert_equal('hEllO', tr('hello', 'eo', 'EO'))
730 call assert_equal('hello', tr('hello', 'xy', 'ab'))
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200731 call assert_fails('call tr("abc", "123", "₁₂")', 'E475:')
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100732 set encoding=utf8
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200733endfunc
734
Bram Moolenaare90858d2017-02-01 17:24:34 +0100735" Tests for the mode() function
736let current_modes = ''
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100737func Save_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100738 let g:current_modes = mode(0) . '-' . mode(1)
739 return ''
740endfunc
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +0100741
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200742" Test for the mode() function
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100743func Test_mode()
Bram Moolenaare90858d2017-02-01 17:24:34 +0100744 new
745 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""])
746
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100747 " Only complete from the current buffer.
748 set complete=.
749
Bram Moolenaare90858d2017-02-01 17:24:34 +0100750 inoremap <F2> <C-R>=Save_mode()<CR>
zeertzjqeaf3f362021-07-28 16:51:53 +0200751 xnoremap <F2> <Cmd>call Save_mode()<CR>
Bram Moolenaare90858d2017-02-01 17:24:34 +0100752
753 normal! 3G
754 exe "normal i\<F2>\<Esc>"
755 call assert_equal('i-i', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100756 " i_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100757 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u"
758 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100759 " i_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100760 exe "normal iBro\<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
Bram Moolenaare90858d2017-02-01 17:24:34 +0100763 exe "normal iBa\<C-X>\<F2>\<Esc>u"
764 call assert_equal('i-ix', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100765 " i_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100766 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u"
767 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100768 " i_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100769 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u"
770 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100771 " i_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100772 exe "normal iBro\<C-X>\<C-P>\<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-L: Multiple matches
775 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u"
776 call assert_equal('i-ic', g:current_modes)
777 " i_CTRL-X CTRL-L: Single match
778 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u"
779 call assert_equal('i-ic', g:current_modes)
780 " i_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100781 exe "normal iCom\<C-P>\<F2>\<Esc>u"
782 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100783 " i_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100784 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u"
785 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100786 " i_CTRL-X CTRL-L: No match
787 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u"
788 call assert_equal('i-ic', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100789
zeertzjqcc8cd442021-10-03 15:19:14 +0100790 exe "normal R\<F2>\<Esc>"
791 call assert_equal('R-R', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100792 " R_CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100793 exe "normal RBa\<C-P>\<F2>\<Esc>u"
794 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100795 " R_CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100796 exe "normal RBro\<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
Bram Moolenaare90858d2017-02-01 17:24:34 +0100799 exe "normal RBa\<C-X>\<F2>\<Esc>u"
800 call assert_equal('R-Rx', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100801 " R_CTRL-X CTRL-P: Multiple matches
Bram Moolenaare90858d2017-02-01 17:24:34 +0100802 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u"
803 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100804 " R_CTRL-X CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100805 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u"
806 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100807 " R_CTRL-X CTRL-P + CTRL-P: Single match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100808 exe "normal RBro\<C-X>\<C-P>\<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-L: Multiple matches
811 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u"
812 call assert_equal('R-Rc', g:current_modes)
813 " R_CTRL-X CTRL-L: Single match
814 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u"
815 call assert_equal('R-Rc', g:current_modes)
816 " R_CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100817 exe "normal RCom\<C-P>\<F2>\<Esc>u"
818 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100819 " R_CTRL-X CTRL-P: No match
Bram Moolenaare90858d2017-02-01 17:24:34 +0100820 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u"
821 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare971df32017-02-05 14:15:29 +0100822 " R_CTRL-X CTRL-L: No match
823 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u"
824 call assert_equal('R-Rc', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100825
zeertzjqcc8cd442021-10-03 15:19:14 +0100826 exe "normal gR\<F2>\<Esc>"
827 call assert_equal('R-Rv', g:current_modes)
828 " gR_CTRL-P: Multiple matches
829 exe "normal gRBa\<C-P>\<F2>\<Esc>u"
830 call assert_equal('R-Rvc', g:current_modes)
831 " gR_CTRL-P: Single match
832 exe "normal gRBro\<C-P>\<F2>\<Esc>u"
833 call assert_equal('R-Rvc', g:current_modes)
834 " gR_CTRL-X
835 exe "normal gRBa\<C-X>\<F2>\<Esc>u"
836 call assert_equal('R-Rvx', g:current_modes)
837 " gR_CTRL-X CTRL-P: Multiple matches
838 exe "normal gRBa\<C-X>\<C-P>\<F2>\<Esc>u"
839 call assert_equal('R-Rvc', g:current_modes)
840 " gR_CTRL-X CTRL-P: Single match
841 exe "normal gRBro\<C-X>\<C-P>\<F2>\<Esc>u"
842 call assert_equal('R-Rvc', g:current_modes)
843 " gR_CTRL-X CTRL-P + CTRL-P: Single match
844 exe "normal gRBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u"
845 call assert_equal('R-Rvc', g:current_modes)
846 " gR_CTRL-X CTRL-L: Multiple matches
847 exe "normal gR\<C-X>\<C-L>\<F2>\<Esc>u"
848 call assert_equal('R-Rvc', g:current_modes)
849 " gR_CTRL-X CTRL-L: Single match
850 exe "normal gRBlu\<C-X>\<C-L>\<F2>\<Esc>u"
851 call assert_equal('R-Rvc', g:current_modes)
852 " gR_CTRL-P: No match
853 exe "normal gRCom\<C-P>\<F2>\<Esc>u"
854 call assert_equal('R-Rvc', g:current_modes)
855 " gR_CTRL-X CTRL-P: No match
856 exe "normal gRCom\<C-X>\<C-P>\<F2>\<Esc>u"
857 call assert_equal('R-Rvc', g:current_modes)
858 " gR_CTRL-X CTRL-L: No match
859 exe "normal gRabc\<C-X>\<C-L>\<F2>\<Esc>u"
860 call assert_equal('R-Rvc', g:current_modes)
861
Bram Moolenaara1449832019-09-01 20:16:52 +0200862 call assert_equal('n', 0->mode())
863 call assert_equal('n', 1->mode())
Bram Moolenaare90858d2017-02-01 17:24:34 +0100864
Bram Moolenaar612cc382018-07-29 15:34:26 +0200865 " i_CTRL-O
866 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>"
867 call assert_equal("n-niI", g:current_modes)
868
869 " R_CTRL-O
870 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>"
871 call assert_equal("n-niR", g:current_modes)
872
873 " gR_CTRL-O
874 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>"
875 call assert_equal("n-niV", g:current_modes)
876
Bram Moolenaare90858d2017-02-01 17:24:34 +0100877 " How to test operator-pending mode?
878
879 call feedkeys("v", 'xt')
880 call assert_equal('v', mode())
881 call assert_equal('v', mode(1))
882 call feedkeys("\<Esc>V", 'xt')
883 call assert_equal('V', mode())
884 call assert_equal('V', mode(1))
885 call feedkeys("\<Esc>\<C-V>", 'xt')
886 call assert_equal("\<C-V>", mode())
887 call assert_equal("\<C-V>", mode(1))
888 call feedkeys("\<Esc>", 'xt')
889
890 call feedkeys("gh", 'xt')
891 call assert_equal('s', mode())
892 call assert_equal('s', mode(1))
893 call feedkeys("\<Esc>gH", 'xt')
894 call assert_equal('S', mode())
895 call assert_equal('S', mode(1))
896 call feedkeys("\<Esc>g\<C-H>", 'xt')
897 call assert_equal("\<C-S>", mode())
898 call assert_equal("\<C-S>", mode(1))
899 call feedkeys("\<Esc>", 'xt')
900
zeertzjqeaf3f362021-07-28 16:51:53 +0200901 " v_CTRL-O
902 exe "normal gh\<C-O>\<F2>\<Esc>"
903 call assert_equal("v-vs", g:current_modes)
904 exe "normal gH\<C-O>\<F2>\<Esc>"
905 call assert_equal("V-Vs", g:current_modes)
906 exe "normal g\<C-H>\<C-O>\<F2>\<Esc>"
907 call assert_equal("\<C-V>-\<C-V>s", g:current_modes)
908
Bram Moolenaare90858d2017-02-01 17:24:34 +0100909 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt')
910 call assert_equal('c-c', g:current_modes)
911 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt')
912 call assert_equal('c-cv', g:current_modes)
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200913 call feedkeys("Qcall Save_mode()\<CR>vi\<CR>", 'xt')
914 call assert_equal('c-ce', g:current_modes)
Bram Moolenaare90858d2017-02-01 17:24:34 +0100915 " How to test Ex mode?
916
naohiro ono75c30e92021-10-19 11:15:41 +0100917 " Test mode in operatorfunc (it used to be Operator-pending).
918 set operatorfunc=OperatorFunc
919 function OperatorFunc(_)
920 call Save_mode()
921 endfunction
922 execute "normal! g@l\<Esc>"
923 call assert_equal('n-n', g:current_modes)
924 execute "normal! i\<C-o>g@l\<Esc>"
925 call assert_equal('n-niI', g:current_modes)
926 execute "normal! R\<C-o>g@l\<Esc>"
927 call assert_equal('n-niR', g:current_modes)
928 execute "normal! gR\<C-o>g@l\<Esc>"
929 call assert_equal('n-niV', g:current_modes)
930
Bram Moolenaar72406a42021-10-02 16:34:55 +0100931 if has('terminal')
932 term
933 call feedkeys("\<C-W>N", 'xt')
934 call assert_equal('n', mode())
935 call assert_equal('nt', mode(1))
936 call feedkeys("aexit\<CR>", 'xt')
937 endif
938
Bram Moolenaare90858d2017-02-01 17:24:34 +0100939 bwipe!
940 iunmap <F2>
zeertzjqeaf3f362021-07-28 16:51:53 +0200941 xunmap <F2>
Bram Moolenaarffea8c92017-03-13 20:37:15 +0100942 set complete&
naohiro ono75c30e92021-10-19 11:15:41 +0100943 set operatorfunc&
944 delfunction OperatorFunc
Bram Moolenaare90858d2017-02-01 17:24:34 +0100945endfunc
Bram Moolenaar79518e22017-02-17 16:31:35 +0100946
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200947" Test for append()
Bram Moolenaard2007022019-08-27 21:56:06 +0200948func Test_append()
949 enew!
950 split
951 call append(0, ["foo"])
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200952 call append(1, [])
953 call append(1, test_null_list())
954 call assert_equal(['foo', ''], getline(1, '$'))
Bram Moolenaard2007022019-08-27 21:56:06 +0200955 split
956 only
957 undo
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200958 undo
Bram Moolenaar08f41572020-04-20 16:50:00 +0200959
960 " Using $ instead of '$' must give an error
961 call assert_fails("call append($, 'foobar')", 'E116:')
Bram Moolenaard2007022019-08-27 21:56:06 +0200962endfunc
963
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200964" Test for setline()
965func Test_setline()
966 new
967 call setline(0, ["foo"])
968 call setline(0, [])
969 call setline(0, test_null_list())
970 call setline(1, ["bar"])
971 call setline(1, [])
972 call setline(1, test_null_list())
973 call setline(2, [])
974 call setline(2, test_null_list())
975 call setline(3, [])
976 call setline(3, test_null_list())
977 call setline(2, ["baz"])
978 call assert_equal(['bar', 'baz'], getline(1, '$'))
979 close!
980endfunc
981
Bram Moolenaar79518e22017-02-17 16:31:35 +0100982func Test_getbufvar()
983 let bnr = bufnr('%')
984 let b:var_num = '1234'
985 let def_num = '5678'
986 call assert_equal('1234', getbufvar(bnr, 'var_num'))
987 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num))
988
989 let bd = getbufvar(bnr, '')
990 call assert_equal('1234', bd['var_num'])
991 call assert_true(exists("bd['changedtick']"))
992 call assert_equal(2, len(bd))
993
994 let bd2 = getbufvar(bnr, '', def_num)
995 call assert_equal(bd, bd2)
996
997 unlet b:var_num
998 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num))
999 call assert_equal('', getbufvar(bnr, 'var_num'))
1000
1001 let bd = getbufvar(bnr, '')
1002 call assert_equal(1, len(bd))
1003 let bd = getbufvar(bnr, '',def_num)
1004 call assert_equal(1, len(bd))
1005
Bram Moolenaar4520d442017-03-19 16:09:46 +01001006 call assert_equal('', getbufvar(9999, ''))
1007 call assert_equal(def_num, getbufvar(9999, '', def_num))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001008 unlet def_num
1009
Bram Moolenaar507647d2017-02-17 16:43:49 +01001010 call assert_equal(0, getbufvar(bnr, '&autoindent'))
1011 call assert_equal(0, getbufvar(bnr, '&autoindent', 1))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001012
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001013 " Set and get a buffer-local variable
1014 call setbufvar(bnr, 'bufvar_test', ['one', 'two'])
1015 call assert_equal(['one', 'two'], getbufvar(bnr, 'bufvar_test'))
1016
Bram Moolenaar79518e22017-02-17 16:31:35 +01001017 " Open new window with forced option values
1018 set fileformats=unix,dos
1019 new ++ff=dos ++bin ++enc=iso-8859-2
1020 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat'))
1021 call assert_equal(1, getbufvar(bufnr('%'), '&bin'))
1022 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc'))
1023 close
1024
Bram Moolenaar52592752020-04-03 18:43:35 +02001025 " Get the b: dict.
1026 let b:testvar = 'one'
1027 new
1028 let b:testvar = 'two'
1029 let thebuf = bufnr()
1030 wincmd w
1031 call assert_equal('two', getbufvar(thebuf, 'testvar'))
1032 call assert_equal('two', getbufvar(thebuf, '').testvar)
1033 bwipe!
1034
Bram Moolenaar79518e22017-02-17 16:31:35 +01001035 set fileformats&
1036endfunc
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001037
Bram Moolenaar41042f32017-03-09 12:09:32 +01001038func Test_last_buffer_nr()
1039 call assert_equal(bufnr('$'), last_buffer_nr())
1040endfunc
1041
1042func Test_stridx()
1043 call assert_equal(-1, stridx('', 'l'))
1044 call assert_equal(0, stridx('', ''))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +02001045 call assert_equal(0, 'hello'->stridx(''))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001046 call assert_equal(-1, stridx('hello', 'L'))
1047 call assert_equal(2, stridx('hello', 'l', -1))
1048 call assert_equal(2, stridx('hello', 'l', 0))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +02001049 call assert_equal(2, 'hello'->stridx('l', 1))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001050 call assert_equal(3, stridx('hello', 'l', 3))
1051 call assert_equal(-1, stridx('hello', 'l', 4))
1052 call assert_equal(-1, stridx('hello', 'l', 10))
1053 call assert_equal(2, stridx('hello', 'll'))
1054 call assert_equal(-1, stridx('hello', 'hello world'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001055 call assert_fails("let n=stridx('hello', [])", 'E730:')
1056 call assert_fails("let n=stridx([], 'l')", 'E730:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001057endfunc
1058
1059func Test_strridx()
1060 call assert_equal(-1, strridx('', 'l'))
1061 call assert_equal(0, strridx('', ''))
1062 call assert_equal(5, strridx('hello', ''))
1063 call assert_equal(-1, strridx('hello', 'L'))
Bram Moolenaarf6ed61e2019-09-07 19:05:09 +02001064 call assert_equal(3, 'hello'->strridx('l'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001065 call assert_equal(3, strridx('hello', 'l', 10))
1066 call assert_equal(3, strridx('hello', 'l', 3))
1067 call assert_equal(2, strridx('hello', 'l', 2))
1068 call assert_equal(-1, strridx('hello', 'l', 1))
1069 call assert_equal(-1, strridx('hello', 'l', 0))
1070 call assert_equal(-1, strridx('hello', 'l', -1))
1071 call assert_equal(2, strridx('hello', 'll'))
1072 call assert_equal(-1, strridx('hello', 'hello world'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001073 call assert_fails("let n=strridx('hello', [])", 'E730:')
1074 call assert_fails("let n=strridx([], 'l')", 'E730:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001075endfunc
1076
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001077func Test_match_func()
1078 call assert_equal(4, match('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001079 call assert_equal(4, 'testing'->match('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001080 call assert_equal(-1, match('testing', 'ing', 5))
1081 call assert_equal(-1, match('testing', 'ing', 8))
1082 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing'))
1083 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001084 call assert_fails("let x=match('vim', [])", 'E730:')
1085 call assert_equal(3, match(['a', 'b', 'c', 'a'], 'a', 1))
1086 call assert_equal(-1, match(['a', 'b', 'c', 'a'], 'a', 5))
1087 call assert_equal(4, match('testing', 'ing', -1))
1088 call assert_fails("let x=match('testing', 'ing', 0, [])", 'E745:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001089 call assert_equal(-1, match(test_null_list(), 2))
Bram Moolenaar531be472020-09-23 22:38:05 +02001090 call assert_equal(-1, match('abc', '\\%('))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001091endfunc
1092
Bram Moolenaar41042f32017-03-09 12:09:32 +01001093func Test_matchend()
1094 call assert_equal(7, matchend('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001095 call assert_equal(7, 'testing'->matchend('ing', 2))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001096 call assert_equal(-1, matchend('testing', 'ing', 5))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001097 call assert_equal(-1, matchend('testing', 'ing', 8))
1098 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing'))
1099 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img'))
1100endfunc
1101
1102func Test_matchlist()
1103 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001104 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001105 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4))
1106endfunc
1107
1108func Test_matchstr()
1109 call assert_equal('ing', matchstr('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001110 call assert_equal('ing', 'testing'->matchstr('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001111 call assert_equal('', matchstr('testing', 'ing', 5))
1112 call assert_equal('', matchstr('testing', 'ing', 8))
1113 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing'))
1114 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img'))
1115endfunc
1116
1117func Test_matchstrpos()
1118 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
Bram Moolenaara1449832019-09-01 20:16:52 +02001119 call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2))
Bram Moolenaar1190cf62017-09-14 14:31:18 +02001120 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
1121 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8))
1122 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
1123 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001124 call assert_equal(['', -1, -1], matchstrpos(test_null_list(), '\a'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001125endfunc
1126
1127func Test_nextnonblank_prevnonblank()
1128 new
1129insert
1130This
1131
1132
1133is
1134
1135a
1136Test
1137.
1138 call assert_equal(0, nextnonblank(-1))
1139 call assert_equal(0, nextnonblank(0))
1140 call assert_equal(1, nextnonblank(1))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001141 call assert_equal(4, 2->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001142 call assert_equal(4, nextnonblank(3))
1143 call assert_equal(4, nextnonblank(4))
1144 call assert_equal(6, nextnonblank(5))
1145 call assert_equal(6, nextnonblank(6))
1146 call assert_equal(7, nextnonblank(7))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001147 call assert_equal(0, 8->nextnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001148
1149 call assert_equal(0, prevnonblank(-1))
1150 call assert_equal(0, prevnonblank(0))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001151 call assert_equal(1, 1->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001152 call assert_equal(1, prevnonblank(2))
1153 call assert_equal(1, prevnonblank(3))
1154 call assert_equal(4, prevnonblank(4))
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001155 call assert_equal(4, 5->prevnonblank())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001156 call assert_equal(6, prevnonblank(6))
1157 call assert_equal(7, prevnonblank(7))
1158 call assert_equal(0, prevnonblank(8))
1159 bw!
1160endfunc
1161
1162func Test_byte2line_line2byte()
1163 new
Bram Moolenaarc26f7c62018-08-20 22:53:04 +02001164 set endofline
Bram Moolenaar41042f32017-03-09 12:09:32 +01001165 call setline(1, ['a', 'bc', 'd'])
1166
1167 set fileformat=unix
1168 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
1169 \ map(range(-1, 8), 'byte2line(v:val)'))
1170 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
1171 \ map(range(-1, 5), 'line2byte(v:val)'))
1172
1173 set fileformat=mac
1174 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1],
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001175 \ map(range(-1, 8), 'v:val->byte2line()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001176 call assert_equal([-1, -1, 1, 3, 6, 8, -1],
Bram Moolenaar02b31112019-08-31 22:16:38 +02001177 \ map(range(-1, 5), 'v:val->line2byte()'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001178
1179 set fileformat=dos
1180 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1],
1181 \ map(range(-1, 11), 'byte2line(v:val)'))
1182 call assert_equal([-1, -1, 1, 4, 8, 11, -1],
1183 \ map(range(-1, 5), 'line2byte(v:val)'))
1184
Bram Moolenaarc26f7c62018-08-20 22:53:04 +02001185 bw!
1186 set noendofline nofixendofline
1187 normal a-
1188 for ff in ["unix", "mac", "dos"]
1189 let &fileformat = ff
1190 call assert_equal(1, line2byte(1))
1191 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte).
1192 endfor
1193
1194 set endofline& fixendofline& fileformat&
Bram Moolenaar41042f32017-03-09 12:09:32 +01001195 bw!
1196endfunc
1197
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001198" Test for byteidx() and byteidxcomp() functions
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001199func Test_byteidx()
1200 let a = '.é.' " one char of two bytes
1201 call assert_equal(0, byteidx(a, 0))
1202 call assert_equal(0, byteidxcomp(a, 0))
1203 call assert_equal(1, byteidx(a, 1))
1204 call assert_equal(1, byteidxcomp(a, 1))
1205 call assert_equal(3, byteidx(a, 2))
1206 call assert_equal(3, byteidxcomp(a, 2))
1207 call assert_equal(4, byteidx(a, 3))
1208 call assert_equal(4, byteidxcomp(a, 3))
1209 call assert_equal(-1, byteidx(a, 4))
1210 call assert_equal(-1, byteidxcomp(a, 4))
1211
1212 let b = '.é.' " normal e with composing char
1213 call assert_equal(0, b->byteidx(0))
1214 call assert_equal(1, b->byteidx(1))
1215 call assert_equal(4, b->byteidx(2))
1216 call assert_equal(5, b->byteidx(3))
1217 call assert_equal(-1, b->byteidx(4))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001218 call assert_fails("call byteidx([], 0)", 'E730:')
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001219
1220 call assert_equal(0, b->byteidxcomp(0))
1221 call assert_equal(1, b->byteidxcomp(1))
1222 call assert_equal(2, b->byteidxcomp(2))
1223 call assert_equal(4, b->byteidxcomp(3))
1224 call assert_equal(5, b->byteidxcomp(4))
1225 call assert_equal(-1, b->byteidxcomp(5))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001226 call assert_fails("call byteidxcomp([], 0)", 'E730:')
Bram Moolenaar64b4d732019-08-22 22:18:17 +02001227endfunc
1228
Bram Moolenaar17793ef2020-12-28 12:56:58 +01001229" Test for charidx()
1230func Test_charidx()
1231 let a = 'xáb́y'
1232 call assert_equal(0, charidx(a, 0))
1233 call assert_equal(1, charidx(a, 3))
1234 call assert_equal(2, charidx(a, 4))
1235 call assert_equal(3, charidx(a, 7))
1236 call assert_equal(-1, charidx(a, 8))
Dominique Pelle6d37e8e2021-05-06 17:36:55 +02001237 call assert_equal(-1, charidx(a, -1))
Bram Moolenaar17793ef2020-12-28 12:56:58 +01001238 call assert_equal(-1, charidx('', 0))
Dominique Pelle6d37e8e2021-05-06 17:36:55 +02001239 call assert_equal(-1, charidx(test_null_string(), 0))
Bram Moolenaar17793ef2020-12-28 12:56:58 +01001240
1241 " count composing characters
1242 call assert_equal(0, charidx(a, 0, 1))
1243 call assert_equal(2, charidx(a, 2, 1))
1244 call assert_equal(3, charidx(a, 4, 1))
1245 call assert_equal(5, charidx(a, 7, 1))
1246 call assert_equal(-1, charidx(a, 8, 1))
1247 call assert_equal(-1, charidx('', 0, 1))
1248
1249 call assert_fails('let x = charidx([], 1)', 'E474:')
1250 call assert_fails('let x = charidx("abc", [])', 'E474:')
1251 call assert_fails('let x = charidx("abc", 1, [])', 'E474:')
1252 call assert_fails('let x = charidx("abc", 1, -1)', 'E1023:')
1253 call assert_fails('let x = charidx("abc", 1, 2)', 'E1023:')
1254endfunc
1255
Bram Moolenaar41042f32017-03-09 12:09:32 +01001256func Test_count()
1257 let l = ['a', 'a', 'A', 'b']
1258 call assert_equal(2, count(l, 'a'))
1259 call assert_equal(1, count(l, 'A'))
1260 call assert_equal(1, count(l, 'b'))
1261 call assert_equal(0, count(l, 'B'))
1262
1263 call assert_equal(2, count(l, 'a', 0))
1264 call assert_equal(1, count(l, 'A', 0))
1265 call assert_equal(1, count(l, 'b', 0))
1266 call assert_equal(0, count(l, 'B', 0))
1267
1268 call assert_equal(3, count(l, 'a', 1))
1269 call assert_equal(3, count(l, 'A', 1))
1270 call assert_equal(1, count(l, 'b', 1))
1271 call assert_equal(1, count(l, 'B', 1))
1272 call assert_equal(0, count(l, 'c', 1))
1273
1274 call assert_equal(1, count(l, 'a', 0, 1))
1275 call assert_equal(2, count(l, 'a', 1, 1))
1276 call assert_fails('call count(l, "a", 0, 10)', 'E684:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02001277 call assert_fails('call count(l, "a", [])', 'E745:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001278
1279 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'}
1280 call assert_equal(2, count(d, 'a'))
1281 call assert_equal(1, count(d, 'A'))
1282 call assert_equal(1, count(d, 'b'))
1283 call assert_equal(0, count(d, 'B'))
1284
1285 call assert_equal(2, count(d, 'a', 0))
1286 call assert_equal(1, count(d, 'A', 0))
1287 call assert_equal(1, count(d, 'b', 0))
1288 call assert_equal(0, count(d, 'B', 0))
1289
1290 call assert_equal(3, count(d, 'a', 1))
1291 call assert_equal(3, count(d, 'A', 1))
1292 call assert_equal(1, count(d, 'b', 1))
1293 call assert_equal(1, count(d, 'B', 1))
1294 call assert_equal(0, count(d, 'c', 1))
1295
1296 call assert_fails('call count(d, "a", 0, 1)', 'E474:')
Bram Moolenaar9966b212017-07-28 16:46:57 +02001297
1298 call assert_equal(0, count("foo", "bar"))
1299 call assert_equal(1, count("foo", "oo"))
1300 call assert_equal(2, count("foo", "o"))
1301 call assert_equal(0, count("foo", "O"))
1302 call assert_equal(2, count("foo", "O", 1))
1303 call assert_equal(2, count("fooooo", "oo"))
Bram Moolenaar338e47f2017-12-19 11:55:26 +01001304 call assert_equal(0, count("foo", ""))
Bram Moolenaar17aca702019-05-16 22:24:55 +02001305
1306 call assert_fails('call count(0, 0)', 'E712:')
Bram Moolenaar41042f32017-03-09 12:09:32 +01001307endfunc
1308
1309func Test_changenr()
1310 new Xchangenr
1311 call assert_equal(0, changenr())
1312 norm ifoo
1313 call assert_equal(1, changenr())
1314 set undolevels=10
1315 norm Sbar
1316 call assert_equal(2, changenr())
1317 undo
1318 call assert_equal(1, changenr())
1319 redo
1320 call assert_equal(2, changenr())
1321 bw!
1322 set undolevels&
1323endfunc
1324
1325func Test_filewritable()
1326 new Xfilewritable
1327 write!
1328 call assert_equal(1, filewritable('Xfilewritable'))
1329
1330 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----'))
1331 call assert_equal(0, filewritable('Xfilewritable'))
1332
1333 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001334 call assert_equal(1, 'Xfilewritable'->filewritable())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001335
1336 call assert_equal(0, filewritable('doesnotexist'))
1337
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001338 call mkdir('Xwritedir')
1339 call assert_equal(2, filewritable('Xwritedir'))
1340 call delete('Xwritedir', 'd')
Bram Moolenaar0ff5ded2020-05-07 18:43:44 +02001341
Bram Moolenaar41042f32017-03-09 12:09:32 +01001342 call delete('Xfilewritable')
1343 bw!
1344endfunc
1345
Bram Moolenaar82956662018-10-06 15:18:45 +02001346func Test_Executable()
1347 if has('win32')
1348 call assert_equal(1, executable('notepad'))
Bram Moolenaara4208962019-08-24 20:50:19 +02001349 call assert_equal(1, 'notepad.exe'->executable())
Bram Moolenaar82956662018-10-06 15:18:45 +02001350 call assert_equal(0, executable('notepad.exe.exe'))
1351 call assert_equal(0, executable('shell32.dll'))
1352 call assert_equal(0, executable('win.ini'))
Bram Moolenaar95da1362020-05-30 18:37:55 +02001353
1354 " get "notepad" path and remove the leading drive and sep. (ex. 'C:\')
1355 let notepadcmd = exepath('notepad.exe')
1356 let driveroot = notepadcmd[:2]
1357 let notepadcmd = notepadcmd[3:]
1358 new
1359 " check that the relative path works in /
1360 execute 'lcd' driveroot
1361 call assert_equal(1, executable(notepadcmd))
1362 call assert_equal(driveroot .. notepadcmd, notepadcmd->exepath())
1363 bwipe
1364
1365 " create "notepad.bat"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001366 call mkdir('Xnotedir')
1367 let notepadbat = fnamemodify('Xnotedir/notepad.bat', ':p')
Bram Moolenaar95da1362020-05-30 18:37:55 +02001368 call writefile([], notepadbat)
1369 new
1370 " check that the path and the pathext order is valid
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001371 lcd Xnotedir
Bram Moolenaar95da1362020-05-30 18:37:55 +02001372 let [pathext, $PATHEXT] = [$PATHEXT, '.com;.exe;.bat;.cmd']
1373 call assert_equal(notepadbat, exepath('notepad'))
1374 let $PATHEXT = pathext
1375 bwipe
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01001376 eval 'Xnotedir'->delete('rf')
Bram Moolenaar82956662018-10-06 15:18:45 +02001377 elseif has('unix')
Bram Moolenaara4208962019-08-24 20:50:19 +02001378 call assert_equal(1, 'cat'->executable())
Bram Moolenaara05a0d32018-10-07 18:43:05 +02001379 call assert_equal(0, executable('nodogshere'))
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001380
1381 " get "cat" path and remove the leading /
1382 let catcmd = exepath('cat')[1:]
1383 new
Bram Moolenaara4208962019-08-24 20:50:19 +02001384 " check that the relative path works in /
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001385 lcd /
1386 call assert_equal(1, executable(catcmd))
Bram Moolenaara3870832021-01-01 14:20:44 +01001387 let result = catcmd->exepath()
1388 " when using chroot looking for sbin/cat can return bin/cat, that is OK
1389 if catcmd =~ '\<sbin\>' && result =~ '\<bin\>'
1390 call assert_equal('/' .. substitute(catcmd, '\<sbin\>', 'bin', ''), result)
1391 else
Bram Moolenaarbf634a02021-07-31 17:20:04 +02001392 " /bin/cat and /usr/bin/cat may be hard linked, we could get either
1393 let result = substitute(result, '/usr/bin/cat', '/bin/cat', '')
1394 let catcmd = substitute(catcmd, 'usr/bin/cat', 'bin/cat', '')
Bram Moolenaara3870832021-01-01 14:20:44 +01001395 call assert_equal('/' .. catcmd, result)
1396 endif
Bram Moolenaard08b8c42019-07-24 14:59:45 +02001397 bwipe
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001398 else
1399 throw 'Skipped: does not work on this platform'
Bram Moolenaar82956662018-10-06 15:18:45 +02001400 endif
1401endfunc
1402
LemonBoy40fd7e62022-05-05 20:18:16 +01001403func Test_executable_windows_store_apps()
1404 CheckMSWindows
1405
1406 " Windows Store apps install some 'decoy' .exe that require some careful
1407 " handling as they behave similarly to symlinks.
1408 let app_dir = expand("$LOCALAPPDATA\\Microsoft\\WindowsApps")
1409 if !isdirectory(app_dir)
1410 return
1411 endif
1412
1413 let save_path = $PATH
1414 let $PATH = app_dir
1415 " Ensure executable() finds all the app .exes
1416 for entry in readdir(app_dir)
1417 if entry =~ '\.exe$'
1418 call assert_true(executable(entry))
1419 endif
1420 endfor
1421
1422 let $PATH = save_path
1423endfunc
1424
Bram Moolenaar86621892019-03-30 21:51:28 +01001425func Test_executable_longname()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001426 CheckMSWindows
Bram Moolenaar86621892019-03-30 21:51:28 +01001427
Bram Moolenaarf637bce2020-11-23 18:14:56 +01001428 " Create a temporary .bat file with 205 characters in the name.
1429 " Maximum length of a filename (including the path) on MS-Windows is 259
1430 " characters.
1431 " See https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
1432 let len = 259 - getcwd()->len() - 6
1433 if len > 200
1434 let len = 200
1435 endif
1436
1437 let fname = 'X' . repeat('あ', len) . '.bat'
Bram Moolenaar86621892019-03-30 21:51:28 +01001438 call writefile([], fname)
1439 call assert_equal(1, executable(fname))
1440 call delete(fname)
1441endfunc
1442
Bram Moolenaar41042f32017-03-09 12:09:32 +01001443func Test_hostname()
1444 let hostname_vim = hostname()
1445 if has('unix')
1446 let hostname_system = systemlist('uname -n')[0]
1447 call assert_equal(hostname_vim, hostname_system)
1448 endif
1449endfunc
1450
1451func Test_getpid()
1452 " getpid() always returns the same value within a vim instance.
1453 call assert_equal(getpid(), getpid())
1454 if has('unix')
1455 call assert_equal(systemlist('echo $PPID')[0], string(getpid()))
1456 endif
1457endfunc
1458
1459func Test_hlexists()
1460 call assert_equal(0, hlexists('does_not_exist'))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001461 call assert_equal(0, 'Number'->hlexists())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001462 call assert_equal(0, highlight_exists('does_not_exist'))
1463 call assert_equal(0, highlight_exists('Number'))
1464 syntax on
1465 call assert_equal(0, hlexists('does_not_exist'))
1466 call assert_equal(1, hlexists('Number'))
1467 call assert_equal(0, highlight_exists('does_not_exist'))
1468 call assert_equal(1, highlight_exists('Number'))
1469 syntax off
1470endfunc
1471
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001472" Test for the col() function
Bram Moolenaar41042f32017-03-09 12:09:32 +01001473func Test_col()
1474 new
1475 call setline(1, 'abcdef')
1476 norm gg4|mx6|mY2|
1477 call assert_equal(2, col('.'))
1478 call assert_equal(7, col('$'))
Bram Moolenaar8b633132020-03-20 18:20:51 +01001479 call assert_equal(2, col('v'))
Bram Moolenaar41042f32017-03-09 12:09:32 +01001480 call assert_equal(4, col("'x"))
1481 call assert_equal(6, col("'Y"))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02001482 call assert_equal(2, [1, 2]->col())
Bram Moolenaar41042f32017-03-09 12:09:32 +01001483 call assert_equal(7, col([1, '$']))
1484
1485 call assert_equal(0, col(''))
1486 call assert_equal(0, col('x'))
1487 call assert_equal(0, col([2, '$']))
1488 call assert_equal(0, col([1, 100]))
1489 call assert_equal(0, col([1]))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02001490 call assert_equal(0, col(test_null_list()))
1491 call assert_fails('let c = col({})', 'E731:')
Bram Moolenaar8b633132020-03-20 18:20:51 +01001492
1493 " test for getting the visual start column
1494 func T()
1495 let g:Vcol = col('v')
1496 return ''
1497 endfunc
1498 let g:Vcol = 0
1499 xmap <expr> <F2> T()
1500 exe "normal gg3|ve\<F2>"
1501 call assert_equal(3, g:Vcol)
1502 xunmap <F2>
1503 delfunc T
1504
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001505 " Test for the visual line start and end marks '< and '>
1506 call setline(1, ['one', 'one two', 'one two three'])
1507 "normal! ggVG
1508 call feedkeys("ggVG\<Esc>", 'xt')
1509 call assert_equal(1, col("'<"))
1510 call assert_equal(14, col("'>"))
1511 " Delete the last line of the visually selected region
1512 $d
1513 call assert_notequal(14, col("'>"))
1514
1515 " Test with 'virtualedit'
1516 set virtualedit=all
1517 call cursor(1, 10)
1518 call assert_equal(4, col('.'))
1519 set virtualedit&
1520
Bram Moolenaar41042f32017-03-09 12:09:32 +01001521 bw!
1522endfunc
1523
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001524" Test for input()
1525func Test_input_func()
1526 " Test for prompt with multiple lines
1527 redir => v
1528 call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt')
1529 redir END
1530 call assert_equal("B", c)
1531 call assert_equal(['A', 'B', 'C'], split(v, "\n"))
1532
1533 " Test for default value
1534 call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt')
1535 call assert_equal('red', c)
1536
1537 " Test for completion at the input prompt
1538 func! Tcomplete(arglead, cmdline, pos)
1539 return "item1\nitem2\nitem3"
1540 endfunc
Bram Moolenaar9d489562020-07-30 20:08:50 +02001541 call feedkeys(":let c = input('Q? ', '', 'custom,Tcomplete')\<CR>"
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001542 \ .. "\<C-A>\<CR>", 'xt')
1543 delfunc Tcomplete
1544 call assert_equal('item1 item2 item3', c)
Bram Moolenaar578fe942020-02-27 21:32:51 +01001545
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +01001546 " Test for using special characters as default input
Bram Moolenaar1f448d92021-03-22 19:37:06 +01001547 call feedkeys(":let c = input('name? ', \"x\\<BS>y\")\<CR>\<CR>", 'xt')
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +01001548 call assert_equal('y', c)
1549
zeertzjqe3a529b2022-06-05 19:01:37 +01001550 " Test for using text with composing characters as default input
1551 call feedkeys(":let c = input('name? ', \"ã̳\")\<CR>\<CR>", 'xt')
1552 call assert_equal('ã̳', c)
1553
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +01001554 " Test for using <CR> as default input
1555 call feedkeys(":let c = input('name? ', \"\\<CR>\")\<CR>x\<CR>", 'xt')
1556 call assert_equal(' x', c)
1557
Bram Moolenaar578fe942020-02-27 21:32:51 +01001558 call assert_fails("call input('F:', '', 'invalid')", 'E180:')
1559 call assert_fails("call input('F:', '', [])", 'E730:')
1560endfunc
1561
1562" Test for the inputdialog() function
1563func Test_inputdialog()
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02001564 set timeout timeoutlen=10
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001565 if has('gui_running')
1566 call assert_fails('let v=inputdialog([], "xx")', 'E730:')
1567 call assert_fails('let v=inputdialog("Q", [])', 'E730:')
1568 else
1569 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt')
1570 call assert_equal('xx', v)
1571 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt')
1572 call assert_equal('yy', v)
1573 endif
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02001574 set timeout& timeoutlen&
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01001575endfunc
1576
1577" Test for inputlist()
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001578func Test_inputlist()
1579 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx')
1580 call assert_equal(1, c)
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001581 call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001582 call assert_equal(2, c)
1583 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx')
1584 call assert_equal(3, c)
1585
Bram Moolenaareebd5552020-06-10 15:45:57 +02001586 " CR to cancel
1587 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<cr>", 'tx')
1588 call assert_equal(0, c)
1589
1590 " Esc to cancel
1591 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<Esc>", 'tx')
1592 call assert_equal(0, c)
1593
1594 " q to cancel
1595 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>q", 'tx')
1596 call assert_equal(0, c)
1597
=?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?=5cf94572021-05-20 21:14:20 +02001598 " Cancel after inputting a number
1599 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>5q", 'tx')
1600 call assert_equal(0, c)
1601
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001602 " Use backspace to delete characters in the prompt
1603 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<BS>3\<BS>2\<cr>", 'tx')
1604 call assert_equal(2, c)
1605
1606 " Use mouse to make a selection
1607 call test_setmouse(&lines - 3, 2)
1608 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
1609 call assert_equal(1, c)
1610 " Mouse click outside of the list
1611 call test_setmouse(&lines - 6, 2)
1612 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx')
1613 call assert_equal(-2, c)
1614
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001615 call assert_fails('call inputlist("")', 'E686:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001616 call assert_fails('call inputlist(test_null_list())', 'E686:')
Bram Moolenaar947b39e2018-07-22 19:36:37 +02001617endfunc
1618
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001619func Test_balloon_show()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001620 CheckFeature balloon_eval
Bram Moolenaarb47bed22021-04-14 17:06:43 +02001621
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001622 " This won't do anything but must not crash either.
1623 call balloon_show('hi!')
1624 if !has('gui_running')
1625 call balloon_show(range(3))
1626 call balloon_show([])
Bram Moolenaara0107bd2017-03-02 22:48:01 +01001627 endif
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001628endfunc
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001629
1630func Test_setbufvar_options()
1631 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the
1632 " window layout.
1633 call assert_equal(1, winnr('$'))
1634 split dummy_preview
1635 resize 2
1636 set winfixheight winfixwidth
1637 let prev_id = win_getid()
1638
1639 wincmd j
Bram Moolenaarc05d1c02020-09-04 18:38:06 +02001640 let wh = winheight(0)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001641 let dummy_buf = bufnr('dummy_buf1', v:true)
1642 call setbufvar(dummy_buf, '&buftype', 'nofile')
1643 execute 'belowright vertical split #' . dummy_buf
Bram Moolenaarc05d1c02020-09-04 18:38:06 +02001644 call assert_equal(wh, winheight(0))
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001645 let dum1_id = win_getid()
1646
1647 wincmd h
Bram Moolenaarc05d1c02020-09-04 18:38:06 +02001648 let wh = winheight(0)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001649 let dummy_buf = bufnr('dummy_buf2', v:true)
Bram Moolenaar196b4662019-09-06 21:34:30 +02001650 eval 'nofile'->setbufvar(dummy_buf, '&buftype')
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001651 execute 'belowright vertical split #' . dummy_buf
Bram Moolenaarc05d1c02020-09-04 18:38:06 +02001652 call assert_equal(wh, winheight(0))
Bram Moolenaar2c90d512017-03-18 22:35:30 +01001653
1654 bwipe!
1655 call win_gotoid(prev_id)
1656 bwipe!
1657 call win_gotoid(dum1_id)
1658 bwipe!
1659endfunc
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001660
Bram Moolenaardff97e62022-01-24 20:00:55 +00001661func Test_setbufvar_keep_window_title()
1662 CheckRunVimInTerminal
Bram Moolenaara6c09a72022-01-24 22:02:15 +00001663 if !has('title') || empty(&t_ts)
1664 throw "Skipped: can't get/set title"
1665 endif
Bram Moolenaardff97e62022-01-24 20:00:55 +00001666
1667 let lines =<< trim END
Bram Moolenaar14501122022-01-24 22:32:28 +00001668 set title
Bram Moolenaardff97e62022-01-24 20:00:55 +00001669 edit Xa.txt
1670 let g:buf = bufadd('Xb.txt')
1671 inoremap <F2> <C-R>=setbufvar(g:buf, '&autoindent', 1) ?? ''<CR>
1672 END
1673 call writefile(lines, 'Xsetbufvar')
1674 let buf = RunVimInTerminal('-S Xsetbufvar', {})
Bram Moolenaar3a8ad592022-01-24 22:18:24 +00001675 call WaitForAssert({-> assert_match('Xa.txt', term_gettitle(buf))}, 1000)
Bram Moolenaardff97e62022-01-24 20:00:55 +00001676
1677 call term_sendkeys(buf, "i\<F2>")
1678 call TermWait(buf)
1679 call term_sendkeys(buf, "\<Esc>")
1680 call TermWait(buf)
1681 call assert_match('Xa.txt', term_gettitle(buf))
1682
1683 call StopVimInTerminal(buf)
1684 call delete('Xsetbufvar')
1685endfunc
1686
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001687func Test_redo_in_nested_functions()
1688 nnoremap g. :set opfunc=Operator<CR>g@
1689 function Operator( type, ... )
1690 let @x = 'XXX'
1691 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp'
1692 endfunction
1693
1694 function! Apply()
1695 5,6normal! .
1696 endfunction
1697
1698 new
1699 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3))
1700 1normal g.i"
1701 call assert_equal('some "XXX" text', getline(1))
1702 3,4normal .
1703 call assert_equal('some "XXX" text', getline(3))
1704 call assert_equal('more "XXX" text', getline(4))
1705 call Apply()
1706 call assert_equal('some "XXX" text', getline(5))
1707 call assert_equal('more "XXX" text', getline(6))
1708 bwipe!
1709
1710 nunmap g.
1711 delfunc Operator
1712 delfunc Apply
1713endfunc
Bram Moolenaar20615522017-06-05 18:46:26 +02001714
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001715func Test_trim()
1716 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B"))
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02001717 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 +01001718 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t"))
1719 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww"))
1720 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail"))
1721 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " "))
1722 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx"))
1723 call assert_equal("RESERVE", trim("RESERVE", "你好"))
1724 call assert_equal("R E SER V E", trim("你好您R E SER V E早好你你", "你好"))
1725 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", ))
1726 call assert_equal("R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好"))
1727 call assert_equal("R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes"))
1728 call assert_equal("R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses"))
1729 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要"))
1730 call assert_equal("", trim("", ""))
1731 call assert_equal("a", trim("a", ""))
1732 call assert_equal("", trim("", "a"))
1733
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001734 call assert_equal("vim", trim(" vim ", " ", 0))
1735 call assert_equal("vim ", trim(" vim ", " ", 1))
1736 call assert_equal(" vim", trim(" vim ", " ", 2))
1737 call assert_fails('eval trim(" vim ", " ", [])', 'E745:')
1738 call assert_fails('eval trim(" vim ", " ", -1)', 'E475:')
1739 call assert_fails('eval trim(" vim ", " ", 3)', 'E475:')
Dominique Pelled176ca32021-09-09 20:45:34 +02001740 call assert_fails('eval trim(" vim ", 0)', 'E475:')
Bram Moolenaar2245ae12020-05-31 22:20:36 +02001741
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +02001742 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001743 call assert_equal("x", trim(chars . "x" . chars))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001744
1745 call assert_fails('let c=trim([])', 'E730:')
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001746endfunc
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001747
1748" Test for reg_recording() and reg_executing()
1749func Test_reg_executing_and_recording()
1750 let s:reg_stat = ''
1751 func s:save_reg_stat()
1752 let s:reg_stat = reg_recording() . ':' . reg_executing()
1753 return ''
1754 endfunc
1755
1756 new
1757 call s:save_reg_stat()
1758 call assert_equal(':', s:reg_stat)
1759 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt')
1760 call assert_equal('a:', s:reg_stat)
1761 call feedkeys("@a", 'xt')
1762 call assert_equal(':a', s:reg_stat)
1763 call feedkeys("qb@aq", 'xt')
1764 call assert_equal('b:a', s:reg_stat)
1765 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt')
1766 call assert_equal('":', s:reg_stat)
1767
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001768 " :normal command saves and restores reg_executing
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001769 let s:reg_stat = ''
Bram Moolenaarcce713d2019-03-04 11:40:12 +01001770 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>"
1771 func TestFunc() abort
1772 normal! ia
1773 endfunc
1774 call feedkeys("@q", 'xt')
1775 call assert_equal(':q', s:reg_stat)
1776 delfunc TestFunc
1777
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001778 " getchar() command saves and restores reg_executing
1779 map W :call TestFunc()<CR>
1780 let @q = "W"
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001781 let g:typed = ''
1782 let g:regs = []
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001783 func TestFunc() abort
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001784 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001785 let g:typed = getchar(0)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001786 let g:regs += [reg_executing()]
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001787 endfunc
1788 call feedkeys("@qy", 'xt')
1789 call assert_equal(char2nr("y"), g:typed)
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001790 call assert_equal(['q', 'q'], g:regs)
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001791 delfunc TestFunc
1792 unmap W
1793 unlet g:typed
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001794 unlet g:regs
1795
1796 " input() command saves and restores reg_executing
1797 map W :call TestFunc()<CR>
1798 let @q = "W"
1799 let g:typed = ''
1800 let g:regs = []
1801 func TestFunc() abort
1802 let g:regs += [reg_executing()]
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001803 let g:typed = '?'->input()
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001804 let g:regs += [reg_executing()]
1805 endfunc
1806 call feedkeys("@qy\<CR>", 'xt')
1807 call assert_equal("y", g:typed)
1808 call assert_equal(['q', 'q'], g:regs)
1809 delfunc TestFunc
1810 unmap W
1811 unlet g:typed
1812 unlet g:regs
Bram Moolenaarf0fab302019-03-05 12:24:10 +01001813
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02001814 bwipe!
1815 delfunc s:save_reg_stat
1816 unlet s:reg_stat
1817endfunc
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001818
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +02001819func Test_inputsecret()
1820 map W :call TestFunc()<CR>
1821 let @q = "W"
1822 let g:typed1 = ''
1823 let g:typed2 = ''
1824 let g:regs = []
1825 func TestFunc() abort
1826 let g:typed1 = '?'->inputsecret()
1827 let g:typed2 = inputsecret('password: ')
1828 endfunc
1829 call feedkeys("@qsomething\<CR>else\<CR>", 'xt')
1830 call assert_equal("something", g:typed1)
1831 call assert_equal("else", g:typed2)
1832 delfunc TestFunc
1833 unmap W
1834 unlet g:typed1
1835 unlet g:typed2
1836endfunc
1837
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001838func Test_getchar()
1839 call feedkeys('a', '')
1840 call assert_equal(char2nr('a'), getchar())
Bram Moolenaar3a7503c2021-06-07 18:29:17 +02001841 call assert_equal(0, getchar(0))
1842 call assert_equal(0, getchar(1))
1843
1844 call feedkeys('a', '')
1845 call assert_equal('a', getcharstr())
1846 call assert_equal('', getcharstr(0))
1847 call assert_equal('', getcharstr(1))
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001848
zeertzjqad6c45f2022-02-20 19:05:10 +00001849 call feedkeys("\<M-F2>", '')
1850 call assert_equal("\<M-F2>", getchar(0))
1851 call assert_equal(0, getchar(0))
1852
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001853 call setline(1, 'xxxx')
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001854 call test_setmouse(1, 3)
1855 let v:mouse_win = 9
1856 let v:mouse_winid = 9
1857 let v:mouse_lnum = 9
1858 let v:mouse_col = 9
1859 call feedkeys("\<S-LeftMouse>", '')
1860 call assert_equal("\<S-LeftMouse>", getchar())
1861 call assert_equal(1, v:mouse_win)
1862 call assert_equal(win_getid(1), v:mouse_winid)
1863 call assert_equal(1, v:mouse_lnum)
1864 call assert_equal(3, v:mouse_col)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01001865 enew!
Bram Moolenaar5d712e42019-09-03 23:37:01 +02001866endfunc
1867
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001868func Test_libcall_libcallnr()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001869 CheckFeature libcall
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001870
1871 if has('win32')
1872 let libc = 'msvcrt.dll'
1873 elseif has('mac')
1874 let libc = 'libSystem.B.dylib'
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001875 elseif executable('ldd')
1876 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>')
1877 endif
1878 if get(l:, 'libc', '') ==# ''
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001879 " On Unix, libc.so can be in various places.
Bram Moolenaar39536dd2019-01-29 22:58:21 +01001880 if has('linux')
1881 " There is not documented but regarding the 1st argument of glibc's
1882 " dlopen an empty string and nullptr are equivalent, so using an empty
1883 " string for the 1st argument of libcall allows to call functions.
1884 let libc = ''
1885 elseif has('sun')
1886 " Set the path to libc.so according to the architecture.
1887 let test_bits = system('file ' . GetVimProg())
1888 let test_arch = system('uname -p')
1889 if test_bits =~ '64-bit' && test_arch =~ 'sparc'
1890 let libc = '/usr/lib/sparcv9/libc.so'
1891 elseif test_bits =~ '64-bit' && test_arch =~ 'i386'
1892 let libc = '/usr/lib/amd64/libc.so'
1893 else
1894 let libc = '/usr/lib/libc.so'
1895 endif
1896 else
1897 " Unfortunately skip this test until a good way is found.
1898 return
1899 endif
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001900 endif
1901
1902 if has('win32')
Bram Moolenaar02b31112019-08-31 22:16:38 +02001903 call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001904 else
Bram Moolenaar02b31112019-08-31 22:16:38 +02001905 call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001906 endif
1907
1908 " If function returns NULL, libcall() should return an empty string.
1909 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT'))
1910
1911 " Test libcallnr() with string and integer argument.
Bram Moolenaar02b31112019-08-31 22:16:38 +02001912 call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen'))
1913 call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper'))
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001914
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001915 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", ['', 'E364:'])
1916 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", ['', 'E364:'])
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001917
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001918 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", ['', 'E364:'])
1919 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", ['', 'E364:'])
Bram Moolenaar1ceebb42018-06-19 19:46:06 +02001920endfunc
Bram Moolenaard90a1442018-07-15 20:24:31 +02001921
1922sandbox function Fsandbox()
1923 normal ix
1924endfunc
1925
1926func Test_func_sandbox()
1927 sandbox let F = {-> 'hello'}
1928 call assert_equal('hello', F())
1929
Bram Moolenaara4208962019-08-24 20:50:19 +02001930 sandbox let F = {-> "normal ix\<Esc>"->execute()}
Bram Moolenaard90a1442018-07-15 20:24:31 +02001931 call assert_fails('call F()', 'E48:')
1932 unlet F
1933
1934 call assert_fails('call Fsandbox()', 'E48:')
1935 delfunc Fsandbox
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001936
1937 " From a sandbox try to set a predefined variable (which cannot be modified
1938 " from a sandbox)
1939 call assert_fails('sandbox let v:lnum = 10', 'E794:')
Bram Moolenaard90a1442018-07-15 20:24:31 +02001940endfunc
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001941
1942func EditAnotherFile()
1943 let word = expand('<cword>')
1944 edit Xfuncrange2
1945endfunc
1946
1947func Test_func_range_with_edit()
1948 " Define a function that edits another buffer, then call it with a range that
1949 " is invalid in that buffer.
1950 call writefile(['just one line'], 'Xfuncrange2')
1951 new
Bram Moolenaar196b4662019-09-06 21:34:30 +02001952 eval 10->range()->setline(1)
Bram Moolenaar9e353b52018-11-04 23:39:38 +01001953 write Xfuncrange1
1954 call assert_fails('5,8call EditAnotherFile()', 'E16:')
1955
1956 call delete('Xfuncrange1')
1957 call delete('Xfuncrange2')
1958 bwipe!
1959endfunc
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001960
1961func Test_func_exists_on_reload()
1962 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists')
1963 call assert_equal(0, exists('*ExistingFunction'))
1964 source Xfuncexists
Bram Moolenaara4208962019-08-24 20:50:19 +02001965 call assert_equal(1, '*ExistingFunction'->exists())
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001966 " Redefining a function when reloading a script is OK.
1967 source Xfuncexists
1968 call assert_equal(1, exists('*ExistingFunction'))
1969
1970 " But redefining in another script is not OK.
1971 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2')
1972 call assert_fails('source Xfuncexists2', 'E122:')
1973
Yegappan Lakshmanan611728f2021-05-24 15:15:47 +02001974 " Defining a new function from the cmdline should fail if the function is
1975 " already defined
1976 call assert_fails('call feedkeys(":func ExistingFunction()\<CR>", "xt")', 'E122:')
1977
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01001978 delfunc ExistingFunction
1979 call assert_equal(0, exists('*ExistingFunction'))
1980 call writefile([
1981 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc',
1982 \ 'func ExistingFunction()', 'echo "no"', 'endfunc',
1983 \ ], 'Xfuncexists')
1984 call assert_fails('source Xfuncexists', 'E122:')
1985 call assert_equal(1, exists('*ExistingFunction'))
1986
1987 call delete('Xfuncexists2')
1988 call delete('Xfuncexists')
1989 delfunc ExistingFunction
1990endfunc
Bram Moolenaar2e050092019-01-27 15:00:36 +01001991
1992" Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1993func Test_confirm()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001994 CheckUnix
1995 CheckNotGui
Bram Moolenaar2e050092019-01-27 15:00:36 +01001996
1997 call feedkeys('o', 'L')
1998 let a = confirm('Press O to proceed')
1999 call assert_equal(1, a)
2000
2001 call feedkeys('y', 'L')
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02002002 let a = 'Are you sure?'->confirm("&Yes\n&No")
Bram Moolenaar2e050092019-01-27 15:00:36 +01002003 call assert_equal(1, a)
2004
2005 call feedkeys('n', 'L')
2006 let a = confirm('Are you sure?', "&Yes\n&No")
2007 call assert_equal(2, a)
2008
2009 " confirm() should return 0 when pressing CTRL-C.
Bram Moolenaar79296512020-03-22 16:17:14 +01002010 call feedkeys("\<C-C>", 'L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01002011 let a = confirm('Are you sure?', "&Yes\n&No")
2012 call assert_equal(0, a)
2013
2014 " <Esc> requires another character to avoid it being seen as the start of an
2015 " escape sequence. Zero should be harmless.
Bram Moolenaara4208962019-08-24 20:50:19 +02002016 eval "\<Esc>0"->feedkeys('L')
Bram Moolenaar2e050092019-01-27 15:00:36 +01002017 let a = confirm('Are you sure?', "&Yes\n&No")
2018 call assert_equal(0, a)
2019
2020 " Default choice is returned when pressing <CR>.
2021 call feedkeys("\<CR>", 'L')
2022 let a = confirm('Are you sure?', "&Yes\n&No")
2023 call assert_equal(1, a)
2024
2025 call feedkeys("\<CR>", 'L')
2026 let a = confirm('Are you sure?', "&Yes\n&No", 2)
2027 call assert_equal(2, a)
2028
2029 call feedkeys("\<CR>", 'L')
2030 let a = confirm('Are you sure?', "&Yes\n&No", 0)
2031 call assert_equal(0, a)
2032
2033 " Test with the {type} 4th argument
2034 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic']
2035 call feedkeys('y', 'L')
2036 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type)
2037 call assert_equal(1, a)
2038 endfor
2039
2040 call assert_fails('call confirm([])', 'E730:')
2041 call assert_fails('call confirm("Are you sure?", [])', 'E730:')
2042 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:')
2043 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:')
2044endfunc
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002045
2046func Test_platform_name()
2047 " The system matches at most only one name.
Bram Moolenaar041c7102020-05-30 18:14:57 +02002048 let names = ['amiga', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix']
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002049 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)')))
2050
2051 " Is Unix?
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002052 call assert_equal(has('bsd'), has('bsd') && has('unix'))
2053 call assert_equal(has('hpux'), has('hpux') && has('unix'))
2054 call assert_equal(has('linux'), has('linux') && has('unix'))
2055 call assert_equal(has('mac'), has('mac') && has('unix'))
2056 call assert_equal(has('qnx'), has('qnx') && has('unix'))
2057 call assert_equal(has('sun'), has('sun') && has('unix'))
2058 call assert_equal(has('win32'), has('win32') && !has('unix'))
2059 call assert_equal(has('win32unix'), has('win32unix') && has('unix'))
2060
2061 if has('unix') && executable('uname')
2062 let uname = system('uname')
Bram Moolenaara02e3f62019-02-07 21:27:14 +01002063 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined
2064 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd'))
Bram Moolenaar39536dd2019-01-29 22:58:21 +01002065 call assert_equal(uname =~? 'HP-UX', has('hpux'))
2066 call assert_equal(uname =~? 'Linux', has('linux'))
2067 call assert_equal(uname =~? 'Darwin', has('mac'))
2068 call assert_equal(uname =~? 'QNX', has('qnx'))
2069 call assert_equal(uname =~? 'SunOS', has('sun'))
2070 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
2071 endif
2072endfunc
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002073
2074func Test_readdir()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002075 call mkdir('Xreaddir')
2076 call writefile([], 'Xreaddir/foo.txt')
2077 call writefile([], 'Xreaddir/bar.txt')
2078 call mkdir('Xreaddir/dir')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002079
2080 " All results
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002081 let files = readdir('Xreaddir')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002082 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
2083
2084 " Only results containing "f"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002085 let files = 'Xreaddir'->readdir({ x -> stridx(x, 'f') != -1 })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002086 call assert_equal(['foo.txt'], sort(files))
2087
2088 " Only .txt files
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002089 let files = readdir('Xreaddir', { x -> x =~ '.txt$' })
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002090 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
2091
2092 " Only .txt files with string
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002093 let files = readdir('Xreaddir', 'v:val =~ ".txt$"')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002094 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
2095
2096 " Limit to 1 result.
2097 let l = []
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002098 let files = readdir('Xreaddir', {x -> len(add(l, x)) == 2 ? -1 : 1})
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002099 call assert_equal(1, len(files))
2100
Bram Moolenaar27da7de2019-09-03 17:13:37 +02002101 " Nested readdir() must not crash
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002102 let files = readdir('Xreaddir', 'readdir("Xreaddir", "1") != []')
Bram Moolenaar27da7de2019-09-03 17:13:37 +02002103 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
2104
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002105 eval 'Xreaddir'->delete('rf')
Bram Moolenaar543c9b12019-04-05 22:50:40 +02002106endfunc
Bram Moolenaar17aca702019-05-16 22:24:55 +02002107
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002108func Test_readdirex()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002109 call mkdir('Xexdir')
2110 call writefile(['foo'], 'Xexdir/foo.txt')
2111 call writefile(['barbar'], 'Xexdir/bar.txt')
2112 call mkdir('Xexdir/dir')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002113
2114 " All results
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002115 let files = readdirex('Xexdir')->map({-> v:val.name})
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002116 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002117 let sizes = readdirex('Xexdir')->map({-> v:val.size})
Bram Moolenaar441d60e2020-06-02 22:19:50 +02002118 call assert_equal([0, 4, 7], sort(sizes))
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002119
2120 " Only results containing "f"
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002121 let files = 'Xexdir'->readdirex({ e -> stridx(e.name, 'f') != -1 })
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002122 \ ->map({-> v:val.name})
2123 call assert_equal(['foo.txt'], sort(files))
2124
2125 " Only .txt files
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002126 let files = readdirex('Xexdir', { e -> e.name =~ '.txt$' })
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002127 \ ->map({-> v:val.name})
2128 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
2129
2130 " Only .txt files with string
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002131 let files = readdirex('Xexdir', 'v:val.name =~ ".txt$"')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002132 \ ->map({-> v:val.name})
2133 call assert_equal(['bar.txt', 'foo.txt'], sort(files))
2134
2135 " Limit to 1 result.
2136 let l = []
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002137 let files = readdirex('Xexdir', {e -> len(add(l, e.name)) == 2 ? -1 : 1})
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002138 \ ->map({-> v:val.name})
2139 call assert_equal(1, len(files))
2140
2141 " Nested readdirex() must not crash
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002142 let files = readdirex('Xexdir', 'readdirex("Xexdir", "1") != []')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002143 \ ->map({-> v:val.name})
2144 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt'])
2145
Bram Moolenaarfdcbe3c2020-06-15 21:41:56 +02002146 " report broken link correctly
Bram Moolenaarab540322020-06-10 15:55:36 +02002147 if has("unix")
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002148 call writefile([], 'Xexdir/abc.txt')
2149 call system("ln -s Xexdir/abc.txt Xexdir/link")
2150 call delete('Xexdir/abc.txt')
2151 let files = readdirex('Xexdir', 'readdirex("Xexdir", "1") != []')
Bram Moolenaarab540322020-06-10 15:55:36 +02002152 \ ->map({-> v:val.name .. '_' .. v:val.type})
2153 call sort(files)->assert_equal(
2154 \ ['bar.txt_file', 'dir_dir', 'foo.txt_file', 'link_link'])
2155 endif
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002156 eval 'Xexdir'->delete('rf')
Bram Moolenaaraab9fad2020-10-11 14:28:11 +02002157
2158 call assert_fails('call readdirex("doesnotexist")', 'E484:')
Bram Moolenaar6c9ba042020-06-01 16:09:41 +02002159endfunc
2160
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002161func Test_readdirex_sort()
2162 CheckUnix
2163 " Skip tests on Mac OS X and Cygwin (does not allow several files with different casing)
2164 if has("osxdarwin") || has("osx") || has("macunix") || has("win32unix")
2165 throw 'Skipped: Test_readdirex_sort on systems that do not allow this using the default filesystem'
2166 endif
2167 let _collate = v:collate
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002168 call mkdir('Xsortdir2')
2169 call writefile(['1'], 'Xsortdir2/README.txt')
2170 call writefile(['2'], 'Xsortdir2/Readme.txt')
2171 call writefile(['3'], 'Xsortdir2/readme.txt')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002172
2173 " 1) default
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002174 let files = readdirex('Xsortdir2')->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002175 let default = copy(files)
2176 call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort using default')
2177
2178 " 2) no sorting
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002179 let files = readdirex('Xsortdir2', 1, #{sort: 'none'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002180 let unsorted = copy(files)
2181 call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], sort(files), 'unsorted')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002182 call assert_fails("call readdirex('Xsortdir2', 1, #{slort: 'none'})", 'E857: Dictionary key "sort" required')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002183
2184 " 3) sort by case (same as default)
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002185 let files = readdirex('Xsortdir2', 1, #{sort: 'case'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002186 call assert_equal(default, files, 'sort by case')
2187
2188 " 4) sort by ignoring case
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002189 let files = readdirex('Xsortdir2', 1, #{sort: 'icase'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002190 call assert_equal(unsorted->sort('i'), files, 'sort by icase')
2191
2192 " 5) Default Collation
2193 let collate = v:collate
2194 lang collate C
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002195 let files = readdirex('Xsortdir2', 1, #{sort: 'collate'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002196 call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort by C collation')
2197
2198 " 6) Collation de_DE
2199 " Switch locale, this may not work on the CI system, if the locale isn't
2200 " available
2201 try
2202 lang collate de_DE
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002203 let files = readdirex('Xsortdir2', 1, #{sort: 'collate'})->map({-> v:val.name})
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002204 call assert_equal(['readme.txt', 'Readme.txt', 'README.txt'], files, 'sort by de_DE collation')
2205 catch
2206 throw 'Skipped: de_DE collation is not available'
2207
2208 finally
2209 exe 'lang collate' collate
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002210 eval 'Xsortdir2'->delete('rf')
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002211 endtry
2212endfunc
2213
2214func Test_readdir_sort()
2215 " some more cases for testing sorting for readdirex
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002216 let dir = 'Xsortdir3'
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002217 call mkdir(dir)
2218 call writefile(['1'], dir .. '/README.txt')
2219 call writefile(['2'], dir .. '/Readm.txt')
2220 call writefile(['3'], dir .. '/read.txt')
2221 call writefile(['4'], dir .. '/Z.txt')
2222 call writefile(['5'], dir .. '/a.txt')
2223 call writefile(['6'], dir .. '/b.txt')
2224
2225 " 1) default
2226 let files = readdir(dir)
2227 let default = copy(files)
2228 call assert_equal(default->sort(), files, 'sort using default')
2229
2230 " 2) sort by case (same as default)
2231 let files = readdir(dir, '1', #{sort: 'case'})
2232 call assert_equal(default, files, 'sort using default')
2233
2234 " 3) sort by ignoring case
2235 let files = readdir(dir, '1', #{sort: 'icase'})
2236 call assert_equal(default->sort('i'), files, 'sort by ignoring case')
2237
Bram Moolenaare17f8812020-06-17 20:30:44 +02002238 " 4) collation
2239 let collate = v:collate
2240 lang collate C
2241 let files = readdir(dir, 1, #{sort: 'collate'})
2242 call assert_equal(default->sort(), files, 'sort by C collation')
2243 exe "lang collate" collate
2244
2245 " 5) Errors
Bram Moolenaare2e40752020-09-04 21:18:46 +02002246 call assert_fails('call readdir(dir, 1, 1)', 'E715:')
Bram Moolenaare17f8812020-06-17 20:30:44 +02002247 call assert_fails('call readdir(dir, 1, #{sorta: 1})')
2248 call assert_fails('call readdirex(dir, 1, #{sorta: 1})')
2249
2250 " 6) ignore other values in dict
2251 let files = readdir(dir, '1', #{sort: 'c'})
2252 call assert_equal(default, files, 'sort using default2')
2253
2254 " Cleanup
2255 exe "lang collate" collate
2256
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +02002257 eval dir->delete('rf')
2258endfunc
2259
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02002260func Test_delete_rf()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002261 call mkdir('Xrfdir')
2262 call writefile([], 'Xrfdir/foo.txt')
2263 call writefile([], 'Xrfdir/bar.txt')
2264 call mkdir('Xrfdir/[a-1]') " issue #696
2265 call writefile([], 'Xrfdir/[a-1]/foo.txt')
2266 call writefile([], 'Xrfdir/[a-1]/bar.txt')
2267 call assert_true(filereadable('Xrfdir/foo.txt'))
2268 call assert_true('Xrfdir/[a-1]/foo.txt'->filereadable())
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02002269
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002270 call assert_equal(0, delete('Xrfdir', 'rf'))
2271 call assert_false(filereadable('Xrfdir/foo.txt'))
2272 call assert_false(filereadable('Xrfdir/[a-1]/foo.txt'))
zeertzjq47870032022-04-05 15:31:01 +01002273
2274 if has('unix')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01002275 call mkdir('Xrfdir/Xdir2', 'p')
2276 silent !chmod 555 Xrfdir
2277 call assert_equal(-1, delete('Xrfdir/Xdir2', 'rf'))
2278 call assert_equal(-1, delete('Xrfdir', 'rf'))
2279 silent !chmod 755 Xrfdir
2280 call assert_equal(0, delete('Xrfdir', 'rf'))
zeertzjq47870032022-04-05 15:31:01 +01002281 endif
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02002282endfunc
2283
Bram Moolenaar17aca702019-05-16 22:24:55 +02002284func Test_call()
2285 call assert_equal(3, call('len', [123]))
Bram Moolenaar64b4d732019-08-22 22:18:17 +02002286 call assert_equal(3, 'len'->call([123]))
Bram Moolenaar17aca702019-05-16 22:24:55 +02002287 call assert_fails("call call('len', 123)", 'E714:')
2288 call assert_equal(0, call('', []))
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002289 call assert_equal(0, call('len', test_null_list()))
Bram Moolenaar17aca702019-05-16 22:24:55 +02002290
2291 function Mylen() dict
2292 return len(self.data)
2293 endfunction
2294 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
Bram Moolenaar64b4d732019-08-22 22:18:17 +02002295 eval mydict.len->call([], mydict)->assert_equal(4)
Bram Moolenaar17aca702019-05-16 22:24:55 +02002296 call assert_fails("call call('Mylen', [], 0)", 'E715:')
Bram Moolenaar67322bf2020-12-06 15:03:19 +01002297 call assert_fails('call foo', 'E107:')
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002298
Bram Moolenaar22db0d52021-06-12 12:16:55 +02002299 " These once caused a crash.
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02002300 call call(test_null_function(), [])
2301 call call(test_null_partial(), [])
Bram Moolenaar22db0d52021-06-12 12:16:55 +02002302 call assert_fails('call test_null_function()()', 'E1192:')
2303 call assert_fails('call test_null_partial()()', 'E117:')
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002304
2305 let lines =<< trim END
2306 let Time = 'localtime'
2307 call Time()
2308 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002309 call v9.CheckScriptFailure(lines, 'E1085:')
Bram Moolenaar17aca702019-05-16 22:24:55 +02002310endfunc
2311
2312func Test_char2nr()
2313 call assert_equal(12354, char2nr('あ', 1))
Bram Moolenaar1a3a8912019-08-23 22:31:37 +02002314 call assert_equal(120, 'x'->char2nr())
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002315 set encoding=latin1
2316 call assert_equal(120, 'x'->char2nr())
2317 set encoding=utf-8
Bram Moolenaar17aca702019-05-16 22:24:55 +02002318endfunc
2319
Bram Moolenaar4e4473c2020-08-28 22:24:57 +02002320func Test_charclass()
2321 call assert_equal(0, charclass(' '))
2322 call assert_equal(1, charclass('.'))
2323 call assert_equal(2, charclass('x'))
2324 call assert_equal(3, charclass("\u203c"))
Christian Brabandt72463f82021-07-02 20:19:31 +02002325 " this used to crash vim
2326 call assert_equal(0, "xxx"[-1]->charclass())
Bram Moolenaar4e4473c2020-08-28 22:24:57 +02002327endfunc
2328
Bram Moolenaar17aca702019-05-16 22:24:55 +02002329func Test_eventhandler()
2330 call assert_equal(0, eventhandler())
2331endfunc
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002332
2333func Test_bufadd_bufload()
2334 call assert_equal(0, bufexists('someName'))
2335 let buf = bufadd('someName')
2336 call assert_notequal(0, buf)
2337 call assert_equal(1, bufexists('someName'))
2338 call assert_equal(0, getbufvar(buf, '&buflisted'))
2339 call assert_equal(0, bufloaded(buf))
2340 call bufload(buf)
2341 call assert_equal(1, bufloaded(buf))
2342 call assert_equal([''], getbufline(buf, 1, '$'))
2343
2344 let curbuf = bufnr('')
Bram Moolenaarf92e58c2019-09-08 21:51:41 +02002345 eval ['some', 'text']->writefile('XotherName')
Bram Moolenaar073e4b92019-08-18 23:01:56 +02002346 let buf = 'XotherName'->bufadd()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002347 call assert_notequal(0, buf)
Bram Moolenaar073e4b92019-08-18 23:01:56 +02002348 eval 'XotherName'->bufexists()->assert_equal(1)
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002349 call assert_equal(0, getbufvar(buf, '&buflisted'))
2350 call assert_equal(0, bufloaded(buf))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02002351 eval buf->bufload()
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002352 call assert_equal(1, bufloaded(buf))
2353 call assert_equal(['some', 'text'], getbufline(buf, 1, '$'))
2354 call assert_equal(curbuf, bufnr(''))
2355
Bram Moolenaar892ae722019-06-30 20:33:01 +02002356 let buf1 = bufadd('')
2357 let buf2 = bufadd('')
2358 call assert_notequal(0, buf1)
2359 call assert_notequal(0, buf2)
2360 call assert_notequal(buf1, buf2)
2361 call assert_equal(1, bufexists(buf1))
2362 call assert_equal(1, bufexists(buf2))
2363 call assert_equal(0, bufloaded(buf1))
2364 exe 'bwipe ' .. buf1
2365 call assert_equal(0, bufexists(buf1))
2366 call assert_equal(1, bufexists(buf2))
2367 exe 'bwipe ' .. buf2
2368 call assert_equal(0, bufexists(buf2))
2369
zeertzjq93f72cc2022-08-26 15:34:52 +01002370 " When 'buftype' is "nofile" then bufload() does not read the file.
2371 " Other values too.
2372 for val in [['nofile', 0],
2373 \ ['nowrite', 1],
2374 \ ['acwrite', 1],
2375 \ ['quickfix', 0],
2376 \ ['help', 1],
2377 \ ['terminal', 0],
2378 \ ['prompt', 0],
2379 \ ['popup', 0],
2380 \ ]
2381 bwipe! XotherName
2382 let buf = bufadd('XotherName')
2383 call setbufvar(buf, '&bt', val[0])
2384 call bufload(buf)
2385 call assert_equal(val[1] ? ['some', 'text'] : [''], getbufline(buf, 1, '$'), val[0])
2386 endfor
Bram Moolenaarc3126192022-08-26 12:58:17 +01002387
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002388 bwipe someName
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002389 bwipe XotherName
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002390 call assert_equal(0, bufexists('someName'))
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002391 call delete('XotherName')
Bram Moolenaar15e248e2019-06-30 20:21:37 +02002392endfunc
Bram Moolenaarc2585492019-09-22 21:29:53 +02002393
2394func Test_state()
2395 CheckRunVimInTerminal
2396
Bram Moolenaar3ed9efc2020-03-26 16:50:57 +01002397 let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>"
2398
Bram Moolenaarc2585492019-09-22 21:29:53 +02002399 let lines =<< trim END
2400 call setline(1, ['one', 'two', 'three'])
2401 map ;; gg
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02002402 set complete=.
Bram Moolenaarc2585492019-09-22 21:29:53 +02002403 func RunTimer()
2404 call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')})
2405 endfunc
2406 au Filetype foobar let g:state = state()|let g:mode = mode()
2407 END
2408 call writefile(lines, 'XState')
2409 let buf = RunVimInTerminal('-S XState', #{rows: 6})
2410
2411 " Using a ":" command Vim is busy, thus "S" is returned
2412 call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>")
2413 call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000)
2414 call term_sendkeys(buf, ":\<CR>")
2415
2416 " Using a timer callback
2417 call term_sendkeys(buf, ":call RunTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002418 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002419 call term_sendkeys(buf, getstate)
2420 call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000)
2421
2422 " Halfway a mapping
2423 call term_sendkeys(buf, ":call RunTimer()\<CR>;")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002424 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002425 call term_sendkeys(buf, ";")
2426 call term_sendkeys(buf, getstate)
2427 call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000)
2428
Bram Moolenaarb7a97ef2019-09-28 22:11:56 +02002429 " Insert mode completion (bit slower on Mac)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002430 call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002431 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002432 call term_sendkeys(buf, "\<Esc>")
2433 call term_sendkeys(buf, getstate)
2434 call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000)
2435
2436 " Autocommand executing
2437 call term_sendkeys(buf, ":set filetype=foobar\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002438 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002439 call term_sendkeys(buf, getstate)
2440 call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000)
2441
2442 " Todo: "w" - waiting for ch_evalexpr()
2443
2444 " messages scrolled
2445 call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002446 call TermWait(buf, 25)
Bram Moolenaarc2585492019-09-22 21:29:53 +02002447 call term_sendkeys(buf, "\<CR>")
2448 call term_sendkeys(buf, getstate)
2449 call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000)
2450
2451 call StopVimInTerminal(buf)
2452 call delete('XState')
2453endfunc
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002454
2455func Test_range()
2456 " destructuring
2457 let [x, y] = range(2)
2458 call assert_equal([0, 1], [x, y])
2459
2460 " index
2461 call assert_equal(4, range(1, 10)[3])
2462
2463 " add()
2464 call assert_equal([0, 1, 2, 3], add(range(3), 3))
2465 call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3)))
2466 call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3)))
2467
2468 " append()
2469 new
2470 call append('.', range(5))
2471 call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$'))
2472 bwipe!
2473
2474 " appendbufline()
2475 new
2476 call appendbufline(bufnr(''), '.', range(5))
2477 call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$'))
2478 bwipe!
2479
2480 " call()
2481 func TwoArgs(a, b)
2482 return [a:a, a:b]
2483 endfunc
2484 call assert_equal([0, 1], call('TwoArgs', range(2)))
2485
2486 " col()
2487 new
2488 call setline(1, ['foo', 'bar'])
2489 call assert_equal(2, col(range(1, 2)))
2490 bwipe!
2491
2492 " complete()
2493 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>"
2494 " complete_info()
2495 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>"
2496
2497 " copy()
2498 call assert_equal([1, 2, 3], copy(range(1, 3)))
2499
2500 " count()
2501 call assert_equal(0, count(range(0), 3))
2502 call assert_equal(0, count(range(2), 3))
2503 call assert_equal(1, count(range(5), 3))
2504
2505 " cursor()
2506 new
2507 call setline(1, ['aaa', 'bbb', 'ccc'])
2508 call cursor(range(1, 2))
2509 call assert_equal([2, 1], [col('.'), line('.')])
2510 bwipe!
2511
2512 " deepcopy()
2513 call assert_equal([1, 2, 3], deepcopy(range(1, 3)))
2514
2515 " empty()
2516 call assert_true(empty(range(0)))
2517 call assert_false(empty(range(2)))
2518
2519 " execute()
2520 new
2521 call setline(1, ['aaa', 'bbb', 'ccc'])
2522 call execute(range(3))
2523 call assert_equal(2, line('.'))
2524 bwipe!
2525
2526 " extend()
2527 call assert_equal([1, 2, 3, 4], extend([1], range(2, 4)))
2528 call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4)))
2529 call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4]))
2530
2531 " filter()
2532 call assert_equal([1, 3], filter(range(5), 'v:val % 2'))
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002533 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 +01002534
2535 " funcref()
2536 call assert_equal([0, 1], funcref('TwoArgs', range(2))())
2537
2538 " function()
2539 call assert_equal([0, 1], function('TwoArgs', range(2))())
2540
2541 " garbagecollect()
2542 let thelist = [1, range(2), 3]
2543 let otherlist = range(3)
2544 call test_garbagecollect_now()
2545
2546 " get()
2547 call assert_equal(4, get(range(1, 10), 3))
2548 call assert_equal(-1, get(range(1, 10), 42, -1))
2549
2550 " index()
2551 call assert_equal(1, index(range(1, 5), 2))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002552 call assert_fails("echo index([1, 2], 1, [])", 'E745:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002553
2554 " inputlist()
Bram Moolenaar272ca952020-01-28 20:49:11 +01002555 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x')
2556 call assert_equal(1, result)
2557 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x')
2558 call assert_equal(1, result)
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002559
2560 " insert()
2561 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42))
2562 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0))
2563 call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1))
2564 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4))
2565 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1))
2566 call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5))
2567
2568 " join()
2569 call assert_equal('0 1 2 3 4', join(range(5)))
2570
Bram Moolenaar272ca952020-01-28 20:49:11 +01002571 " json_encode()
2572 call assert_equal('[0,1,2,3]', json_encode(range(4)))
2573
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002574 " len()
2575 call assert_equal(0, len(range(0)))
2576 call assert_equal(2, len(range(2)))
2577 call assert_equal(5, len(range(0, 12, 3)))
2578 call assert_equal(4, len(range(3, 0, -1)))
2579
2580 " list2str()
2581 call assert_equal('ABC', list2str(range(65, 67)))
Bram Moolenaar08f41572020-04-20 16:50:00 +02002582 call assert_fails('let s = list2str(5)', 'E474:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002583
2584 " lock()
2585 let thelist = range(5)
2586 lockvar thelist
2587
2588 " map()
2589 call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2'))
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002590 call assert_equal([3, 5, 7, 9, 11], map(map(range(5), 'v:val * 2'), 'v:val + 3'))
2591 call assert_equal([2, 6], map(filter(range(5), 'v:val % 2'), 'v:val * 2'))
2592 call assert_equal([2, 4, 8], filter(map(range(5), 'v:val * 2'), 'v:val % 3'))
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002593
2594 " match()
2595 call assert_equal(3, match(range(5), 3))
2596
2597 " matchaddpos()
2598 highlight MyGreenGroup ctermbg=green guibg=green
2599 call matchaddpos('MyGreenGroup', range(line('.'), line('.')))
2600
2601 " matchend()
2602 call assert_equal(4, matchend(range(5), '4'))
2603 call assert_equal(3, matchend(range(1, 5), '4'))
2604 call assert_equal(-1, matchend(range(1, 5), '42'))
2605
2606 " matchstrpos()
2607 call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4'))
2608 call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4'))
2609 call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42'))
2610
2611 " max() reverse()
2612 call assert_equal(0, max(range(0)))
2613 call assert_equal(0, max(range(10, 9)))
2614 call assert_equal(9, max(range(10)))
2615 call assert_equal(18, max(range(0, 20, 3)))
2616 call assert_equal(20, max(range(20, 0, -3)))
2617 call assert_equal(99999, max(range(100000)))
2618 call assert_equal(99999, max(range(99999, 0, -1)))
2619 call assert_equal(99999, max(reverse(range(100000))))
2620 call assert_equal(99999, max(reverse(range(99999, 0, -1))))
2621
2622 " min() reverse()
2623 call assert_equal(0, min(range(0)))
2624 call assert_equal(0, min(range(10, 9)))
2625 call assert_equal(5, min(range(5, 10)))
2626 call assert_equal(5, min(range(5, 10, 3)))
2627 call assert_equal(2, min(range(20, 0, -3)))
2628 call assert_equal(0, min(range(100000)))
2629 call assert_equal(0, min(range(99999, 0, -1)))
2630 call assert_equal(0, min(reverse(range(100000))))
2631 call assert_equal(0, min(reverse(range(99999, 0, -1))))
2632
2633 " remove()
2634 call assert_equal(1, remove(range(1, 10), 0))
2635 call assert_equal(2, remove(range(1, 10), 1))
2636 call assert_equal(9, remove(range(1, 10), 8))
2637 call assert_equal(10, remove(range(1, 10), 9))
2638 call assert_equal(10, remove(range(1, 10), -1))
2639 call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4))
2640
2641 " repeat()
2642 call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2))
2643 call assert_equal([0, 1, 2], repeat(range(3), 1))
2644 call assert_equal([], repeat(range(3), 0))
2645 call assert_equal([], repeat(range(5, 4), 2))
2646 call assert_equal([], repeat(range(5, 4), 0))
2647
2648 " reverse()
2649 call assert_equal([2, 1, 0], reverse(range(3)))
2650 call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1)))
2651 call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10)))
2652 call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20)))
2653 call assert_equal([16, 13, 10], reverse(range(10, 18, 3)))
2654 call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3)))
2655 call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3)))
2656 call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3)))
2657 call assert_equal([], reverse(range(0)))
2658
2659 " TODO: setpos()
2660 " new
2661 " call setline(1, repeat([''], bufnr('')))
2662 " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6))
2663 " call setpos('x', range(bufnr(''), bufnr('') + 3))
2664 " bwipe!
2665
2666 " setreg()
2667 call setreg('a', range(3))
2668 call assert_equal("0\n1\n2\n", getreg('a'))
2669
Bram Moolenaarb0992022020-01-30 14:55:42 +01002670 " settagstack()
2671 call settagstack(1, #{items : range(4)})
Bram Moolenaar94255df2020-02-05 20:10:33 +01002672
Bram Moolenaarb0992022020-01-30 14:55:42 +01002673 " sign_define()
2674 call assert_fails("call sign_define(range(5))", "E715:")
2675 call assert_fails("call sign_placelist(range(5))", "E715:")
2676
2677 " sign_undefine()
2678 call assert_fails("call sign_undefine(range(5))", "E908:")
2679
2680 " sign_unplacelist()
2681 call assert_fails("call sign_unplacelist(range(5))", "E715:")
2682
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002683 " sort()
2684 call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1)))
2685
2686 " string()
2687 call assert_equal('[0, 1, 2, 3, 4]', string(range(5)))
2688
Bram Moolenaarb0992022020-01-30 14:55:42 +01002689 " taglist() with 'tagfunc'
2690 func TagFunc(pattern, flags, info)
2691 return range(10)
2692 endfunc
2693 set tagfunc=TagFunc
2694 call assert_fails("call taglist('asdf')", 'E987:')
2695 set tagfunc=
Bram Moolenaar94255df2020-02-05 20:10:33 +01002696
Bram Moolenaarb0992022020-01-30 14:55:42 +01002697 " term_start()
Bram Moolenaar705724e2020-01-31 21:13:42 +01002698 if has('terminal') && has('termguicolors')
Bram Moolenaarb0992022020-01-30 14:55:42 +01002699 call assert_fails('call term_start(range(3, 4))', 'E474:')
2700 let g:terminal_ansi_colors = range(16)
Bram Moolenaar94255df2020-02-05 20:10:33 +01002701 if has('win32')
2702 let cmd = "cmd /c dir"
2703 else
2704 let cmd = "ls"
2705 endif
LemonBoyb2b3acb2022-05-20 10:10:34 +01002706 call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"'
2707 \ .. ', ansi_colors: range(16)})', 'E475:')
Bram Moolenaarb0855f52022-05-20 10:39:18 +01002708 unlet g:terminal_ansi_colors
Bram Moolenaarb0992022020-01-30 14:55:42 +01002709 endif
2710
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002711 " type()
2712 call assert_equal(v:t_list, type(range(5)))
2713
2714 " uniq()
2715 call assert_equal([0, 1, 2, 3, 4], uniq(range(5)))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002716
2717 " errors
2718 call assert_fails('let x=range(2, 8, 0)', 'E726:')
2719 call assert_fails('let x=range(3, 1)', 'E727:')
2720 call assert_fails('let x=range(1, 3, -2)', 'E727:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +02002721 call assert_fails('let x=range([])', 'E745:')
2722 call assert_fails('let x=range(1, [])', 'E745:')
2723 call assert_fails('let x=range(1, 4, [])', 'E745:')
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002724endfunc
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002725
Bram Moolenaarb3d83982022-01-27 19:59:47 +00002726func Test_garbagecollect_now_fails()
2727 let v:testing = 0
2728 call assert_fails('call test_garbagecollect_now()', 'E1142:')
2729 let v:testing = 1
2730endfunc
2731
Bram Moolenaar4132eb52020-02-14 16:53:00 +01002732func Test_echoraw()
2733 CheckScreendump
2734
2735 " Normally used for escape codes, but let's test with a CR.
2736 let lines =<< trim END
2737 call echoraw("hello\<CR>x")
2738 END
2739 call writefile(lines, 'XTest_echoraw')
2740 let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40})
2741 call VerifyScreenDump(buf, 'Test_functions_echoraw', {})
2742
2743 " clean up
2744 call StopVimInTerminal(buf)
2745 call delete('XTest_echoraw')
2746endfunc
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002747
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002748" Test for echo highlighting
2749func Test_echohl()
2750 echohl Search
2751 echo 'Vim'
2752 call assert_equal('Vim', Screenline(&lines))
2753 " TODO: How to check the highlight group used by echohl?
2754 " ScreenAttrs() returns all zeros.
2755 echohl None
2756endfunc
2757
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002758" Test for the eval() function
2759func Test_eval()
2760 call assert_fails("call eval('5 a')", 'E488:')
2761endfunc
2762
2763" Test for the nr2char() function
2764func Test_nr2char()
2765 set encoding=latin1
2766 call assert_equal('@', nr2char(64))
2767 set encoding=utf8
2768 call assert_equal('a', nr2char(97, 1))
2769 call assert_equal('a', nr2char(97, 0))
Bram Moolenaarf7271e82020-05-24 18:45:07 +02002770
zeertzjqdb088872022-05-02 22:53:45 +01002771 call assert_equal("\x80\xfc\b" .. nr2char(0x100000), eval('"\<M-' .. nr2char(0x100000) .. '>"'))
2772 call assert_equal("\x80\xfc\b" .. nr2char(0x40000000), eval('"\<M-' .. nr2char(0x40000000) .. '>"'))
Bram Moolenaar0e05de42020-03-25 22:23:46 +01002773endfunc
2774
2775" Test for screenattr(), screenchar() and screenchars() functions
2776func Test_screen_functions()
2777 call assert_equal(-1, screenattr(-1, -1))
2778 call assert_equal(-1, screenchar(-1, -1))
2779 call assert_equal([], screenchars(-1, -1))
2780endfunc
2781
Bram Moolenaar08f41572020-04-20 16:50:00 +02002782" Test for getcurpos() and setpos()
2783func Test_getcurpos_setpos()
2784 new
2785 call setline(1, ['012345678', '012345678'])
2786 normal gg6l
2787 let sp = getcurpos()
2788 normal 0
2789 call setpos('.', sp)
2790 normal jyl
2791 call assert_equal('6', @")
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002792 call assert_equal(-1, setpos('.', test_null_list()))
2793 call assert_equal(-1, setpos('.', {}))
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02002794
2795 let winid = win_getid()
2796 normal G$
2797 let pos = getcurpos()
2798 wincmd w
2799 call assert_equal(pos, getcurpos(winid))
2800
2801 wincmd w
Bram Moolenaar08f41572020-04-20 16:50:00 +02002802 close!
Bram Moolenaar99ca9c42020-09-22 21:55:41 +02002803
2804 call assert_equal(getcurpos(), getcurpos(0))
2805 call assert_equal([0, 0, 0, 0, 0], getcurpos(-1))
2806 call assert_equal([0, 0, 0, 0, 0], getcurpos(1999))
Bram Moolenaar08f41572020-04-20 16:50:00 +02002807endfunc
2808
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00002809func Test_getmousepos()
2810 enew!
2811 call setline(1, "\t\t\t1234")
Bram Moolenaar533870a2022-03-13 15:52:44 +00002812 call test_setmouse(1, 1)
2813 call assert_equal(#{
2814 \ screenrow: 1,
2815 \ screencol: 1,
2816 \ winid: win_getid(),
2817 \ winrow: 1,
2818 \ wincol: 1,
2819 \ line: 1,
2820 \ column: 1,
2821 \ }, getmousepos())
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00002822 call test_setmouse(1, 25)
2823 call assert_equal(#{
2824 \ screenrow: 1,
2825 \ screencol: 25,
2826 \ winid: win_getid(),
2827 \ winrow: 1,
2828 \ wincol: 25,
2829 \ line: 1,
Bram Moolenaar533870a2022-03-13 15:52:44 +00002830 \ column: 4,
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00002831 \ }, getmousepos())
2832 call test_setmouse(1, 50)
2833 call assert_equal(#{
2834 \ screenrow: 1,
2835 \ screencol: 50,
2836 \ winid: win_getid(),
2837 \ winrow: 1,
2838 \ wincol: 50,
2839 \ line: 1,
Bram Moolenaar533870a2022-03-13 15:52:44 +00002840 \ column: 8,
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00002841 \ }, getmousepos())
Sean Dewar10792fe2022-03-15 09:46:54 +00002842
2843 " If the mouse is positioned past the last buffer line, "line" and "column"
2844 " should act like it's positioned on the last buffer line.
2845 call test_setmouse(2, 25)
2846 call assert_equal(#{
2847 \ screenrow: 2,
2848 \ screencol: 25,
2849 \ winid: win_getid(),
2850 \ winrow: 2,
2851 \ wincol: 25,
2852 \ line: 1,
2853 \ column: 4,
2854 \ }, getmousepos())
2855 call test_setmouse(2, 50)
2856 call assert_equal(#{
2857 \ screenrow: 2,
2858 \ screencol: 50,
2859 \ winid: win_getid(),
2860 \ winrow: 2,
2861 \ wincol: 50,
2862 \ line: 1,
2863 \ column: 8,
2864 \ }, getmousepos())
Bram Moolenaar986b0fd2022-03-13 12:06:07 +00002865 bwipe!
2866endfunc
2867
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002868" Test for glob()
2869func Test_glob()
2870 call assert_equal('', glob(test_null_string()))
2871 call assert_equal('', globpath(test_null_string(), test_null_string()))
Yegappan Lakshmanan46aa6f92021-05-19 17:15:04 +02002872 call assert_fails("let x = globpath(&rtp, 'syntax/c.vim', [])", 'E745:')
Bram Moolenaar1b04ce22020-08-21 22:46:11 +02002873
2874 call writefile([], 'Xglob1')
2875 call writefile([], 'XGLOB2')
2876 set wildignorecase
2877 " Sort output of glob() otherwise we end up with different
2878 " ordering depending on whether file system is case-sensitive.
2879 call assert_equal(['XGLOB2', 'Xglob1'], sort(glob('Xglob[12]', 0, 1)))
LemonBoya3157a42022-04-03 11:58:31 +01002880 " wildignorecase shall be applied even when the pattern contains no wildcards.
2881 call assert_equal('XGLOB2', glob('xglob2'))
Bram Moolenaar1b04ce22020-08-21 22:46:11 +02002882 set wildignorecase&
2883
2884 call delete('Xglob1')
2885 call delete('XGLOB2')
2886
2887 call assert_fails("call glob('*', 0, {})", 'E728:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002888endfunc
2889
2890" Test for browse()
2891func Test_browse()
2892 CheckFeature browse
2893 call assert_fails('call browse([], "open", "x", "a.c")', 'E745:')
2894endfunc
2895
2896" Test for browsedir()
2897func Test_browsedir()
2898 CheckFeature browse
2899 call assert_fails('call browsedir("open", [])', 'E730:')
2900endfunc
2901
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002902func HasDefault(msg = 'msg')
2903 return a:msg
2904endfunc
2905
2906func Test_default_arg_value()
2907 call assert_equal('msg', HasDefault())
2908endfunc
2909
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +02002910" Test for gettext()
2911func Test_gettext()
2912 call assert_fails('call gettext(1)', 'E475:')
2913endfunc
2914
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002915func Test_builtin_check()
2916 call assert_fails('let g:["trim"] = {x -> " " .. x}', 'E704:')
2917 call assert_fails('let g:.trim = {x -> " " .. x}', 'E704:')
Bram Moolenaarb54abee2021-06-02 11:49:23 +02002918 call assert_fails('let l:["trim"] = {x -> " " .. x}', 'E704:')
2919 call assert_fails('let l:.trim = {x -> " " .. x}', 'E704:')
2920 let lines =<< trim END
2921 vim9script
Bram Moolenaar62b191c2022-02-12 20:34:50 +00002922 var trim = (x) => " " .. x
Bram Moolenaarb54abee2021-06-02 11:49:23 +02002923 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00002924 call v9.CheckScriptFailure(lines, 'E704:')
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02002925
2926 call assert_fails('call extend(g:, #{foo: { -> "foo" }})', 'E704:')
2927 let g:bar = 123
2928 call extend(g:, #{bar: { -> "foo" }}, "keep")
2929 call assert_fails('call extend(g:, #{bar: { -> "foo" }}, "force")', 'E704:')
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002930endfunc
2931
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00002932func Test_funcref_to_string()
2933 let Fn = funcref('g:Test_funcref_to_string')
2934 call assert_equal("function('g:Test_funcref_to_string')", string(Fn))
2935endfunc
2936
LemonBoydca1d402022-04-28 15:26:33 +01002937" Test for isabsolutepath()
2938func Test_isabsolutepath()
2939 call assert_false(isabsolutepath(''))
2940 call assert_false(isabsolutepath('.'))
2941 call assert_false(isabsolutepath('../Foo'))
2942 call assert_false(isabsolutepath('Foo/'))
2943 if has('win32')
2944 call assert_true(isabsolutepath('A:\'))
2945 call assert_true(isabsolutepath('A:\Foo'))
2946 call assert_true(isabsolutepath('A:/Foo'))
2947 call assert_false(isabsolutepath('A:Foo'))
2948 call assert_false(isabsolutepath('\Windows'))
2949 call assert_true(isabsolutepath('\\Server2\Share\Test\Foo.txt'))
2950 else
2951 call assert_true(isabsolutepath('/'))
2952 call assert_true(isabsolutepath('/usr/share/'))
2953 endif
2954endfunc
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02002955
Yasuhiro Matsumoto05cf63e2022-05-03 11:02:28 +01002956" Test for exepath()
2957func Test_exepath()
2958 if has('win32')
2959 call assert_notequal(exepath('cmd'), '')
2960
2961 let oldNoDefaultCurrentDirectoryInExePath = $NoDefaultCurrentDirectoryInExePath
2962 call writefile(['@echo off', 'echo Evil'], 'vim-test-evil.bat')
2963 let $NoDefaultCurrentDirectoryInExePath = ''
2964 call assert_notequal(exepath("vim-test-evil.bat"), '')
2965 let $NoDefaultCurrentDirectoryInExePath = '1'
2966 call assert_equal(exepath("vim-test-evil.bat"), '')
2967 let $NoDefaultCurrentDirectoryInExePath = oldNoDefaultCurrentDirectoryInExePath
2968 call delete('vim-test-evil.bat')
2969 else
2970 call assert_notequal(exepath('sh'), '')
2971 endif
2972endfunc
2973
LemonBoy0f7a3e12022-05-26 12:10:37 +01002974" Test for virtcol()
2975func Test_virtcol()
2976 enew!
2977 call setline(1, "the\tquick\tbrown\tfox")
2978 norm! 4|
2979 call assert_equal(8, virtcol('.'))
2980 call assert_equal(8, virtcol('.', v:false))
2981 call assert_equal([4, 8], virtcol('.', v:true))
2982 bwipe!
2983endfunc
2984
Bram Moolenaar8d588cc2020-02-25 21:47:45 +01002985" vim: shiftwidth=2 sts=2 expandtab