/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #include #include #include #include "PythonTraceMessageSink.h" #include "PythonTestingUtility.h" #include #include #include #include #include #include #include "PythonLogSymbolsComponent.h" namespace UnitTest { ////////////////////////////////////////////////////////////////////////// // test classes/structs class PythonLogSymbolsTestComponent : public EditorPythonBindings::PythonLogSymbolsComponent { public: AZ_COMPONENT(PythonLogSymbolsTestComponent, "{D5802A34-1B57-470B-8C30-FFC273C9F4ED}", EditorPythonBindings::PythonLogSymbolsComponent); AZStd::string_view FetchPythonTypeAndTraitsWrapper(const AZ::TypeId& typeId, AZ::u32 traits) { return FetchPythonTypeAndTraits(typeId, traits); } AZStd::string_view FetchPythonTypeWrapper(const AZ::BehaviorParameter& param) { return FetchPythonType(param); } }; class SimpleClass { public: AZ_TYPE_INFO(SimpleClass, "{DFA153D8-F168-44F9-8DEF-55CDBBAA5AA2}") }; class CustomClass { public: AZ_TYPE_INFO(CustomClass, "{361A9A18-40E6-4D16-920A-0F38F55D63BF}") void NoOp() const {} }; struct TestTypesReflectionContainer { AZ_TYPE_INFO(TestTypesReflectionContainer, "{5DE28B62-F9A1-4307-9684-6C95B9EE3225}") void Reflect(AZ::ReflectContext* context) { if (AZ::SerializeContext* serializeContext = azrtti_cast(context)) { serializeContext->RegisterGenericType>(); serializeContext->RegisterGenericType>(); serializeContext->RegisterGenericType>(); serializeContext->RegisterGenericType>(); serializeContext->RegisterGenericType>(); serializeContext->RegisterGenericType>(); serializeContext->RegisterGenericType>(); serializeContext->RegisterGenericType>(); serializeContext->RegisterGenericType>(); serializeContext->Class() ->Version(1) ; // SimpleClass registration ommited for testing cases where type cannot be determined. } } }; ////////////////////////////////////////////////////////////////////////// // fixtures struct PythonLogSymbolsComponentTest : public PythonTestingFixture { void SetUp() override { PythonTestingFixture::SetUp(); PythonTestingFixture::RegisterComponentDescriptors(); // Registering test types TestTypesReflectionContainer typesContainer; typesContainer.Reflect(m_app.GetSerializeContext()); typesContainer.Reflect(m_app.GetBehaviorContext()); } void TearDown() override { // clearing up memory PythonTestingFixture::TearDown(); } }; ////////////////////////////////////////////////////////////////////////// // tests TEST_F(PythonLogSymbolsComponentTest, FetchSupportedTypesByTypeAndTraits_PythonTypeReturned) { PythonLogSymbolsTestComponent pythonLogSymbolsComponent; AZStd::vector> typesToTest = { // Simple types AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "str"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "str"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_POINTER | AZ::BehaviorParameter::TR_CONST, "str"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "float"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "float"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "float"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "bool"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "int"), AZStd::make_tuple(AZ::AzTypeInfo>::Uuid(), AZ::BehaviorParameter::TR_NONE, "bytes"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "object"), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "None"), // Container types AZStd::make_tuple(AZ::AzTypeInfo>::Uuid(), AZ::BehaviorParameter::TR_NONE, "list"), AZStd::make_tuple(AZ::AzTypeInfo>::Uuid(), AZ::BehaviorParameter::TR_NONE, "List[int]"), AZStd::make_tuple(AZ::AzTypeInfo>::Uuid(), AZ::BehaviorParameter::TR_NONE, "List[CustomClass]"), AZStd::make_tuple(AZ::AzTypeInfo>::Uuid(), AZ::BehaviorParameter::TR_NONE, "dict"), AZStd::make_tuple(AZ::AzTypeInfo>::Uuid(), AZ::BehaviorParameter::TR_NONE, "Dict[int, int]"), AZStd::make_tuple(AZ::AzTypeInfo>::Uuid(), AZ::BehaviorParameter::TR_NONE, "Dict[int, CustomClass]"), AZStd::make_tuple(AZ::AzTypeInfo>::Uuid(), AZ::BehaviorParameter::TR_NONE, "Outcome"), AZStd::make_tuple(AZ::AzTypeInfo>::Uuid(), AZ::BehaviorParameter::TR_NONE, "Outcome[int, int]"), AZStd::make_tuple(AZ::AzTypeInfo>::Uuid(), AZ::BehaviorParameter::TR_NONE, "Outcome[int, CustomClass]"), // Fallback to name AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, ""), AZStd::make_tuple(AZ::AzTypeInfo::Uuid(), AZ::BehaviorParameter::TR_NONE, "CustomClass") }; auto stringViewHelper = [](const AZStd::string_view& s) { return AZStd::string::format(AZ_STRING_FORMAT, AZ_STRING_ARG(s)); }; auto uuidHelper = [](const AZ::Uuid& uuid) { char buffer[AZ::Uuid::MaxStringBuffer]; uuid.ToString(buffer, AZ::Uuid::MaxStringBuffer, true, true); return AZStd::string(buffer); }; for (auto& typeInfo : typesToTest) { AZStd::string_view result = pythonLogSymbolsComponent.FetchPythonTypeAndTraitsWrapper(AZStd::get<0>(typeInfo), AZStd::get<1>(typeInfo)); EXPECT_EQ(result, AZStd::get<2>(typeInfo)) << "Expected '" << stringViewHelper(AZStd::get<2>(typeInfo)).c_str() << "' when converting type with id " << uuidHelper(AZStd::get<0>(typeInfo)).c_str() << " but got '" << stringViewHelper(result).c_str() << "'."; } } TEST_F(PythonLogSymbolsComponentTest, FetchByParam_ReturnPythonType) { PythonLogSymbolsTestComponent pythonLogSymbolsComponent; AZ::BehaviorParameter intParam; intParam.m_name = "foo"; intParam.m_typeId = AZ::AzTypeInfo::Uuid(); // Uuid for a supported type intParam.m_traits = AZ::BehaviorParameter::TR_NONE; AZStd::string_view result = pythonLogSymbolsComponent.FetchPythonTypeWrapper(intParam); EXPECT_EQ(result, "int"); } TEST_F(PythonLogSymbolsComponentTest, FetchVoidByParam_ReturnNone) { PythonLogSymbolsTestComponent m_pythonLogSymbolsComponent; AZ::BehaviorParameter voidParam; voidParam.m_name = "void"; voidParam.m_typeId = AZ::Uuid("{9B3E8886-B749-418E-A696-6D7E9EB4D691}"); // A random Uuid voidParam.m_traits = AZ::BehaviorParameter::TR_NONE; AZStd::string_view result = m_pythonLogSymbolsComponent.FetchPythonTypeWrapper(voidParam); EXPECT_EQ(result, "None"); } }