Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 1 | " Tests for various functions. |
Bram Moolenaar | f1c118b | 2018-09-03 22:08:10 +0200 | [diff] [blame] | 2 | source shared.vim |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 3 | |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 4 | " Must be done first, since the alternate buffer must be unset. |
| 5 | func Test_00_bufexists() |
| 6 | call assert_equal(0, bufexists('does_not_exist')) |
| 7 | call assert_equal(1, bufexists(bufnr('%'))) |
| 8 | call assert_equal(0, bufexists(0)) |
| 9 | new Xfoo |
| 10 | let bn = bufnr('%') |
| 11 | call assert_equal(1, bufexists(bn)) |
| 12 | call assert_equal(1, bufexists('Xfoo')) |
| 13 | call assert_equal(1, bufexists(getcwd() . '/Xfoo')) |
| 14 | call assert_equal(1, bufexists(0)) |
| 15 | bw |
| 16 | call assert_equal(0, bufexists(bn)) |
| 17 | call assert_equal(0, bufexists('Xfoo')) |
| 18 | endfunc |
| 19 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 20 | func Test_empty() |
| 21 | call assert_equal(1, empty('')) |
| 22 | call assert_equal(0, empty('a')) |
| 23 | |
| 24 | call assert_equal(1, empty(0)) |
| 25 | call assert_equal(1, empty(-0)) |
| 26 | call assert_equal(0, empty(1)) |
| 27 | call assert_equal(0, empty(-1)) |
| 28 | |
| 29 | call assert_equal(1, empty(0.0)) |
| 30 | call assert_equal(1, empty(-0.0)) |
| 31 | call assert_equal(0, empty(1.0)) |
| 32 | call assert_equal(0, empty(-1.0)) |
| 33 | call assert_equal(0, empty(1.0/0.0)) |
| 34 | call assert_equal(0, empty(0.0/0.0)) |
| 35 | |
| 36 | call assert_equal(1, empty([])) |
| 37 | call assert_equal(0, empty(['a'])) |
| 38 | |
| 39 | call assert_equal(1, empty({})) |
| 40 | call assert_equal(0, empty({'a':1})) |
| 41 | |
| 42 | call assert_equal(1, empty(v:null)) |
| 43 | call assert_equal(1, empty(v:none)) |
| 44 | call assert_equal(1, empty(v:false)) |
| 45 | call assert_equal(0, empty(v:true)) |
| 46 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 47 | if has('channel') |
| 48 | call assert_equal(1, empty(test_null_channel())) |
| 49 | endif |
| 50 | if has('job') |
| 51 | call assert_equal(1, empty(test_null_job())) |
| 52 | endif |
| 53 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 54 | call assert_equal(0, empty(function('Test_empty'))) |
| 55 | endfunc |
| 56 | |
| 57 | func Test_len() |
| 58 | call assert_equal(1, len(0)) |
| 59 | call assert_equal(2, len(12)) |
| 60 | |
| 61 | call assert_equal(0, len('')) |
| 62 | call assert_equal(2, len('ab')) |
| 63 | |
| 64 | call assert_equal(0, len([])) |
| 65 | call assert_equal(2, len([2, 1])) |
| 66 | |
| 67 | call assert_equal(0, len({})) |
| 68 | call assert_equal(2, len({'a': 1, 'b': 2})) |
| 69 | |
| 70 | call assert_fails('call len(v:none)', 'E701:') |
| 71 | call assert_fails('call len({-> 0})', 'E701:') |
| 72 | endfunc |
| 73 | |
| 74 | func Test_max() |
| 75 | call assert_equal(0, max([])) |
| 76 | call assert_equal(2, max([2])) |
| 77 | call assert_equal(2, max([1, 2])) |
| 78 | call assert_equal(2, max([1, 2, v:null])) |
| 79 | |
| 80 | call assert_equal(0, max({})) |
| 81 | call assert_equal(2, max({'a':1, 'b':2})) |
| 82 | |
| 83 | call assert_fails('call max(1)', 'E712:') |
| 84 | call assert_fails('call max(v:none)', 'E712:') |
| 85 | endfunc |
| 86 | |
| 87 | func Test_min() |
| 88 | call assert_equal(0, min([])) |
| 89 | call assert_equal(2, min([2])) |
| 90 | call assert_equal(1, min([1, 2])) |
| 91 | call assert_equal(0, min([1, 2, v:null])) |
| 92 | |
| 93 | call assert_equal(0, min({})) |
| 94 | call assert_equal(1, min({'a':1, 'b':2})) |
| 95 | |
| 96 | call assert_fails('call min(1)', 'E712:') |
| 97 | call assert_fails('call min(v:none)', 'E712:') |
| 98 | endfunc |
| 99 | |
Bram Moolenaar | 42ab17b | 2018-05-20 14:11:10 +0200 | [diff] [blame] | 100 | func Test_strwidth() |
| 101 | for aw in ['single', 'double'] |
| 102 | exe 'set ambiwidth=' . aw |
| 103 | call assert_equal(0, strwidth('')) |
| 104 | call assert_equal(1, strwidth("\t")) |
| 105 | call assert_equal(3, strwidth('Vim')) |
| 106 | call assert_equal(4, strwidth(1234)) |
| 107 | call assert_equal(5, strwidth(-1234)) |
| 108 | |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 109 | call assert_equal(2, strwidth('😉')) |
| 110 | call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde')) |
| 111 | call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße')) |
Bram Moolenaar | 42ab17b | 2018-05-20 14:11:10 +0200 | [diff] [blame] | 112 | |
| 113 | call assert_fails('call strwidth({->0})', 'E729:') |
| 114 | call assert_fails('call strwidth([])', 'E730:') |
| 115 | call assert_fails('call strwidth({})', 'E731:') |
| 116 | call assert_fails('call strwidth(1.2)', 'E806:') |
| 117 | endfor |
| 118 | |
| 119 | set ambiwidth& |
| 120 | endfunc |
| 121 | |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 122 | func Test_str2nr() |
| 123 | call assert_equal(0, str2nr('')) |
| 124 | call assert_equal(1, str2nr('1')) |
| 125 | call assert_equal(1, str2nr(' 1 ')) |
| 126 | |
| 127 | call assert_equal(1, str2nr('+1')) |
| 128 | call assert_equal(1, str2nr('+ 1')) |
| 129 | call assert_equal(1, str2nr(' + 1 ')) |
| 130 | |
| 131 | call assert_equal(-1, str2nr('-1')) |
| 132 | call assert_equal(-1, str2nr('- 1')) |
| 133 | call assert_equal(-1, str2nr(' - 1 ')) |
| 134 | |
| 135 | call assert_equal(123456789, str2nr('123456789')) |
| 136 | call assert_equal(-123456789, str2nr('-123456789')) |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 137 | |
| 138 | call assert_equal(5, str2nr('101', 2)) |
| 139 | call assert_equal(5, str2nr('0b101', 2)) |
| 140 | call assert_equal(5, str2nr('0B101', 2)) |
| 141 | call assert_equal(-5, str2nr('-101', 2)) |
| 142 | call assert_equal(-5, str2nr('-0b101', 2)) |
| 143 | call assert_equal(-5, str2nr('-0B101', 2)) |
| 144 | |
| 145 | call assert_equal(65, str2nr('101', 8)) |
| 146 | call assert_equal(65, str2nr('0101', 8)) |
| 147 | call assert_equal(-65, str2nr('-101', 8)) |
| 148 | call assert_equal(-65, str2nr('-0101', 8)) |
| 149 | |
| 150 | call assert_equal(11259375, str2nr('abcdef', 16)) |
| 151 | call assert_equal(11259375, str2nr('ABCDEF', 16)) |
| 152 | call assert_equal(-11259375, str2nr('-ABCDEF', 16)) |
| 153 | call assert_equal(11259375, str2nr('0xabcdef', 16)) |
| 154 | call assert_equal(11259375, str2nr('0Xabcdef', 16)) |
| 155 | call assert_equal(11259375, str2nr('0XABCDEF', 16)) |
| 156 | call assert_equal(-11259375, str2nr('-0xABCDEF', 16)) |
| 157 | |
| 158 | call assert_equal(0, str2nr('0x10')) |
| 159 | call assert_equal(0, str2nr('0b10')) |
| 160 | call assert_equal(1, str2nr('12', 2)) |
| 161 | call assert_equal(1, str2nr('18', 8)) |
| 162 | call assert_equal(1, str2nr('1g', 16)) |
| 163 | |
| 164 | call assert_equal(0, str2nr(v:null)) |
| 165 | call assert_equal(0, str2nr(v:none)) |
| 166 | |
| 167 | call assert_fails('call str2nr([])', 'E730:') |
| 168 | call assert_fails('call str2nr({->2})', 'E729:') |
| 169 | call assert_fails('call str2nr(1.2)', 'E806:') |
| 170 | call assert_fails('call str2nr(10, [])', 'E474:') |
| 171 | endfunc |
| 172 | |
| 173 | func Test_strftime() |
| 174 | if !exists('*strftime') |
| 175 | return |
| 176 | endif |
| 177 | " Format of strftime() depends on system. We assume |
| 178 | " that basic formats tested here are available and |
| 179 | " identical on all systems which support strftime(). |
| 180 | " |
| 181 | " The 2nd parameter of strftime() is a local time, so the output day |
| 182 | " of strftime() can be 17 or 18, depending on timezone. |
| 183 | call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512)) |
| 184 | " |
| 185 | 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\)$', strftime('%Y-%m-%d %H:%M:%S')) |
| 186 | |
| 187 | call assert_fails('call strftime([])', 'E730:') |
| 188 | call assert_fails('call strftime("%Y", [])', 'E745:') |
| 189 | endfunc |
| 190 | |
Bram Moolenaar | 2610990 | 2018-10-06 15:43:17 +0200 | [diff] [blame] | 191 | func Test_resolve() |
| 192 | if !has('unix') |
| 193 | return |
| 194 | endif |
| 195 | |
| 196 | " Xlink1 -> Xlink2 |
| 197 | " Xlink2 -> Xlink3 |
| 198 | silent !ln -s -f Xlink2 Xlink1 |
| 199 | silent !ln -s -f Xlink3 Xlink2 |
| 200 | call assert_equal('Xlink3', resolve('Xlink1')) |
| 201 | call assert_equal('./Xlink3', resolve('./Xlink1')) |
| 202 | call assert_equal('Xlink3/', resolve('Xlink2/')) |
| 203 | " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?! |
| 204 | "call assert_equal('Xlink3/', resolve('Xlink1/')) |
| 205 | "call assert_equal('./Xlink3/', resolve('./Xlink1/')) |
| 206 | "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/')) |
| 207 | call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1')) |
| 208 | |
| 209 | " Test resolve() with a symlink cycle. |
| 210 | " Xlink1 -> Xlink2 |
| 211 | " Xlink2 -> Xlink3 |
| 212 | " Xlink3 -> Xlink1 |
| 213 | silent !ln -s -f Xlink1 Xlink3 |
| 214 | call assert_fails('call resolve("Xlink1")', 'E655:') |
| 215 | call assert_fails('call resolve("./Xlink1")', 'E655:') |
| 216 | call assert_fails('call resolve("Xlink2")', 'E655:') |
| 217 | call assert_fails('call resolve("Xlink3")', 'E655:') |
| 218 | call delete('Xlink1') |
| 219 | call delete('Xlink2') |
| 220 | call delete('Xlink3') |
| 221 | |
| 222 | silent !ln -s -f Xdir//Xfile Xlink |
| 223 | call assert_equal('Xdir/Xfile', resolve('Xlink')) |
| 224 | call delete('Xlink') |
| 225 | |
| 226 | silent !ln -s -f Xlink2/ Xlink1 |
| 227 | call assert_equal('Xlink2', resolve('Xlink1')) |
| 228 | call assert_equal('Xlink2/', resolve('Xlink1/')) |
| 229 | call delete('Xlink1') |
| 230 | |
| 231 | silent !ln -s -f ./Xlink2 Xlink1 |
| 232 | call assert_equal('Xlink2', resolve('Xlink1')) |
| 233 | call assert_equal('./Xlink2', resolve('./Xlink1')) |
| 234 | call delete('Xlink1') |
| 235 | endfunc |
| 236 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 237 | func Test_simplify() |
| 238 | call assert_equal('', simplify('')) |
| 239 | call assert_equal('/', simplify('/')) |
| 240 | call assert_equal('/', simplify('/.')) |
| 241 | call assert_equal('/', simplify('/..')) |
| 242 | call assert_equal('/...', simplify('/...')) |
| 243 | call assert_equal('./dir/file', simplify('./dir/file')) |
| 244 | call assert_equal('./dir/file', simplify('.///dir//file')) |
| 245 | call assert_equal('./dir/file', simplify('./dir/./file')) |
| 246 | call assert_equal('./file', simplify('./dir/../file')) |
| 247 | call assert_equal('../dir/file', simplify('dir/../../dir/file')) |
| 248 | call assert_equal('./file', simplify('dir/.././file')) |
| 249 | |
| 250 | call assert_fails('call simplify({->0})', 'E729:') |
| 251 | call assert_fails('call simplify([])', 'E730:') |
| 252 | call assert_fails('call simplify({})', 'E731:') |
| 253 | call assert_fails('call simplify(1.2)', 'E806:') |
Bram Moolenaar | 08243d2 | 2017-01-10 16:12:29 +0100 | [diff] [blame] | 254 | endfunc |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 255 | |
Bram Moolenaar | bfde0b4 | 2018-08-08 22:27:31 +0200 | [diff] [blame] | 256 | func Test_pathshorten() |
| 257 | call assert_equal('', pathshorten('')) |
| 258 | call assert_equal('foo', pathshorten('foo')) |
| 259 | call assert_equal('/foo', pathshorten('/foo')) |
| 260 | call assert_equal('f/', pathshorten('foo/')) |
| 261 | call assert_equal('f/bar', pathshorten('foo/bar')) |
| 262 | call assert_equal('f/b/foobar', pathshorten('foo/bar/foobar')) |
| 263 | call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar')) |
| 264 | call assert_equal('.f/bar', pathshorten('.foo/bar')) |
| 265 | call assert_equal('~f/bar', pathshorten('~foo/bar')) |
| 266 | call assert_equal('~.f/bar', pathshorten('~.foo/bar')) |
| 267 | call assert_equal('.~f/bar', pathshorten('.~foo/bar')) |
| 268 | call assert_equal('~/f/bar', pathshorten('~/foo/bar')) |
| 269 | endfunc |
| 270 | |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 271 | func Test_strpart() |
| 272 | call assert_equal('de', strpart('abcdefg', 3, 2)) |
| 273 | call assert_equal('ab', strpart('abcdefg', -2, 4)) |
| 274 | call assert_equal('abcdefg', strpart('abcdefg', -2)) |
| 275 | call assert_equal('fg', strpart('abcdefg', 5, 4)) |
| 276 | call assert_equal('defg', strpart('abcdefg', 3)) |
| 277 | |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 278 | call assert_equal('lép', strpart('éléphant', 2, 4)) |
| 279 | call assert_equal('léphant', strpart('éléphant', 2)) |
Bram Moolenaar | c7d16dc | 2017-11-18 20:32:03 +0100 | [diff] [blame] | 280 | endfunc |
| 281 | |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 282 | func Test_tolower() |
| 283 | call assert_equal("", tolower("")) |
| 284 | |
| 285 | " Test with all printable ASCII characters. |
| 286 | call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~', |
| 287 | \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')) |
| 288 | |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 289 | " Test with a few uppercase diacritics. |
| 290 | call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ")) |
| 291 | call assert_equal("bḃḇ", tolower("BḂḆ")) |
| 292 | call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ")) |
| 293 | call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ")) |
| 294 | call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ")) |
| 295 | call assert_equal("fḟ ", tolower("FḞ ")) |
| 296 | call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ")) |
| 297 | call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ")) |
| 298 | call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ")) |
| 299 | call assert_equal("jĵ", tolower("JĴ")) |
| 300 | call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ")) |
| 301 | call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ")) |
| 302 | call assert_equal("mḿṁ", tolower("MḾṀ")) |
| 303 | call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ")) |
| 304 | call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ")) |
| 305 | call assert_equal("pṕṗ", tolower("PṔṖ")) |
| 306 | call assert_equal("q", tolower("Q")) |
| 307 | call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ")) |
| 308 | call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ")) |
| 309 | call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ")) |
| 310 | call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ")) |
| 311 | call assert_equal("vṽ", tolower("VṼ")) |
| 312 | call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ")) |
| 313 | call assert_equal("xẋẍ", tolower("XẊẌ")) |
| 314 | call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ")) |
| 315 | call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ")) |
| 316 | |
| 317 | " Test with a few lowercase diacritics, which should remain unchanged. |
| 318 | call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả")) |
| 319 | call assert_equal("bḃḇ", tolower("bḃḇ")) |
| 320 | call assert_equal("cçćĉċč", tolower("cçćĉċč")) |
| 321 | call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ")) |
| 322 | call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ")) |
| 323 | call assert_equal("fḟ", tolower("fḟ")) |
| 324 | call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ")) |
| 325 | call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ")) |
| 326 | call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ")) |
| 327 | call assert_equal("jĵǰ", tolower("jĵǰ")) |
| 328 | call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ")) |
| 329 | call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ")) |
| 330 | call assert_equal("mḿṁ ", tolower("mḿṁ ")) |
| 331 | call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ")) |
| 332 | call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ")) |
| 333 | call assert_equal("pṕṗ", tolower("pṕṗ")) |
| 334 | call assert_equal("q", tolower("q")) |
| 335 | call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ")) |
| 336 | call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ")) |
| 337 | call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ")) |
| 338 | call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ")) |
| 339 | call assert_equal("vṽ", tolower("vṽ")) |
| 340 | call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ")) |
| 341 | call assert_equal("ẋẍ", tolower("ẋẍ")) |
| 342 | call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ")) |
| 343 | call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ")) |
| 344 | |
| 345 | " According to https://twitter.com/jifa/status/625776454479970304 |
| 346 | " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase |
| 347 | " in length (2 to 3 bytes) when lowercased. So let's test them. |
| 348 | call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ")) |
Bram Moolenaar | e6640ad | 2017-12-22 21:06:56 +0100 | [diff] [blame] | 349 | |
| 350 | " This call to tolower with invalid utf8 sequence used to cause access to |
| 351 | " invalid memory. |
| 352 | call tolower("\xC0\x80\xC0") |
| 353 | call tolower("123\xC0\x80\xC0") |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 354 | endfunc |
| 355 | |
| 356 | func Test_toupper() |
| 357 | call assert_equal("", toupper("")) |
| 358 | |
| 359 | " Test with all printable ASCII characters. |
| 360 | call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~', |
| 361 | \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')) |
| 362 | |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 363 | " Test with a few lowercase diacritics. |
| 364 | call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("aàáâãäåāăąǎǟǡả")) |
| 365 | call assert_equal("BḂḆ", toupper("bḃḇ")) |
| 366 | call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč")) |
| 367 | call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ")) |
| 368 | call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ")) |
| 369 | call assert_equal("FḞ", toupper("fḟ")) |
| 370 | call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ")) |
| 371 | call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ")) |
| 372 | call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ")) |
| 373 | call assert_equal("JĴǰ", toupper("jĵǰ")) |
| 374 | call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ")) |
| 375 | call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ")) |
| 376 | call assert_equal("MḾṀ ", toupper("mḿṁ ")) |
| 377 | call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ")) |
| 378 | call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ")) |
| 379 | call assert_equal("PṔṖ", toupper("pṕṗ")) |
| 380 | call assert_equal("Q", toupper("q")) |
| 381 | call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ")) |
| 382 | call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ")) |
| 383 | call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ")) |
| 384 | call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ")) |
| 385 | call assert_equal("VṼ", toupper("vṽ")) |
| 386 | call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ")) |
| 387 | call assert_equal("ẊẌ", toupper("ẋẍ")) |
| 388 | call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ")) |
| 389 | call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ")) |
| 390 | |
| 391 | " Test that uppercase diacritics, which should remain unchanged. |
| 392 | call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ")) |
| 393 | call assert_equal("BḂḆ", toupper("BḂḆ")) |
| 394 | call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ")) |
| 395 | call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ")) |
| 396 | call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ")) |
| 397 | call assert_equal("FḞ ", toupper("FḞ ")) |
| 398 | call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ")) |
| 399 | call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ")) |
| 400 | call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ")) |
| 401 | call assert_equal("JĴ", toupper("JĴ")) |
| 402 | call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ")) |
| 403 | call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ")) |
| 404 | call assert_equal("MḾṀ", toupper("MḾṀ")) |
| 405 | call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ")) |
| 406 | call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ")) |
| 407 | call assert_equal("PṔṖ", toupper("PṔṖ")) |
| 408 | call assert_equal("Q", toupper("Q")) |
| 409 | call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ")) |
| 410 | call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ")) |
| 411 | call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ")) |
| 412 | call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ")) |
| 413 | call assert_equal("VṼ", toupper("VṼ")) |
| 414 | call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ")) |
| 415 | call assert_equal("XẊẌ", toupper("XẊẌ")) |
| 416 | call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ")) |
| 417 | call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ")) |
| 418 | |
Bram Moolenaar | 24c2e48 | 2017-01-29 15:45:12 +0100 | [diff] [blame] | 419 | call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ")) |
Bram Moolenaar | e6640ad | 2017-12-22 21:06:56 +0100 | [diff] [blame] | 420 | |
| 421 | " This call to toupper with invalid utf8 sequence used to cause access to |
| 422 | " invalid memory. |
| 423 | call toupper("\xC0\x80\xC0") |
| 424 | call toupper("123\xC0\x80\xC0") |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 425 | endfunc |
| 426 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 427 | " Tests for the mode() function |
| 428 | let current_modes = '' |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 429 | func Save_mode() |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 430 | let g:current_modes = mode(0) . '-' . mode(1) |
| 431 | return '' |
| 432 | endfunc |
Bram Moolenaar | cc5b22b | 2017-01-26 22:51:56 +0100 | [diff] [blame] | 433 | |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 434 | func Test_mode() |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 435 | new |
| 436 | call append(0, ["Blue Ball Black", "Brown Band Bowl", ""]) |
| 437 | |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 438 | " Only complete from the current buffer. |
| 439 | set complete=. |
| 440 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 441 | inoremap <F2> <C-R>=Save_mode()<CR> |
| 442 | |
| 443 | normal! 3G |
| 444 | exe "normal i\<F2>\<Esc>" |
| 445 | call assert_equal('i-i', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 446 | " i_CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 447 | exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u" |
| 448 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 449 | " i_CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 450 | exe "normal iBro\<C-P>\<F2>\<Esc>u" |
| 451 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 452 | " i_CTRL-X |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 453 | exe "normal iBa\<C-X>\<F2>\<Esc>u" |
| 454 | call assert_equal('i-ix', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 455 | " i_CTRL-X CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 456 | exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u" |
| 457 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 458 | " i_CTRL-X CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 459 | exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u" |
| 460 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 461 | " i_CTRL-X CTRL-P + CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 462 | exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" |
| 463 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 464 | " i_CTRL-X CTRL-L: Multiple matches |
| 465 | exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u" |
| 466 | call assert_equal('i-ic', g:current_modes) |
| 467 | " i_CTRL-X CTRL-L: Single match |
| 468 | exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u" |
| 469 | call assert_equal('i-ic', g:current_modes) |
| 470 | " i_CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 471 | exe "normal iCom\<C-P>\<F2>\<Esc>u" |
| 472 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 473 | " i_CTRL-X CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 474 | exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u" |
| 475 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 476 | " i_CTRL-X CTRL-L: No match |
| 477 | exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u" |
| 478 | call assert_equal('i-ic', g:current_modes) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 479 | |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 480 | " R_CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 481 | exe "normal RBa\<C-P>\<F2>\<Esc>u" |
| 482 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 483 | " R_CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 484 | exe "normal RBro\<C-P>\<F2>\<Esc>u" |
| 485 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 486 | " R_CTRL-X |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 487 | exe "normal RBa\<C-X>\<F2>\<Esc>u" |
| 488 | call assert_equal('R-Rx', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 489 | " R_CTRL-X CTRL-P: Multiple matches |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 490 | exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u" |
| 491 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 492 | " R_CTRL-X CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 493 | exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u" |
| 494 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 495 | " R_CTRL-X CTRL-P + CTRL-P: Single match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 496 | exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" |
| 497 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 498 | " R_CTRL-X CTRL-L: Multiple matches |
| 499 | exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u" |
| 500 | call assert_equal('R-Rc', g:current_modes) |
| 501 | " R_CTRL-X CTRL-L: Single match |
| 502 | exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u" |
| 503 | call assert_equal('R-Rc', g:current_modes) |
| 504 | " R_CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 505 | exe "normal RCom\<C-P>\<F2>\<Esc>u" |
| 506 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 507 | " R_CTRL-X CTRL-P: No match |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 508 | exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u" |
| 509 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e971df3 | 2017-02-05 14:15:29 +0100 | [diff] [blame] | 510 | " R_CTRL-X CTRL-L: No match |
| 511 | exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u" |
| 512 | call assert_equal('R-Rc', g:current_modes) |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 513 | |
| 514 | call assert_equal('n', mode(0)) |
| 515 | call assert_equal('n', mode(1)) |
| 516 | |
Bram Moolenaar | 612cc38 | 2018-07-29 15:34:26 +0200 | [diff] [blame] | 517 | " i_CTRL-O |
| 518 | exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>" |
| 519 | call assert_equal("n-niI", g:current_modes) |
| 520 | |
| 521 | " R_CTRL-O |
| 522 | exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>" |
| 523 | call assert_equal("n-niR", g:current_modes) |
| 524 | |
| 525 | " gR_CTRL-O |
| 526 | exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>" |
| 527 | call assert_equal("n-niV", g:current_modes) |
| 528 | |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 529 | " How to test operator-pending mode? |
| 530 | |
| 531 | call feedkeys("v", 'xt') |
| 532 | call assert_equal('v', mode()) |
| 533 | call assert_equal('v', mode(1)) |
| 534 | call feedkeys("\<Esc>V", 'xt') |
| 535 | call assert_equal('V', mode()) |
| 536 | call assert_equal('V', mode(1)) |
| 537 | call feedkeys("\<Esc>\<C-V>", 'xt') |
| 538 | call assert_equal("\<C-V>", mode()) |
| 539 | call assert_equal("\<C-V>", mode(1)) |
| 540 | call feedkeys("\<Esc>", 'xt') |
| 541 | |
| 542 | call feedkeys("gh", 'xt') |
| 543 | call assert_equal('s', mode()) |
| 544 | call assert_equal('s', mode(1)) |
| 545 | call feedkeys("\<Esc>gH", 'xt') |
| 546 | call assert_equal('S', mode()) |
| 547 | call assert_equal('S', mode(1)) |
| 548 | call feedkeys("\<Esc>g\<C-H>", 'xt') |
| 549 | call assert_equal("\<C-S>", mode()) |
| 550 | call assert_equal("\<C-S>", mode(1)) |
| 551 | call feedkeys("\<Esc>", 'xt') |
| 552 | |
| 553 | call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt') |
| 554 | call assert_equal('c-c', g:current_modes) |
| 555 | call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt') |
| 556 | call assert_equal('c-cv', g:current_modes) |
| 557 | " How to test Ex mode? |
| 558 | |
| 559 | bwipe! |
| 560 | iunmap <F2> |
Bram Moolenaar | ffea8c9 | 2017-03-13 20:37:15 +0100 | [diff] [blame] | 561 | set complete& |
Bram Moolenaar | e90858d | 2017-02-01 17:24:34 +0100 | [diff] [blame] | 562 | endfunc |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 563 | |
| 564 | func Test_getbufvar() |
| 565 | let bnr = bufnr('%') |
| 566 | let b:var_num = '1234' |
| 567 | let def_num = '5678' |
| 568 | call assert_equal('1234', getbufvar(bnr, 'var_num')) |
| 569 | call assert_equal('1234', getbufvar(bnr, 'var_num', def_num)) |
| 570 | |
| 571 | let bd = getbufvar(bnr, '') |
| 572 | call assert_equal('1234', bd['var_num']) |
| 573 | call assert_true(exists("bd['changedtick']")) |
| 574 | call assert_equal(2, len(bd)) |
| 575 | |
| 576 | let bd2 = getbufvar(bnr, '', def_num) |
| 577 | call assert_equal(bd, bd2) |
| 578 | |
| 579 | unlet b:var_num |
| 580 | call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num)) |
| 581 | call assert_equal('', getbufvar(bnr, 'var_num')) |
| 582 | |
| 583 | let bd = getbufvar(bnr, '') |
| 584 | call assert_equal(1, len(bd)) |
| 585 | let bd = getbufvar(bnr, '',def_num) |
| 586 | call assert_equal(1, len(bd)) |
| 587 | |
Bram Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 588 | call assert_equal('', getbufvar(9999, '')) |
| 589 | call assert_equal(def_num, getbufvar(9999, '', def_num)) |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 590 | unlet def_num |
| 591 | |
Bram Moolenaar | 507647d | 2017-02-17 16:43:49 +0100 | [diff] [blame] | 592 | call assert_equal(0, getbufvar(bnr, '&autoindent')) |
| 593 | call assert_equal(0, getbufvar(bnr, '&autoindent', 1)) |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 594 | |
| 595 | " Open new window with forced option values |
| 596 | set fileformats=unix,dos |
| 597 | new ++ff=dos ++bin ++enc=iso-8859-2 |
| 598 | call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat')) |
| 599 | call assert_equal(1, getbufvar(bufnr('%'), '&bin')) |
| 600 | call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc')) |
| 601 | close |
| 602 | |
| 603 | set fileformats& |
| 604 | endfunc |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 605 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 606 | func Test_last_buffer_nr() |
| 607 | call assert_equal(bufnr('$'), last_buffer_nr()) |
| 608 | endfunc |
| 609 | |
| 610 | func Test_stridx() |
| 611 | call assert_equal(-1, stridx('', 'l')) |
| 612 | call assert_equal(0, stridx('', '')) |
| 613 | call assert_equal(0, stridx('hello', '')) |
| 614 | call assert_equal(-1, stridx('hello', 'L')) |
| 615 | call assert_equal(2, stridx('hello', 'l', -1)) |
| 616 | call assert_equal(2, stridx('hello', 'l', 0)) |
| 617 | call assert_equal(2, stridx('hello', 'l', 1)) |
| 618 | call assert_equal(3, stridx('hello', 'l', 3)) |
| 619 | call assert_equal(-1, stridx('hello', 'l', 4)) |
| 620 | call assert_equal(-1, stridx('hello', 'l', 10)) |
| 621 | call assert_equal(2, stridx('hello', 'll')) |
| 622 | call assert_equal(-1, stridx('hello', 'hello world')) |
| 623 | endfunc |
| 624 | |
| 625 | func Test_strridx() |
| 626 | call assert_equal(-1, strridx('', 'l')) |
| 627 | call assert_equal(0, strridx('', '')) |
| 628 | call assert_equal(5, strridx('hello', '')) |
| 629 | call assert_equal(-1, strridx('hello', 'L')) |
| 630 | call assert_equal(3, strridx('hello', 'l')) |
| 631 | call assert_equal(3, strridx('hello', 'l', 10)) |
| 632 | call assert_equal(3, strridx('hello', 'l', 3)) |
| 633 | call assert_equal(2, strridx('hello', 'l', 2)) |
| 634 | call assert_equal(-1, strridx('hello', 'l', 1)) |
| 635 | call assert_equal(-1, strridx('hello', 'l', 0)) |
| 636 | call assert_equal(-1, strridx('hello', 'l', -1)) |
| 637 | call assert_equal(2, strridx('hello', 'll')) |
| 638 | call assert_equal(-1, strridx('hello', 'hello world')) |
| 639 | endfunc |
| 640 | |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 641 | func Test_match_func() |
| 642 | call assert_equal(4, match('testing', 'ing')) |
| 643 | call assert_equal(4, match('testing', 'ing', 2)) |
| 644 | call assert_equal(-1, match('testing', 'ing', 5)) |
| 645 | call assert_equal(-1, match('testing', 'ing', 8)) |
| 646 | call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing')) |
| 647 | call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img')) |
| 648 | endfunc |
| 649 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 650 | func Test_matchend() |
| 651 | call assert_equal(7, matchend('testing', 'ing')) |
| 652 | call assert_equal(7, matchend('testing', 'ing', 2)) |
| 653 | call assert_equal(-1, matchend('testing', 'ing', 5)) |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 654 | call assert_equal(-1, matchend('testing', 'ing', 8)) |
| 655 | call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing')) |
| 656 | call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img')) |
| 657 | endfunc |
| 658 | |
| 659 | func Test_matchlist() |
| 660 | call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)')) |
| 661 | call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2)) |
| 662 | call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4)) |
| 663 | endfunc |
| 664 | |
| 665 | func Test_matchstr() |
| 666 | call assert_equal('ing', matchstr('testing', 'ing')) |
| 667 | call assert_equal('ing', matchstr('testing', 'ing', 2)) |
| 668 | call assert_equal('', matchstr('testing', 'ing', 5)) |
| 669 | call assert_equal('', matchstr('testing', 'ing', 8)) |
| 670 | call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing')) |
| 671 | call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img')) |
| 672 | endfunc |
| 673 | |
| 674 | func Test_matchstrpos() |
| 675 | call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing')) |
| 676 | call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2)) |
| 677 | call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5)) |
| 678 | call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8)) |
| 679 | call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing')) |
| 680 | call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img')) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 681 | endfunc |
| 682 | |
| 683 | func Test_nextnonblank_prevnonblank() |
| 684 | new |
| 685 | insert |
| 686 | This |
| 687 | |
| 688 | |
| 689 | is |
| 690 | |
| 691 | a |
| 692 | Test |
| 693 | . |
| 694 | call assert_equal(0, nextnonblank(-1)) |
| 695 | call assert_equal(0, nextnonblank(0)) |
| 696 | call assert_equal(1, nextnonblank(1)) |
| 697 | call assert_equal(4, nextnonblank(2)) |
| 698 | call assert_equal(4, nextnonblank(3)) |
| 699 | call assert_equal(4, nextnonblank(4)) |
| 700 | call assert_equal(6, nextnonblank(5)) |
| 701 | call assert_equal(6, nextnonblank(6)) |
| 702 | call assert_equal(7, nextnonblank(7)) |
| 703 | call assert_equal(0, nextnonblank(8)) |
| 704 | |
| 705 | call assert_equal(0, prevnonblank(-1)) |
| 706 | call assert_equal(0, prevnonblank(0)) |
| 707 | call assert_equal(1, prevnonblank(1)) |
| 708 | call assert_equal(1, prevnonblank(2)) |
| 709 | call assert_equal(1, prevnonblank(3)) |
| 710 | call assert_equal(4, prevnonblank(4)) |
| 711 | call assert_equal(4, prevnonblank(5)) |
| 712 | call assert_equal(6, prevnonblank(6)) |
| 713 | call assert_equal(7, prevnonblank(7)) |
| 714 | call assert_equal(0, prevnonblank(8)) |
| 715 | bw! |
| 716 | endfunc |
| 717 | |
| 718 | func Test_byte2line_line2byte() |
| 719 | new |
Bram Moolenaar | c26f7c6 | 2018-08-20 22:53:04 +0200 | [diff] [blame] | 720 | set endofline |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 721 | call setline(1, ['a', 'bc', 'd']) |
| 722 | |
| 723 | set fileformat=unix |
| 724 | call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1], |
| 725 | \ map(range(-1, 8), 'byte2line(v:val)')) |
| 726 | call assert_equal([-1, -1, 1, 3, 6, 8, -1], |
| 727 | \ map(range(-1, 5), 'line2byte(v:val)')) |
| 728 | |
| 729 | set fileformat=mac |
| 730 | call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1], |
| 731 | \ map(range(-1, 8), 'byte2line(v:val)')) |
| 732 | call assert_equal([-1, -1, 1, 3, 6, 8, -1], |
| 733 | \ map(range(-1, 5), 'line2byte(v:val)')) |
| 734 | |
| 735 | set fileformat=dos |
| 736 | call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1], |
| 737 | \ map(range(-1, 11), 'byte2line(v:val)')) |
| 738 | call assert_equal([-1, -1, 1, 4, 8, 11, -1], |
| 739 | \ map(range(-1, 5), 'line2byte(v:val)')) |
| 740 | |
Bram Moolenaar | c26f7c6 | 2018-08-20 22:53:04 +0200 | [diff] [blame] | 741 | bw! |
| 742 | set noendofline nofixendofline |
| 743 | normal a- |
| 744 | for ff in ["unix", "mac", "dos"] |
| 745 | let &fileformat = ff |
| 746 | call assert_equal(1, line2byte(1)) |
| 747 | call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte). |
| 748 | endfor |
| 749 | |
| 750 | set endofline& fixendofline& fileformat& |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 751 | bw! |
| 752 | endfunc |
| 753 | |
| 754 | func Test_count() |
| 755 | let l = ['a', 'a', 'A', 'b'] |
| 756 | call assert_equal(2, count(l, 'a')) |
| 757 | call assert_equal(1, count(l, 'A')) |
| 758 | call assert_equal(1, count(l, 'b')) |
| 759 | call assert_equal(0, count(l, 'B')) |
| 760 | |
| 761 | call assert_equal(2, count(l, 'a', 0)) |
| 762 | call assert_equal(1, count(l, 'A', 0)) |
| 763 | call assert_equal(1, count(l, 'b', 0)) |
| 764 | call assert_equal(0, count(l, 'B', 0)) |
| 765 | |
| 766 | call assert_equal(3, count(l, 'a', 1)) |
| 767 | call assert_equal(3, count(l, 'A', 1)) |
| 768 | call assert_equal(1, count(l, 'b', 1)) |
| 769 | call assert_equal(1, count(l, 'B', 1)) |
| 770 | call assert_equal(0, count(l, 'c', 1)) |
| 771 | |
| 772 | call assert_equal(1, count(l, 'a', 0, 1)) |
| 773 | call assert_equal(2, count(l, 'a', 1, 1)) |
| 774 | call assert_fails('call count(l, "a", 0, 10)', 'E684:') |
| 775 | |
| 776 | let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'} |
| 777 | call assert_equal(2, count(d, 'a')) |
| 778 | call assert_equal(1, count(d, 'A')) |
| 779 | call assert_equal(1, count(d, 'b')) |
| 780 | call assert_equal(0, count(d, 'B')) |
| 781 | |
| 782 | call assert_equal(2, count(d, 'a', 0)) |
| 783 | call assert_equal(1, count(d, 'A', 0)) |
| 784 | call assert_equal(1, count(d, 'b', 0)) |
| 785 | call assert_equal(0, count(d, 'B', 0)) |
| 786 | |
| 787 | call assert_equal(3, count(d, 'a', 1)) |
| 788 | call assert_equal(3, count(d, 'A', 1)) |
| 789 | call assert_equal(1, count(d, 'b', 1)) |
| 790 | call assert_equal(1, count(d, 'B', 1)) |
| 791 | call assert_equal(0, count(d, 'c', 1)) |
| 792 | |
| 793 | call assert_fails('call count(d, "a", 0, 1)', 'E474:') |
Bram Moolenaar | 9966b21 | 2017-07-28 16:46:57 +0200 | [diff] [blame] | 794 | |
| 795 | call assert_equal(0, count("foo", "bar")) |
| 796 | call assert_equal(1, count("foo", "oo")) |
| 797 | call assert_equal(2, count("foo", "o")) |
| 798 | call assert_equal(0, count("foo", "O")) |
| 799 | call assert_equal(2, count("foo", "O", 1)) |
| 800 | call assert_equal(2, count("fooooo", "oo")) |
Bram Moolenaar | 338e47f | 2017-12-19 11:55:26 +0100 | [diff] [blame] | 801 | call assert_equal(0, count("foo", "")) |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 802 | endfunc |
| 803 | |
| 804 | func Test_changenr() |
| 805 | new Xchangenr |
| 806 | call assert_equal(0, changenr()) |
| 807 | norm ifoo |
| 808 | call assert_equal(1, changenr()) |
| 809 | set undolevels=10 |
| 810 | norm Sbar |
| 811 | call assert_equal(2, changenr()) |
| 812 | undo |
| 813 | call assert_equal(1, changenr()) |
| 814 | redo |
| 815 | call assert_equal(2, changenr()) |
| 816 | bw! |
| 817 | set undolevels& |
| 818 | endfunc |
| 819 | |
| 820 | func Test_filewritable() |
| 821 | new Xfilewritable |
| 822 | write! |
| 823 | call assert_equal(1, filewritable('Xfilewritable')) |
| 824 | |
| 825 | call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----')) |
| 826 | call assert_equal(0, filewritable('Xfilewritable')) |
| 827 | |
| 828 | call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----')) |
| 829 | call assert_equal(1, filewritable('Xfilewritable')) |
| 830 | |
| 831 | call assert_equal(0, filewritable('doesnotexist')) |
| 832 | |
| 833 | call delete('Xfilewritable') |
| 834 | bw! |
| 835 | endfunc |
| 836 | |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 837 | func Test_Executable() |
| 838 | if has('win32') |
| 839 | call assert_equal(1, executable('notepad')) |
| 840 | call assert_equal(1, executable('notepad.exe')) |
| 841 | call assert_equal(0, executable('notepad.exe.exe')) |
| 842 | call assert_equal(0, executable('shell32.dll')) |
| 843 | call assert_equal(0, executable('win.ini')) |
| 844 | elseif has('unix') |
| 845 | call assert_equal(1, executable('cat')) |
Bram Moolenaar | a05a0d3 | 2018-10-07 18:43:05 +0200 | [diff] [blame] | 846 | call assert_equal(0, executable('nodogshere')) |
Bram Moolenaar | 8295666 | 2018-10-06 15:18:45 +0200 | [diff] [blame] | 847 | endif |
| 848 | endfunc |
| 849 | |
Bram Moolenaar | 41042f3 | 2017-03-09 12:09:32 +0100 | [diff] [blame] | 850 | func Test_hostname() |
| 851 | let hostname_vim = hostname() |
| 852 | if has('unix') |
| 853 | let hostname_system = systemlist('uname -n')[0] |
| 854 | call assert_equal(hostname_vim, hostname_system) |
| 855 | endif |
| 856 | endfunc |
| 857 | |
| 858 | func Test_getpid() |
| 859 | " getpid() always returns the same value within a vim instance. |
| 860 | call assert_equal(getpid(), getpid()) |
| 861 | if has('unix') |
| 862 | call assert_equal(systemlist('echo $PPID')[0], string(getpid())) |
| 863 | endif |
| 864 | endfunc |
| 865 | |
| 866 | func Test_hlexists() |
| 867 | call assert_equal(0, hlexists('does_not_exist')) |
| 868 | call assert_equal(0, hlexists('Number')) |
| 869 | call assert_equal(0, highlight_exists('does_not_exist')) |
| 870 | call assert_equal(0, highlight_exists('Number')) |
| 871 | syntax on |
| 872 | call assert_equal(0, hlexists('does_not_exist')) |
| 873 | call assert_equal(1, hlexists('Number')) |
| 874 | call assert_equal(0, highlight_exists('does_not_exist')) |
| 875 | call assert_equal(1, highlight_exists('Number')) |
| 876 | syntax off |
| 877 | endfunc |
| 878 | |
| 879 | func Test_col() |
| 880 | new |
| 881 | call setline(1, 'abcdef') |
| 882 | norm gg4|mx6|mY2| |
| 883 | call assert_equal(2, col('.')) |
| 884 | call assert_equal(7, col('$')) |
| 885 | call assert_equal(4, col("'x")) |
| 886 | call assert_equal(6, col("'Y")) |
| 887 | call assert_equal(2, col([1, 2])) |
| 888 | call assert_equal(7, col([1, '$'])) |
| 889 | |
| 890 | call assert_equal(0, col('')) |
| 891 | call assert_equal(0, col('x')) |
| 892 | call assert_equal(0, col([2, '$'])) |
| 893 | call assert_equal(0, col([1, 100])) |
| 894 | call assert_equal(0, col([1])) |
| 895 | bw! |
| 896 | endfunc |
| 897 | |
Bram Moolenaar | 947b39e | 2018-07-22 19:36:37 +0200 | [diff] [blame] | 898 | func Test_inputlist() |
| 899 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx') |
| 900 | call assert_equal(1, c) |
| 901 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>2\<cr>", 'tx') |
| 902 | call assert_equal(2, c) |
| 903 | call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx') |
| 904 | call assert_equal(3, c) |
| 905 | |
| 906 | call assert_fails('call inputlist("")', 'E686:') |
| 907 | endfunc |
| 908 | |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 909 | func Test_balloon_show() |
Bram Moolenaar | a0107bd | 2017-03-02 22:48:01 +0100 | [diff] [blame] | 910 | if has('balloon_eval') |
| 911 | " This won't do anything but must not crash either. |
| 912 | call balloon_show('hi!') |
| 913 | endif |
Bram Moolenaar | caf6434 | 2017-03-02 22:11:33 +0100 | [diff] [blame] | 914 | endfunc |
Bram Moolenaar | 2c90d51 | 2017-03-18 22:35:30 +0100 | [diff] [blame] | 915 | |
| 916 | func Test_setbufvar_options() |
| 917 | " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the |
| 918 | " window layout. |
| 919 | call assert_equal(1, winnr('$')) |
| 920 | split dummy_preview |
| 921 | resize 2 |
| 922 | set winfixheight winfixwidth |
| 923 | let prev_id = win_getid() |
| 924 | |
| 925 | wincmd j |
| 926 | let wh = winheight('.') |
| 927 | let dummy_buf = bufnr('dummy_buf1', v:true) |
| 928 | call setbufvar(dummy_buf, '&buftype', 'nofile') |
| 929 | execute 'belowright vertical split #' . dummy_buf |
| 930 | call assert_equal(wh, winheight('.')) |
| 931 | let dum1_id = win_getid() |
| 932 | |
| 933 | wincmd h |
| 934 | let wh = winheight('.') |
| 935 | let dummy_buf = bufnr('dummy_buf2', v:true) |
| 936 | call setbufvar(dummy_buf, '&buftype', 'nofile') |
| 937 | execute 'belowright vertical split #' . dummy_buf |
| 938 | call assert_equal(wh, winheight('.')) |
| 939 | |
| 940 | bwipe! |
| 941 | call win_gotoid(prev_id) |
| 942 | bwipe! |
| 943 | call win_gotoid(dum1_id) |
| 944 | bwipe! |
| 945 | endfunc |
Bram Moolenaar | d4863aa | 2017-04-07 19:50:12 +0200 | [diff] [blame] | 946 | |
| 947 | func Test_redo_in_nested_functions() |
| 948 | nnoremap g. :set opfunc=Operator<CR>g@ |
| 949 | function Operator( type, ... ) |
| 950 | let @x = 'XXX' |
| 951 | execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp' |
| 952 | endfunction |
| 953 | |
| 954 | function! Apply() |
| 955 | 5,6normal! . |
| 956 | endfunction |
| 957 | |
| 958 | new |
| 959 | call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3)) |
| 960 | 1normal g.i" |
| 961 | call assert_equal('some "XXX" text', getline(1)) |
| 962 | 3,4normal . |
| 963 | call assert_equal('some "XXX" text', getline(3)) |
| 964 | call assert_equal('more "XXX" text', getline(4)) |
| 965 | call Apply() |
| 966 | call assert_equal('some "XXX" text', getline(5)) |
| 967 | call assert_equal('more "XXX" text', getline(6)) |
| 968 | bwipe! |
| 969 | |
| 970 | nunmap g. |
| 971 | delfunc Operator |
| 972 | delfunc Apply |
| 973 | endfunc |
Bram Moolenaar | 2061552 | 2017-06-05 18:46:26 +0200 | [diff] [blame] | 974 | |
| 975 | func Test_shellescape() |
| 976 | let save_shell = &shell |
| 977 | set shell=bash |
| 978 | call assert_equal("'text'", shellescape('text')) |
| 979 | call assert_equal("'te\"xt'", shellescape('te"xt')) |
| 980 | call assert_equal("'te'\\''xt'", shellescape("te'xt")) |
| 981 | |
| 982 | call assert_equal("'te%xt'", shellescape("te%xt")) |
| 983 | call assert_equal("'te\\%xt'", shellescape("te%xt", 1)) |
| 984 | call assert_equal("'te#xt'", shellescape("te#xt")) |
| 985 | call assert_equal("'te\\#xt'", shellescape("te#xt", 1)) |
| 986 | call assert_equal("'te!xt'", shellescape("te!xt")) |
| 987 | call assert_equal("'te\\!xt'", shellescape("te!xt", 1)) |
| 988 | |
| 989 | call assert_equal("'te\nxt'", shellescape("te\nxt")) |
| 990 | call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1)) |
| 991 | set shell=tcsh |
| 992 | call assert_equal("'te\\!xt'", shellescape("te!xt")) |
| 993 | call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1)) |
| 994 | call assert_equal("'te\\\nxt'", shellescape("te\nxt")) |
| 995 | call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1)) |
| 996 | |
| 997 | let &shell = save_shell |
| 998 | endfunc |
Bram Moolenaar | 295ac5a | 2018-03-22 23:04:02 +0100 | [diff] [blame] | 999 | |
| 1000 | func Test_trim() |
| 1001 | call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B")) |
| 1002 | call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B")) |
| 1003 | call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t")) |
| 1004 | call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww")) |
| 1005 | call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail")) |
| 1006 | call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " ")) |
| 1007 | call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx")) |
| 1008 | call assert_equal("RESERVE", trim("你RESERVE好", "你好")) |
| 1009 | call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好")) |
| 1010 | call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", )) |
| 1011 | call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好")) |
| 1012 | call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes")) |
| 1013 | call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses")) |
| 1014 | call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要")) |
| 1015 | call assert_equal("", trim("", "")) |
| 1016 | call assert_equal("a", trim("a", "")) |
| 1017 | call assert_equal("", trim("", "a")) |
| 1018 | |
| 1019 | let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '') |
| 1020 | call assert_equal("x", trim(chars . "x" . chars)) |
| 1021 | endfunc |
Bram Moolenaar | 0b6d911 | 2018-05-22 20:35:17 +0200 | [diff] [blame] | 1022 | |
| 1023 | " Test for reg_recording() and reg_executing() |
| 1024 | func Test_reg_executing_and_recording() |
| 1025 | let s:reg_stat = '' |
| 1026 | func s:save_reg_stat() |
| 1027 | let s:reg_stat = reg_recording() . ':' . reg_executing() |
| 1028 | return '' |
| 1029 | endfunc |
| 1030 | |
| 1031 | new |
| 1032 | call s:save_reg_stat() |
| 1033 | call assert_equal(':', s:reg_stat) |
| 1034 | call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt') |
| 1035 | call assert_equal('a:', s:reg_stat) |
| 1036 | call feedkeys("@a", 'xt') |
| 1037 | call assert_equal(':a', s:reg_stat) |
| 1038 | call feedkeys("qb@aq", 'xt') |
| 1039 | call assert_equal('b:a', s:reg_stat) |
| 1040 | call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt') |
| 1041 | call assert_equal('":', s:reg_stat) |
| 1042 | |
| 1043 | bwipe! |
| 1044 | delfunc s:save_reg_stat |
| 1045 | unlet s:reg_stat |
| 1046 | endfunc |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1047 | |
| 1048 | func Test_libcall_libcallnr() |
| 1049 | if !has('libcall') |
| 1050 | return |
| 1051 | endif |
| 1052 | |
| 1053 | if has('win32') |
| 1054 | let libc = 'msvcrt.dll' |
| 1055 | elseif has('mac') |
| 1056 | let libc = 'libSystem.B.dylib' |
Bram Moolenaar | f1c118b | 2018-09-03 22:08:10 +0200 | [diff] [blame] | 1057 | elseif system('uname -s') =~ 'SunOS' |
| 1058 | " Set the path to libc.so according to the architecture. |
| 1059 | let test_bits = system('file ' . GetVimProg()) |
| 1060 | let test_arch = system('uname -p') |
| 1061 | if test_bits =~ '64-bit' && test_arch =~ 'sparc' |
| 1062 | let libc = '/usr/lib/sparcv9/libc.so' |
| 1063 | elseif test_bits =~ '64-bit' && test_arch =~ 'i386' |
| 1064 | let libc = '/usr/lib/amd64/libc.so' |
| 1065 | else |
| 1066 | let libc = '/usr/lib/libc.so' |
| 1067 | endif |
Bram Moolenaar | 1ceebb4 | 2018-06-19 19:46:06 +0200 | [diff] [blame] | 1068 | else |
| 1069 | " On Unix, libc.so can be in various places. |
| 1070 | " Interestingly, using an empty string for the 1st argument of libcall |
| 1071 | " allows to call functions from libc which is not documented. |
| 1072 | let libc = '' |
| 1073 | endif |
| 1074 | |
| 1075 | if has('win32') |
| 1076 | call assert_equal($USERPROFILE, libcall(libc, 'getenv', 'USERPROFILE')) |
| 1077 | else |
| 1078 | call assert_equal($HOME, libcall(libc, 'getenv', 'HOME')) |
| 1079 | endif |
| 1080 | |
| 1081 | " If function returns NULL, libcall() should return an empty string. |
| 1082 | call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT')) |
| 1083 | |
| 1084 | " Test libcallnr() with string and integer argument. |
| 1085 | call assert_equal(4, libcallnr(libc, 'strlen', 'abcd')) |
| 1086 | call assert_equal(char2nr('A'), libcallnr(libc, 'toupper', char2nr('a'))) |
| 1087 | |
| 1088 | call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:') |
| 1089 | call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:') |
| 1090 | |
| 1091 | call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:') |
| 1092 | call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:') |
| 1093 | endfunc |
Bram Moolenaar | d90a144 | 2018-07-15 20:24:31 +0200 | [diff] [blame] | 1094 | |
| 1095 | sandbox function Fsandbox() |
| 1096 | normal ix |
| 1097 | endfunc |
| 1098 | |
| 1099 | func Test_func_sandbox() |
| 1100 | sandbox let F = {-> 'hello'} |
| 1101 | call assert_equal('hello', F()) |
| 1102 | |
| 1103 | sandbox let F = {-> execute("normal ix\<Esc>")} |
| 1104 | call assert_fails('call F()', 'E48:') |
| 1105 | unlet F |
| 1106 | |
| 1107 | call assert_fails('call Fsandbox()', 'E48:') |
| 1108 | delfunc Fsandbox |
| 1109 | endfunc |
Bram Moolenaar | 9e353b5 | 2018-11-04 23:39:38 +0100 | [diff] [blame] | 1110 | |
| 1111 | func EditAnotherFile() |
| 1112 | let word = expand('<cword>') |
| 1113 | edit Xfuncrange2 |
| 1114 | endfunc |
| 1115 | |
| 1116 | func Test_func_range_with_edit() |
| 1117 | " Define a function that edits another buffer, then call it with a range that |
| 1118 | " is invalid in that buffer. |
| 1119 | call writefile(['just one line'], 'Xfuncrange2') |
| 1120 | new |
| 1121 | call setline(1, range(10)) |
| 1122 | write Xfuncrange1 |
| 1123 | call assert_fails('5,8call EditAnotherFile()', 'E16:') |
| 1124 | |
| 1125 | call delete('Xfuncrange1') |
| 1126 | call delete('Xfuncrange2') |
| 1127 | bwipe! |
| 1128 | endfunc |
Bram Moolenaar | ded5f1b | 2018-11-10 17:33:29 +0100 | [diff] [blame] | 1129 | |
| 1130 | func Test_func_exists_on_reload() |
| 1131 | call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists') |
| 1132 | call assert_equal(0, exists('*ExistingFunction')) |
| 1133 | source Xfuncexists |
| 1134 | call assert_equal(1, exists('*ExistingFunction')) |
| 1135 | " Redefining a function when reloading a script is OK. |
| 1136 | source Xfuncexists |
| 1137 | call assert_equal(1, exists('*ExistingFunction')) |
| 1138 | |
| 1139 | " But redefining in another script is not OK. |
| 1140 | call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2') |
| 1141 | call assert_fails('source Xfuncexists2', 'E122:') |
| 1142 | |
| 1143 | delfunc ExistingFunction |
| 1144 | call assert_equal(0, exists('*ExistingFunction')) |
| 1145 | call writefile([ |
| 1146 | \ 'func ExistingFunction()', 'echo "yes"', 'endfunc', |
| 1147 | \ 'func ExistingFunction()', 'echo "no"', 'endfunc', |
| 1148 | \ ], 'Xfuncexists') |
| 1149 | call assert_fails('source Xfuncexists', 'E122:') |
| 1150 | call assert_equal(1, exists('*ExistingFunction')) |
| 1151 | |
| 1152 | call delete('Xfuncexists2') |
| 1153 | call delete('Xfuncexists') |
| 1154 | delfunc ExistingFunction |
| 1155 | endfunc |