lunarmodules / luassert

Assertion library for Lua
MIT License
202 stars 76 forks source link

Feature request: Print passed in and expected arguments for assert.spy #155

Closed jbbjarnason closed 3 years ago

jbbjarnason commented 5 years ago

When using assert spy to check function call with defined arguments, a wrong argument is not displayed. Test example:

    it("#42 lua assert spy test", function()
        local someTableWithFunc = {foo = function(myArg)end}
        spy.on(someTableWithFunc, "foo")
        someTableWithFunc.foo(2)
        assert.spy(someTableWithFunc.foo).was_called_with(1)
    end)

The following error is displayed:

$ busted -t 42
0 successes / 1 failure / 0 errors / 0 pending : 0.077245 seconds
Failure → spec/takeawayDischarger_spec.lua @ 390
Takeaway discharger construction Unit test #42 lua assert spy test
spec/takeawayDischarger_spec.lua:394: Function was not called with the arguments
Tieske commented 5 years ago

see also: https://github.com/Olivine-Labs/busted/issues/578

jbbjarnason commented 5 years ago

see also: Olivine-Labs/busted#578

According to the busted issue, this could be solved somewhere here: https://github.com/Olivine-Labs/luassert/blob/b8fb59ac1eb1a2d59793ee1707ac42f979438e8f/src/spy.lua#L54

qha commented 5 years ago

I've made an attempt to address this issue over in qha/luassert format-called-returned-with-args.

The formatting of passed in and expected args/return values is absolutely abysmal though (see the output from the four new failing tests in spec/spies_spec.lua).

The first reason is that the top level list is rendered as a table which is not particularly helpful. I presume this should be addressed by adding a formatter but i don't know how it should be kept from firing on too many tables (any table with integer keys and "n" seems way to wide).

The second reason is that matchers are rendered as tables. This too should probably be addressed with a formatter but again i need some tips. Could one compare with namespace.matcher[name] to identify appropriate targets? Is there any way to get to the matcher arguments?

For both of these formatters it seems they may not be desirable at all times, would it make sense to try to register them only during arguments/return value formatting?

qha commented 5 years ago

Is there any way to get to the matcher arguments?

Short of changing the matcher interface to return something like:

setmetatable({ arguments = arguments }, { __call = function(_, value) ... end, })
qha commented 4 years ago

I think i figured out the answers to my questions above, please see: #165

Tieske commented 3 years ago

pr was merged