|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include <memory> |
| 3 | + |
| 4 | +#include "../Helpers/Classes/Basic.hpp" |
| 5 | +#include "SevenBit/DI/Exceptions.hpp" |
| 6 | +#include "SevenBit/DI/ServiceCollection.hpp" |
| 7 | + |
| 8 | +class BasicExternalTest : public testing::Test |
| 9 | +{ |
| 10 | + protected: |
| 11 | + static void SetUpTestSuite() {} |
| 12 | + |
| 13 | + BasicExternalTest() {} |
| 14 | + |
| 15 | + void SetUp() override {} |
| 16 | + |
| 17 | + void TearDown() override {} |
| 18 | + |
| 19 | + ~BasicExternalTest() override = default; |
| 20 | + |
| 21 | + static void TearDownTestSuite() {} |
| 22 | +}; |
| 23 | + |
| 24 | +// buildServiceProvider Tests |
| 25 | + |
| 26 | +TEST_F(BasicExternalTest, ShouldFailGetServiceDueToAlreadyRegisteredService) |
| 27 | +{ |
| 28 | + sb::di::ServiceCollection collection; |
| 29 | + |
| 30 | + TestClass1 test1; |
| 31 | + TestClass2 test2; |
| 32 | + collection.addSingleton(&test1); |
| 33 | + collection.addSingleton(&test1); |
| 34 | + collection.addSingleton(&test2); |
| 35 | + |
| 36 | + sb::di::ServiceProviderOptions options; |
| 37 | + options.checkServiceGlobalUniqueness = true; |
| 38 | + EXPECT_THROW(collection.buildServiceProvider(options), sb::di::ServiceAlreadyRegisteredException); |
| 39 | +} |
| 40 | + |
| 41 | +TEST_F(BasicExternalTest, ShouldNotFailGetServiceDueToAlreadyRegisteredService) |
| 42 | +{ |
| 43 | + sb::di::ServiceCollection collection; |
| 44 | + |
| 45 | + TestClass1 test1; |
| 46 | + TestClass2 test2; |
| 47 | + collection.addSingleton(&test1); |
| 48 | + collection.addSingleton(&test1); |
| 49 | + collection.addSingleton(&test2); |
| 50 | + |
| 51 | + sb::di::ServiceProviderOptions options; |
| 52 | + options.checkServiceGlobalUniqueness = false; |
| 53 | + EXPECT_NO_THROW(collection.buildServiceProvider(options)); |
| 54 | +} |
| 55 | + |
| 56 | +// tryGetService Tests |
| 57 | + |
| 58 | +TEST_F(BasicExternalTest, ShouldTryGetService) |
| 59 | +{ |
| 60 | + TestClass1 test1; |
| 61 | + TestClass2 test2; |
| 62 | + TestClass3 test3; |
| 63 | + auto provider = sb::di::ServiceCollection{} |
| 64 | + .addSingleton(&test1) |
| 65 | + .addSingleton(&test2) |
| 66 | + .addSingleton(&test3) |
| 67 | + .buildServiceProvider(); |
| 68 | + |
| 69 | + EXPECT_EQ(provider.tryGetService<TestClass1>(), &test1); |
| 70 | + EXPECT_EQ(provider.tryGetService<TestClass2>(), &test2); |
| 71 | + EXPECT_EQ(provider.tryGetService<TestClass3>(), &test3); |
| 72 | + EXPECT_FALSE(provider.tryGetService<TestClass4>()); |
| 73 | +} |
| 74 | + |
| 75 | +// getService Tests |
| 76 | + |
| 77 | +TEST_F(BasicExternalTest, ShouldGetService) |
| 78 | +{ |
| 79 | + TestClass1 test1; |
| 80 | + TestClass2 test2; |
| 81 | + TestClass3 test3; |
| 82 | + auto provider = sb::di::ServiceCollection{} |
| 83 | + .addSingleton(&test1) |
| 84 | + .addSingleton(&test2) |
| 85 | + .addSingleton(&test3) |
| 86 | + .buildServiceProvider(); |
| 87 | + |
| 88 | + EXPECT_EQ(&provider.getService<TestClass1>(), &test1); |
| 89 | + EXPECT_EQ(&provider.getService<TestClass2>(), &test2); |
| 90 | + EXPECT_EQ(&provider.getService<TestClass3>(), &test3); |
| 91 | + EXPECT_THROW(provider.getService<TestClass4>(), sb::di::ServiceNotFoundException); |
| 92 | +} |
| 93 | + |
| 94 | +TEST_F(BasicExternalTest, ShouldGetServices) |
| 95 | +{ |
| 96 | + TestClass1 test1; |
| 97 | + TestClass2 test2; |
| 98 | + TestClass3 test3; |
| 99 | + auto provider = sb::di::ServiceCollection{} |
| 100 | + .addSingleton(&test1) |
| 101 | + .addSingleton(&test2) |
| 102 | + .addSingleton(&test3) |
| 103 | + .buildServiceProvider(); |
| 104 | + |
| 105 | + EXPECT_EQ(provider.getServices<TestClass1>().size(), 1); |
| 106 | + EXPECT_EQ(provider.getServices<TestClass1>()[0], &test1); |
| 107 | + EXPECT_EQ(provider.getServices<TestClass2>().size(), 1); |
| 108 | + EXPECT_EQ(provider.getServices<TestClass2>()[0], &test2); |
| 109 | + EXPECT_EQ(provider.getServices<TestClass3>().size(), 1); |
| 110 | + EXPECT_EQ(provider.getServices<TestClass3>()[0], &test3); |
| 111 | +} |
| 112 | + |
| 113 | +// tryCreateService Tests |
| 114 | + |
| 115 | +TEST_F(BasicExternalTest, ShouldTryCreateService) |
| 116 | +{ |
| 117 | + TestClass1 test1; |
| 118 | + TestClass2 test2; |
| 119 | + TestClass3 test3; |
| 120 | + auto provider = sb::di::ServiceCollection{} |
| 121 | + .addSingleton(&test1) |
| 122 | + .addSingleton(&test2) |
| 123 | + .addSingleton(&test3) |
| 124 | + .buildServiceProvider(); |
| 125 | + |
| 126 | + EXPECT_FALSE(provider.tryCreateService<TestClass1>()); |
| 127 | + EXPECT_FALSE(provider.tryCreateService<TestClass2>()); |
| 128 | + EXPECT_FALSE(provider.tryCreateService<TestClass3>()); |
| 129 | + EXPECT_FALSE(provider.tryCreateService<TestClass4>()); |
| 130 | +} |
| 131 | + |
| 132 | +// createService Tests |
| 133 | + |
| 134 | +TEST_F(BasicExternalTest, ShouldCreateService) |
| 135 | +{ |
| 136 | + TestClass1 test1; |
| 137 | + TestClass2 test2; |
| 138 | + TestClass3 test3; |
| 139 | + auto provider = sb::di::ServiceCollection{} |
| 140 | + .addSingleton(&test1) |
| 141 | + .addSingleton(&test2) |
| 142 | + .addSingleton(&test3) |
| 143 | + .buildServiceProvider(); |
| 144 | + |
| 145 | + EXPECT_THROW(provider.createService<TestClass1>(), sb::di::ServiceNotFoundException); |
| 146 | + EXPECT_THROW(provider.createService<TestClass2>(), sb::di::ServiceNotFoundException); |
| 147 | + EXPECT_THROW(provider.createService<TestClass3>(), sb::di::ServiceNotFoundException); |
| 148 | + EXPECT_THROW(provider.createService<TestClass4>(), sb::di::ServiceNotFoundException); |
| 149 | +} |
| 150 | + |
| 151 | +// createServiceInPlace Tests |
| 152 | + |
| 153 | +TEST_F(BasicExternalTest, ShouldCreateServiceInPlace) |
| 154 | +{ |
| 155 | + TestClass1 test1; |
| 156 | + TestClass2 test2; |
| 157 | + TestClass3 test3; |
| 158 | + auto provider = sb::di::ServiceCollection{} |
| 159 | + .addSingleton(&test1) |
| 160 | + .addSingleton(&test2) |
| 161 | + .addSingleton(&test3) |
| 162 | + .buildServiceProvider(); |
| 163 | + |
| 164 | + EXPECT_THROW(provider.createServiceInPlace<TestClass1>(), sb::di::ServiceNotFoundException); |
| 165 | + EXPECT_THROW(provider.createServiceInPlace<TestClass2>(), sb::di::ServiceNotFoundException); |
| 166 | + EXPECT_THROW(provider.createServiceInPlace<TestClass3>(), sb::di::ServiceNotFoundException); |
| 167 | + EXPECT_THROW(provider.createServiceInPlace<TestClass4>(), sb::di::ServiceNotFoundException); |
| 168 | +} |
| 169 | + |
| 170 | +// createServices Tests |
| 171 | + |
| 172 | +TEST_F(BasicExternalTest, ShouldCreateServices) |
| 173 | +{ |
| 174 | + TestClass1 test1; |
| 175 | + TestClass2 test2; |
| 176 | + TestClass3 test3; |
| 177 | + auto provider = sb::di::ServiceCollection{} |
| 178 | + .addSingleton(&test1) |
| 179 | + .addSingleton(&test2) |
| 180 | + .addSingleton(&test3) |
| 181 | + .buildServiceProvider(); |
| 182 | + |
| 183 | + EXPECT_EQ(provider.createServices<TestClass1>().size(), 0); |
| 184 | + EXPECT_EQ(provider.createServices<TestClass2>().size(), 0); |
| 185 | + EXPECT_EQ(provider.createServices<TestClass3>().size(), 0); |
| 186 | +} |
0 commit comments