Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1 | " Tests for Vim9 script expressions |
| 2 | |
| 3 | source check.vim |
| 4 | |
| 5 | " Check that "line" inside ":def" results in an "error" message. |
| 6 | func 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') |
| 10 | endfunc |
| 11 | |
| 12 | func CheckDefFailureList(lines, error) |
| 13 | call writefile(['def! Func()'] + a:lines + ['enddef'], 'Xdef') |
| 14 | call assert_fails('so Xdef', a:error, string(a:lines)) |
| 15 | call delete('Xdef') |
| 16 | endfunc |
| 17 | |
| 18 | " test cond ? expr : expr |
| 19 | def Test_expr1() |
| 20 | assert_equal('one', true ? 'one' : 'two') |
| 21 | assert_equal('one', 1 ? 'one' : 'two') |
| 22 | assert_equal('one', 0.1 ? 'one' : 'two') |
| 23 | assert_equal('one', 'x' ? 'one' : 'two') |
| 24 | " assert_equal('one', 0z1234 ? 'one' : 'two') |
| 25 | assert_equal('one', [0] ? 'one' : 'two') |
| 26 | " assert_equal('one', #{x: 0} ? 'one' : 'two') |
| 27 | let var = 1 |
| 28 | assert_equal('one', var ? 'one' : 'two') |
| 29 | |
| 30 | assert_equal('two', false ? 'one' : 'two') |
| 31 | assert_equal('two', 0 ? 'one' : 'two') |
| 32 | assert_equal('two', 0.0 ? 'one' : 'two') |
| 33 | assert_equal('two', '' ? 'one' : 'two') |
| 34 | " assert_equal('one', 0z ? 'one' : 'two') |
| 35 | assert_equal('two', [] ? 'one' : 'two') |
| 36 | " assert_equal('two', {} ? 'one' : 'two') |
| 37 | var = 0 |
| 38 | assert_equal('two', var ? 'one' : 'two') |
| 39 | enddef |
| 40 | |
| 41 | func Test_expr1_fails() |
| 42 | call CheckDefFailure("let x = 1 ? 'one'", "Missing ':' after '?'") |
| 43 | |
| 44 | let msg = "white space required before and after '?'" |
| 45 | call CheckDefFailure("let x = 1? 'one' : 'two'", msg) |
| 46 | call CheckDefFailure("let x = 1 ?'one' : 'two'", msg) |
| 47 | call CheckDefFailure("let x = 1?'one' : 'two'", msg) |
| 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 | endfunc |
| 54 | |
| 55 | " TODO: define inside test function |
| 56 | def Record(val: any): any |
| 57 | g:vals->add(val) |
| 58 | return val |
| 59 | enddef |
| 60 | |
| 61 | " test || |
| 62 | def Test_expr2() |
| 63 | assert_equal(2, 2 || 0) |
| 64 | assert_equal(7, 0 || 0 || 7) |
| 65 | assert_equal(0, 0 || 0) |
| 66 | assert_equal('', 0 || '') |
| 67 | |
| 68 | g:vals = [] |
| 69 | assert_equal(3, Record(3) || Record(1)) |
| 70 | assert_equal([3], g:vals) |
| 71 | |
| 72 | g:vals = [] |
| 73 | assert_equal(5, Record(0) || Record(5)) |
| 74 | assert_equal([0, 5], g:vals) |
| 75 | |
| 76 | g:vals = [] |
| 77 | assert_equal(4, Record(0) || Record(4) || Record(0)) |
| 78 | assert_equal([0, 4], g:vals) |
| 79 | |
| 80 | g:vals = [] |
| 81 | assert_equal(0, Record([]) || Record('') || Record(0)) |
| 82 | assert_equal([[], '', 0], g:vals) |
| 83 | enddef |
| 84 | |
| 85 | func Test_expr2_fails() |
| 86 | let msg = "white space required before and after '||'" |
| 87 | call CheckDefFailure("let x = 1||2", msg) |
| 88 | call CheckDefFailure("let x = 1 ||2", msg) |
| 89 | call CheckDefFailure("let x = 1|| 2", msg) |
| 90 | endfunc |
| 91 | |
| 92 | " test && |
| 93 | def Test_expr3() |
| 94 | assert_equal(0, 2 && 0) |
| 95 | assert_equal(0, 0 && 0 && 7) |
| 96 | assert_equal(7, 2 && 3 && 7) |
| 97 | assert_equal(0, 0 && 0) |
| 98 | assert_equal(0, 0 && '') |
| 99 | assert_equal('', 8 && '') |
| 100 | |
| 101 | g:vals = [] |
| 102 | assert_equal(1, Record(3) && Record(1)) |
| 103 | assert_equal([3, 1], g:vals) |
| 104 | |
| 105 | g:vals = [] |
| 106 | assert_equal(0, Record(0) && Record(5)) |
| 107 | assert_equal([0], g:vals) |
| 108 | |
| 109 | g:vals = [] |
| 110 | assert_equal(0, Record(0) && Record(4) && Record(0)) |
| 111 | assert_equal([0], g:vals) |
| 112 | |
| 113 | g:vals = [] |
| 114 | assert_equal(0, Record(8) && Record(4) && Record(0)) |
| 115 | assert_equal([8, 4, 0], g:vals) |
| 116 | |
| 117 | g:vals = [] |
| 118 | assert_equal(0, Record([1]) && Record('z') && Record(0)) |
| 119 | assert_equal([[1], 'z', 0], g:vals) |
| 120 | enddef |
| 121 | |
| 122 | func Test_expr3_fails() |
| 123 | let msg = "white space required before and after '&&'" |
| 124 | call CheckDefFailure("let x = 1&&2", msg) |
| 125 | call CheckDefFailure("let x = 1 &&2", msg) |
| 126 | call CheckDefFailure("let x = 1&& 2", msg) |
| 127 | endfunc |
| 128 | |
| 129 | let atrue = v:true |
| 130 | let afalse = v:false |
| 131 | let anone = v:none |
| 132 | let anull = v:null |
| 133 | let anint = 10 |
| 134 | let alsoint = 4 |
| 135 | if has('float') |
| 136 | let afloat = 0.1 |
| 137 | endif |
| 138 | let astring = 'asdf' |
| 139 | let ablob = 0z01ab |
| 140 | let alist = [2, 3, 4] |
| 141 | let adict = #{aaa: 2, bbb: 8} |
| 142 | |
| 143 | " test == comperator |
| 144 | def Test_expr4_equal() |
| 145 | assert_equal(true, true == true) |
| 146 | assert_equal(false, true == false) |
| 147 | assert_equal(true, true == g:atrue) |
| 148 | assert_equal(false, g:atrue == false) |
| 149 | |
| 150 | assert_equal(true, v:none == v:none) |
| 151 | assert_equal(false, v:none == v:null) |
| 152 | assert_equal(true, g:anone == v:none) |
| 153 | assert_equal(false, v:none == g:anull) |
| 154 | |
| 155 | assert_equal(false, 2 == 0) |
| 156 | assert_equal(true, 61 == 61) |
| 157 | assert_equal(true, g:anint == 10) |
| 158 | assert_equal(false, 61 == g:anint) |
| 159 | |
| 160 | if has('float') |
| 161 | assert_equal(true, 0.3 == 0.3) |
| 162 | assert_equal(false, 0.4 == 0.3) |
| 163 | assert_equal(true, 0.1 == g:afloat) |
| 164 | assert_equal(false, g:afloat == 0.3) |
| 165 | |
| 166 | assert_equal(true, 3.0 == 3) |
| 167 | assert_equal(true, 3 == 3.0) |
| 168 | assert_equal(false, 3.1 == 3) |
| 169 | assert_equal(false, 3 == 3.1) |
| 170 | endif |
| 171 | |
| 172 | assert_equal(true, 'abc' == 'abc') |
| 173 | assert_equal(false, 'xyz' == 'abc') |
| 174 | assert_equal(true, g:astring == 'asdf') |
| 175 | assert_equal(false, 'xyz' == g:astring) |
| 176 | |
| 177 | assert_equal(false, 'abc' == 'ABC') |
| 178 | set ignorecase |
| 179 | assert_equal(false, 'abc' == 'ABC') |
| 180 | set noignorecase |
| 181 | |
| 182 | assert_equal(true, 0z3f == 0z3f) |
| 183 | assert_equal(false, 0z3f == 0z4f) |
| 184 | assert_equal(true, g:ablob == 0z01ab) |
| 185 | assert_equal(false, 0z3f == g:ablob) |
| 186 | |
| 187 | assert_equal(true, [1, 2, 3] == [1, 2, 3]) |
| 188 | assert_equal(false, [1, 2, 3] == [2, 3, 1]) |
| 189 | assert_equal(true, [2, 3, 4] == g:alist) |
| 190 | assert_equal(false, g:alist == [2, 3, 1]) |
| 191 | assert_equal(false, [1, 2, 3] == []) |
| 192 | assert_equal(false, [1, 2, 3] == ['1', '2', '3']) |
| 193 | |
| 194 | assert_equal(true, #{one: 1, two: 2} == #{one: 1, two: 2}) |
| 195 | assert_equal(false, #{one: 1, two: 2} == #{one: 2, two: 2}) |
| 196 | assert_equal(false, #{one: 1, two: 2} == #{two: 2}) |
| 197 | assert_equal(false, #{one: 1, two: 2} == #{}) |
| 198 | assert_equal(true, g:adict == #{bbb: 8, aaa: 2}) |
| 199 | assert_equal(false, #{ccc: 9, aaa: 2} == g:adict) |
| 200 | |
| 201 | assert_equal(true, function('Test_expr4_equal') == function('Test_expr4_equal')) |
| 202 | assert_equal(false, function('Test_expr4_equal') == function('Test_expr4_is')) |
| 203 | |
| 204 | assert_equal(true, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [123])) |
| 205 | assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_is', [123])) |
| 206 | assert_equal(false, function('Test_expr4_equal', [123]) == function('Test_expr4_equal', [999])) |
| 207 | enddef |
| 208 | |
| 209 | " test != comperator |
| 210 | def Test_expr4_notequal() |
| 211 | assert_equal(false, true != true) |
| 212 | assert_equal(true, true != false) |
| 213 | assert_equal(false, true != g:atrue) |
| 214 | assert_equal(true, g:atrue != false) |
| 215 | |
| 216 | assert_equal(false, v:none != v:none) |
| 217 | assert_equal(true, v:none != v:null) |
| 218 | assert_equal(false, g:anone != v:none) |
| 219 | assert_equal(true, v:none != g:anull) |
| 220 | |
| 221 | assert_equal(true, 2 != 0) |
| 222 | assert_equal(false, 55 != 55) |
| 223 | assert_equal(false, g:anint != 10) |
| 224 | assert_equal(true, 61 != g:anint) |
| 225 | |
| 226 | if has('float') |
| 227 | assert_equal(false, 0.3 != 0.3) |
| 228 | assert_equal(true, 0.4 != 0.3) |
| 229 | assert_equal(false, 0.1 != g:afloat) |
| 230 | assert_equal(true, g:afloat != 0.3) |
| 231 | |
| 232 | assert_equal(false, 3.0 != 3) |
| 233 | assert_equal(false, 3 != 3.0) |
| 234 | assert_equal(true, 3.1 != 3) |
| 235 | assert_equal(true, 3 != 3.1) |
| 236 | endif |
| 237 | |
| 238 | assert_equal(false, 'abc' != 'abc') |
| 239 | assert_equal(true, 'xyz' != 'abc') |
| 240 | assert_equal(false, g:astring != 'asdf') |
| 241 | assert_equal(true, 'xyz' != g:astring) |
| 242 | |
| 243 | assert_equal(true, 'abc' != 'ABC') |
| 244 | set ignorecase |
| 245 | assert_equal(true, 'abc' != 'ABC') |
| 246 | set noignorecase |
| 247 | |
| 248 | assert_equal(false, 0z3f != 0z3f) |
| 249 | assert_equal(true, 0z3f != 0z4f) |
| 250 | assert_equal(false, g:ablob != 0z01ab) |
| 251 | assert_equal(true, 0z3f != g:ablob) |
| 252 | |
| 253 | assert_equal(false, [1, 2, 3] != [1, 2, 3]) |
| 254 | assert_equal(true, [1, 2, 3] != [2, 3, 1]) |
| 255 | assert_equal(false, [2, 3, 4] != g:alist) |
| 256 | assert_equal(true, g:alist != [2, 3, 1]) |
| 257 | assert_equal(true, [1, 2, 3] != []) |
| 258 | assert_equal(true, [1, 2, 3] != ['1', '2', '3']) |
| 259 | |
| 260 | assert_equal(false, #{one: 1, two: 2} != #{one: 1, two: 2}) |
| 261 | assert_equal(true, #{one: 1, two: 2} != #{one: 2, two: 2}) |
| 262 | assert_equal(true, #{one: 1, two: 2} != #{two: 2}) |
| 263 | assert_equal(true, #{one: 1, two: 2} != #{}) |
| 264 | assert_equal(false, g:adict != #{bbb: 8, aaa: 2}) |
| 265 | assert_equal(true, #{ccc: 9, aaa: 2} != g:adict) |
| 266 | |
| 267 | assert_equal(false, function('Test_expr4_equal') != function('Test_expr4_equal')) |
| 268 | assert_equal(true, function('Test_expr4_equal') != function('Test_expr4_is')) |
| 269 | |
| 270 | assert_equal(false, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [123])) |
| 271 | assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_is', [123])) |
| 272 | assert_equal(true, function('Test_expr4_equal', [123]) != function('Test_expr4_equal', [999])) |
| 273 | enddef |
| 274 | |
| 275 | " test > comperator |
| 276 | def Test_expr4_greater() |
| 277 | assert_equal(true, 2 > 0) |
| 278 | assert_equal(true, 2 > 1) |
| 279 | assert_equal(false, 2 > 2) |
| 280 | assert_equal(false, 2 > 3) |
| 281 | enddef |
| 282 | |
| 283 | " test >= comperator |
| 284 | def Test_expr4_greaterequal() |
| 285 | assert_equal(true, 2 >= 0) |
| 286 | assert_equal(true, 2 >= 2) |
| 287 | assert_equal(false, 2 >= 3) |
| 288 | enddef |
| 289 | |
| 290 | " test < comperator |
| 291 | def Test_expr4_smaller() |
| 292 | assert_equal(false, 2 < 0) |
| 293 | assert_equal(false, 2 < 2) |
| 294 | assert_equal(true, 2 < 3) |
| 295 | enddef |
| 296 | |
| 297 | " test <= comperator |
| 298 | def Test_expr4_smallerequal() |
| 299 | assert_equal(false, 2 <= 0) |
| 300 | assert_equal(false, 2 <= 1) |
| 301 | assert_equal(true, 2 <= 2) |
| 302 | assert_equal(true, 2 <= 3) |
| 303 | enddef |
| 304 | |
| 305 | " test =~ comperator |
| 306 | def Test_expr4_match() |
| 307 | assert_equal(false, '2' =~ '0') |
| 308 | assert_equal(true, '2' =~ '[0-9]') |
| 309 | enddef |
| 310 | |
| 311 | " test !~ comperator |
| 312 | def Test_expr4_nomatch() |
| 313 | assert_equal(true, '2' !~ '0') |
| 314 | assert_equal(false, '2' !~ '[0-9]') |
| 315 | enddef |
| 316 | |
| 317 | " test is comperator |
| 318 | def Test_expr4_is() |
| 319 | let mylist = [2] |
| 320 | assert_equal(false, mylist is [2]) |
| 321 | let other = mylist |
| 322 | assert_equal(true, mylist is other) |
| 323 | enddef |
| 324 | |
| 325 | " test isnot comperator |
| 326 | def Test_expr4_isnot() |
| 327 | let mylist = [2] |
| 328 | assert_equal(true, '2' isnot '0') |
| 329 | assert_equal(true, mylist isnot [2]) |
| 330 | let other = mylist |
| 331 | assert_equal(false, mylist isnot other) |
| 332 | enddef |
| 333 | |
| 334 | def RetVoid() |
| 335 | let x = 1 |
| 336 | enddef |
| 337 | |
| 338 | func Test_expr4_fails() |
| 339 | let msg = "white space required before and after '>'" |
| 340 | call CheckDefFailure("let x = 1>2", msg) |
| 341 | call CheckDefFailure("let x = 1 >2", msg) |
| 342 | call CheckDefFailure("let x = 1> 2", msg) |
| 343 | |
| 344 | let msg = "white space required before and after '=='" |
| 345 | call CheckDefFailure("let x = 1==2", msg) |
| 346 | call CheckDefFailure("let x = 1 ==2", msg) |
| 347 | call CheckDefFailure("let x = 1== 2", msg) |
| 348 | |
| 349 | let msg = "white space required before and after 'is'" |
| 350 | call CheckDefFailure("let x = '1'is'2'", msg) |
| 351 | call CheckDefFailure("let x = '1' is'2'", msg) |
| 352 | call CheckDefFailure("let x = '1'is '2'", msg) |
| 353 | |
| 354 | let msg = "white space required before and after 'isnot'" |
| 355 | call CheckDefFailure("let x = '1'isnot'2'", msg) |
| 356 | call CheckDefFailure("let x = '1' isnot'2'", msg) |
| 357 | call CheckDefFailure("let x = '1'isnot '2'", msg) |
| 358 | |
| 359 | call CheckDefFailure("let x = 1 is# 2", 'E15:') |
| 360 | call CheckDefFailure("let x = 1 is? 2", 'E15:') |
| 361 | call CheckDefFailure("let x = 1 isnot# 2", 'E15:') |
| 362 | call CheckDefFailure("let x = 1 isnot? 2", 'E15:') |
| 363 | |
| 364 | call CheckDefFailure("let x = 1 == '2'", 'Cannot compare number with string') |
| 365 | call CheckDefFailure("let x = '1' == 2", 'Cannot compare string with number') |
| 366 | call CheckDefFailure("let x = 1 == RetVoid()", 'Cannot use void value') |
| 367 | call CheckDefFailure("let x = RetVoid() == 1", 'Cannot compare void with number') |
| 368 | |
| 369 | call CheckDefFailure("let x = true > false", 'Cannot compare bool with bool') |
| 370 | call CheckDefFailure("let x = true >= false", 'Cannot compare bool with bool') |
| 371 | call CheckDefFailure("let x = true < false", 'Cannot compare bool with bool') |
| 372 | call CheckDefFailure("let x = true <= false", 'Cannot compare bool with bool') |
| 373 | call CheckDefFailure("let x = true =~ false", 'Cannot compare bool with bool') |
| 374 | call CheckDefFailure("let x = true !~ false", 'Cannot compare bool with bool') |
| 375 | call CheckDefFailure("let x = true is false", 'Cannot use "is" with bool') |
| 376 | call CheckDefFailure("let x = true isnot false", 'Cannot use "isnot" with bool') |
| 377 | |
| 378 | call CheckDefFailure("let x = v:none is v:null", 'Cannot use "is" with special') |
| 379 | call CheckDefFailure("let x = v:none isnot v:null", 'Cannot use "isnot" with special') |
| 380 | call CheckDefFailure("let x = 123 is 123", 'Cannot use "is" with number') |
| 381 | call CheckDefFailure("let x = 123 isnot 123", 'Cannot use "isnot" with number') |
| 382 | if has('float') |
| 383 | call CheckDefFailure("let x = 1.3 is 1.3", 'Cannot use "is" with float') |
| 384 | call CheckDefFailure("let x = 1.3 isnot 1.3", 'Cannot use "isnot" with float') |
| 385 | endif |
| 386 | |
| 387 | call CheckDefFailure("let x = 0za1 > 0z34", 'Cannot compare blob with blob') |
| 388 | call CheckDefFailure("let x = 0za1 >= 0z34", 'Cannot compare blob with blob') |
| 389 | call CheckDefFailure("let x = 0za1 < 0z34", 'Cannot compare blob with blob') |
| 390 | call CheckDefFailure("let x = 0za1 <= 0z34", 'Cannot compare blob with blob') |
| 391 | call CheckDefFailure("let x = 0za1 =~ 0z34", 'Cannot compare blob with blob') |
| 392 | call CheckDefFailure("let x = 0za1 !~ 0z34", 'Cannot compare blob with blob') |
| 393 | |
| 394 | call CheckDefFailure("let x = [13] > [88]", 'Cannot compare list with list') |
| 395 | call CheckDefFailure("let x = [13] >= [88]", 'Cannot compare list with list') |
| 396 | call CheckDefFailure("let x = [13] < [88]", 'Cannot compare list with list') |
| 397 | call CheckDefFailure("let x = [13] <= [88]", 'Cannot compare list with list') |
| 398 | call CheckDefFailure("let x = [13] =~ [88]", 'Cannot compare list with list') |
| 399 | call CheckDefFailure("let x = [13] !~ [88]", 'Cannot compare list with list') |
| 400 | endfunc |
| 401 | |
| 402 | " test addition, subtraction, concatenation |
| 403 | def Test_expr5() |
| 404 | assert_equal(66, 60 + 6) |
| 405 | assert_equal(70, 60 + g:anint) |
| 406 | assert_equal(9, g:alsoint + 5) |
| 407 | assert_equal(14, g:alsoint + g:anint) |
| 408 | |
| 409 | assert_equal(54, 60 - 6) |
| 410 | assert_equal(50, 60 - g:anint) |
| 411 | assert_equal(-1, g:alsoint - 5) |
| 412 | assert_equal(-6, g:alsoint - g:anint) |
| 413 | |
| 414 | assert_equal('hello', 'hel' .. 'lo') |
| 415 | assert_equal('hello 123', 'hello ' .. 123) |
| 416 | assert_equal('123 hello', 123 .. ' hello') |
| 417 | assert_equal('123456', 123 .. 456) |
| 418 | enddef |
| 419 | |
| 420 | def Test_expr5_float() |
| 421 | CheckFeature float |
| 422 | assert_equal(66.0, 60.0 + 6.0) |
| 423 | assert_equal(66.0, 60.0 + 6) |
| 424 | assert_equal(66.0, 60 + 6.0) |
| 425 | assert_equal(5.1, g:afloat + 5) |
| 426 | assert_equal(8.1, 8 + g:afloat) |
| 427 | assert_equal(10.1, g:anint + g:afloat) |
| 428 | assert_equal(10.1, g:afloat + g:anint) |
| 429 | |
| 430 | assert_equal(54.0, 60.0 - 6.0) |
| 431 | assert_equal(54.0, 60.0 - 6) |
| 432 | assert_equal(54.0, 60 - 6.0) |
| 433 | assert_equal(-4.9, g:afloat - 5) |
| 434 | assert_equal(7.9, 8 - g:afloat) |
| 435 | assert_equal(9.9, g:anint - g:afloat) |
| 436 | assert_equal(-9.9, g:afloat - g:anint) |
| 437 | enddef |
| 438 | |
| 439 | func Test_expr5_fails() |
| 440 | let msg = "white space required before and after '+'" |
| 441 | call CheckDefFailure("let x = 1+2", msg) |
| 442 | call CheckDefFailure("let x = 1 +2", msg) |
| 443 | call CheckDefFailure("let x = 1+ 2", msg) |
| 444 | |
| 445 | let msg = "white space required before and after '-'" |
| 446 | call CheckDefFailure("let x = 1-2", msg) |
| 447 | call CheckDefFailure("let x = 1 -2", msg) |
| 448 | call CheckDefFailure("let x = 1- 2", msg) |
| 449 | |
| 450 | let msg = "white space required before and after '..'" |
| 451 | call CheckDefFailure("let x = '1'..'2'", msg) |
| 452 | call CheckDefFailure("let x = '1' ..'2'", msg) |
| 453 | call CheckDefFailure("let x = '1'.. '2'", msg) |
| 454 | endfunc |
| 455 | |
| 456 | " test multiply, divide, modulo |
| 457 | def Test_expr6() |
| 458 | assert_equal(36, 6 * 6) |
| 459 | assert_equal(24, 6 * g:alsoint) |
| 460 | assert_equal(24, g:alsoint * 6) |
| 461 | assert_equal(40, g:anint * g:alsoint) |
| 462 | |
| 463 | assert_equal(10, 60 / 6) |
| 464 | assert_equal(6, 60 / g:anint) |
| 465 | assert_equal(1, g:anint / 6) |
| 466 | assert_equal(2, g:anint / g:alsoint) |
| 467 | |
| 468 | assert_equal(5, 11 % 6) |
| 469 | assert_equal(4, g:anint % 6) |
| 470 | assert_equal(3, 13 % g:anint) |
| 471 | assert_equal(2, g:anint % g:alsoint) |
| 472 | |
| 473 | assert_equal(4, 6 * 4 / 6) |
| 474 | enddef |
| 475 | |
| 476 | def Test_expr6_float() |
| 477 | CheckFeature float |
| 478 | |
| 479 | assert_equal(36.0, 6.0 * 6) |
| 480 | assert_equal(36.0, 6 * 6.0) |
| 481 | assert_equal(36.0, 6.0 * 6.0) |
| 482 | assert_equal(1.0, g:afloat * g:anint) |
| 483 | |
| 484 | assert_equal(10.0, 60 / 6.0) |
| 485 | assert_equal(10.0, 60.0 / 6) |
| 486 | assert_equal(10.0, 60.0 / 6.0) |
| 487 | assert_equal(0.01, g:afloat / g:anint) |
| 488 | |
| 489 | assert_equal(4.0, 6.0 * 4 / 6) |
| 490 | assert_equal(4.0, 6 * 4.0 / 6) |
| 491 | assert_equal(4.0, 6 * 4 / 6.0) |
| 492 | assert_equal(4.0, 6.0 * 4.0 / 6) |
| 493 | assert_equal(4.0, 6 * 4.0 / 6.0) |
| 494 | assert_equal(4.0, 6.0 * 4 / 6.0) |
| 495 | assert_equal(4.0, 6.0 * 4.0 / 6.0) |
| 496 | |
| 497 | assert_equal(4.0, 6.0 * 4.0 / 6.0) |
| 498 | enddef |
| 499 | |
| 500 | func Test_expr6_fails() |
| 501 | let msg = "white space required before and after '*'" |
| 502 | call CheckDefFailure("let x = 1*2", msg) |
| 503 | call CheckDefFailure("let x = 1 *2", msg) |
| 504 | call CheckDefFailure("let x = 1* 2", msg) |
| 505 | |
| 506 | let msg = "white space required before and after '/'" |
| 507 | call CheckDefFailure("let x = 1/2", msg) |
| 508 | call CheckDefFailure("let x = 1 /2", msg) |
| 509 | call CheckDefFailure("let x = 1/ 2", msg) |
| 510 | |
| 511 | let msg = "white space required before and after '%'" |
| 512 | call CheckDefFailure("let x = 1%2", msg) |
| 513 | call CheckDefFailure("let x = 1 %2", msg) |
| 514 | call CheckDefFailure("let x = 1% 2", msg) |
| 515 | |
| 516 | call CheckDefFailure("let x = '1' * '2'", 'E1036:') |
| 517 | call CheckDefFailure("let x = '1' / '2'", 'E1036:') |
| 518 | call CheckDefFailure("let x = '1' % '2'", 'E1035:') |
| 519 | |
| 520 | call CheckDefFailure("let x = 0z01 * 0z12", 'E1036:') |
| 521 | call CheckDefFailure("let x = 0z01 / 0z12", 'E1036:') |
| 522 | call CheckDefFailure("let x = 0z01 % 0z12", 'E1035:') |
| 523 | |
| 524 | call CheckDefFailure("let x = [1] * [2]", 'E1036:') |
| 525 | call CheckDefFailure("let x = [1] / [2]", 'E1036:') |
| 526 | call CheckDefFailure("let x = [1] % [2]", 'E1035:') |
| 527 | |
| 528 | call CheckDefFailure("let x = #{one: 1} * #{two: 2}", 'E1036:') |
| 529 | call CheckDefFailure("let x = #{one: 1} / #{two: 2}", 'E1036:') |
| 530 | call CheckDefFailure("let x = #{one: 1} % #{two: 2}", 'E1035:') |
| 531 | |
| 532 | endfunc |
| 533 | |
| 534 | func Test_expr6_float_fails() |
| 535 | CheckFeature float |
| 536 | call CheckDefFailure("let x = 1.0 % 2", 'E1035:') |
| 537 | endfunc |
| 538 | |
| 539 | " define here to use old style parsing |
| 540 | if has('float') |
| 541 | let g:float_zero = 0.0 |
| 542 | let g:float_neg = -9.8 |
| 543 | let g:float_big = 9.9e99 |
| 544 | endif |
| 545 | let g:blob_empty = 0z |
| 546 | let g:blob_one = 0z01 |
| 547 | let g:blob_long = 0z0102.0304 |
| 548 | |
| 549 | let g:string_empty = '' |
| 550 | let g:string_short = 'x' |
| 551 | let g:string_long = 'abcdefghijklm' |
| 552 | let g:string_special = "ab\ncd\ref\ekk" |
| 553 | |
| 554 | let g:special_true = v:true |
| 555 | let g:special_false = v:false |
| 556 | let g:special_null = v:null |
| 557 | let g:special_none = v:none |
| 558 | |
| 559 | let g:list_empty = [] |
| 560 | let g:list_mixed = [1, 'b', v:false] |
| 561 | |
| 562 | let g:dict_empty = {} |
| 563 | let g:dict_one = #{one: 1} |
| 564 | |
| 565 | let $TESTVAR = 'testvar' |
| 566 | |
| 567 | let @a = 'register a' |
| 568 | |
| 569 | " test low level expression |
| 570 | def Test_expr7_number() |
| 571 | " number constant |
| 572 | assert_equal(0, 0) |
| 573 | assert_equal(654, 0654) |
| 574 | |
| 575 | assert_equal(6, 0x6) |
| 576 | assert_equal(15, 0xf) |
| 577 | assert_equal(255, 0xff) |
| 578 | enddef |
| 579 | |
| 580 | def Test_expr7_float() |
| 581 | " float constant |
| 582 | if has('float') |
| 583 | assert_equal(g:float_zero, .0) |
| 584 | assert_equal(g:float_zero, 0.0) |
| 585 | assert_equal(g:float_neg, -9.8) |
| 586 | assert_equal(g:float_big, 9.9e99) |
| 587 | endif |
| 588 | enddef |
| 589 | |
| 590 | def Test_expr7_blob() |
| 591 | " blob constant |
| 592 | assert_equal(g:blob_empty, 0z) |
| 593 | assert_equal(g:blob_one, 0z01) |
| 594 | assert_equal(g:blob_long, 0z0102.0304) |
| 595 | enddef |
| 596 | |
| 597 | def Test_expr7_string() |
| 598 | " string constant |
| 599 | assert_equal(g:string_empty, '') |
| 600 | assert_equal(g:string_empty, "") |
| 601 | assert_equal(g:string_short, 'x') |
| 602 | assert_equal(g:string_short, "x") |
| 603 | assert_equal(g:string_long, 'abcdefghijklm') |
| 604 | assert_equal(g:string_long, "abcdefghijklm") |
| 605 | assert_equal(g:string_special, "ab\ncd\ref\ekk") |
| 606 | enddef |
| 607 | |
| 608 | def Test_expr7_special() |
| 609 | " special constant |
| 610 | assert_equal(g:special_true, true) |
| 611 | assert_equal(g:special_false, false) |
| 612 | assert_equal(g:special_null, v:null) |
| 613 | assert_equal(g:special_none, v:none) |
| 614 | enddef |
| 615 | |
| 616 | def Test_expr7_list() |
| 617 | " list |
| 618 | assert_equal(g:list_empty, []) |
| 619 | assert_equal(g:list_empty, [ ]) |
| 620 | assert_equal(g:list_mixed, [1, 'b', false]) |
| 621 | enddef |
| 622 | |
| 623 | def Test_expr7_lambda() |
| 624 | " lambda |
| 625 | let La = { -> 'result'} |
| 626 | assert_equal('result', La()) |
| 627 | assert_equal([1, 3, 5], [1, 2, 3]->map({key, val -> key + val})) |
| 628 | enddef |
| 629 | |
| 630 | def Test_expr7_dict() |
| 631 | " dictionary |
| 632 | assert_equal(g:dict_empty, {}) |
| 633 | assert_equal(g:dict_empty, { }) |
| 634 | assert_equal(g:dict_one, {'one': 1}) |
| 635 | let key = 'one' |
| 636 | let val = 1 |
| 637 | assert_equal(g:dict_one, {key: val}) |
| 638 | enddef |
| 639 | |
| 640 | def Test_expr7_option() |
| 641 | " option |
| 642 | set ts=11 |
| 643 | assert_equal(11, &ts) |
| 644 | set ts=8 |
| 645 | set grepprg=some\ text |
| 646 | assert_equal('some text', &grepprg) |
| 647 | set grepprg& |
| 648 | enddef |
| 649 | |
| 650 | def Test_expr7_environment() |
| 651 | " environment variable |
| 652 | assert_equal('testvar', $TESTVAR) |
| 653 | assert_equal('', $ASDF_ASD_XXX) |
| 654 | enddef |
| 655 | |
| 656 | def Test_expr7_register() |
| 657 | " register |
| 658 | assert_equal('register a', @a) |
| 659 | enddef |
| 660 | |
| 661 | def Test_expr7_parens() |
| 662 | " (expr) |
| 663 | assert_equal(4, (6 * 4) / 6) |
| 664 | assert_equal(0, 6 * ( 4 / 6 )) |
| 665 | |
| 666 | assert_equal(6, +6) |
| 667 | assert_equal(-6, -6) |
| 668 | assert_equal(6, --6) |
| 669 | assert_equal(6, -+-6) |
| 670 | assert_equal(-6, ---6) |
| 671 | enddef |
| 672 | |
| 673 | def Test_expr7_not() |
| 674 | assert_equal(true, !'') |
| 675 | assert_equal(true, ![]) |
| 676 | assert_equal(false, !'asdf') |
| 677 | assert_equal(false, ![2]) |
| 678 | assert_equal(true, !!'asdf') |
| 679 | assert_equal(true, !![2]) |
| 680 | enddef |
| 681 | |
| 682 | func Test_expr7_fails() |
| 683 | call CheckDefFailure("let x = (12", "E110:") |
| 684 | |
| 685 | call CheckDefFailure("let x = -'xx'", "E1030:") |
| 686 | call CheckDefFailure("let x = +'xx'", "E1030:") |
| 687 | |
| 688 | call CheckDefFailure("let x = @", "E1002:") |
| 689 | call CheckDefFailure("let x = @<", "E354:") |
Bram Moolenaar | 58ceca5 | 2020-01-28 22:46:22 +0100 | [diff] [blame^] | 690 | |
| 691 | call CheckDefFailure("let x = ¬exist", "E113:") |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 692 | endfunc |
| 693 | |
| 694 | let g:Funcrefs = [function('add')] |
| 695 | |
| 696 | func CallMe(arg) |
| 697 | return a:arg |
| 698 | endfunc |
| 699 | |
| 700 | def Test_expr7_trailing() |
| 701 | " user function call |
| 702 | assert_equal(123, CallMe(123)) |
| 703 | assert_equal('nothing', CallMe('nothing')) |
| 704 | |
| 705 | " partial call |
| 706 | let Part = function('CallMe') |
| 707 | assert_equal('yes', Part('yes')) |
| 708 | |
| 709 | " funcref call, using list index |
| 710 | let l = [] |
| 711 | g:Funcrefs[0](l, 2) |
| 712 | assert_equal([2], l) |
| 713 | |
| 714 | " method call |
| 715 | l = [2, 5, 6] |
| 716 | l->map({k, v -> k + v}) |
| 717 | assert_equal([2, 6, 8], l) |
| 718 | |
| 719 | " lambda method call |
| 720 | l = [2, 5] |
| 721 | l->{l -> add(l, 8)}() |
| 722 | assert_equal([2, 5, 8], l) |
| 723 | |
| 724 | " dict member |
| 725 | let d = #{key: 123} |
| 726 | assert_equal(123, d.key) |
| 727 | enddef |
| 728 | |
| 729 | func Test_expr7_trailing_fails() |
| 730 | call CheckDefFailureList(['let l = [2]', 'l->{l -> add(l, 8)}'], 'E107') |
| 731 | endfunc |
| 732 | |
| 733 | func Test_expr_fails() |
| 734 | call CheckDefFailure("let x = '1'is2", 'E488:') |
| 735 | call CheckDefFailure("let x = '1'isnot2", 'E488:') |
| 736 | endfunc |