lua: move test helper function, map and filter, to vim.shared module

This commit is contained in:
Hirokazu Hata 2020-02-18 17:41:29 +09:00
parent a2efc9cf8b
commit e2ed8053bf
No known key found for this signature in database
GPG Key ID: 9AA9860369AE0DE2
8 changed files with 61 additions and 25 deletions

View File

@ -135,6 +135,36 @@ function vim.tbl_values(t)
return values
end
--- Apply a function to all values of a table.
---
--@param func function or callable table
--@param t table
function vim.tbl_map(func, t)
vim.validate{func={func,'c'},t={t,'t'}}
local rettab = {}
for k, v in pairs(t) do
rettab[k] = func(v)
end
return rettab
end
--- Filter a table using a predicate function
---
--@param func function or callable table
--@param t table
function vim.tbl_filter(func, t)
vim.validate{func={func,'c'},t={t,'t'}}
local rettab = {}
for _, entry in pairs(t) do
if func(entry) then
table.insert(rettab, entry)
end
end
return rettab
end
--- Checks if a list-like (vector) table contains `value`.
---
--@param t Table to check

View File

@ -6,7 +6,7 @@ local command = helpers.command
local eq = helpers.eq
local eval = helpers.eval
local feed = helpers.feed
local map = helpers.map
local map = helpers.tbl_map
local nvim = helpers.nvim
local parse_context = helpers.parse_context
local redir_exec = helpers.redir_exec

View File

@ -15,9 +15,9 @@ local check_cores = global_helpers.check_cores
local check_logs = global_helpers.check_logs
local dedent = global_helpers.dedent
local eq = global_helpers.eq
local filter = global_helpers.filter
local filter = global_helpers.tbl_filter
local is_os = global_helpers.is_os
local map = global_helpers.map
local map = global_helpers.tbl_map
local ok = global_helpers.ok
local sleep = global_helpers.sleep
local tbl_contains = global_helpers.tbl_contains

View File

@ -384,6 +384,30 @@ describe('lua stdlib', function()
end
end)
it('vim.tbl_map', function()
eq({}, exec_lua([[
return vim.tbl_map(function(v) return v * 2 end, {})
]]))
eq({2, 4, 6}, exec_lua([[
return vim.tbl_map(function(v) return v * 2 end, {1, 2, 3})
]]))
eq({{i=2}, {i=4}, {i=6}}, exec_lua([[
return vim.tbl_map(function(v) return { i = v.i * 2 } end, {{i=1}, {i=2}, {i=3}})
]]))
end)
it('vim.tbl_filter', function()
eq({}, exec_lua([[
return vim.tbl_filter(function(v) return (v % 2) == 0 end, {})
]]))
eq({2}, exec_lua([[
return vim.tbl_filter(function(v) return (v % 2) == 0 end, {1, 2, 3})
]]))
eq({{i=2}}, exec_lua([[
return vim.tbl_filter(function(v) return (v.i % 2) == 0 end, {{i=1}, {i=2}, {i=3}})
]]))
end)
it('vim.tbl_islist', function()
eq(true, exec_lua("return vim.tbl_islist({})"))
eq(false, exec_lua("return vim.tbl_islist(vim.empty_dict())"))

View File

@ -6,8 +6,8 @@ local insert = helpers.insert
local feed = helpers.feed
local expect = helpers.expect
local eq = helpers.eq
local map = helpers.map
local filter = helpers.filter
local map = helpers.tbl_map
local filter = helpers.tbl_filter
local feed_command = helpers.feed_command
local curbuf_contents = helpers.curbuf_contents
local funcs = helpers.funcs

View File

@ -290,24 +290,6 @@ module.tmpname = (function()
end)
end)()
function module.map(func, tab)
local rettab = {}
for k, v in pairs(tab) do
rettab[k] = func(v)
end
return rettab
end
function module.filter(filter_func, tab)
local rettab = {}
for _, entry in pairs(tab) do
if filter_func(entry) then
table.insert(rettab, entry)
end
end
return rettab
end
function module.hasenv(name)
local env = os.getenv(name)
if env and env ~= '' then

View File

@ -14,7 +14,7 @@ local cimport = helpers.cimport
local to_cstr = helpers.to_cstr
local alloc_log_new = helpers.alloc_log_new
local concat_tables = helpers.concat_tables
local map = helpers.map
local map = helpers.tbl_map
local a = eval_helpers.alloc_logging_helpers
local int = eval_helpers.int

View File

@ -13,7 +13,7 @@ local syscall = nil
local check_cores = global_helpers.check_cores
local dedent = global_helpers.dedent
local neq = global_helpers.neq
local map = global_helpers.map
local map = global_helpers.tbl_map
local eq = global_helpers.eq
local trim = global_helpers.trim