?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2020, Dmitry Tarakanov
  3. // SPDX-License-Identifier: MIT
  4.  
  5. #include "TestBase.h"
  6.  
  7. struct StackTests : TestBase
  8. {
  9. };
  10.  
  11. #ifdef LUABRIDGE_CXX17
  12. #define CXX17(EXPR) EXPR
  13. #else
  14. #define CXX17(EXPR)
  15. #endif
  16.  
  17. #if LUA_VERSION_NUM <= 502
  18. #define LUA_501_502(EXPR) EXPR
  19. #else
  20. #define LUA_501_502(EXPR)
  21. #endif
  22.  
  23. #if LUA_VERSION_NUM >= 503
  24. #define LUA_503_504(EXPR) EXPR
  25. #else
  26. #define LUA_503_504(EXPR)
  27. #endif
  28.  
  29. TEST_F(StackTests, Nil)
  30. {
  31.     luabridge::push(L, luabridge::Nil());
  32.  
  33.     ASSERT_TRUE(luabridge::isInstance<luabridge::Nil>(L, -1));
  34.     ASSERT_FALSE(luabridge::isInstance<bool>(L, -1));
  35.     ASSERT_FALSE(luabridge::isInstance<int>(L, -1));
  36.     ASSERT_FALSE(luabridge::isInstance<unsigned>(L, -1));
  37.     ASSERT_FALSE(luabridge::isInstance<float>(L, -1));
  38.     ASSERT_FALSE(luabridge::isInstance<double>(L, -1));
  39.     ASSERT_FALSE(luabridge::isInstance<std::string>(L, -1));
  40.     CXX17(ASSERT_FALSE(luabridge::isInstance<std::string_view>(L, -1)));
  41. }
  42.  
  43. TEST_F(StackTests, Bool)
  44. {
  45.     luabridge::push(L, true);
  46.  
  47.     ASSERT_FALSE(luabridge::isInstance<luabridge::Nil>(L, -1));
  48.     ASSERT_FALSE(luabridge::isInstance<luabridge::Nil>(L, -1));
  49.     ASSERT_TRUE(luabridge::isInstance<bool>(L, -1));
  50.     ASSERT_FALSE(luabridge::isInstance<int>(L, -1));
  51.     ASSERT_FALSE(luabridge::isInstance<unsigned>(L, -1));
  52.     ASSERT_FALSE(luabridge::isInstance<float>(L, -1));
  53.     ASSERT_FALSE(luabridge::isInstance<double>(L, -1));
  54.     ASSERT_FALSE(luabridge::isInstance<const char*>(L, -1));
  55.     ASSERT_FALSE(luabridge::isInstance<std::string>(L, -1));
  56.     CXX17(ASSERT_FALSE(luabridge::isInstance<std::string_view>(L, -1)));
  57.  
  58.     ASSERT_EQ(true, luabridge::get<bool>(L, -1));
  59. }
  60.  
  61. TEST_F(StackTests, Int)
  62. {
  63.     luabridge::push(L, 5);
  64.  
  65.     ASSERT_FALSE(luabridge::isInstance<luabridge::Nil>(L, -1));
  66.     ASSERT_FALSE(luabridge::isInstance<bool>(L, -1));
  67.     ASSERT_TRUE(luabridge::isInstance<int>(L, -1));
  68.     ASSERT_TRUE(luabridge::isInstance<unsigned>(L, -1));
  69.     ASSERT_TRUE(luabridge::isInstance<float>(L, -1));
  70.     ASSERT_TRUE(luabridge::isInstance<double>(L, -1));
  71.     ASSERT_FALSE(luabridge::isInstance<const char*>(L, -1));
  72.     ASSERT_FALSE(luabridge::isInstance<std::string>(L, -1));
  73.     CXX17(ASSERT_FALSE(luabridge::isInstance<std::string_view>(L, -1)));
  74.  
  75.     ASSERT_EQ(5, luabridge::get<int>(L, -1));
  76.     ASSERT_NEAR(5.f, luabridge::get<float>(L, -1), 1e-5);
  77.     ASSERT_NEAR(5.0, luabridge::get<double>(L, -1), 1e-6);
  78. }
  79.  
  80. TEST_F(StackTests, Float)
  81. {
  82.     luabridge::push(L, 3.14f);
  83.  
  84.     ASSERT_FALSE(luabridge::isInstance<luabridge::Nil>(L, -1));
  85.     ASSERT_FALSE(luabridge::isInstance<bool>(L, -1));
  86.     LUA_501_502(ASSERT_TRUE(luabridge::isInstance<int>(L, -1)));
  87.     LUA_503_504(ASSERT_FALSE(luabridge::isInstance<int>(L, -1)));
  88.     LUA_501_502(ASSERT_TRUE(luabridge::isInstance<unsigned>(L, -1)));
  89.     LUA_503_504(ASSERT_FALSE(luabridge::isInstance<unsigned>(L, -1)));
  90.     ASSERT_TRUE(luabridge::isInstance<float>(L, -1));
  91.     ASSERT_TRUE(luabridge::isInstance<double>(L, -1));
  92.     ASSERT_FALSE(luabridge::isInstance<const char*>(L, -1));
  93.     ASSERT_FALSE(luabridge::isInstance<std::string>(L, -1));
  94.     CXX17(ASSERT_FALSE(luabridge::isInstance<std::string_view>(L, -1)));
  95.  
  96.     LUA_501_502(ASSERT_EQ(3, luabridge::get<int>(L, -1)));
  97.     LUA_501_502(ASSERT_EQ(3, luabridge::get<unsigned>(L, -1)));
  98.     ASSERT_NEAR(3.14f, luabridge::get<float>(L, -1), 1e-5);
  99.     ASSERT_NEAR(3.14, luabridge::get<double>(L, -1), 1e-6);
  100. }
  101.  
  102. TEST_F(StackTests, CString)
  103. {
  104.     luabridge::push(L, "abc");
  105.  
  106.     ASSERT_FALSE(luabridge::isInstance<luabridge::Nil>(L, -1));
  107.     ASSERT_FALSE(luabridge::isInstance<bool>(L, -1));
  108.     ASSERT_FALSE(luabridge::isInstance<int>(L, -1));
  109.     ASSERT_FALSE(luabridge::isInstance<unsigned>(L, -1));
  110.     ASSERT_FALSE(luabridge::isInstance<float>(L, -1));
  111.     ASSERT_FALSE(luabridge::isInstance<double>(L, -1));
  112.     ASSERT_TRUE(luabridge::isInstance<const char*>(L, -1));
  113.     ASSERT_TRUE(luabridge::isInstance<std::string>(L, -1));
  114.     CXX17(ASSERT_TRUE(luabridge::isInstance<std::string_view>(L, -1)));
  115.  
  116.     ASSERT_STREQ("abc", luabridge::get<const char*>(L, -1));
  117.     ASSERT_EQ("abc", luabridge::get<std::string>(L, -1));
  118.     CXX17(ASSERT_EQ("abc", luabridge::get<std::string_view>(L, -1)));
  119. }
  120.  
  121. TEST_F(StackTests, StdString)
  122. {
  123.     luabridge::push(L, std::string("abc"));
  124.  
  125.     ASSERT_FALSE(luabridge::isInstance<luabridge::Nil>(L, -1));
  126.     ASSERT_FALSE(luabridge::isInstance<bool>(L, -1));
  127.     ASSERT_FALSE(luabridge::isInstance<int>(L, -1));
  128.     ASSERT_FALSE(luabridge::isInstance<unsigned>(L, -1));
  129.     ASSERT_FALSE(luabridge::isInstance<float>(L, -1));
  130.     ASSERT_FALSE(luabridge::isInstance<double>(L, -1));
  131.     ASSERT_TRUE(luabridge::isInstance<const char*>(L, -1));
  132.     ASSERT_TRUE(luabridge::isInstance<std::string>(L, -1));
  133.     CXX17(ASSERT_TRUE(luabridge::isInstance<std::string_view>(L, -1)));
  134.  
  135.     ASSERT_STREQ("abc", luabridge::get<const char*>(L, -1));
  136.     ASSERT_EQ("abc", luabridge::get<std::string>(L, -1));
  137.     CXX17(ASSERT_EQ("abc", luabridge::get<std::string_view>(L, -1)));
  138. }
  139.  
  140. #ifdef LUABRIDGE_CXX17
  141.  
  142. TEST_F(StackTests, StdStringView)
  143. {
  144.     luabridge::push(L, std::string_view("abc"));
  145.  
  146.     ASSERT_FALSE(luabridge::isInstance<luabridge::Nil>(L, -1));
  147.     ASSERT_FALSE(luabridge::isInstance<bool>(L, -1));
  148.     ASSERT_FALSE(luabridge::isInstance<int>(L, -1));
  149.     ASSERT_FALSE(luabridge::isInstance<unsigned>(L, -1));
  150.     ASSERT_FALSE(luabridge::isInstance<float>(L, -1));
  151.     ASSERT_FALSE(luabridge::isInstance<double>(L, -1));
  152.     ASSERT_TRUE(luabridge::isInstance<const char*>(L, -1));
  153.     ASSERT_TRUE(luabridge::isInstance<std::string>(L, -1));
  154.     CXX17(ASSERT_TRUE(luabridge::isInstance<std::string_view>(L, -1)));
  155.  
  156.     ASSERT_STREQ("abc", luabridge::get<const char*>(L, -1));
  157.     ASSERT_EQ("abc", luabridge::get<std::string>(L, -1));
  158.     CXX17(ASSERT_EQ("abc", luabridge::get<std::string_view>(L, -1)));
  159. }
  160.  
  161. #endif // LUABRIDGE_CXX17
  162.