// https://github.com/kunitoki/LuaBridge3 // Copyright 2023, Lucio Asnaghi // SPDX-License-Identifier: MIT #include "TestBase.h" #include "LuaBridge/Set.h" #include namespace { struct Unregistered { bool operator<(const Unregistered& other) const { return true; } }; struct Data { /* explicit */ Data(int i) : i(i) {} int i; }; bool operator==(const Data& lhs, const Data& rhs) { return lhs.i == rhs.i; } bool operator<(const Data& lhs, const Data& rhs) { return lhs.i < rhs.i; } std::ostream& operator<<(std::ostream& lhs, const Data& rhs) { lhs << "{" << rhs.i << "}"; return lhs; } std::set processValues(const std::set& data) { return data; } std::set processPointers(const std::set& data) { std::set result; for (const auto& item : data) result.emplace(*item); return result; } } // namespace namespace std { template <> struct hash { std::size_t operator()(const Unregistered& value) const { return 0; } }; } // namespace std struct SetTests : TestBase { }; TEST_F(SetTests, LuaRef) { { using Set = std::set; const Set expected { 1, 2, 3 }; runLua("result = {1, 2, 3}"); Set actual = result(); EXPECT_EQ(expected, actual); EXPECT_EQ(expected, result()); } { using Set = std::set; const Set expected { {"abcdef"}, {"bcdef"}, {"cdef"} }; runLua("result = {'abcdef', 'bcdef', 'cdef'}"); Set actual = result(); EXPECT_EQ(expected, actual); EXPECT_EQ(expected, result()); } { using Set = std::set; const Set expected { luabridge::LuaRef(L, 'a'), luabridge::LuaRef(L, 1), luabridge::LuaRef(L, 3.14), luabridge::LuaRef(L, "abc"), luabridge::LuaRef(L, 5), luabridge::LuaRef(L, -1.1), }; runLua("result = {'a', 1, 3.14, 'abc', 5, -1.1}"); auto resultRef = result(); EXPECT_TRUE(resultRef.isInstance()); Set actual = resultRef; EXPECT_EQ(expected, actual); EXPECT_EQ(expected, result()); } } TEST_F(SetTests, CastToSet) { using StringSet = std::set; runLua("result = {'1', 'a'}"); ASSERT_EQ((StringSet{"1", "a"}), result()); using IntSet = std::set; runLua("result = {2, 'a'}"); #if LUABRIDGE_HAS_EXCEPTIONS ASSERT_ANY_THROW((result())); #else ASSERT_DEATH((result()), ""); #endif } TEST_F(SetTests, PassToFunction) { runLua("function foo (set) " " result = set " "end"); auto foo = luabridge::getGlobal(L, "foo"); using IntSet = std::set; resetResult(); IntSet lvalue{ 10, 20, 30 }; foo(lvalue); ASSERT_TRUE(result().isTable()); ASSERT_EQ(lvalue, result()); resetResult(); const IntSet constLvalue = lvalue; foo(constLvalue); ASSERT_TRUE(result().isTable()); ASSERT_EQ(constLvalue, result()); } TEST_F(SetTests, PassFromLua) { luabridge::getGlobalNamespace(L) .beginClass("Data") .addConstructor() .endClass() .addFunction("processValues", &processValues) .addFunction("processPointers", &processPointers); { resetResult(); runLua("result = processValues ({ Data(-1), Data(2) })"); std::set expected{ Data(-1), Data(2) }; const auto actual = result>(); ASSERT_EQ(expected, actual); } { resetResult(); runLua("result = processPointers ({ Data(3), Data(-4) })"); std::set expected{ Data(3), Data(-4) }; const auto actual = result>(); ASSERT_EQ(expected, actual); } } TEST_F(SetTests, UnregisteredClass) { { #if LUABRIDGE_HAS_EXCEPTIONS [[maybe_unused]] luabridge::Result r; ASSERT_THROW((r = luabridge::push(L, std::set{ Unregistered() })), std::exception); #else ASSERT_FALSE((luabridge::push(L, std::set{ Unregistered() }))); #endif } } TEST_F(SetTests, IsInstance) { ASSERT_TRUE((luabridge::push(L, std::set{ "x", "y", "z" }))); EXPECT_TRUE((luabridge::isInstance>(L, -1))); lua_pop(L, 1); ASSERT_TRUE((luabridge::push(L, 1))); EXPECT_FALSE((luabridge::isInstance>(L, -1))); } TEST_F(SetTests, StackOverflow) { exhaustStackSpace(); std::set value{ "x", "y", "z" }; ASSERT_FALSE(luabridge::push(L, value)); } #if !LUABRIDGE_HAS_EXCEPTIONS TEST_F(SetTests, PushUnregisteredWithNoExceptionsShouldFailButRestoreStack) { { const int initialStackSize = lua_gettop(L); lua_pushnumber(L, 1); EXPECT_EQ(1, lua_gettop(L) - initialStackSize); std::set v; v.emplace(Unregistered{}); v.emplace(Unregistered{}); auto result = luabridge::Stack::push(L, v); EXPECT_FALSE(result); EXPECT_EQ(1, lua_gettop(L) - initialStackSize); lua_pop(L, 1); EXPECT_EQ(0, lua_gettop(L) - initialStackSize); } } #endif