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