-
Notifications
You must be signed in to change notification settings - Fork 955
Expand file tree
/
Copy pathTestNodeDelegateModelRegistry.cpp
More file actions
165 lines (136 loc) · 5.25 KB
/
Copy pathTestNodeDelegateModelRegistry.cpp
File metadata and controls
165 lines (136 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <QtNodes/NodeDelegateModel>
#include <QtNodes/NodeDelegateModelRegistry>
#include <catch2/catch.hpp>
using QtNodes::NodeDelegateModel;
using QtNodes::NodeDelegateModelRegistry;
namespace {
class TestModelWithStaticName : public NodeDelegateModel
{
public:
static QString Name() { return "StaticNameModel"; }
QString name() const override { return "StaticNameModel"; }
QString caption() const override { return "Static Name Model"; }
unsigned int nPorts(QtNodes::PortType) const override { return 0; }
QtNodes::NodeDataType dataType(QtNodes::PortType, QtNodes::PortIndex) const override { return {}; }
void setInData(std::shared_ptr<QtNodes::NodeData>, QtNodes::PortIndex const) override {}
std::shared_ptr<QtNodes::NodeData> outData(QtNodes::PortIndex const) override { return nullptr; }
QWidget* embeddedWidget() override { return nullptr; }
};
class TestModelWithName : public NodeDelegateModel
{
public:
TestModelWithName(const QString &name = "DefaultName")
: _modelName(name)
{}
QString name() const override { return _modelName; }
QString caption() const override { return QString("Model: %1").arg(_modelName); }
unsigned int nPorts(QtNodes::PortType) const override { return 0; }
QtNodes::NodeDataType dataType(QtNodes::PortType, QtNodes::PortIndex) const override { return {}; }
void setInData(std::shared_ptr<QtNodes::NodeData>, QtNodes::PortIndex const) override {}
std::shared_ptr<QtNodes::NodeData> outData(QtNodes::PortIndex const) override { return nullptr; }
QWidget* embeddedWidget() override { return nullptr; }
private:
QString _modelName;
};
} // namespace
TEST_CASE("NodeDelegateModelRegistry registration and creation", "[registry]")
{
NodeDelegateModelRegistry registry;
SECTION("Register model with static Name() method")
{
registry.registerModel<TestModelWithStaticName>();
auto model = registry.create("StaticNameModel");
REQUIRE(model != nullptr);
CHECK(model->name() == "StaticNameModel");
CHECK(model->caption() == "Static Name Model");
}
SECTION("Register model with category")
{
registry.registerModel<TestModelWithName>("CustomCategory");
auto model = registry.create("DefaultName");
REQUIRE(model != nullptr);
CHECK(model->name() == "DefaultName");
CHECK(model->caption() == "Model: DefaultName");
}
SECTION("Register with lambda factory")
{
registry.registerModel(
[]() { return std::make_unique<TestModelWithName>("LambdaModel"); },
"LambdaCategory"
);
auto model = registry.create("LambdaModel");
REQUIRE(model != nullptr);
CHECK(model->name() == "LambdaModel");
CHECK(model->caption() == "Model: LambdaModel");
}
SECTION("Create non-existent model")
{
auto model = registry.create("NonExistentModel");
CHECK(model == nullptr);
}
}
TEST_CASE("NodeDelegateModelRegistry categories", "[registry]")
{
NodeDelegateModelRegistry registry;
SECTION("Register models with categories")
{
registry.registerModel<TestModelWithStaticName>("Category1");
registry.registerModel<TestModelWithName>("Category2");
auto categories = registry.categories();
bool foundCategory1 = false;
bool foundCategory2 = false;
for (const auto &cat : categories) {
if (cat == "Category1") {
foundCategory1 = true;
}
if (cat == "Category2") {
foundCategory2 = true;
}
}
CHECK(foundCategory1);
CHECK(foundCategory2);
CHECK(categories.size() >= 2);
}
SECTION("Registered model names")
{
registry.registerModel<TestModelWithStaticName>();
registry.registerModel<TestModelWithName>("CustomCategory");
auto creators = registry.registeredModelCreators();
bool foundStatic = false;
bool foundDefault = false;
for (const auto &pair : creators) {
if (pair.first == "StaticNameModel") {
foundStatic = true;
}
if (pair.first == "DefaultName") {
foundDefault = true;
}
}
CHECK(foundStatic);
CHECK(foundDefault);
CHECK(creators.size() >= 2);
}
}
TEST_CASE("NodeDelegateModelRegistry models by category", "[registry]")
{
NodeDelegateModelRegistry registry;
registry.registerModel<TestModelWithStaticName>("Inputs");
registry.registerModel<TestModelWithName>("Outputs");
SECTION("Get models by existing category")
{
auto inputModels = registry.registeredModelsCategoryAssociation();
// Check that our categories exist
bool foundInputs = false;
bool foundOutputs = false;
for (const auto &pair : inputModels) {
if (pair.first == "StaticNameModel" && pair.second == "Inputs") {
foundInputs = true;
}
if (pair.first == "DefaultName" && pair.second == "Outputs") {
foundOutputs = true;
}
}
CHECK(foundInputs);
CHECK(foundOutputs);
}
}