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 @@ -21,10 +21,12 @@
#include "Inference/PokemonLZA_OverworldPartySelectionDetector.h"
#include "Inference/PokemonLZA_PartyBarDetector.h"
#include "Inference/PokemonLZA_SelectionArrowDetector.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryFailures.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand All @@ -49,10 +51,12 @@ void add_tests(UnitTestDatabase& database){
add_tests_OverworldPartySelectionDetector(database);
add_tests_PartyBarDetector(database);
add_tests_SelectionArrowDetector(database);
add_tests_AutoStoryScreenEvents(database);
add_tests_AutoStoryDetectors(database);
add_tests_AutoStoryConfirmChoicesReader(database);
add_tests_AutoStoryDialogChoiceBox(database);
add_tests_AutoStoryObjectiveMarkerReader(database);
add_tests_AutoStoryFailures(database);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* PokemonLZA AutoStory - Failures
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <algorithm>
#include <utility>
#include "PokemonLZA_AutoStoryFailures.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{

using Pokemon::StoryFailureClass;


const std::vector<PlzaFailure>& plza_failures(){

Check warning on line 18 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::plza_failures(): function_lines=38 (advisory above 25, limit 50)
using C = StoryFailureClass;
static const std::vector<PlzaFailure> failures{
// The checkpoint's start and the reload.
{"not_at_start", C::WRONG_START},
{"older_save", C::WRONG_START},
{"no_reload", C::RELOAD_FAILED},
// Walks. A walk that ends where the gate is out of view, or facing
// away from the heading it turned to, is a pose not as planned.
{"gate_not_seen", C::POSE_LOST},
{"heading_missed", C::POSE_LOST},
// walk_back_to_zone_road's stops: a catch spot on Wild Zone 1's
// road the objective's distance never reached (03.08).
{"zone_road_missed", C::WALK_SHORT},
// check_out_of_wild_zone's end check: the minimap's arrow still in
// the zone's green after the gate.
{"zone_exit_missed", C::WALK_SHORT},
// A walk held until a fade that never showed.
{"walk_no_end", C::LATE_FADE},
// A walk that never got free control: a banner, text or a fade
// that stayed.
{"walk_not_free", C::INTERRUPT_FAILED},
// A screen the step waits for, not seen: the A prompt a walk ends
// on (a missed gate, the console), the compass, the
// platform, a scene's end.
{"no_prompt", C::SCREEN_UNREAD},
{"heading_unread", C::SCREEN_UNREAD},
{"platform_not_seen", C::SCREEN_UNREAD},
{"scene_quiet", C::SCREEN_UNREAD},
{"scene_timeout", C::SCREEN_UNREAD},
{"scene_stuck", C::SCREEN_UNREAD},
{"scene_unknown", C::SCREEN_UNREAD},
{"scene_never_started", C::SCREEN_UNREAD},
// Battles.
{"battle_failed", C::BATTLE_LOST},
};
return failures;
}

std::optional<StoryFailureClass> plza_failure_class(std::string_view name){
const std::vector<PlzaFailure>& failures = plza_failures();
const auto found = std::find_if(failures.begin(), failures.end(), [name](const PlzaFailure& failure){
return failure.name == name;
});
if (found == failures.end()){
return std::nullopt;
}
return found->failure_class;
}

Pokemon::Outcome plza_failed(std::string name, std::string detail){
const std::optional<StoryFailureClass> failure_class = plza_failure_class(name);
return Pokemon::outcome_failed(std::move(name), std::move(detail), failure_class);
}

std::string_view console_step_failure(std::string_view action){
if (action == "walk_back_to_zone_road"){
return "zone_road_missed";
}
if (action == "check_out_of_wild_zone"){
return "zone_exit_missed";
}
return "operation_failed";
}




} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* PokemonLZA AutoStory - Failures
*
* From: https://github.com/PokemonAutomation/
*
* PLZA's own step failure names, each with its class in the shared list
* (Pokemon_StoryFailureClasses.h), so the run log tags a PLZA failure
* "(class=<id>)" as it does the engine's.
*
*/

#ifndef PokemonAutomation_PokemonLZA_AutoStoryFailures_H
#define PokemonAutomation_PokemonLZA_AutoStoryFailures_H

#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "Pokemon/AutoStory/Pokemon_StoryFailureClasses.h"
#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


// One of PLZA's failure names and its class.
struct PlzaFailure{
std::string_view name;
Pokemon::StoryFailureClass failure_class;
};

// Every failure name PLZA's steps report that the shared list has no class
// for, with the class it takes.
const std::vector<PlzaFailure>& plza_failures();

// The class of PLZA's failure `name`; nothing for a name not in
// plza_failures() (the shared list's names, "operation_failed", keep theirs).
std::optional<Pokemon::StoryFailureClass> plza_failure_class(std::string_view name);

// Pokemon::outcome_failed() with plza_failure_class()'s class for `name`.
Pokemon::Outcome plza_failed(std::string name, std::string detail);

// A console step's failure name when it throws: its own where a console
// run showed what its failures mean, else "operation_failed".
std::string_view console_step_failure(std::string_view action);

// Registers the tests of the names' classes.
void add_tests_AutoStoryFailures(UnitTestDatabase& database);



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/* PokemonLZA AutoStory - Failures Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "Common/Cpp/TestRunners/UnitTest.h"
#include "Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h"
#include "PokemonLZA_AutoStoryFailures.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{

namespace{

struct FailureClassCase{
std::string name;
struct Input{
std::string failure;
};
Input input;
struct Expectation{
// The class the log tags; none: no tag.
std::optional<std::string_view> tag;
};
Expectation expectation;
};

const FailureClassCase FAILURE_CLASS_CASES[] = {
{.name = "a missed gate is a pose lost", .input = {.failure = "gate_not_seen"}, .expectation = {.tag = "class=pose_lost"}},
{.name = "no A prompt is a screen unread", .input = {.failure = "no_prompt"}, .expectation = {.tag = "class=screen_unread"}},
{.name = "a road stop missed is a walk short", .input = {.failure = "zone_road_missed"}, .expectation = {.tag = "class=walk_short"}},
{.name = "a zone exit missed is a walk short", .input = {.failure = "zone_exit_missed"}, .expectation = {.tag = "class=walk_short"}},
{.name = "not at the start is a wrong start", .input = {.failure = "not_at_start"}, .expectation = {.tag = "class=wrong_start"}},
{.name = "a shared name keeps the shared class", .input = {.failure = "operation_failed"}, .expectation = {.tag = "class=operation_failed"}},
{.name = "an unknown name has no class", .input = {.failure = "no_such_failure"}, .expectation = {.tag = std::nullopt}},
};

UnitTestResult check_failure_class(const FailureClassCase& test){
const std::string line = Pokemon::to_string_with_class(plza_failed(test.input.failure, "detail"));
const size_t tag = line.find("class=");
if (!test.expectation.tag){
if (tag != std::string::npos){
return std::format("Expected no class, got \"{}\".", line);
}
return true;
}
if (line.find(*test.expectation.tag) == std::string::npos){
return std::format("Expected \"{}\" in \"{}\".", *test.expectation.tag, line);
}
return true;
}

// A table of PLZA's names, none of which the shared list classes: the
// game's class would hide the list's.
struct NameTableCase{
std::string name;
struct Input{
const std::vector<PlzaFailure>& (*table)();
};
Input input;
struct Expectation{
// The names the shared list already has.
std::vector<std::string_view> shared;
};
Expectation expectation;
};

const NameTableCase NAME_TABLE_CASES[] = {
{.name = "no PLZA name is a shared one", .input = {.table = plza_failures}, .expectation = {.shared = {}}},
};

UnitTestResult check_name_table(const NameTableCase& test){
std::vector<std::string_view> shared;
for (const PlzaFailure& failure : test.input.table()){
if (Pokemon::story_failure_class_of_outcome(failure.name)){
shared.push_back(failure.name);
}
}
if (shared != test.expectation.shared){
std::string names;
for (std::string_view name : shared){
names += std::format(" {}", name);
}
return std::format("In the shared list already:{}", names);
}
return true;
}

// The failure name of a console step that throws.
struct ConsoleStepCase{
std::string name;
struct Input{
std::string action;
};
Input input;
struct Expectation{
std::string failure;
};
Expectation expectation;
};

const ConsoleStepCase CONSOLE_STEP_CASES[] = {
{.name = "03.08's walk back to the road", .input = {.action = "walk_back_to_zone_road"}, .expectation = {.failure = "zone_road_missed"}},
{.name = "the check out of the wild zone", .input = {.action = "check_out_of_wild_zone"}, .expectation = {.failure = "zone_exit_missed"}},
{.name = "03.08's catches keep the shared name", .input = {.action = "catch_in_zone"}, .expectation = {.failure = "operation_failed"}},
};

UnitTestResult check_console_step(const ConsoleStepCase& test){
const std::string_view failure = console_step_failure(test.input.action);
if (failure != test.expectation.failure){
return std::format("Expected {}, got {}.", test.expectation.failure, failure);
}
return true;
}

class Test_FailureClass : public UnitTest{
public:
explicit Test_FailureClass(const FailureClassCase& test)
: UnitTest(std::format("PokemonLZA::AutoStory - Failure class: {}", test.name))
, m_test(test)
{}
virtual UnitTestResult run(Logger&, CancellableScope&) const override{
return check_failure_class(m_test);
}

private:
const FailureClassCase& m_test;
};

class Test_NameTable : public UnitTest{
public:
explicit Test_NameTable(const NameTableCase& test)
: UnitTest(std::format("PokemonLZA::AutoStory - Failure class: {}", test.name))
, m_test(test)
{}
virtual UnitTestResult run(Logger&, CancellableScope&) const override{
return check_name_table(m_test);
}

private:
const NameTableCase& m_test;
};

class Test_ConsoleStep : public UnitTest{
public:
explicit Test_ConsoleStep(const ConsoleStepCase& test)
: UnitTest(std::format("PokemonLZA::AutoStory - Console step failure: {}", test.name))
, m_test(test)
{}
virtual UnitTestResult run(Logger&, CancellableScope&) const override{
return check_console_step(m_test);
}

private:
const ConsoleStepCase& m_test;
};

} // namespace


void add_tests_AutoStoryFailures(UnitTestDatabase& database){
for (const FailureClassCase& test : FAILURE_CLASS_CASES){
database.add<Test_FailureClass>(test);
}
for (const NameTableCase& test : NAME_TABLE_CASES){
database.add<Test_NameTable>(test);
}
for (const ConsoleStepCase& test : CONSOLE_STEP_CASES){
database.add<Test_ConsoleStep>(test);
}
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Loading
Loading