diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2019-09-03 22:51:45 +0200 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2019-09-06 17:19:07 -0700 |
commit | af946046b922dc5d5285a70a23d11916d8389a5d (patch) | |
tree | 090d68a1dd675b5ad6e99abc829be0daafe2a7d9 /test/functional/api | |
parent | 638f2b6dee7439de303bea12dec80240617e8034 (diff) | |
download | rneovim-af946046b922dc5d5285a70a23d11916d8389a5d.tar.gz rneovim-af946046b922dc5d5285a70a23d11916d8389a5d.tar.bz2 rneovim-af946046b922dc5d5285a70a23d11916d8389a5d.zip |
test: Rename meth_pcall to pcall_err
- Rename `meth_pcall`.
- Make `pcall_err` raise an error if the function does not fail.
- Add `vim.pesc()` to treat a string as literal where a Lua pattern is
expected.
Diffstat (limited to 'test/functional/api')
-rw-r--r-- | test/functional/api/buffer_spec.lua | 27 | ||||
-rw-r--r-- | test/functional/api/server_requests_spec.lua | 6 | ||||
-rw-r--r-- | test/functional/api/tabpage_spec.lua | 8 | ||||
-rw-r--r-- | test/functional/api/vim_spec.lua | 45 | ||||
-rw-r--r-- | test/functional/api/window_spec.lua | 18 |
5 files changed, 48 insertions, 56 deletions
diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua index 849fbedd01..1322daa64d 100644 --- a/test/functional/api/buffer_spec.lua +++ b/test/functional/api/buffer_spec.lua @@ -10,11 +10,11 @@ local exc_exec = helpers.exc_exec local feed_command = helpers.feed_command local insert = helpers.insert local NIL = helpers.NIL -local meth_pcall = helpers.meth_pcall local command = helpers.command local bufmeths = helpers.bufmeths local feed = helpers.feed local expect_err = helpers.expect_err +local pcall_err = helpers.pcall_err describe('api/buf', function() before_each(clear) @@ -191,11 +191,8 @@ describe('api/buf', function() it('fails correctly when input is not valid', function() eq(1, curbufmeths.get_number()) - local err, emsg = pcall(bufmeths.set_lines, 1, 1, 2, false, {'b\na'}) - eq(false, err) - local exp_emsg = 'String cannot contain newlines' - -- Expected {filename}:{lnum}: {exp_emsg} - eq(': ' .. exp_emsg, emsg:sub(-#exp_emsg - 2)) + expect_err([[String cannot contain newlines]], + bufmeths.set_lines, 1, 1, 2, false, {'b\na'}) end) it("fails if 'nomodifiable'", function() @@ -407,8 +404,8 @@ describe('api/buf', function() eq(16, get_offset(3)) eq(24, get_offset(4)) eq(29, get_offset(5)) - eq({false,'Index out of bounds'}, meth_pcall(get_offset, 6)) - eq({false,'Index out of bounds'}, meth_pcall(get_offset, -1)) + eq('Index out of bounds', pcall_err(get_offset, 6)) + eq('Index out of bounds', pcall_err(get_offset, -1)) curbufmeths.set_option('eol', false) curbufmeths.set_option('fixeol', false) @@ -441,15 +438,15 @@ describe('api/buf', function() eq(1, funcs.exists('b:lua')) curbufmeths.del_var('lua') eq(0, funcs.exists('b:lua')) - eq({false, 'Key not found: lua'}, meth_pcall(curbufmeths.del_var, 'lua')) + eq( 'Key not found: lua', pcall_err(curbufmeths.del_var, 'lua')) curbufmeths.set_var('lua', 1) command('lockvar b:lua') - eq({false, 'Key is locked: lua'}, meth_pcall(curbufmeths.del_var, 'lua')) - eq({false, 'Key is locked: lua'}, meth_pcall(curbufmeths.set_var, 'lua', 1)) - eq({false, 'Key is read-only: changedtick'}, - meth_pcall(curbufmeths.del_var, 'changedtick')) - eq({false, 'Key is read-only: changedtick'}, - meth_pcall(curbufmeths.set_var, 'changedtick', 1)) + eq('Key is locked: lua', pcall_err(curbufmeths.del_var, 'lua')) + eq('Key is locked: lua', pcall_err(curbufmeths.set_var, 'lua', 1)) + eq('Key is read-only: changedtick', + pcall_err(curbufmeths.del_var, 'changedtick')) + eq('Key is read-only: changedtick', + pcall_err(curbufmeths.set_var, 'changedtick', 1)) end) end) diff --git a/test/functional/api/server_requests_spec.lua b/test/functional/api/server_requests_spec.lua index a20667de40..e275d8cd35 100644 --- a/test/functional/api/server_requests_spec.lua +++ b/test/functional/api/server_requests_spec.lua @@ -10,7 +10,7 @@ local ok = helpers.ok local meths = helpers.meths local spawn, merge_args = helpers.spawn, helpers.merge_args local set_session = helpers.set_session -local meth_pcall = helpers.meth_pcall +local pcall_err = helpers.pcall_err describe('server -> client', function() local cid @@ -220,8 +220,8 @@ describe('server -> client', function() end) it('returns an error if the request failed', function() - eq({false, "Vim:Error invoking 'does-not-exist' on channel 3:\nInvalid method: does-not-exist" }, - meth_pcall(eval, "rpcrequest(vim, 'does-not-exist')")) + eq("Vim:Error invoking 'does-not-exist' on channel 3:\nInvalid method: does-not-exist", + pcall_err(eval, "rpcrequest(vim, 'does-not-exist')")) end) end) diff --git a/test/functional/api/tabpage_spec.lua b/test/functional/api/tabpage_spec.lua index c49091db02..ed7ce72597 100644 --- a/test/functional/api/tabpage_spec.lua +++ b/test/functional/api/tabpage_spec.lua @@ -6,7 +6,7 @@ local curtabmeths = helpers.curtabmeths local funcs = helpers.funcs local request = helpers.request local NIL = helpers.NIL -local meth_pcall = helpers.meth_pcall +local pcall_err = helpers.pcall_err local command = helpers.command describe('api/tabpage', function() @@ -34,11 +34,11 @@ describe('api/tabpage', function() eq(1, funcs.exists('t:lua')) curtabmeths.del_var('lua') eq(0, funcs.exists('t:lua')) - eq({false, 'Key not found: lua'}, meth_pcall(curtabmeths.del_var, 'lua')) + eq('Key not found: lua', pcall_err(curtabmeths.del_var, 'lua')) curtabmeths.set_var('lua', 1) command('lockvar t:lua') - eq({false, 'Key is locked: lua'}, meth_pcall(curtabmeths.del_var, 'lua')) - eq({false, 'Key is locked: lua'}, meth_pcall(curtabmeths.set_var, 'lua', 1)) + eq('Key is locked: lua', pcall_err(curtabmeths.del_var, 'lua')) + eq('Key is locked: lua', pcall_err(curtabmeths.set_var, 'lua', 1)) end) it('tabpage_set_var returns the old value', function() diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index b80b1c87af..bfaa984042 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -8,7 +8,6 @@ local eval = helpers.eval local expect = helpers.expect local funcs = helpers.funcs local iswin = helpers.iswin -local meth_pcall = helpers.meth_pcall local meths = helpers.meths local ok, nvim_async, feed = helpers.ok, helpers.nvim_async, helpers.feed local is_os = helpers.is_os @@ -17,6 +16,7 @@ local request = helpers.request local source = helpers.source local next_msg = helpers.next_msg +local pcall_err = helpers.pcall_err local expect_err = helpers.expect_err local format_string = helpers.format_string local intchar2lua = helpers.intchar2lua @@ -326,25 +326,20 @@ describe('API', function() end) it('reports errors', function() - eq({false, 'Error loading lua: [string "<nvim>"]:1: '.. - "'=' expected near '+'"}, - meth_pcall(meths.execute_lua, 'a+*b', {})) + eq([[Error loading lua: [string "<nvim>"]:1: '=' expected near '+']], + pcall_err(meths.execute_lua, 'a+*b', {})) - eq({false, 'Error loading lua: [string "<nvim>"]:1: '.. - "unexpected symbol near '1'"}, - meth_pcall(meths.execute_lua, '1+2', {})) + eq([[Error loading lua: [string "<nvim>"]:1: unexpected symbol near '1']], + pcall_err(meths.execute_lua, '1+2', {})) - eq({false, 'Error loading lua: [string "<nvim>"]:1: '.. - "unexpected symbol"}, - meth_pcall(meths.execute_lua, 'aa=bb\0', {})) + eq([[Error loading lua: [string "<nvim>"]:1: unexpected symbol]], + pcall_err(meths.execute_lua, 'aa=bb\0', {})) - eq({false, 'Error executing lua: [string "<nvim>"]:1: '.. - "attempt to call global 'bork' (a nil value)"}, - meth_pcall(meths.execute_lua, 'bork()', {})) + eq([[Error executing lua: [string "<nvim>"]:1: attempt to call global 'bork' (a nil value)]], + pcall_err(meths.execute_lua, 'bork()', {})) - eq({false, 'Error executing lua: [string "<nvim>"]:1: '.. - "did\nthe\nfail"}, - meth_pcall(meths.execute_lua, 'error("did\\nthe\\nfail")', {})) + eq('Error executing lua: [string "<nvim>"]:1: did\nthe\nfail', + pcall_err(meths.execute_lua, 'error("did\\nthe\\nfail")', {})) end) it('uses native float values', function() @@ -571,10 +566,10 @@ describe('API', function() yyybc line 2 line 3 ]]) - eq({false, "Invalid type: 'bx'"}, - meth_pcall(meths.put, {'xxx', 'yyy'}, 'bx', false, true)) - eq({false, "Invalid type: 'b3x'"}, - meth_pcall(meths.put, {'xxx', 'yyy'}, 'b3x', false, true)) + eq("Invalid type: 'bx'", + pcall_err(meths.put, {'xxx', 'yyy'}, 'bx', false, true)) + eq("Invalid type: 'b3x'", + pcall_err(meths.put, {'xxx', 'yyy'}, 'b3x', false, true)) end) end) @@ -607,13 +602,13 @@ describe('API', function() eq(1, funcs.exists('g:lua')) meths.del_var('lua') eq(0, funcs.exists('g:lua')) - eq({false, "Key not found: lua"}, meth_pcall(meths.del_var, 'lua')) + eq("Key not found: lua", pcall_err(meths.del_var, 'lua')) meths.set_var('lua', 1) -- Set locked g: var. command('lockvar lua') - eq({false, 'Key is locked: lua'}, meth_pcall(meths.del_var, 'lua')) - eq({false, 'Key is locked: lua'}, meth_pcall(meths.set_var, 'lua', 1)) + eq('Key is locked: lua', pcall_err(meths.del_var, 'lua')) + eq('Key is locked: lua', pcall_err(meths.set_var, 'lua', 1)) end) it('nvim_get_vvar, nvim_set_vvar', function() @@ -1195,8 +1190,8 @@ describe('API', function() eq({info=info}, meths.get_var("info_event")) eq({[1]=testinfo,[2]=stderr,[3]=info}, meths.list_chans()) - eq({false, "Vim:Error invoking 'nvim_set_current_buf' on channel 3 (amazing-cat):\nWrong type for argument 1, expecting Buffer"}, - meth_pcall(eval, 'rpcrequest(3, "nvim_set_current_buf", -1)')) + eq("Vim:Error invoking 'nvim_set_current_buf' on channel 3 (amazing-cat):\nWrong type for argument 1, expecting Buffer", + pcall_err(eval, 'rpcrequest(3, "nvim_set_current_buf", -1)')) end) it('works for :terminal channel', function() diff --git a/test/functional/api/window_spec.lua b/test/functional/api/window_spec.lua index 3bc53cfc83..3323d3866d 100644 --- a/test/functional/api/window_spec.lua +++ b/test/functional/api/window_spec.lua @@ -8,10 +8,10 @@ local curwinmeths = helpers.curwinmeths local funcs = helpers.funcs local request = helpers.request local NIL = helpers.NIL -local meth_pcall = helpers.meth_pcall local meths = helpers.meths local command = helpers.command local expect_err = helpers.expect_err +local pcall_err = helpers.pcall_err -- check if str is visible at the beginning of some line local function is_visible(str) @@ -74,8 +74,7 @@ describe('API/win', function() it('does not leak memory when using invalid window ID with invalid pos', function() - eq({false, 'Invalid window id'}, - meth_pcall(meths.win_set_cursor, 1, {"b\na"})) + eq('Invalid window id', pcall_err(meths.win_set_cursor, 1, {"b\na"})) end) it('updates the screen, and also when the window is unfocused', function() @@ -185,11 +184,11 @@ describe('API/win', function() eq(1, funcs.exists('w:lua')) curwinmeths.del_var('lua') eq(0, funcs.exists('w:lua')) - eq({false, 'Key not found: lua'}, meth_pcall(curwinmeths.del_var, 'lua')) + eq('Key not found: lua', pcall_err(curwinmeths.del_var, 'lua')) curwinmeths.set_var('lua', 1) command('lockvar w:lua') - eq({false, 'Key is locked: lua'}, meth_pcall(curwinmeths.del_var, 'lua')) - eq({false, 'Key is locked: lua'}, meth_pcall(curwinmeths.set_var, 'lua', 1)) + eq('Key is locked: lua', pcall_err(curwinmeths.del_var, 'lua')) + eq('Key is locked: lua', pcall_err(curwinmeths.set_var, 'lua', 1)) end) it('window_set_var returns the old value', function() @@ -222,7 +221,8 @@ describe('API/win', function() eq('', nvim('get_option', 'statusline')) command("set modified") command("enew") -- global-local: not preserved in new buffer - eq({false, "Failed to get value for option 'statusline'"}, meth_pcall(curwin, 'get_option', 'statusline')) + eq("Failed to get value for option 'statusline'", + pcall_err(curwin, 'get_option', 'statusline')) eq('', eval('&l:statusline')) -- confirm local value was not copied end) end) @@ -317,8 +317,8 @@ describe('API/win', function() insert('text') command('new') local newwin = meths.get_current_win() - eq({false,"Vim:E37: No write since last change (add ! to override)"}, - meth_pcall(meths.win_close, oldwin,false)) + eq("Vim:E37: No write since last change (add ! to override)", + pcall_err(meths.win_close, oldwin,false)) eq({newwin,oldwin}, meths.list_wins()) end) |