|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include <memory> |
| 3 | + |
| 4 | +#include "../../Helpers/Classes/Basic.hpp" |
| 5 | +#include "../../Helpers/Classes/Inherit.hpp" |
| 6 | +#include "../../Helpers/Mocks/ServiceProviderMock.hpp" |
| 7 | +#include <SevenBit/DI/Details/Core/ServiceAliasesCreator.hpp> |
| 8 | +#include <SevenBit/DI/ServiceDescriber.hpp> |
| 9 | + |
| 10 | +class ServiceAliasesCreatorTest : public testing::Test |
| 11 | +{ |
| 12 | + protected: |
| 13 | + static void SetUpTestSuite() {} |
| 14 | + |
| 15 | + ServiceAliasesCreatorTest() {} |
| 16 | + |
| 17 | + void SetUp() override {} |
| 18 | + |
| 19 | + void TearDown() override {} |
| 20 | + |
| 21 | + ~ServiceAliasesCreatorTest() override = default; |
| 22 | + |
| 23 | + static void TearDownTestSuite() {} |
| 24 | +}; |
| 25 | + |
| 26 | +TEST_F(ServiceAliasesCreatorTest, ShouldFailCreateAliasForInvalidService) |
| 27 | +{ |
| 28 | + constexpr sb::di::details::ServiceAliasesCreator creator; |
| 29 | + |
| 30 | + const auto descriptor = sb::di::ServiceDescriber::describeAlias<TestInheritClass3, TestInheritClass5>(); |
| 31 | + |
| 32 | + const sb::di::ServiceInstance external; |
| 33 | + |
| 34 | + EXPECT_THROW(creator.tryCreate(descriptor, &external), sb::di::InvalidServiceException); |
| 35 | +} |
| 36 | + |
| 37 | +TEST_F(ServiceAliasesCreatorTest, ShouldFailCreateAliasForNullService) |
| 38 | +{ |
| 39 | + constexpr sb::di::details::ServiceAliasesCreator creator; |
| 40 | + |
| 41 | + const auto descriptor = sb::di::ServiceDescriber::describeAlias<TestInheritClass3, TestInheritClass5>(); |
| 42 | + |
| 43 | + TestInheritClass5 *test = nullptr; |
| 44 | + const sb::di::ServiceInstance external{std::make_unique<sb::di::details::ExternalService<TestInheritClass5>>(test)}; |
| 45 | + |
| 46 | + EXPECT_THROW(creator.tryCreate(descriptor, &external), sb::di::InvalidServiceException); |
| 47 | +} |
| 48 | + |
| 49 | +TEST_F(ServiceAliasesCreatorTest, ShouldFailCreateAliasForWrongDescriptor) |
| 50 | +{ |
| 51 | + constexpr sb::di::details::ServiceAliasesCreator creator; |
| 52 | + |
| 53 | + const auto descriptor = sb::di::ServiceDescriber::describeSingleton<TestInheritClass3, TestInheritClass5>(); |
| 54 | + |
| 55 | + TestInheritClass5 *test = nullptr; |
| 56 | + const sb::di::ServiceInstance external{std::make_unique<sb::di::details::ExternalService<TestInheritClass5>>(test)}; |
| 57 | + |
| 58 | + EXPECT_THROW(creator.tryCreate(descriptor, &external), sb::di::InjectorException); |
| 59 | +} |
| 60 | + |
| 61 | +TEST_F(ServiceAliasesCreatorTest, ShouldCreate) |
| 62 | +{ |
| 63 | + constexpr sb::di::details::ServiceAliasesCreator creator; |
| 64 | + |
| 65 | + TestInheritClass6 test; |
| 66 | + const sb::di::ServiceInstance external{ |
| 67 | + std::make_unique<sb::di::details::ExternalService<TestInheritClass6>>(&test)}; |
| 68 | + |
| 69 | + const auto instance = |
| 70 | + creator.tryCreate(sb::di::ServiceDescriber::describeAlias<TestInheritClass1, TestInheritClass5>(), &external); |
| 71 | + |
| 72 | + EXPECT_TRUE(instance); |
| 73 | + EXPECT_TRUE(instance.isValid()); |
| 74 | + EXPECT_TRUE(instance.getAs<void>()); |
| 75 | + EXPECT_EQ(instance.tryGetImplementation()->getTypeId(), typeid(TestInheritClass5)); |
| 76 | +} |
| 77 | + |
| 78 | +TEST_F(ServiceAliasesCreatorTest, ShouldNotCreateForNullOriginal) |
| 79 | +{ |
| 80 | + constexpr sb::di::details::ServiceAliasesCreator creator; |
| 81 | + |
| 82 | + TestInheritClass6 test; |
| 83 | + const sb::di::ServiceInstance external{ |
| 84 | + std::make_unique<sb::di::details::ExternalService<TestInheritClass6>>(&test)}; |
| 85 | + |
| 86 | + const auto instance = |
| 87 | + creator.tryCreate(sb::di::ServiceDescriber::describeAlias<TestInheritClass1, TestInheritClass5>(), nullptr); |
| 88 | + |
| 89 | + EXPECT_FALSE(instance); |
| 90 | + EXPECT_FALSE(instance.isValid()); |
| 91 | +} |
| 92 | + |
| 93 | +TEST_F(ServiceAliasesCreatorTest, ShouldCreateAllAliases) |
| 94 | +{ |
| 95 | + sb::di::details::ServiceDescriptorList descriptors{ |
| 96 | + sb::di::ServiceDescriber::describeAlias<TestInheritClass1, TestInheritClass2>()}; |
| 97 | + |
| 98 | + constexpr sb::di::details::ServiceAliasesCreator creator; |
| 99 | + |
| 100 | + TestInheritClass3 test3; |
| 101 | + TestInheritClass4 test4; |
| 102 | + TestInheritClass5 test5; |
| 103 | + sb::di::details::ServiceInstanceList externals{ |
| 104 | + sb::di::ServiceInstance{std::make_unique<sb::di::details::ExternalService<TestInheritClass3>>(&test3)}}; |
| 105 | + externals.add( |
| 106 | + sb::di::ServiceInstance{std::make_unique<sb::di::details::ExternalService<TestInheritClass4>>(&test4)}); |
| 107 | + externals.add( |
| 108 | + sb::di::ServiceInstance{std::make_unique<sb::di::details::ExternalService<TestInheritClass5>>(&test5)}); |
| 109 | + |
| 110 | + const auto instances = |
| 111 | + creator.tryCreateAll(descriptors, [&](const sb::di::ServiceDescriptor &) { return &externals; }); |
| 112 | + |
| 113 | + EXPECT_EQ(instances.size(), 3); |
| 114 | + EXPECT_FALSE(instances.isSealed()); |
| 115 | + EXPECT_TRUE(instances[0].isValid()); |
| 116 | + EXPECT_TRUE(instances[0].getAs<void>()); |
| 117 | + EXPECT_EQ(instances[0].tryGetImplementation()->getTypeId(), typeid(TestInheritClass2)); |
| 118 | + EXPECT_TRUE(instances[1].isValid()); |
| 119 | + EXPECT_TRUE(instances[1].getAs<void>()); |
| 120 | + EXPECT_EQ(instances[1].tryGetImplementation()->getTypeId(), typeid(TestInheritClass2)); |
| 121 | + EXPECT_TRUE(instances[2].isValid()); |
| 122 | + EXPECT_TRUE(instances[2].getAs<void>()); |
| 123 | + EXPECT_EQ(instances[2].tryGetImplementation()->getTypeId(), typeid(TestInheritClass2)); |
| 124 | +} |
| 125 | + |
| 126 | +TEST_F(ServiceAliasesCreatorTest, ShouldNotCreateAllAliasesForEmpty) |
| 127 | +{ |
| 128 | + sb::di::details::ServiceDescriptorList descriptors; |
| 129 | + |
| 130 | + constexpr sb::di::details::ServiceAliasesCreator creator; |
| 131 | + |
| 132 | + sb::di::details::ServiceInstanceList externals; |
| 133 | + |
| 134 | + const auto instances = |
| 135 | + creator.tryCreateAll(descriptors, [&](const sb::di::ServiceDescriptor &) { return &externals; }); |
| 136 | + |
| 137 | + EXPECT_FALSE(instances); |
| 138 | + EXPECT_TRUE(instances.empty()); |
| 139 | +} |
| 140 | + |
| 141 | +TEST_F(ServiceAliasesCreatorTest, ShouldNotCreateAllAliasesForNull) |
| 142 | +{ |
| 143 | + sb::di::details::ServiceDescriptorList descriptors{ |
| 144 | + sb::di::ServiceDescriber::describeAlias<TestInheritClass1, TestInheritClass2>()}; |
| 145 | + |
| 146 | + constexpr sb::di::details::ServiceAliasesCreator creator; |
| 147 | + const auto instances = creator.tryCreateAll( |
| 148 | + descriptors, |
| 149 | + [&](const sb::di::ServiceDescriptor &) -> sb::di::details::ServiceInstanceList * { return nullptr; }); |
| 150 | + |
| 151 | + EXPECT_FALSE(instances); |
| 152 | + EXPECT_TRUE(instances.empty()); |
| 153 | +} |
| 154 | + |
| 155 | +TEST_F(ServiceAliasesCreatorTest, ShouldCreateRestAliases) |
| 156 | +{ |
| 157 | + sb::di::details::ServiceDescriptorList descriptors{ |
| 158 | + sb::di::ServiceDescriber::describeAlias<TestInheritClass1, TestInheritClass2>()}; |
| 159 | + |
| 160 | + constexpr sb::di::details::ServiceAliasesCreator creator; |
| 161 | + |
| 162 | + TestInheritClass3 test3; |
| 163 | + TestInheritClass4 test4; |
| 164 | + TestInheritClass5 test5; |
| 165 | + sb::di::details::ServiceInstanceList externals{ |
| 166 | + sb::di::ServiceInstance{std::make_unique<sb::di::details::ExternalService<TestInheritClass3>>(&test3)}}; |
| 167 | + externals.add( |
| 168 | + sb::di::ServiceInstance{std::make_unique<sb::di::details::ExternalService<TestInheritClass4>>(&test4)}); |
| 169 | + externals.add( |
| 170 | + sb::di::ServiceInstance{std::make_unique<sb::di::details::ExternalService<TestInheritClass5>>(&test5)}); |
| 171 | + |
| 172 | + sb::di::details::ServiceInstanceList instances{ |
| 173 | + sb::di::ServiceInstance{std::make_unique<sb::di::details::ExternalService<TestInheritClass2>>(&test5)}}; |
| 174 | + |
| 175 | + creator.tryCreateRest(descriptors, instances, [&](const sb::di::ServiceDescriptor &) { return &externals; }); |
| 176 | + |
| 177 | + EXPECT_EQ(instances.size(), 3); |
| 178 | + EXPECT_FALSE(instances.isSealed()); |
| 179 | + EXPECT_TRUE(instances[0].isValid()); |
| 180 | + EXPECT_TRUE(instances[0].getAs<void>()); |
| 181 | + EXPECT_EQ(instances[0].tryGetImplementation()->getTypeId(), typeid(TestInheritClass2)); |
| 182 | + EXPECT_TRUE(instances[1].isValid()); |
| 183 | + EXPECT_TRUE(instances[1].getAs<void>()); |
| 184 | + EXPECT_EQ(instances[1].tryGetImplementation()->getTypeId(), typeid(TestInheritClass2)); |
| 185 | + EXPECT_TRUE(instances[2].isValid()); |
| 186 | + EXPECT_TRUE(instances[2].getAs<void>()); |
| 187 | + EXPECT_EQ(instances[2].tryGetImplementation()->getTypeId(), typeid(TestInheritClass2)); |
| 188 | +} |
0 commit comments