?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2019, Dmitry Tarakanov
  3. // SPDX-License-Identifier: MIT
  4.  
  5. #include "TestBase.h"
  6.  
  7. #include "LuaBridge/Map.h"
  8.  
  9. #include <map>
  10.  
  11. struct MapTests : TestBase
  12. {
  13. };
  14.  
  15. TEST_F(MapTests, LuaRef)
  16. {
  17.     {
  18.         runLua("result = {[false] = true, a = 'abc', [1] = 5, [3.14] = -1.1}");
  19.  
  20.         using Map = std::map<luabridge::LuaRef, luabridge::LuaRef>;
  21.         Map expected{
  22.             {luabridge::LuaRef(L, false), luabridge::LuaRef(L, true)},
  23.             {luabridge::LuaRef(L, 'a'), luabridge::LuaRef(L, "abc")},
  24.             {luabridge::LuaRef(L, 1), luabridge::LuaRef(L, 5)},
  25.             {luabridge::LuaRef(L, 3.14), luabridge::LuaRef(L, -1.1)},
  26.         };
  27.         Map actual = result();
  28.         ASSERT_EQ(expected, actual);
  29.         ASSERT_EQ(expected, result<Map>());
  30.     }
  31.  
  32.     {
  33.         runLua("result = {'a', 'b', 'c'}");
  34.  
  35.         using Int2Char = std::map<int, char>;
  36.         Int2Char expected{{1, 'a'}, {2, 'b'}, {3, 'c'}};
  37.         Int2Char actual = result();
  38.         ASSERT_EQ(expected, actual);
  39.         ASSERT_EQ(expected, result<Int2Char>());
  40.     }
  41. }
  42.  
  43. TEST_F(MapTests, CastToMap)
  44. {
  45.     luabridge::LuaException::enableExceptions(L);
  46.  
  47.     using StrToInt = std::map<std::string, int>;
  48.     runLua("result = {[1] = 2, a = 3}");
  49.     ASSERT_EQ((StrToInt{{"1", 2}, {"a", 3}}), result().cast<StrToInt>());
  50.  
  51.     using IntToInt = std::map<int, int>;
  52.     runLua("result = {[1] = 2, a = 3}");
  53.     ASSERT_THROW((result().cast<IntToInt>()), std::exception);
  54. }
  55.  
  56. TEST_F(MapTests, PassToFunction)
  57. {
  58.     runLua("function foo (map) "
  59.            "  result = map "
  60.            "end");
  61.  
  62.     auto foo = luabridge::getGlobal(L, "foo");
  63.     using Int2Bool = std::map<int, bool>;
  64.  
  65.     resetResult();
  66.  
  67.     Int2Bool lvalue{{10, false}, {20, true}, {30, true}};
  68.     foo(lvalue);
  69.     ASSERT_TRUE(result().isTable());
  70.     ASSERT_EQ(lvalue, result<Int2Bool>());
  71.  
  72.     resetResult();
  73.  
  74.     const Int2Bool constLvalue = lvalue;
  75.     foo(constLvalue);
  76.     ASSERT_TRUE(result().isTable());
  77.     ASSERT_EQ(constLvalue, result<Int2Bool>());
  78. }
  79.  
  80. namespace {
  81.  
  82. struct Data
  83. {
  84.     /* explicit */ Data(int i) : i(i) {}
  85.  
  86.     int i;
  87. };
  88.  
  89. bool operator==(const Data& lhs, const Data& rhs)
  90. {
  91.     return lhs.i == rhs.i;
  92. }
  93.  
  94. bool operator<(const Data& lhs, const Data& rhs)
  95. {
  96.     return lhs.i < rhs.i;
  97. }
  98.  
  99. std::ostream& operator<<(std::ostream& lhs, const Data& rhs)
  100. {
  101.     lhs << "{" << rhs.i << "}";
  102.     return lhs;
  103. }
  104.  
  105. std::map<Data, Data> processValues(const std::map<Data, Data>& data)
  106. {
  107.     return data;
  108. }
  109.  
  110. std::map<Data, Data> processPointers(const std::map<Data, const Data*>& data)
  111. {
  112.     std::map<Data, Data> result;
  113.     for (const auto& item : data)
  114.     {
  115.         result.emplace(item.first, *item.second);
  116.     }
  117.     return result;
  118. }
  119.  
  120. } // namespace
  121.  
  122. TEST_F(MapTests, PassFromLua)
  123. {
  124.     luabridge::getGlobalNamespace(L)
  125.         .beginClass<Data>("Data")
  126.         .addConstructor<void (*)(int)>()
  127.         .endClass()
  128.         .addFunction("processValues", &processValues)
  129.         .addFunction("processPointers", &processPointers);
  130.  
  131.     {
  132.         resetResult();
  133.         runLua("result = processValues ({[Data (-1)] = Data (2)})");
  134.         std::map<Data, Data> expected{{Data(-1), Data(2)}};
  135.         const auto actual = result<std::map<Data, Data>>();
  136.         ASSERT_EQ(expected, actual);
  137.     }
  138.  
  139.     {
  140.         resetResult();
  141.         runLua("result = processPointers ({[Data (3)] = Data (-4)})");
  142.         std::map<Data, Data> expected{{Data(3), Data(-4)}};
  143.         const auto actual = result<std::map<Data, Data>>();
  144.         ASSERT_EQ(expected, actual);
  145.     }
  146. }
  147.