?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. #include "TestBase.h"
  8. #include "LuaBridge/detail/dump.h"
  9.  
  10. #include <sstream>
  11.  
  12. struct LuaRefTests : TestBase
  13. {
  14. };
  15.  
  16. TEST_F(LuaRefTests, ValueAccess)
  17. {
  18.     runLua("result = true");
  19.     ASSERT_TRUE(result().isBool());
  20.     ASSERT_TRUE(result<bool>());
  21.  
  22.     runLua("result = 7");
  23.     ASSERT_TRUE(result().isNumber());
  24.     ASSERT_EQ(7u, result<unsigned char>());
  25.     ASSERT_EQ(7, result<short>());
  26.     ASSERT_EQ(7u, result<unsigned short>());
  27.     ASSERT_EQ(7, result<int>());
  28.     ASSERT_EQ(7u, result<unsigned int>());
  29.     ASSERT_EQ(7, result<long>());
  30.     ASSERT_EQ(7u, result<unsigned long>());
  31.     ASSERT_EQ(7, result<long long>());
  32.     ASSERT_EQ(7u, result<unsigned long long>());
  33.  
  34.     runLua("result = 3.14");
  35.     ASSERT_TRUE(result().isNumber());
  36.     ASSERT_FLOAT_EQ(3.14f, result<float>());
  37.     ASSERT_DOUBLE_EQ(3.14, result<double>());
  38.  
  39.     runLua("result = 'D'");
  40.     ASSERT_TRUE(result().isString());
  41.     ASSERT_EQ('D', result<char>());
  42.     ASSERT_EQ("D", result<std::string>());
  43.     ASSERT_STREQ("D", result<const char*>());
  44.  
  45.     runLua("result = 'abc'");
  46.     ASSERT_TRUE(result().isString());
  47.     ASSERT_EQ("abc", result<std::string>());
  48.     ASSERT_STREQ("abc", result<char const*>());
  49.  
  50.     runLua("result = function (i) "
  51.            "  result = i + 1 "
  52.            "  return i "
  53.            "end");
  54.     ASSERT_TRUE(result().isFunction());
  55.     auto fnResult = result()(41); // Replaces result variable
  56.     ASSERT_TRUE(fnResult.isNumber());
  57.     ASSERT_EQ(41, fnResult.cast<int>());
  58.     ASSERT_TRUE(result().isNumber());
  59.     ASSERT_EQ(42, result<int>());
  60. }
  61.  
  62. TEST_F(LuaRefTests, DictionaryRead)
  63. {
  64.     runLua("result = {"
  65.            "  bool = true,"
  66.            "  int = 5,"
  67.            "  c = 3.14,"
  68.            "  [true] = 'D',"
  69.            "  [8] = 'abc',"
  70.            "  fn = function (i) "
  71.            "    result = i + 1 "
  72.            "    return i "
  73.            "  end"
  74.            "}");
  75.  
  76.     ASSERT_TRUE(result()["bool"].isBool());
  77.     ASSERT_TRUE(result()["bool"].cast<bool>());
  78.  
  79.     ASSERT_TRUE(result()["int"].isNumber());
  80.     ASSERT_EQ(5u, result()["int"].cast<unsigned char>());
  81.     ASSERT_EQ(5, result()["int"].cast<short>());
  82.     ASSERT_EQ(5u, result()["int"].cast<unsigned short>());
  83.     ASSERT_EQ(5, result()["int"].cast<int>());
  84.     ASSERT_EQ(5u, result()["int"].cast<unsigned int>());
  85.     ASSERT_EQ(5, result()["int"].cast<long>());
  86.     ASSERT_EQ(5u, result()["int"].cast<unsigned long>());
  87.     ASSERT_EQ(5, result()["int"].cast<long long>());
  88.     ASSERT_EQ(5u, result()["int"].cast<unsigned long long>());
  89.  
  90.     ASSERT_TRUE(result()['c'].isNumber());
  91.     ASSERT_FLOAT_EQ(3.14f, result()['c'].cast<float>());
  92.     ASSERT_DOUBLE_EQ(3.14, result()['c'].cast<double>());
  93.  
  94.     ASSERT_TRUE(result()[true].isString());
  95.     ASSERT_EQ('D', result()[true].cast<char>());
  96.     ASSERT_EQ("D", result()[true].cast<std::string>());
  97.     ASSERT_STREQ("D", result()[true].cast<const char*>());
  98.  
  99.     ASSERT_TRUE(result()[8].isString());
  100.     ASSERT_EQ("abc", result()[8].cast<std::string>());
  101.     ASSERT_STREQ("abc", result()[8].cast<char const*>());
  102.  
  103.     ASSERT_TRUE(result()["fn"].isFunction());
  104.     auto fnResult = result()["fn"](41); // Replaces result variable
  105.     ASSERT_TRUE(fnResult.isNumber());
  106.     ASSERT_EQ(41, fnResult.cast<int>());
  107.     ASSERT_TRUE(result().isNumber());
  108.     ASSERT_EQ(42, result<int>());
  109. }
  110.  
  111. TEST_F(LuaRefTests, DictionaryWrite)
  112. {
  113.     runLua("result = {a = 5}");
  114.     ASSERT_TRUE(result()["a"].isNumber());
  115.     ASSERT_EQ(5, result()["a"].cast<int>());
  116.  
  117.     result()["a"] = 7;
  118.     ASSERT_EQ(7, result()["a"].cast<int>());
  119.  
  120.     runLua("result = result.a");
  121.     ASSERT_EQ(7, result<int>());
  122.  
  123.     runLua("result = {a = {b = 1}}");
  124.     ASSERT_EQ(1, result()["a"]["b"].cast<int>());
  125.  
  126.     result()["a"]["b"] = 2;
  127.     ASSERT_EQ(2, result()["a"]["b"].cast<int>());
  128. }
  129.  
  130. struct Class
  131. {
  132. };
  133.  
  134. TEST_F(LuaRefTests, Comparison)
  135. {
  136.     runLua("function foo () end "
  137.            "local m = {} "
  138.            "m.__eq = function (l, r) return l.a == r.a end "
  139.            "m.__lt = function (l, r) return l.a < r.a end "
  140.            "m.__le = function (l, r) return l.a <= r.a end "
  141.            "table1aMT = {a = 1} setmetatable (table1aMT, m) "
  142.            "table1bMT = {a = 1} setmetatable (table1bMT, m) "
  143.            "table2aMT = {a = 2} setmetatable (table2aMT, m) "
  144.            "table2b = {a = 2} "
  145.            "table2c = {a = 2}");
  146.  
  147.     luabridge::getGlobalNamespace(L).beginClass<Class>("Class").endClass();
  148.  
  149.     luabridge::LuaRef nil(L, luabridge::Nil());
  150.     luabridge::LuaRef boolFalse(L, false);
  151.     luabridge::LuaRef boolTrue(L, true);
  152.     luabridge::LuaRef minus5(L, -5);
  153.     luabridge::LuaRef numPi(L, 3.14);
  154.     luabridge::LuaRef stringA(L, 'a');
  155.     luabridge::LuaRef stringAB(L, "ab");
  156.     luabridge::LuaRef table1aMT = luabridge::getGlobal(L, "table1aMT");
  157.     luabridge::LuaRef table1bMT = luabridge::getGlobal(L, "table1bMT");
  158.     luabridge::LuaRef table2aMT = luabridge::getGlobal(L, "table2aMT");
  159.     luabridge::LuaRef table2b = luabridge::getGlobal(L, "table2b");
  160.     luabridge::LuaRef table2c = luabridge::getGlobal(L, "table2c");
  161.  
  162.     ASSERT_TRUE(nil == nil);
  163.  
  164.     ASSERT_TRUE(nil < boolFalse);
  165.  
  166.     ASSERT_TRUE(boolFalse == boolFalse);
  167.     ASSERT_TRUE(boolTrue == boolTrue);
  168.  
  169.     ASSERT_TRUE(boolTrue < minus5);
  170.  
  171.     ASSERT_TRUE(minus5 == minus5);
  172.     ASSERT_FALSE(minus5 == numPi);
  173.     ASSERT_TRUE(minus5 < numPi);
  174.     ASSERT_TRUE(minus5 <= numPi);
  175.     ASSERT_FALSE(minus5 > numPi);
  176.     ASSERT_FALSE(minus5 >= numPi);
  177.  
  178.     ASSERT_TRUE(numPi < stringA);
  179.  
  180.     ASSERT_TRUE(stringA == stringA);
  181.     ASSERT_FALSE(stringA == stringAB);
  182.     ASSERT_TRUE(stringA < stringAB);
  183.     ASSERT_TRUE(stringA <= stringAB);
  184.     ASSERT_FALSE(stringA > stringAB);
  185.     ASSERT_FALSE(stringA >= stringAB);
  186.  
  187.     ASSERT_TRUE(stringA < table1aMT);
  188.  
  189.     ASSERT_TRUE(table1aMT == table1aMT);
  190.     ASSERT_TRUE(table1aMT == table1bMT);
  191.     ASSERT_FALSE(table1aMT == table2aMT);
  192.     ASSERT_FALSE(table1aMT.rawequal(table1bMT));
  193.     ASSERT_FALSE(table1aMT == table2b);
  194.     ASSERT_TRUE(table2aMT == table2aMT);
  195.     ASSERT_FALSE(table2aMT == table1bMT);
  196.  
  197. #if LUABRIDGE_TEST_LUA_VERSION < 503
  198.     // Despite its documentation Lua <= 5.2 compares
  199.     // the metamethods and ignores them if they differ
  200.     ASSERT_FALSE(table2aMT == table2b);
  201. #else
  202.     ASSERT_TRUE(table2aMT == table2b);
  203. #endif
  204.  
  205.     ASSERT_TRUE(table1bMT == table1bMT);
  206.     ASSERT_FALSE(table1bMT == table2b);
  207.     ASSERT_FALSE(table2b == table2c);
  208.  
  209.     ASSERT_FALSE(table1aMT < table1aMT);
  210.     ASSERT_TRUE(table1aMT < table2aMT);
  211.     ASSERT_FALSE(table1aMT < table1bMT);
  212.     ASSERT_FALSE(table2aMT < table1bMT);
  213.  
  214.     ASSERT_TRUE(table1aMT <= table1aMT);
  215.     ASSERT_TRUE(table1aMT <= table2aMT);
  216.     ASSERT_TRUE(table1aMT <= table1bMT);
  217.     ASSERT_FALSE(table2aMT <= table1bMT);
  218.  
  219.     ASSERT_FALSE(table1aMT > table1aMT);
  220.     ASSERT_FALSE(table1aMT > table2aMT);
  221.     ASSERT_FALSE(table1aMT > table1bMT);
  222.     ASSERT_TRUE(table2aMT > table1bMT);
  223.  
  224.     ASSERT_TRUE(table1aMT >= table1aMT);
  225.     ASSERT_FALSE(table1aMT >= table2aMT);
  226.     ASSERT_TRUE(table1aMT >= table1bMT);
  227.     ASSERT_TRUE(table2aMT >= table1bMT);
  228. }
  229.  
  230. TEST_F(LuaRefTests, Assignment)
  231. {
  232.     runLua("value = {a = 5}");
  233.     auto value = luabridge::getGlobal(L, "value");
  234.     ASSERT_TRUE(value.isTable());
  235.     ASSERT_TRUE(value["a"].isNumber());
  236.     ASSERT_EQ(5, value["a"].cast<int>());
  237.  
  238.     value = value["a"];
  239.     ASSERT_TRUE(value.isNumber());
  240.     ASSERT_EQ(5, value.cast<int>());
  241.  
  242.     value = value;
  243.     ASSERT_EQ(LUA_TNUMBER, value.type());
  244.     ASSERT_TRUE(value.isNumber());
  245.     ASSERT_EQ(5, value.cast<int>());
  246.  
  247.     runLua("t = {a = {b = 5}}");
  248.     auto table = luabridge::getGlobal(L, "t");
  249.     luabridge::LuaRef entry = table["a"];
  250.     luabridge::LuaRef b1 = entry["b"];
  251.     luabridge::LuaRef b2 = table["a"]["b"];
  252.     ASSERT_TRUE(b1 == b2);
  253. }
  254.  
  255. TEST_F(LuaRefTests, IsInstance)
  256. {
  257.     struct Base
  258.     {
  259.     };
  260.  
  261.     struct Derived : Base
  262.     {
  263.     };
  264.  
  265.     struct Other
  266.     {
  267.     };
  268.  
  269.     struct Unknown : Base
  270.     {
  271.     };
  272.  
  273.     luabridge::getGlobalNamespace(L)
  274.         .beginClass<Base>("Base")
  275.         .addConstructor<void (*)()>()
  276.         .endClass()
  277.         .deriveClass<Derived, Base>("Derived")
  278.         .addConstructor<void (*)()>()
  279.         .endClass()
  280.         .beginClass<Other>("Other")
  281.         .addConstructor<void (*)()>()
  282.         .endClass();
  283.  
  284.     runLua("result = Base ()");
  285.     ASSERT_TRUE(result().isInstance<Base>());
  286.     ASSERT_FALSE(result().isInstance<Derived>());
  287.     ASSERT_FALSE(result().isInstance<Other>());
  288.     ASSERT_FALSE(result().isInstance<Unknown>());
  289.     ASSERT_TRUE(result().isUserdata());
  290.  
  291.     runLua("result = Derived ()");
  292.     ASSERT_TRUE(result().isInstance<Base>());
  293.     ASSERT_TRUE(result().isInstance<Derived>());
  294.     ASSERT_FALSE(result().isInstance<Other>());
  295.     ASSERT_FALSE(result().isInstance<Unknown>());
  296.     ASSERT_TRUE(result().isUserdata());
  297.  
  298.     runLua("result = Other ()");
  299.     ASSERT_FALSE(result().isInstance<Base>());
  300.     ASSERT_FALSE(result().isInstance<Derived>());
  301.     ASSERT_TRUE(result().isInstance<Other>());
  302.     ASSERT_FALSE(result().isInstance<Unknown>());
  303.     ASSERT_TRUE(result().isUserdata());
  304.  
  305.     runLua("result = 3.14");
  306.     ASSERT_FALSE(result().isInstance<Base>());
  307.     ASSERT_FALSE(result().isInstance<Derived>());
  308.     ASSERT_FALSE(result().isInstance<Other>());
  309.     ASSERT_FALSE(result().isInstance<Unknown>());
  310.     ASSERT_FALSE(result().isUserdata());
  311. }
  312.  
  313. TEST_F(LuaRefTests, Print)
  314. {
  315.     {
  316.         runLua("result = true");
  317.         std::ostringstream stream;
  318.         stream << result();
  319.         ASSERT_EQ("true", stream.str());
  320.     }
  321.     {
  322.         runLua("result = false");
  323.         std::ostringstream stream;
  324.         stream << result();
  325.         ASSERT_EQ("false", stream.str());
  326.     }
  327.     {
  328.         runLua("result = 5");
  329.         std::ostringstream stream;
  330.         stream << result();
  331.         ASSERT_EQ("5", stream.str());
  332.     }
  333.     {
  334.         runLua("result = 'abc'");
  335.         std::ostringstream stream;
  336.         stream << result();
  337.         ASSERT_EQ("\"abc\"", stream.str());
  338.     }
  339.  
  340.     runLua("result = {"
  341.            "  true_ = true,"
  342.            "  false_ = false,"
  343.            "  five = 5,"
  344.            "  abc = 'abc'"
  345.            "}");
  346.     {
  347.         std::ostringstream stream;
  348.         stream << result()["true_"];
  349.         ASSERT_EQ("true", stream.str());
  350.     }
  351.     {
  352.         std::ostringstream stream;
  353.         stream << result()["false_"];
  354.         ASSERT_EQ("false", stream.str());
  355.     }
  356.     {
  357.         std::ostringstream stream;
  358.         stream << result()["five"];
  359.         ASSERT_EQ("5", stream.str());
  360.     }
  361.     {
  362.         std::ostringstream stream;
  363.         stream << result()["abc"];
  364.         ASSERT_EQ("\"abc\"", stream.str());
  365.     }
  366. }
  367.