Skip to content

Commit 2f362bf

Browse files
committed
patch 8.1.0136: Lua tests don't cover new features
Problem: Lua tests don't cover new features. Solution: Add more tests. (Dominique Pelle, closes #3130)
1 parent e042968 commit 2f362bf

3 files changed

Lines changed: 67 additions & 24 deletions

File tree

runtime/doc/if_lua.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Vim evaluation and command execution, and others.
127127
rules. Example: >
128128
:lua t = {math.pi, false, say = 'hi'}
129129
:echo luaeval('vim.list(t)')
130-
:" [3.141593, 0], 'say' is ignored
130+
:" [3.141593, v:false], 'say' is ignored
131131
<
132132
vim.dict([arg]) Returns an empty dictionary or, if "arg" is a
133133
Lua table, returns a dict d such that d[k] =
@@ -141,8 +141,7 @@ Vim evaluation and command execution, and others.
141141
:" {'say': 'hi'}, numeric keys ignored
142142
<
143143
vim.funcref({name}) Returns a Funcref to function {name} (see
144-
|Funcref|). It is equivalent to Vim's
145-
"function". NOT IMPLEMENTED YET
144+
|Funcref|). It is equivalent to Vim's function().
146145

147146
vim.buffer([arg]) If "arg" is a number, returns buffer with
148147
number "arg" in the buffer list or, if "arg"
@@ -166,7 +165,7 @@ Vim evaluation and command execution, and others.
166165
or window, respectively. Examples: >
167166
:lua l = vim.list()
168167
:lua print(type(l), vim.type(l))
169-
:" userdata list
168+
:" list
170169
<
171170
vim.command({cmd}) Executes the vim (ex-mode) command {cmd}.
172171
Examples: >

src/testdir/test_lua.vim

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func Test_window_set_current()
124124
lua w2()
125125
call assert_equal('Xfoo2', bufname('%'))
126126

127-
lua w1, w2 = nil, nil
127+
lua w1, w2 = nil
128128
%bwipe!
129129
endfunc
130130

@@ -142,7 +142,7 @@ func Test_window_buffer()
142142
lua b2()
143143
call assert_equal('Xfoo2', bufname('%'))
144144

145-
lua b1, b2 = nil, nil
145+
lua b1, b2, w1, w2 = nil
146146
%bwipe!
147147
endfunc
148148

@@ -191,7 +191,7 @@ func Test_buffer()
191191
call assert_equal('Xfoo1', luaeval("vim.buffer(" . bn1 . ").name"))
192192
call assert_equal('Xfoo2', luaeval("vim.buffer(" . bn2 . ").name"))
193193

194-
lua bn1, bn2 = nil, nil
194+
lua bn1, bn2 = nil
195195
%bwipe!
196196
endfunc
197197

@@ -275,7 +275,7 @@ func Test_buffer_next_previous()
275275
call assert_equal('Xfoo1', luaeval('vim.buffer().name'))
276276
call assert_equal('Xfoo1', bufname('%'))
277277

278-
lua bn, bp = nil, nil
278+
lua bn, bp = nil
279279
%bwipe!
280280
endfunc
281281

@@ -295,12 +295,6 @@ endfunc
295295
func Test_list()
296296
call assert_equal([], luaeval('vim.list()'))
297297

298-
" Same example as in :help lua-vim.
299-
" FIXME: test is disabled because it does not work.
300-
" See https://github.com/vim/vim/issues/3086
301-
" lua t = {math.pi, false, say = 'hi'}
302-
" call assert_equal([3.141593, 0], luaeval('vim.list(t)'))
303-
304298
let l = []
305299
lua l = vim.eval('l')
306300
lua l:add(123)
@@ -319,17 +313,34 @@ func Test_list()
319313
lua l:insert('xx', 3)
320314
call assert_equal(['first', 124.0, 'abc', 'xx', v:true, v:false, {'a': 1, 'b': 2, 'c': 3}], l)
321315

316+
lockvar 1 l
317+
call assert_fails('lua l:add("x")', '[string "vim chunk"]:1: list is locked')
318+
322319
lua l = nil
323320
endfunc
324321

322+
func Test_list_table()
323+
" See :help lua-vim
324+
" Non-numeric keys should not be used to initialize the list
325+
" so say = 'hi' should be ignored.
326+
lua t = {3.14, 'hello', false, true, say = 'hi'}
327+
call assert_equal([3.14, 'hello', v:false, v:true], luaeval('vim.list(t)'))
328+
lua t = nil
329+
330+
call assert_fails('lua vim.list(1)', '[string "vim chunk"]:1: table expected, got number')
331+
call assert_fails('lua vim.list("x")', '[string "vim chunk"]:1: table expected, got string')
332+
call assert_fails('lua vim.list(print)', '[string "vim chunk"]:1: table expected, got function')
333+
call assert_fails('lua vim.list(true)', '[string "vim chunk"]:1: table expected, got boolean')
334+
endfunc
335+
325336
" Test l() i.e. iterator on list
326337
func Test_list_iter()
327338
lua l = vim.list():add('foo'):add('bar')
328339
lua str = ''
329340
lua for v in l() do str = str .. v end
330341
call assert_equal('foobar', luaeval('str'))
331342

332-
lua str, v, l = nil, nil, nil
343+
lua str, l = nil
333344
endfunc
334345

335346
func Test_recursive_list()
@@ -359,12 +370,6 @@ endfunc
359370
func Test_dict()
360371
call assert_equal({}, luaeval('vim.dict()'))
361372

362-
" Same example as in :help lua-vim.
363-
" FIXME: test is disabled because it does not work.
364-
" See https://github.com/vim/vim/issues/3086
365-
" lua t = {math.pi, false, say = 'hi'}
366-
" call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)'))
367-
368373
let d = {}
369374
lua d = vim.eval('d')
370375
lua d[0] = 123
@@ -383,9 +388,32 @@ func Test_dict()
383388
lua d[4] = nil
384389
call assert_equal({'0':124.0, '1':'abc', '2':v:true, '3':v:false, '5': {'a':1, 'b':2, 'c':3}}, d)
385390

391+
lockvar 1 d
392+
call assert_fails('lua d[6] = 1', '[string "vim chunk"]:1: dict is locked')
393+
386394
lua d = nil
387395
endfunc
388396

397+
func Test_dict_table()
398+
lua t = {key1 = 'x', key2 = 3.14, key3 = true, key4 = false}
399+
call assert_equal({'key1': 'x', 'key2': 3.14, 'key3': v:true, 'key4': v:false},
400+
\ luaeval('vim.dict(t)'))
401+
402+
" Same example as in :help lua-vim.
403+
lua t = {math.pi, false, say = 'hi'}
404+
" FIXME: commented out as it currently does not work as documented:
405+
" Expected {'say': 'hi'}
406+
" but got {'1': 3.141593, '2': v:false, 'say': 'hi'}
407+
" Is the documentation or the code wrong?
408+
"call assert_equal({'say' : 'hi'}, luaeval('vim.dict(t)'))
409+
lua t = nil
410+
411+
call assert_fails('lua vim.dict(1)', '[string "vim chunk"]:1: table expected, got number')
412+
call assert_fails('lua vim.dict("x")', '[string "vim chunk"]:1: table expected, got string')
413+
call assert_fails('lua vim.dict(print)', '[string "vim chunk"]:1: table expected, got function')
414+
call assert_fails('lua vim.dict(true)', '[string "vim chunk"]:1: table expected, got boolean')
415+
endfunc
416+
389417
" Test d() i.e. iterator on dictionary
390418
func Test_dict_iter()
391419
let d = {'a': 1, 'b':2}
@@ -394,7 +422,7 @@ func Test_dict_iter()
394422
lua for k,v in d() do str = str .. k ..':' .. v .. ',' end
395423
call assert_equal('a:1,b:2,', luaeval('str'))
396424

397-
lua str, k, v, d = nil, nil, nil, nil
425+
lua str, d = nil
398426
endfunc
399427

400428
func Test_funcref()
@@ -418,6 +446,8 @@ func Test_funcref()
418446
lua d.len = vim.funcref"Mylen" -- assign d as 'self'
419447
lua res = (d.len() == vim.funcref"len"(vim.eval"l")) and "OK" or "FAIL"
420448
call assert_equal("OK", luaeval('res'))
449+
450+
lua i1, i2, msg, d, res = nil
421451
endfunc
422452

423453
" Test vim.type()
@@ -496,7 +526,7 @@ func Test_luafile()
496526
call assert_equal('hello', luaeval('str'))
497527
call assert_equal(123.0, luaeval('num'))
498528

499-
lua str, num = nil, nil
529+
lua str, num = nil
500530
call delete('Xlua_file')
501531
endfunc
502532

@@ -512,7 +542,19 @@ func Test_luafile_percent()
512542
let msg = split(execute('message'), "\n")[-1]
513543
call assert_equal('str=foo, num=321', msg)
514544

515-
lua str, num = nil, nil
545+
lua str, num = nil
546+
call delete('Xlua_file')
547+
bwipe!
548+
endfunc
549+
550+
" Test :luafile with syntax error
551+
func Test_luafile_error()
552+
new Xlua_file
553+
call writefile(['nil = 0' ], 'Xlua_file')
554+
call setfperm('Xlua_file', 'r-xr-xr-x')
555+
556+
call assert_fails('luafile Xlua_file', "Xlua_file:1: unexpected symbol near 'nil'")
557+
516558
call delete('Xlua_file')
517559
bwipe!
518560
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,8 @@ static char *(features[]) =
789789

790790
static int included_patches[] =
791791
{ /* Add new patch number below this line */
792+
/**/
793+
136,
792794
/**/
793795
135,
794796
/**/

0 commit comments

Comments
 (0)