KAGUYA_TEST_FUNCTION_DEF(compare_null_ptr)(kaguya::State& state) { { kaguya::LuaRef nullref = state.newRef(nullptr); TEST_CHECK(nullref.typeTest<std::nullptr_t>()); TEST_CHECK(nullref.weakTypeTest<std::nullptr_t>()); TEST_CHECK(nullref == nullptr); void* ptr = nullref.get<std::nullptr_t>(); TEST_CHECK(!ptr); } { kaguya::LuaRef ref = state.newRef(1); TEST_CHECK(!(ref.typeTest<std::nullptr_t>())); TEST_CHECK(!(ref.weakTypeTest<std::nullptr_t>())); TEST_CHECK(ref != nullptr); bool catch_except = false; try { ref.get<std::nullptr_t>(); } catch (const kaguya::LuaTypeMismatch&) { catch_except = true; } TEST_CHECK(catch_except); } }
KAGUYA_TEST_FUNCTION_DEF(native_function_call_test)(kaguya::State& state) { using namespace kaguya::nativefunction; Foo foo; state.newRef(6).push(); state.newRef(9).push(); state.newRef(2).push(); call(state.state(), &free_standing_function); lua_settop(state.state(), 0); state.newRef(&foo).push(); state.newRef("Bar").push(); call(state.state(), &Foo::setBar); #if KAGUYA_USE_CPP11 state.newRef(&foo).push(); state.newRef(9).push(); call(state.state(), [](Foo* foo, int b) { foo->setBar("fromlambda"); }); TEST_EQUAL(foo.bar, "fromlambda"); std::string capture("capture"); call(state.state(), [=](Foo* foo, int b) { foo->setBar(capture + "lambda"); }); TEST_EQUAL(foo.bar, "capturelambda"); #endif lua_settop(state.state(), 0); }
KAGUYA_TEST_FUNCTION_DEF(function_call_error)(kaguya::State& state) { error_count = 0; state.setErrorHandler(error_fun); state["errofun"] = kaguya::function(error_fun); state["errofun"](33); TEST_CHECK(error_count == 1); kaguya::LuaRef f; f.resume<void>(); f.call<void>(); TEST_COMPARE_EQ(f.threadStatus(), LUA_ERRRUN); TEST_COMPARE_EQ(state.newRef(1).threadStatus(), LUA_ERRRUN); }
KAGUYA_TEST_FUNCTION_DEF(movable_class_test)(kaguya::State& state) { state["MoveOnlyClass"].setClass(kaguya::UserdataMetatable<MoveOnlyClass>() .setConstructors<MoveOnlyClass(int)>() .addProperty("member", &MoveOnlyClass::member) ); state["moveonly"] = MoveOnlyClass(2); const MoveOnlyClass* ref = state["moveonly"]; TEST_CHECK(ref); TEST_CHECK(ref->member == 2); state("func =function(arg) return assert(arg.member == 5) end"); state["func"](MoveOnlyClass(5)); state.newRef(MoveOnlyClass(5)); }