From 520c0b91a528e7943bc6163a65de7adde387869b Mon Sep 17 00:00:00 2001 From: ZyX Date: Fri, 29 Sep 2017 00:57:23 +0300 Subject: test/helpers: Add format_string and format_luav First intended to provide %r functionality like in Python (and also support for %*.*s, but this was not checked), second adds nice table formatting for use in cases similar to screen:snapshot_util(). --- test/helpers.lua | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 260f10002e..d5356416af 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -307,6 +307,83 @@ local function dedent(str, leave_indent) return str end +local SUBTBL = { + '\\000', '\\001', '\\002', '\\003', '\\004', + '\\005', '\\006', '\\007', '\\008', '\\t', + '\\n', '\\011', '\\012', '\\r', '\\014', + '\\015', '\\016', '\\017', '\\018', '\\019', + '\\020', '\\021', '\\022', '\\023', '\\024', + '\\025', '\\026', '\\027', '\\028', '\\029', + '\\030', '\\031', +} + +local format_luav + +format_luav = function(v, indent) + local linesep = '\n' + local next_indent = nil + if indent == nil then + indent = '' + linesep = '' + else + next_indent = indent .. ' ' + end + local ret = '' + if type(v) == 'string' then + ret = '\'' .. tostring(v):gsub('[\'\\]', '\\%0'):gsub('[%z\1-\31]', function(match) + return SUBTBL[match:byte()] + end) .. '\'' + elseif type(v) == 'table' then + local processed_keys = {} + ret = '{' .. linesep + for i, subv in ipairs(v) do + ret = ret .. (next_indent or '') .. format_luav(subv, next_indent) .. ',\n' + processed_keys[i] = true + end + for k, subv in pairs(v) do + if not processed_keys[k] then + if type(k) == 'string' and k:match('^[a-zA-Z_][a-zA-Z0-9_]*$') then + ret = ret .. next_indent .. k .. ' = ' + else + ret = ret .. next_indent .. '[' .. format_luav(k) .. '] = ' + end + ret = ret .. format_luav(subv, next_indent) .. ',\n' + end + end + ret = ret .. indent .. '}' + else + -- Not implemented yet + assert(false) + end + return ret +end + +local function format_string(fmt, ...) + local i = 0 + local args = {...} + local function getarg() + i = i + 1 + return args[i] + end + local ret = fmt:gsub('%%[0-9*]*%.?[0-9*]*[cdEefgGiouXxqsr%%]', function(match) + local subfmt = match:gsub('%*', function(match) + return getarg() + end) + local arg = nil + if subfmt:sub(-1) ~= '%' then + arg = getarg() + end + if subfmt:sub(-1) == 'r' then + -- %r is like %q, but it is supposed to single-quote strings and not + -- double-quote them, and also work not only for strings. + subfmt = subfmt:sub(1, -2) .. 's' + arg = format_luav(arg) + end + return subfmt:format(arg) + end) + return ret +end + return { eq = eq, neq = neq, @@ -323,4 +400,6 @@ return { shallowcopy = shallowcopy, concat_tables = concat_tables, dedent = dedent, + format_luav = format_luav, + format_string = format_string, } -- cgit From af38cea133f5ebb67208cedd289e408cd1dad15a Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 8 Oct 2017 21:52:38 +0300 Subject: viml/parser/expressions: Add support for string parsing --- test/helpers.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index d5356416af..e7d8c185ba 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -330,9 +330,10 @@ format_luav = function(v, indent) end local ret = '' if type(v) == 'string' then - ret = '\'' .. tostring(v):gsub('[\'\\]', '\\%0'):gsub('[%z\1-\31]', function(match) - return SUBTBL[match:byte()] - end) .. '\'' + ret = tostring(v):gsub('[\'\\]', '\\%0'):gsub('[%z\1-\31]', function(match) + return SUBTBL[match:byte() + 1] + end) + ret = '\'' .. ret .. '\'' elseif type(v) == 'table' then local processed_keys = {} ret = '{' .. linesep -- cgit From c286155bfa53c828ebe5479fd81a544740a92403 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 15 Oct 2017 19:06:41 +0300 Subject: viml/parser/expressions: Create tests for latest additions --- test/helpers.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index e7d8c185ba..6a42963d7f 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -352,7 +352,14 @@ format_luav = function(v, indent) end end ret = ret .. indent .. '}' + elseif type(v) == 'number' then + if v % 1 == 0 then + ret = ('%d'):format(v) + else + ret = ('%e'):format(v) + end else + print(type(v)) -- Not implemented yet assert(false) end -- cgit From 6c19cbef2611c389da6f3e06d8c8eb635d065774 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 15 Oct 2017 20:05:35 +0300 Subject: viml/parser/expressions,tests: Add AST freeing, with sanity checks --- test/helpers.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 6a42963d7f..83e78ba059 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -358,6 +358,8 @@ format_luav = function(v, indent) else ret = ('%e'):format(v) end + elseif type(v) == 'nil' then + ret = 'nil' else print(type(v)) -- Not implemented yet -- cgit From 3ecb95298ffd9ef6ee681876f2d32553fd222b96 Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 30 Oct 2017 01:48:32 +0300 Subject: tests: Fix testlint errors --- test/helpers.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 83e78ba059..10f2f80191 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -376,8 +376,8 @@ local function format_string(fmt, ...) return args[i] end local ret = fmt:gsub('%%[0-9*]*%.?[0-9*]*[cdEefgGiouXxqsr%%]', function(match) - local subfmt = match:gsub('%*', function(match) - return getarg() + local subfmt = match:gsub('%*', function() + return tostring(getarg()) end) local arg = nil if subfmt:sub(-1) ~= '%' then -- cgit From 7bc6de75263f58c6c4f999bc86a6454ae9f28b80 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 5 Nov 2017 02:41:44 +0300 Subject: api/vim,functests: Add tests for nvim_parse_expression, fix found bugs --- test/helpers.lua | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 10f2f80191..67ac82e6fd 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -394,6 +394,11 @@ local function format_string(fmt, ...) return ret end +local function intchar2lua(ch) + ch = tonumber(ch) + return (20 <= ch and ch < 127) and ('%c'):format(ch) or ch +end + return { eq = eq, neq = neq, @@ -412,4 +417,5 @@ return { dedent = dedent, format_luav = format_luav, format_string = format_string, + intchar2lua = intchar2lua, } -- cgit From 7849070f998902bb6aae5d6f9147d4daf5421b36 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 5 Nov 2017 21:06:12 +0300 Subject: tests: Add missing test cases --- test/helpers.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 67ac82e6fd..20fe23821f 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -271,6 +271,45 @@ local function shallowcopy(orig) return copy end +local deepcopy + +local function id(v) + return v +end + +local deepcopy_funcs = { + table = function(orig) + local copy = {} + for k, v in pairs(orig) do + copy[deepcopy(k)] = deepcopy(v) + end + end, + number = id, + string = id, + ['nil'] = id, + boolean = id, +} + +deepcopy = function(orig) + return deepcopy_funcs[type(orig)](orig) +end + +local REMOVE_THIS = {} + +local function mergedicts_copy(d1, d2) + local ret = shallowcopy(d1) + for k, v in pairs(d2) do + if d2[k] == REMOVE_THIS then + ret[k] = nil + elseif type(d1[k]) == 'table' and type(d2[k]) == 'table' then + ret[k] = mergedicts_copy(d1[k], d2[k]) + else + ret[k] = d2[k] + end + end + return ret +end + local function concat_tables(...) local ret = {} for i = 1, select('#', ...) do @@ -413,6 +452,9 @@ return { hasenv = hasenv, which = which, shallowcopy = shallowcopy, + deepcopy = deepcopy, + mergedicts_copy = mergedicts_copy, + REMOVE_THIS = REMOVE_THIS, concat_tables = concat_tables, dedent = dedent, format_luav = format_luav, -- cgit From 42959d0e8f9e779ba4983016b24967bf02abb59f Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 6 Nov 2017 20:15:05 +0300 Subject: unittests: Add tests for vim_str2nr --- test/helpers.lua | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 20fe23821f..16b9818f12 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -310,6 +310,13 @@ local function mergedicts_copy(d1, d2) return ret end +local function updated(d, d2) + for k, v in pairs(d2) do + d[k] = v + end + return d +end + local function concat_tables(...) local ret = {} for i = 1, select('#', ...) do @@ -460,4 +467,5 @@ return { format_luav = format_luav, format_string = format_string, intchar2lua = intchar2lua, + updated = updated, } -- cgit From 4aebd00a9eeeb2f56ff53dd4e383825e997ee7be Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 6 Nov 2017 20:28:37 +0300 Subject: *: Fix linter errors --- test/helpers.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 16b9818f12..d24fae745b 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -301,10 +301,10 @@ local function mergedicts_copy(d1, d2) for k, v in pairs(d2) do if d2[k] == REMOVE_THIS then ret[k] = nil - elseif type(d1[k]) == 'table' and type(d2[k]) == 'table' then - ret[k] = mergedicts_copy(d1[k], d2[k]) + elseif type(d1[k]) == 'table' and type(v) == 'table' then + ret[k] = mergedicts_copy(d1[k], v) else - ret[k] = d2[k] + ret[k] = v end end return ret -- cgit From 39c75d31be98e4cbb63a01c398d89e231f71f380 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 12 Nov 2017 18:52:49 +0300 Subject: unittests: Fix automatic test case generation --- test/helpers.lua | 93 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 21 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index d24fae745b..6c6611d061 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -310,6 +310,43 @@ local function mergedicts_copy(d1, d2) return ret end +-- dictdiff: find a diff so that mergedicts_copy(d1, diff) is equal to d2 +-- +-- Note: does not copy values from d2 +local function dictdiff(d1, d2) + local ret = {} + local hasdiff = false + for k, v in pairs(d1) do + if d2[k] == nil then + hasdiff = true + ret[k] = REMOVE_THIS + elseif type(v) == type(d2[k]) then + if type(v) == 'table' and type(d2[k]) == 'table' then + local subdiff = dictdiff(v, d2[k]) + if subdiff ~= nil then + hasdiff = true + ret[k] = subdiff + end + elseif v ~= d2[k] then + ret[k] = d2[k] + end + else + ret[k] = d2[k] + end + end + for k, v in pairs(d2) do + if d1[k] == nil then + ret[k] = v + hasdiff = true + end + end + if hasdiff then + return ret + else + return nil + end +end + local function updated(d, d2) for k, v in pairs(d2) do d[k] = v @@ -365,39 +402,52 @@ local SUBTBL = { local format_luav -format_luav = function(v, indent) +format_luav = function(v, indent, opts) + opts = opts or {} local linesep = '\n' - local next_indent = nil + local next_indent_arg = nil if indent == nil then indent = '' linesep = '' else - next_indent = indent .. ' ' + next_indent_arg = indent .. ' ' end + local next_indent = indent .. ' ' local ret = '' if type(v) == 'string' then - ret = tostring(v):gsub('[\'\\]', '\\%0'):gsub('[%z\1-\31]', function(match) - return SUBTBL[match:byte() + 1] - end) - ret = '\'' .. ret .. '\'' - elseif type(v) == 'table' then - local processed_keys = {} - ret = '{' .. linesep - for i, subv in ipairs(v) do - ret = ret .. (next_indent or '') .. format_luav(subv, next_indent) .. ',\n' - processed_keys[i] = true + if opts.literal_strings then + ret = v + else + ret = tostring(v):gsub('[\'\\]', '\\%0'):gsub( + '[%z\1-\31]', function(match) + return SUBTBL[match:byte() + 1] + end) + ret = '\'' .. ret .. '\'' end - for k, subv in pairs(v) do - if not processed_keys[k] then - if type(k) == 'string' and k:match('^[a-zA-Z_][a-zA-Z0-9_]*$') then - ret = ret .. next_indent .. k .. ' = ' - else - ret = ret .. next_indent .. '[' .. format_luav(k) .. '] = ' + elseif type(v) == 'table' then + if v == REMOVE_THIS then + ret = 'REMOVE_THIS' + else + local processed_keys = {} + ret = '{' .. linesep + for i, subv in ipairs(v) do + ret = ('%s%s%s,\n'):format(ret, next_indent, + format_luav(subv, next_indent_arg, opts)) + processed_keys[i] = true + end + for k, subv in pairs(v) do + if not processed_keys[k] then + if type(k) == 'string' and k:match('^[a-zA-Z_][a-zA-Z0-9_]*$') then + ret = ret .. next_indent .. k .. ' = ' + else + ret = ('%s%s[%s] = '):format(ret, next_indent, + format_luav(k, nil, opts)) + end + ret = ret .. format_luav(subv, next_indent_arg, opts) .. ',\n' end - ret = ret .. format_luav(subv, next_indent) .. ',\n' end + ret = ret .. indent .. '}' end - ret = ret .. indent .. '}' elseif type(v) == 'number' then if v % 1 == 0 then ret = ('%d'):format(v) @@ -461,6 +511,7 @@ return { shallowcopy = shallowcopy, deepcopy = deepcopy, mergedicts_copy = mergedicts_copy, + dictdiff = dictdiff, REMOVE_THIS = REMOVE_THIS, concat_tables = concat_tables, dedent = dedent, -- cgit From 342239a9c53cf4857d18c0583d4cab1fdca534fa Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 13 Nov 2017 01:10:39 +0300 Subject: unittests,viml/parser/expressions: Start adding asgn parsing tests --- test/helpers.lua | 53 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 6c6611d061..ada690a4d2 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -264,6 +264,9 @@ local function which(exe) end local function shallowcopy(orig) + if type(orig) ~= 'table' then + return orig + end local copy = {} for orig_key, orig_value in pairs(orig) do copy[orig_key] = orig_value @@ -312,7 +315,7 @@ end -- dictdiff: find a diff so that mergedicts_copy(d1, diff) is equal to d2 -- --- Note: does not copy values from d2 +-- Note: does not do copies of d2 values used. local function dictdiff(d1, d2) local ret = {} local hasdiff = false @@ -321,7 +324,7 @@ local function dictdiff(d1, d2) hasdiff = true ret[k] = REMOVE_THIS elseif type(v) == type(d2[k]) then - if type(v) == 'table' and type(d2[k]) == 'table' then + if type(v) == 'table' then local subdiff = dictdiff(v, d2[k]) if subdiff ~= nil then hasdiff = true @@ -329,14 +332,16 @@ local function dictdiff(d1, d2) end elseif v ~= d2[k] then ret[k] = d2[k] + hasdiff = true end else ret[k] = d2[k] + hasdiff = true end end for k, v in pairs(d2) do if d1[k] == nil then - ret[k] = v + ret[k] = shallowcopy(v) hasdiff = true end end @@ -406,13 +411,18 @@ format_luav = function(v, indent, opts) opts = opts or {} local linesep = '\n' local next_indent_arg = nil + local indent_shift = opts.indent_shift or ' ' + local next_indent + local nl = '\n' if indent == nil then indent = '' linesep = '' + next_indent = '' + nl = ' ' else - next_indent_arg = indent .. ' ' + next_indent_arg = indent .. indent_shift + next_indent = indent .. indent_shift end - local next_indent = indent .. ' ' local ret = '' if type(v) == 'string' then if opts.literal_strings then @@ -430,10 +440,12 @@ format_luav = function(v, indent, opts) else local processed_keys = {} ret = '{' .. linesep + local non_empty = false for i, subv in ipairs(v) do - ret = ('%s%s%s,\n'):format(ret, next_indent, - format_luav(subv, next_indent_arg, opts)) + ret = ('%s%s%s,%s'):format(ret, next_indent, + format_luav(subv, next_indent_arg, opts), nl) processed_keys[i] = true + non_empty = true end for k, subv in pairs(v) do if not processed_keys[k] then @@ -443,9 +455,13 @@ format_luav = function(v, indent, opts) ret = ('%s%s[%s] = '):format(ret, next_indent, format_luav(k, nil, opts)) end - ret = ret .. format_luav(subv, next_indent_arg, opts) .. ',\n' + ret = ret .. format_luav(subv, next_indent_arg, opts) .. ',' .. nl + non_empty = true end end + if nl == ' ' and non_empty then + ret = ret:sub(1, -3) + end ret = ret .. indent .. '}' end elseif type(v) == 'number' then @@ -495,6 +511,25 @@ local function intchar2lua(ch) return (20 <= ch and ch < 127) and ('%c'):format(ch) or ch end +local fixtbl_metatable = { + __newindex = function() + assert(false) + end, +} + +local function fixtbl(tbl) + return setmetatable(tbl, fixtbl_metatable) +end + +local function fixtbl_rec(tbl) + for k, v in pairs(tbl) do + if type(v) == 'table' then + fixtbl_rec(v) + end + end + return fixtbl(tbl) +end + return { eq = eq, neq = neq, @@ -519,4 +554,6 @@ return { format_string = format_string, intchar2lua = intchar2lua, updated = updated, + fixtbl = fixtbl, + fixtbl_rec = fixtbl_rec, } -- cgit From f20f97c936f1438589c8176f62ce69c26e255f85 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Nov 2017 21:13:27 +0300 Subject: *: Fix linter errors --- test/helpers.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index ada690a4d2..fc3936a4ce 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -522,7 +522,7 @@ local function fixtbl(tbl) end local function fixtbl_rec(tbl) - for k, v in pairs(tbl) do + for _, v in pairs(tbl) do if type(v) == 'table' then fixtbl_rec(v) end -- cgit From ebb33eddd9ad0e9cec5013be2e37c8f9b0546c77 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 19 Nov 2017 21:40:34 +0300 Subject: tests: Stabilize float format and %e in format_luav and format_string --- test/helpers.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index fc3936a4ce..cef05046d8 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -395,6 +395,13 @@ local function dedent(str, leave_indent) return str end +local function format_float(v) + -- On windows exponent appears to have three digits and not two + local ret = ('%.6e'):format(v) + local l, f, es, e = ret:match('^(%-?%d)%.(%d+)e([+%-])0*(%d%d+)$') + return l .. '.' .. f .. 'e' .. es .. e +end + local SUBTBL = { '\\000', '\\001', '\\002', '\\003', '\\004', '\\005', '\\006', '\\007', '\\008', '\\t', @@ -468,7 +475,7 @@ format_luav = function(v, indent, opts) if v % 1 == 0 then ret = ('%d'):format(v) else - ret = ('%e'):format(v) + ret = format_float(v) end elseif type(v) == 'nil' then ret = 'nil' @@ -501,7 +508,11 @@ local function format_string(fmt, ...) subfmt = subfmt:sub(1, -2) .. 's' arg = format_luav(arg) end - return subfmt:format(arg) + if subfmt == '%e' then + return format_float(arg) + else + return subfmt:format(arg) + end end) return ret end -- cgit From 753d0091e8f5d3358eeb2f53288b5b07a8f13a7f Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Tue, 5 Sep 2017 17:38:14 +0200 Subject: core dumps: don't use pipe, it does not work --- test/helpers.lua | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 260f10002e..123dd111ee 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -225,21 +225,8 @@ local function check_cores(app, force) local esigns = ('='):rep(len / 2) out:write(('\n%s Core file %s %s\n'):format(esigns, core, esigns)) out:flush() - local pipe = io.popen( - db_cmd:gsub('%$_NVIM_TEST_APP', app):gsub('%$_NVIM_TEST_CORE', core) - .. ' 2>&1', 'r') - if pipe then - local bt = pipe:read('*a') - if bt then - out:write(bt) - out:write('\n') - else - out:write('Failed to read from the pipe\n') - end - else - out:write('Failed to create pipe\n') - end - out:flush() + os.execute(db_cmd:gsub('%$_NVIM_TEST_APP', app):gsub('%$_NVIM_TEST_CORE', core) .. ' 2>&1') + out:write('\n') found_cores = found_cores + 1 os.remove(core) end -- cgit From fbdc3ac4efbc97e2965b6083d429beabe261461c Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 3 Dec 2017 20:22:09 +0300 Subject: tests: Fix linter errors --- test/helpers.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 3ec4aa511f..faf5c8e7f2 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -273,6 +273,7 @@ local deepcopy_funcs = { for k, v in pairs(orig) do copy[deepcopy(k)] = deepcopy(v) end + return copy end, number = id, string = id, -- cgit From 2316a38dd117b0bbd3b6ebb04856029c227633f6 Mon Sep 17 00:00:00 2001 From: ZyX Date: Fri, 2 Feb 2018 01:23:20 +0300 Subject: tests: Make format_string('%q', ...) output more stable It appears to be different on lua and luajit. --- test/helpers.lua | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index faf5c8e7f2..3e7e70fe07 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -423,11 +423,13 @@ format_luav = function(v, indent, opts) if opts.literal_strings then ret = v else - ret = tostring(v):gsub('[\'\\]', '\\%0'):gsub( - '[%z\1-\31]', function(match) - return SUBTBL[match:byte() + 1] - end) - ret = '\'' .. ret .. '\'' + quote = opts.dquote_strings and '"' or '\'' + ret = quote .. tostring(v):gsub( + opts.dquote_strings and '["\\]' or '[\'\\]', + '\\%0'):gsub( + '[%z\1-\31]', function(match) + return SUBTBL[match:byte() + 1] + end) .. quote end elseif type(v) == 'table' then if v == REMOVE_THIS then @@ -490,11 +492,14 @@ local function format_string(fmt, ...) if subfmt:sub(-1) ~= '%' then arg = getarg() end - if subfmt:sub(-1) == 'r' then - -- %r is like %q, but it is supposed to single-quote strings and not - -- double-quote them, and also work not only for strings. + if subfmt:sub(-1) == 'r' or subfmt:sub(-1) == 'q' then + -- %r is like built-in %q, but it is supposed to single-quote strings and + -- not double-quote them, and also work not only for strings. + -- Builtin %q is replaced here as it gives invalid and inconsistent with + -- luajit results for e.g. "\e" on lua: luajit transforms that into `\27`, + -- lua leaves as-is. + arg = format_luav(arg, nil, {dquote_strings = (subfmt:sub(-1) == 'q')}) subfmt = subfmt:sub(1, -2) .. 's' - arg = format_luav(arg) end if subfmt == '%e' then return format_float(arg) -- cgit From de10ea55f370cd57b79d23c2ae09bebf154a0f1a Mon Sep 17 00:00:00 2001 From: James McCoy Date: Fri, 2 Feb 2018 08:57:17 -0500 Subject: lint --- test/helpers.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 3e7e70fe07..1c64f41b65 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -423,7 +423,7 @@ format_luav = function(v, indent, opts) if opts.literal_strings then ret = v else - quote = opts.dquote_strings and '"' or '\'' + local quote = opts.dquote_strings and '"' or '\'' ret = quote .. tostring(v):gsub( opts.dquote_strings and '["\\]' or '[\'\\]', '\\%0'):gsub( @@ -493,7 +493,7 @@ local function format_string(fmt, ...) arg = getarg() end if subfmt:sub(-1) == 'r' or subfmt:sub(-1) == 'q' then - -- %r is like built-in %q, but it is supposed to single-quote strings and + -- %r is like built-in %q, but it is supposed to single-quote strings and -- not double-quote them, and also work not only for strings. -- Builtin %q is replaced here as it gives invalid and inconsistent with -- luajit results for e.g. "\e" on lua: luajit transforms that into `\27`, -- cgit From e72ecdb7ca1face1d16e622824aa7bd3c98f5673 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Fri, 16 Feb 2018 19:42:05 +0100 Subject: test/util: expect_msg_seq() job_spec.lua on AppVeyor (Windows) often fails like this: FAILED ] C:/projects/neovim/test/functional\core\job_spec.lua @ 72: jobs changes to given `cwd` directory C:/projects/neovim/test/functional\core\job_spec.lua:81: Expected objects to be the same. Passed in: (table) { [1] = 'notification' [2] = 'stdout' *[3] = { [1] = 0 *[2] = { [1] = 'C:\projects\neovim\Xtest-tmpdir\nvimmSjq1S\0' } } } Expected: (table) { [1] = 'notification' [2] = 'stdout' *[3] = { [1] = 0 *[2] = { [1] = 'C:\projects\neovim\Xtest-tmpdir\nvimmSjq1S\0' *[2] = '' } } } stack traceback: Message chunking is non-deterministic, so we need to try different variants. --- test/helpers.lua | 84 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 22 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index 1c64f41b65..ee7dc5dfe5 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -7,13 +7,33 @@ local check_logs_useless_lines = { ['See README_MISSING_SYSCALL_OR_IOCTL for guidance']=3, } -local eq = function(exp, act) - return assert.are.same(exp, act) +local function eq(expected, actual) + return assert.are.same(expected, actual) end -local neq = function(exp, act) +-- Tries multiple accepted ("expected") values until one succeeds. +-- First N-1 arguments are the accepted values. +-- Last (Nth) argument is the "actual" value to be compared. +local function eq_any(...) + if select('#', ...) < 2 then + error('need at least 2 arguments') + end + local args = {...} + local actual = args[#args] + local final_error = '' + for anum = 1, select('#', ...) - 1 do + local accepted = args[anum] + local status, result = pcall(eq, accepted, actual) + if status then + return result + end + final_error = final_error..tostring(result) + end + error(final_error) +end +local function neq(exp, act) return assert.are_not.same(exp, act) end -local ok = function(res) +local function ok(res) return assert.is_true(res) end @@ -534,30 +554,50 @@ 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 + return { - eq = eq, - neq = neq, - ok = ok, - check_logs = check_logs, - uname = uname, - tmpname = tmpname, - map = map, - filter = filter, - glob = glob, - check_cores = check_cores, - hasenv = hasenv, - which = which, - shallowcopy = shallowcopy, - deepcopy = deepcopy, - mergedicts_copy = mergedicts_copy, - dictdiff = dictdiff, REMOVE_THIS = REMOVE_THIS, + check_cores = check_cores, + check_logs = check_logs, concat_tables = concat_tables, dedent = dedent, + deepcopy = deepcopy, + dictdiff = dictdiff, + eq = eq, + eq_any = eq_any, + filter = filter, + fixtbl = fixtbl, + fixtbl_rec = fixtbl_rec, format_luav = format_luav, format_string = format_string, + glob = glob, + hasenv = hasenv, intchar2lua = intchar2lua, + map = map, + mergedicts_copy = mergedicts_copy, + neq = neq, + ok = ok, + shallowcopy = shallowcopy, + table_flatten = table_flatten, + tmpname = tmpname, + uname = uname, updated = updated, - fixtbl = fixtbl, - fixtbl_rec = fixtbl_rec, + which = which, } -- cgit From 7973847d025aa7af431f1e92c1ee977ceb0eb7e5 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 18 Feb 2018 19:22:44 +0100 Subject: test/util: remove eq_any() It was added in the parent commit, but ended up not being used. And I can't think of a case where it will be used: instead we would probably want to generalize expect_msg_seq() if necessary. --- test/helpers.lua | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) (limited to 'test/helpers.lua') diff --git a/test/helpers.lua b/test/helpers.lua index ee7dc5dfe5..91ceed4df1 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -10,28 +10,8 @@ local check_logs_useless_lines = { local function eq(expected, actual) return assert.are.same(expected, actual) end --- Tries multiple accepted ("expected") values until one succeeds. --- First N-1 arguments are the accepted values. --- Last (Nth) argument is the "actual" value to be compared. -local function eq_any(...) - if select('#', ...) < 2 then - error('need at least 2 arguments') - end - local args = {...} - local actual = args[#args] - local final_error = '' - for anum = 1, select('#', ...) - 1 do - local accepted = args[anum] - local status, result = pcall(eq, accepted, actual) - if status then - return result - end - final_error = final_error..tostring(result) - end - error(final_error) -end -local function neq(exp, act) - return assert.are_not.same(exp, act) +local function neq(expected, actual) + return assert.are_not.same(expected, actual) end local function ok(res) return assert.is_true(res) @@ -581,7 +561,6 @@ return { deepcopy = deepcopy, dictdiff = dictdiff, eq = eq, - eq_any = eq_any, filter = filter, fixtbl = fixtbl, fixtbl_rec = fixtbl_rec, -- cgit