#include #include #include #include #include #include using namespace Void::Fsm; namespace { // Embedder-driven machine (no worker thread): post + run_pending make // the tests fully deterministic. struct Bench { std::shared_ptr def; std::unique_ptr machine; std::vector log; explicit Bench(Builder& b) { auto built = b.build(); Void::Testing::expect(built.has_value()); def = *built; machine = std::make_unique( def, MachineOptions{.own_worker = false}); } SendResult step(EventId ev) { machine->post(Event{ev, {}}); machine->run_pending(); return SendResult::Handled; // result checked via send paths } }; } // namespace Void::Testing::Test basic_initial_state_entered_on_start() { Builder b; auto idle = b.state("idle"); auto run = b.state("running"); b.initial(idle); (void)run; Bench bench{b}; bench.machine->start(); Void::Testing::expect(bench.machine->is_in("idle")); Void::Testing::expect(!bench.machine->is_in("running")); Void::Testing::expect(bench.machine->running()); } Void::Testing::Test basic_transition_changes_state() { Builder b; auto idle = b.state("idle"); auto run = b.state("running"); b.initial(idle); const auto go = b.event("go"); b.transition(idle, go, run); Bench bench{b}; bench.machine->start(); bench.machine->post(Event{go, {}}); bench.machine->run_pending(); Void::Testing::expect(bench.machine->is_in("running")); Void::Testing::expect(!bench.machine->is_in("idle")); } Void::Testing::Test basic_entry_exit_and_action_ordering() { std::vector log; Builder b; auto a = b.state("a"); auto c = b.state("c"); b.initial(a); const auto go = b.event("go"); b.on_entry(a, SyncAction{[&](Machine&, const Event&) { log.push_back("enter a"); }}); b.on_exit(a, SyncAction{[&](Machine&, const Event&) { log.push_back("exit a"); }}); b.on_entry(c, SyncAction{[&](Machine&, const Event&) { log.push_back("enter c"); }}); b.transition(a, go, c).action(SyncAction{ [&](Machine&, const Event&) { log.push_back("action"); }}); Bench bench{b}; bench.machine->start(); bench.machine->post(Event{go, {}}); bench.machine->run_pending(); const std::vector expected{"enter a", "exit a", "action", "enter c"}; Void::Testing::expect(log == expected); } Void::Testing::Test basic_guard_blocks_and_allows() { Builder b; auto a = b.state("a"); auto c = b.state("c"); b.initial(a); const auto go = b.event("go"); bool open = false; b.transition(a, go, c).guard( [&](const Machine&, const Event&) { return open; }); Bench bench{b}; bench.machine->start(); bench.machine->post(Event{go, {}}); bench.machine->run_pending(); Void::Testing::expect(bench.machine->is_in("a")); open = true; bench.machine->post(Event{go, {}}); bench.machine->run_pending(); Void::Testing::expect(bench.machine->is_in("c")); } Void::Testing::Test basic_internal_transition_no_exit_entry() { std::vector log; Builder b; auto a = b.state("a"); b.initial(a); const auto tick = b.event("tick"); b.on_entry(a, SyncAction{[&](Machine&, const Event&) { log.push_back("enter"); }}); b.on_exit(a, SyncAction{[&](Machine&, const Event&) { log.push_back("exit"); }}); b.internal(a, tick).action(SyncAction{ [&](Machine&, const Event&) { log.push_back("internal"); }}); Bench bench{b}; bench.machine->start(); bench.machine->post(Event{tick, {}}); bench.machine->run_pending(); const std::vector expected{"enter", "internal"}; Void::Testing::expect(log == expected); Void::Testing::expect(bench.machine->is_in("a")); } Void::Testing::Test basic_self_transition_exits_and_reenters() { std::vector log; Builder b; auto a = b.state("a"); b.initial(a); const auto reset = b.event("reset"); b.on_entry(a, SyncAction{[&](Machine&, const Event&) { log.push_back("enter"); }}); b.on_exit(a, SyncAction{[&](Machine&, const Event&) { log.push_back("exit"); }}); b.transition(a, reset, a); Bench bench{b}; bench.machine->start(); bench.machine->post(Event{reset, {}}); bench.machine->run_pending(); const std::vector expected{"enter", "exit", "enter"}; Void::Testing::expect(log == expected); } Void::Testing::Test basic_payload_reaches_action() { int received = 0; Builder b; auto a = b.state("a"); auto c = b.state("c"); b.initial(a); const auto go = b.event("go"); b.transition(a, go, c).action( SyncAction{[&](Machine&, const Event& e) { received = std::any_cast(e.payload); }}); Bench bench{b}; bench.machine->start(); bench.machine->post(Event{go, 42}); bench.machine->run_pending(); Void::Testing::expect_eq(received, 42); } Void::Testing::Test basic_unhandled_event_keeps_state() { Builder b; auto a = b.state("a"); b.initial(a); const auto noop = b.event("noop"); (void)noop; Bench bench{b}; bench.machine->start(); bench.machine->post(Event{noop, {}}); bench.machine->run_pending(); Void::Testing::expect(bench.machine->is_in("a")); } Void::Testing::Test basic_post_rejected_before_start_and_after_stop() { Builder b; auto a = b.state("a"); b.initial(a); const auto go = b.event("go"); Bench bench{b}; Void::Testing::expect(!bench.machine->post(Event{go, {}})); bench.machine->start(); Void::Testing::expect(bench.machine->post(Event{go, {}})); bench.machine->run_pending(); bench.machine->stop(); Void::Testing::expect(!bench.machine->post(Event{go, {}})); Void::Testing::expect(!bench.machine->running()); } Void::Testing::Test builder_rejects_composite_without_initial() { Builder b; auto parent = b.state("parent"); (void)b.state("c1", parent); (void)b.state("c2", parent); b.initial(parent); // parent initial of root, but no initial child auto built = b.build(); Void::Testing::expect(!built.has_value()); } Void::Testing::Test builder_defaults_first_top_level_as_initial() { Builder b; (void)b.state("first"); (void)b.state("second"); Bench bench{b}; bench.machine->start(); Void::Testing::expect(bench.machine->is_in("first")); }