PythonLogSymbolsComponentTests.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "PythonTestingUtility.h"
  9. #include "PythonTraceMessageSink.h"
  10. #include <EditorPythonBindings/PythonCommon.h>
  11. #include <pybind11/embed.h>
  12. #include <pybind11/pybind11.h>
  13. #include <Source/PythonSystemComponent.h>
  14. #include <Source/PythonReflectionComponent.h>
  15. #include <Source/PythonMarshalComponent.h>
  16. #include <Source/PythonLogSymbolsComponent.h>
  17. #include <AzCore/Math/Vector3.h>
  18. #include <AzCore/RTTI/BehaviorContext.h>
  19. #include <AzFramework/StringFunc/StringFunc.h>
  20. namespace UnitTest
  21. {
  22. //////////////////////////////////////////////////////////////////////////
  23. // test classes/structs
  24. class PythonLogSymbolsTestComponent :
  25. public EditorPythonBindings::PythonLogSymbolsComponent
  26. {
  27. public:
  28. AZ_COMPONENT(PythonLogSymbolsTestComponent, "{D5802A34-1B57-470B-8C30-FFC273C9F4ED}", EditorPythonBindings::PythonLogSymbolsComponent);
  29. AZStd::string_view FetchPythonTypeAndTraitsWrapper(const AZ::TypeId& typeId, AZ::u32 traits)
  30. {
  31. return FetchPythonTypeAndTraits(typeId, traits);
  32. }
  33. AZStd::string FetchPythonTypeWrapper(const AZ::BehaviorParameter& param)
  34. {
  35. return FetchPythonTypeName(param);
  36. }
  37. };
  38. class SimpleClass
  39. {
  40. public:
  41. AZ_TYPE_INFO(SimpleClass, "{DFA153D8-F168-44F9-8DEF-55CDBBAA5AA2}")
  42. };
  43. class CustomClass
  44. {
  45. public:
  46. AZ_TYPE_INFO(CustomClass, "{361A9A18-40E6-4D16-920A-0F38F55D63BF}")
  47. void NoOp() const
  48. {}
  49. };
  50. struct TestTypesReflectionContainer
  51. {
  52. AZ_TYPE_INFO(TestTypesReflectionContainer, "{5DE28B62-F9A1-4307-9684-6C95B9EE3225}")
  53. void Reflect(AZ::ReflectContext* context)
  54. {
  55. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  56. {
  57. serializeContext->RegisterGenericType<AZStd::vector<int>>();
  58. serializeContext->RegisterGenericType<AZStd::vector<SimpleClass>>();
  59. serializeContext->RegisterGenericType<AZStd::vector<CustomClass>>();
  60. serializeContext->RegisterGenericType<AZStd::map<int, int>>();
  61. serializeContext->RegisterGenericType<AZStd::map<int, SimpleClass>>();
  62. serializeContext->RegisterGenericType<AZStd::map<int, CustomClass>>();
  63. serializeContext->RegisterGenericType<AZ::Outcome<int, int>>();
  64. serializeContext->RegisterGenericType<AZ::Outcome<int, SimpleClass>>();
  65. serializeContext->RegisterGenericType<AZ::Outcome<int, CustomClass>>();
  66. serializeContext->Class<CustomClass>()
  67. ->Version(1)
  68. ;
  69. // SimpleClass registration ommited for testing cases where type cannot be determined.
  70. }
  71. }
  72. };
  73. //////////////////////////////////////////////////////////////////////////
  74. // fixtures
  75. struct PythonLogSymbolsComponentTest
  76. : public PythonTestingFixture
  77. {
  78. void SetUp() override
  79. {
  80. PythonTestingFixture::SetUp();
  81. PythonTestingFixture::RegisterComponentDescriptors();
  82. // Registering test types
  83. TestTypesReflectionContainer typesContainer;
  84. typesContainer.Reflect(m_app.GetSerializeContext());
  85. typesContainer.Reflect(m_app.GetBehaviorContext());
  86. }
  87. void TearDown() override
  88. {
  89. // clearing up memory
  90. PythonTestingFixture::TearDown();
  91. }
  92. };
  93. //////////////////////////////////////////////////////////////////////////
  94. // tests
  95. TEST_F(PythonLogSymbolsComponentTest, FetchSupportedTypesByTypeAndTraits_PythonTypeReturned)
  96. {
  97. PythonLogSymbolsTestComponent pythonLogSymbolsComponent;
  98. AZStd::vector<AZStd::tuple<AZ::TypeId, AZ::u32, AZStd::string>> typesToTest =
  99. {
  100. // Simple types
  101. AZStd::make_tuple(AZ::AzTypeInfo<AZStd::string_view>::Uuid(), AZ::BehaviorParameter::TR_NONE, "str"),
  102. AZStd::make_tuple(AZ::AzTypeInfo<AZStd::string>::Uuid(), AZ::BehaviorParameter::TR_NONE, "str"),
  103. AZStd::make_tuple(AZ::AzTypeInfo<char>::Uuid(), AZ::BehaviorParameter::TR_POINTER | AZ::BehaviorParameter::TR_CONST, "str"),
  104. AZStd::make_tuple(AZ::AzTypeInfo<float>::Uuid(), AZ::BehaviorParameter::TR_NONE, "float"),
  105. AZStd::make_tuple(AZ::AzTypeInfo<double>::Uuid(), AZ::BehaviorParameter::TR_NONE, "float"),
  106. AZStd::make_tuple(AZ::AzTypeInfo<bool>::Uuid(), AZ::BehaviorParameter::TR_NONE, "bool"),
  107. AZStd::make_tuple(AZ::AzTypeInfo<AZ::s8>::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"),
  108. AZStd::make_tuple(AZ::AzTypeInfo<AZ::u8>::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"),
  109. AZStd::make_tuple(AZ::AzTypeInfo<AZ::s16>::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"),
  110. AZStd::make_tuple(AZ::AzTypeInfo<AZ::u16>::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"),
  111. AZStd::make_tuple(AZ::AzTypeInfo<AZ::s32>::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"),
  112. AZStd::make_tuple(AZ::AzTypeInfo<AZ::u32>::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"),
  113. AZStd::make_tuple(AZ::AzTypeInfo<AZ::s64>::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"),
  114. AZStd::make_tuple(AZ::AzTypeInfo<AZ::u64>::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"),
  115. AZStd::make_tuple(AZ::AzTypeInfo<AZStd::vector<AZ::u8>>::Uuid(), AZ::BehaviorParameter::TR_NONE, "bytes"),
  116. AZStd::make_tuple(AZ::AzTypeInfo<AZStd::any>::Uuid(), AZ::BehaviorParameter::TR_NONE, "object"),
  117. AZStd::make_tuple(AZ::AzTypeInfo<void>::Uuid(), AZ::BehaviorParameter::TR_NONE, "None"),
  118. // Container types
  119. AZStd::make_tuple(AZ::AzTypeInfo<AZStd::vector<SimpleClass>>::Uuid(), AZ::BehaviorParameter::TR_NONE, "list"),
  120. AZStd::make_tuple(AZ::AzTypeInfo<AZStd::vector<int>>::Uuid(), AZ::BehaviorParameter::TR_NONE, "List[int]"),
  121. AZStd::make_tuple(AZ::AzTypeInfo<AZStd::vector<CustomClass>>::Uuid(), AZ::BehaviorParameter::TR_NONE, "List[CustomClass]"),
  122. AZStd::make_tuple(AZ::AzTypeInfo<AZStd::map<int, SimpleClass>>::Uuid(), AZ::BehaviorParameter::TR_NONE, "dict"),
  123. AZStd::make_tuple(AZ::AzTypeInfo<AZStd::map<int, int>>::Uuid(), AZ::BehaviorParameter::TR_NONE, "Dict[int, int]"),
  124. AZStd::make_tuple(AZ::AzTypeInfo<AZStd::map<int, CustomClass>>::Uuid(), AZ::BehaviorParameter::TR_NONE, "Dict[int, CustomClass]"),
  125. AZStd::make_tuple(AZ::AzTypeInfo<AZ::Outcome<int, SimpleClass>>::Uuid(), AZ::BehaviorParameter::TR_NONE, "Outcome"),
  126. AZStd::make_tuple(AZ::AzTypeInfo<AZ::Outcome<int, int>>::Uuid(), AZ::BehaviorParameter::TR_NONE, "Outcome[int, int]"),
  127. AZStd::make_tuple(AZ::AzTypeInfo<AZ::Outcome<int, CustomClass>>::Uuid(), AZ::BehaviorParameter::TR_NONE, "Outcome[int, CustomClass]"),
  128. // Fallback to name
  129. AZStd::make_tuple(AZ::AzTypeInfo<SimpleClass>::Uuid(), AZ::BehaviorParameter::TR_NONE, ""),
  130. AZStd::make_tuple(AZ::AzTypeInfo<CustomClass>::Uuid(), AZ::BehaviorParameter::TR_NONE, "CustomClass")
  131. };
  132. auto stringViewHelper = [](const AZStd::string_view& s)
  133. {
  134. return AZStd::string::format(AZ_STRING_FORMAT, AZ_STRING_ARG(s));
  135. };
  136. for (auto& typeInfo : typesToTest)
  137. {
  138. AZStd::string_view result = pythonLogSymbolsComponent.FetchPythonTypeAndTraitsWrapper(AZStd::get<0>(typeInfo), AZStd::get<1>(typeInfo));
  139. EXPECT_EQ(result, AZStd::get<2>(typeInfo))
  140. << "Expected '" << stringViewHelper(AZStd::get<2>(typeInfo)).c_str()
  141. << "' when converting type with id " << AZStd::get<0>(typeInfo).ToFixedString().c_str()
  142. << " but got '" << stringViewHelper(result).c_str() << "'.";
  143. }
  144. }
  145. TEST_F(PythonLogSymbolsComponentTest, FetchByParam_ReturnPythonType)
  146. {
  147. PythonLogSymbolsTestComponent pythonLogSymbolsComponent;
  148. AZ::BehaviorParameter intParam;
  149. intParam.m_name = "foo";
  150. intParam.m_typeId = AZ::AzTypeInfo<AZ::s8>::Uuid(); // Uuid for a supported type
  151. intParam.m_traits = AZ::BehaviorParameter::TR_NONE;
  152. AZStd::string result = pythonLogSymbolsComponent.FetchPythonTypeWrapper(intParam);
  153. EXPECT_EQ(result, "int");
  154. }
  155. TEST_F(PythonLogSymbolsComponentTest, FetchVoidByParam_ReturnNone)
  156. {
  157. PythonLogSymbolsTestComponent m_pythonLogSymbolsComponent;
  158. AZ::BehaviorParameter voidParam;
  159. voidParam.m_name = "void";
  160. voidParam.m_typeId = AZ::Uuid("{9B3E8886-B749-418E-A696-6D7E9EB4D691}"); // A random Uuid
  161. voidParam.m_traits = AZ::BehaviorParameter::TR_NONE;
  162. AZStd::string result = m_pythonLogSymbolsComponent.FetchPythonTypeWrapper(voidParam);
  163. EXPECT_EQ(result, "None");
  164. }
  165. }