PythonBindingLibTests.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 <Source/PythonSystemComponent.h>
  9. #include "PythonTestingUtility.h"
  10. #include "PythonTraceMessageSink.h"
  11. #include <EditorPythonBindings/PythonCommon.h>
  12. #include <Source/PythonTypeCasters.h>
  13. #include <pybind11/embed.h>
  14. #include <pybind11/pybind11.h>
  15. #include <EditorPythonBindings/EditorPythonBindingsBus.h>
  16. #include <AzFramework/StringFunc/StringFunc.h>
  17. // an example converter for an "AZ type"
  18. namespace TestTypes
  19. {
  20. void RegisterAzEntityId(pybind11::module m)
  21. {
  22. auto classEntityId = pybind11::class_<AZ::EntityId>(m, AZ::AzTypeInfo<AZ::EntityId>::Name());
  23. classEntityId.def(pybind11::init<AZ::u64>());
  24. classEntityId.def("isValid", &AZ::EntityId::IsValid);
  25. classEntityId.def("setInvalid", &AZ::EntityId::SetInvalid);
  26. classEntityId.def_property_readonly("id", [](const AZ::EntityId& e) { return static_cast<AZ::u64>(e); });
  27. classEntityId.def("__repr__", &AZ::EntityId::ToString);
  28. }
  29. }
  30. // this is called the first time a Python script "import azlmbrtest"
  31. PYBIND11_EMBEDDED_MODULE(azlmbrtest, m)
  32. {
  33. EditorPythonBindings::EditorPythonBindingsNotificationBus::Broadcast(&EditorPythonBindings::EditorPythonBindingsNotificationBus::Events::OnImportModule, m.ptr());
  34. TestTypes::RegisterAzEntityId(m);
  35. }
  36. namespace UnitTest
  37. {
  38. struct MyPythonBindings final
  39. : public EditorPythonBindings::EditorPythonBindingsNotificationBus::Handler
  40. {
  41. int m_onImportModuleCount = 0;
  42. MyPythonBindings()
  43. {
  44. EditorPythonBindings::EditorPythonBindingsNotificationBus::Handler::BusConnect();
  45. }
  46. ~MyPythonBindings()
  47. {
  48. EditorPythonBindings::EditorPythonBindingsNotificationBus::Handler::BusDisconnect();
  49. }
  50. static long DoAdd(int lhs, int rhs)
  51. {
  52. return lhs + rhs;
  53. }
  54. static void AZPrintf([[maybe_unused]] const AZStd::string& message)
  55. {
  56. AZ_TracePrintf("python", "%s", message.c_str());
  57. }
  58. void ImportTestSubModule(pybind11::module module)
  59. {
  60. pybind11::module subModule = module.def_submodule("tester", "A submodule for 'test'");
  61. subModule.def("add", &DoAdd);
  62. subModule.def("print", &AZPrintf);
  63. }
  64. void OnImportModule(PyObject* module) override
  65. {
  66. pybind11::module m = pybind11::cast<pybind11::module>(module);
  67. std::string szName = pybind11::cast<std::string>(m.attr("__name__"));
  68. if (szName == "azlmbrtest")
  69. {
  70. m_onImportModuleCount++;
  71. ImportTestSubModule(m);
  72. }
  73. }
  74. };
  75. class PythonBindingLibTest
  76. : public PythonTestingFixture
  77. {
  78. protected:
  79. void SetUp() override
  80. {
  81. PythonTestingFixture::SetUp();
  82. RegisterComponentDescriptors();
  83. }
  84. void TearDown() override
  85. {
  86. PythonTestingFixture::TearDown();
  87. }
  88. };
  89. TEST_F(PythonBindingLibTest, ImportBaseModule)
  90. {
  91. AZ::Entity entity;
  92. entity.CreateComponent<EditorPythonBindings::PythonSystemComponent>();
  93. entity.Init();
  94. entity.Activate();
  95. SimulateEditorBecomingInitialized();
  96. {
  97. MyPythonBindings pythonBindings;
  98. pybind11::module::import("azlmbrtest");
  99. EXPECT_EQ(pythonBindings.m_onImportModuleCount, 1);
  100. }
  101. entity.Deactivate();
  102. }
  103. TEST_F(PythonBindingLibTest, ImportBaseModuleTwice)
  104. {
  105. const char* script =
  106. R"(
  107. import azlmbrtest
  108. import azlmbrtest
  109. )";
  110. AZ::Entity entity;
  111. entity.CreateComponent<EditorPythonBindings::PythonSystemComponent>();
  112. entity.Init();
  113. entity.Activate();
  114. SimulateEditorBecomingInitialized();
  115. // Python keeps track of the module import count so that multiple attempts should result into a single import count
  116. {
  117. MyPythonBindings pythonBindings;
  118. EXPECT_EQ(PyRun_SimpleString(script), 0);
  119. EXPECT_EQ(pythonBindings.m_onImportModuleCount, 1);
  120. }
  121. entity.Deactivate();
  122. }
  123. TEST_F(PythonBindingLibTest, ExecuteSimpleBinding)
  124. {
  125. enum class LogTypes
  126. {
  127. Skip = 0,
  128. TesterAdd,
  129. TesterPrinted
  130. };
  131. PythonTraceMessageSink testSink;
  132. testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  133. {
  134. AZStd::string_view w(window);
  135. if (w == "python")
  136. {
  137. AZStd::string_view m(message);
  138. if (m == "tester add equals 42")
  139. {
  140. return (int)LogTypes::TesterAdd;
  141. }
  142. if (m == "tester says yo")
  143. {
  144. return (int)LogTypes::TesterPrinted;
  145. }
  146. }
  147. return (int)LogTypes::Skip;
  148. };
  149. const char* script =
  150. R"(
  151. import azlmbrtest
  152. value = azlmbrtest.tester.add(40, 2)
  153. print ('tester add equals ' + str(value))
  154. value = azlmbrtest.tester.print('tester says yo')
  155. )";
  156. AZ::Entity entity;
  157. entity.CreateComponent<EditorPythonBindings::PythonSystemComponent>();
  158. entity.Init();
  159. entity.Activate();
  160. SimulateEditorBecomingInitialized();
  161. {
  162. MyPythonBindings pythonBindings;
  163. EXPECT_EQ(PyRun_SimpleString(script), 0);
  164. EXPECT_EQ(pythonBindings.m_onImportModuleCount, 1);
  165. EXPECT_EQ(testSink.m_evaluationMap[(int)LogTypes::TesterAdd], 1);
  166. EXPECT_EQ(testSink.m_evaluationMap[(int)LogTypes::TesterPrinted], 1);
  167. }
  168. entity.Deactivate();
  169. }
  170. TEST_F(PythonBindingLibTest, ConvertAZTypes)
  171. {
  172. enum class LogTypes
  173. {
  174. Skip = 0,
  175. TypeConverted,
  176. IdIsValid,
  177. IdHasRepr,
  178. IdNowInvalid
  179. };
  180. PythonTraceMessageSink testSink;
  181. testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  182. {
  183. AZStd::string_view m(message);
  184. AZStd::string_view w(window);
  185. if (w == "python")
  186. {
  187. if (m == "entityId equals 10")
  188. {
  189. return (int)LogTypes::TypeConverted;
  190. }
  191. else if (m == "entityId is valid True")
  192. {
  193. return (int)LogTypes::IdIsValid;
  194. }
  195. else if (m == "entityId is repr [10]")
  196. {
  197. return (int)LogTypes::IdHasRepr;
  198. }
  199. else if (m == "entityId invalid is 4294967295")
  200. {
  201. return (int)LogTypes::IdNowInvalid;
  202. }
  203. }
  204. return (int)LogTypes::Skip;
  205. };
  206. const char* script =
  207. R"(
  208. import azlmbrtest
  209. entityId = azlmbrtest.EntityId(10)
  210. print ('entityId equals ' + str(entityId.id))
  211. print ('entityId is valid ' + str(entityId.isValid()))
  212. print ('entityId is repr ' + str(entityId))
  213. entityId.setInvalid()
  214. print ('entityId invalid is ' + str(entityId.id))
  215. )";
  216. AZ::Entity entity;
  217. entity.CreateComponent<EditorPythonBindings::PythonSystemComponent>();
  218. entity.Init();
  219. entity.Activate();
  220. SimulateEditorBecomingInitialized();
  221. EXPECT_EQ(PyRun_SimpleString(script), 0);
  222. EXPECT_EQ(testSink.m_evaluationMap[(int)LogTypes::TypeConverted], 1);
  223. EXPECT_EQ(testSink.m_evaluationMap[(int)LogTypes::IdIsValid], 1);
  224. EXPECT_EQ(testSink.m_evaluationMap[(int)LogTypes::IdHasRepr], 1);
  225. EXPECT_EQ(testSink.m_evaluationMap[(int)LogTypes::IdNowInvalid], 1);
  226. entity.Deactivate();
  227. }
  228. TEST_F(PythonBindingLibTest, ImportProjectModules)
  229. {
  230. enum class LogTypes
  231. {
  232. Skip = 0,
  233. ImportModule,
  234. TestCallHit,
  235. TestTypeDoCall1
  236. };
  237. PythonTraceMessageSink testSink;
  238. testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  239. {
  240. if (AzFramework::StringFunc::Equal(window, "python"))
  241. {
  242. if (AzFramework::StringFunc::Equal(message, "ImportModule"))
  243. {
  244. return static_cast<int>(LogTypes::ImportModule);
  245. }
  246. else if (AzFramework::StringFunc::Equal(message, "test_call_hit"))
  247. {
  248. return static_cast<int>(LogTypes::TestCallHit);
  249. }
  250. else if (AzFramework::StringFunc::Equal(message, "TestType.do_call.1"))
  251. {
  252. return static_cast<int>(LogTypes::TestTypeDoCall1);
  253. }
  254. }
  255. return static_cast<int>(LogTypes::Skip);
  256. };
  257. AZ::Entity e;
  258. Activate(e);
  259. SimulateEditorBecomingInitialized();
  260. try
  261. {
  262. pybind11::exec(R"(
  263. import sys, os
  264. import azlmbr.paths
  265. sys.path.append(os.path.join(azlmbr.paths.engroot,'Gems','EditorPythonBindings','Code','Tests'))
  266. from test_package import import_test as itest
  267. print('ImportModule')
  268. itest.test_call()
  269. testInst = itest.TestType()
  270. testInst.do_call(1)
  271. )");
  272. }
  273. catch ([[maybe_unused]] const std::exception& exception)
  274. {
  275. AZ_Error("UnitTest", false, "Failed on with Python exception: %s", exception.what());
  276. }
  277. e.Deactivate();
  278. EXPECT_EQ(1, testSink.m_evaluationMap[static_cast<int>(LogTypes::ImportModule)]);
  279. EXPECT_EQ(1, testSink.m_evaluationMap[static_cast<int>(LogTypes::TestCallHit)]);
  280. EXPECT_EQ(1, testSink.m_evaluationMap[static_cast<int>(LogTypes::TestTypeDoCall1)]);
  281. }
  282. TEST_F(PythonBindingLibTest, PyDocHelp_AzlmbrGlobals_Works)
  283. {
  284. enum class LogTypes
  285. {
  286. Skip = 0,
  287. Worked
  288. };
  289. PythonTraceMessageSink testSink;
  290. testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  291. {
  292. if (AzFramework::StringFunc::Equal(window, "python"))
  293. {
  294. if (AzFramework::StringFunc::StartsWith(message, "Worked"))
  295. {
  296. return aznumeric_cast<int>(LogTypes::Worked);
  297. }
  298. }
  299. return aznumeric_cast<int>(LogTypes::Skip);
  300. };
  301. AZ::Entity e;
  302. Activate(e);
  303. SimulateEditorBecomingInitialized();
  304. try
  305. {
  306. pybind11::exec(R"(
  307. import pydoc
  308. import azlmbr.globals
  309. pydoc.help(azlmbr.globals)
  310. print('Worked')
  311. )");
  312. }
  313. catch ([[maybe_unused]] const std::exception& exception)
  314. {
  315. AZ_Error("UnitTest", false, "Failed on with Python exception: %s", exception.what());
  316. }
  317. e.Deactivate();
  318. EXPECT_EQ(1, testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::Worked)]);
  319. }
  320. TEST_F(PythonBindingLibTest, ImportAzLmbrTwice)
  321. {
  322. enum class LogTypes
  323. {
  324. Skip = 0,
  325. ImportAzLmbrTwice,
  326. SawEntityId
  327. };
  328. PythonTraceMessageSink testSink;
  329. testSink.m_evaluateMessage = [](const char* window, const char* message) -> int
  330. {
  331. if (AzFramework::StringFunc::Equal(window, "python"))
  332. {
  333. if (AzFramework::StringFunc::Equal(message, "ImportAzLmbrTwice"))
  334. {
  335. return aznumeric_cast<int>(LogTypes::ImportAzLmbrTwice);
  336. }
  337. else if (AzFramework::StringFunc::StartsWith(message, "entity_id 101"))
  338. {
  339. return aznumeric_cast<int>(LogTypes::SawEntityId);
  340. }
  341. }
  342. return aznumeric_cast<int>(LogTypes::Skip);
  343. };
  344. AZ::Entity e;
  345. Activate(e);
  346. SimulateEditorBecomingInitialized();
  347. try
  348. {
  349. pybind11::exec(R"(
  350. import sys, os
  351. import azlmbr.paths
  352. sys.path.append(os.path.join(azlmbr.paths.engroot,'Gems','EditorPythonBindings','Code','Tests'))
  353. sys.path.append(os.path.join(azlmbr.paths.engroot,'Gems','EditorPythonBindings','Code','Tests','test_package'))
  354. from test_package import import_many
  355. import_many.test_many_entity_id()
  356. print('ImportAzLmbrTwice')
  357. )");
  358. }
  359. catch ([[maybe_unused]] const std::exception& exception)
  360. {
  361. AZ_Error("UnitTest", false, "Failed on with Python exception: %s", exception.what());
  362. }
  363. e.Deactivate();
  364. EXPECT_EQ(1, testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::ImportAzLmbrTwice)]);
  365. EXPECT_EQ(1, testSink.m_evaluationMap[aznumeric_cast<int>(LogTypes::SawEntityId)]);
  366. }
  367. }