1" Tests for Lua. 2 3source check.vim 4CheckFeature lua 5CheckFeature float 6 7func TearDown() 8 " Run garbage collection after each test to exercise luaV_setref(). 9 call test_garbagecollect_now() 10endfunc 11 12" Check that switching to another buffer does not trigger ml_get error. 13func Test_lua_command_new_no_ml_get_error() 14 new 15 let wincount = winnr('$') 16 call setline(1, ['one', 'two', 'three']) 17 luado vim.command("new") 18 call assert_equal(wincount + 1, winnr('$')) 19 %bwipe! 20endfunc 21 22" Test vim.command() 23func Test_lua_command() 24 new 25 call setline(1, ['one', 'two', 'three']) 26 luado vim.command("1,2d_") 27 call assert_equal(['three'], getline(1, '$')) 28 bwipe! 29endfunc 30 31" Test vim.eval() 32func Test_lua_eval() 33 " lua.eval with a number 34 lua v = vim.eval('123') 35 call assert_equal('number', luaeval('vim.type(v)')) 36 call assert_equal(123, luaeval('v')) 37 38 " lua.eval with a string 39 lua v = vim.eval('"abc"') 40 call assert_equal('string', 'vim.type(v)'->luaeval()) 41 call assert_equal('abc', luaeval('v')) 42 43 " lua.eval with a list 44 lua v = vim.eval("['a']") 45 call assert_equal('list', luaeval('vim.type(v)')) 46 call assert_equal(['a'], luaeval('v')) 47 48 " lua.eval with a dict 49 lua v = vim.eval("{'a':'b'}") 50 call assert_equal('dict', luaeval('vim.type(v)')) 51 call assert_equal({'a':'b'}, luaeval('v')) 52 53 " lua.eval with a blob 54 lua v = vim.eval("0z00112233.deadbeef") 55 call assert_equal('blob', luaeval('vim.type(v)')) 56 call assert_equal(0z00112233.deadbeef, luaeval('v')) 57 58 call assert_fails('lua v = vim.eval(nil)', 59 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)") 60 call assert_fails('lua v = vim.eval(true)', 61 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)") 62 call assert_fails('lua v = vim.eval({})', 63 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)") 64 call assert_fails('lua v = vim.eval(print)', 65 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)") 66 call assert_fails('lua v = vim.eval(vim.buffer())', 67 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)") 68 69 lua v = nil 70endfunc 71 72" Test vim.window() 73func Test_lua_window() 74 e Xfoo2 75 new Xfoo1 76 77 " Window 1 (top window) contains Xfoo1 78 " Window 2 (bottom window) contains Xfoo2 79 call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name')) 80 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name')) 81 82 " Window 3 does not exist so vim.window(3) should return nil 83 call assert_equal('nil', luaeval('tostring(vim.window(3))')) 84 85 %bwipe! 86endfunc 87 88" Test vim.window().height 89func Test_lua_window_height() 90 new 91 lua vim.window().height = 2 92 call assert_equal(2, winheight(0)) 93 lua vim.window().height = vim.window().height + 1 94 call assert_equal(3, winheight(0)) 95 bwipe! 96endfunc 97 98" Test vim.window().width 99func Test_lua_window_width() 100 vert new 101 lua vim.window().width = 2 102 call assert_equal(2, winwidth(0)) 103 lua vim.window().width = vim.window().width + 1 104 call assert_equal(3, winwidth(0)) 105 bwipe! 106endfunc 107 108" Test vim.window().line and vim.window.col 109func Test_lua_window_line_col() 110 new 111 call setline(1, ['line1', 'line2', 'line3']) 112 lua vim.window().line = 2 113 lua vim.window().col = 4 114 call assert_equal([0, 2, 4, 0], getpos('.')) 115 lua vim.window().line = vim.window().line + 1 116 lua vim.window().col = vim.window().col - 1 117 call assert_equal([0, 3, 3, 0], getpos('.')) 118 119 call assert_fails('lua vim.window().line = 10', 120 \ '[string "vim chunk"]:1: line out of range') 121 bwipe! 122endfunc 123 124" Test vim.call 125func Test_lua_call() 126 call assert_equal(has('lua'), luaeval('vim.call("has", "lua")')) 127 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")')) 128endfunc 129 130" Test vim.fn.* 131func Test_lua_fn() 132 call assert_equal(has('lua'), luaeval('vim.fn.has("lua")')) 133 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")')) 134endfunc 135 136" Test setting the current window 137func Test_lua_window_set_current() 138 new Xfoo1 139 lua w1 = vim.window() 140 new Xfoo2 141 lua w2 = vim.window() 142 143 call assert_equal('Xfoo2', bufname('%')) 144 lua w1() 145 call assert_equal('Xfoo1', bufname('%')) 146 lua w2() 147 call assert_equal('Xfoo2', bufname('%')) 148 149 lua w1, w2 = nil 150 %bwipe! 151endfunc 152 153" Test vim.window().buffer 154func Test_lua_window_buffer() 155 new Xfoo1 156 lua w1 = vim.window() 157 lua b1 = w1.buffer() 158 new Xfoo2 159 lua w2 = vim.window() 160 lua b2 = w2.buffer() 161 162 lua b1() 163 call assert_equal('Xfoo1', bufname('%')) 164 lua b2() 165 call assert_equal('Xfoo2', bufname('%')) 166 167 lua b1, b2, w1, w2 = nil 168 %bwipe! 169endfunc 170 171" Test vim.window():previous() and vim.window():next() 172func Test_lua_window_next_previous() 173 new Xfoo1 174 new Xfoo2 175 new Xfoo3 176 wincmd j 177 178 call assert_equal('Xfoo2', luaeval('vim.window().buffer().name')) 179 call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name')) 180 call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name')) 181 182 %bwipe! 183endfunc 184 185" Test vim.window():isvalid() 186func Test_lua_window_isvalid() 187 new Xfoo 188 lua w = vim.window() 189 call assert_true(luaeval('w:isvalid()')) 190 191 " FIXME: how to test the case when isvalid() returns v:false? 192 " isvalid() gives errors when the window is deleted. Is it a bug? 193 194 lua w = nil 195 bwipe! 196endfunc 197 198" Test vim.buffer() with and without argument 199func Test_lua_buffer() 200 new Xfoo1 201 let bn1 = bufnr('%') 202 new Xfoo2 203 let bn2 = bufnr('%') 204 205 " Test vim.buffer() without argument. 206 call assert_equal('Xfoo2', luaeval("vim.buffer().name")) 207 208 " Test vim.buffer() with string argument. 209 call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name")) 210 call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name")) 211 212 " Test vim.buffer() with integer argument. 213 call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name")) 214 call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name")) 215 216 lua bn1, bn2 = nil 217 %bwipe! 218endfunc 219 220" Test vim.buffer().name and vim.buffer().fname 221func Test_lua_buffer_name() 222 new 223 call assert_equal('', luaeval('vim.buffer().name')) 224 call assert_equal('', luaeval('vim.buffer().fname')) 225 bwipe! 226 227 new Xfoo 228 call assert_equal('Xfoo', luaeval('vim.buffer().name')) 229 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname')) 230 bwipe! 231endfunc 232 233" Test vim.buffer().number 234func Test_lua_buffer_number() 235 " All numbers in Lua are floating points number (no integers). 236 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number'))) 237endfunc 238 239" Test inserting lines in buffer. 240func Test_lua_buffer_insert() 241 new 242 lua vim.buffer()[1] = '3' 243 lua vim.buffer():insert('1', 0) 244 lua vim.buffer():insert('2', 1) 245 lua vim.buffer():insert('4', 10) 246 247 call assert_equal(['1', '2', '3', '4'], getline(1, '$')) 248 bwipe! 249endfunc 250 251" Test deleting line in buffer 252func Test_lua_buffer_delete() 253 new 254 call setline(1, ['1', '2', '3']) 255 lua vim.buffer()[2] = nil 256 call assert_equal(['1', '3'], getline(1, '$')) 257 258 call assert_fails('lua vim.buffer()[3] = nil', 259 \ '[string "vim chunk"]:1: invalid line number') 260 bwipe! 261endfunc 262 263" Test #vim.buffer() i.e. number of lines in buffer 264func Test_lua_buffer_number_lines() 265 new 266 call setline(1, ['a', 'b', 'c']) 267 call assert_equal(3, luaeval('#vim.buffer()')) 268 bwipe! 269endfunc 270 271" Test vim.buffer():next() and vim.buffer():previous() 272" Note that these functions get the next or previous buffers 273" but do not switch buffer. 274func Test_lua_buffer_next_previous() 275 new Xfoo1 276 new Xfoo2 277 new Xfoo3 278 b Xfoo2 279 280 lua bn = vim.buffer():next() 281 lua bp = vim.buffer():previous() 282 283 call assert_equal('Xfoo2', luaeval('vim.buffer().name')) 284 call assert_equal('Xfoo1', luaeval('bp.name')) 285 call assert_equal('Xfoo3', luaeval('bn.name')) 286 287 call assert_equal('Xfoo2', bufname('%')) 288 289 lua bn() 290 call assert_equal('Xfoo3', luaeval('vim.buffer().name')) 291 call assert_equal('Xfoo3', bufname('%')) 292 293 lua bp() 294 call assert_equal('Xfoo1', luaeval('vim.buffer().name')) 295 call assert_equal('Xfoo1', bufname('%')) 296 297 lua bn, bp = nil 298 %bwipe! 299endfunc 300 301" Test vim.buffer():isvalid() 302func Test_lua_buffer_isvalid() 303 new Xfoo 304 lua b = vim.buffer() 305 call assert_true(luaeval('b:isvalid()')) 306 307 " FIXME: how to test the case when isvalid() returns v:false? 308 " isvalid() gives errors when the buffer is wiped. Is it a bug? 309 310 lua b = nil 311 bwipe! 312endfunc 313 314func Test_lua_list() 315 call assert_equal([], luaeval('vim.list()')) 316 317 let l = [] 318 lua l = vim.eval('l') 319 lua l:add(123) 320 lua l:add('abc') 321 lua l:add(true) 322 lua l:add(false) 323 lua l:add(nil) 324 lua l:add(vim.eval("[1, 2, 3]")) 325 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}")) 326 call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l) 327 call assert_equal(7, luaeval('#l')) 328 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) 329 330 lua l[0] = 124 331 lua l[5] = nil 332 lua l:insert('first') 333 lua l:insert('xx', 3) 334 call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l) 335 336 lockvar 1 l 337 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked') 338 339 lua l = nil 340endfunc 341 342func Test_lua_list_table() 343 " See :help lua-vim 344 " Non-numeric keys should not be used to initialize the list 345 " so say = 'hi' should be ignored. 346 lua t = {3.14, 'hello', false, true, say = 'hi'} 347 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)')) 348 lua t = nil 349 350 call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number') 351 call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string') 352 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function') 353 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean') 354endfunc 355 356" Test l() i.e. iterator on list 357func Test_lua_list_iter() 358 lua l = vim.list():add('foo'):add('bar') 359 lua str = '' 360 lua for v in l() do str = str .. v end 361 call assert_equal('foobar', luaeval('str')) 362 363 lua str, l = nil 364endfunc 365 366func Test_lua_recursive_list() 367 lua l = vim.list():add(1):add(2) 368 lua l = l:add(l) 369 370 call assert_equal(1, luaeval('l[0]')) 371 call assert_equal(2, luaeval('l[1]')) 372 373 call assert_equal(1, luaeval('l[2][0]')) 374 call assert_equal(2, luaeval('l[2][1]')) 375 376 call assert_equal(1, luaeval('l[2][2][0]')) 377 call assert_equal(2, luaeval('l[2][2][1]')) 378 379 call assert_equal('[1, 2, [...]]', string(luaeval('l'))) 380 381 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) 382 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[2])')) 383 384 call assert_equal(luaeval('l'), luaeval('l[2]')) 385 call assert_equal(luaeval('l'), luaeval('l[2][2]')) 386 387 lua l = nil 388endfunc 389 390func Test_lua_dict() 391 call assert_equal({}, luaeval('vim.dict()')) 392 393 let d = {} 394 lua d = vim.eval('d') 395 lua d[0] = 123 396 lua d[1] = "abc" 397 lua d[2] = true 398 lua d[3] = false 399 lua d[4] = vim.eval("[1, 2, 3]") 400 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}") 401 call assert_equal({'0':123, '1':'abc', '2':v:true, '3':v:false, '4': [1, 2, 3], '5': {'a':1, 'b':2, 'c':3}}, d) 402 call assert_equal(6, luaeval('#d')) 403 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)')) 404 405 call assert_equal('abc', luaeval('d[1]')) 406 407 lua d[0] = 124 408 lua d[4] = nil 409 call assert_equal({'0':124, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d) 410 411 lockvar 1 d 412 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked') 413 414 lua d = nil 415endfunc 416 417func Test_lua_dict_table() 418 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false} 419 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false}, 420 \ luaeval('vim.dict(t)')) 421 422 " Same example as in :help lua-vim. 423 lua t = {math.pi, false, say = 'hi'} 424 " FIXME: commented out as it currently does not work as documented: 425 " Expected {'say': 'hi'} 426 " but got {'1': 3.141593, '2': v:false, 'say': 'hi'} 427 " Is the documentation or the code wrong? 428 "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)')) 429 lua t = nil 430 431 call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number') 432 call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string') 433 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function') 434 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean') 435endfunc 436 437" Test d() i.e. iterator on dictionary 438func Test_lua_dict_iter() 439 let d = {'a': 1, 'b':2} 440 lua d = vim.eval('d') 441 lua str = '' 442 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end 443 call assert_equal('a:1,b:2,', luaeval('str')) 444 445 lua str, d = nil 446endfunc 447 448func Test_lua_blob() 449 call assert_equal(0z, luaeval('vim.blob("")')) 450 call assert_equal(0z31326162, luaeval('vim.blob("12ab")')) 451 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")')) 452 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")')) 453 454 lua b = vim.blob("\x00\x00\x00\x00") 455 call assert_equal(0z00000000, luaeval('b')) 456 call assert_equal(4, luaeval('#b')) 457 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff 458 call assert_equal(0z012000ff, luaeval('b')) 459 lua b[4] = string.byte("z", 1) 460 call assert_equal(0z012000ff.7a, luaeval('b')) 461 call assert_equal(5, luaeval('#b')) 462 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range') 463 lua b:add("12ab") 464 call assert_equal(0z012000ff.7a313261.62, luaeval('b')) 465 call assert_equal(9, luaeval('#b')) 466 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil') 467 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean') 468 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table') 469 lua b = nil 470endfunc 471 472func Test_lua_funcref() 473 function I(x) 474 return a:x 475 endfunction 476 let R = function('I') 477 lua i1 = vim.funcref"I" 478 lua i2 = vim.eval"R" 479 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL") 480 lua msg = vim.funcref"tr"(msg, "|", " ") 481 call assert_equal("funcref test OK", luaeval('msg')) 482 483 " dict funcref 484 function Mylen() dict 485 return len(self.data) 486 endfunction 487 let l = [0, 1, 2, 3] 488 let mydict = {'data': l} 489 lua d = vim.eval"mydict" 490 lua d.len = vim.funcref"Mylen" -- assign d as 'self' 491 lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL" 492 call assert_equal("OK", luaeval('res')) 493 call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len) 494 495 lua i1, i2, msg, d, res = nil 496endfunc 497 498" Test vim.type() 499func Test_lua_type() 500 " The following values are identical to Lua's type function. 501 call assert_equal('string', luaeval('vim.type("foo")')) 502 call assert_equal('number', luaeval('vim.type(1)')) 503 call assert_equal('number', luaeval('vim.type(1.2)')) 504 call assert_equal('function', luaeval('vim.type(print)')) 505 call assert_equal('table', luaeval('vim.type({})')) 506 call assert_equal('boolean', luaeval('vim.type(true)')) 507 call assert_equal('boolean', luaeval('vim.type(false)')) 508 call assert_equal('nil', luaeval('vim.type(nil)')) 509 510 " The following values are specific to Vim. 511 call assert_equal('window', luaeval('vim.type(vim.window())')) 512 call assert_equal('buffer', luaeval('vim.type(vim.buffer())')) 513 call assert_equal('list', luaeval('vim.type(vim.list())')) 514 call assert_equal('dict', luaeval('vim.type(vim.dict())')) 515 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))')) 516endfunc 517 518" Test vim.open() 519func Test_lua_open() 520 call assert_notmatch('XOpen', execute('ls')) 521 522 " Open a buffer XOpen1, but do not jump to it. 523 lua b = vim.open('XOpen1') 524 call assert_equal('XOpen1', luaeval('b.name')) 525 call assert_equal('', bufname('%')) 526 527 call assert_match('XOpen1', execute('ls')) 528 call assert_notequal('XOpen2', bufname('%')) 529 530 " Open a buffer XOpen2 and jump to it. 531 lua b = vim.open('XOpen2')() 532 call assert_equal('XOpen2', luaeval('b.name')) 533 call assert_equal('XOpen2', bufname('%')) 534 535 lua b = nil 536 %bwipe! 537endfunc 538 539" Test vim.line() 540func Test_lua_line() 541 new 542 call setline(1, ['first line', 'second line']) 543 1 544 call assert_equal('first line', luaeval('vim.line()')) 545 2 546 call assert_equal('second line', luaeval('vim.line()')) 547 bwipe! 548endfunc 549 550" Test vim.beep() 551func Test_lua_beep() 552 call assert_beeps('lua vim.beep()') 553endfunc 554 555" Test errors in luaeval() 556func Test_luaeval_error() 557 " Compile error 558 call assert_fails("call luaeval('-nil')", 559 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value') 560 call assert_fails("call luaeval(']')", 561 \ "[string \"luaeval\"]:1: unexpected symbol near ']'") 562endfunc 563 564" Test :luafile foo.lua 565func Test_luafile() 566 call delete('Xlua_file') 567 call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file') 568 call setfperm('Xlua_file', 'r-xr-xr-x') 569 570 luafile Xlua_file 571 call assert_equal('hello', luaeval('str')) 572 call assert_equal(123, luaeval('num')) 573 574 lua str, num = nil 575 call delete('Xlua_file') 576endfunc 577 578" Test :luafile % 579func Test_luafile_percent() 580 new Xlua_file 581 append 582 str, num = 'foo', 321.0 583 print(string.format('str=%s, num=%d', str, num)) 584. 585 w! 586 luafile % 587 let msg = split(execute('message'), "\n")[-1] 588 call assert_equal('str=foo, num=321', msg) 589 590 lua str, num = nil 591 call delete('Xlua_file') 592 bwipe! 593endfunc 594 595" Test :luafile with syntax error 596func Test_luafile_error() 597 new Xlua_file 598 call writefile(['nil = 0' ], 'Xlua_file') 599 call setfperm('Xlua_file', 'r-xr-xr-x') 600 601 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'") 602 603 call delete('Xlua_file') 604 bwipe! 605endfunc 606 607func Test_lua_set_cursor() 608 " Check that setting the cursor position works. 609 new 610 call setline(1, ['first line', 'second line']) 611 normal gg 612 lua << trim EOF 613 w = vim.window() 614 w.line = 1 615 w.col = 5 616 EOF 617 call assert_equal([1, 5], [line('.'), col('.')]) 618 619 " Check that movement after setting cursor position keeps current column. 620 normal j 621 call assert_equal([2, 5], [line('.'), col('.')]) 622endfunc 623 624" Test for various heredoc syntax 625func Test_lua_heredoc() 626 lua << END 627vim.command('let s = "A"') 628END 629 lua << 630vim.command('let s ..= "B"') 631. 632 lua << trim END 633 vim.command('let s ..= "C"') 634 END 635 lua << trim 636 vim.command('let s ..= "D"') 637 . 638 lua << trim eof 639 vim.command('let s ..= "E"') 640 eof 641 call assert_equal('ABCDE', s) 642endfunc 643 644" vim: shiftwidth=2 sts=2 expandtab 645