Skip to content
Merged
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 @@ -43,6 +43,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"
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=46 (advisory above 25, limit 50)
UnitTestDatabase ret;

add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal());
Expand Down Expand Up @@ -89,6 +90,7 @@
Pokemon::add_tests_MoveMechanics(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);
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 <optional>
#include "Common/Cpp/Filesystem/FileIO.h"
#include "Pokemon_SimGolden.h"

namespace PokemonAutomation{
namespace Pokemon{


namespace{

// 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


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());

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

View workflow job for this annotation

GitHub Actions / code-health

Code health (bypass)

char_pointer bypassed by the tag at line 61: "std::getenv returns a const char pointer"
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::optional<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 std::nullopt;
}

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::optional<std::string> 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
);
}
return true;
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Sim Golden

Check warning on line 1 in SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=7 (advisory above 6, limit 8)
*
* 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 <optional>
#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);

// `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: ...". None if they are
// the same.
std::optional<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,151 @@
/* 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 <optional>
#include <string>
#include <string_view>
#include <utility>
#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{
struct Input{
std::string name;
UnitTestResult (*body)(Logger& logger);
};

struct Expectation{
std::string_view description;
};

Input input;
Expectation expectation;
};

class Test_SimGolden : public UnitTest{
public:
explicit Test_SimGolden(SimGoldenTestCase scenario)
: UnitTest("Pokemon::Navigation - Sim golden: " + scenario.input.name)

Check warning on line 42 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 57 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")
}

// 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){
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", difference_of("# x\r\na\r\nb\r\n", "# x\na\nb\n"), "(none)");
}

UnitTestResult test_first_differing_line(Logger&){
return expect_equal(
"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", 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", 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");
}
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 = "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 = {}},
};

} // 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
4 changes: 4 additions & 0 deletions SerialPrograms/cmake/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,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
Expand Down
Loading