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