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 @@ -14,7 +14,7 @@
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "Common/Qt/QtThreadPool_Tests.h"
#include "Kernels/Kernels_Tests.h"
#include "Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h"
#include "Pokemon/AutoStory/Pokemon_AutoStory_Tests.h"
#include "CommonFramework/AudioPipeline/AudioInfo_Tests.h"
#include "CommonFramework/AudioPipeline/Tools/ChannelMixer.h"
#include "CommonFramework/AudioPipeline/SinkPipeline_Tests.h"
Expand Down Expand Up @@ -71,7 +71,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){
add_tests_AudioFileLoader(ret);
OCR::add_tests(ret);
Kernels::add_tests(ret);
Pokemon::add_tests_AutoStorySelect(ret);
Pokemon::add_tests_AutoStory(ret);
NintendoSwitch::add_tests_CheckOnlineDetector(ret);
NintendoSwitch::add_tests_FailedToConnectDetector(ret);
NintendoSwitch::add_tests_UpdatePopupDetector(ret);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Story Range Options
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <utility>
#include "Pokemon_StoryRangeOptions.h"

namespace PokemonAutomation{
namespace Pokemon{


StoryRangeOptions::DropdownLabels StoryRangeOptions::default_labels(){
return DropdownLabels{
.start = "<b>Start checkpoint:</b><br>The program starts from this checkpoint's save.",
.end = "<b>End checkpoint:</b><br>The program stops after this checkpoint.",
};
}

StoryRangeOptions::StoryRangeOptions(
std::string_view label,
const StringSelectDatabase& checkpoints,
LockMode end_lock,
DropdownLabels labels
)
: GroupOption(std::string(label), LockMode::LOCK_WHILE_RUNNING)
, START_CHECKPOINT(
std::move(labels.start),
checkpoints,
LockMode::LOCK_WHILE_RUNNING,
/*default_index=*/0
)
, END_CHECKPOINT(
std::move(labels.end),
checkpoints,
end_lock,
/*default_index=*/checkpoints.size() == 0 ? 0 : checkpoints.size() - 1
)
{
PA_ADD_OPTION(START_CHECKPOINT);
PA_ADD_OPTION(END_CHECKPOINT);
}

size_t StoryRangeOptions::start_index() const{
return START_CHECKPOINT.index();
}

size_t StoryRangeOptions::end_index() const{
return END_CHECKPOINT.index();
}

std::string StoryRangeOptions::check_validity() const{
if (start_index() > end_index()){
return "The start checkpoint comes after the end checkpoint.";
}
return GroupOption::check_validity();
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Story Range Options
*
* From: https://github.com/PokemonAutomation/
*
* The start and end checkpoint dropdowns of an AutoStory program, over one
* list of the story's checkpoints, and the check that the start does not
* come after the end.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryRangeOptions_H
#define PokemonAutomation_Pokemon_StoryRangeOptions_H

#include <string>
#include <string_view>
#include "Common/Cpp/Options/GroupOption.h"
#include "CommonTools/Options/StringSelectOption.h"

namespace PokemonAutomation{
namespace Pokemon{


// The options in an AutoStory program that pick the range of checkpoints
// to run.
class StoryRangeOptions : public GroupOption{
public:
// The labels of the two dropdowns.
struct DropdownLabels{
std::string start;
std::string end;
};
static DropdownLabels default_labels();

// `checkpoints`: the dropdown database (make_story_select_database()),
Comment thread
dayfine marked this conversation as resolved.
Resolved
// kept alive by the caller. The dropdowns start on its first and last
// entries.
// `end_lock`: UNLOCK_WHILE_RUNNING for a program that reads the end
// after each checkpoint, so the end can be moved during a run.
StoryRangeOptions(
std::string_view label,
const StringSelectDatabase& checkpoints,
LockMode end_lock = LockMode::LOCK_WHILE_RUNNING,
DropdownLabels labels = default_labels()
);

// The checkpoints chosen, by index in the database, both inclusive.
size_t start_index() const;
size_t end_index() const;
// Checks that the start checkpoint does not come after the end.
virtual std::string check_validity() const override;

StringSelectOption START_CHECKPOINT;
StringSelectOption END_CHECKPOINT;
};



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/* Story Range Options Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "Common/Cpp/TestRunners/UnitTest.h"
#include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h"
#include "Pokemon_StoryRangeOptions.h"
#include "Pokemon_StoryRangeOptions_Tests.h"

namespace PokemonAutomation{
namespace Pokemon{

namespace{

// A stand-in for a story checkpoint: only its name is read.
struct NamedCheckpoint{
std::string label;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need both name and label here?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make_story_select_database() labels each entry with list[i]->name(), a member function as on the real checkpoint classes, so the stand-in needs name(); label is just the string it returns. They hold the same value, but neither can go: the template needs the function and the function needs somewhere to keep the string. Pokemon_AutoStorySelect_Tests.cpp uses the same stand-in.

std::string name() const{
return label;
}
};

// Two tutorial checkpoints, then three of the main story, as SV's list is
// laid out.
const std::vector<std::unique_ptr<NamedCheckpoint>>& story_list(){
static const std::vector<std::unique_ptr<NamedCheckpoint>> list = []{
std::vector<std::unique_ptr<NamedCheckpoint>> ret;
for (std::string_view label : {"tutorial 0", "tutorial 1", "main 0", "main 1", "main 2"}){
ret.emplace_back(std::make_unique<NamedCheckpoint>(NamedCheckpoint{.label = std::string(label)}));
}
return ret;
}();
return list;
}

// The main story's dropdowns: entries 2..4 of the list, slugged 2..4.
const StringSelectDatabase& main_story_database(){
static const StringSelectDatabase database = make_story_select_database(
story_list(),
/*first=*/2,
/*last=*/story_list().size()
);
return database;
}

// A range chosen in the main story's dropdowns and what the group reads from it.
struct StoryRangeCase{
// The start and end set by slug, as saved settings load them; empty
// leaves the default.
struct Input{
std::string start_slug;
std::string end_slug;
};

// The indexes in the dropdown, and whether the range is valid.
struct Expectation{
size_t start;
size_t end;
bool valid;
};

std::string name;
Input input;
Expectation expectation;
};

class Test_StoryRange : public UnitTest{
public:
Test_StoryRange(StoryRangeCase test_case)
: UnitTest(std::format("Pokemon::StoryRangeOptions - {}", test_case.name))
, m_case(std::move(test_case))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
StoryRangeOptions range("Checkpoints", main_story_database());
if (!m_case.input.start_slug.empty()){
range.START_CHECKPOINT.set_by_slug(m_case.input.start_slug);
}
if (!m_case.input.end_slug.empty()){
range.END_CHECKPOINT.set_by_slug(m_case.input.end_slug);
}
const StoryRangeCase::Expectation& expected = m_case.expectation;
if (range.start_index() != expected.start || range.end_index() != expected.end){
return std::format(
"Expected indexes {} to {}, got {} to {}",
expected.start, expected.end, range.start_index(), range.end_index()
);
}
const std::string error = range.check_validity();
if (error.empty() != expected.valid){
return expected.valid ? std::format("Expected a valid range, got: {}", error) : "Expected the range refused.";
}
if (!expected.valid && error != "The start checkpoint comes after the end checkpoint."){
return std::format("Unexpected message: {}", error);
}
return true;
}

private:
const StoryRangeCase m_case;
};

const StoryRangeCase STORY_RANGE_CASES[] = {
{
.name = "the whole database by default",
.input = {.start_slug = "", .end_slug = ""},
.expectation = {.start = 0, .end = 2, .valid = true},
},
{
.name = "slugs are whole-list indexes, indexes are the dropdown's",
.input = {.start_slug = "3", .end_slug = "4"},
.expectation = {.start = 1, .end = 2, .valid = true},
},
{
.name = "one checkpoint",
.input = {.start_slug = "3", .end_slug = "3"},
.expectation = {.start = 1, .end = 1, .valid = true},
},
{
.name = "a start after the end is refused",
.input = {.start_slug = "4", .end_slug = "2"},
.expectation = {.start = 2, .end = 0, .valid = false},
},
};

// The dropdown labels a program passes, and the labels the dropdowns show.
struct StoryRangeLabelsCase{
// The labels passed to the constructor; none uses the default.
struct Input{
std::optional<StoryRangeOptions::DropdownLabels> labels;
};

// The labels of the start and end dropdowns.
struct Expectation{
std::string start;
std::string end;
};

std::string name;
Input input;
Expectation expectation;
};

class Test_StoryRangeLabels : public UnitTest{
public:
Test_StoryRangeLabels(StoryRangeLabelsCase test_case)
: UnitTest(std::format("Pokemon::StoryRangeOptions labels - {}", test_case.name))
, m_case(std::move(test_case))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
const std::optional<StoryRangeOptions::DropdownLabels>& labels = m_case.input.labels;
const StoryRangeOptions range = labels
? StoryRangeOptions("Checkpoints", main_story_database(), LockMode::LOCK_WHILE_RUNNING, *labels)
: StoryRangeOptions("Checkpoints", main_story_database());
const StoryRangeLabelsCase::Expectation& expected = m_case.expectation;
if (range.START_CHECKPOINT.label() != expected.start || range.END_CHECKPOINT.label() != expected.end){
return std::format(
"Expected labels \"{}\" and \"{}\", got \"{}\" and \"{}\"",
expected.start, expected.end, range.START_CHECKPOINT.label(), range.END_CHECKPOINT.label()
);
}
return true;
}

private:
const StoryRangeLabelsCase m_case;
};

const StoryRangeLabelsCase STORY_RANGE_LABELS_CASES[] = {
{
.name = "the shared labels by default",
.input = {.labels = std::nullopt},
.expectation = {
.start = "<b>Start checkpoint:</b><br>The program starts from this checkpoint's save.",
.end = "<b>End checkpoint:</b><br>The program stops after this checkpoint.",
},
},
{
.name = "a program keeps its own labels",
.input = {.labels = StoryRangeOptions::DropdownLabels{.start = "<b>Start Checkpoint:</b>", .end = "<b>End Checkpoint:</b>"}},
.expectation = {.start = "<b>Start Checkpoint:</b>", .end = "<b>End Checkpoint:</b>"},
},
};

} // namespace


void add_tests_StoryRangeOptions(UnitTestDatabase& database){
for (const StoryRangeCase& test_case : STORY_RANGE_CASES){
database.add<Test_StoryRange>(test_case);
}
for (const StoryRangeLabelsCase& test_case : STORY_RANGE_LABELS_CASES){
database.add<Test_StoryRangeLabels>(test_case);
}
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Story Range Options Tests
*
* From: https://github.com/PokemonAutomation/
Comment thread
dayfine marked this conversation as resolved.
Resolved
*
*/

#ifndef PokemonAutomation_Pokemon_StoryRangeOptions_Tests_H
#define PokemonAutomation_Pokemon_StoryRangeOptions_Tests_H

#include "Common/Cpp/TestRunners/UnitTestDatabase.h"

namespace PokemonAutomation{
namespace Pokemon{


// Registers unit tests for the shared start/end checkpoint range options.
void add_tests_StoryRangeOptions(UnitTestDatabase& database);



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Loading
Loading