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 " TODO: a line break here doesn't work 489" assert_equal('hello 123', 'hello ' .. 490" 123) 491 assert_equal('hello 123', 'hello ' .. 123) 492 assert_equal('123 hello', 123 .. ' hello') 493 assert_equal('123456', 123 .. 456) 494 495 assert_equal([1, 2, 3, 4], [1, 2] + [3, 4]) 496 assert_equal(0z11223344, 0z1122 + 0z3344) 497 assert_equal(0z112201ab, 0z1122 + g:ablob) 498 assert_equal(0z01ab3344, g:ablob + 0z3344) 499 assert_equal(0z01ab01ab, g:ablob + g:ablob) 500enddef 501 502def Test_expr5_float() 503 if !has('float') 504 MissingFeature 'float' 505 else 506 assert_equal(66.0, 60.0 + 6.0) 507 assert_equal(66.0, 60.0 + 6) 508 assert_equal(66.0, 60 + 509 6.0) 510 assert_equal(5.1, g:afloat + 5) 511 assert_equal(8.1, 8 + g:afloat) 512 assert_equal(10.1, g:anint + g:afloat) 513 assert_equal(10.1, g:afloat + g:anint) 514 515 assert_equal(54.0, 60.0 - 6.0) 516 assert_equal(54.0, 60.0 - 6) 517 assert_equal(54.0, 60 - 6.0) 518 assert_equal(-4.9, g:afloat - 5) 519 assert_equal(7.9, 8 - g:afloat) 520 assert_equal(9.9, g:anint - g:afloat) 521 assert_equal(-9.9, g:afloat - g:anint) 522 endif 523enddef 524 525func Test_expr5_fails() 526 let msg = "white space required before and after '+'" 527 call CheckDefFailure(["let x = 1+2"], msg) 528 call CheckDefFailure(["let x = 1 +2"], msg) 529 call CheckDefFailure(["let x = 1+ 2"], msg) 530 531 let msg = "white space required before and after '-'" 532 call CheckDefFailure(["let x = 1-2"], msg) 533 call CheckDefFailure(["let x = 1 -2"], msg) 534 call CheckDefFailure(["let x = 1- 2"], msg) 535 536 let msg = "white space required before and after '..'" 537 call CheckDefFailure(["let x = '1'..'2'"], msg) 538 call CheckDefFailure(["let x = '1' ..'2'"], msg) 539 call CheckDefFailure(["let x = '1'.. '2'"], msg) 540 541 call CheckDefFailure(["let x = 0z1122 + 33"], 'E1035') 542 call CheckDefFailure(["let x = 0z1122 + [3]"], 'E1035') 543 call CheckDefFailure(["let x = 0z1122 + 'asd'"], 'E1035') 544 call CheckDefFailure(["let x = 33 + 0z1122"], 'E1035') 545 call CheckDefFailure(["let x = [3] + 0z1122"], 'E1035') 546 call CheckDefFailure(["let x = 'asdf' + 0z1122"], 'E1035') 547 call CheckDefFailure(["let x = 6 + xxx"], 'E1001') 548endfunc 549 550" test multiply, divide, modulo 551def Test_expr6() 552 assert_equal(36, 6 * 6) 553 assert_equal(24, 6 * 554 g:alsoint) 555 assert_equal(24, g:alsoint * 6) 556 assert_equal(40, g:anint * g:alsoint) 557 558 assert_equal(10, 60 / 6) 559 assert_equal(6, 60 / 560 g:anint) 561 assert_equal(1, g:anint / 6) 562 assert_equal(2, g:anint / g:alsoint) 563 564 assert_equal(5, 11 % 6) 565 assert_equal(4, g:anint % 6) 566 assert_equal(3, 13 % 567 g:anint) 568 assert_equal(2, g:anint % g:alsoint) 569 570 assert_equal(4, 6 * 4 / 6) 571 572 let x = [2] 573 let y = [3] 574 assert_equal(5, x[0] + y[0]) 575 assert_equal(6, x[0] * y[0]) 576 if has('float') 577 let xf = [2.0] 578 let yf = [3.0] 579 assert_equal(5.0, xf[0] + yf[0]) 580 assert_equal(6.0, xf[0] * yf[0]) 581 endif 582 583 call CheckDefFailure(["let x = 6 * xxx"], 'E1001') 584enddef 585 586def Test_expr6_float() 587 if !has('float') 588 MissingFeature 'float' 589 else 590 assert_equal(36.0, 6.0 * 6) 591 assert_equal(36.0, 6 * 592 6.0) 593 assert_equal(36.0, 6.0 * 6.0) 594 assert_equal(1.0, g:afloat * g:anint) 595 596 assert_equal(10.0, 60 / 6.0) 597 assert_equal(10.0, 60.0 / 598 6) 599 assert_equal(10.0, 60.0 / 6.0) 600 assert_equal(0.01, g:afloat / g:anint) 601 602 assert_equal(4.0, 6.0 * 4 / 6) 603 assert_equal(4.0, 6 * 604 4.0 / 605 6) 606 assert_equal(4.0, 6 * 4 / 6.0) 607 assert_equal(4.0, 6.0 * 4.0 / 6) 608 assert_equal(4.0, 6 * 4.0 / 6.0) 609 assert_equal(4.0, 6.0 * 4 / 6.0) 610 assert_equal(4.0, 6.0 * 4.0 / 6.0) 611 612 assert_equal(4.0, 6.0 * 4.0 / 6.0) 613 endif 614enddef 615 616func Test_expr6_fails() 617 let msg = "white space required before and after '*'" 618 call CheckDefFailure(["let x = 1*2"], msg) 619 call CheckDefFailure(["let x = 1 *2"], msg) 620 call CheckDefFailure(["let x = 1* 2"], msg) 621 622 let msg = "white space required before and after '/'" 623 call CheckDefFailure(["let x = 1/2"], msg) 624 call CheckDefFailure(["let x = 1 /2"], msg) 625 call CheckDefFailure(["let x = 1/ 2"], msg) 626 627 let msg = "white space required before and after '%'" 628 call CheckDefFailure(["let x = 1%2"], msg) 629 call CheckDefFailure(["let x = 1 %2"], msg) 630 call CheckDefFailure(["let x = 1% 2"], msg) 631 632 call CheckDefFailure(["let x = '1' * '2'"], 'E1036:') 633 call CheckDefFailure(["let x = '1' / '2'"], 'E1036:') 634 call CheckDefFailure(["let x = '1' % '2'"], 'E1035:') 635 636 call CheckDefFailure(["let x = 0z01 * 0z12"], 'E1036:') 637 call CheckDefFailure(["let x = 0z01 / 0z12"], 'E1036:') 638 call CheckDefFailure(["let x = 0z01 % 0z12"], 'E1035:') 639 640 call CheckDefFailure(["let x = [1] * [2]"], 'E1036:') 641 call CheckDefFailure(["let x = [1] / [2]"], 'E1036:') 642 call CheckDefFailure(["let x = [1] % [2]"], 'E1035:') 643 644 call CheckDefFailure(["let x = #{one: 1} * #{two: 2}"], 'E1036:') 645 call CheckDefFailure(["let x = #{one: 1} / #{two: 2}"], 'E1036:') 646 call CheckDefFailure(["let x = #{one: 1} % #{two: 2}"], 'E1035:') 647 648 call CheckDefFailure(["let x = 0xff[1]"], 'E714:') 649 if has('float') 650 call CheckDefFailure(["let x = 0.7[1]"], 'E714:') 651 endif 652endfunc 653 654func Test_expr6_float_fails() 655 CheckFeature float 656 call CheckDefFailure(["let x = 1.0 % 2"], 'E1035:') 657endfunc 658 659" define here to use old style parsing 660if has('float') 661 let g:float_zero = 0.0 662 let g:float_neg = -9.8 663 let g:float_big = 9.9e99 664endif 665let g:blob_empty = 0z 666let g:blob_one = 0z01 667let g:blob_long = 0z0102.0304 668 669let g:string_empty = '' 670let g:string_short = 'x' 671let g:string_long = 'abcdefghijklm' 672let g:string_special = "ab\ncd\ref\ekk" 673 674let g:special_true = v:true 675let g:special_false = v:false 676let g:special_null = v:null 677let g:special_none = v:none 678 679let g:list_empty = [] 680let g:list_mixed = [1, 'b', v:false] 681 682let g:dict_empty = {} 683let g:dict_one = #{one: 1} 684 685let $TESTVAR = 'testvar' 686 687" test low level expression 688def Test_expr7_number() 689 " number constant 690 assert_equal(0, 0) 691 assert_equal(654, 0654) 692 693 assert_equal(6, 0x6) 694 assert_equal(15, 0xf) 695 assert_equal(255, 0xff) 696enddef 697 698def Test_expr7_float() 699 " float constant 700 if !has('float') 701 MissingFeature 'float' 702 else 703 assert_equal(g:float_zero, .0) 704 assert_equal(g:float_zero, 0.0) 705 assert_equal(g:float_neg, -9.8) 706 assert_equal(g:float_big, 9.9e99) 707 endif 708enddef 709 710def Test_expr7_blob() 711 " blob constant 712 assert_equal(g:blob_empty, 0z) 713 assert_equal(g:blob_one, 0z01) 714 assert_equal(g:blob_long, 0z0102.0304) 715 716 call CheckDefFailure(["let x = 0z123"], 'E973:') 717enddef 718 719def Test_expr7_string() 720 " string constant 721 assert_equal(g:string_empty, '') 722 assert_equal(g:string_empty, "") 723 assert_equal(g:string_short, 'x') 724 assert_equal(g:string_short, "x") 725 assert_equal(g:string_long, 'abcdefghijklm') 726 assert_equal(g:string_long, "abcdefghijklm") 727 assert_equal(g:string_special, "ab\ncd\ref\ekk") 728 729 call CheckDefFailure(['let x = "abc'], 'E114:') 730 call CheckDefFailure(["let x = 'abc"], 'E115:') 731enddef 732 733def Test_expr7_vimvar() 734 let old: list<string> = v:oldfiles 735 let compl: dict<any> = v:completed_item 736 737 call CheckDefFailure(["let old: list<number> = v:oldfiles"], 'E1013: type mismatch, expected list<number> but got list<string>') 738 call CheckDefFailure(["let old: dict<number> = v:completed_item"], 'E1013: type mismatch, expected dict<number> but got dict<any>') 739enddef 740 741def Test_expr7_special() 742 " special constant 743 assert_equal(g:special_true, true) 744 assert_equal(g:special_false, false) 745 assert_equal(g:special_true, v:true) 746 assert_equal(g:special_false, v:false) 747 assert_equal(g:special_null, v:null) 748 assert_equal(g:special_none, v:none) 749 750 call CheckDefFailure(['v:true = true'], 'E46:') 751 call CheckDefFailure(['v:true = false'], 'E46:') 752 call CheckDefFailure(['v:false = true'], 'E46:') 753 call CheckDefFailure(['v:null = 11'], 'E46:') 754 call CheckDefFailure(['v:none = 22'], 'E46:') 755enddef 756 757def Test_expr7_list() 758 " list 759 assert_equal(g:list_empty, []) 760 assert_equal(g:list_empty, [ ]) 761 assert_equal(g:list_mixed, [1, 'b', false]) 762 assert_equal('b', g:list_mixed[1]) 763 764 call CheckDefExecFailure(["let x = g:anint[3]"], 'E714:') 765 call CheckDefFailure(["let x = g:list_mixed[xxx]"], 'E1001:') 766 call CheckDefExecFailure(["let x = g:list_mixed['xx']"], 'E39:') 767 call CheckDefFailure(["let x = g:list_mixed[0"], 'E111:') 768 call CheckDefExecFailure(["let x = g:list_empty[3]"], 'E684:') 769enddef 770 771def Test_expr7_lambda() 772 " lambda 773 let La = { -> 'result'} 774 assert_equal('result', La()) 775 assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val})) 776enddef 777 778def Test_expr7_dict() 779 " dictionary 780 assert_equal(g:dict_empty, {}) 781 assert_equal(g:dict_empty, { }) 782 assert_equal(g:dict_one, {'one': 1}) 783 let key = 'one' 784 let val = 1 785 assert_equal(g:dict_one, {key: val}) 786 787 call CheckDefFailure(["let x = #{8: 8}"], 'E1014:') 788 call CheckDefFailure(["let x = #{xxx}"], 'E720:') 789 call CheckDefFailure(["let x = #{xxx: 1", "let y = 2"], 'E722:') 790 call CheckDefFailure(["let x = #{xxx: 1,"], 'E723:') 791 call CheckDefFailure(["let x = {'a': xxx}"], 'E1001:') 792 call CheckDefFailure(["let x = {xxx: 8}"], 'E1001:') 793 call CheckDefFailure(["let x = #{a: 1, a: 2}"], 'E721:') 794 call CheckDefFailure(["let x = #"], 'E1015:') 795 call CheckDefFailure(["let x += 1"], 'E1020:') 796 call CheckDefFailure(["let x = x + 1"], 'E1001:') 797 call CheckDefExecFailure(["let x = g:anint.member"], 'E715:') 798 call CheckDefExecFailure(["let x = g:dict_empty.member"], 'E716:') 799enddef 800 801def Test_expr_member() 802 assert_equal(1, g:dict_one.one) 803 804 call CheckDefFailure(["let x = g:dict_one.#$!"], 'E1002:') 805enddef 806 807def Test_expr7_option() 808 " option 809 set ts=11 810 assert_equal(11, &ts) 811 &ts = 9 812 assert_equal(9, &ts) 813 set ts=8 814 set grepprg=some\ text 815 assert_equal('some text', &grepprg) 816 &grepprg = test_null_string() 817 assert_equal('', &grepprg) 818 set grepprg& 819enddef 820 821def Test_expr7_environment() 822 " environment variable 823 assert_equal('testvar', $TESTVAR) 824 assert_equal('', $ASDF_ASD_XXX) 825 826 call CheckDefFailure(["let x = $$$"], 'E1002:') 827enddef 828 829def Test_expr7_register() 830 @a = 'register a' 831 assert_equal('register a', @a) 832enddef 833 834def Test_expr7_parens() 835 " (expr) 836 assert_equal(4, (6 * 4) / 6) 837 assert_equal(0, 6 * ( 4 / 6 )) 838 839 assert_equal(6, +6) 840 assert_equal(-6, -6) 841 assert_equal(6, --6) 842 assert_equal(6, -+-6) 843 assert_equal(-6, ---6) 844enddef 845 846def Test_expr7_negate() 847 assert_equal(-99, -99) 848 assert_equal(99, --99) 849 let nr = 88 850 assert_equal(-88, -nr) 851 assert_equal(88, --nr) 852enddef 853 854def Echo(arg: any): string 855 return arg 856enddef 857 858def s:EchoArg(arg: any): string 859 return arg 860enddef 861 862def Test_expr7_call() 863 assert_equal('yes', 'yes'->Echo()) 864 assert_equal('yes', 'yes'->s:EchoArg()) 865 866 call CheckDefFailure(["let x = 'yes'->Echo"], 'E107:') 867enddef 868 869 870def Test_expr7_not() 871 assert_equal(true, !'') 872 assert_equal(true, ![]) 873 assert_equal(false, !'asdf') 874 assert_equal(false, ![2]) 875 assert_equal(true, !!'asdf') 876 assert_equal(true, !![2]) 877 878 assert_equal(true, !test_null_partial()) 879 assert_equal(false, !{-> 'yes'}) 880 881 assert_equal(true, !test_null_dict()) 882 assert_equal(true, !{}) 883 assert_equal(false, !{'yes': 'no'}) 884 885 if has('channel') 886 assert_equal(true, !test_null_job()) 887 assert_equal(true, !test_null_channel()) 888 endif 889 890 assert_equal(true, !test_null_blob()) 891 assert_equal(true, !0z) 892 assert_equal(false, !0z01) 893 894 assert_equal(true, !test_void()) 895 assert_equal(true, !test_unknown()) 896enddef 897 898func Test_expr7_fails() 899 call CheckDefFailure(["let x = (12"], "E110:") 900 901 call CheckDefFailure(["let x = -'xx'"], "E1030:") 902 call CheckDefFailure(["let x = +'xx'"], "E1030:") 903 call CheckDefFailure(["let x = -0z12"], "E974:") 904 call CheckDefExecFailure(["let x = -[8]"], "E39:") 905 call CheckDefExecFailure(["let x = -{'a': 1}"], "E39:") 906 907 call CheckDefFailure(["let x = @"], "E1002:") 908 call CheckDefFailure(["let x = @<"], "E354:") 909 910 call CheckDefFailure(["let x = [1, 2"], "E697:") 911 call CheckDefFailure(["let x = [notfound]"], "E1001:") 912 913 call CheckDefFailure(["let x = { -> 123) }"], "E451:") 914 call CheckDefFailure(["let x = 123->{x -> x + 5) }"], "E451:") 915 916 call CheckDefFailure(["let x = ¬exist"], 'E113:') 917 call CheckDefFailure(["&grepprg = [343]"], 'E1013:') 918 919 call CheckDefExecFailure(["echo s:doesnt_exist"], 'E121:') 920 call CheckDefExecFailure(["echo g:doesnt_exist"], 'E121:') 921 922 call CheckDefFailure(["echo a:somevar"], 'E1075:') 923 call CheckDefFailure(["echo l:somevar"], 'E1075:') 924 call CheckDefFailure(["echo x:somevar"], 'E1075:') 925 926 call CheckDefExecFailure(["let x = +g:astring"], 'E1030:') 927 call CheckDefExecFailure(["let x = +g:ablob"], 'E974:') 928 call CheckDefExecFailure(["let x = +g:alist"], 'E745:') 929 call CheckDefExecFailure(["let x = +g:adict"], 'E728:') 930 931 call CheckDefFailure(["let x = ''", "let y = x.memb"], 'E715:') 932 933 call CheckDefExecFailure(["[1, 2->len()"], 'E492:') 934 call CheckDefExecFailure(["#{a: 1->len()"], 'E488:') 935 call CheckDefExecFailure(["{'a': 1->len()"], 'E492:') 936endfunc 937 938let g:Funcrefs = [function('add')] 939 940func CallMe(arg) 941 return a:arg 942endfunc 943 944func CallMe2(one, two) 945 return a:one .. a:two 946endfunc 947 948def Test_expr7_trailing() 949 " user function call 950 assert_equal(123, g:CallMe(123)) 951 assert_equal(123, g:CallMe( 123)) 952 assert_equal(123, g:CallMe(123 )) 953 assert_equal('yesno', g:CallMe2('yes', 'no')) 954 assert_equal('yesno', g:CallMe2( 'yes', 'no' )) 955 assert_equal('nothing', g:CallMe('nothing')) 956 957 " partial call 958 let Part = function('g:CallMe') 959 assert_equal('yes', Part('yes')) 960 961 " funcref call, using list index 962 let l = [] 963 g:Funcrefs[0](l, 2) 964 assert_equal([2], l) 965 966 " method call 967 l = [2, 5, 6] 968 l->map({k, v -> k + v}) 969 assert_equal([2, 6, 8], l) 970 971 " lambda method call 972 l = [2, 5] 973 l->{l -> add(l, 8)}() 974 assert_equal([2, 5, 8], l) 975 976 " dict member 977 let d = #{key: 123} 978 assert_equal(123, d.key) 979enddef 980 981 982func Test_expr7_trailing_fails() 983 call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107') 984 call CheckDefFailure(['let l = [2]', 'l->{l -> add(l, 8)} ()'], 'E274') 985endfunc 986 987func Test_expr_fails() 988 call CheckDefFailure(["let x = '1'is2"], 'E488:') 989 call CheckDefFailure(["let x = '1'isnot2"], 'E488:') 990 991 call CheckDefExecFailure(["CallMe ('yes')"], 'E492:') 992 call CheckDefFailure(["CallMe2('yes','no')"], 'E1069:') 993 call CheckDefFailure(["CallMe2('yes' , 'no')"], 'E1068:') 994 995 call CheckDefFailure(["v:nosuch += 3"], 'E1001:') 996 call CheckDefFailure(["let v:statusmsg = ''"], 'E1064:') 997 call CheckDefFailure(["let asdf = v:nosuch"], 'E1001:') 998 999 call CheckDefFailure(["echo len('asdf'"], 'E110:') 1000 call CheckDefFailure(["echo Func0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789()"], 'E1011:') 1001 call CheckDefFailure(["echo doesnotexist()"], 'E117:') 1002endfunc 1003