Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 1 | " Tests for various functions. |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 2 | |
Bram Moolenaar | f1c118b | 2018-09-03 22:08:10 +0200 | [diff] [blame] | 3 | source shared.vim |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 4 | source check.vim |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 5 | source term_util.vim |
Bram Moolenaar | 4132eb5 | 2020-02-14 16:53:00 +0100 | [diff] [blame] | 6 | source screendump.vim |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 7 | import './vim9.vim' as v9 |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 8 | |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 9 | " Must be done first, since the alternate buffer must be unset. |
| 10 | func Test_00_bufexists() |
| 11 | call assert_equal(0, bufexists('does_not_exist')) |
| 12 | call assert_equal(1, bufexists(bufnr('%'))) |
| 13 | call assert_equal(0, bufexists(0)) |
| 14 | new Xfoo |
| 15 | let bn = bufnr('%') |
| 16 | call assert_equal(1, bufexists(bn)) |
| 17 | call assert_equal(1, bufexists('Xfoo')) |
| 18 | call assert_equal(1, bufexists(getcwd() . '/Xfoo')) |
| 19 | call assert_equal(1, bufexists(0)) |
| 20 | bw |
| 21 | call assert_equal(0, bufexists(bn)) |
| 22 | call assert_equal(0, bufexists('Xfoo')) |
| 23 | endfunc |
| 24 | |
Bram Moolenaar | 7929651 | 2020-03-22 16:17:14 +0100 | [diff] [blame] | 25 | func Test_has() |
| 26 | call assert_equal(1, has('eval')) |
| 27 | call assert_equal(1, has('eval', 1)) |
| 28 | |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 29 | if has('unix') |
| 30 | call assert_equal(1, or(has('ttyin'), 1)) |
| 31 | call assert_equal(0, and(has('ttyout'), 0)) |
| 32 | call assert_equal(1, has('multi_byte_encoding')) |
Bram Moolenaar | 80adaa8 | 2023-07-07 18:57:40 +0100 | [diff] [blame] | 33 | call assert_equal(0, has(':tearoff')) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 34 | endif |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 35 | call assert_equal(1, has('vcon', 1)) |
| 36 | call assert_equal(1, has('mouse_gpm_enabled', 1)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 37 | |
Bram Moolenaar | 80adaa8 | 2023-07-07 18:57:40 +0100 | [diff] [blame] | 38 | call assert_equal(has('gui_win32') && has('menu'), has(':tearoff')) |
| 39 | |
Bram Moolenaar | 7929651 | 2020-03-22 16:17:14 +0100 | [diff] [blame] | 40 | call assert_equal(0, has('nonexistent')) |
| 41 | call assert_equal(0, has('nonexistent', 1)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 42 | |
| 43 | " Will we ever have patch 9999? |
| 44 | let ver = 'patch-' .. v:version / 100 .. '.' .. v:version % 100 .. '.9999' |
| 45 | call assert_equal(0, has(ver)) |
Bram Moolenaar | b0375d4 | 2022-06-30 11:03:39 +0100 | [diff] [blame] | 46 | |
| 47 | " There actually isn't a patch 9.0.0, but this is more consistent. |
| 48 | call assert_equal(1, has('patch-9.0.0')) |
Bram Moolenaar | 7929651 | 2020-03-22 16:17:14 +0100 | [diff] [blame] | 49 | endfunc |
| 50 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 51 | func Test_empty() |
| 52 | call assert_equal(1, empty('')) |
| 53 | call assert_equal(0, empty('a')) |
| 54 | |
| 55 | call assert_equal(1, empty(0)) |
| 56 | call assert_equal(1, empty(-0)) |
| 57 | call assert_equal(0, empty(1)) |
| 58 | call assert_equal(0, empty(-1)) |
| 59 | |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 60 | call assert_equal(1, empty(0.0)) |
| 61 | call assert_equal(1, empty(-0.0)) |
| 62 | call assert_equal(0, empty(1.0)) |
| 63 | call assert_equal(0, empty(-1.0)) |
| 64 | call assert_equal(0, empty(1.0/0.0)) |
| 65 | call assert_equal(0, empty(0.0/0.0)) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 66 | |
| 67 | call assert_equal(1, empty([])) |
| 68 | call assert_equal(0, empty(['a'])) |
| 69 | |
| 70 | call assert_equal(1, empty({})) |
| 71 | call assert_equal(0, empty({'a':1})) |
| 72 | |
| 73 | call assert_equal(1, empty(v:null)) |
| 74 | call assert_equal(1, empty(v:none)) |
| 75 | call assert_equal(1, empty(v:false)) |
| 76 | call assert_equal(0, empty(v:true)) |
| 77 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 78 | if has('channel') |
| 79 | call assert_equal(1, empty(test_null_channel())) |
| 80 | endif |
| 81 | if has('job') |
| 82 | call assert_equal(1, empty(test_null_job())) |
| 83 | endif |
| 84 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 85 | call assert_equal(0, empty(function('Test_empty'))) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 86 | call assert_equal(0, empty(function('Test_empty', [0]))) |
Bram Moolenaar | 7c215c5 | 2020-02-29 13:43:27 +0100 | [diff] [blame] | 87 | |
Bram Moolenaar | 097c537 | 2023-05-24 21:02:24 +0100 | [diff] [blame] | 88 | call assert_fails("call empty(test_void())", ['E340:', 'E685:']) |
| 89 | call assert_fails("call empty(test_unknown())", ['E340:', 'E685:']) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 90 | endfunc |
| 91 | |
Bram Moolenaar | 80adaa8 | 2023-07-07 18:57:40 +0100 | [diff] [blame] | 92 | func Test_err_teapot() |
| 93 | call assert_fails('call err_teapot()', "E418: I'm a teapot") |
| 94 | call assert_fails('call err_teapot(0)', "E418: I'm a teapot") |
| 95 | call assert_fails('call err_teapot(v:false)', "E418: I'm a teapot") |
| 96 | |
| 97 | call assert_fails('call err_teapot("1")', "E503: Coffee is currently not available") |
| 98 | call assert_fails('call err_teapot(v:true)', "E503: Coffee is currently not available") |
| 99 | let expr = 1 |
| 100 | call assert_fails('call err_teapot(expr)', "E503: Coffee is currently not available") |
| 101 | endfunc |
| 102 | |
Bram Moolenaar | dd58923 | 2020-02-29 17:38:12 +0100 | [diff] [blame] | 103 | func Test_test_void() |
Bram Moolenaar | 61a417b | 2021-06-15 22:54:28 +0200 | [diff] [blame] | 104 | call assert_fails('echo 1 == test_void()', 'E1031:') |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 105 | call assert_fails('echo 1.0 == test_void()', 'E1031:') |
Bram Moolenaar | 097c537 | 2023-05-24 21:02:24 +0100 | [diff] [blame] | 106 | call assert_fails('let x = json_encode(test_void())', ['E340:', 'E685:']) |
| 107 | call assert_fails('let x = copy(test_void())', ['E340:', 'E685:']) |
Bram Moolenaar | 61a417b | 2021-06-15 22:54:28 +0200 | [diff] [blame] | 108 | call assert_fails('let x = copy([test_void()])', 'E1031:') |
Bram Moolenaar | dd58923 | 2020-02-29 17:38:12 +0100 | [diff] [blame] | 109 | endfunc |
| 110 | |
Bram Moolenaar | 1840a7b | 2021-07-13 20:32:29 +0200 | [diff] [blame] | 111 | func Test_islocked() |
| 112 | call assert_fails('call islocked(99)', 'E475:') |
| 113 | call assert_fails('call islocked("s: x")', 'E488:') |
| 114 | endfunc |
| 115 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 116 | func Test_len() |
| 117 | call assert_equal(1, len(0)) |
| 118 | call assert_equal(2, len(12)) |
| 119 | |
| 120 | call assert_equal(0, len('')) |
| 121 | call assert_equal(2, len('ab')) |
| 122 | |
| 123 | call assert_equal(0, len([])) |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 124 | call assert_equal(0, len(test_null_list())) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 125 | call assert_equal(2, len([2, 1])) |
| 126 | |
| 127 | call assert_equal(0, len({})) |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 128 | call assert_equal(0, len(test_null_dict())) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 129 | call assert_equal(2, len({'a': 1, 'b': 2})) |
| 130 | |
| 131 | call assert_fails('call len(v:none)', 'E701:') |
| 132 | call assert_fails('call len({-> 0})', 'E701:') |
| 133 | endfunc |
| 134 | |
| 135 | func Test_max() |
| 136 | call assert_equal(0, max([])) |
| 137 | call assert_equal(2, max([2])) |
| 138 | call assert_equal(2, max([1, 2])) |
| 139 | call assert_equal(2, max([1, 2, v:null])) |
| 140 | |
| 141 | call assert_equal(0, max({})) |
| 142 | call assert_equal(2, max({'a':1, 'b':2})) |
| 143 | |
| 144 | call assert_fails('call max(1)', 'E712:') |
| 145 | call assert_fails('call max(v:none)', 'E712:') |
Bram Moolenaar | ab65fc7 | 2021-02-04 22:07:16 +0100 | [diff] [blame] | 146 | |
| 147 | " check we only get one error |
| 148 | call assert_fails('call max([#{}, [1]])', ['E728:', 'E728:']) |
| 149 | call assert_fails('call max(#{a: {}, b: [1]})', ['E728:', 'E728:']) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 150 | endfunc |
| 151 | |
| 152 | func Test_min() |
| 153 | call assert_equal(0, min([])) |
| 154 | call assert_equal(2, min([2])) |
| 155 | call assert_equal(1, min([1, 2])) |
| 156 | call assert_equal(0, min([1, 2, v:null])) |
| 157 | |
| 158 | call assert_equal(0, min({})) |
| 159 | call assert_equal(1, min({'a':1, 'b':2})) |
| 160 | |
| 161 | call assert_fails('call min(1)', 'E712:') |
| 162 | call assert_fails('call min(v:none)', 'E712:') |
Yegappan Lakshmanan | 34fcb69 | 2021-05-25 20:14:00 +0200 | [diff] [blame] | 163 | call assert_fails('call min([1, {}])', 'E728:') |
Bram Moolenaar | ab65fc7 | 2021-02-04 22:07:16 +0100 | [diff] [blame] | 164 | |
| 165 | " check we only get one error |
| 166 | call assert_fails('call min([[1], #{}])', ['E745:', 'E745:']) |
| 167 | call assert_fails('call min(#{a: [1], b: #{}})', ['E745:', 'E745:']) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 168 | endfunc |
| 169 | |
Bram Moolenaar | 42ab17b | 2018-05-20 14:11:10 +0200 | [diff] [blame] | 170 | func Test_strwidth() |
| 171 | for aw in ['single', 'double'] |
| 172 | exe 'set ambiwidth=' . aw |
| 173 | call assert_equal(0, strwidth('')) |
| 174 | call assert_equal(1, strwidth("\t")) |
| 175 | call assert_equal(3, strwidth('Vim')) |
| 176 | call assert_equal(4, strwidth(1234)) |
| 177 | call assert_equal(5, strwidth(-1234)) |
| 178 | |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 179 | call assert_equal(2, strwidth('😉')) |
| 180 | call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde')) |
| 181 | call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße')) |
Bram Moolenaar | 42ab17b | 2018-05-20 14:11:10 +0200 | [diff] [blame] | 182 | |
| 183 | call assert_fails('call strwidth({->0})', 'E729:') |
| 184 | call assert_fails('call strwidth([])', 'E730:') |
| 185 | call assert_fails('call strwidth({})', 'E731:') |
Bram Moolenaar | 42ab17b | 2018-05-20 14:11:10 +0200 | [diff] [blame] | 186 | endfor |
| 187 | |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 188 | call assert_equal(3, strwidth(1.2)) |
| 189 | call v9.CheckDefAndScriptFailure(['echo strwidth(1.2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1']) |
Bram Moolenaar | 3cfa5b1 | 2021-06-06 14:14:39 +0200 | [diff] [blame] | 190 | |
Bram Moolenaar | 42ab17b | 2018-05-20 14:11:10 +0200 | [diff] [blame] | 191 | set ambiwidth& |
| 192 | endfunc |
| 193 | |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 194 | func Test_str2nr() |
| 195 | call assert_equal(0, str2nr('')) |
| 196 | call assert_equal(1, str2nr('1')) |
| 197 | call assert_equal(1, str2nr(' 1 ')) |
| 198 | |
| 199 | call assert_equal(1, str2nr('+1')) |
| 200 | call assert_equal(1, str2nr('+ 1')) |
| 201 | call assert_equal(1, str2nr(' + 1 ')) |
| 202 | |
| 203 | call assert_equal(-1, str2nr('-1')) |
| 204 | call assert_equal(-1, str2nr('- 1')) |
| 205 | call assert_equal(-1, str2nr(' - 1 ')) |
| 206 | |
| 207 | call assert_equal(123456789, str2nr('123456789')) |
| 208 | call assert_equal(-123456789, str2nr('-123456789')) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 209 | |
| 210 | call assert_equal(5, str2nr('101', 2)) |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 211 | call assert_equal(5, '0b101'->str2nr(2)) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 212 | call assert_equal(5, str2nr('0B101', 2)) |
| 213 | call assert_equal(-5, str2nr('-101', 2)) |
| 214 | call assert_equal(-5, str2nr('-0b101', 2)) |
| 215 | call assert_equal(-5, str2nr('-0B101', 2)) |
| 216 | |
| 217 | call assert_equal(65, str2nr('101', 8)) |
| 218 | call assert_equal(65, str2nr('0101', 8)) |
| 219 | call assert_equal(-65, str2nr('-101', 8)) |
| 220 | call assert_equal(-65, str2nr('-0101', 8)) |
Bram Moolenaar | c17e66c | 2020-06-02 21:38:22 +0200 | [diff] [blame] | 221 | call assert_equal(65, str2nr('0o101', 8)) |
| 222 | call assert_equal(65, str2nr('0O0101', 8)) |
| 223 | call assert_equal(-65, str2nr('-0O101', 8)) |
| 224 | call assert_equal(-65, str2nr('-0o0101', 8)) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 225 | |
| 226 | call assert_equal(11259375, str2nr('abcdef', 16)) |
| 227 | call assert_equal(11259375, str2nr('ABCDEF', 16)) |
| 228 | call assert_equal(-11259375, str2nr('-ABCDEF', 16)) |
| 229 | call assert_equal(11259375, str2nr('0xabcdef', 16)) |
| 230 | call assert_equal(11259375, str2nr('0Xabcdef', 16)) |
| 231 | call assert_equal(11259375, str2nr('0XABCDEF', 16)) |
| 232 | call assert_equal(-11259375, str2nr('-0xABCDEF', 16)) |
| 233 | |
Bram Moolenaar | 60a8de2 | 2019-09-15 14:33:22 +0200 | [diff] [blame] | 234 | call assert_equal(1, str2nr("1'000'000", 10, 0)) |
| 235 | call assert_equal(256, str2nr("1'0000'0000", 2, 1)) |
| 236 | call assert_equal(262144, str2nr("1'000'000", 8, 1)) |
| 237 | call assert_equal(1000000, str2nr("1'000'000", 10, 1)) |
Bram Moolenaar | ea8dcf8 | 2019-09-15 21:12:22 +0200 | [diff] [blame] | 238 | call assert_equal(1000, str2nr("1'000''000", 10, 1)) |
Bram Moolenaar | 60a8de2 | 2019-09-15 14:33:22 +0200 | [diff] [blame] | 239 | call assert_equal(65536, str2nr("1'00'00", 16, 1)) |
| 240 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 241 | call assert_equal(0, str2nr('0x10')) |
| 242 | call assert_equal(0, str2nr('0b10')) |
Bram Moolenaar | c17e66c | 2020-06-02 21:38:22 +0200 | [diff] [blame] | 243 | call assert_equal(0, str2nr('0o10')) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 244 | call assert_equal(1, str2nr('12', 2)) |
| 245 | call assert_equal(1, str2nr('18', 8)) |
| 246 | call assert_equal(1, str2nr('1g', 16)) |
| 247 | |
| 248 | call assert_equal(0, str2nr(v:null)) |
| 249 | call assert_equal(0, str2nr(v:none)) |
| 250 | |
| 251 | call assert_fails('call str2nr([])', 'E730:') |
| 252 | call assert_fails('call str2nr({->2})', 'E729:') |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 253 | call assert_equal(1, str2nr(1.2)) |
| 254 | call v9.CheckDefAndScriptFailure(['echo str2nr(1.2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1']) |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 255 | call assert_fails('call str2nr(10, [])', 'E745:') |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 256 | endfunc |
| 257 | |
| 258 | func Test_strftime() |
Bram Moolenaar | 10455d4 | 2019-11-21 15:36:18 +0100 | [diff] [blame] | 259 | CheckFunction strftime |
| 260 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 261 | " Format of strftime() depends on system. We assume |
| 262 | " that basic formats tested here are available and |
| 263 | " identical on all systems which support strftime(). |
| 264 | " |
| 265 | " The 2nd parameter of strftime() is a local time, so the output day |
| 266 | " of strftime() can be 17 or 18, depending on timezone. |
| 267 | call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512)) |
| 268 | " |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 269 | 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] | 270 | |
| 271 | call assert_fails('call strftime([])', 'E730:') |
| 272 | call assert_fails('call strftime("%Y", [])', 'E745:') |
Bram Moolenaar | db51730 | 2019-06-18 22:53:24 +0200 | [diff] [blame] | 273 | |
| 274 | " Check that the time changes after we change the timezone |
| 275 | " Save previous timezone value, if any |
| 276 | if exists('$TZ') |
| 277 | let tz = $TZ |
| 278 | endif |
| 279 | |
| 280 | " Force EST and then UTC, save the current hour (24-hour clock) for each |
| 281 | let $TZ = 'EST' | let est = strftime('%H') |
| 282 | let $TZ = 'UTC' | let utc = strftime('%H') |
| 283 | |
| 284 | " Those hours should be two bytes long, and should not be the same; if they |
| 285 | " are, a tzset(3) call may have failed somewhere |
| 286 | call assert_equal(strlen(est), 2) |
| 287 | call assert_equal(strlen(utc), 2) |
Bram Moolenaar | 87652a7 | 2019-06-18 23:07:37 +0200 | [diff] [blame] | 288 | " TODO: this fails on MS-Windows |
| 289 | if has('unix') |
| 290 | call assert_notequal(est, utc) |
| 291 | endif |
Bram Moolenaar | db51730 | 2019-06-18 22:53:24 +0200 | [diff] [blame] | 292 | |
| 293 | " If we cached a timezone value, put it back, otherwise clear it |
| 294 | if exists('tz') |
| 295 | let $TZ = tz |
| 296 | else |
| 297 | unlet $TZ |
| 298 | endif |
Bram Moolenaar | 10455d4 | 2019-11-21 15:36:18 +0100 | [diff] [blame] | 299 | endfunc |
Bram Moolenaar | db51730 | 2019-06-18 22:53:24 +0200 | [diff] [blame] | 300 | |
Bram Moolenaar | 10455d4 | 2019-11-21 15:36:18 +0100 | [diff] [blame] | 301 | func Test_strptime() |
| 302 | CheckFunction strptime |
| 303 | |
| 304 | if exists('$TZ') |
| 305 | let tz = $TZ |
| 306 | endif |
| 307 | let $TZ = 'UTC' |
| 308 | |
Bram Moolenaar | 9a838fe | 2019-12-06 12:45:01 +0100 | [diff] [blame] | 309 | 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] | 310 | |
Bram Moolenaar | ea1233f | 2020-06-10 16:54:13 +0200 | [diff] [blame] | 311 | " Force DST and check that it's considered |
| 312 | let $TZ = 'WINTER0SUMMER,J1,J365' |
| 313 | call assert_equal(1484653763 - 3600, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23')) |
| 314 | |
Bram Moolenaar | 10455d4 | 2019-11-21 15:36:18 +0100 | [diff] [blame] | 315 | call assert_fails('call strptime()', 'E119:') |
| 316 | call assert_fails('call strptime("xxx")', 'E119:') |
| 317 | call assert_equal(0, strptime("%Y", '')) |
| 318 | call assert_equal(0, strptime("%Y", "xxx")) |
| 319 | |
| 320 | if exists('tz') |
| 321 | let $TZ = tz |
| 322 | else |
| 323 | unlet $TZ |
| 324 | endif |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 325 | endfunc |
| 326 | |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 327 | func Test_resolve_unix() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 328 | CheckUnix |
Bram Moolenaar | 2610990 | 2018-10-06 15:43:17 +0200 | [diff] [blame] | 329 | |
| 330 | " Xlink1 -> Xlink2 |
| 331 | " Xlink2 -> Xlink3 |
| 332 | silent !ln -s -f Xlink2 Xlink1 |
| 333 | silent !ln -s -f Xlink3 Xlink2 |
| 334 | call assert_equal('Xlink3', resolve('Xlink1')) |
| 335 | call assert_equal('./Xlink3', resolve('./Xlink1')) |
| 336 | call assert_equal('Xlink3/', resolve('Xlink2/')) |
| 337 | " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?! |
| 338 | "call assert_equal('Xlink3/', resolve('Xlink1/')) |
| 339 | "call assert_equal('./Xlink3/', resolve('./Xlink1/')) |
| 340 | "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/')) |
| 341 | call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1')) |
| 342 | |
| 343 | " Test resolve() with a symlink cycle. |
| 344 | " Xlink1 -> Xlink2 |
| 345 | " Xlink2 -> Xlink3 |
| 346 | " Xlink3 -> Xlink1 |
| 347 | silent !ln -s -f Xlink1 Xlink3 |
| 348 | call assert_fails('call resolve("Xlink1")', 'E655:') |
| 349 | call assert_fails('call resolve("./Xlink1")', 'E655:') |
| 350 | call assert_fails('call resolve("Xlink2")', 'E655:') |
| 351 | call assert_fails('call resolve("Xlink3")', 'E655:') |
| 352 | call delete('Xlink1') |
| 353 | call delete('Xlink2') |
| 354 | call delete('Xlink3') |
| 355 | |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 356 | silent !ln -s -f Xresolvedir//Xfile Xresolvelink |
| 357 | call assert_equal('Xresolvedir/Xfile', resolve('Xresolvelink')) |
| 358 | call delete('Xresolvelink') |
Bram Moolenaar | 2610990 | 2018-10-06 15:43:17 +0200 | [diff] [blame] | 359 | |
| 360 | silent !ln -s -f Xlink2/ Xlink1 |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 361 | call assert_equal('Xlink2', 'Xlink1'->resolve()) |
Bram Moolenaar | 2610990 | 2018-10-06 15:43:17 +0200 | [diff] [blame] | 362 | call assert_equal('Xlink2/', resolve('Xlink1/')) |
| 363 | call delete('Xlink1') |
| 364 | |
| 365 | silent !ln -s -f ./Xlink2 Xlink1 |
| 366 | call assert_equal('Xlink2', resolve('Xlink1')) |
| 367 | call assert_equal('./Xlink2', resolve('./Xlink1')) |
| 368 | call delete('Xlink1') |
Bram Moolenaar | 50c4e9e | 2020-10-05 20:38:06 +0200 | [diff] [blame] | 369 | |
| 370 | call assert_equal('/', resolve('/')) |
Bram Moolenaar | 2610990 | 2018-10-06 15:43:17 +0200 | [diff] [blame] | 371 | endfunc |
| 372 | |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 373 | func s:normalize_fname(fname) |
| 374 | let ret = substitute(a:fname, '\', '/', 'g') |
| 375 | let ret = substitute(ret, '//', '/', 'g') |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 376 | return ret->tolower() |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 377 | endfunc |
| 378 | |
| 379 | func Test_resolve_win32() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 380 | CheckMSWindows |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 381 | |
| 382 | " test for shortcut file |
| 383 | if executable('cscript') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 384 | new Xresfile |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 385 | wq |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 386 | let lines =<< trim END |
| 387 | Set fs = CreateObject("Scripting.FileSystemObject") |
| 388 | Set ws = WScript.CreateObject("WScript.Shell") |
| 389 | Set shortcut = ws.CreateShortcut("Xlink.lnk") |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 390 | shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xresfile") |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 391 | shortcut.Save |
| 392 | END |
| 393 | call writefile(lines, 'link.vbs') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 394 | silent !cscript link.vbs |
| 395 | call delete('link.vbs') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 396 | call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink.lnk'))) |
| 397 | call delete('Xresfile') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 398 | |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 399 | call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink.lnk'))) |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 400 | call delete('Xlink.lnk') |
| 401 | else |
| 402 | echomsg 'skipped test for shortcut file' |
| 403 | endif |
| 404 | |
| 405 | " remove files |
| 406 | call delete('Xlink') |
Bram Moolenaar | 15cae5c | 2022-08-29 22:51:38 +0100 | [diff] [blame] | 407 | call delete('Xdir', 'd') |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 408 | call delete('Xresfile') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 409 | |
| 410 | " test for symbolic link to a file |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 411 | new Xresfile |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 412 | wq |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 413 | call assert_equal('Xresfile', resolve('Xresfile')) |
| 414 | silent !mklink Xlink Xresfile |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 415 | if !v:shell_error |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 416 | call assert_equal(s:normalize_fname(getcwd() . '\Xresfile'), s:normalize_fname(resolve('./Xlink'))) |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 417 | call delete('Xlink') |
| 418 | else |
| 419 | echomsg 'skipped test for symbolic link to a file' |
| 420 | endif |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 421 | call delete('Xresfile') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 422 | |
| 423 | " test for junction to a directory |
Bram Moolenaar | 15cae5c | 2022-08-29 22:51:38 +0100 | [diff] [blame] | 424 | call mkdir('Xdir') |
| 425 | silent !mklink /J Xlink Xdir |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 426 | if !v:shell_error |
Bram Moolenaar | 15cae5c | 2022-08-29 22:51:38 +0100 | [diff] [blame] | 427 | call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 428 | |
Bram Moolenaar | 15cae5c | 2022-08-29 22:51:38 +0100 | [diff] [blame] | 429 | call delete('Xdir', 'd') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 430 | |
| 431 | " test for junction already removed |
| 432 | call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) |
| 433 | call delete('Xlink') |
| 434 | else |
| 435 | echomsg 'skipped test for junction to a directory' |
Bram Moolenaar | 15cae5c | 2022-08-29 22:51:38 +0100 | [diff] [blame] | 436 | call delete('Xdir', 'd') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 437 | endif |
| 438 | |
| 439 | " test for symbolic link to a directory |
Bram Moolenaar | 15cae5c | 2022-08-29 22:51:38 +0100 | [diff] [blame] | 440 | call mkdir('Xdir') |
| 441 | silent !mklink /D Xlink Xdir |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 442 | if !v:shell_error |
Bram Moolenaar | 15cae5c | 2022-08-29 22:51:38 +0100 | [diff] [blame] | 443 | call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 444 | |
Bram Moolenaar | 15cae5c | 2022-08-29 22:51:38 +0100 | [diff] [blame] | 445 | call delete('Xdir', 'd') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 446 | |
| 447 | " test for symbolic link already removed |
| 448 | call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) |
| 449 | call delete('Xlink') |
| 450 | else |
| 451 | echomsg 'skipped test for symbolic link to a directory' |
Bram Moolenaar | 15cae5c | 2022-08-29 22:51:38 +0100 | [diff] [blame] | 452 | call delete('Xdir', 'd') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 453 | endif |
| 454 | |
| 455 | " test for buffer name |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 456 | new Xbuffile |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 457 | wq |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 458 | silent !mklink Xlink Xbuffile |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 459 | if !v:shell_error |
| 460 | edit Xlink |
| 461 | call assert_equal('Xlink', bufname('%')) |
| 462 | call delete('Xlink') |
| 463 | bw! |
| 464 | else |
| 465 | echomsg 'skipped test for buffer name' |
| 466 | endif |
Bram Moolenaar | b18b496 | 2022-09-02 21:55:50 +0100 | [diff] [blame] | 467 | call delete('Xbuffile') |
Bram Moolenaar | 1bbebab | 2019-05-29 20:36:54 +0200 | [diff] [blame] | 468 | |
| 469 | " test for reparse point |
Bram Moolenaar | 15cae5c | 2022-08-29 22:51:38 +0100 | [diff] [blame] | 470 | call mkdir('Xdir') |
| 471 | call assert_equal('Xdir', resolve('Xdir')) |
| 472 | silent !mklink /D Xdirlink Xdir |
Bram Moolenaar | 1bbebab | 2019-05-29 20:36:54 +0200 | [diff] [blame] | 473 | if !v:shell_error |
Bram Moolenaar | 15cae5c | 2022-08-29 22:51:38 +0100 | [diff] [blame] | 474 | w Xdir/text.txt |
| 475 | call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt')) |
| 476 | call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt'))) |
| 477 | 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] | 478 | call delete('Xdirlink') |
Bram Moolenaar | 1bbebab | 2019-05-29 20:36:54 +0200 | [diff] [blame] | 479 | else |
| 480 | echomsg 'skipped test for reparse point' |
| 481 | endif |
| 482 | |
Bram Moolenaar | 15cae5c | 2022-08-29 22:51:38 +0100 | [diff] [blame] | 483 | call delete('Xdir', 'rf') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 484 | endfunc |
| 485 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 486 | func Test_simplify() |
| 487 | call assert_equal('', simplify('')) |
| 488 | call assert_equal('/', simplify('/')) |
| 489 | call assert_equal('/', simplify('/.')) |
| 490 | call assert_equal('/', simplify('/..')) |
| 491 | call assert_equal('/...', simplify('/...')) |
Bram Moolenaar | fdcbe3c | 2020-06-15 21:41:56 +0200 | [diff] [blame] | 492 | call assert_equal('//path', simplify('//path')) |
Bram Moolenaar | c70222d | 2020-06-15 23:18:12 +0200 | [diff] [blame] | 493 | if has('unix') |
| 494 | call assert_equal('/path', simplify('///path')) |
| 495 | call assert_equal('/path', simplify('////path')) |
| 496 | endif |
Bram Moolenaar | fdcbe3c | 2020-06-15 21:41:56 +0200 | [diff] [blame] | 497 | |
Bram Moolenaar | 7035fd9 | 2020-04-08 20:03:52 +0200 | [diff] [blame] | 498 | call assert_equal('./dir/file', './dir/file'->simplify()) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 499 | call assert_equal('./dir/file', simplify('.///dir//file')) |
| 500 | call assert_equal('./dir/file', simplify('./dir/./file')) |
| 501 | call assert_equal('./file', simplify('./dir/../file')) |
| 502 | call assert_equal('../dir/file', simplify('dir/../../dir/file')) |
| 503 | call assert_equal('./file', simplify('dir/.././file')) |
Bram Moolenaar | bdd2c29 | 2020-06-22 21:34:30 +0200 | [diff] [blame] | 504 | call assert_equal('../dir', simplify('./../dir')) |
| 505 | call assert_equal('..', simplify('../testdir/..')) |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 506 | call mkdir('Xsimpdir') |
| 507 | call assert_equal('.', simplify('Xsimpdir/../.')) |
| 508 | call delete('Xsimpdir', 'd') |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 509 | |
| 510 | call assert_fails('call simplify({->0})', 'E729:') |
| 511 | call assert_fails('call simplify([])', 'E730:') |
| 512 | call assert_fails('call simplify({})', 'E731:') |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 513 | call assert_equal('1.2', simplify(1.2)) |
| 514 | call v9.CheckDefAndScriptFailure(['echo simplify(1.2)'], ['E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1']) |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 515 | endfunc |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 516 | |
Bram Moolenaar | bfde0b4 | 2018-08-08 22:27:31 +0200 | [diff] [blame] | 517 | func Test_pathshorten() |
| 518 | call assert_equal('', pathshorten('')) |
| 519 | call assert_equal('foo', pathshorten('foo')) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 520 | call assert_equal('/foo', '/foo'->pathshorten()) |
Bram Moolenaar | bfde0b4 | 2018-08-08 22:27:31 +0200 | [diff] [blame] | 521 | call assert_equal('f/', pathshorten('foo/')) |
| 522 | call assert_equal('f/bar', pathshorten('foo/bar')) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 523 | call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten()) |
Bram Moolenaar | bfde0b4 | 2018-08-08 22:27:31 +0200 | [diff] [blame] | 524 | call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar')) |
| 525 | call assert_equal('.f/bar', pathshorten('.foo/bar')) |
| 526 | call assert_equal('~f/bar', pathshorten('~foo/bar')) |
| 527 | call assert_equal('~.f/bar', pathshorten('~.foo/bar')) |
| 528 | call assert_equal('.~f/bar', pathshorten('.~foo/bar')) |
| 529 | call assert_equal('~/f/bar', pathshorten('~/foo/bar')) |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 530 | call assert_fails('call pathshorten([])', 'E730:') |
Bram Moolenaar | 6a33ef0 | 2020-09-25 22:42:48 +0200 | [diff] [blame] | 531 | |
| 532 | " test pathshorten with optional variable to set preferred size of shortening |
| 533 | call assert_equal('', pathshorten('', 2)) |
| 534 | call assert_equal('foo', pathshorten('foo', 2)) |
| 535 | call assert_equal('/foo', pathshorten('/foo', 2)) |
| 536 | call assert_equal('fo/', pathshorten('foo/', 2)) |
| 537 | call assert_equal('fo/bar', pathshorten('foo/bar', 2)) |
| 538 | call assert_equal('fo/ba/foobar', pathshorten('foo/bar/foobar', 2)) |
| 539 | call assert_equal('/fo/ba/foobar', pathshorten('/foo/bar/foobar', 2)) |
| 540 | call assert_equal('.fo/bar', pathshorten('.foo/bar', 2)) |
| 541 | call assert_equal('~fo/bar', pathshorten('~foo/bar', 2)) |
| 542 | call assert_equal('~.fo/bar', pathshorten('~.foo/bar', 2)) |
| 543 | call assert_equal('.~fo/bar', pathshorten('.~foo/bar', 2)) |
| 544 | call assert_equal('~/fo/bar', pathshorten('~/foo/bar', 2)) |
| 545 | call assert_fails('call pathshorten([],2)', 'E730:') |
| 546 | call assert_notequal('~/fo/bar', pathshorten('~/foo/bar', 3)) |
| 547 | call assert_equal('~/foo/bar', pathshorten('~/foo/bar', 3)) |
| 548 | call assert_equal('~/f/bar', pathshorten('~/foo/bar', 0)) |
Bram Moolenaar | bfde0b4 | 2018-08-08 22:27:31 +0200 | [diff] [blame] | 549 | endfunc |
| 550 | |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 551 | func Test_strpart() |
| 552 | call assert_equal('de', strpart('abcdefg', 3, 2)) |
| 553 | call assert_equal('ab', strpart('abcdefg', -2, 4)) |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 554 | call assert_equal('abcdefg', 'abcdefg'->strpart(-2)) |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 555 | call assert_equal('fg', strpart('abcdefg', 5, 4)) |
| 556 | call assert_equal('defg', strpart('abcdefg', 3)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 557 | call assert_equal('', strpart('abcdefg', 10)) |
| 558 | call assert_fails("let s=strpart('abcdef', [])", 'E745:') |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 559 | |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 560 | call assert_equal('lép', strpart('éléphant', 2, 4)) |
| 561 | call assert_equal('léphant', strpart('éléphant', 2)) |
Bram Moolenaar | 6c53fca | 2020-08-23 17:34:46 +0200 | [diff] [blame] | 562 | |
| 563 | call assert_equal('é', strpart('éléphant', 0, 1, 1)) |
| 564 | call assert_equal('ép', strpart('éléphant', 3, 2, v:true)) |
| 565 | call assert_equal('ó', strpart('cómposed', 1, 1, 1)) |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 566 | endfunc |
| 567 | |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 568 | func Test_tolower() |
| 569 | call assert_equal("", tolower("")) |
| 570 | |
| 571 | " Test with all printable ASCII characters. |
| 572 | call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~', |
| 573 | \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')) |
| 574 | |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 575 | " Test with a few uppercase diacritics. |
| 576 | call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ")) |
| 577 | call assert_equal("bḃḇ", tolower("BḂḆ")) |
| 578 | call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ")) |
| 579 | call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ")) |
| 580 | call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ")) |
| 581 | call assert_equal("fḟ ", tolower("FḞ ")) |
| 582 | call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ")) |
| 583 | call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ")) |
| 584 | call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ")) |
| 585 | call assert_equal("jĵ", tolower("JĴ")) |
| 586 | call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ")) |
| 587 | call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ")) |
| 588 | call assert_equal("mḿṁ", tolower("MḾṀ")) |
| 589 | call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ")) |
| 590 | call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ")) |
| 591 | call assert_equal("pṕṗ", tolower("PṔṖ")) |
| 592 | call assert_equal("q", tolower("Q")) |
| 593 | call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ")) |
| 594 | call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ")) |
| 595 | call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ")) |
| 596 | call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ")) |
| 597 | call assert_equal("vṽ", tolower("VṼ")) |
| 598 | call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ")) |
| 599 | call assert_equal("xẋẍ", tolower("XẊẌ")) |
| 600 | call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ")) |
| 601 | call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ")) |
| 602 | |
| 603 | " Test with a few lowercase diacritics, which should remain unchanged. |
| 604 | call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả")) |
| 605 | call assert_equal("bḃḇ", tolower("bḃḇ")) |
| 606 | call assert_equal("cçćĉċč", tolower("cçćĉċč")) |
| 607 | call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ")) |
| 608 | call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ")) |
| 609 | call assert_equal("fḟ", tolower("fḟ")) |
| 610 | call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ")) |
| 611 | call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ")) |
| 612 | call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ")) |
| 613 | call assert_equal("jĵǰ", tolower("jĵǰ")) |
| 614 | call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ")) |
| 615 | call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ")) |
| 616 | call assert_equal("mḿṁ ", tolower("mḿṁ ")) |
| 617 | call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ")) |
| 618 | call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ")) |
| 619 | call assert_equal("pṕṗ", tolower("pṕṗ")) |
| 620 | call assert_equal("q", tolower("q")) |
| 621 | call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ")) |
| 622 | call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ")) |
| 623 | call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ")) |
| 624 | call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ")) |
| 625 | call assert_equal("vṽ", tolower("vṽ")) |
| 626 | call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ")) |
| 627 | call assert_equal("ẋẍ", tolower("ẋẍ")) |
| 628 | call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ")) |
| 629 | call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ")) |
| 630 | |
| 631 | " According to https://twitter.com/jifa/status/625776454479970304 |
| 632 | " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase |
| 633 | " in length (2 to 3 bytes) when lowercased. So let's test them. |
| 634 | call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ")) |
Bram Moolenaar | e6640ad | 2017-12-22 21:06:56 +0100 | [diff] [blame] | 635 | |
| 636 | " This call to tolower with invalid utf8 sequence used to cause access to |
| 637 | " invalid memory. |
| 638 | call tolower("\xC0\x80\xC0") |
| 639 | call tolower("123\xC0\x80\xC0") |
Bram Moolenaar | 0ff5ded | 2020-05-07 18:43:44 +0200 | [diff] [blame] | 640 | |
| 641 | " Test in latin1 encoding |
| 642 | let save_enc = &encoding |
| 643 | set encoding=latin1 |
| 644 | call assert_equal("abc", tolower("ABC")) |
| 645 | let &encoding = save_enc |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 646 | endfunc |
| 647 | |
| 648 | func Test_toupper() |
| 649 | call assert_equal("", toupper("")) |
| 650 | |
| 651 | " Test with all printable ASCII characters. |
| 652 | call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~', |
| 653 | \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')) |
| 654 | |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 655 | " Test with a few lowercase diacritics. |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 656 | call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper()) |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 657 | call assert_equal("BḂḆ", toupper("bḃḇ")) |
| 658 | call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč")) |
| 659 | call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ")) |
| 660 | call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ")) |
| 661 | call assert_equal("FḞ", toupper("fḟ")) |
| 662 | call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ")) |
| 663 | call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ")) |
| 664 | call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ")) |
| 665 | call assert_equal("JĴǰ", toupper("jĵǰ")) |
| 666 | call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ")) |
| 667 | call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ")) |
| 668 | call assert_equal("MḾṀ ", toupper("mḿṁ ")) |
| 669 | call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ")) |
| 670 | call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ")) |
| 671 | call assert_equal("PṔṖ", toupper("pṕṗ")) |
| 672 | call assert_equal("Q", toupper("q")) |
| 673 | call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ")) |
| 674 | call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ")) |
| 675 | call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ")) |
| 676 | call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ")) |
| 677 | call assert_equal("VṼ", toupper("vṽ")) |
| 678 | call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ")) |
| 679 | call assert_equal("ẊẌ", toupper("ẋẍ")) |
| 680 | call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ")) |
| 681 | call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ")) |
| 682 | |
| 683 | " Test that uppercase diacritics, which should remain unchanged. |
| 684 | call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ")) |
| 685 | call assert_equal("BḂḆ", toupper("BḂḆ")) |
| 686 | call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ")) |
| 687 | call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ")) |
| 688 | call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ")) |
| 689 | call assert_equal("FḞ ", toupper("FḞ ")) |
| 690 | call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ")) |
| 691 | call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ")) |
| 692 | call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ")) |
| 693 | call assert_equal("JĴ", toupper("JĴ")) |
| 694 | call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ")) |
| 695 | call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ")) |
| 696 | call assert_equal("MḾṀ", toupper("MḾṀ")) |
| 697 | call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ")) |
| 698 | call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ")) |
| 699 | call assert_equal("PṔṖ", toupper("PṔṖ")) |
| 700 | call assert_equal("Q", toupper("Q")) |
| 701 | call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ")) |
| 702 | call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ")) |
| 703 | call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ")) |
| 704 | call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ")) |
| 705 | call assert_equal("VṼ", toupper("VṼ")) |
| 706 | call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ")) |
| 707 | call assert_equal("XẊẌ", toupper("XẊẌ")) |
| 708 | call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ")) |
| 709 | call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ")) |
| 710 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 711 | call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ")) |
Bram Moolenaar | e6640ad | 2017-12-22 21:06:56 +0100 | [diff] [blame] | 712 | |
| 713 | " This call to toupper with invalid utf8 sequence used to cause access to |
| 714 | " invalid memory. |
| 715 | call toupper("\xC0\x80\xC0") |
| 716 | call toupper("123\xC0\x80\xC0") |
Bram Moolenaar | 0ff5ded | 2020-05-07 18:43:44 +0200 | [diff] [blame] | 717 | |
| 718 | " Test in latin1 encoding |
| 719 | let save_enc = &encoding |
| 720 | set encoding=latin1 |
| 721 | call assert_equal("ABC", toupper("abc")) |
| 722 | let &encoding = save_enc |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 723 | endfunc |
| 724 | |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 725 | func Test_tr() |
| 726 | call assert_equal('foo', tr('bar', 'bar', 'foo')) |
| 727 | call assert_equal('zxy', 'cab'->tr('abc', 'xyz')) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 728 | call assert_fails("let s=tr([], 'abc', 'def')", 'E730:') |
| 729 | call assert_fails("let s=tr('abc', [], 'def')", 'E730:') |
| 730 | call assert_fails("let s=tr('abc', 'abc', [])", 'E730:') |
| 731 | call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:') |
| 732 | set encoding=latin1 |
| 733 | call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:') |
| 734 | call assert_equal('hEllO', tr('hello', 'eo', 'EO')) |
| 735 | call assert_equal('hello', tr('hello', 'xy', 'ab')) |
Yegappan Lakshmanan | 34fcb69 | 2021-05-25 20:14:00 +0200 | [diff] [blame] | 736 | call assert_fails('call tr("abc", "123", "₁₂")', 'E475:') |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 737 | set encoding=utf8 |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 738 | endfunc |
| 739 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 740 | " Tests for the mode() function |
| 741 | let current_modes = '' |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 742 | func Save_mode() |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 743 | let g:current_modes = mode(0) . '-' . mode(1) |
| 744 | return '' |
| 745 | endfunc |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 746 | |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 747 | " Test for the mode() function |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 748 | func Test_mode() |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 749 | new |
| 750 | call append(0, ["Blue Ball Black", "Brown Band Bowl", ""]) |
| 751 | |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 752 | " Only complete from the current buffer. |
| 753 | set complete=. |
| 754 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 755 | inoremap <F2> <C-R>=Save_mode()<CR> |
zeertzjq | eaf3f36 | 2021-07-28 16:51:53 +0200 | [diff] [blame] | 756 | xnoremap <F2> <Cmd>call Save_mode()<CR> |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 757 | |
| 758 | normal! 3G |
| 759 | exe "normal i\<F2>\<Esc>" |
| 760 | call assert_equal('i-i', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 761 | " i_CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 762 | exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u" |
| 763 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 764 | " i_CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 765 | exe "normal iBro\<C-P>\<F2>\<Esc>u" |
| 766 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 767 | " i_CTRL-X |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 768 | exe "normal iBa\<C-X>\<F2>\<Esc>u" |
| 769 | call assert_equal('i-ix', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 770 | " i_CTRL-X CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 771 | exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u" |
| 772 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 773 | " i_CTRL-X CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 774 | exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u" |
| 775 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 776 | " i_CTRL-X CTRL-P + CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 777 | exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" |
| 778 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 779 | " i_CTRL-X CTRL-L: Multiple matches |
| 780 | exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u" |
| 781 | call assert_equal('i-ic', g:current_modes) |
| 782 | " i_CTRL-X CTRL-L: Single match |
| 783 | exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u" |
| 784 | call assert_equal('i-ic', g:current_modes) |
| 785 | " i_CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 786 | exe "normal iCom\<C-P>\<F2>\<Esc>u" |
| 787 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 788 | " i_CTRL-X CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 789 | exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u" |
| 790 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 791 | " i_CTRL-X CTRL-L: No match |
| 792 | exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u" |
| 793 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 794 | |
zeertzjq | cc8cd44 | 2021-10-03 15:19:14 +0100 | [diff] [blame] | 795 | exe "normal R\<F2>\<Esc>" |
| 796 | call assert_equal('R-R', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 797 | " R_CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 798 | exe "normal RBa\<C-P>\<F2>\<Esc>u" |
| 799 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 800 | " R_CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 801 | exe "normal RBro\<C-P>\<F2>\<Esc>u" |
| 802 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 803 | " R_CTRL-X |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 804 | exe "normal RBa\<C-X>\<F2>\<Esc>u" |
| 805 | call assert_equal('R-Rx', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 806 | " R_CTRL-X CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 807 | exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u" |
| 808 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 809 | " R_CTRL-X CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 810 | exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u" |
| 811 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 812 | " R_CTRL-X CTRL-P + CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 813 | exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" |
| 814 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 815 | " R_CTRL-X CTRL-L: Multiple matches |
| 816 | exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u" |
| 817 | call assert_equal('R-Rc', g:current_modes) |
| 818 | " R_CTRL-X CTRL-L: Single match |
| 819 | exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u" |
| 820 | call assert_equal('R-Rc', g:current_modes) |
| 821 | " R_CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 822 | exe "normal RCom\<C-P>\<F2>\<Esc>u" |
| 823 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 824 | " R_CTRL-X CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 825 | exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u" |
| 826 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 827 | " R_CTRL-X CTRL-L: No match |
| 828 | exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u" |
| 829 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 830 | |
zeertzjq | cc8cd44 | 2021-10-03 15:19:14 +0100 | [diff] [blame] | 831 | exe "normal gR\<F2>\<Esc>" |
| 832 | call assert_equal('R-Rv', g:current_modes) |
| 833 | " gR_CTRL-P: Multiple matches |
| 834 | exe "normal gRBa\<C-P>\<F2>\<Esc>u" |
| 835 | call assert_equal('R-Rvc', g:current_modes) |
| 836 | " gR_CTRL-P: Single match |
| 837 | exe "normal gRBro\<C-P>\<F2>\<Esc>u" |
| 838 | call assert_equal('R-Rvc', g:current_modes) |
| 839 | " gR_CTRL-X |
| 840 | exe "normal gRBa\<C-X>\<F2>\<Esc>u" |
| 841 | call assert_equal('R-Rvx', g:current_modes) |
| 842 | " gR_CTRL-X CTRL-P: Multiple matches |
| 843 | exe "normal gRBa\<C-X>\<C-P>\<F2>\<Esc>u" |
| 844 | call assert_equal('R-Rvc', g:current_modes) |
| 845 | " gR_CTRL-X CTRL-P: Single match |
| 846 | exe "normal gRBro\<C-X>\<C-P>\<F2>\<Esc>u" |
| 847 | call assert_equal('R-Rvc', g:current_modes) |
| 848 | " gR_CTRL-X CTRL-P + CTRL-P: Single match |
| 849 | exe "normal gRBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" |
| 850 | call assert_equal('R-Rvc', g:current_modes) |
| 851 | " gR_CTRL-X CTRL-L: Multiple matches |
| 852 | exe "normal gR\<C-X>\<C-L>\<F2>\<Esc>u" |
| 853 | call assert_equal('R-Rvc', g:current_modes) |
| 854 | " gR_CTRL-X CTRL-L: Single match |
| 855 | exe "normal gRBlu\<C-X>\<C-L>\<F2>\<Esc>u" |
| 856 | call assert_equal('R-Rvc', g:current_modes) |
| 857 | " gR_CTRL-P: No match |
| 858 | exe "normal gRCom\<C-P>\<F2>\<Esc>u" |
| 859 | call assert_equal('R-Rvc', g:current_modes) |
| 860 | " gR_CTRL-X CTRL-P: No match |
| 861 | exe "normal gRCom\<C-X>\<C-P>\<F2>\<Esc>u" |
| 862 | call assert_equal('R-Rvc', g:current_modes) |
| 863 | " gR_CTRL-X CTRL-L: No match |
| 864 | exe "normal gRabc\<C-X>\<C-L>\<F2>\<Esc>u" |
| 865 | call assert_equal('R-Rvc', g:current_modes) |
| 866 | |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 867 | call assert_equal('n', 0->mode()) |
| 868 | call assert_equal('n', 1->mode()) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 869 | |
Bram Moolenaar | 612cc38 | 2018-07-29 15:34:26 +0200 | [diff] [blame] | 870 | " i_CTRL-O |
| 871 | exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>" |
| 872 | call assert_equal("n-niI", g:current_modes) |
| 873 | |
| 874 | " R_CTRL-O |
| 875 | exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>" |
| 876 | call assert_equal("n-niR", g:current_modes) |
| 877 | |
| 878 | " gR_CTRL-O |
| 879 | exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>" |
| 880 | call assert_equal("n-niV", g:current_modes) |
| 881 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 882 | " How to test operator-pending mode? |
| 883 | |
| 884 | call feedkeys("v", 'xt') |
| 885 | call assert_equal('v', mode()) |
| 886 | call assert_equal('v', mode(1)) |
| 887 | call feedkeys("\<Esc>V", 'xt') |
| 888 | call assert_equal('V', mode()) |
| 889 | call assert_equal('V', mode(1)) |
| 890 | call feedkeys("\<Esc>\<C-V>", 'xt') |
| 891 | call assert_equal("\<C-V>", mode()) |
| 892 | call assert_equal("\<C-V>", mode(1)) |
| 893 | call feedkeys("\<Esc>", 'xt') |
| 894 | |
| 895 | call feedkeys("gh", 'xt') |
| 896 | call assert_equal('s', mode()) |
| 897 | call assert_equal('s', mode(1)) |
| 898 | call feedkeys("\<Esc>gH", 'xt') |
| 899 | call assert_equal('S', mode()) |
| 900 | call assert_equal('S', mode(1)) |
| 901 | call feedkeys("\<Esc>g\<C-H>", 'xt') |
| 902 | call assert_equal("\<C-S>", mode()) |
| 903 | call assert_equal("\<C-S>", mode(1)) |
| 904 | call feedkeys("\<Esc>", 'xt') |
| 905 | |
zeertzjq | eaf3f36 | 2021-07-28 16:51:53 +0200 | [diff] [blame] | 906 | " v_CTRL-O |
| 907 | exe "normal gh\<C-O>\<F2>\<Esc>" |
| 908 | call assert_equal("v-vs", g:current_modes) |
| 909 | exe "normal gH\<C-O>\<F2>\<Esc>" |
| 910 | call assert_equal("V-Vs", g:current_modes) |
| 911 | exe "normal g\<C-H>\<C-O>\<F2>\<Esc>" |
| 912 | call assert_equal("\<C-V>-\<C-V>s", g:current_modes) |
| 913 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 914 | call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt') |
| 915 | call assert_equal('c-c', g:current_modes) |
| 916 | call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt') |
| 917 | call assert_equal('c-cv', g:current_modes) |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 918 | call feedkeys("Qcall Save_mode()\<CR>vi\<CR>", 'xt') |
| 919 | call assert_equal('c-ce', g:current_modes) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 920 | " How to test Ex mode? |
| 921 | |
naohiro ono | 75c30e9 | 2021-10-19 11:15:41 +0100 | [diff] [blame] | 922 | " Test mode in operatorfunc (it used to be Operator-pending). |
| 923 | set operatorfunc=OperatorFunc |
| 924 | function OperatorFunc(_) |
| 925 | call Save_mode() |
| 926 | endfunction |
| 927 | execute "normal! g@l\<Esc>" |
| 928 | call assert_equal('n-n', g:current_modes) |
| 929 | execute "normal! i\<C-o>g@l\<Esc>" |
| 930 | call assert_equal('n-niI', g:current_modes) |
| 931 | execute "normal! R\<C-o>g@l\<Esc>" |
| 932 | call assert_equal('n-niR', g:current_modes) |
| 933 | execute "normal! gR\<C-o>g@l\<Esc>" |
| 934 | call assert_equal('n-niV', g:current_modes) |
| 935 | |
Bram Moolenaar | 72406a4 | 2021-10-02 16:34:55 +0100 | [diff] [blame] | 936 | if has('terminal') |
| 937 | term |
| 938 | call feedkeys("\<C-W>N", 'xt') |
| 939 | call assert_equal('n', mode()) |
| 940 | call assert_equal('nt', mode(1)) |
| 941 | call feedkeys("aexit\<CR>", 'xt') |
| 942 | endif |
| 943 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 944 | bwipe! |
| 945 | iunmap <F2> |
zeertzjq | eaf3f36 | 2021-07-28 16:51:53 +0200 | [diff] [blame] | 946 | xunmap <F2> |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 947 | set complete& |
naohiro ono | 75c30e9 | 2021-10-19 11:15:41 +0100 | [diff] [blame] | 948 | set operatorfunc& |
| 949 | delfunction OperatorFunc |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 950 | endfunc |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 951 | |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 952 | " Test for append() |
Bram Moolenaar | d200702 | 2019-08-27 21:56:06 +0200 | [diff] [blame] | 953 | func Test_append() |
| 954 | enew! |
| 955 | split |
Bram Moolenaar | cd9c8d4 | 2022-11-05 23:46:43 +0000 | [diff] [blame] | 956 | call assert_equal(0, append(1, [])) |
| 957 | call assert_equal(0, append(1, test_null_list())) |
| 958 | call assert_equal(0, append(0, ["foo"])) |
| 959 | call assert_equal(0, append(1, [])) |
| 960 | call assert_equal(0, append(1, test_null_list())) |
| 961 | call assert_equal(0, append(8, [])) |
| 962 | call assert_equal(0, append(9, test_null_list())) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 963 | call assert_equal(['foo', ''], getline(1, '$')) |
Bram Moolenaar | d200702 | 2019-08-27 21:56:06 +0200 | [diff] [blame] | 964 | split |
| 965 | only |
| 966 | undo |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 967 | undo |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 968 | |
| 969 | " Using $ instead of '$' must give an error |
| 970 | call assert_fails("call append($, 'foobar')", 'E116:') |
Bram Moolenaar | 801cd35 | 2022-10-10 16:08:16 +0100 | [diff] [blame] | 971 | |
| 972 | call assert_fails("call append({}, '')", ['E728:', 'E728:']) |
Bram Moolenaar | d200702 | 2019-08-27 21:56:06 +0200 | [diff] [blame] | 973 | endfunc |
| 974 | |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 975 | " Test for setline() |
| 976 | func Test_setline() |
| 977 | new |
| 978 | call setline(0, ["foo"]) |
| 979 | call setline(0, []) |
| 980 | call setline(0, test_null_list()) |
| 981 | call setline(1, ["bar"]) |
| 982 | call setline(1, []) |
| 983 | call setline(1, test_null_list()) |
| 984 | call setline(2, []) |
| 985 | call setline(2, test_null_list()) |
| 986 | call setline(3, []) |
| 987 | call setline(3, test_null_list()) |
| 988 | call setline(2, ["baz"]) |
| 989 | call assert_equal(['bar', 'baz'], getline(1, '$')) |
| 990 | close! |
| 991 | endfunc |
| 992 | |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 993 | func Test_getbufvar() |
| 994 | let bnr = bufnr('%') |
| 995 | let b:var_num = '1234' |
| 996 | let def_num = '5678' |
| 997 | call assert_equal('1234', getbufvar(bnr, 'var_num')) |
| 998 | call assert_equal('1234', getbufvar(bnr, 'var_num', def_num)) |
| 999 | |
| 1000 | let bd = getbufvar(bnr, '') |
| 1001 | call assert_equal('1234', bd['var_num']) |
| 1002 | call assert_true(exists("bd['changedtick']")) |
| 1003 | call assert_equal(2, len(bd)) |
| 1004 | |
| 1005 | let bd2 = getbufvar(bnr, '', def_num) |
| 1006 | call assert_equal(bd, bd2) |
| 1007 | |
| 1008 | unlet b:var_num |
| 1009 | call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num)) |
| 1010 | call assert_equal('', getbufvar(bnr, 'var_num')) |
| 1011 | |
| 1012 | let bd = getbufvar(bnr, '') |
| 1013 | call assert_equal(1, len(bd)) |
| 1014 | let bd = getbufvar(bnr, '',def_num) |
| 1015 | call assert_equal(1, len(bd)) |
| 1016 | |
Bram Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 1017 | call assert_equal('', getbufvar(9999, '')) |
| 1018 | call assert_equal(def_num, getbufvar(9999, '', def_num)) |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 1019 | unlet def_num |
| 1020 | |
Bram Moolenaar | 507647d | 2017-02-17 16:43:49 +0100 | [diff] [blame] | 1021 | call assert_equal(0, getbufvar(bnr, '&autoindent')) |
| 1022 | call assert_equal(0, getbufvar(bnr, '&autoindent', 1)) |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 1023 | |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 1024 | " Set and get a buffer-local variable |
| 1025 | call setbufvar(bnr, 'bufvar_test', ['one', 'two']) |
| 1026 | call assert_equal(['one', 'two'], getbufvar(bnr, 'bufvar_test')) |
| 1027 | |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 1028 | " Open new window with forced option values |
| 1029 | set fileformats=unix,dos |
| 1030 | new ++ff=dos ++bin ++enc=iso-8859-2 |
| 1031 | call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat')) |
| 1032 | call assert_equal(1, getbufvar(bufnr('%'), '&bin')) |
| 1033 | call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc')) |
| 1034 | close |
| 1035 | |
Bram Moolenaar | 5259275 | 2020-04-03 18:43:35 +0200 | [diff] [blame] | 1036 | " Get the b: dict. |
| 1037 | let b:testvar = 'one' |
| 1038 | new |
| 1039 | let b:testvar = 'two' |
| 1040 | let thebuf = bufnr() |
| 1041 | wincmd w |
| 1042 | call assert_equal('two', getbufvar(thebuf, 'testvar')) |
| 1043 | call assert_equal('two', getbufvar(thebuf, '').testvar) |
| 1044 | bwipe! |
| 1045 | |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 1046 | set fileformats& |
| 1047 | endfunc |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 1048 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1049 | func Test_last_buffer_nr() |
| 1050 | call assert_equal(bufnr('$'), last_buffer_nr()) |
| 1051 | endfunc |
| 1052 | |
| 1053 | func Test_stridx() |
| 1054 | call assert_equal(-1, stridx('', 'l')) |
| 1055 | call assert_equal(0, stridx('', '')) |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 1056 | call assert_equal(0, 'hello'->stridx('')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1057 | call assert_equal(-1, stridx('hello', 'L')) |
| 1058 | call assert_equal(2, stridx('hello', 'l', -1)) |
| 1059 | call assert_equal(2, stridx('hello', 'l', 0)) |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 1060 | call assert_equal(2, 'hello'->stridx('l', 1)) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1061 | call assert_equal(3, stridx('hello', 'l', 3)) |
| 1062 | call assert_equal(-1, stridx('hello', 'l', 4)) |
| 1063 | call assert_equal(-1, stridx('hello', 'l', 10)) |
| 1064 | call assert_equal(2, stridx('hello', 'll')) |
| 1065 | call assert_equal(-1, stridx('hello', 'hello world')) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 1066 | call assert_fails("let n=stridx('hello', [])", 'E730:') |
| 1067 | call assert_fails("let n=stridx([], 'l')", 'E730:') |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1068 | endfunc |
| 1069 | |
| 1070 | func Test_strridx() |
| 1071 | call assert_equal(-1, strridx('', 'l')) |
| 1072 | call assert_equal(0, strridx('', '')) |
| 1073 | call assert_equal(5, strridx('hello', '')) |
| 1074 | call assert_equal(-1, strridx('hello', 'L')) |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 1075 | call assert_equal(3, 'hello'->strridx('l')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1076 | call assert_equal(3, strridx('hello', 'l', 10)) |
| 1077 | call assert_equal(3, strridx('hello', 'l', 3)) |
| 1078 | call assert_equal(2, strridx('hello', 'l', 2)) |
| 1079 | call assert_equal(-1, strridx('hello', 'l', 1)) |
| 1080 | call assert_equal(-1, strridx('hello', 'l', 0)) |
| 1081 | call assert_equal(-1, strridx('hello', 'l', -1)) |
| 1082 | call assert_equal(2, strridx('hello', 'll')) |
| 1083 | call assert_equal(-1, strridx('hello', 'hello world')) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 1084 | call assert_fails("let n=strridx('hello', [])", 'E730:') |
| 1085 | call assert_fails("let n=strridx([], 'l')", 'E730:') |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1086 | endfunc |
| 1087 | |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1088 | func Test_match_func() |
| 1089 | call assert_equal(4, match('testing', 'ing')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 1090 | call assert_equal(4, 'testing'->match('ing', 2)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1091 | call assert_equal(-1, match('testing', 'ing', 5)) |
| 1092 | call assert_equal(-1, match('testing', 'ing', 8)) |
| 1093 | call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing')) |
| 1094 | call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img')) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 1095 | call assert_fails("let x=match('vim', [])", 'E730:') |
| 1096 | call assert_equal(3, match(['a', 'b', 'c', 'a'], 'a', 1)) |
| 1097 | call assert_equal(-1, match(['a', 'b', 'c', 'a'], 'a', 5)) |
| 1098 | call assert_equal(4, match('testing', 'ing', -1)) |
| 1099 | call assert_fails("let x=match('testing', 'ing', 0, [])", 'E745:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 1100 | call assert_equal(-1, match(test_null_list(), 2)) |
Bram Moolenaar | 531be47 | 2020-09-23 22:38:05 +0200 | [diff] [blame] | 1101 | call assert_equal(-1, match('abc', '\\%(')) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1102 | endfunc |
| 1103 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1104 | func Test_matchend() |
| 1105 | call assert_equal(7, matchend('testing', 'ing')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 1106 | call assert_equal(7, 'testing'->matchend('ing', 2)) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1107 | call assert_equal(-1, matchend('testing', 'ing', 5)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1108 | call assert_equal(-1, matchend('testing', 'ing', 8)) |
| 1109 | call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing')) |
| 1110 | call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img')) |
| 1111 | endfunc |
| 1112 | |
| 1113 | func Test_matchlist() |
| 1114 | call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 1115 | call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1116 | call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4)) |
| 1117 | endfunc |
| 1118 | |
| 1119 | func Test_matchstr() |
| 1120 | call assert_equal('ing', matchstr('testing', 'ing')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 1121 | call assert_equal('ing', 'testing'->matchstr('ing', 2)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1122 | call assert_equal('', matchstr('testing', 'ing', 5)) |
| 1123 | call assert_equal('', matchstr('testing', 'ing', 8)) |
| 1124 | call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing')) |
| 1125 | call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img')) |
| 1126 | endfunc |
| 1127 | |
| 1128 | func Test_matchstrpos() |
| 1129 | call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 1130 | call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1131 | call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5)) |
| 1132 | call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8)) |
| 1133 | call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing')) |
| 1134 | call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img')) |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 1135 | call assert_equal(['', -1, -1], matchstrpos(test_null_list(), '\a')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1136 | endfunc |
| 1137 | |
| 1138 | func Test_nextnonblank_prevnonblank() |
| 1139 | new |
| 1140 | insert |
| 1141 | This |
| 1142 | |
| 1143 | |
| 1144 | is |
| 1145 | |
| 1146 | a |
| 1147 | Test |
| 1148 | . |
| 1149 | call assert_equal(0, nextnonblank(-1)) |
| 1150 | call assert_equal(0, nextnonblank(0)) |
| 1151 | call assert_equal(1, nextnonblank(1)) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 1152 | call assert_equal(4, 2->nextnonblank()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1153 | call assert_equal(4, nextnonblank(3)) |
| 1154 | call assert_equal(4, nextnonblank(4)) |
| 1155 | call assert_equal(6, nextnonblank(5)) |
| 1156 | call assert_equal(6, nextnonblank(6)) |
| 1157 | call assert_equal(7, nextnonblank(7)) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 1158 | call assert_equal(0, 8->nextnonblank()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1159 | |
| 1160 | call assert_equal(0, prevnonblank(-1)) |
| 1161 | call assert_equal(0, prevnonblank(0)) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 1162 | call assert_equal(1, 1->prevnonblank()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1163 | call assert_equal(1, prevnonblank(2)) |
| 1164 | call assert_equal(1, prevnonblank(3)) |
| 1165 | call assert_equal(4, prevnonblank(4)) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 1166 | call assert_equal(4, 5->prevnonblank()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1167 | call assert_equal(6, prevnonblank(6)) |
| 1168 | call assert_equal(7, prevnonblank(7)) |
| 1169 | call assert_equal(0, prevnonblank(8)) |
| 1170 | bw! |
| 1171 | endfunc |
| 1172 | |
| 1173 | func Test_byte2line_line2byte() |
| 1174 | new |
Bram Moolenaar | c26f7c6 | 2018-08-20 22:53:04 +0200 | [diff] [blame] | 1175 | set endofline |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1176 | call setline(1, ['a', 'bc', 'd']) |
| 1177 | |
| 1178 | set fileformat=unix |
| 1179 | call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1], |
| 1180 | \ map(range(-1, 8), 'byte2line(v:val)')) |
| 1181 | call assert_equal([-1, -1, 1, 3, 6, 8, -1], |
| 1182 | \ map(range(-1, 5), 'line2byte(v:val)')) |
| 1183 | |
| 1184 | set fileformat=mac |
| 1185 | 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] | 1186 | \ map(range(-1, 8), 'v:val->byte2line()')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1187 | call assert_equal([-1, -1, 1, 3, 6, 8, -1], |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 1188 | \ map(range(-1, 5), 'v:val->line2byte()')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1189 | |
| 1190 | set fileformat=dos |
| 1191 | call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1], |
| 1192 | \ map(range(-1, 11), 'byte2line(v:val)')) |
| 1193 | call assert_equal([-1, -1, 1, 4, 8, 11, -1], |
| 1194 | \ map(range(-1, 5), 'line2byte(v:val)')) |
| 1195 | |
Bram Moolenaar | c26f7c6 | 2018-08-20 22:53:04 +0200 | [diff] [blame] | 1196 | bw! |
| 1197 | set noendofline nofixendofline |
| 1198 | normal a- |
| 1199 | for ff in ["unix", "mac", "dos"] |
| 1200 | let &fileformat = ff |
| 1201 | call assert_equal(1, line2byte(1)) |
| 1202 | call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte). |
| 1203 | endfor |
| 1204 | |
| 1205 | set endofline& fixendofline& fileformat& |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1206 | bw! |
| 1207 | endfunc |
| 1208 | |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1209 | " Test for byteidx() using a character index |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1210 | func Test_byteidx() |
| 1211 | let a = '.é.' " one char of two bytes |
| 1212 | call assert_equal(0, byteidx(a, 0)) |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1213 | call assert_equal(1, byteidx(a, 1)) |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1214 | call assert_equal(3, byteidx(a, 2)) |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1215 | call assert_equal(4, byteidx(a, 3)) |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1216 | call assert_equal(-1, byteidx(a, 4)) |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1217 | |
| 1218 | let b = '.é.' " normal e with composing char |
| 1219 | call assert_equal(0, b->byteidx(0)) |
| 1220 | call assert_equal(1, b->byteidx(1)) |
| 1221 | call assert_equal(4, b->byteidx(2)) |
| 1222 | call assert_equal(5, b->byteidx(3)) |
| 1223 | call assert_equal(-1, b->byteidx(4)) |
| 1224 | |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1225 | " string with multiple composing characters |
| 1226 | let str = '-ą́-ą́' |
| 1227 | call assert_equal(0, byteidx(str, 0)) |
| 1228 | call assert_equal(1, byteidx(str, 1)) |
| 1229 | call assert_equal(6, byteidx(str, 2)) |
| 1230 | call assert_equal(7, byteidx(str, 3)) |
| 1231 | call assert_equal(12, byteidx(str, 4)) |
| 1232 | call assert_equal(-1, byteidx(str, 5)) |
| 1233 | |
| 1234 | " empty string |
| 1235 | call assert_equal(0, byteidx('', 0)) |
| 1236 | call assert_equal(-1, byteidx('', 1)) |
| 1237 | |
| 1238 | " error cases |
| 1239 | call assert_fails("call byteidx([], 0)", 'E730:') |
| 1240 | call assert_fails("call byteidx('abc', [])", 'E745:') |
Bram Moolenaar | e409845 | 2023-05-07 18:53:49 +0100 | [diff] [blame] | 1241 | call assert_fails("call byteidx('abc', 0, {})", ['E728:', 'E728:']) |
zeertzjq | 8cf5137 | 2023-05-08 15:31:38 +0100 | [diff] [blame] | 1242 | call assert_fails("call byteidx('abc', 0, -1)", ['E1023:', 'E1023:']) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1243 | endfunc |
| 1244 | |
| 1245 | " Test for byteidxcomp() using a character index |
| 1246 | func Test_byteidxcomp() |
| 1247 | let a = '.é.' " one char of two bytes |
| 1248 | call assert_equal(0, byteidxcomp(a, 0)) |
| 1249 | call assert_equal(1, byteidxcomp(a, 1)) |
| 1250 | call assert_equal(3, byteidxcomp(a, 2)) |
| 1251 | call assert_equal(4, byteidxcomp(a, 3)) |
| 1252 | call assert_equal(-1, byteidxcomp(a, 4)) |
| 1253 | |
| 1254 | let b = '.é.' " normal e with composing char |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1255 | call assert_equal(0, b->byteidxcomp(0)) |
| 1256 | call assert_equal(1, b->byteidxcomp(1)) |
| 1257 | call assert_equal(2, b->byteidxcomp(2)) |
| 1258 | call assert_equal(4, b->byteidxcomp(3)) |
| 1259 | call assert_equal(5, b->byteidxcomp(4)) |
| 1260 | call assert_equal(-1, b->byteidxcomp(5)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1261 | |
| 1262 | " string with multiple composing characters |
| 1263 | let str = '-ą́-ą́' |
| 1264 | call assert_equal(0, byteidxcomp(str, 0)) |
| 1265 | call assert_equal(1, byteidxcomp(str, 1)) |
| 1266 | call assert_equal(2, byteidxcomp(str, 2)) |
| 1267 | call assert_equal(4, byteidxcomp(str, 3)) |
| 1268 | call assert_equal(6, byteidxcomp(str, 4)) |
| 1269 | call assert_equal(7, byteidxcomp(str, 5)) |
| 1270 | call assert_equal(8, byteidxcomp(str, 6)) |
| 1271 | call assert_equal(10, byteidxcomp(str, 7)) |
| 1272 | call assert_equal(12, byteidxcomp(str, 8)) |
| 1273 | call assert_equal(-1, byteidxcomp(str, 9)) |
| 1274 | |
| 1275 | " empty string |
| 1276 | call assert_equal(0, byteidxcomp('', 0)) |
| 1277 | call assert_equal(-1, byteidxcomp('', 1)) |
| 1278 | |
| 1279 | " error cases |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 1280 | call assert_fails("call byteidxcomp([], 0)", 'E730:') |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1281 | call assert_fails("call byteidxcomp('abc', [])", 'E745:') |
Bram Moolenaar | e409845 | 2023-05-07 18:53:49 +0100 | [diff] [blame] | 1282 | call assert_fails("call byteidxcomp('abc', 0, {})", ['E728:', 'E728:']) |
zeertzjq | 8cf5137 | 2023-05-08 15:31:38 +0100 | [diff] [blame] | 1283 | call assert_fails("call byteidxcomp('abc', 0, -1)", ['E1023:', 'E1023:']) |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1284 | endfunc |
| 1285 | |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1286 | " Test for byteidx() using a UTF-16 index |
| 1287 | func Test_byteidx_from_utf16_index() |
| 1288 | " string with single byte characters |
| 1289 | let str = "abc" |
| 1290 | for i in range(3) |
| 1291 | call assert_equal(i, byteidx(str, i, v:true)) |
| 1292 | endfor |
| 1293 | call assert_equal(3, byteidx(str, 3, v:true)) |
| 1294 | call assert_equal(-1, byteidx(str, 4, v:true)) |
| 1295 | |
| 1296 | " string with two byte characters |
| 1297 | let str = "a©©b" |
| 1298 | call assert_equal(0, byteidx(str, 0, v:true)) |
| 1299 | call assert_equal(1, byteidx(str, 1, v:true)) |
| 1300 | call assert_equal(3, byteidx(str, 2, v:true)) |
| 1301 | call assert_equal(5, byteidx(str, 3, v:true)) |
| 1302 | call assert_equal(6, byteidx(str, 4, v:true)) |
| 1303 | call assert_equal(-1, byteidx(str, 5, v:true)) |
| 1304 | |
| 1305 | " string with two byte characters |
| 1306 | let str = "a😊😊b" |
| 1307 | call assert_equal(0, byteidx(str, 0, v:true)) |
| 1308 | call assert_equal(1, byteidx(str, 1, v:true)) |
| 1309 | call assert_equal(1, byteidx(str, 2, v:true)) |
| 1310 | call assert_equal(5, byteidx(str, 3, v:true)) |
| 1311 | call assert_equal(5, byteidx(str, 4, v:true)) |
| 1312 | call assert_equal(9, byteidx(str, 5, v:true)) |
| 1313 | call assert_equal(10, byteidx(str, 6, v:true)) |
| 1314 | call assert_equal(-1, byteidx(str, 7, v:true)) |
| 1315 | |
| 1316 | " string with composing characters |
| 1317 | let str = '-á-b́' |
| 1318 | call assert_equal(0, byteidx(str, 0, v:true)) |
| 1319 | call assert_equal(1, byteidx(str, 1, v:true)) |
| 1320 | call assert_equal(4, byteidx(str, 2, v:true)) |
| 1321 | call assert_equal(5, byteidx(str, 3, v:true)) |
| 1322 | call assert_equal(8, byteidx(str, 4, v:true)) |
| 1323 | call assert_equal(-1, byteidx(str, 5, v:true)) |
| 1324 | |
| 1325 | " string with multiple composing characters |
| 1326 | let str = '-ą́-ą́' |
| 1327 | call assert_equal(0, byteidx(str, 0, v:true)) |
| 1328 | call assert_equal(1, byteidx(str, 1, v:true)) |
| 1329 | call assert_equal(6, byteidx(str, 2, v:true)) |
| 1330 | call assert_equal(7, byteidx(str, 3, v:true)) |
| 1331 | call assert_equal(12, byteidx(str, 4, v:true)) |
| 1332 | call assert_equal(-1, byteidx(str, 5, v:true)) |
| 1333 | |
| 1334 | " empty string |
| 1335 | call assert_equal(0, byteidx('', 0, v:true)) |
| 1336 | call assert_equal(-1, byteidx('', 1, v:true)) |
| 1337 | |
| 1338 | " error cases |
| 1339 | call assert_fails('call byteidx(str, 0, [])', 'E745:') |
| 1340 | endfunc |
| 1341 | |
| 1342 | " Test for byteidxcomp() using a UTF-16 index |
| 1343 | func Test_byteidxcomp_from_utf16_index() |
| 1344 | " string with single byte characters |
| 1345 | let str = "abc" |
| 1346 | for i in range(3) |
| 1347 | call assert_equal(i, byteidxcomp(str, i, v:true)) |
| 1348 | endfor |
| 1349 | call assert_equal(3, byteidxcomp(str, 3, v:true)) |
| 1350 | call assert_equal(-1, byteidxcomp(str, 4, v:true)) |
| 1351 | |
| 1352 | " string with two byte characters |
| 1353 | let str = "a©©b" |
| 1354 | call assert_equal(0, byteidxcomp(str, 0, v:true)) |
| 1355 | call assert_equal(1, byteidxcomp(str, 1, v:true)) |
| 1356 | call assert_equal(3, byteidxcomp(str, 2, v:true)) |
| 1357 | call assert_equal(5, byteidxcomp(str, 3, v:true)) |
| 1358 | call assert_equal(6, byteidxcomp(str, 4, v:true)) |
| 1359 | call assert_equal(-1, byteidxcomp(str, 5, v:true)) |
| 1360 | |
| 1361 | " string with two byte characters |
| 1362 | let str = "a😊😊b" |
| 1363 | call assert_equal(0, byteidxcomp(str, 0, v:true)) |
| 1364 | call assert_equal(1, byteidxcomp(str, 1, v:true)) |
| 1365 | call assert_equal(1, byteidxcomp(str, 2, v:true)) |
| 1366 | call assert_equal(5, byteidxcomp(str, 3, v:true)) |
| 1367 | call assert_equal(5, byteidxcomp(str, 4, v:true)) |
| 1368 | call assert_equal(9, byteidxcomp(str, 5, v:true)) |
| 1369 | call assert_equal(10, byteidxcomp(str, 6, v:true)) |
| 1370 | call assert_equal(-1, byteidxcomp(str, 7, v:true)) |
| 1371 | |
| 1372 | " string with composing characters |
| 1373 | let str = '-á-b́' |
| 1374 | call assert_equal(0, byteidxcomp(str, 0, v:true)) |
| 1375 | call assert_equal(1, byteidxcomp(str, 1, v:true)) |
| 1376 | call assert_equal(2, byteidxcomp(str, 2, v:true)) |
| 1377 | call assert_equal(4, byteidxcomp(str, 3, v:true)) |
| 1378 | call assert_equal(5, byteidxcomp(str, 4, v:true)) |
| 1379 | call assert_equal(6, byteidxcomp(str, 5, v:true)) |
| 1380 | call assert_equal(8, byteidxcomp(str, 6, v:true)) |
| 1381 | call assert_equal(-1, byteidxcomp(str, 7, v:true)) |
| 1382 | call assert_fails('call byteidxcomp(str, 0, [])', 'E745:') |
| 1383 | |
| 1384 | " string with multiple composing characters |
| 1385 | let str = '-ą́-ą́' |
| 1386 | call assert_equal(0, byteidxcomp(str, 0, v:true)) |
| 1387 | call assert_equal(1, byteidxcomp(str, 1, v:true)) |
| 1388 | call assert_equal(2, byteidxcomp(str, 2, v:true)) |
| 1389 | call assert_equal(4, byteidxcomp(str, 3, v:true)) |
| 1390 | call assert_equal(6, byteidxcomp(str, 4, v:true)) |
| 1391 | call assert_equal(7, byteidxcomp(str, 5, v:true)) |
| 1392 | call assert_equal(8, byteidxcomp(str, 6, v:true)) |
| 1393 | call assert_equal(10, byteidxcomp(str, 7, v:true)) |
| 1394 | call assert_equal(12, byteidxcomp(str, 8, v:true)) |
| 1395 | call assert_equal(-1, byteidxcomp(str, 9, v:true)) |
| 1396 | |
| 1397 | " empty string |
| 1398 | call assert_equal(0, byteidxcomp('', 0, v:true)) |
| 1399 | call assert_equal(-1, byteidxcomp('', 1, v:true)) |
| 1400 | |
| 1401 | " error cases |
| 1402 | call assert_fails('call byteidxcomp(str, 0, [])', 'E745:') |
| 1403 | endfunc |
| 1404 | |
| 1405 | " Test for charidx() using a byte index |
Bram Moolenaar | 17793ef | 2020-12-28 12:56:58 +0100 | [diff] [blame] | 1406 | func Test_charidx() |
| 1407 | let a = 'xáb́y' |
| 1408 | call assert_equal(0, charidx(a, 0)) |
| 1409 | call assert_equal(1, charidx(a, 3)) |
| 1410 | call assert_equal(2, charidx(a, 4)) |
| 1411 | call assert_equal(3, charidx(a, 7)) |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1412 | call assert_equal(4, charidx(a, 8)) |
| 1413 | call assert_equal(-1, charidx(a, 9)) |
Dominique Pelle | 6d37e8e | 2021-05-06 17:36:55 +0200 | [diff] [blame] | 1414 | call assert_equal(-1, charidx(a, -1)) |
Bram Moolenaar | 17793ef | 2020-12-28 12:56:58 +0100 | [diff] [blame] | 1415 | |
| 1416 | " count composing characters |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1417 | call assert_equal(0, a->charidx(0, 1)) |
| 1418 | call assert_equal(2, a->charidx(2, 1)) |
| 1419 | call assert_equal(3, a->charidx(4, 1)) |
| 1420 | call assert_equal(5, a->charidx(7, 1)) |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1421 | call assert_equal(6, a->charidx(8, 1)) |
| 1422 | call assert_equal(-1, a->charidx(9, 1)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1423 | |
| 1424 | " empty string |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1425 | call assert_equal(0, charidx('', 0)) |
| 1426 | call assert_equal(-1, charidx('', 1)) |
| 1427 | call assert_equal(0, charidx('', 0, 1)) |
| 1428 | call assert_equal(-1, charidx('', 1, 1)) |
Bram Moolenaar | 17793ef | 2020-12-28 12:56:58 +0100 | [diff] [blame] | 1429 | |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1430 | " error cases |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1431 | call assert_equal(0, charidx(test_null_string(), 0)) |
| 1432 | call assert_equal(-1, charidx(test_null_string(), 1)) |
Yegappan Lakshmanan | 8deb2b3 | 2022-09-02 15:15:27 +0100 | [diff] [blame] | 1433 | call assert_fails('let x = charidx([], 1)', 'E1174:') |
| 1434 | call assert_fails('let x = charidx("abc", [])', 'E1210:') |
| 1435 | call assert_fails('let x = charidx("abc", 1, [])', 'E1212:') |
| 1436 | call assert_fails('let x = charidx("abc", 1, -1)', 'E1212:') |
| 1437 | call assert_fails('let x = charidx("abc", 1, 2)', 'E1212:') |
Bram Moolenaar | 17793ef | 2020-12-28 12:56:58 +0100 | [diff] [blame] | 1438 | endfunc |
| 1439 | |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1440 | " Test for charidx() using a UTF-16 index |
| 1441 | func Test_charidx_from_utf16_index() |
| 1442 | " string with single byte characters |
| 1443 | let str = "abc" |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1444 | for i in range(4) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1445 | call assert_equal(i, charidx(str, i, v:false, v:true)) |
| 1446 | endfor |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1447 | call assert_equal(-1, charidx(str, 4, v:false, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1448 | |
| 1449 | " string with two byte characters |
| 1450 | let str = "a©©b" |
| 1451 | call assert_equal(0, charidx(str, 0, v:false, v:true)) |
| 1452 | call assert_equal(1, charidx(str, 1, v:false, v:true)) |
| 1453 | call assert_equal(2, charidx(str, 2, v:false, v:true)) |
| 1454 | call assert_equal(3, charidx(str, 3, v:false, v:true)) |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1455 | call assert_equal(4, charidx(str, 4, v:false, v:true)) |
| 1456 | call assert_equal(-1, charidx(str, 5, v:false, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1457 | |
| 1458 | " string with four byte characters |
| 1459 | let str = "a😊😊b" |
| 1460 | call assert_equal(0, charidx(str, 0, v:false, v:true)) |
| 1461 | call assert_equal(1, charidx(str, 1, v:false, v:true)) |
| 1462 | call assert_equal(1, charidx(str, 2, v:false, v:true)) |
| 1463 | call assert_equal(2, charidx(str, 3, v:false, v:true)) |
| 1464 | call assert_equal(2, charidx(str, 4, v:false, v:true)) |
| 1465 | call assert_equal(3, charidx(str, 5, v:false, v:true)) |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1466 | call assert_equal(4, charidx(str, 6, v:false, v:true)) |
| 1467 | call assert_equal(-1, charidx(str, 7, v:false, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1468 | |
| 1469 | " string with composing characters |
| 1470 | let str = '-á-b́' |
| 1471 | for i in str->strcharlen()->range() |
| 1472 | call assert_equal(i, charidx(str, i, v:false, v:true)) |
| 1473 | endfor |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1474 | call assert_equal(4, charidx(str, 4, v:false, v:true)) |
| 1475 | call assert_equal(-1, charidx(str, 5, v:false, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1476 | for i in str->strchars()->range() |
| 1477 | call assert_equal(i, charidx(str, i, v:true, v:true)) |
| 1478 | endfor |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1479 | call assert_equal(6, charidx(str, 6, v:true, v:true)) |
| 1480 | call assert_equal(-1, charidx(str, 7, v:true, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1481 | |
| 1482 | " string with multiple composing characters |
| 1483 | let str = '-ą́-ą́' |
| 1484 | for i in str->strcharlen()->range() |
| 1485 | call assert_equal(i, charidx(str, i, v:false, v:true)) |
| 1486 | endfor |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1487 | call assert_equal(4, charidx(str, 4, v:false, v:true)) |
| 1488 | call assert_equal(-1, charidx(str, 5, v:false, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1489 | for i in str->strchars()->range() |
| 1490 | call assert_equal(i, charidx(str, i, v:true, v:true)) |
| 1491 | endfor |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1492 | call assert_equal(8, charidx(str, 8, v:true, v:true)) |
| 1493 | call assert_equal(-1, charidx(str, 9, v:true, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1494 | |
| 1495 | " empty string |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1496 | call assert_equal(0, charidx('', 0, v:false, v:true)) |
| 1497 | call assert_equal(-1, charidx('', 1, v:false, v:true)) |
| 1498 | call assert_equal(0, charidx('', 0, v:true, v:true)) |
| 1499 | call assert_equal(-1, charidx('', 1, v:true, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1500 | |
| 1501 | " error cases |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1502 | call assert_equal(0, charidx('', 0, v:false, v:true)) |
| 1503 | call assert_equal(-1, charidx('', 1, v:false, v:true)) |
| 1504 | call assert_equal(0, charidx('', 0, v:true, v:true)) |
| 1505 | call assert_equal(-1, charidx('', 1, v:true, v:true)) |
| 1506 | call assert_equal(0, charidx(test_null_string(), 0, v:false, v:true)) |
| 1507 | call assert_equal(-1, charidx(test_null_string(), 1, v:false, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1508 | call assert_fails('let x = charidx("abc", 1, v:false, [])', 'E1212:') |
| 1509 | call assert_fails('let x = charidx("abc", 1, v:true, [])', 'E1212:') |
| 1510 | endfunc |
| 1511 | |
| 1512 | " Test for utf16idx() using a byte index |
| 1513 | func Test_utf16idx_from_byteidx() |
| 1514 | " UTF-16 index of a string with single byte characters |
| 1515 | let str = "abc" |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1516 | for i in range(4) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1517 | call assert_equal(i, utf16idx(str, i)) |
| 1518 | endfor |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1519 | call assert_equal(-1, utf16idx(str, 4)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1520 | |
| 1521 | " UTF-16 index of a string with two byte characters |
| 1522 | let str = 'a©©b' |
| 1523 | call assert_equal(0, str->utf16idx(0)) |
| 1524 | call assert_equal(1, str->utf16idx(1)) |
| 1525 | call assert_equal(1, str->utf16idx(2)) |
| 1526 | call assert_equal(2, str->utf16idx(3)) |
| 1527 | call assert_equal(2, str->utf16idx(4)) |
| 1528 | call assert_equal(3, str->utf16idx(5)) |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1529 | call assert_equal(4, str->utf16idx(6)) |
| 1530 | call assert_equal(-1, str->utf16idx(7)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1531 | |
| 1532 | " UTF-16 index of a string with four byte characters |
| 1533 | let str = 'a😊😊b' |
| 1534 | call assert_equal(0, utf16idx(str, 0)) |
Yegappan Lakshmanan | 9570703 | 2023-06-14 13:10:15 +0100 | [diff] [blame] | 1535 | call assert_equal(1, utf16idx(str, 1)) |
| 1536 | call assert_equal(1, utf16idx(str, 2)) |
| 1537 | call assert_equal(1, utf16idx(str, 3)) |
| 1538 | call assert_equal(1, utf16idx(str, 4)) |
| 1539 | call assert_equal(3, utf16idx(str, 5)) |
| 1540 | call assert_equal(3, utf16idx(str, 6)) |
| 1541 | call assert_equal(3, utf16idx(str, 7)) |
| 1542 | call assert_equal(3, utf16idx(str, 8)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1543 | call assert_equal(5, utf16idx(str, 9)) |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1544 | call assert_equal(6, utf16idx(str, 10)) |
| 1545 | call assert_equal(-1, utf16idx(str, 11)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1546 | |
| 1547 | " UTF-16 index of a string with composing characters |
| 1548 | let str = '-á-b́' |
| 1549 | call assert_equal(0, utf16idx(str, 0)) |
| 1550 | call assert_equal(1, utf16idx(str, 1)) |
| 1551 | call assert_equal(1, utf16idx(str, 2)) |
| 1552 | call assert_equal(1, utf16idx(str, 3)) |
| 1553 | call assert_equal(2, utf16idx(str, 4)) |
| 1554 | call assert_equal(3, utf16idx(str, 5)) |
| 1555 | call assert_equal(3, utf16idx(str, 6)) |
| 1556 | call assert_equal(3, utf16idx(str, 7)) |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1557 | call assert_equal(4, utf16idx(str, 8)) |
| 1558 | call assert_equal(-1, utf16idx(str, 9)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1559 | call assert_equal(0, utf16idx(str, 0, v:true)) |
| 1560 | call assert_equal(1, utf16idx(str, 1, v:true)) |
| 1561 | call assert_equal(2, utf16idx(str, 2, v:true)) |
| 1562 | call assert_equal(2, utf16idx(str, 3, v:true)) |
| 1563 | call assert_equal(3, utf16idx(str, 4, v:true)) |
| 1564 | call assert_equal(4, utf16idx(str, 5, v:true)) |
| 1565 | call assert_equal(5, utf16idx(str, 6, v:true)) |
| 1566 | call assert_equal(5, utf16idx(str, 7, v:true)) |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1567 | call assert_equal(6, utf16idx(str, 8, v:true)) |
| 1568 | call assert_equal(-1, utf16idx(str, 9, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1569 | |
| 1570 | " string with multiple composing characters |
| 1571 | let str = '-ą́-ą́' |
| 1572 | call assert_equal(0, utf16idx(str, 0)) |
| 1573 | call assert_equal(1, utf16idx(str, 1)) |
| 1574 | call assert_equal(1, utf16idx(str, 2)) |
| 1575 | call assert_equal(1, utf16idx(str, 3)) |
| 1576 | call assert_equal(1, utf16idx(str, 4)) |
| 1577 | call assert_equal(1, utf16idx(str, 5)) |
| 1578 | call assert_equal(2, utf16idx(str, 6)) |
| 1579 | call assert_equal(3, utf16idx(str, 7)) |
| 1580 | call assert_equal(3, utf16idx(str, 8)) |
| 1581 | call assert_equal(3, utf16idx(str, 9)) |
| 1582 | call assert_equal(3, utf16idx(str, 10)) |
| 1583 | call assert_equal(3, utf16idx(str, 11)) |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1584 | call assert_equal(4, utf16idx(str, 12)) |
| 1585 | call assert_equal(-1, utf16idx(str, 13)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1586 | call assert_equal(0, utf16idx(str, 0, v:true)) |
| 1587 | call assert_equal(1, utf16idx(str, 1, v:true)) |
| 1588 | call assert_equal(2, utf16idx(str, 2, v:true)) |
| 1589 | call assert_equal(2, utf16idx(str, 3, v:true)) |
| 1590 | call assert_equal(3, utf16idx(str, 4, v:true)) |
| 1591 | call assert_equal(3, utf16idx(str, 5, v:true)) |
| 1592 | call assert_equal(4, utf16idx(str, 6, v:true)) |
| 1593 | call assert_equal(5, utf16idx(str, 7, v:true)) |
| 1594 | call assert_equal(6, utf16idx(str, 8, v:true)) |
| 1595 | call assert_equal(6, utf16idx(str, 9, v:true)) |
| 1596 | call assert_equal(7, utf16idx(str, 10, v:true)) |
| 1597 | call assert_equal(7, utf16idx(str, 11, v:true)) |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1598 | call assert_equal(8, utf16idx(str, 12, v:true)) |
| 1599 | call assert_equal(-1, utf16idx(str, 13, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1600 | |
| 1601 | " empty string |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1602 | call assert_equal(0, utf16idx('', 0)) |
| 1603 | call assert_equal(-1, utf16idx('', 1)) |
| 1604 | call assert_equal(0, utf16idx('', 0, v:true)) |
| 1605 | call assert_equal(-1, utf16idx('', 1, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1606 | |
| 1607 | " error cases |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1608 | call assert_equal(0, utf16idx("", 0)) |
| 1609 | call assert_equal(-1, utf16idx("", 1)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1610 | call assert_equal(-1, utf16idx("abc", -1)) |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1611 | call assert_equal(0, utf16idx(test_null_string(), 0)) |
| 1612 | call assert_equal(-1, utf16idx(test_null_string(), 1)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1613 | call assert_fails('let l = utf16idx([], 0)', 'E1174:') |
| 1614 | call assert_fails('let l = utf16idx("ab", [])', 'E1210:') |
| 1615 | call assert_fails('let l = utf16idx("ab", 0, [])', 'E1212:') |
| 1616 | endfunc |
| 1617 | |
| 1618 | " Test for utf16idx() using a character index |
| 1619 | func Test_utf16idx_from_charidx() |
| 1620 | let str = "abc" |
| 1621 | for i in str->strcharlen()->range() |
| 1622 | call assert_equal(i, utf16idx(str, i, v:false, v:true)) |
| 1623 | endfor |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1624 | call assert_equal(3, utf16idx(str, 3, v:false, v:true)) |
| 1625 | call assert_equal(-1, utf16idx(str, 4, v:false, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1626 | |
| 1627 | " UTF-16 index of a string with two byte characters |
| 1628 | let str = "a©©b" |
| 1629 | for i in str->strcharlen()->range() |
| 1630 | call assert_equal(i, utf16idx(str, i, v:false, v:true)) |
| 1631 | endfor |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1632 | call assert_equal(4, utf16idx(str, 4, v:false, v:true)) |
| 1633 | call assert_equal(-1, utf16idx(str, 5, v:false, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1634 | |
| 1635 | " UTF-16 index of a string with four byte characters |
| 1636 | let str = "a😊😊b" |
| 1637 | call assert_equal(0, utf16idx(str, 0, v:false, v:true)) |
Yegappan Lakshmanan | 9570703 | 2023-06-14 13:10:15 +0100 | [diff] [blame] | 1638 | call assert_equal(1, utf16idx(str, 1, v:false, v:true)) |
| 1639 | call assert_equal(3, utf16idx(str, 2, v:false, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1640 | call assert_equal(5, utf16idx(str, 3, v:false, v:true)) |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1641 | call assert_equal(6, utf16idx(str, 4, v:false, v:true)) |
| 1642 | call assert_equal(-1, utf16idx(str, 5, v:false, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1643 | |
| 1644 | " UTF-16 index of a string with composing characters |
| 1645 | let str = '-á-b́' |
| 1646 | for i in str->strcharlen()->range() |
| 1647 | call assert_equal(i, utf16idx(str, i, v:false, v:true)) |
| 1648 | endfor |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1649 | call assert_equal(4, utf16idx(str, 4, v:false, v:true)) |
| 1650 | call assert_equal(-1, utf16idx(str, 5, v:false, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1651 | for i in str->strchars()->range() |
| 1652 | call assert_equal(i, utf16idx(str, i, v:true, v:true)) |
| 1653 | endfor |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1654 | call assert_equal(6, utf16idx(str, 6, v:true, v:true)) |
| 1655 | call assert_equal(-1, utf16idx(str, 7, v:true, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1656 | |
| 1657 | " string with multiple composing characters |
| 1658 | let str = '-ą́-ą́' |
| 1659 | for i in str->strcharlen()->range() |
| 1660 | call assert_equal(i, utf16idx(str, i, v:false, v:true)) |
| 1661 | endfor |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1662 | call assert_equal(4, utf16idx(str, 4, v:false, v:true)) |
| 1663 | call assert_equal(-1, utf16idx(str, 5, v:false, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1664 | for i in str->strchars()->range() |
| 1665 | call assert_equal(i, utf16idx(str, i, v:true, v:true)) |
| 1666 | endfor |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1667 | call assert_equal(8, utf16idx(str, 8, v:true, v:true)) |
| 1668 | call assert_equal(-1, utf16idx(str, 9, v:true, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1669 | |
| 1670 | " empty string |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1671 | call assert_equal(0, utf16idx('', 0, v:false, v:true)) |
| 1672 | call assert_equal(-1, utf16idx('', 1, v:false, v:true)) |
| 1673 | call assert_equal(0, utf16idx('', 0, v:true, v:true)) |
| 1674 | call assert_equal(-1, utf16idx('', 1, v:true, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1675 | |
| 1676 | " error cases |
Yegappan Lakshmanan | 577922b | 2023-06-08 17:09:45 +0100 | [diff] [blame] | 1677 | call assert_equal(0, utf16idx(test_null_string(), 0, v:true, v:true)) |
| 1678 | call assert_equal(-1, utf16idx(test_null_string(), 1, v:true, v:true)) |
Christian Brabandt | 67672ef | 2023-04-24 21:09:54 +0100 | [diff] [blame] | 1679 | call assert_fails('let l = utf16idx("ab", 0, v:false, [])', 'E1212:') |
| 1680 | endfunc |
| 1681 | |
| 1682 | " Test for strutf16len() |
| 1683 | func Test_strutf16len() |
| 1684 | call assert_equal(3, strutf16len('abc')) |
| 1685 | call assert_equal(3, 'abc'->strutf16len(v:true)) |
| 1686 | call assert_equal(4, strutf16len('a©©b')) |
| 1687 | call assert_equal(4, strutf16len('a©©b', v:true)) |
| 1688 | call assert_equal(6, strutf16len('a😊😊b')) |
| 1689 | call assert_equal(6, strutf16len('a😊😊b', v:true)) |
| 1690 | call assert_equal(4, strutf16len('-á-b́')) |
| 1691 | call assert_equal(6, strutf16len('-á-b́', v:true)) |
| 1692 | call assert_equal(4, strutf16len('-ą́-ą́')) |
| 1693 | call assert_equal(8, strutf16len('-ą́-ą́', v:true)) |
| 1694 | call assert_equal(0, strutf16len('')) |
| 1695 | |
| 1696 | " error cases |
| 1697 | call assert_fails('let l = strutf16len([])', 'E1174:') |
| 1698 | call assert_fails('let l = strutf16len("a", [])', 'E1212:') |
| 1699 | call assert_equal(0, strutf16len(test_null_string())) |
| 1700 | endfunc |
| 1701 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1702 | func Test_count() |
| 1703 | let l = ['a', 'a', 'A', 'b'] |
| 1704 | call assert_equal(2, count(l, 'a')) |
| 1705 | call assert_equal(1, count(l, 'A')) |
| 1706 | call assert_equal(1, count(l, 'b')) |
| 1707 | call assert_equal(0, count(l, 'B')) |
| 1708 | |
| 1709 | call assert_equal(2, count(l, 'a', 0)) |
| 1710 | call assert_equal(1, count(l, 'A', 0)) |
| 1711 | call assert_equal(1, count(l, 'b', 0)) |
| 1712 | call assert_equal(0, count(l, 'B', 0)) |
| 1713 | |
| 1714 | call assert_equal(3, count(l, 'a', 1)) |
| 1715 | call assert_equal(3, count(l, 'A', 1)) |
| 1716 | call assert_equal(1, count(l, 'b', 1)) |
| 1717 | call assert_equal(1, count(l, 'B', 1)) |
| 1718 | call assert_equal(0, count(l, 'c', 1)) |
| 1719 | |
| 1720 | call assert_equal(1, count(l, 'a', 0, 1)) |
| 1721 | call assert_equal(2, count(l, 'a', 1, 1)) |
| 1722 | call assert_fails('call count(l, "a", 0, 10)', 'E684:') |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 1723 | call assert_fails('call count(l, "a", [])', 'E745:') |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1724 | |
| 1725 | let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'} |
| 1726 | call assert_equal(2, count(d, 'a')) |
| 1727 | call assert_equal(1, count(d, 'A')) |
| 1728 | call assert_equal(1, count(d, 'b')) |
| 1729 | call assert_equal(0, count(d, 'B')) |
| 1730 | |
| 1731 | call assert_equal(2, count(d, 'a', 0)) |
| 1732 | call assert_equal(1, count(d, 'A', 0)) |
| 1733 | call assert_equal(1, count(d, 'b', 0)) |
| 1734 | call assert_equal(0, count(d, 'B', 0)) |
| 1735 | |
| 1736 | call assert_equal(3, count(d, 'a', 1)) |
| 1737 | call assert_equal(3, count(d, 'A', 1)) |
| 1738 | call assert_equal(1, count(d, 'b', 1)) |
| 1739 | call assert_equal(1, count(d, 'B', 1)) |
| 1740 | call assert_equal(0, count(d, 'c', 1)) |
| 1741 | |
| 1742 | call assert_fails('call count(d, "a", 0, 1)', 'E474:') |
Bram Moolenaar | 9966b21 | 2017-07-28 16:46:57 +0200 | [diff] [blame] | 1743 | |
| 1744 | call assert_equal(0, count("foo", "bar")) |
| 1745 | call assert_equal(1, count("foo", "oo")) |
| 1746 | call assert_equal(2, count("foo", "o")) |
| 1747 | call assert_equal(0, count("foo", "O")) |
| 1748 | call assert_equal(2, count("foo", "O", 1)) |
| 1749 | call assert_equal(2, count("fooooo", "oo")) |
Bram Moolenaar | 338e47f | 2017-12-19 11:55:26 +0100 | [diff] [blame] | 1750 | call assert_equal(0, count("foo", "")) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 1751 | |
zeertzjq | 4f389e7 | 2023-08-17 22:10:40 +0200 | [diff] [blame] | 1752 | call assert_fails('call count(0, 0)', 'E706:') |
| 1753 | call assert_fails('call count("", "", {})', ['E728:', 'E728:']) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1754 | endfunc |
| 1755 | |
| 1756 | func Test_changenr() |
| 1757 | new Xchangenr |
| 1758 | call assert_equal(0, changenr()) |
| 1759 | norm ifoo |
| 1760 | call assert_equal(1, changenr()) |
| 1761 | set undolevels=10 |
| 1762 | norm Sbar |
| 1763 | call assert_equal(2, changenr()) |
| 1764 | undo |
| 1765 | call assert_equal(1, changenr()) |
| 1766 | redo |
| 1767 | call assert_equal(2, changenr()) |
| 1768 | bw! |
| 1769 | set undolevels& |
| 1770 | endfunc |
| 1771 | |
| 1772 | func Test_filewritable() |
| 1773 | new Xfilewritable |
| 1774 | write! |
| 1775 | call assert_equal(1, filewritable('Xfilewritable')) |
| 1776 | |
| 1777 | call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----')) |
| 1778 | call assert_equal(0, filewritable('Xfilewritable')) |
| 1779 | |
| 1780 | call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----')) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1781 | call assert_equal(1, 'Xfilewritable'->filewritable()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1782 | |
| 1783 | call assert_equal(0, filewritable('doesnotexist')) |
| 1784 | |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 1785 | call mkdir('Xwritedir', 'D') |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 1786 | call assert_equal(2, filewritable('Xwritedir')) |
Bram Moolenaar | 0ff5ded | 2020-05-07 18:43:44 +0200 | [diff] [blame] | 1787 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1788 | call delete('Xfilewritable') |
| 1789 | bw! |
| 1790 | endfunc |
| 1791 | |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 1792 | func Test_Executable() |
| 1793 | if has('win32') |
| 1794 | call assert_equal(1, executable('notepad')) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1795 | call assert_equal(1, 'notepad.exe'->executable()) |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 1796 | call assert_equal(0, executable('notepad.exe.exe')) |
| 1797 | call assert_equal(0, executable('shell32.dll')) |
| 1798 | call assert_equal(0, executable('win.ini')) |
Bram Moolenaar | 95da136 | 2020-05-30 18:37:55 +0200 | [diff] [blame] | 1799 | |
| 1800 | " get "notepad" path and remove the leading drive and sep. (ex. 'C:\') |
| 1801 | let notepadcmd = exepath('notepad.exe') |
| 1802 | let driveroot = notepadcmd[:2] |
| 1803 | let notepadcmd = notepadcmd[3:] |
| 1804 | new |
| 1805 | " check that the relative path works in / |
| 1806 | execute 'lcd' driveroot |
| 1807 | call assert_equal(1, executable(notepadcmd)) |
| 1808 | call assert_equal(driveroot .. notepadcmd, notepadcmd->exepath()) |
| 1809 | bwipe |
| 1810 | |
| 1811 | " create "notepad.bat" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 1812 | call mkdir('Xnotedir') |
| 1813 | let notepadbat = fnamemodify('Xnotedir/notepad.bat', ':p') |
Bram Moolenaar | 95da136 | 2020-05-30 18:37:55 +0200 | [diff] [blame] | 1814 | call writefile([], notepadbat) |
| 1815 | new |
| 1816 | " check that the path and the pathext order is valid |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 1817 | lcd Xnotedir |
Bram Moolenaar | 95da136 | 2020-05-30 18:37:55 +0200 | [diff] [blame] | 1818 | let [pathext, $PATHEXT] = [$PATHEXT, '.com;.exe;.bat;.cmd'] |
| 1819 | call assert_equal(notepadbat, exepath('notepad')) |
| 1820 | let $PATHEXT = pathext |
| 1821 | bwipe |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 1822 | eval 'Xnotedir'->delete('rf') |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 1823 | elseif has('unix') |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1824 | call assert_equal(1, 'cat'->executable()) |
Bram Moolenaar | a05a0d3 | 2018-10-07 18:43:05 +0200 | [diff] [blame] | 1825 | call assert_equal(0, executable('nodogshere')) |
Bram Moolenaar | d08b8c4 | 2019-07-24 14:59:45 +0200 | [diff] [blame] | 1826 | |
| 1827 | " get "cat" path and remove the leading / |
| 1828 | let catcmd = exepath('cat')[1:] |
| 1829 | new |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1830 | " check that the relative path works in / |
Bram Moolenaar | d08b8c4 | 2019-07-24 14:59:45 +0200 | [diff] [blame] | 1831 | lcd / |
| 1832 | call assert_equal(1, executable(catcmd)) |
Bram Moolenaar | a387083 | 2021-01-01 14:20:44 +0100 | [diff] [blame] | 1833 | let result = catcmd->exepath() |
| 1834 | " when using chroot looking for sbin/cat can return bin/cat, that is OK |
| 1835 | if catcmd =~ '\<sbin\>' && result =~ '\<bin\>' |
| 1836 | call assert_equal('/' .. substitute(catcmd, '\<sbin\>', 'bin', ''), result) |
| 1837 | else |
Bram Moolenaar | bf634a0 | 2021-07-31 17:20:04 +0200 | [diff] [blame] | 1838 | " /bin/cat and /usr/bin/cat may be hard linked, we could get either |
| 1839 | let result = substitute(result, '/usr/bin/cat', '/bin/cat', '') |
| 1840 | let catcmd = substitute(catcmd, 'usr/bin/cat', 'bin/cat', '') |
Bram Moolenaar | a387083 | 2021-01-01 14:20:44 +0100 | [diff] [blame] | 1841 | call assert_equal('/' .. catcmd, result) |
| 1842 | endif |
Bram Moolenaar | d08b8c4 | 2019-07-24 14:59:45 +0200 | [diff] [blame] | 1843 | bwipe |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1844 | else |
| 1845 | throw 'Skipped: does not work on this platform' |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 1846 | endif |
| 1847 | endfunc |
| 1848 | |
LemonBoy | 40fd7e6 | 2022-05-05 20:18:16 +0100 | [diff] [blame] | 1849 | func Test_executable_windows_store_apps() |
| 1850 | CheckMSWindows |
| 1851 | |
| 1852 | " Windows Store apps install some 'decoy' .exe that require some careful |
| 1853 | " handling as they behave similarly to symlinks. |
| 1854 | let app_dir = expand("$LOCALAPPDATA\\Microsoft\\WindowsApps") |
| 1855 | if !isdirectory(app_dir) |
| 1856 | return |
| 1857 | endif |
| 1858 | |
| 1859 | let save_path = $PATH |
| 1860 | let $PATH = app_dir |
| 1861 | " Ensure executable() finds all the app .exes |
| 1862 | for entry in readdir(app_dir) |
| 1863 | if entry =~ '\.exe$' |
| 1864 | call assert_true(executable(entry)) |
| 1865 | endif |
| 1866 | endfor |
| 1867 | |
| 1868 | let $PATH = save_path |
| 1869 | endfunc |
| 1870 | |
Bram Moolenaar | 8662189 | 2019-03-30 21:51:28 +0100 | [diff] [blame] | 1871 | func Test_executable_longname() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1872 | CheckMSWindows |
Bram Moolenaar | 8662189 | 2019-03-30 21:51:28 +0100 | [diff] [blame] | 1873 | |
Bram Moolenaar | f637bce | 2020-11-23 18:14:56 +0100 | [diff] [blame] | 1874 | " Create a temporary .bat file with 205 characters in the name. |
| 1875 | " Maximum length of a filename (including the path) on MS-Windows is 259 |
| 1876 | " characters. |
| 1877 | " See https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation |
| 1878 | let len = 259 - getcwd()->len() - 6 |
| 1879 | if len > 200 |
| 1880 | let len = 200 |
| 1881 | endif |
| 1882 | |
| 1883 | let fname = 'X' . repeat('あ', len) . '.bat' |
Bram Moolenaar | 8662189 | 2019-03-30 21:51:28 +0100 | [diff] [blame] | 1884 | call writefile([], fname) |
| 1885 | call assert_equal(1, executable(fname)) |
| 1886 | call delete(fname) |
| 1887 | endfunc |
| 1888 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1889 | func Test_hostname() |
| 1890 | let hostname_vim = hostname() |
| 1891 | if has('unix') |
| 1892 | let hostname_system = systemlist('uname -n')[0] |
| 1893 | call assert_equal(hostname_vim, hostname_system) |
| 1894 | endif |
| 1895 | endfunc |
| 1896 | |
| 1897 | func Test_getpid() |
| 1898 | " getpid() always returns the same value within a vim instance. |
| 1899 | call assert_equal(getpid(), getpid()) |
| 1900 | if has('unix') |
| 1901 | call assert_equal(systemlist('echo $PPID')[0], string(getpid())) |
| 1902 | endif |
| 1903 | endfunc |
| 1904 | |
| 1905 | func Test_hlexists() |
| 1906 | call assert_equal(0, hlexists('does_not_exist')) |
Bram Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 1907 | call assert_equal(0, 'Number'->hlexists()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1908 | call assert_equal(0, highlight_exists('does_not_exist')) |
| 1909 | call assert_equal(0, highlight_exists('Number')) |
| 1910 | syntax on |
| 1911 | call assert_equal(0, hlexists('does_not_exist')) |
| 1912 | call assert_equal(1, hlexists('Number')) |
| 1913 | call assert_equal(0, highlight_exists('does_not_exist')) |
| 1914 | call assert_equal(1, highlight_exists('Number')) |
| 1915 | syntax off |
| 1916 | endfunc |
| 1917 | |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 1918 | " Test for the col() function |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1919 | func Test_col() |
| 1920 | new |
| 1921 | call setline(1, 'abcdef') |
| 1922 | norm gg4|mx6|mY2| |
| 1923 | call assert_equal(2, col('.')) |
| 1924 | call assert_equal(7, col('$')) |
Bram Moolenaar | 8b63313 | 2020-03-20 18:20:51 +0100 | [diff] [blame] | 1925 | call assert_equal(2, col('v')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1926 | call assert_equal(4, col("'x")) |
| 1927 | call assert_equal(6, col("'Y")) |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 1928 | call assert_equal(2, [1, 2]->col()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1929 | call assert_equal(7, col([1, '$'])) |
| 1930 | |
| 1931 | call assert_equal(0, col('')) |
| 1932 | call assert_equal(0, col('x')) |
| 1933 | call assert_equal(0, col([2, '$'])) |
| 1934 | call assert_equal(0, col([1, 100])) |
| 1935 | call assert_equal(0, col([1])) |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 1936 | call assert_equal(0, col(test_null_list())) |
Yegappan Lakshmanan | 4c8d2f0 | 2022-11-12 16:07:47 +0000 | [diff] [blame] | 1937 | call assert_fails('let c = col({})', 'E1222:') |
| 1938 | call assert_fails('let c = col(".", [])', 'E1210:') |
Bram Moolenaar | 8b63313 | 2020-03-20 18:20:51 +0100 | [diff] [blame] | 1939 | |
| 1940 | " test for getting the visual start column |
| 1941 | func T() |
| 1942 | let g:Vcol = col('v') |
| 1943 | return '' |
| 1944 | endfunc |
| 1945 | let g:Vcol = 0 |
| 1946 | xmap <expr> <F2> T() |
| 1947 | exe "normal gg3|ve\<F2>" |
| 1948 | call assert_equal(3, g:Vcol) |
| 1949 | xunmap <F2> |
| 1950 | delfunc T |
| 1951 | |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 1952 | " Test for the visual line start and end marks '< and '> |
| 1953 | call setline(1, ['one', 'one two', 'one two three']) |
| 1954 | "normal! ggVG |
| 1955 | call feedkeys("ggVG\<Esc>", 'xt') |
| 1956 | call assert_equal(1, col("'<")) |
| 1957 | call assert_equal(14, col("'>")) |
| 1958 | " Delete the last line of the visually selected region |
| 1959 | $d |
| 1960 | call assert_notequal(14, col("'>")) |
| 1961 | |
| 1962 | " Test with 'virtualedit' |
| 1963 | set virtualedit=all |
| 1964 | call cursor(1, 10) |
| 1965 | call assert_equal(4, col('.')) |
| 1966 | set virtualedit& |
| 1967 | |
Yegappan Lakshmanan | 4c8d2f0 | 2022-11-12 16:07:47 +0000 | [diff] [blame] | 1968 | " Test for getting the column number in another window |
| 1969 | let winid = win_getid() |
| 1970 | new |
| 1971 | call win_execute(winid, 'normal 1G$') |
| 1972 | call assert_equal(3, col('.', winid)) |
| 1973 | call win_execute(winid, 'normal 2G') |
| 1974 | call assert_equal(8, col('$', winid)) |
| 1975 | call assert_equal(0, col('.', 5001)) |
| 1976 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1977 | bw! |
| 1978 | endfunc |
| 1979 | |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 1980 | " Test for input() |
| 1981 | func Test_input_func() |
| 1982 | " Test for prompt with multiple lines |
| 1983 | redir => v |
| 1984 | call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt') |
| 1985 | redir END |
| 1986 | call assert_equal("B", c) |
| 1987 | call assert_equal(['A', 'B', 'C'], split(v, "\n")) |
| 1988 | |
| 1989 | " Test for default value |
| 1990 | call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt') |
| 1991 | call assert_equal('red', c) |
| 1992 | |
| 1993 | " Test for completion at the input prompt |
| 1994 | func! Tcomplete(arglead, cmdline, pos) |
| 1995 | return "item1\nitem2\nitem3" |
| 1996 | endfunc |
Bram Moolenaar | 9d48956 | 2020-07-30 20:08:50 +0200 | [diff] [blame] | 1997 | call feedkeys(":let c = input('Q? ', '', 'custom,Tcomplete')\<CR>" |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 1998 | \ .. "\<C-A>\<CR>", 'xt') |
| 1999 | delfunc Tcomplete |
| 2000 | call assert_equal('item1 item2 item3', c) |
Bram Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 2001 | |
Bram Moolenaar | f4fcedc | 2021-03-15 18:36:20 +0100 | [diff] [blame] | 2002 | " Test for using special characters as default input |
Bram Moolenaar | 1f448d9 | 2021-03-22 19:37:06 +0100 | [diff] [blame] | 2003 | call feedkeys(":let c = input('name? ', \"x\\<BS>y\")\<CR>\<CR>", 'xt') |
Bram Moolenaar | f4fcedc | 2021-03-15 18:36:20 +0100 | [diff] [blame] | 2004 | call assert_equal('y', c) |
| 2005 | |
zeertzjq | e3a529b | 2022-06-05 19:01:37 +0100 | [diff] [blame] | 2006 | " Test for using text with composing characters as default input |
| 2007 | call feedkeys(":let c = input('name? ', \"ã̳\")\<CR>\<CR>", 'xt') |
| 2008 | call assert_equal('ã̳', c) |
| 2009 | |
Bram Moolenaar | f4fcedc | 2021-03-15 18:36:20 +0100 | [diff] [blame] | 2010 | " Test for using <CR> as default input |
| 2011 | call feedkeys(":let c = input('name? ', \"\\<CR>\")\<CR>x\<CR>", 'xt') |
| 2012 | call assert_equal(' x', c) |
| 2013 | |
Bram Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 2014 | call assert_fails("call input('F:', '', 'invalid')", 'E180:') |
| 2015 | call assert_fails("call input('F:', '', [])", 'E730:') |
| 2016 | endfunc |
| 2017 | |
| 2018 | " Test for the inputdialog() function |
| 2019 | func Test_inputdialog() |
Bram Moolenaar | fdcbe3c | 2020-06-15 21:41:56 +0200 | [diff] [blame] | 2020 | set timeout timeoutlen=10 |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 2021 | if has('gui_running') |
| 2022 | call assert_fails('let v=inputdialog([], "xx")', 'E730:') |
| 2023 | call assert_fails('let v=inputdialog("Q", [])', 'E730:') |
| 2024 | else |
| 2025 | call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt') |
| 2026 | call assert_equal('xx', v) |
| 2027 | call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt') |
| 2028 | call assert_equal('yy', v) |
| 2029 | endif |
Bram Moolenaar | fdcbe3c | 2020-06-15 21:41:56 +0200 | [diff] [blame] | 2030 | set timeout& timeoutlen& |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 2031 | endfunc |
| 2032 | |
| 2033 | " Test for inputlist() |
Bram Moolenaar | 947b39e | 2018-07-22 19:36:37 +0200 | [diff] [blame] | 2034 | func Test_inputlist() |
| 2035 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx') |
| 2036 | call assert_equal(1, c) |
Bram Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 2037 | 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] | 2038 | call assert_equal(2, c) |
| 2039 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx') |
| 2040 | call assert_equal(3, c) |
| 2041 | |
Bram Moolenaar | eebd555 | 2020-06-10 15:45:57 +0200 | [diff] [blame] | 2042 | " CR to cancel |
| 2043 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<cr>", 'tx') |
| 2044 | call assert_equal(0, c) |
| 2045 | |
| 2046 | " Esc to cancel |
| 2047 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<Esc>", 'tx') |
| 2048 | call assert_equal(0, c) |
| 2049 | |
| 2050 | " q to cancel |
| 2051 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>q", 'tx') |
| 2052 | call assert_equal(0, c) |
| 2053 | |
=?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?= | 5cf9457 | 2021-05-20 21:14:20 +0200 | [diff] [blame] | 2054 | " Cancel after inputting a number |
| 2055 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>5q", 'tx') |
| 2056 | call assert_equal(0, c) |
| 2057 | |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 2058 | " Use backspace to delete characters in the prompt |
| 2059 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<BS>3\<BS>2\<cr>", 'tx') |
| 2060 | call assert_equal(2, c) |
| 2061 | |
| 2062 | " Use mouse to make a selection |
| 2063 | call test_setmouse(&lines - 3, 2) |
| 2064 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx') |
| 2065 | call assert_equal(1, c) |
| 2066 | " Mouse click outside of the list |
| 2067 | call test_setmouse(&lines - 6, 2) |
| 2068 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx') |
| 2069 | call assert_equal(-2, c) |
| 2070 | |
Bram Moolenaar | 947b39e | 2018-07-22 19:36:37 +0200 | [diff] [blame] | 2071 | call assert_fails('call inputlist("")', 'E686:') |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 2072 | call assert_fails('call inputlist(test_null_list())', 'E686:') |
Bram Moolenaar | 947b39e | 2018-07-22 19:36:37 +0200 | [diff] [blame] | 2073 | endfunc |
| 2074 | |
Bram Moolenaar | 7bdcba0 | 2023-01-02 11:59:26 +0000 | [diff] [blame] | 2075 | func Test_range_inputlist() |
| 2076 | " flush out any garbage left in the buffer |
| 2077 | while getchar(0) |
| 2078 | endwhile |
| 2079 | |
| 2080 | call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x') |
| 2081 | call assert_equal(1, result) |
| 2082 | call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x') |
| 2083 | call assert_equal(1, result) |
| 2084 | |
| 2085 | unlet result |
| 2086 | endfunc |
| 2087 | |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 2088 | func Test_balloon_show() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 2089 | CheckFeature balloon_eval |
Bram Moolenaar | b47bed2 | 2021-04-14 17:06:43 +0200 | [diff] [blame] | 2090 | |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 2091 | " This won't do anything but must not crash either. |
| 2092 | call balloon_show('hi!') |
| 2093 | if !has('gui_running') |
| 2094 | call balloon_show(range(3)) |
| 2095 | call balloon_show([]) |
Bram Moolenaar | a0107bd | 2017-03-02 22:48:01 +0100 | [diff] [blame] | 2096 | endif |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 2097 | endfunc |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 2098 | |
| 2099 | func Test_setbufvar_options() |
| 2100 | " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the |
zeertzjq | 49f0524 | 2023-02-04 10:58:34 +0000 | [diff] [blame] | 2101 | " window layout and cursor position. |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 2102 | call assert_equal(1, winnr('$')) |
| 2103 | split dummy_preview |
| 2104 | resize 2 |
| 2105 | set winfixheight winfixwidth |
| 2106 | let prev_id = win_getid() |
| 2107 | |
| 2108 | wincmd j |
Bram Moolenaar | c05d1c0 | 2020-09-04 18:38:06 +0200 | [diff] [blame] | 2109 | let wh = winheight(0) |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 2110 | let dummy_buf = bufnr('dummy_buf1', v:true) |
| 2111 | call setbufvar(dummy_buf, '&buftype', 'nofile') |
| 2112 | execute 'belowright vertical split #' . dummy_buf |
Bram Moolenaar | c05d1c0 | 2020-09-04 18:38:06 +0200 | [diff] [blame] | 2113 | call assert_equal(wh, winheight(0)) |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 2114 | let dum1_id = win_getid() |
zeertzjq | 49f0524 | 2023-02-04 10:58:34 +0000 | [diff] [blame] | 2115 | call setline(1, 'foo') |
| 2116 | normal! V$ |
| 2117 | call assert_equal(4, col('.')) |
| 2118 | call setbufvar('dummy_preview', '&buftype', 'nofile') |
| 2119 | call assert_equal(4, col('.')) |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 2120 | |
| 2121 | wincmd h |
Bram Moolenaar | c05d1c0 | 2020-09-04 18:38:06 +0200 | [diff] [blame] | 2122 | let wh = winheight(0) |
zeertzjq | 49f0524 | 2023-02-04 10:58:34 +0000 | [diff] [blame] | 2123 | call setline(1, 'foo') |
| 2124 | normal! V$ |
| 2125 | call assert_equal(4, col('.')) |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 2126 | let dummy_buf = bufnr('dummy_buf2', v:true) |
Bram Moolenaar | 196b466 | 2019-09-06 21:34:30 +0200 | [diff] [blame] | 2127 | eval 'nofile'->setbufvar(dummy_buf, '&buftype') |
zeertzjq | 49f0524 | 2023-02-04 10:58:34 +0000 | [diff] [blame] | 2128 | call assert_equal(4, col('.')) |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 2129 | execute 'belowright vertical split #' . dummy_buf |
Bram Moolenaar | c05d1c0 | 2020-09-04 18:38:06 +0200 | [diff] [blame] | 2130 | call assert_equal(wh, winheight(0)) |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 2131 | |
| 2132 | bwipe! |
| 2133 | call win_gotoid(prev_id) |
| 2134 | bwipe! |
| 2135 | call win_gotoid(dum1_id) |
| 2136 | bwipe! |
| 2137 | endfunc |
Bram Moolenaar | d4863aa | 2017-04-07 19:50:12 +0200 | [diff] [blame] | 2138 | |
Bram Moolenaar | dff97e6 | 2022-01-24 20:00:55 +0000 | [diff] [blame] | 2139 | func Test_setbufvar_keep_window_title() |
| 2140 | CheckRunVimInTerminal |
Bram Moolenaar | a6c09a7 | 2022-01-24 22:02:15 +0000 | [diff] [blame] | 2141 | if !has('title') || empty(&t_ts) |
| 2142 | throw "Skipped: can't get/set title" |
| 2143 | endif |
Bram Moolenaar | dff97e6 | 2022-01-24 20:00:55 +0000 | [diff] [blame] | 2144 | |
| 2145 | let lines =<< trim END |
Bram Moolenaar | 1450112 | 2022-01-24 22:32:28 +0000 | [diff] [blame] | 2146 | set title |
Bram Moolenaar | dff97e6 | 2022-01-24 20:00:55 +0000 | [diff] [blame] | 2147 | edit Xa.txt |
| 2148 | let g:buf = bufadd('Xb.txt') |
| 2149 | inoremap <F2> <C-R>=setbufvar(g:buf, '&autoindent', 1) ?? ''<CR> |
| 2150 | END |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 2151 | call writefile(lines, 'Xsetbufvar', 'D') |
Bram Moolenaar | dff97e6 | 2022-01-24 20:00:55 +0000 | [diff] [blame] | 2152 | let buf = RunVimInTerminal('-S Xsetbufvar', {}) |
Bram Moolenaar | 3a8ad59 | 2022-01-24 22:18:24 +0000 | [diff] [blame] | 2153 | call WaitForAssert({-> assert_match('Xa.txt', term_gettitle(buf))}, 1000) |
Bram Moolenaar | dff97e6 | 2022-01-24 20:00:55 +0000 | [diff] [blame] | 2154 | |
| 2155 | call term_sendkeys(buf, "i\<F2>") |
| 2156 | call TermWait(buf) |
| 2157 | call term_sendkeys(buf, "\<Esc>") |
| 2158 | call TermWait(buf) |
| 2159 | call assert_match('Xa.txt', term_gettitle(buf)) |
| 2160 | |
| 2161 | call StopVimInTerminal(buf) |
Bram Moolenaar | dff97e6 | 2022-01-24 20:00:55 +0000 | [diff] [blame] | 2162 | endfunc |
| 2163 | |
Bram Moolenaar | d4863aa | 2017-04-07 19:50:12 +0200 | [diff] [blame] | 2164 | func Test_redo_in_nested_functions() |
| 2165 | nnoremap g. :set opfunc=Operator<CR>g@ |
| 2166 | function Operator( type, ... ) |
| 2167 | let @x = 'XXX' |
| 2168 | execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp' |
| 2169 | endfunction |
| 2170 | |
| 2171 | function! Apply() |
| 2172 | 5,6normal! . |
| 2173 | endfunction |
| 2174 | |
| 2175 | new |
| 2176 | call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3)) |
| 2177 | 1normal g.i" |
| 2178 | call assert_equal('some "XXX" text', getline(1)) |
| 2179 | 3,4normal . |
| 2180 | call assert_equal('some "XXX" text', getline(3)) |
| 2181 | call assert_equal('more "XXX" text', getline(4)) |
| 2182 | call Apply() |
| 2183 | call assert_equal('some "XXX" text', getline(5)) |
| 2184 | call assert_equal('more "XXX" text', getline(6)) |
| 2185 | bwipe! |
| 2186 | |
| 2187 | nunmap g. |
| 2188 | delfunc Operator |
| 2189 | delfunc Apply |
| 2190 | endfunc |
Bram Moolenaar | 2061552 | 2017-06-05 18:46:26 +0200 | [diff] [blame] | 2191 | |
Bram Moolenaar | 295ac5a | 2018-03-22 23:04:02 +0100 | [diff] [blame] | 2192 | func Test_trim() |
| 2193 | 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] | 2194 | 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] | 2195 | call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t")) |
| 2196 | call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww")) |
| 2197 | call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail")) |
| 2198 | call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " ")) |
| 2199 | call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx")) |
| 2200 | call assert_equal("RESERVE", trim("你RESERVE好", "你好")) |
| 2201 | call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好")) |
| 2202 | call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", )) |
| 2203 | call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好")) |
| 2204 | call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes")) |
| 2205 | call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses")) |
| 2206 | call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要")) |
| 2207 | call assert_equal("", trim("", "")) |
| 2208 | call assert_equal("a", trim("a", "")) |
| 2209 | call assert_equal("", trim("", "a")) |
| 2210 | |
Bram Moolenaar | 2245ae1 | 2020-05-31 22:20:36 +0200 | [diff] [blame] | 2211 | call assert_equal("vim", trim(" vim ", " ", 0)) |
| 2212 | call assert_equal("vim ", trim(" vim ", " ", 1)) |
| 2213 | call assert_equal(" vim", trim(" vim ", " ", 2)) |
| 2214 | call assert_fails('eval trim(" vim ", " ", [])', 'E745:') |
| 2215 | call assert_fails('eval trim(" vim ", " ", -1)', 'E475:') |
| 2216 | call assert_fails('eval trim(" vim ", " ", 3)', 'E475:') |
Yegappan Lakshmanan | 8deb2b3 | 2022-09-02 15:15:27 +0100 | [diff] [blame] | 2217 | call assert_fails('eval trim(" vim ", 0)', 'E1174:') |
Bram Moolenaar | 2245ae1 | 2020-05-31 22:20:36 +0200 | [diff] [blame] | 2218 | |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 2219 | let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '') |
Bram Moolenaar | 295ac5a | 2018-03-22 23:04:02 +0100 | [diff] [blame] | 2220 | call assert_equal("x", trim(chars . "x" . chars)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 2221 | |
| 2222 | call assert_fails('let c=trim([])', 'E730:') |
Bram Moolenaar | 295ac5a | 2018-03-22 23:04:02 +0100 | [diff] [blame] | 2223 | endfunc |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 2224 | |
| 2225 | " Test for reg_recording() and reg_executing() |
| 2226 | func Test_reg_executing_and_recording() |
| 2227 | let s:reg_stat = '' |
| 2228 | func s:save_reg_stat() |
| 2229 | let s:reg_stat = reg_recording() . ':' . reg_executing() |
| 2230 | return '' |
| 2231 | endfunc |
| 2232 | |
| 2233 | new |
| 2234 | call s:save_reg_stat() |
| 2235 | call assert_equal(':', s:reg_stat) |
| 2236 | call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt') |
| 2237 | call assert_equal('a:', s:reg_stat) |
| 2238 | call feedkeys("@a", 'xt') |
| 2239 | call assert_equal(':a', s:reg_stat) |
| 2240 | call feedkeys("qb@aq", 'xt') |
| 2241 | call assert_equal('b:a', s:reg_stat) |
| 2242 | call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt') |
| 2243 | call assert_equal('":', s:reg_stat) |
| 2244 | |
Bram Moolenaar | cce713d | 2019-03-04 11:40:12 +0100 | [diff] [blame] | 2245 | " :normal command saves and restores reg_executing |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 2246 | let s:reg_stat = '' |
Bram Moolenaar | cce713d | 2019-03-04 11:40:12 +0100 | [diff] [blame] | 2247 | let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>" |
| 2248 | func TestFunc() abort |
| 2249 | normal! ia |
| 2250 | endfunc |
| 2251 | call feedkeys("@q", 'xt') |
| 2252 | call assert_equal(':q', s:reg_stat) |
| 2253 | delfunc TestFunc |
| 2254 | |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 2255 | " getchar() command saves and restores reg_executing |
| 2256 | map W :call TestFunc()<CR> |
| 2257 | let @q = "W" |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 2258 | let g:typed = '' |
| 2259 | let g:regs = [] |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 2260 | func TestFunc() abort |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 2261 | let g:regs += [reg_executing()] |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 2262 | let g:typed = getchar(0) |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 2263 | let g:regs += [reg_executing()] |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 2264 | endfunc |
| 2265 | call feedkeys("@qy", 'xt') |
| 2266 | call assert_equal(char2nr("y"), g:typed) |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 2267 | call assert_equal(['q', 'q'], g:regs) |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 2268 | delfunc TestFunc |
| 2269 | unmap W |
| 2270 | unlet g:typed |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 2271 | unlet g:regs |
| 2272 | |
| 2273 | " input() command saves and restores reg_executing |
| 2274 | map W :call TestFunc()<CR> |
| 2275 | let @q = "W" |
| 2276 | let g:typed = '' |
| 2277 | let g:regs = [] |
| 2278 | func TestFunc() abort |
| 2279 | let g:regs += [reg_executing()] |
Bram Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 2280 | let g:typed = '?'->input() |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 2281 | let g:regs += [reg_executing()] |
| 2282 | endfunc |
| 2283 | call feedkeys("@qy\<CR>", 'xt') |
| 2284 | call assert_equal("y", g:typed) |
| 2285 | call assert_equal(['q', 'q'], g:regs) |
| 2286 | delfunc TestFunc |
| 2287 | unmap W |
| 2288 | unlet g:typed |
| 2289 | unlet g:regs |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 2290 | |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 2291 | bwipe! |
| 2292 | delfunc s:save_reg_stat |
| 2293 | unlet s:reg_stat |
| 2294 | endfunc |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 2295 | |
Bram Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 2296 | func Test_inputsecret() |
| 2297 | map W :call TestFunc()<CR> |
| 2298 | let @q = "W" |
| 2299 | let g:typed1 = '' |
| 2300 | let g:typed2 = '' |
| 2301 | let g:regs = [] |
| 2302 | func TestFunc() abort |
| 2303 | let g:typed1 = '?'->inputsecret() |
| 2304 | let g:typed2 = inputsecret('password: ') |
| 2305 | endfunc |
| 2306 | call feedkeys("@qsomething\<CR>else\<CR>", 'xt') |
| 2307 | call assert_equal("something", g:typed1) |
| 2308 | call assert_equal("else", g:typed2) |
| 2309 | delfunc TestFunc |
| 2310 | unmap W |
| 2311 | unlet g:typed1 |
| 2312 | unlet g:typed2 |
| 2313 | endfunc |
| 2314 | |
Bram Moolenaar | 5d712e4 | 2019-09-03 23:37:01 +0200 | [diff] [blame] | 2315 | func Test_getchar() |
| 2316 | call feedkeys('a', '') |
| 2317 | call assert_equal(char2nr('a'), getchar()) |
Bram Moolenaar | 3a7503c | 2021-06-07 18:29:17 +0200 | [diff] [blame] | 2318 | call assert_equal(0, getchar(0)) |
| 2319 | call assert_equal(0, getchar(1)) |
| 2320 | |
| 2321 | call feedkeys('a', '') |
| 2322 | call assert_equal('a', getcharstr()) |
| 2323 | call assert_equal('', getcharstr(0)) |
| 2324 | call assert_equal('', getcharstr(1)) |
Bram Moolenaar | 5d712e4 | 2019-09-03 23:37:01 +0200 | [diff] [blame] | 2325 | |
zeertzjq | ad6c45f | 2022-02-20 19:05:10 +0000 | [diff] [blame] | 2326 | call feedkeys("\<M-F2>", '') |
| 2327 | call assert_equal("\<M-F2>", getchar(0)) |
| 2328 | call assert_equal(0, getchar(0)) |
| 2329 | |
Bram Moolenaar | db3a205 | 2019-11-16 18:22:41 +0100 | [diff] [blame] | 2330 | call setline(1, 'xxxx') |
Bram Moolenaar | 5d712e4 | 2019-09-03 23:37:01 +0200 | [diff] [blame] | 2331 | call test_setmouse(1, 3) |
| 2332 | let v:mouse_win = 9 |
| 2333 | let v:mouse_winid = 9 |
| 2334 | let v:mouse_lnum = 9 |
| 2335 | let v:mouse_col = 9 |
| 2336 | call feedkeys("\<S-LeftMouse>", '') |
| 2337 | call assert_equal("\<S-LeftMouse>", getchar()) |
| 2338 | call assert_equal(1, v:mouse_win) |
| 2339 | call assert_equal(win_getid(1), v:mouse_winid) |
| 2340 | call assert_equal(1, v:mouse_lnum) |
| 2341 | call assert_equal(3, v:mouse_col) |
Bram Moolenaar | db3a205 | 2019-11-16 18:22:41 +0100 | [diff] [blame] | 2342 | enew! |
Bram Moolenaar | 5d712e4 | 2019-09-03 23:37:01 +0200 | [diff] [blame] | 2343 | endfunc |
| 2344 | |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 2345 | func Test_libcall_libcallnr() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 2346 | CheckFeature libcall |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 2347 | |
| 2348 | if has('win32') |
| 2349 | let libc = 'msvcrt.dll' |
| 2350 | elseif has('mac') |
| 2351 | let libc = 'libSystem.B.dylib' |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 2352 | elseif executable('ldd') |
| 2353 | let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>') |
| 2354 | endif |
| 2355 | if get(l:, 'libc', '') ==# '' |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 2356 | " On Unix, libc.so can be in various places. |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 2357 | if has('linux') |
| 2358 | " There is not documented but regarding the 1st argument of glibc's |
| 2359 | " dlopen an empty string and nullptr are equivalent, so using an empty |
| 2360 | " string for the 1st argument of libcall allows to call functions. |
| 2361 | let libc = '' |
| 2362 | elseif has('sun') |
| 2363 | " Set the path to libc.so according to the architecture. |
| 2364 | let test_bits = system('file ' . GetVimProg()) |
| 2365 | let test_arch = system('uname -p') |
| 2366 | if test_bits =~ '64-bit' && test_arch =~ 'sparc' |
| 2367 | let libc = '/usr/lib/sparcv9/libc.so' |
| 2368 | elseif test_bits =~ '64-bit' && test_arch =~ 'i386' |
| 2369 | let libc = '/usr/lib/amd64/libc.so' |
| 2370 | else |
| 2371 | let libc = '/usr/lib/libc.so' |
| 2372 | endif |
| 2373 | else |
| 2374 | " Unfortunately skip this test until a good way is found. |
| 2375 | return |
| 2376 | endif |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 2377 | endif |
| 2378 | |
| 2379 | if has('win32') |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 2380 | call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv')) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 2381 | else |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 2382 | call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv')) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 2383 | endif |
| 2384 | |
| 2385 | " If function returns NULL, libcall() should return an empty string. |
| 2386 | call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT')) |
| 2387 | |
| 2388 | " Test libcallnr() with string and integer argument. |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 2389 | call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen')) |
| 2390 | call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper')) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 2391 | |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 2392 | call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", ['', 'E364:']) |
| 2393 | call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", ['', 'E364:']) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 2394 | |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 2395 | call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", ['', 'E364:']) |
| 2396 | call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", ['', 'E364:']) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 2397 | endfunc |
Bram Moolenaar | d90a144 | 2018-07-15 20:24:31 +0200 | [diff] [blame] | 2398 | |
| 2399 | sandbox function Fsandbox() |
| 2400 | normal ix |
| 2401 | endfunc |
| 2402 | |
| 2403 | func Test_func_sandbox() |
| 2404 | sandbox let F = {-> 'hello'} |
| 2405 | call assert_equal('hello', F()) |
| 2406 | |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 2407 | sandbox let F = {-> "normal ix\<Esc>"->execute()} |
Bram Moolenaar | d90a144 | 2018-07-15 20:24:31 +0200 | [diff] [blame] | 2408 | call assert_fails('call F()', 'E48:') |
| 2409 | unlet F |
| 2410 | |
| 2411 | call assert_fails('call Fsandbox()', 'E48:') |
| 2412 | delfunc Fsandbox |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 2413 | |
| 2414 | " From a sandbox try to set a predefined variable (which cannot be modified |
| 2415 | " from a sandbox) |
| 2416 | call assert_fails('sandbox let v:lnum = 10', 'E794:') |
Bram Moolenaar | d90a144 | 2018-07-15 20:24:31 +0200 | [diff] [blame] | 2417 | endfunc |
Bram Moolenaar | 9e353b5 | 2018-11-04 23:39:38 +0100 | [diff] [blame] | 2418 | |
| 2419 | func EditAnotherFile() |
| 2420 | let word = expand('<cword>') |
| 2421 | edit Xfuncrange2 |
| 2422 | endfunc |
| 2423 | |
| 2424 | func Test_func_range_with_edit() |
| 2425 | " Define a function that edits another buffer, then call it with a range that |
| 2426 | " is invalid in that buffer. |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 2427 | call writefile(['just one line'], 'Xfuncrange2', 'D') |
Bram Moolenaar | 9e353b5 | 2018-11-04 23:39:38 +0100 | [diff] [blame] | 2428 | new |
Bram Moolenaar | 196b466 | 2019-09-06 21:34:30 +0200 | [diff] [blame] | 2429 | eval 10->range()->setline(1) |
Bram Moolenaar | 9e353b5 | 2018-11-04 23:39:38 +0100 | [diff] [blame] | 2430 | write Xfuncrange1 |
| 2431 | call assert_fails('5,8call EditAnotherFile()', 'E16:') |
| 2432 | |
| 2433 | call delete('Xfuncrange1') |
Bram Moolenaar | 9e353b5 | 2018-11-04 23:39:38 +0100 | [diff] [blame] | 2434 | bwipe! |
| 2435 | endfunc |
Bram Moolenaar | ded5f1b | 2018-11-10 17:33:29 +0100 | [diff] [blame] | 2436 | |
| 2437 | func Test_func_exists_on_reload() |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 2438 | call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists', 'D') |
Bram Moolenaar | ded5f1b | 2018-11-10 17:33:29 +0100 | [diff] [blame] | 2439 | call assert_equal(0, exists('*ExistingFunction')) |
| 2440 | source Xfuncexists |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 2441 | call assert_equal(1, '*ExistingFunction'->exists()) |
Bram Moolenaar | ded5f1b | 2018-11-10 17:33:29 +0100 | [diff] [blame] | 2442 | " Redefining a function when reloading a script is OK. |
| 2443 | source Xfuncexists |
| 2444 | call assert_equal(1, exists('*ExistingFunction')) |
| 2445 | |
| 2446 | " But redefining in another script is not OK. |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 2447 | call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2', 'D') |
Bram Moolenaar | ded5f1b | 2018-11-10 17:33:29 +0100 | [diff] [blame] | 2448 | call assert_fails('source Xfuncexists2', 'E122:') |
| 2449 | |
Yegappan Lakshmanan | 611728f | 2021-05-24 15:15:47 +0200 | [diff] [blame] | 2450 | " Defining a new function from the cmdline should fail if the function is |
| 2451 | " already defined |
| 2452 | call assert_fails('call feedkeys(":func ExistingFunction()\<CR>", "xt")', 'E122:') |
| 2453 | |
Bram Moolenaar | ded5f1b | 2018-11-10 17:33:29 +0100 | [diff] [blame] | 2454 | delfunc ExistingFunction |
| 2455 | call assert_equal(0, exists('*ExistingFunction')) |
| 2456 | call writefile([ |
| 2457 | \ 'func ExistingFunction()', 'echo "yes"', 'endfunc', |
| 2458 | \ 'func ExistingFunction()', 'echo "no"', 'endfunc', |
| 2459 | \ ], 'Xfuncexists') |
| 2460 | call assert_fails('source Xfuncexists', 'E122:') |
| 2461 | call assert_equal(1, exists('*ExistingFunction')) |
| 2462 | |
Bram Moolenaar | ded5f1b | 2018-11-10 17:33:29 +0100 | [diff] [blame] | 2463 | delfunc ExistingFunction |
| 2464 | endfunc |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 2465 | |
| 2466 | " Test confirm({msg} [, {choices} [, {default} [, {type}]]]) |
| 2467 | func Test_confirm() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 2468 | CheckUnix |
| 2469 | CheckNotGui |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 2470 | |
| 2471 | call feedkeys('o', 'L') |
| 2472 | let a = confirm('Press O to proceed') |
| 2473 | call assert_equal(1, a) |
| 2474 | |
| 2475 | call feedkeys('y', 'L') |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 2476 | let a = 'Are you sure?'->confirm("&Yes\n&No") |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 2477 | call assert_equal(1, a) |
| 2478 | |
| 2479 | call feedkeys('n', 'L') |
| 2480 | let a = confirm('Are you sure?', "&Yes\n&No") |
| 2481 | call assert_equal(2, a) |
| 2482 | |
| 2483 | " confirm() should return 0 when pressing CTRL-C. |
Bram Moolenaar | 7929651 | 2020-03-22 16:17:14 +0100 | [diff] [blame] | 2484 | call feedkeys("\<C-C>", 'L') |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 2485 | let a = confirm('Are you sure?', "&Yes\n&No") |
| 2486 | call assert_equal(0, a) |
| 2487 | |
| 2488 | " <Esc> requires another character to avoid it being seen as the start of an |
| 2489 | " escape sequence. Zero should be harmless. |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 2490 | eval "\<Esc>0"->feedkeys('L') |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 2491 | let a = confirm('Are you sure?', "&Yes\n&No") |
| 2492 | call assert_equal(0, a) |
| 2493 | |
| 2494 | " Default choice is returned when pressing <CR>. |
| 2495 | call feedkeys("\<CR>", 'L') |
| 2496 | let a = confirm('Are you sure?', "&Yes\n&No") |
| 2497 | call assert_equal(1, a) |
| 2498 | |
| 2499 | call feedkeys("\<CR>", 'L') |
| 2500 | let a = confirm('Are you sure?', "&Yes\n&No", 2) |
| 2501 | call assert_equal(2, a) |
| 2502 | |
| 2503 | call feedkeys("\<CR>", 'L') |
| 2504 | let a = confirm('Are you sure?', "&Yes\n&No", 0) |
| 2505 | call assert_equal(0, a) |
| 2506 | |
| 2507 | " Test with the {type} 4th argument |
| 2508 | for type in ['Error', 'Question', 'Info', 'Warning', 'Generic'] |
| 2509 | call feedkeys('y', 'L') |
| 2510 | let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type) |
| 2511 | call assert_equal(1, a) |
| 2512 | endfor |
| 2513 | |
| 2514 | call assert_fails('call confirm([])', 'E730:') |
| 2515 | call assert_fails('call confirm("Are you sure?", [])', 'E730:') |
| 2516 | call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:') |
| 2517 | call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:') |
| 2518 | endfunc |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 2519 | |
| 2520 | func Test_platform_name() |
| 2521 | " The system matches at most only one name. |
Bram Moolenaar | 041c710 | 2020-05-30 18:14:57 +0200 | [diff] [blame] | 2522 | let names = ['amiga', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix'] |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 2523 | call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)'))) |
| 2524 | |
| 2525 | " Is Unix? |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 2526 | call assert_equal(has('bsd'), has('bsd') && has('unix')) |
| 2527 | call assert_equal(has('hpux'), has('hpux') && has('unix')) |
| 2528 | call assert_equal(has('linux'), has('linux') && has('unix')) |
| 2529 | call assert_equal(has('mac'), has('mac') && has('unix')) |
| 2530 | call assert_equal(has('qnx'), has('qnx') && has('unix')) |
| 2531 | call assert_equal(has('sun'), has('sun') && has('unix')) |
| 2532 | call assert_equal(has('win32'), has('win32') && !has('unix')) |
| 2533 | call assert_equal(has('win32unix'), has('win32unix') && has('unix')) |
| 2534 | |
| 2535 | if has('unix') && executable('uname') |
| 2536 | let uname = system('uname') |
Bram Moolenaar | a02e3f6 | 2019-02-07 21:27:14 +0100 | [diff] [blame] | 2537 | " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined |
| 2538 | call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd')) |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 2539 | call assert_equal(uname =~? 'HP-UX', has('hpux')) |
| 2540 | call assert_equal(uname =~? 'Linux', has('linux')) |
| 2541 | call assert_equal(uname =~? 'Darwin', has('mac')) |
| 2542 | call assert_equal(uname =~? 'QNX', has('qnx')) |
| 2543 | call assert_equal(uname =~? 'SunOS', has('sun')) |
| 2544 | call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix')) |
| 2545 | endif |
| 2546 | endfunc |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 2547 | |
| 2548 | func Test_readdir() |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 2549 | call mkdir('Xreaddir', 'R') |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2550 | call writefile([], 'Xreaddir/foo.txt') |
| 2551 | call writefile([], 'Xreaddir/bar.txt') |
| 2552 | call mkdir('Xreaddir/dir') |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 2553 | |
| 2554 | " All results |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2555 | let files = readdir('Xreaddir') |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 2556 | call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files)) |
| 2557 | |
| 2558 | " Only results containing "f" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2559 | let files = 'Xreaddir'->readdir({ x -> stridx(x, 'f') != -1 }) |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 2560 | call assert_equal(['foo.txt'], sort(files)) |
| 2561 | |
| 2562 | " Only .txt files |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2563 | let files = readdir('Xreaddir', { x -> x =~ '.txt$' }) |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 2564 | call assert_equal(['bar.txt', 'foo.txt'], sort(files)) |
| 2565 | |
| 2566 | " Only .txt files with string |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2567 | let files = readdir('Xreaddir', 'v:val =~ ".txt$"') |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 2568 | call assert_equal(['bar.txt', 'foo.txt'], sort(files)) |
| 2569 | |
| 2570 | " Limit to 1 result. |
| 2571 | let l = [] |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2572 | let files = readdir('Xreaddir', {x -> len(add(l, x)) == 2 ? -1 : 1}) |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 2573 | call assert_equal(1, len(files)) |
| 2574 | |
Bram Moolenaar | 27da7de | 2019-09-03 17:13:37 +0200 | [diff] [blame] | 2575 | " Nested readdir() must not crash |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2576 | let files = readdir('Xreaddir', 'readdir("Xreaddir", "1") != []') |
Bram Moolenaar | 27da7de | 2019-09-03 17:13:37 +0200 | [diff] [blame] | 2577 | call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt']) |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 2578 | endfunc |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2579 | |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 2580 | func Test_readdirex() |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 2581 | call mkdir('Xexdir', 'R') |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2582 | call writefile(['foo'], 'Xexdir/foo.txt') |
| 2583 | call writefile(['barbar'], 'Xexdir/bar.txt') |
| 2584 | call mkdir('Xexdir/dir') |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 2585 | |
| 2586 | " All results |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2587 | let files = readdirex('Xexdir')->map({-> v:val.name}) |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 2588 | call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files)) |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2589 | let sizes = readdirex('Xexdir')->map({-> v:val.size}) |
Bram Moolenaar | 441d60e | 2020-06-02 22:19:50 +0200 | [diff] [blame] | 2590 | call assert_equal([0, 4, 7], sort(sizes)) |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 2591 | |
| 2592 | " Only results containing "f" |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2593 | let files = 'Xexdir'->readdirex({ e -> stridx(e.name, 'f') != -1 }) |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 2594 | \ ->map({-> v:val.name}) |
| 2595 | call assert_equal(['foo.txt'], sort(files)) |
| 2596 | |
| 2597 | " Only .txt files |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2598 | let files = readdirex('Xexdir', { e -> e.name =~ '.txt$' }) |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 2599 | \ ->map({-> v:val.name}) |
| 2600 | call assert_equal(['bar.txt', 'foo.txt'], sort(files)) |
| 2601 | |
| 2602 | " Only .txt files with string |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2603 | let files = readdirex('Xexdir', 'v:val.name =~ ".txt$"') |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 2604 | \ ->map({-> v:val.name}) |
| 2605 | call assert_equal(['bar.txt', 'foo.txt'], sort(files)) |
| 2606 | |
| 2607 | " Limit to 1 result. |
| 2608 | let l = [] |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2609 | let files = readdirex('Xexdir', {e -> len(add(l, e.name)) == 2 ? -1 : 1}) |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 2610 | \ ->map({-> v:val.name}) |
| 2611 | call assert_equal(1, len(files)) |
| 2612 | |
| 2613 | " Nested readdirex() must not crash |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2614 | let files = readdirex('Xexdir', 'readdirex("Xexdir", "1") != []') |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 2615 | \ ->map({-> v:val.name}) |
| 2616 | call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt']) |
| 2617 | |
Bram Moolenaar | fdcbe3c | 2020-06-15 21:41:56 +0200 | [diff] [blame] | 2618 | " report broken link correctly |
Bram Moolenaar | ab54032 | 2020-06-10 15:55:36 +0200 | [diff] [blame] | 2619 | if has("unix") |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2620 | call writefile([], 'Xexdir/abc.txt') |
| 2621 | call system("ln -s Xexdir/abc.txt Xexdir/link") |
| 2622 | call delete('Xexdir/abc.txt') |
| 2623 | let files = readdirex('Xexdir', 'readdirex("Xexdir", "1") != []') |
Bram Moolenaar | ab54032 | 2020-06-10 15:55:36 +0200 | [diff] [blame] | 2624 | \ ->map({-> v:val.name .. '_' .. v:val.type}) |
| 2625 | call sort(files)->assert_equal( |
| 2626 | \ ['bar.txt_file', 'dir_dir', 'foo.txt_file', 'link_link']) |
| 2627 | endif |
Bram Moolenaar | aab9fad | 2020-10-11 14:28:11 +0200 | [diff] [blame] | 2628 | |
| 2629 | call assert_fails('call readdirex("doesnotexist")', 'E484:') |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 2630 | endfunc |
| 2631 | |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2632 | func Test_readdirex_sort() |
| 2633 | CheckUnix |
| 2634 | " Skip tests on Mac OS X and Cygwin (does not allow several files with different casing) |
| 2635 | if has("osxdarwin") || has("osx") || has("macunix") || has("win32unix") |
| 2636 | throw 'Skipped: Test_readdirex_sort on systems that do not allow this using the default filesystem' |
| 2637 | endif |
| 2638 | let _collate = v:collate |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 2639 | call mkdir('Xsortdir2', 'R') |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2640 | call writefile(['1'], 'Xsortdir2/README.txt') |
| 2641 | call writefile(['2'], 'Xsortdir2/Readme.txt') |
| 2642 | call writefile(['3'], 'Xsortdir2/readme.txt') |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2643 | |
| 2644 | " 1) default |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2645 | let files = readdirex('Xsortdir2')->map({-> v:val.name}) |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2646 | let default = copy(files) |
| 2647 | call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort using default') |
| 2648 | |
| 2649 | " 2) no sorting |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2650 | let files = readdirex('Xsortdir2', 1, #{sort: 'none'})->map({-> v:val.name}) |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2651 | let unsorted = copy(files) |
| 2652 | call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], sort(files), 'unsorted') |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2653 | call assert_fails("call readdirex('Xsortdir2', 1, #{slort: 'none'})", 'E857: Dictionary key "sort" required') |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2654 | |
| 2655 | " 3) sort by case (same as default) |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2656 | let files = readdirex('Xsortdir2', 1, #{sort: 'case'})->map({-> v:val.name}) |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2657 | call assert_equal(default, files, 'sort by case') |
| 2658 | |
| 2659 | " 4) sort by ignoring case |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2660 | let files = readdirex('Xsortdir2', 1, #{sort: 'icase'})->map({-> v:val.name}) |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2661 | call assert_equal(unsorted->sort('i'), files, 'sort by icase') |
| 2662 | |
| 2663 | " 5) Default Collation |
| 2664 | let collate = v:collate |
| 2665 | lang collate C |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2666 | let files = readdirex('Xsortdir2', 1, #{sort: 'collate'})->map({-> v:val.name}) |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2667 | call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort by C collation') |
| 2668 | |
| 2669 | " 6) Collation de_DE |
| 2670 | " Switch locale, this may not work on the CI system, if the locale isn't |
| 2671 | " available |
| 2672 | try |
| 2673 | lang collate de_DE |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2674 | let files = readdirex('Xsortdir2', 1, #{sort: 'collate'})->map({-> v:val.name}) |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2675 | call assert_equal(['readme.txt', 'Readme.txt', 'README.txt'], files, 'sort by de_DE collation') |
| 2676 | catch |
| 2677 | throw 'Skipped: de_DE collation is not available' |
| 2678 | |
| 2679 | finally |
| 2680 | exe 'lang collate' collate |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2681 | endtry |
| 2682 | endfunc |
| 2683 | |
| 2684 | func Test_readdir_sort() |
| 2685 | " some more cases for testing sorting for readdirex |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2686 | let dir = 'Xsortdir3' |
Bram Moolenaar | 70e6725 | 2022-09-27 19:34:35 +0100 | [diff] [blame] | 2687 | call mkdir(dir, 'R') |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2688 | call writefile(['1'], dir .. '/README.txt') |
| 2689 | call writefile(['2'], dir .. '/Readm.txt') |
| 2690 | call writefile(['3'], dir .. '/read.txt') |
| 2691 | call writefile(['4'], dir .. '/Z.txt') |
| 2692 | call writefile(['5'], dir .. '/a.txt') |
| 2693 | call writefile(['6'], dir .. '/b.txt') |
| 2694 | |
| 2695 | " 1) default |
| 2696 | let files = readdir(dir) |
| 2697 | let default = copy(files) |
| 2698 | call assert_equal(default->sort(), files, 'sort using default') |
| 2699 | |
| 2700 | " 2) sort by case (same as default) |
| 2701 | let files = readdir(dir, '1', #{sort: 'case'}) |
| 2702 | call assert_equal(default, files, 'sort using default') |
| 2703 | |
| 2704 | " 3) sort by ignoring case |
| 2705 | let files = readdir(dir, '1', #{sort: 'icase'}) |
| 2706 | call assert_equal(default->sort('i'), files, 'sort by ignoring case') |
| 2707 | |
Bram Moolenaar | e17f881 | 2020-06-17 20:30:44 +0200 | [diff] [blame] | 2708 | " 4) collation |
| 2709 | let collate = v:collate |
| 2710 | lang collate C |
| 2711 | let files = readdir(dir, 1, #{sort: 'collate'}) |
| 2712 | call assert_equal(default->sort(), files, 'sort by C collation') |
| 2713 | exe "lang collate" collate |
| 2714 | |
| 2715 | " 5) Errors |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 2716 | call assert_fails('call readdir(dir, 1, 1)', 'E1206:') |
Bram Moolenaar | e17f881 | 2020-06-17 20:30:44 +0200 | [diff] [blame] | 2717 | call assert_fails('call readdir(dir, 1, #{sorta: 1})') |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 2718 | call assert_fails('call readdir(dir, 1, test_null_dict())', 'E1297:') |
| 2719 | call assert_fails('call readdirex(dir, 1, 1)', 'E1206:') |
Bram Moolenaar | e17f881 | 2020-06-17 20:30:44 +0200 | [diff] [blame] | 2720 | call assert_fails('call readdirex(dir, 1, #{sorta: 1})') |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 2721 | call assert_fails('call readdirex(dir, 1, test_null_dict())', 'E1297:') |
Bram Moolenaar | e17f881 | 2020-06-17 20:30:44 +0200 | [diff] [blame] | 2722 | |
| 2723 | " 6) ignore other values in dict |
| 2724 | let files = readdir(dir, '1', #{sort: 'c'}) |
| 2725 | call assert_equal(default, files, 'sort using default2') |
| 2726 | |
| 2727 | " Cleanup |
| 2728 | exe "lang collate" collate |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2729 | endfunc |
| 2730 | |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 2731 | func Test_delete_rf() |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2732 | call mkdir('Xrfdir') |
| 2733 | call writefile([], 'Xrfdir/foo.txt') |
| 2734 | call writefile([], 'Xrfdir/bar.txt') |
| 2735 | call mkdir('Xrfdir/[a-1]') " issue #696 |
| 2736 | call writefile([], 'Xrfdir/[a-1]/foo.txt') |
| 2737 | call writefile([], 'Xrfdir/[a-1]/bar.txt') |
| 2738 | call assert_true(filereadable('Xrfdir/foo.txt')) |
| 2739 | call assert_true('Xrfdir/[a-1]/foo.txt'->filereadable()) |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 2740 | |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2741 | call assert_equal(0, delete('Xrfdir', 'rf')) |
| 2742 | call assert_false(filereadable('Xrfdir/foo.txt')) |
| 2743 | call assert_false(filereadable('Xrfdir/[a-1]/foo.txt')) |
zeertzjq | 4787003 | 2022-04-05 15:31:01 +0100 | [diff] [blame] | 2744 | |
| 2745 | if has('unix') |
Bram Moolenaar | 3b0d70f | 2022-08-29 22:31:20 +0100 | [diff] [blame] | 2746 | call mkdir('Xrfdir/Xdir2', 'p') |
| 2747 | silent !chmod 555 Xrfdir |
| 2748 | call assert_equal(-1, delete('Xrfdir/Xdir2', 'rf')) |
| 2749 | call assert_equal(-1, delete('Xrfdir', 'rf')) |
| 2750 | silent !chmod 755 Xrfdir |
| 2751 | call assert_equal(0, delete('Xrfdir', 'rf')) |
zeertzjq | 4787003 | 2022-04-05 15:31:01 +0100 | [diff] [blame] | 2752 | endif |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 2753 | endfunc |
| 2754 | |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2755 | func Test_call() |
| 2756 | call assert_equal(3, call('len', [123])) |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 2757 | call assert_equal(3, 'len'->call([123])) |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 2758 | call assert_fails("call call('len', 123)", 'E1211:') |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2759 | call assert_equal(0, call('', [])) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 2760 | call assert_equal(0, call('len', test_null_list())) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2761 | |
| 2762 | function Mylen() dict |
| 2763 | return len(self.data) |
| 2764 | endfunction |
| 2765 | let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")} |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 2766 | eval mydict.len->call([], mydict)->assert_equal(4) |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 2767 | call assert_fails("call call('Mylen', [], 0)", 'E1206:') |
Bram Moolenaar | 67322bf | 2020-12-06 15:03:19 +0100 | [diff] [blame] | 2768 | call assert_fails('call foo', 'E107:') |
Dominique Pelle | fe8ebdb | 2021-05-13 14:55:55 +0200 | [diff] [blame] | 2769 | |
Bram Moolenaar | 22db0d5 | 2021-06-12 12:16:55 +0200 | [diff] [blame] | 2770 | " These once caused a crash. |
Dominique Pelle | fe8ebdb | 2021-05-13 14:55:55 +0200 | [diff] [blame] | 2771 | call call(test_null_function(), []) |
| 2772 | call call(test_null_partial(), []) |
Bram Moolenaar | 22db0d5 | 2021-06-12 12:16:55 +0200 | [diff] [blame] | 2773 | call assert_fails('call test_null_function()()', 'E1192:') |
| 2774 | call assert_fails('call test_null_partial()()', 'E117:') |
Bram Moolenaar | 2ef9156 | 2021-12-11 16:14:07 +0000 | [diff] [blame] | 2775 | |
| 2776 | let lines =<< trim END |
| 2777 | let Time = 'localtime' |
| 2778 | call Time() |
| 2779 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 2780 | call v9.CheckScriptFailure(lines, 'E1085:') |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2781 | endfunc |
| 2782 | |
| 2783 | func Test_char2nr() |
| 2784 | call assert_equal(12354, char2nr('あ', 1)) |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 2785 | call assert_equal(120, 'x'->char2nr()) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 2786 | set encoding=latin1 |
| 2787 | call assert_equal(120, 'x'->char2nr()) |
| 2788 | set encoding=utf-8 |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2789 | endfunc |
| 2790 | |
Bram Moolenaar | 4e4473c | 2020-08-28 22:24:57 +0200 | [diff] [blame] | 2791 | func Test_charclass() |
| 2792 | call assert_equal(0, charclass(' ')) |
| 2793 | call assert_equal(1, charclass('.')) |
| 2794 | call assert_equal(2, charclass('x')) |
| 2795 | call assert_equal(3, charclass("\u203c")) |
Christian Brabandt | 72463f8 | 2021-07-02 20:19:31 +0200 | [diff] [blame] | 2796 | " this used to crash vim |
| 2797 | call assert_equal(0, "xxx"[-1]->charclass()) |
Bram Moolenaar | 4e4473c | 2020-08-28 22:24:57 +0200 | [diff] [blame] | 2798 | endfunc |
| 2799 | |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2800 | func Test_eventhandler() |
| 2801 | call assert_equal(0, eventhandler()) |
| 2802 | endfunc |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2803 | |
| 2804 | func Test_bufadd_bufload() |
| 2805 | call assert_equal(0, bufexists('someName')) |
| 2806 | let buf = bufadd('someName') |
| 2807 | call assert_notequal(0, buf) |
| 2808 | call assert_equal(1, bufexists('someName')) |
| 2809 | call assert_equal(0, getbufvar(buf, '&buflisted')) |
| 2810 | call assert_equal(0, bufloaded(buf)) |
| 2811 | call bufload(buf) |
| 2812 | call assert_equal(1, bufloaded(buf)) |
| 2813 | call assert_equal([''], getbufline(buf, 1, '$')) |
| 2814 | |
| 2815 | let curbuf = bufnr('') |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 2816 | eval ['some', 'text']->writefile('XotherName') |
Bram Moolenaar | 073e4b9 | 2019-08-18 23:01:56 +0200 | [diff] [blame] | 2817 | let buf = 'XotherName'->bufadd() |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2818 | call assert_notequal(0, buf) |
Bram Moolenaar | 073e4b9 | 2019-08-18 23:01:56 +0200 | [diff] [blame] | 2819 | eval 'XotherName'->bufexists()->assert_equal(1) |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2820 | call assert_equal(0, getbufvar(buf, '&buflisted')) |
| 2821 | call assert_equal(0, bufloaded(buf)) |
Bram Moolenaar | 073e4b9 | 2019-08-18 23:01:56 +0200 | [diff] [blame] | 2822 | eval buf->bufload() |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2823 | call assert_equal(1, bufloaded(buf)) |
| 2824 | call assert_equal(['some', 'text'], getbufline(buf, 1, '$')) |
| 2825 | call assert_equal(curbuf, bufnr('')) |
| 2826 | |
Bram Moolenaar | 892ae72 | 2019-06-30 20:33:01 +0200 | [diff] [blame] | 2827 | let buf1 = bufadd('') |
| 2828 | let buf2 = bufadd('') |
| 2829 | call assert_notequal(0, buf1) |
| 2830 | call assert_notequal(0, buf2) |
| 2831 | call assert_notequal(buf1, buf2) |
| 2832 | call assert_equal(1, bufexists(buf1)) |
| 2833 | call assert_equal(1, bufexists(buf2)) |
| 2834 | call assert_equal(0, bufloaded(buf1)) |
| 2835 | exe 'bwipe ' .. buf1 |
| 2836 | call assert_equal(0, bufexists(buf1)) |
| 2837 | call assert_equal(1, bufexists(buf2)) |
| 2838 | exe 'bwipe ' .. buf2 |
| 2839 | call assert_equal(0, bufexists(buf2)) |
| 2840 | |
zeertzjq | 93f72cc | 2022-08-26 15:34:52 +0100 | [diff] [blame] | 2841 | " When 'buftype' is "nofile" then bufload() does not read the file. |
| 2842 | " Other values too. |
| 2843 | for val in [['nofile', 0], |
| 2844 | \ ['nowrite', 1], |
| 2845 | \ ['acwrite', 1], |
| 2846 | \ ['quickfix', 0], |
| 2847 | \ ['help', 1], |
| 2848 | \ ['terminal', 0], |
| 2849 | \ ['prompt', 0], |
| 2850 | \ ['popup', 0], |
| 2851 | \ ] |
| 2852 | bwipe! XotherName |
| 2853 | let buf = bufadd('XotherName') |
| 2854 | call setbufvar(buf, '&bt', val[0]) |
| 2855 | call bufload(buf) |
| 2856 | call assert_equal(val[1] ? ['some', 'text'] : [''], getbufline(buf, 1, '$'), val[0]) |
| 2857 | endfor |
Bram Moolenaar | c312619 | 2022-08-26 12:58:17 +0100 | [diff] [blame] | 2858 | |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2859 | bwipe someName |
Bram Moolenaar | 3940ec6 | 2019-07-05 21:53:24 +0200 | [diff] [blame] | 2860 | bwipe XotherName |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2861 | call assert_equal(0, bufexists('someName')) |
Bram Moolenaar | 3940ec6 | 2019-07-05 21:53:24 +0200 | [diff] [blame] | 2862 | call delete('XotherName') |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2863 | endfunc |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2864 | |
| 2865 | func Test_state() |
| 2866 | CheckRunVimInTerminal |
| 2867 | |
Bram Moolenaar | 3ed9efc | 2020-03-26 16:50:57 +0100 | [diff] [blame] | 2868 | let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>" |
| 2869 | |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2870 | let lines =<< trim END |
| 2871 | call setline(1, ['one', 'two', 'three']) |
| 2872 | map ;; gg |
Bram Moolenaar | b7a97ef | 2019-09-28 22:11:56 +0200 | [diff] [blame] | 2873 | set complete=. |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2874 | func RunTimer() |
| 2875 | call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')}) |
| 2876 | endfunc |
| 2877 | au Filetype foobar let g:state = state()|let g:mode = mode() |
| 2878 | END |
| 2879 | call writefile(lines, 'XState') |
| 2880 | let buf = RunVimInTerminal('-S XState', #{rows: 6}) |
| 2881 | |
| 2882 | " Using a ":" command Vim is busy, thus "S" is returned |
| 2883 | call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>") |
| 2884 | call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000) |
| 2885 | call term_sendkeys(buf, ":\<CR>") |
| 2886 | |
| 2887 | " Using a timer callback |
| 2888 | call term_sendkeys(buf, ":call RunTimer()\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2889 | call TermWait(buf, 25) |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2890 | call term_sendkeys(buf, getstate) |
| 2891 | call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000) |
| 2892 | |
| 2893 | " Halfway a mapping |
| 2894 | call term_sendkeys(buf, ":call RunTimer()\<CR>;") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2895 | call TermWait(buf, 25) |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2896 | call term_sendkeys(buf, ";") |
| 2897 | call term_sendkeys(buf, getstate) |
| 2898 | call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000) |
| 2899 | |
Bram Moolenaar | b7a97ef | 2019-09-28 22:11:56 +0200 | [diff] [blame] | 2900 | " Insert mode completion (bit slower on Mac) |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2901 | call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2902 | call TermWait(buf, 25) |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2903 | call term_sendkeys(buf, "\<Esc>") |
| 2904 | call term_sendkeys(buf, getstate) |
| 2905 | call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000) |
| 2906 | |
| 2907 | " Autocommand executing |
| 2908 | call term_sendkeys(buf, ":set filetype=foobar\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2909 | call TermWait(buf, 25) |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2910 | call term_sendkeys(buf, getstate) |
| 2911 | call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000) |
| 2912 | |
| 2913 | " Todo: "w" - waiting for ch_evalexpr() |
| 2914 | |
| 2915 | " messages scrolled |
| 2916 | call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2917 | call TermWait(buf, 25) |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2918 | call term_sendkeys(buf, "\<CR>") |
| 2919 | call term_sendkeys(buf, getstate) |
| 2920 | call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000) |
| 2921 | |
| 2922 | call StopVimInTerminal(buf) |
| 2923 | call delete('XState') |
| 2924 | endfunc |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 2925 | |
| 2926 | func Test_range() |
| 2927 | " destructuring |
| 2928 | let [x, y] = range(2) |
| 2929 | call assert_equal([0, 1], [x, y]) |
| 2930 | |
| 2931 | " index |
| 2932 | call assert_equal(4, range(1, 10)[3]) |
| 2933 | |
| 2934 | " add() |
| 2935 | call assert_equal([0, 1, 2, 3], add(range(3), 3)) |
| 2936 | call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3))) |
| 2937 | call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3))) |
| 2938 | |
| 2939 | " append() |
| 2940 | new |
| 2941 | call append('.', range(5)) |
| 2942 | call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$')) |
| 2943 | bwipe! |
| 2944 | |
| 2945 | " appendbufline() |
| 2946 | new |
| 2947 | call appendbufline(bufnr(''), '.', range(5)) |
| 2948 | call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$')) |
| 2949 | bwipe! |
| 2950 | |
| 2951 | " call() |
| 2952 | func TwoArgs(a, b) |
| 2953 | return [a:a, a:b] |
| 2954 | endfunc |
| 2955 | call assert_equal([0, 1], call('TwoArgs', range(2))) |
| 2956 | |
| 2957 | " col() |
| 2958 | new |
| 2959 | call setline(1, ['foo', 'bar']) |
| 2960 | call assert_equal(2, col(range(1, 2))) |
| 2961 | bwipe! |
| 2962 | |
| 2963 | " complete() |
| 2964 | execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>" |
| 2965 | " complete_info() |
| 2966 | execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>" |
| 2967 | |
| 2968 | " copy() |
| 2969 | call assert_equal([1, 2, 3], copy(range(1, 3))) |
| 2970 | |
| 2971 | " count() |
| 2972 | call assert_equal(0, count(range(0), 3)) |
| 2973 | call assert_equal(0, count(range(2), 3)) |
| 2974 | call assert_equal(1, count(range(5), 3)) |
| 2975 | |
| 2976 | " cursor() |
| 2977 | new |
| 2978 | call setline(1, ['aaa', 'bbb', 'ccc']) |
| 2979 | call cursor(range(1, 2)) |
| 2980 | call assert_equal([2, 1], [col('.'), line('.')]) |
| 2981 | bwipe! |
| 2982 | |
| 2983 | " deepcopy() |
| 2984 | call assert_equal([1, 2, 3], deepcopy(range(1, 3))) |
| 2985 | |
| 2986 | " empty() |
| 2987 | call assert_true(empty(range(0))) |
| 2988 | call assert_false(empty(range(2))) |
| 2989 | |
| 2990 | " execute() |
| 2991 | new |
| 2992 | call setline(1, ['aaa', 'bbb', 'ccc']) |
| 2993 | call execute(range(3)) |
| 2994 | call assert_equal(2, line('.')) |
| 2995 | bwipe! |
| 2996 | |
| 2997 | " extend() |
| 2998 | call assert_equal([1, 2, 3, 4], extend([1], range(2, 4))) |
| 2999 | call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4))) |
| 3000 | call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4])) |
| 3001 | |
| 3002 | " filter() |
| 3003 | call assert_equal([1, 3], filter(range(5), 'v:val % 2')) |
Bram Moolenaar | f8ca03b | 2020-11-28 20:32:29 +0100 | [diff] [blame] | 3004 | call assert_equal([1, 5, 7, 11, 13], filter(filter(range(15), 'v:val % 2'), 'v:val % 3')) |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 3005 | |
| 3006 | " funcref() |
| 3007 | call assert_equal([0, 1], funcref('TwoArgs', range(2))()) |
| 3008 | |
| 3009 | " function() |
| 3010 | call assert_equal([0, 1], function('TwoArgs', range(2))()) |
| 3011 | |
| 3012 | " garbagecollect() |
| 3013 | let thelist = [1, range(2), 3] |
| 3014 | let otherlist = range(3) |
| 3015 | call test_garbagecollect_now() |
| 3016 | |
| 3017 | " get() |
| 3018 | call assert_equal(4, get(range(1, 10), 3)) |
| 3019 | call assert_equal(-1, get(range(1, 10), 42, -1)) |
| 3020 | |
| 3021 | " index() |
| 3022 | call assert_equal(1, index(range(1, 5), 2)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 3023 | call assert_fails("echo index([1, 2], 1, [])", 'E745:') |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 3024 | |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 3025 | " insert() |
| 3026 | call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42)) |
| 3027 | call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0)) |
| 3028 | call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1)) |
| 3029 | call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4)) |
| 3030 | call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1)) |
| 3031 | call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5)) |
| 3032 | |
| 3033 | " join() |
| 3034 | call assert_equal('0 1 2 3 4', join(range(5))) |
| 3035 | |
Bram Moolenaar | 272ca95 | 2020-01-28 20:49:11 +0100 | [diff] [blame] | 3036 | " json_encode() |
| 3037 | call assert_equal('[0,1,2,3]', json_encode(range(4))) |
| 3038 | |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 3039 | " len() |
| 3040 | call assert_equal(0, len(range(0))) |
| 3041 | call assert_equal(2, len(range(2))) |
| 3042 | call assert_equal(5, len(range(0, 12, 3))) |
| 3043 | call assert_equal(4, len(range(3, 0, -1))) |
| 3044 | |
| 3045 | " list2str() |
| 3046 | call assert_equal('ABC', list2str(range(65, 67))) |
Bram Moolenaar | d83392a | 2022-09-01 12:22:46 +0100 | [diff] [blame] | 3047 | call assert_fails('let s = list2str(5)', 'E1211:') |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 3048 | |
| 3049 | " lock() |
| 3050 | let thelist = range(5) |
| 3051 | lockvar thelist |
| 3052 | |
| 3053 | " map() |
| 3054 | call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2')) |
Bram Moolenaar | f8ca03b | 2020-11-28 20:32:29 +0100 | [diff] [blame] | 3055 | call assert_equal([3, 5, 7, 9, 11], map(map(range(5), 'v:val * 2'), 'v:val + 3')) |
| 3056 | call assert_equal([2, 6], map(filter(range(5), 'v:val % 2'), 'v:val * 2')) |
| 3057 | call assert_equal([2, 4, 8], filter(map(range(5), 'v:val * 2'), 'v:val % 3')) |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 3058 | |
| 3059 | " match() |
| 3060 | call assert_equal(3, match(range(5), 3)) |
| 3061 | |
| 3062 | " matchaddpos() |
| 3063 | highlight MyGreenGroup ctermbg=green guibg=green |
| 3064 | call matchaddpos('MyGreenGroup', range(line('.'), line('.'))) |
| 3065 | |
| 3066 | " matchend() |
| 3067 | call assert_equal(4, matchend(range(5), '4')) |
| 3068 | call assert_equal(3, matchend(range(1, 5), '4')) |
| 3069 | call assert_equal(-1, matchend(range(1, 5), '42')) |
| 3070 | |
| 3071 | " matchstrpos() |
| 3072 | call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4')) |
| 3073 | call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4')) |
| 3074 | call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42')) |
| 3075 | |
| 3076 | " max() reverse() |
| 3077 | call assert_equal(0, max(range(0))) |
| 3078 | call assert_equal(0, max(range(10, 9))) |
| 3079 | call assert_equal(9, max(range(10))) |
| 3080 | call assert_equal(18, max(range(0, 20, 3))) |
| 3081 | call assert_equal(20, max(range(20, 0, -3))) |
| 3082 | call assert_equal(99999, max(range(100000))) |
| 3083 | call assert_equal(99999, max(range(99999, 0, -1))) |
| 3084 | call assert_equal(99999, max(reverse(range(100000)))) |
| 3085 | call assert_equal(99999, max(reverse(range(99999, 0, -1)))) |
| 3086 | |
| 3087 | " min() reverse() |
| 3088 | call assert_equal(0, min(range(0))) |
| 3089 | call assert_equal(0, min(range(10, 9))) |
| 3090 | call assert_equal(5, min(range(5, 10))) |
| 3091 | call assert_equal(5, min(range(5, 10, 3))) |
| 3092 | call assert_equal(2, min(range(20, 0, -3))) |
| 3093 | call assert_equal(0, min(range(100000))) |
| 3094 | call assert_equal(0, min(range(99999, 0, -1))) |
| 3095 | call assert_equal(0, min(reverse(range(100000)))) |
| 3096 | call assert_equal(0, min(reverse(range(99999, 0, -1)))) |
| 3097 | |
| 3098 | " remove() |
| 3099 | call assert_equal(1, remove(range(1, 10), 0)) |
| 3100 | call assert_equal(2, remove(range(1, 10), 1)) |
| 3101 | call assert_equal(9, remove(range(1, 10), 8)) |
| 3102 | call assert_equal(10, remove(range(1, 10), 9)) |
| 3103 | call assert_equal(10, remove(range(1, 10), -1)) |
| 3104 | call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4)) |
| 3105 | |
| 3106 | " repeat() |
| 3107 | call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2)) |
| 3108 | call assert_equal([0, 1, 2], repeat(range(3), 1)) |
| 3109 | call assert_equal([], repeat(range(3), 0)) |
| 3110 | call assert_equal([], repeat(range(5, 4), 2)) |
| 3111 | call assert_equal([], repeat(range(5, 4), 0)) |
| 3112 | |
| 3113 | " reverse() |
| 3114 | call assert_equal([2, 1, 0], reverse(range(3))) |
| 3115 | call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1))) |
| 3116 | call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10))) |
| 3117 | call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20))) |
| 3118 | call assert_equal([16, 13, 10], reverse(range(10, 18, 3))) |
| 3119 | call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3))) |
| 3120 | call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3))) |
| 3121 | call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3))) |
| 3122 | call assert_equal([], reverse(range(0))) |
| 3123 | |
| 3124 | " TODO: setpos() |
| 3125 | " new |
| 3126 | " call setline(1, repeat([''], bufnr(''))) |
| 3127 | " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6)) |
| 3128 | " call setpos('x', range(bufnr(''), bufnr('') + 3)) |
| 3129 | " bwipe! |
| 3130 | |
| 3131 | " setreg() |
| 3132 | call setreg('a', range(3)) |
| 3133 | call assert_equal("0\n1\n2\n", getreg('a')) |
| 3134 | |
Bram Moolenaar | b099202 | 2020-01-30 14:55:42 +0100 | [diff] [blame] | 3135 | " settagstack() |
| 3136 | call settagstack(1, #{items : range(4)}) |
Bram Moolenaar | 94255df | 2020-02-05 20:10:33 +0100 | [diff] [blame] | 3137 | |
Bram Moolenaar | b099202 | 2020-01-30 14:55:42 +0100 | [diff] [blame] | 3138 | " sign_define() |
| 3139 | call assert_fails("call sign_define(range(5))", "E715:") |
| 3140 | call assert_fails("call sign_placelist(range(5))", "E715:") |
| 3141 | |
| 3142 | " sign_undefine() |
| 3143 | call assert_fails("call sign_undefine(range(5))", "E908:") |
| 3144 | |
| 3145 | " sign_unplacelist() |
| 3146 | call assert_fails("call sign_unplacelist(range(5))", "E715:") |
| 3147 | |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 3148 | " sort() |
| 3149 | call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1))) |
| 3150 | |
| 3151 | " string() |
| 3152 | call assert_equal('[0, 1, 2, 3, 4]', string(range(5))) |
| 3153 | |
Bram Moolenaar | b099202 | 2020-01-30 14:55:42 +0100 | [diff] [blame] | 3154 | " taglist() with 'tagfunc' |
| 3155 | func TagFunc(pattern, flags, info) |
| 3156 | return range(10) |
| 3157 | endfunc |
| 3158 | set tagfunc=TagFunc |
| 3159 | call assert_fails("call taglist('asdf')", 'E987:') |
| 3160 | set tagfunc= |
Bram Moolenaar | 94255df | 2020-02-05 20:10:33 +0100 | [diff] [blame] | 3161 | |
Bram Moolenaar | b099202 | 2020-01-30 14:55:42 +0100 | [diff] [blame] | 3162 | " term_start() |
Bram Moolenaar | 705724e | 2020-01-31 21:13:42 +0100 | [diff] [blame] | 3163 | if has('terminal') && has('termguicolors') |
Bram Moolenaar | b099202 | 2020-01-30 14:55:42 +0100 | [diff] [blame] | 3164 | call assert_fails('call term_start(range(3, 4))', 'E474:') |
| 3165 | let g:terminal_ansi_colors = range(16) |
Bram Moolenaar | 94255df | 2020-02-05 20:10:33 +0100 | [diff] [blame] | 3166 | if has('win32') |
| 3167 | let cmd = "cmd /c dir" |
| 3168 | else |
| 3169 | let cmd = "ls" |
| 3170 | endif |
LemonBoy | b2b3acb | 2022-05-20 10:10:34 +0100 | [diff] [blame] | 3171 | call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"' |
| 3172 | \ .. ', ansi_colors: range(16)})', 'E475:') |
Bram Moolenaar | b0855f5 | 2022-05-20 10:39:18 +0100 | [diff] [blame] | 3173 | unlet g:terminal_ansi_colors |
Bram Moolenaar | b099202 | 2020-01-30 14:55:42 +0100 | [diff] [blame] | 3174 | endif |
| 3175 | |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 3176 | " type() |
| 3177 | call assert_equal(v:t_list, type(range(5))) |
| 3178 | |
| 3179 | " uniq() |
| 3180 | call assert_equal([0, 1, 2, 3, 4], uniq(range(5))) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 3181 | |
| 3182 | " errors |
| 3183 | call assert_fails('let x=range(2, 8, 0)', 'E726:') |
| 3184 | call assert_fails('let x=range(3, 1)', 'E727:') |
| 3185 | call assert_fails('let x=range(1, 3, -2)', 'E727:') |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 3186 | call assert_fails('let x=range([])', 'E745:') |
| 3187 | call assert_fails('let x=range(1, [])', 'E745:') |
| 3188 | call assert_fails('let x=range(1, 4, [])', 'E745:') |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 3189 | endfunc |
Bram Moolenaar | 4132eb5 | 2020-02-14 16:53:00 +0100 | [diff] [blame] | 3190 | |
Bram Moolenaar | b3d8398 | 2022-01-27 19:59:47 +0000 | [diff] [blame] | 3191 | func Test_garbagecollect_now_fails() |
| 3192 | let v:testing = 0 |
| 3193 | call assert_fails('call test_garbagecollect_now()', 'E1142:') |
| 3194 | let v:testing = 1 |
| 3195 | endfunc |
| 3196 | |
Bram Moolenaar | 4132eb5 | 2020-02-14 16:53:00 +0100 | [diff] [blame] | 3197 | func Test_echoraw() |
| 3198 | CheckScreendump |
| 3199 | |
| 3200 | " Normally used for escape codes, but let's test with a CR. |
| 3201 | let lines =<< trim END |
| 3202 | call echoraw("hello\<CR>x") |
| 3203 | END |
| 3204 | call writefile(lines, 'XTest_echoraw') |
| 3205 | let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40}) |
| 3206 | call VerifyScreenDump(buf, 'Test_functions_echoraw', {}) |
| 3207 | |
| 3208 | " clean up |
| 3209 | call StopVimInTerminal(buf) |
| 3210 | call delete('XTest_echoraw') |
| 3211 | endfunc |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 3212 | |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 3213 | " Test for echo highlighting |
| 3214 | func Test_echohl() |
| 3215 | echohl Search |
| 3216 | echo 'Vim' |
| 3217 | call assert_equal('Vim', Screenline(&lines)) |
| 3218 | " TODO: How to check the highlight group used by echohl? |
| 3219 | " ScreenAttrs() returns all zeros. |
| 3220 | echohl None |
| 3221 | endfunc |
| 3222 | |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 3223 | " Test for the eval() function |
| 3224 | func Test_eval() |
| 3225 | call assert_fails("call eval('5 a')", 'E488:') |
| 3226 | endfunc |
| 3227 | |
zeertzjq | cdc8393 | 2022-09-12 13:38:41 +0100 | [diff] [blame] | 3228 | " Test for the keytrans() function |
| 3229 | func Test_keytrans() |
| 3230 | call assert_equal('<Space>', keytrans(' ')) |
| 3231 | call assert_equal('<lt>', keytrans('<')) |
| 3232 | call assert_equal('<lt>Tab>', keytrans('<Tab>')) |
| 3233 | call assert_equal('<Tab>', keytrans("\<Tab>")) |
| 3234 | call assert_equal('<C-V>', keytrans("\<C-V>")) |
| 3235 | call assert_equal('<BS>', keytrans("\<BS>")) |
| 3236 | call assert_equal('<Home>', keytrans("\<Home>")) |
| 3237 | call assert_equal('<C-Home>', keytrans("\<C-Home>")) |
| 3238 | call assert_equal('<M-Home>', keytrans("\<M-Home>")) |
| 3239 | call assert_equal('<C-Space>', keytrans("\<C-Space>")) |
| 3240 | call assert_equal('<M-Space>', keytrans("\<*M-Space>")) |
| 3241 | call assert_equal('<M-x>', "\<*M-x>"->keytrans()) |
| 3242 | call assert_equal('<C-I>', "\<*C-I>"->keytrans()) |
| 3243 | call assert_equal('<S-3>', "\<*S-3>"->keytrans()) |
| 3244 | call assert_equal('π', 'π'->keytrans()) |
| 3245 | call assert_equal('<M-π>', "\<M-π>"->keytrans()) |
| 3246 | call assert_equal('ě', 'ě'->keytrans()) |
| 3247 | call assert_equal('<M-ě>', "\<M-ě>"->keytrans()) |
| 3248 | call assert_equal('', ''->keytrans()) |
| 3249 | call assert_equal('', test_null_string()->keytrans()) |
| 3250 | call assert_fails('call keytrans(1)', 'E1174:') |
| 3251 | call assert_fails('call keytrans()', 'E119:') |
| 3252 | endfunc |
| 3253 | |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 3254 | " Test for the nr2char() function |
| 3255 | func Test_nr2char() |
| 3256 | set encoding=latin1 |
| 3257 | call assert_equal('@', nr2char(64)) |
| 3258 | set encoding=utf8 |
| 3259 | call assert_equal('a', nr2char(97, 1)) |
| 3260 | call assert_equal('a', nr2char(97, 0)) |
Bram Moolenaar | f7271e8 | 2020-05-24 18:45:07 +0200 | [diff] [blame] | 3261 | |
zeertzjq | db08887 | 2022-05-02 22:53:45 +0100 | [diff] [blame] | 3262 | call assert_equal("\x80\xfc\b" .. nr2char(0x100000), eval('"\<M-' .. nr2char(0x100000) .. '>"')) |
| 3263 | call assert_equal("\x80\xfc\b" .. nr2char(0x40000000), eval('"\<M-' .. nr2char(0x40000000) .. '>"')) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 3264 | endfunc |
| 3265 | |
| 3266 | " Test for screenattr(), screenchar() and screenchars() functions |
| 3267 | func Test_screen_functions() |
| 3268 | call assert_equal(-1, screenattr(-1, -1)) |
| 3269 | call assert_equal(-1, screenchar(-1, -1)) |
| 3270 | call assert_equal([], screenchars(-1, -1)) |
zeertzjq | 47eec67 | 2023-06-01 20:26:55 +0100 | [diff] [blame] | 3271 | |
| 3272 | " Run this in a separate Vim instance to avoid messing up. |
| 3273 | let after =<< trim [CODE] |
| 3274 | scriptencoding utf-8 |
| 3275 | call setline(1, '口') |
| 3276 | redraw |
| 3277 | call assert_equal(0, screenattr(1, 1)) |
| 3278 | call assert_equal(char2nr('口'), screenchar(1, 1)) |
| 3279 | call assert_equal([char2nr('口')], screenchars(1, 1)) |
| 3280 | call assert_equal('口', screenstring(1, 1)) |
| 3281 | call writefile(v:errors, 'Xresult') |
| 3282 | qall! |
| 3283 | [CODE] |
| 3284 | |
| 3285 | let encodings = ['utf-8', 'cp932', 'cp936', 'cp949', 'cp950'] |
| 3286 | if !has('win32') |
| 3287 | let encodings += ['euc-jp'] |
| 3288 | endif |
| 3289 | for enc in encodings |
| 3290 | let msg = 'enc=' .. enc |
| 3291 | if RunVim([], after, $'--clean --cmd "set encoding={enc}"') |
| 3292 | call assert_equal([], readfile('Xresult'), msg) |
| 3293 | endif |
| 3294 | call delete('Xresult') |
| 3295 | endfor |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 3296 | endfunc |
| 3297 | |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 3298 | " Test for getcurpos() and setpos() |
| 3299 | func Test_getcurpos_setpos() |
| 3300 | new |
| 3301 | call setline(1, ['012345678', '012345678']) |
| 3302 | normal gg6l |
| 3303 | let sp = getcurpos() |
| 3304 | normal 0 |
| 3305 | call setpos('.', sp) |
| 3306 | normal jyl |
| 3307 | call assert_equal('6', @") |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 3308 | call assert_equal(-1, setpos('.', test_null_list())) |
| 3309 | call assert_equal(-1, setpos('.', {})) |
Bram Moolenaar | 99ca9c4 | 2020-09-22 21:55:41 +0200 | [diff] [blame] | 3310 | |
| 3311 | let winid = win_getid() |
| 3312 | normal G$ |
| 3313 | let pos = getcurpos() |
| 3314 | wincmd w |
| 3315 | call assert_equal(pos, getcurpos(winid)) |
| 3316 | |
| 3317 | wincmd w |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 3318 | close! |
Bram Moolenaar | 99ca9c4 | 2020-09-22 21:55:41 +0200 | [diff] [blame] | 3319 | |
| 3320 | call assert_equal(getcurpos(), getcurpos(0)) |
| 3321 | call assert_equal([0, 0, 0, 0, 0], getcurpos(-1)) |
| 3322 | call assert_equal([0, 0, 0, 0, 0], getcurpos(1999)) |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 3323 | endfunc |
| 3324 | |
Bram Moolenaar | 986b0fd | 2022-03-13 12:06:07 +0000 | [diff] [blame] | 3325 | func Test_getmousepos() |
| 3326 | enew! |
| 3327 | call setline(1, "\t\t\t1234") |
Bram Moolenaar | 533870a | 2022-03-13 15:52:44 +0000 | [diff] [blame] | 3328 | call test_setmouse(1, 1) |
| 3329 | call assert_equal(#{ |
| 3330 | \ screenrow: 1, |
| 3331 | \ screencol: 1, |
| 3332 | \ winid: win_getid(), |
| 3333 | \ winrow: 1, |
| 3334 | \ wincol: 1, |
| 3335 | \ line: 1, |
| 3336 | \ column: 1, |
| 3337 | \ }, getmousepos()) |
Bram Moolenaar | 986b0fd | 2022-03-13 12:06:07 +0000 | [diff] [blame] | 3338 | call test_setmouse(1, 25) |
| 3339 | call assert_equal(#{ |
| 3340 | \ screenrow: 1, |
| 3341 | \ screencol: 25, |
| 3342 | \ winid: win_getid(), |
| 3343 | \ winrow: 1, |
| 3344 | \ wincol: 25, |
| 3345 | \ line: 1, |
Bram Moolenaar | 533870a | 2022-03-13 15:52:44 +0000 | [diff] [blame] | 3346 | \ column: 4, |
Bram Moolenaar | 986b0fd | 2022-03-13 12:06:07 +0000 | [diff] [blame] | 3347 | \ }, getmousepos()) |
| 3348 | call test_setmouse(1, 50) |
| 3349 | call assert_equal(#{ |
| 3350 | \ screenrow: 1, |
| 3351 | \ screencol: 50, |
| 3352 | \ winid: win_getid(), |
| 3353 | \ winrow: 1, |
| 3354 | \ wincol: 50, |
| 3355 | \ line: 1, |
Bram Moolenaar | 533870a | 2022-03-13 15:52:44 +0000 | [diff] [blame] | 3356 | \ column: 8, |
Bram Moolenaar | 986b0fd | 2022-03-13 12:06:07 +0000 | [diff] [blame] | 3357 | \ }, getmousepos()) |
Sean Dewar | 10792fe | 2022-03-15 09:46:54 +0000 | [diff] [blame] | 3358 | |
| 3359 | " If the mouse is positioned past the last buffer line, "line" and "column" |
| 3360 | " should act like it's positioned on the last buffer line. |
| 3361 | call test_setmouse(2, 25) |
| 3362 | call assert_equal(#{ |
| 3363 | \ screenrow: 2, |
| 3364 | \ screencol: 25, |
| 3365 | \ winid: win_getid(), |
| 3366 | \ winrow: 2, |
| 3367 | \ wincol: 25, |
| 3368 | \ line: 1, |
| 3369 | \ column: 4, |
| 3370 | \ }, getmousepos()) |
| 3371 | call test_setmouse(2, 50) |
| 3372 | call assert_equal(#{ |
| 3373 | \ screenrow: 2, |
| 3374 | \ screencol: 50, |
| 3375 | \ winid: win_getid(), |
| 3376 | \ winrow: 2, |
| 3377 | \ wincol: 50, |
| 3378 | \ line: 1, |
| 3379 | \ column: 8, |
| 3380 | \ }, getmousepos()) |
Bram Moolenaar | 986b0fd | 2022-03-13 12:06:07 +0000 | [diff] [blame] | 3381 | bwipe! |
| 3382 | endfunc |
| 3383 | |
Bram Moolenaar | 24dc19c | 2022-11-14 19:49:15 +0000 | [diff] [blame] | 3384 | func Test_getmouseshape() |
| 3385 | CheckFeature mouseshape |
| 3386 | |
| 3387 | call assert_equal('arrow', getmouseshape()) |
| 3388 | endfunc |
| 3389 | |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 3390 | " Test for glob() |
| 3391 | func Test_glob() |
| 3392 | call assert_equal('', glob(test_null_string())) |
| 3393 | call assert_equal('', globpath(test_null_string(), test_null_string())) |
Yegappan Lakshmanan | 46aa6f9 | 2021-05-19 17:15:04 +0200 | [diff] [blame] | 3394 | call assert_fails("let x = globpath(&rtp, 'syntax/c.vim', [])", 'E745:') |
Bram Moolenaar | 1b04ce2 | 2020-08-21 22:46:11 +0200 | [diff] [blame] | 3395 | |
| 3396 | call writefile([], 'Xglob1') |
| 3397 | call writefile([], 'XGLOB2') |
| 3398 | set wildignorecase |
| 3399 | " Sort output of glob() otherwise we end up with different |
| 3400 | " ordering depending on whether file system is case-sensitive. |
| 3401 | call assert_equal(['XGLOB2', 'Xglob1'], sort(glob('Xglob[12]', 0, 1))) |
LemonBoy | a3157a4 | 2022-04-03 11:58:31 +0100 | [diff] [blame] | 3402 | " wildignorecase shall be applied even when the pattern contains no wildcards. |
| 3403 | call assert_equal('XGLOB2', glob('xglob2')) |
Bram Moolenaar | 1b04ce2 | 2020-08-21 22:46:11 +0200 | [diff] [blame] | 3404 | set wildignorecase& |
| 3405 | |
| 3406 | call delete('Xglob1') |
| 3407 | call delete('XGLOB2') |
| 3408 | |
| 3409 | call assert_fails("call glob('*', 0, {})", 'E728:') |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 3410 | endfunc |
| 3411 | |
| 3412 | " Test for browse() |
| 3413 | func Test_browse() |
| 3414 | CheckFeature browse |
| 3415 | call assert_fails('call browse([], "open", "x", "a.c")', 'E745:') |
| 3416 | endfunc |
| 3417 | |
| 3418 | " Test for browsedir() |
| 3419 | func Test_browsedir() |
| 3420 | CheckFeature browse |
| 3421 | call assert_fails('call browsedir("open", [])', 'E730:') |
| 3422 | endfunc |
| 3423 | |
Bram Moolenaar | b47bed2 | 2021-04-14 17:06:43 +0200 | [diff] [blame] | 3424 | func HasDefault(msg = 'msg') |
| 3425 | return a:msg |
| 3426 | endfunc |
| 3427 | |
| 3428 | func Test_default_arg_value() |
| 3429 | call assert_equal('msg', HasDefault()) |
| 3430 | endfunc |
| 3431 | |
Yegappan Lakshmanan | 34fcb69 | 2021-05-25 20:14:00 +0200 | [diff] [blame] | 3432 | " Test for gettext() |
| 3433 | func Test_gettext() |
Yegappan Lakshmanan | 8deb2b3 | 2022-09-02 15:15:27 +0100 | [diff] [blame] | 3434 | call assert_fails('call gettext(1)', 'E1174:') |
Yegappan Lakshmanan | 34fcb69 | 2021-05-25 20:14:00 +0200 | [diff] [blame] | 3435 | endfunc |
| 3436 | |
Bram Moolenaar | 3d9c4ee | 2021-05-31 22:15:26 +0200 | [diff] [blame] | 3437 | func Test_builtin_check() |
| 3438 | call assert_fails('let g:["trim"] = {x -> " " .. x}', 'E704:') |
| 3439 | call assert_fails('let g:.trim = {x -> " " .. x}', 'E704:') |
Bram Moolenaar | b54abee | 2021-06-02 11:49:23 +0200 | [diff] [blame] | 3440 | call assert_fails('let l:["trim"] = {x -> " " .. x}', 'E704:') |
| 3441 | call assert_fails('let l:.trim = {x -> " " .. x}', 'E704:') |
| 3442 | let lines =<< trim END |
| 3443 | vim9script |
Bram Moolenaar | 62b191c | 2022-02-12 20:34:50 +0000 | [diff] [blame] | 3444 | var trim = (x) => " " .. x |
Bram Moolenaar | b54abee | 2021-06-02 11:49:23 +0200 | [diff] [blame] | 3445 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 3446 | call v9.CheckScriptFailure(lines, 'E704:') |
Bram Moolenaar | 6f1d2aa | 2021-06-01 21:21:55 +0200 | [diff] [blame] | 3447 | |
| 3448 | call assert_fails('call extend(g:, #{foo: { -> "foo" }})', 'E704:') |
| 3449 | let g:bar = 123 |
| 3450 | call extend(g:, #{bar: { -> "foo" }}, "keep") |
| 3451 | call assert_fails('call extend(g:, #{bar: { -> "foo" }}, "force")', 'E704:') |
zeertzjq | 91c75d1 | 2022-11-05 20:21:58 +0000 | [diff] [blame] | 3452 | unlet g:bar |
| 3453 | |
| 3454 | call assert_fails('call extend(l:, #{foo: { -> "foo" }})', 'E704:') |
| 3455 | let bar = 123 |
| 3456 | call extend(l:, #{bar: { -> "foo" }}, "keep") |
| 3457 | call assert_fails('call extend(l:, #{bar: { -> "foo" }}, "force")', 'E704:') |
| 3458 | unlet bar |
| 3459 | |
| 3460 | call assert_fails('call extend(g:, #{foo: function("extend")})', 'E704:') |
| 3461 | let g:bar = 123 |
| 3462 | call extend(g:, #{bar: function("extend")}, "keep") |
| 3463 | call assert_fails('call extend(g:, #{bar: function("extend")}, "force")', 'E704:') |
| 3464 | unlet g:bar |
| 3465 | |
| 3466 | call assert_fails('call extend(l:, #{foo: function("extend")})', 'E704:') |
| 3467 | let bar = 123 |
| 3468 | call extend(l:, #{bar: function("extend")}, "keep") |
| 3469 | call assert_fails('call extend(l:, #{bar: function("extend")}, "force")', 'E704:') |
| 3470 | unlet bar |
Bram Moolenaar | 3d9c4ee | 2021-05-31 22:15:26 +0200 | [diff] [blame] | 3471 | endfunc |
| 3472 | |
Bram Moolenaar | c4ec338 | 2021-12-09 16:40:18 +0000 | [diff] [blame] | 3473 | func Test_funcref_to_string() |
| 3474 | let Fn = funcref('g:Test_funcref_to_string') |
| 3475 | call assert_equal("function('g:Test_funcref_to_string')", string(Fn)) |
| 3476 | endfunc |
| 3477 | |
LemonBoy | dca1d40 | 2022-04-28 15:26:33 +0100 | [diff] [blame] | 3478 | " Test for isabsolutepath() |
| 3479 | func Test_isabsolutepath() |
| 3480 | call assert_false(isabsolutepath('')) |
| 3481 | call assert_false(isabsolutepath('.')) |
| 3482 | call assert_false(isabsolutepath('../Foo')) |
| 3483 | call assert_false(isabsolutepath('Foo/')) |
| 3484 | if has('win32') |
| 3485 | call assert_true(isabsolutepath('A:\')) |
| 3486 | call assert_true(isabsolutepath('A:\Foo')) |
| 3487 | call assert_true(isabsolutepath('A:/Foo')) |
| 3488 | call assert_false(isabsolutepath('A:Foo')) |
| 3489 | call assert_false(isabsolutepath('\Windows')) |
| 3490 | call assert_true(isabsolutepath('\\Server2\Share\Test\Foo.txt')) |
| 3491 | else |
| 3492 | call assert_true(isabsolutepath('/')) |
| 3493 | call assert_true(isabsolutepath('/usr/share/')) |
| 3494 | endif |
| 3495 | endfunc |
Bram Moolenaar | 3d9c4ee | 2021-05-31 22:15:26 +0200 | [diff] [blame] | 3496 | |
Yasuhiro Matsumoto | 05cf63e | 2022-05-03 11:02:28 +0100 | [diff] [blame] | 3497 | " Test for exepath() |
| 3498 | func Test_exepath() |
| 3499 | if has('win32') |
| 3500 | call assert_notequal(exepath('cmd'), '') |
| 3501 | |
| 3502 | let oldNoDefaultCurrentDirectoryInExePath = $NoDefaultCurrentDirectoryInExePath |
| 3503 | call writefile(['@echo off', 'echo Evil'], 'vim-test-evil.bat') |
| 3504 | let $NoDefaultCurrentDirectoryInExePath = '' |
| 3505 | call assert_notequal(exepath("vim-test-evil.bat"), '') |
| 3506 | let $NoDefaultCurrentDirectoryInExePath = '1' |
| 3507 | call assert_equal(exepath("vim-test-evil.bat"), '') |
| 3508 | let $NoDefaultCurrentDirectoryInExePath = oldNoDefaultCurrentDirectoryInExePath |
| 3509 | call delete('vim-test-evil.bat') |
| 3510 | else |
| 3511 | call assert_notequal(exepath('sh'), '') |
| 3512 | endif |
| 3513 | endfunc |
| 3514 | |
LemonBoy | 0f7a3e1 | 2022-05-26 12:10:37 +0100 | [diff] [blame] | 3515 | " Test for virtcol() |
| 3516 | func Test_virtcol() |
| 3517 | enew! |
| 3518 | call setline(1, "the\tquick\tbrown\tfox") |
| 3519 | norm! 4| |
| 3520 | call assert_equal(8, virtcol('.')) |
| 3521 | call assert_equal(8, virtcol('.', v:false)) |
| 3522 | call assert_equal([4, 8], virtcol('.', v:true)) |
| 3523 | bwipe! |
| 3524 | endfunc |
| 3525 | |
Bram Moolenaar | 398a26f | 2022-11-13 22:13:33 +0000 | [diff] [blame] | 3526 | func Test_delfunc_while_listing() |
| 3527 | CheckRunVimInTerminal |
| 3528 | |
| 3529 | let lines =<< trim END |
| 3530 | set nocompatible |
| 3531 | for i in range(1, 999) |
| 3532 | exe 'func ' .. 'MyFunc' .. i .. '()' |
| 3533 | endfunc |
| 3534 | endfor |
| 3535 | au CmdlineLeave : call timer_start(0, {-> execute('delfunc MyFunc622')}) |
| 3536 | END |
| 3537 | call writefile(lines, 'Xfunctionclear', 'D') |
| 3538 | let buf = RunVimInTerminal('-S Xfunctionclear', {'rows': 12}) |
| 3539 | |
| 3540 | " This was using freed memory. The height of the terminal must be so that |
| 3541 | " the next function to be listed with "j" is the one that is deleted in the |
| 3542 | " timer callback, tricky! |
| 3543 | call term_sendkeys(buf, ":func /MyFunc\<CR>") |
| 3544 | call TermWait(buf, 50) |
| 3545 | call term_sendkeys(buf, "j") |
| 3546 | call TermWait(buf, 50) |
| 3547 | call term_sendkeys(buf, "\<CR>") |
| 3548 | |
| 3549 | call StopVimInTerminal(buf) |
| 3550 | endfunc |
| 3551 | |
Yegappan Lakshmanan | 03ff1c2 | 2023-05-06 14:08:21 +0100 | [diff] [blame] | 3552 | " Test for the reverse() function with a string |
| 3553 | func Test_string_reverse() |
Yegappan Lakshmanan | f9dc278 | 2023-05-11 15:02:56 +0100 | [diff] [blame] | 3554 | let lines =<< trim END |
| 3555 | call assert_equal('', reverse(test_null_string())) |
| 3556 | for [s1, s2] in [['', ''], ['a', 'a'], ['ab', 'ba'], ['abc', 'cba'], |
| 3557 | \ ['abcd', 'dcba'], ['«-«-»-»', '»-»-«-«'], |
| 3558 | \ ['🇦', '🇦'], ['🇦🇧', '🇧🇦'], ['🇦🇧🇨', '🇨🇧🇦'], |
| 3559 | \ ['🇦«🇧-🇨»🇩', '🇩»🇨-🇧«🇦']] |
| 3560 | call assert_equal(s2, reverse(s1)) |
| 3561 | endfor |
| 3562 | END |
| 3563 | call v9.CheckLegacyAndVim9Success(lines) |
Yegappan Lakshmanan | 03ff1c2 | 2023-05-06 14:08:21 +0100 | [diff] [blame] | 3564 | |
| 3565 | " test in latin1 encoding |
| 3566 | let save_enc = &encoding |
| 3567 | set encoding=latin1 |
| 3568 | call assert_equal('dcba', reverse('abcd')) |
| 3569 | let &encoding = save_enc |
| 3570 | endfunc |
| 3571 | |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 3572 | " vim: shiftwidth=2 sts=2 expandtab |