Skip to content

Commit 1d91b35

Browse files
committed
Add unified diff support for luatest.assert_equals
This patch adds unified diff output for `t.assert_equals()` failures when the test suite is run with the `--diff` option, using a vendored Lua implementation of google/diff-match-patch (`luatest/vendor/diff_match_patch.lua` taken from [^1]). Closes #412 [^1]: https://github.com/google/diff-match-patch/blob/master/lua/diff_match_patch.lua
1 parent 731ba6f commit 1d91b35

File tree

6 files changed

+2802
-1
lines changed

6 files changed

+2802
-1
lines changed

.luacheckrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
include_files = {"**/*.lua", "*.rockspec", "*.luacheckrc"}
2-
exclude_files = {"build.luarocks/", "lua_modules/", "tmp/", ".luarocks/", ".rocks/"}
2+
exclude_files = {"build.luarocks/", "lua_modules/", "tmp/", ".luarocks/", ".rocks/", "luatest/vendor/"}
33

44
max_line_length = 120

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Added support for unified diff output in `t.assert_equals()` failure messages
6+
when expected and actual values are YAML-serializable (gh-412).
7+
38
## 1.3.0
49

510
- Fixed a bug when `assert_covers` treats arrays as maps (gh-405).

luatest/assertions.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
local math = require('math')
77

88
local comparator = require('luatest.comparator')
9+
local diff = require('luatest.diff')
910
local mismatch_formatter = require('luatest.mismatch_formatter')
1011
local pp = require('luatest.pp')
1112
local log = require('luatest.log')
@@ -20,12 +21,17 @@ local prettystr_pairs = pp.tostring_pair
2021
local M = {}
2122

2223
local xfail = false
24+
local diff_enabled = true
2325

2426
local box_error_type = ffi.typeof(box.error.new(box.error.UNKNOWN))
2527

2628
-- private exported functions (for testing)
2729
M.private = {}
2830

31+
function M.private.set_diff_enabled(value)
32+
diff_enabled = value and true or false
33+
end
34+
2935
function M.private.is_xfail()
3036
local xfail_status = xfail
3137
xfail = false
@@ -83,6 +89,14 @@ local function error_msg_equality(actual, expected, deep_analysis)
8389
if success then
8490
result = table.concat({result, mismatchResult}, '\n')
8591
end
92+
93+
if diff_enabled then
94+
local diff_result = diff.build_unified_diff(expected, actual)
95+
if diff_result then
96+
result = table.concat({result, 'diff:', diff_result}, '\n')
97+
end
98+
end
99+
86100
return result
87101
end
88102
return string.format("expected: %s, actual: %s",

0 commit comments

Comments
 (0)