?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2019, Dmitry Tarakanov
  3. // Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
  4. // Copyright 2007, Nathan Reed
  5. // SPDX-License-Identifier: MIT
  6.  
  7. // A set of tests of different types' communication with Lua
  8.  
  9. #include "TestBase.h"
  10.  
  11. #include <gtest/gtest.h>
  12.  
  13. #include <cstring>
  14. #include <iostream>
  15. #include <memory>
  16. #include <string>
  17.  
  18. void printValue(lua_State* L, int index)
  19. {
  20.     int type = lua_type(L, index);
  21.     switch (type)
  22.     {
  23.     case LUA_TBOOLEAN:
  24.         std::cerr << std::boolalpha << (lua_toboolean(L, index) != 0);
  25.         break;
  26.     case LUA_TSTRING:
  27.         std::cerr << lua_tostring(L, index);
  28.         break;
  29.     case LUA_TNUMBER:
  30.         std::cerr << lua_tonumber(L, index);
  31.         break;
  32.     case LUA_TTABLE:
  33.     case LUA_TTHREAD:
  34.     case LUA_TFUNCTION:
  35.         std::cerr << lua_topointer(L, index);
  36.         break;
  37.     }
  38.     std::cerr << ": " << lua_typename(L, type) << " (" << type << ")" << std::endl;
  39. }
  40.  
  41. struct LuaBridgeTest : TestBase
  42. {
  43. };
  44.  
  45. template<class T>
  46. T identityCFunction(T value)
  47. {
  48.     return value;
  49. }
  50.  
  51. TEST_F(LuaBridgeTest, CFunction)
  52. {
  53.     luabridge::getGlobalNamespace(L)
  54.         .addFunction("boolFn", &identityCFunction<bool>)
  55.         .addFunction("ucharFn", &identityCFunction<unsigned char>)
  56.         .addFunction("shortFn", &identityCFunction<short>)
  57.         .addFunction("ushortFn", &identityCFunction<unsigned short>)
  58.         .addFunction("intFn", &identityCFunction<int>)
  59.         .addFunction("uintFn", &identityCFunction<unsigned int>)
  60.         .addFunction("longFn", &identityCFunction<long>)
  61.         .addFunction("ulongFn", &identityCFunction<unsigned long>)
  62.         .addFunction("longlongFn", &identityCFunction<long long>)
  63.         .addFunction("ulonglongFn", &identityCFunction<unsigned long long>)
  64.         .addFunction("floatFn", &identityCFunction<float>)
  65.         .addFunction("doubleFn", &identityCFunction<double>)
  66.         .addFunction("charFn", &identityCFunction<char>)
  67.         .addFunction("cstringFn", &identityCFunction<const char*>)
  68.         .addFunction("stringFn", &identityCFunction<std::string>);
  69.  
  70.     {
  71.         runLua("result = ucharFn (255)");
  72.         ASSERT_EQ(true, result().isNumber());
  73.         ASSERT_EQ(255u, result<unsigned char>());
  74.     }
  75.  
  76.     {
  77.         runLua("result = boolFn (false)");
  78.         ASSERT_EQ(true, result().isBool());
  79.         ASSERT_EQ(false, result<bool>());
  80.     }
  81.     {
  82.         runLua("result = boolFn (true)");
  83.         ASSERT_EQ(true, result().isBool());
  84.         ASSERT_EQ(true, result<bool>());
  85.     }
  86.  
  87.     {
  88.         runLua("result = shortFn (-32768)");
  89.         ASSERT_EQ(true, result().isNumber());
  90.         ASSERT_EQ(-32768, result<int>());
  91.     }
  92.  
  93.     {
  94.         runLua("result = ushortFn (32767)");
  95.         ASSERT_EQ(true, result().isNumber());
  96.         ASSERT_EQ(32767u, result<unsigned int>());
  97.     }
  98.     {
  99.         runLua("result = intFn (-500)");
  100.         ASSERT_EQ(true, result().isNumber());
  101.         ASSERT_EQ(-500, result<int>());
  102.     }
  103.  
  104.     {
  105.         runLua("result = uintFn (42)");
  106.         ASSERT_EQ(true, result().isNumber());
  107.         ASSERT_EQ(42u, result<unsigned int>());
  108.     }
  109.  
  110.     {
  111.         runLua("result = longFn (-8000)");
  112.         ASSERT_EQ(true, result().isNumber());
  113.         ASSERT_EQ(-8000, result<long>());
  114.     }
  115.  
  116.     {
  117.         runLua("result = ulongFn (9000)");
  118.         ASSERT_EQ(true, result().isNumber());
  119.         ASSERT_EQ(9000u, result<unsigned long>());
  120.     }
  121.  
  122.     {
  123.         runLua("result = longlongFn (-8000)");
  124.         ASSERT_EQ(true, result().isNumber());
  125.         ASSERT_EQ(-8000, result<long long>());
  126.     }
  127.  
  128.     {
  129.         runLua("result = ulonglongFn (9000)");
  130.         ASSERT_EQ(true, result().isNumber());
  131.         ASSERT_EQ(9000u, result<unsigned long long>());
  132.     }
  133.  
  134.     {
  135.         runLua("result = floatFn (3.14)");
  136.         ASSERT_EQ(true, result().isNumber());
  137.         ASSERT_FLOAT_EQ(3.14f, result<float>());
  138.     }
  139.  
  140.     {
  141.         runLua("result = doubleFn (-12.3)");
  142.         ASSERT_EQ(true, result().isNumber());
  143.         ASSERT_DOUBLE_EQ(-12.3, result<double>());
  144.     }
  145.  
  146.     {
  147.         runLua("result = charFn ('a')");
  148.         ASSERT_EQ(true, result().isString());
  149.         ASSERT_EQ('a', result<char>());
  150.     }
  151.  
  152.     {
  153.         runLua("result = cstringFn ('abc')");
  154.         ASSERT_EQ(true, result().isString());
  155.         ASSERT_STREQ("abc", result<const char*>());
  156.     }
  157.  
  158.     {
  159.         runLua("result = stringFn ('lua')");
  160.         ASSERT_EQ(true, result().isString());
  161.         ASSERT_EQ("lua", result<std::string>());
  162.     }
  163. }
  164.  
  165. template<class T>
  166. struct TestClass
  167. {
  168.     TestClass(T data) : data(data), constData(data) {}
  169.  
  170.     T getValue() { return data; }
  171.     T* getPtr() { return &data; }
  172.     T const* getConstPtr() { return &data; }
  173.     T& getRef() { return data; }
  174.     T const& getConstRef() { return data; }
  175.     T getValueConst() const { return data; }
  176.     T* getPtrConst() const { return &data; }
  177.     T const* getConstPtrConst() const { return &data; }
  178.     T& getRefConst() const { return data; }
  179.     T const& getConstRefConst() const { return data; }
  180.  
  181.     mutable T data;
  182.     mutable T constData;
  183. };
  184.  
  185. TEST_F(LuaBridgeTest, ClassFunction)
  186. {
  187.     typedef TestClass<int> Inner;
  188.     typedef TestClass<Inner> Outer;
  189.  
  190.     luabridge::getGlobalNamespace(L)
  191.         .beginClass<Inner>("Inner")
  192.         .addConstructor<void (*)(int)>()
  193.         .addData("data", &Inner::data)
  194.         .endClass()
  195.         .beginClass<Outer>("Outer")
  196.         .addConstructor<void (*)(Inner)>()
  197.         .addFunction("getValue", &Outer::getValue)
  198.         .addFunction("getPtr", &Outer::getPtr)
  199.         .addFunction("getConstPtr", &Outer::getConstPtr)
  200.         .addFunction("getRef", &Outer::getRef)
  201.         .addFunction("getConstRef", &Outer::getConstRef)
  202.         .addFunction("getValueConst", &Outer::getValueConst)
  203.         .addFunction("getPtrConst", &Outer::getPtrConst)
  204.         .addFunction("getConstPtrConst", &Outer::getConstPtrConst)
  205.         .addFunction("getRefConst", &Outer::getRefConst)
  206.         .addFunction("getConstRefConst", &Outer::getConstRefConst)
  207.         .endClass();
  208.  
  209.     Outer outer(Inner(0));
  210.     luabridge::setGlobal(L, &outer, "outer");
  211.  
  212.     outer.data.data = 0;
  213.     runLua("outer:getValue ().data = 1");
  214.     ASSERT_EQ(0, outer.data.data);
  215.  
  216.     outer.data.data = 1;
  217.     runLua("outer:getPtr ().data = 10");
  218.     ASSERT_EQ(10, outer.data.data);
  219.  
  220.     outer.data.data = 2;
  221.     ASSERT_THROW(runLua("outer:getConstPtr ().data = 20"), std::runtime_error);
  222.  
  223.     outer.data.data = 3;
  224.     runLua("outer:getRef().data = 30");
  225.     ASSERT_EQ(30, outer.data.data);
  226.  
  227.     outer.data.data = 4;
  228.     ASSERT_THROW(runLua("outer:getConstPtr ().data = 40"), std::runtime_error);
  229.  
  230.     outer.data.data = 5;
  231.     runLua("outer:getValueConst ().data = 50");
  232.     ASSERT_EQ(5, outer.data.data);
  233.  
  234.     outer.data.data = 6;
  235.     runLua("outer:getPtrConst ().data = 60");
  236.     ASSERT_EQ(60, outer.data.data);
  237.  
  238.     outer.data.data = 7;
  239.     ASSERT_THROW(runLua("outer:getConstPtr ().data = 70"), std::runtime_error);
  240.  
  241.     outer.data.data = 8;
  242.     runLua("outer:getRef().data = 80");
  243.     ASSERT_EQ(80, outer.data.data);
  244.  
  245.     outer.data.data = 9;
  246.     ASSERT_THROW(runLua("outer:getConstPtr ().data = 90"), std::runtime_error);
  247. }
  248.