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