test_expr.vim (37963B)
1 " Tests for expressions. 2 3 source check.vim 4 source vim9.vim 5 6 func Test_equal() 7 let base = {} 8 func base.method() 9 return 1 10 endfunc 11 func base.other() dict 12 return 1 13 endfunc 14 let instance = copy(base) 15 call assert_true(base.method == instance.method) 16 call assert_true([base.method] == [instance.method]) 17 call assert_true(base.other == instance.other) 18 call assert_true([base.other] == [instance.other]) 19 20 call assert_false(base.method == base.other) 21 call assert_false([base.method] == [base.other]) 22 call assert_false(base.method == instance.other) 23 call assert_false([base.method] == [instance.other]) 24 25 call assert_fails('echo base.method > instance.method', 'E694: Invalid operation for Funcrefs') 26 " Nvim doesn't have null functions 27 " call assert_equal(0, test_null_function() == function('min')) 28 " call assert_equal(1, test_null_function() == test_null_function()) 29 " Nvim doesn't have test_unknown() 30 " call assert_fails('eval 10 == test_unknown()', 'E685:') 31 endfunc 32 33 func Test_version() 34 call assert_true(has('patch-7.4.001')) 35 call assert_true(has('patch-7.4.01')) 36 call assert_true(has('patch-7.4.1')) 37 call assert_true(has('patch-6.9.999')) 38 call assert_true(has('patch-7.1.999')) 39 call assert_true(has('patch-7.4.123')) 40 call assert_true(has('patch-7.4.123 ')) " Traling space can be allowed. 41 42 call assert_false(has('patch-7')) 43 call assert_false(has('patch-7.4')) 44 call assert_false(has('patch-7.4.')) 45 call assert_false(has('patch-9.1.0')) 46 call assert_false(has('patch-9.9.1')) 47 48 call assert_false(has('patch-abc')) 49 call assert_false(has('patchabc')) 50 51 call assert_false(has('patch-8x001')) 52 call assert_false(has('patch-9X0X0')) 53 call assert_false(has('patch-9-0-0')) 54 call assert_false(has('patch-09.0.0')) 55 call assert_false(has('patch-9.00.0')) 56 endfunc 57 58 func Test_op_ternary() 59 let lines =<< trim END 60 call assert_equal('yes', 1 ? 'yes' : 'no') 61 call assert_equal('no', 0 ? 'yes' : 'no') 62 63 call assert_fails('echo [1] ? "yes" : "no"', 'E745:') 64 call assert_fails('echo {} ? "yes" : "no"', 'E728:') 65 END 66 call CheckLegacyAndVim9Success(lines) 67 68 call assert_equal('no', 'x' ? 'yes' : 'no') 69 call CheckDefAndScriptFailure(["'x' ? 'yes' : 'no'"], 'E1135:') 70 call assert_equal('yes', '1x' ? 'yes' : 'no') 71 call CheckDefAndScriptFailure(["'1x' ? 'yes' : 'no'"], 'E1135:') 72 endfunc 73 74 func Test_op_falsy() 75 let lines =<< trim END 76 call assert_equal(v:true, v:true ?? 456) 77 call assert_equal(123, 123 ?? 456) 78 call assert_equal('yes', 'yes' ?? 456) 79 call assert_equal(0z00, 0z00 ?? 456) 80 call assert_equal([1], [1] ?? 456) 81 call assert_equal({'one': 1}, {'one': 1} ?? 456) 82 call assert_equal(0.1, 0.1 ?? 456) 83 84 call assert_equal(456, v:false ?? 456) 85 call assert_equal(456, 0 ?? 456) 86 call assert_equal(456, '' ?? 456) 87 call assert_equal(456, 0z ?? 456) 88 call assert_equal(456, [] ?? 456) 89 call assert_equal(456, {} ?? 456) 90 call assert_equal(456, 0.0 ?? 456) 91 92 call assert_equal(456, v:null ?? 456) 93 #" call assert_equal(456, v:none ?? 456) 94 call assert_equal(456, v:_null_string ?? 456) 95 call assert_equal(456, v:_null_blob ?? 456) 96 call assert_equal(456, v:_null_list ?? 456) 97 call assert_equal(456, v:_null_dict ?? 456) 98 #" Nvim doesn't have null functions 99 #" call assert_equal(456, test_null_function() ?? 456) 100 #" Nvim doesn't have null partials 101 #" call assert_equal(456, test_null_partial() ?? 456) 102 if has('job') 103 call assert_equal(456, test_null_job() ?? 456) 104 endif 105 if has('channel') 106 call assert_equal(456, test_null_channel() ?? 456) 107 endif 108 END 109 call CheckLegacyAndVim9Success(lines) 110 endfunc 111 112 func Test_dict() 113 let lines =<< trim END 114 VAR d = {'': 'empty', 'a': 'a', 0: 'zero'} 115 call assert_equal('empty', d['']) 116 call assert_equal('a', d['a']) 117 call assert_equal('zero', d[0]) 118 call assert_true(has_key(d, '')) 119 call assert_true(has_key(d, 'a')) 120 121 LET d[''] = 'none' 122 LET d['a'] = 'aaa' 123 call assert_equal('none', d['']) 124 call assert_equal('aaa', d['a']) 125 126 LET d[ 'b' ] = 'bbb' 127 call assert_equal('bbb', d[ 'b' ]) 128 END 129 call CheckLegacyAndVim9Success(lines) 130 131 call CheckLegacyAndVim9Failure(["VAR i = has_key([], 'a')"], ['E1206:', 'E1013:', 'E1206:']) 132 endfunc 133 134 func Test_strgetchar() 135 let lines =<< trim END 136 call assert_equal(char2nr('a'), strgetchar('axb', 0)) 137 call assert_equal(char2nr('x'), 'axb'->strgetchar(1)) 138 call assert_equal(char2nr('b'), strgetchar('axb', 2)) 139 140 call assert_equal(-1, strgetchar('axb', -1)) 141 call assert_equal(-1, strgetchar('axb', 3)) 142 call assert_equal(-1, strgetchar('', 0)) 143 END 144 call CheckLegacyAndVim9Success(lines) 145 146 call CheckLegacyAndVim9Failure(["VAR c = strgetchar([], 1)"], ['E730:', 'E1013:', 'E1174:']) 147 call CheckLegacyAndVim9Failure(["VAR c = strgetchar('axb', [])"], ['E745:', 'E1013:', 'E1210:']) 148 endfunc 149 150 func Test_strcharpart() 151 let lines =<< trim END 152 call assert_equal('a', strcharpart('axb', 0, 1)) 153 call assert_equal('x', 'axb'->strcharpart(1, 1)) 154 call assert_equal('b', strcharpart('axb', 2, 1)) 155 call assert_equal('xb', strcharpart('axb', 1)) 156 157 call assert_equal('', strcharpart('axb', 1, 0)) 158 call assert_equal('', strcharpart('axb', 1, -1)) 159 call assert_equal('', strcharpart('axb', -1, 1)) 160 call assert_equal('', strcharpart('axb', -2, 2)) 161 162 call assert_equal('a', strcharpart('axb', -1, 2)) 163 164 call assert_equal('edit', "editor"[-10 : 3]) 165 END 166 call CheckLegacyAndVim9Success(lines) 167 168 call assert_fails('call strcharpart("", 0, 0, {})', ['E728:', 'E728:']) 169 call assert_fails('call strcharpart("", 0, 0, -1)', ['E1023:', 'E1023:']) 170 endfunc 171 172 func Test_getreg_empty_list() 173 let lines =<< trim END 174 call assert_equal('', getreg('x')) 175 call assert_equal([], getreg('x', 1, 1)) 176 VAR x = getreg('x', 1, 1) 177 VAR y = x 178 call add(x, 'foo') 179 call assert_equal(['foo'], y) 180 END 181 call CheckLegacyAndVim9Success(lines) 182 183 call CheckLegacyAndVim9Failure(['call getreg([])'], ['E730:', 'E1013:', 'E1174:']) 184 endfunc 185 186 func Test_loop_over_null_list() 187 let lines =<< trim END 188 VAR null_list = v:_null_list 189 for i in null_list 190 call assert_report('should not get here') 191 endfor 192 END 193 call CheckLegacyAndVim9Success(lines) 194 endfunc 195 196 func Test_setreg_null_list() 197 let lines =<< trim END 198 call setreg('x', v:_null_list) 199 END 200 call CheckLegacyAndVim9Success(lines) 201 endfunc 202 203 func Test_special_char() 204 " The failure is only visible using valgrind. 205 call CheckLegacyAndVim9Failure(['echo "\<C-">'], ['E15:', 'E1004:', 'E1004:']) 206 endfunc 207 208 func Test_method_with_prefix() 209 let lines =<< trim END 210 call assert_equal(TRUE, !range(5)->empty()) 211 call assert_equal(FALSE, !-3) 212 END 213 call CheckLegacyAndVim9Success(lines) 214 215 call assert_equal([0, 1, 2], --3->range()) 216 call CheckDefAndScriptFailure(['eval --3->range()'], 'E15:') 217 218 call assert_equal(1, !+-+0) 219 call CheckDefAndScriptFailure(['eval !+-+0'], 'E15:') 220 endfunc 221 222 func Test_option_value() 223 let lines =<< trim END 224 #" boolean 225 set bri 226 call assert_equal(TRUE, &bri) 227 set nobri 228 call assert_equal(FALSE, &bri) 229 230 #" number 231 set ts=1 232 call assert_equal(1, &ts) 233 set ts=8 234 call assert_equal(8, &ts) 235 236 #" string 237 exe "set cedit=\<Esc>" 238 call assert_equal("\<Esc>", &cedit) 239 set cpo= 240 call assert_equal("", &cpo) 241 set cpo=abcdefi 242 call assert_equal("abcdefi", &cpo) 243 set cpo&vim 244 END 245 call CheckLegacyAndVim9Success(lines) 246 endfunc 247 248 func Test_printf_misc() 249 let lines =<< trim END 250 call assert_equal('123', printf('123')) 251 252 call assert_equal('', printf('%')) 253 call assert_equal('', printf('%.0d', 0)) 254 call assert_equal('123', printf('%d', 123)) 255 call assert_equal('123', printf('%i', 123)) 256 call assert_equal('123', printf('%D', 123)) 257 call assert_equal('123', printf('%U', 123)) 258 call assert_equal('173', printf('%o', 123)) 259 call assert_equal('173', printf('%O', 123)) 260 call assert_equal('7b', printf('%x', 123)) 261 call assert_equal('7B', printf('%X', 123)) 262 263 call assert_equal('123', printf('%hd', 123)) 264 call assert_equal('-123', printf('%hd', -123)) 265 call assert_equal('-1', printf('%hd', 0xFFFF)) 266 call assert_equal('-1', printf('%hd', 0x1FFFFF)) 267 268 call assert_equal('123', printf('%hu', 123)) 269 call assert_equal('65413', printf('%hu', -123)) 270 call assert_equal('65535', printf('%hu', 0xFFFF)) 271 call assert_equal('65535', printf('%hu', 0x1FFFFF)) 272 273 call assert_equal('123', printf('%ld', 123)) 274 call assert_equal('-123', printf('%ld', -123)) 275 call assert_equal('65535', printf('%ld', 0xFFFF)) 276 call assert_equal('131071', printf('%ld', 0x1FFFF)) 277 278 call assert_equal('{', printf('%c', 123)) 279 call assert_equal('abc', printf('%s', 'abc')) 280 call assert_equal('abc', printf('%S', 'abc')) 281 282 call assert_equal('+123', printf('%+d', 123)) 283 call assert_equal('-123', printf('%+d', -123)) 284 call assert_equal('+123', printf('%+ d', 123)) 285 call assert_equal(' 123', printf('% d', 123)) 286 call assert_equal(' 123', printf('% d', 123)) 287 call assert_equal('-123', printf('% d', -123)) 288 289 call assert_equal('123', printf('%2d', 123)) 290 call assert_equal(' 123', printf('%6d', 123)) 291 call assert_equal('000123', printf('%06d', 123)) 292 call assert_equal('+00123', printf('%+06d', 123)) 293 call assert_equal(' 00123', printf('% 06d', 123)) 294 call assert_equal(' +123', printf('%+6d', 123)) 295 call assert_equal(' 123', printf('% 6d', 123)) 296 call assert_equal(' -123', printf('% 6d', -123)) 297 298 #" Test left adjusted. 299 call assert_equal('123 ', printf('%-6d', 123)) 300 call assert_equal('+123 ', printf('%-+6d', 123)) 301 call assert_equal(' 123 ', printf('%- 6d', 123)) 302 call assert_equal('-123 ', printf('%- 6d', -123)) 303 304 call assert_equal(' 00123', printf('%7.5d', 123)) 305 call assert_equal(' -00123', printf('%7.5d', -123)) 306 call assert_equal(' +00123', printf('%+7.5d', 123)) 307 308 #" Precision field should not be used when combined with %0 309 call assert_equal(' 00123', printf('%07.5d', 123)) 310 call assert_equal(' -00123', printf('%07.5d', -123)) 311 312 call assert_equal(' 123', printf('%*d', 5, 123)) 313 call assert_equal('123 ', printf('%*d', -5, 123)) 314 call assert_equal('00123', printf('%.*d', 5, 123)) 315 call assert_equal(' 123', printf('% *d', 5, 123)) 316 call assert_equal(' +123', printf('%+ *d', 5, 123)) 317 318 call assert_equal('foobar', printf('%.*s', 9, 'foobar')) 319 call assert_equal('foo', printf('%.*s', 3, 'foobar')) 320 call assert_equal('', printf('%.*s', 0, 'foobar')) 321 call assert_equal('foobar', printf('%.*s', -1, 'foobar')) 322 323 #" Simple quote (thousand grouping char) is ignored. 324 call assert_equal('+00123456', printf("%+'09d", 123456)) 325 326 #" Unrecognized format specifier kept as-is. 327 call assert_equal('_123', printf("%_%d", 123)) 328 329 #" Test alternate forms. 330 call assert_equal('0x7b', printf('%#x', 123)) 331 call assert_equal('0X7B', printf('%#X', 123)) 332 call assert_equal('0173', printf('%#o', 123)) 333 call assert_equal('0173', printf('%#O', 123)) 334 call assert_equal('abc', printf('%#s', 'abc')) 335 call assert_equal('abc', printf('%#S', 'abc')) 336 call assert_equal(' 0173', printf('%#6o', 123)) 337 call assert_equal(' 00173', printf('%#6.5o', 123)) 338 call assert_equal(' 0173', printf('%#6.2o', 123)) 339 call assert_equal(' 0173', printf('%#6.2o', 123)) 340 call assert_equal('0173', printf('%#2.2o', 123)) 341 342 call assert_equal(' 00123', printf('%6.5d', 123)) 343 call assert_equal(' 0007b', printf('%6.5x', 123)) 344 345 call assert_equal('123', printf('%.2d', 123)) 346 call assert_equal('0123', printf('%.4d', 123)) 347 call assert_equal('0000000123', printf('%.10d', 123)) 348 call assert_equal('123', printf('%.0d', 123)) 349 350 call assert_equal('abc', printf('%2s', 'abc')) 351 call assert_equal('abc', printf('%2S', 'abc')) 352 call assert_equal('abc', printf('%.4s', 'abc')) 353 call assert_equal('abc', printf('%.4S', 'abc')) 354 call assert_equal('ab', printf('%.2s', 'abc')) 355 call assert_equal('ab', printf('%.2S', 'abc')) 356 call assert_equal('', printf('%.0s', 'abc')) 357 call assert_equal('', printf('%.s', 'abc')) 358 call assert_equal(' abc', printf('%4s', 'abc')) 359 call assert_equal(' abc', printf('%4S', 'abc')) 360 call assert_equal('0abc', printf('%04s', 'abc')) 361 call assert_equal('0abc', printf('%04S', 'abc')) 362 call assert_equal('abc ', printf('%-4s', 'abc')) 363 call assert_equal('abc ', printf('%-4S', 'abc')) 364 365 call assert_equal('🐍', printf('%.2S', '🐍🐍')) 366 call assert_equal('', printf('%.1S', '🐍🐍')) 367 368 call assert_equal('[ あいう]', printf('[%10.6S]', 'あいうえお')) 369 call assert_equal('[ あいうえ]', printf('[%10.8S]', 'あいうえお')) 370 call assert_equal('[あいうえお]', printf('[%10.10S]', 'あいうえお')) 371 call assert_equal('[あいうえお]', printf('[%10.12S]', 'あいうえお')) 372 373 call assert_equal('あいう', printf('%S', 'あいう')) 374 call assert_equal('あいう', printf('%#S', 'あいう')) 375 376 call assert_equal('あb', printf('%2S', 'あb')) 377 call assert_equal('あb', printf('%.4S', 'あb')) 378 call assert_equal('あ', printf('%.2S', 'あb')) 379 call assert_equal(' あb', printf('%4S', 'あb')) 380 call assert_equal('0あb', printf('%04S', 'あb')) 381 call assert_equal('あb ', printf('%-4S', 'あb')) 382 call assert_equal('あ ', printf('%-4.2S', 'あb')) 383 384 call assert_equal('aい', printf('%2S', 'aい')) 385 call assert_equal('aい', printf('%.4S', 'aい')) 386 call assert_equal('a', printf('%.2S', 'aい')) 387 call assert_equal(' aい', printf('%4S', 'aい')) 388 call assert_equal('0aい', printf('%04S', 'aい')) 389 call assert_equal('aい ', printf('%-4S', 'aい')) 390 call assert_equal('a ', printf('%-4.2S', 'aい')) 391 392 call assert_equal('[あいう]', printf('[%05S]', 'あいう')) 393 call assert_equal('[あいう]', printf('[%06S]', 'あいう')) 394 call assert_equal('[0あいう]', printf('[%07S]', 'あいう')) 395 396 call assert_equal('[あiう]', printf('[%05S]', 'あiう')) 397 call assert_equal('[0あiう]', printf('[%06S]', 'あiう')) 398 call assert_equal('[00あiう]', printf('[%07S]', 'あiう')) 399 400 call assert_equal('[0あい]', printf('[%05.4S]', 'あいう')) 401 call assert_equal('[00あい]', printf('[%06.4S]', 'あいう')) 402 call assert_equal('[000あい]', printf('[%07.4S]', 'あいう')) 403 404 call assert_equal('[00あi]', printf('[%05.4S]', 'あiう')) 405 call assert_equal('[000あi]', printf('[%06.4S]', 'あiう')) 406 call assert_equal('[0000あi]', printf('[%07.4S]', 'あiう')) 407 408 call assert_equal('[0あい]', printf('[%05.5S]', 'あいう')) 409 call assert_equal('[00あい]', printf('[%06.5S]', 'あいう')) 410 call assert_equal('[000あい]', printf('[%07.5S]', 'あいう')) 411 412 call assert_equal('[あiう]', printf('[%05.5S]', 'あiう')) 413 call assert_equal('[0あiう]', printf('[%06.5S]', 'あiう')) 414 call assert_equal('[00あiう]', printf('[%07.5S]', 'あiう')) 415 416 call assert_equal('[0000000000]', printf('[%010.0S]', 'あいう')) 417 call assert_equal('[0000000000]', printf('[%010.1S]', 'あいう')) 418 call assert_equal('[00000000あ]', printf('[%010.2S]', 'あいう')) 419 call assert_equal('[00000000あ]', printf('[%010.3S]', 'あいう')) 420 call assert_equal('[000000あい]', printf('[%010.4S]', 'あいう')) 421 call assert_equal('[000000あい]', printf('[%010.5S]', 'あいう')) 422 call assert_equal('[0000あいう]', printf('[%010.6S]', 'あいう')) 423 call assert_equal('[0000あいう]', printf('[%010.7S]', 'あいう')) 424 425 call assert_equal('[0000000000]', printf('[%010.1S]', 'あiう')) 426 call assert_equal('[00000000あ]', printf('[%010.2S]', 'あiう')) 427 call assert_equal('[0000000あi]', printf('[%010.3S]', 'あiう')) 428 call assert_equal('[0000000あi]', printf('[%010.4S]', 'あiう')) 429 call assert_equal('[00000あiう]', printf('[%010.5S]', 'あiう')) 430 call assert_equal('[00000あiう]', printf('[%010.6S]', 'あiう')) 431 call assert_equal('[00000あiう]', printf('[%010.7S]', 'あiう')) 432 433 call assert_equal('1%', printf('%d%%', 1)) 434 call assert_notequal('', printf('%p', "abc")) 435 END 436 call CheckLegacyAndVim9Success(lines) 437 438 call CheckLegacyAndVim9Failure(["call printf('123', 3)"], "E767:") 439 440 " this was using uninitialized memory 441 call CheckLegacyAndVim9Failure(["eval ''->printf()"], "E119:") 442 endfunc 443 444 func Test_printf_float() 445 if has('float') 446 let lines =<< trim END 447 call assert_equal('1.000000', printf('%f', 1)) 448 call assert_equal('1.230000', printf('%f', 1.23)) 449 call assert_equal('1.230000', printf('%F', 1.23)) 450 call assert_equal('9999999.9', printf('%g', 9999999.9)) 451 call assert_equal('9999999.9', printf('%G', 9999999.9)) 452 call assert_equal('1.00000001e7', printf('%.8g', 10000000.1)) 453 call assert_equal('1.00000001E7', printf('%.8G', 10000000.1)) 454 call assert_equal('1.230000e+00', printf('%e', 1.23)) 455 call assert_equal('1.230000E+00', printf('%E', 1.23)) 456 call assert_equal('1.200000e-02', printf('%e', 0.012)) 457 call assert_equal('-1.200000e-02', printf('%e', -0.012)) 458 call assert_equal('0.33', printf('%.2f', 1.0 / 3.0)) 459 call assert_equal(' 0.33', printf('%6.2f', 1.0 / 3.0)) 460 call assert_equal(' -0.33', printf('%6.2f', -1.0 / 3.0)) 461 call assert_equal('000.33', printf('%06.2f', 1.0 / 3.0)) 462 call assert_equal('-00.33', printf('%06.2f', -1.0 / 3.0)) 463 call assert_equal('-00.33', printf('%+06.2f', -1.0 / 3.0)) 464 call assert_equal('+00.33', printf('%+06.2f', 1.0 / 3.0)) 465 call assert_equal(' 00.33', printf('% 06.2f', 1.0 / 3.0)) 466 call assert_equal('000.33', printf('%06.2g', 1.0 / 3.0)) 467 call assert_equal('-00.33', printf('%06.2g', -1.0 / 3.0)) 468 call assert_equal('0.33', printf('%3.2f', 1.0 / 3.0)) 469 call assert_equal('003.33e-01', printf('%010.2e', 1.0 / 3.0)) 470 call assert_equal(' 03.33e-01', printf('% 010.2e', 1.0 / 3.0)) 471 call assert_equal('+03.33e-01', printf('%+010.2e', 1.0 / 3.0)) 472 call assert_equal('-03.33e-01', printf('%010.2e', -1.0 / 3.0)) 473 474 #" When precision is 0, the dot should be omitted. 475 call assert_equal(' 2', printf('%3.f', 7.0 / 3.0)) 476 call assert_equal(' 2', printf('%3.g', 7.0 / 3.0)) 477 call assert_equal(' 2e+00', printf('%7.e', 7.0 / 3.0)) 478 479 #" Float zero can be signed. 480 call assert_equal('+0.000000', printf('%+f', 0.0)) 481 call assert_equal('0.000000', printf('%f', 1.0 / (1.0 / 0.0))) 482 call assert_equal('-0.000000', printf('%f', 1.0 / (-1.0 / 0.0))) 483 call assert_equal('0.0', printf('%s', 1.0 / (1.0 / 0.0))) 484 call assert_equal('-0.0', printf('%s', 1.0 / (-1.0 / 0.0))) 485 call assert_equal('0.0', printf('%S', 1.0 / (1.0 / 0.0))) 486 call assert_equal('-0.0', printf('%S', 1.0 / (-1.0 / 0.0))) 487 488 #" Float infinity can be signed. 489 call assert_equal('inf', printf('%f', 1.0 / 0.0)) 490 call assert_equal('-inf', printf('%f', -1.0 / 0.0)) 491 call assert_equal('inf', printf('%g', 1.0 / 0.0)) 492 call assert_equal('-inf', printf('%g', -1.0 / 0.0)) 493 call assert_equal('inf', printf('%e', 1.0 / 0.0)) 494 call assert_equal('-inf', printf('%e', -1.0 / 0.0)) 495 call assert_equal('INF', printf('%F', 1.0 / 0.0)) 496 call assert_equal('-INF', printf('%F', -1.0 / 0.0)) 497 call assert_equal('INF', printf('%E', 1.0 / 0.0)) 498 call assert_equal('-INF', printf('%E', -1.0 / 0.0)) 499 call assert_equal('INF', printf('%E', 1.0 / 0.0)) 500 call assert_equal('-INF', printf('%G', -1.0 / 0.0)) 501 call assert_equal('+inf', printf('%+f', 1.0 / 0.0)) 502 call assert_equal('-inf', printf('%+f', -1.0 / 0.0)) 503 call assert_equal(' inf', printf('% f', 1.0 / 0.0)) 504 call assert_equal(' inf', printf('%6f', 1.0 / 0.0)) 505 call assert_equal(' -inf', printf('%6f', -1.0 / 0.0)) 506 call assert_equal(' inf', printf('%6g', 1.0 / 0.0)) 507 call assert_equal(' -inf', printf('%6g', -1.0 / 0.0)) 508 call assert_equal(' +inf', printf('%+6f', 1.0 / 0.0)) 509 call assert_equal(' inf', printf('% 6f', 1.0 / 0.0)) 510 call assert_equal(' +inf', printf('%+06f', 1.0 / 0.0)) 511 call assert_equal('inf ', printf('%-6f', 1.0 / 0.0)) 512 call assert_equal('-inf ', printf('%-6f', -1.0 / 0.0)) 513 call assert_equal('+inf ', printf('%-+6f', 1.0 / 0.0)) 514 call assert_equal(' inf ', printf('%- 6f', 1.0 / 0.0)) 515 call assert_equal('-INF ', printf('%-6F', -1.0 / 0.0)) 516 call assert_equal('+INF ', printf('%-+6F', 1.0 / 0.0)) 517 call assert_equal(' INF ', printf('%- 6F', 1.0 / 0.0)) 518 call assert_equal('INF ', printf('%-6G', 1.0 / 0.0)) 519 call assert_equal('-INF ', printf('%-6G', -1.0 / 0.0)) 520 call assert_equal('INF ', printf('%-6E', 1.0 / 0.0)) 521 call assert_equal('-INF ', printf('%-6E', -1.0 / 0.0)) 522 call assert_equal("str2float('inf')", printf('%s', 1.0 / 0.0)) 523 call assert_equal("-str2float('inf')", printf('%s', -1.0 / 0.0)) 524 525 #" Test special case where max precision is truncated at 340. 526 call assert_equal('1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%.330f', 1.0)) 527 call assert_equal('1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%.340f', 1.0)) 528 call assert_equal('1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%.350f', 1.0)) 529 530 #" Float nan (not a number) has no sign. 531 call assert_equal('nan', printf('%f', sqrt(-1.0))) 532 call assert_equal('nan', printf('%f', 0.0 / 0.0)) 533 call assert_equal('nan', printf('%f', -0.0 / 0.0)) 534 call assert_equal('nan', printf('%g', 0.0 / 0.0)) 535 call assert_equal('nan', printf('%e', 0.0 / 0.0)) 536 call assert_equal('NAN', printf('%F', 0.0 / 0.0)) 537 call assert_equal('NAN', printf('%G', 0.0 / 0.0)) 538 call assert_equal('NAN', printf('%E', 0.0 / 0.0)) 539 call assert_equal('NAN', printf('%F', -0.0 / 0.0)) 540 call assert_equal('NAN', printf('%G', -0.0 / 0.0)) 541 call assert_equal('NAN', printf('%E', -0.0 / 0.0)) 542 call assert_equal(' nan', printf('%6f', 0.0 / 0.0)) 543 call assert_equal(' nan', printf('%06f', 0.0 / 0.0)) 544 call assert_equal('nan ', printf('%-6f', 0.0 / 0.0)) 545 call assert_equal('nan ', printf('%- 6f', 0.0 / 0.0)) 546 call assert_equal("str2float('nan')", printf('%s', 0.0 / 0.0)) 547 call assert_equal("str2float('nan')", printf('%s', -0.0 / 0.0)) 548 call assert_equal("str2float('nan')", printf('%S', 0.0 / 0.0)) 549 call assert_equal("str2float('nan')", printf('%S', -0.0 / 0.0)) 550 END 551 call CheckLegacyAndVim9Success(lines) 552 553 call CheckLegacyAndVim9Failure(['echo printf("%f", "a")'], 'E807:') 554 endif 555 endfunc 556 557 func Test_printf_errors() 558 call CheckLegacyAndVim9Failure(['echo printf("%d", {})'], 'E728:') 559 call CheckLegacyAndVim9Failure(['echo printf("%d", [])'], 'E745:') 560 call CheckLegacyAndVim9Failure(['echo printf("%d", 1, 2)'], 'E767:') 561 call CheckLegacyAndVim9Failure(['echo printf("%*d", 1)'], 'E766:') 562 call CheckLegacyAndVim9Failure(['echo printf("%s")'], 'E766:') 563 if has('float') 564 call CheckLegacyAndVim9Failure(['echo printf("%d", 1.2)'], 'E805:') 565 call CheckLegacyAndVim9Failure(['echo printf("%f")'], 'E766:') 566 endif 567 endfunc 568 569 func Test_printf_64bit() 570 let lines =<< trim END 571 call assert_equal("123456789012345", printf('%d', 123456789012345)) 572 END 573 call CheckLegacyAndVim9Success(lines) 574 endfunc 575 576 func Test_printf_spec_s() 577 let lines =<< trim END 578 #" number 579 call assert_equal("1234567890", printf('%s', 1234567890)) 580 581 #" string 582 call assert_equal("abcdefgi", printf('%s', "abcdefgi")) 583 584 #" float 585 if has('float') 586 call assert_equal("1.23", printf('%s', 1.23)) 587 endif 588 589 #" list 590 VAR lvalue = [1, 'two', ['three', 4]] 591 call assert_equal(string(lvalue), printf('%s', lvalue)) 592 593 #" dict 594 VAR dvalue = {'key1': 'value1', 'key2': ['list', 'lvalue'], 'key3': {'dict': 'lvalue'}} 595 call assert_equal(string(dvalue), printf('%s', dvalue)) 596 597 #" funcref 598 call assert_equal('printf', printf('%s', 'printf'->function())) 599 600 #" partial 601 call assert_equal(string(function('printf', ['%s'])), printf('%s', function('printf', ['%s']))) 602 END 603 call CheckLegacyAndVim9Success(lines) 604 endfunc 605 606 func Test_printf_spec_b() 607 let lines =<< trim END 608 call assert_equal("0", printf('%b', 0)) 609 call assert_equal("00001100", printf('%08b', 12)) 610 call assert_equal("11111111", printf('%08b', 0xff)) 611 call assert_equal(" 1111011", printf('%10b', 123)) 612 call assert_equal("0001111011", printf('%010b', 123)) 613 call assert_equal(" 0b1111011", printf('%#10b', 123)) 614 call assert_equal("0B01111011", printf('%#010B', 123)) 615 call assert_equal("1001001100101100000001011010010", printf('%b', 1234567890)) 616 call assert_equal("11100000100100010000110000011011101111101111001", printf('%b', 123456789012345)) 617 call assert_equal("1111111111111111111111111111111111111111111111111111111111111111", printf('%b', -1)) 618 END 619 call CheckLegacyAndVim9Success(lines) 620 endfunc 621 622 func Test_max_min_errors() 623 call CheckLegacyAndVim9Failure(['call max(v:true)'], ['E712:', 'E1013:', 'E1227:']) 624 call CheckLegacyAndVim9Failure(['call max(v:true)'], ['max()', 'E1013:', 'E1227:']) 625 call CheckLegacyAndVim9Failure(['call min(v:true)'], ['E712:', 'E1013:', 'E1227:']) 626 call CheckLegacyAndVim9Failure(['call min(v:true)'], ['min()', 'E1013:', 'E1227:']) 627 endfunc 628 629 func Test_function_with_funcref() 630 let lines =<< trim END 631 let s:F = function('type') 632 let s:Fref = function(s:F) 633 call assert_equal(v:t_string, s:Fref('x')) 634 call assert_fails("call function('s:F')", 'E700:') 635 636 call assert_fails("call function('foo()')", 'E475:') 637 call assert_fails("call function('foo()')", 'foo()') 638 call assert_fails("function('')", 'E129:') 639 640 let s:Len = {s -> strlen(s)} 641 call assert_equal(6, s:Len('foobar')) 642 let name = string(s:Len) 643 " can evaluate "function('<lambda>99')" 644 call execute('let Ref = ' .. name) 645 call assert_equal(4, Ref('text')) 646 END 647 call CheckScriptSuccess(lines) 648 649 let lines =<< trim END 650 vim9script 651 var F = function('type') 652 var Fref = function(F) 653 call assert_equal(v:t_string, Fref('x')) 654 call assert_fails("call function('F')", 'E700:') 655 656 call assert_fails("call function('foo()')", 'E475:') 657 call assert_fails("call function('foo()')", 'foo()') 658 call assert_fails("function('')", 'E129:') 659 660 var Len = (s) => strlen(s) 661 call assert_equal(6, Len('foobar')) 662 var name = string(Len) 663 # can evaluate "function('<lambda>99')" 664 call execute('var Ref = ' .. name) 665 call assert_equal(4, Ref('text')) 666 END 667 call CheckScriptSuccess(lines) 668 endfunc 669 670 func Test_funcref() 671 func! One() 672 return 1 673 endfunc 674 let OneByName = function('One') 675 let OneByRef = funcref('One') 676 func! One() 677 return 2 678 endfunc 679 call assert_equal(2, OneByName()) 680 call assert_equal(1, OneByRef()) 681 let OneByRef = 'One'->funcref() 682 call assert_equal(2, OneByRef()) 683 call assert_fails('echo funcref("{")', 'E475:') 684 let OneByRef = funcref("One", repeat(["foo"], 20)) 685 call assert_fails('let OneByRef = funcref("One", repeat(["foo"], 21))', 'E118:') 686 call assert_fails('echo function("min") =~ function("min")', 'E694:') 687 endfunc 688 689 " Test for calling function() and funcref() outside of a Vim script context. 690 func Test_function_outside_script() 691 let cleanup =<< trim END 692 call writefile([execute('messages')], 'Xtest.out') 693 qall 694 END 695 call writefile(cleanup, 'Xverify.vim') 696 call RunVim([], [], "-c \"echo function('s:abc')\" -S Xverify.vim") 697 call assert_match('E81: Using <SID> not in a', readfile('Xtest.out')[0]) 698 call RunVim([], [], "-c \"echo funcref('s:abc')\" -S Xverify.vim") 699 call assert_match('E81: Using <SID> not in a', readfile('Xtest.out')[0]) 700 call delete('Xtest.out') 701 call delete('Xverify.vim') 702 endfunc 703 704 func Test_setmatches() 705 let lines =<< trim END 706 hi def link 1 Comment 707 hi def link 2 PreProc 708 VAR set = [{"group": 1, "pattern": 2, "id": 3, "priority": 4}] 709 VAR exp = [{"group": '1', "pattern": '2', "id": 3, "priority": 4}] 710 if has('conceal') 711 LET set[0]['conceal'] = 5 712 LET exp[0]['conceal'] = '5' 713 endif 714 eval set->setmatches() 715 call assert_equal(exp, getmatches()) 716 END 717 call CheckLegacyAndVim9Success(lines) 718 719 call CheckLegacyAndVim9Failure(['VAR m = setmatches([], [])'], ['E745:', 'E1013:', 'E1210:']) 720 endfunc 721 722 func Test_empty_concatenate() 723 let lines =<< trim END 724 call assert_equal('b', 'a'[4 : 0] .. 'b') 725 call assert_equal('b', 'b' .. 'a'[4 : 0]) 726 END 727 call CheckLegacyAndVim9Success(lines) 728 endfunc 729 730 func Test_broken_number() 731 call CheckLegacyAndVim9Failure(['VAR X = "bad"', 'echo 1X'], 'E15:') 732 call CheckLegacyAndVim9Failure(['VAR X = "bad"', 'echo 0b1X'], 'E15:') 733 call CheckLegacyAndVim9Failure(['echo 0b12'], 'E15:') 734 call CheckLegacyAndVim9Failure(['VAR X = "bad"', 'echo 0x1X'], 'E15:') 735 call CheckLegacyAndVim9Failure(['VAR X = "bad"', 'echo 011X'], 'E15:') 736 737 call CheckLegacyAndVim9Success(['call assert_equal(2, str2nr("2a"))']) 738 739 call CheckLegacyAndVim9Failure(['inoremap <Char-0b1z> b'], 'E474:') 740 endfunc 741 742 func Test_eval_after_if() 743 let s:val = '' 744 func SetVal(x) 745 let s:val ..= a:x 746 endfunc 747 if 0 | eval SetVal('a') | endif | call SetVal('b') 748 call assert_equal('b', s:val) 749 endfunc 750 751 func Test_divide_by_zero() 752 " only tests that this doesn't crash, the result is not important 753 echo 0 / 0 754 echo 0 / 0 / -1 755 endfunc 756 757 " Test for command-line completion of expressions 758 func Test_expr_completion() 759 CheckFeature cmdline_compl 760 for cmd in [ 761 \ 'let a = ', 762 \ 'const a = ', 763 \ 'if', 764 \ 'elseif', 765 \ 'while', 766 \ 'for', 767 \ 'echo', 768 \ 'echon', 769 \ 'execute', 770 \ 'echomsg', 771 \ 'echoerr', 772 \ 'call', 773 \ 'return', 774 \ 'cexpr', 775 \ 'caddexpr', 776 \ 'cgetexpr', 777 \ 'lexpr', 778 \ 'laddexpr', 779 \ 'lgetexpr'] 780 call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt') 781 call assert_equal('"' . cmd . ' getline(', getreg(':')) 782 endfor 783 784 " completion for the expression register 785 call feedkeys(":\"\<C-R>=float2\t\"\<C-B>\"\<CR>", 'xt') 786 call assert_equal('"float2nr("', @=) 787 788 " completion for window local variables 789 let w:wvar1 = 10 790 let w:wvar2 = 10 791 call feedkeys(":echo w:wvar\<C-A>\<C-B>\"\<CR>", 'xt') 792 call assert_equal('"echo w:wvar1 w:wvar2', @:) 793 unlet w:wvar1 w:wvar2 794 795 " completion for tab local variables 796 let t:tvar1 = 10 797 let t:tvar2 = 10 798 call feedkeys(":echo t:tvar\<C-A>\<C-B>\"\<CR>", 'xt') 799 call assert_equal('"echo t:tvar1 t:tvar2', @:) 800 unlet t:tvar1 t:tvar2 801 802 " completion for variables 803 let g:tvar1 = 1 804 let g:tvar2 = 2 805 call feedkeys(":let g:tv\<C-A>\<C-B>\"\<CR>", 'xt') 806 call assert_equal('"let g:tvar1 g:tvar2', @:) 807 " completion for variables after a || 808 call feedkeys(":echo 1 || g:tv\<C-A>\<C-B>\"\<CR>", 'xt') 809 call assert_equal('"echo 1 || g:tvar1 g:tvar2', @:) 810 811 " completion for options 812 "call feedkeys(":echo &compat\<C-A>\<C-B>\"\<CR>", 'xt') 813 "call assert_equal('"echo &compatible', @:) 814 "call feedkeys(":echo 1 && &compat\<C-A>\<C-B>\"\<CR>", 'xt') 815 "call assert_equal('"echo 1 && &compatible', @:) 816 call feedkeys(":echo &g:equala\<C-A>\<C-B>\"\<CR>", 'xt') 817 call assert_equal('"echo &g:equalalways', @:) 818 819 " completion for string 820 call feedkeys(":echo \"Hello\\ World\"\<C-A>\<C-B>\"\<CR>", 'xt') 821 call assert_equal("\"echo \"Hello\\ World\"\<C-A>", @:) 822 call feedkeys(":echo 'Hello World'\<C-A>\<C-B>\"\<CR>", 'xt') 823 call assert_equal("\"echo 'Hello World'\<C-A>", @:) 824 825 " completion for command after a | 826 call feedkeys(":echo 'Hello' | cwin\<C-A>\<C-B>\"\<CR>", 'xt') 827 call assert_equal("\"echo 'Hello' | cwindow", @:) 828 829 " completion for environment variable 830 let $X_VIM_TEST_COMPLETE_ENV = 'foo' 831 call feedkeys(":let $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx') 832 call assert_match('"let $X_VIM_TEST_COMPLETE_ENV', @:) 833 unlet $X_VIM_TEST_COMPLETE_ENV 834 endfunc 835 836 " Test for errors in expression evaluation 837 func Test_expr_eval_error() 838 call CheckLegacyAndVim9Failure(["VAR i = 'abc' .. []"], ['E730:', 'E1105:', 'E730:']) 839 call CheckLegacyAndVim9Failure(["VAR l = [] + 10"], ['E745:', 'E1051:', 'E745:']) 840 call CheckLegacyAndVim9Failure(["VAR v = 10 + []"], ['E745:', 'E1051:', 'E745:']) 841 call CheckLegacyAndVim9Failure(["VAR v = 10 / []"], ['E745:', 'E1036:', 'E745:']) 842 call CheckLegacyAndVim9Failure(["VAR v = -{}"], ['E728:', 'E1012:', 'E728:']) 843 endfunc 844 845 func Test_white_in_function_call() 846 let lines =<< trim END 847 VAR text = substitute ( 'some text' , 't' , 'T' , 'g' ) 848 call assert_equal('some TexT', text) 849 END 850 call CheckTransLegacySuccess(lines) 851 852 let lines =<< trim END 853 var text = substitute ( 'some text' , 't' , 'T' , 'g' ) 854 call assert_equal('some TexT', text) 855 END 856 call CheckDefAndScriptFailure(lines, ['E1001:', 'E121:']) 857 endfunc 858 859 " Test for float value comparison 860 func Test_float_compare() 861 CheckFeature float 862 863 let lines =<< trim END 864 call assert_true(1.2 == 1.2) 865 call assert_true(1.0 != 1.2) 866 call assert_true(1.2 > 1.0) 867 call assert_true(1.2 >= 1.2) 868 call assert_true(1.0 < 1.2) 869 call assert_true(1.2 <= 1.2) 870 call assert_true(+0.0 == -0.0) 871 #" two NaNs (not a number) are not equal 872 call assert_true(sqrt(-4.01) != (0.0 / 0.0)) 873 #" two inf (infinity) are equal 874 call assert_true((1.0 / 0) == (2.0 / 0)) 875 #" two -inf (infinity) are equal 876 call assert_true(-(1.0 / 0) == -(2.0 / 0)) 877 #" +infinity != -infinity 878 call assert_true((1.0 / 0) != -(2.0 / 0)) 879 END 880 call CheckLegacyAndVim9Success(lines) 881 endfunc 882 883 func Test_string_interp() 884 let lines =<< trim END 885 call assert_equal('', $"") 886 call assert_equal('foobar', $"foobar") 887 #" Escaping rules. 888 call assert_equal('"foo"{bar}', $"\"foo\"{{bar}}") 889 call assert_equal('"foo"{bar}', $'"foo"{{bar}}') 890 call assert_equal('foobar', $"{"foo"}" .. $'{'bar'}') 891 #" Whitespace before/after the expression. 892 call assert_equal('3', $"{ 1 + 2 }") 893 #" String conversion. 894 call assert_equal('hello from ' .. v:version, $"hello from {v:version}") 895 call assert_equal('hello from ' .. v:version, $'hello from {v:version}') 896 #" Paper over a small difference between Vim script behaviour. 897 call assert_equal(string(v:true), $"{v:true}") 898 call assert_equal('(1+1=2)', $"(1+1={1 + 1})") 899 #" Hex-escaped opening brace: char2nr('{') == 0x7b 900 call assert_equal('esc123ape', $"esc{123}ape") 901 call assert_equal('me{}me', $"me{"\x7b"}\x7dme") 902 VAR var1 = "sun" 903 VAR var2 = "shine" 904 call assert_equal('sunshine', $"{var1}{var2}") 905 call assert_equal('sunsunsun', $"{var1->repeat(3)}") 906 #" Multibyte strings. 907 call assert_equal('say ハロー・ワールド', $"say {'ハロー・ワールド'}") 908 #" Nested. 909 call assert_equal('foobarbaz', $"foo{$"{'bar'}"}baz") 910 #" Do not evaluate blocks when the expr is skipped. 911 VAR tmp = 0 912 if v:false 913 echo "${ LET tmp += 1 }" 914 endif 915 call assert_equal(0, tmp) 916 917 #" Dict interpolation 918 VAR d = {'a': 10, 'b': [1, 2]} 919 call assert_equal("{'a': 10, 'b': [1, 2]}", $'{d}') 920 VAR emptydict = {} 921 call assert_equal("a{}b", $'a{emptydict}b') 922 VAR nulldict = v:_null_dict 923 call assert_equal("a{}b", $'a{nulldict}b') 924 925 #" List interpolation 926 VAR l = ['a', 'b', 'c'] 927 call assert_equal("['a', 'b', 'c']", $'{l}') 928 VAR emptylist = [] 929 call assert_equal("a[]b", $'a{emptylist}b') 930 VAR nulllist = v:_null_list 931 call assert_equal("a[]b", $'a{nulllist}b') 932 933 #" Stray closing brace. 934 call assert_fails('echo $"moo}"', 'E1278:') 935 #" Undefined variable in expansion. 936 call assert_fails('echo $"{moo}"', 'E121:') 937 #" Empty blocks are rejected. 938 call assert_fails('echo $"{}"', 'E15:') 939 call assert_fails('echo $"{ }"', 'E15:') 940 END 941 call CheckLegacyAndVim9Success(lines) 942 943 let lines =<< trim END 944 call assert_equal('5', $"{({x -> x + 1})(4)}") 945 END 946 call CheckLegacySuccess(lines) 947 948 let lines =<< trim END 949 call assert_equal('5', $"{((x) => x + 1)(4)}") 950 call assert_fails('echo $"{ # foo }"', 'E1279:') 951 END 952 call CheckDefAndScriptSuccess(lines) 953 endfunc 954 955 " vim: shiftwidth=2 sts=2 expandtab