Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Mar 17, 2024
1 parent 32221ba commit acd7dac
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
12 changes: 11 additions & 1 deletion Source/LuaBridge/detail/Invoke.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,17 @@ template <class Impl, class LuaRef>
template <class F, class... Args>
LuaResult LuaRefBase<Impl, LuaRef>::callWithHandler(F&& errorHandler, Args&&... args) const
{
return luabridge::callWithHandler(*this, std::forward<F>(errorHandler), std::forward<Args>(args)...);
if constexpr (! std::is_convertible_v<F, bool>)
{
return luabridge::callWithHandler(*this, std::forward<F>(errorHandler), std::forward<Args>(args)...);
}
else
{
if (errorHandler)
return luabridge::callWithHandler(*this, std::forward<F>(errorHandler), std::forward<Args>(args)...);
}

return luabridge::call(*this, std::forward<Args>(args)...);
}

} // namespace luabridge
64 changes: 63 additions & 1 deletion Tests/Source/LuaRefTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,18 +510,20 @@ TEST_F(LuaRefTests, Callable)
EXPECT_EQ(200, obj["i"].unsafe_cast<int>());
}

TEST_F(LuaRefTests, CallableWithHandler)
TEST_F(LuaRefTests, CallableWithAndWithoutHandler)
{
runLua("function f(x) error('we failed ' .. x) end");
auto f = luabridge::getGlobal(L, "f");
EXPECT_TRUE(f.isCallable());

// Call without
#if LUABRIDGE_HAS_EXCEPTIONS
EXPECT_ANY_THROW(f.call("badly"));
#else
EXPECT_FALSE(f.call("badly"));
#endif

// Call with
bool calledHandler = false;
std::string errorMessage;
auto handler = [&](lua_State*) -> int
Expand All @@ -540,6 +542,66 @@ TEST_F(LuaRefTests, CallableWithHandler)
EXPECT_TRUE(errorMessage.find("we failed badly") != std::string::npos);
}

TEST_F(LuaRefTests, CallableWithStdFunction)
{
runLua("function f(x) error('we failed ' .. x) end");
auto f = luabridge::getGlobal(L, "f");
EXPECT_TRUE(f.isCallable());

bool calledHandler = false;
std::string errorMessage;
auto handler = [&](lua_State*) -> int
{
calledHandler = true;

if (auto msg = lua_tostring(L, 1))
errorMessage = msg;

return 0;
};

std::function<int(lua_State*)> pHandler = handler;

EXPECT_FALSE(f.callWithHandler(pHandler, "badly"));
EXPECT_TRUE(calledHandler);
EXPECT_TRUE(errorMessage.find("we failed badly") != std::string::npos);
}

TEST_F(LuaRefTests, CallableWithNullifiedStdFunction)
{
runLua("function f(x) error('we failed ' .. x) end");
auto f = luabridge::getGlobal(L, "f");
EXPECT_TRUE(f.isCallable());

std::function<int(lua_State*)> pHandler = nullptr;
EXPECT_FALSE(f.callWithHandler(pHandler, "badly"));
}

TEST_F(LuaRefTests, CallableWithCFunction)
{
runLua("function f(x) error('we failed ' .. x) end");
auto f = luabridge::getGlobal(L, "f");
EXPECT_TRUE(f.isCallable());

lua_CFunction pHandler = +[](lua_State* L) { return 0; };
EXPECT_FALSE(f.callWithHandler(pHandler, "badly"));
}

TEST_F(LuaRefTests, CallableWithNullCFunction)
{
runLua("function f(x) error('we failed ' .. x) end");
auto f = luabridge::getGlobal(L, "f");
EXPECT_TRUE(f.isCallable());

lua_CFunction pHandler = nullptr;

#if LUABRIDGE_HAS_EXCEPTIONS
EXPECT_ANY_THROW(f.callWithHandler(pHandler, "badly"));
#else
EXPECT_FALSE(f.callWithHandler(pHandler, "badly"));
#endif
}

TEST_F(LuaRefTests, Pop)
{
lua_pushstring(L, "hello");
Expand Down

0 comments on commit acd7dac

Please sign in to comment.