From 1836dd3713bbcf2dd202a4446a4c6b8d3597c80d Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 18:49:52 -0800 Subject: [PATCH 1/3] AutoStory: shared RNG handoff contract Moved unchanged from bdsp/autostory-starter-rng (7a46b79b4), so that BDSP and FRLG both build on it from the shared base. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_AutoStory_Tests.h | 2 + .../AutoStory/Pokemon_StoryRngHandoff.cpp | 38 ++++++ .../AutoStory/Pokemon_StoryRngHandoff.h | 64 ++++++++++ .../Pokemon_StoryRngHandoff_Tests.cpp | 113 ++++++++++++++++++ .../AutoStory/Pokemon_StoryRngHandoff_Tests.h | 23 ++++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 6 files changed, 244 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index fad5698fcc..a04e1a1c77 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -23,6 +23,7 @@ #include "Pokemon_ScreenEvents_Tests.h" #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryLog_Tests.h" +#include "Pokemon_StoryRngHandoff_Tests.h" #include "Pokemon_StorySavePolicy_Tests.h" #include "Pokemon_StorySegments_Tests.h" #include "Pokemon_StoryStepExecutor_Tests.h" @@ -37,6 +38,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_AutoStorySelect(database); add_tests_AutoStoryStats(database); add_tests_StoryLog(database); + add_tests_StoryRngHandoff(database); add_tests_StoryRangeOptions(database); add_tests_StoryEvents(database); add_tests_StorySegments(database); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp new file mode 100644 index 0000000000..e23256397a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp @@ -0,0 +1,38 @@ +/* Story RNG Handoff + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "Pokemon_StoryRngHandoff.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +RngHandoffMode rng_handoff_mode(bool stop, bool run){ + if (run){ + return RngHandoffMode::RUN; + } + return stop ? RngHandoffMode::STOP : RngHandoffMode::OFF; +} + +bool RngHandoffResult::succeeded() const{ + return failure.empty(); +} + +Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHandoffCall& call){ + const RngHandoffResult result = call(); + if (!result.succeeded()){ + throw_and_log(logger, ErrorReportMode::SEND_ERROR_REPORT, result.failure); + } + logger.log(program + " succeeded: " + result.summary, COLOR_BLUE); + return outcome_ok(); +} + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h new file mode 100644 index 0000000000..140d20e5ea --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h @@ -0,0 +1,64 @@ +/* Story RNG Handoff + * + * From: https://github.com/PokemonAutomation/ + * + * The part of an AutoStory run that hands the game to an RNG program (BDSP + * Starter RNG, FRLG Starter RNG and Gift RNG) and takes it back. A game + * supplies the program's call; the contract around it is shared: + * + * - A handoff row has a mode: OFF (the story plays it by hand), STOP (the run + * ends after the row's handoff save, for the user to run the RNG program) + * or RUN (the story calls the RNG program itself and continues). + * - The call ends in an RngHandoffResult: no failure means the game stands + * where the story goes on; a failure is the reason it does not. + * - A failed RUN stops the program. It never retries the row: the program's + * own reset loop is the retry, and a reload of the save would lose a + * shiny it caught. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRngHandoff_H +#define PokemonAutomation_Pokemon_StoryRngHandoff_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Pokemon_StoryOutcome.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +enum class RngHandoffMode{ + OFF, + STOP, + RUN, +}; + +// The mode for a handoff's two options: RUN takes precedence over STOP. +RngHandoffMode rng_handoff_mode(bool stop, bool run); + + +// How an RNG program's call ended, as the story needs it. +struct RngHandoffResult{ + // Empty: the game stands where the story goes on. Otherwise why it does not. + std::string failure; + // What a success did, for the log: the resets used, the target hit. + std::string summary; + + bool succeeded() const; +}; + +// The call a handoff row runs in place of its steps. +using RngHandoffCall = std::function; + +// Runs `call` for the RNG program `program` ("Starter RNG"). A success is +// logged with its summary and returned as an ok Outcome. A failure throws +// OperationFailedException with an error report, so that no row retry +// reruns the program's reset loop. +Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHandoffCall& call); + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp new file mode 100644 index 0000000000..72a847d509 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp @@ -0,0 +1,113 @@ +/* Story RNG Handoff Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "Pokemon_StoryRngHandoff.h" +#include "Pokemon_StoryRngHandoff_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +// rng_handoff_mode()'s two options. +struct ModeInput{ + bool stop; + bool run; +}; + +struct ModeCase{ + ModeInput input; + RngHandoffMode expected; +}; + +const ModeCase MODE_CASES[] = { + {.input = {.stop = false, .run = false}, .expected = RngHandoffMode::OFF}, + {.input = {.stop = true, .run = false}, .expected = RngHandoffMode::STOP}, + {.input = {.stop = false, .run = true}, .expected = RngHandoffMode::RUN}, + {.input = {.stop = true, .run = true}, .expected = RngHandoffMode::RUN}, +}; + +class Test_RngHandoffMode : public UnitTest{ +public: + Test_RngHandoffMode() + : UnitTest("Pokemon::StoryRngHandoff - RUN takes precedence over STOP") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + for (const ModeCase& item : MODE_CASES){ + if (rng_handoff_mode(item.input.stop, item.input.run) != item.expected){ + return std::format( + "stop={}, run={}: wrong mode", item.input.stop, item.input.run + ); + } + } + return true; + } +}; + +// A success is an ok Outcome and calls the program once. +class Test_RngHandoffSuccess : public UnitTest{ +public: + Test_RngHandoffSuccess() + : UnitTest("Pokemon::StoryRngHandoff - a success is ok") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + int calls = 0; + const Outcome outcome = run_rng_handoff(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.summary = "2 resets"}; + }); + if (!outcome.succeeded()){ + return "Expected an ok outcome"; + } + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + return true; + } +}; + +// A failure throws, once: nothing reruns the program. +class Test_RngHandoffFailure : public UnitTest{ +public: + Test_RngHandoffFailure() + : UnitTest("Pokemon::StoryRngHandoff - a failure throws without a retry") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + int calls = 0; + try{ + run_rng_handoff(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.failure = "out of resets"}; + }); + }catch (OperationFailedException&){ + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + return true; + } + return "Expected OperationFailedException"; + } +}; + +} // namespace + + +void add_tests_StoryRngHandoff(UnitTestDatabase& database){ + database.add(); + database.add(); + database.add(); +} + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h new file mode 100644 index 0000000000..aca6f82c1b --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h @@ -0,0 +1,23 @@ +/* Story RNG Handoff Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRngHandoff_Tests_H +#define PokemonAutomation_Pokemon_StoryRngHandoff_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the shared RNG handoff contract. +void add_tests_StoryRngHandoff(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 79a8c47dae..739b7096d0 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1531,6 +1531,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_AutoStorySelect.h Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h + Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp + Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h + Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.cpp Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.h Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp From 00624b4c3556b0968d83b68ea413dc40c7703e07 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 18:50:09 -0800 Subject: [PATCH 2/3] AutoStory RNG handoff: a program's end can be the user's turn FRLG's loop also ends where the user takes over (target hit but not shiny, attempts spent); that is a stop with a message, not an error. A shiny is a continuation whose rows the game builds, so the seam carries no rows. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_StoryRngHandoff.cpp | 17 ++++++++- .../AutoStory/Pokemon_StoryRngHandoff.h | 17 ++++++++- .../Pokemon_StoryRngHandoff_Tests.cpp | 38 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp index e23256397a..1d0d290e1c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp @@ -24,12 +24,27 @@ bool RngHandoffResult::succeeded() const{ return failure.empty(); } -Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHandoffCall& call){ +std::optional run_rng_handoff_or_user_turn( + Logger& logger, const std::string& program, const RngHandoffCall& call +){ const RngHandoffResult result = call(); if (!result.succeeded()){ throw_and_log(logger, ErrorReportMode::SEND_ERROR_REPORT, result.failure); } + if (!result.user_turn.empty()){ + logger.log(program + " stopped for the user: " + result.user_turn, COLOR_BLUE); + return result.user_turn; + } logger.log(program + " succeeded: " + result.summary, COLOR_BLUE); + return std::nullopt; +} + +Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHandoffCall& call){ + const std::optional user_turn = run_rng_handoff_or_user_turn(logger, program, call); + if (user_turn){ + // A story action has no stop to give the user's turn to. + throw_and_log(logger, ErrorReportMode::SEND_ERROR_REPORT, *user_turn); + } return outcome_ok(); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h index 140d20e5ea..a3f14f1d38 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h @@ -10,7 +10,11 @@ * ends after the row's handoff save, for the user to run the RNG program) * or RUN (the story calls the RNG program itself and continues). * - The call ends in an RngHandoffResult: no failure means the game stands - * where the story goes on; a failure is the reason it does not. + * where the story goes on; a failure is the reason it does not. A game + * whose program can also end where the user takes over (FRLG: the target + * hit but not shiny, the attempts spent) gives that end a user_turn, which + * is a stop and not an error. What the story runs after a continuation + * (FRLG: the rows after a shiny) is the game's, built from the call. * - A failed RUN stops the program. It never retries the row: the program's * own reset loop is the retry, and a reload of the save would lose a * shiny it caught. @@ -21,6 +25,7 @@ #define PokemonAutomation_Pokemon_StoryRngHandoff_H #include +#include #include #include "Common/Cpp/Logging/AbstractLogger.h" #include "Pokemon_StoryOutcome.h" @@ -45,6 +50,9 @@ struct RngHandoffResult{ std::string failure; // What a success did, for the log: the resets used, the target hit. std::string summary; + // Not empty (and no failure): the program ended where the user takes + // over, with this for them. The run stops without an error report. + std::string user_turn; bool succeeded() const; }; @@ -58,6 +66,13 @@ using RngHandoffCall = std::function; // reruns the program's reset loop. Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHandoffCall& call); +// As run_rng_handoff(), for a program whose call may end in the user's turn: +// returns that message, which is logged, and the run stops there. Returns +// nothing when the game stands where the story goes on. +std::optional run_rng_handoff_or_user_turn( + Logger& logger, const std::string& program, const RngHandoffCall& call +); + } // namespace Pokemon } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp index 72a847d509..3608fb064b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/Exceptions/OperationFailedException.h" @@ -99,6 +100,42 @@ class Test_RngHandoffFailure : public UnitTest{ } }; +// The user's turn is returned, not thrown, once. +class Test_RngHandoffUserTurn : public UnitTest{ +public: + Test_RngHandoffUserTurn() + : UnitTest("Pokemon::StoryRngHandoff - a user's turn is returned, not an error") + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + int calls = 0; + const std::optional turn = run_rng_handoff_or_user_turn(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.user_turn = "save by hand"}; + }); + if (!turn || *turn != "save by hand"){ + return "Expected the user's turn"; + } + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + const std::optional none = run_rng_handoff_or_user_turn(logger, "Test RNG", []{ + return RngHandoffResult{.summary = "shiny"}; + }); + if (none){ + return "A success has no user's turn"; + } + try{ + run_rng_handoff_or_user_turn(logger, "Test RNG", []{ + return RngHandoffResult{.failure = "out of resets", .user_turn = "ignored"}; + }); + }catch (OperationFailedException&){ + return true; + } + return "A failure must throw even with a user's turn"; + } +}; + } // namespace @@ -106,6 +143,7 @@ void add_tests_StoryRngHandoff(UnitTestDatabase& database){ database.add(); database.add(); database.add(); + database.add(); } From eed3b65e0746a34cecb501affa9c9752d16075ae Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 18:52:07 -0800 Subject: [PATCH 3/3] AutoStory RNG handoff: options struct for the mode, test table Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_StoryRngHandoff.cpp | 6 +- .../AutoStory/Pokemon_StoryRngHandoff.h | 9 +- .../Pokemon_StoryRngHandoff_Tests.cpp | 204 +++++++++--------- 3 files changed, 119 insertions(+), 100 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp index 1d0d290e1c..67fc164c82 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp @@ -13,11 +13,11 @@ namespace PokemonAutomation{ namespace Pokemon{ -RngHandoffMode rng_handoff_mode(bool stop, bool run){ - if (run){ +RngHandoffMode rng_handoff_mode(const RngHandoffOptions& options){ + if (options.run){ return RngHandoffMode::RUN; } - return stop ? RngHandoffMode::STOP : RngHandoffMode::OFF; + return options.stop ? RngHandoffMode::STOP : RngHandoffMode::OFF; } bool RngHandoffResult::succeeded() const{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h index a3f14f1d38..7887658546 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h @@ -34,14 +34,21 @@ namespace PokemonAutomation{ namespace Pokemon{ +// What a handoff row does with its RNG program. enum class RngHandoffMode{ OFF, STOP, RUN, }; +// A handoff's two options. +struct RngHandoffOptions{ + bool stop = false; + bool run = false; +}; + // The mode for a handoff's two options: RUN takes precedence over STOP. -RngHandoffMode rng_handoff_mode(bool stop, bool run); +RngHandoffMode rng_handoff_mode(const RngHandoffOptions& options); // How an RNG program's call ended, as the story needs it. diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp index 3608fb064b..e6d72119a4 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/Exceptions/OperationFailedException.h" #include "Pokemon_StoryRngHandoff.h" @@ -17,133 +18,144 @@ namespace Pokemon{ namespace{ -// rng_handoff_mode()'s two options. -struct ModeInput{ - bool stop; - bool run; +struct RngHandoffInput{ + std::string_view name; + UnitTestResult (*body)(Logger& logger); }; -struct ModeCase{ - ModeInput input; - RngHandoffMode expected; +struct RngHandoffExpectation{ + std::string_view description; }; -const ModeCase MODE_CASES[] = { - {.input = {.stop = false, .run = false}, .expected = RngHandoffMode::OFF}, - {.input = {.stop = true, .run = false}, .expected = RngHandoffMode::STOP}, - {.input = {.stop = false, .run = true}, .expected = RngHandoffMode::RUN}, - {.input = {.stop = true, .run = true}, .expected = RngHandoffMode::RUN}, +struct RngHandoffTestCase{ + RngHandoffInput input; + RngHandoffExpectation expectation; }; -class Test_RngHandoffMode : public UnitTest{ +class Test_RngHandoff : public UnitTest{ public: - Test_RngHandoffMode() - : UnitTest("Pokemon::StoryRngHandoff - RUN takes precedence over STOP") + explicit Test_RngHandoff(RngHandoffTestCase scenario) + : UnitTest("Pokemon::StoryRngHandoff - " + std::string(scenario.input.name)) + , m_scenario(scenario) {} - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - for (const ModeCase& item : MODE_CASES){ - if (rng_handoff_mode(item.input.stop, item.input.run) != item.expected){ - return std::format( - "stop={}, run={}: wrong mode", item.input.stop, item.input.run - ); - } - } - return true; + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + return m_scenario.input.body(logger); } + +private: + RngHandoffTestCase m_scenario; }; +// rng_handoff_mode()'s two options. +UnitTestResult test_mode(Logger&){ + struct Case{ + RngHandoffOptions options; + RngHandoffMode expected; + }; + const Case cases[] = { + {.options = {.stop = false, .run = false}, .expected = RngHandoffMode::OFF}, + {.options = {.stop = true, .run = false}, .expected = RngHandoffMode::STOP}, + {.options = {.stop = false, .run = true}, .expected = RngHandoffMode::RUN}, + {.options = {.stop = true, .run = true}, .expected = RngHandoffMode::RUN}, + }; + for (const Case& item : cases){ + if (rng_handoff_mode(item.options) != item.expected){ + return std::format("stop={}, run={}: wrong mode", item.options.stop, item.options.run); + } + } + return true; +} + // A success is an ok Outcome and calls the program once. -class Test_RngHandoffSuccess : public UnitTest{ -public: - Test_RngHandoffSuccess() - : UnitTest("Pokemon::StoryRngHandoff - a success is ok") - {} +UnitTestResult test_success(Logger& logger){ + int calls = 0; + const Outcome outcome = run_rng_handoff(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.summary = "2 resets"}; + }); + if (!outcome.succeeded()){ + return "Expected an ok outcome"; + } + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + return true; +} - virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - int calls = 0; - const Outcome outcome = run_rng_handoff(logger, "Test RNG", [&]{ +// A failure throws, once: nothing reruns the program. +UnitTestResult test_failure(Logger& logger){ + int calls = 0; + try{ + run_rng_handoff(logger, "Test RNG", [&]{ calls++; - return RngHandoffResult{.summary = "2 resets"}; + return RngHandoffResult{.failure = "out of resets"}; }); - if (!outcome.succeeded()){ - return "Expected an ok outcome"; - } + }catch (OperationFailedException&){ if (calls != 1){ return std::format("Expected 1 call, got {}", calls); } return true; } -}; - -// A failure throws, once: nothing reruns the program. -class Test_RngHandoffFailure : public UnitTest{ -public: - Test_RngHandoffFailure() - : UnitTest("Pokemon::StoryRngHandoff - a failure throws without a retry") - {} + return "Expected OperationFailedException"; +} - virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - int calls = 0; - try{ - run_rng_handoff(logger, "Test RNG", [&]{ - calls++; - return RngHandoffResult{.failure = "out of resets"}; - }); - }catch (OperationFailedException&){ - if (calls != 1){ - return std::format("Expected 1 call, got {}", calls); - } - return true; - } - return "Expected OperationFailedException"; +// The user's turn is returned, not thrown, once; a success has none; a +// failure throws even with one. +UnitTestResult test_user_turn(Logger& logger){ + int calls = 0; + const std::optional turn = run_rng_handoff_or_user_turn(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.user_turn = "save by hand"}; + }); + if (!turn || *turn != "save by hand"){ + return "Expected the user's turn"; } -}; - -// The user's turn is returned, not thrown, once. -class Test_RngHandoffUserTurn : public UnitTest{ -public: - Test_RngHandoffUserTurn() - : UnitTest("Pokemon::StoryRngHandoff - a user's turn is returned, not an error") - {} - - virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - int calls = 0; - const std::optional turn = run_rng_handoff_or_user_turn(logger, "Test RNG", [&]{ - calls++; - return RngHandoffResult{.user_turn = "save by hand"}; - }); - if (!turn || *turn != "save by hand"){ - return "Expected the user's turn"; - } - if (calls != 1){ - return std::format("Expected 1 call, got {}", calls); - } - const std::optional none = run_rng_handoff_or_user_turn(logger, "Test RNG", []{ - return RngHandoffResult{.summary = "shiny"}; + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + const std::optional none = run_rng_handoff_or_user_turn(logger, "Test RNG", []{ + return RngHandoffResult{.summary = "shiny"}; + }); + if (none){ + return "A success has no user's turn"; + } + try{ + run_rng_handoff_or_user_turn(logger, "Test RNG", []{ + return RngHandoffResult{.failure = "out of resets", .user_turn = "ignored"}; }); - if (none){ - return "A success has no user's turn"; - } - try{ - run_rng_handoff_or_user_turn(logger, "Test RNG", []{ - return RngHandoffResult{.failure = "out of resets", .user_turn = "ignored"}; - }); - }catch (OperationFailedException&){ - return true; - } - return "A failure must throw even with a user's turn"; + }catch (OperationFailedException&){ + return true; } + return "A failure must throw even with a user's turn"; +} + +const RngHandoffTestCase RNG_HANDOFF_CASES[] = { + { + .input = {.name = "RUN takes precedence over STOP", .body = test_mode}, + .expectation = {.description = "RUN wins when both options are on"}, + }, + { + .input = {.name = "a success is ok", .body = test_success}, + .expectation = {.description = "an ok outcome after one call"}, + }, + { + .input = {.name = "a failure throws without a retry", .body = test_failure}, + .expectation = {.description = "OperationFailedException after one call"}, + }, + { + .input = {.name = "a user's turn is returned, not an error", .body = test_user_turn}, + .expectation = {.description = "the message comes back; a failure still throws"}, + }, }; } // namespace void add_tests_StoryRngHandoff(UnitTestDatabase& database){ - database.add(); - database.add(); - database.add(); - database.add(); + for (const RngHandoffTestCase& scenario : RNG_HANDOFF_CASES){ + database.add(scenario); + } }