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.
This commit is contained in:
Justin M. Keyes
2019-09-03 22:51:45 +02:00
parent 638f2b6dee
commit af946046b9
13 changed files with 124 additions and 112 deletions

View File

@@ -178,9 +178,20 @@ local function trim(s)
return s:match('^%s*(.*%S)') or '' return s:match('^%s*(.*%S)') or ''
end end
--- Escapes magic chars in a Lua pattern string.
---
--@see https://github.com/rxi/lume
--@param s String to escape
--@returns %-escaped pattern string
local function pesc(s)
assert(type(s) == 'string')
return s:gsub('[%(%)%.%%%+%-%*%?%[%]%^%$]', '%%%1')
end
local module = { local module = {
deepcopy = deepcopy, deepcopy = deepcopy,
gsplit = gsplit, gsplit = gsplit,
pesc = pesc,
split = split, split = split,
tbl_contains = tbl_contains, tbl_contains = tbl_contains,
tbl_extend = tbl_extend, tbl_extend = tbl_extend,

View File

@@ -10,11 +10,11 @@ local exc_exec = helpers.exc_exec
local feed_command = helpers.feed_command local feed_command = helpers.feed_command
local insert = helpers.insert local insert = helpers.insert
local NIL = helpers.NIL local NIL = helpers.NIL
local meth_pcall = helpers.meth_pcall
local command = helpers.command local command = helpers.command
local bufmeths = helpers.bufmeths local bufmeths = helpers.bufmeths
local feed = helpers.feed local feed = helpers.feed
local expect_err = helpers.expect_err local expect_err = helpers.expect_err
local pcall_err = helpers.pcall_err
describe('api/buf', function() describe('api/buf', function()
before_each(clear) before_each(clear)
@@ -191,11 +191,8 @@ describe('api/buf', function()
it('fails correctly when input is not valid', function() it('fails correctly when input is not valid', function()
eq(1, curbufmeths.get_number()) eq(1, curbufmeths.get_number())
local err, emsg = pcall(bufmeths.set_lines, 1, 1, 2, false, {'b\na'}) expect_err([[String cannot contain newlines]],
eq(false, err) bufmeths.set_lines, 1, 1, 2, false, {'b\na'})
local exp_emsg = 'String cannot contain newlines'
-- Expected {filename}:{lnum}: {exp_emsg}
eq(': ' .. exp_emsg, emsg:sub(-#exp_emsg - 2))
end) end)
it("fails if 'nomodifiable'", function() it("fails if 'nomodifiable'", function()
@@ -407,8 +404,8 @@ describe('api/buf', function()
eq(16, get_offset(3)) eq(16, get_offset(3))
eq(24, get_offset(4)) eq(24, get_offset(4))
eq(29, get_offset(5)) eq(29, get_offset(5))
eq({false,'Index out of bounds'}, meth_pcall(get_offset, 6)) eq('Index out of bounds', pcall_err(get_offset, 6))
eq({false,'Index out of bounds'}, meth_pcall(get_offset, -1)) eq('Index out of bounds', pcall_err(get_offset, -1))
curbufmeths.set_option('eol', false) curbufmeths.set_option('eol', false)
curbufmeths.set_option('fixeol', false) curbufmeths.set_option('fixeol', false)
@@ -441,15 +438,15 @@ describe('api/buf', function()
eq(1, funcs.exists('b:lua')) eq(1, funcs.exists('b:lua'))
curbufmeths.del_var('lua') curbufmeths.del_var('lua')
eq(0, funcs.exists('b: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) curbufmeths.set_var('lua', 1)
command('lockvar b:lua') command('lockvar b:lua')
eq({false, 'Key is locked: lua'}, meth_pcall(curbufmeths.del_var, 'lua')) eq('Key is locked: lua', pcall_err(curbufmeths.del_var, 'lua'))
eq({false, 'Key is locked: lua'}, meth_pcall(curbufmeths.set_var, 'lua', 1)) eq('Key is locked: lua', pcall_err(curbufmeths.set_var, 'lua', 1))
eq({false, 'Key is read-only: changedtick'}, eq('Key is read-only: changedtick',
meth_pcall(curbufmeths.del_var, 'changedtick')) pcall_err(curbufmeths.del_var, 'changedtick'))
eq({false, 'Key is read-only: changedtick'}, eq('Key is read-only: changedtick',
meth_pcall(curbufmeths.set_var, 'changedtick', 1)) pcall_err(curbufmeths.set_var, 'changedtick', 1))
end) end)
end) end)

View File

@@ -10,7 +10,7 @@ local ok = helpers.ok
local meths = helpers.meths local meths = helpers.meths
local spawn, merge_args = helpers.spawn, helpers.merge_args local spawn, merge_args = helpers.spawn, helpers.merge_args
local set_session = helpers.set_session local set_session = helpers.set_session
local meth_pcall = helpers.meth_pcall local pcall_err = helpers.pcall_err
describe('server -> client', function() describe('server -> client', function()
local cid local cid
@@ -220,8 +220,8 @@ describe('server -> client', function()
end) end)
it('returns an error if the request failed', function() 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" }, eq("Vim:Error invoking 'does-not-exist' on channel 3:\nInvalid method: does-not-exist",
meth_pcall(eval, "rpcrequest(vim, 'does-not-exist')")) pcall_err(eval, "rpcrequest(vim, 'does-not-exist')"))
end) end)
end) end)

View File

@@ -6,7 +6,7 @@ local curtabmeths = helpers.curtabmeths
local funcs = helpers.funcs local funcs = helpers.funcs
local request = helpers.request local request = helpers.request
local NIL = helpers.NIL local NIL = helpers.NIL
local meth_pcall = helpers.meth_pcall local pcall_err = helpers.pcall_err
local command = helpers.command local command = helpers.command
describe('api/tabpage', function() describe('api/tabpage', function()
@@ -34,11 +34,11 @@ describe('api/tabpage', function()
eq(1, funcs.exists('t:lua')) eq(1, funcs.exists('t:lua'))
curtabmeths.del_var('lua') curtabmeths.del_var('lua')
eq(0, funcs.exists('t: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) curtabmeths.set_var('lua', 1)
command('lockvar t:lua') command('lockvar t:lua')
eq({false, 'Key is locked: lua'}, meth_pcall(curtabmeths.del_var, 'lua')) eq('Key is locked: lua', pcall_err(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.set_var, 'lua', 1))
end) end)
it('tabpage_set_var returns the old value', function() it('tabpage_set_var returns the old value', function()

View File

@@ -8,7 +8,6 @@ local eval = helpers.eval
local expect = helpers.expect local expect = helpers.expect
local funcs = helpers.funcs local funcs = helpers.funcs
local iswin = helpers.iswin local iswin = helpers.iswin
local meth_pcall = helpers.meth_pcall
local meths = helpers.meths local meths = helpers.meths
local ok, nvim_async, feed = helpers.ok, helpers.nvim_async, helpers.feed local ok, nvim_async, feed = helpers.ok, helpers.nvim_async, helpers.feed
local is_os = helpers.is_os local is_os = helpers.is_os
@@ -17,6 +16,7 @@ local request = helpers.request
local source = helpers.source local source = helpers.source
local next_msg = helpers.next_msg local next_msg = helpers.next_msg
local pcall_err = helpers.pcall_err
local expect_err = helpers.expect_err local expect_err = helpers.expect_err
local format_string = helpers.format_string local format_string = helpers.format_string
local intchar2lua = helpers.intchar2lua local intchar2lua = helpers.intchar2lua
@@ -326,25 +326,20 @@ describe('API', function()
end) end)
it('reports errors', function() it('reports errors', function()
eq({false, 'Error loading lua: [string "<nvim>"]:1: '.. eq([[Error loading lua: [string "<nvim>"]:1: '=' expected near '+']],
"'=' expected near '+'"}, pcall_err(meths.execute_lua, 'a+*b', {}))
meth_pcall(meths.execute_lua, 'a+*b', {}))
eq({false, 'Error loading lua: [string "<nvim>"]:1: '.. eq([[Error loading lua: [string "<nvim>"]:1: unexpected symbol near '1']],
"unexpected symbol near '1'"}, pcall_err(meths.execute_lua, '1+2', {}))
meth_pcall(meths.execute_lua, '1+2', {}))
eq({false, 'Error loading lua: [string "<nvim>"]:1: '.. eq([[Error loading lua: [string "<nvim>"]:1: unexpected symbol]],
"unexpected symbol"}, pcall_err(meths.execute_lua, 'aa=bb\0', {}))
meth_pcall(meths.execute_lua, 'aa=bb\0', {}))
eq({false, 'Error executing lua: [string "<nvim>"]:1: '.. eq([[Error executing lua: [string "<nvim>"]:1: attempt to call global 'bork' (a nil value)]],
"attempt to call global 'bork' (a nil value)"}, pcall_err(meths.execute_lua, 'bork()', {}))
meth_pcall(meths.execute_lua, 'bork()', {}))
eq({false, 'Error executing lua: [string "<nvim>"]:1: '.. eq('Error executing lua: [string "<nvim>"]:1: did\nthe\nfail',
"did\nthe\nfail"}, pcall_err(meths.execute_lua, 'error("did\\nthe\\nfail")', {}))
meth_pcall(meths.execute_lua, 'error("did\\nthe\\nfail")', {}))
end) end)
it('uses native float values', function() it('uses native float values', function()
@@ -571,10 +566,10 @@ describe('API', function()
yyybc line 2 yyybc line 2
line 3 line 3
]]) ]])
eq({false, "Invalid type: 'bx'"}, eq("Invalid type: 'bx'",
meth_pcall(meths.put, {'xxx', 'yyy'}, 'bx', false, true)) pcall_err(meths.put, {'xxx', 'yyy'}, 'bx', false, true))
eq({false, "Invalid type: 'b3x'"}, eq("Invalid type: 'b3x'",
meth_pcall(meths.put, {'xxx', 'yyy'}, 'b3x', false, true)) pcall_err(meths.put, {'xxx', 'yyy'}, 'b3x', false, true))
end) end)
end) end)
@@ -607,13 +602,13 @@ describe('API', function()
eq(1, funcs.exists('g:lua')) eq(1, funcs.exists('g:lua'))
meths.del_var('lua') meths.del_var('lua')
eq(0, funcs.exists('g: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) meths.set_var('lua', 1)
-- Set locked g: var. -- Set locked g: var.
command('lockvar lua') command('lockvar lua')
eq({false, 'Key is locked: lua'}, meth_pcall(meths.del_var, 'lua')) eq('Key is locked: lua', pcall_err(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.set_var, 'lua', 1))
end) end)
it('nvim_get_vvar, nvim_set_vvar', function() it('nvim_get_vvar, nvim_set_vvar', function()
@@ -1195,8 +1190,8 @@ describe('API', function()
eq({info=info}, meths.get_var("info_event")) eq({info=info}, meths.get_var("info_event"))
eq({[1]=testinfo,[2]=stderr,[3]=info}, meths.list_chans()) 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"}, eq("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)')) pcall_err(eval, 'rpcrequest(3, "nvim_set_current_buf", -1)'))
end) end)
it('works for :terminal channel', function() it('works for :terminal channel', function()

View File

@@ -8,10 +8,10 @@ local curwinmeths = helpers.curwinmeths
local funcs = helpers.funcs local funcs = helpers.funcs
local request = helpers.request local request = helpers.request
local NIL = helpers.NIL local NIL = helpers.NIL
local meth_pcall = helpers.meth_pcall
local meths = helpers.meths local meths = helpers.meths
local command = helpers.command local command = helpers.command
local expect_err = helpers.expect_err local expect_err = helpers.expect_err
local pcall_err = helpers.pcall_err
-- check if str is visible at the beginning of some line -- check if str is visible at the beginning of some line
local function is_visible(str) 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', it('does not leak memory when using invalid window ID with invalid pos',
function() function()
eq({false, 'Invalid window id'}, eq('Invalid window id', pcall_err(meths.win_set_cursor, 1, {"b\na"}))
meth_pcall(meths.win_set_cursor, 1, {"b\na"}))
end) end)
it('updates the screen, and also when the window is unfocused', function() 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')) eq(1, funcs.exists('w:lua'))
curwinmeths.del_var('lua') curwinmeths.del_var('lua')
eq(0, funcs.exists('w: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) curwinmeths.set_var('lua', 1)
command('lockvar w:lua') command('lockvar w:lua')
eq({false, 'Key is locked: lua'}, meth_pcall(curwinmeths.del_var, 'lua')) eq('Key is locked: lua', pcall_err(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.set_var, 'lua', 1))
end) end)
it('window_set_var returns the old value', function() it('window_set_var returns the old value', function()
@@ -222,7 +221,8 @@ describe('API/win', function()
eq('', nvim('get_option', 'statusline')) eq('', nvim('get_option', 'statusline'))
command("set modified") command("set modified")
command("enew") -- global-local: not preserved in new buffer 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 eq('', eval('&l:statusline')) -- confirm local value was not copied
end) end)
end) end)
@@ -317,8 +317,8 @@ describe('API/win', function()
insert('text') insert('text')
command('new') command('new')
local newwin = meths.get_current_win() local newwin = meths.get_current_win()
eq({false,"Vim:E37: No write since last change (add ! to override)"}, eq("Vim:E37: No write since last change (add ! to override)",
meth_pcall(meths.win_close, oldwin,false)) pcall_err(meths.win_close, oldwin,false))
eq({newwin,oldwin}, meths.list_wins()) eq({newwin,oldwin}, meths.list_wins())
end) end)

View File

@@ -7,7 +7,7 @@ local eval = helpers.eval
local feed = helpers.feed local feed = helpers.feed
local clear = helpers.clear local clear = helpers.clear
local meths = helpers.meths local meths = helpers.meths
local meth_pcall = helpers.meth_pcall local pcall_err = helpers.pcall_err
local funcs = helpers.funcs local funcs = helpers.funcs
local expect = helpers.expect local expect = helpers.expect
local command = helpers.command local command = helpers.command
@@ -219,8 +219,8 @@ describe('autocmd', function()
eq(7, eval('g:test')) eq(7, eval('g:test'))
-- API calls are blocked when aucmd_win is not in scope -- API calls are blocked when aucmd_win is not in scope
eq({false, 'Vim(call):E5555: API call: Invalid window id'}, eq('Vim(call):E5555: API call: Invalid window id',
meth_pcall(command, "call nvim_set_current_win(g:winid)")) pcall_err(command, "call nvim_set_current_win(g:winid)"))
-- second time aucmd_win is needed, a different code path is invoked -- second time aucmd_win is needed, a different code path is invoked
-- to reuse the same window, so check again -- to reuse the same window, so check again
@@ -257,8 +257,8 @@ describe('autocmd', function()
eq(0, eval('g:had_value')) eq(0, eval('g:had_value'))
eq(7, eval('g:test')) eq(7, eval('g:test'))
eq({false, 'Vim(call):E5555: API call: Invalid window id'}, eq('Vim(call):E5555: API call: Invalid window id',
meth_pcall(command, "call nvim_set_current_win(g:winid)")) pcall_err(command, "call nvim_set_current_win(g:winid)"))
end) end)
it(':doautocmd does not warn "No matching autocommands" #10689', function() it(':doautocmd does not warn "No matching autocommands" #10689', function()

View File

@@ -4,7 +4,7 @@ local lfs = require('lfs')
local neq, eq, command = helpers.neq, helpers.eq, helpers.command local neq, eq, command = helpers.neq, helpers.eq, helpers.command
local clear, curbufmeths = helpers.clear, helpers.curbufmeths local clear, curbufmeths = helpers.clear, helpers.curbufmeths
local exc_exec, expect, eval = helpers.exc_exec, helpers.expect, helpers.eval local exc_exec, expect, eval = helpers.exc_exec, helpers.expect, helpers.eval
local insert, meth_pcall = helpers.insert, helpers.meth_pcall local insert, pcall_err = helpers.insert, helpers.pcall_err
local meths = helpers.meths local meths = helpers.meths
describe('eval-API', function() describe('eval-API', function()
@@ -148,8 +148,8 @@ describe('eval-API', function()
end) end)
it('cannot be called from sandbox', function() it('cannot be called from sandbox', function()
eq({false, 'Vim(call):E48: Not allowed in sandbox'}, eq('Vim(call):E48: Not allowed in sandbox',
meth_pcall(command, "sandbox call nvim_input('ievil')")) pcall_err(command, "sandbox call nvim_input('ievil')"))
eq({''}, meths.buf_get_lines(0, 0, -1, true)) eq({''}, meths.buf_get_lines(0, 0, -1, true))
end) end)
end) end)

View File

@@ -9,7 +9,7 @@ local meths = helpers.meths
local command = helpers.command local command = helpers.command
local exc_exec = helpers.exc_exec local exc_exec = helpers.exc_exec
local redir_exec = helpers.redir_exec local redir_exec = helpers.redir_exec
local meth_pcall = helpers.meth_pcall local pcall_err = helpers.pcall_err
local curbufmeths = helpers.curbufmeths local curbufmeths = helpers.curbufmeths
before_each(clear) before_each(clear)
@@ -64,8 +64,8 @@ describe('b:changedtick', function()
redir_exec('let b:.changedtick = ' .. ctn)) redir_exec('let b:.changedtick = ' .. ctn))
eq('\nE46: Cannot change read-only variable "d.changedtick"', eq('\nE46: Cannot change read-only variable "d.changedtick"',
redir_exec('let d.changedtick = ' .. ctn)) redir_exec('let d.changedtick = ' .. ctn))
eq({false, 'Key is read-only: changedtick'}, eq('Key is read-only: changedtick',
meth_pcall(curbufmeths.set_var, 'changedtick', ctn)) pcall_err(curbufmeths.set_var, 'changedtick', ctn))
eq('\nE795: Cannot delete variable b:changedtick', eq('\nE795: Cannot delete variable b:changedtick',
redir_exec('unlet b:changedtick')) redir_exec('unlet b:changedtick'))
@@ -75,8 +75,8 @@ describe('b:changedtick', function()
redir_exec('unlet b:["changedtick"]')) redir_exec('unlet b:["changedtick"]'))
eq('\nE46: Cannot change read-only variable "d.changedtick"', eq('\nE46: Cannot change read-only variable "d.changedtick"',
redir_exec('unlet d.changedtick')) redir_exec('unlet d.changedtick'))
eq({false, 'Key is read-only: changedtick'}, eq('Key is read-only: changedtick',
meth_pcall(curbufmeths.del_var, 'changedtick')) pcall_err(curbufmeths.del_var, 'changedtick'))
eq(ct, changedtick()) eq(ct, changedtick())
eq('\nE46: Cannot change read-only variable "b:["changedtick"]"', eq('\nE46: Cannot change read-only variable "b:["changedtick"]"',

View File

@@ -687,14 +687,6 @@ function module.skip_fragile(pending_fn, cond)
return false return false
end end
function module.meth_pcall(...)
local ret = {pcall(...)}
if type(ret[2]) == 'string' then
ret[2] = ret[2]:gsub('^[^:]+:%d+: ', '')
end
return ret
end
module.funcs = module.create_callindex(module.call) module.funcs = module.create_callindex(module.call)
module.meths = module.create_callindex(module.nvim) module.meths = module.create_callindex(module.nvim)
module.uimeths = module.create_callindex(ui) module.uimeths = module.create_callindex(ui)

View File

@@ -7,12 +7,12 @@ local clear = helpers.clear
local eq = helpers.eq local eq = helpers.eq
local eval = helpers.eval local eval = helpers.eval
local feed = helpers.feed local feed = helpers.feed
local meth_pcall = helpers.meth_pcall local pcall_err = helpers.pcall_err
local exec_lua = helpers.exec_lua local exec_lua = helpers.exec_lua
before_each(clear) before_each(clear)
describe('lua function', function() describe('lua stdlib', function()
-- İ: `tolower("İ")` is `i` which has length 1 while `İ` itself has -- İ: `tolower("İ")` is `i` which has length 1 while `İ` itself has
-- length 2 (in bytes). -- length 2 (in bytes).
-- Ⱥ: `tolower("Ⱥ")` is `ⱥ` which has length 2 while `Ⱥ` itself has -- Ⱥ: `tolower("Ⱥ")` is `ⱥ` which has length 2 while `Ⱥ` itself has
@@ -147,11 +147,11 @@ describe('lua function', function()
eq({"yy","xx"}, exec_lua("return test_table")) eq({"yy","xx"}, exec_lua("return test_table"))
-- type checked args -- type checked args
eq({false, 'Error executing lua: vim.schedule: expected function'}, eq('Error executing lua: vim.schedule: expected function',
meth_pcall(exec_lua, "vim.schedule('stringly')")) pcall_err(exec_lua, "vim.schedule('stringly')"))
eq({false, 'Error executing lua: vim.schedule: expected function'}, eq('Error executing lua: vim.schedule: expected function',
meth_pcall(exec_lua, "vim.schedule()")) pcall_err(exec_lua, "vim.schedule()"))
exec_lua([[ exec_lua([[
vim.schedule(function() vim.schedule(function()
@@ -283,4 +283,9 @@ describe('lua function', function()
assert(is_dc) assert(is_dc)
end) end)
it('vim.pesc', function()
eq('foo%-bar', exec_lua([[return vim.pesc('foo-bar')]]))
eq('foo%%%-bar', exec_lua([[return vim.pesc(vim.pesc('foo-bar'))]]))
end)
end) end)

View File

@@ -10,7 +10,7 @@ local meths = helpers.meths
local curbufmeths = helpers.curbufmeths local curbufmeths = helpers.curbufmeths
local funcs = helpers.funcs local funcs = helpers.funcs
local run = helpers.run local run = helpers.run
local meth_pcall = helpers.meth_pcall local pcall_err = helpers.pcall_err
describe('floating windows', function() describe('floating windows', function()
before_each(function() before_each(function()
@@ -636,26 +636,26 @@ describe('floating windows', function()
it('API has proper error messages', function() it('API has proper error messages', function()
local buf = meths.create_buf(false,false) local buf = meths.create_buf(false,false)
eq({false, "Invalid key 'bork'"}, eq("Invalid key 'bork'",
meth_pcall(meths.open_win,buf, false, {width=20,height=2,bork=true})) pcall_err(meths.open_win,buf, false, {width=20,height=2,bork=true}))
eq({false, "'win' key is only valid with relative='win'"}, eq("'win' key is only valid with relative='win'",
meth_pcall(meths.open_win,buf, false, {width=20,height=2,relative='editor',row=0,col=0,win=0})) pcall_err(meths.open_win,buf, false, {width=20,height=2,relative='editor',row=0,col=0,win=0}))
eq({false, "Only one of 'relative' and 'external' must be used"}, eq("Only one of 'relative' and 'external' must be used",
meth_pcall(meths.open_win,buf, false, {width=20,height=2,relative='editor',row=0,col=0,external=true})) pcall_err(meths.open_win,buf, false, {width=20,height=2,relative='editor',row=0,col=0,external=true}))
eq({false, "Invalid value of 'relative' key"}, eq("Invalid value of 'relative' key",
meth_pcall(meths.open_win,buf, false, {width=20,height=2,relative='shell',row=0,col=0})) pcall_err(meths.open_win,buf, false, {width=20,height=2,relative='shell',row=0,col=0}))
eq({false, "Invalid value of 'anchor' key"}, eq("Invalid value of 'anchor' key",
meth_pcall(meths.open_win,buf, false, {width=20,height=2,relative='editor',row=0,col=0,anchor='bottom'})) pcall_err(meths.open_win,buf, false, {width=20,height=2,relative='editor',row=0,col=0,anchor='bottom'}))
eq({false, "All of 'relative', 'row', and 'col' has to be specified at once"}, eq("All of 'relative', 'row', and 'col' has to be specified at once",
meth_pcall(meths.open_win,buf, false, {width=20,height=2,relative='editor'})) pcall_err(meths.open_win,buf, false, {width=20,height=2,relative='editor'}))
eq({false, "'width' key must be a positive Integer"}, eq("'width' key must be a positive Integer",
meth_pcall(meths.open_win,buf, false, {width=-1,height=2,relative='editor'})) pcall_err(meths.open_win,buf, false, {width=-1,height=2,relative='editor'}))
eq({false, "'height' key must be a positive Integer"}, eq("'height' key must be a positive Integer",
meth_pcall(meths.open_win,buf, false, {width=20,height=-1,relative='editor'})) pcall_err(meths.open_win,buf, false, {width=20,height=-1,relative='editor'}))
eq({false, "'height' key must be a positive Integer"}, eq("'height' key must be a positive Integer",
meth_pcall(meths.open_win,buf, false, {width=20,height=0,relative='editor'})) pcall_err(meths.open_win,buf, false, {width=20,height=0,relative='editor'}))
eq({false, "Must specify 'width' and 'height'"}, eq("Must specify 'width' and 'height'",
meth_pcall(meths.open_win,buf, false, {relative='editor'})) pcall_err(meths.open_win,buf, false, {relative='editor'}))
end) end)
it('can be placed relative window or cursor', function() it('can be placed relative window or cursor', function()
@@ -4528,8 +4528,8 @@ describe('floating windows', function()
{0:~ }| {0:~ }|
]], float_pos=expected_pos} ]], float_pos=expected_pos}
else else
eq({false, "UI doesn't support external windows"}, eq("UI doesn't support external windows",
meth_pcall(meths.win_set_config, 0, {external=true, width=30, height=2})) pcall_err(meths.win_set_config, 0, {external=true, width=30, height=2}))
return return
end end
@@ -4844,8 +4844,8 @@ describe('floating windows', function()
{0:~ }| {0:~ }|
]], float_pos=expected_pos} ]], float_pos=expected_pos}
else else
eq({false, "UI doesn't support external windows"}, eq("UI doesn't support external windows",
meth_pcall(meths.win_set_config, 0, {external=true, width=65, height=4})) pcall_err(meths.win_set_config, 0, {external=true, width=65, height=4}))
end end
feed(":tabnext<cr>") feed(":tabnext<cr>")

View File

@@ -73,14 +73,26 @@ function module.matches(pat, actual)
end end
error(string.format('Pattern does not match.\nPattern:\n%s\nActual:\n%s', pat, actual)) error(string.format('Pattern does not match.\nPattern:\n%s\nActual:\n%s', pat, actual))
end end
-- Expect an error matching pattern `pat`.
function module.expect_err(pat, ...) -- Expects an error matching Lua pattern `pat`.
local fn = select(1, ...) --
function module.expect_err(pat, fn, ...)
assert(type(fn) == 'function')
local fn_args = {...} local fn_args = {...}
table.remove(fn_args, 1)
assert.error_matches(function() return fn(unpack(fn_args)) end, pat) assert.error_matches(function() return fn(unpack(fn_args)) end, pat)
end end
-- Invokes `fn` and returns the error string, or raises an error if `fn` succeeds.
function module.pcall_err(fn, ...)
assert(type(fn) == 'function')
local status, rv = pcall(fn, ...)
if status == true then
error('expected failure, but got success')
end
local errmsg = tostring(rv):gsub('^[^:]+:%d+: ', '')
return errmsg
end
-- initial_path: directory to recurse into -- initial_path: directory to recurse into
-- re: include pattern (string) -- re: include pattern (string)
-- exc_re: exclude pattern(s) (string or table) -- exc_re: exclude pattern(s) (string or table)