|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include <memory> |
| 3 | + |
| 4 | +#include "../Helpers/Classes/Basic.hpp" |
| 5 | +#include "../Helpers/Classes/CirularDependency.hpp" |
| 6 | +#include "SevenBit/DI/Exceptions.hpp" |
| 7 | +#include "SevenBit/DI/ServiceCollection.hpp" |
| 8 | + |
| 9 | +class BasicUniqFactoryTest : public testing::Test |
| 10 | +{ |
| 11 | + protected: |
| 12 | + static void SetUpTestSuite() {} |
| 13 | + |
| 14 | + BasicUniqFactoryTest() {} |
| 15 | + |
| 16 | + void SetUp() override {} |
| 17 | + |
| 18 | + void TearDown() override {} |
| 19 | + |
| 20 | + ~BasicUniqFactoryTest() override = default; |
| 21 | + |
| 22 | + static void TearDownTestSuite() {} |
| 23 | +}; |
| 24 | + |
| 25 | +// buildServiceProvider Tests |
| 26 | + |
| 27 | +TEST_F(BasicUniqFactoryTest, ShouldFailGetServiceDueToAlreadyRegisteredService) |
| 28 | +{ |
| 29 | + sb::di::ServiceCollection collection; |
| 30 | + |
| 31 | + collection.addSingleton([] { return std::make_unique<TestClass1>(); }); |
| 32 | + collection.addSingleton([] { return std::make_unique<TestClass1>(); }); |
| 33 | + collection.addSingleton([] { return std::make_unique<TestClass2>(); }); |
| 34 | + |
| 35 | + sb::di::ServiceProviderOptions options; |
| 36 | + options.checkServiceGlobalUniqueness = true; |
| 37 | + EXPECT_THROW(collection.buildServiceProvider(options), sb::di::ServiceAlreadyRegisteredException); |
| 38 | +} |
| 39 | + |
| 40 | +TEST_F(BasicUniqFactoryTest, ShouldNotFailGetServiceDueToAlreadyRegisteredService) |
| 41 | +{ |
| 42 | + sb::di::ServiceCollection collection; |
| 43 | + |
| 44 | + collection.addSingleton([] { return std::make_unique<TestClass1>(); }); |
| 45 | + collection.addSingleton([] { return std::make_unique<TestClass1>(); }); |
| 46 | + collection.addSingleton([] { return std::make_unique<TestClass2>(); }); |
| 47 | + |
| 48 | + sb::di::ServiceProviderOptions options; |
| 49 | + options.checkServiceGlobalUniqueness = false; |
| 50 | + EXPECT_NO_THROW(collection.buildServiceProvider(options)); |
| 51 | +} |
| 52 | + |
| 53 | +// tryGetService Tests |
| 54 | + |
| 55 | +TEST_F(BasicUniqFactoryTest, ShouldTryGetService) |
| 56 | +{ |
| 57 | + auto provider = sb::di::ServiceCollection{} |
| 58 | + .addSingleton([] { return std::make_unique<TestClass1>(); }) |
| 59 | + .addScoped([] { return std::make_unique<TestClass2>(); }) |
| 60 | + .addTransient([] { return std::make_unique<TestClass3>(); }) |
| 61 | + .buildServiceProvider(); |
| 62 | + |
| 63 | + EXPECT_TRUE(provider.tryGetService<TestClass1>()); |
| 64 | + EXPECT_TRUE(provider.tryGetService<TestClass2>()); |
| 65 | + EXPECT_FALSE(provider.tryGetService<TestClass3>()); |
| 66 | + EXPECT_FALSE(provider.tryGetService<TestClass4>()); |
| 67 | +} |
| 68 | + |
| 69 | +TEST_F(BasicUniqFactoryTest, ShouldFailTryGetServiceDueToCircularDependency) |
| 70 | +{ |
| 71 | + auto provider = sb::di::ServiceCollection{} |
| 72 | + .addSingleton([](CircularDependencyB *b) { return std::make_unique<CircularDependencyA>(b); }) |
| 73 | + .addScoped([](CircularDependencyA *a) { return std::make_unique<CircularDependencyB>(a); }) |
| 74 | + .buildServiceProvider(); |
| 75 | + |
| 76 | + EXPECT_THROW(provider.tryGetService<CircularDependencyA>(), sb::di::CircularDependencyException); |
| 77 | +} |
| 78 | + |
| 79 | +// getService Tests |
| 80 | + |
| 81 | +TEST_F(BasicUniqFactoryTest, ShouldGetService) |
| 82 | +{ |
| 83 | + auto provider = sb::di::ServiceCollection{} |
| 84 | + .addSingleton([] { return std::make_unique<TestClass1>(); }) |
| 85 | + .addScoped([] { return std::make_unique<TestClass2>(); }) |
| 86 | + .addTransient([] { return std::make_unique<TestClass3>(); }) |
| 87 | + .buildServiceProvider(); |
| 88 | + |
| 89 | + EXPECT_NO_THROW(provider.getService<TestClass1>()); |
| 90 | + EXPECT_NO_THROW(provider.getService<TestClass2>()); |
| 91 | + EXPECT_THROW(provider.getService<TestClass3>(), sb::di::ServiceNotFoundException); |
| 92 | + EXPECT_THROW(provider.getService<TestClass4>(), sb::di::ServiceNotFoundException); |
| 93 | +} |
| 94 | +TEST_F(BasicUniqFactoryTest, ShouldFailGetServiceDueToCircularDependency) |
| 95 | +{ |
| 96 | + |
| 97 | + auto provider = sb::di::ServiceCollection{} |
| 98 | + .addSingleton([](CircularDependencyB *b) { return std::make_unique<CircularDependencyA>(b); }) |
| 99 | + .addScoped([](CircularDependencyA *a) { return std::make_unique<CircularDependencyB>(a); }) |
| 100 | + .buildServiceProvider(); |
| 101 | + |
| 102 | + EXPECT_THROW(provider.getService<CircularDependencyA>(), sb::di::CircularDependencyException); |
| 103 | +} |
| 104 | + |
| 105 | +// getServices Tests |
| 106 | + |
| 107 | +TEST_F(BasicUniqFactoryTest, ShouldFailGetServicesDueToCircularDependency) |
| 108 | +{ |
| 109 | + auto provider = sb::di::ServiceCollection{} |
| 110 | + .addSingleton([](CircularDependencyB *b) { return std::make_unique<CircularDependencyA>(b); }) |
| 111 | + .addScoped([](CircularDependencyA *a) { return std::make_unique<CircularDependencyB>(a); }) |
| 112 | + .buildServiceProvider(); |
| 113 | + |
| 114 | + EXPECT_THROW(provider.getServices<CircularDependencyA>(), sb::di::CircularDependencyException); |
| 115 | +} |
| 116 | + |
| 117 | +// tryCreateService Tests |
| 118 | + |
| 119 | +TEST_F(BasicUniqFactoryTest, ShouldTryCreateService) |
| 120 | +{ |
| 121 | + auto provider = sb::di::ServiceCollection{} |
| 122 | + .addSingleton([] { return std::make_unique<TestClass1>(); }) |
| 123 | + .addScoped([] { return std::make_unique<TestClass2>(); }) |
| 124 | + .addTransient([] { return std::make_unique<TestClass3>(); }) |
| 125 | + .buildServiceProvider(); |
| 126 | + |
| 127 | + EXPECT_FALSE(provider.tryCreateService<TestClass1>()); |
| 128 | + EXPECT_FALSE(provider.tryCreateService<TestClass2>()); |
| 129 | + EXPECT_TRUE(provider.tryCreateService<TestClass3>()); |
| 130 | + EXPECT_FALSE(provider.tryCreateService<TestClass4>()); |
| 131 | +} |
| 132 | + |
| 133 | +TEST_F(BasicUniqFactoryTest, ShouldFailTryCreateServiceDueToCircularDependency) |
| 134 | +{ |
| 135 | + auto provider = sb::di::ServiceCollection{} |
| 136 | + .addTransient([](std::unique_ptr<CircularDependencyUniqueB> b) { |
| 137 | + return std::make_unique<CircularDependencyUniqueA>(std::move(b)); |
| 138 | + }) |
| 139 | + .addTransient([](std::unique_ptr<CircularDependencyUniqueA> a) { |
| 140 | + return std::make_unique<CircularDependencyUniqueB>(std::move(a)); |
| 141 | + }) |
| 142 | + .buildServiceProvider(); |
| 143 | + |
| 144 | + EXPECT_THROW(provider.tryCreateService<CircularDependencyUniqueA>(), sb::di::CircularDependencyException); |
| 145 | +} |
| 146 | + |
| 147 | +// createService Tests |
| 148 | + |
| 149 | +TEST_F(BasicUniqFactoryTest, ShouldCreateService) |
| 150 | +{ |
| 151 | + auto provider = sb::di::ServiceCollection{} |
| 152 | + .addSingleton([] { return std::make_unique<TestClass1>(); }) |
| 153 | + .addScoped([] { return std::make_unique<TestClass2>(); }) |
| 154 | + .addTransient([] { return std::make_unique<TestClass3>(); }) |
| 155 | + .buildServiceProvider(); |
| 156 | + |
| 157 | + EXPECT_THROW(provider.createService<TestClass1>(), sb::di::ServiceNotFoundException); |
| 158 | + EXPECT_THROW(provider.createService<TestClass2>(), sb::di::ServiceNotFoundException); |
| 159 | + EXPECT_NO_THROW(provider.createService<TestClass3>()); |
| 160 | + EXPECT_THROW(provider.createService<TestClass4>(), sb::di::ServiceNotFoundException); |
| 161 | +} |
| 162 | + |
| 163 | +TEST_F(BasicUniqFactoryTest, ShouldFailCreateServiceDueToCircularDependency) |
| 164 | +{ |
| 165 | + auto provider = sb::di::ServiceCollection{} |
| 166 | + .addTransient([](std::unique_ptr<CircularDependencyUniqueB> b) { |
| 167 | + return std::make_unique<CircularDependencyUniqueA>(std::move(b)); |
| 168 | + }) |
| 169 | + .addTransient([](std::unique_ptr<CircularDependencyUniqueA> a) { |
| 170 | + return std::make_unique<CircularDependencyUniqueB>(std::move(a)); |
| 171 | + }) |
| 172 | + .buildServiceProvider(); |
| 173 | + |
| 174 | + EXPECT_THROW(provider.createService<CircularDependencyUniqueA>(), sb::di::CircularDependencyException); |
| 175 | +} |
| 176 | + |
| 177 | +// createServiceInPlace Tests |
| 178 | + |
| 179 | +TEST_F(BasicUniqFactoryTest, ShouldCreateServiceInPlace) |
| 180 | +{ |
| 181 | + auto provider = sb::di::ServiceCollection{} |
| 182 | + .addSingleton([] { return std::make_unique<TestClass1>(); }) |
| 183 | + .addScoped([] { return std::make_unique<TestClass2>(); }) |
| 184 | + .addTransient([] { return std::make_unique<TestClass3>(); }) |
| 185 | + .buildServiceProvider(); |
| 186 | + |
| 187 | + EXPECT_THROW(provider.createServiceInPlace<TestClass1>(), sb::di::ServiceNotFoundException); |
| 188 | + EXPECT_THROW(provider.createServiceInPlace<TestClass2>(), sb::di::ServiceNotFoundException); |
| 189 | + EXPECT_NO_THROW(provider.createServiceInPlace<TestClass3>()); |
| 190 | + EXPECT_THROW(provider.createServiceInPlace<TestClass4>(), sb::di::ServiceNotFoundException); |
| 191 | +} |
| 192 | + |
| 193 | +TEST_F(BasicUniqFactoryTest, ShouldFailCreateServiceInPlaceDueToCircularDependency) |
| 194 | +{ |
| 195 | + auto provider = sb::di::ServiceCollection{} |
| 196 | + .addTransient([](std::unique_ptr<CircularDependencyUniqueB> b) { |
| 197 | + return std::make_unique<CircularDependencyUniqueA>(std::move(b)); |
| 198 | + }) |
| 199 | + .addTransient([](std::unique_ptr<CircularDependencyUniqueA> a) { |
| 200 | + return std::make_unique<CircularDependencyUniqueB>(std::move(a)); |
| 201 | + }) |
| 202 | + .buildServiceProvider(); |
| 203 | + |
| 204 | + EXPECT_THROW(provider.createServiceInPlace<CircularDependencyUniqueA>(), sb::di::CircularDependencyException); |
| 205 | +} |
| 206 | + |
| 207 | +// createServices Tests |
| 208 | + |
| 209 | +TEST_F(BasicUniqFactoryTest, ShouldFaildCreateServicesDueToCircularDependency) |
| 210 | +{ |
| 211 | + auto provider = sb::di::ServiceCollection{} |
| 212 | + .addTransient([](std::unique_ptr<CircularDependencyUniqueB> b) { |
| 213 | + return std::make_unique<CircularDependencyUniqueA>(std::move(b)); |
| 214 | + }) |
| 215 | + .addTransient([](std::unique_ptr<CircularDependencyUniqueA> a) { |
| 216 | + return std::make_unique<CircularDependencyUniqueB>(std::move(a)); |
| 217 | + }) |
| 218 | + .buildServiceProvider(); |
| 219 | + |
| 220 | + EXPECT_THROW(provider.createServices<CircularDependencyUniqueA>(), sb::di::CircularDependencyException); |
| 221 | +} |
0 commit comments