PythonPairTests.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #pragma once
  9. #include <EditorPythonBindings/PythonCommon.h>
  10. #include <pybind11/embed.h>
  11. #include <pybind11/pybind11.h>
  12. #include "PythonTraceMessageSink.h"
  13. #include "PythonTestingUtility.h"
  14. #include <Source/PythonSystemComponent.h>
  15. #include <Source/PythonReflectionComponent.h>
  16. #include <Source/PythonMarshalComponent.h>
  17. #include <Source/PythonProxyObject.h>
  18. #include <AzCore/RTTI/BehaviorContext.h>
  19. #include <AzFramework/StringFunc/StringFunc.h>
  20. #include <AzCore/std/hash.h>
  21. namespace UnitTest
  22. {
  23. //////////////////////////////////////////////////////////////////////////
  24. // test class/structs
  25. struct MyCustomType
  26. {
  27. AZ_TYPE_INFO(MyCustomType, "{E4BE9816-E3E0-49EA-99B0-D72403461548}");
  28. public:
  29. AZ::u8 m_data;
  30. void SetData(AZ::u8 v)
  31. {
  32. m_data = v;
  33. }
  34. AZ::u8 GetData() const
  35. {
  36. return m_data;
  37. }
  38. static void Reflect(AZ::ReflectContext* context)
  39. {
  40. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  41. {
  42. serializeContext->Class<MyCustomType>()
  43. ->Version(1)
  44. ->Field("data", &MyCustomType::m_data)
  45. ;
  46. }
  47. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  48. {
  49. behaviorContext->Class<MyCustomType>("MyCustomType")
  50. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  51. ->Attribute(AZ::Script::Attributes::Module, "test.pair")
  52. ->Method("set_data", &MyCustomType::SetData)
  53. ->Method("get_data", &MyCustomType::GetData)
  54. ;
  55. }
  56. }
  57. };
  58. }
  59. // AZStd::hash specialization for UnitTest::MyCustomType, required by BehaviorContext for AZStd::pair with custom types.
  60. template<>
  61. struct AZStd::hash<UnitTest::MyCustomType>
  62. {
  63. typedef UnitTest::MyCustomType argument_type;
  64. typedef AZStd::size_t result_type;
  65. constexpr result_type operator()(const argument_type& value) const
  66. {
  67. return AZStd::hash<AZ::u8>()(value.m_data);
  68. }
  69. };