-
Notifications
You must be signed in to change notification settings - Fork 12
Add unified diff support for luatest.assert_equals
#438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| local pp = require("luatest.pp") | ||
|
|
||
| local M = {} | ||
|
|
||
| local function diff_by_lines(text1, text2) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I presume you used this wiki page as a reference? It'd be nice to put a link to it somewhere in a comment. |
||
| local lines1 = string.split(text1, '\n') | ||
| local lines2 = string.split(text2, '\n') | ||
|
|
||
| local m = #lines1 | ||
| local n = #lines2 | ||
| local lcs = {} | ||
|
|
||
| for i = 0, m do | ||
| lcs[i] = {} | ||
| lcs[i][0] = 0 | ||
| end | ||
|
|
||
| for j = 0, n do | ||
| lcs[0][j] = 0 | ||
| end | ||
|
|
||
| for i = 1, m do | ||
| for j = 1, n do | ||
| if lines1[i] == lines2[j] then | ||
| lcs[i][j] = lcs[i - 1][j - 1] + 1 | ||
| else | ||
| local left = lcs[i - 1][j] | ||
| local top = lcs[i][j - 1] | ||
| lcs[i][j] = left >= top and left or top | ||
| end | ||
| end | ||
| end | ||
|
|
||
| local diffs = {} | ||
| local i = m | ||
| local j = n | ||
|
|
||
| while i > 0 or j > 0 do | ||
| if i > 0 and j > 0 and lines1[i] == lines2[j] then | ||
| table.insert(diffs, 1, {'equal', lines1[i]}) | ||
| i = i - 1 | ||
| j = j - 1 | ||
| elseif j > 0 and (i == 0 or lcs[i][j - 1] >= lcs[i - 1][j]) then | ||
| table.insert(diffs, 1, {'insert', lines2[j]}) | ||
| j = j - 1 | ||
| else | ||
| table.insert(diffs, 1, {'delete', lines1[i]}) | ||
| i = i - 1 | ||
| end | ||
| end | ||
|
|
||
| return diffs | ||
| end | ||
|
|
||
| local function prettify_patch(diffs) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't look like there's much point in splitting this function in two. I'd simply merge |
||
| local out = {} | ||
|
|
||
| for _, diff in ipairs(diffs) do | ||
| local tag, line = diff[1], diff[2] | ||
| if tag == 'equal' then | ||
| table.insert(out, ' ' .. line) | ||
| elseif tag == 'delete' then | ||
| table.insert(out, '-' .. line) | ||
| elseif tag == 'insert' then | ||
| table.insert(out, '+' .. line) | ||
| end | ||
| end | ||
|
|
||
| if #out == 0 then | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can it be? Why do we need to care, anyways? |
||
| return nil | ||
| end | ||
|
|
||
| return table.concat(out, '\n') | ||
| end | ||
|
|
||
| --- Build a simple line-by-line diff for expected and actual values | ||
| -- serialized to text. Returns nil when values can't be serialized | ||
| -- or there is no diff. | ||
| function M.build_line_diff(expected, actual) | ||
| local old = pp.LINE_LENGTH | ||
| pp.LINE_LENGTH = 0 | ||
| local expected_text = pp.tostring(expected) | ||
| local actual_text = pp.tostring(actual) | ||
| pp.LINE_LENGTH = old | ||
|
|
||
| if expected_text == nil or actual_text == nil then | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICS |
||
| return nil | ||
| end | ||
|
|
||
| if expected_text == actual_text then | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this check. |
||
| return nil | ||
| end | ||
|
|
||
| local diffs = diff_by_lines(expected_text, actual_text) | ||
| local patch_text = prettify_patch(diffs) | ||
|
|
||
| if patch_text == '' or patch_text == nil then | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this special case, either. |
||
| return nil | ||
| end | ||
|
|
||
| return patch_text | ||
| end | ||
|
|
||
| return M | ||
Uh oh!
There was an error while loading. Please reload this page.