ScriptCanvasReporter.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. bool IsActivated() const;
  68. bool IsCompiled() const;
  69. bool IsComplete() const;
  70. bool IsDeactivated() const;
  71. bool IsErrorFree() const;
  72. bool IsGraphLoaded() const;
  73. bool IsParseAttemptMade() const;
  74. bool IsProcessOnly() const;
  75. bool IsReportFinished() const;
  76. void MarkCompiled();
  77. void MarkExpectParseError();
  78. void MarkExpectRuntimeFailure();
  79. void MarkGraphLoaded();
  80. void MarkParseAttemptMade();
  81. bool operator==(const Reporter& other) const;
  82. void SetDurations(AZStd::sys_time_t parse, AZStd::sys_time_t translate);
  83. void SetExecutionConfiguration(ExecutionConfiguration configuration);
  84. void SetExecutionMode(ExecutionMode mode);
  85. void SetEntity(const AZ::EntityId& entityID);
  86. void SetGraph(const AZ::Data::AssetId& graphID);
  87. void SetProcessOnly(bool processOnly);
  88. // Bus::Handler
  89. void AddFailure(const Report& report) override;
  90. void AddSuccess(const Report& report) override;
  91. void Checkpoint(const Report& report) override;
  92. void ExpectFalse(const bool value, const Report& report) override;
  93. void ExpectTrue(const bool value, const Report& report) override;
  94. void MarkComplete(const Report& report) override;
  95. SCRIPT_CANVAS_UNIT_TEST_EQUALITY_OVERLOAD_OVERRIDES(ExpectEqual);
  96. SCRIPT_CANVAS_UNIT_TEST_EQUALITY_OVERLOAD_OVERRIDES(ExpectNotEqual);
  97. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_OVERRIDES(ExpectGreaterThan);
  98. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_OVERRIDES(ExpectGreaterThanEqual);
  99. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_OVERRIDES(ExpectLessThan);
  100. SCRIPT_CANVAS_UNIT_TEST_COMPARE_OVERLOAD_OVERRIDES(ExpectLessThanEqual);
  101. protected:
  102. void OnEntityActivated(const AZ::EntityId&) override;
  103. void OnEntityDeactivated(const AZ::EntityId&) override;
  104. //////////////////////////////////////////////////////////////////////////
  105. // ExecutionNotificationsBus
  106. // IsGraphObserved is needed for unit testing code support only, no other event is
  107. void GraphActivated(const GraphActivation&) override {}
  108. void GraphDeactivated(const GraphActivation&) override {}
  109. bool IsGraphObserved(const ExecutionState& executionState) override;
  110. bool IsVariableObserved(const VariableId&) override { return false; }
  111. void NodeSignaledOutput(const OutputSignal&) override {}
  112. void NodeSignaledInput(const InputSignal&) override {}
  113. void NodeStateUpdated(const NodeStateChange&) override {}
  114. void RuntimeError(const ExecutionState& executionState, const AZStd::string_view& description) override;
  115. void VariableChanged(const VariableChange&) override {}
  116. void AnnotateNode(const AnnotateNodeSignal&) override {}
  117. private:
  118. bool m_expectParseError = false;
  119. bool m_expectsRuntimeError = false;
  120. bool m_graphIsCompiled = false;
  121. bool m_graphIsActivated = false;
  122. bool m_graphIsDeactivated = false;
  123. bool m_graphIsComplete = false;
  124. bool m_isGraphLoaded = false;
  125. bool m_isParseAttemptMade = false;
  126. bool m_isReportFinished = false;
  127. bool m_processOnly = false;
  128. ExecutionConfiguration m_configuration = ExecutionConfiguration::Release;
  129. ExecutionMode m_mode;
  130. Execution::PerformanceTrackingReport m_performanceReport;
  131. AZStd::sys_time_t m_parseDuration;
  132. AZStd::sys_time_t m_translationDuration;
  133. AZ::Data::AssetId m_graph;
  134. AZ::EntityId m_entityId;
  135. AZStd::vector<Report> m_checkpoints;
  136. AZStd::vector<Report> m_failures;
  137. AZStd::vector<Report> m_successes;
  138. #if defined(LINUX) //////////////////////////////////////////////////////////////////////////
  139. // Temporarily disable testing on the Linux build until the file name casing discrepancy
  140. // is sorted out through the SC build and testing pipeline.
  141. public:
  142. inline void MarkLinuxDependencyTestBypass()
  143. {
  144. m_graphIsCompiled = true;
  145. m_graphIsActivated = true;
  146. m_graphIsDeactivated = true;
  147. m_graphIsComplete = true;
  148. m_isGraphLoaded = true;
  149. m_isParseAttemptMade = true;
  150. m_isReportFinished = true;
  151. }
  152. #endif ///////////////////////////////////////////////////////////////////////////////////////
  153. }; // class Reporter
  154. using Reporters = AZStd::vector<Reporter>;
  155. }
  156. #include "ScriptCanvasReporter.inl"