Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 1 | " Tests for various functions. |
Bram Moolenaar | f1c118b | 2018-09-03 22:08:10 +0200 | [diff] [blame] | 2 | source shared.vim |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 3 | source check.vim |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 4 | |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 5 | " Must be done first, since the alternate buffer must be unset. |
| 6 | func Test_00_bufexists() |
| 7 | call assert_equal(0, bufexists('does_not_exist')) |
| 8 | call assert_equal(1, bufexists(bufnr('%'))) |
| 9 | call assert_equal(0, bufexists(0)) |
| 10 | new Xfoo |
| 11 | let bn = bufnr('%') |
| 12 | call assert_equal(1, bufexists(bn)) |
| 13 | call assert_equal(1, bufexists('Xfoo')) |
| 14 | call assert_equal(1, bufexists(getcwd() . '/Xfoo')) |
| 15 | call assert_equal(1, bufexists(0)) |
| 16 | bw |
| 17 | call assert_equal(0, bufexists(bn)) |
| 18 | call assert_equal(0, bufexists('Xfoo')) |
| 19 | endfunc |
| 20 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 21 | func Test_empty() |
| 22 | call assert_equal(1, empty('')) |
| 23 | call assert_equal(0, empty('a')) |
| 24 | |
| 25 | call assert_equal(1, empty(0)) |
| 26 | call assert_equal(1, empty(-0)) |
| 27 | call assert_equal(0, empty(1)) |
| 28 | call assert_equal(0, empty(-1)) |
| 29 | |
| 30 | call assert_equal(1, empty(0.0)) |
| 31 | call assert_equal(1, empty(-0.0)) |
| 32 | call assert_equal(0, empty(1.0)) |
| 33 | call assert_equal(0, empty(-1.0)) |
| 34 | call assert_equal(0, empty(1.0/0.0)) |
| 35 | call assert_equal(0, empty(0.0/0.0)) |
| 36 | |
| 37 | call assert_equal(1, empty([])) |
| 38 | call assert_equal(0, empty(['a'])) |
| 39 | |
| 40 | call assert_equal(1, empty({})) |
| 41 | call assert_equal(0, empty({'a':1})) |
| 42 | |
| 43 | call assert_equal(1, empty(v:null)) |
| 44 | call assert_equal(1, empty(v:none)) |
| 45 | call assert_equal(1, empty(v:false)) |
| 46 | call assert_equal(0, empty(v:true)) |
| 47 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 48 | if has('channel') |
| 49 | call assert_equal(1, empty(test_null_channel())) |
| 50 | endif |
| 51 | if has('job') |
| 52 | call assert_equal(1, empty(test_null_job())) |
| 53 | endif |
| 54 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 55 | call assert_equal(0, empty(function('Test_empty'))) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 56 | call assert_equal(0, empty(function('Test_empty', [0]))) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 57 | endfunc |
| 58 | |
| 59 | func Test_len() |
| 60 | call assert_equal(1, len(0)) |
| 61 | call assert_equal(2, len(12)) |
| 62 | |
| 63 | call assert_equal(0, len('')) |
| 64 | call assert_equal(2, len('ab')) |
| 65 | |
| 66 | call assert_equal(0, len([])) |
| 67 | call assert_equal(2, len([2, 1])) |
| 68 | |
| 69 | call assert_equal(0, len({})) |
| 70 | call assert_equal(2, len({'a': 1, 'b': 2})) |
| 71 | |
| 72 | call assert_fails('call len(v:none)', 'E701:') |
| 73 | call assert_fails('call len({-> 0})', 'E701:') |
| 74 | endfunc |
| 75 | |
| 76 | func Test_max() |
| 77 | call assert_equal(0, max([])) |
| 78 | call assert_equal(2, max([2])) |
| 79 | call assert_equal(2, max([1, 2])) |
| 80 | call assert_equal(2, max([1, 2, v:null])) |
| 81 | |
| 82 | call assert_equal(0, max({})) |
| 83 | call assert_equal(2, max({'a':1, 'b':2})) |
| 84 | |
| 85 | call assert_fails('call max(1)', 'E712:') |
| 86 | call assert_fails('call max(v:none)', 'E712:') |
| 87 | endfunc |
| 88 | |
| 89 | func Test_min() |
| 90 | call assert_equal(0, min([])) |
| 91 | call assert_equal(2, min([2])) |
| 92 | call assert_equal(1, min([1, 2])) |
| 93 | call assert_equal(0, min([1, 2, v:null])) |
| 94 | |
| 95 | call assert_equal(0, min({})) |
| 96 | call assert_equal(1, min({'a':1, 'b':2})) |
| 97 | |
| 98 | call assert_fails('call min(1)', 'E712:') |
| 99 | call assert_fails('call min(v:none)', 'E712:') |
| 100 | endfunc |
| 101 | |
Bram Moolenaar | 42ab17b | 2018-05-20 14:11:10 +0200 | [diff] [blame] | 102 | func Test_strwidth() |
| 103 | for aw in ['single', 'double'] |
| 104 | exe 'set ambiwidth=' . aw |
| 105 | call assert_equal(0, strwidth('')) |
| 106 | call assert_equal(1, strwidth("\t")) |
| 107 | call assert_equal(3, strwidth('Vim')) |
| 108 | call assert_equal(4, strwidth(1234)) |
| 109 | call assert_equal(5, strwidth(-1234)) |
| 110 | |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 111 | call assert_equal(2, strwidth('😉')) |
| 112 | call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde')) |
| 113 | call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße')) |
Bram Moolenaar | 42ab17b | 2018-05-20 14:11:10 +0200 | [diff] [blame] | 114 | |
| 115 | call assert_fails('call strwidth({->0})', 'E729:') |
| 116 | call assert_fails('call strwidth([])', 'E730:') |
| 117 | call assert_fails('call strwidth({})', 'E731:') |
| 118 | call assert_fails('call strwidth(1.2)', 'E806:') |
| 119 | endfor |
| 120 | |
| 121 | set ambiwidth& |
| 122 | endfunc |
| 123 | |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 124 | func Test_str2nr() |
| 125 | call assert_equal(0, str2nr('')) |
| 126 | call assert_equal(1, str2nr('1')) |
| 127 | call assert_equal(1, str2nr(' 1 ')) |
| 128 | |
| 129 | call assert_equal(1, str2nr('+1')) |
| 130 | call assert_equal(1, str2nr('+ 1')) |
| 131 | call assert_equal(1, str2nr(' + 1 ')) |
| 132 | |
| 133 | call assert_equal(-1, str2nr('-1')) |
| 134 | call assert_equal(-1, str2nr('- 1')) |
| 135 | call assert_equal(-1, str2nr(' - 1 ')) |
| 136 | |
| 137 | call assert_equal(123456789, str2nr('123456789')) |
| 138 | call assert_equal(-123456789, str2nr('-123456789')) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 139 | |
| 140 | call assert_equal(5, str2nr('101', 2)) |
| 141 | call assert_equal(5, str2nr('0b101', 2)) |
| 142 | call assert_equal(5, str2nr('0B101', 2)) |
| 143 | call assert_equal(-5, str2nr('-101', 2)) |
| 144 | call assert_equal(-5, str2nr('-0b101', 2)) |
| 145 | call assert_equal(-5, str2nr('-0B101', 2)) |
| 146 | |
| 147 | call assert_equal(65, str2nr('101', 8)) |
| 148 | call assert_equal(65, str2nr('0101', 8)) |
| 149 | call assert_equal(-65, str2nr('-101', 8)) |
| 150 | call assert_equal(-65, str2nr('-0101', 8)) |
| 151 | |
| 152 | call assert_equal(11259375, str2nr('abcdef', 16)) |
| 153 | call assert_equal(11259375, str2nr('ABCDEF', 16)) |
| 154 | call assert_equal(-11259375, str2nr('-ABCDEF', 16)) |
| 155 | call assert_equal(11259375, str2nr('0xabcdef', 16)) |
| 156 | call assert_equal(11259375, str2nr('0Xabcdef', 16)) |
| 157 | call assert_equal(11259375, str2nr('0XABCDEF', 16)) |
| 158 | call assert_equal(-11259375, str2nr('-0xABCDEF', 16)) |
| 159 | |
| 160 | call assert_equal(0, str2nr('0x10')) |
| 161 | call assert_equal(0, str2nr('0b10')) |
| 162 | call assert_equal(1, str2nr('12', 2)) |
| 163 | call assert_equal(1, str2nr('18', 8)) |
| 164 | call assert_equal(1, str2nr('1g', 16)) |
| 165 | |
| 166 | call assert_equal(0, str2nr(v:null)) |
| 167 | call assert_equal(0, str2nr(v:none)) |
| 168 | |
| 169 | call assert_fails('call str2nr([])', 'E730:') |
| 170 | call assert_fails('call str2nr({->2})', 'E729:') |
| 171 | call assert_fails('call str2nr(1.2)', 'E806:') |
| 172 | call assert_fails('call str2nr(10, [])', 'E474:') |
| 173 | endfunc |
| 174 | |
| 175 | func Test_strftime() |
| 176 | if !exists('*strftime') |
| 177 | return |
| 178 | endif |
| 179 | " Format of strftime() depends on system. We assume |
| 180 | " that basic formats tested here are available and |
| 181 | " identical on all systems which support strftime(). |
| 182 | " |
| 183 | " The 2nd parameter of strftime() is a local time, so the output day |
| 184 | " of strftime() can be 17 or 18, depending on timezone. |
| 185 | call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512)) |
| 186 | " |
| 187 | call assert_match('^\d\d\d\d-\(0\d\|1[012]\)-\([012]\d\|3[01]\) \([01]\d\|2[0-3]\):[0-5]\d:\([0-5]\d\|60\)$', strftime('%Y-%m-%d %H:%M:%S')) |
| 188 | |
| 189 | call assert_fails('call strftime([])', 'E730:') |
| 190 | call assert_fails('call strftime("%Y", [])', 'E745:') |
Bram Moolenaar | db51730 | 2019-06-18 22:53:24 +0200 | [diff] [blame] | 191 | |
| 192 | " Check that the time changes after we change the timezone |
| 193 | " Save previous timezone value, if any |
| 194 | if exists('$TZ') |
| 195 | let tz = $TZ |
| 196 | endif |
| 197 | |
| 198 | " Force EST and then UTC, save the current hour (24-hour clock) for each |
| 199 | let $TZ = 'EST' | let est = strftime('%H') |
| 200 | let $TZ = 'UTC' | let utc = strftime('%H') |
| 201 | |
| 202 | " Those hours should be two bytes long, and should not be the same; if they |
| 203 | " are, a tzset(3) call may have failed somewhere |
| 204 | call assert_equal(strlen(est), 2) |
| 205 | call assert_equal(strlen(utc), 2) |
Bram Moolenaar | 87652a7 | 2019-06-18 23:07:37 +0200 | [diff] [blame] | 206 | " TODO: this fails on MS-Windows |
| 207 | if has('unix') |
| 208 | call assert_notequal(est, utc) |
| 209 | endif |
Bram Moolenaar | db51730 | 2019-06-18 22:53:24 +0200 | [diff] [blame] | 210 | |
| 211 | " If we cached a timezone value, put it back, otherwise clear it |
| 212 | if exists('tz') |
| 213 | let $TZ = tz |
| 214 | else |
| 215 | unlet $TZ |
| 216 | endif |
| 217 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 218 | endfunc |
| 219 | |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 220 | func Test_resolve_unix() |
Bram Moolenaar | 2610990 | 2018-10-06 15:43:17 +0200 | [diff] [blame] | 221 | if !has('unix') |
| 222 | return |
| 223 | endif |
| 224 | |
| 225 | " Xlink1 -> Xlink2 |
| 226 | " Xlink2 -> Xlink3 |
| 227 | silent !ln -s -f Xlink2 Xlink1 |
| 228 | silent !ln -s -f Xlink3 Xlink2 |
| 229 | call assert_equal('Xlink3', resolve('Xlink1')) |
| 230 | call assert_equal('./Xlink3', resolve('./Xlink1')) |
| 231 | call assert_equal('Xlink3/', resolve('Xlink2/')) |
| 232 | " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?! |
| 233 | "call assert_equal('Xlink3/', resolve('Xlink1/')) |
| 234 | "call assert_equal('./Xlink3/', resolve('./Xlink1/')) |
| 235 | "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/')) |
| 236 | call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1')) |
| 237 | |
| 238 | " Test resolve() with a symlink cycle. |
| 239 | " Xlink1 -> Xlink2 |
| 240 | " Xlink2 -> Xlink3 |
| 241 | " Xlink3 -> Xlink1 |
| 242 | silent !ln -s -f Xlink1 Xlink3 |
| 243 | call assert_fails('call resolve("Xlink1")', 'E655:') |
| 244 | call assert_fails('call resolve("./Xlink1")', 'E655:') |
| 245 | call assert_fails('call resolve("Xlink2")', 'E655:') |
| 246 | call assert_fails('call resolve("Xlink3")', 'E655:') |
| 247 | call delete('Xlink1') |
| 248 | call delete('Xlink2') |
| 249 | call delete('Xlink3') |
| 250 | |
| 251 | silent !ln -s -f Xdir//Xfile Xlink |
| 252 | call assert_equal('Xdir/Xfile', resolve('Xlink')) |
| 253 | call delete('Xlink') |
| 254 | |
| 255 | silent !ln -s -f Xlink2/ Xlink1 |
| 256 | call assert_equal('Xlink2', resolve('Xlink1')) |
| 257 | call assert_equal('Xlink2/', resolve('Xlink1/')) |
| 258 | call delete('Xlink1') |
| 259 | |
| 260 | silent !ln -s -f ./Xlink2 Xlink1 |
| 261 | call assert_equal('Xlink2', resolve('Xlink1')) |
| 262 | call assert_equal('./Xlink2', resolve('./Xlink1')) |
| 263 | call delete('Xlink1') |
| 264 | endfunc |
| 265 | |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 266 | func s:normalize_fname(fname) |
| 267 | let ret = substitute(a:fname, '\', '/', 'g') |
| 268 | let ret = substitute(ret, '//', '/', 'g') |
Bram Moolenaar | 1bbebab | 2019-05-29 20:36:54 +0200 | [diff] [blame] | 269 | return tolower(ret) |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 270 | endfunc |
| 271 | |
| 272 | func Test_resolve_win32() |
| 273 | if !has('win32') |
| 274 | return |
| 275 | endif |
| 276 | |
| 277 | " test for shortcut file |
| 278 | if executable('cscript') |
| 279 | new Xfile |
| 280 | wq |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 281 | let lines =<< trim END |
| 282 | Set fs = CreateObject("Scripting.FileSystemObject") |
| 283 | Set ws = WScript.CreateObject("WScript.Shell") |
| 284 | Set shortcut = ws.CreateShortcut("Xlink.lnk") |
| 285 | shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile") |
| 286 | shortcut.Save |
| 287 | END |
| 288 | call writefile(lines, 'link.vbs') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 289 | silent !cscript link.vbs |
| 290 | call delete('link.vbs') |
| 291 | call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk'))) |
| 292 | call delete('Xfile') |
| 293 | |
| 294 | call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk'))) |
| 295 | call delete('Xlink.lnk') |
| 296 | else |
| 297 | echomsg 'skipped test for shortcut file' |
| 298 | endif |
| 299 | |
| 300 | " remove files |
| 301 | call delete('Xlink') |
| 302 | call delete('Xdir', 'd') |
| 303 | call delete('Xfile') |
| 304 | |
| 305 | " test for symbolic link to a file |
| 306 | new Xfile |
| 307 | wq |
Bram Moolenaar | 4a792c8 | 2019-06-06 12:22:41 +0200 | [diff] [blame] | 308 | call assert_equal('Xfile', resolve('Xfile')) |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 309 | silent !mklink Xlink Xfile |
| 310 | if !v:shell_error |
| 311 | call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink'))) |
| 312 | call delete('Xlink') |
| 313 | else |
| 314 | echomsg 'skipped test for symbolic link to a file' |
| 315 | endif |
| 316 | call delete('Xfile') |
| 317 | |
| 318 | " test for junction to a directory |
| 319 | call mkdir('Xdir') |
| 320 | silent !mklink /J Xlink Xdir |
| 321 | if !v:shell_error |
| 322 | call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) |
| 323 | |
| 324 | call delete('Xdir', 'd') |
| 325 | |
| 326 | " test for junction already removed |
| 327 | call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) |
| 328 | call delete('Xlink') |
| 329 | else |
| 330 | echomsg 'skipped test for junction to a directory' |
| 331 | call delete('Xdir', 'd') |
| 332 | endif |
| 333 | |
| 334 | " test for symbolic link to a directory |
| 335 | call mkdir('Xdir') |
| 336 | silent !mklink /D Xlink Xdir |
| 337 | if !v:shell_error |
| 338 | call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) |
| 339 | |
| 340 | call delete('Xdir', 'd') |
| 341 | |
| 342 | " test for symbolic link already removed |
| 343 | call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) |
| 344 | call delete('Xlink') |
| 345 | else |
| 346 | echomsg 'skipped test for symbolic link to a directory' |
| 347 | call delete('Xdir', 'd') |
| 348 | endif |
| 349 | |
| 350 | " test for buffer name |
| 351 | new Xfile |
| 352 | wq |
| 353 | silent !mklink Xlink Xfile |
| 354 | if !v:shell_error |
| 355 | edit Xlink |
| 356 | call assert_equal('Xlink', bufname('%')) |
| 357 | call delete('Xlink') |
| 358 | bw! |
| 359 | else |
| 360 | echomsg 'skipped test for buffer name' |
| 361 | endif |
| 362 | call delete('Xfile') |
Bram Moolenaar | 1bbebab | 2019-05-29 20:36:54 +0200 | [diff] [blame] | 363 | |
| 364 | " test for reparse point |
| 365 | call mkdir('Xdir') |
Bram Moolenaar | 4a792c8 | 2019-06-06 12:22:41 +0200 | [diff] [blame] | 366 | call assert_equal('Xdir', resolve('Xdir')) |
Bram Moolenaar | 1bbebab | 2019-05-29 20:36:54 +0200 | [diff] [blame] | 367 | silent !mklink /D Xdirlink Xdir |
| 368 | if !v:shell_error |
| 369 | w Xdir/text.txt |
Bram Moolenaar | 4a792c8 | 2019-06-06 12:22:41 +0200 | [diff] [blame] | 370 | call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt')) |
Bram Moolenaar | 1bbebab | 2019-05-29 20:36:54 +0200 | [diff] [blame] | 371 | call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt'))) |
| 372 | call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink'))) |
Bram Moolenaar | 4a792c8 | 2019-06-06 12:22:41 +0200 | [diff] [blame] | 373 | call delete('Xdirlink') |
Bram Moolenaar | 1bbebab | 2019-05-29 20:36:54 +0200 | [diff] [blame] | 374 | else |
| 375 | echomsg 'skipped test for reparse point' |
| 376 | endif |
| 377 | |
| 378 | call delete('Xdir', 'rf') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 379 | endfunc |
| 380 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 381 | func Test_simplify() |
| 382 | call assert_equal('', simplify('')) |
| 383 | call assert_equal('/', simplify('/')) |
| 384 | call assert_equal('/', simplify('/.')) |
| 385 | call assert_equal('/', simplify('/..')) |
| 386 | call assert_equal('/...', simplify('/...')) |
| 387 | call assert_equal('./dir/file', simplify('./dir/file')) |
| 388 | call assert_equal('./dir/file', simplify('.///dir//file')) |
| 389 | call assert_equal('./dir/file', simplify('./dir/./file')) |
| 390 | call assert_equal('./file', simplify('./dir/../file')) |
| 391 | call assert_equal('../dir/file', simplify('dir/../../dir/file')) |
| 392 | call assert_equal('./file', simplify('dir/.././file')) |
| 393 | |
| 394 | call assert_fails('call simplify({->0})', 'E729:') |
| 395 | call assert_fails('call simplify([])', 'E730:') |
| 396 | call assert_fails('call simplify({})', 'E731:') |
| 397 | call assert_fails('call simplify(1.2)', 'E806:') |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 398 | endfunc |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 399 | |
Bram Moolenaar | bfde0b4 | 2018-08-08 22:27:31 +0200 | [diff] [blame] | 400 | func Test_pathshorten() |
| 401 | call assert_equal('', pathshorten('')) |
| 402 | call assert_equal('foo', pathshorten('foo')) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 403 | call assert_equal('/foo', '/foo'->pathshorten()) |
Bram Moolenaar | bfde0b4 | 2018-08-08 22:27:31 +0200 | [diff] [blame] | 404 | call assert_equal('f/', pathshorten('foo/')) |
| 405 | call assert_equal('f/bar', pathshorten('foo/bar')) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 406 | call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten()) |
Bram Moolenaar | bfde0b4 | 2018-08-08 22:27:31 +0200 | [diff] [blame] | 407 | call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar')) |
| 408 | call assert_equal('.f/bar', pathshorten('.foo/bar')) |
| 409 | call assert_equal('~f/bar', pathshorten('~foo/bar')) |
| 410 | call assert_equal('~.f/bar', pathshorten('~.foo/bar')) |
| 411 | call assert_equal('.~f/bar', pathshorten('.~foo/bar')) |
| 412 | call assert_equal('~/f/bar', pathshorten('~/foo/bar')) |
| 413 | endfunc |
| 414 | |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 415 | func Test_strpart() |
| 416 | call assert_equal('de', strpart('abcdefg', 3, 2)) |
| 417 | call assert_equal('ab', strpart('abcdefg', -2, 4)) |
| 418 | call assert_equal('abcdefg', strpart('abcdefg', -2)) |
| 419 | call assert_equal('fg', strpart('abcdefg', 5, 4)) |
| 420 | call assert_equal('defg', strpart('abcdefg', 3)) |
| 421 | |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 422 | call assert_equal('lép', strpart('éléphant', 2, 4)) |
| 423 | call assert_equal('léphant', strpart('éléphant', 2)) |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 424 | endfunc |
| 425 | |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 426 | func Test_tolower() |
| 427 | call assert_equal("", tolower("")) |
| 428 | |
| 429 | " Test with all printable ASCII characters. |
| 430 | call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~', |
| 431 | \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')) |
| 432 | |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 433 | " Test with a few uppercase diacritics. |
| 434 | call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ")) |
| 435 | call assert_equal("bḃḇ", tolower("BḂḆ")) |
| 436 | call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ")) |
| 437 | call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ")) |
| 438 | call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ")) |
| 439 | call assert_equal("fḟ ", tolower("FḞ ")) |
| 440 | call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ")) |
| 441 | call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ")) |
| 442 | call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ")) |
| 443 | call assert_equal("jĵ", tolower("JĴ")) |
| 444 | call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ")) |
| 445 | call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ")) |
| 446 | call assert_equal("mḿṁ", tolower("MḾṀ")) |
| 447 | call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ")) |
| 448 | call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ")) |
| 449 | call assert_equal("pṕṗ", tolower("PṔṖ")) |
| 450 | call assert_equal("q", tolower("Q")) |
| 451 | call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ")) |
| 452 | call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ")) |
| 453 | call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ")) |
| 454 | call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ")) |
| 455 | call assert_equal("vṽ", tolower("VṼ")) |
| 456 | call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ")) |
| 457 | call assert_equal("xẋẍ", tolower("XẊẌ")) |
| 458 | call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ")) |
| 459 | call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ")) |
| 460 | |
| 461 | " Test with a few lowercase diacritics, which should remain unchanged. |
| 462 | call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả")) |
| 463 | call assert_equal("bḃḇ", tolower("bḃḇ")) |
| 464 | call assert_equal("cçćĉċč", tolower("cçćĉċč")) |
| 465 | call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ")) |
| 466 | call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ")) |
| 467 | call assert_equal("fḟ", tolower("fḟ")) |
| 468 | call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ")) |
| 469 | call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ")) |
| 470 | call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ")) |
| 471 | call assert_equal("jĵǰ", tolower("jĵǰ")) |
| 472 | call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ")) |
| 473 | call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ")) |
| 474 | call assert_equal("mḿṁ ", tolower("mḿṁ ")) |
| 475 | call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ")) |
| 476 | call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ")) |
| 477 | call assert_equal("pṕṗ", tolower("pṕṗ")) |
| 478 | call assert_equal("q", tolower("q")) |
| 479 | call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ")) |
| 480 | call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ")) |
| 481 | call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ")) |
| 482 | call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ")) |
| 483 | call assert_equal("vṽ", tolower("vṽ")) |
| 484 | call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ")) |
| 485 | call assert_equal("ẋẍ", tolower("ẋẍ")) |
| 486 | call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ")) |
| 487 | call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ")) |
| 488 | |
| 489 | " According to https://twitter.com/jifa/status/625776454479970304 |
| 490 | " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase |
| 491 | " in length (2 to 3 bytes) when lowercased. So let's test them. |
| 492 | call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ")) |
Bram Moolenaar | e6640ad | 2017-12-22 21:06:56 +0100 | [diff] [blame] | 493 | |
| 494 | " This call to tolower with invalid utf8 sequence used to cause access to |
| 495 | " invalid memory. |
| 496 | call tolower("\xC0\x80\xC0") |
| 497 | call tolower("123\xC0\x80\xC0") |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 498 | endfunc |
| 499 | |
| 500 | func Test_toupper() |
| 501 | call assert_equal("", toupper("")) |
| 502 | |
| 503 | " Test with all printable ASCII characters. |
| 504 | call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~', |
| 505 | \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')) |
| 506 | |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 507 | " Test with a few lowercase diacritics. |
| 508 | call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("aàáâãäåāăąǎǟǡả")) |
| 509 | call assert_equal("BḂḆ", toupper("bḃḇ")) |
| 510 | call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč")) |
| 511 | call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ")) |
| 512 | call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ")) |
| 513 | call assert_equal("FḞ", toupper("fḟ")) |
| 514 | call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ")) |
| 515 | call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ")) |
| 516 | call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ")) |
| 517 | call assert_equal("JĴǰ", toupper("jĵǰ")) |
| 518 | call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ")) |
| 519 | call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ")) |
| 520 | call assert_equal("MḾṀ ", toupper("mḿṁ ")) |
| 521 | call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ")) |
| 522 | call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ")) |
| 523 | call assert_equal("PṔṖ", toupper("pṕṗ")) |
| 524 | call assert_equal("Q", toupper("q")) |
| 525 | call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ")) |
| 526 | call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ")) |
| 527 | call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ")) |
| 528 | call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ")) |
| 529 | call assert_equal("VṼ", toupper("vṽ")) |
| 530 | call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ")) |
| 531 | call assert_equal("ẊẌ", toupper("ẋẍ")) |
| 532 | call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ")) |
| 533 | call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ")) |
| 534 | |
| 535 | " Test that uppercase diacritics, which should remain unchanged. |
| 536 | call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ")) |
| 537 | call assert_equal("BḂḆ", toupper("BḂḆ")) |
| 538 | call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ")) |
| 539 | call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ")) |
| 540 | call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ")) |
| 541 | call assert_equal("FḞ ", toupper("FḞ ")) |
| 542 | call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ")) |
| 543 | call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ")) |
| 544 | call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ")) |
| 545 | call assert_equal("JĴ", toupper("JĴ")) |
| 546 | call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ")) |
| 547 | call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ")) |
| 548 | call assert_equal("MḾṀ", toupper("MḾṀ")) |
| 549 | call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ")) |
| 550 | call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ")) |
| 551 | call assert_equal("PṔṖ", toupper("PṔṖ")) |
| 552 | call assert_equal("Q", toupper("Q")) |
| 553 | call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ")) |
| 554 | call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ")) |
| 555 | call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ")) |
| 556 | call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ")) |
| 557 | call assert_equal("VṼ", toupper("VṼ")) |
| 558 | call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ")) |
| 559 | call assert_equal("XẊẌ", toupper("XẊẌ")) |
| 560 | call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ")) |
| 561 | call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ")) |
| 562 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 563 | call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ")) |
Bram Moolenaar | e6640ad | 2017-12-22 21:06:56 +0100 | [diff] [blame] | 564 | |
| 565 | " This call to toupper with invalid utf8 sequence used to cause access to |
| 566 | " invalid memory. |
| 567 | call toupper("\xC0\x80\xC0") |
| 568 | call toupper("123\xC0\x80\xC0") |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 569 | endfunc |
| 570 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 571 | " Tests for the mode() function |
| 572 | let current_modes = '' |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 573 | func Save_mode() |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 574 | let g:current_modes = mode(0) . '-' . mode(1) |
| 575 | return '' |
| 576 | endfunc |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 577 | |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 578 | func Test_mode() |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 579 | new |
| 580 | call append(0, ["Blue Ball Black", "Brown Band Bowl", ""]) |
| 581 | |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 582 | " Only complete from the current buffer. |
| 583 | set complete=. |
| 584 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 585 | inoremap <F2> <C-R>=Save_mode()<CR> |
| 586 | |
| 587 | normal! 3G |
| 588 | exe "normal i\<F2>\<Esc>" |
| 589 | call assert_equal('i-i', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 590 | " i_CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 591 | exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u" |
| 592 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 593 | " i_CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 594 | exe "normal iBro\<C-P>\<F2>\<Esc>u" |
| 595 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 596 | " i_CTRL-X |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 597 | exe "normal iBa\<C-X>\<F2>\<Esc>u" |
| 598 | call assert_equal('i-ix', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 599 | " i_CTRL-X CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 600 | exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u" |
| 601 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 602 | " i_CTRL-X CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 603 | exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u" |
| 604 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 605 | " i_CTRL-X CTRL-P + CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 606 | exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" |
| 607 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 608 | " i_CTRL-X CTRL-L: Multiple matches |
| 609 | exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u" |
| 610 | call assert_equal('i-ic', g:current_modes) |
| 611 | " i_CTRL-X CTRL-L: Single match |
| 612 | exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u" |
| 613 | call assert_equal('i-ic', g:current_modes) |
| 614 | " i_CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 615 | exe "normal iCom\<C-P>\<F2>\<Esc>u" |
| 616 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 617 | " i_CTRL-X CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 618 | exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u" |
| 619 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 620 | " i_CTRL-X CTRL-L: No match |
| 621 | exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u" |
| 622 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 623 | |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 624 | " R_CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 625 | exe "normal RBa\<C-P>\<F2>\<Esc>u" |
| 626 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 627 | " R_CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 628 | exe "normal RBro\<C-P>\<F2>\<Esc>u" |
| 629 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 630 | " R_CTRL-X |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 631 | exe "normal RBa\<C-X>\<F2>\<Esc>u" |
| 632 | call assert_equal('R-Rx', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 633 | " R_CTRL-X CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 634 | exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u" |
| 635 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 636 | " R_CTRL-X CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 637 | exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u" |
| 638 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 639 | " R_CTRL-X CTRL-P + CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 640 | exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" |
| 641 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 642 | " R_CTRL-X CTRL-L: Multiple matches |
| 643 | exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u" |
| 644 | call assert_equal('R-Rc', g:current_modes) |
| 645 | " R_CTRL-X CTRL-L: Single match |
| 646 | exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u" |
| 647 | call assert_equal('R-Rc', g:current_modes) |
| 648 | " R_CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 649 | exe "normal RCom\<C-P>\<F2>\<Esc>u" |
| 650 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 651 | " R_CTRL-X CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 652 | exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u" |
| 653 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 654 | " R_CTRL-X CTRL-L: No match |
| 655 | exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u" |
| 656 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 657 | |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 658 | call assert_equal('n', 0->mode()) |
| 659 | call assert_equal('n', 1->mode()) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 660 | |
Bram Moolenaar | 612cc38 | 2018-07-29 15:34:26 +0200 | [diff] [blame] | 661 | " i_CTRL-O |
| 662 | exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>" |
| 663 | call assert_equal("n-niI", g:current_modes) |
| 664 | |
| 665 | " R_CTRL-O |
| 666 | exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>" |
| 667 | call assert_equal("n-niR", g:current_modes) |
| 668 | |
| 669 | " gR_CTRL-O |
| 670 | exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>" |
| 671 | call assert_equal("n-niV", g:current_modes) |
| 672 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 673 | " How to test operator-pending mode? |
| 674 | |
| 675 | call feedkeys("v", 'xt') |
| 676 | call assert_equal('v', mode()) |
| 677 | call assert_equal('v', mode(1)) |
| 678 | call feedkeys("\<Esc>V", 'xt') |
| 679 | call assert_equal('V', mode()) |
| 680 | call assert_equal('V', mode(1)) |
| 681 | call feedkeys("\<Esc>\<C-V>", 'xt') |
| 682 | call assert_equal("\<C-V>", mode()) |
| 683 | call assert_equal("\<C-V>", mode(1)) |
| 684 | call feedkeys("\<Esc>", 'xt') |
| 685 | |
| 686 | call feedkeys("gh", 'xt') |
| 687 | call assert_equal('s', mode()) |
| 688 | call assert_equal('s', mode(1)) |
| 689 | call feedkeys("\<Esc>gH", 'xt') |
| 690 | call assert_equal('S', mode()) |
| 691 | call assert_equal('S', mode(1)) |
| 692 | call feedkeys("\<Esc>g\<C-H>", 'xt') |
| 693 | call assert_equal("\<C-S>", mode()) |
| 694 | call assert_equal("\<C-S>", mode(1)) |
| 695 | call feedkeys("\<Esc>", 'xt') |
| 696 | |
| 697 | call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt') |
| 698 | call assert_equal('c-c', g:current_modes) |
| 699 | call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt') |
| 700 | call assert_equal('c-cv', g:current_modes) |
| 701 | " How to test Ex mode? |
| 702 | |
| 703 | bwipe! |
| 704 | iunmap <F2> |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 705 | set complete& |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 706 | endfunc |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 707 | |
Bram Moolenaar | d200702 | 2019-08-27 21:56:06 +0200 | [diff] [blame] | 708 | func Test_append() |
| 709 | enew! |
| 710 | split |
| 711 | call append(0, ["foo"]) |
| 712 | split |
| 713 | only |
| 714 | undo |
| 715 | endfunc |
| 716 | |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 717 | func Test_getbufvar() |
| 718 | let bnr = bufnr('%') |
| 719 | let b:var_num = '1234' |
| 720 | let def_num = '5678' |
| 721 | call assert_equal('1234', getbufvar(bnr, 'var_num')) |
| 722 | call assert_equal('1234', getbufvar(bnr, 'var_num', def_num)) |
| 723 | |
| 724 | let bd = getbufvar(bnr, '') |
| 725 | call assert_equal('1234', bd['var_num']) |
| 726 | call assert_true(exists("bd['changedtick']")) |
| 727 | call assert_equal(2, len(bd)) |
| 728 | |
| 729 | let bd2 = getbufvar(bnr, '', def_num) |
| 730 | call assert_equal(bd, bd2) |
| 731 | |
| 732 | unlet b:var_num |
| 733 | call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num)) |
| 734 | call assert_equal('', getbufvar(bnr, 'var_num')) |
| 735 | |
| 736 | let bd = getbufvar(bnr, '') |
| 737 | call assert_equal(1, len(bd)) |
| 738 | let bd = getbufvar(bnr, '',def_num) |
| 739 | call assert_equal(1, len(bd)) |
| 740 | |
Bram Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 741 | call assert_equal('', getbufvar(9999, '')) |
| 742 | call assert_equal(def_num, getbufvar(9999, '', def_num)) |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 743 | unlet def_num |
| 744 | |
Bram Moolenaar | 507647d | 2017-02-17 16:43:49 +0100 | [diff] [blame] | 745 | call assert_equal(0, getbufvar(bnr, '&autoindent')) |
| 746 | call assert_equal(0, getbufvar(bnr, '&autoindent', 1)) |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 747 | |
| 748 | " Open new window with forced option values |
| 749 | set fileformats=unix,dos |
| 750 | new ++ff=dos ++bin ++enc=iso-8859-2 |
| 751 | call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat')) |
| 752 | call assert_equal(1, getbufvar(bufnr('%'), '&bin')) |
| 753 | call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc')) |
| 754 | close |
| 755 | |
| 756 | set fileformats& |
| 757 | endfunc |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 758 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 759 | func Test_last_buffer_nr() |
| 760 | call assert_equal(bufnr('$'), last_buffer_nr()) |
| 761 | endfunc |
| 762 | |
| 763 | func Test_stridx() |
| 764 | call assert_equal(-1, stridx('', 'l')) |
| 765 | call assert_equal(0, stridx('', '')) |
| 766 | call assert_equal(0, stridx('hello', '')) |
| 767 | call assert_equal(-1, stridx('hello', 'L')) |
| 768 | call assert_equal(2, stridx('hello', 'l', -1)) |
| 769 | call assert_equal(2, stridx('hello', 'l', 0)) |
| 770 | call assert_equal(2, stridx('hello', 'l', 1)) |
| 771 | call assert_equal(3, stridx('hello', 'l', 3)) |
| 772 | call assert_equal(-1, stridx('hello', 'l', 4)) |
| 773 | call assert_equal(-1, stridx('hello', 'l', 10)) |
| 774 | call assert_equal(2, stridx('hello', 'll')) |
| 775 | call assert_equal(-1, stridx('hello', 'hello world')) |
| 776 | endfunc |
| 777 | |
| 778 | func Test_strridx() |
| 779 | call assert_equal(-1, strridx('', 'l')) |
| 780 | call assert_equal(0, strridx('', '')) |
| 781 | call assert_equal(5, strridx('hello', '')) |
| 782 | call assert_equal(-1, strridx('hello', 'L')) |
| 783 | call assert_equal(3, strridx('hello', 'l')) |
| 784 | call assert_equal(3, strridx('hello', 'l', 10)) |
| 785 | call assert_equal(3, strridx('hello', 'l', 3)) |
| 786 | call assert_equal(2, strridx('hello', 'l', 2)) |
| 787 | call assert_equal(-1, strridx('hello', 'l', 1)) |
| 788 | call assert_equal(-1, strridx('hello', 'l', 0)) |
| 789 | call assert_equal(-1, strridx('hello', 'l', -1)) |
| 790 | call assert_equal(2, strridx('hello', 'll')) |
| 791 | call assert_equal(-1, strridx('hello', 'hello world')) |
| 792 | endfunc |
| 793 | |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 794 | func Test_match_func() |
| 795 | call assert_equal(4, match('testing', 'ing')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 796 | call assert_equal(4, 'testing'->match('ing', 2)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 797 | call assert_equal(-1, match('testing', 'ing', 5)) |
| 798 | call assert_equal(-1, match('testing', 'ing', 8)) |
| 799 | call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing')) |
| 800 | call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img')) |
| 801 | endfunc |
| 802 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 803 | func Test_matchend() |
| 804 | call assert_equal(7, matchend('testing', 'ing')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 805 | call assert_equal(7, 'testing'->matchend('ing', 2)) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 806 | call assert_equal(-1, matchend('testing', 'ing', 5)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 807 | call assert_equal(-1, matchend('testing', 'ing', 8)) |
| 808 | call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing')) |
| 809 | call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img')) |
| 810 | endfunc |
| 811 | |
| 812 | func Test_matchlist() |
| 813 | call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 814 | call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 815 | call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4)) |
| 816 | endfunc |
| 817 | |
| 818 | func Test_matchstr() |
| 819 | call assert_equal('ing', matchstr('testing', 'ing')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 820 | call assert_equal('ing', 'testing'->matchstr('ing', 2)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 821 | call assert_equal('', matchstr('testing', 'ing', 5)) |
| 822 | call assert_equal('', matchstr('testing', 'ing', 8)) |
| 823 | call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing')) |
| 824 | call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img')) |
| 825 | endfunc |
| 826 | |
| 827 | func Test_matchstrpos() |
| 828 | call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 829 | call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 830 | call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5)) |
| 831 | call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8)) |
| 832 | call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing')) |
| 833 | call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 834 | endfunc |
| 835 | |
| 836 | func Test_nextnonblank_prevnonblank() |
| 837 | new |
| 838 | insert |
| 839 | This |
| 840 | |
| 841 | |
| 842 | is |
| 843 | |
| 844 | a |
| 845 | Test |
| 846 | . |
| 847 | call assert_equal(0, nextnonblank(-1)) |
| 848 | call assert_equal(0, nextnonblank(0)) |
| 849 | call assert_equal(1, nextnonblank(1)) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 850 | call assert_equal(4, 2->nextnonblank()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 851 | call assert_equal(4, nextnonblank(3)) |
| 852 | call assert_equal(4, nextnonblank(4)) |
| 853 | call assert_equal(6, nextnonblank(5)) |
| 854 | call assert_equal(6, nextnonblank(6)) |
| 855 | call assert_equal(7, nextnonblank(7)) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 856 | call assert_equal(0, 8->nextnonblank()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 857 | |
| 858 | call assert_equal(0, prevnonblank(-1)) |
| 859 | call assert_equal(0, prevnonblank(0)) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 860 | call assert_equal(1, 1->prevnonblank()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 861 | call assert_equal(1, prevnonblank(2)) |
| 862 | call assert_equal(1, prevnonblank(3)) |
| 863 | call assert_equal(4, prevnonblank(4)) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 864 | call assert_equal(4, 5->prevnonblank()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 865 | call assert_equal(6, prevnonblank(6)) |
| 866 | call assert_equal(7, prevnonblank(7)) |
| 867 | call assert_equal(0, prevnonblank(8)) |
| 868 | bw! |
| 869 | endfunc |
| 870 | |
| 871 | func Test_byte2line_line2byte() |
| 872 | new |
Bram Moolenaar | c26f7c6 | 2018-08-20 22:53:04 +0200 | [diff] [blame] | 873 | set endofline |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 874 | call setline(1, ['a', 'bc', 'd']) |
| 875 | |
| 876 | set fileformat=unix |
| 877 | call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1], |
| 878 | \ map(range(-1, 8), 'byte2line(v:val)')) |
| 879 | call assert_equal([-1, -1, 1, 3, 6, 8, -1], |
| 880 | \ map(range(-1, 5), 'line2byte(v:val)')) |
| 881 | |
| 882 | set fileformat=mac |
| 883 | call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1], |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 884 | \ map(range(-1, 8), 'v:val->byte2line()')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 885 | call assert_equal([-1, -1, 1, 3, 6, 8, -1], |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 886 | \ map(range(-1, 5), 'v:val->line2byte()')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 887 | |
| 888 | set fileformat=dos |
| 889 | call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1], |
| 890 | \ map(range(-1, 11), 'byte2line(v:val)')) |
| 891 | call assert_equal([-1, -1, 1, 4, 8, 11, -1], |
| 892 | \ map(range(-1, 5), 'line2byte(v:val)')) |
| 893 | |
Bram Moolenaar | c26f7c6 | 2018-08-20 22:53:04 +0200 | [diff] [blame] | 894 | bw! |
| 895 | set noendofline nofixendofline |
| 896 | normal a- |
| 897 | for ff in ["unix", "mac", "dos"] |
| 898 | let &fileformat = ff |
| 899 | call assert_equal(1, line2byte(1)) |
| 900 | call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte). |
| 901 | endfor |
| 902 | |
| 903 | set endofline& fixendofline& fileformat& |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 904 | bw! |
| 905 | endfunc |
| 906 | |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 907 | func Test_byteidx() |
| 908 | let a = '.é.' " one char of two bytes |
| 909 | call assert_equal(0, byteidx(a, 0)) |
| 910 | call assert_equal(0, byteidxcomp(a, 0)) |
| 911 | call assert_equal(1, byteidx(a, 1)) |
| 912 | call assert_equal(1, byteidxcomp(a, 1)) |
| 913 | call assert_equal(3, byteidx(a, 2)) |
| 914 | call assert_equal(3, byteidxcomp(a, 2)) |
| 915 | call assert_equal(4, byteidx(a, 3)) |
| 916 | call assert_equal(4, byteidxcomp(a, 3)) |
| 917 | call assert_equal(-1, byteidx(a, 4)) |
| 918 | call assert_equal(-1, byteidxcomp(a, 4)) |
| 919 | |
| 920 | let b = '.é.' " normal e with composing char |
| 921 | call assert_equal(0, b->byteidx(0)) |
| 922 | call assert_equal(1, b->byteidx(1)) |
| 923 | call assert_equal(4, b->byteidx(2)) |
| 924 | call assert_equal(5, b->byteidx(3)) |
| 925 | call assert_equal(-1, b->byteidx(4)) |
| 926 | |
| 927 | call assert_equal(0, b->byteidxcomp(0)) |
| 928 | call assert_equal(1, b->byteidxcomp(1)) |
| 929 | call assert_equal(2, b->byteidxcomp(2)) |
| 930 | call assert_equal(4, b->byteidxcomp(3)) |
| 931 | call assert_equal(5, b->byteidxcomp(4)) |
| 932 | call assert_equal(-1, b->byteidxcomp(5)) |
| 933 | endfunc |
| 934 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 935 | func Test_count() |
| 936 | let l = ['a', 'a', 'A', 'b'] |
| 937 | call assert_equal(2, count(l, 'a')) |
| 938 | call assert_equal(1, count(l, 'A')) |
| 939 | call assert_equal(1, count(l, 'b')) |
| 940 | call assert_equal(0, count(l, 'B')) |
| 941 | |
| 942 | call assert_equal(2, count(l, 'a', 0)) |
| 943 | call assert_equal(1, count(l, 'A', 0)) |
| 944 | call assert_equal(1, count(l, 'b', 0)) |
| 945 | call assert_equal(0, count(l, 'B', 0)) |
| 946 | |
| 947 | call assert_equal(3, count(l, 'a', 1)) |
| 948 | call assert_equal(3, count(l, 'A', 1)) |
| 949 | call assert_equal(1, count(l, 'b', 1)) |
| 950 | call assert_equal(1, count(l, 'B', 1)) |
| 951 | call assert_equal(0, count(l, 'c', 1)) |
| 952 | |
| 953 | call assert_equal(1, count(l, 'a', 0, 1)) |
| 954 | call assert_equal(2, count(l, 'a', 1, 1)) |
| 955 | call assert_fails('call count(l, "a", 0, 10)', 'E684:') |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 956 | call assert_fails('call count(l, "a", [])', 'E745:') |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 957 | |
| 958 | let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'} |
| 959 | call assert_equal(2, count(d, 'a')) |
| 960 | call assert_equal(1, count(d, 'A')) |
| 961 | call assert_equal(1, count(d, 'b')) |
| 962 | call assert_equal(0, count(d, 'B')) |
| 963 | |
| 964 | call assert_equal(2, count(d, 'a', 0)) |
| 965 | call assert_equal(1, count(d, 'A', 0)) |
| 966 | call assert_equal(1, count(d, 'b', 0)) |
| 967 | call assert_equal(0, count(d, 'B', 0)) |
| 968 | |
| 969 | call assert_equal(3, count(d, 'a', 1)) |
| 970 | call assert_equal(3, count(d, 'A', 1)) |
| 971 | call assert_equal(1, count(d, 'b', 1)) |
| 972 | call assert_equal(1, count(d, 'B', 1)) |
| 973 | call assert_equal(0, count(d, 'c', 1)) |
| 974 | |
| 975 | call assert_fails('call count(d, "a", 0, 1)', 'E474:') |
Bram Moolenaar | 9966b21 | 2017-07-28 16:46:57 +0200 | [diff] [blame] | 976 | |
| 977 | call assert_equal(0, count("foo", "bar")) |
| 978 | call assert_equal(1, count("foo", "oo")) |
| 979 | call assert_equal(2, count("foo", "o")) |
| 980 | call assert_equal(0, count("foo", "O")) |
| 981 | call assert_equal(2, count("foo", "O", 1)) |
| 982 | call assert_equal(2, count("fooooo", "oo")) |
Bram Moolenaar | 338e47f | 2017-12-19 11:55:26 +0100 | [diff] [blame] | 983 | call assert_equal(0, count("foo", "")) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 984 | |
| 985 | call assert_fails('call count(0, 0)', 'E712:') |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 986 | endfunc |
| 987 | |
| 988 | func Test_changenr() |
| 989 | new Xchangenr |
| 990 | call assert_equal(0, changenr()) |
| 991 | norm ifoo |
| 992 | call assert_equal(1, changenr()) |
| 993 | set undolevels=10 |
| 994 | norm Sbar |
| 995 | call assert_equal(2, changenr()) |
| 996 | undo |
| 997 | call assert_equal(1, changenr()) |
| 998 | redo |
| 999 | call assert_equal(2, changenr()) |
| 1000 | bw! |
| 1001 | set undolevels& |
| 1002 | endfunc |
| 1003 | |
| 1004 | func Test_filewritable() |
| 1005 | new Xfilewritable |
| 1006 | write! |
| 1007 | call assert_equal(1, filewritable('Xfilewritable')) |
| 1008 | |
| 1009 | call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----')) |
| 1010 | call assert_equal(0, filewritable('Xfilewritable')) |
| 1011 | |
| 1012 | call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----')) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1013 | call assert_equal(1, 'Xfilewritable'->filewritable()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1014 | |
| 1015 | call assert_equal(0, filewritable('doesnotexist')) |
| 1016 | |
| 1017 | call delete('Xfilewritable') |
| 1018 | bw! |
| 1019 | endfunc |
| 1020 | |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 1021 | func Test_Executable() |
| 1022 | if has('win32') |
| 1023 | call assert_equal(1, executable('notepad')) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1024 | call assert_equal(1, 'notepad.exe'->executable()) |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 1025 | call assert_equal(0, executable('notepad.exe.exe')) |
| 1026 | call assert_equal(0, executable('shell32.dll')) |
| 1027 | call assert_equal(0, executable('win.ini')) |
| 1028 | elseif has('unix') |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1029 | call assert_equal(1, 'cat'->executable()) |
Bram Moolenaar | a05a0d3 | 2018-10-07 18:43:05 +0200 | [diff] [blame] | 1030 | call assert_equal(0, executable('nodogshere')) |
Bram Moolenaar | d08b8c4 | 2019-07-24 14:59:45 +0200 | [diff] [blame] | 1031 | |
| 1032 | " get "cat" path and remove the leading / |
| 1033 | let catcmd = exepath('cat')[1:] |
| 1034 | new |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1035 | " check that the relative path works in / |
Bram Moolenaar | d08b8c4 | 2019-07-24 14:59:45 +0200 | [diff] [blame] | 1036 | lcd / |
| 1037 | call assert_equal(1, executable(catcmd)) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1038 | call assert_equal('/' .. catcmd, catcmd->exepath()) |
Bram Moolenaar | d08b8c4 | 2019-07-24 14:59:45 +0200 | [diff] [blame] | 1039 | bwipe |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 1040 | endif |
| 1041 | endfunc |
| 1042 | |
Bram Moolenaar | 8662189 | 2019-03-30 21:51:28 +0100 | [diff] [blame] | 1043 | func Test_executable_longname() |
| 1044 | if !has('win32') |
| 1045 | return |
| 1046 | endif |
| 1047 | |
| 1048 | let fname = 'X' . repeat('あ', 200) . '.bat' |
| 1049 | call writefile([], fname) |
| 1050 | call assert_equal(1, executable(fname)) |
| 1051 | call delete(fname) |
| 1052 | endfunc |
| 1053 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1054 | func Test_hostname() |
| 1055 | let hostname_vim = hostname() |
| 1056 | if has('unix') |
| 1057 | let hostname_system = systemlist('uname -n')[0] |
| 1058 | call assert_equal(hostname_vim, hostname_system) |
| 1059 | endif |
| 1060 | endfunc |
| 1061 | |
| 1062 | func Test_getpid() |
| 1063 | " getpid() always returns the same value within a vim instance. |
| 1064 | call assert_equal(getpid(), getpid()) |
| 1065 | if has('unix') |
| 1066 | call assert_equal(systemlist('echo $PPID')[0], string(getpid())) |
| 1067 | endif |
| 1068 | endfunc |
| 1069 | |
| 1070 | func Test_hlexists() |
| 1071 | call assert_equal(0, hlexists('does_not_exist')) |
Bram Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 1072 | call assert_equal(0, 'Number'->hlexists()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1073 | call assert_equal(0, highlight_exists('does_not_exist')) |
| 1074 | call assert_equal(0, highlight_exists('Number')) |
| 1075 | syntax on |
| 1076 | call assert_equal(0, hlexists('does_not_exist')) |
| 1077 | call assert_equal(1, hlexists('Number')) |
| 1078 | call assert_equal(0, highlight_exists('does_not_exist')) |
| 1079 | call assert_equal(1, highlight_exists('Number')) |
| 1080 | syntax off |
| 1081 | endfunc |
| 1082 | |
| 1083 | func Test_col() |
| 1084 | new |
| 1085 | call setline(1, 'abcdef') |
| 1086 | norm gg4|mx6|mY2| |
| 1087 | call assert_equal(2, col('.')) |
| 1088 | call assert_equal(7, col('$')) |
| 1089 | call assert_equal(4, col("'x")) |
| 1090 | call assert_equal(6, col("'Y")) |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 1091 | call assert_equal(2, [1, 2]->col()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1092 | call assert_equal(7, col([1, '$'])) |
| 1093 | |
| 1094 | call assert_equal(0, col('')) |
| 1095 | call assert_equal(0, col('x')) |
| 1096 | call assert_equal(0, col([2, '$'])) |
| 1097 | call assert_equal(0, col([1, 100])) |
| 1098 | call assert_equal(0, col([1])) |
| 1099 | bw! |
| 1100 | endfunc |
| 1101 | |
Bram Moolenaar | 947b39e | 2018-07-22 19:36:37 +0200 | [diff] [blame] | 1102 | func Test_inputlist() |
| 1103 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx') |
| 1104 | call assert_equal(1, c) |
Bram Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 1105 | call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx') |
Bram Moolenaar | 947b39e | 2018-07-22 19:36:37 +0200 | [diff] [blame] | 1106 | call assert_equal(2, c) |
| 1107 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx') |
| 1108 | call assert_equal(3, c) |
| 1109 | |
| 1110 | call assert_fails('call inputlist("")', 'E686:') |
| 1111 | endfunc |
| 1112 | |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 1113 | func Test_balloon_show() |
Bram Moolenaar | a0107bd | 2017-03-02 22:48:01 +0100 | [diff] [blame] | 1114 | if has('balloon_eval') |
| 1115 | " This won't do anything but must not crash either. |
| 1116 | call balloon_show('hi!') |
| 1117 | endif |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 1118 | endfunc |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 1119 | |
| 1120 | func Test_setbufvar_options() |
| 1121 | " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the |
| 1122 | " window layout. |
| 1123 | call assert_equal(1, winnr('$')) |
| 1124 | split dummy_preview |
| 1125 | resize 2 |
| 1126 | set winfixheight winfixwidth |
| 1127 | let prev_id = win_getid() |
| 1128 | |
| 1129 | wincmd j |
| 1130 | let wh = winheight('.') |
| 1131 | let dummy_buf = bufnr('dummy_buf1', v:true) |
| 1132 | call setbufvar(dummy_buf, '&buftype', 'nofile') |
| 1133 | execute 'belowright vertical split #' . dummy_buf |
| 1134 | call assert_equal(wh, winheight('.')) |
| 1135 | let dum1_id = win_getid() |
| 1136 | |
| 1137 | wincmd h |
| 1138 | let wh = winheight('.') |
| 1139 | let dummy_buf = bufnr('dummy_buf2', v:true) |
| 1140 | call setbufvar(dummy_buf, '&buftype', 'nofile') |
| 1141 | execute 'belowright vertical split #' . dummy_buf |
| 1142 | call assert_equal(wh, winheight('.')) |
| 1143 | |
| 1144 | bwipe! |
| 1145 | call win_gotoid(prev_id) |
| 1146 | bwipe! |
| 1147 | call win_gotoid(dum1_id) |
| 1148 | bwipe! |
| 1149 | endfunc |
Bram Moolenaar | d4863aa | 2017-04-07 19:50:12 +0200 | [diff] [blame] | 1150 | |
| 1151 | func Test_redo_in_nested_functions() |
| 1152 | nnoremap g. :set opfunc=Operator<CR>g@ |
| 1153 | function Operator( type, ... ) |
| 1154 | let @x = 'XXX' |
| 1155 | execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp' |
| 1156 | endfunction |
| 1157 | |
| 1158 | function! Apply() |
| 1159 | 5,6normal! . |
| 1160 | endfunction |
| 1161 | |
| 1162 | new |
| 1163 | call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3)) |
| 1164 | 1normal g.i" |
| 1165 | call assert_equal('some "XXX" text', getline(1)) |
| 1166 | 3,4normal . |
| 1167 | call assert_equal('some "XXX" text', getline(3)) |
| 1168 | call assert_equal('more "XXX" text', getline(4)) |
| 1169 | call Apply() |
| 1170 | call assert_equal('some "XXX" text', getline(5)) |
| 1171 | call assert_equal('more "XXX" text', getline(6)) |
| 1172 | bwipe! |
| 1173 | |
| 1174 | nunmap g. |
| 1175 | delfunc Operator |
| 1176 | delfunc Apply |
| 1177 | endfunc |
Bram Moolenaar | 2061552 | 2017-06-05 18:46:26 +0200 | [diff] [blame] | 1178 | |
| 1179 | func Test_shellescape() |
| 1180 | let save_shell = &shell |
| 1181 | set shell=bash |
| 1182 | call assert_equal("'text'", shellescape('text')) |
| 1183 | call assert_equal("'te\"xt'", shellescape('te"xt')) |
| 1184 | call assert_equal("'te'\\''xt'", shellescape("te'xt")) |
| 1185 | |
| 1186 | call assert_equal("'te%xt'", shellescape("te%xt")) |
| 1187 | call assert_equal("'te\\%xt'", shellescape("te%xt", 1)) |
| 1188 | call assert_equal("'te#xt'", shellescape("te#xt")) |
| 1189 | call assert_equal("'te\\#xt'", shellescape("te#xt", 1)) |
| 1190 | call assert_equal("'te!xt'", shellescape("te!xt")) |
| 1191 | call assert_equal("'te\\!xt'", shellescape("te!xt", 1)) |
| 1192 | |
| 1193 | call assert_equal("'te\nxt'", shellescape("te\nxt")) |
| 1194 | call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1)) |
| 1195 | set shell=tcsh |
| 1196 | call assert_equal("'te\\!xt'", shellescape("te!xt")) |
| 1197 | call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1)) |
| 1198 | call assert_equal("'te\\\nxt'", shellescape("te\nxt")) |
| 1199 | call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1)) |
| 1200 | |
| 1201 | let &shell = save_shell |
| 1202 | endfunc |
Bram Moolenaar | 295ac5a | 2018-03-22 23:04:02 +0100 | [diff] [blame] | 1203 | |
| 1204 | func Test_trim() |
| 1205 | call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B")) |
| 1206 | call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B")) |
| 1207 | call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t")) |
| 1208 | call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww")) |
| 1209 | call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail")) |
| 1210 | call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " ")) |
| 1211 | call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx")) |
| 1212 | call assert_equal("RESERVE", trim("你RESERVE好", "你好")) |
| 1213 | call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好")) |
| 1214 | call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", )) |
| 1215 | call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好")) |
| 1216 | call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes")) |
| 1217 | call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses")) |
| 1218 | call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要")) |
| 1219 | call assert_equal("", trim("", "")) |
| 1220 | call assert_equal("a", trim("a", "")) |
| 1221 | call assert_equal("", trim("", "a")) |
| 1222 | |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 1223 | let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '') |
Bram Moolenaar | 295ac5a | 2018-03-22 23:04:02 +0100 | [diff] [blame] | 1224 | call assert_equal("x", trim(chars . "x" . chars)) |
| 1225 | endfunc |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 1226 | |
| 1227 | " Test for reg_recording() and reg_executing() |
| 1228 | func Test_reg_executing_and_recording() |
| 1229 | let s:reg_stat = '' |
| 1230 | func s:save_reg_stat() |
| 1231 | let s:reg_stat = reg_recording() . ':' . reg_executing() |
| 1232 | return '' |
| 1233 | endfunc |
| 1234 | |
| 1235 | new |
| 1236 | call s:save_reg_stat() |
| 1237 | call assert_equal(':', s:reg_stat) |
| 1238 | call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt') |
| 1239 | call assert_equal('a:', s:reg_stat) |
| 1240 | call feedkeys("@a", 'xt') |
| 1241 | call assert_equal(':a', s:reg_stat) |
| 1242 | call feedkeys("qb@aq", 'xt') |
| 1243 | call assert_equal('b:a', s:reg_stat) |
| 1244 | call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt') |
| 1245 | call assert_equal('":', s:reg_stat) |
| 1246 | |
Bram Moolenaar | cce713d | 2019-03-04 11:40:12 +0100 | [diff] [blame] | 1247 | " :normal command saves and restores reg_executing |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1248 | let s:reg_stat = '' |
Bram Moolenaar | cce713d | 2019-03-04 11:40:12 +0100 | [diff] [blame] | 1249 | let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>" |
| 1250 | func TestFunc() abort |
| 1251 | normal! ia |
| 1252 | endfunc |
| 1253 | call feedkeys("@q", 'xt') |
| 1254 | call assert_equal(':q', s:reg_stat) |
| 1255 | delfunc TestFunc |
| 1256 | |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1257 | " getchar() command saves and restores reg_executing |
| 1258 | map W :call TestFunc()<CR> |
| 1259 | let @q = "W" |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 1260 | let g:typed = '' |
| 1261 | let g:regs = [] |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1262 | func TestFunc() abort |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 1263 | let g:regs += [reg_executing()] |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1264 | let g:typed = getchar(0) |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 1265 | let g:regs += [reg_executing()] |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1266 | endfunc |
| 1267 | call feedkeys("@qy", 'xt') |
| 1268 | call assert_equal(char2nr("y"), g:typed) |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 1269 | call assert_equal(['q', 'q'], g:regs) |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1270 | delfunc TestFunc |
| 1271 | unmap W |
| 1272 | unlet g:typed |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 1273 | unlet g:regs |
| 1274 | |
| 1275 | " input() command saves and restores reg_executing |
| 1276 | map W :call TestFunc()<CR> |
| 1277 | let @q = "W" |
| 1278 | let g:typed = '' |
| 1279 | let g:regs = [] |
| 1280 | func TestFunc() abort |
| 1281 | let g:regs += [reg_executing()] |
Bram Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 1282 | let g:typed = '?'->input() |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 1283 | let g:regs += [reg_executing()] |
| 1284 | endfunc |
| 1285 | call feedkeys("@qy\<CR>", 'xt') |
| 1286 | call assert_equal("y", g:typed) |
| 1287 | call assert_equal(['q', 'q'], g:regs) |
| 1288 | delfunc TestFunc |
| 1289 | unmap W |
| 1290 | unlet g:typed |
| 1291 | unlet g:regs |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1292 | |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 1293 | bwipe! |
| 1294 | delfunc s:save_reg_stat |
| 1295 | unlet s:reg_stat |
| 1296 | endfunc |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1297 | |
Bram Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 1298 | func Test_inputsecret() |
| 1299 | map W :call TestFunc()<CR> |
| 1300 | let @q = "W" |
| 1301 | let g:typed1 = '' |
| 1302 | let g:typed2 = '' |
| 1303 | let g:regs = [] |
| 1304 | func TestFunc() abort |
| 1305 | let g:typed1 = '?'->inputsecret() |
| 1306 | let g:typed2 = inputsecret('password: ') |
| 1307 | endfunc |
| 1308 | call feedkeys("@qsomething\<CR>else\<CR>", 'xt') |
| 1309 | call assert_equal("something", g:typed1) |
| 1310 | call assert_equal("else", g:typed2) |
| 1311 | delfunc TestFunc |
| 1312 | unmap W |
| 1313 | unlet g:typed1 |
| 1314 | unlet g:typed2 |
| 1315 | endfunc |
| 1316 | |
Bram Moolenaar | 5d712e4 | 2019-09-03 23:37:01 +0200 | [diff] [blame] | 1317 | func Test_getchar() |
| 1318 | call feedkeys('a', '') |
| 1319 | call assert_equal(char2nr('a'), getchar()) |
| 1320 | |
| 1321 | call test_setmouse(1, 3) |
| 1322 | let v:mouse_win = 9 |
| 1323 | let v:mouse_winid = 9 |
| 1324 | let v:mouse_lnum = 9 |
| 1325 | let v:mouse_col = 9 |
| 1326 | call feedkeys("\<S-LeftMouse>", '') |
| 1327 | call assert_equal("\<S-LeftMouse>", getchar()) |
| 1328 | call assert_equal(1, v:mouse_win) |
| 1329 | call assert_equal(win_getid(1), v:mouse_winid) |
| 1330 | call assert_equal(1, v:mouse_lnum) |
| 1331 | call assert_equal(3, v:mouse_col) |
| 1332 | endfunc |
| 1333 | |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1334 | func Test_libcall_libcallnr() |
| 1335 | if !has('libcall') |
| 1336 | return |
| 1337 | endif |
| 1338 | |
| 1339 | if has('win32') |
| 1340 | let libc = 'msvcrt.dll' |
| 1341 | elseif has('mac') |
| 1342 | let libc = 'libSystem.B.dylib' |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 1343 | elseif executable('ldd') |
| 1344 | let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>') |
| 1345 | endif |
| 1346 | if get(l:, 'libc', '') ==# '' |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1347 | " On Unix, libc.so can be in various places. |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 1348 | if has('linux') |
| 1349 | " There is not documented but regarding the 1st argument of glibc's |
| 1350 | " dlopen an empty string and nullptr are equivalent, so using an empty |
| 1351 | " string for the 1st argument of libcall allows to call functions. |
| 1352 | let libc = '' |
| 1353 | elseif has('sun') |
| 1354 | " Set the path to libc.so according to the architecture. |
| 1355 | let test_bits = system('file ' . GetVimProg()) |
| 1356 | let test_arch = system('uname -p') |
| 1357 | if test_bits =~ '64-bit' && test_arch =~ 'sparc' |
| 1358 | let libc = '/usr/lib/sparcv9/libc.so' |
| 1359 | elseif test_bits =~ '64-bit' && test_arch =~ 'i386' |
| 1360 | let libc = '/usr/lib/amd64/libc.so' |
| 1361 | else |
| 1362 | let libc = '/usr/lib/libc.so' |
| 1363 | endif |
| 1364 | else |
| 1365 | " Unfortunately skip this test until a good way is found. |
| 1366 | return |
| 1367 | endif |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1368 | endif |
| 1369 | |
| 1370 | if has('win32') |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 1371 | call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv')) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1372 | else |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 1373 | call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv')) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1374 | endif |
| 1375 | |
| 1376 | " If function returns NULL, libcall() should return an empty string. |
| 1377 | call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT')) |
| 1378 | |
| 1379 | " Test libcallnr() with string and integer argument. |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 1380 | call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen')) |
| 1381 | call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper')) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1382 | |
| 1383 | call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:') |
| 1384 | call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:') |
| 1385 | |
| 1386 | call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:') |
| 1387 | call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:') |
| 1388 | endfunc |
Bram Moolenaar | d90a144 | 2018-07-15 20:24:31 +0200 | [diff] [blame] | 1389 | |
| 1390 | sandbox function Fsandbox() |
| 1391 | normal ix |
| 1392 | endfunc |
| 1393 | |
| 1394 | func Test_func_sandbox() |
| 1395 | sandbox let F = {-> 'hello'} |
| 1396 | call assert_equal('hello', F()) |
| 1397 | |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1398 | sandbox let F = {-> "normal ix\<Esc>"->execute()} |
Bram Moolenaar | d90a144 | 2018-07-15 20:24:31 +0200 | [diff] [blame] | 1399 | call assert_fails('call F()', 'E48:') |
| 1400 | unlet F |
| 1401 | |
| 1402 | call assert_fails('call Fsandbox()', 'E48:') |
| 1403 | delfunc Fsandbox |
| 1404 | endfunc |
Bram Moolenaar | 9e353b5 | 2018-11-04 23:39:38 +0100 | [diff] [blame] | 1405 | |
| 1406 | func EditAnotherFile() |
| 1407 | let word = expand('<cword>') |
| 1408 | edit Xfuncrange2 |
| 1409 | endfunc |
| 1410 | |
| 1411 | func Test_func_range_with_edit() |
| 1412 | " Define a function that edits another buffer, then call it with a range that |
| 1413 | " is invalid in that buffer. |
| 1414 | call writefile(['just one line'], 'Xfuncrange2') |
| 1415 | new |
| 1416 | call setline(1, range(10)) |
| 1417 | write Xfuncrange1 |
| 1418 | call assert_fails('5,8call EditAnotherFile()', 'E16:') |
| 1419 | |
| 1420 | call delete('Xfuncrange1') |
| 1421 | call delete('Xfuncrange2') |
| 1422 | bwipe! |
| 1423 | endfunc |
Bram Moolenaar | ded5f1b | 2018-11-10 17:33:29 +0100 | [diff] [blame] | 1424 | |
| 1425 | func Test_func_exists_on_reload() |
| 1426 | call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists') |
| 1427 | call assert_equal(0, exists('*ExistingFunction')) |
| 1428 | source Xfuncexists |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1429 | call assert_equal(1, '*ExistingFunction'->exists()) |
Bram Moolenaar | ded5f1b | 2018-11-10 17:33:29 +0100 | [diff] [blame] | 1430 | " Redefining a function when reloading a script is OK. |
| 1431 | source Xfuncexists |
| 1432 | call assert_equal(1, exists('*ExistingFunction')) |
| 1433 | |
| 1434 | " But redefining in another script is not OK. |
| 1435 | call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2') |
| 1436 | call assert_fails('source Xfuncexists2', 'E122:') |
| 1437 | |
| 1438 | delfunc ExistingFunction |
| 1439 | call assert_equal(0, exists('*ExistingFunction')) |
| 1440 | call writefile([ |
| 1441 | \ 'func ExistingFunction()', 'echo "yes"', 'endfunc', |
| 1442 | \ 'func ExistingFunction()', 'echo "no"', 'endfunc', |
| 1443 | \ ], 'Xfuncexists') |
| 1444 | call assert_fails('source Xfuncexists', 'E122:') |
| 1445 | call assert_equal(1, exists('*ExistingFunction')) |
| 1446 | |
| 1447 | call delete('Xfuncexists2') |
| 1448 | call delete('Xfuncexists') |
| 1449 | delfunc ExistingFunction |
| 1450 | endfunc |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 1451 | |
| 1452 | " Test confirm({msg} [, {choices} [, {default} [, {type}]]]) |
| 1453 | func Test_confirm() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 1454 | CheckUnix |
| 1455 | CheckNotGui |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 1456 | |
| 1457 | call feedkeys('o', 'L') |
| 1458 | let a = confirm('Press O to proceed') |
| 1459 | call assert_equal(1, a) |
| 1460 | |
| 1461 | call feedkeys('y', 'L') |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 1462 | let a = 'Are you sure?'->confirm("&Yes\n&No") |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 1463 | call assert_equal(1, a) |
| 1464 | |
| 1465 | call feedkeys('n', 'L') |
| 1466 | let a = confirm('Are you sure?', "&Yes\n&No") |
| 1467 | call assert_equal(2, a) |
| 1468 | |
| 1469 | " confirm() should return 0 when pressing CTRL-C. |
| 1470 | call feedkeys("\<C-c>", 'L') |
| 1471 | let a = confirm('Are you sure?', "&Yes\n&No") |
| 1472 | call assert_equal(0, a) |
| 1473 | |
| 1474 | " <Esc> requires another character to avoid it being seen as the start of an |
| 1475 | " escape sequence. Zero should be harmless. |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1476 | eval "\<Esc>0"->feedkeys('L') |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 1477 | let a = confirm('Are you sure?', "&Yes\n&No") |
| 1478 | call assert_equal(0, a) |
| 1479 | |
| 1480 | " Default choice is returned when pressing <CR>. |
| 1481 | call feedkeys("\<CR>", 'L') |
| 1482 | let a = confirm('Are you sure?', "&Yes\n&No") |
| 1483 | call assert_equal(1, a) |
| 1484 | |
| 1485 | call feedkeys("\<CR>", 'L') |
| 1486 | let a = confirm('Are you sure?', "&Yes\n&No", 2) |
| 1487 | call assert_equal(2, a) |
| 1488 | |
| 1489 | call feedkeys("\<CR>", 'L') |
| 1490 | let a = confirm('Are you sure?', "&Yes\n&No", 0) |
| 1491 | call assert_equal(0, a) |
| 1492 | |
| 1493 | " Test with the {type} 4th argument |
| 1494 | for type in ['Error', 'Question', 'Info', 'Warning', 'Generic'] |
| 1495 | call feedkeys('y', 'L') |
| 1496 | let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type) |
| 1497 | call assert_equal(1, a) |
| 1498 | endfor |
| 1499 | |
| 1500 | call assert_fails('call confirm([])', 'E730:') |
| 1501 | call assert_fails('call confirm("Are you sure?", [])', 'E730:') |
| 1502 | call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:') |
| 1503 | call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:') |
| 1504 | endfunc |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 1505 | |
| 1506 | func Test_platform_name() |
| 1507 | " The system matches at most only one name. |
| 1508 | let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix'] |
| 1509 | call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)'))) |
| 1510 | |
| 1511 | " Is Unix? |
| 1512 | call assert_equal(has('beos'), has('beos') && has('unix')) |
| 1513 | call assert_equal(has('bsd'), has('bsd') && has('unix')) |
| 1514 | call assert_equal(has('hpux'), has('hpux') && has('unix')) |
| 1515 | call assert_equal(has('linux'), has('linux') && has('unix')) |
| 1516 | call assert_equal(has('mac'), has('mac') && has('unix')) |
| 1517 | call assert_equal(has('qnx'), has('qnx') && has('unix')) |
| 1518 | call assert_equal(has('sun'), has('sun') && has('unix')) |
| 1519 | call assert_equal(has('win32'), has('win32') && !has('unix')) |
| 1520 | call assert_equal(has('win32unix'), has('win32unix') && has('unix')) |
| 1521 | |
| 1522 | if has('unix') && executable('uname') |
| 1523 | let uname = system('uname') |
| 1524 | call assert_equal(uname =~? 'BeOS', has('beos')) |
Bram Moolenaar | a02e3f6 | 2019-02-07 21:27:14 +0100 | [diff] [blame] | 1525 | " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined |
| 1526 | call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd')) |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 1527 | call assert_equal(uname =~? 'HP-UX', has('hpux')) |
| 1528 | call assert_equal(uname =~? 'Linux', has('linux')) |
| 1529 | call assert_equal(uname =~? 'Darwin', has('mac')) |
| 1530 | call assert_equal(uname =~? 'QNX', has('qnx')) |
| 1531 | call assert_equal(uname =~? 'SunOS', has('sun')) |
| 1532 | call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix')) |
| 1533 | endif |
| 1534 | endfunc |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 1535 | |
| 1536 | func Test_readdir() |
| 1537 | call mkdir('Xdir') |
| 1538 | call writefile([], 'Xdir/foo.txt') |
| 1539 | call writefile([], 'Xdir/bar.txt') |
| 1540 | call mkdir('Xdir/dir') |
| 1541 | |
| 1542 | " All results |
| 1543 | let files = readdir('Xdir') |
| 1544 | call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files)) |
| 1545 | |
| 1546 | " Only results containing "f" |
| 1547 | let files = readdir('Xdir', { x -> stridx(x, 'f') !=- 1 }) |
| 1548 | call assert_equal(['foo.txt'], sort(files)) |
| 1549 | |
| 1550 | " Only .txt files |
| 1551 | let files = readdir('Xdir', { x -> x =~ '.txt$' }) |
| 1552 | call assert_equal(['bar.txt', 'foo.txt'], sort(files)) |
| 1553 | |
| 1554 | " Only .txt files with string |
| 1555 | let files = readdir('Xdir', 'v:val =~ ".txt$"') |
| 1556 | call assert_equal(['bar.txt', 'foo.txt'], sort(files)) |
| 1557 | |
| 1558 | " Limit to 1 result. |
| 1559 | let l = [] |
| 1560 | let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1}) |
| 1561 | call assert_equal(1, len(files)) |
| 1562 | |
Bram Moolenaar | 27da7de | 2019-09-03 17:13:37 +0200 | [diff] [blame] | 1563 | " Nested readdir() must not crash |
| 1564 | let files = readdir('Xdir', 'readdir("Xdir", "1") != []') |
| 1565 | call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt']) |
| 1566 | |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 1567 | eval 'Xdir'->delete('rf') |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 1568 | endfunc |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 1569 | |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 1570 | func Test_delete_rf() |
| 1571 | call mkdir('Xdir') |
| 1572 | call writefile([], 'Xdir/foo.txt') |
| 1573 | call writefile([], 'Xdir/bar.txt') |
| 1574 | call mkdir('Xdir/[a-1]') " issue #696 |
| 1575 | call writefile([], 'Xdir/[a-1]/foo.txt') |
| 1576 | call writefile([], 'Xdir/[a-1]/bar.txt') |
| 1577 | call assert_true(filereadable('Xdir/foo.txt')) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1578 | call assert_true('Xdir/[a-1]/foo.txt'->filereadable()) |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 1579 | |
| 1580 | call assert_equal(0, delete('Xdir', 'rf')) |
| 1581 | call assert_false(filereadable('Xdir/foo.txt')) |
| 1582 | call assert_false(filereadable('Xdir/[a-1]/foo.txt')) |
| 1583 | endfunc |
| 1584 | |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 1585 | func Test_call() |
| 1586 | call assert_equal(3, call('len', [123])) |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1587 | call assert_equal(3, 'len'->call([123])) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 1588 | call assert_fails("call call('len', 123)", 'E714:') |
| 1589 | call assert_equal(0, call('', [])) |
| 1590 | |
| 1591 | function Mylen() dict |
| 1592 | return len(self.data) |
| 1593 | endfunction |
| 1594 | let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")} |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1595 | eval mydict.len->call([], mydict)->assert_equal(4) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 1596 | call assert_fails("call call('Mylen', [], 0)", 'E715:') |
| 1597 | endfunc |
| 1598 | |
| 1599 | func Test_char2nr() |
| 1600 | call assert_equal(12354, char2nr('あ', 1)) |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 1601 | call assert_equal(120, 'x'->char2nr()) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 1602 | endfunc |
| 1603 | |
| 1604 | func Test_eventhandler() |
| 1605 | call assert_equal(0, eventhandler()) |
| 1606 | endfunc |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 1607 | |
| 1608 | func Test_bufadd_bufload() |
| 1609 | call assert_equal(0, bufexists('someName')) |
| 1610 | let buf = bufadd('someName') |
| 1611 | call assert_notequal(0, buf) |
| 1612 | call assert_equal(1, bufexists('someName')) |
| 1613 | call assert_equal(0, getbufvar(buf, '&buflisted')) |
| 1614 | call assert_equal(0, bufloaded(buf)) |
| 1615 | call bufload(buf) |
| 1616 | call assert_equal(1, bufloaded(buf)) |
| 1617 | call assert_equal([''], getbufline(buf, 1, '$')) |
| 1618 | |
| 1619 | let curbuf = bufnr('') |
Bram Moolenaar | 3940ec6 | 2019-07-05 21:53:24 +0200 | [diff] [blame] | 1620 | call writefile(['some', 'text'], 'XotherName') |
Bram Moolenaar | 073e4b9 | 2019-08-18 23:01:56 +0200 | [diff] [blame] | 1621 | let buf = 'XotherName'->bufadd() |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 1622 | call assert_notequal(0, buf) |
Bram Moolenaar | 073e4b9 | 2019-08-18 23:01:56 +0200 | [diff] [blame] | 1623 | eval 'XotherName'->bufexists()->assert_equal(1) |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 1624 | call assert_equal(0, getbufvar(buf, '&buflisted')) |
| 1625 | call assert_equal(0, bufloaded(buf)) |
Bram Moolenaar | 073e4b9 | 2019-08-18 23:01:56 +0200 | [diff] [blame] | 1626 | eval buf->bufload() |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 1627 | call assert_equal(1, bufloaded(buf)) |
| 1628 | call assert_equal(['some', 'text'], getbufline(buf, 1, '$')) |
| 1629 | call assert_equal(curbuf, bufnr('')) |
| 1630 | |
Bram Moolenaar | 892ae72 | 2019-06-30 20:33:01 +0200 | [diff] [blame] | 1631 | let buf1 = bufadd('') |
| 1632 | let buf2 = bufadd('') |
| 1633 | call assert_notequal(0, buf1) |
| 1634 | call assert_notequal(0, buf2) |
| 1635 | call assert_notequal(buf1, buf2) |
| 1636 | call assert_equal(1, bufexists(buf1)) |
| 1637 | call assert_equal(1, bufexists(buf2)) |
| 1638 | call assert_equal(0, bufloaded(buf1)) |
| 1639 | exe 'bwipe ' .. buf1 |
| 1640 | call assert_equal(0, bufexists(buf1)) |
| 1641 | call assert_equal(1, bufexists(buf2)) |
| 1642 | exe 'bwipe ' .. buf2 |
| 1643 | call assert_equal(0, bufexists(buf2)) |
| 1644 | |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 1645 | bwipe someName |
Bram Moolenaar | 3940ec6 | 2019-07-05 21:53:24 +0200 | [diff] [blame] | 1646 | bwipe XotherName |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 1647 | call assert_equal(0, bufexists('someName')) |
Bram Moolenaar | 3940ec6 | 2019-07-05 21:53:24 +0200 | [diff] [blame] | 1648 | call delete('XotherName') |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 1649 | endfunc |