Files
Library/tests/standalone/Fsm/Entry.cpp
T

138 lines
3.5 KiB
C++

// @requires-reflection
#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
using Void::Coro::Task;
using Void::Fsm::ChildSuccess;
using Void::Fsm::State;
using Void::Fsm::StateKey;
namespace Fsm = Void::Fsm;
// A state's entry is inferred by signature (a 0-arg member returning StateKey /
// Task<StateKey>). When several members match, one annotated
// `[[= Void::Fsm::entry]]` is preferred; otherwise the first-declared wins. A
// startup-data overload run(Cfg) is selected when the machine is launched with
// data, and the nullary entry otherwise.
namespace
{
// Two nullary entry candidates; the annotated one is chosen even though it
// is not declared first.
class Annotated : public Fsm::Machine<Annotated>
{
public:
using Success = int;
struct States {
struct Pick : State {
Task<StateKey> first() // declared first
{
co_return succeed(1);
}
[[= Fsm::entry]] Task<StateKey> chosen()
{
co_return succeed(2);
}
};
};
};
// Two nullary entry candidates, none annotated → first-declared wins.
class FirstWins : public Fsm::Machine<FirstWins>
{
public:
using Success = int;
struct States {
struct Pick : State {
Task<StateKey> first() { co_return succeed(1); }
Task<StateKey> second() { co_return succeed(2); }
};
};
};
} // namespace
// @tag integration
Void::Testing::Test annotated_entry_beats_first_declared()
{
Annotated fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 2); // the [[= Fsm::entry]] member ran
}
// @tag integration
Void::Testing::Test first_declared_entry_wins_without_annotation()
{
FirstWins fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 1); // first-declared ran
}
// ── startup-data overload selection ─────────────────────────────────────────
namespace
{
// Begin has both a nullary entry and a startup-data overload. The right one
// is picked by whether the machine was launched with data.
class Seeded : public Fsm::Machine<Seeded>
{
public:
using Success = int;
struct States {
struct Begin : State {
Task<StateKey> run() // no startup data
{
co_return succeed(0);
}
Task<StateKey> run(int seed) // with startup data
{
co_return succeed(seed);
}
};
};
};
class Launcher : public Fsm::Machine<Launcher>
{
public:
using Success = int;
struct States {
struct Go : State {
Task<StateKey> run()
{
co_return startMachine<Seeded, After>(7);
}
};
struct After : State {
Task<StateKey> ok(ChildSuccess s)
{
co_return succeed(s.as<int>());
}
};
};
};
} // namespace
// @tag integration
Void::Testing::Test startup_entry_used_when_launched_with_data()
{
Launcher fsm; // Seeded launched via startMachine<Seeded, After>(7)
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 7); // run(int) selected
}
// @tag integration
Void::Testing::Test nullary_entry_used_without_startup_data()
{
Seeded fsm; // started normally, no startup data
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 0); // run() selected
}