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

FEATURE: Assert equal for tables #7

Open
averms opened this issue Mar 9, 2019 · 4 comments
Open

FEATURE: Assert equal for tables #7

averms opened this issue Mar 9, 2019 · 4 comments
Assignees

Comments

@averms
Copy link

averms commented Mar 9, 2019

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.

@mihacooper
Copy link
Contributor

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

@firas-assaad
Copy link
Contributor

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.

@IUdalov
Copy link
Owner

IUdalov commented Mar 12, 2019

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 IUdalov self-assigned this Mar 12, 2019
@mihacooper
Copy link
Contributor

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

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

4 participants