mirror of
https://github.com/neovim/neovim.git
synced 2025-12-11 09:02:40 +00:00
lua: move test helper function, map and filter, to vim.shared module
This commit is contained in:
@@ -135,6 +135,36 @@ function vim.tbl_values(t)
|
|||||||
return values
|
return values
|
||||||
end
|
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`.
|
--- Checks if a list-like (vector) table contains `value`.
|
||||||
---
|
---
|
||||||
--@param t Table to check
|
--@param t Table to check
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ local command = helpers.command
|
|||||||
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 map = helpers.map
|
local map = helpers.tbl_map
|
||||||
local nvim = helpers.nvim
|
local nvim = helpers.nvim
|
||||||
local parse_context = helpers.parse_context
|
local parse_context = helpers.parse_context
|
||||||
local redir_exec = helpers.redir_exec
|
local redir_exec = helpers.redir_exec
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ local check_cores = global_helpers.check_cores
|
|||||||
local check_logs = global_helpers.check_logs
|
local check_logs = global_helpers.check_logs
|
||||||
local dedent = global_helpers.dedent
|
local dedent = global_helpers.dedent
|
||||||
local eq = global_helpers.eq
|
local eq = global_helpers.eq
|
||||||
local filter = global_helpers.filter
|
local filter = global_helpers.tbl_filter
|
||||||
local is_os = global_helpers.is_os
|
local is_os = global_helpers.is_os
|
||||||
local map = global_helpers.map
|
local map = global_helpers.tbl_map
|
||||||
local ok = global_helpers.ok
|
local ok = global_helpers.ok
|
||||||
local sleep = global_helpers.sleep
|
local sleep = global_helpers.sleep
|
||||||
local tbl_contains = global_helpers.tbl_contains
|
local tbl_contains = global_helpers.tbl_contains
|
||||||
|
|||||||
@@ -384,6 +384,30 @@ describe('lua stdlib', function()
|
|||||||
end
|
end
|
||||||
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()
|
it('vim.tbl_islist', function()
|
||||||
eq(true, exec_lua("return vim.tbl_islist({})"))
|
eq(true, exec_lua("return vim.tbl_islist({})"))
|
||||||
eq(false, exec_lua("return vim.tbl_islist(vim.empty_dict())"))
|
eq(false, exec_lua("return vim.tbl_islist(vim.empty_dict())"))
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ local insert = helpers.insert
|
|||||||
local feed = helpers.feed
|
local feed = helpers.feed
|
||||||
local expect = helpers.expect
|
local expect = helpers.expect
|
||||||
local eq = helpers.eq
|
local eq = helpers.eq
|
||||||
local map = helpers.map
|
local map = helpers.tbl_map
|
||||||
local filter = helpers.filter
|
local filter = helpers.tbl_filter
|
||||||
local feed_command = helpers.feed_command
|
local feed_command = helpers.feed_command
|
||||||
local curbuf_contents = helpers.curbuf_contents
|
local curbuf_contents = helpers.curbuf_contents
|
||||||
local funcs = helpers.funcs
|
local funcs = helpers.funcs
|
||||||
|
|||||||
@@ -290,24 +290,6 @@ module.tmpname = (function()
|
|||||||
end)
|
end)
|
||||||
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)
|
function module.hasenv(name)
|
||||||
local env = os.getenv(name)
|
local env = os.getenv(name)
|
||||||
if env and env ~= '' then
|
if env and env ~= '' then
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ local cimport = helpers.cimport
|
|||||||
local to_cstr = helpers.to_cstr
|
local to_cstr = helpers.to_cstr
|
||||||
local alloc_log_new = helpers.alloc_log_new
|
local alloc_log_new = helpers.alloc_log_new
|
||||||
local concat_tables = helpers.concat_tables
|
local concat_tables = helpers.concat_tables
|
||||||
local map = helpers.map
|
local map = helpers.tbl_map
|
||||||
|
|
||||||
local a = eval_helpers.alloc_logging_helpers
|
local a = eval_helpers.alloc_logging_helpers
|
||||||
local int = eval_helpers.int
|
local int = eval_helpers.int
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ local syscall = nil
|
|||||||
local check_cores = global_helpers.check_cores
|
local check_cores = global_helpers.check_cores
|
||||||
local dedent = global_helpers.dedent
|
local dedent = global_helpers.dedent
|
||||||
local neq = global_helpers.neq
|
local neq = global_helpers.neq
|
||||||
local map = global_helpers.map
|
local map = global_helpers.tbl_map
|
||||||
local eq = global_helpers.eq
|
local eq = global_helpers.eq
|
||||||
local trim = global_helpers.trim
|
local trim = global_helpers.trim
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user