Skip to content

Print a diff for compared data #412

@ligurio

Description

@ligurio

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

Metadata

Metadata

Assignees

Labels

featureA new functionality

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions