Skip to content
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

Print a diff for compared data #412

Open
ligurio opened this issue Jan 16, 2025 · 0 comments
Open

Print a diff for compared data #412

ligurio opened this issue Jan 16, 2025 · 0 comments

Comments

@ligurio
Copy link
Member

ligurio commented Jan 16, 2025

luatest has an is_equal/is_not_equal assertions.
Currently, output for tables/multiline strings/msgpack buffers looks as the following:

code
local t = require('luatest')

local g = t.group()

g.test_table_assertion = function()
    local a = {
      a = {
        ["a"] = 1,
        ["b"] = 2,
        ["c"] = 3,
      },
    }

    local b = {
      a = {
        ["a"] = 1,
        ["b"] = 5,
        ["c"] = 8,
      },
    }
    t.assert_equals(a, b, 'compare tables')
end

g.test_string_assertion = function()
    local a = [[
aaaaa
bbbbb
ccccc
]]
    local b = [[
aaaaa
ddddd
ccccc
]]

    t.assert_equals(a, b, 'compare multiline strings')
end

g.test_msgpack_assertion = function()
    local msgpack = require('msgpack')
    local a = {1, 2, 3}
    local b = {1, 4, 3}

    t.assert_equals(msgpack.encode(a), msgpack.encode(b), 'compare encoded msgpack')
end

Tables:

1) app-luatest.sample.test_table_assertion         
...b/sources/MRG/tarantool/test/app-luatest/sample_test.lua:21: compare tables
expected: {a = {a = 1, b = 5, c = 8}}
actual: {a = {a = 1, b = 2, c = 3}}

Multiline strings:

2) app-luatest.sample.test_string_assertion
...b/sources/MRG/tarantool/test/app-luatest/sample_test.lua:36: compare multiline strings
expected: 
"aaaaa\
ddddd\
ccccc\
"
actual: 
"aaaaa\
bbbbb\
ccccc\
"

msgpack:

3) app-luatest.sample.test_msgpack_assertion
...b/sources/MRG/tarantool/test/app-luatest/sample_test.lua:43: compare encoded msgpack
expected: "\1\4\3"
actual: "\1\2\3"

This output is not convenient for debugging.

I propose to print a diff for this data in case of triggered assertion. This would be much more convenient to get a difference in comparison:

String:

@@ -1,3 +1,3 @@
 aaaaa
-ddddd
+bbbbb
 ccccc

Tables:

@@ -1,6 +1,6 @@
 ---
 - a:
-    b: 2
+    b: 5
     a: 1
-    c: 3
+    c: 8
 ...

How it can be achieved? I propose to use a diff-match Lua module 1 for comparing strings, Tables should be serialized to YAML before comparing. Msgpack buffers should be decoded and serialized to YAML if it is a table.

See also:

Footnotes

  1. https://github.com/google/diff-match-patch/blob/master/lua/diff_match_patch.lua

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant