8#include <gtest/gtest.h>
10#include "arcane/utils/internal/DependencyInjection.h"
11#include "arcane/utils/FatalErrorException.h"
13#include "arcane/utils/ITraceMng.h"
22using namespace Arcane::DependencyInjection;
27 virtual ~IA() =
default;
28 virtual int value()
const = 0;
34 virtual ~IB() =
default;
35 virtual int value()
const = 0;
41 virtual ~IA2() =
default;
42 virtual int value()
const = 0;
43 virtual IB* bValue()
const = 0;
49 virtual ~IB2() =
default;
50 virtual int value()
const = 0;
51 virtual String stringValue()
const = 0;
57 virtual ~IC() =
default;
58 virtual int value()
const = 0;
64 virtual ~ID() =
default;
70 virtual ~IE() =
default;
71 virtual int intValue()
const = 0;
72 virtual String stringValue()
const = 0;
79 virtual ~INone() =
default;
87 int value()
const override {
return 5; }
95 int value()
const override {
return 12; }
105 int value()
const override {
return 32; }
106 String stringValue()
const override {
return m_test; }
116 EImpl(Int32 int_value,
const String& string_value)
117 : m_string_value(string_value)
118 , m_int_value(int_value)
121 int intValue()
const override {
return m_int_value; }
122 String stringValue()
const override {
return m_string_value; }
134 CDImpl(
int a,
double b) : m_int_value(a+(
int)b){}
135 CDImpl(
int a) : m_int_value(a){}
136 CDImpl() : m_int_value(2){}
137 int value()
const override {
return m_int_value; }
146 A2Impl(
int a,
IB* ib,
IA*) : m_a(a), m_ib(ib) {}
147 A2Impl(
int a,
IB* ib)
153 int value()
const override {
return m_a; }
154 IB* bValue()
const override {
return m_ib; }
161ARCANE_DI_REGISTER_PROVIDER(
AImpl,
162 ProviderProperty(
"AImplProvider"),
163 ARCANE_DI_INTERFACES(
IA),
164 ARCANE_DI_EMPTY_CONSTRUCTOR());
166ARCANE_DI_REGISTER_PROVIDER(
BImpl,
167 ProviderProperty(
"BImplProvider"),
168 ARCANE_DI_INTERFACES(
IB),
169 ARCANE_DI_EMPTY_CONSTRUCTOR());
171ARCANE_DI_REGISTER_PROVIDER(
B2Impl,
172 ProviderProperty(
"B2ImplProvider"),
173 ARCANE_DI_INTERFACES(
IB2),
174 ARCANE_DI_CONSTRUCTOR(
String));
176ARCANE_DI_REGISTER_PROVIDER(
EImpl,
177 ProviderProperty(
"EImplProvider"),
178 ARCANE_DI_INTERFACES(
IE),
179 ARCANE_DI_CONSTRUCTOR(Int32,
String));
181ARCANE_DI_REGISTER_PROVIDER(
A2Impl,
182 ProviderProperty(
"A2ImplProvider"),
183 ARCANE_DI_INTERFACES(
IA2),
184 ARCANE_DI_CONSTRUCTOR(
int,
IB*));
186ARCANE_DI_REGISTER_PROVIDER(
CDImpl,
187 ProviderProperty(
"CDImplProvider2"),
188 ARCANE_DI_INTERFACES(
IC,
ID),
189 ARCANE_DI_CONSTRUCTOR(
int));
191ARCANE_DI_REGISTER_PROVIDER(
CDImpl,
192 ProviderProperty(
"CDImplProvider3"),
193 ARCANE_DI_INTERFACES(
IC),
194 ARCANE_DI_EMPTY_CONSTRUCTOR());
196ARCANE_DI_REGISTER_PROVIDER(
CDImpl,
197 ProviderProperty(
"CDImplProvider4"),
198 ARCANE_DI_INTERFACES(
IC,
ID),
199 ARCANE_DI_CONSTRUCTOR(
int),
200 ARCANE_DI_CONSTRUCTOR(
int,
double),
201 ARCANE_DI_EMPTY_CONSTRUCTOR());
204TEST(DependencyInjection,TestPrintFactories)
206 using namespace Arcane::DependencyInjection;
208 injector.fillWithGlobalFactories();
210 std::cout <<
"FACTORIES=" << injector.printFactories() <<
"\n";
215template <
typename T>
void
216_testNotFoundThrow(Arcane::DependencyInjection::Injector& injector)
219 Ref<T> ic = injector.createInstance<T>(
"Test1");
220 FAIL() <<
"Expected FatalErrorException";
223 std::cout <<
"EX=" << ex <<
"\n";
226 FAIL() <<
"Expected FatalErrorException";
231TEST(DependencyInjection, TestNotFound)
233 using namespace Arcane::DependencyInjection;
234 using namespace DI_Test;
236 injector.fillWithGlobalFactories();
238 _testNotFoundThrow<INone>(injector);
239 _testNotFoundThrow<IA>(injector);
240 _testNotFoundThrow<IC>(injector);
241 Ref<IC> ic2 = injector.createInstance<
IC>(
"Test1",
true);
242 ASSERT_EQ(ic2.
get(),
nullptr);
245TEST(DependencyInjection,TestBind1)
247 using namespace Arcane::DependencyInjection;
248 std::cout <<
"INJECTOR TEST\n";
250 ITraceMng* tm = Arccore::arccoreCreateDefaultTraceMng();
252 injector.bind(ref_tm);
254 std::cout <<
"TM=" << tm <<
"TM2=" << tm2.
get() <<
"\n";
255 ASSERT_EQ(tm,tm2.
get()) <<
"Bad Get Reference";
258TEST(DependencyInjection,ProcessGlobalProviders)
260 using namespace Arcane::DependencyInjection;
261 using namespace DI_Test;
264 injector.fillWithGlobalFactories();
266 Ref<IA> ia = injector.createInstance<
IA>({});
267 EXPECT_TRUE(ia.
get());
268 ASSERT_EQ(ia->value(),5);
270 Ref<IA> ia2 = injector.createInstance<
IA>(
"AImplProvider");
271 EXPECT_TRUE(ia2.
get());
272 ASSERT_EQ(ia2->value(),5);
274 Ref<IB> ib = injector.createInstance<
IB>({});
275 EXPECT_TRUE(ib.
get());
276 ASSERT_EQ(ib->value(),12);
281 using namespace Arcane::DependencyInjection;
282 using namespace DI_Test;
286 injector.fillWithGlobalFactories();
287 String wanted_string(
"Toto");
289 injector.bind(wanted_string);
292 EXPECT_TRUE(ib.
get());
293 ASSERT_EQ(ib->value(), 32);
294 ASSERT_EQ(ib->stringValue(), wanted_string);
299 injector.fillWithGlobalFactories();
300 String wanted_string{
"Tata" };
301 Int32 wanted_int{ 25 };
304 injector.bind(wanted_string,
"Name");
305 injector.bind(wanted_int,
"Value");
312 Ref<IE> ie = injector.createInstance<
IE>(
"EImplProvider");
313 EXPECT_TRUE(ie.
get());
314 ASSERT_EQ(ie->intValue(), wanted_int);
315 ASSERT_EQ(ie->stringValue(), wanted_string);
319TEST(DependencyInjection,TestBindValue)
325 std::cerr <<
"ERROR=" << ex <<
"\n";
330TEST(DependencyInjection, ConstructorCall)
332 using namespace DI_Test;
333 namespace di = Arcane::DependencyInjection;
334 using ConstructorType = di::impl::ConstructorRegisterer<int, IB*>;
336 di::impl::ConcreteFactory<IA2, A2Impl, ConstructorType> c2f;
342 std::unique_ptr<IB> ib{ std::make_unique<BImpl>() };
343 injector.bind(ib.get());
345 Ref<IA2> a2 = c2f.createReference(injector);
347 ASSERT_EQ(a2->value(), 3);
348 ASSERT_EQ(a2->bValue(), ib.
get());
351 std::cerr <<
"ERROR=" << ex <<
"\n";
356TEST(DependencyInjection,Impl2)
358 using namespace DI_Test;
359 namespace di = Arcane::DependencyInjection;
365 injector.fillWithGlobalFactories();
367 injector.bind<
int>(25);
368 Ref<IC> ic = injector.createInstance<
IC>(
"CDImplProvider2");
370 ASSERT_EQ(ic->value(),25);
377 injector.fillWithGlobalFactories();
378 Ref<IC> ic = injector.createInstance<
IC>(
"CDImplProvider3");
380 ASSERT_EQ(ic->value(),2);
387 injector.fillWithGlobalFactories();
388 injector.bind<
int>(25);
389 injector.bind<
double>(12.0);
390 Ref<IC> ic = injector.createInstance<
IC>(
"CDImplProvider4");
392 ASSERT_EQ(ic->value(),37);
396 std::cerr <<
"ERROR=" << ex <<
"\n";
#define ARCANE_CHECK_POINTER(ptr)
Macro retournant le pointeur ptr s'il est non nul ou lancant une exception s'il est nul.
Classe de base d'une exception.
Exception lorsqu'une erreur fatale est survenue.
Interface du gestionnaire de traces.
Référence à une instance.
InstanceType * get() const
Instance associée ou nullptr si aucune.
Chaîne de caractères unicode.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
Ref< InstanceType > makeRefFromInstance(InstanceType2 *t)
Récupère une référence sur le pointeur t.