aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/utility_functions_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/functional/lua/utility_functions_spec.lua')
-rw-r--r--test/functional/lua/utility_functions_spec.lua92
1 files changed, 92 insertions, 0 deletions
diff --git a/test/functional/lua/utility_functions_spec.lua b/test/functional/lua/utility_functions_spec.lua
index d5756e134d..5f511f4a43 100644
--- a/test/functional/lua/utility_functions_spec.lua
+++ b/test/functional/lua/utility_functions_spec.lua
@@ -2,8 +2,10 @@
local helpers = require('test.functional.helpers')(after_each)
local funcs = helpers.funcs
+local meths = helpers.meths
local clear = helpers.clear
local eq = helpers.eq
+local command = helpers.command
before_each(clear)
@@ -106,3 +108,93 @@ describe('vim.stricmp', function()
eq(1, funcs.luaeval('vim.stricmp("\\0C\\0", "\\0B\\0")'))
end)
end)
+
+describe("vim.split", function()
+ it("works", function()
+ local split = function(str, sep)
+ return meths.execute_lua('return vim.split(...)', {str, sep})
+ end
+
+ local tests = {
+ { "a,b", ",", false, { 'a', 'b' } },
+ { ":aa::bb:", ":", false, { '', 'aa', '', 'bb', '' } },
+ { "::ee::ff:", ":", false, { '', '', 'ee', '', 'ff', '' } },
+ { "ab", ".", false, { '', '', '' } },
+ { "a1b2c", "[0-9]", false, { 'a', 'b', 'c' } },
+ { "xy", "", false, { 'x', 'y' } },
+ { "here be dragons", " ", false, { "here", "be", "dragons"} },
+ { "axaby", "ab?", false, { '', 'x', 'y' } },
+ { "f v2v v3v w2w ", "([vw])2%1", false, { 'f ', ' v3v ', ' ' } },
+ { "x*yz*oo*l", "*", true, { 'x', 'yz', 'oo', 'l' } },
+ }
+
+ for _, t in ipairs(tests) do
+ eq(t[4], split(t[1], t[2], t[3]))
+ end
+
+ local loops = {
+ { "abc", ".-" },
+ }
+
+ for _, t in ipairs(loops) do
+ local status, err = pcall(split, t[1], t[2])
+ eq(false, status)
+ assert(string.match(err, "Infinite loop detected"))
+ end
+ end)
+end)
+
+describe("vim.trim", function()
+ it('works', function()
+ local trim = function(s)
+ return meths.execute_lua('return vim.trim(...)', { s })
+ end
+
+ local trims = {
+ { " a", "a" },
+ { " b ", "b" },
+ { "\tc" , "c" },
+ { "r\n", "r" },
+ }
+
+ for _, t in ipairs(trims) do
+ assert(t[2], trim(t[1]))
+ end
+
+ local status, err = pcall(trim, 2)
+ eq(false, status)
+ assert(string.match(err, "Only strings can be trimmed"))
+ end)
+end)
+
+describe("vim.inspect", function()
+ it('works', function()
+ command("source runtime/plugin/nvim.vim")
+ -- just make sure it basically works, it has its own test suite
+ local inspect = function(t, opts)
+ return meths.execute_lua('return vim.inspect(...)', { t, opts })
+ end
+
+ eq('2', inspect(2))
+
+ local i = inspect({ a = { b = 1 } }, { newline = '+', indent = '' })
+ eq('{+a = {+b = 1+}+}', i)
+ end)
+end)
+
+describe("vim.deepcopy", function()
+ it("works", function()
+ local is_dc = meths.execute_lua([[
+ local a = { x = { 1, 2 }, y = 5}
+ local b = vim.deepcopy(a)
+
+ local count = 0
+ for _ in pairs(b) do count = count + 1 end
+
+ return b.x[1] == 1 and b.x[2] == 2 and b.y == 5 and count == 2
+ and tostring(a) ~= tostring(b)
+ ]], {})
+
+ assert(is_dc)
+ end)
+end)