luassert icon indicating copy to clipboard operation
luassert copied to clipboard

is_calleable assertion

Open devurandom opened this issue 6 years ago • 10 comments

It would be useful to have an is_calleable assertion that tests whether is_function(argument) or is_function(debug.getmetatable(argument)._call).

devurandom avatar Jan 06 '19 20:01 devurandom

this should be easy to add, see http://stevedonovan.github.io/Penlight/api/libraries/pl.types.html#is_callable

care to create a PR @devurandom ?

Tieske avatar Jan 08 '19 08:01 Tieske

You mean to copy the code from Penlight?

--- is the object either a function or a callable object?.
-- @param obj Object to check.
function types.is_callable (obj)
    return type(obj) == 'function' or getmetatable(obj) and getmetatable(obj).__call and true
end

Sure, I can do that.

devurandom avatar Jan 08 '19 08:01 devurandom

No, busted already relies on Penlight, so all you have to do is add an assertion that uses that function.

Tieske avatar Jan 08 '19 15:01 Tieske

But the assertions reside in luassert/src/assertions.lua, i.e. in luassert, which currently does not depend on Penlight.

devurandom avatar Jan 08 '19 17:01 devurandom

@devurandom you are totally right, sorry for the noise.

Tieske avatar Jan 08 '19 18:01 Tieske

Feel free to make a PR and reference this issue

DorianGray avatar Jul 22 '19 22:07 DorianGray

luassert has its own implementation btw; in luassert.util, see https://github.com/Olivine-Labs/luassert/blob/master/src/util.lua#L274

Tieske avatar Sep 13 '20 07:09 Tieske

luassert has its own implementation btw; in luassert.util, see https://github.com/Olivine-Labs/luassert/blob/master/src/util.lua#L274

Technically, that impl is incorrect. See here: https://github.com/neovim/neovim/pull/29536

The Penlight impl is also incorrect.

tmillr avatar Sep 15 '24 02:09 tmillr

wouldn't an object with metatable that has another metatable with a __call method be callable? iirc those lookups are chained.

Tieske avatar Sep 17 '24 09:09 Tieske

wouldn't an object with metatable that has another metatable with a __call method be callable? iirc those lookups are chained.

Not for Lua 5.1 and Luajit. Maybe this has changed since 5.1 though, not sure.

You can see the tests here (neovim is pinned to 5.1): https://github.com/neovim/neovim/pull/29536/files

https://github.com/neovim/neovim/pull/27457#issuecomment-1940787881

tmillr avatar Sep 18 '24 01:09 tmillr