#include #include #include #include #include #include using Void::Coro::Task; using Void::Fsm::State; using Void::Fsm::StateKey; namespace Fsm = Void::Fsm; // Imperative equivalents of the old reactive extras: // - deferred events : an event a state does not await stays buffered in the // typed mailbox and is consumed by a later state — defer // is automatic, no declaration needed. // - entry/exit actions: the first/last lines of run(). // - internal transition: handle an event in a loop without leaving the state. namespace { struct Click { }; struct Done { }; // Busy awaits only Done; a Click posted meanwhile stays buffered (deferred) // and is handled by Idle. enter/exit are the first/last lines of run(). class Flow : public Fsm::Machine { public: using Success = int; std::vector log; struct States { struct Busy : State { Task run() { auto& m = self(); m.log.push_back("enter"); co_await await_event(); // Click buffered m.log.push_back("exit"); co_return next(); } }; struct Idle : State { Task run() { co_await await_event(); // buffered Click self().log.push_back("click"); co_return next(); } }; struct End : State { Task run() { co_return succeed(1); } }; }; }; } // namespace // @tag integration Void::Testing::Test deferred_event_replays_after_transition() { Flow fsm; fsm.start(); fsm.send(Click{}); // not awaited in Busy → stays buffered (not dropped) fsm.send(Done{}); // Busy -> Idle; Idle consumes the buffered Click -> End const auto outcome = fsm.wait(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 1); // enter/exit ran around Busy; the buffered Click was handled in Idle. const std::vector expected{"enter", "exit", "click"}; Void::Testing::expect(fsm.log == expected); } // ── internal transition ───────────────────────────────────────────────────── namespace { struct Tick { }; struct Stop { }; // Counter handles Tick internally (loop, no re-entry); Stop leaves with the // accumulated count. class Counter : public Fsm::Machine { public: using Success = int; int ticks = 0; int enters = 0; struct States { struct Run : State { Task run() { auto& m = self(); ++m.enters; // entered exactly once for (;;) { auto ev = co_await await_any(); if (std::holds_alternative(ev)) { co_return next(); } ++m.ticks; // internal: handled, stay here } } }; struct Done : State { Task run() { co_return succeed(self().ticks); } }; }; }; } // namespace // @tag integration Void::Testing::Test internal_transition_stays_in_state() { Counter fsm; fsm.start(); fsm.send(Tick{}, Tick{}, Tick{}); // handled internally, no re-entry fsm.send(Stop{}); const auto outcome = fsm.wait(); Void::Testing::expect(outcome.has_value()); Void::Testing::expect_eq(*outcome, 3); // three internal ticks Void::Testing::expect_eq(fsm.enters, 1); // entered Run exactly once }