?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. struct IssueTests : TestBase
  8. {
  9. };
  10.  
  11. struct AbstractClass
  12. {
  13.     virtual int sum(int a, int b) = 0;
  14. };
  15.  
  16. struct ConcreteClass : AbstractClass
  17. {
  18.     int sum(int a, int b) override { return a + b; }
  19.  
  20.     static AbstractClass& get()
  21.     {
  22.         static ConcreteClass instance;
  23.         return instance;
  24.     }
  25. };
  26.  
  27. TEST_F(IssueTests, Issue87)
  28. {
  29.     luabridge::getGlobalNamespace(L)
  30.         .beginClass<AbstractClass>("Class")
  31.         .addFunction("sum", &AbstractClass::sum)
  32.         .endClass()
  33.         .addFunction("getAbstractClass", &ConcreteClass::get);
  34.  
  35.     runLua("result = getAbstractClass ():sum (1, 2)");
  36.     ASSERT_TRUE(result().isNumber());
  37.     ASSERT_EQ(3, result<int>());
  38. }
  39.  
  40. TEST_F(IssueTests, Issue121)
  41. {
  42.     runLua(R"(
  43.     first = {
  44.       second = {
  45.         actual = "data"
  46.       }
  47.     }
  48.   )");
  49.     auto first = luabridge::getGlobal(L, "first");
  50.     ASSERT_TRUE(first.isTable());
  51.     ASSERT_EQ(0, first.length());
  52.     ASSERT_TRUE(first["second"].isTable());
  53.     ASSERT_EQ(0, first["second"].length());
  54. }
  55.  
  56. void pushArgs(lua_State*)
  57. {
  58. }
  59.  
  60. template<class Arg, class... Args>
  61. void pushArgs(lua_State* L, Arg arg, Args... args)
  62. {
  63.     luabridge::Stack<Arg>::push(L, arg);
  64.     pushArgs(L, args...);
  65. }
  66.  
  67. template<class... Args>
  68. std::vector<luabridge::LuaRef> callFunction(const luabridge::LuaRef& function, Args... args)
  69. {
  70.     assert(function.isFunction());
  71.  
  72.     lua_State* L = function.state();
  73.     int originalTop = lua_gettop(L);
  74.     function.push(L);
  75.     pushArgs(L, args...);
  76.  
  77.     luabridge::LuaException::pcall(L, sizeof...(args), LUA_MULTRET);
  78.  
  79.     std::vector<luabridge::LuaRef> results;
  80.     int top = lua_gettop(L);
  81.     results.reserve(top - originalTop);
  82.     for (int i = originalTop + 1; i <= top; ++i)
  83.     {
  84.         results.push_back(luabridge::LuaRef::fromStack(L, i));
  85.     }
  86.     return results;
  87. }
  88.  
  89. TEST_F(IssueTests, Issue160)
  90. {
  91.     runLua("function isConnected (arg1, arg2) "
  92.            " return 1, 'srvname', 'ip:10.0.0.1', arg1, arg2 "
  93.            "end");
  94.  
  95.     luabridge::LuaRef f_isConnected = luabridge::getGlobal(L, "isConnected");
  96.  
  97.     auto v = callFunction(f_isConnected, 2, "abc");
  98.     ASSERT_EQ(5u, v.size());
  99.     ASSERT_EQ(1, v[0].cast<int>());
  100.     ASSERT_EQ("srvname", v[1].cast<std::string>());
  101.     ASSERT_EQ("ip:10.0.0.1", v[2].cast<std::string>());
  102.     ASSERT_EQ(2, v[3].cast<int>());
  103.     ASSERT_EQ("abc", v[4].cast<std::string>());
  104. }
  105.  
  106. struct Vector
  107. {
  108.     float getX() const { return x; }
  109.  
  110.     float x = 0;
  111. };
  112.  
  113. struct WideVector : Vector
  114. {
  115.     WideVector(float, float, float, float w) { x = w; }
  116. };
  117.  
  118. TEST_F(IssueTests, Issue178)
  119. {
  120.     luabridge::getGlobalNamespace(L)
  121.         .beginClass<Vector>("Vector")
  122.         .addFunction("getX", &Vector::getX)
  123.         .addProperty("X", &Vector::getX)
  124.         .addData("x", &Vector::x, true)
  125.         .endClass()
  126.         .deriveClass<WideVector, Vector>("WideVector")
  127.         .addConstructor<void (*)(float, float, float, float)>()
  128.         .endClass();
  129.  
  130.     runLua("result = WideVector (0, 1, 2, 3).x");
  131.  
  132.     ASSERT_TRUE(result().isNumber());
  133.     ASSERT_EQ(3.f, result<float>());
  134. }
  135.  
  136. enum class MyEnum
  137. {
  138.     VALUE0,
  139.     VALUE1,
  140. };
  141.  
  142. template<typename T>
  143. struct EnumWrapper
  144. {
  145.     static typename std::enable_if<std::is_enum<T>::value, void>::type push(lua_State* L, T value)
  146.     {
  147.         lua_pushnumber(L, static_cast<std::size_t>(value));
  148.     }
  149.  
  150.     static typename std::enable_if<std::is_enum<T>::value, T>::type get(lua_State* L, int index)
  151.     {
  152.         return static_cast<T>(lua_tointeger(L, index));
  153.     }
  154. };
  155.  
  156. namespace luabridge {
  157.  
  158. template<>
  159. struct Stack<MyEnum> : EnumWrapper<MyEnum>
  160. {
  161. };
  162.  
  163. } // namespace luabridge
  164.  
  165. TEST_F(IssueTests, Issue127)
  166. {
  167.     runLua("result = 1");
  168.     ASSERT_EQ(MyEnum::VALUE1, result<MyEnum>());
  169. }
  170.