1" Tests for Lua. 2 3source check.vim 4 5" This test also works without the lua feature. 6func Test_skip_lua() 7 if 0 8 lua print("Not executed") 9 endif 10endfunc 11 12CheckFeature lua 13CheckFeature float 14 15let s:luaver = split(split(luaeval('_VERSION'), ' ')[1], '\.') 16let s:major = str2nr(s:luaver[0]) 17let s:minor = str2nr(s:luaver[1]) 18if s:major < 5 || (s:major == 5 && s:minor < 3) 19 let s:lua_53_or_later = 0 20else 21 let s:lua_53_or_later = 1 22endif 23 24func TearDown() 25 " Run garbage collection after each test to exercise luaV_setref(). 26 call test_garbagecollect_now() 27endfunc 28 29" Check that switching to another buffer does not trigger ml_get error. 30func Test_lua_command_new_no_ml_get_error() 31 new 32 let wincount = winnr('$') 33 call setline(1, ['one', 'two', 'three']) 34 luado vim.command("new") 35 call assert_equal(wincount + 1, winnr('$')) 36 %bwipe! 37endfunc 38 39" Test vim.command() 40func Test_lua_command() 41 new 42 call setline(1, ['one', 'two', 'three']) 43 luado vim.command("1,2d_") 44 call assert_equal(['three'], getline(1, '$')) 45 bwipe! 46endfunc 47 48func Test_lua_luado() 49 new 50 call setline(1, ['one', 'two']) 51 luado return(linenr) 52 call assert_equal(['1', '2'], getline(1, '$')) 53 close! 54 55 " Error cases 56 call assert_fails('luado string.format()', 57 \ "[string \"vim chunk\"]:1: bad argument #1 to 'format' (string expected, got no value)") 58 call assert_fails('luado func()', 59 \ s:lua_53_or_later 60 \ ? "[string \"vim chunk\"]:1: attempt to call a nil value (global 'func')" 61 \ : "[string \"vim chunk\"]:1: attempt to call global 'func' (a nil value)") 62 call assert_fails('luado error("failed")', "[string \"vim chunk\"]:1: failed") 63endfunc 64 65" Test vim.eval() 66func Test_lua_eval() 67 " lua.eval with a number 68 lua v = vim.eval('123') 69 call assert_equal('number', luaeval('vim.type(v)')) 70 call assert_equal(123, luaeval('v')) 71 72 " lua.eval with a string 73 lua v = vim.eval('"abc"') 74 call assert_equal('string', 'vim.type(v)'->luaeval()) 75 call assert_equal('abc', luaeval('v')) 76 77 " lua.eval with a list 78 lua v = vim.eval("['a']") 79 call assert_equal('list', luaeval('vim.type(v)')) 80 call assert_equal(['a'], luaeval('v')) 81 82 " lua.eval with a dict 83 lua v = vim.eval("{'a':'b'}") 84 call assert_equal('dict', luaeval('vim.type(v)')) 85 call assert_equal({'a':'b'}, luaeval('v')) 86 87 " lua.eval with a blob 88 lua v = vim.eval("0z00112233.deadbeef") 89 call assert_equal('blob', luaeval('vim.type(v)')) 90 call assert_equal(0z00112233.deadbeef, luaeval('v')) 91 92 " lua.eval with a float 93 lua v = vim.eval('3.14') 94 call assert_equal('number', luaeval('vim.type(v)')) 95 call assert_equal(3.14, luaeval('v')) 96 97 " lua.eval with a bool 98 lua v = vim.eval('v:true') 99 call assert_equal('number', luaeval('vim.type(v)')) 100 call assert_equal(1, luaeval('v')) 101 lua v = vim.eval('v:false') 102 call assert_equal('number', luaeval('vim.type(v)')) 103 call assert_equal(0, luaeval('v')) 104 105 " lua.eval with a null 106 lua v = vim.eval('v:null') 107 call assert_equal('nil', luaeval('vim.type(v)')) 108 call assert_equal(v:null, luaeval('v')) 109 110 call assert_fails('lua v = vim.eval(nil)', 111 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got nil)") 112 call assert_fails('lua v = vim.eval(true)', 113 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got boolean)") 114 call assert_fails('lua v = vim.eval({})', 115 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got table)") 116 call assert_fails('lua v = vim.eval(print)', 117 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got function)") 118 call assert_fails('lua v = vim.eval(vim.buffer())', 119 \ "[string \"vim chunk\"]:1: bad argument #1 to 'eval' (string expected, got userdata)") 120 121 lua v = nil 122endfunc 123 124" Test luaeval() with lambda 125func Test_luaeval_with_lambda() 126 lua function hello_luaeval_lambda(a, cb) return a .. cb() end 127 call assert_equal('helloworld', 128 \ luaeval('hello_luaeval_lambda(_A[1], _A[2])', 129 \ ['hello', {->'world'}])) 130 lua hello_luaeval_lambda = nil 131endfunc 132 133" Test vim.window() 134func Test_lua_window() 135 e Xfoo2 136 new Xfoo1 137 138 " Window 1 (top window) contains Xfoo1 139 " Window 2 (bottom window) contains Xfoo2 140 call assert_equal('Xfoo1', luaeval('vim.window(1):buffer().name')) 141 call assert_equal('Xfoo2', luaeval('vim.window(2):buffer().name')) 142 143 " Window 3 does not exist so vim.window(3) should return nil 144 call assert_equal('nil', luaeval('tostring(vim.window(3))')) 145 146 call assert_fails("let n = luaeval('vim.window().xyz()')", 147 \ s:lua_53_or_later 148 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (field 'xyz')" 149 \ : "[string \"luaeval\"]:1: attempt to call field 'xyz' (a nil value)") 150 call assert_fails('lua vim.window().xyz = 1', 151 \ "[string \"vim chunk\"]:1: invalid window property: `xyz'") 152 153 %bwipe! 154endfunc 155 156" Test vim.window().height 157func Test_lua_window_height() 158 new 159 lua vim.window().height = 2 160 call assert_equal(2, winheight(0)) 161 lua vim.window().height = vim.window().height + 1 162 call assert_equal(3, winheight(0)) 163 bwipe! 164endfunc 165 166" Test vim.window().width 167func Test_lua_window_width() 168 vert new 169 lua vim.window().width = 2 170 call assert_equal(2, winwidth(0)) 171 lua vim.window().width = vim.window().width + 1 172 call assert_equal(3, winwidth(0)) 173 bwipe! 174endfunc 175 176" Test vim.window().line and vim.window.col 177func Test_lua_window_line_col() 178 new 179 call setline(1, ['line1', 'line2', 'line3']) 180 lua vim.window().line = 2 181 lua vim.window().col = 4 182 call assert_equal([0, 2, 4, 0], getpos('.')) 183 lua vim.window().line = vim.window().line + 1 184 lua vim.window().col = vim.window().col - 1 185 call assert_equal([0, 3, 3, 0], getpos('.')) 186 187 call assert_fails('lua vim.window().line = 10', 188 \ '[string "vim chunk"]:1: line out of range') 189 bwipe! 190endfunc 191 192" Test vim.call 193func Test_lua_call() 194 call assert_equal(has('lua'), luaeval('vim.call("has", "lua")')) 195 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.call("printf", "Hello %s", "vim")')) 196 197 " Error cases 198 call assert_fails("call luaeval('vim.call(\"min\", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)')", 199 \ s:lua_53_or_later 200 \ ? '[string "luaeval"]:1: Function called with too many arguments' 201 \ : 'Function called with too many arguments') 202 lua co = coroutine.create(function () print("hi") end) 203 call assert_fails("call luaeval('vim.call(\"type\", co)')", 204 \ s:lua_53_or_later 205 \ ? '[string "luaeval"]:1: lua: cannot convert value' 206 \ : 'lua: cannot convert value') 207 lua co = nil 208 call assert_fails("call luaeval('vim.call(\"abc\")')", 209 \ ['E117:', s:lua_53_or_later ? '\[string "luaeval"]:1: lua: call_vim_function failed' 210 \ : 'lua: call_vim_function failed']) 211endfunc 212 213" Test vim.fn.* 214func Test_lua_fn() 215 call assert_equal(has('lua'), luaeval('vim.fn.has("lua")')) 216 call assert_equal(printf("Hello %s", "vim"), luaeval('vim.fn.printf("Hello %s", "vim")')) 217endfunc 218 219" Test setting the current window 220func Test_lua_window_set_current() 221 new Xfoo1 222 lua w1 = vim.window() 223 new Xfoo2 224 lua w2 = vim.window() 225 226 call assert_equal('Xfoo2', bufname('%')) 227 lua w1() 228 call assert_equal('Xfoo1', bufname('%')) 229 lua w2() 230 call assert_equal('Xfoo2', bufname('%')) 231 232 lua w1, w2 = nil 233 %bwipe! 234endfunc 235 236" Test vim.window().buffer 237func Test_lua_window_buffer() 238 new Xfoo1 239 lua w1 = vim.window() 240 lua b1 = w1.buffer() 241 new Xfoo2 242 lua w2 = vim.window() 243 lua b2 = w2.buffer() 244 245 lua b1() 246 call assert_equal('Xfoo1', bufname('%')) 247 lua b2() 248 call assert_equal('Xfoo2', bufname('%')) 249 250 lua b1, b2, w1, w2 = nil 251 %bwipe! 252endfunc 253 254" Test vim.window():previous() and vim.window():next() 255func Test_lua_window_next_previous() 256 new Xfoo1 257 new Xfoo2 258 new Xfoo3 259 wincmd j 260 261 call assert_equal('Xfoo2', luaeval('vim.window().buffer().name')) 262 call assert_equal('Xfoo1', luaeval('vim.window():next():buffer().name')) 263 call assert_equal('Xfoo3', luaeval('vim.window():previous():buffer().name')) 264 265 %bwipe! 266endfunc 267 268" Test vim.window():isvalid() 269func Test_lua_window_isvalid() 270 new Xfoo 271 lua w = vim.window() 272 call assert_true(luaeval('w:isvalid()')) 273 274 " FIXME: how to test the case when isvalid() returns v:false? 275 " isvalid() gives errors when the window is deleted. Is it a bug? 276 277 lua w = nil 278 bwipe! 279endfunc 280 281" Test vim.buffer() with and without argument 282func Test_lua_buffer() 283 new Xfoo1 284 let bn1 = bufnr('%') 285 new Xfoo2 286 let bn2 = bufnr('%') 287 288 " Test vim.buffer() without argument. 289 call assert_equal('Xfoo2', luaeval("vim.buffer().name")) 290 291 " Test vim.buffer() with string argument. 292 call assert_equal('Xfoo1', luaeval("vim.buffer('Xfoo1').name")) 293 call assert_equal('Xfoo2', luaeval("vim.buffer('Xfoo2').name")) 294 295 " Test vim.buffer() with integer argument. 296 call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name")) 297 call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name")) 298 299 lua bn1, bn2 = nil 300 %bwipe! 301endfunc 302 303" Test vim.buffer().name and vim.buffer().fname 304func Test_lua_buffer_name() 305 new 306 call assert_equal('', luaeval('vim.buffer().name')) 307 call assert_equal('', luaeval('vim.buffer().fname')) 308 bwipe! 309 310 new Xfoo 311 call assert_equal('Xfoo', luaeval('vim.buffer().name')) 312 call assert_equal(expand('%:p'), luaeval('vim.buffer().fname')) 313 bwipe! 314endfunc 315 316" Test vim.buffer().number 317func Test_lua_buffer_number() 318 " All numbers in Lua are floating points number (no integers). 319 call assert_equal(bufnr('%'), float2nr(luaeval('vim.buffer().number'))) 320endfunc 321 322" Test inserting lines in buffer. 323func Test_lua_buffer_insert() 324 new 325 lua vim.buffer()[1] = '3' 326 lua vim.buffer():insert('1', 0) 327 lua vim.buffer():insert('2', 1) 328 lua vim.buffer():insert('4', 10) 329 330 call assert_equal(['1', '2', '3', '4'], getline(1, '$')) 331 call assert_equal('4', luaeval('vim.buffer()[4]')) 332 call assert_equal(v:null, luaeval('vim.buffer()[5]')) 333 call assert_equal(v:null, luaeval('vim.buffer()[{}]')) 334 call assert_fails('lua vim.buffer():xyz()', 335 \ s:lua_53_or_later 336 \ ? "[string \"vim chunk\"]:1: attempt to call a nil value (method 'xyz')" 337 \ : "[string \"vim chunk\"]:1: attempt to call method 'xyz' (a nil value)") 338 call assert_fails('lua vim.buffer()[1] = {}', 339 \ '[string "vim chunk"]:1: wrong argument to change') 340 bwipe! 341endfunc 342 343" Test deleting line in buffer 344func Test_lua_buffer_delete() 345 new 346 call setline(1, ['1', '2', '3']) 347 call cursor(3, 1) 348 lua vim.buffer()[2] = nil 349 call assert_equal(['1', '3'], getline(1, '$')) 350 351 call assert_fails('lua vim.buffer()[3] = nil', 352 \ '[string "vim chunk"]:1: invalid line number') 353 bwipe! 354endfunc 355 356" Test #vim.buffer() i.e. number of lines in buffer 357func Test_lua_buffer_number_lines() 358 new 359 call setline(1, ['a', 'b', 'c']) 360 call assert_equal(3, luaeval('#vim.buffer()')) 361 bwipe! 362endfunc 363 364" Test vim.buffer():next() and vim.buffer():previous() 365" Note that these functions get the next or previous buffers 366" but do not switch buffer. 367func Test_lua_buffer_next_previous() 368 new Xfoo1 369 new Xfoo2 370 new Xfoo3 371 b Xfoo2 372 373 lua bn = vim.buffer():next() 374 lua bp = vim.buffer():previous() 375 376 call assert_equal('Xfoo2', luaeval('vim.buffer().name')) 377 call assert_equal('Xfoo1', luaeval('bp.name')) 378 call assert_equal('Xfoo3', luaeval('bn.name')) 379 380 call assert_equal('Xfoo2', bufname('%')) 381 382 lua bn() 383 call assert_equal('Xfoo3', luaeval('vim.buffer().name')) 384 call assert_equal('Xfoo3', bufname('%')) 385 386 lua bp() 387 call assert_equal('Xfoo1', luaeval('vim.buffer().name')) 388 call assert_equal('Xfoo1', bufname('%')) 389 390 lua bn, bp = nil 391 %bwipe! 392endfunc 393 394" Test vim.buffer():isvalid() 395func Test_lua_buffer_isvalid() 396 new Xfoo 397 lua b = vim.buffer() 398 call assert_true(luaeval('b:isvalid()')) 399 400 " FIXME: how to test the case when isvalid() returns v:false? 401 " isvalid() gives errors when the buffer is wiped. Is it a bug? 402 403 lua b = nil 404 bwipe! 405endfunc 406 407func Test_lua_list() 408 call assert_equal([], luaeval('vim.list()')) 409 410 let l = [] 411 lua l = vim.eval('l') 412 lua l:add(123) 413 lua l:add('abc') 414 lua l:add(true) 415 lua l:add(false) 416 lua l:add(nil) 417 lua l:add(vim.eval("[1, 2, 3]")) 418 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}")) 419 call assert_equal([123, 'abc', v:true, v:false, v:null, [1, 2, 3], {'a': 1, 'b': 2, 'c': 3}], l) 420 call assert_equal(7, luaeval('#l')) 421 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) 422 423 lua l[1] = 124 424 lua l[6] = nil 425 lua l:insert('first') 426 lua l:insert('xx', 3) 427 call assert_fails('lua l:insert("xx", -20)', 428 \ '[string "vim chunk"]:1: invalid position') 429 call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l) 430 431 lockvar 1 l 432 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked') 433 call assert_fails('lua l:insert(2)', '[string "vim chunk"]:1: list is locked') 434 call assert_fails('lua l[9] = 1', '[string "vim chunk"]:1: list is locked') 435 436 unlockvar l 437 let l = [1, 2] 438 lua ll = vim.eval('l') 439 let x = luaeval("ll[3]") 440 call assert_equal(v:null, x) 441 call assert_fails('let x = luaeval("ll:xyz(3)")', 442 \ s:lua_53_or_later 443 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')" 444 \ : "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)") 445 let y = luaeval("ll[{}]") 446 call assert_equal(v:null, y) 447 448 lua l = nil 449endfunc 450 451func Test_lua_list_table() 452 " See :help lua-vim 453 " Non-numeric keys should not be used to initialize the list 454 " so say = 'hi' should be ignored. 455 lua t = {3.14, 'hello', false, true, say = 'hi'} 456 call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)')) 457 lua t = nil 458 459 call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number') 460 call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string') 461 call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function') 462 call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean') 463endfunc 464 465func Test_lua_list_table_insert_remove() 466 if !s:lua_53_or_later 467 throw 'Skipped: Lua version < 5.3' 468 endif 469 470 let l = [1, 2] 471 lua t = vim.eval('l') 472 lua table.insert(t, 10) 473 lua t[#t + 1] = 20 474 lua table.insert(t, 2, 30) 475 call assert_equal(l, [1, 30, 2, 10, 20]) 476 lua table.remove(t, 2) 477 call assert_equal(l, [1, 2, 10, 20]) 478 lua t[3] = nil 479 call assert_equal(l, [1, 2, 20]) 480 lua removed_value = table.remove(t, 3) 481 call assert_equal(luaeval('removed_value'), 20) 482 lua t = nil 483 lua removed_value = nil 484 unlet l 485endfunc 486 487" Test l() i.e. iterator on list 488func Test_lua_list_iter() 489 lua l = vim.list():add('foo'):add('bar') 490 lua str = '' 491 lua for v in l() do str = str .. v end 492 call assert_equal('foobar', luaeval('str')) 493 494 lua str, l = nil 495endfunc 496 497func Test_lua_recursive_list() 498 lua l = vim.list():add(1):add(2) 499 lua l = l:add(l) 500 501 call assert_equal(1, luaeval('l[1]')) 502 call assert_equal(2, luaeval('l[2]')) 503 504 call assert_equal(1, luaeval('l[3][1]')) 505 call assert_equal(2, luaeval('l[3][2]')) 506 507 call assert_equal(1, luaeval('l[3][3][1]')) 508 call assert_equal(2, luaeval('l[3][3][2]')) 509 510 call assert_equal('[1, 2, [...]]', string(luaeval('l'))) 511 512 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) 513 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])')) 514 515 call assert_equal(luaeval('l'), luaeval('l[3]')) 516 call assert_equal(luaeval('l'), luaeval('l[3][3]')) 517 518 lua l = nil 519endfunc 520 521func Test_lua_dict() 522 call assert_equal({}, luaeval('vim.dict()')) 523 524 let d = {} 525 lua d = vim.eval('d') 526 lua d[0] = 123 527 lua d[1] = "abc" 528 lua d[2] = true 529 lua d[3] = false 530 lua d[4] = vim.eval("[1, 2, 3]") 531 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}") 532 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) 533 call assert_equal(6, luaeval('#d')) 534 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)')) 535 536 call assert_equal('abc', luaeval('d[1]')) 537 call assert_equal(v:null, luaeval('d[22]')) 538 539 lua d[0] = 124 540 lua d[4] = nil 541 call assert_equal({'0':124, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d) 542 543 lockvar 1 d 544 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked') 545 unlockvar d 546 547 " Error case 548 lua d = {} 549 lua d[''] = 10 550 call assert_fails("let t = luaeval('vim.dict(d)')", 551 \ s:lua_53_or_later 552 \ ? '[string "luaeval"]:1: table has empty key' 553 \ : 'table has empty key') 554 let d = {} 555 lua x = vim.eval('d') 556 call assert_fails("lua x[''] = 10", '[string "vim chunk"]:1: empty key') 557 lua x['a'] = nil 558 call assert_equal({}, d) 559 560 " cannot assign funcrefs in the global scope 561 lua x = vim.eval('g:') 562 call assert_fails("lua x['min'] = vim.funcref('max')", 563 \ '[string "vim chunk"]:1: cannot assign funcref to builtin scope') 564 565 lua d = nil 566endfunc 567 568func Test_lua_dict_table() 569 lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false} 570 call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false}, 571 \ luaeval('vim.dict(t)')) 572 573 " Same example as in :help lua-vim. 574 lua t = {math.pi, false, say = 'hi'} 575 " FIXME: commented out as it currently does not work as documented: 576 " Expected {'say': 'hi'} 577 " but got {'1': 3.141593, '2': v:false, 'say': 'hi'} 578 " Is the documentation or the code wrong? 579 "call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)')) 580 lua t = nil 581 582 call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number') 583 call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string') 584 call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function') 585 call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean') 586endfunc 587 588" Test d() i.e. iterator on dictionary 589func Test_lua_dict_iter() 590 let d = {'a': 1, 'b':2} 591 lua d = vim.eval('d') 592 lua str = '' 593 lua for k,v in d() do str = str .. k ..':' .. v .. ',' end 594 call assert_equal('a:1,b:2,', luaeval('str')) 595 596 lua str, d = nil 597endfunc 598 599func Test_lua_blob() 600 call assert_equal(0z, luaeval('vim.blob("")')) 601 call assert_equal(0z31326162, luaeval('vim.blob("12ab")')) 602 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")')) 603 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")')) 604 605 lua b = vim.blob("\x00\x00\x00\x00") 606 call assert_equal(0z00000000, luaeval('b')) 607 call assert_equal(4, luaeval('#b')) 608 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff 609 call assert_equal(0z012000ff, luaeval('b')) 610 lua b[4] = string.byte("z", 1) 611 call assert_equal(0z012000ff.7a, luaeval('b')) 612 call assert_equal(5, luaeval('#b')) 613 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range') 614 lua b:add("12ab") 615 call assert_equal(0z012000ff.7a313261.62, luaeval('b')) 616 call assert_equal(9, luaeval('#b')) 617 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil') 618 call assert_fails('lua b:add(true)', '[string "vim chunk"]:1: string expected, got boolean') 619 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table') 620 lua b = nil 621 622 let b = 0z0102 623 lua lb = vim.eval('b') 624 let n = luaeval('lb[1]') 625 call assert_equal(2, n) 626 let n = luaeval('lb[6]') 627 call assert_equal(v:null, n) 628 call assert_fails('let x = luaeval("lb:xyz(3)")', 629 \ s:lua_53_or_later 630 \ ? "[string \"luaeval\"]:1: attempt to call a nil value (method 'xyz')" 631 \ : "[string \"luaeval\"]:1: attempt to call method 'xyz' (a nil value)") 632 let y = luaeval("lb[{}]") 633 call assert_equal(v:null, y) 634 635 lockvar b 636 call assert_fails('lua lb[1] = 2', '[string "vim chunk"]:1: blob is locked') 637 call assert_fails('lua lb:add("12")', '[string "vim chunk"]:1: blob is locked') 638 639 " Error cases 640 lua t = {} 641 call assert_fails('lua b = vim.blob(t)', 642 \ '[string "vim chunk"]:1: string expected, got table') 643endfunc 644 645func Test_lua_funcref() 646 function I(x) 647 return a:x 648 endfunction 649 let R = function('I') 650 lua i1 = vim.funcref"I" 651 lua i2 = vim.eval"R" 652 lua msg = "funcref|test|" .. (#i2(i1) == #i1(i2) and "OK" or "FAIL") 653 lua msg = vim.funcref"tr"(msg, "|", " ") 654 call assert_equal("funcref test OK", luaeval('msg')) 655 656 " Error cases 657 call assert_fails('lua f1 = vim.funcref("")', 658 \ '[string "vim chunk"]:1: invalid function name: ') 659 call assert_fails('lua f1 = vim.funcref("10")', 660 \ '[string "vim chunk"]:1: invalid function name: 10') 661 let fname = test_null_string() 662 call assert_fails('lua f1 = vim.funcref(fname)', 663 \ "[string \"vim chunk\"]:1: bad argument #1 to 'funcref' (string expected, got nil)") 664 call assert_fails('lua vim.funcref("abc")()', 665 \ ['E117:', '\[string "vim chunk"]:1: cannot call funcref']) 666 667 " dict funcref 668 function Mylen() dict 669 return len(self.data) 670 endfunction 671 let l = [0, 1, 2, 3] 672 let mydict = {'data': l} 673 lua d = vim.eval"mydict" 674 lua d.len = vim.funcref"Mylen" -- assign d as 'self' 675 lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL" 676 call assert_equal("OK", luaeval('res')) 677 call assert_equal(function('Mylen', {'data': l, 'len': function('Mylen')}), mydict.len) 678 679 lua i1, i2, msg, d, res = nil 680endfunc 681 682" Test vim.type() 683func Test_lua_type() 684 " The following values are identical to Lua's type function. 685 call assert_equal('string', luaeval('vim.type("foo")')) 686 call assert_equal('number', luaeval('vim.type(1)')) 687 call assert_equal('number', luaeval('vim.type(1.2)')) 688 call assert_equal('function', luaeval('vim.type(print)')) 689 call assert_equal('table', luaeval('vim.type({})')) 690 call assert_equal('boolean', luaeval('vim.type(true)')) 691 call assert_equal('boolean', luaeval('vim.type(false)')) 692 call assert_equal('nil', luaeval('vim.type(nil)')) 693 694 " The following values are specific to Vim. 695 call assert_equal('window', luaeval('vim.type(vim.window())')) 696 call assert_equal('buffer', luaeval('vim.type(vim.buffer())')) 697 call assert_equal('list', luaeval('vim.type(vim.list())')) 698 call assert_equal('dict', luaeval('vim.type(vim.dict())')) 699 call assert_equal('funcref', luaeval('vim.type(vim.funcref("Test_type"))')) 700endfunc 701 702" Test vim.open() 703func Test_lua_open() 704 call assert_notmatch('XOpen', execute('ls')) 705 706 " Open a buffer XOpen1, but do not jump to it. 707 lua b = vim.open('XOpen1') 708 call assert_equal('XOpen1', luaeval('b.name')) 709 call assert_equal('', bufname('%')) 710 711 call assert_match('XOpen1', execute('ls')) 712 call assert_notequal('XOpen2', bufname('%')) 713 714 " Open a buffer XOpen2 and jump to it. 715 lua b = vim.open('XOpen2')() 716 call assert_equal('XOpen2', luaeval('b.name')) 717 call assert_equal('XOpen2', bufname('%')) 718 719 lua b = nil 720 %bwipe! 721endfunc 722 723func Test_update_package_paths() 724 set runtimepath+=./testluaplugin 725 call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()")) 726endfunc 727 728func Vim_func_call_lua_callback(Concat, Cb) 729 let l:message = a:Concat("hello", "vim") 730 call a:Cb(l:message) 731endfunc 732 733func Test_pass_lua_callback_to_vim_from_lua() 734 lua pass_lua_callback_to_vim_from_lua_result = "" 735 call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result")) 736 lua <<EOF 737 vim.funcref('Vim_func_call_lua_callback')( 738 function(greeting, message) 739 return greeting .. " " .. message 740 end, 741 function(message) 742 pass_lua_callback_to_vim_from_lua_result = message 743 end) 744EOF 745 call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result")) 746endfunc 747 748func Vim_func_call_metatable_lua_callback(Greet) 749 return a:Greet("world") 750endfunc 751 752func Test_pass_lua_metatable_callback_to_vim_from_lua() 753 let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )") 754 call assert_equal("hello world", result) 755endfunc 756 757" Test vim.line() 758func Test_lua_line() 759 new 760 call setline(1, ['first line', 'second line']) 761 1 762 call assert_equal('first line', luaeval('vim.line()')) 763 2 764 call assert_equal('second line', luaeval('vim.line()')) 765 bwipe! 766endfunc 767 768" Test vim.beep() 769func Test_lua_beep() 770 call assert_beeps('lua vim.beep()') 771endfunc 772 773" Test errors in luaeval() 774func Test_luaeval_error() 775 " Compile error 776 call assert_fails("call luaeval('-nil')", 777 \ '[string "luaeval"]:1: attempt to perform arithmetic on a nil value') 778 call assert_fails("call luaeval(']')", 779 \ "[string \"luaeval\"]:1: unexpected symbol near ']'") 780 lua co = coroutine.create(function () print("hi") end) 781 call assert_fails('let i = luaeval("co")', 'luaeval: cannot convert value') 782 lua co = nil 783 call assert_fails('let m = luaeval("{}")', 'luaeval: cannot convert value') 784endfunc 785 786" Test :luafile foo.lua 787func Test_luafile() 788 call delete('Xlua_file') 789 call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file') 790 call setfperm('Xlua_file', 'r-xr-xr-x') 791 792 luafile Xlua_file 793 call assert_equal('hello', luaeval('str')) 794 call assert_equal(123, luaeval('num')) 795 796 lua str, num = nil 797 call delete('Xlua_file') 798endfunc 799 800" Test :luafile % 801func Test_luafile_percent() 802 new Xlua_file 803 append 804 str, num = 'foo', 321.0 805 print(string.format('str=%s, num=%d', str, num)) 806. 807 w! 808 luafile % 809 let msg = split(execute('message'), "\n")[-1] 810 call assert_equal('str=foo, num=321', msg) 811 812 lua str, num = nil 813 call delete('Xlua_file') 814 bwipe! 815endfunc 816 817" Test :luafile with syntax error 818func Test_luafile_error() 819 new Xlua_file 820 call writefile(['nil = 0' ], 'Xlua_file') 821 call setfperm('Xlua_file', 'r-xr-xr-x') 822 823 call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'") 824 825 call delete('Xlua_file') 826 bwipe! 827endfunc 828 829" Test for dealing with strings containing newlines and null character 830func Test_lua_string_with_newline() 831 let x = execute('lua print("Hello\nWorld")') 832 call assert_equal("\nHello\nWorld", x) 833 new 834 lua k = vim.buffer(vim.eval('bufnr()')) 835 lua k:insert("Hello\0World", 0) 836 call assert_equal(["Hello\nWorld", ''], getline(1, '$')) 837 close! 838endfunc 839 840func Test_lua_set_cursor() 841 " Check that setting the cursor position works. 842 new 843 call setline(1, ['first line', 'second line']) 844 normal gg 845 lua << trim EOF 846 w = vim.window() 847 w.line = 1 848 w.col = 5 849 EOF 850 call assert_equal([1, 5], [line('.'), col('.')]) 851 852 " Check that movement after setting cursor position keeps current column. 853 normal j 854 call assert_equal([2, 5], [line('.'), col('.')]) 855endfunc 856 857" Test for various heredoc syntax 858func Test_lua_heredoc() 859 lua << END 860vim.command('let s = "A"') 861END 862 lua << 863vim.command('let s ..= "B"') 864. 865 lua << trim END 866 vim.command('let s ..= "C"') 867 END 868 lua << trim 869 vim.command('let s ..= "D"') 870 . 871 lua << trim eof 872 vim.command('let s ..= "E"') 873 eof 874 call assert_equal('ABCDE', s) 875endfunc 876 877" vim: shiftwidth=2 sts=2 expandtab 878