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 | b54abee | 2021-06-02 11:49:23 +0200 | [diff] [blame] | 7 | source vim9.vim |
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')) |
| 33 | endif |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 34 | call assert_equal(1, has('vcon', 1)) |
| 35 | call assert_equal(1, has('mouse_gpm_enabled', 1)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 36 | |
Bram Moolenaar | 7929651 | 2020-03-22 16:17:14 +0100 | [diff] [blame] | 37 | call assert_equal(0, has('nonexistent')) |
| 38 | call assert_equal(0, has('nonexistent', 1)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 39 | |
| 40 | " Will we ever have patch 9999? |
| 41 | let ver = 'patch-' .. v:version / 100 .. '.' .. v:version % 100 .. '.9999' |
| 42 | call assert_equal(0, has(ver)) |
Bram Moolenaar | 7929651 | 2020-03-22 16:17:14 +0100 | [diff] [blame] | 43 | endfunc |
| 44 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 45 | func Test_empty() |
| 46 | call assert_equal(1, empty('')) |
| 47 | call assert_equal(0, empty('a')) |
| 48 | |
| 49 | call assert_equal(1, empty(0)) |
| 50 | call assert_equal(1, empty(-0)) |
| 51 | call assert_equal(0, empty(1)) |
| 52 | call assert_equal(0, empty(-1)) |
| 53 | |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 54 | if has('float') |
| 55 | call assert_equal(1, empty(0.0)) |
| 56 | call assert_equal(1, empty(-0.0)) |
| 57 | call assert_equal(0, empty(1.0)) |
| 58 | call assert_equal(0, empty(-1.0)) |
| 59 | call assert_equal(0, empty(1.0/0.0)) |
| 60 | call assert_equal(0, empty(0.0/0.0)) |
| 61 | endif |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 62 | |
| 63 | call assert_equal(1, empty([])) |
| 64 | call assert_equal(0, empty(['a'])) |
| 65 | |
| 66 | call assert_equal(1, empty({})) |
| 67 | call assert_equal(0, empty({'a':1})) |
| 68 | |
| 69 | call assert_equal(1, empty(v:null)) |
| 70 | call assert_equal(1, empty(v:none)) |
| 71 | call assert_equal(1, empty(v:false)) |
| 72 | call assert_equal(0, empty(v:true)) |
| 73 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 74 | if has('channel') |
| 75 | call assert_equal(1, empty(test_null_channel())) |
| 76 | endif |
| 77 | if has('job') |
| 78 | call assert_equal(1, empty(test_null_job())) |
| 79 | endif |
| 80 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 81 | call assert_equal(0, empty(function('Test_empty'))) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 82 | call assert_equal(0, empty(function('Test_empty', [0]))) |
Bram Moolenaar | 7c215c5 | 2020-02-29 13:43:27 +0100 | [diff] [blame] | 83 | |
| 84 | call assert_fails("call empty(test_void())", 'E685:') |
| 85 | call assert_fails("call empty(test_unknown())", 'E685:') |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 86 | endfunc |
| 87 | |
Bram Moolenaar | dd58923 | 2020-02-29 17:38:12 +0100 | [diff] [blame] | 88 | func Test_test_void() |
| 89 | call assert_fails('echo 1 == test_void()', 'E685:') |
| 90 | if has('float') |
| 91 | call assert_fails('echo 1.0 == test_void()', 'E685:') |
| 92 | endif |
| 93 | call assert_fails('let x = json_encode(test_void())', 'E685:') |
| 94 | call assert_fails('let x = copy(test_void())', 'E685:') |
| 95 | call assert_fails('let x = copy([test_void()])', 'E685:') |
| 96 | endfunc |
| 97 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 98 | func Test_len() |
| 99 | call assert_equal(1, len(0)) |
| 100 | call assert_equal(2, len(12)) |
| 101 | |
| 102 | call assert_equal(0, len('')) |
| 103 | call assert_equal(2, len('ab')) |
| 104 | |
| 105 | call assert_equal(0, len([])) |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 106 | call assert_equal(0, len(test_null_list())) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 107 | call assert_equal(2, len([2, 1])) |
| 108 | |
| 109 | call assert_equal(0, len({})) |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 110 | call assert_equal(0, len(test_null_dict())) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 111 | call assert_equal(2, len({'a': 1, 'b': 2})) |
| 112 | |
| 113 | call assert_fails('call len(v:none)', 'E701:') |
| 114 | call assert_fails('call len({-> 0})', 'E701:') |
| 115 | endfunc |
| 116 | |
| 117 | func Test_max() |
| 118 | call assert_equal(0, max([])) |
| 119 | call assert_equal(2, max([2])) |
| 120 | call assert_equal(2, max([1, 2])) |
| 121 | call assert_equal(2, max([1, 2, v:null])) |
| 122 | |
| 123 | call assert_equal(0, max({})) |
| 124 | call assert_equal(2, max({'a':1, 'b':2})) |
| 125 | |
| 126 | call assert_fails('call max(1)', 'E712:') |
| 127 | call assert_fails('call max(v:none)', 'E712:') |
Bram Moolenaar | ab65fc7 | 2021-02-04 22:07:16 +0100 | [diff] [blame] | 128 | |
| 129 | " check we only get one error |
| 130 | call assert_fails('call max([#{}, [1]])', ['E728:', 'E728:']) |
| 131 | call assert_fails('call max(#{a: {}, b: [1]})', ['E728:', 'E728:']) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 132 | endfunc |
| 133 | |
| 134 | func Test_min() |
| 135 | call assert_equal(0, min([])) |
| 136 | call assert_equal(2, min([2])) |
| 137 | call assert_equal(1, min([1, 2])) |
| 138 | call assert_equal(0, min([1, 2, v:null])) |
| 139 | |
| 140 | call assert_equal(0, min({})) |
| 141 | call assert_equal(1, min({'a':1, 'b':2})) |
| 142 | |
| 143 | call assert_fails('call min(1)', 'E712:') |
| 144 | call assert_fails('call min(v:none)', 'E712:') |
Yegappan Lakshmanan | 34fcb69 | 2021-05-25 20:14:00 +0200 | [diff] [blame] | 145 | call assert_fails('call min([1, {}])', 'E728:') |
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 min([[1], #{}])', ['E745:', 'E745:']) |
| 149 | call assert_fails('call min(#{a: [1], b: #{}})', ['E745:', 'E745:']) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 150 | endfunc |
| 151 | |
Bram Moolenaar | 42ab17b | 2018-05-20 14:11:10 +0200 | [diff] [blame] | 152 | func Test_strwidth() |
| 153 | for aw in ['single', 'double'] |
| 154 | exe 'set ambiwidth=' . aw |
| 155 | call assert_equal(0, strwidth('')) |
| 156 | call assert_equal(1, strwidth("\t")) |
| 157 | call assert_equal(3, strwidth('Vim')) |
| 158 | call assert_equal(4, strwidth(1234)) |
| 159 | call assert_equal(5, strwidth(-1234)) |
| 160 | |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 161 | call assert_equal(2, strwidth('😉')) |
| 162 | call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde')) |
| 163 | call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße')) |
Bram Moolenaar | 42ab17b | 2018-05-20 14:11:10 +0200 | [diff] [blame] | 164 | |
| 165 | call assert_fails('call strwidth({->0})', 'E729:') |
| 166 | call assert_fails('call strwidth([])', 'E730:') |
| 167 | call assert_fails('call strwidth({})', 'E731:') |
Bram Moolenaar | 42ab17b | 2018-05-20 14:11:10 +0200 | [diff] [blame] | 168 | endfor |
| 169 | |
Bram Moolenaar | 3cfa5b1 | 2021-06-06 14:14:39 +0200 | [diff] [blame] | 170 | if has('float') |
| 171 | call assert_equal(3, strwidth(1.2)) |
| 172 | call CheckDefExecAndScriptFailure(['echo strwidth(1.2)'], 'E806:') |
| 173 | endif |
| 174 | |
Bram Moolenaar | 42ab17b | 2018-05-20 14:11:10 +0200 | [diff] [blame] | 175 | set ambiwidth& |
| 176 | endfunc |
| 177 | |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 178 | func Test_str2nr() |
| 179 | call assert_equal(0, str2nr('')) |
| 180 | call assert_equal(1, str2nr('1')) |
| 181 | call assert_equal(1, str2nr(' 1 ')) |
| 182 | |
| 183 | call assert_equal(1, str2nr('+1')) |
| 184 | call assert_equal(1, str2nr('+ 1')) |
| 185 | call assert_equal(1, str2nr(' + 1 ')) |
| 186 | |
| 187 | call assert_equal(-1, str2nr('-1')) |
| 188 | call assert_equal(-1, str2nr('- 1')) |
| 189 | call assert_equal(-1, str2nr(' - 1 ')) |
| 190 | |
| 191 | call assert_equal(123456789, str2nr('123456789')) |
| 192 | call assert_equal(-123456789, str2nr('-123456789')) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 193 | |
| 194 | call assert_equal(5, str2nr('101', 2)) |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 195 | call assert_equal(5, '0b101'->str2nr(2)) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 196 | call assert_equal(5, str2nr('0B101', 2)) |
| 197 | call assert_equal(-5, str2nr('-101', 2)) |
| 198 | call assert_equal(-5, str2nr('-0b101', 2)) |
| 199 | call assert_equal(-5, str2nr('-0B101', 2)) |
| 200 | |
| 201 | call assert_equal(65, str2nr('101', 8)) |
| 202 | call assert_equal(65, str2nr('0101', 8)) |
| 203 | call assert_equal(-65, str2nr('-101', 8)) |
| 204 | call assert_equal(-65, str2nr('-0101', 8)) |
Bram Moolenaar | c17e66c | 2020-06-02 21:38:22 +0200 | [diff] [blame] | 205 | call assert_equal(65, str2nr('0o101', 8)) |
| 206 | call assert_equal(65, str2nr('0O0101', 8)) |
| 207 | call assert_equal(-65, str2nr('-0O101', 8)) |
| 208 | call assert_equal(-65, str2nr('-0o0101', 8)) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 209 | |
| 210 | call assert_equal(11259375, str2nr('abcdef', 16)) |
| 211 | call assert_equal(11259375, str2nr('ABCDEF', 16)) |
| 212 | call assert_equal(-11259375, str2nr('-ABCDEF', 16)) |
| 213 | call assert_equal(11259375, str2nr('0xabcdef', 16)) |
| 214 | call assert_equal(11259375, str2nr('0Xabcdef', 16)) |
| 215 | call assert_equal(11259375, str2nr('0XABCDEF', 16)) |
| 216 | call assert_equal(-11259375, str2nr('-0xABCDEF', 16)) |
| 217 | |
Bram Moolenaar | 60a8de2 | 2019-09-15 14:33:22 +0200 | [diff] [blame] | 218 | call assert_equal(1, str2nr("1'000'000", 10, 0)) |
| 219 | call assert_equal(256, str2nr("1'0000'0000", 2, 1)) |
| 220 | call assert_equal(262144, str2nr("1'000'000", 8, 1)) |
| 221 | call assert_equal(1000000, str2nr("1'000'000", 10, 1)) |
Bram Moolenaar | ea8dcf8 | 2019-09-15 21:12:22 +0200 | [diff] [blame] | 222 | call assert_equal(1000, str2nr("1'000''000", 10, 1)) |
Bram Moolenaar | 60a8de2 | 2019-09-15 14:33:22 +0200 | [diff] [blame] | 223 | call assert_equal(65536, str2nr("1'00'00", 16, 1)) |
| 224 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 225 | call assert_equal(0, str2nr('0x10')) |
| 226 | call assert_equal(0, str2nr('0b10')) |
Bram Moolenaar | c17e66c | 2020-06-02 21:38:22 +0200 | [diff] [blame] | 227 | call assert_equal(0, str2nr('0o10')) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 228 | call assert_equal(1, str2nr('12', 2)) |
| 229 | call assert_equal(1, str2nr('18', 8)) |
| 230 | call assert_equal(1, str2nr('1g', 16)) |
| 231 | |
| 232 | call assert_equal(0, str2nr(v:null)) |
| 233 | call assert_equal(0, str2nr(v:none)) |
| 234 | |
| 235 | call assert_fails('call str2nr([])', 'E730:') |
| 236 | call assert_fails('call str2nr({->2})', 'E729:') |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 237 | if has('float') |
Bram Moolenaar | 3cfa5b1 | 2021-06-06 14:14:39 +0200 | [diff] [blame] | 238 | call assert_equal(1, str2nr(1.2)) |
| 239 | call CheckDefExecFailure(['echo str2nr(1.2)'], 'E1013:') |
| 240 | call CheckScriptFailure(['vim9script', 'echo str2nr(1.2)'], 'E806:') |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 241 | endif |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 242 | call assert_fails('call str2nr(10, [])', 'E745:') |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 243 | endfunc |
| 244 | |
| 245 | func Test_strftime() |
Bram Moolenaar | 10455d4 | 2019-11-21 15:36:18 +0100 | [diff] [blame] | 246 | CheckFunction strftime |
| 247 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 248 | " Format of strftime() depends on system. We assume |
| 249 | " that basic formats tested here are available and |
| 250 | " identical on all systems which support strftime(). |
| 251 | " |
| 252 | " The 2nd parameter of strftime() is a local time, so the output day |
| 253 | " of strftime() can be 17 or 18, depending on timezone. |
| 254 | call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512)) |
| 255 | " |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 256 | 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] | 257 | |
| 258 | call assert_fails('call strftime([])', 'E730:') |
| 259 | call assert_fails('call strftime("%Y", [])', 'E745:') |
Bram Moolenaar | db51730 | 2019-06-18 22:53:24 +0200 | [diff] [blame] | 260 | |
| 261 | " Check that the time changes after we change the timezone |
| 262 | " Save previous timezone value, if any |
| 263 | if exists('$TZ') |
| 264 | let tz = $TZ |
| 265 | endif |
| 266 | |
| 267 | " Force EST and then UTC, save the current hour (24-hour clock) for each |
| 268 | let $TZ = 'EST' | let est = strftime('%H') |
| 269 | let $TZ = 'UTC' | let utc = strftime('%H') |
| 270 | |
| 271 | " Those hours should be two bytes long, and should not be the same; if they |
| 272 | " are, a tzset(3) call may have failed somewhere |
| 273 | call assert_equal(strlen(est), 2) |
| 274 | call assert_equal(strlen(utc), 2) |
Bram Moolenaar | 87652a7 | 2019-06-18 23:07:37 +0200 | [diff] [blame] | 275 | " TODO: this fails on MS-Windows |
| 276 | if has('unix') |
| 277 | call assert_notequal(est, utc) |
| 278 | endif |
Bram Moolenaar | db51730 | 2019-06-18 22:53:24 +0200 | [diff] [blame] | 279 | |
| 280 | " If we cached a timezone value, put it back, otherwise clear it |
| 281 | if exists('tz') |
| 282 | let $TZ = tz |
| 283 | else |
| 284 | unlet $TZ |
| 285 | endif |
Bram Moolenaar | 10455d4 | 2019-11-21 15:36:18 +0100 | [diff] [blame] | 286 | endfunc |
Bram Moolenaar | db51730 | 2019-06-18 22:53:24 +0200 | [diff] [blame] | 287 | |
Bram Moolenaar | 10455d4 | 2019-11-21 15:36:18 +0100 | [diff] [blame] | 288 | func Test_strptime() |
| 289 | CheckFunction strptime |
| 290 | |
| 291 | if exists('$TZ') |
| 292 | let tz = $TZ |
| 293 | endif |
| 294 | let $TZ = 'UTC' |
| 295 | |
Bram Moolenaar | 9a838fe | 2019-12-06 12:45:01 +0100 | [diff] [blame] | 296 | 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] | 297 | |
Bram Moolenaar | ea1233f | 2020-06-10 16:54:13 +0200 | [diff] [blame] | 298 | " Force DST and check that it's considered |
| 299 | let $TZ = 'WINTER0SUMMER,J1,J365' |
| 300 | call assert_equal(1484653763 - 3600, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23')) |
| 301 | |
Bram Moolenaar | 10455d4 | 2019-11-21 15:36:18 +0100 | [diff] [blame] | 302 | call assert_fails('call strptime()', 'E119:') |
| 303 | call assert_fails('call strptime("xxx")', 'E119:') |
| 304 | call assert_equal(0, strptime("%Y", '')) |
| 305 | call assert_equal(0, strptime("%Y", "xxx")) |
| 306 | |
| 307 | if exists('tz') |
| 308 | let $TZ = tz |
| 309 | else |
| 310 | unlet $TZ |
| 311 | endif |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 312 | endfunc |
| 313 | |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 314 | func Test_resolve_unix() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 315 | CheckUnix |
Bram Moolenaar | 2610990 | 2018-10-06 15:43:17 +0200 | [diff] [blame] | 316 | |
| 317 | " Xlink1 -> Xlink2 |
| 318 | " Xlink2 -> Xlink3 |
| 319 | silent !ln -s -f Xlink2 Xlink1 |
| 320 | silent !ln -s -f Xlink3 Xlink2 |
| 321 | call assert_equal('Xlink3', resolve('Xlink1')) |
| 322 | call assert_equal('./Xlink3', resolve('./Xlink1')) |
| 323 | call assert_equal('Xlink3/', resolve('Xlink2/')) |
| 324 | " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?! |
| 325 | "call assert_equal('Xlink3/', resolve('Xlink1/')) |
| 326 | "call assert_equal('./Xlink3/', resolve('./Xlink1/')) |
| 327 | "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/')) |
| 328 | call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1')) |
| 329 | |
| 330 | " Test resolve() with a symlink cycle. |
| 331 | " Xlink1 -> Xlink2 |
| 332 | " Xlink2 -> Xlink3 |
| 333 | " Xlink3 -> Xlink1 |
| 334 | silent !ln -s -f Xlink1 Xlink3 |
| 335 | call assert_fails('call resolve("Xlink1")', 'E655:') |
| 336 | call assert_fails('call resolve("./Xlink1")', 'E655:') |
| 337 | call assert_fails('call resolve("Xlink2")', 'E655:') |
| 338 | call assert_fails('call resolve("Xlink3")', 'E655:') |
| 339 | call delete('Xlink1') |
| 340 | call delete('Xlink2') |
| 341 | call delete('Xlink3') |
| 342 | |
| 343 | silent !ln -s -f Xdir//Xfile Xlink |
| 344 | call assert_equal('Xdir/Xfile', resolve('Xlink')) |
| 345 | call delete('Xlink') |
| 346 | |
| 347 | silent !ln -s -f Xlink2/ Xlink1 |
Bram Moolenaar | a0d1fef | 2019-09-04 22:29:14 +0200 | [diff] [blame] | 348 | call assert_equal('Xlink2', 'Xlink1'->resolve()) |
Bram Moolenaar | 2610990 | 2018-10-06 15:43:17 +0200 | [diff] [blame] | 349 | call assert_equal('Xlink2/', resolve('Xlink1/')) |
| 350 | call delete('Xlink1') |
| 351 | |
| 352 | silent !ln -s -f ./Xlink2 Xlink1 |
| 353 | call assert_equal('Xlink2', resolve('Xlink1')) |
| 354 | call assert_equal('./Xlink2', resolve('./Xlink1')) |
| 355 | call delete('Xlink1') |
Bram Moolenaar | 50c4e9e | 2020-10-05 20:38:06 +0200 | [diff] [blame] | 356 | |
| 357 | call assert_equal('/', resolve('/')) |
Bram Moolenaar | 2610990 | 2018-10-06 15:43:17 +0200 | [diff] [blame] | 358 | endfunc |
| 359 | |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 360 | func s:normalize_fname(fname) |
| 361 | let ret = substitute(a:fname, '\', '/', 'g') |
| 362 | let ret = substitute(ret, '//', '/', 'g') |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 363 | return ret->tolower() |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 364 | endfunc |
| 365 | |
| 366 | func Test_resolve_win32() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 367 | CheckMSWindows |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 368 | |
| 369 | " test for shortcut file |
| 370 | if executable('cscript') |
| 371 | new Xfile |
| 372 | wq |
Bram Moolenaar | e7eb927 | 2019-06-24 00:58:07 +0200 | [diff] [blame] | 373 | let lines =<< trim END |
| 374 | Set fs = CreateObject("Scripting.FileSystemObject") |
| 375 | Set ws = WScript.CreateObject("WScript.Shell") |
| 376 | Set shortcut = ws.CreateShortcut("Xlink.lnk") |
| 377 | shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile") |
| 378 | shortcut.Save |
| 379 | END |
| 380 | call writefile(lines, 'link.vbs') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 381 | silent !cscript link.vbs |
| 382 | call delete('link.vbs') |
| 383 | call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk'))) |
| 384 | call delete('Xfile') |
| 385 | |
| 386 | call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk'))) |
| 387 | call delete('Xlink.lnk') |
| 388 | else |
| 389 | echomsg 'skipped test for shortcut file' |
| 390 | endif |
| 391 | |
| 392 | " remove files |
| 393 | call delete('Xlink') |
| 394 | call delete('Xdir', 'd') |
| 395 | call delete('Xfile') |
| 396 | |
| 397 | " test for symbolic link to a file |
| 398 | new Xfile |
| 399 | wq |
Bram Moolenaar | 4a792c8 | 2019-06-06 12:22:41 +0200 | [diff] [blame] | 400 | call assert_equal('Xfile', resolve('Xfile')) |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 401 | silent !mklink Xlink Xfile |
| 402 | if !v:shell_error |
| 403 | call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink'))) |
| 404 | call delete('Xlink') |
| 405 | else |
| 406 | echomsg 'skipped test for symbolic link to a file' |
| 407 | endif |
| 408 | call delete('Xfile') |
| 409 | |
| 410 | " test for junction to a directory |
| 411 | call mkdir('Xdir') |
| 412 | silent !mklink /J Xlink Xdir |
| 413 | if !v:shell_error |
| 414 | call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) |
| 415 | |
| 416 | call delete('Xdir', 'd') |
| 417 | |
| 418 | " test for junction already removed |
| 419 | call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) |
| 420 | call delete('Xlink') |
| 421 | else |
| 422 | echomsg 'skipped test for junction to a directory' |
| 423 | call delete('Xdir', 'd') |
| 424 | endif |
| 425 | |
| 426 | " test for symbolic link to a directory |
| 427 | call mkdir('Xdir') |
| 428 | silent !mklink /D Xlink Xdir |
| 429 | if !v:shell_error |
| 430 | call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) |
| 431 | |
| 432 | call delete('Xdir', 'd') |
| 433 | |
| 434 | " test for symbolic link already removed |
| 435 | call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) |
| 436 | call delete('Xlink') |
| 437 | else |
| 438 | echomsg 'skipped test for symbolic link to a directory' |
| 439 | call delete('Xdir', 'd') |
| 440 | endif |
| 441 | |
| 442 | " test for buffer name |
| 443 | new Xfile |
| 444 | wq |
| 445 | silent !mklink Xlink Xfile |
| 446 | if !v:shell_error |
| 447 | edit Xlink |
| 448 | call assert_equal('Xlink', bufname('%')) |
| 449 | call delete('Xlink') |
| 450 | bw! |
| 451 | else |
| 452 | echomsg 'skipped test for buffer name' |
| 453 | endif |
| 454 | call delete('Xfile') |
Bram Moolenaar | 1bbebab | 2019-05-29 20:36:54 +0200 | [diff] [blame] | 455 | |
| 456 | " test for reparse point |
| 457 | call mkdir('Xdir') |
Bram Moolenaar | 4a792c8 | 2019-06-06 12:22:41 +0200 | [diff] [blame] | 458 | call assert_equal('Xdir', resolve('Xdir')) |
Bram Moolenaar | 1bbebab | 2019-05-29 20:36:54 +0200 | [diff] [blame] | 459 | silent !mklink /D Xdirlink Xdir |
| 460 | if !v:shell_error |
| 461 | w Xdir/text.txt |
Bram Moolenaar | 4a792c8 | 2019-06-06 12:22:41 +0200 | [diff] [blame] | 462 | call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt')) |
Bram Moolenaar | 1bbebab | 2019-05-29 20:36:54 +0200 | [diff] [blame] | 463 | call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt'))) |
| 464 | 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] | 465 | call delete('Xdirlink') |
Bram Moolenaar | 1bbebab | 2019-05-29 20:36:54 +0200 | [diff] [blame] | 466 | else |
| 467 | echomsg 'skipped test for reparse point' |
| 468 | endif |
| 469 | |
| 470 | call delete('Xdir', 'rf') |
Bram Moolenaar | dce1e89 | 2019-02-10 23:18:53 +0100 | [diff] [blame] | 471 | endfunc |
| 472 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 473 | func Test_simplify() |
| 474 | call assert_equal('', simplify('')) |
| 475 | call assert_equal('/', simplify('/')) |
| 476 | call assert_equal('/', simplify('/.')) |
| 477 | call assert_equal('/', simplify('/..')) |
| 478 | call assert_equal('/...', simplify('/...')) |
Bram Moolenaar | fdcbe3c | 2020-06-15 21:41:56 +0200 | [diff] [blame] | 479 | call assert_equal('//path', simplify('//path')) |
Bram Moolenaar | c70222d | 2020-06-15 23:18:12 +0200 | [diff] [blame] | 480 | if has('unix') |
| 481 | call assert_equal('/path', simplify('///path')) |
| 482 | call assert_equal('/path', simplify('////path')) |
| 483 | endif |
Bram Moolenaar | fdcbe3c | 2020-06-15 21:41:56 +0200 | [diff] [blame] | 484 | |
Bram Moolenaar | 7035fd9 | 2020-04-08 20:03:52 +0200 | [diff] [blame] | 485 | call assert_equal('./dir/file', './dir/file'->simplify()) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 486 | call assert_equal('./dir/file', simplify('.///dir//file')) |
| 487 | call assert_equal('./dir/file', simplify('./dir/./file')) |
| 488 | call assert_equal('./file', simplify('./dir/../file')) |
| 489 | call assert_equal('../dir/file', simplify('dir/../../dir/file')) |
| 490 | call assert_equal('./file', simplify('dir/.././file')) |
Bram Moolenaar | bdd2c29 | 2020-06-22 21:34:30 +0200 | [diff] [blame] | 491 | call assert_equal('../dir', simplify('./../dir')) |
| 492 | call assert_equal('..', simplify('../testdir/..')) |
| 493 | call mkdir('Xdir') |
| 494 | call assert_equal('.', simplify('Xdir/../.')) |
| 495 | call delete('Xdir', 'd') |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 496 | |
| 497 | call assert_fails('call simplify({->0})', 'E729:') |
| 498 | call assert_fails('call simplify([])', 'E730:') |
| 499 | call assert_fails('call simplify({})', 'E731:') |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 500 | if has('float') |
Bram Moolenaar | 3cfa5b1 | 2021-06-06 14:14:39 +0200 | [diff] [blame] | 501 | call assert_equal('1.2', simplify(1.2)) |
| 502 | call CheckDefExecAndScriptFailure(['echo simplify(1.2)'], 'E806:') |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 503 | endif |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 504 | endfunc |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 505 | |
Bram Moolenaar | bfde0b4 | 2018-08-08 22:27:31 +0200 | [diff] [blame] | 506 | func Test_pathshorten() |
| 507 | call assert_equal('', pathshorten('')) |
| 508 | call assert_equal('foo', pathshorten('foo')) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 509 | call assert_equal('/foo', '/foo'->pathshorten()) |
Bram Moolenaar | bfde0b4 | 2018-08-08 22:27:31 +0200 | [diff] [blame] | 510 | call assert_equal('f/', pathshorten('foo/')) |
| 511 | call assert_equal('f/bar', pathshorten('foo/bar')) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 512 | call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten()) |
Bram Moolenaar | bfde0b4 | 2018-08-08 22:27:31 +0200 | [diff] [blame] | 513 | call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar')) |
| 514 | call assert_equal('.f/bar', pathshorten('.foo/bar')) |
| 515 | call assert_equal('~f/bar', pathshorten('~foo/bar')) |
| 516 | call assert_equal('~.f/bar', pathshorten('~.foo/bar')) |
| 517 | call assert_equal('.~f/bar', pathshorten('.~foo/bar')) |
| 518 | call assert_equal('~/f/bar', pathshorten('~/foo/bar')) |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 519 | call assert_fails('call pathshorten([])', 'E730:') |
Bram Moolenaar | 6a33ef0 | 2020-09-25 22:42:48 +0200 | [diff] [blame] | 520 | |
| 521 | " test pathshorten with optional variable to set preferred size of shortening |
| 522 | call assert_equal('', pathshorten('', 2)) |
| 523 | call assert_equal('foo', pathshorten('foo', 2)) |
| 524 | call assert_equal('/foo', pathshorten('/foo', 2)) |
| 525 | call assert_equal('fo/', pathshorten('foo/', 2)) |
| 526 | call assert_equal('fo/bar', pathshorten('foo/bar', 2)) |
| 527 | call assert_equal('fo/ba/foobar', pathshorten('foo/bar/foobar', 2)) |
| 528 | call assert_equal('/fo/ba/foobar', pathshorten('/foo/bar/foobar', 2)) |
| 529 | call assert_equal('.fo/bar', pathshorten('.foo/bar', 2)) |
| 530 | call assert_equal('~fo/bar', pathshorten('~foo/bar', 2)) |
| 531 | call assert_equal('~.fo/bar', pathshorten('~.foo/bar', 2)) |
| 532 | call assert_equal('.~fo/bar', pathshorten('.~foo/bar', 2)) |
| 533 | call assert_equal('~/fo/bar', pathshorten('~/foo/bar', 2)) |
| 534 | call assert_fails('call pathshorten([],2)', 'E730:') |
| 535 | call assert_notequal('~/fo/bar', pathshorten('~/foo/bar', 3)) |
| 536 | call assert_equal('~/foo/bar', pathshorten('~/foo/bar', 3)) |
| 537 | call assert_equal('~/f/bar', pathshorten('~/foo/bar', 0)) |
Bram Moolenaar | bfde0b4 | 2018-08-08 22:27:31 +0200 | [diff] [blame] | 538 | endfunc |
| 539 | |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 540 | func Test_strpart() |
| 541 | call assert_equal('de', strpart('abcdefg', 3, 2)) |
| 542 | call assert_equal('ab', strpart('abcdefg', -2, 4)) |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 543 | call assert_equal('abcdefg', 'abcdefg'->strpart(-2)) |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 544 | call assert_equal('fg', strpart('abcdefg', 5, 4)) |
| 545 | call assert_equal('defg', strpart('abcdefg', 3)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 546 | call assert_equal('', strpart('abcdefg', 10)) |
| 547 | call assert_fails("let s=strpart('abcdef', [])", 'E745:') |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 548 | |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 549 | call assert_equal('lép', strpart('éléphant', 2, 4)) |
| 550 | call assert_equal('léphant', strpart('éléphant', 2)) |
Bram Moolenaar | 6c53fca | 2020-08-23 17:34:46 +0200 | [diff] [blame] | 551 | |
| 552 | call assert_equal('é', strpart('éléphant', 0, 1, 1)) |
| 553 | call assert_equal('ép', strpart('éléphant', 3, 2, v:true)) |
| 554 | call assert_equal('ó', strpart('cómposed', 1, 1, 1)) |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 555 | endfunc |
| 556 | |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 557 | func Test_tolower() |
| 558 | call assert_equal("", tolower("")) |
| 559 | |
| 560 | " Test with all printable ASCII characters. |
| 561 | call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~', |
| 562 | \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')) |
| 563 | |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 564 | " Test with a few uppercase diacritics. |
| 565 | call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ")) |
| 566 | call assert_equal("bḃḇ", tolower("BḂḆ")) |
| 567 | call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ")) |
| 568 | call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ")) |
| 569 | call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ")) |
| 570 | call assert_equal("fḟ ", tolower("FḞ ")) |
| 571 | call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ")) |
| 572 | call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ")) |
| 573 | call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ")) |
| 574 | call assert_equal("jĵ", tolower("JĴ")) |
| 575 | call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ")) |
| 576 | call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ")) |
| 577 | call assert_equal("mḿṁ", tolower("MḾṀ")) |
| 578 | call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ")) |
| 579 | call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ")) |
| 580 | call assert_equal("pṕṗ", tolower("PṔṖ")) |
| 581 | call assert_equal("q", tolower("Q")) |
| 582 | call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ")) |
| 583 | call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ")) |
| 584 | call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ")) |
| 585 | call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ")) |
| 586 | call assert_equal("vṽ", tolower("VṼ")) |
| 587 | call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ")) |
| 588 | call assert_equal("xẋẍ", tolower("XẊẌ")) |
| 589 | call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ")) |
| 590 | call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ")) |
| 591 | |
| 592 | " Test with a few lowercase diacritics, which should remain unchanged. |
| 593 | call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả")) |
| 594 | call assert_equal("bḃḇ", tolower("bḃḇ")) |
| 595 | call assert_equal("cçćĉċč", tolower("cçćĉċč")) |
| 596 | call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ")) |
| 597 | call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ")) |
| 598 | call assert_equal("fḟ", tolower("fḟ")) |
| 599 | call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ")) |
| 600 | call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ")) |
| 601 | call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ")) |
| 602 | call assert_equal("jĵǰ", tolower("jĵǰ")) |
| 603 | call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ")) |
| 604 | call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ")) |
| 605 | call assert_equal("mḿṁ ", tolower("mḿṁ ")) |
| 606 | call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ")) |
| 607 | call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ")) |
| 608 | call assert_equal("pṕṗ", tolower("pṕṗ")) |
| 609 | call assert_equal("q", tolower("q")) |
| 610 | call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ")) |
| 611 | call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ")) |
| 612 | call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ")) |
| 613 | call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ")) |
| 614 | call assert_equal("vṽ", tolower("vṽ")) |
| 615 | call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ")) |
| 616 | call assert_equal("ẋẍ", tolower("ẋẍ")) |
| 617 | call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ")) |
| 618 | call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ")) |
| 619 | |
| 620 | " According to https://twitter.com/jifa/status/625776454479970304 |
| 621 | " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase |
| 622 | " in length (2 to 3 bytes) when lowercased. So let's test them. |
| 623 | call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ")) |
Bram Moolenaar | e6640ad | 2017-12-22 21:06:56 +0100 | [diff] [blame] | 624 | |
| 625 | " This call to tolower with invalid utf8 sequence used to cause access to |
| 626 | " invalid memory. |
| 627 | call tolower("\xC0\x80\xC0") |
| 628 | call tolower("123\xC0\x80\xC0") |
Bram Moolenaar | 0ff5ded | 2020-05-07 18:43:44 +0200 | [diff] [blame] | 629 | |
| 630 | " Test in latin1 encoding |
| 631 | let save_enc = &encoding |
| 632 | set encoding=latin1 |
| 633 | call assert_equal("abc", tolower("ABC")) |
| 634 | let &encoding = save_enc |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 635 | endfunc |
| 636 | |
| 637 | func Test_toupper() |
| 638 | call assert_equal("", toupper("")) |
| 639 | |
| 640 | " Test with all printable ASCII characters. |
| 641 | call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~', |
| 642 | \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')) |
| 643 | |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 644 | " Test with a few lowercase diacritics. |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 645 | call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper()) |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 646 | call assert_equal("BḂḆ", toupper("bḃḇ")) |
| 647 | call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč")) |
| 648 | call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ")) |
| 649 | call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ")) |
| 650 | call assert_equal("FḞ", toupper("fḟ")) |
| 651 | call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ")) |
| 652 | call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ")) |
| 653 | call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ")) |
| 654 | call assert_equal("JĴǰ", toupper("jĵǰ")) |
| 655 | call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ")) |
| 656 | call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ")) |
| 657 | call assert_equal("MḾṀ ", toupper("mḿṁ ")) |
| 658 | call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ")) |
| 659 | call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ")) |
| 660 | call assert_equal("PṔṖ", toupper("pṕṗ")) |
| 661 | call assert_equal("Q", toupper("q")) |
| 662 | call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ")) |
| 663 | call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ")) |
| 664 | call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ")) |
| 665 | call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ")) |
| 666 | call assert_equal("VṼ", toupper("vṽ")) |
| 667 | call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ")) |
| 668 | call assert_equal("ẊẌ", toupper("ẋẍ")) |
| 669 | call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ")) |
| 670 | call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ")) |
| 671 | |
| 672 | " Test that uppercase diacritics, which should remain unchanged. |
| 673 | call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ")) |
| 674 | call assert_equal("BḂḆ", toupper("BḂḆ")) |
| 675 | call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ")) |
| 676 | call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ")) |
| 677 | call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ")) |
| 678 | call assert_equal("FḞ ", toupper("FḞ ")) |
| 679 | call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ")) |
| 680 | call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ")) |
| 681 | call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ")) |
| 682 | call assert_equal("JĴ", toupper("JĴ")) |
| 683 | call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ")) |
| 684 | call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ")) |
| 685 | call assert_equal("MḾṀ", toupper("MḾṀ")) |
| 686 | call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ")) |
| 687 | call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ")) |
| 688 | call assert_equal("PṔṖ", toupper("PṔṖ")) |
| 689 | call assert_equal("Q", toupper("Q")) |
| 690 | call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ")) |
| 691 | call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ")) |
| 692 | call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ")) |
| 693 | call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ")) |
| 694 | call assert_equal("VṼ", toupper("VṼ")) |
| 695 | call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ")) |
| 696 | call assert_equal("XẊẌ", toupper("XẊẌ")) |
| 697 | call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ")) |
| 698 | call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ")) |
| 699 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 700 | call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ")) |
Bram Moolenaar | e6640ad | 2017-12-22 21:06:56 +0100 | [diff] [blame] | 701 | |
| 702 | " This call to toupper with invalid utf8 sequence used to cause access to |
| 703 | " invalid memory. |
| 704 | call toupper("\xC0\x80\xC0") |
| 705 | call toupper("123\xC0\x80\xC0") |
Bram Moolenaar | 0ff5ded | 2020-05-07 18:43:44 +0200 | [diff] [blame] | 706 | |
| 707 | " Test in latin1 encoding |
| 708 | let save_enc = &encoding |
| 709 | set encoding=latin1 |
| 710 | call assert_equal("ABC", toupper("abc")) |
| 711 | let &encoding = save_enc |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 712 | endfunc |
| 713 | |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 714 | func Test_tr() |
| 715 | call assert_equal('foo', tr('bar', 'bar', 'foo')) |
| 716 | call assert_equal('zxy', 'cab'->tr('abc', 'xyz')) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 717 | call assert_fails("let s=tr([], 'abc', 'def')", 'E730:') |
| 718 | call assert_fails("let s=tr('abc', [], 'def')", 'E730:') |
| 719 | call assert_fails("let s=tr('abc', 'abc', [])", 'E730:') |
| 720 | call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:') |
| 721 | set encoding=latin1 |
| 722 | call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:') |
| 723 | call assert_equal('hEllO', tr('hello', 'eo', 'EO')) |
| 724 | call assert_equal('hello', tr('hello', 'xy', 'ab')) |
Yegappan Lakshmanan | 34fcb69 | 2021-05-25 20:14:00 +0200 | [diff] [blame] | 725 | call assert_fails('call tr("abc", "123", "₁₂")', 'E475:') |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 726 | set encoding=utf8 |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 727 | endfunc |
| 728 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 729 | " Tests for the mode() function |
| 730 | let current_modes = '' |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 731 | func Save_mode() |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 732 | let g:current_modes = mode(0) . '-' . mode(1) |
| 733 | return '' |
| 734 | endfunc |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 735 | |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 736 | " Test for the mode() function |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 737 | func Test_mode() |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 738 | new |
| 739 | call append(0, ["Blue Ball Black", "Brown Band Bowl", ""]) |
| 740 | |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 741 | " Only complete from the current buffer. |
| 742 | set complete=. |
| 743 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 744 | inoremap <F2> <C-R>=Save_mode()<CR> |
| 745 | |
| 746 | normal! 3G |
| 747 | exe "normal i\<F2>\<Esc>" |
| 748 | call assert_equal('i-i', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 749 | " i_CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 750 | exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u" |
| 751 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 752 | " i_CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 753 | exe "normal iBro\<C-P>\<F2>\<Esc>u" |
| 754 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 755 | " i_CTRL-X |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 756 | exe "normal iBa\<C-X>\<F2>\<Esc>u" |
| 757 | call assert_equal('i-ix', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 758 | " i_CTRL-X CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 759 | exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u" |
| 760 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 761 | " i_CTRL-X CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 762 | exe "normal iBro\<C-X>\<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-X CTRL-P + CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 765 | exe "normal iBro\<C-X>\<C-P>\<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 CTRL-L: Multiple matches |
| 768 | exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u" |
| 769 | call assert_equal('i-ic', g:current_modes) |
| 770 | " i_CTRL-X CTRL-L: Single match |
| 771 | exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u" |
| 772 | call assert_equal('i-ic', g:current_modes) |
| 773 | " i_CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 774 | exe "normal iCom\<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: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 777 | exe "normal iCom\<C-X>\<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: No match |
| 780 | exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u" |
| 781 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 782 | |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 783 | " R_CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 784 | exe "normal RBa\<C-P>\<F2>\<Esc>u" |
| 785 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 786 | " R_CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 787 | exe "normal RBro\<C-P>\<F2>\<Esc>u" |
| 788 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 789 | " R_CTRL-X |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 790 | exe "normal RBa\<C-X>\<F2>\<Esc>u" |
| 791 | call assert_equal('R-Rx', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 792 | " R_CTRL-X CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 793 | exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u" |
| 794 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 795 | " R_CTRL-X CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 796 | exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u" |
| 797 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 798 | " R_CTRL-X CTRL-P + CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 799 | exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" |
| 800 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 801 | " R_CTRL-X CTRL-L: Multiple matches |
| 802 | exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u" |
| 803 | call assert_equal('R-Rc', g:current_modes) |
| 804 | " R_CTRL-X CTRL-L: Single match |
| 805 | exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u" |
| 806 | call assert_equal('R-Rc', g:current_modes) |
| 807 | " R_CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 808 | exe "normal RCom\<C-P>\<F2>\<Esc>u" |
| 809 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 810 | " R_CTRL-X CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 811 | exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u" |
| 812 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 813 | " R_CTRL-X CTRL-L: No match |
| 814 | exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u" |
| 815 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 816 | |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 817 | call assert_equal('n', 0->mode()) |
| 818 | call assert_equal('n', 1->mode()) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 819 | |
Bram Moolenaar | 612cc38 | 2018-07-29 15:34:26 +0200 | [diff] [blame] | 820 | " i_CTRL-O |
| 821 | exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>" |
| 822 | call assert_equal("n-niI", g:current_modes) |
| 823 | |
| 824 | " R_CTRL-O |
| 825 | exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>" |
| 826 | call assert_equal("n-niR", g:current_modes) |
| 827 | |
| 828 | " gR_CTRL-O |
| 829 | exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>" |
| 830 | call assert_equal("n-niV", g:current_modes) |
| 831 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 832 | " How to test operator-pending mode? |
| 833 | |
| 834 | call feedkeys("v", 'xt') |
| 835 | call assert_equal('v', mode()) |
| 836 | call assert_equal('v', mode(1)) |
| 837 | call feedkeys("\<Esc>V", 'xt') |
| 838 | call assert_equal('V', mode()) |
| 839 | call assert_equal('V', mode(1)) |
| 840 | call feedkeys("\<Esc>\<C-V>", 'xt') |
| 841 | call assert_equal("\<C-V>", mode()) |
| 842 | call assert_equal("\<C-V>", mode(1)) |
| 843 | call feedkeys("\<Esc>", 'xt') |
| 844 | |
| 845 | call feedkeys("gh", 'xt') |
| 846 | call assert_equal('s', mode()) |
| 847 | call assert_equal('s', mode(1)) |
| 848 | call feedkeys("\<Esc>gH", 'xt') |
| 849 | call assert_equal('S', mode()) |
| 850 | call assert_equal('S', mode(1)) |
| 851 | call feedkeys("\<Esc>g\<C-H>", 'xt') |
| 852 | call assert_equal("\<C-S>", mode()) |
| 853 | call assert_equal("\<C-S>", mode(1)) |
| 854 | call feedkeys("\<Esc>", 'xt') |
| 855 | |
| 856 | call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt') |
| 857 | call assert_equal('c-c', g:current_modes) |
| 858 | call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt') |
| 859 | call assert_equal('c-cv', g:current_modes) |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 860 | call feedkeys("Qcall Save_mode()\<CR>vi\<CR>", 'xt') |
| 861 | call assert_equal('c-ce', g:current_modes) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 862 | " How to test Ex mode? |
| 863 | |
| 864 | bwipe! |
| 865 | iunmap <F2> |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 866 | set complete& |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 867 | endfunc |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 868 | |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 869 | " Test for append() |
Bram Moolenaar | d200702 | 2019-08-27 21:56:06 +0200 | [diff] [blame] | 870 | func Test_append() |
| 871 | enew! |
| 872 | split |
| 873 | call append(0, ["foo"]) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 874 | call append(1, []) |
| 875 | call append(1, test_null_list()) |
| 876 | call assert_equal(['foo', ''], getline(1, '$')) |
Bram Moolenaar | d200702 | 2019-08-27 21:56:06 +0200 | [diff] [blame] | 877 | split |
| 878 | only |
| 879 | undo |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 880 | undo |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 881 | |
| 882 | " Using $ instead of '$' must give an error |
| 883 | call assert_fails("call append($, 'foobar')", 'E116:') |
Bram Moolenaar | d200702 | 2019-08-27 21:56:06 +0200 | [diff] [blame] | 884 | endfunc |
| 885 | |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 886 | " Test for setline() |
| 887 | func Test_setline() |
| 888 | new |
| 889 | call setline(0, ["foo"]) |
| 890 | call setline(0, []) |
| 891 | call setline(0, test_null_list()) |
| 892 | call setline(1, ["bar"]) |
| 893 | call setline(1, []) |
| 894 | call setline(1, test_null_list()) |
| 895 | call setline(2, []) |
| 896 | call setline(2, test_null_list()) |
| 897 | call setline(3, []) |
| 898 | call setline(3, test_null_list()) |
| 899 | call setline(2, ["baz"]) |
| 900 | call assert_equal(['bar', 'baz'], getline(1, '$')) |
| 901 | close! |
| 902 | endfunc |
| 903 | |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 904 | func Test_getbufvar() |
| 905 | let bnr = bufnr('%') |
| 906 | let b:var_num = '1234' |
| 907 | let def_num = '5678' |
| 908 | call assert_equal('1234', getbufvar(bnr, 'var_num')) |
| 909 | call assert_equal('1234', getbufvar(bnr, 'var_num', def_num)) |
| 910 | |
| 911 | let bd = getbufvar(bnr, '') |
| 912 | call assert_equal('1234', bd['var_num']) |
| 913 | call assert_true(exists("bd['changedtick']")) |
| 914 | call assert_equal(2, len(bd)) |
| 915 | |
| 916 | let bd2 = getbufvar(bnr, '', def_num) |
| 917 | call assert_equal(bd, bd2) |
| 918 | |
| 919 | unlet b:var_num |
| 920 | call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num)) |
| 921 | call assert_equal('', getbufvar(bnr, 'var_num')) |
| 922 | |
| 923 | let bd = getbufvar(bnr, '') |
| 924 | call assert_equal(1, len(bd)) |
| 925 | let bd = getbufvar(bnr, '',def_num) |
| 926 | call assert_equal(1, len(bd)) |
| 927 | |
Bram Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 928 | call assert_equal('', getbufvar(9999, '')) |
| 929 | call assert_equal(def_num, getbufvar(9999, '', def_num)) |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 930 | unlet def_num |
| 931 | |
Bram Moolenaar | 507647d | 2017-02-17 16:43:49 +0100 | [diff] [blame] | 932 | call assert_equal(0, getbufvar(bnr, '&autoindent')) |
| 933 | call assert_equal(0, getbufvar(bnr, '&autoindent', 1)) |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 934 | |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 935 | " Set and get a buffer-local variable |
| 936 | call setbufvar(bnr, 'bufvar_test', ['one', 'two']) |
| 937 | call assert_equal(['one', 'two'], getbufvar(bnr, 'bufvar_test')) |
| 938 | |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 939 | " Open new window with forced option values |
| 940 | set fileformats=unix,dos |
| 941 | new ++ff=dos ++bin ++enc=iso-8859-2 |
| 942 | call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat')) |
| 943 | call assert_equal(1, getbufvar(bufnr('%'), '&bin')) |
| 944 | call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc')) |
| 945 | close |
| 946 | |
Bram Moolenaar | 5259275 | 2020-04-03 18:43:35 +0200 | [diff] [blame] | 947 | " Get the b: dict. |
| 948 | let b:testvar = 'one' |
| 949 | new |
| 950 | let b:testvar = 'two' |
| 951 | let thebuf = bufnr() |
| 952 | wincmd w |
| 953 | call assert_equal('two', getbufvar(thebuf, 'testvar')) |
| 954 | call assert_equal('two', getbufvar(thebuf, '').testvar) |
| 955 | bwipe! |
| 956 | |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 957 | set fileformats& |
| 958 | endfunc |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 959 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 960 | func Test_last_buffer_nr() |
| 961 | call assert_equal(bufnr('$'), last_buffer_nr()) |
| 962 | endfunc |
| 963 | |
| 964 | func Test_stridx() |
| 965 | call assert_equal(-1, stridx('', 'l')) |
| 966 | call assert_equal(0, stridx('', '')) |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 967 | call assert_equal(0, 'hello'->stridx('')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 968 | call assert_equal(-1, stridx('hello', 'L')) |
| 969 | call assert_equal(2, stridx('hello', 'l', -1)) |
| 970 | call assert_equal(2, stridx('hello', 'l', 0)) |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 971 | call assert_equal(2, 'hello'->stridx('l', 1)) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 972 | call assert_equal(3, stridx('hello', 'l', 3)) |
| 973 | call assert_equal(-1, stridx('hello', 'l', 4)) |
| 974 | call assert_equal(-1, stridx('hello', 'l', 10)) |
| 975 | call assert_equal(2, stridx('hello', 'll')) |
| 976 | call assert_equal(-1, stridx('hello', 'hello world')) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 977 | call assert_fails("let n=stridx('hello', [])", 'E730:') |
| 978 | call assert_fails("let n=stridx([], 'l')", 'E730:') |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 979 | endfunc |
| 980 | |
| 981 | func Test_strridx() |
| 982 | call assert_equal(-1, strridx('', 'l')) |
| 983 | call assert_equal(0, strridx('', '')) |
| 984 | call assert_equal(5, strridx('hello', '')) |
| 985 | call assert_equal(-1, strridx('hello', 'L')) |
Bram Moolenaar | f6ed61e | 2019-09-07 19:05:09 +0200 | [diff] [blame] | 986 | call assert_equal(3, 'hello'->strridx('l')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 987 | call assert_equal(3, strridx('hello', 'l', 10)) |
| 988 | call assert_equal(3, strridx('hello', 'l', 3)) |
| 989 | call assert_equal(2, strridx('hello', 'l', 2)) |
| 990 | call assert_equal(-1, strridx('hello', 'l', 1)) |
| 991 | call assert_equal(-1, strridx('hello', 'l', 0)) |
| 992 | call assert_equal(-1, strridx('hello', 'l', -1)) |
| 993 | call assert_equal(2, strridx('hello', 'll')) |
| 994 | call assert_equal(-1, strridx('hello', 'hello world')) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 995 | call assert_fails("let n=strridx('hello', [])", 'E730:') |
| 996 | call assert_fails("let n=strridx([], 'l')", 'E730:') |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 997 | endfunc |
| 998 | |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 999 | func Test_match_func() |
| 1000 | call assert_equal(4, match('testing', 'ing')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 1001 | call assert_equal(4, 'testing'->match('ing', 2)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1002 | call assert_equal(-1, match('testing', 'ing', 5)) |
| 1003 | call assert_equal(-1, match('testing', 'ing', 8)) |
| 1004 | call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing')) |
| 1005 | call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img')) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 1006 | call assert_fails("let x=match('vim', [])", 'E730:') |
| 1007 | call assert_equal(3, match(['a', 'b', 'c', 'a'], 'a', 1)) |
| 1008 | call assert_equal(-1, match(['a', 'b', 'c', 'a'], 'a', 5)) |
| 1009 | call assert_equal(4, match('testing', 'ing', -1)) |
| 1010 | call assert_fails("let x=match('testing', 'ing', 0, [])", 'E745:') |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 1011 | call assert_equal(-1, match(test_null_list(), 2)) |
Bram Moolenaar | 531be47 | 2020-09-23 22:38:05 +0200 | [diff] [blame] | 1012 | call assert_equal(-1, match('abc', '\\%(')) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1013 | endfunc |
| 1014 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1015 | func Test_matchend() |
| 1016 | call assert_equal(7, matchend('testing', 'ing')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 1017 | call assert_equal(7, 'testing'->matchend('ing', 2)) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1018 | call assert_equal(-1, matchend('testing', 'ing', 5)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1019 | call assert_equal(-1, matchend('testing', 'ing', 8)) |
| 1020 | call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing')) |
| 1021 | call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img')) |
| 1022 | endfunc |
| 1023 | |
| 1024 | func Test_matchlist() |
| 1025 | call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 1026 | call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1027 | call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4)) |
| 1028 | endfunc |
| 1029 | |
| 1030 | func Test_matchstr() |
| 1031 | call assert_equal('ing', matchstr('testing', 'ing')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 1032 | call assert_equal('ing', 'testing'->matchstr('ing', 2)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1033 | call assert_equal('', matchstr('testing', 'ing', 5)) |
| 1034 | call assert_equal('', matchstr('testing', 'ing', 8)) |
| 1035 | call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing')) |
| 1036 | call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img')) |
| 1037 | endfunc |
| 1038 | |
| 1039 | func Test_matchstrpos() |
| 1040 | call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing')) |
Bram Moolenaar | a144983 | 2019-09-01 20:16:52 +0200 | [diff] [blame] | 1041 | call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 1042 | call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5)) |
| 1043 | call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8)) |
| 1044 | call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing')) |
| 1045 | call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img')) |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 1046 | call assert_equal(['', -1, -1], matchstrpos(test_null_list(), '\a')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1047 | endfunc |
| 1048 | |
| 1049 | func Test_nextnonblank_prevnonblank() |
| 1050 | new |
| 1051 | insert |
| 1052 | This |
| 1053 | |
| 1054 | |
| 1055 | is |
| 1056 | |
| 1057 | a |
| 1058 | Test |
| 1059 | . |
| 1060 | call assert_equal(0, nextnonblank(-1)) |
| 1061 | call assert_equal(0, nextnonblank(0)) |
| 1062 | call assert_equal(1, nextnonblank(1)) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 1063 | call assert_equal(4, 2->nextnonblank()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1064 | call assert_equal(4, nextnonblank(3)) |
| 1065 | call assert_equal(4, nextnonblank(4)) |
| 1066 | call assert_equal(6, nextnonblank(5)) |
| 1067 | call assert_equal(6, nextnonblank(6)) |
| 1068 | call assert_equal(7, nextnonblank(7)) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 1069 | call assert_equal(0, 8->nextnonblank()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1070 | |
| 1071 | call assert_equal(0, prevnonblank(-1)) |
| 1072 | call assert_equal(0, prevnonblank(0)) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 1073 | call assert_equal(1, 1->prevnonblank()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1074 | call assert_equal(1, prevnonblank(2)) |
| 1075 | call assert_equal(1, prevnonblank(3)) |
| 1076 | call assert_equal(4, prevnonblank(4)) |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 1077 | call assert_equal(4, 5->prevnonblank()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1078 | call assert_equal(6, prevnonblank(6)) |
| 1079 | call assert_equal(7, prevnonblank(7)) |
| 1080 | call assert_equal(0, prevnonblank(8)) |
| 1081 | bw! |
| 1082 | endfunc |
| 1083 | |
| 1084 | func Test_byte2line_line2byte() |
| 1085 | new |
Bram Moolenaar | c26f7c6 | 2018-08-20 22:53:04 +0200 | [diff] [blame] | 1086 | set endofline |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1087 | call setline(1, ['a', 'bc', 'd']) |
| 1088 | |
| 1089 | set fileformat=unix |
| 1090 | call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1], |
| 1091 | \ map(range(-1, 8), 'byte2line(v:val)')) |
| 1092 | call assert_equal([-1, -1, 1, 3, 6, 8, -1], |
| 1093 | \ map(range(-1, 5), 'line2byte(v:val)')) |
| 1094 | |
| 1095 | set fileformat=mac |
| 1096 | 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] | 1097 | \ map(range(-1, 8), 'v:val->byte2line()')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1098 | call assert_equal([-1, -1, 1, 3, 6, 8, -1], |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 1099 | \ map(range(-1, 5), 'v:val->line2byte()')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1100 | |
| 1101 | set fileformat=dos |
| 1102 | call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1], |
| 1103 | \ map(range(-1, 11), 'byte2line(v:val)')) |
| 1104 | call assert_equal([-1, -1, 1, 4, 8, 11, -1], |
| 1105 | \ map(range(-1, 5), 'line2byte(v:val)')) |
| 1106 | |
Bram Moolenaar | c26f7c6 | 2018-08-20 22:53:04 +0200 | [diff] [blame] | 1107 | bw! |
| 1108 | set noendofline nofixendofline |
| 1109 | normal a- |
| 1110 | for ff in ["unix", "mac", "dos"] |
| 1111 | let &fileformat = ff |
| 1112 | call assert_equal(1, line2byte(1)) |
| 1113 | call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte). |
| 1114 | endfor |
| 1115 | |
| 1116 | set endofline& fixendofline& fileformat& |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1117 | bw! |
| 1118 | endfunc |
| 1119 | |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 1120 | " Test for byteidx() and byteidxcomp() functions |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1121 | func Test_byteidx() |
| 1122 | let a = '.é.' " one char of two bytes |
| 1123 | call assert_equal(0, byteidx(a, 0)) |
| 1124 | call assert_equal(0, byteidxcomp(a, 0)) |
| 1125 | call assert_equal(1, byteidx(a, 1)) |
| 1126 | call assert_equal(1, byteidxcomp(a, 1)) |
| 1127 | call assert_equal(3, byteidx(a, 2)) |
| 1128 | call assert_equal(3, byteidxcomp(a, 2)) |
| 1129 | call assert_equal(4, byteidx(a, 3)) |
| 1130 | call assert_equal(4, byteidxcomp(a, 3)) |
| 1131 | call assert_equal(-1, byteidx(a, 4)) |
| 1132 | call assert_equal(-1, byteidxcomp(a, 4)) |
| 1133 | |
| 1134 | let b = '.é.' " normal e with composing char |
| 1135 | call assert_equal(0, b->byteidx(0)) |
| 1136 | call assert_equal(1, b->byteidx(1)) |
| 1137 | call assert_equal(4, b->byteidx(2)) |
| 1138 | call assert_equal(5, b->byteidx(3)) |
| 1139 | call assert_equal(-1, b->byteidx(4)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 1140 | call assert_fails("call byteidx([], 0)", 'E730:') |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1141 | |
| 1142 | call assert_equal(0, b->byteidxcomp(0)) |
| 1143 | call assert_equal(1, b->byteidxcomp(1)) |
| 1144 | call assert_equal(2, b->byteidxcomp(2)) |
| 1145 | call assert_equal(4, b->byteidxcomp(3)) |
| 1146 | call assert_equal(5, b->byteidxcomp(4)) |
| 1147 | call assert_equal(-1, b->byteidxcomp(5)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 1148 | call assert_fails("call byteidxcomp([], 0)", 'E730:') |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 1149 | endfunc |
| 1150 | |
Bram Moolenaar | 17793ef | 2020-12-28 12:56:58 +0100 | [diff] [blame] | 1151 | " Test for charidx() |
| 1152 | func Test_charidx() |
| 1153 | let a = 'xáb́y' |
| 1154 | call assert_equal(0, charidx(a, 0)) |
| 1155 | call assert_equal(1, charidx(a, 3)) |
| 1156 | call assert_equal(2, charidx(a, 4)) |
| 1157 | call assert_equal(3, charidx(a, 7)) |
| 1158 | call assert_equal(-1, charidx(a, 8)) |
Dominique Pelle | 6d37e8e | 2021-05-06 17:36:55 +0200 | [diff] [blame] | 1159 | call assert_equal(-1, charidx(a, -1)) |
Bram Moolenaar | 17793ef | 2020-12-28 12:56:58 +0100 | [diff] [blame] | 1160 | call assert_equal(-1, charidx('', 0)) |
Dominique Pelle | 6d37e8e | 2021-05-06 17:36:55 +0200 | [diff] [blame] | 1161 | call assert_equal(-1, charidx(test_null_string(), 0)) |
Bram Moolenaar | 17793ef | 2020-12-28 12:56:58 +0100 | [diff] [blame] | 1162 | |
| 1163 | " count composing characters |
| 1164 | call assert_equal(0, charidx(a, 0, 1)) |
| 1165 | call assert_equal(2, charidx(a, 2, 1)) |
| 1166 | call assert_equal(3, charidx(a, 4, 1)) |
| 1167 | call assert_equal(5, charidx(a, 7, 1)) |
| 1168 | call assert_equal(-1, charidx(a, 8, 1)) |
| 1169 | call assert_equal(-1, charidx('', 0, 1)) |
| 1170 | |
| 1171 | call assert_fails('let x = charidx([], 1)', 'E474:') |
| 1172 | call assert_fails('let x = charidx("abc", [])', 'E474:') |
| 1173 | call assert_fails('let x = charidx("abc", 1, [])', 'E474:') |
| 1174 | call assert_fails('let x = charidx("abc", 1, -1)', 'E1023:') |
| 1175 | call assert_fails('let x = charidx("abc", 1, 2)', 'E1023:') |
| 1176 | endfunc |
| 1177 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1178 | func Test_count() |
| 1179 | let l = ['a', 'a', 'A', 'b'] |
| 1180 | call assert_equal(2, count(l, 'a')) |
| 1181 | call assert_equal(1, count(l, 'A')) |
| 1182 | call assert_equal(1, count(l, 'b')) |
| 1183 | call assert_equal(0, count(l, 'B')) |
| 1184 | |
| 1185 | call assert_equal(2, count(l, 'a', 0)) |
| 1186 | call assert_equal(1, count(l, 'A', 0)) |
| 1187 | call assert_equal(1, count(l, 'b', 0)) |
| 1188 | call assert_equal(0, count(l, 'B', 0)) |
| 1189 | |
| 1190 | call assert_equal(3, count(l, 'a', 1)) |
| 1191 | call assert_equal(3, count(l, 'A', 1)) |
| 1192 | call assert_equal(1, count(l, 'b', 1)) |
| 1193 | call assert_equal(1, count(l, 'B', 1)) |
| 1194 | call assert_equal(0, count(l, 'c', 1)) |
| 1195 | |
| 1196 | call assert_equal(1, count(l, 'a', 0, 1)) |
| 1197 | call assert_equal(2, count(l, 'a', 1, 1)) |
| 1198 | call assert_fails('call count(l, "a", 0, 10)', 'E684:') |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 1199 | call assert_fails('call count(l, "a", [])', 'E745:') |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1200 | |
| 1201 | let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'} |
| 1202 | call assert_equal(2, count(d, 'a')) |
| 1203 | call assert_equal(1, count(d, 'A')) |
| 1204 | call assert_equal(1, count(d, 'b')) |
| 1205 | call assert_equal(0, count(d, 'B')) |
| 1206 | |
| 1207 | call assert_equal(2, count(d, 'a', 0)) |
| 1208 | call assert_equal(1, count(d, 'A', 0)) |
| 1209 | call assert_equal(1, count(d, 'b', 0)) |
| 1210 | call assert_equal(0, count(d, 'B', 0)) |
| 1211 | |
| 1212 | call assert_equal(3, count(d, 'a', 1)) |
| 1213 | call assert_equal(3, count(d, 'A', 1)) |
| 1214 | call assert_equal(1, count(d, 'b', 1)) |
| 1215 | call assert_equal(1, count(d, 'B', 1)) |
| 1216 | call assert_equal(0, count(d, 'c', 1)) |
| 1217 | |
| 1218 | call assert_fails('call count(d, "a", 0, 1)', 'E474:') |
Bram Moolenaar | 9966b21 | 2017-07-28 16:46:57 +0200 | [diff] [blame] | 1219 | |
| 1220 | call assert_equal(0, count("foo", "bar")) |
| 1221 | call assert_equal(1, count("foo", "oo")) |
| 1222 | call assert_equal(2, count("foo", "o")) |
| 1223 | call assert_equal(0, count("foo", "O")) |
| 1224 | call assert_equal(2, count("foo", "O", 1)) |
| 1225 | call assert_equal(2, count("fooooo", "oo")) |
Bram Moolenaar | 338e47f | 2017-12-19 11:55:26 +0100 | [diff] [blame] | 1226 | call assert_equal(0, count("foo", "")) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 1227 | |
| 1228 | call assert_fails('call count(0, 0)', 'E712:') |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1229 | endfunc |
| 1230 | |
| 1231 | func Test_changenr() |
| 1232 | new Xchangenr |
| 1233 | call assert_equal(0, changenr()) |
| 1234 | norm ifoo |
| 1235 | call assert_equal(1, changenr()) |
| 1236 | set undolevels=10 |
| 1237 | norm Sbar |
| 1238 | call assert_equal(2, changenr()) |
| 1239 | undo |
| 1240 | call assert_equal(1, changenr()) |
| 1241 | redo |
| 1242 | call assert_equal(2, changenr()) |
| 1243 | bw! |
| 1244 | set undolevels& |
| 1245 | endfunc |
| 1246 | |
| 1247 | func Test_filewritable() |
| 1248 | new Xfilewritable |
| 1249 | write! |
| 1250 | call assert_equal(1, filewritable('Xfilewritable')) |
| 1251 | |
| 1252 | call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----')) |
| 1253 | call assert_equal(0, filewritable('Xfilewritable')) |
| 1254 | |
| 1255 | call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----')) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1256 | call assert_equal(1, 'Xfilewritable'->filewritable()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1257 | |
| 1258 | call assert_equal(0, filewritable('doesnotexist')) |
| 1259 | |
Bram Moolenaar | 0ff5ded | 2020-05-07 18:43:44 +0200 | [diff] [blame] | 1260 | call mkdir('Xdir') |
| 1261 | call assert_equal(2, filewritable('Xdir')) |
| 1262 | call delete('Xdir', 'd') |
| 1263 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1264 | call delete('Xfilewritable') |
| 1265 | bw! |
| 1266 | endfunc |
| 1267 | |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 1268 | func Test_Executable() |
| 1269 | if has('win32') |
| 1270 | call assert_equal(1, executable('notepad')) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1271 | call assert_equal(1, 'notepad.exe'->executable()) |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 1272 | call assert_equal(0, executable('notepad.exe.exe')) |
| 1273 | call assert_equal(0, executable('shell32.dll')) |
| 1274 | call assert_equal(0, executable('win.ini')) |
Bram Moolenaar | 95da136 | 2020-05-30 18:37:55 +0200 | [diff] [blame] | 1275 | |
| 1276 | " get "notepad" path and remove the leading drive and sep. (ex. 'C:\') |
| 1277 | let notepadcmd = exepath('notepad.exe') |
| 1278 | let driveroot = notepadcmd[:2] |
| 1279 | let notepadcmd = notepadcmd[3:] |
| 1280 | new |
| 1281 | " check that the relative path works in / |
| 1282 | execute 'lcd' driveroot |
| 1283 | call assert_equal(1, executable(notepadcmd)) |
| 1284 | call assert_equal(driveroot .. notepadcmd, notepadcmd->exepath()) |
| 1285 | bwipe |
| 1286 | |
| 1287 | " create "notepad.bat" |
| 1288 | call mkdir('Xdir') |
| 1289 | let notepadbat = fnamemodify('Xdir/notepad.bat', ':p') |
| 1290 | call writefile([], notepadbat) |
| 1291 | new |
| 1292 | " check that the path and the pathext order is valid |
| 1293 | lcd Xdir |
| 1294 | let [pathext, $PATHEXT] = [$PATHEXT, '.com;.exe;.bat;.cmd'] |
| 1295 | call assert_equal(notepadbat, exepath('notepad')) |
| 1296 | let $PATHEXT = pathext |
| 1297 | bwipe |
| 1298 | eval 'Xdir'->delete('rf') |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 1299 | elseif has('unix') |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1300 | call assert_equal(1, 'cat'->executable()) |
Bram Moolenaar | a05a0d3 | 2018-10-07 18:43:05 +0200 | [diff] [blame] | 1301 | call assert_equal(0, executable('nodogshere')) |
Bram Moolenaar | d08b8c4 | 2019-07-24 14:59:45 +0200 | [diff] [blame] | 1302 | |
| 1303 | " get "cat" path and remove the leading / |
| 1304 | let catcmd = exepath('cat')[1:] |
| 1305 | new |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1306 | " check that the relative path works in / |
Bram Moolenaar | d08b8c4 | 2019-07-24 14:59:45 +0200 | [diff] [blame] | 1307 | lcd / |
| 1308 | call assert_equal(1, executable(catcmd)) |
Bram Moolenaar | a387083 | 2021-01-01 14:20:44 +0100 | [diff] [blame] | 1309 | let result = catcmd->exepath() |
| 1310 | " when using chroot looking for sbin/cat can return bin/cat, that is OK |
| 1311 | if catcmd =~ '\<sbin\>' && result =~ '\<bin\>' |
| 1312 | call assert_equal('/' .. substitute(catcmd, '\<sbin\>', 'bin', ''), result) |
| 1313 | else |
| 1314 | call assert_equal('/' .. catcmd, result) |
| 1315 | endif |
Bram Moolenaar | d08b8c4 | 2019-07-24 14:59:45 +0200 | [diff] [blame] | 1316 | bwipe |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1317 | else |
| 1318 | throw 'Skipped: does not work on this platform' |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 1319 | endif |
| 1320 | endfunc |
| 1321 | |
Bram Moolenaar | 8662189 | 2019-03-30 21:51:28 +0100 | [diff] [blame] | 1322 | func Test_executable_longname() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1323 | CheckMSWindows |
Bram Moolenaar | 8662189 | 2019-03-30 21:51:28 +0100 | [diff] [blame] | 1324 | |
Bram Moolenaar | f637bce | 2020-11-23 18:14:56 +0100 | [diff] [blame] | 1325 | " Create a temporary .bat file with 205 characters in the name. |
| 1326 | " Maximum length of a filename (including the path) on MS-Windows is 259 |
| 1327 | " characters. |
| 1328 | " See https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation |
| 1329 | let len = 259 - getcwd()->len() - 6 |
| 1330 | if len > 200 |
| 1331 | let len = 200 |
| 1332 | endif |
| 1333 | |
| 1334 | let fname = 'X' . repeat('あ', len) . '.bat' |
Bram Moolenaar | 8662189 | 2019-03-30 21:51:28 +0100 | [diff] [blame] | 1335 | call writefile([], fname) |
| 1336 | call assert_equal(1, executable(fname)) |
| 1337 | call delete(fname) |
| 1338 | endfunc |
| 1339 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1340 | func Test_hostname() |
| 1341 | let hostname_vim = hostname() |
| 1342 | if has('unix') |
| 1343 | let hostname_system = systemlist('uname -n')[0] |
| 1344 | call assert_equal(hostname_vim, hostname_system) |
| 1345 | endif |
| 1346 | endfunc |
| 1347 | |
| 1348 | func Test_getpid() |
| 1349 | " getpid() always returns the same value within a vim instance. |
| 1350 | call assert_equal(getpid(), getpid()) |
| 1351 | if has('unix') |
| 1352 | call assert_equal(systemlist('echo $PPID')[0], string(getpid())) |
| 1353 | endif |
| 1354 | endfunc |
| 1355 | |
| 1356 | func Test_hlexists() |
| 1357 | call assert_equal(0, hlexists('does_not_exist')) |
Bram Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 1358 | call assert_equal(0, 'Number'->hlexists()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1359 | call assert_equal(0, highlight_exists('does_not_exist')) |
| 1360 | call assert_equal(0, highlight_exists('Number')) |
| 1361 | syntax on |
| 1362 | call assert_equal(0, hlexists('does_not_exist')) |
| 1363 | call assert_equal(1, hlexists('Number')) |
| 1364 | call assert_equal(0, highlight_exists('does_not_exist')) |
| 1365 | call assert_equal(1, highlight_exists('Number')) |
| 1366 | syntax off |
| 1367 | endfunc |
| 1368 | |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 1369 | " Test for the col() function |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1370 | func Test_col() |
| 1371 | new |
| 1372 | call setline(1, 'abcdef') |
| 1373 | norm gg4|mx6|mY2| |
| 1374 | call assert_equal(2, col('.')) |
| 1375 | call assert_equal(7, col('$')) |
Bram Moolenaar | 8b63313 | 2020-03-20 18:20:51 +0100 | [diff] [blame] | 1376 | call assert_equal(2, col('v')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1377 | call assert_equal(4, col("'x")) |
| 1378 | call assert_equal(6, col("'Y")) |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 1379 | call assert_equal(2, [1, 2]->col()) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1380 | call assert_equal(7, col([1, '$'])) |
| 1381 | |
| 1382 | call assert_equal(0, col('')) |
| 1383 | call assert_equal(0, col('x')) |
| 1384 | call assert_equal(0, col([2, '$'])) |
| 1385 | call assert_equal(0, col([1, 100])) |
| 1386 | call assert_equal(0, col([1])) |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 1387 | call assert_equal(0, col(test_null_list())) |
| 1388 | call assert_fails('let c = col({})', 'E731:') |
Bram Moolenaar | 8b63313 | 2020-03-20 18:20:51 +0100 | [diff] [blame] | 1389 | |
| 1390 | " test for getting the visual start column |
| 1391 | func T() |
| 1392 | let g:Vcol = col('v') |
| 1393 | return '' |
| 1394 | endfunc |
| 1395 | let g:Vcol = 0 |
| 1396 | xmap <expr> <F2> T() |
| 1397 | exe "normal gg3|ve\<F2>" |
| 1398 | call assert_equal(3, g:Vcol) |
| 1399 | xunmap <F2> |
| 1400 | delfunc T |
| 1401 | |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 1402 | " Test for the visual line start and end marks '< and '> |
| 1403 | call setline(1, ['one', 'one two', 'one two three']) |
| 1404 | "normal! ggVG |
| 1405 | call feedkeys("ggVG\<Esc>", 'xt') |
| 1406 | call assert_equal(1, col("'<")) |
| 1407 | call assert_equal(14, col("'>")) |
| 1408 | " Delete the last line of the visually selected region |
| 1409 | $d |
| 1410 | call assert_notequal(14, col("'>")) |
| 1411 | |
| 1412 | " Test with 'virtualedit' |
| 1413 | set virtualedit=all |
| 1414 | call cursor(1, 10) |
| 1415 | call assert_equal(4, col('.')) |
| 1416 | set virtualedit& |
| 1417 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 1418 | bw! |
| 1419 | endfunc |
| 1420 | |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 1421 | " Test for input() |
| 1422 | func Test_input_func() |
| 1423 | " Test for prompt with multiple lines |
| 1424 | redir => v |
| 1425 | call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt') |
| 1426 | redir END |
| 1427 | call assert_equal("B", c) |
| 1428 | call assert_equal(['A', 'B', 'C'], split(v, "\n")) |
| 1429 | |
| 1430 | " Test for default value |
| 1431 | call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt') |
| 1432 | call assert_equal('red', c) |
| 1433 | |
| 1434 | " Test for completion at the input prompt |
| 1435 | func! Tcomplete(arglead, cmdline, pos) |
| 1436 | return "item1\nitem2\nitem3" |
| 1437 | endfunc |
Bram Moolenaar | 9d48956 | 2020-07-30 20:08:50 +0200 | [diff] [blame] | 1438 | call feedkeys(":let c = input('Q? ', '', 'custom,Tcomplete')\<CR>" |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 1439 | \ .. "\<C-A>\<CR>", 'xt') |
| 1440 | delfunc Tcomplete |
| 1441 | call assert_equal('item1 item2 item3', c) |
Bram Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 1442 | |
Bram Moolenaar | f4fcedc | 2021-03-15 18:36:20 +0100 | [diff] [blame] | 1443 | " Test for using special characters as default input |
Bram Moolenaar | 1f448d9 | 2021-03-22 19:37:06 +0100 | [diff] [blame] | 1444 | call feedkeys(":let c = input('name? ', \"x\\<BS>y\")\<CR>\<CR>", 'xt') |
Bram Moolenaar | f4fcedc | 2021-03-15 18:36:20 +0100 | [diff] [blame] | 1445 | call assert_equal('y', c) |
| 1446 | |
| 1447 | " Test for using <CR> as default input |
| 1448 | call feedkeys(":let c = input('name? ', \"\\<CR>\")\<CR>x\<CR>", 'xt') |
| 1449 | call assert_equal(' x', c) |
| 1450 | |
Bram Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 1451 | call assert_fails("call input('F:', '', 'invalid')", 'E180:') |
| 1452 | call assert_fails("call input('F:', '', [])", 'E730:') |
| 1453 | endfunc |
| 1454 | |
| 1455 | " Test for the inputdialog() function |
| 1456 | func Test_inputdialog() |
Bram Moolenaar | fdcbe3c | 2020-06-15 21:41:56 +0200 | [diff] [blame] | 1457 | set timeout timeoutlen=10 |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 1458 | if has('gui_running') |
| 1459 | call assert_fails('let v=inputdialog([], "xx")', 'E730:') |
| 1460 | call assert_fails('let v=inputdialog("Q", [])', 'E730:') |
| 1461 | else |
| 1462 | call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt') |
| 1463 | call assert_equal('xx', v) |
| 1464 | call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt') |
| 1465 | call assert_equal('yy', v) |
| 1466 | endif |
Bram Moolenaar | fdcbe3c | 2020-06-15 21:41:56 +0200 | [diff] [blame] | 1467 | set timeout& timeoutlen& |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 1468 | endfunc |
| 1469 | |
| 1470 | " Test for inputlist() |
Bram Moolenaar | 947b39e | 2018-07-22 19:36:37 +0200 | [diff] [blame] | 1471 | func Test_inputlist() |
| 1472 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx') |
| 1473 | call assert_equal(1, c) |
Bram Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 1474 | 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] | 1475 | call assert_equal(2, c) |
| 1476 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx') |
| 1477 | call assert_equal(3, c) |
| 1478 | |
Bram Moolenaar | eebd555 | 2020-06-10 15:45:57 +0200 | [diff] [blame] | 1479 | " CR to cancel |
| 1480 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<cr>", 'tx') |
| 1481 | call assert_equal(0, c) |
| 1482 | |
| 1483 | " Esc to cancel |
| 1484 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<Esc>", 'tx') |
| 1485 | call assert_equal(0, c) |
| 1486 | |
| 1487 | " q to cancel |
| 1488 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>q", 'tx') |
| 1489 | call assert_equal(0, c) |
| 1490 | |
=?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?= | 5cf9457 | 2021-05-20 21:14:20 +0200 | [diff] [blame] | 1491 | " Cancel after inputting a number |
| 1492 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>5q", 'tx') |
| 1493 | call assert_equal(0, c) |
| 1494 | |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 1495 | " Use backspace to delete characters in the prompt |
| 1496 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<BS>3\<BS>2\<cr>", 'tx') |
| 1497 | call assert_equal(2, c) |
| 1498 | |
| 1499 | " Use mouse to make a selection |
| 1500 | call test_setmouse(&lines - 3, 2) |
| 1501 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx') |
| 1502 | call assert_equal(1, c) |
| 1503 | " Mouse click outside of the list |
| 1504 | call test_setmouse(&lines - 6, 2) |
| 1505 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx') |
| 1506 | call assert_equal(-2, c) |
| 1507 | |
Bram Moolenaar | 947b39e | 2018-07-22 19:36:37 +0200 | [diff] [blame] | 1508 | call assert_fails('call inputlist("")', 'E686:') |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 1509 | call assert_fails('call inputlist(test_null_list())', 'E686:') |
Bram Moolenaar | 947b39e | 2018-07-22 19:36:37 +0200 | [diff] [blame] | 1510 | endfunc |
| 1511 | |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 1512 | func Test_balloon_show() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1513 | CheckFeature balloon_eval |
Bram Moolenaar | b47bed2 | 2021-04-14 17:06:43 +0200 | [diff] [blame] | 1514 | |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1515 | " This won't do anything but must not crash either. |
| 1516 | call balloon_show('hi!') |
| 1517 | if !has('gui_running') |
| 1518 | call balloon_show(range(3)) |
| 1519 | call balloon_show([]) |
Bram Moolenaar | a0107bd | 2017-03-02 22:48:01 +0100 | [diff] [blame] | 1520 | endif |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 1521 | endfunc |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 1522 | |
| 1523 | func Test_setbufvar_options() |
| 1524 | " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the |
| 1525 | " window layout. |
| 1526 | call assert_equal(1, winnr('$')) |
| 1527 | split dummy_preview |
| 1528 | resize 2 |
| 1529 | set winfixheight winfixwidth |
| 1530 | let prev_id = win_getid() |
| 1531 | |
| 1532 | wincmd j |
Bram Moolenaar | c05d1c0 | 2020-09-04 18:38:06 +0200 | [diff] [blame] | 1533 | let wh = winheight(0) |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 1534 | let dummy_buf = bufnr('dummy_buf1', v:true) |
| 1535 | call setbufvar(dummy_buf, '&buftype', 'nofile') |
| 1536 | execute 'belowright vertical split #' . dummy_buf |
Bram Moolenaar | c05d1c0 | 2020-09-04 18:38:06 +0200 | [diff] [blame] | 1537 | call assert_equal(wh, winheight(0)) |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 1538 | let dum1_id = win_getid() |
| 1539 | |
| 1540 | wincmd h |
Bram Moolenaar | c05d1c0 | 2020-09-04 18:38:06 +0200 | [diff] [blame] | 1541 | let wh = winheight(0) |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 1542 | let dummy_buf = bufnr('dummy_buf2', v:true) |
Bram Moolenaar | 196b466 | 2019-09-06 21:34:30 +0200 | [diff] [blame] | 1543 | eval 'nofile'->setbufvar(dummy_buf, '&buftype') |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 1544 | execute 'belowright vertical split #' . dummy_buf |
Bram Moolenaar | c05d1c0 | 2020-09-04 18:38:06 +0200 | [diff] [blame] | 1545 | call assert_equal(wh, winheight(0)) |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 1546 | |
| 1547 | bwipe! |
| 1548 | call win_gotoid(prev_id) |
| 1549 | bwipe! |
| 1550 | call win_gotoid(dum1_id) |
| 1551 | bwipe! |
| 1552 | endfunc |
Bram Moolenaar | d4863aa | 2017-04-07 19:50:12 +0200 | [diff] [blame] | 1553 | |
| 1554 | func Test_redo_in_nested_functions() |
| 1555 | nnoremap g. :set opfunc=Operator<CR>g@ |
| 1556 | function Operator( type, ... ) |
| 1557 | let @x = 'XXX' |
| 1558 | execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp' |
| 1559 | endfunction |
| 1560 | |
| 1561 | function! Apply() |
| 1562 | 5,6normal! . |
| 1563 | endfunction |
| 1564 | |
| 1565 | new |
| 1566 | call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3)) |
| 1567 | 1normal g.i" |
| 1568 | call assert_equal('some "XXX" text', getline(1)) |
| 1569 | 3,4normal . |
| 1570 | call assert_equal('some "XXX" text', getline(3)) |
| 1571 | call assert_equal('more "XXX" text', getline(4)) |
| 1572 | call Apply() |
| 1573 | call assert_equal('some "XXX" text', getline(5)) |
| 1574 | call assert_equal('more "XXX" text', getline(6)) |
| 1575 | bwipe! |
| 1576 | |
| 1577 | nunmap g. |
| 1578 | delfunc Operator |
| 1579 | delfunc Apply |
| 1580 | endfunc |
Bram Moolenaar | 2061552 | 2017-06-05 18:46:26 +0200 | [diff] [blame] | 1581 | |
| 1582 | func Test_shellescape() |
| 1583 | let save_shell = &shell |
| 1584 | set shell=bash |
| 1585 | call assert_equal("'text'", shellescape('text')) |
Bram Moolenaar | aad222c | 2019-09-06 22:46:09 +0200 | [diff] [blame] | 1586 | call assert_equal("'te\"xt'", 'te"xt'->shellescape()) |
Bram Moolenaar | 2061552 | 2017-06-05 18:46:26 +0200 | [diff] [blame] | 1587 | call assert_equal("'te'\\''xt'", shellescape("te'xt")) |
| 1588 | |
| 1589 | call assert_equal("'te%xt'", shellescape("te%xt")) |
| 1590 | call assert_equal("'te\\%xt'", shellescape("te%xt", 1)) |
| 1591 | call assert_equal("'te#xt'", shellescape("te#xt")) |
| 1592 | call assert_equal("'te\\#xt'", shellescape("te#xt", 1)) |
| 1593 | call assert_equal("'te!xt'", shellescape("te!xt")) |
| 1594 | call assert_equal("'te\\!xt'", shellescape("te!xt", 1)) |
| 1595 | |
| 1596 | call assert_equal("'te\nxt'", shellescape("te\nxt")) |
| 1597 | call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1)) |
| 1598 | set shell=tcsh |
| 1599 | call assert_equal("'te\\!xt'", shellescape("te!xt")) |
| 1600 | call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1)) |
| 1601 | call assert_equal("'te\\\nxt'", shellescape("te\nxt")) |
| 1602 | call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1)) |
| 1603 | |
| 1604 | let &shell = save_shell |
| 1605 | endfunc |
Bram Moolenaar | 295ac5a | 2018-03-22 23:04:02 +0100 | [diff] [blame] | 1606 | |
| 1607 | func Test_trim() |
| 1608 | 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] | 1609 | 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] | 1610 | call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t")) |
| 1611 | call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww")) |
| 1612 | call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail")) |
| 1613 | call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " ")) |
| 1614 | call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx")) |
| 1615 | call assert_equal("RESERVE", trim("你RESERVE好", "你好")) |
| 1616 | call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好")) |
| 1617 | call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", )) |
| 1618 | call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好")) |
| 1619 | call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes")) |
| 1620 | call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses")) |
| 1621 | call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要")) |
| 1622 | call assert_equal("", trim("", "")) |
| 1623 | call assert_equal("a", trim("a", "")) |
| 1624 | call assert_equal("", trim("", "a")) |
| 1625 | |
Bram Moolenaar | 2245ae1 | 2020-05-31 22:20:36 +0200 | [diff] [blame] | 1626 | call assert_equal("vim", trim(" vim ", " ", 0)) |
| 1627 | call assert_equal("vim ", trim(" vim ", " ", 1)) |
| 1628 | call assert_equal(" vim", trim(" vim ", " ", 2)) |
| 1629 | call assert_fails('eval trim(" vim ", " ", [])', 'E745:') |
| 1630 | call assert_fails('eval trim(" vim ", " ", -1)', 'E475:') |
| 1631 | call assert_fails('eval trim(" vim ", " ", 3)', 'E475:') |
| 1632 | |
Bram Moolenaar | 3f4f3d8 | 2019-09-04 20:05:59 +0200 | [diff] [blame] | 1633 | let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '') |
Bram Moolenaar | 295ac5a | 2018-03-22 23:04:02 +0100 | [diff] [blame] | 1634 | call assert_equal("x", trim(chars . "x" . chars)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 1635 | |
| 1636 | call assert_fails('let c=trim([])', 'E730:') |
Bram Moolenaar | 295ac5a | 2018-03-22 23:04:02 +0100 | [diff] [blame] | 1637 | endfunc |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 1638 | |
| 1639 | " Test for reg_recording() and reg_executing() |
| 1640 | func Test_reg_executing_and_recording() |
| 1641 | let s:reg_stat = '' |
| 1642 | func s:save_reg_stat() |
| 1643 | let s:reg_stat = reg_recording() . ':' . reg_executing() |
| 1644 | return '' |
| 1645 | endfunc |
| 1646 | |
| 1647 | new |
| 1648 | call s:save_reg_stat() |
| 1649 | call assert_equal(':', s:reg_stat) |
| 1650 | call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt') |
| 1651 | call assert_equal('a:', s:reg_stat) |
| 1652 | call feedkeys("@a", 'xt') |
| 1653 | call assert_equal(':a', s:reg_stat) |
| 1654 | call feedkeys("qb@aq", 'xt') |
| 1655 | call assert_equal('b:a', s:reg_stat) |
| 1656 | call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt') |
| 1657 | call assert_equal('":', s:reg_stat) |
| 1658 | |
Bram Moolenaar | cce713d | 2019-03-04 11:40:12 +0100 | [diff] [blame] | 1659 | " :normal command saves and restores reg_executing |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1660 | let s:reg_stat = '' |
Bram Moolenaar | cce713d | 2019-03-04 11:40:12 +0100 | [diff] [blame] | 1661 | let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>" |
| 1662 | func TestFunc() abort |
| 1663 | normal! ia |
| 1664 | endfunc |
| 1665 | call feedkeys("@q", 'xt') |
| 1666 | call assert_equal(':q', s:reg_stat) |
| 1667 | delfunc TestFunc |
| 1668 | |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1669 | " getchar() command saves and restores reg_executing |
| 1670 | map W :call TestFunc()<CR> |
| 1671 | let @q = "W" |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 1672 | let g:typed = '' |
| 1673 | let g:regs = [] |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1674 | func TestFunc() abort |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 1675 | let g:regs += [reg_executing()] |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1676 | let g:typed = getchar(0) |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 1677 | let g:regs += [reg_executing()] |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1678 | endfunc |
| 1679 | call feedkeys("@qy", 'xt') |
| 1680 | call assert_equal(char2nr("y"), g:typed) |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 1681 | call assert_equal(['q', 'q'], g:regs) |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1682 | delfunc TestFunc |
| 1683 | unmap W |
| 1684 | unlet g:typed |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 1685 | unlet g:regs |
| 1686 | |
| 1687 | " input() command saves and restores reg_executing |
| 1688 | map W :call TestFunc()<CR> |
| 1689 | let @q = "W" |
| 1690 | let g:typed = '' |
| 1691 | let g:regs = [] |
| 1692 | func TestFunc() abort |
| 1693 | let g:regs += [reg_executing()] |
Bram Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 1694 | let g:typed = '?'->input() |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 1695 | let g:regs += [reg_executing()] |
| 1696 | endfunc |
| 1697 | call feedkeys("@qy\<CR>", 'xt') |
| 1698 | call assert_equal("y", g:typed) |
| 1699 | call assert_equal(['q', 'q'], g:regs) |
| 1700 | delfunc TestFunc |
| 1701 | unmap W |
| 1702 | unlet g:typed |
| 1703 | unlet g:regs |
Bram Moolenaar | f0fab30 | 2019-03-05 12:24:10 +0100 | [diff] [blame] | 1704 | |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 1705 | bwipe! |
| 1706 | delfunc s:save_reg_stat |
| 1707 | unlet s:reg_stat |
| 1708 | endfunc |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1709 | |
Bram Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 1710 | func Test_inputsecret() |
| 1711 | map W :call TestFunc()<CR> |
| 1712 | let @q = "W" |
| 1713 | let g:typed1 = '' |
| 1714 | let g:typed2 = '' |
| 1715 | let g:regs = [] |
| 1716 | func TestFunc() abort |
| 1717 | let g:typed1 = '?'->inputsecret() |
| 1718 | let g:typed2 = inputsecret('password: ') |
| 1719 | endfunc |
| 1720 | call feedkeys("@qsomething\<CR>else\<CR>", 'xt') |
| 1721 | call assert_equal("something", g:typed1) |
| 1722 | call assert_equal("else", g:typed2) |
| 1723 | delfunc TestFunc |
| 1724 | unmap W |
| 1725 | unlet g:typed1 |
| 1726 | unlet g:typed2 |
| 1727 | endfunc |
| 1728 | |
Bram Moolenaar | 5d712e4 | 2019-09-03 23:37:01 +0200 | [diff] [blame] | 1729 | func Test_getchar() |
| 1730 | call feedkeys('a', '') |
| 1731 | call assert_equal(char2nr('a'), getchar()) |
| 1732 | |
Bram Moolenaar | db3a205 | 2019-11-16 18:22:41 +0100 | [diff] [blame] | 1733 | call setline(1, 'xxxx') |
Bram Moolenaar | 5d712e4 | 2019-09-03 23:37:01 +0200 | [diff] [blame] | 1734 | call test_setmouse(1, 3) |
| 1735 | let v:mouse_win = 9 |
| 1736 | let v:mouse_winid = 9 |
| 1737 | let v:mouse_lnum = 9 |
| 1738 | let v:mouse_col = 9 |
| 1739 | call feedkeys("\<S-LeftMouse>", '') |
| 1740 | call assert_equal("\<S-LeftMouse>", getchar()) |
| 1741 | call assert_equal(1, v:mouse_win) |
| 1742 | call assert_equal(win_getid(1), v:mouse_winid) |
| 1743 | call assert_equal(1, v:mouse_lnum) |
| 1744 | call assert_equal(3, v:mouse_col) |
Bram Moolenaar | db3a205 | 2019-11-16 18:22:41 +0100 | [diff] [blame] | 1745 | enew! |
Bram Moolenaar | 5d712e4 | 2019-09-03 23:37:01 +0200 | [diff] [blame] | 1746 | endfunc |
| 1747 | |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1748 | func Test_libcall_libcallnr() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1749 | CheckFeature libcall |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1750 | |
| 1751 | if has('win32') |
| 1752 | let libc = 'msvcrt.dll' |
| 1753 | elseif has('mac') |
| 1754 | let libc = 'libSystem.B.dylib' |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 1755 | elseif executable('ldd') |
| 1756 | let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>') |
| 1757 | endif |
| 1758 | if get(l:, 'libc', '') ==# '' |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1759 | " On Unix, libc.so can be in various places. |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 1760 | if has('linux') |
| 1761 | " There is not documented but regarding the 1st argument of glibc's |
| 1762 | " dlopen an empty string and nullptr are equivalent, so using an empty |
| 1763 | " string for the 1st argument of libcall allows to call functions. |
| 1764 | let libc = '' |
| 1765 | elseif has('sun') |
| 1766 | " Set the path to libc.so according to the architecture. |
| 1767 | let test_bits = system('file ' . GetVimProg()) |
| 1768 | let test_arch = system('uname -p') |
| 1769 | if test_bits =~ '64-bit' && test_arch =~ 'sparc' |
| 1770 | let libc = '/usr/lib/sparcv9/libc.so' |
| 1771 | elseif test_bits =~ '64-bit' && test_arch =~ 'i386' |
| 1772 | let libc = '/usr/lib/amd64/libc.so' |
| 1773 | else |
| 1774 | let libc = '/usr/lib/libc.so' |
| 1775 | endif |
| 1776 | else |
| 1777 | " Unfortunately skip this test until a good way is found. |
| 1778 | return |
| 1779 | endif |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1780 | endif |
| 1781 | |
| 1782 | if has('win32') |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 1783 | call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv')) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1784 | else |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 1785 | call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv')) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1786 | endif |
| 1787 | |
| 1788 | " If function returns NULL, libcall() should return an empty string. |
| 1789 | call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT')) |
| 1790 | |
| 1791 | " Test libcallnr() with string and integer argument. |
Bram Moolenaar | 02b3111 | 2019-08-31 22:16:38 +0200 | [diff] [blame] | 1792 | call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen')) |
| 1793 | call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper')) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1794 | |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1795 | call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", ['', 'E364:']) |
| 1796 | call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", ['', 'E364:']) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1797 | |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 1798 | call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", ['', 'E364:']) |
| 1799 | call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", ['', 'E364:']) |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1800 | endfunc |
Bram Moolenaar | d90a144 | 2018-07-15 20:24:31 +0200 | [diff] [blame] | 1801 | |
| 1802 | sandbox function Fsandbox() |
| 1803 | normal ix |
| 1804 | endfunc |
| 1805 | |
| 1806 | func Test_func_sandbox() |
| 1807 | sandbox let F = {-> 'hello'} |
| 1808 | call assert_equal('hello', F()) |
| 1809 | |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1810 | sandbox let F = {-> "normal ix\<Esc>"->execute()} |
Bram Moolenaar | d90a144 | 2018-07-15 20:24:31 +0200 | [diff] [blame] | 1811 | call assert_fails('call F()', 'E48:') |
| 1812 | unlet F |
| 1813 | |
| 1814 | call assert_fails('call Fsandbox()', 'E48:') |
| 1815 | delfunc Fsandbox |
Bram Moolenaar | 8dfcce3 | 2020-03-18 19:32:26 +0100 | [diff] [blame] | 1816 | |
| 1817 | " From a sandbox try to set a predefined variable (which cannot be modified |
| 1818 | " from a sandbox) |
| 1819 | call assert_fails('sandbox let v:lnum = 10', 'E794:') |
Bram Moolenaar | d90a144 | 2018-07-15 20:24:31 +0200 | [diff] [blame] | 1820 | endfunc |
Bram Moolenaar | 9e353b5 | 2018-11-04 23:39:38 +0100 | [diff] [blame] | 1821 | |
| 1822 | func EditAnotherFile() |
| 1823 | let word = expand('<cword>') |
| 1824 | edit Xfuncrange2 |
| 1825 | endfunc |
| 1826 | |
| 1827 | func Test_func_range_with_edit() |
| 1828 | " Define a function that edits another buffer, then call it with a range that |
| 1829 | " is invalid in that buffer. |
| 1830 | call writefile(['just one line'], 'Xfuncrange2') |
| 1831 | new |
Bram Moolenaar | 196b466 | 2019-09-06 21:34:30 +0200 | [diff] [blame] | 1832 | eval 10->range()->setline(1) |
Bram Moolenaar | 9e353b5 | 2018-11-04 23:39:38 +0100 | [diff] [blame] | 1833 | write Xfuncrange1 |
| 1834 | call assert_fails('5,8call EditAnotherFile()', 'E16:') |
| 1835 | |
| 1836 | call delete('Xfuncrange1') |
| 1837 | call delete('Xfuncrange2') |
| 1838 | bwipe! |
| 1839 | endfunc |
Bram Moolenaar | ded5f1b | 2018-11-10 17:33:29 +0100 | [diff] [blame] | 1840 | |
| 1841 | func Test_func_exists_on_reload() |
| 1842 | call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists') |
| 1843 | call assert_equal(0, exists('*ExistingFunction')) |
| 1844 | source Xfuncexists |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1845 | call assert_equal(1, '*ExistingFunction'->exists()) |
Bram Moolenaar | ded5f1b | 2018-11-10 17:33:29 +0100 | [diff] [blame] | 1846 | " Redefining a function when reloading a script is OK. |
| 1847 | source Xfuncexists |
| 1848 | call assert_equal(1, exists('*ExistingFunction')) |
| 1849 | |
| 1850 | " But redefining in another script is not OK. |
| 1851 | call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2') |
| 1852 | call assert_fails('source Xfuncexists2', 'E122:') |
| 1853 | |
Yegappan Lakshmanan | 611728f | 2021-05-24 15:15:47 +0200 | [diff] [blame] | 1854 | " Defining a new function from the cmdline should fail if the function is |
| 1855 | " already defined |
| 1856 | call assert_fails('call feedkeys(":func ExistingFunction()\<CR>", "xt")', 'E122:') |
| 1857 | |
Bram Moolenaar | ded5f1b | 2018-11-10 17:33:29 +0100 | [diff] [blame] | 1858 | delfunc ExistingFunction |
| 1859 | call assert_equal(0, exists('*ExistingFunction')) |
| 1860 | call writefile([ |
| 1861 | \ 'func ExistingFunction()', 'echo "yes"', 'endfunc', |
| 1862 | \ 'func ExistingFunction()', 'echo "no"', 'endfunc', |
| 1863 | \ ], 'Xfuncexists') |
| 1864 | call assert_fails('source Xfuncexists', 'E122:') |
| 1865 | call assert_equal(1, exists('*ExistingFunction')) |
| 1866 | |
| 1867 | call delete('Xfuncexists2') |
| 1868 | call delete('Xfuncexists') |
| 1869 | delfunc ExistingFunction |
| 1870 | endfunc |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 1871 | |
| 1872 | " Test confirm({msg} [, {choices} [, {default} [, {type}]]]) |
| 1873 | func Test_confirm() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 1874 | CheckUnix |
| 1875 | CheckNotGui |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 1876 | |
| 1877 | call feedkeys('o', 'L') |
| 1878 | let a = confirm('Press O to proceed') |
| 1879 | call assert_equal(1, a) |
| 1880 | |
| 1881 | call feedkeys('y', 'L') |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 1882 | let a = 'Are you sure?'->confirm("&Yes\n&No") |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 1883 | call assert_equal(1, a) |
| 1884 | |
| 1885 | call feedkeys('n', 'L') |
| 1886 | let a = confirm('Are you sure?', "&Yes\n&No") |
| 1887 | call assert_equal(2, a) |
| 1888 | |
| 1889 | " confirm() should return 0 when pressing CTRL-C. |
Bram Moolenaar | 7929651 | 2020-03-22 16:17:14 +0100 | [diff] [blame] | 1890 | call feedkeys("\<C-C>", 'L') |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 1891 | let a = confirm('Are you sure?', "&Yes\n&No") |
| 1892 | call assert_equal(0, a) |
| 1893 | |
| 1894 | " <Esc> requires another character to avoid it being seen as the start of an |
| 1895 | " escape sequence. Zero should be harmless. |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 1896 | eval "\<Esc>0"->feedkeys('L') |
Bram Moolenaar | 2e05009 | 2019-01-27 15:00:36 +0100 | [diff] [blame] | 1897 | let a = confirm('Are you sure?', "&Yes\n&No") |
| 1898 | call assert_equal(0, a) |
| 1899 | |
| 1900 | " Default choice is returned when pressing <CR>. |
| 1901 | call feedkeys("\<CR>", 'L') |
| 1902 | let a = confirm('Are you sure?', "&Yes\n&No") |
| 1903 | call assert_equal(1, a) |
| 1904 | |
| 1905 | call feedkeys("\<CR>", 'L') |
| 1906 | let a = confirm('Are you sure?', "&Yes\n&No", 2) |
| 1907 | call assert_equal(2, a) |
| 1908 | |
| 1909 | call feedkeys("\<CR>", 'L') |
| 1910 | let a = confirm('Are you sure?', "&Yes\n&No", 0) |
| 1911 | call assert_equal(0, a) |
| 1912 | |
| 1913 | " Test with the {type} 4th argument |
| 1914 | for type in ['Error', 'Question', 'Info', 'Warning', 'Generic'] |
| 1915 | call feedkeys('y', 'L') |
| 1916 | let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type) |
| 1917 | call assert_equal(1, a) |
| 1918 | endfor |
| 1919 | |
| 1920 | call assert_fails('call confirm([])', 'E730:') |
| 1921 | call assert_fails('call confirm("Are you sure?", [])', 'E730:') |
| 1922 | call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:') |
| 1923 | call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:') |
| 1924 | endfunc |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 1925 | |
| 1926 | func Test_platform_name() |
| 1927 | " The system matches at most only one name. |
Bram Moolenaar | 041c710 | 2020-05-30 18:14:57 +0200 | [diff] [blame] | 1928 | let names = ['amiga', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix'] |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 1929 | call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)'))) |
| 1930 | |
| 1931 | " Is Unix? |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 1932 | call assert_equal(has('bsd'), has('bsd') && has('unix')) |
| 1933 | call assert_equal(has('hpux'), has('hpux') && has('unix')) |
| 1934 | call assert_equal(has('linux'), has('linux') && has('unix')) |
| 1935 | call assert_equal(has('mac'), has('mac') && has('unix')) |
| 1936 | call assert_equal(has('qnx'), has('qnx') && has('unix')) |
| 1937 | call assert_equal(has('sun'), has('sun') && has('unix')) |
| 1938 | call assert_equal(has('win32'), has('win32') && !has('unix')) |
| 1939 | call assert_equal(has('win32unix'), has('win32unix') && has('unix')) |
| 1940 | |
| 1941 | if has('unix') && executable('uname') |
| 1942 | let uname = system('uname') |
Bram Moolenaar | a02e3f6 | 2019-02-07 21:27:14 +0100 | [diff] [blame] | 1943 | " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined |
| 1944 | call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd')) |
Bram Moolenaar | 39536dd | 2019-01-29 22:58:21 +0100 | [diff] [blame] | 1945 | call assert_equal(uname =~? 'HP-UX', has('hpux')) |
| 1946 | call assert_equal(uname =~? 'Linux', has('linux')) |
| 1947 | call assert_equal(uname =~? 'Darwin', has('mac')) |
| 1948 | call assert_equal(uname =~? 'QNX', has('qnx')) |
| 1949 | call assert_equal(uname =~? 'SunOS', has('sun')) |
| 1950 | call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix')) |
| 1951 | endif |
| 1952 | endfunc |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 1953 | |
| 1954 | func Test_readdir() |
| 1955 | call mkdir('Xdir') |
| 1956 | call writefile([], 'Xdir/foo.txt') |
| 1957 | call writefile([], 'Xdir/bar.txt') |
| 1958 | call mkdir('Xdir/dir') |
| 1959 | |
| 1960 | " All results |
| 1961 | let files = readdir('Xdir') |
| 1962 | call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files)) |
| 1963 | |
| 1964 | " Only results containing "f" |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1965 | let files = 'Xdir'->readdir({ x -> stridx(x, 'f') != -1 }) |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 1966 | call assert_equal(['foo.txt'], sort(files)) |
| 1967 | |
| 1968 | " Only .txt files |
| 1969 | let files = readdir('Xdir', { x -> x =~ '.txt$' }) |
| 1970 | call assert_equal(['bar.txt', 'foo.txt'], sort(files)) |
| 1971 | |
| 1972 | " Only .txt files with string |
| 1973 | let files = readdir('Xdir', 'v:val =~ ".txt$"') |
| 1974 | call assert_equal(['bar.txt', 'foo.txt'], sort(files)) |
| 1975 | |
| 1976 | " Limit to 1 result. |
| 1977 | let l = [] |
| 1978 | let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1}) |
| 1979 | call assert_equal(1, len(files)) |
| 1980 | |
Bram Moolenaar | 27da7de | 2019-09-03 17:13:37 +0200 | [diff] [blame] | 1981 | " Nested readdir() must not crash |
| 1982 | let files = readdir('Xdir', 'readdir("Xdir", "1") != []') |
| 1983 | call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt']) |
| 1984 | |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 1985 | eval 'Xdir'->delete('rf') |
Bram Moolenaar | 543c9b1 | 2019-04-05 22:50:40 +0200 | [diff] [blame] | 1986 | endfunc |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 1987 | |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1988 | func Test_readdirex() |
| 1989 | call mkdir('Xdir') |
Bram Moolenaar | 441d60e | 2020-06-02 22:19:50 +0200 | [diff] [blame] | 1990 | call writefile(['foo'], 'Xdir/foo.txt') |
| 1991 | call writefile(['barbar'], 'Xdir/bar.txt') |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1992 | call mkdir('Xdir/dir') |
| 1993 | |
| 1994 | " All results |
| 1995 | let files = readdirex('Xdir')->map({-> v:val.name}) |
| 1996 | call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files)) |
Bram Moolenaar | 441d60e | 2020-06-02 22:19:50 +0200 | [diff] [blame] | 1997 | let sizes = readdirex('Xdir')->map({-> v:val.size}) |
| 1998 | call assert_equal([0, 4, 7], sort(sizes)) |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 1999 | |
| 2000 | " Only results containing "f" |
| 2001 | let files = 'Xdir'->readdirex({ e -> stridx(e.name, 'f') != -1 }) |
| 2002 | \ ->map({-> v:val.name}) |
| 2003 | call assert_equal(['foo.txt'], sort(files)) |
| 2004 | |
| 2005 | " Only .txt files |
| 2006 | let files = readdirex('Xdir', { e -> e.name =~ '.txt$' }) |
| 2007 | \ ->map({-> v:val.name}) |
| 2008 | call assert_equal(['bar.txt', 'foo.txt'], sort(files)) |
| 2009 | |
| 2010 | " Only .txt files with string |
| 2011 | let files = readdirex('Xdir', 'v:val.name =~ ".txt$"') |
| 2012 | \ ->map({-> v:val.name}) |
| 2013 | call assert_equal(['bar.txt', 'foo.txt'], sort(files)) |
| 2014 | |
| 2015 | " Limit to 1 result. |
| 2016 | let l = [] |
| 2017 | let files = readdirex('Xdir', {e -> len(add(l, e.name)) == 2 ? -1 : 1}) |
| 2018 | \ ->map({-> v:val.name}) |
| 2019 | call assert_equal(1, len(files)) |
| 2020 | |
| 2021 | " Nested readdirex() must not crash |
| 2022 | let files = readdirex('Xdir', 'readdirex("Xdir", "1") != []') |
| 2023 | \ ->map({-> v:val.name}) |
| 2024 | call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt']) |
| 2025 | |
Bram Moolenaar | fdcbe3c | 2020-06-15 21:41:56 +0200 | [diff] [blame] | 2026 | " report broken link correctly |
Bram Moolenaar | ab54032 | 2020-06-10 15:55:36 +0200 | [diff] [blame] | 2027 | if has("unix") |
| 2028 | call writefile([], 'Xdir/abc.txt') |
| 2029 | call system("ln -s Xdir/abc.txt Xdir/link") |
| 2030 | call delete('Xdir/abc.txt') |
| 2031 | let files = readdirex('Xdir', 'readdirex("Xdir", "1") != []') |
| 2032 | \ ->map({-> v:val.name .. '_' .. v:val.type}) |
| 2033 | call sort(files)->assert_equal( |
| 2034 | \ ['bar.txt_file', 'dir_dir', 'foo.txt_file', 'link_link']) |
| 2035 | endif |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 2036 | eval 'Xdir'->delete('rf') |
Bram Moolenaar | aab9fad | 2020-10-11 14:28:11 +0200 | [diff] [blame] | 2037 | |
| 2038 | call assert_fails('call readdirex("doesnotexist")', 'E484:') |
Bram Moolenaar | 6c9ba04 | 2020-06-01 16:09:41 +0200 | [diff] [blame] | 2039 | endfunc |
| 2040 | |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2041 | func Test_readdirex_sort() |
| 2042 | CheckUnix |
| 2043 | " Skip tests on Mac OS X and Cygwin (does not allow several files with different casing) |
| 2044 | if has("osxdarwin") || has("osx") || has("macunix") || has("win32unix") |
| 2045 | throw 'Skipped: Test_readdirex_sort on systems that do not allow this using the default filesystem' |
| 2046 | endif |
| 2047 | let _collate = v:collate |
| 2048 | call mkdir('Xdir2') |
| 2049 | call writefile(['1'], 'Xdir2/README.txt') |
| 2050 | call writefile(['2'], 'Xdir2/Readme.txt') |
| 2051 | call writefile(['3'], 'Xdir2/readme.txt') |
| 2052 | |
| 2053 | " 1) default |
| 2054 | let files = readdirex('Xdir2')->map({-> v:val.name}) |
| 2055 | let default = copy(files) |
| 2056 | call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort using default') |
| 2057 | |
| 2058 | " 2) no sorting |
| 2059 | let files = readdirex('Xdir2', 1, #{sort: 'none'})->map({-> v:val.name}) |
| 2060 | let unsorted = copy(files) |
| 2061 | call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], sort(files), 'unsorted') |
Bram Moolenaar | a1bc6f1 | 2020-06-16 22:27:39 +0200 | [diff] [blame] | 2062 | call assert_fails("call readdirex('Xdir2', 1, #{slort: 'none'})", 'E857: Dictionary key "sort" required') |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2063 | |
| 2064 | " 3) sort by case (same as default) |
| 2065 | let files = readdirex('Xdir2', 1, #{sort: 'case'})->map({-> v:val.name}) |
| 2066 | call assert_equal(default, files, 'sort by case') |
| 2067 | |
| 2068 | " 4) sort by ignoring case |
| 2069 | let files = readdirex('Xdir2', 1, #{sort: 'icase'})->map({-> v:val.name}) |
| 2070 | call assert_equal(unsorted->sort('i'), files, 'sort by icase') |
| 2071 | |
| 2072 | " 5) Default Collation |
| 2073 | let collate = v:collate |
| 2074 | lang collate C |
| 2075 | let files = readdirex('Xdir2', 1, #{sort: 'collate'})->map({-> v:val.name}) |
| 2076 | call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort by C collation') |
| 2077 | |
| 2078 | " 6) Collation de_DE |
| 2079 | " Switch locale, this may not work on the CI system, if the locale isn't |
| 2080 | " available |
| 2081 | try |
| 2082 | lang collate de_DE |
| 2083 | let files = readdirex('Xdir2', 1, #{sort: 'collate'})->map({-> v:val.name}) |
| 2084 | call assert_equal(['readme.txt', 'Readme.txt', 'README.txt'], files, 'sort by de_DE collation') |
| 2085 | catch |
| 2086 | throw 'Skipped: de_DE collation is not available' |
| 2087 | |
| 2088 | finally |
| 2089 | exe 'lang collate' collate |
| 2090 | eval 'Xdir2'->delete('rf') |
| 2091 | endtry |
| 2092 | endfunc |
| 2093 | |
| 2094 | func Test_readdir_sort() |
| 2095 | " some more cases for testing sorting for readdirex |
| 2096 | let dir = 'Xdir3' |
| 2097 | call mkdir(dir) |
| 2098 | call writefile(['1'], dir .. '/README.txt') |
| 2099 | call writefile(['2'], dir .. '/Readm.txt') |
| 2100 | call writefile(['3'], dir .. '/read.txt') |
| 2101 | call writefile(['4'], dir .. '/Z.txt') |
| 2102 | call writefile(['5'], dir .. '/a.txt') |
| 2103 | call writefile(['6'], dir .. '/b.txt') |
| 2104 | |
| 2105 | " 1) default |
| 2106 | let files = readdir(dir) |
| 2107 | let default = copy(files) |
| 2108 | call assert_equal(default->sort(), files, 'sort using default') |
| 2109 | |
| 2110 | " 2) sort by case (same as default) |
| 2111 | let files = readdir(dir, '1', #{sort: 'case'}) |
| 2112 | call assert_equal(default, files, 'sort using default') |
| 2113 | |
| 2114 | " 3) sort by ignoring case |
| 2115 | let files = readdir(dir, '1', #{sort: 'icase'}) |
| 2116 | call assert_equal(default->sort('i'), files, 'sort by ignoring case') |
| 2117 | |
Bram Moolenaar | e17f881 | 2020-06-17 20:30:44 +0200 | [diff] [blame] | 2118 | " 4) collation |
| 2119 | let collate = v:collate |
| 2120 | lang collate C |
| 2121 | let files = readdir(dir, 1, #{sort: 'collate'}) |
| 2122 | call assert_equal(default->sort(), files, 'sort by C collation') |
| 2123 | exe "lang collate" collate |
| 2124 | |
| 2125 | " 5) Errors |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 2126 | call assert_fails('call readdir(dir, 1, 1)', 'E715:') |
Bram Moolenaar | e17f881 | 2020-06-17 20:30:44 +0200 | [diff] [blame] | 2127 | call assert_fails('call readdir(dir, 1, #{sorta: 1})') |
| 2128 | call assert_fails('call readdirex(dir, 1, #{sorta: 1})') |
| 2129 | |
| 2130 | " 6) ignore other values in dict |
| 2131 | let files = readdir(dir, '1', #{sort: 'c'}) |
| 2132 | call assert_equal(default, files, 'sort using default2') |
| 2133 | |
| 2134 | " Cleanup |
| 2135 | exe "lang collate" collate |
| 2136 | |
Bram Moolenaar | 84cf6bd | 2020-06-16 20:03:43 +0200 | [diff] [blame] | 2137 | eval dir->delete('rf') |
| 2138 | endfunc |
| 2139 | |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 2140 | func Test_delete_rf() |
| 2141 | call mkdir('Xdir') |
| 2142 | call writefile([], 'Xdir/foo.txt') |
| 2143 | call writefile([], 'Xdir/bar.txt') |
| 2144 | call mkdir('Xdir/[a-1]') " issue #696 |
| 2145 | call writefile([], 'Xdir/[a-1]/foo.txt') |
| 2146 | call writefile([], 'Xdir/[a-1]/bar.txt') |
| 2147 | call assert_true(filereadable('Xdir/foo.txt')) |
Bram Moolenaar | a420896 | 2019-08-24 20:50:19 +0200 | [diff] [blame] | 2148 | call assert_true('Xdir/[a-1]/foo.txt'->filereadable()) |
Bram Moolenaar | 701ff0a | 2019-05-24 14:14:14 +0200 | [diff] [blame] | 2149 | |
| 2150 | call assert_equal(0, delete('Xdir', 'rf')) |
| 2151 | call assert_false(filereadable('Xdir/foo.txt')) |
| 2152 | call assert_false(filereadable('Xdir/[a-1]/foo.txt')) |
| 2153 | endfunc |
| 2154 | |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2155 | func Test_call() |
| 2156 | call assert_equal(3, call('len', [123])) |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 2157 | call assert_equal(3, 'len'->call([123])) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2158 | call assert_fails("call call('len', 123)", 'E714:') |
| 2159 | call assert_equal(0, call('', [])) |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 2160 | call assert_equal(0, call('len', test_null_list())) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2161 | |
| 2162 | function Mylen() dict |
| 2163 | return len(self.data) |
| 2164 | endfunction |
| 2165 | let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")} |
Bram Moolenaar | 64b4d73 | 2019-08-22 22:18:17 +0200 | [diff] [blame] | 2166 | eval mydict.len->call([], mydict)->assert_equal(4) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2167 | call assert_fails("call call('Mylen', [], 0)", 'E715:') |
Bram Moolenaar | 67322bf | 2020-12-06 15:03:19 +0100 | [diff] [blame] | 2168 | call assert_fails('call foo', 'E107:') |
Dominique Pelle | fe8ebdb | 2021-05-13 14:55:55 +0200 | [diff] [blame] | 2169 | |
| 2170 | " This once caused a crash. |
| 2171 | call call(test_null_function(), []) |
| 2172 | call call(test_null_partial(), []) |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2173 | endfunc |
| 2174 | |
| 2175 | func Test_char2nr() |
| 2176 | call assert_equal(12354, char2nr('あ', 1)) |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 2177 | call assert_equal(120, 'x'->char2nr()) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 2178 | set encoding=latin1 |
| 2179 | call assert_equal(120, 'x'->char2nr()) |
| 2180 | set encoding=utf-8 |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2181 | endfunc |
| 2182 | |
Bram Moolenaar | 4e4473c | 2020-08-28 22:24:57 +0200 | [diff] [blame] | 2183 | func Test_charclass() |
| 2184 | call assert_equal(0, charclass(' ')) |
| 2185 | call assert_equal(1, charclass('.')) |
| 2186 | call assert_equal(2, charclass('x')) |
| 2187 | call assert_equal(3, charclass("\u203c")) |
| 2188 | endfunc |
| 2189 | |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 2190 | func Test_eventhandler() |
| 2191 | call assert_equal(0, eventhandler()) |
| 2192 | endfunc |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2193 | |
| 2194 | func Test_bufadd_bufload() |
| 2195 | call assert_equal(0, bufexists('someName')) |
| 2196 | let buf = bufadd('someName') |
| 2197 | call assert_notequal(0, buf) |
| 2198 | call assert_equal(1, bufexists('someName')) |
| 2199 | call assert_equal(0, getbufvar(buf, '&buflisted')) |
| 2200 | call assert_equal(0, bufloaded(buf)) |
| 2201 | call bufload(buf) |
| 2202 | call assert_equal(1, bufloaded(buf)) |
| 2203 | call assert_equal([''], getbufline(buf, 1, '$')) |
| 2204 | |
| 2205 | let curbuf = bufnr('') |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 2206 | eval ['some', 'text']->writefile('XotherName') |
Bram Moolenaar | 073e4b9 | 2019-08-18 23:01:56 +0200 | [diff] [blame] | 2207 | let buf = 'XotherName'->bufadd() |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2208 | call assert_notequal(0, buf) |
Bram Moolenaar | 073e4b9 | 2019-08-18 23:01:56 +0200 | [diff] [blame] | 2209 | eval 'XotherName'->bufexists()->assert_equal(1) |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2210 | call assert_equal(0, getbufvar(buf, '&buflisted')) |
| 2211 | call assert_equal(0, bufloaded(buf)) |
Bram Moolenaar | 073e4b9 | 2019-08-18 23:01:56 +0200 | [diff] [blame] | 2212 | eval buf->bufload() |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2213 | call assert_equal(1, bufloaded(buf)) |
| 2214 | call assert_equal(['some', 'text'], getbufline(buf, 1, '$')) |
| 2215 | call assert_equal(curbuf, bufnr('')) |
| 2216 | |
Bram Moolenaar | 892ae72 | 2019-06-30 20:33:01 +0200 | [diff] [blame] | 2217 | let buf1 = bufadd('') |
| 2218 | let buf2 = bufadd('') |
| 2219 | call assert_notequal(0, buf1) |
| 2220 | call assert_notequal(0, buf2) |
| 2221 | call assert_notequal(buf1, buf2) |
| 2222 | call assert_equal(1, bufexists(buf1)) |
| 2223 | call assert_equal(1, bufexists(buf2)) |
| 2224 | call assert_equal(0, bufloaded(buf1)) |
| 2225 | exe 'bwipe ' .. buf1 |
| 2226 | call assert_equal(0, bufexists(buf1)) |
| 2227 | call assert_equal(1, bufexists(buf2)) |
| 2228 | exe 'bwipe ' .. buf2 |
| 2229 | call assert_equal(0, bufexists(buf2)) |
| 2230 | |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2231 | bwipe someName |
Bram Moolenaar | 3940ec6 | 2019-07-05 21:53:24 +0200 | [diff] [blame] | 2232 | bwipe XotherName |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2233 | call assert_equal(0, bufexists('someName')) |
Bram Moolenaar | 3940ec6 | 2019-07-05 21:53:24 +0200 | [diff] [blame] | 2234 | call delete('XotherName') |
Bram Moolenaar | 15e248e | 2019-06-30 20:21:37 +0200 | [diff] [blame] | 2235 | endfunc |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2236 | |
| 2237 | func Test_state() |
| 2238 | CheckRunVimInTerminal |
| 2239 | |
Bram Moolenaar | 3ed9efc | 2020-03-26 16:50:57 +0100 | [diff] [blame] | 2240 | let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>" |
| 2241 | |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2242 | let lines =<< trim END |
| 2243 | call setline(1, ['one', 'two', 'three']) |
| 2244 | map ;; gg |
Bram Moolenaar | b7a97ef | 2019-09-28 22:11:56 +0200 | [diff] [blame] | 2245 | set complete=. |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2246 | func RunTimer() |
| 2247 | call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')}) |
| 2248 | endfunc |
| 2249 | au Filetype foobar let g:state = state()|let g:mode = mode() |
| 2250 | END |
| 2251 | call writefile(lines, 'XState') |
| 2252 | let buf = RunVimInTerminal('-S XState', #{rows: 6}) |
| 2253 | |
| 2254 | " Using a ":" command Vim is busy, thus "S" is returned |
| 2255 | call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>") |
| 2256 | call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000) |
| 2257 | call term_sendkeys(buf, ":\<CR>") |
| 2258 | |
| 2259 | " Using a timer callback |
| 2260 | call term_sendkeys(buf, ":call RunTimer()\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2261 | call TermWait(buf, 25) |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2262 | call term_sendkeys(buf, getstate) |
| 2263 | call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000) |
| 2264 | |
| 2265 | " Halfway a mapping |
| 2266 | call term_sendkeys(buf, ":call RunTimer()\<CR>;") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2267 | call TermWait(buf, 25) |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2268 | call term_sendkeys(buf, ";") |
| 2269 | call term_sendkeys(buf, getstate) |
| 2270 | call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000) |
| 2271 | |
Bram Moolenaar | b7a97ef | 2019-09-28 22:11:56 +0200 | [diff] [blame] | 2272 | " Insert mode completion (bit slower on Mac) |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2273 | call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2274 | call TermWait(buf, 25) |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2275 | call term_sendkeys(buf, "\<Esc>") |
| 2276 | call term_sendkeys(buf, getstate) |
| 2277 | call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000) |
| 2278 | |
| 2279 | " Autocommand executing |
| 2280 | call term_sendkeys(buf, ":set filetype=foobar\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2281 | call TermWait(buf, 25) |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2282 | call term_sendkeys(buf, getstate) |
| 2283 | call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000) |
| 2284 | |
| 2285 | " Todo: "w" - waiting for ch_evalexpr() |
| 2286 | |
| 2287 | " messages scrolled |
| 2288 | call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2289 | call TermWait(buf, 25) |
Bram Moolenaar | c258549 | 2019-09-22 21:29:53 +0200 | [diff] [blame] | 2290 | call term_sendkeys(buf, "\<CR>") |
| 2291 | call term_sendkeys(buf, getstate) |
| 2292 | call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000) |
| 2293 | |
| 2294 | call StopVimInTerminal(buf) |
| 2295 | call delete('XState') |
| 2296 | endfunc |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 2297 | |
| 2298 | func Test_range() |
| 2299 | " destructuring |
| 2300 | let [x, y] = range(2) |
| 2301 | call assert_equal([0, 1], [x, y]) |
| 2302 | |
| 2303 | " index |
| 2304 | call assert_equal(4, range(1, 10)[3]) |
| 2305 | |
| 2306 | " add() |
| 2307 | call assert_equal([0, 1, 2, 3], add(range(3), 3)) |
| 2308 | call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3))) |
| 2309 | call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3))) |
| 2310 | |
| 2311 | " append() |
| 2312 | new |
| 2313 | call append('.', range(5)) |
| 2314 | call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$')) |
| 2315 | bwipe! |
| 2316 | |
| 2317 | " appendbufline() |
| 2318 | new |
| 2319 | call appendbufline(bufnr(''), '.', range(5)) |
| 2320 | call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$')) |
| 2321 | bwipe! |
| 2322 | |
| 2323 | " call() |
| 2324 | func TwoArgs(a, b) |
| 2325 | return [a:a, a:b] |
| 2326 | endfunc |
| 2327 | call assert_equal([0, 1], call('TwoArgs', range(2))) |
| 2328 | |
| 2329 | " col() |
| 2330 | new |
| 2331 | call setline(1, ['foo', 'bar']) |
| 2332 | call assert_equal(2, col(range(1, 2))) |
| 2333 | bwipe! |
| 2334 | |
| 2335 | " complete() |
| 2336 | execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>" |
| 2337 | " complete_info() |
| 2338 | execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>" |
| 2339 | |
| 2340 | " copy() |
| 2341 | call assert_equal([1, 2, 3], copy(range(1, 3))) |
| 2342 | |
| 2343 | " count() |
| 2344 | call assert_equal(0, count(range(0), 3)) |
| 2345 | call assert_equal(0, count(range(2), 3)) |
| 2346 | call assert_equal(1, count(range(5), 3)) |
| 2347 | |
| 2348 | " cursor() |
| 2349 | new |
| 2350 | call setline(1, ['aaa', 'bbb', 'ccc']) |
| 2351 | call cursor(range(1, 2)) |
| 2352 | call assert_equal([2, 1], [col('.'), line('.')]) |
| 2353 | bwipe! |
| 2354 | |
| 2355 | " deepcopy() |
| 2356 | call assert_equal([1, 2, 3], deepcopy(range(1, 3))) |
| 2357 | |
| 2358 | " empty() |
| 2359 | call assert_true(empty(range(0))) |
| 2360 | call assert_false(empty(range(2))) |
| 2361 | |
| 2362 | " execute() |
| 2363 | new |
| 2364 | call setline(1, ['aaa', 'bbb', 'ccc']) |
| 2365 | call execute(range(3)) |
| 2366 | call assert_equal(2, line('.')) |
| 2367 | bwipe! |
| 2368 | |
| 2369 | " extend() |
| 2370 | call assert_equal([1, 2, 3, 4], extend([1], range(2, 4))) |
| 2371 | call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4))) |
| 2372 | call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4])) |
| 2373 | |
| 2374 | " filter() |
| 2375 | call assert_equal([1, 3], filter(range(5), 'v:val % 2')) |
Bram Moolenaar | f8ca03b | 2020-11-28 20:32:29 +0100 | [diff] [blame] | 2376 | 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] | 2377 | |
| 2378 | " funcref() |
| 2379 | call assert_equal([0, 1], funcref('TwoArgs', range(2))()) |
| 2380 | |
| 2381 | " function() |
| 2382 | call assert_equal([0, 1], function('TwoArgs', range(2))()) |
| 2383 | |
| 2384 | " garbagecollect() |
| 2385 | let thelist = [1, range(2), 3] |
| 2386 | let otherlist = range(3) |
| 2387 | call test_garbagecollect_now() |
| 2388 | |
| 2389 | " get() |
| 2390 | call assert_equal(4, get(range(1, 10), 3)) |
| 2391 | call assert_equal(-1, get(range(1, 10), 42, -1)) |
| 2392 | |
| 2393 | " index() |
| 2394 | call assert_equal(1, index(range(1, 5), 2)) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 2395 | call assert_fails("echo index([1, 2], 1, [])", 'E745:') |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 2396 | |
| 2397 | " inputlist() |
Bram Moolenaar | 272ca95 | 2020-01-28 20:49:11 +0100 | [diff] [blame] | 2398 | call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x') |
| 2399 | call assert_equal(1, result) |
| 2400 | call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x') |
| 2401 | call assert_equal(1, result) |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 2402 | |
| 2403 | " insert() |
| 2404 | call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42)) |
| 2405 | call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0)) |
| 2406 | call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1)) |
| 2407 | call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4)) |
| 2408 | call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1)) |
| 2409 | call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5)) |
| 2410 | |
| 2411 | " join() |
| 2412 | call assert_equal('0 1 2 3 4', join(range(5))) |
| 2413 | |
Bram Moolenaar | 272ca95 | 2020-01-28 20:49:11 +0100 | [diff] [blame] | 2414 | " json_encode() |
| 2415 | call assert_equal('[0,1,2,3]', json_encode(range(4))) |
| 2416 | |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 2417 | " len() |
| 2418 | call assert_equal(0, len(range(0))) |
| 2419 | call assert_equal(2, len(range(2))) |
| 2420 | call assert_equal(5, len(range(0, 12, 3))) |
| 2421 | call assert_equal(4, len(range(3, 0, -1))) |
| 2422 | |
| 2423 | " list2str() |
| 2424 | call assert_equal('ABC', list2str(range(65, 67))) |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 2425 | call assert_fails('let s = list2str(5)', 'E474:') |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 2426 | |
| 2427 | " lock() |
| 2428 | let thelist = range(5) |
| 2429 | lockvar thelist |
| 2430 | |
| 2431 | " map() |
| 2432 | 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] | 2433 | call assert_equal([3, 5, 7, 9, 11], map(map(range(5), 'v:val * 2'), 'v:val + 3')) |
| 2434 | call assert_equal([2, 6], map(filter(range(5), 'v:val % 2'), 'v:val * 2')) |
| 2435 | 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] | 2436 | |
| 2437 | " match() |
| 2438 | call assert_equal(3, match(range(5), 3)) |
| 2439 | |
| 2440 | " matchaddpos() |
| 2441 | highlight MyGreenGroup ctermbg=green guibg=green |
| 2442 | call matchaddpos('MyGreenGroup', range(line('.'), line('.'))) |
| 2443 | |
| 2444 | " matchend() |
| 2445 | call assert_equal(4, matchend(range(5), '4')) |
| 2446 | call assert_equal(3, matchend(range(1, 5), '4')) |
| 2447 | call assert_equal(-1, matchend(range(1, 5), '42')) |
| 2448 | |
| 2449 | " matchstrpos() |
| 2450 | call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4')) |
| 2451 | call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4')) |
| 2452 | call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42')) |
| 2453 | |
| 2454 | " max() reverse() |
| 2455 | call assert_equal(0, max(range(0))) |
| 2456 | call assert_equal(0, max(range(10, 9))) |
| 2457 | call assert_equal(9, max(range(10))) |
| 2458 | call assert_equal(18, max(range(0, 20, 3))) |
| 2459 | call assert_equal(20, max(range(20, 0, -3))) |
| 2460 | call assert_equal(99999, max(range(100000))) |
| 2461 | call assert_equal(99999, max(range(99999, 0, -1))) |
| 2462 | call assert_equal(99999, max(reverse(range(100000)))) |
| 2463 | call assert_equal(99999, max(reverse(range(99999, 0, -1)))) |
| 2464 | |
| 2465 | " min() reverse() |
| 2466 | call assert_equal(0, min(range(0))) |
| 2467 | call assert_equal(0, min(range(10, 9))) |
| 2468 | call assert_equal(5, min(range(5, 10))) |
| 2469 | call assert_equal(5, min(range(5, 10, 3))) |
| 2470 | call assert_equal(2, min(range(20, 0, -3))) |
| 2471 | call assert_equal(0, min(range(100000))) |
| 2472 | call assert_equal(0, min(range(99999, 0, -1))) |
| 2473 | call assert_equal(0, min(reverse(range(100000)))) |
| 2474 | call assert_equal(0, min(reverse(range(99999, 0, -1)))) |
| 2475 | |
| 2476 | " remove() |
| 2477 | call assert_equal(1, remove(range(1, 10), 0)) |
| 2478 | call assert_equal(2, remove(range(1, 10), 1)) |
| 2479 | call assert_equal(9, remove(range(1, 10), 8)) |
| 2480 | call assert_equal(10, remove(range(1, 10), 9)) |
| 2481 | call assert_equal(10, remove(range(1, 10), -1)) |
| 2482 | call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4)) |
| 2483 | |
| 2484 | " repeat() |
| 2485 | call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2)) |
| 2486 | call assert_equal([0, 1, 2], repeat(range(3), 1)) |
| 2487 | call assert_equal([], repeat(range(3), 0)) |
| 2488 | call assert_equal([], repeat(range(5, 4), 2)) |
| 2489 | call assert_equal([], repeat(range(5, 4), 0)) |
| 2490 | |
| 2491 | " reverse() |
| 2492 | call assert_equal([2, 1, 0], reverse(range(3))) |
| 2493 | call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1))) |
| 2494 | call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10))) |
| 2495 | call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20))) |
| 2496 | call assert_equal([16, 13, 10], reverse(range(10, 18, 3))) |
| 2497 | call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3))) |
| 2498 | call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3))) |
| 2499 | call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3))) |
| 2500 | call assert_equal([], reverse(range(0))) |
| 2501 | |
| 2502 | " TODO: setpos() |
| 2503 | " new |
| 2504 | " call setline(1, repeat([''], bufnr(''))) |
| 2505 | " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6)) |
| 2506 | " call setpos('x', range(bufnr(''), bufnr('') + 3)) |
| 2507 | " bwipe! |
| 2508 | |
| 2509 | " setreg() |
| 2510 | call setreg('a', range(3)) |
| 2511 | call assert_equal("0\n1\n2\n", getreg('a')) |
| 2512 | |
Bram Moolenaar | b099202 | 2020-01-30 14:55:42 +0100 | [diff] [blame] | 2513 | " settagstack() |
| 2514 | call settagstack(1, #{items : range(4)}) |
Bram Moolenaar | 94255df | 2020-02-05 20:10:33 +0100 | [diff] [blame] | 2515 | |
Bram Moolenaar | b099202 | 2020-01-30 14:55:42 +0100 | [diff] [blame] | 2516 | " sign_define() |
| 2517 | call assert_fails("call sign_define(range(5))", "E715:") |
| 2518 | call assert_fails("call sign_placelist(range(5))", "E715:") |
| 2519 | |
| 2520 | " sign_undefine() |
| 2521 | call assert_fails("call sign_undefine(range(5))", "E908:") |
| 2522 | |
| 2523 | " sign_unplacelist() |
| 2524 | call assert_fails("call sign_unplacelist(range(5))", "E715:") |
| 2525 | |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 2526 | " sort() |
| 2527 | call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1))) |
| 2528 | |
| 2529 | " string() |
| 2530 | call assert_equal('[0, 1, 2, 3, 4]', string(range(5))) |
| 2531 | |
Bram Moolenaar | b099202 | 2020-01-30 14:55:42 +0100 | [diff] [blame] | 2532 | " taglist() with 'tagfunc' |
| 2533 | func TagFunc(pattern, flags, info) |
| 2534 | return range(10) |
| 2535 | endfunc |
| 2536 | set tagfunc=TagFunc |
| 2537 | call assert_fails("call taglist('asdf')", 'E987:') |
| 2538 | set tagfunc= |
Bram Moolenaar | 94255df | 2020-02-05 20:10:33 +0100 | [diff] [blame] | 2539 | |
Bram Moolenaar | b099202 | 2020-01-30 14:55:42 +0100 | [diff] [blame] | 2540 | " term_start() |
Bram Moolenaar | 705724e | 2020-01-31 21:13:42 +0100 | [diff] [blame] | 2541 | if has('terminal') && has('termguicolors') |
Bram Moolenaar | b099202 | 2020-01-30 14:55:42 +0100 | [diff] [blame] | 2542 | call assert_fails('call term_start(range(3, 4))', 'E474:') |
| 2543 | let g:terminal_ansi_colors = range(16) |
Bram Moolenaar | 94255df | 2020-02-05 20:10:33 +0100 | [diff] [blame] | 2544 | if has('win32') |
| 2545 | let cmd = "cmd /c dir" |
| 2546 | else |
| 2547 | let cmd = "ls" |
| 2548 | endif |
| 2549 | call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"})', 'E475:') |
Bram Moolenaar | b099202 | 2020-01-30 14:55:42 +0100 | [diff] [blame] | 2550 | unlet g:terminal_ansi_colors |
| 2551 | endif |
| 2552 | |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 2553 | " type() |
| 2554 | call assert_equal(v:t_list, type(range(5))) |
| 2555 | |
| 2556 | " uniq() |
| 2557 | call assert_equal([0, 1, 2, 3, 4], uniq(range(5))) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 2558 | |
| 2559 | " errors |
| 2560 | call assert_fails('let x=range(2, 8, 0)', 'E726:') |
| 2561 | call assert_fails('let x=range(3, 1)', 'E727:') |
| 2562 | call assert_fails('let x=range(1, 3, -2)', 'E727:') |
Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 2563 | call assert_fails('let x=range([])', 'E745:') |
| 2564 | call assert_fails('let x=range(1, [])', 'E745:') |
| 2565 | call assert_fails('let x=range(1, 4, [])', 'E745:') |
Bram Moolenaar | 50985eb | 2020-01-27 22:09:39 +0100 | [diff] [blame] | 2566 | endfunc |
Bram Moolenaar | 4132eb5 | 2020-02-14 16:53:00 +0100 | [diff] [blame] | 2567 | |
| 2568 | func Test_echoraw() |
| 2569 | CheckScreendump |
| 2570 | |
| 2571 | " Normally used for escape codes, but let's test with a CR. |
| 2572 | let lines =<< trim END |
| 2573 | call echoraw("hello\<CR>x") |
| 2574 | END |
| 2575 | call writefile(lines, 'XTest_echoraw') |
| 2576 | let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40}) |
| 2577 | call VerifyScreenDump(buf, 'Test_functions_echoraw', {}) |
| 2578 | |
| 2579 | " clean up |
| 2580 | call StopVimInTerminal(buf) |
| 2581 | call delete('XTest_echoraw') |
| 2582 | endfunc |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 2583 | |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 2584 | " Test for echo highlighting |
| 2585 | func Test_echohl() |
| 2586 | echohl Search |
| 2587 | echo 'Vim' |
| 2588 | call assert_equal('Vim', Screenline(&lines)) |
| 2589 | " TODO: How to check the highlight group used by echohl? |
| 2590 | " ScreenAttrs() returns all zeros. |
| 2591 | echohl None |
| 2592 | endfunc |
| 2593 | |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 2594 | " Test for the eval() function |
| 2595 | func Test_eval() |
| 2596 | call assert_fails("call eval('5 a')", 'E488:') |
| 2597 | endfunc |
| 2598 | |
| 2599 | " Test for the nr2char() function |
| 2600 | func Test_nr2char() |
| 2601 | set encoding=latin1 |
| 2602 | call assert_equal('@', nr2char(64)) |
| 2603 | set encoding=utf8 |
| 2604 | call assert_equal('a', nr2char(97, 1)) |
| 2605 | call assert_equal('a', nr2char(97, 0)) |
Bram Moolenaar | f7271e8 | 2020-05-24 18:45:07 +0200 | [diff] [blame] | 2606 | |
| 2607 | call assert_equal("\x80\xfc\b\xf4\x80\xfeX\x80\xfeX\x80\xfeX", eval('"\<M-' .. nr2char(0x100000) .. '>"')) |
Bram Moolenaar | 1919371 | 2020-05-25 23:01:42 +0200 | [diff] [blame] | 2608 | call assert_equal("\x80\xfc\b\xfd\x80\xfeX\x80\xfeX\x80\xfeX\x80\xfeX\x80\xfeX", eval('"\<M-' .. nr2char(0x40000000) .. '>"')) |
Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 2609 | endfunc |
| 2610 | |
| 2611 | " Test for screenattr(), screenchar() and screenchars() functions |
| 2612 | func Test_screen_functions() |
| 2613 | call assert_equal(-1, screenattr(-1, -1)) |
| 2614 | call assert_equal(-1, screenchar(-1, -1)) |
| 2615 | call assert_equal([], screenchars(-1, -1)) |
| 2616 | endfunc |
| 2617 | |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 2618 | " Test for getcurpos() and setpos() |
| 2619 | func Test_getcurpos_setpos() |
| 2620 | new |
| 2621 | call setline(1, ['012345678', '012345678']) |
| 2622 | normal gg6l |
| 2623 | let sp = getcurpos() |
| 2624 | normal 0 |
| 2625 | call setpos('.', sp) |
| 2626 | normal jyl |
| 2627 | call assert_equal('6', @") |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 2628 | call assert_equal(-1, setpos('.', test_null_list())) |
| 2629 | call assert_equal(-1, setpos('.', {})) |
Bram Moolenaar | 99ca9c4 | 2020-09-22 21:55:41 +0200 | [diff] [blame] | 2630 | |
| 2631 | let winid = win_getid() |
| 2632 | normal G$ |
| 2633 | let pos = getcurpos() |
| 2634 | wincmd w |
| 2635 | call assert_equal(pos, getcurpos(winid)) |
| 2636 | |
| 2637 | wincmd w |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 2638 | close! |
Bram Moolenaar | 99ca9c4 | 2020-09-22 21:55:41 +0200 | [diff] [blame] | 2639 | |
| 2640 | call assert_equal(getcurpos(), getcurpos(0)) |
| 2641 | call assert_equal([0, 0, 0, 0, 0], getcurpos(-1)) |
| 2642 | call assert_equal([0, 0, 0, 0, 0], getcurpos(1999)) |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 2643 | endfunc |
| 2644 | |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 2645 | " Test for glob() |
| 2646 | func Test_glob() |
| 2647 | call assert_equal('', glob(test_null_string())) |
| 2648 | call assert_equal('', globpath(test_null_string(), test_null_string())) |
Yegappan Lakshmanan | 46aa6f9 | 2021-05-19 17:15:04 +0200 | [diff] [blame] | 2649 | call assert_fails("let x = globpath(&rtp, 'syntax/c.vim', [])", 'E745:') |
Bram Moolenaar | 1b04ce2 | 2020-08-21 22:46:11 +0200 | [diff] [blame] | 2650 | |
| 2651 | call writefile([], 'Xglob1') |
| 2652 | call writefile([], 'XGLOB2') |
| 2653 | set wildignorecase |
| 2654 | " Sort output of glob() otherwise we end up with different |
| 2655 | " ordering depending on whether file system is case-sensitive. |
| 2656 | call assert_equal(['XGLOB2', 'Xglob1'], sort(glob('Xglob[12]', 0, 1))) |
| 2657 | set wildignorecase& |
| 2658 | |
| 2659 | call delete('Xglob1') |
| 2660 | call delete('XGLOB2') |
| 2661 | |
| 2662 | call assert_fails("call glob('*', 0, {})", 'E728:') |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 2663 | endfunc |
| 2664 | |
| 2665 | " Test for browse() |
| 2666 | func Test_browse() |
| 2667 | CheckFeature browse |
| 2668 | call assert_fails('call browse([], "open", "x", "a.c")', 'E745:') |
| 2669 | endfunc |
| 2670 | |
| 2671 | " Test for browsedir() |
| 2672 | func Test_browsedir() |
| 2673 | CheckFeature browse |
| 2674 | call assert_fails('call browsedir("open", [])', 'E730:') |
| 2675 | endfunc |
| 2676 | |
Bram Moolenaar | b47bed2 | 2021-04-14 17:06:43 +0200 | [diff] [blame] | 2677 | func HasDefault(msg = 'msg') |
| 2678 | return a:msg |
| 2679 | endfunc |
| 2680 | |
| 2681 | func Test_default_arg_value() |
| 2682 | call assert_equal('msg', HasDefault()) |
| 2683 | endfunc |
| 2684 | |
Yegappan Lakshmanan | 34fcb69 | 2021-05-25 20:14:00 +0200 | [diff] [blame] | 2685 | " Test for gettext() |
| 2686 | func Test_gettext() |
| 2687 | call assert_fails('call gettext(1)', 'E475:') |
| 2688 | endfunc |
| 2689 | |
Bram Moolenaar | 3d9c4ee | 2021-05-31 22:15:26 +0200 | [diff] [blame] | 2690 | func Test_builtin_check() |
| 2691 | call assert_fails('let g:["trim"] = {x -> " " .. x}', 'E704:') |
| 2692 | call assert_fails('let g:.trim = {x -> " " .. x}', 'E704:') |
Bram Moolenaar | b54abee | 2021-06-02 11:49:23 +0200 | [diff] [blame] | 2693 | call assert_fails('let l:["trim"] = {x -> " " .. x}', 'E704:') |
| 2694 | call assert_fails('let l:.trim = {x -> " " .. x}', 'E704:') |
| 2695 | let lines =<< trim END |
| 2696 | vim9script |
| 2697 | var s:trim = (x) => " " .. x |
| 2698 | END |
| 2699 | call CheckScriptFailure(lines, 'E704:') |
Bram Moolenaar | 6f1d2aa | 2021-06-01 21:21:55 +0200 | [diff] [blame] | 2700 | |
| 2701 | call assert_fails('call extend(g:, #{foo: { -> "foo" }})', 'E704:') |
| 2702 | let g:bar = 123 |
| 2703 | call extend(g:, #{bar: { -> "foo" }}, "keep") |
| 2704 | call assert_fails('call extend(g:, #{bar: { -> "foo" }}, "force")', 'E704:') |
Bram Moolenaar | 3d9c4ee | 2021-05-31 22:15:26 +0200 | [diff] [blame] | 2705 | endfunc |
| 2706 | |
| 2707 | |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 2708 | " vim: shiftwidth=2 sts=2 expandtab |