Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "Pokemon/Navigation/Pokemon_RouteScorer_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 "PokemonBDSP/PokemonBDSP_Tests.h"
#include "VideoGames/PokemonHome/PokemonHome_Tests.h"
#include "VideoGames/PokemonSwSh/PokemonSwSh_Tests.h"
Expand All @@ -61,7 +62,7 @@



UnitTestDatabase make_UNIT_TESTS_ALL(){

Check warning on line 65 in SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::ComputerPrograms::make_UNIT_TESTS_ALL(): function_lines=45 (advisory above 25, limit 50)
UnitTestDatabase ret;
add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal());
add_tests_Sockets(ret, GlobalThreadPools::unlimited_normal());
Expand Down Expand Up @@ -89,6 +90,7 @@
Pokemon::add_tests_RuntimeMap(ret);
Pokemon::add_tests_RouteLog(ret);
Pokemon::add_tests_RouteScorer(ret);
Pokemon::add_tests_SimGolden(ret);
Pokemon::add_tests_AutoStory(ret);
NintendoSwitch::add_tests_CheckOnlineDetector(ret);
NintendoSwitch::add_tests_FailedToConnectDetector(ret);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* Sim Golden
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <algorithm>
#include <cstdlib>
#include <format>
#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<std::string>& 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 + ".";

Check warning on line 110 in SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
logger.log("Wrote " + path + ".");

Check warning on line 112 in SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
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
Original file line number Diff line number Diff line change
@@ -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 <string>
#include <string_view>
#include <vector>
#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<std::string>& 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/* 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 <string>
#include <string_view>
#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 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.input.name)

Check warning on line 40 in SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
, m_scenario(std::move(scenario))
{}
virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{
return m_scenario.input.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) + "\".";

Check warning on line 55 in SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}

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[] = {
{.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


void add_tests_SimGolden(UnitTestDatabase& database){
for (const SimGoldenTestCase& scenario : SIM_GOLDEN_CASES){
database.add<Test_SimGolden>(scenario);
}
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.h"
#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FaultSweep_Tests.h"
#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_SimFailureCases_Tests.h"
#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_SimGolden_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h"
#include "Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h"
Expand All @@ -36,7 +37,7 @@



void add_tests(UnitTestDatabase& database){

Check warning on line 40 in SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::add_tests( UnitTestDatabase & database): function_lines=26 (advisory above 25, limit 50)
add_tests_DialogDetector(database);
add_tests_MenuDetector(database);
add_tests_AutoStory_Actions(database);
Expand All @@ -59,6 +60,7 @@
add_tests_AutoStory_StorySim(database);
add_tests_AutoStory_SimFailureCases(database);
add_tests_AutoStory_FaultSweep(database);
add_tests_AutoStory_SimGolden(database);
add_tests_AutoStory_StoryActions(database);
}

Expand Down
Loading
Loading