eval: Stop executing *eval() function at error

Fixes #4822
Closes #4823
This commit is contained in:
ZyX
2016-05-27 15:10:41 +03:00
parent 1d63672c77
commit 5758432be2
2 changed files with 60 additions and 23 deletions

View File

@@ -22018,6 +22018,7 @@ static void script_host_eval(char *name, typval_T *argvars, typval_T *rettv)
if (argvars[0].v_type != VAR_STRING) { if (argvars[0].v_type != VAR_STRING) {
EMSG(_(e_invarg)); EMSG(_(e_invarg));
return;
} }
list_T *args = list_alloc(); list_T *args = list_alloc();

View File

@@ -1,12 +1,22 @@
local helpers = require('test.functional.helpers') local helpers = require('test.functional.helpers')
local eval, command, feed = helpers.eval, helpers.command, helpers.feed
local eq, clear, insert = helpers.eq, helpers.clear, helpers.insert local eq = helpers.eq
local expect, write_file = helpers.expect, helpers.write_file local neq = helpers.neq
local feed = helpers.feed
local clear = helpers.clear
local funcs = helpers.funcs
local meths = helpers.meths
local insert = helpers.insert
local expect = helpers.expect
local command = helpers.command
local exc_exec = helpers.exc_exec
local write_file = helpers.write_file
local curbufmeths = helpers.curbufmeths
do do
clear() clear()
command('let [g:interp, g:errors] = provider#pythonx#Detect(2)') command('let [g:interp, g:errors] = provider#pythonx#Detect(2)')
local errors = eval('g:errors') local errors = meths.get_var('errors')
if errors ~= '' then if errors ~= '' then
pending( pending(
'Python 2 (or the Python 2 neovim module) is broken or missing:\n' .. errors, 'Python 2 (or the Python 2 neovim module) is broken or missing:\n' .. errors,
@@ -15,49 +25,58 @@ do
end end
end end
describe('python commands and functions', function() before_each(function()
before_each(function()
clear() clear()
command('python import vim') command('python import vim')
end) end)
it('feature test', function() describe('python feature test', function()
eq(1, eval('has("python")')) it('works', function()
eq(1, funcs.has('python'))
end) end)
end)
it('python_execute', function() describe(':python command', function()
it('works with a line', function()
command('python vim.vars["set_by_python"] = [100, 0]') command('python vim.vars["set_by_python"] = [100, 0]')
eq({100, 0}, eval('g:set_by_python')) eq({100, 0}, meths.get_var('set_by_python'))
end) end)
it('python_execute with nested commands', function() -- TODO(ZyX-I): works with << EOF
-- TODO(ZyX-I): works with execute 'python' line1."\n".line2."\n"…
it('supports nesting', function()
command([[python vim.command('python vim.command("python vim.command(\'let set_by_nested_python = 555\')")')]]) command([[python vim.command('python vim.command("python vim.command(\'let set_by_nested_python = 555\')")')]])
eq(555, eval('g:set_by_nested_python')) eq(555, meths.get_var('set_by_nested_python'))
end) end)
it('python_execute with range', function() it('supports range', function()
insert([[ insert([[
line1 line1
line2 line2
line3 line3
line4]]) line4]])
feed('ggjvj:python vim.vars["range"] = vim.current.range[:]<CR>') feed('ggjvj:python vim.vars["range"] = vim.current.range[:]<CR>')
eq({'line2', 'line3'}, eval('g:range')) eq({'line2', 'line3'}, meths.get_var('range'))
end) end)
end)
it('pyfile', function() describe(':pyfile command', function()
it('works', function()
local fname = 'pyfile.py' local fname = 'pyfile.py'
write_file(fname, 'vim.command("let set_by_pyfile = 123")') write_file(fname, 'vim.command("let set_by_pyfile = 123")')
command('pyfile pyfile.py') command('pyfile pyfile.py')
eq(123, eval('g:set_by_pyfile')) eq(123, meths.get_var('set_by_pyfile'))
os.remove(fname) os.remove(fname)
end) end)
end)
it('pydo', function() describe(':pydo command', function()
it('works', function()
-- :pydo 42 returns None for all lines, -- :pydo 42 returns None for all lines,
-- the buffer should not be changed -- the buffer should not be changed
command('normal :pydo 42') command('normal :pydo 42')
eq(0, eval('&mod')) eq(false, curbufmeths.get_option('modified'))
-- insert some text -- insert some text
insert('abc\ndef\nghi') insert('abc\ndef\nghi')
expect([[ expect([[
@@ -71,8 +90,25 @@ describe('python commands and functions', function()
2 2
ghi]]) ghi]])
end) end)
end)
it('pyeval', function() describe('pyeval()', function()
eq({1, 2, {['key'] = 'val'}}, eval([[pyeval('[1, 2, {"key": "val"}]')]])) it('works', function()
eq({1, 2, {['key'] = 'val'}}, funcs.pyeval('[1, 2, {"key": "val"}]'))
end)
it('errors out when given non-string', function()
eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(10)'))
eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(v:_null_dict)'))
eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(v:_null_list)'))
eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(0.0)'))
eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(function("tr"))'))
eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(v:true)'))
eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(v:false)'))
eq('Vim(call):E474: Invalid argument', exc_exec('call pyeval(v:null)'))
end)
it('accepts NULL string', function()
neq(0, exc_exec('call pyeval($XXX_NONEXISTENT_VAR_XXX)'))
end) end)
end) end)