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