?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. #include "TestTypes.h"
  7.  
  8. #include "LuaBridge/Vector.h"
  9.  
  10. #include <vector>
  11.  
  12. template<class T>
  13. struct VectorTest : TestBase
  14. {
  15. };
  16.  
  17. TYPED_TEST_CASE_P(VectorTest);
  18.  
  19. TYPED_TEST_P(VectorTest, LuaRef)
  20. {
  21.     using Traits = TypeTraits<TypeParam>;
  22.  
  23.     this->runLua("result = {" + Traits::list() + "}");
  24.  
  25.     std::vector<TypeParam> expected(Traits::values());
  26.     std::vector<TypeParam> actual = this->result();
  27.     ASSERT_EQ(expected, actual);
  28. }
  29.  
  30. REGISTER_TYPED_TEST_CASE_P(VectorTest, LuaRef);
  31.  
  32. INSTANTIATE_TYPED_TEST_CASE_P(VectorTest, VectorTest, TestTypes);
  33.  
  34. namespace {
  35.  
  36. struct Data
  37. {
  38.     /* explicit */ Data(int i) : i(i) {}
  39.  
  40.     int i;
  41. };
  42.  
  43. bool operator==(const Data& lhs, const Data& rhs)
  44. {
  45.     return lhs.i == rhs.i;
  46. }
  47.  
  48. std::ostream& operator<<(std::ostream& lhs, const Data& rhs)
  49. {
  50.     lhs << "{" << rhs.i << "}";
  51.     return lhs;
  52. }
  53.  
  54. std::vector<Data> processValues(const std::vector<Data>& data)
  55. {
  56.     return data;
  57. }
  58.  
  59. std::vector<Data> processPointers(const std::vector<const Data*>& data)
  60. {
  61.     std::vector<Data> result;
  62.     for (const auto* item : data)
  63.     {
  64.         result.emplace_back(*item);
  65.     }
  66.     return result;
  67. }
  68.  
  69. } // namespace
  70.  
  71. struct VectorTests : TestBase
  72. {
  73. };
  74.  
  75. TEST_F(VectorTests, PassFromLua)
  76. {
  77.     luabridge::getGlobalNamespace(L)
  78.         .beginClass<Data>("Data")
  79.         .addConstructor<void (*)(int)>()
  80.         .endClass()
  81.         .addFunction("processValues", &processValues)
  82.         .addFunction("processPointers", &processPointers);
  83.  
  84.     resetResult();
  85.     runLua("result = processValues ({Data (-1), Data (2)})");
  86.  
  87.     ASSERT_EQ(std::vector<Data>({-1, 2}), result<std::vector<Data>>());
  88.  
  89.     resetResult();
  90.     runLua("result = processPointers ({Data (-3), Data (4)})");
  91.  
  92.     ASSERT_EQ(std::vector<Data>({-3, 4}), result<std::vector<Data>>());
  93. }
  94.