1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include "PythonTestingUtility.h"
- #include "PythonTraceMessageSink.h"
- #include <EditorPythonBindings/PythonCommon.h>
- #include <pybind11/embed.h>
- #include <pybind11/pybind11.h>
- #include <Source/PythonSystemComponent.h>
- #include <Source/PythonReflectionComponent.h>
- #include <Source/PythonProxyObject.h>
- #include <AzCore/Serialization/SerializeContext.h>
- #include <AzCore/RTTI/BehaviorContext.h>
- #include <AzCore/Casting/numeric_cast.h>
- #include <AzFramework/StringFunc/StringFunc.h>
- #include <AzCore/Component/Entity.h>
- namespace UnitTest
- {
- enum class TestEnumClass : AZ::s32
- {
- Zaphod = 1,
- Arthur
- };
- }
- // give the enum values types
- namespace AZ
- {
- AZ_TYPE_INFO_SPECIALIZE(UnitTest::TestEnumClass, "{F8EBD52A-D508-4A37-81CA-40E1DC176BCC}")
- }
- namespace UnitTest
- {
- //////////////////////////////////////////////////////////////////////////
- // test class/struts
- struct PythonReflectionObjectProxyPropertyTester
- {
- AZ_TYPE_INFO(PythonReflectionObjectProxyPropertyTester, "{F7966C89-7671-43F1-9DA2-677898DACED1}");
- float m_myFloat = 0.0f;
- AZ::s64 m_s64 = 0;
- AZ::s32 m_s32 = 0;
- TestEnumClass m_enumClass = TestEnumClass::Arthur;
- float GetFloat() const
- {
- AZ_TracePrintf("python", "ReflectingObjectProxySimple_GetFloat");
- return m_myFloat;
- }
- void SetFloat(float value)
- {
- AZ_TracePrintf("python", "ReflectingObjectProxySimple_SetFloat");
- m_myFloat = value;
- }
- AZ::u16 PrintMessage(const char* message)
- {
- AZ_TracePrintf("python", message);
- return static_cast<AZ::u16>(::strlen(message));
- }
- enum TestEnum
- {
- TE_Zero = 0,
- TE_One = 1,
- };
- void Reflect(AZ::ReflectContext* context)
- {
- if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
- {
- serializeContext->Enum<TestEnumClass>()
- ->Value("Zaphod", TestEnumClass::Zaphod)
- ->Value("Arthur", TestEnumClass::Arthur)
- ;
- serializeContext->Class<PythonReflectionObjectProxyPropertyTester>()
- ->Field("EnumClass", &PythonReflectionObjectProxyPropertyTester::m_enumClass)
- ->Field("Int32", &PythonReflectionObjectProxyPropertyTester::m_s32)
- ;
- }
- if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
- {
- behaviorContext->Class<PythonReflectionObjectProxyPropertyTester>("TestObjectProxy")
- ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
- ->Attribute(AZ::Script::Attributes::Module, "test.proxy")
- ->Property("myFloat", [](PythonReflectionObjectProxyPropertyTester* that) { return that->GetFloat(); }, [](PythonReflectionObjectProxyPropertyTester* that, float value) { that->SetFloat(value); })
- ->Property("mySignedInt64", [](PythonReflectionObjectProxyPropertyTester* that) { return that->m_s64; }, [](PythonReflectionObjectProxyPropertyTester* that, AZ::s64 value) { that->m_s64 = value; })
- ->Property("s8", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::s8>(-8); }, nullptr)
- ->Property("u8", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::u8>(8); }, nullptr)
- ->Property("s16", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::s16>(-16); }, nullptr)
- ->Property("u16", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::u16>(16); }, nullptr)
- ->Property("s32", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::s32>(-32); }, nullptr)
- ->Property("u32", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::u32>(32); }, nullptr)
- ->Property("s64", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::s64>(-64); }, nullptr)
- ->Property("u64", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::u64>(64); }, nullptr)
- ->Property("f32", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<float>(32.0f); }, nullptr)
- ->Property("d64", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<double>(64.0); }, nullptr)
- ->Method("printMessage", &PythonReflectionObjectProxyPropertyTester::PrintMessage)
- ->Enum<TE_Zero>("Zero")
- ->Enum<TE_One>("One")
- ->Property("enumClass", BehaviorValueProperty(&PythonReflectionObjectProxyPropertyTester::m_enumClass))
- ->Enum<aznumeric_cast<AZ::s32>(TestEnumClass::Zaphod)>("Zaphod")
- ->Enum<aznumeric_cast<AZ::s32>(TestEnumClass::Arthur)>("Arthur")
- ;
- }
- }
- };
- struct PythonReflectionObjectProxyTester
- {
- AZ_TYPE_INFO(PythonReflectionObjectProxyTester, "{4FC01B6B-D738-46AD-BF74-6F72506DD9B1}");
- int DoAdd(int a, int b)
- {
- return a + b;
- }
- AZStd::string_view m_testString;
- AZStd::string m_testBuffer = "initial";
- AZ::s32 m_answer = 0;
- void SetBuffer(AZStd::string_view buffer)
- {
- m_testBuffer = buffer;
- AZ_TracePrintf("python", m_testBuffer.c_str());
- }
- AZStd::string_view GetBuffer() const
- {
- return AZStd::string_view{ m_testBuffer };
- }
- AZ::s32 GetAnswer() const
- {
- return m_answer;
- }
- void SetAnswer(AZ::s32 value)
- {
- m_answer = value;
- }
- void Reflect(AZ::ReflectContext* context)
- {
- if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
- {
- behaviorContext->Class<PythonReflectionObjectProxyTester>("TestObject")
- ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
- ->Attribute(AZ::Script::Attributes::Module, "test.proxy")
- ->Method("doAdd", &PythonReflectionObjectProxyTester::DoAdd)
- ->Property("myString", [](PythonReflectionObjectProxyTester* that) { return that->m_testString; }, [](PythonReflectionObjectProxyTester* that, AZStd::string_view value) { that->m_testString = value; })
- ->Property("theBuffer", &PythonReflectionObjectProxyTester::GetBuffer, &PythonReflectionObjectProxyTester::SetBuffer)
- ->Method("GetAnswer", &PythonReflectionObjectProxyTester::GetAnswer)
- ->Attribute(AZ::Script::Attributes::Alias, "get_answer")
- ->Method("SetAnswer", &PythonReflectionObjectProxyTester::SetAnswer)
- ->Attribute(AZ::Script::Attributes::Alias, "set_answer")
- ->Property("Answer", &PythonReflectionObjectProxyTester::GetAnswer, &PythonReflectionObjectProxyTester::SetAnswer)
- ->Attribute(AZ::Script::Attributes::Alias, "answer")
- ;
- }
- }
- };
- struct EntityIdByValueTester
- : public AZ::EntityId
- {
- AZ_TYPE_INFO(EntityIdByValueTester, "{DE8A9968-B6E1-49D1-86B4-8DC946AC3FC7}");
- AZ_CLASS_ALLOCATOR(EntityIdByValueTester, AZ::SystemAllocator);
- EntityIdByValueTester() = default;
- explicit EntityIdByValueTester(AZ::u64 id)
- : AZ::EntityId(id)
- {}
- static void Reflect(AZ::ReflectContext* context)
- {
- if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
- {
- behaviorContext->Class<EntityIdByValueTester>("EntityIdByValueTester")
- ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
- ->Attribute(AZ::Script::Attributes::Module, "entity")
- ->Method("is_valid", &EntityId::IsValid)
- ->Method("to_string", &EntityId::ToString)
- ->Method("equal", &EntityId::operator==)
- ;
- }
- }
- };
- class PythonObjectBaseTester
- {
- private:
- AZ::s64 m_value = -1;
- AZ::EntityId m_entityId;
- EntityIdByValueTester m_testerId;
- public:
- AZ_TYPE_INFO(PythonObjectBaseTester, "{99978809-BB9F-4915-81B2-E44DF0C59A9E}");
- AZ_CLASS_ALLOCATOR(PythonObjectBaseTester, AZ::SystemAllocator);
- PythonObjectBaseTester()
- {
- m_entityId = AZ::EntityId(0xbabb1e);
- m_testerId = EntityIdByValueTester(0x1010);
- }
- void AcceptAzType(PythonObjectBaseTester* that)
- {
- AZ_TracePrintf("python", "this value:%d, that value:%d", m_value, that->m_value);
- m_value = that->m_value;
- }
- PythonObjectBaseTester* ResultTest(int value)
- {
- PythonObjectBaseTester* tester = aznew PythonObjectBaseTester{};
- tester->m_value = value;
- return tester;
- }
- void SetEntityId(const AZ::EntityId& value)
- {
- m_entityId = value;
- AZ_TracePrintf("python", "setting entity = %s", m_entityId.ToString().c_str() );
- }
- const AZ::EntityId& GetEntityId() const
- {
- return m_entityId;
- }
- EntityIdByValueTester GetEntityIdByValue()
- {
- return m_testerId;
- }
- bool CompareEntityIdByValue(EntityIdByValueTester entityId)
- {
- return m_testerId == entityId;
- }
- AZStd::vector<int> ReturnVectorByValue()
- {
- AZStd::vector<int> aTemp;
- aTemp.push_back(1);
- aTemp.push_back(2);
- aTemp.push_back(3);
- return aTemp;
- }
- void Reflect(AZ::ReflectContext* context)
- {
- if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
- {
- behaviorContext->Class<PythonObjectBaseTester>("TestObjectBase")
- ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
- ->Attribute(AZ::Script::Attributes::Module, "test.proxy")
- ->Property("value", [](PythonObjectBaseTester* that) { return that->m_value; }, [](PythonObjectBaseTester* that, AZ::s64 value) { that->m_value = value; })
- ->Property("entityId", &PythonObjectBaseTester::GetEntityId, &PythonObjectBaseTester::SetEntityId)
- ->Method("acceptAzType", &PythonObjectBaseTester::AcceptAzType)
- ->Method("resultTest", &PythonObjectBaseTester::ResultTest)
- ->Method("get_entity_id_by_value", &PythonObjectBaseTester::GetEntityIdByValue)
- ->Method("compare_entity_id_by_value", &PythonObjectBaseTester::CompareEntityIdByValue)
- ->Method("return_vector_by_value", &PythonObjectBaseTester::ReturnVectorByValue)
- ;
- }
- }
- };
- class PythonObjectConstructionTester
- {
- private:
- AZ::s64 m_s64 = 0;
- AZStd::string m_text;
- public:
- AZ_TYPE_INFO(PythonObjectConstructionTester, "{35F7EE10-CA36-4F77-95B5-8001BA384E5A}");
- AZ_CLASS_ALLOCATOR(PythonObjectConstructionTester, AZ::SystemAllocator);
- PythonObjectConstructionTester()
- {
- m_text = "default";
- }
- PythonObjectConstructionTester(const AZStd::string& textValue)
- {
- m_text = textValue;
- }
- PythonObjectConstructionTester(AZ::s64 longValue)
- {
- m_text = "with_int";
- m_s64 = longValue;
- }
- PythonObjectConstructionTester(const AZStd::string& textValue, AZ::s64 longValue)
- {
- m_text = textValue;
- m_s64 = longValue;
- }
- const AZStd::string& GetText() const
- {
- return m_text;
- }
- void Reflect(AZ::ReflectContext* context)
- {
- if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
- {
- behaviorContext->Class<PythonObjectConstructionTester>("TestConstruct")
- ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
- ->Attribute(AZ::Script::Attributes::Module, "test")
- ->Constructor<const AZStd::string&>()
- ->Constructor<AZ::s64>()
- ->Constructor<const AZStd::string&, AZ::s64>()
- ->Property("s64", [](PythonObjectConstructionTester* that) { return that->m_s64; }, nullptr)
- ->Property("text", &PythonObjectConstructionTester::GetText, nullptr)
- ;
- }
- }
- };
- class PythonObjectLambdaTester
- {
- public:
- int m_myInt = 42;
- public:
- AZ_TYPE_INFO(PythonObjectLambdaTester, "{E423E0ED-038F-4496-97D3-00932289AF72}");
- AZ_CLASS_ALLOCATOR(PythonObjectLambdaTester, AZ::SystemAllocator);
- void Reflect(AZ::ReflectContext* context)
- {
- if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
- {
- auto testLambda = [](PythonObjectLambdaTester* testerPtr) -> int { return testerPtr->m_myInt; };
- behaviorContext->Class<PythonObjectLambdaTester>("PythonObjectLambdaTester")
- ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
- ->Attribute(AZ::Script::Attributes::Module, "test.proxy")
- ->Method("testLambda", testLambda)
- ;
- }
- }
- };
- class PythonObjectReprTester
- {
- private:
- AZ::s64 m_value;
- AZStd::string GetRepr() const
- {
- return AZStd::string::format("TestReprObject(%lld)", m_value);
- }
- public:
- AZ_TYPE_INFO(PythonObjectReprTester, "{eb29174e-c556-4b35-8ee0-9ca5aa287420}");
- PythonObjectReprTester()
- {
- m_value = -1;
- }
- PythonObjectReprTester(AZ::s64 value) : m_value(value)
- {
- }
- AZStd::string ToString() const
- {
- return AZStd::string::format("MyValue: %lld", m_value);
- }
- void Reflect(AZ::ReflectContext* context)
- {
- if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
- {
- behaviorContext->Class<PythonObjectReprTester>("TestReprObject")
- ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
- ->Attribute(AZ::Script::Attributes::Module, "test.proxy")
- ->Property("value", [](PythonObjectReprTester* that) { return that->m_value; }, [](PythonObjectReprTester* that, AZ::s64 value) { that->m_value = value; })
- ->Method("__repr__", [](PythonObjectReprTester* that) { return that->GetRepr(); })
- ->Method("ToString", [](PythonObjectReprTester* that) { return that->ToString(); })
- ->Attribute(AZ::Script::Attributes::Operator, AZ::Script::Attributes::OperatorType::ToString)
- ->Constructor()
- ->Constructor<AZ::s64>()
- ;
- }
- }
- };
- //////////////////////////////////////////////////////////////////////////
- // fixtures
- struct PythonObjectProxyTests
- : public PythonTestingFixture
- {
- PythonTraceMessageSink m_testSink;
- void SetUp() override
- {
- PythonTestingFixture::SetUp();
- PythonTestingFixture::RegisterComponentDescriptors();
- }
- void TearDown() override
- {
- // clearing up memory
- m_testSink.CleanUp();
- PythonTestingFixture::TearDown();
- }
- };
- //////////////////////////////////////////////////////////////////////////
- // tests
- TEST_F(PythonObjectProxyTests, ObjectProxyProperties)
- {
- enum class LogTypes
- {
- Skip = 0,
- ReflectingObjectProxySimple_SetFloat,
- ReflectingObjectProxySimple_GetFloat,
- ReflectingObjectProxySimple_CreateTestObjectProxy,
- ReflectingObjectProxySimple_TestObjectProxyTypename,
- ReflectingObjectProxySimple_mySignedInt64,
- ReflectingObjectProxySimple_printedMessage
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::Equal(message, "ReflectingObjectProxySimple_CreateTestObjectProxy"))
- {
- return static_cast<int>(LogTypes::ReflectingObjectProxySimple_CreateTestObjectProxy);
- }
- else if (AzFramework::StringFunc::Equal(message, "ReflectingObjectProxySimple_GetFloat"))
- {
- return static_cast<int>(LogTypes::ReflectingObjectProxySimple_GetFloat);
- }
- else if (AzFramework::StringFunc::Equal(message, "ReflectingObjectProxySimple_SetFloat"))
- {
- return static_cast<int>(LogTypes::ReflectingObjectProxySimple_SetFloat);
- }
- else if (AzFramework::StringFunc::Equal(message, "ReflectingObjectProxySimple_TestObjectProxyTypename"))
- {
- return static_cast<int>(LogTypes::ReflectingObjectProxySimple_TestObjectProxyTypename);
- }
- else if (AzFramework::StringFunc::Equal(message, "ReflectingObjectProxySimple_mySignedInt64"))
- {
- return static_cast<int>(LogTypes::ReflectingObjectProxySimple_mySignedInt64);
- }
- else if (AzFramework::StringFunc::Equal(message, "ReflectingObjectProxySimple_printedMessage"))
- {
- return static_cast<int>(LogTypes::ReflectingObjectProxySimple_printedMessage);
- }
- }
- return static_cast<int>(LogTypes::Skip);
- };
- PythonReflectionObjectProxyPropertyTester pythonReflectionObjectProxyPropertyTester;
- pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetSerializeContext());
- pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- proxy = azlmbr.object.create('TestObjectProxy')
- if proxy is not None:
- print ('ReflectingObjectProxySimple_CreateTestObjectProxy')
- if proxy.typename == 'TestObjectProxy':
- print ('ReflectingObjectProxySimple_TestObjectProxyTypename')
- proxy.set_property('myFloat', 20.19)
- value = proxy.get_property('myFloat')
- print ('ReflectingObjectProxySimple_{}'.format(value))
- # int64
- proxy.set_property('mySignedInt64', 729)
- value = proxy.get_property('mySignedInt64')
- if value == 729:
- print ('ReflectingObjectProxySimple_mySignedInt64')
- value = proxy.invoke('printMessage', 'ReflectingObjectProxySimple_printedMessage')
- if (value == 42):
- print ('ReflectingObjectProxySimple_printedMessage')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ReflectingObjectProxySimple_CreateTestObjectProxy)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ReflectingObjectProxySimple_TestObjectProxyTypename)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ReflectingObjectProxySimple_GetFloat)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ReflectingObjectProxySimple_SetFloat)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ReflectingObjectProxySimple_mySignedInt64)]);
- EXPECT_EQ(2, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ReflectingObjectProxySimple_printedMessage)]);
- }
- TEST_F(PythonObjectProxyTests, AsNativelyUsed)
- {
- PythonReflectionObjectProxyTester pythonReflectionObjectProxyTester;
- pythonReflectionObjectProxyTester.Reflect(m_app.GetBehaviorContext());
- EditorPythonBindings::PythonProxyObject obj(azrtti_typeid<PythonReflectionObjectProxyTester>());
- EXPECT_STREQ(obj.GetWrappedTypeName(), "TestObject");
- EXPECT_TRUE(obj.GetWrappedType().has_value());
- EXPECT_EQ(obj.GetWrappedType(), azrtti_typeid<PythonReflectionObjectProxyTester>());
- }
- TEST_F(PythonObjectProxyTests, OutputTypes)
- {
- enum class LogTypes
- {
- Skip = 0,
- OutputTypes_ReturnCheck,
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::Equal(message, "OutputTypes_ReturnCheck"))
- {
- return static_cast<int>(LogTypes::OutputTypes_ReturnCheck);
- }
- }
- return static_cast<int>(LogTypes::Skip);
- };
- PythonReflectionObjectProxyPropertyTester pythonReflectionObjectProxyPropertyTester;
- pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetSerializeContext());
- pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- proxy = azlmbr.object.create('TestObjectProxy')
- fValue = proxy.get_property('f32')
- if(fValue == 32.0):
- print('OutputTypes_ReturnCheck')
-
- dValue = proxy.get_property('d64')
- if(dValue == 64.0):
- print('OutputTypes_ReturnCheck')
- typeList = [8, 16, 32, 64]
- for typeValue in typeList:
- signed = proxy.get_property('s{}'.format(str(typeValue)))
- if( (-signed) == typeValue):
- print('OutputTypes_ReturnCheck')
- unsigned = proxy.get_property('u{}'.format(str(typeValue)))
- if( unsigned == typeValue):
- print('OutputTypes_ReturnCheck')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(10, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::OutputTypes_ReturnCheck)]);
- }
- TEST_F(PythonObjectProxyTests, ObjectProxyFeatures)
- {
- enum class LogTypes
- {
- Skip = 0,
- ObjectProxyFeatures_ChangeType,
- ObjectProxyFeatures_TestObjectProxyTypename,
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::Equal(message, "ObjectProxyFeatures_ChangeType"))
- {
- return static_cast<int>(LogTypes::ObjectProxyFeatures_ChangeType);
- }
- else if (AzFramework::StringFunc::Equal(message, "ObjectProxyFeatures_TestObjectProxyTypename"))
- {
- return static_cast<int>(LogTypes::ObjectProxyFeatures_TestObjectProxyTypename);
- }
- }
- return static_cast<int>(LogTypes::Skip);
- };
- PythonReflectionObjectProxyPropertyTester pythonReflectionObjectProxyPropertyTester;
- pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetSerializeContext());
- pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetBehaviorContext());
- PythonReflectionObjectProxyTester pythonReflectionObjectProxyTester;
- pythonReflectionObjectProxyTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- proxy = azlmbr.object.create('TestObjectProxy')
- if proxy.typename == 'TestObjectProxy':
- print ('ObjectProxyFeatures_TestObjectProxyTypename')
- proxy.set_type('TestObject')
- value = proxy.invoke('doAdd', 2, 3)
- if (proxy.typename == 'TestObject') and (value == 5):
- print ('ObjectProxyFeatures_ChangeType')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ObjectProxyFeatures_ChangeType)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ObjectProxyFeatures_TestObjectProxyTypename)]);
- }
- TEST_F(PythonObjectProxyTests, DecoratedObjectProxy)
- {
- enum class LogTypes
- {
- Skip = 0,
- DidAdd,
- PropertyIsFish
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::Equal(message, "DidAdd"))
- {
- return static_cast<int>(LogTypes::DidAdd);
- }
- else if (AzFramework::StringFunc::Equal(message, "PropertyIsFish"))
- {
- return static_cast<int>(LogTypes::PropertyIsFish);
- }
- }
- return static_cast<int>(LogTypes::Skip);
- };
- PythonReflectionObjectProxyTester pythonReflectionObjectProxyTester;
- pythonReflectionObjectProxyTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- proxy = azlmbr.object.create('TestObject')
- value = proxy.doAdd(40, 2)
- if (value == 42):
- print ('DidAdd')
- proxy.myString = 'fish'
- if (proxy.myString == 'fish'):
- print ('PropertyIsFish')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::DidAdd)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PropertyIsFish)]);
- }
- TEST_F(PythonObjectProxyTests, DecoratedObjectProperties)
- {
- enum class LogTypes
- {
- Skip = 0,
- PropertyFetch,
- PropertySet,
- PropertyMatch,
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::Equal(message, "PropertyFetch"))
- {
- return static_cast<int>(LogTypes::PropertyFetch);
- }
- else if (AzFramework::StringFunc::Equal(message, "PropertySet"))
- {
- return static_cast<int>(LogTypes::PropertySet);
- }
- else if (AzFramework::StringFunc::Equal(message, "PropertyMatch"))
- {
- return static_cast<int>(LogTypes::PropertyMatch);
- }
- }
- return static_cast<int>(LogTypes::Skip);
- };
- PythonReflectionObjectProxyTester pythonReflectionObjectProxyTester;
- pythonReflectionObjectProxyTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- proxy = azlmbr.object.create('TestObject')
- value = proxy.theBuffer
- if (value == 'initial'):
- print ('PropertyFetch')
- theMatchValue = 'PropertySet'
- proxy.theBuffer = 'PropertySet'
- if (proxy.theBuffer == theMatchValue):
- print ('PropertyMatch')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PropertyFetch)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PropertySet)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PropertyMatch)]);
- }
- TEST_F(PythonObjectProxyTests, PythonicDecorations)
- {
- enum class LogTypes
- {
- Skip = 0,
- MethodGetAnswer,
- MethodSetAnswer,
- PropertyFetchAnswer,
- PropertyStoreAnswer
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::Equal(message, "MethodGetAnswer"))
- {
- return static_cast<int>(LogTypes::MethodGetAnswer);
- }
- else if (AzFramework::StringFunc::Equal(message, "MethodSetAnswer"))
- {
- return static_cast<int>(LogTypes::MethodSetAnswer);
- }
- else if (AzFramework::StringFunc::Equal(message, "PropertyFetchAnswer"))
- {
- return static_cast<int>(LogTypes::PropertyFetchAnswer);
- }
- else if (AzFramework::StringFunc::Equal(message, "PropertyStoreAnswer"))
- {
- return static_cast<int>(LogTypes::PropertyStoreAnswer);
- }
- }
- return static_cast<int>(LogTypes::Skip);
- };
- PythonReflectionObjectProxyTester pythonReflectionObjectProxyTester;
- pythonReflectionObjectProxyTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- proxy = azlmbr.object.create('TestObject')
- value = proxy.get_answer()
- if (value == 0):
- print ('MethodGetAnswer')
- proxy.set_answer(40)
- value = proxy.get_answer()
- if (value == 40):
- print ('MethodSetAnswer')
- if (proxy.answer == 40):
- print ('PropertyFetchAnswer')
- proxy.answer = proxy.answer + 2
- if (proxy.answer == 42):
- print ('PropertyStoreAnswer')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::MethodGetAnswer)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::MethodSetAnswer)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PropertyFetchAnswer)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PropertyStoreAnswer)]);
- }
- TEST_F(PythonObjectProxyTests, ObjectAzTypePassing)
- {
- enum class LogTypes
- {
- Skip = 0,
- ObjectAzTypePassing_Input,
- ObjectAzTypePassing_Output,
- ObjectAzTypePassing_EntityPassed
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::Equal(message, "this value:22, that value:11"))
- {
- return static_cast<int>(LogTypes::ObjectAzTypePassing_Input);
- }
- else if (AzFramework::StringFunc::Equal(message, "ObjectAzTypePassing_Output"))
- {
- return static_cast<int>(LogTypes::ObjectAzTypePassing_Output);
- }
- else if (AzFramework::StringFunc::Equal(message, "setting entity = [12237598]"))
- {
- return static_cast<int>(LogTypes::ObjectAzTypePassing_EntityPassed);
- }
- }
- return static_cast<int>(LogTypes::Skip);
- };
- PythonObjectBaseTester pythonObjectBaseTester;
- pythonObjectBaseTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- payload = azlmbr.object.PythonProxyObject('TestObjectBase')
- payload.set_property('value', 11)
- target = azlmbr.object.PythonProxyObject('TestObjectBase')
- target.set_property('value', 22)
- target.invoke('acceptAzType', payload)
- result = target.invoke('resultTest', 33)
- if(result.get_property('value') == 33):
- print ('ObjectAzTypePassing_Output')
- entityId = target.get_property('entityId')
- target.set_property('entityId', entityId)
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ObjectAzTypePassing_Input)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ObjectAzTypePassing_Output)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ObjectAzTypePassing_EntityPassed)]);
- }
- TEST_F(PythonObjectProxyTests, ConstructWithArgs)
- {
- enum class LogTypes
- {
- Skip = 0,
- ConstructWithDefault,
- ConstructWithInt,
- ConstructWithString,
- ConstructWithStringAndInt,
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::Equal(message, "ConstructWithDefault"))
- {
- return static_cast<int>(LogTypes::ConstructWithDefault);
- }
- else if (AzFramework::StringFunc::Equal(message, "ConstructWithInt"))
- {
- return static_cast<int>(LogTypes::ConstructWithInt);
- }
- else if (AzFramework::StringFunc::Equal(message, "ConstructWithString"))
- {
- return static_cast<int>(LogTypes::ConstructWithString);
- }
- else if (AzFramework::StringFunc::Equal(message, "ConstructWithStringAndInt"))
- {
- return static_cast<int>(LogTypes::ConstructWithStringAndInt);
- }
- }
- return static_cast<int>(LogTypes::Skip);
- };
- PythonObjectConstructionTester pythonObjectConstructionTester;
- pythonObjectConstructionTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- defaultObj = azlmbr.object.construct('TestConstruct')
- if defaultObj.get_property('text') == 'default':
- print ('ConstructWithDefault')
- defaultObj = azlmbr.object.construct('TestConstruct', 101)
- if defaultObj.get_property('text') == 'with_int':
- print ('ConstructWithInt')
- defaultObj = azlmbr.object.construct('TestConstruct', 'with_string')
- if defaultObj.get_property('text') == 'with_string':
- print ('ConstructWithString')
- defaultObj = azlmbr.object.construct('TestConstruct', 'foo', 201)
- if defaultObj.get_property('text') == 'foo':
- print ('ConstructWithStringAndInt')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ConstructWithDefault)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ConstructWithInt)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ConstructWithString)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ConstructWithStringAndInt)]);
- }
- TEST_F(PythonObjectProxyTests, PassByValue)
- {
- enum class LogTypes
- {
- Skip = 0,
- PassByValue_CreateReturnByValue,
- PassByValue_CallReturnByValue,
- PassByValue_InputByValue
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::Equal(message, "PassByValue_CreateReturnByValue"))
- {
- return static_cast<int>(LogTypes::PassByValue_CreateReturnByValue);
- }
- else if (AzFramework::StringFunc::Equal(message, "PassByValue_CallReturnByValue"))
- {
- return static_cast<int>(LogTypes::PassByValue_CallReturnByValue);
- }
- else if (AzFramework::StringFunc::Equal(message, "PassByValue_InputByValue"))
- {
- return static_cast<int>(LogTypes::PassByValue_InputByValue);
- }
- }
- return static_cast<int>(LogTypes::Skip);
- };
- if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(m_app.GetSerializeContext()))
- {
- serializeContext->RegisterGenericType<AZStd::vector<int>>();
- }
- EntityIdByValueTester::Reflect(m_app.GetBehaviorContext());
- PythonObjectBaseTester pythonObjectBaseTester;
- pythonObjectBaseTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.entity
- import azlmbr.object
- import azlmbr.test.proxy
- proxyEntityId = azlmbr.object.create('EntityIdByValueTester')
- if proxyEntityId.typename == 'EntityIdByValueTester':
- print ('PassByValue_CreateReturnByValue')
- testObjectBase = azlmbr.object.create('TestObjectBase')
- entityIdValue = testObjectBase.invoke('get_entity_id_by_value')
- if (entityIdValue.typename == 'EntityIdByValueTester'):
- print ('PassByValue_CallReturnByValue')
- if (testObjectBase.invoke('compare_entity_id_by_value', entityIdValue)):
- print ('PassByValue_InputByValue')
- intList = testObjectBase.invoke('return_vector_by_value')
- if (len(intList) == 3):
- print ('PassByValue_CallReturnByValue')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PassByValue_CreateReturnByValue)]);
- EXPECT_EQ(2, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PassByValue_CallReturnByValue)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PassByValue_InputByValue)]);
- }
- TEST_F(PythonObjectProxyTests, CallLambdaAsMember)
- {
- enum class LogTypes
- {
- Skip = 0,
- PythonObjectLambdaTester_CreateObject,
- PythonObjectLambdaTester_InvokeLambda
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::Equal(message, "PythonObjectLambdaTester_CreateObject"))
- {
- return static_cast<int>(LogTypes::PythonObjectLambdaTester_CreateObject);
- }
- else if (AzFramework::StringFunc::Equal(message, "PythonObjectLambdaTester_InvokeLambda"))
- {
- return static_cast<int>(LogTypes::PythonObjectLambdaTester_InvokeLambda);
- }
- }
- return static_cast<int>(LogTypes::Skip);
- };
- PythonObjectLambdaTester pythonObjectLambdaTester;
- pythonObjectLambdaTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- import azlmbr.test.proxy
- proxy = azlmbr.object.create('PythonObjectLambdaTester')
- if proxy is not None:
- print ('PythonObjectLambdaTester_CreateObject')
- value = proxy.invoke('testLambda')
- if (value == 42):
- print ('PythonObjectLambdaTester_InvokeLambda')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(1, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::PythonObjectLambdaTester_CreateObject)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::PythonObjectLambdaTester_InvokeLambda)]);
- }
- TEST_F(PythonObjectProxyTests, ObjectAsType)
- {
- enum class LogTypes
- {
- Skip = 0,
- Vector2_Non_None,
- Vector2_Constructed
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::Equal(message, "Vector2_Non_None"))
- {
- return aznumeric_cast<int>(LogTypes::Vector2_Non_None);
- }
- else if (AzFramework::StringFunc::Equal(message, "Vector2_Constructed"))
- {
- return aznumeric_cast<int>(LogTypes::Vector2_Constructed);
- }
- }
- return aznumeric_cast<int>(LogTypes::Skip);
- };
- PythonObjectLambdaTester pythonObjectLambdaTester;
- pythonObjectLambdaTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.math
- proxy = azlmbr.math.Vector2(40.0, 2.0)
- if proxy is not None:
- print ('Vector2_Non_None')
- value = proxy.x + proxy.y
- if (azlmbr.math.Math_IsClose(value, 42.0)):
- print ('Vector2_Constructed')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZStd::string failReason = AZStd::string::format("Failed on with Python exception: %s", e.what());
- //AZ_Warning("UnitTest", false, );
- GTEST_FATAL_FAILURE_(failReason.c_str());
- }
- e.Deactivate();
- EXPECT_EQ(1, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Vector2_Non_None)]);
- EXPECT_EQ(1, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Vector2_Constructed)]);
- }
- TEST_F(PythonObjectProxyTests, Crc32Type)
- {
- enum class LogTypes
- {
- Skip = 0,
- Crc32Type_Created,
- Crc32Type_Read
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::StartsWith(message, "Crc32Type_Created"))
- {
- return aznumeric_cast<int>(LogTypes::Crc32Type_Created);
- }
- else if (AzFramework::StringFunc::StartsWith(message, "Crc32Type_Read"))
- {
- return aznumeric_cast<int>(LogTypes::Crc32Type_Read);
- }
- }
- return aznumeric_cast<int>(LogTypes::Skip);
- };
- PythonObjectBaseTester pythonObjectBaseTester;
- pythonObjectBaseTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.math
- result = azlmbr.math.Crc32()
- if result is not None:
- print ('Crc32Type_Created_basic')
- result = azlmbr.math.Crc32('withstring')
- if result is not None:
- print ('Crc32Type_Created_withstring')
- if (result.value == 3101708170): # CRC32 of withstring
- print ('Crc32Type_Read_matches')
- if (azlmbr.math.Crc32('withstring').value == azlmbr.math.Crc32('WithString').value):
- print ('Crc32Type_Read_matches_with_mixed_string_cases')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(2, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Crc32Type_Created)]);
- EXPECT_EQ(2, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Crc32Type_Read)]);
- }
- TEST_F(PythonObjectProxyTests, ObjectDirectory)
- {
- enum class LogTypes
- {
- Skip = 0,
- Found
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::StartsWith(message, "Found"))
- {
- return aznumeric_cast<int>(LogTypes::Found);
- }
- }
- return aznumeric_cast<int>(LogTypes::Skip);
- };
- PythonReflectionObjectProxyTester pythonReflectionObjectProxyTester;
- pythonReflectionObjectProxyTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- import azlmbr.test.proxy
- proxyTest = azlmbr.test.proxy.TestObject()
- listOfAttributes = azlmbr.object.dir(proxyTest)
- if (listOfAttributes is not None):
- print ('Found_list')
- for attribute in listOfAttributes:
- if (attribute == 'doAdd'):
- print ('Found_{}'.format(attribute))
- elif (attribute == 'myString'):
- print ('Found_{}'.format(attribute))
- elif (attribute == 'theBuffer'):
- print ('Found_{}'.format(attribute))
- elif (attribute == 'get_answer'):
- print ('Found_{}'.format(attribute))
- elif (attribute == 'set_answer'):
- print ('Found_{}'.format(attribute))
- elif (attribute == 'answer'):
- print ('Found_{}'.format(attribute))
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(7, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Found)]);
- }
- TEST_F(PythonObjectProxyTests, EnumsAreFound)
- {
- enum class LogTypes
- {
- Skip = 0,
- Found,
- Equals
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::StartsWith(message, "Found"))
- {
- return aznumeric_cast<int>(LogTypes::Found);
- }
- else if (AzFramework::StringFunc::StartsWith(message, "Equals"))
- {
- return aznumeric_cast<int>(LogTypes::Equals);
- }
- }
- return aznumeric_cast<int>(LogTypes::Skip);
- };
- PythonReflectionObjectProxyPropertyTester pythonReflectionObjectProxyPropertyTester;
- pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetSerializeContext());
- pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- import azlmbr.test.proxy
- value = azlmbr.test.proxy.TestObjectProxy_Zero
- print ('Found_TestObjectProxy_Zero')
- if (value == 0):
- print ('Equals_TestObjectProxy_Zero is 0')
- value = azlmbr.test.proxy.TestObjectProxy_One
- print ('Found_TestObjectProxy_One')
- if (value == 1):
- print ('Equals_TestObjectProxy_One is 1')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Error("UnitTest", false, "Failed on with Python exception: %s", e.what());
- }
- e.Deactivate();
- EXPECT_EQ(2, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Found)]);
- EXPECT_EQ(2, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Equals)]);
- }
- TEST_F(PythonObjectProxyTests, ClassEnumsAreFound)
- {
- enum class LogTypes
- {
- Skip = 0,
- Assigned,
- Equals
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::StartsWith(message, "Assigned"))
- {
- return aznumeric_cast<int>(LogTypes::Assigned);
- }
- else if (AzFramework::StringFunc::StartsWith(message, "Equals"))
- {
- return aznumeric_cast<int>(LogTypes::Equals);
- }
- }
- return aznumeric_cast<int>(LogTypes::Skip);
- };
- PythonReflectionObjectProxyPropertyTester pythonReflectionObjectProxyPropertyTester;
- pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetSerializeContext());
- pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- import azlmbr.test.proxy
- value = azlmbr.test.proxy.TestObjectProxy_Zaphod
- print ('Assigned TestObjectProxy_Zaphod')
- if (value == 1):
- print ('Equals TestObjectProxy_Zaphod is 1')
- value = azlmbr.test.proxy.TestObjectProxy_Arthur
- print ('Assigned TestObjectProxy_Arthur')
- if (value == 2):
- print ('Equals TestObjectProxy_Arthur is 2')
- proxy = azlmbr.test.proxy.TestObjectProxy()
- enumValue = azlmbr.test.proxy.TestObjectProxy_Arthur
- proxyValue = proxy.enumClass
- if (proxyValue == enumValue):
- print ('Equals TestObjectProxy_Arthur is value.enumClass')
- proxy.enumClass = azlmbr.test.proxy.TestObjectProxy_Zaphod
- print ('Assigned TestObjectProxy_Zaphod to value.enumClass ({})'.format(proxy.enumClass))
- if (proxy.enumClass == azlmbr.test.proxy.TestObjectProxy_Zaphod):
- print ('Equals TestObjectProxy_Zaphod now is value.enumClass')
- )");
- }
- catch ([[maybe_unused]] const std::exception& e)
- {
- AZ_Error("UnitTest", false, "Failed on with Python exception: %s", e.what());
- }
- e.Deactivate();
- EXPECT_EQ(3, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Assigned)]);
- EXPECT_EQ(4, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Equals)]);
- }
- TEST_F(PythonObjectProxyTests, Repr)
- {
- enum class LogTypes
- {
- Skip = 0,
- Found
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::StartsWith(message, "Found"))
- {
- return aznumeric_cast<int>(LogTypes::Found);
- }
- }
- return aznumeric_cast<int>(LogTypes::Skip);
- };
- // class that implements repr
- PythonObjectReprTester reprTester;
- reprTester.Reflect(m_app.GetBehaviorContext());
- // class that doesn't implement repr
- PythonObjectBaseTester pythonObjectBaseTester;
- pythonObjectBaseTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- import azlmbr.test.proxy
- # test repr with a newly created object with default values
- proxyTest = azlmbr.object.create('TestReprObject')
- retrieved_repr = repr(proxyTest)
- if retrieved_repr == 'TestReprObject(-1)':
- print ('Found default repr')
- # test repr with different state, via constructor
- proxyTest = azlmbr.test.proxy.TestReprObject(42)
- retrieved_repr = repr(proxyTest)
- if retrieved_repr == 'TestReprObject(42)':
- print ('Found valid state repr')
- # test repr with a modified object
- proxyTest.value = 84
- retrieved_repr = repr(proxyTest)
- if retrieved_repr == 'TestReprObject(84)':
- print ('Found valid modified state repr')
- # test repr for objects that don't implement it
- proxyTest = azlmbr.test.proxy.TestObjectBase()
- retrieved_repr = repr(proxyTest)
- if retrieved_repr.startswith('<TestObjectBase via PythonProxyObject'):
- print ('Found fallback repr')
- )");
- }
- catch ([[maybe_unused]] const std::exception& ex)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", ex.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(4, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Found)]);
- }
- TEST_F(PythonObjectProxyTests, ToString)
- {
- enum class LogTypes
- {
- Skip = 0,
- Found
- };
- m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
- {
- if (AzFramework::StringFunc::Equal(window, "python"))
- {
- if (AzFramework::StringFunc::StartsWith(message, "Found"))
- {
- return aznumeric_cast<int>(LogTypes::Found);
- }
- }
- return aznumeric_cast<int>(LogTypes::Skip);
- };
- // class that implements ToString
- PythonObjectReprTester reprTester;
- reprTester.Reflect(m_app.GetBehaviorContext());
- // class that doesn't implement ToString
- PythonObjectBaseTester pythonObjectBaseTester;
- pythonObjectBaseTester.Reflect(m_app.GetBehaviorContext());
- AZ::Entity e;
- Activate(e);
- SimulateEditorBecomingInitialized();
- try
- {
- pybind11::exec(R"(
- import azlmbr.object
- import azlmbr.test.proxy
- # test str with a newly created object with default values
- proxyTest = azlmbr.object.create('TestReprObject')
- retrieved_str = str(proxyTest)
- if retrieved_str == 'MyValue: -1':
- print ('Found str')
- # test str with a modified object
- proxyTest.value = 42
- retrieved_str = str(proxyTest)
- if retrieved_str == 'MyValue: 42':
- print ('Found valid modified state str')
- # test str for objects that don't implement it, fallback to repr
- proxyTest = azlmbr.test.proxy.TestObjectBase()
- retrieved_str = str(proxyTest)
- retrieved_repr = repr(proxyTest)
- if retrieved_str == retrieved_repr:
- print ('Found fallback str based on repr')
- )");
- }
- catch ([[maybe_unused]] const std::exception& ex)
- {
- AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", ex.what());
- FAIL();
- }
- e.Deactivate();
- EXPECT_EQ(3, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Found)]);
- }
- }
|