1" Tests for Vim9 script expressions 2 3source check.vim 4source vim9.vim 5 6" test cond ? expr : expr 7def Test_expr1() 8 assert_equal('one', true ? 'one' : 'two') 9 assert_equal('one', 1 ? 10 'one' : 11 'two') 12 if has('float') 13 assert_equal('one', 0.1 ? 'one' : 'two') 14 endif 15 assert_equal('one', 'x' ? 'one' : 'two') 16 assert_equal('one', 0z1234 ? 'one' : 'two') 17 assert_equal('one', [0] ? 'one' : 'two') 18 assert_equal('one', #{x: 0} ? 'one' : 'two') 19 let var = 1 20 assert_equal('one', var ? 'one' : 'two') 21 22 assert_equal('two', false ? 'one' : 'two') 23 assert_equal('two', 0 ? 'one' : 'two') 24 if has('float') 25 assert_equal('two', 0.0 ? 'one' : 'two') 26 endif 27 assert_equal('two', '' ? 'one' : 'two') 28 assert_equal('two', 0z ? 'one' : 'two') 29 assert_equal('two', [] ? 'one' : 'two') 30 assert_equal('two', {} ? 'one' : 'two') 31 var = 0 32 assert_equal('two', var ? 'one' : 'two') 33 34 let Some: func = function('len') 35 let Other: func = function('winnr') 36 let Res: func = g:atrue ? Some : Other 37 assert_equal(function('len'), Res) 38 39 let RetOne: func(string): number = function('len') 40 let RetTwo: func(string): number = function('winnr') 41 let RetThat: func = g:atrue ? RetOne : RetTwo 42 assert_equal(function('len'), RetThat) 43enddef 44 45func Test_expr1_fails() 46 call CheckDefFailure(["let x = 1 ? 'one'"], "Missing ':' after '?'") 47 call CheckDefFailure(["let x = 1 ? 'one' : xxx"], "E1001:") 48 49 let msg = "white space required before and after '?'" 50 call CheckDefFailure(["let x = 1? 'one' : 'two'"], msg) 51 call CheckDefFailure(["let x = 1 ?'one' : 'two'"], msg) 52 call CheckDefFailure(["let x = 1?'one' : 'two'"], msg) 53 54 let msg = "white space required before and after ':'" 55 call CheckDefFailure(["let x = 1 ? 'one': 'two'"], msg) 56 call CheckDefFailure(["let x = 1 ? 'one' :'two'"], msg) 57 call CheckDefFailure(["let x = 1 ? 'one':'two'"], msg) 58endfunc 59 60" TODO: define inside test function 61def Record(val: any): any 62 g:vals->add(val) 63 return val 64enddef 65 66" test || 67def Test_expr2() 68 assert_equal(2, 2 || 0) 69 assert_equal(7, 0 || 70 0 || 71 7) 72 assert_equal(0, 0 || 0) 73 assert_equal('', 0 || '') 74 75 g:vals = [] 76 assert_equal(3, Record(3) || Record(1)) 77 assert_equal([3], g:vals) 78 79 g:vals = [] 80 assert_equal(5, Record(0) || Record(5)) 81 assert_equal([0, 5], g:vals) 82 83 g:vals = [] 84 assert_equal(4, Record(0) || Record(4) || Record(0)) 85 assert_equal([0, 4], g:vals) 86 87 g:vals = [] 88 assert_equal(0, Record([]) || Record('') || Record(0)) 89 assert_equal([[], '', 0], g:vals) 90enddef 91 92func Test_expr2_fails() 93 let msg = "white space required before and after '||'" 94 call CheckDefFailure(["let x = 1||2"], msg) 95 call CheckDefFailure(["let x = 1 ||2"], msg) 96 call CheckDefFailure(["let x = 1|| 2"], msg) 97 98 call CheckDefFailure(["let x = 1 || xxx"], 'E1001:') 99endfunc 100 101" test && 102def Test_expr3() 103 assert_equal(0, 2 && 0) 104 assert_equal(0, 0 && 105 0 && 106 7) 107 assert_equal(7, 2 && 3 && 7) 108 assert_equal(0, 0 && 0) 109 assert_equal(0, 0 && '') 110 assert_equal('', 8 && '') 111 112 g:vals = [] 113 assert_equal(1, Record(3) && Record(1)) 114 assert_equal([3, 1], g:vals) 115 116 g:vals = [] 117 assert_equal(0, Record(0) && Record(5)) 118 assert_equal([0], g:vals) 119 120 g:vals = [] 121 assert_equal(0, Record(0) && Record(4) && Record(0)) 122 assert_equal([0], g:vals) 123 124 g:vals = [] 125 assert_equal(0, Record(8) && Record(4) && Record(0)) 126 assert_equal([8, 4, 0], g:vals) 127 128 g:vals = [] 129 assert_equal(0, Record([1]) && Record('z') && Record(0)) 130 assert_equal([[1], 'z', 0], g:vals) 131enddef 132 133func Test_expr3_fails() 134 let msg = "white space required before and after '&&'" 135 call CheckDefFailure(["let x = 1&&2"], msg) 136 call CheckDefFailure(["let x = 1 &&2"], msg) 137 call CheckDefFailure(["let x = 1&& 2"], msg) 138endfunc 139 140let atrue = v:true 141let afalse = v:false 142let anone = v:none 143let anull = v:null 144let anint = 10 145let alsoint = 4 146if has('float') 147 let afloat = 0.1 148endif 149let astring = 'asdf' 150let ablob = 0z01ab 151let alist = [2, 3, 4] 152let adict = #{aaa: 2, bbb: 8} 153 154" test == comperator 155def Test_expr4_equal() 156 assert_equal(true, true == true) 157 assert_equal(false, true == 158 false) 159 assert_equal(true, true == g:atrue) 160 assert_equal(false, g:atrue == false) 161 162 assert_equal(true, v:none == v:none) 163 assert_equal(false, v:none == v:null) 164 assert_equal(true, g:anone == v:none) 165 assert_equal(false, v:none == g:anull) 166 167 assert_equal(false, 2 == 0) 168 assert_equal(true, 61 == 61) 169 assert_equal(true, g:anint == 10) 170 assert_equal(false, 61 == g:anint) 171 172 if has('float') 173 assert_equal(true, 0.3 == 0.3) 174 assert_equal(false, 0.4 == 0.3) 175 assert_equal(true, 0.1 == g:afloat) 176 assert_equal(false, g:afloat == 0.3) 177 178 assert_equal(true, 3.0 == 3) 179 assert_equal(true, 3 == 3.0) 180 assert_equal(false, 3.1 == 3) 181 assert_equal(false, 3 == 3.1) 182 endif 183 184 assert_equal(true, 'abc' == 'abc') 185 assert_equal(false, 'xyz' == 'abc') 186 assert_equal(true, g:astring == 'asdf') 187 assert_equal(false, 'xyz' == g:astring) 188 189 assert_equal(false, 'abc' == 'aBc') 190 assert_equal(false, 'abc' ==# 'aBc') 191 assert_equal(true, 'abc' ==? 'aBc') 192 193 assert_equal(false, 'abc' == 'ABC') 194 set ignorecase 195 assert_equal(false, 'abc' == 'ABC') 196 assert_equal(false, 'abc' ==# 'ABC') 197 set noignorecase 198 199 call CheckDefFailure(["let x = 'a' == xxx"], 'E1001:') 200 201 assert_equal(true, 0z3f == 0z3f) 202 assert_equal(false, 0z3f == 0z4f) 203 assert_equal(true, g:ablob == 0z01ab) 204 assert_equal(false, 0z3f == g:ablob) 205 206 assert_equal(true, [1, 2, 3] == [1, 2, 3]) 207 assert_equal(false, [1, 2, 3] == [2, 3, 1]) 208 assert_equal(true, [2, 3, 4] == g:alist) 209 assert_equal(false, g:alist == [2, 3, 1]) 210 assert_equal(false, [1, 2, 3] == []) 211 assert_equal(false, [1, 2, 3] == ['1', '2', '3']) 212 213 assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2}) 214 assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2}) 215 assert_equal(false, #{one: 1, two: 2} == #{two: 2}) 216 assert_equal(false, #{one: 1, two: 2} == #{}) 217 assert_equal(true, g:adict == #{bbb: 8, aaa: 2}) 218 assert_equal(false, #{ccc: 9, aaa: 2} == g:adict) 219 220 assert_equal(true, function('g:Test_expr4_equal') == function('g:Test_expr4_equal')) 221 assert_equal(false, function('g:Test_expr4_equal') == function('g:Test_expr4_is')) 222 223 assert_equal(true, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [123])) 224 assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_is', [123])) 225 assert_equal(false, function('g:Test_expr4_equal', [123]) == function('g:Test_expr4_equal', [999])) 226 227 let OneFunc: func 228 let TwoFunc: func 229 OneFunc = function('len') 230 TwoFunc = function('len') 231 assert_equal(true, OneFunc('abc') == TwoFunc('123')) 232enddef 233 234" test != comperator 235def Test_expr4_notequal() 236 assert_equal(false, true != true) 237 assert_equal(true, true != 238 false) 239 assert_equal(false, true != g:atrue) 240 assert_equal(true, g:atrue != false) 241 242 assert_equal(false, v:none != v:none) 243 assert_equal(true, v:none != v:null) 244 assert_equal(false, g:anone != v:none) 245 assert_equal(true, v:none != g:anull) 246 247 assert_equal(true, 2 != 0) 248 assert_equal(false, 55 != 55) 249 assert_equal(false, g:anint != 10) 250 assert_equal(true, 61 != g:anint) 251 252 if has('float') 253 assert_equal(false, 0.3 != 0.3) 254 assert_equal(true, 0.4 != 0.3) 255 assert_equal(false, 0.1 != g:afloat) 256 assert_equal(true, g:afloat != 0.3) 257 258 assert_equal(false, 3.0 != 3) 259 assert_equal(false, 3 != 3.0) 260 assert_equal(true, 3.1 != 3) 261 assert_equal(true, 3 != 3.1) 262 endif 263 264 assert_equal(false, 'abc' != 'abc') 265 assert_equal(true, 'xyz' != 'abc') 266 assert_equal(false, g:astring != 'asdf') 267 assert_equal(true, 'xyz' != g:astring) 268 269 assert_equal(true, 'abc' != 'ABC') 270 set ignorecase 271 assert_equal(true, 'abc' != 'ABC') 272 set noignorecase 273 274 assert_equal(false, 0z3f != 0z3f) 275 assert_equal(true, 0z3f != 0z4f) 276 assert_equal(false, g:ablob != 0z01ab) 277 assert_equal(true, 0z3f != g:ablob) 278 279 assert_equal(false, [1, 2, 3] != [1, 2, 3]) 280 assert_equal(true, [1, 2, 3] != [2, 3, 1]) 281 assert_equal(false, [2, 3, 4] != g:alist) 282 assert_equal(true, g:alist != [2, 3, 1]) 283 assert_equal(true, [1, 2, 3] != []) 284 assert_equal(true, [1, 2, 3] != ['1', '2', '3']) 285 286 assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2}) 287 assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2}) 288 assert_equal(true, #{one: 1, two: 2} != #{two: 2}) 289 assert_equal(true, #{one: 1, two: 2} != #{}) 290 assert_equal(false, g:adict != #{bbb: 8, aaa: 2}) 291 assert_equal(true, #{ccc: 9, aaa: 2} != g:adict) 292 293 assert_equal(false, function('g:Test_expr4_equal') != function('g:Test_expr4_equal')) 294 assert_equal(true, function('g:Test_expr4_equal') != function('g:Test_expr4_is')) 295 296 assert_equal(false, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [123])) 297 assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_is', [123])) 298 assert_equal(true, function('g:Test_expr4_equal', [123]) != function('g:Test_expr4_equal', [999])) 299enddef 300 301" test > comperator 302def Test_expr4_greater() 303 assert_true(2 > 0) 304 assert_true(2 > 305 1) 306 assert_false(2 > 2) 307 assert_false(2 > 3) 308 if has('float') 309 assert_true(2.0 > 0.0) 310 assert_true(2.0 > 1.0) 311 assert_false(2.0 > 2.0) 312 assert_false(2.0 > 3.0) 313 endif 314enddef 315 316" test >= comperator 317def Test_expr4_greaterequal() 318 assert_true(2 >= 0) 319 assert_true(2 >= 320 2) 321 assert_false(2 >= 3) 322 if has('float') 323 assert_true(2.0 >= 0.0) 324 assert_true(2.0 >= 2.0) 325 assert_false(2.0 >= 3.0) 326 endif 327enddef 328 329" test < comperator 330def Test_expr4_smaller() 331 assert_false(2 < 0) 332 assert_false(2 < 333 2) 334 assert_true(2 < 3) 335 if has('float') 336 assert_false(2.0 < 0.0) 337 assert_false(2.0 < 2.0) 338 assert_true(2.0 < 3.0) 339 endif 340enddef 341 342" test <= comperator 343def Test_expr4_smallerequal() 344 assert_false(2 <= 0) 345 assert_false(2 <= 346 1) 347 assert_true(2 <= 2) 348 assert_true(2 <= 3) 349 if has('float') 350 assert_false(2.0 <= 0.0) 351 assert_false(2.0 <= 1.0) 352 assert_true(2.0 <= 2.0) 353 assert_true(2.0 <= 3.0) 354 endif 355enddef 356 357" test =~ comperator 358def Test_expr4_match() 359 assert_equal(false, '2' =~ '0') 360 assert_equal(true, '2' =~ 361 '[0-9]') 362enddef 363 364" test !~ comperator 365def Test_expr4_nomatch() 366 assert_equal(true, '2' !~ '0') 367 assert_equal(false, '2' !~ 368 '[0-9]') 369enddef 370 371" test is comperator 372def Test_expr4_is() 373 let mylist = [2] 374 assert_false(mylist is [2]) 375 let other = mylist 376 assert_true(mylist is 377 other) 378 379 let myblob = 0z1234 380 assert_false(myblob is 0z1234) 381 let otherblob = myblob 382 assert_true(myblob is otherblob) 383enddef 384 385" test isnot comperator 386def Test_expr4_isnot() 387 let mylist = [2] 388 assert_true('2' isnot '0') 389 assert_true(mylist isnot [2]) 390 let other = mylist 391 assert_false(mylist isnot 392 other) 393 394 let myblob = 0z1234 395 assert_true(myblob isnot 0z1234) 396 let otherblob = myblob 397 assert_false(myblob isnot otherblob) 398enddef 399 400def RetVoid() 401 let x = 1 402enddef 403 404func Test_expr4_fails() 405 let msg = "white space required before and after '>'" 406 call CheckDefFailure(["let x = 1>2"], msg) 407 call CheckDefFailure(["let x = 1 >2"], msg) 408 call CheckDefFailure(["let x = 1> 2"], msg) 409 410 let msg = "white space required before and after '=='" 411 call CheckDefFailure(["let x = 1==2"], msg) 412 call CheckDefFailure(["let x = 1 ==2"], msg) 413 call CheckDefFailure(["let x = 1== 2"], msg) 414 415 let msg = "white space required before and after 'is'" 416 call CheckDefFailure(["let x = '1'is'2'"], msg) 417 call CheckDefFailure(["let x = '1' is'2'"], msg) 418 call CheckDefFailure(["let x = '1'is '2'"], msg) 419 420 let msg = "white space required before and after 'isnot'" 421 call CheckDefFailure(["let x = '1'isnot'2'"], msg) 422 call CheckDefFailure(["let x = '1' isnot'2'"], msg) 423 call CheckDefFailure(["let x = '1'isnot '2'"], msg) 424 425 call CheckDefFailure(["let x = 1 is# 2"], 'E15:') 426 call CheckDefFailure(["let x = 1 is? 2"], 'E15:') 427 call CheckDefFailure(["let x = 1 isnot# 2"], 'E15:') 428 call CheckDefFailure(["let x = 1 isnot? 2"], 'E15:') 429 430 call CheckDefFailure(["let x = 1 == '2'"], 'Cannot compare number with string') 431 call CheckDefFailure(["let x = '1' == 2"], 'Cannot compare string with number') 432 call CheckDefFailure(["let x = 1 == RetVoid()"], 'Cannot use void value') 433 call CheckDefFailure(["let x = RetVoid() == 1"], 'Cannot compare void with number') 434 435 call CheckDefFailure(["let x = true > false"], 'Cannot compare bool with bool') 436 call CheckDefFailure(["let x = true >= false"], 'Cannot compare bool with bool') 437 call CheckDefFailure(["let x = true < false"], 'Cannot compare bool with bool') 438 call CheckDefFailure(["let x = true <= false"], 'Cannot compare bool with bool') 439 call CheckDefFailure(["let x = true =~ false"], 'Cannot compare bool with bool') 440 call CheckDefFailure(["let x = true !~ false"], 'Cannot compare bool with bool') 441 call CheckDefFailure(["let x = true is false"], 'Cannot use "is" with bool') 442 call CheckDefFailure(["let x = true isnot false"], 'Cannot use "isnot" with bool') 443 444 call CheckDefFailure(["let x = v:none is v:null"], 'Cannot use "is" with special') 445 call CheckDefFailure(["let x = v:none isnot v:null"], 'Cannot use "isnot" with special') 446 call CheckDefFailure(["let x = 123 is 123"], 'Cannot use "is" with number') 447 call CheckDefFailure(["let x = 123 isnot 123"], 'Cannot use "isnot" with number') 448 if has('float') 449 call CheckDefFailure(["let x = 1.3 is 1.3"], 'Cannot use "is" with float') 450 call CheckDefFailure(["let x = 1.3 isnot 1.3"], 'Cannot use "isnot" with float') 451 endif 452 453 call CheckDefFailure(["let x = 0za1 > 0z34"], 'Cannot compare blob with blob') 454 call CheckDefFailure(["let x = 0za1 >= 0z34"], 'Cannot compare blob with blob') 455 call CheckDefFailure(["let x = 0za1 < 0z34"], 'Cannot compare blob with blob') 456 call CheckDefFailure(["let x = 0za1 <= 0z34"], 'Cannot compare blob with blob') 457 call CheckDefFailure(["let x = 0za1 =~ 0z34"], 'Cannot compare blob with blob') 458 call CheckDefFailure(["let x = 0za1 !~ 0z34"], 'Cannot compare blob with blob') 459 460 call CheckDefFailure(["let x = [13] > [88]"], 'Cannot compare list with list') 461 call CheckDefFailure(["let x = [13] >= [88]"], 'Cannot compare list with list') 462 call CheckDefFailure(["let x = [13] < [88]"], 'Cannot compare list with list') 463 call CheckDefFailure(["let x = [13] <= [88]"], 'Cannot compare list with list') 464 call CheckDefFailure(["let x = [13] =~ [88]"], 'Cannot compare list with list') 465 call CheckDefFailure(["let x = [13] !~ [88]"], 'Cannot compare list with list') 466 467 call CheckDefFailure(['let j: job', 'let chan: channel', 'let r = j == chan'], 'Cannot compare job with channel') 468 call CheckDefFailure(['let j: job', 'let x: list<any>', 'let r = j == x'], 'Cannot compare job with list') 469 call CheckDefFailure(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func') 470 call CheckDefFailure(['let j: job', 'let Xx: func', 'let r = j == Xx'], 'Cannot compare job with func') 471endfunc 472 473" test addition, subtraction, concatenation 474def Test_expr5() 475 assert_equal(66, 60 + 6) 476 assert_equal(70, 60 + 477 g:anint) 478 assert_equal(9, g:alsoint + 5) 479 assert_equal(14, g:alsoint + g:anint) 480 481 assert_equal(54, 60 - 6) 482 assert_equal(50, 60 - 483 g:anint) 484 assert_equal(-1, g:alsoint - 5) 485 assert_equal(-6, g:alsoint - g:anint) 486 487 assert_equal('hello', 'hel' .. 'lo') 488 assert_equal('hello 123', 'hello ' .. 489 123) 490 assert_equal('123 hello', 123 .. ' hello') 491 assert_equal('123456', 123 .. 456) 492 493 assert_equal([1, 2, 3, 4], [1, 2] + [3, 4]) 494 assert_equal(0z11223344, 0z1122 + 0z3344) 495 assert_equal(0z112201ab, 0z1122 + g:ablob) 496 assert_equal(0z01ab3344, g:ablob + 0z3344) 497 assert_equal(0z01ab01ab, g:ablob + g:ablob) 498enddef 499 500def Test_expr5_float() 501 if !has('float') 502 MissingFeature 'float' 503 else 504 assert_equal(66.0, 60.0 + 6.0) 505 assert_equal(66.0, 60.0 + 6) 506 assert_equal(66.0, 60 + 507 6.0) 508 assert_equal(5.1, g:afloat + 5) 509 assert_equal(8.1, 8 + g:afloat) 510 assert_equal(10.1, g:anint + g:afloat) 511 assert_equal(10.1, g:afloat + g:anint) 512 513 assert_equal(54.0, 60.0 - 6.0) 514 assert_equal(54.0, 60.0 - 6) 515 assert_equal(54.0, 60 - 6.0) 516 assert_equal(-4.9, g:afloat - 5) 517 assert_equal(7.9, 8 - g:afloat) 518 assert_equal(9.9, g:anint - g:afloat) 519 assert_equal(-9.9, g:afloat - g:anint) 520 endif 521enddef 522 523func Test_expr5_fails() 524 let msg = "white space required before and after '+'" 525 call CheckDefFailure(["let x = 1+2"], msg) 526 call CheckDefFailure(["let x = 1 +2"], msg) 527 call CheckDefFailure(["let x = 1+ 2"], msg) 528 529 let msg = "white space required before and after '-'" 530 call CheckDefFailure(["let x = 1-2"], msg) 531 call CheckDefFailure(["let x = 1 -2"], msg) 532 call CheckDefFailure(["let x = 1- 2"], msg) 533 534 let msg = "white space required before and after '..'" 535 call CheckDefFailure(["let x = '1'..'2'"], msg) 536 call CheckDefFailure(["let x = '1' ..'2'"], msg) 537 call CheckDefFailure(["let x = '1'.. '2'"], msg) 538 539 call CheckDefFailure(["let x = 0z1122 + 33"], 'E1035') 540 call CheckDefFailure(["let x = 0z1122 + [3]"], 'E1035') 541 call CheckDefFailure(["let x = 0z1122 + 'asd'"], 'E1035') 542 call CheckDefFailure(["let x = 33 + 0z1122"], 'E1035') 543 call CheckDefFailure(["let x = [3] + 0z1122"], 'E1035') 544 call CheckDefFailure(["let x = 'asdf' + 0z1122"], 'E1035') 545 call CheckDefFailure(["let x = 6 + xxx"], 'E1001') 546endfunc 547 548" test multiply, divide, modulo 549def Test_expr6() 550 assert_equal(36, 6 * 6) 551 assert_equal(24, 6 * 552 g:alsoint) 553 assert_equal(24, g:alsoint * 6) 554 assert_equal(40, g:anint * g:alsoint) 555 556 assert_equal(10, 60 / 6) 557 assert_equal(6, 60 / 558 g:anint) 559 assert_equal(1, g:anint / 6) 560 assert_equal(2, g:anint / g:alsoint) 561 562 assert_equal(5, 11 % 6) 563 assert_equal(4, g:anint % 6) 564 assert_equal(3, 13 % 565 g:anint) 566 assert_equal(2, g:anint % g:alsoint) 567 568 assert_equal(4, 6 * 4 / 6) 569 570 let x = [2] 571 let y = [3] 572 assert_equal(5, x[0] + y[0]) 573 assert_equal(6, x[0] * y[0]) 574 if has('float') 575 let xf = [2.0] 576 let yf = [3.0] 577 assert_equal(5.0, xf[0] + yf[0]) 578 assert_equal(6.0, xf[0] * yf[0]) 579 endif 580 581 call CheckDefFailure(["let x = 6 * xxx"], 'E1001') 582enddef 583 584def Test_expr6_float() 585 if !has('float') 586 MissingFeature 'float' 587 else 588 assert_equal(36.0, 6.0 * 6) 589 assert_equal(36.0, 6 * 590 6.0) 591 assert_equal(36.0, 6.0 * 6.0) 592 assert_equal(1.0, g:afloat * g:anint) 593 594 assert_equal(10.0, 60 / 6.0) 595 assert_equal(10.0, 60.0 / 596 6) 597 assert_equal(10.0, 60.0 / 6.0) 598 assert_equal(0.01, g:afloat / g:anint) 599 600 assert_equal(4.0, 6.0 * 4 / 6) 601 assert_equal(4.0, 6 * 602 4.0 / 603 6) 604 assert_equal(4.0, 6 * 4 / 6.0) 605 assert_equal(4.0, 6.0 * 4.0 / 6) 606 assert_equal(4.0, 6 * 4.0 / 6.0) 607 assert_equal(4.0, 6.0 * 4 / 6.0) 608 assert_equal(4.0, 6.0 * 4.0 / 6.0) 609 610 assert_equal(4.0, 6.0 * 4.0 / 6.0) 611 endif 612enddef 613 614func Test_expr6_fails() 615 let msg = "white space required before and after '*'" 616 call CheckDefFailure(["let x = 1*2"], msg) 617 call CheckDefFailure(["let x = 1 *2"], msg) 618 call CheckDefFailure(["let x = 1* 2"], msg) 619 620 let msg = "white space required before and after '/'" 621 call CheckDefFailure(["let x = 1/2"], msg) 622 call CheckDefFailure(["let x = 1 /2"], msg) 623 call CheckDefFailure(["let x = 1/ 2"], msg) 624 625 let msg = "white space required before and after '%'" 626 call CheckDefFailure(["let x = 1%2"], msg) 627 call CheckDefFailure(["let x = 1 %2"], msg) 628 call CheckDefFailure(["let x = 1% 2"], msg) 629 630 call CheckDefFailure(["let x = '1' * '2'"], 'E1036:') 631 call CheckDefFailure(["let x = '1' / '2'"], 'E1036:') 632 call CheckDefFailure(["let x = '1' % '2'"], 'E1035:') 633 634 call CheckDefFailure(["let x = 0z01 * 0z12"], 'E1036:') 635 call CheckDefFailure(["let x = 0z01 / 0z12"], 'E1036:') 636 call CheckDefFailure(["let x = 0z01 % 0z12"], 'E1035:') 637 638 call CheckDefFailure(["let x = [1] * [2]"], 'E1036:') 639 call CheckDefFailure(["let x = [1] / [2]"], 'E1036:') 640 call CheckDefFailure(["let x = [1] % [2]"], 'E1035:') 641 642 call CheckDefFailure(["let x = #{one: 1} * #{two: 2}"], 'E1036:') 643 call CheckDefFailure(["let x = #{one: 1} / #{two: 2}"], 'E1036:') 644 call CheckDefFailure(["let x = #{one: 1} % #{two: 2}"], 'E1035:') 645 646 call CheckDefFailure(["let x = 0xff[1]"], 'E714:') 647 if has('float') 648 call CheckDefFailure(["let x = 0.7[1]"], 'E714:') 649 endif 650endfunc 651 652func Test_expr6_float_fails() 653 CheckFeature float 654 call CheckDefFailure(["let x = 1.0 % 2"], 'E1035:') 655endfunc 656 657" define here to use old style parsing 658if has('float') 659 let g:float_zero = 0.0 660 let g:float_neg = -9.8 661 let g:float_big = 9.9e99 662endif 663let g:blob_empty = 0z 664let g:blob_one = 0z01 665let g:blob_long = 0z0102.0304 666 667let g:string_empty = '' 668let g:string_short = 'x' 669let g:string_long = 'abcdefghijklm' 670let g:string_special = "ab\ncd\ref\ekk" 671 672let g:special_true = v:true 673let g:special_false = v:false 674let g:special_null = v:null 675let g:special_none = v:none 676 677let g:list_empty = [] 678let g:list_mixed = [1, 'b', v:false] 679 680let g:dict_empty = {} 681let g:dict_one = #{one: 1} 682 683let $TESTVAR = 'testvar' 684 685" test low level expression 686def Test_expr7_number() 687 " number constant 688 assert_equal(0, 0) 689 assert_equal(654, 0654) 690 691 assert_equal(6, 0x6) 692 assert_equal(15, 0xf) 693 assert_equal(255, 0xff) 694enddef 695 696def Test_expr7_float() 697 " float constant 698 if !has('float') 699 MissingFeature 'float' 700 else 701 assert_equal(g:float_zero, .0) 702 assert_equal(g:float_zero, 0.0) 703 assert_equal(g:float_neg, -9.8) 704 assert_equal(g:float_big, 9.9e99) 705 endif 706enddef 707 708def Test_expr7_blob() 709 " blob constant 710 assert_equal(g:blob_empty, 0z) 711 assert_equal(g:blob_one, 0z01) 712 assert_equal(g:blob_long, 0z0102.0304) 713 714 call CheckDefFailure(["let x = 0z123"], 'E973:') 715enddef 716 717def Test_expr7_string() 718 " string constant 719 assert_equal(g:string_empty, '') 720 assert_equal(g:string_empty, "") 721 assert_equal(g:string_short, 'x') 722 assert_equal(g:string_short, "x") 723 assert_equal(g:string_long, 'abcdefghijklm') 724 assert_equal(g:string_long, "abcdefghijklm") 725 assert_equal(g:string_special, "ab\ncd\ref\ekk") 726 727 call CheckDefFailure(['let x = "abc'], 'E114:') 728 call CheckDefFailure(["let x = 'abc"], 'E115:') 729enddef 730 731def Test_expr7_vimvar() 732 let old: list<string> = v:oldfiles 733 let compl: dict<any> = v:completed_item 734 735 call CheckDefFailure(["let old: list<number> = v:oldfiles"], 'E1013: type mismatch, expected list<number> but got list<string>') 736 call CheckDefFailure(["let old: dict<number> = v:completed_item"], 'E1013: type mismatch, expected dict<number> but got dict<any>') 737enddef 738 739def Test_expr7_special() 740 " special constant 741 assert_equal(g:special_true, true) 742 assert_equal(g:special_false, false) 743 assert_equal(g:special_true, v:true) 744 assert_equal(g:special_false, v:false) 745 assert_equal(g:special_null, v:null) 746 assert_equal(g:special_none, v:none) 747 748 call CheckDefFailure(['v:true = true'], 'E46:') 749 call CheckDefFailure(['v:true = false'], 'E46:') 750 call CheckDefFailure(['v:false = true'], 'E46:') 751 call CheckDefFailure(['v:null = 11'], 'E46:') 752 call CheckDefFailure(['v:none = 22'], 'E46:') 753enddef 754 755def Test_expr7_list() 756 " list 757 assert_equal(g:list_empty, []) 758 assert_equal(g:list_empty, [ ]) 759 assert_equal(g:list_mixed, [1, 'b', false]) 760 assert_equal('b', g:list_mixed[1]) 761 762 call CheckDefExecFailure("let x = g:anint[3]", 'E714:') 763 call CheckDefFailure(["let x = g:list_mixed[xxx]"], 'E1001:') 764 call CheckDefExecFailure("let x = g:list_mixed['xx']", 'E39:') 765 call CheckDefFailure(["let x = g:list_mixed[0"], 'E111:') 766 call CheckDefExecFailure("let x = g:list_empty[3]", 'E684:') 767enddef 768 769def Test_expr7_lambda() 770 " lambda 771 let La = { -> 'result'} 772 assert_equal('result', La()) 773 assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val})) 774enddef 775 776def Test_expr7_dict() 777 " dictionary 778 assert_equal(g:dict_empty, {}) 779 assert_equal(g:dict_empty, { }) 780 assert_equal(g:dict_one, {'one': 1}) 781 let key = 'one' 782 let val = 1 783 assert_equal(g:dict_one, {key: val}) 784 785 call CheckDefFailure(["let x = #{8: 8}"], 'E1014:') 786 call CheckDefFailure(["let x = #{xxx}"], 'E720:') 787 call CheckDefFailure(["let x = #{xxx: 1", "let y = 2"], 'E722:') 788 call CheckDefFailure(["let x = #{xxx: 1,"], 'E723:') 789 call CheckDefFailure(["let x = {'a': xxx}"], 'E1001:') 790 call CheckDefFailure(["let x = {xxx: 8}"], 'E1001:') 791 call CheckDefFailure(["let x = #{a: 1, a: 2}"], 'E721:') 792 call CheckDefFailure(["let x = #"], 'E1015:') 793 call CheckDefFailure(["let x += 1"], 'E1020:') 794 call CheckDefFailure(["let x = x + 1"], 'E1001:') 795 call CheckDefExecFailure("let x = g:anint.member", 'E715:') 796 call CheckDefExecFailure("let x = g:dict_empty.member", 'E716:') 797enddef 798 799def Test_expr_member() 800 assert_equal(1, g:dict_one.one) 801 802 call CheckDefFailure(["let x = g:dict_one.#$!"], 'E1002:') 803enddef 804 805def Test_expr7_option() 806 " option 807 set ts=11 808 assert_equal(11, &ts) 809 &ts = 9 810 assert_equal(9, &ts) 811 set ts=8 812 set grepprg=some\ text 813 assert_equal('some text', &grepprg) 814 &grepprg = test_null_string() 815 assert_equal('', &grepprg) 816 set grepprg& 817enddef 818 819def Test_expr7_environment() 820 " environment variable 821 assert_equal('testvar', $TESTVAR) 822 assert_equal('', $ASDF_ASD_XXX) 823 824 call CheckDefFailure(["let x = $$$"], 'E1002:') 825enddef 826 827def Test_expr7_register() 828 @a = 'register a' 829 assert_equal('register a', @a) 830enddef 831 832def Test_expr7_parens() 833 " (expr) 834 assert_equal(4, (6 * 4) / 6) 835 assert_equal(0, 6 * ( 4 / 6 )) 836 837 assert_equal(6, +6) 838 assert_equal(-6, -6) 839 assert_equal(6, --6) 840 assert_equal(6, -+-6) 841 assert_equal(-6, ---6) 842enddef 843 844def Test_expr7_negate() 845 assert_equal(-99, -99) 846 assert_equal(99, --99) 847 let nr = 88 848 assert_equal(-88, -nr) 849 assert_equal(88, --nr) 850enddef 851 852def Echo(arg: any): string 853 return arg 854enddef 855 856def s:EchoArg(arg: any): string 857 return arg 858enddef 859 860def Test_expr7_call() 861 assert_equal('yes', 'yes'->Echo()) 862 assert_equal('yes', 'yes'->s:EchoArg()) 863 864 call CheckDefFailure(["let x = 'yes'->Echo"], 'E107:') 865enddef 866 867 868def Test_expr7_not() 869 assert_equal(true, !'') 870 assert_equal(true, ![]) 871 assert_equal(false, !'asdf') 872 assert_equal(false, ![2]) 873 assert_equal(true, !!'asdf') 874 assert_equal(true, !![2]) 875 876 assert_equal(true, !test_null_partial()) 877 assert_equal(false, !{-> 'yes'}) 878 879 assert_equal(true, !test_null_dict()) 880 assert_equal(true, !{}) 881 assert_equal(false, !{'yes': 'no'}) 882 883 if has('channel') 884 assert_equal(true, !test_null_job()) 885 assert_equal(true, !test_null_channel()) 886 endif 887 888 assert_equal(true, !test_null_blob()) 889 assert_equal(true, !0z) 890 assert_equal(false, !0z01) 891 892 assert_equal(true, !test_void()) 893 assert_equal(true, !test_unknown()) 894enddef 895 896func Test_expr7_fails() 897 call CheckDefFailure(["let x = (12"], "E110:") 898 899 call CheckDefFailure(["let x = -'xx'"], "E1030:") 900 call CheckDefFailure(["let x = +'xx'"], "E1030:") 901 call CheckDefFailure(["let x = -0z12"], "E974:") 902 call CheckDefExecFailure("let x = -[8]", "E39:") 903 call CheckDefExecFailure("let x = -{'a': 1}", "E39:") 904 905 call CheckDefFailure(["let x = @"], "E1002:") 906 call CheckDefFailure(["let x = @<"], "E354:") 907 908 call CheckDefFailure(["let x = [1, 2"], "E697:") 909 call CheckDefFailure(["let x = [notfound]"], "E1001:") 910 911 call CheckDefFailure(["let x = { -> 123) }"], "E451:") 912 call CheckDefFailure(["let x = 123->{x -> x + 5) }"], "E451:") 913 914 call CheckDefFailure(["let x = ¬exist"], 'E113:') 915 call CheckDefFailure(["&grepprg = [343]"], 'E1013:') 916 917 call CheckDefExecFailure("echo s:doesnt_exist", 'E121:') 918 call CheckDefExecFailure("echo g:doesnt_exist", 'E121:') 919 920 call CheckDefFailure(["echo a:somevar"], 'E1075:') 921 call CheckDefFailure(["echo l:somevar"], 'E1075:') 922 call CheckDefFailure(["echo x:somevar"], 'E1075:') 923 924 call CheckDefExecFailure("let x = +g:astring", 'E1030:') 925 call CheckDefExecFailure("let x = +g:ablob", 'E974:') 926 call CheckDefExecFailure("let x = +g:alist", 'E745:') 927 call CheckDefExecFailure("let x = +g:adict", 'E728:') 928 929 call CheckDefFailure(["let x = ''", "let y = x.memb"], 'E715:') 930 931 call CheckDefExecFailure("[1, 2->len()", 'E492:') 932 call CheckDefExecFailure("#{a: 1->len()", 'E488:') 933 call CheckDefExecFailure("{'a': 1->len()", 'E492:') 934endfunc 935 936let g:Funcrefs = [function('add')] 937 938func CallMe(arg) 939 return a:arg 940endfunc 941 942func CallMe2(one, two) 943 return a:one .. a:two 944endfunc 945 946def Test_expr7_trailing() 947 " user function call 948 assert_equal(123, g:CallMe(123)) 949 assert_equal(123, g:CallMe( 123)) 950 assert_equal(123, g:CallMe(123 )) 951 assert_equal('yesno', g:CallMe2('yes', 'no')) 952 assert_equal('yesno', g:CallMe2( 'yes', 'no' )) 953 assert_equal('nothing', g:CallMe('nothing')) 954 955 " partial call 956 let Part = function('g:CallMe') 957 assert_equal('yes', Part('yes')) 958 959 " funcref call, using list index 960 let l = [] 961 g:Funcrefs[0](l, 2) 962 assert_equal([2], l) 963 964 " method call 965 l = [2, 5, 6] 966 l->map({k, v -> k + v}) 967 assert_equal([2, 6, 8], l) 968 969 " lambda method call 970 l = [2, 5] 971 l->{l -> add(l, 8)}() 972 assert_equal([2, 5, 8], l) 973 974 " dict member 975 let d = #{key: 123} 976 assert_equal(123, d.key) 977enddef 978 979 980func Test_expr7_trailing_fails() 981 call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107') 982 call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274') 983endfunc 984 985func Test_expr_fails() 986 call CheckDefFailure(["let x = '1'is2"], 'E488:') 987 call CheckDefFailure(["let x = '1'isnot2"], 'E488:') 988 989 call CheckDefExecFailure("CallMe ('yes')", 'E492:') 990 call CheckDefFailure(["CallMe2('yes','no')"], 'E1069:') 991 call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:') 992 993 call CheckDefFailure(["v:nosuch += 3"], 'E1001:') 994 call CheckDefFailure(["let v:statusmsg = ''"], 'E1064:') 995 call CheckDefFailure(["let asdf = v:nosuch"], 'E1001:') 996 997 call CheckDefFailure(["echo len('asdf'"], 'E110:') 998 call CheckDefFailure(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:') 999 call CheckDefFailure(["echo doesnotexist()"], 'E117:') 1000endfunc 1001