Mercurial > vim
comparison src/testdir/test_lua.vim @ 20441:86dde5c4b375 v8.2.0775
patch 8.2.0775: not easy to call a Vim function from Lua
Commit: https://github.com/vim/vim/commit/eb04f0893afe01faff272ef84c70d8cc16d8e80a
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun May 17 14:32:35 2020 +0200
patch 8.2.0775: not easy to call a Vim function from Lua
Problem: Not easy to call a Vim function from Lua.
Solution: Add vim.call() and vim.fn(). (Prabir Shrestha, closes https://github.com/vim/vim/issues/6063)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 17 May 2020 14:45:04 +0200 |
parents | 5f9c2c7d3d73 |
children | 054ba681412d |
comparison
equal
deleted
inserted
replaced
20440:74db00344bf6 | 20441:86dde5c4b375 |
---|---|
31 " Test vim.eval() | 31 " Test vim.eval() |
32 func Test_lua_eval() | 32 func Test_lua_eval() |
33 " lua.eval with a number | 33 " lua.eval with a number |
34 lua v = vim.eval('123') | 34 lua v = vim.eval('123') |
35 call assert_equal('number', luaeval('vim.type(v)')) | 35 call assert_equal('number', luaeval('vim.type(v)')) |
36 call assert_equal(123.0, luaeval('v')) | 36 call assert_equal(123, luaeval('v')) |
37 | 37 |
38 " lua.eval with a string | 38 " lua.eval with a string |
39 lua v = vim.eval('"abc"') | 39 lua v = vim.eval('"abc"') |
40 call assert_equal('string', 'vim.type(v)'->luaeval()) | 40 call assert_equal('string', 'vim.type(v)'->luaeval()) |
41 call assert_equal('abc', luaeval('v')) | 41 call assert_equal('abc', luaeval('v')) |
119 call assert_fails('lua vim.window().line = 10', | 119 call assert_fails('lua vim.window().line = 10', |
120 \ '[string "vim chunk"]:1: line out of range') | 120 \ '[string "vim chunk"]:1: line out of range') |
121 bwipe! | 121 bwipe! |
122 endfunc | 122 endfunc |
123 | 123 |
124 " Test vim.call | |
125 func 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")')) | |
128 endfunc | |
129 | |
130 " Test vim.fn.* | |
131 func 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")')) | |
134 endfunc | |
135 | |
124 " Test setting the current window | 136 " Test setting the current window |
125 func Test_lua_window_set_current() | 137 func Test_lua_window_set_current() |
126 new Xfoo1 | 138 new Xfoo1 |
127 lua w1 = vim.window() | 139 lua w1 = vim.window() |
128 new Xfoo2 | 140 new Xfoo2 |
250 | 262 |
251 " Test #vim.buffer() i.e. number of lines in buffer | 263 " Test #vim.buffer() i.e. number of lines in buffer |
252 func Test_lua_buffer_number_lines() | 264 func Test_lua_buffer_number_lines() |
253 new | 265 new |
254 call setline(1, ['a', 'b', 'c']) | 266 call setline(1, ['a', 'b', 'c']) |
255 call assert_equal(3.0, luaeval('#vim.buffer()')) | 267 call assert_equal(3, luaeval('#vim.buffer()')) |
256 bwipe! | 268 bwipe! |
257 endfunc | 269 endfunc |
258 | 270 |
259 " Test vim.buffer():next() and vim.buffer():previous() | 271 " Test vim.buffer():next() and vim.buffer():previous() |
260 " Note that these functions get the next or previous buffers | 272 " Note that these functions get the next or previous buffers |
309 lua l:add(true) | 321 lua l:add(true) |
310 lua l:add(false) | 322 lua l:add(false) |
311 lua l:add(nil) | 323 lua l:add(nil) |
312 lua l:add(vim.eval("[1, 2, 3]")) | 324 lua l:add(vim.eval("[1, 2, 3]")) |
313 lua l:add(vim.eval("{'a':1, 'b':2, 'c':3}")) | 325 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) | 326 call assert_equal([123, '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')) | 327 call assert_equal(7, luaeval('#l')) |
316 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) | 328 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) |
317 | 329 |
318 lua l[0] = 124 | 330 lua l[0] = 124 |
319 lua l[5] = nil | 331 lua l[5] = nil |
320 lua l:insert('first') | 332 lua l:insert('first') |
321 lua l:insert('xx', 3) | 333 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) | 334 call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l) |
323 | 335 |
324 lockvar 1 l | 336 lockvar 1 l |
325 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked') | 337 call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked') |
326 | 338 |
327 lua l = nil | 339 lua l = nil |
353 | 365 |
354 func Test_lua_recursive_list() | 366 func Test_lua_recursive_list() |
355 lua l = vim.list():add(1):add(2) | 367 lua l = vim.list():add(1):add(2) |
356 lua l = l:add(l) | 368 lua l = l:add(l) |
357 | 369 |
358 call assert_equal(1.0, luaeval('l[0]')) | 370 call assert_equal(1, luaeval('l[0]')) |
359 call assert_equal(2.0, luaeval('l[1]')) | 371 call assert_equal(2, luaeval('l[1]')) |
360 | 372 |
361 call assert_equal(1.0, luaeval('l[2][0]')) | 373 call assert_equal(1, luaeval('l[2][0]')) |
362 call assert_equal(2.0, luaeval('l[2][1]')) | 374 call assert_equal(2, luaeval('l[2][1]')) |
363 | 375 |
364 call assert_equal(1.0, luaeval('l[2][2][0]')) | 376 call assert_equal(1, luaeval('l[2][2][0]')) |
365 call assert_equal(2.0, luaeval('l[2][2][1]')) | 377 call assert_equal(2, luaeval('l[2][2][1]')) |
366 | 378 |
367 call assert_equal('[1.0, 2.0, [...]]', string(luaeval('l'))) | 379 call assert_equal('[1, 2, [...]]', string(luaeval('l'))) |
368 | 380 |
369 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) | 381 call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) |
370 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[2])')) | 382 call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[2])')) |
371 | 383 |
372 call assert_equal(luaeval('l'), luaeval('l[2]')) | 384 call assert_equal(luaeval('l'), luaeval('l[2]')) |
384 lua d[1] = "abc" | 396 lua d[1] = "abc" |
385 lua d[2] = true | 397 lua d[2] = true |
386 lua d[3] = false | 398 lua d[3] = false |
387 lua d[4] = vim.eval("[1, 2, 3]") | 399 lua d[4] = vim.eval("[1, 2, 3]") |
388 lua d[5] = vim.eval("{'a':1, 'b':2, 'c':3}") | 400 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) | 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) |
390 call assert_equal(6.0, luaeval('#d')) | 402 call assert_equal(6, luaeval('#d')) |
391 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)')) | 403 call assert_match('^dict: \%(0x\)\?\x\+$', luaeval('tostring(d)')) |
392 | 404 |
393 call assert_equal('abc', luaeval('d[1]')) | 405 call assert_equal('abc', luaeval('d[1]')) |
394 | 406 |
395 lua d[0] = 124 | 407 lua d[0] = 124 |
396 lua d[4] = nil | 408 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) | 409 call assert_equal({'0':124, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d) |
398 | 410 |
399 lockvar 1 d | 411 lockvar 1 d |
400 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked') | 412 call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked') |
401 | 413 |
402 lua d = nil | 414 lua d = nil |
439 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")')) | 451 call assert_equal(0z00010203, luaeval('vim.blob("\x00\x01\x02\x03")')) |
440 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")')) | 452 call assert_equal(0z8081FEFF, luaeval('vim.blob("\x80\x81\xfe\xff")')) |
441 | 453 |
442 lua b = vim.blob("\x00\x00\x00\x00") | 454 lua b = vim.blob("\x00\x00\x00\x00") |
443 call assert_equal(0z00000000, luaeval('b')) | 455 call assert_equal(0z00000000, luaeval('b')) |
444 call assert_equal(4.0, luaeval('#b')) | 456 call assert_equal(4, luaeval('#b')) |
445 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff | 457 lua b[0], b[1], b[2], b[3] = 1, 32, 256, 0xff |
446 call assert_equal(0z012000ff, luaeval('b')) | 458 call assert_equal(0z012000ff, luaeval('b')) |
447 lua b[4] = string.byte("z", 1) | 459 lua b[4] = string.byte("z", 1) |
448 call assert_equal(0z012000ff.7a, luaeval('b')) | 460 call assert_equal(0z012000ff.7a, luaeval('b')) |
449 call assert_equal(5.0, luaeval('#b')) | 461 call assert_equal(5, luaeval('#b')) |
450 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range') | 462 call assert_fails('lua b[#b+1] = 0x80', '[string "vim chunk"]:1: index out of range') |
451 lua b:add("12ab") | 463 lua b:add("12ab") |
452 call assert_equal(0z012000ff.7a313261.62, luaeval('b')) | 464 call assert_equal(0z012000ff.7a313261.62, luaeval('b')) |
453 call assert_equal(9.0, luaeval('#b')) | 465 call assert_equal(9, luaeval('#b')) |
454 call assert_fails('lua b:add(nil)', '[string "vim chunk"]:1: string expected, got nil') | 466 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') | 467 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') | 468 call assert_fails('lua b:add({})', '[string "vim chunk"]:1: string expected, got table') |
457 lua b = nil | 469 lua b = nil |
458 endfunc | 470 endfunc |
550 endfunc | 562 endfunc |
551 | 563 |
552 " Test :luafile foo.lua | 564 " Test :luafile foo.lua |
553 func Test_luafile() | 565 func Test_luafile() |
554 call delete('Xlua_file') | 566 call delete('Xlua_file') |
555 call writefile(["str = 'hello'", "num = 123.0" ], 'Xlua_file') | 567 call writefile(["str = 'hello'", "num = 123" ], 'Xlua_file') |
556 call setfperm('Xlua_file', 'r-xr-xr-x') | 568 call setfperm('Xlua_file', 'r-xr-xr-x') |
557 | 569 |
558 luafile Xlua_file | 570 luafile Xlua_file |
559 call assert_equal('hello', luaeval('str')) | 571 call assert_equal('hello', luaeval('str')) |
560 call assert_equal(123.0, luaeval('num')) | 572 call assert_equal(123, luaeval('num')) |
561 | 573 |
562 lua str, num = nil | 574 lua str, num = nil |
563 call delete('Xlua_file') | 575 call delete('Xlua_file') |
564 endfunc | 576 endfunc |
565 | 577 |