SILICON
Toggle main menu visibility
Loading...
Searching...
No Matches
factory.cpp
Go to the documentation of this file.
1
/*
2
Copyright (c) 2026. Giulio Cocconi
3
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include "
tests.hpp
"
19
20
#include <
core/io.hpp
>
21
#include <
core/serialization/component_registration.hpp
>
22
#include <
core/serialization/component_registry.hpp
>
23
24
using namespace
SILICON::core
;
25
26
class
FactoryTest
:
public
::testing::Test {
27
protected
:
28
ComponentRegistry
registry
;
29
30
FactoryTest
() {
registerAllComponents
(
registry
); }
31
};
32
33
TEST_F
(
FactoryTest
, CreateReturnsNonNullAndMatchesTypeName)
34
{
35
for
(
const
auto
& typeName : registry.availableTypes()) {
36
auto
comp = registry.create(typeName);
37
ASSERT_NE(comp,
nullptr
);
38
EXPECT_EQ(comp->typeName(), typeName);
39
}
40
}
41
42
TEST_F
(
FactoryTest
, UnknownTypeThrows)
43
{
44
EXPECT_THROW(registry.create(
"NonExistentComponent"
), std::runtime_error);
45
}
46
47
TEST_F
(
FactoryTest
, AvailableTypesIsNonEmpty)
48
{
49
EXPECT_FALSE(registry.availableTypes().empty());
50
}
51
52
TEST_F
(
FactoryTest
, CreatedComponentsHaveEmptyBuses)
53
{
54
for
(
const
auto
& typeName : registry.availableTypes()) {
55
auto
comp = registry.create(typeName);
56
EXPECT_TRUE(comp->getInputs().empty());
57
EXPECT_TRUE(comp->getOutputs().empty());
58
}
59
}
60
61
TEST_F
(
FactoryTest
, CreatedComponentsAreDistinctInstances)
62
{
63
auto
a = registry.create(
"AndGate"
);
64
auto
b = registry.create(
"AndGate"
);
65
EXPECT_NE(a.get(), b.get());
66
}
67
68
TEST_F
(
FactoryTest
, DuplicateRegistrationThrows)
69
{
70
EXPECT_THROW(
registerAllComponents
(registry), std::logic_error);
71
}
72
73
TEST_F
(
FactoryTest
, RegisteredComponentsHaveCatalogMetadata)
74
{
75
for
(
const
auto
& typeName : registry.availableTypes()) {
76
const
auto
metadata = registry.metadata(typeName);
77
EXPECT_FALSE(metadata.displayName.empty()) << typeName;
78
EXPECT_FALSE(metadata.description.empty()) << typeName;
79
}
80
}
81
82
TEST_F
(
FactoryTest
, RegistryMetadataComesFromComponentObject)
83
{
84
for
(
const
auto
& typeName : registry.availableTypes()) {
85
const
auto
component = registry.create(typeName);
86
const
auto
registryMetadata = registry.metadata(typeName);
87
const
auto
objectMetadata = component->metadata();
88
89
EXPECT_EQ(registryMetadata.displayName, objectMetadata.displayName) << typeName;
90
EXPECT_EQ(registryMetadata.description, objectMetadata.description) << typeName;
91
EXPECT_EQ(registryMetadata.category, objectMetadata.category) << typeName;
92
EXPECT_EQ(registryMetadata.portRole, objectMetadata.portRole) << typeName;
93
}
94
}
95
96
TEST_F
(
FactoryTest
, OnlyBoundaryIoComponentsDeclarePortRoles)
97
{
98
EXPECT_EQ(registry.metadata(
DummyInputComponent::Type
).portRole,
PortRole::Input
);
99
EXPECT_EQ(registry.metadata(
DummyBusInputComponent::Type
).portRole,
PortRole::Input
);
100
EXPECT_EQ(registry.metadata(
DummyOutputComponent::Type
).portRole,
PortRole::Output
);
101
EXPECT_EQ(registry.metadata(
DummyBusOutputComponent::Type
).portRole,
PortRole::Output
);
102
EXPECT_EQ(registry.metadata(
ConstantComponent::Type
).portRole,
PortRole::None
);
103
}
FactoryTest
Definition
factory.cpp:26
FactoryTest::FactoryTest
FactoryTest()
Definition
factory.cpp:30
FactoryTest::registry
ComponentRegistry registry
Definition
factory.cpp:28
SILICON::core::ComponentRegistry
Definition
component_registry.hpp:35
SILICON::core::ConstantComponent::Type
static constexpr std::string_view Type
Definition
io.hpp:31
SILICON::core::DummyBusInputComponent::Type
static constexpr std::string_view Type
Definition
io.hpp:91
SILICON::core::DummyBusOutputComponent::Type
static constexpr std::string_view Type
Definition
io.hpp:131
SILICON::core::DummyInputComponent::Type
static constexpr std::string_view Type
Definition
io.hpp:69
SILICON::core::DummyOutputComponent::Type
static constexpr std::string_view Type
Definition
io.hpp:114
component_registration.hpp
component_registry.hpp
TEST_F
TEST_F(FactoryTest, CreateReturnsNonNullAndMatchesTypeName)
Definition
factory.cpp:33
io.hpp
SILICON::core
Definition
activeKeyGuard.hpp:28
SILICON::core::registerAllComponents
void registerAllComponents(ComponentRegistry ®istry)
Definition
component_registration.cpp:41
SILICON::core::PortRole::Output
@ Output
Definition
component.hpp:69
SILICON::core::PortRole::Input
@ Input
Definition
component.hpp:68
SILICON::core::PortRole::None
@ None
Definition
component.hpp:67
tests.hpp
tests
factory.cpp
Generated by
1.17.0