aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/vim_spec.lua
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2024-10-18 11:33:12 +0100
committerLewis Russell <me@lewisr.dev>2024-10-21 11:32:06 +0100
commit3572319b4cb1a4163624a5fe328886f1928dbc4a (patch)
tree5e190a6d9ca8a2e0f998ef578f895efee9450fb4 /test/functional/lua/vim_spec.lua
parent6fd13eeddaf5db89c1b81cc7d3d3f1a7da5401a7 (diff)
downloadrneovim-3572319b4cb1a4163624a5fe328886f1928dbc4a.tar.gz
rneovim-3572319b4cb1a4163624a5fe328886f1928dbc4a.tar.bz2
rneovim-3572319b4cb1a4163624a5fe328886f1928dbc4a.zip
feat(vim.validate): improve fast form and deprecate spec form
Problem: `vim.validate()` takes two forms when it only needs one. Solution: - Teach the fast form all the features of the spec form. - Deprecate the spec form. - General optimizations for both forms. - Add a `message` argument which can be used alongside or in place of the `optional` argument.
Diffstat (limited to 'test/functional/lua/vim_spec.lua')
-rw-r--r--test/functional/lua/vim_spec.lua105
1 files changed, 82 insertions, 23 deletions
diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua
index cba6c89c16..b32712860a 100644
--- a/test/functional/lua/vim_spec.lua
+++ b/test/functional/lua/vim_spec.lua
@@ -1358,7 +1358,79 @@ describe('lua stdlib', function()
eq('{"a": {}, "b": []}', exec_lua([[ return vim.fn.json_encode({a=vim.empty_dict(), b={}}) ]]))
end)
- it('vim.validate', function()
+ it('vim.validate (fast form)', function()
+ exec_lua("vim.validate('arg1', {}, 'table')")
+ exec_lua("vim.validate('arg1', nil, 'table', true)")
+ exec_lua("vim.validate('arg1', { foo='foo' }, 'table')")
+ exec_lua("vim.validate('arg1', { 'foo' }, 'table')")
+ exec_lua("vim.validate('arg1', 'foo', 'string')")
+ exec_lua("vim.validate('arg1', nil, 'string', true)")
+ exec_lua("vim.validate('arg1', 1, 'number')")
+ exec_lua("vim.validate('arg1', 0, 'number')")
+ exec_lua("vim.validate('arg1', 0.1, 'number')")
+ exec_lua("vim.validate('arg1', nil, 'number', true)")
+ exec_lua("vim.validate('arg1', true, 'boolean')")
+ exec_lua("vim.validate('arg1', false, 'boolean')")
+ exec_lua("vim.validate('arg1', nil, 'boolean', true)")
+ exec_lua("vim.validate('arg1', function()end, 'function')")
+ exec_lua("vim.validate('arg1', nil, 'function', true)")
+ exec_lua("vim.validate('arg1', nil, 'nil')")
+ exec_lua("vim.validate('arg1', nil, 'nil', true)")
+ exec_lua("vim.validate('arg1', coroutine.create(function()end), 'thread')")
+ exec_lua("vim.validate('arg1', nil, 'thread', true)")
+ exec_lua("vim.validate('arg1', 2, function(a) return (a % 2) == 0 end, 'even number')")
+ exec_lua("vim.validate('arg1', 5, {'number', 'string'})")
+ exec_lua("vim.validate('arg2', 'foo', {'number', 'string'})")
+
+ matches('arg1: expected number, got nil', pcall_err(vim.validate, 'arg1', nil, 'number'))
+ matches('arg1: expected string, got nil', pcall_err(vim.validate, 'arg1', nil, 'string'))
+ matches('arg1: expected table, got nil', pcall_err(vim.validate, 'arg1', nil, 'table'))
+ matches('arg1: expected function, got nil', pcall_err(vim.validate, 'arg1', nil, 'function'))
+ matches('arg1: expected string, got number', pcall_err(vim.validate, 'arg1', 5, 'string'))
+ matches('arg1: expected table, got number', pcall_err(vim.validate, 'arg1', 5, 'table'))
+ matches('arg1: expected function, got number', pcall_err(vim.validate, 'arg1', 5, 'function'))
+ matches('arg1: expected number, got string', pcall_err(vim.validate, 'arg1', '5', 'number'))
+ matches('arg1: expected x, got number', pcall_err(exec_lua, "vim.validate('arg1', 1, 'x')"))
+ matches('invalid validator: 1', pcall_err(exec_lua, "vim.validate('arg1', 1, 1)"))
+ matches('invalid arguments', pcall_err(exec_lua, "vim.validate('arg1', { 1 })"))
+
+ -- Validated parameters are required by default.
+ matches(
+ 'arg1: expected string, got nil',
+ pcall_err(exec_lua, "vim.validate('arg1', nil, 'string')")
+ )
+ -- Explicitly required.
+ matches(
+ 'arg1: expected string, got nil',
+ pcall_err(exec_lua, "vim.validate('arg1', nil, 'string', false)")
+ )
+
+ matches(
+ 'arg1: expected table, got number',
+ pcall_err(exec_lua, "vim.validate('arg1', 1, 'table')")
+ )
+
+ matches(
+ 'arg1: expected even number, got 3',
+ pcall_err(exec_lua, "vim.validate('arg1', 3, function(a) return a == 1 end, 'even number')")
+ )
+ matches(
+ 'arg1: expected %?, got 3',
+ pcall_err(exec_lua, "vim.validate('arg1', 3, function(a) return a == 1 end)")
+ )
+ matches(
+ 'arg1: expected number|string, got nil',
+ pcall_err(exec_lua, "vim.validate('arg1', nil, {'number', 'string'})")
+ )
+
+ -- Pass an additional message back.
+ matches(
+ 'arg1: expected %?, got 3. Info: TEST_MSG',
+ pcall_err(exec_lua, "vim.validate('arg1', 3, function(a) return a == 1, 'TEST_MSG' end)")
+ )
+ end)
+
+ it('vim.validate (spec form)', function()
exec_lua("vim.validate{arg1={{}, 'table' }}")
exec_lua("vim.validate{arg1={{}, 't' }}")
exec_lua("vim.validate{arg1={nil, 't', true }}")
@@ -1387,29 +1459,11 @@ describe('lua stdlib', function()
exec_lua("vim.validate{arg1={{}, 't' }, arg2={ 'foo', 's' }}")
exec_lua("vim.validate{arg1={2, function(a) return (a % 2) == 0 end, 'even number' }}")
exec_lua("vim.validate{arg1={5, {'n', 's'} }, arg2={ 'foo', {'n', 's'} }}")
- vim.validate('arg1', 5, 'number')
- vim.validate('arg1', '5', 'string')
- vim.validate('arg1', { 5 }, 'table')
- vim.validate('arg1', function()
- return 5
- end, 'function')
- vim.validate('arg1', nil, 'number', true)
- vim.validate('arg1', nil, 'string', true)
- vim.validate('arg1', nil, 'table', true)
- vim.validate('arg1', nil, 'function', true)
- matches('arg1: expected number, got nil', pcall_err(vim.validate, 'arg1', nil, 'number'))
- matches('arg1: expected string, got nil', pcall_err(vim.validate, 'arg1', nil, 'string'))
- matches('arg1: expected table, got nil', pcall_err(vim.validate, 'arg1', nil, 'table'))
- matches('arg1: expected function, got nil', pcall_err(vim.validate, 'arg1', nil, 'function'))
- matches('arg1: expected string, got number', pcall_err(vim.validate, 'arg1', 5, 'string'))
- matches('arg1: expected table, got number', pcall_err(vim.validate, 'arg1', 5, 'table'))
- matches('arg1: expected function, got number', pcall_err(vim.validate, 'arg1', 5, 'function'))
- matches('arg1: expected number, got string', pcall_err(vim.validate, 'arg1', '5', 'number'))
matches('expected table, got number', pcall_err(exec_lua, "vim.validate{ 1, 'x' }"))
- matches('invalid type name: x', pcall_err(exec_lua, "vim.validate{ arg1={ 1, 'x' }}"))
- matches('invalid type name: 1', pcall_err(exec_lua, 'vim.validate{ arg1={ 1, 1 }}'))
- matches('invalid type name: nil', pcall_err(exec_lua, 'vim.validate{ arg1={ 1 }}'))
+ matches('arg1: expected x, got number', pcall_err(exec_lua, "vim.validate{ arg1={ 1, 'x' }}"))
+ matches('invalid validator: 1', pcall_err(exec_lua, 'vim.validate{ arg1={ 1, 1 }}'))
+ matches('invalid validator: nil', pcall_err(exec_lua, 'vim.validate{ arg1={ 1 }}'))
-- Validated parameters are required by default.
matches(
@@ -4094,11 +4148,16 @@ describe('vim.keymap', function()
)
matches(
- 'opts: expected table, got function',
+ 'rhs: expected string|function, got number',
pcall_err(exec_lua, [[vim.keymap.set({}, 'x', 42, function() end)]])
)
matches(
+ 'opts: expected table, got function',
+ pcall_err(exec_lua, [[vim.keymap.set({}, 'x', 'x', function() end)]])
+ )
+
+ matches(
'rhs: expected string|function, got number',
pcall_err(exec_lua, [[vim.keymap.set('z', 'x', 42)]])
)