Skip to content

Commit b05b256

Browse files
committed
Add substring filter to test program. Port another SV test.
1 parent f21052f commit b05b256

9 files changed

Lines changed: 158 additions & 30 deletions

File tree

Common/Cpp/Options/TextEditOption.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace PokemonAutomation{
1616

1717

1818
struct TextEditOption::Data{
19-
const std::string m_label;
19+
std::string m_label;
2020
const std::string m_default;
2121
const std::string m_placeholder_text;
2222
const bool m_report_all_text_changes;
@@ -80,9 +80,17 @@ std::unique_ptr<ConfigOption> TextEditOption::clone() const{
8080
}
8181
#endif
8282

83-
const std::string& TextEditOption::label() const{
83+
std::string TextEditOption::label() const{
84+
ReadSpinLock lg(m_data->m_lock);
8485
return m_data->m_label;
8586
}
87+
void TextEditOption::set_label(std::string label){
88+
{
89+
ReadSpinLock lg(m_data->m_lock);
90+
m_data->m_label = std::move(label);
91+
}
92+
report_value_changed(this);
93+
}
8694
const std::string& TextEditOption::placeholder_text() const{
8795
return m_data->m_placeholder_text;
8896
}

Common/Cpp/Options/TextEditOption.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ class TextEditOption : public ConfigOptionImpl<TextEditOption>{
4141
);
4242
// virtual std::unique_ptr<ConfigOption> clone() const override;
4343

44-
const std::string& label() const;
44+
std::string label() const;
45+
void set_label(std::string label);
46+
4547
const std::string& placeholder_text() const;
4648
bool signal_all_text_changes() const;
4749

Common/Qt/Options/TextEditWidget.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ TextEditWidget::TextEditWidget(QWidget& parent, TextEditOption& value)
8686

8787
QVBoxLayout* layout = new QVBoxLayout(this);
8888
layout->setContentsMargins(0, 0, 0, 0);
89-
QLabel* label = new QLabel(QString::fromStdString(value.label()), this);
90-
label->setWordWrap(true);
91-
layout->addWidget(label);
89+
m_label = new QLabel(QString::fromStdString(value.label()), this);
90+
m_label->setWordWrap(true);
91+
layout->addWidget(m_label);
9292
m_box = new Box(*this);
9393
m_box->setText(QString::fromStdString(value));
9494
if (value.lock_mode() == LockMode::READ_ONLY){
@@ -99,12 +99,20 @@ TextEditWidget::TextEditWidget(QWidget& parent, TextEditOption& value)
9999
m_value.ConfigOption::add_listener(*this);
100100
}
101101
void TextEditWidget::update_value(){
102-
std::string new_value = (std::string)m_value;
103-
std::string text = m_box->toPlainText().toStdString();
104-
if (new_value == text){
105-
return;
102+
{
103+
std::string new_value = m_value.label();
104+
std::string text = m_label->text().toStdString();
105+
if (new_value != text){
106+
m_label->setText(QString::fromStdString(new_value));
107+
}
108+
}
109+
{
110+
std::string new_value = (std::string)m_value;
111+
std::string text = m_box->toPlainText().toStdString();
112+
if (new_value != text){
113+
m_box->setText(QString::fromStdString(new_value));
114+
}
106115
}
107-
m_box->setText(QString::fromStdString(m_value));
108116
}
109117
void TextEditWidget::on_config_value_changed(void* object){
110118
// This function gets called every time the contents changes.

Common/Qt/Options/TextEditWidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Common/Cpp/Options/TextEditOption.h"
1212
#include "ConfigWidget.h"
1313

14+
class QLabel;
1415
class QTextEdit;
1516

1617
namespace PokemonAutomation{
@@ -33,6 +34,7 @@ class TextEditWidget : public QWidget, public ConfigWidget, public TextEditOptio
3334
class Box;
3435

3536
TextEditOption& m_value;
37+
QLabel* m_label;
3638
QTextEdit* m_box;
3739

3840
bool m_pending_append = false;

SerialPrograms/Source/ComputerPrograms/UnitTestRunner.cpp

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ std::unique_ptr<StatsTracker> UnitTestRunner_Descriptor::make_stats() const{
9898

9999

100100
UnitTestRunner::~UnitTestRunner(){
101+
SUBSTRING_TEST.remove_listener(*this);
101102
RUN_MODE.remove_listener(*this);
102103
}
103104
UnitTestRunner::UnitTestRunner()
@@ -111,25 +112,33 @@ UnitTestRunner::UnitTestRunner()
111112
, RUN_MODE(
112113
"<b>Run Mode:</b>",
113114
{
114-
{RunMode::RUN_ALL, "run-all", "Run All Tests"},
115-
{RunMode::RUN_ONE, "run-one", "Run One Test"},
115+
{RunMode::RUN_ONE, "run-one", "Run One Test"},
116+
{RunMode::RUN_SUBSTRING_MATCH, "run-substring-match", "Run Matching Substrings"},
117+
{RunMode::RUN_ALL, "run-all", "Run All Tests"},
116118
},
117119
LockMode::LOCK_WHILE_RUNNING,
118-
RunMode::RUN_ALL
120+
RunMode::RUN_SUBSTRING_MATCH
119121
)
120122
, SINGLE_TEST_LABEL("<b>Test Name:</b>")
121123
, SINGLE_TEST(
122124
TEST_DATABASE(),
123125
LockMode::LOCK_WHILE_RUNNING,
124126
0
125127
)
128+
, SUBSTRING_TEST_LABEL("<b>Run All Tests Containing This Substring:</b>")
129+
, SUBSTRING_TEST(false, LockMode::LOCK_WHILE_RUNNING, "", "PokemonSwSh")
130+
, TESTS_TO_RUN(
131+
"<b>Tests to Run:</b>",
132+
LockMode::READ_ONLY,
133+
"", ""
134+
)
126135
, PASSED_TESTS(
127-
"<b>Passing Tests</b>",
136+
"<b>Passing Tests:</b>",
128137
LockMode::READ_ONLY,
129138
"", ""
130139
)
131140
, FAILED_TESTS(
132-
"<b>Failing Tests</b>",
141+
"<b>Failing Tests:</b>",
133142
LockMode::READ_ONLY,
134143
"", ""
135144
)
@@ -139,20 +148,51 @@ UnitTestRunner::UnitTestRunner()
139148
PA_ADD_OPTION(RUN_MODE);
140149
PA_ADD_STATIC(SINGLE_TEST_LABEL);
141150
PA_ADD_OPTION(SINGLE_TEST);
151+
PA_ADD_STATIC(SUBSTRING_TEST_LABEL);
152+
PA_ADD_OPTION(SUBSTRING_TEST);
153+
PA_ADD_OPTION(TESTS_TO_RUN);
142154
PA_ADD_OPTION(PASSED_TESTS);
143155
PA_ADD_OPTION(FAILED_TESTS);
144156

145157
UnitTestRunner::on_config_value_changed(this);
146158

147159
RUN_MODE.add_listener(*this);
160+
SUBSTRING_TEST.add_listener(*this);
148161
}
149162
void UnitTestRunner::on_config_value_changed(void* object){
150-
if ((RunMode)RUN_MODE == RunMode::RUN_ONE){
151-
SINGLE_TEST_LABEL.set_visibility(ConfigOptionState::ENABLED);
152-
SINGLE_TEST.set_visibility(ConfigOptionState::ENABLED);
153-
}else{
163+
if (object == &RUN_MODE){
154164
SINGLE_TEST_LABEL.set_visibility(ConfigOptionState::HIDDEN);
155165
SINGLE_TEST.set_visibility(ConfigOptionState::HIDDEN);
166+
SUBSTRING_TEST_LABEL.set_visibility(ConfigOptionState::HIDDEN);
167+
SUBSTRING_TEST.set_visibility(ConfigOptionState::HIDDEN);
168+
TESTS_TO_RUN.set_visibility(ConfigOptionState::HIDDEN);
169+
170+
switch ((RunMode)RUN_MODE){
171+
case RunMode::RUN_ONE:
172+
SINGLE_TEST_LABEL.set_visibility(ConfigOptionState::ENABLED);
173+
SINGLE_TEST.set_visibility(ConfigOptionState::ENABLED);
174+
break;
175+
case RunMode::RUN_SUBSTRING_MATCH:
176+
SUBSTRING_TEST_LABEL.set_visibility(ConfigOptionState::ENABLED);
177+
SUBSTRING_TEST.set_visibility(ConfigOptionState::ENABLED);
178+
TESTS_TO_RUN.set_visibility(ConfigOptionState::ENABLED);
179+
break;
180+
case RunMode::RUN_ALL:
181+
break;
182+
}
183+
return;
184+
}
185+
if (object == &SUBSTRING_TEST){
186+
std::string name = SUBSTRING_TEST;
187+
TESTS_TO_RUN.set("");
188+
size_t count = 0;
189+
for (const auto& test : UNIT_TESTS_ALL()){
190+
if (test.first.contains(name)){
191+
count++;
192+
TESTS_TO_RUN.append(test.first + "\n");
193+
}
194+
}
195+
TESTS_TO_RUN.set_label("<b>Tests to Run: " + tostr_u_commas(count) + "</b>");
156196
}
157197
}
158198

@@ -176,11 +216,8 @@ void UnitTestRunner::program(ProgramEnvironment& env, CancellableScope& scope){
176216

177217
const UnitTestDatabase& all_tests = UNIT_TESTS_ALL();
178218

179-
if ((RunMode)RUN_MODE == RunMode::RUN_ALL){
180-
for (const auto& test : all_tests){
181-
runner.add_test(test.second);
182-
}
183-
}else{
219+
switch ((RunMode)RUN_MODE){
220+
case RunMode::RUN_ONE:{
184221
auto iter = all_tests.find(SINGLE_TEST.slug());
185222
if (iter == all_tests.end()){
186223
throw InternalProgramError(
@@ -190,6 +227,23 @@ void UnitTestRunner::program(ProgramEnvironment& env, CancellableScope& scope){
190227
);
191228
}
192229
runner.add_test(iter->second);
230+
break;
231+
}
232+
case RunMode::RUN_SUBSTRING_MATCH:{
233+
std::string name = SUBSTRING_TEST;
234+
for (const auto& test : all_tests){
235+
if (test.first.contains(name)){
236+
runner.add_test(test.second);
237+
}
238+
}
239+
break;
240+
}
241+
case RunMode::RUN_ALL:{
242+
for (const auto& test : all_tests){
243+
runner.add_test(test.second);
244+
}
245+
break;
246+
}
193247
}
194248

195249
runner.run();

SerialPrograms/Source/ComputerPrograms/UnitTestRunner.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,19 @@ class UnitTestRunner
5555
StringCell RESOURCE_PATH;
5656

5757
enum class RunMode{
58-
RUN_ALL,
5958
RUN_ONE,
59+
RUN_SUBSTRING_MATCH,
60+
RUN_ALL,
6061
};
6162
EnumDropdownOption<RunMode> RUN_MODE;
6263

6364
StaticTextOption SINGLE_TEST_LABEL;
6465
StringSelectCell SINGLE_TEST;
6566

67+
StaticTextOption SUBSTRING_TEST_LABEL;
68+
StringCell SUBSTRING_TEST;
69+
70+
TextEditOption TESTS_TO_RUN;
6671
TextEditOption PASSED_TESTS;
6772
TextEditOption FAILED_TESTS;
6873

SerialPrograms/Source/PokemonSV/Inference/Picnics/PokemonSV_PicnicDetector.cpp

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
*
55
*/
66

7+
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
78
#include "CommonFramework/Globals.h"
89
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
910
#include "PokemonSV_PicnicDetector.h"
1011

11-
#include <iostream>
12-
using std::cout;
13-
using std::endl;
12+
//#include <iostream>
13+
//using std::cout;
14+
//using std::endl;
1415

1516
namespace PokemonAutomation{
1617
namespace NintendoSwitch{
@@ -37,6 +38,44 @@ bool PicnicDetector::detect(const ImageViewRGB32& frame){
3738
}
3839

3940

41+
42+
43+
44+
45+
class Test_PicnicDetector : public UnitTest{
46+
public:
47+
Test_PicnicDetector(
48+
const std::string& image,
49+
bool expected
50+
)
51+
: UnitTest("PokemonSV::PicnicDetector - " + image)
52+
, m_image(UNIT_TEST_RESOURCE_PATH() + image)
53+
, m_expected(expected)
54+
{}
55+
56+
virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{
57+
PicnicDetector detector;
58+
ImageRGB32 image(m_image);
59+
return detector.detect(image) == m_expected;
60+
};
61+
62+
private:
63+
std::string m_image;
64+
bool m_expected;
65+
};
66+
67+
68+
69+
70+
void add_tests_PicnicDetector(UnitTestDatabase& database){
71+
database.add<Test_PicnicDetector>("PokemonSV/PicnicDetector/macOS/Picinic_1_True.png", true);
72+
database.add<Test_PicnicDetector>("PokemonSV/PicnicDetector/macOS/Picnic_0_True.png", true);
73+
}
74+
75+
76+
77+
78+
4079
}
4180
}
4281
}

SerialPrograms/Source/PokemonSV/Inference/Picnics/PokemonSV_PicnicDetector.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define PokemonAutomation_PokemonSV_PicnicDetector_H
99

1010
#include "Common/Cpp/Color.h"
11+
#include "Common/Cpp/TestRunners/UnitTest.h"
1112
#include "CommonFramework/ImageTools/ImageBoxes.h"
1213
#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h"
1314
#include "CommonTools/VisualDetector.h"
@@ -41,6 +42,14 @@ class PicnicWatcher : public DetectorToFinder<PicnicDetector>{
4142

4243

4344

45+
46+
47+
48+
void add_tests_PicnicDetector(UnitTestDatabase& database);
49+
50+
51+
52+
4453
}
4554
}
4655
}

SerialPrograms/Source/PokemonSV/PokemonSV_Tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
88
#include "Inference/PokemonSV_WhiteButtonDetector.h"
99
#include "Inference/Map/PokemonSV_MapDetector.h"
10+
#include "Inference/Picnics/PokemonSV_PicnicDetector.h"
1011
#include "PokemonSV_Tests.h"
1112

1213
namespace PokemonAutomation{
@@ -18,7 +19,7 @@ namespace PokemonSV{
1819
void add_tests(UnitTestDatabase& database){
1920
add_tests_WhiteButtonDetector(database);
2021
add_tests_MapDetector(database);
21-
22+
add_tests_PicnicDetector(database);
2223
}
2324

2425

0 commit comments

Comments
 (0)