u-test icon indicating copy to clipboard operation
u-test copied to clipboard

FEATURE: Assert equal for tables

Open averms opened this issue 6 years ago • 4 comments

An assertion for tables the does a deep comparison.

Also, if the error messages could serialize tables that would be nice too. Right now it just shows table: 0x7fac41603780 ~= table: 0x7fac41700130. The inspect library is good for this.

averms avatar Mar 09 '19 08:03 averms

I think it's better to integrate the assert lib: https://github.com/Olivine-Labs/luassert

it would solve more problems with asserts in u-test

mihacooper avatar Mar 12 '19 11:03 mihacooper

I like this feature and actually implemented something similar in my own version of u-test, but on the other hand I like the simplicity of this library.

How about allowing library users to override how assertion parameters are displayed? Then it would be easy to use inspect or any other custom display code while keeping u-test simple and sane.

firas-assaad avatar Mar 12 '19 17:03 firas-assaad

You can use recently added "Custom assertions" and craft something like.

local function tables_equal(tbl1, tbl2)
    if not (inspect(tbl1) == inspect(tbl2)) then
        local failure_msg = inspect(tbl1) .. " ~= " .. inspect(tbl2)
        return false, msg
    end
end

test.register_assert("tables_equal", tables_equal)

P.S. I have found curious bug. Implementations of equal is a little bit broken.

api.equal = function (l, r)
    if l ~= r then
        fail(tostring(l) .. " ~= " .. tostring(r))
    end
end

Implementation assumes l ~= r same as not (l == r). In case of meta methods this assumption is wrong. Same with not_equal. I will fix it ASAP.

IUdalov avatar Mar 12 '19 19:03 IUdalov

As far as I remeber lua does not have metamethod for 'not equal' operation. So '~=' becomes 'not __eq' as you expected.

mihacooper avatar Mar 13 '19 02:03 mihacooper