From 2b87485c22e2e42eba54d57454b33dca02f9d67c Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 29 Nov 2018 23:26:21 +0100 Subject: test: Extend {unit,functional}.helpers with global helpers Automatically include all "global helper" util functions in the unit.helpers and functional.helpers and modules. So tests don't need to expicitly do: local global_helpers = require('test.helpers') --- test/helpers.lua | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 89cf13f917..966b7b408f 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -3,6 +3,7 @@ local assert = require('luassert') local luv = require('luv') local lfs = require('lfs') local relpath = require('pl.path').relpath +local Paths = require('test.config.paths') local quote_me = '[^.%w%+%-%@%_%/]' -- complement (needn't quote) local function shell_quote(str) @@ -420,6 +421,7 @@ local function updated(d, d2) return d end +-- Concat list-like tables. local function concat_tables(...) local ret = {} for i = 1, select('#', ...) do @@ -433,6 +435,34 @@ local function concat_tables(...) return ret end +-- Concat map-like tables. +-- +-- behavior: Decides what to do if a key is found in more than one map: +-- "error": raise an error +-- "keep": skip +-- "force": set the item again +local function map_extend(behavior, ...) + if (behavior ~= 'error' and behavior ~= 'keep' and behavior ~= 'force') then + error('invalid "behavior": '..tostring(behavior)) + end + local ret = {} + for i = 1, select('#', ...) do + local tbl = select(i, ...) + if tbl then + for k, v in pairs(tbl) do + if behavior ~= 'force' and ret[k] ~= nil then + if behavior == 'error' then + error('key found in more than one map: '..k) + end -- Else behavior is "keep". + else + ret[k] = v + end + end + end + end + return ret +end + local function dedent(str, leave_indent) -- find minimum common indent across lines local indent = nil @@ -771,6 +801,7 @@ local module = { intchar2lua = intchar2lua, isCI = isCI, map = map, + map_extend = map_extend, matches = matches, mergedicts_copy = mergedicts_copy, near = near, @@ -792,5 +823,6 @@ local module = { which = which, write_file = write_file, } +module = map_extend('error', module, Paths) return module -- cgit From bba75eb184cee3d96264a392e2083f5b50732214 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Fri, 18 Jan 2019 00:44:35 +0100 Subject: lua/stdlib: Introduce vim.shared This is where "pure functions" can live, which can be shared by Nvim and test logic which may not have a running Nvim instance available. If in the future we use Nvim itself as the Lua engine for tests, then these functions could be moved directly onto the `vim` Lua module. closes #6580 --- test/helpers.lua | 33 +++------------------------------ 1 file changed, 3 insertions(+), 30 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 966b7b408f..e987fa69db 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -1,4 +1,5 @@ require('vim.compat') +local shared = require('vim.shared') local assert = require('luassert') local luv = require('luv') local lfs = require('lfs') @@ -435,34 +436,6 @@ local function concat_tables(...) return ret end --- Concat map-like tables. --- --- behavior: Decides what to do if a key is found in more than one map: --- "error": raise an error --- "keep": skip --- "force": set the item again -local function map_extend(behavior, ...) - if (behavior ~= 'error' and behavior ~= 'keep' and behavior ~= 'force') then - error('invalid "behavior": '..tostring(behavior)) - end - local ret = {} - for i = 1, select('#', ...) do - local tbl = select(i, ...) - if tbl then - for k, v in pairs(tbl) do - if behavior ~= 'force' and ret[k] ~= nil then - if behavior == 'error' then - error('key found in more than one map: '..k) - end -- Else behavior is "keep". - else - ret[k] = v - end - end - end - end - return ret -end - local function dedent(str, leave_indent) -- find minimum common indent across lines local indent = nil @@ -801,7 +774,6 @@ local module = { intchar2lua = intchar2lua, isCI = isCI, map = map, - map_extend = map_extend, matches = matches, mergedicts_copy = mergedicts_copy, near = near, @@ -816,6 +788,7 @@ local module = { shallowcopy = shallowcopy, sleep = sleep, table_contains = table_contains, + tbl_extend = shared.tbl_extend, table_flatten = table_flatten, tmpname = tmpname, uname = uname, @@ -823,6 +796,6 @@ local module = { which = which, write_file = write_file, } -module = map_extend('error', module, Paths) +module = shared.tbl_extend('error', module, Paths) return module -- cgit From 9d7aaf7149a5543b1ddfc89fb506a1019bec3ffb Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sat, 18 May 2019 16:00:06 +0200 Subject: lua/shared: move table util funcs to vim.shared Use `tbl_` prefix for all table-util functions. Specify in the function docstring if it expects a list-like or map-like table. --- test/helpers.lua | 36 +----------------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index e987fa69db..cc5f05bdee 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -612,37 +612,6 @@ local function fixtbl_rec(tbl) return fixtbl(tbl) end --- From https://github.com/premake/premake-core/blob/master/src/base/table.lua -local function table_flatten(arr) - local result = {} - local function _table_flatten(_arr) - local n = #_arr - for i = 1, n do - local v = _arr[i] - if type(v) == "table" then - _table_flatten(v) - elseif v then - table.insert(result, v) - end - end - end - _table_flatten(arr) - return result -end - --- Checks if a list-like (vector) table contains `value`. -local function table_contains(t, value) - if type(t) ~= 'table' then - error('t must be a table') - end - for _,v in ipairs(t) do - if v == value then - return true - end - end - return false -end - local function hexdump(str) local len = string.len(str) local dump = "" @@ -787,15 +756,12 @@ local module = { repeated_read_cmd = repeated_read_cmd, shallowcopy = shallowcopy, sleep = sleep, - table_contains = table_contains, - tbl_extend = shared.tbl_extend, - table_flatten = table_flatten, tmpname = tmpname, uname = uname, updated = updated, which = which, write_file = write_file, } -module = shared.tbl_extend('error', module, Paths) +module = shared.tbl_extend('error', module, Paths, shared) return module -- cgit