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