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