lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Python's functions can be introspected and you can extract such things like number of arguments to call the function, consts inside the function and variable names and so on. Python uses function signatures and it's a runtime exception if you call function defined to use 3 arguments with only 2 arguments (Python semantics is quite strict comparing to Lua semantics). In Lua on the other hand it is ok to call any funciton with any number of arguments so given a Lua function you have no way to introspect with how much arguments it was defined. So, i don't think you're technically able to automatically assert equality number of arguments passed and number of arguments in function definition without rude hacks (though it probably could be done with some code parsers but it's out of scope of your question actually).




On Mon, Aug 11, 2014 at 11:39 AM, albert_200200 <albert_200200@126.com> wrote:
On 2014/8/8 11:24, albert_200200 wrote:
In python's Mock module, there's autospeccing thing, you can let the mock object
behave exactly like the object you mocked, like you should have the same args signature.
I don't know if there's the same thing in lemock.
Now take such situation.


public.lua
---------------------------------------------------
local M = {}
function M.add_internal(a, b)
    return a+b
    --add_internal may call some global function, so I should mock add_internal
end
function M.add(a, b)
    return M.add_internal(a, b)
end
return M

Then I need to test public.add function, because add_internal may could call some global function,
I'd like to mock add_internal function.


test.lua
---------------------------------------------------
require("lunit")
lunit.setprivfenv()
lunit.import "assertions"
lunit.import "checks"

require("lemock")


local ut = lunit.TestCase("public.add function test")
function ut:test()
    local public   = require("public")
    mc = lemock.controller()
    mock_add_internal    = mc:mock()
    package.loaded.public.add_internal = mock_add_internal
    mock_add_internal(2, 3); mc:returns(5)
    mc:replay()
    local result = public.add(2, 3)
    assert_equal(5, result)
    
    mc:verify()
end
lunit.run()


Now take such situation, I modify public.add_internal'code as below:
public.lua
---------------------------------------------------
local M = {}
function M.add_internal(a, b, c)
    return a+b+c
    --add_internal may call some global function, so I should mock add_internal
end

function M.add(a, b)
    return M.add_internal(a, b)
end
return M

But I leave the test.lua unmodified, the unittest would pass too.
Is there a way to solve this problem?
Could someone know how to solve this problem? It really bothers me for a while.