// https://github.com/vinniefalco/LuaBridge // // Copyright 2019, Dmitry Tarakanov // SPDX-License-Identifier: MIT #include "TestBase.h" #include "LuaBridge/Map.h" #include struct MapTests : TestBase { }; TEST_F (MapTests, LuaRef) { { runLua ("result = {[false] = true, a = 'abc', [1] = 5, [3.14] = -1.1}"); using Map = std::map ; Map expected { {luabridge::LuaRef (L, false), luabridge::LuaRef (L, true)}, {luabridge::LuaRef (L, 'a'), luabridge::LuaRef (L, "abc")}, {luabridge::LuaRef (L, 1), luabridge::LuaRef (L, 5)}, {luabridge::LuaRef (L, 3.14), luabridge::LuaRef (L, -1.1)}, }; Map actual = result (); ASSERT_EQ (expected, actual); ASSERT_EQ (expected, result ()); } { runLua ("result = {'a', 'b', 'c'}"); using Int2Char = std::map ; Int2Char expected {{1, 'a'}, {2, 'b'}, {3, 'c'}}; Int2Char actual = result (); ASSERT_EQ (expected, actual); ASSERT_EQ (expected, result ()); } } TEST_F (MapTests, PassToFunction) { runLua ( "function foo (map) " " result = map " "end"); auto foo = luabridge::getGlobal (L, "foo"); using Int2Bool = std::map ; resetResult (); Int2Bool lvalue {{10, false}, {20, true}, {30, true}}; foo (lvalue); ASSERT_TRUE (result ().isTable ()); ASSERT_EQ (lvalue, result ()); resetResult (); const Int2Bool constLvalue = lvalue; foo (constLvalue); ASSERT_TRUE (result ().isTable ()); ASSERT_EQ (constLvalue, result ()); } namespace { 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::map processValues (const std::map & data) { return data; } std::map processPointers (const std::map & data) { std::map result; for (const auto& item : data) { result.emplace (item.first, *item.second); } return result; } } // namespace TEST_F (MapTests, PassFromLua) { luabridge::getGlobalNamespace (L) .beginClass ("Data") .addConstructor () .endClass () .addFunction ("processValues", &processValues) .addFunction ("processPointers", &processPointers); { resetResult (); runLua ("result = processValues ({[Data (-1)] = Data (2)})"); std::map expected {{Data (-1), Data (2)}}; const auto actual = result > (); ASSERT_EQ (expected, actual); } { resetResult (); runLua ("result = processValues ({[Data (3)] = Data (-4)})"); std::map expected {{Data (3), Data (-4)}}; const auto actual = result > (); ASSERT_EQ (expected, actual); } }