1" Tests for Lua. 2 3if !has('lua') 4 finish 5endif 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_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_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_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.0, luaeval('v')) 37 38 " lua.eval with a string 39 lua v = vim.eval('"abc"') 40 call assert_equal('string', luaeval('vim.type(v)')) 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_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_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_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_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 setting the current window 125func Test_window_set_current() 126 new Xfoo1 127 lua w1 = vim.window() 128 new Xfoo2 129 lua w2 = vim.window() 130 131 call assert_equal('Xfoo2', bufname('%')) 132 lua w1() 133 call assert_equal('Xfoo1', bufname('%')) 134 lua w2() 135 call assert_equal('Xfoo2', bufname('%')) 136 137 lua w1, w2 = nil 138 %bwipe! 139endfunc 140 141" Test vim.window().buffer 142func Test_window_buffer() 143 new Xfoo1 144 lua w1 = vim.window() 145 lua b1 = w1.buffer() 146 new Xfoo2 147 lua w2 = vim.window() 148 lua b2 = w2.buffer() 149 150 lua b1() 151 call assert_equal('Xfoo1', bufname('%')) 152 lua b2() 153 call assert_equal('Xfoo2', bufname('%')) 154 155 lua b1, b2, w1, w2 = nil 156 %bwipe! 157endfunc 158 159" Test vim.window():previous() and vim.window():next() 160func Test_window_next_previous() 161 new Xfoo1 162 new Xfoo2 163 new Xfoo3 164 wincmd j 165 166 call assert_equal('Xfoo2', luaeval('vim.window().buffer().name')) 167 call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name')) 168 call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name')) 169 170 %bwipe! 171endfunc 172 173" Test vim.window():isvalid() 174func Test_window_isvalid() 175 new Xfoo 176 lua w = vim.window() 177 call assert_true(luaeval('w:isvalid()')) 178 179 " FIXME: how to test the case when isvalid() returns v:false? 180 " isvalid() gives errors when the window is deleted. Is it a bug? 181 182 lua w = nil 183 bwipe! 184endfunc 185 186" Test vim.buffer() with and without argument 187func Test_buffer() 188 new Xfoo1 189 let bn1 = bufnr('%') 190 new Xfoo2 191 let bn2 = bufnr('%') 192 193 " Test vim.buffer() without argument. 194 call assert_equal('Xfoo2', luaeval("vim.buffer().name")) 195 196 " Test vim.buffer() with string argument. 197 call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name")) 198 call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name")) 199 200 " Test vim.buffer() with integer argument. 201 call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name")) 202 call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name")) 203 204 lua bn1, bn2 = nil 205 %bwipe! 206endfunc 207 208" Test vim.buffer().name and vim.buffer().fname 209func Test_buffer_name() 210 new 211 call assert_equal('', luaeval('vim.buffer().name')) 212 call assert_equal('', luaeval('vim.buffer().fname')) 213 bwipe! 214 215 new Xfoo 216 call assert_equal('Xfoo', luaeval('vim.buffer().name')) 217 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname')) 218 bwipe! 219endfunc 220 221" Test vim.buffer().number 222func Test_buffer_number() 223 " All numbers in Lua are floating points number (no integers). 224 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number'))) 225endfunc 226 227" Test inserting lines in buffer. 228func Test_buffer_insert() 229 new 230 lua vim.buffer()[1] = '3' 231 lua vim.buffer():insert('1', 0) 232 lua vim.buffer():insert('2', 1) 233 lua vim.buffer():insert('4', 10) 234 235 call assert_equal(['1', '2', '3', '4'], getline(1, '$')) 236 bwipe! 237endfunc 238 239" Test deleting line in buffer 240func Test_buffer_delete() 241 new 242 call setline(1, ['1', '2', '3']) 243 lua vim.buffer()[2] = nil 244 call assert_equal(['1', '3'], getline(1, '$')) 245 246 call assert_fails('lua vim.buffer()[3] = nil', 247 \ '[string "vim chunk"]:1: invalid line number') 248 bwipe! 249endfunc 250 251" Test #vim.buffer() i.e. number of lines in buffer 252func Test_buffer_number_lines() 253 new 254 call setline(1, ['a', 'b', 'c']) 255 call assert_equal(3.0, luaeval('#vim.buffer()')) 256 bwipe! 257endfunc 258 259" Test vim.buffer():next() and vim.buffer():previous() 260" Note that these functions get the next or previous buffers 261" but do not switch buffer. 262func Test_buffer_next_previous() 263 new Xfoo1 264 new Xfoo2 265 new Xfoo3 266 b Xfoo2 267 268 lua bn = vim.buffer():next() 269 lua bp = vim.buffer():previous() 270 271 call assert_equal('Xfoo2', luaeval('vim.buffer().name')) 272 call assert_equal('Xfoo1', luaeval('bp.name')) 273 call assert_equal('Xfoo3', luaeval('bn.name')) 274 275 call assert_equal('Xfoo2', bufname('%')) 276 277 lua bn() 278 call assert_equal('Xfoo3', luaeval('vim.buffer().name')) 279 call assert_equal('Xfoo3', bufname('%')) 280 281 lua bp() 282 call assert_equal('Xfoo1', luaeval('vim.buffer().name')) 283 call assert_equal('Xfoo1', bufname('%')) 284 285 lua bn, bp = nil 286 %bwipe! 287endfunc 288 289" Test vim.buffer():isvalid() 290func Test_buffer_isvalid() 291 new Xfoo 292 lua b = vim.buffer() 293 call assert_true(luaeval('b:isvalid()')) 294 295 " FIXME: how to test the case when isvalid() returns v:false? 296 " isvalid() gives errors when the buffer is wiped. Is it a bug? 297 298 lua b = nil 299 bwipe! 300endfunc 301 302func Test_list() 303 call assert_equal([], luaeval('vim.list()')) 304 305 let l = [] 306 lua l = vim.eval('l') 307 lua l:add(123) 308 lua l:add('abc') 309 lua l:add(true) 310 lua l:add(false) 311 lua l:add(nil) 312 lua l:add(vim.eval("[1, 2, 3]")) 313 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}")) 314 call assert_equal([123.0, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l) 315 call assert_equal(7.0, luaeval('#l')) 316 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) 317 318 lua l[0] = 124 319 lua l[5] = nil 320 lua l:insert('first') 321 lua l:insert('xx', 3) 322 call assert_equal(['first', 124.0, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l) 323 324 lockvar 1 l 325 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked') 326 327 lua l = nil 328endfunc 329 330func Test_list_table() 331 " See :help lua-vim 332 " Non-numeric keys should not be used to initialize the list 333 " so say = 'hi' should be ignored. 334 lua t = {3.14, 'hello', false, true, say = 'hi'} 335 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)')) 336 lua t = nil 337 338 call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number') 339 call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string') 340 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function') 341 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean') 342endfunc 343 344" Test l() i.e. iterator on list 345func Test_list_iter() 346 lua l = vim.list():add('foo'):add('bar') 347 lua str = '' 348 lua for v in l() do str = str .. v end 349 call assert_equal('foobar', luaeval('str')) 350 351 lua str, l = nil 352endfunc 353 354func Test_recursive_list() 355 lua l = vim.list():add(1):add(2) 356 lua l = l:add(l) 357 358 call assert_equal(1.0, luaeval('l[0]')) 359 call assert_equal(2.0, luaeval('l[1]')) 360 361 call assert_equal(1.0, luaeval('l[2][0]')) 362 call assert_equal(2.0, luaeval('l[2][1]')) 363 364 call assert_equal(1.0, luaeval('l[2][2][0]')) 365 call assert_equal(2.0, luaeval('l[2][2][1]')) 366 367 call assert_equal('[1.0, 2.0, [...]]', string(luaeval('l'))) 368 369 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) 370 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[2])')) 371 372 call assert_equal(luaeval('l'), luaeval('l[2]')) 373 call assert_equal(luaeval('l'), luaeval('l[2][2]')) 374 375 lua l = nil 376endfunc 377 378func Test_dict() 379 call assert_equal({}, luaeval('vim.dict()')) 380 381 let d = {} 382 lua d = vim.eval('d') 383 lua d[0] = 123 384 lua d[1] = "abc" 385 lua d[2] = true 386 lua d[3] = false 387 lua d[4] = vim.eval("[1, 2, 3]") 388 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}") 389 call assert_equal({'0':123.0, '1':'abc', '2':v:true, '3':v:false, '4': [1, 2, 3], '5': {'a':1, 'b':2, 'c':3}}, d) 390 call assert_equal(6.0, luaeval('#d')) 391 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)')) 392 393 call assert_equal('abc', luaeval('d[1]')) 394 395 lua d[0] = 124 396 lua d[4] = nil 397 call assert_equal({'0':124.0, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d) 398 399 lockvar 1 d 400 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked') 401 402 lua d = nil 403endfunc 404 405func Test_dict_table() 406 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false} 407 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false}, 408 \ luaeval('vim.dict(t)')) 409 410 " Same example as in :help lua-vim. 411 lua t = {math.pi, false, say = 'hi'} 412 " FIXME: commented out as it currently does not work as documented: 413 " Expected {'say': 'hi'} 414 " but got {'1': 3.141593, '2': v:false, 'say': 'hi'} 415 " Is the documentation or the code wrong? 416 "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)')) 417 lua t = nil 418 419 call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number') 420 call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string') 421 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function') 422 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean') 423endfunc 424 425" Test d() i.e. iterator on dictionary 426func Test_dict_iter() 427 let d = {'a': 1, 'b':2} 428 lua d = vim.eval('d') 429 lua str = '' 430 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end 431 call assert_equal('a:1,b:2,', luaeval('str')) 432 433 lua str, d = nil 434endfunc 435 436func Test_blob() 437 call assert_equal(0z, luaeval('vim.blob("")')) 438 call assert_equal(0z31326162, luaeval('vim.blob("12ab")')) 439 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")')) 440 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")')) 441 442 lua b = vim.blob("\x00\x00\x00\x00") 443 call assert_equal(0z00000000, luaeval('b')) 444 call assert_equal(4.0, luaeval('#b')) 445 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff 446 call assert_equal(0z012000ff, luaeval('b')) 447 lua b[4] = string.byte("z", 1) 448 call assert_equal(0z012000ff.7a, luaeval('b')) 449 call assert_equal(5.0, luaeval('#b')) 450 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range') 451 lua b:add("12ab") 452 call assert_equal(0z012000ff.7a313261.62, luaeval('b')) 453 call assert_equal(9.0, luaeval('#b')) 454 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil') 455 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean') 456 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table') 457 lua b = nil 458endfunc 459 460func Test_funcref() 461 function I(x) 462 return a:x 463 endfunction 464 let R = function('I') 465 lua i1 = vim.funcref"I" 466 lua i2 = vim.eval"R" 467 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL") 468 lua msg = vim.funcref"tr"(msg, "|", " ") 469 call assert_equal("funcref test OK", luaeval('msg')) 470 471 " dict funcref 472 function Mylen() dict 473 return len(self.data) 474 endfunction 475 let l = [0, 1, 2, 3] 476 let mydict = {'data': l} 477 lua d = vim.eval"mydict" 478 lua d.len = vim.funcref"Mylen" -- assign d as 'self' 479 lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL" 480 call assert_equal("OK", luaeval('res')) 481 call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len) 482 483 lua i1, i2, msg, d, res = nil 484endfunc 485 486" Test vim.type() 487func Test_type() 488 " The following values are identical to Lua's type function. 489 call assert_equal('string', luaeval('vim.type("foo")')) 490 call assert_equal('number', luaeval('vim.type(1)')) 491 call assert_equal('number', luaeval('vim.type(1.2)')) 492 call assert_equal('function', luaeval('vim.type(print)')) 493 call assert_equal('table', luaeval('vim.type({})')) 494 call assert_equal('boolean', luaeval('vim.type(true)')) 495 call assert_equal('boolean', luaeval('vim.type(false)')) 496 call assert_equal('nil', luaeval('vim.type(nil)')) 497 498 " The following values are specific to Vim. 499 call assert_equal('window', luaeval('vim.type(vim.window())')) 500 call assert_equal('buffer', luaeval('vim.type(vim.buffer())')) 501 call assert_equal('list', luaeval('vim.type(vim.list())')) 502 call assert_equal('dict', luaeval('vim.type(vim.dict())')) 503 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))')) 504endfunc 505 506" Test vim.open() 507func Test_open() 508 call assert_notmatch('XOpen', execute('ls')) 509 510 " Open a buffer XOpen1, but do not jump to it. 511 lua b = vim.open('XOpen1') 512 call assert_equal('XOpen1', luaeval('b.name')) 513 call assert_equal('', bufname('%')) 514 515 call assert_match('XOpen1', execute('ls')) 516 call assert_notequal('XOpen2', bufname('%')) 517 518 " Open a buffer XOpen2 and jump to it. 519 lua b = vim.open('XOpen2')() 520 call assert_equal('XOpen2', luaeval('b.name')) 521 call assert_equal('XOpen2', bufname('%')) 522 523 lua b = nil 524 %bwipe! 525endfunc 526 527" Test vim.line() 528func Test_line() 529 new 530 call setline(1, ['first line', 'second line']) 531 1 532 call assert_equal('first line', luaeval('vim.line()')) 533 2 534 call assert_equal('second line', luaeval('vim.line()')) 535 bwipe! 536endfunc 537 538" Test vim.beep() 539func Test_beep() 540 call assert_beeps('lua vim.beep()') 541endfunc 542 543" Test errors in luaeval() 544func Test_luaeval_error() 545 " Compile error 546 call assert_fails("call luaeval('-nil')", 547 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value') 548 call assert_fails("call luaeval(']')", 549 \ "[string \"luaeval\"]:1: unexpected symbol near ']'") 550endfunc 551 552" Test :luafile foo.lua 553func Test_luafile() 554 call delete('Xlua_file') 555 call writefile(["str = 'hello'", "num = 123.0" ], 'Xlua_file') 556 call setfperm('Xlua_file', 'r-xr-xr-x') 557 558 luafile Xlua_file 559 call assert_equal('hello', luaeval('str')) 560 call assert_equal(123.0, luaeval('num')) 561 562 lua str, num = nil 563 call delete('Xlua_file') 564endfunc 565 566" Test :luafile % 567func Test_luafile_percent() 568 new Xlua_file 569 append 570 str, num = 'foo', 321.0 571 print(string.format('str=%s, num=%d', str, num)) 572. 573 w! 574 luafile % 575 let msg = split(execute('message'), "\n")[-1] 576 call assert_equal('str=foo, num=321', msg) 577 578 lua str, num = nil 579 call delete('Xlua_file') 580 bwipe! 581endfunc 582 583" Test :luafile with syntax error 584func Test_luafile_error() 585 new Xlua_file 586 call writefile(['nil = 0' ], 'Xlua_file') 587 call setfperm('Xlua_file', 'r-xr-xr-x') 588 589 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'") 590 591 call delete('Xlua_file') 592 bwipe! 593endfunc 594 595func Test_set_cursor() 596 " Check that setting the cursor position works. 597 new 598 call setline(1, ['first line', 'second line']) 599 normal gg 600 lua << EOF 601w = vim.window() 602w.line = 1 603w.col = 5 604EOF 605 call assert_equal([1, 5], [line('.'), col('.')]) 606 607 " Check that movement after setting cursor position keeps current column. 608 normal j 609 call assert_equal([2, 5], [line('.'), col('.')]) 610endfunc 611