From 208f732f5c52b1544d7f19ed416bc0932850174c Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 00:36:24 -0800 Subject: [PATCH 1/4] Pokemon Navigation: golden files for the overworld simulations Shared plumbing for seam 3's sim goldens: a golden's path beside its test, its text, the first differing line, and a check that rewrites the file when PA_UPDATE_SIM_GOLDENS is set. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../ComputerPrograms_UnitTestRunner.cpp | 2 + .../Simulation/Pokemon_SimGolden.cpp | 132 ++++++++++++++++++ .../Navigation/Simulation/Pokemon_SimGolden.h | 57 ++++++++ .../Simulation/Pokemon_SimGolden_Tests.cpp | 122 ++++++++++++++++ .../Simulation/Pokemon_SimGolden_Tests.h | 23 +++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 6 files changed, 340 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.h diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 57691f3c19..45575d5d68 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -41,6 +41,7 @@ #include "Pokemon/Navigation/Pokemon_RouteLog_Tests.h" #include "Pokemon/Navigation/Pokemon_RuntimeMap_Tests.h" #include "Pokemon/Navigation/Pokemon_WallRecovery_Tests.h" +#include "Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.h" #include "VideoGames/PokemonHome/PokemonHome_Tests.h" #include "VideoGames/PokemonSwSh/PokemonSwSh_Tests.h" #include "VideoGames/PokemonLA/PokemonLA_Tests.h" @@ -85,6 +86,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ Pokemon::add_tests_MapData(ret); Pokemon::add_tests_RuntimeMap(ret); Pokemon::add_tests_RouteLog(ret); + Pokemon::add_tests_SimGolden(ret); Pokemon::add_tests_AutoStory(ret); NintendoSwitch::add_tests_CheckOnlineDetector(ret); NintendoSwitch::add_tests_FailedToConnectDetector(ret); diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp new file mode 100644 index 0000000000..ea8b6fe80d --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp @@ -0,0 +1,132 @@ +/* Sim Golden + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Filesystem/FileIO.h" +#include "Pokemon_SimGolden.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +// `text` with every "\n" made "\r\n" and no "\r" of its own. +std::string with_crlf(std::string_view text){ + std::string out; + out.reserve(text.size() + text.size() / 16); + for (char ch : text){ + if (ch == '\r'){ + continue; + } + if (ch == '\n'){ + out += '\r'; + } + out += ch; + } + return out; +} + +// Line `begin` of `text` up to its "\n", without a "\r" at its end. +std::string_view line_at(std::string_view text, size_t begin){ + if (begin >= text.size()){ + return {}; + } + const size_t end = std::min(text.find('\n', begin), text.size()); + std::string_view line = text.substr(begin, end - begin); + if (line.ends_with('\r')){ + line.remove_suffix(1); + } + return line; +} + +// Where the line after the one at `begin` starts; past the end if none. +size_t next_line(std::string_view text, size_t begin){ + if (begin >= text.size()){ + return text.size() + 1; + } + const size_t end = text.find('\n', begin); + return end == std::string_view::npos ? text.size() + 1 : end + 1; +} + +} // namespace + + +bool sim_golden_update_requested(){ + // code-health: allow char_pointer -- std::getenv returns a const char pointer + const char* value = std::getenv(std::string(SIM_GOLDEN_UPDATE_VARIABLE).c_str()); + return value != nullptr && value[0] != '\0' && std::string_view(value) != "0"; +} + +std::string sim_golden_path(std::string_view source_file, std::string_view name){ + const size_t slash = source_file.find_last_of("/\\"); + std::string out = slash == std::string_view::npos ? std::string() : std::string(source_file.substr(0, slash + 1)); + out += name; + return out; +} + +std::string sim_golden_text(std::string_view about, const std::vector& lines){ + std::string out = "# "; + out += about; + out += "\n"; + for (const std::string& line : lines){ + out += line; + out += "\n"; + } + return out; +} + +std::string first_golden_difference(std::string_view golden, std::string_view actual){ + size_t a = 0; + size_t b = 0; + for (size_t line = 1; a < golden.size() || b < actual.size(); line++){ + const std::string_view golden_line = line_at(golden, a); + const std::string_view actual_line = line_at(actual, b); + const bool golden_ended = a >= golden.size(); + const bool actual_ended = b >= actual.size(); + if (golden_line != actual_line || golden_ended != actual_ended){ + return std::format( + "line {}:\n--- golden: {}\n+++ actual: {}", line, + golden_ended ? "(end of file)" : golden_line, + actual_ended ? "(end of file)" : actual_line + ); + } + a = next_line(golden, a); + b = next_line(actual, b); + } + return ""; +} + +UnitTestResult check_sim_golden(Logger& logger, const std::string& path, const std::string& text){ + if (sim_golden_update_requested()){ + const std::string crlf = with_crlf(text); + FileIO file; + if (!file.open(path, FileMode::WRITE | FileMode::BINARY) || file.write(crlf) != crlf.size()){ + return "Could not write " + path + "."; + } + logger.log("Wrote " + path + "."); + return true; + } + std::string golden; + if (!file_to_string(path, golden)){ + return std::format("No golden at {}: run with {}=1 to write it.", path, SIM_GOLDEN_UPDATE_VARIABLE); + } + const std::string difference = first_golden_difference(golden, text); + if (!difference.empty()){ + return std::format( + "{} differs from the simulation at {}\nRun with {}=1 to rewrite it if the change is meant.", + path, difference, SIM_GOLDEN_UPDATE_VARIABLE + ); + } + return true; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h new file mode 100644 index 0000000000..4d3210a1fc --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h @@ -0,0 +1,57 @@ +/* Sim Golden + * + * From: https://github.com/PokemonAutomation/ + * + * Golden files for the games' overworld simulations: what a simulation + * observably does (the legs it walks, where steps end, the events it + * shows, how injected faults turn out), written as text a line per fact + * and checked in beside the test that writes it. A refactor of a + * simulation that should change nothing must leave its golden as it is; + * a change that should, shows as the golden's diff in review. + * + * A golden test builds its text and hands it to check_sim_golden(). With + * SIM_GOLDEN_UPDATE_VARIABLE set to anything but "" or "0", the golden is + * rewritten instead, with CRLF line endings as the source tree keeps. + * + */ + +#ifndef PokemonAutomation_Pokemon_SimGolden_H +#define PokemonAutomation_Pokemon_SimGolden_H + +#include +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Set it to rewrite the golden files instead of checking them. +inline constexpr std::string_view SIM_GOLDEN_UPDATE_VARIABLE = "PA_UPDATE_SIM_GOLDENS"; + +// Whether SIM_GOLDEN_UPDATE_VARIABLE asks for the goldens to be rewritten. +bool sim_golden_update_requested(); + +// The file `name` in the directory of `source_file`, a test's __FILE__. +std::string sim_golden_path(std::string_view source_file, std::string_view name); + +// The golden text: `about` as a "# " comment line, then `lines`, each +// ended by "\n". +std::string sim_golden_text(std::string_view about, const std::vector& lines); + +// The first line where `golden` and `actual` differ, carriage returns +// ignored: "line N:\n--- golden: ...\n+++ actual: ...". "" if none. +std::string first_golden_difference(std::string_view golden, std::string_view actual); + +// Checks `text` against the golden file at `path`, or rewrites the file +// with it when SIM_GOLDEN_UPDATE_VARIABLE is set. Fails with the first +// difference, or with how to write a golden that is missing. +UnitTestResult check_sim_golden(Logger& logger, const std::string& path, const std::string& text); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp new file mode 100644 index 0000000000..99d4d2d7a8 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp @@ -0,0 +1,122 @@ +/* Sim Golden Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The golden file plumbing on its own: paths beside a test, the text, and + * the first difference a check reports. + * + */ + +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon_SimGolden.h" +#include "Pokemon_SimGolden_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +struct SimGoldenTestCase{ + std::string name; + UnitTestResult (*body)(Logger& logger); +}; + +class Test_SimGolden : public UnitTest{ +public: + explicit Test_SimGolden(SimGoldenTestCase scenario) + : UnitTest("Pokemon::Navigation - Sim golden: " + scenario.name) + , m_scenario(std::move(scenario)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + return m_scenario.body(logger); + } + +private: + SimGoldenTestCase m_scenario; +}; + +UnitTestResult expect_equal(std::string_view what, std::string_view actual, std::string_view expected){ + if (actual == expected){ + return true; + } + return std::string(what) + ": expected \"" + std::string(expected) + "\", got \"" + std::string(actual) + "\"."; +} + +UnitTestResult test_path_beside_source(Logger&){ + UnitTestResult result = expect_equal("posix", sim_golden_path("/a/b/Test.cpp", "x.golden.txt"), "/a/b/x.golden.txt"); + if (result.result != UnitTestResult::PASSED){ + return result; + } + result = expect_equal("windows", sim_golden_path("C:\\a\\Test.cpp", "x.txt"), "C:\\a\\x.txt"); + if (result.result != UnitTestResult::PASSED){ + return result; + } + return expect_equal("no directory", sim_golden_path("Test.cpp", "x.txt"), "x.txt"); +} + +UnitTestResult test_text(Logger&){ + return expect_equal("text", sim_golden_text("about", {"a", "b"}), "# about\na\nb\n"); +} + +UnitTestResult test_same_text_ignoring_crlf(Logger&){ + return expect_equal("difference", first_golden_difference("# x\r\na\r\nb\r\n", "# x\na\nb\n"), ""); +} + +UnitTestResult test_first_differing_line(Logger&){ + return expect_equal( + "difference", first_golden_difference("# x\r\na\r\nb\r\n", "# x\na\nc\n"), + "line 3:\n--- golden: b\n+++ actual: c" + ); +} + +UnitTestResult test_extra_line(Logger&){ + UnitTestResult result = expect_equal( + "longer actual", first_golden_difference("a\n", "a\nb\n"), + "line 2:\n--- golden: (end of file)\n+++ actual: b" + ); + if (result.result != UnitTestResult::PASSED){ + return result; + } + return expect_equal( + "shorter actual", first_golden_difference("a\nb\n", "a\n"), + "line 2:\n--- golden: b\n+++ actual: (end of file)" + ); +} + +UnitTestResult test_missing_golden_fails(Logger& logger){ + if (sim_golden_update_requested()){ + return UnitTestResult(UnitTestResult::SKIPPED, "the goldens are being rewritten"); + } + const UnitTestResult result = check_sim_golden(logger, sim_golden_path(__FILE__, "no_such.golden.txt"), "x\n"); + if (result.result == UnitTestResult::PASSED){ + return "Expected a missing golden to fail the check."; + } + return true; +} + +const SimGoldenTestCase SIM_GOLDEN_CASES[] = { + {.name = "the golden's path is beside the test's source", .body = test_path_beside_source}, + {.name = "the text is a comment line, then a line each", .body = test_text}, + {.name = "CRLF and LF text compare the same", .body = test_same_text_ignoring_crlf}, + {.name = "the first differing line is reported", .body = test_first_differing_line}, + {.name = "a line more or less is reported", .body = test_extra_line}, + {.name = "a missing golden fails", .body = test_missing_golden_fails}, +}; + +} // namespace + + +void add_tests_SimGolden(UnitTestDatabase& database){ + for (const SimGoldenTestCase& scenario : SIM_GOLDEN_CASES){ + database.add(scenario); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.h b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.h new file mode 100644 index 0000000000..3119fb8b6a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.h @@ -0,0 +1,23 @@ +/* Sim Golden Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_SimGolden_Tests_H +#define PokemonAutomation_Pokemon_SimGolden_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the sim golden file tests. +void add_tests_SimGolden(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index f90c6bc950..dc1aad5e85 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1500,6 +1500,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/Navigation/Pokemon_RouteLog.h Source/Pokemon/Navigation/Pokemon_RouteLog_Tests.cpp Source/Pokemon/Navigation/Pokemon_RouteLog_Tests.h + Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp + Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h + Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp + Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.h Source/Pokemon/Options/Pokemon_BoxSortingTable.cpp Source/Pokemon/Options/Pokemon_BoxSortingTable.h Source/Pokemon/Options/Pokemon_EncounterBotOptions.h From db23c630668c4527976042be9fe79a20c5e8b608 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 00:45:21 -0800 Subject: [PATCH 2/4] Pokemon Navigation: sim golden tests as input and expectation cases Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Simulation/Pokemon_SimGolden_Tests.cpp | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp index 99d4d2d7a8..a71b34adc8 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp @@ -20,19 +20,28 @@ namespace Pokemon{ namespace{ -struct SimGoldenTestCase{ +struct SimGoldenInput{ std::string name; UnitTestResult (*body)(Logger& logger); }; +struct SimGoldenExpectation{ + std::string_view description; +}; + +struct SimGoldenTestCase{ + SimGoldenInput input; + SimGoldenExpectation expectation; +}; + class Test_SimGolden : public UnitTest{ public: explicit Test_SimGolden(SimGoldenTestCase scenario) - : UnitTest("Pokemon::Navigation - Sim golden: " + scenario.name) + : UnitTest("Pokemon::Navigation - Sim golden: " + scenario.input.name) , m_scenario(std::move(scenario)) {} virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - return m_scenario.body(logger); + return m_scenario.input.body(logger); } private: @@ -99,12 +108,12 @@ UnitTestResult test_missing_golden_fails(Logger& logger){ } const SimGoldenTestCase SIM_GOLDEN_CASES[] = { - {.name = "the golden's path is beside the test's source", .body = test_path_beside_source}, - {.name = "the text is a comment line, then a line each", .body = test_text}, - {.name = "CRLF and LF text compare the same", .body = test_same_text_ignoring_crlf}, - {.name = "the first differing line is reported", .body = test_first_differing_line}, - {.name = "a line more or less is reported", .body = test_extra_line}, - {.name = "a missing golden fails", .body = test_missing_golden_fails}, + {.input = {.name = "the golden's path is beside the test's source", .body = test_path_beside_source}, .expectation = {}}, + {.input = {.name = "the text is a comment line, then a line each", .body = test_text}, .expectation = {}}, + {.input = {.name = "CRLF and LF text compare the same", .body = test_same_text_ignoring_crlf}, .expectation = {}}, + {.input = {.name = "the first differing line is reported", .body = test_first_differing_line}, .expectation = {}}, + {.input = {.name = "a line more or less is reported", .body = test_extra_line}, .expectation = {}}, + {.input = {.name = "a missing golden fails", .body = test_missing_golden_fails}, .expectation = {}}, }; } // namespace From b3990fd0304bc85b3b2d48d313df7a57c355d54d Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 14:35:42 -0800 Subject: [PATCH 3/4] Tests: nest Input and Expectation inside each native test case struct Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Simulation/Pokemon_SimGolden_Tests.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp index a71b34adc8..06b48f2f5f 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp @@ -20,18 +20,18 @@ namespace Pokemon{ namespace{ -struct SimGoldenInput{ - std::string name; - UnitTestResult (*body)(Logger& logger); -}; +struct SimGoldenTestCase{ + struct Input{ + std::string name; + UnitTestResult (*body)(Logger& logger); + }; -struct SimGoldenExpectation{ - std::string_view description; -}; + struct Expectation{ + std::string_view description; + }; -struct SimGoldenTestCase{ - SimGoldenInput input; - SimGoldenExpectation expectation; + Input input; + Expectation expectation; }; class Test_SimGolden : public UnitTest{ From 047fd8a153b36c5b23bc0b8d3e69d6e100bbc828 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 12 Nov 2026 15:05:38 -0800 Subject: [PATCH 4/4] Review fixes: first_golden_difference returns an optional, with_crlf is public and tested, Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Simulation/Pokemon_SimGolden.cpp | 42 +++++++++---------- .../Navigation/Simulation/Pokemon_SimGolden.h | 10 ++++- .../Simulation/Pokemon_SimGolden_Tests.cpp | 28 +++++++++++-- 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp index ea8b6fe80d..0ab221c99e 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "Common/Cpp/Filesystem/FileIO.h" #include "Pokemon_SimGolden.h" @@ -16,22 +17,6 @@ namespace Pokemon{ namespace{ -// `text` with every "\n" made "\r\n" and no "\r" of its own. -std::string with_crlf(std::string_view text){ - std::string out; - out.reserve(text.size() + text.size() / 16); - for (char ch : text){ - if (ch == '\r'){ - continue; - } - if (ch == '\n'){ - out += '\r'; - } - out += ch; - } - return out; -} - // Line `begin` of `text` up to its "\n", without a "\r" at its end. std::string_view line_at(std::string_view text, size_t begin){ if (begin >= text.size()){ @@ -57,6 +42,21 @@ size_t next_line(std::string_view text, size_t begin){ } // namespace +std::string with_crlf(std::string_view text){ + std::string out; + out.reserve(text.size() + text.size() / 16); + for (char ch : text){ + if (ch == '\r'){ + continue; + } + if (ch == '\n'){ + out += '\r'; + } + out += ch; + } + return out; +} + bool sim_golden_update_requested(){ // code-health: allow char_pointer -- std::getenv returns a const char pointer const char* value = std::getenv(std::string(SIM_GOLDEN_UPDATE_VARIABLE).c_str()); @@ -81,7 +81,7 @@ std::string sim_golden_text(std::string_view about, const std::vector first_golden_difference(std::string_view golden, std::string_view actual){ size_t a = 0; size_t b = 0; for (size_t line = 1; a < golden.size() || b < actual.size(); line++){ @@ -99,7 +99,7 @@ std::string first_golden_difference(std::string_view golden, std::string_view ac a = next_line(golden, a); b = next_line(actual, b); } - return ""; + return std::nullopt; } UnitTestResult check_sim_golden(Logger& logger, const std::string& path, const std::string& text){ @@ -116,11 +116,11 @@ UnitTestResult check_sim_golden(Logger& logger, const std::string& path, const s if (!file_to_string(path, golden)){ return std::format("No golden at {}: run with {}=1 to write it.", path, SIM_GOLDEN_UPDATE_VARIABLE); } - const std::string difference = first_golden_difference(golden, text); - if (!difference.empty()){ + const std::optional difference = first_golden_difference(golden, text); + if (difference.has_value()){ return std::format( "{} differs from the simulation at {}\nRun with {}=1 to rewrite it if the change is meant.", - path, difference, SIM_GOLDEN_UPDATE_VARIABLE + path, *difference, SIM_GOLDEN_UPDATE_VARIABLE ); } return true; diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h index 4d3210a1fc..23486fe72e 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h @@ -18,6 +18,7 @@ #ifndef PokemonAutomation_Pokemon_SimGolden_H #define PokemonAutomation_Pokemon_SimGolden_H +#include #include #include #include @@ -41,9 +42,14 @@ std::string sim_golden_path(std::string_view source_file, std::string_view name) // ended by "\n". std::string sim_golden_text(std::string_view about, const std::vector& lines); +// `text` with every "\n" made "\r\n" and no "\r" of its own: the line +// endings the goldens are written with. +std::string with_crlf(std::string_view text); + // The first line where `golden` and `actual` differ, carriage returns -// ignored: "line N:\n--- golden: ...\n+++ actual: ...". "" if none. -std::string first_golden_difference(std::string_view golden, std::string_view actual); +// ignored: "line N:\n--- golden: ...\n+++ actual: ...". None if they are +// the same. +std::optional first_golden_difference(std::string_view golden, std::string_view actual); // Checks `text` against the golden file at `path`, or rewrites the file // with it when SIM_GOLDEN_UPDATE_VARIABLE is set. Fails with the first diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp index 06b48f2f5f..552dcd42cb 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp @@ -7,8 +7,10 @@ * */ +#include #include #include +#include #include "Common/Cpp/TestRunners/UnitTest.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "Pokemon_SimGolden.h" @@ -55,6 +57,11 @@ UnitTestResult expect_equal(std::string_view what, std::string_view actual, std: return std::string(what) + ": expected \"" + std::string(expected) + "\", got \"" + std::string(actual) + "\"."; } +// The difference as text for expect_equal(): "(none)" when the texts match. +std::string difference_of(std::string_view golden, std::string_view actual){ + return first_golden_difference(golden, actual).value_or("(none)"); +} + UnitTestResult test_path_beside_source(Logger&){ UnitTestResult result = expect_equal("posix", sim_golden_path("/a/b/Test.cpp", "x.golden.txt"), "/a/b/x.golden.txt"); if (result.result != UnitTestResult::PASSED){ @@ -72,30 +79,42 @@ UnitTestResult test_text(Logger&){ } UnitTestResult test_same_text_ignoring_crlf(Logger&){ - return expect_equal("difference", first_golden_difference("# x\r\na\r\nb\r\n", "# x\na\nb\n"), ""); + return expect_equal("difference", difference_of("# x\r\na\r\nb\r\n", "# x\na\nb\n"), "(none)"); } UnitTestResult test_first_differing_line(Logger&){ return expect_equal( - "difference", first_golden_difference("# x\r\na\r\nb\r\n", "# x\na\nc\n"), + "difference", difference_of("# x\r\na\r\nb\r\n", "# x\na\nc\n"), "line 3:\n--- golden: b\n+++ actual: c" ); } UnitTestResult test_extra_line(Logger&){ UnitTestResult result = expect_equal( - "longer actual", first_golden_difference("a\n", "a\nb\n"), + "longer actual", difference_of("a\n", "a\nb\n"), "line 2:\n--- golden: (end of file)\n+++ actual: b" ); if (result.result != UnitTestResult::PASSED){ return result; } return expect_equal( - "shorter actual", first_golden_difference("a\nb\n", "a\n"), + "shorter actual", difference_of("a\nb\n", "a\n"), "line 2:\n--- golden: b\n+++ actual: (end of file)" ); } +UnitTestResult test_with_crlf(Logger&){ + UnitTestResult result = expect_equal("bare LF", with_crlf("a\nb\n"), "a\r\nb\r\n"); + if (result.result != UnitTestResult::PASSED){ + return result; + } + result = expect_equal("CRLF", with_crlf("a\r\nb\r\n"), "a\r\nb\r\n"); + if (result.result != UnitTestResult::PASSED){ + return result; + } + return expect_equal("mixed and a stray CR", with_crlf("a\r\nb\rc\n"), "a\r\nbc\r\n"); +} + UnitTestResult test_missing_golden_fails(Logger& logger){ if (sim_golden_update_requested()){ return UnitTestResult(UnitTestResult::SKIPPED, "the goldens are being rewritten"); @@ -113,6 +132,7 @@ const SimGoldenTestCase SIM_GOLDEN_CASES[] = { {.input = {.name = "CRLF and LF text compare the same", .body = test_same_text_ignoring_crlf}, .expectation = {}}, {.input = {.name = "the first differing line is reported", .body = test_first_differing_line}, .expectation = {}}, {.input = {.name = "a line more or less is reported", .body = test_extra_line}, .expectation = {}}, + {.input = {.name = "goldens are written with exactly one CRLF a line", .body = test_with_crlf}, .expectation = {}}, {.input = {.name = "a missing golden fails", .body = test_missing_golden_fails}, .expectation = {}}, };