diff options
author | ZyX <kp-pav@yandex.ru> | 2017-11-12 18:52:49 +0300 |
---|---|---|
committer | ZyX <kp-pav@yandex.ru> | 2017-11-13 01:11:13 +0300 |
commit | 39c75d31be98e4cbb63a01c398d89e231f71f380 (patch) | |
tree | 331628fd5d82d12a5524157c0967cf4d12e7cb7e | |
parent | 556451a7f2fd513db33b9d7ac1b653d356b7b915 (diff) | |
download | rneovim-39c75d31be98e4cbb63a01c398d89e231f71f380.tar.gz rneovim-39c75d31be98e4cbb63a01c398d89e231f71f380.tar.bz2 rneovim-39c75d31be98e4cbb63a01c398d89e231f71f380.zip |
unittests: Fix automatic test case generation
-rw-r--r-- | test/helpers.lua | 93 | ||||
-rw-r--r-- | test/unit/viml/expressions/parser_spec.lua | 98 |
2 files changed, 148 insertions, 43 deletions
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, diff --git a/test/unit/viml/expressions/parser_spec.lua b/test/unit/viml/expressions/parser_spec.lua index cfcd63f005..25a41518eb 100644 --- a/test/unit/viml/expressions/parser_spec.lua +++ b/test/unit/viml/expressions/parser_spec.lua @@ -27,6 +27,7 @@ local format_string = global_helpers.format_string local format_luav = global_helpers.format_luav local intchar2lua = global_helpers.intchar2lua local REMOVE_THIS = global_helpers.REMOVE_THIS +local dictdiff = global_helpers.dictdiff local lib = cimport('./src/nvim/viml/parser/expressions.h', './src/nvim/syntax.h') @@ -180,9 +181,31 @@ child_call_once(function() end end) -local function format_check(expr, flags, ast, hls) +local function hls_to_hl_fs(hls) + local ret = {} + local next_col = 0 + for i, v in ipairs(hls) do + local group, line, col, str = v:match('^NVim([a-zA-Z]+):(%d+):(%d+):(.*)$') + col = tonumber(col) + line = tonumber(line) + assert(line == 0) + local col_shift = col - next_col + assert(col_shift >= 0) + next_col = col + #str + ret[i] = format_string('hl(%r, %r%s)', + group, + str, + (col_shift == 0 + and '' + or (', %u'):format(col_shift))) + end + return ret +end + +local function format_check(expr, format_check_data) -- That forces specific order. - print( format_string('\ncheck_parsing(%r, %u, {', expr, flags)) + local zdata = format_check_data[0] + print(format_string('\ncheck_parsing(%r, {', expr, flags)) local digits = ' -- ' local digits2 = ' -- ' for i = 0, #expr - 1 do @@ -195,27 +218,56 @@ local function format_check(expr, flags, ast, hls) if #expr > 10 then print(digits2) end - print(' ast = ' .. format_luav(ast.ast, ' ') .. ',') - if ast.err then + print(' ast = ' .. format_luav(zdata.ast.ast, ' ') .. ',') + if zdata.ast.err then print(' err = {') - print(' arg = ' .. format_luav(ast.err.arg) .. ',') - print(' msg = ' .. format_luav(ast.err.msg) .. ',') + print(' arg = ' .. format_luav(zdata.ast.err.arg) .. ',') + print(' msg = ' .. format_luav(zdata.ast.err.msg) .. ',') print(' },') end print('}, {') - local next_col = 0 - for _, v in ipairs(hls) do - local group, line, col, str = v:match('NVim([a-zA-Z]+):(%d+):(%d+):(.*)') - col = tonumber(col) - line = tonumber(line) - assert(line == 0) - local col_shift = col - next_col - assert(col_shift >= 0) - next_col = col + #str - print(format_string(' hl(%r, %r%s),', - group, - str, - (col_shift == 0 and '' or (', %u'):format(col_shift)))) + for _, v in ipairs(zdata.hl_fs) do + print(' ' .. v .. ',') + end + local diffs = {} + local diffs_num = 0 + for flags, v in pairs(format_check_data) do + if flags ~= 0 then + diffs[flags] = dictdiff(zdata, v) + if diffs[flags] then + if flags == 3 then + if (dictdiff(format_check_data[1], format_check_data[3]) == nil + or dictdiff(format_check_data[2], format_check_data[3]) == nil) then + diffs[flags] = nil + else + diffs_num = diffs_num + 1 + end + else + diffs_num = diffs_num + 1 + end + end + end + end + if diffs_num ~= 0 then + print('}, {') + local flags = 1 + while diffs_num ~= 0 do + if diffs[flags] then + diffs_num = diffs_num - 1 + local diff = diffs[flags] + print((' [%u] = {'):format(flags)) + if diff.ast then + print(' ast = ' .. format_luav(diff.ast, ' ')) + end + if diff.hl_fs then + print(' hl_fs = ' .. format_luav(diff.hl_fs, ' ', { + literal_strings=true + })) + end + print(' },') + end + flags = flags + 1 + end end print('})') end @@ -387,8 +439,7 @@ end) describe('Expressions parser', function() local function check_parsing(str, exp_ast, exp_highlighting_fs, nz_flags_exps) nz_flags_exps = nz_flags_exps or {} - local format_check_data = function() - end + local format_check_data = {} for _, flags in ipairs({0, 1, 2, 3}) do debug_log(('Running test case (%s, %u)'):format(str, flags)) local err, msg = pcall(function() @@ -418,7 +469,7 @@ describe('Expressions parser', function() end end if exp_ast == nil then - format_check(str, ast, hls) + format_check_data[flags] = {ast=ast, hl_fs=hls_to_hl_fs(hls)} else eq(exps.ast, ast) if exp_highlighting_fs then @@ -441,6 +492,9 @@ describe('Expressions parser', function() error(msg) end end + if exp_ast == nil then + format_check(str, format_check_data) + end end local function hl(group, str, shift) return function(next_col) |