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