diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2019-01-14 02:22:37 +0100 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2019-01-14 02:25:45 +0100 |
commit | 89d7e24891c2281e301c232c4ad882b135c11bde (patch) | |
tree | c1ad4edae69595124cffa99dd80807f5cd6f3270 /test/functional/lua/utility_functions_spec.lua | |
parent | 989fbad5028a2f7d31fef944aac166d562665477 (diff) | |
parent | 6c02ff474732dcc6ae5cf103c79298c9602fd116 (diff) | |
download | rneovim-89d7e24891c2281e301c232c4ad882b135c11bde.tar.gz rneovim-89d7e24891c2281e301c232c4ad882b135c11bde.tar.bz2 rneovim-89d7e24891c2281e301c232c4ad882b135c11bde.zip |
Merge #9463 'Lua stdlib'
Diffstat (limited to 'test/functional/lua/utility_functions_spec.lua')
-rw-r--r-- | test/functional/lua/utility_functions_spec.lua | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/test/functional/lua/utility_functions_spec.lua b/test/functional/lua/utility_functions_spec.lua index d5756e134d..6e4cfd98ef 100644 --- a/test/functional/lua/utility_functions_spec.lua +++ b/test/functional/lua/utility_functions_spec.lua @@ -2,6 +2,7 @@ local helpers = require('test.functional.helpers')(after_each) local funcs = helpers.funcs +local meths = helpers.meths local clear = helpers.clear local eq = helpers.eq @@ -106,3 +107,91 @@ 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() + -- 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)) + eq('{+a = {+b = 1+}+}', + inspect({ a = { b = 1 } }, { newline = '+', indent = '' })) + 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) |