PythonProxyObjectTests.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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/PythonProxyObject.h>
  16. #include <AzCore/Serialization/SerializeContext.h>
  17. #include <AzCore/RTTI/BehaviorContext.h>
  18. #include <AzCore/Casting/numeric_cast.h>
  19. #include <AzFramework/StringFunc/StringFunc.h>
  20. #include <AzCore/Component/Entity.h>
  21. namespace UnitTest
  22. {
  23. enum class TestEnumClass : AZ::s32
  24. {
  25. Zaphod = 1,
  26. Arthur
  27. };
  28. }
  29. // give the enum values types
  30. namespace AZ
  31. {
  32. AZ_TYPE_INFO_SPECIALIZE(UnitTest::TestEnumClass, "{F8EBD52A-D508-4A37-81CA-40E1DC176BCC}")
  33. }
  34. namespace UnitTest
  35. {
  36. //////////////////////////////////////////////////////////////////////////
  37. // test class/struts
  38. struct PythonReflectionObjectProxyPropertyTester
  39. {
  40. AZ_TYPE_INFO(PythonReflectionObjectProxyPropertyTester, "{F7966C89-7671-43F1-9DA2-677898DACED1}");
  41. float m_myFloat = 0.0f;
  42. AZ::s64 m_s64 = 0;
  43. AZ::s32 m_s32 = 0;
  44. TestEnumClass m_enumClass = TestEnumClass::Arthur;
  45. float GetFloat() const
  46. {
  47. AZ_TracePrintf("python", "ReflectingObjectProxySimple_GetFloat");
  48. return m_myFloat;
  49. }
  50. void SetFloat(float value)
  51. {
  52. AZ_TracePrintf("python", "ReflectingObjectProxySimple_SetFloat");
  53. m_myFloat = value;
  54. }
  55. AZ::u16 PrintMessage(const char* message)
  56. {
  57. AZ_TracePrintf("python", message);
  58. return static_cast<AZ::u16>(::strlen(message));
  59. }
  60. enum TestEnum
  61. {
  62. TE_Zero = 0,
  63. TE_One = 1,
  64. };
  65. void Reflect(AZ::ReflectContext* context)
  66. {
  67. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  68. {
  69. serializeContext->Enum<TestEnumClass>()
  70. ->Value("Zaphod", TestEnumClass::Zaphod)
  71. ->Value("Arthur", TestEnumClass::Arthur)
  72. ;
  73. serializeContext->Class<PythonReflectionObjectProxyPropertyTester>()
  74. ->Field("EnumClass", &PythonReflectionObjectProxyPropertyTester::m_enumClass)
  75. ->Field("Int32", &PythonReflectionObjectProxyPropertyTester::m_s32)
  76. ;
  77. }
  78. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  79. {
  80. behaviorContext->Class<PythonReflectionObjectProxyPropertyTester>("TestObjectProxy")
  81. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  82. ->Attribute(AZ::Script::Attributes::Module, "test.proxy")
  83. ->Property("myFloat", [](PythonReflectionObjectProxyPropertyTester* that) { return that->GetFloat(); }, [](PythonReflectionObjectProxyPropertyTester* that, float value) { that->SetFloat(value); })
  84. ->Property("mySignedInt64", [](PythonReflectionObjectProxyPropertyTester* that) { return that->m_s64; }, [](PythonReflectionObjectProxyPropertyTester* that, AZ::s64 value) { that->m_s64 = value; })
  85. ->Property("s8", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::s8>(-8); }, nullptr)
  86. ->Property("u8", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::u8>(8); }, nullptr)
  87. ->Property("s16", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::s16>(-16); }, nullptr)
  88. ->Property("u16", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::u16>(16); }, nullptr)
  89. ->Property("s32", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::s32>(-32); }, nullptr)
  90. ->Property("u32", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::u32>(32); }, nullptr)
  91. ->Property("s64", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::s64>(-64); }, nullptr)
  92. ->Property("u64", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<AZ::u64>(64); }, nullptr)
  93. ->Property("f32", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<float>(32.0f); }, nullptr)
  94. ->Property("d64", []([[maybe_unused]] PythonReflectionObjectProxyPropertyTester* that) { return static_cast<double>(64.0); }, nullptr)
  95. ->Method("printMessage", &PythonReflectionObjectProxyPropertyTester::PrintMessage)
  96. ->Enum<TE_Zero>("Zero")
  97. ->Enum<TE_One>("One")
  98. ->Property("enumClass", BehaviorValueProperty(&PythonReflectionObjectProxyPropertyTester::m_enumClass))
  99. ->Enum<aznumeric_cast<AZ::s32>(TestEnumClass::Zaphod)>("Zaphod")
  100. ->Enum<aznumeric_cast<AZ::s32>(TestEnumClass::Arthur)>("Arthur")
  101. ;
  102. }
  103. }
  104. };
  105. struct PythonReflectionObjectProxyTester
  106. {
  107. AZ_TYPE_INFO(PythonReflectionObjectProxyTester, "{4FC01B6B-D738-46AD-BF74-6F72506DD9B1}");
  108. int DoAdd(int a, int b)
  109. {
  110. return a + b;
  111. }
  112. AZStd::string_view m_testString;
  113. AZStd::string m_testBuffer = "initial";
  114. AZ::s32 m_answer = 0;
  115. void SetBuffer(AZStd::string_view buffer)
  116. {
  117. m_testBuffer = buffer;
  118. AZ_TracePrintf("python", m_testBuffer.c_str());
  119. }
  120. AZStd::string_view GetBuffer() const
  121. {
  122. return AZStd::string_view{ m_testBuffer };
  123. }
  124. AZ::s32 GetAnswer() const
  125. {
  126. return m_answer;
  127. }
  128. void SetAnswer(AZ::s32 value)
  129. {
  130. m_answer = value;
  131. }
  132. void Reflect(AZ::ReflectContext* context)
  133. {
  134. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  135. {
  136. behaviorContext->Class<PythonReflectionObjectProxyTester>("TestObject")
  137. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  138. ->Attribute(AZ::Script::Attributes::Module, "test.proxy")
  139. ->Method("doAdd", &PythonReflectionObjectProxyTester::DoAdd)
  140. ->Property("myString", [](PythonReflectionObjectProxyTester* that) { return that->m_testString; }, [](PythonReflectionObjectProxyTester* that, AZStd::string_view value) { that->m_testString = value; })
  141. ->Property("theBuffer", &PythonReflectionObjectProxyTester::GetBuffer, &PythonReflectionObjectProxyTester::SetBuffer)
  142. ->Method("GetAnswer", &PythonReflectionObjectProxyTester::GetAnswer)
  143. ->Attribute(AZ::Script::Attributes::Alias, "get_answer")
  144. ->Method("SetAnswer", &PythonReflectionObjectProxyTester::SetAnswer)
  145. ->Attribute(AZ::Script::Attributes::Alias, "set_answer")
  146. ->Property("Answer", &PythonReflectionObjectProxyTester::GetAnswer, &PythonReflectionObjectProxyTester::SetAnswer)
  147. ->Attribute(AZ::Script::Attributes::Alias, "answer")
  148. ;
  149. }
  150. }
  151. };
  152. struct EntityIdByValueTester
  153. : public AZ::EntityId
  154. {
  155. AZ_TYPE_INFO(EntityIdByValueTester, "{DE8A9968-B6E1-49D1-86B4-8DC946AC3FC7}");
  156. AZ_CLASS_ALLOCATOR(EntityIdByValueTester, AZ::SystemAllocator);
  157. EntityIdByValueTester() = default;
  158. explicit EntityIdByValueTester(AZ::u64 id)
  159. : AZ::EntityId(id)
  160. {}
  161. static void Reflect(AZ::ReflectContext* context)
  162. {
  163. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  164. {
  165. behaviorContext->Class<EntityIdByValueTester>("EntityIdByValueTester")
  166. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  167. ->Attribute(AZ::Script::Attributes::Module, "entity")
  168. ->Method("is_valid", &EntityId::IsValid)
  169. ->Method("to_string", &EntityId::ToString)
  170. ->Method("equal", &EntityId::operator==)
  171. ;
  172. }
  173. }
  174. };
  175. class PythonObjectBaseTester
  176. {
  177. private:
  178. AZ::s64 m_value = -1;
  179. AZ::EntityId m_entityId;
  180. EntityIdByValueTester m_testerId;
  181. public:
  182. AZ_TYPE_INFO(PythonObjectBaseTester, "{99978809-BB9F-4915-81B2-E44DF0C59A9E}");
  183. AZ_CLASS_ALLOCATOR(PythonObjectBaseTester, AZ::SystemAllocator);
  184. PythonObjectBaseTester()
  185. {
  186. m_entityId = AZ::EntityId(0xbabb1e);
  187. m_testerId = EntityIdByValueTester(0x1010);
  188. }
  189. void AcceptAzType(PythonObjectBaseTester* that)
  190. {
  191. AZ_TracePrintf("python", "this value:%d, that value:%d", m_value, that->m_value);
  192. m_value = that->m_value;
  193. }
  194. PythonObjectBaseTester* ResultTest(int value)
  195. {
  196. PythonObjectBaseTester* tester = aznew PythonObjectBaseTester{};
  197. tester->m_value = value;
  198. return tester;
  199. }
  200. void SetEntityId(const AZ::EntityId& value)
  201. {
  202. m_entityId = value;
  203. AZ_TracePrintf("python", "setting entity = %s", m_entityId.ToString().c_str() );
  204. }
  205. const AZ::EntityId& GetEntityId() const
  206. {
  207. return m_entityId;
  208. }
  209. EntityIdByValueTester GetEntityIdByValue()
  210. {
  211. return m_testerId;
  212. }
  213. bool CompareEntityIdByValue(EntityIdByValueTester entityId)
  214. {
  215. return m_testerId == entityId;
  216. }
  217. AZStd::vector<int> ReturnVectorByValue()
  218. {
  219. AZStd::vector<int> aTemp;
  220. aTemp.push_back(1);
  221. aTemp.push_back(2);
  222. aTemp.push_back(3);
  223. return aTemp;
  224. }
  225. void Reflect(AZ::ReflectContext* context)
  226. {
  227. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  228. {
  229. behaviorContext->Class<PythonObjectBaseTester>("TestObjectBase")
  230. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  231. ->Attribute(AZ::Script::Attributes::Module, "test.proxy")
  232. ->Property("value", [](PythonObjectBaseTester* that) { return that->m_value; }, [](PythonObjectBaseTester* that, AZ::s64 value) { that->m_value = value; })
  233. ->Property("entityId", &PythonObjectBaseTester::GetEntityId, &PythonObjectBaseTester::SetEntityId)
  234. ->Method("acceptAzType", &PythonObjectBaseTester::AcceptAzType)
  235. ->Method("resultTest", &PythonObjectBaseTester::ResultTest)
  236. ->Method("get_entity_id_by_value", &PythonObjectBaseTester::GetEntityIdByValue)
  237. ->Method("compare_entity_id_by_value", &PythonObjectBaseTester::CompareEntityIdByValue)
  238. ->Method("return_vector_by_value", &PythonObjectBaseTester::ReturnVectorByValue)
  239. ;
  240. }
  241. }
  242. };
  243. class PythonObjectConstructionTester
  244. {
  245. private:
  246. AZ::s64 m_s64 = 0;
  247. AZStd::string m_text;
  248. public:
  249. AZ_TYPE_INFO(PythonObjectConstructionTester, "{35F7EE10-CA36-4F77-95B5-8001BA384E5A}");
  250. AZ_CLASS_ALLOCATOR(PythonObjectConstructionTester, AZ::SystemAllocator);
  251. PythonObjectConstructionTester()
  252. {
  253. m_text = "default";
  254. }
  255. PythonObjectConstructionTester(const AZStd::string& textValue)
  256. {
  257. m_text = textValue;
  258. }
  259. PythonObjectConstructionTester(AZ::s64 longValue)
  260. {
  261. m_text = "with_int";
  262. m_s64 = longValue;
  263. }
  264. PythonObjectConstructionTester(const AZStd::string& textValue, AZ::s64 longValue)
  265. {
  266. m_text = textValue;
  267. m_s64 = longValue;
  268. }
  269. const AZStd::string& GetText() const
  270. {
  271. return m_text;
  272. }
  273. void Reflect(AZ::ReflectContext* context)
  274. {
  275. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  276. {
  277. behaviorContext->Class<PythonObjectConstructionTester>("TestConstruct")
  278. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  279. ->Attribute(AZ::Script::Attributes::Module, "test")
  280. ->Constructor<const AZStd::string&>()
  281. ->Constructor<AZ::s64>()
  282. ->Constructor<const AZStd::string&, AZ::s64>()
  283. ->Property("s64", [](PythonObjectConstructionTester* that) { return that->m_s64; }, nullptr)
  284. ->Property("text", &PythonObjectConstructionTester::GetText, nullptr)
  285. ;
  286. }
  287. }
  288. };
  289. class PythonObjectLambdaTester
  290. {
  291. public:
  292. int m_myInt = 42;
  293. public:
  294. AZ_TYPE_INFO(PythonObjectLambdaTester, "{E423E0ED-038F-4496-97D3-00932289AF72}");
  295. AZ_CLASS_ALLOCATOR(PythonObjectLambdaTester, AZ::SystemAllocator);
  296. void Reflect(AZ::ReflectContext* context)
  297. {
  298. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  299. {
  300. auto testLambda = [](PythonObjectLambdaTester* testerPtr) -> int { return testerPtr->m_myInt; };
  301. behaviorContext->Class<PythonObjectLambdaTester>("PythonObjectLambdaTester")
  302. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  303. ->Attribute(AZ::Script::Attributes::Module, "test.proxy")
  304. ->Method("testLambda", testLambda)
  305. ;
  306. }
  307. }
  308. };
  309. class PythonObjectReprTester
  310. {
  311. private:
  312. AZ::s64 m_value;
  313. AZStd::string GetRepr() const
  314. {
  315. return AZStd::string::format("TestReprObject(%lld)", m_value);
  316. }
  317. public:
  318. AZ_TYPE_INFO(PythonObjectReprTester, "{eb29174e-c556-4b35-8ee0-9ca5aa287420}");
  319. PythonObjectReprTester()
  320. {
  321. m_value = -1;
  322. }
  323. PythonObjectReprTester(AZ::s64 value) : m_value(value)
  324. {
  325. }
  326. AZStd::string ToString() const
  327. {
  328. return AZStd::string::format("MyValue: %lld", m_value);
  329. }
  330. void Reflect(AZ::ReflectContext* context)
  331. {
  332. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  333. {
  334. behaviorContext->Class<PythonObjectReprTester>("TestReprObject")
  335. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
  336. ->Attribute(AZ::Script::Attributes::Module, "test.proxy")
  337. ->Property("value", [](PythonObjectReprTester* that) { return that->m_value; }, [](PythonObjectReprTester* that, AZ::s64 value) { that->m_value = value; })
  338. ->Method("__repr__", [](PythonObjectReprTester* that) { return that->GetRepr(); })
  339. ->Method("ToString", [](PythonObjectReprTester* that) { return that->ToString(); })
  340. ->Attribute(AZ::Script::Attributes::Operator, AZ::Script::Attributes::OperatorType::ToString)
  341. ->Constructor()
  342. ->Constructor<AZ::s64>()
  343. ;
  344. }
  345. }
  346. };
  347. //////////////////////////////////////////////////////////////////////////
  348. // fixtures
  349. struct PythonObjectProxyTests
  350. : public PythonTestingFixture
  351. {
  352. PythonTraceMessageSink m_testSink;
  353. void SetUp() override
  354. {
  355. PythonTestingFixture::SetUp();
  356. PythonTestingFixture::RegisterComponentDescriptors();
  357. }
  358. void TearDown() override
  359. {
  360. // clearing up memory
  361. m_testSink.CleanUp();
  362. PythonTestingFixture::TearDown();
  363. }
  364. };
  365. //////////////////////////////////////////////////////////////////////////
  366. // tests
  367. TEST_F(PythonObjectProxyTests, ObjectProxyProperties)
  368. {
  369. enum class LogTypes
  370. {
  371. Skip = 0,
  372. ReflectingObjectProxySimple_SetFloat,
  373. ReflectingObjectProxySimple_GetFloat,
  374. ReflectingObjectProxySimple_CreateTestObjectProxy,
  375. ReflectingObjectProxySimple_TestObjectProxyTypename,
  376. ReflectingObjectProxySimple_mySignedInt64,
  377. ReflectingObjectProxySimple_printedMessage
  378. };
  379. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  380. {
  381. if (AzFramework::StringFunc::Equal(window, "python"))
  382. {
  383. if (AzFramework::StringFunc::Equal(message, "ReflectingObjectProxySimple_CreateTestObjectProxy"))
  384. {
  385. return static_cast<int>(LogTypes::ReflectingObjectProxySimple_CreateTestObjectProxy);
  386. }
  387. else if (AzFramework::StringFunc::Equal(message, "ReflectingObjectProxySimple_GetFloat"))
  388. {
  389. return static_cast<int>(LogTypes::ReflectingObjectProxySimple_GetFloat);
  390. }
  391. else if (AzFramework::StringFunc::Equal(message, "ReflectingObjectProxySimple_SetFloat"))
  392. {
  393. return static_cast<int>(LogTypes::ReflectingObjectProxySimple_SetFloat);
  394. }
  395. else if (AzFramework::StringFunc::Equal(message, "ReflectingObjectProxySimple_TestObjectProxyTypename"))
  396. {
  397. return static_cast<int>(LogTypes::ReflectingObjectProxySimple_TestObjectProxyTypename);
  398. }
  399. else if (AzFramework::StringFunc::Equal(message, "ReflectingObjectProxySimple_mySignedInt64"))
  400. {
  401. return static_cast<int>(LogTypes::ReflectingObjectProxySimple_mySignedInt64);
  402. }
  403. else if (AzFramework::StringFunc::Equal(message, "ReflectingObjectProxySimple_printedMessage"))
  404. {
  405. return static_cast<int>(LogTypes::ReflectingObjectProxySimple_printedMessage);
  406. }
  407. }
  408. return static_cast<int>(LogTypes::Skip);
  409. };
  410. PythonReflectionObjectProxyPropertyTester pythonReflectionObjectProxyPropertyTester;
  411. pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetSerializeContext());
  412. pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetBehaviorContext());
  413. AZ::Entity e;
  414. Activate(e);
  415. SimulateEditorBecomingInitialized();
  416. try
  417. {
  418. pybind11::exec(R"(
  419. import azlmbr.object
  420. proxy = azlmbr.object.create('TestObjectProxy')
  421. if proxy is not None:
  422. print ('ReflectingObjectProxySimple_CreateTestObjectProxy')
  423. if proxy.typename == 'TestObjectProxy':
  424. print ('ReflectingObjectProxySimple_TestObjectProxyTypename')
  425. proxy.set_property('myFloat', 20.19)
  426. value = proxy.get_property('myFloat')
  427. print ('ReflectingObjectProxySimple_{}'.format(value))
  428. # int64
  429. proxy.set_property('mySignedInt64', 729)
  430. value = proxy.get_property('mySignedInt64')
  431. if value == 729:
  432. print ('ReflectingObjectProxySimple_mySignedInt64')
  433. value = proxy.invoke('printMessage', 'ReflectingObjectProxySimple_printedMessage')
  434. if (value == 42):
  435. print ('ReflectingObjectProxySimple_printedMessage')
  436. )");
  437. }
  438. catch ([[maybe_unused]] const std::exception& e)
  439. {
  440. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
  441. FAIL();
  442. }
  443. e.Deactivate();
  444. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ReflectingObjectProxySimple_CreateTestObjectProxy)]);
  445. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ReflectingObjectProxySimple_TestObjectProxyTypename)]);
  446. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ReflectingObjectProxySimple_GetFloat)]);
  447. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ReflectingObjectProxySimple_SetFloat)]);
  448. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ReflectingObjectProxySimple_mySignedInt64)]);
  449. EXPECT_EQ(2, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ReflectingObjectProxySimple_printedMessage)]);
  450. }
  451. TEST_F(PythonObjectProxyTests, AsNativelyUsed)
  452. {
  453. PythonReflectionObjectProxyTester pythonReflectionObjectProxyTester;
  454. pythonReflectionObjectProxyTester.Reflect(m_app.GetBehaviorContext());
  455. EditorPythonBindings::PythonProxyObject obj(azrtti_typeid<PythonReflectionObjectProxyTester>());
  456. EXPECT_STREQ(obj.GetWrappedTypeName(), "TestObject");
  457. EXPECT_TRUE(obj.GetWrappedType().has_value());
  458. EXPECT_EQ(obj.GetWrappedType(), azrtti_typeid<PythonReflectionObjectProxyTester>());
  459. }
  460. TEST_F(PythonObjectProxyTests, OutputTypes)
  461. {
  462. enum class LogTypes
  463. {
  464. Skip = 0,
  465. OutputTypes_ReturnCheck,
  466. };
  467. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  468. {
  469. if (AzFramework::StringFunc::Equal(window, "python"))
  470. {
  471. if (AzFramework::StringFunc::Equal(message, "OutputTypes_ReturnCheck"))
  472. {
  473. return static_cast<int>(LogTypes::OutputTypes_ReturnCheck);
  474. }
  475. }
  476. return static_cast<int>(LogTypes::Skip);
  477. };
  478. PythonReflectionObjectProxyPropertyTester pythonReflectionObjectProxyPropertyTester;
  479. pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetSerializeContext());
  480. pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetBehaviorContext());
  481. AZ::Entity e;
  482. Activate(e);
  483. SimulateEditorBecomingInitialized();
  484. try
  485. {
  486. pybind11::exec(R"(
  487. import azlmbr.object
  488. proxy = azlmbr.object.create('TestObjectProxy')
  489. fValue = proxy.get_property('f32')
  490. if(fValue == 32.0):
  491. print('OutputTypes_ReturnCheck')
  492. dValue = proxy.get_property('d64')
  493. if(dValue == 64.0):
  494. print('OutputTypes_ReturnCheck')
  495. typeList = [8, 16, 32, 64]
  496. for typeValue in typeList:
  497. signed = proxy.get_property('s{}'.format(str(typeValue)))
  498. if( (-signed) == typeValue):
  499. print('OutputTypes_ReturnCheck')
  500. unsigned = proxy.get_property('u{}'.format(str(typeValue)))
  501. if( unsigned == typeValue):
  502. print('OutputTypes_ReturnCheck')
  503. )");
  504. }
  505. catch ([[maybe_unused]] const std::exception& e)
  506. {
  507. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
  508. FAIL();
  509. }
  510. e.Deactivate();
  511. EXPECT_EQ(10, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::OutputTypes_ReturnCheck)]);
  512. }
  513. TEST_F(PythonObjectProxyTests, ObjectProxyFeatures)
  514. {
  515. enum class LogTypes
  516. {
  517. Skip = 0,
  518. ObjectProxyFeatures_ChangeType,
  519. ObjectProxyFeatures_TestObjectProxyTypename,
  520. };
  521. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  522. {
  523. if (AzFramework::StringFunc::Equal(window, "python"))
  524. {
  525. if (AzFramework::StringFunc::Equal(message, "ObjectProxyFeatures_ChangeType"))
  526. {
  527. return static_cast<int>(LogTypes::ObjectProxyFeatures_ChangeType);
  528. }
  529. else if (AzFramework::StringFunc::Equal(message, "ObjectProxyFeatures_TestObjectProxyTypename"))
  530. {
  531. return static_cast<int>(LogTypes::ObjectProxyFeatures_TestObjectProxyTypename);
  532. }
  533. }
  534. return static_cast<int>(LogTypes::Skip);
  535. };
  536. PythonReflectionObjectProxyPropertyTester pythonReflectionObjectProxyPropertyTester;
  537. pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetSerializeContext());
  538. pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetBehaviorContext());
  539. PythonReflectionObjectProxyTester pythonReflectionObjectProxyTester;
  540. pythonReflectionObjectProxyTester.Reflect(m_app.GetBehaviorContext());
  541. AZ::Entity e;
  542. Activate(e);
  543. SimulateEditorBecomingInitialized();
  544. try
  545. {
  546. pybind11::exec(R"(
  547. import azlmbr.object
  548. proxy = azlmbr.object.create('TestObjectProxy')
  549. if proxy.typename == 'TestObjectProxy':
  550. print ('ObjectProxyFeatures_TestObjectProxyTypename')
  551. proxy.set_type('TestObject')
  552. value = proxy.invoke('doAdd', 2, 3)
  553. if (proxy.typename == 'TestObject') and (value == 5):
  554. print ('ObjectProxyFeatures_ChangeType')
  555. )");
  556. }
  557. catch ([[maybe_unused]] const std::exception& e)
  558. {
  559. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
  560. FAIL();
  561. }
  562. e.Deactivate();
  563. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ObjectProxyFeatures_ChangeType)]);
  564. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ObjectProxyFeatures_TestObjectProxyTypename)]);
  565. }
  566. TEST_F(PythonObjectProxyTests, DecoratedObjectProxy)
  567. {
  568. enum class LogTypes
  569. {
  570. Skip = 0,
  571. DidAdd,
  572. PropertyIsFish
  573. };
  574. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  575. {
  576. if (AzFramework::StringFunc::Equal(window, "python"))
  577. {
  578. if (AzFramework::StringFunc::Equal(message, "DidAdd"))
  579. {
  580. return static_cast<int>(LogTypes::DidAdd);
  581. }
  582. else if (AzFramework::StringFunc::Equal(message, "PropertyIsFish"))
  583. {
  584. return static_cast<int>(LogTypes::PropertyIsFish);
  585. }
  586. }
  587. return static_cast<int>(LogTypes::Skip);
  588. };
  589. PythonReflectionObjectProxyTester pythonReflectionObjectProxyTester;
  590. pythonReflectionObjectProxyTester.Reflect(m_app.GetBehaviorContext());
  591. AZ::Entity e;
  592. Activate(e);
  593. SimulateEditorBecomingInitialized();
  594. try
  595. {
  596. pybind11::exec(R"(
  597. import azlmbr.object
  598. proxy = azlmbr.object.create('TestObject')
  599. value = proxy.doAdd(40, 2)
  600. if (value == 42):
  601. print ('DidAdd')
  602. proxy.myString = 'fish'
  603. if (proxy.myString == 'fish'):
  604. print ('PropertyIsFish')
  605. )");
  606. }
  607. catch ([[maybe_unused]] const std::exception& e)
  608. {
  609. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
  610. FAIL();
  611. }
  612. e.Deactivate();
  613. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::DidAdd)]);
  614. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PropertyIsFish)]);
  615. }
  616. TEST_F(PythonObjectProxyTests, DecoratedObjectProperties)
  617. {
  618. enum class LogTypes
  619. {
  620. Skip = 0,
  621. PropertyFetch,
  622. PropertySet,
  623. PropertyMatch,
  624. };
  625. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  626. {
  627. if (AzFramework::StringFunc::Equal(window, "python"))
  628. {
  629. if (AzFramework::StringFunc::Equal(message, "PropertyFetch"))
  630. {
  631. return static_cast<int>(LogTypes::PropertyFetch);
  632. }
  633. else if (AzFramework::StringFunc::Equal(message, "PropertySet"))
  634. {
  635. return static_cast<int>(LogTypes::PropertySet);
  636. }
  637. else if (AzFramework::StringFunc::Equal(message, "PropertyMatch"))
  638. {
  639. return static_cast<int>(LogTypes::PropertyMatch);
  640. }
  641. }
  642. return static_cast<int>(LogTypes::Skip);
  643. };
  644. PythonReflectionObjectProxyTester pythonReflectionObjectProxyTester;
  645. pythonReflectionObjectProxyTester.Reflect(m_app.GetBehaviorContext());
  646. AZ::Entity e;
  647. Activate(e);
  648. SimulateEditorBecomingInitialized();
  649. try
  650. {
  651. pybind11::exec(R"(
  652. import azlmbr.object
  653. proxy = azlmbr.object.create('TestObject')
  654. value = proxy.theBuffer
  655. if (value == 'initial'):
  656. print ('PropertyFetch')
  657. theMatchValue = 'PropertySet'
  658. proxy.theBuffer = 'PropertySet'
  659. if (proxy.theBuffer == theMatchValue):
  660. print ('PropertyMatch')
  661. )");
  662. }
  663. catch ([[maybe_unused]] const std::exception& e)
  664. {
  665. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
  666. FAIL();
  667. }
  668. e.Deactivate();
  669. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PropertyFetch)]);
  670. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PropertySet)]);
  671. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PropertyMatch)]);
  672. }
  673. TEST_F(PythonObjectProxyTests, PythonicDecorations)
  674. {
  675. enum class LogTypes
  676. {
  677. Skip = 0,
  678. MethodGetAnswer,
  679. MethodSetAnswer,
  680. PropertyFetchAnswer,
  681. PropertyStoreAnswer
  682. };
  683. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  684. {
  685. if (AzFramework::StringFunc::Equal(window, "python"))
  686. {
  687. if (AzFramework::StringFunc::Equal(message, "MethodGetAnswer"))
  688. {
  689. return static_cast<int>(LogTypes::MethodGetAnswer);
  690. }
  691. else if (AzFramework::StringFunc::Equal(message, "MethodSetAnswer"))
  692. {
  693. return static_cast<int>(LogTypes::MethodSetAnswer);
  694. }
  695. else if (AzFramework::StringFunc::Equal(message, "PropertyFetchAnswer"))
  696. {
  697. return static_cast<int>(LogTypes::PropertyFetchAnswer);
  698. }
  699. else if (AzFramework::StringFunc::Equal(message, "PropertyStoreAnswer"))
  700. {
  701. return static_cast<int>(LogTypes::PropertyStoreAnswer);
  702. }
  703. }
  704. return static_cast<int>(LogTypes::Skip);
  705. };
  706. PythonReflectionObjectProxyTester pythonReflectionObjectProxyTester;
  707. pythonReflectionObjectProxyTester.Reflect(m_app.GetBehaviorContext());
  708. AZ::Entity e;
  709. Activate(e);
  710. SimulateEditorBecomingInitialized();
  711. try
  712. {
  713. pybind11::exec(R"(
  714. import azlmbr.object
  715. proxy = azlmbr.object.create('TestObject')
  716. value = proxy.get_answer()
  717. if (value == 0):
  718. print ('MethodGetAnswer')
  719. proxy.set_answer(40)
  720. value = proxy.get_answer()
  721. if (value == 40):
  722. print ('MethodSetAnswer')
  723. if (proxy.answer == 40):
  724. print ('PropertyFetchAnswer')
  725. proxy.answer = proxy.answer + 2
  726. if (proxy.answer == 42):
  727. print ('PropertyStoreAnswer')
  728. )");
  729. }
  730. catch ([[maybe_unused]] const std::exception& e)
  731. {
  732. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
  733. FAIL();
  734. }
  735. e.Deactivate();
  736. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::MethodGetAnswer)]);
  737. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::MethodSetAnswer)]);
  738. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PropertyFetchAnswer)]);
  739. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PropertyStoreAnswer)]);
  740. }
  741. TEST_F(PythonObjectProxyTests, ObjectAzTypePassing)
  742. {
  743. enum class LogTypes
  744. {
  745. Skip = 0,
  746. ObjectAzTypePassing_Input,
  747. ObjectAzTypePassing_Output,
  748. ObjectAzTypePassing_EntityPassed
  749. };
  750. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  751. {
  752. if (AzFramework::StringFunc::Equal(window, "python"))
  753. {
  754. if (AzFramework::StringFunc::Equal(message, "this value:22, that value:11"))
  755. {
  756. return static_cast<int>(LogTypes::ObjectAzTypePassing_Input);
  757. }
  758. else if (AzFramework::StringFunc::Equal(message, "ObjectAzTypePassing_Output"))
  759. {
  760. return static_cast<int>(LogTypes::ObjectAzTypePassing_Output);
  761. }
  762. else if (AzFramework::StringFunc::Equal(message, "setting entity = [12237598]"))
  763. {
  764. return static_cast<int>(LogTypes::ObjectAzTypePassing_EntityPassed);
  765. }
  766. }
  767. return static_cast<int>(LogTypes::Skip);
  768. };
  769. PythonObjectBaseTester pythonObjectBaseTester;
  770. pythonObjectBaseTester.Reflect(m_app.GetBehaviorContext());
  771. AZ::Entity e;
  772. Activate(e);
  773. SimulateEditorBecomingInitialized();
  774. try
  775. {
  776. pybind11::exec(R"(
  777. import azlmbr.object
  778. payload = azlmbr.object.PythonProxyObject('TestObjectBase')
  779. payload.set_property('value', 11)
  780. target = azlmbr.object.PythonProxyObject('TestObjectBase')
  781. target.set_property('value', 22)
  782. target.invoke('acceptAzType', payload)
  783. result = target.invoke('resultTest', 33)
  784. if(result.get_property('value') == 33):
  785. print ('ObjectAzTypePassing_Output')
  786. entityId = target.get_property('entityId')
  787. target.set_property('entityId', entityId)
  788. )");
  789. }
  790. catch ([[maybe_unused]] const std::exception& e)
  791. {
  792. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
  793. FAIL();
  794. }
  795. e.Deactivate();
  796. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ObjectAzTypePassing_Input)]);
  797. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ObjectAzTypePassing_Output)]);
  798. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ObjectAzTypePassing_EntityPassed)]);
  799. }
  800. TEST_F(PythonObjectProxyTests, ConstructWithArgs)
  801. {
  802. enum class LogTypes
  803. {
  804. Skip = 0,
  805. ConstructWithDefault,
  806. ConstructWithInt,
  807. ConstructWithString,
  808. ConstructWithStringAndInt,
  809. };
  810. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  811. {
  812. if (AzFramework::StringFunc::Equal(window, "python"))
  813. {
  814. if (AzFramework::StringFunc::Equal(message, "ConstructWithDefault"))
  815. {
  816. return static_cast<int>(LogTypes::ConstructWithDefault);
  817. }
  818. else if (AzFramework::StringFunc::Equal(message, "ConstructWithInt"))
  819. {
  820. return static_cast<int>(LogTypes::ConstructWithInt);
  821. }
  822. else if (AzFramework::StringFunc::Equal(message, "ConstructWithString"))
  823. {
  824. return static_cast<int>(LogTypes::ConstructWithString);
  825. }
  826. else if (AzFramework::StringFunc::Equal(message, "ConstructWithStringAndInt"))
  827. {
  828. return static_cast<int>(LogTypes::ConstructWithStringAndInt);
  829. }
  830. }
  831. return static_cast<int>(LogTypes::Skip);
  832. };
  833. PythonObjectConstructionTester pythonObjectConstructionTester;
  834. pythonObjectConstructionTester.Reflect(m_app.GetBehaviorContext());
  835. AZ::Entity e;
  836. Activate(e);
  837. SimulateEditorBecomingInitialized();
  838. try
  839. {
  840. pybind11::exec(R"(
  841. import azlmbr.object
  842. defaultObj = azlmbr.object.construct('TestConstruct')
  843. if defaultObj.get_property('text') == 'default':
  844. print ('ConstructWithDefault')
  845. defaultObj = azlmbr.object.construct('TestConstruct', 101)
  846. if defaultObj.get_property('text') == 'with_int':
  847. print ('ConstructWithInt')
  848. defaultObj = azlmbr.object.construct('TestConstruct', 'with_string')
  849. if defaultObj.get_property('text') == 'with_string':
  850. print ('ConstructWithString')
  851. defaultObj = azlmbr.object.construct('TestConstruct', 'foo', 201)
  852. if defaultObj.get_property('text') == 'foo':
  853. print ('ConstructWithStringAndInt')
  854. )");
  855. }
  856. catch ([[maybe_unused]] const std::exception& e)
  857. {
  858. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
  859. FAIL();
  860. }
  861. e.Deactivate();
  862. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ConstructWithDefault)]);
  863. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ConstructWithInt)]);
  864. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ConstructWithString)]);
  865. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::ConstructWithStringAndInt)]);
  866. }
  867. TEST_F(PythonObjectProxyTests, PassByValue)
  868. {
  869. enum class LogTypes
  870. {
  871. Skip = 0,
  872. PassByValue_CreateReturnByValue,
  873. PassByValue_CallReturnByValue,
  874. PassByValue_InputByValue
  875. };
  876. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  877. {
  878. if (AzFramework::StringFunc::Equal(window, "python"))
  879. {
  880. if (AzFramework::StringFunc::Equal(message, "PassByValue_CreateReturnByValue"))
  881. {
  882. return static_cast<int>(LogTypes::PassByValue_CreateReturnByValue);
  883. }
  884. else if (AzFramework::StringFunc::Equal(message, "PassByValue_CallReturnByValue"))
  885. {
  886. return static_cast<int>(LogTypes::PassByValue_CallReturnByValue);
  887. }
  888. else if (AzFramework::StringFunc::Equal(message, "PassByValue_InputByValue"))
  889. {
  890. return static_cast<int>(LogTypes::PassByValue_InputByValue);
  891. }
  892. }
  893. return static_cast<int>(LogTypes::Skip);
  894. };
  895. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(m_app.GetSerializeContext()))
  896. {
  897. serializeContext->RegisterGenericType<AZStd::vector<int>>();
  898. }
  899. EntityIdByValueTester::Reflect(m_app.GetBehaviorContext());
  900. PythonObjectBaseTester pythonObjectBaseTester;
  901. pythonObjectBaseTester.Reflect(m_app.GetBehaviorContext());
  902. AZ::Entity e;
  903. Activate(e);
  904. SimulateEditorBecomingInitialized();
  905. try
  906. {
  907. pybind11::exec(R"(
  908. import azlmbr.entity
  909. import azlmbr.object
  910. import azlmbr.test.proxy
  911. proxyEntityId = azlmbr.object.create('EntityIdByValueTester')
  912. if proxyEntityId.typename == 'EntityIdByValueTester':
  913. print ('PassByValue_CreateReturnByValue')
  914. testObjectBase = azlmbr.object.create('TestObjectBase')
  915. entityIdValue = testObjectBase.invoke('get_entity_id_by_value')
  916. if (entityIdValue.typename == 'EntityIdByValueTester'):
  917. print ('PassByValue_CallReturnByValue')
  918. if (testObjectBase.invoke('compare_entity_id_by_value', entityIdValue)):
  919. print ('PassByValue_InputByValue')
  920. intList = testObjectBase.invoke('return_vector_by_value')
  921. if (len(intList) == 3):
  922. print ('PassByValue_CallReturnByValue')
  923. )");
  924. }
  925. catch ([[maybe_unused]] const std::exception& e)
  926. {
  927. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
  928. FAIL();
  929. }
  930. e.Deactivate();
  931. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PassByValue_CreateReturnByValue)]);
  932. EXPECT_EQ(2, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PassByValue_CallReturnByValue)]);
  933. EXPECT_EQ(1, m_testSink.m_evaluationMap[static_cast<int>(LogTypes::PassByValue_InputByValue)]);
  934. }
  935. TEST_F(PythonObjectProxyTests, CallLambdaAsMember)
  936. {
  937. enum class LogTypes
  938. {
  939. Skip = 0,
  940. PythonObjectLambdaTester_CreateObject,
  941. PythonObjectLambdaTester_InvokeLambda
  942. };
  943. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  944. {
  945. if (AzFramework::StringFunc::Equal(window, "python"))
  946. {
  947. if (AzFramework::StringFunc::Equal(message, "PythonObjectLambdaTester_CreateObject"))
  948. {
  949. return static_cast<int>(LogTypes::PythonObjectLambdaTester_CreateObject);
  950. }
  951. else if (AzFramework::StringFunc::Equal(message, "PythonObjectLambdaTester_InvokeLambda"))
  952. {
  953. return static_cast<int>(LogTypes::PythonObjectLambdaTester_InvokeLambda);
  954. }
  955. }
  956. return static_cast<int>(LogTypes::Skip);
  957. };
  958. PythonObjectLambdaTester pythonObjectLambdaTester;
  959. pythonObjectLambdaTester.Reflect(m_app.GetBehaviorContext());
  960. AZ::Entity e;
  961. Activate(e);
  962. SimulateEditorBecomingInitialized();
  963. try
  964. {
  965. pybind11::exec(R"(
  966. import azlmbr.object
  967. import azlmbr.test.proxy
  968. proxy = azlmbr.object.create('PythonObjectLambdaTester')
  969. if proxy is not None:
  970. print ('PythonObjectLambdaTester_CreateObject')
  971. value = proxy.invoke('testLambda')
  972. if (value == 42):
  973. print ('PythonObjectLambdaTester_InvokeLambda')
  974. )");
  975. }
  976. catch ([[maybe_unused]] const std::exception& e)
  977. {
  978. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
  979. FAIL();
  980. }
  981. e.Deactivate();
  982. EXPECT_EQ(1, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::PythonObjectLambdaTester_CreateObject)]);
  983. EXPECT_EQ(1, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::PythonObjectLambdaTester_InvokeLambda)]);
  984. }
  985. TEST_F(PythonObjectProxyTests, ObjectAsType)
  986. {
  987. enum class LogTypes
  988. {
  989. Skip = 0,
  990. Vector2_Non_None,
  991. Vector2_Constructed
  992. };
  993. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  994. {
  995. if (AzFramework::StringFunc::Equal(window, "python"))
  996. {
  997. if (AzFramework::StringFunc::Equal(message, "Vector2_Non_None"))
  998. {
  999. return aznumeric_cast<int>(LogTypes::Vector2_Non_None);
  1000. }
  1001. else if (AzFramework::StringFunc::Equal(message, "Vector2_Constructed"))
  1002. {
  1003. return aznumeric_cast<int>(LogTypes::Vector2_Constructed);
  1004. }
  1005. }
  1006. return aznumeric_cast<int>(LogTypes::Skip);
  1007. };
  1008. PythonObjectLambdaTester pythonObjectLambdaTester;
  1009. pythonObjectLambdaTester.Reflect(m_app.GetBehaviorContext());
  1010. AZ::Entity e;
  1011. Activate(e);
  1012. SimulateEditorBecomingInitialized();
  1013. try
  1014. {
  1015. pybind11::exec(R"(
  1016. import azlmbr.math
  1017. proxy = azlmbr.math.Vector2(40.0, 2.0)
  1018. if proxy is not None:
  1019. print ('Vector2_Non_None')
  1020. value = proxy.x + proxy.y
  1021. if (azlmbr.math.Math_IsClose(value, 42.0)):
  1022. print ('Vector2_Constructed')
  1023. )");
  1024. }
  1025. catch ([[maybe_unused]] const std::exception& e)
  1026. {
  1027. AZStd::string failReason = AZStd::string::format("Failed on with Python exception: %s", e.what());
  1028. //AZ_Warning("UnitTest", false, );
  1029. GTEST_FATAL_FAILURE_(failReason.c_str());
  1030. }
  1031. e.Deactivate();
  1032. EXPECT_EQ(1, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Vector2_Non_None)]);
  1033. EXPECT_EQ(1, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Vector2_Constructed)]);
  1034. }
  1035. TEST_F(PythonObjectProxyTests, Crc32Type)
  1036. {
  1037. enum class LogTypes
  1038. {
  1039. Skip = 0,
  1040. Crc32Type_Created,
  1041. Crc32Type_Read
  1042. };
  1043. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  1044. {
  1045. if (AzFramework::StringFunc::Equal(window, "python"))
  1046. {
  1047. if (AzFramework::StringFunc::StartsWith(message, "Crc32Type_Created"))
  1048. {
  1049. return aznumeric_cast<int>(LogTypes::Crc32Type_Created);
  1050. }
  1051. else if (AzFramework::StringFunc::StartsWith(message, "Crc32Type_Read"))
  1052. {
  1053. return aznumeric_cast<int>(LogTypes::Crc32Type_Read);
  1054. }
  1055. }
  1056. return aznumeric_cast<int>(LogTypes::Skip);
  1057. };
  1058. PythonObjectBaseTester pythonObjectBaseTester;
  1059. pythonObjectBaseTester.Reflect(m_app.GetBehaviorContext());
  1060. AZ::Entity e;
  1061. Activate(e);
  1062. SimulateEditorBecomingInitialized();
  1063. try
  1064. {
  1065. pybind11::exec(R"(
  1066. import azlmbr.math
  1067. result = azlmbr.math.Crc32()
  1068. if result is not None:
  1069. print ('Crc32Type_Created_basic')
  1070. result = azlmbr.math.Crc32('withstring')
  1071. if result is not None:
  1072. print ('Crc32Type_Created_withstring')
  1073. if (result.value == 3101708170): # CRC32 of withstring
  1074. print ('Crc32Type_Read_matches')
  1075. if (azlmbr.math.Crc32('withstring').value == azlmbr.math.Crc32('WithString').value):
  1076. print ('Crc32Type_Read_matches_with_mixed_string_cases')
  1077. )");
  1078. }
  1079. catch ([[maybe_unused]] const std::exception& e)
  1080. {
  1081. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
  1082. FAIL();
  1083. }
  1084. e.Deactivate();
  1085. EXPECT_EQ(2, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Crc32Type_Created)]);
  1086. EXPECT_EQ(2, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Crc32Type_Read)]);
  1087. }
  1088. TEST_F(PythonObjectProxyTests, ObjectDirectory)
  1089. {
  1090. enum class LogTypes
  1091. {
  1092. Skip = 0,
  1093. Found
  1094. };
  1095. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  1096. {
  1097. if (AzFramework::StringFunc::Equal(window, "python"))
  1098. {
  1099. if (AzFramework::StringFunc::StartsWith(message, "Found"))
  1100. {
  1101. return aznumeric_cast<int>(LogTypes::Found);
  1102. }
  1103. }
  1104. return aznumeric_cast<int>(LogTypes::Skip);
  1105. };
  1106. PythonReflectionObjectProxyTester pythonReflectionObjectProxyTester;
  1107. pythonReflectionObjectProxyTester.Reflect(m_app.GetBehaviorContext());
  1108. AZ::Entity e;
  1109. Activate(e);
  1110. SimulateEditorBecomingInitialized();
  1111. try
  1112. {
  1113. pybind11::exec(R"(
  1114. import azlmbr.object
  1115. import azlmbr.test.proxy
  1116. proxyTest = azlmbr.test.proxy.TestObject()
  1117. listOfAttributes = azlmbr.object.dir(proxyTest)
  1118. if (listOfAttributes is not None):
  1119. print ('Found_list')
  1120. for attribute in listOfAttributes:
  1121. if (attribute == 'doAdd'):
  1122. print ('Found_{}'.format(attribute))
  1123. elif (attribute == 'myString'):
  1124. print ('Found_{}'.format(attribute))
  1125. elif (attribute == 'theBuffer'):
  1126. print ('Found_{}'.format(attribute))
  1127. elif (attribute == 'get_answer'):
  1128. print ('Found_{}'.format(attribute))
  1129. elif (attribute == 'set_answer'):
  1130. print ('Found_{}'.format(attribute))
  1131. elif (attribute == 'answer'):
  1132. print ('Found_{}'.format(attribute))
  1133. )");
  1134. }
  1135. catch ([[maybe_unused]] const std::exception& e)
  1136. {
  1137. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", e.what());
  1138. FAIL();
  1139. }
  1140. e.Deactivate();
  1141. EXPECT_EQ(7, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Found)]);
  1142. }
  1143. TEST_F(PythonObjectProxyTests, EnumsAreFound)
  1144. {
  1145. enum class LogTypes
  1146. {
  1147. Skip = 0,
  1148. Found,
  1149. Equals
  1150. };
  1151. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  1152. {
  1153. if (AzFramework::StringFunc::Equal(window, "python"))
  1154. {
  1155. if (AzFramework::StringFunc::StartsWith(message, "Found"))
  1156. {
  1157. return aznumeric_cast<int>(LogTypes::Found);
  1158. }
  1159. else if (AzFramework::StringFunc::StartsWith(message, "Equals"))
  1160. {
  1161. return aznumeric_cast<int>(LogTypes::Equals);
  1162. }
  1163. }
  1164. return aznumeric_cast<int>(LogTypes::Skip);
  1165. };
  1166. PythonReflectionObjectProxyPropertyTester pythonReflectionObjectProxyPropertyTester;
  1167. pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetSerializeContext());
  1168. pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetBehaviorContext());
  1169. AZ::Entity e;
  1170. Activate(e);
  1171. SimulateEditorBecomingInitialized();
  1172. try
  1173. {
  1174. pybind11::exec(R"(
  1175. import azlmbr.object
  1176. import azlmbr.test.proxy
  1177. value = azlmbr.test.proxy.TestObjectProxy_Zero
  1178. print ('Found_TestObjectProxy_Zero')
  1179. if (value == 0):
  1180. print ('Equals_TestObjectProxy_Zero is 0')
  1181. value = azlmbr.test.proxy.TestObjectProxy_One
  1182. print ('Found_TestObjectProxy_One')
  1183. if (value == 1):
  1184. print ('Equals_TestObjectProxy_One is 1')
  1185. )");
  1186. }
  1187. catch ([[maybe_unused]] const std::exception& e)
  1188. {
  1189. AZ_Error("UnitTest", false, "Failed on with Python exception: %s", e.what());
  1190. }
  1191. e.Deactivate();
  1192. EXPECT_EQ(2, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Found)]);
  1193. EXPECT_EQ(2, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Equals)]);
  1194. }
  1195. TEST_F(PythonObjectProxyTests, ClassEnumsAreFound)
  1196. {
  1197. enum class LogTypes
  1198. {
  1199. Skip = 0,
  1200. Assigned,
  1201. Equals
  1202. };
  1203. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  1204. {
  1205. if (AzFramework::StringFunc::Equal(window, "python"))
  1206. {
  1207. if (AzFramework::StringFunc::StartsWith(message, "Assigned"))
  1208. {
  1209. return aznumeric_cast<int>(LogTypes::Assigned);
  1210. }
  1211. else if (AzFramework::StringFunc::StartsWith(message, "Equals"))
  1212. {
  1213. return aznumeric_cast<int>(LogTypes::Equals);
  1214. }
  1215. }
  1216. return aznumeric_cast<int>(LogTypes::Skip);
  1217. };
  1218. PythonReflectionObjectProxyPropertyTester pythonReflectionObjectProxyPropertyTester;
  1219. pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetSerializeContext());
  1220. pythonReflectionObjectProxyPropertyTester.Reflect(m_app.GetBehaviorContext());
  1221. AZ::Entity e;
  1222. Activate(e);
  1223. SimulateEditorBecomingInitialized();
  1224. try
  1225. {
  1226. pybind11::exec(R"(
  1227. import azlmbr.object
  1228. import azlmbr.test.proxy
  1229. value = azlmbr.test.proxy.TestObjectProxy_Zaphod
  1230. print ('Assigned TestObjectProxy_Zaphod')
  1231. if (value == 1):
  1232. print ('Equals TestObjectProxy_Zaphod is 1')
  1233. value = azlmbr.test.proxy.TestObjectProxy_Arthur
  1234. print ('Assigned TestObjectProxy_Arthur')
  1235. if (value == 2):
  1236. print ('Equals TestObjectProxy_Arthur is 2')
  1237. proxy = azlmbr.test.proxy.TestObjectProxy()
  1238. enumValue = azlmbr.test.proxy.TestObjectProxy_Arthur
  1239. proxyValue = proxy.enumClass
  1240. if (proxyValue == enumValue):
  1241. print ('Equals TestObjectProxy_Arthur is value.enumClass')
  1242. proxy.enumClass = azlmbr.test.proxy.TestObjectProxy_Zaphod
  1243. print ('Assigned TestObjectProxy_Zaphod to value.enumClass ({})'.format(proxy.enumClass))
  1244. if (proxy.enumClass == azlmbr.test.proxy.TestObjectProxy_Zaphod):
  1245. print ('Equals TestObjectProxy_Zaphod now is value.enumClass')
  1246. )");
  1247. }
  1248. catch ([[maybe_unused]] const std::exception& e)
  1249. {
  1250. AZ_Error("UnitTest", false, "Failed on with Python exception: %s", e.what());
  1251. }
  1252. e.Deactivate();
  1253. EXPECT_EQ(3, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Assigned)]);
  1254. EXPECT_EQ(4, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Equals)]);
  1255. }
  1256. TEST_F(PythonObjectProxyTests, Repr)
  1257. {
  1258. enum class LogTypes
  1259. {
  1260. Skip = 0,
  1261. Found
  1262. };
  1263. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  1264. {
  1265. if (AzFramework::StringFunc::Equal(window, "python"))
  1266. {
  1267. if (AzFramework::StringFunc::StartsWith(message, "Found"))
  1268. {
  1269. return aznumeric_cast<int>(LogTypes::Found);
  1270. }
  1271. }
  1272. return aznumeric_cast<int>(LogTypes::Skip);
  1273. };
  1274. // class that implements repr
  1275. PythonObjectReprTester reprTester;
  1276. reprTester.Reflect(m_app.GetBehaviorContext());
  1277. // class that doesn't implement repr
  1278. PythonObjectBaseTester pythonObjectBaseTester;
  1279. pythonObjectBaseTester.Reflect(m_app.GetBehaviorContext());
  1280. AZ::Entity e;
  1281. Activate(e);
  1282. SimulateEditorBecomingInitialized();
  1283. try
  1284. {
  1285. pybind11::exec(R"(
  1286. import azlmbr.object
  1287. import azlmbr.test.proxy
  1288. # test repr with a newly created object with default values
  1289. proxyTest = azlmbr.object.create('TestReprObject')
  1290. retrieved_repr = repr(proxyTest)
  1291. if retrieved_repr == 'TestReprObject(-1)':
  1292. print ('Found default repr')
  1293. # test repr with different state, via constructor
  1294. proxyTest = azlmbr.test.proxy.TestReprObject(42)
  1295. retrieved_repr = repr(proxyTest)
  1296. if retrieved_repr == 'TestReprObject(42)':
  1297. print ('Found valid state repr')
  1298. # test repr with a modified object
  1299. proxyTest.value = 84
  1300. retrieved_repr = repr(proxyTest)
  1301. if retrieved_repr == 'TestReprObject(84)':
  1302. print ('Found valid modified state repr')
  1303. # test repr for objects that don't implement it
  1304. proxyTest = azlmbr.test.proxy.TestObjectBase()
  1305. retrieved_repr = repr(proxyTest)
  1306. if retrieved_repr.startswith('<TestObjectBase via PythonProxyObject'):
  1307. print ('Found fallback repr')
  1308. )");
  1309. }
  1310. catch ([[maybe_unused]] const std::exception& ex)
  1311. {
  1312. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", ex.what());
  1313. FAIL();
  1314. }
  1315. e.Deactivate();
  1316. EXPECT_EQ(4, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Found)]);
  1317. }
  1318. TEST_F(PythonObjectProxyTests, ToString)
  1319. {
  1320. enum class LogTypes
  1321. {
  1322. Skip = 0,
  1323. Found
  1324. };
  1325. m_testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  1326. {
  1327. if (AzFramework::StringFunc::Equal(window, "python"))
  1328. {
  1329. if (AzFramework::StringFunc::StartsWith(message, "Found"))
  1330. {
  1331. return aznumeric_cast<int>(LogTypes::Found);
  1332. }
  1333. }
  1334. return aznumeric_cast<int>(LogTypes::Skip);
  1335. };
  1336. // class that implements ToString
  1337. PythonObjectReprTester reprTester;
  1338. reprTester.Reflect(m_app.GetBehaviorContext());
  1339. // class that doesn't implement ToString
  1340. PythonObjectBaseTester pythonObjectBaseTester;
  1341. pythonObjectBaseTester.Reflect(m_app.GetBehaviorContext());
  1342. AZ::Entity e;
  1343. Activate(e);
  1344. SimulateEditorBecomingInitialized();
  1345. try
  1346. {
  1347. pybind11::exec(R"(
  1348. import azlmbr.object
  1349. import azlmbr.test.proxy
  1350. # test str with a newly created object with default values
  1351. proxyTest = azlmbr.object.create('TestReprObject')
  1352. retrieved_str = str(proxyTest)
  1353. if retrieved_str == 'MyValue: -1':
  1354. print ('Found str')
  1355. # test str with a modified object
  1356. proxyTest.value = 42
  1357. retrieved_str = str(proxyTest)
  1358. if retrieved_str == 'MyValue: 42':
  1359. print ('Found valid modified state str')
  1360. # test str for objects that don't implement it, fallback to repr
  1361. proxyTest = azlmbr.test.proxy.TestObjectBase()
  1362. retrieved_str = str(proxyTest)
  1363. retrieved_repr = repr(proxyTest)
  1364. if retrieved_str == retrieved_repr:
  1365. print ('Found fallback str based on repr')
  1366. )");
  1367. }
  1368. catch ([[maybe_unused]] const std::exception& ex)
  1369. {
  1370. AZ_Warning("UnitTest", false, "Failed on with Python exception: %s", ex.what());
  1371. FAIL();
  1372. }
  1373. e.Deactivate();
  1374. EXPECT_EQ(3, m_testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Found)]);
  1375. }
  1376. }