ScriptCanvasReporter.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #pragma once
  9. #include <AzCore/Asset/AssetCommon.h>
  10. #include <AzCore/Component/Entity.h>
  11. #include <AzCore/Component/EntityBus.h>
  12. #include <AzCore/Component/EntityId.h>
  13. #include <AzCore/std/chrono/chrono.h>
  14. #include <ScriptCanvas/Data/Data.h>
  15. #include <ScriptCanvas/Execution/RuntimeComponent.h>
  16. #include <ScriptCanvas/Libraries/UnitTesting/UnitTestBus.h>
  17. #include <ScriptCanvas/Execution/ExecutionBus.h>
  18. #include <ScriptCanvas/Core/ExecutionNotificationsBus.h>
  19. #define SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_EQ(LHS, RHS)\
  20. if(!(LHS == RHS)) { AddFailure(AZStd::string::format("Error | Expected (candidate: %s) == (reference: %s): %s", Datum(LHS).ToString().data(), Datum(RHS).ToString().data(), report.data())); }
  21. #define SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_NE(LHS, RHS)\
  22. if(!(LHS != RHS)) { AddFailure(AZStd::string::format("Error | Expected (candidate: %s) != (reference: %s): %s", Datum(LHS).ToString().data(), Datum(RHS).ToString().data(), report.data())); }
  23. #define SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_GT(LHS, RHS)\
  24. if(!(LHS > RHS)) { AddFailure(AZStd::string::format("Error | Expected (candidate: %s) > (reference: %s): %s", Datum(LHS).ToString().data(), Datum(RHS).ToString().data(), report.data())); }
  25. #define SCRIPT_CANVAS_UNIT_TEST_REPORTER_VECTOR_EXPECT_GT(LHS, RHS)\
  26. if(!(LHS.IsGreaterThan(RHS))) { AddFailure(AZStd::string::format("Error | Expected (candidate: %s) <= (reference: %s): %s", Datum(LHS).ToString().data(), Datum(RHS).ToString().data(), report.data())); }
  27. #define SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_GE(LHS, RHS)\
  28. if(!(LHS >= RHS)) { AddFailure(AZStd::string::format("Error | Expected (candidate: %s) >= (reference: %s): %s", Datum(LHS).ToString().data(), Datum(RHS).ToString().data(), report.data())); }
  29. #define SCRIPT_CANVAS_UNIT_TEST_REPORTER_VECTOR_EXPECT_GE(LHS, RHS)\
  30. if(!(LHS.IsGreaterEqualThan(RHS))) { AddFailure(AZStd::string::format("Error | Expected (candidate: %s) <= (reference: %s): %s", Datum(LHS).ToString().data(), Datum(RHS).ToString().data(), report.data())); }
  31. #define SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_LT(LHS, RHS)\
  32. if(!(LHS < RHS)) { AddFailure(AZStd::string::format("Error | Expected (candidate: %s) < (reference: %s): %s", Datum(LHS).ToString().data(), Datum(RHS).ToString().data(), report.data())); }
  33. #define SCRIPT_CANVAS_UNIT_TEST_REPORTER_VECTOR_EXPECT_LT(LHS, RHS)\
  34. if(!(LHS.IsLessThan(RHS))) { AddFailure(AZStd::string::format("Error | Expected (candidate: %s) <= (reference: %s): %s", Datum(LHS).ToString().data(), Datum(RHS).ToString().data(), report.data())); }
  35. #define SCRIPT_CANVAS_UNIT_TEST_REPORTER_EXPECT_LE(LHS, RHS)\
  36. if(!(LHS <= RHS)) { AddFailure(AZStd::string::format("Error | Expected (candidate: %s) <= (reference: %s): %s", Datum(LHS).ToString().data(), Datum(RHS).ToString().data(), report.data())); }
  37. #define SCRIPT_CANVAS_UNIT_TEST_REPORTER_VECTOR_EXPECT_LE(LHS, RHS)\
  38. if(!(LHS.IsLessEqualThan(RHS))) { AddFailure(AZStd::string::format("Error | Expected (candidate: %s) <= (reference: %s): %s", Datum(LHS).ToString().data(), Datum(RHS).ToString().data(), report.data())); }
  39. namespace ScriptCanvasEditor
  40. {
  41. using namespace ScriptCanvas;
  42. using namespace ScriptCanvas::UnitTesting;
  43. constexpr const char* k_unitTestDirPathRelative = "@gemroot:ScriptCanvasTesting@/Assets/ScriptCanvas/UnitTests";
  44. class Reporter
  45. : public Bus::Handler
  46. , public AZ::EntityBus::Handler
  47. , private ScriptCanvas::ExecutionNotificationsBus::Handler // only to support IsGraphObserved, ErrorHandling in Testing
  48. {
  49. public:
  50. Reporter();
  51. Reporter(const AZ::EntityId& entityID);
  52. ~Reporter();
  53. void CollectPerformanceTiming();
  54. bool ExpectsParseError() const;
  55. bool ExpectsRuntimeFailure() const;
  56. void FinishReport();
  57. void FinishReport(const bool inErrorState);
  58. const AZStd::vector<Report>& GetCheckpoints() const;
  59. ExecutionConfiguration GetExecutionConfiguration() const;
  60. ExecutionMode GetExecutionMode() const;
  61. const AZStd::vector<Report>& GetFailure() const;
  62. const AZ::Data::AssetId& GetGraph() const;
  63. AZStd::sys_time_t GetParseDuration() const;
  64. const Execution::PerformanceTrackingReport& GetPerformanceReport() const;
  65. const AZStd::vector<Report>& GetSuccess() const;
  66. AZStd::sys_time_t GetTranslateDuration() const;
  67. const AZ::IO::Path& GetFilePath() const;
  68. bool IsActivated() const;
  69. bool IsCompiled() const;
  70. bool IsComplete() const;
  71. bool IsDeactivated() const;
  72. bool IsErrorFree() const;
  73. bool IsGraphLoaded() const;
  74. bool IsParseAttemptMade() const;
  75. bool IsProcessOnly() const;
  76. bool IsReportFinished() const;
  77. void MarkCompiled();
  78. void MarkExpectParseError();
  79. void MarkExpectRuntimeFailure();
  80. void MarkGraphLoaded();
  81. void MarkParseAttemptMade();
  82. bool operator==(const Reporter& other) const;
  83. void SetDurations(AZStd::sys_time_t parse, AZStd::sys_time_t translate);
  84. void SetExecutionConfiguration(ExecutionConfiguration configuration);
  85. void SetExecutionMode(ExecutionMode mode);
  86. void SetEntity(const AZ::EntityId& entityID);
  87. void SetGraph(const AZ::Data::AssetId& graphID);
  88. void SetProcessOnly(bool processOnly);
  89. void SetFilePath(const AZ::IO::PathView& filePath);
  90. // Bus::Handler
  91. void AddFailure(const Report& report) override;
  92. void AddSuccess(const Report& report) override;
  93. void Checkpoint(const Report& report) override;
  94. void ExpectFalse(const bool value, const Report& report) override;
  95. void ExpectTrue(const bool value, const Report& report) override;
  96. void MarkComplete(const Report& report) override;
  97. SCRIPT_CANVAS_UNIT_TEST_EQUALITY_OVERLOAD_OVERRIDES(ExpectEqual);
  98. SCRIPT_CANVAS_UNIT_TEST_EQUALITY_OVERLOAD_OVERRIDES(ExpectNotEqual);
  99. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_OVERRIDES(ExpectGreaterThan);
  100. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_OVERRIDES(ExpectGreaterThanEqual);
  101. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_OVERRIDES(ExpectLessThan);
  102. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_OVERRIDES(ExpectLessThanEqual);
  103. protected:
  104. void OnEntityActivated(const AZ::EntityId&) override;
  105. void OnEntityDeactivated(const AZ::EntityId&) override;
  106. //////////////////////////////////////////////////////////////////////////
  107. // ExecutionNotificationsBus
  108. // IsGraphObserved is needed for unit testing code support only, no other event is
  109. void GraphActivated(const GraphActivation&) override {}
  110. void GraphDeactivated(const GraphDeactivation&) override {}
  111. bool IsGraphObserved(const AZ::EntityId& entityId, const GraphIdentifier& identifier) override;
  112. bool IsVariableObserved(const VariableId&) override { return false; }
  113. void NodeSignaledOutput(const OutputSignal&) override {}
  114. void NodeSignaledInput(const InputSignal&) override {}
  115. void GraphSignaledReturn(const ReturnSignal&) override {};
  116. void NodeStateUpdated(const NodeStateChange&) override {}
  117. void RuntimeError(const ExecutionState& executionState, const AZStd::string_view& description) override;
  118. void VariableChanged(const VariableChange&) override {}
  119. void AnnotateNode(const AnnotateNodeSignal&) override {}
  120. private:
  121. bool m_expectParseError = false;
  122. bool m_expectsRuntimeError = false;
  123. bool m_graphIsCompiled = false;
  124. bool m_graphIsActivated = false;
  125. bool m_graphIsDeactivated = false;
  126. bool m_graphIsComplete = false;
  127. bool m_isGraphLoaded = false;
  128. bool m_isParseAttemptMade = false;
  129. bool m_isReportFinished = false;
  130. bool m_processOnly = false;
  131. AZ::IO::Path m_filePath;
  132. ExecutionConfiguration m_configuration = ExecutionConfiguration::Release;
  133. ExecutionMode m_mode;
  134. Execution::PerformanceTrackingReport m_performanceReport;
  135. AZStd::sys_time_t m_parseDuration;
  136. AZStd::sys_time_t m_translationDuration;
  137. AZ::Data::AssetId m_graph;
  138. AZ::EntityId m_entityId;
  139. AZStd::vector<Report> m_checkpoints;
  140. AZStd::vector<Report> m_failures;
  141. AZStd::vector<Report> m_successes;
  142. #if defined(LINUX) //////////////////////////////////////////////////////////////////////////
  143. // Temporarily disable testing on the Linux build until the file name casing discrepancy
  144. // is sorted out through the SC build and testing pipeline.
  145. public:
  146. inline void MarkLinuxDependencyTestBypass()
  147. {
  148. m_graphIsCompiled = true;
  149. m_graphIsActivated = true;
  150. m_graphIsDeactivated = true;
  151. m_graphIsComplete = true;
  152. m_isGraphLoaded = true;
  153. m_isParseAttemptMade = true;
  154. m_isReportFinished = true;
  155. }
  156. #endif ///////////////////////////////////////////////////////////////////////////////////////
  157. }; // class Reporter
  158. using Reporters = AZStd::vector<Reporter>;
  159. }
  160. #include "ScriptCanvasReporter.inl"