SILICON
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>
23
24using namespace SILICON::core;
25
26class FactoryTest : public ::testing::Test {
27protected:
29
31};
32
33TEST_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
42TEST_F(FactoryTest, UnknownTypeThrows)
43{
44 EXPECT_THROW(registry.create("NonExistentComponent"), std::runtime_error);
45}
46
47TEST_F(FactoryTest, AvailableTypesIsNonEmpty)
48{
49 EXPECT_FALSE(registry.availableTypes().empty());
50}
51
52TEST_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
61TEST_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
68TEST_F(FactoryTest, DuplicateRegistrationThrows)
69{
70 EXPECT_THROW(registerAllComponents(registry), std::logic_error);
71}
72
73TEST_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
82TEST_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
96TEST_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}
ComponentRegistry registry
Definition factory.cpp:28
static constexpr std::string_view Type
Definition io.hpp:31
static constexpr std::string_view Type
Definition io.hpp:91
static constexpr std::string_view Type
Definition io.hpp:131
static constexpr std::string_view Type
Definition io.hpp:69
static constexpr std::string_view Type
Definition io.hpp:114
TEST_F(FactoryTest, CreateReturnsNonNullAndMatchesTypeName)
Definition factory.cpp:33
void registerAllComponents(ComponentRegistry &registry)