|
| 1 | +#include "wf/pipeline.hpp" |
| 2 | + |
| 3 | +#include <catch2/catch_test_macros.hpp> |
| 4 | +#include <memory> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +#include "wf/eval_context.hpp" |
| 8 | + |
| 9 | +namespace ams |
| 10 | +{ |
| 11 | + |
| 12 | +namespace |
| 13 | +{ |
| 14 | + |
| 15 | +class IncAction final : public Action |
| 16 | +{ |
| 17 | +public: |
| 18 | + const char* name() const noexcept override { return "IncAction"; } |
| 19 | + |
| 20 | + AMSStatus run(EvalContext& ctx) override |
| 21 | + { |
| 22 | + ctx.Threshold = ctx.Threshold.value_or(0.0f) + 1.0f; |
| 23 | + return {}; |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +class FailAction final : public Action |
| 28 | +{ |
| 29 | +public: |
| 30 | + const char* name() const noexcept override { return "FailAction"; } |
| 31 | + |
| 32 | + AMSStatus run(EvalContext&) override |
| 33 | + { |
| 34 | + return AMS_MAKE_ERROR(AMSErrorType::Generic, "FailAction triggered"); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +} // namespace |
| 39 | + |
| 40 | +CATCH_TEST_CASE("Pipeline runs actions in order and short-circuits on error", |
| 41 | + "[wf][pipeline]") |
| 42 | +{ |
| 43 | + EvalContext Ctx{}; |
| 44 | + Pipeline P; |
| 45 | + |
| 46 | + // Two increments -> Threshold becomes 2, then FailAction stops the pipeline. |
| 47 | + P.add(std::make_unique<IncAction>()) |
| 48 | + .add(std::make_unique<IncAction>()) |
| 49 | + .add(std::make_unique<FailAction>()) |
| 50 | + .add(std::make_unique<IncAction>()); // must NOT execute |
| 51 | + |
| 52 | + Ctx.Threshold = 0.0f; |
| 53 | + |
| 54 | + auto St = P.run(Ctx); |
| 55 | + CATCH_REQUIRE_FALSE(St); |
| 56 | + CATCH_REQUIRE(St.error().getType() == AMSErrorType::Generic); |
| 57 | + |
| 58 | + // Only the first two IncAction should have run. |
| 59 | + CATCH_REQUIRE(Ctx.Threshold.value() == 2.0f); |
| 60 | +} |
| 61 | + |
| 62 | +CATCH_TEST_CASE("Pipeline succeeds when all actions succeed", "[wf][pipeline]") |
| 63 | +{ |
| 64 | + EvalContext Ctx{}; |
| 65 | + Pipeline P; |
| 66 | + |
| 67 | + P.add(std::make_unique<IncAction>()).add(std::make_unique<IncAction>()); |
| 68 | + |
| 69 | + Ctx.Threshold = 0.0f; |
| 70 | + auto St = P.run(Ctx); |
| 71 | + CATCH_REQUIRE(St); |
| 72 | + CATCH_REQUIRE(Ctx.Threshold.value() == 2.0f); |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace ams |
0 commit comments