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