ScriptCanvas_FileHandling.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/Framework/ScriptCanvasTestFixture.h>
  9. #include <Source/Framework/ScriptCanvasTestNodes.h>
  10. #include <Source/Framework/ScriptCanvasTestUtilities.h>
  11. #include <ScriptCanvas/Components/EditorGraph.h>
  12. #include <ScriptCanvas/Core/GraphSerialization.h>
  13. namespace ScriptCanvasTests
  14. {
  15. struct EntityIdComparer
  16. {
  17. bool operator()(AZ::EntityId lhs, AZ::EntityId rhs)
  18. {
  19. return lhs < rhs;
  20. }
  21. };
  22. void PopulateEntityIdsFromFile(AZStd::set<AZ::EntityId, EntityIdComparer>& sortedEntityIds, const char* fileName, bool makeEntityIdsUnique)
  23. {
  24. using namespace ScriptCanvas;
  25. AZ::IO::FixedMaxPath filePath = ScriptCanvasTests::GetUnitTestDirPathRelative();
  26. filePath /= fileName;
  27. auto result = ScriptCanvasEditor::LoadFromFile
  28. (filePath.c_str()
  29. , makeEntityIdsUnique
  30. ? MakeInternalGraphEntitiesUnique::Yes
  31. : MakeInternalGraphEntitiesUnique::No);
  32. EXPECT_TRUE(result.m_isSuccess);
  33. if (result.m_isSuccess)
  34. {
  35. const ScriptCanvas::GraphData* graphData = result.m_handle.Get()->GetGraphDataConst();
  36. for (auto& entityNode : graphData->m_nodes)
  37. {
  38. sortedEntityIds.insert(entityNode->GetId());
  39. }
  40. }
  41. }
  42. TEST_F(ScriptCanvasTestFixture, LoadFromString_MultipleTimes_NotMakeEntityIdsUnique_EntityIdsMatchSourceString)
  43. {
  44. // This uses a different file extension, so it won't get processed by asset processor. This file is only intended to be used for these tests.
  45. AZStd::set<AZ::EntityId, EntityIdComparer> sortedEntityIds;
  46. PopulateEntityIdsFromFile(sortedEntityIds, "SC_UnitTest_MultipleCanvasEntities.sctestfile", /*makeEntityIdsUnique*/ false);
  47. EXPECT_THAT(
  48. sortedEntityIds,
  49. ::testing::ElementsAre(
  50. AZ::EntityId(599577287851), AZ::EntityId(1501520420011), AZ::EntityId(2231664860331), AZ::EntityId(2747060935851)));
  51. }
  52. TEST_F(ScriptCanvasTestFixture, LoadFromString_MultipleTimes_makeEntityIdsUnique_EntityIdsAreUnique)
  53. {
  54. AZStd::set<AZ::EntityId, EntityIdComparer> sortedEntityIdsFirst;
  55. PopulateEntityIdsFromFile(sortedEntityIdsFirst, "SC_UnitTest_MultipleCanvasEntities.sctestfile", /*makeEntityIdsUnique*/ true);
  56. AZStd::set<AZ::EntityId, EntityIdComparer> sortedEntityIdsSecond;
  57. PopulateEntityIdsFromFile(sortedEntityIdsSecond, "SC_UnitTest_MultipleCanvasEntities.sctestfile", /*makeEntityIdsUnique*/ true);
  58. EXPECT_THAT(sortedEntityIdsFirst, ::testing::Not(::testing::ElementsAreArray(sortedEntityIdsSecond)));
  59. }
  60. } // namespace UnitTest