Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TestDependencyInjection.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7
8#include <gtest/gtest.h>
9
10#include "arcane/utils/DependencyInjection.h"
11
12#include "arcane/utils/ITraceMng.h"
13
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17using namespace Arcane;
18
19namespace DI_Test
20{
21using namespace Arcane::DependencyInjection;
22
23class IA
24{
25 public:
26 virtual ~IA() = default;
27 virtual int value() const = 0;
28};
29
30class IB
31{
32 public:
33 virtual ~IB() = default;
34 virtual int value() const = 0;
35};
36
37class IA2
38{
39 public:
40 virtual ~IA2() = default;
41 virtual int value() const = 0;
42 virtual IB* bValue() const = 0;
43};
44
45class IB2
46{
47 public:
48 virtual ~IB2() = default;
49 virtual int value() const = 0;
50 virtual String stringValue() const = 0;
51};
52
53class IC
54{
55 public:
56 virtual ~IC() = default;
57 virtual int value() const = 0;
58};
59
60class ID
61{
62 public:
63 virtual ~ID() = default;
64};
65
66class IE
67{
68 public:
69 virtual ~IE() = default;
70 virtual int intValue() const = 0;
71 virtual String stringValue() const = 0;
72};
73
74class AImpl
75: public IA
76{
77 public:
78 AImpl() {}
79 int value() const override { return 5; }
80};
81
82class BImpl
83: public IB
84{
85 public:
86 BImpl() {}
87 int value() const override { return 12; }
88};
89
90class B2Impl
91: public IB2
92{
93 public:
94 B2Impl(const String& x)
95 : m_test(x)
96 {}
97 int value() const override { return 32; }
98 String stringValue() const override { return m_test; }
99
100 private:
101 String m_test;
102};
103
104class EImpl
105: public IE
106{
107 public:
109 : m_string_value(string_value)
110 , m_int_value(int_value)
111 {
112 }
113 int intValue() const override { return m_int_value; }
114 String stringValue() const override { return m_string_value; }
115
116 private:
117 String m_string_value;
118 Int32 m_int_value;
119};
120
122: public ID
123, public IC
124{
125 public:
126 CDImpl(int a,double b) : m_int_value(a+(int)b){}
127 CDImpl(int a) : m_int_value(a){}
128 CDImpl() : m_int_value(2){}
129 int value() const override { return m_int_value; }
130 private:
131 int m_int_value;
132};
133
135: public IA2
136{
137 public:
138 A2Impl(int a,IB* ib,IA*) : m_a(a), m_ib(ib) {}
139 A2Impl(int a, IB* ib)
140 : m_a(a)
141 , m_ib(ib)
142 {}
143
144 public:
145 int value() const override { return m_a; }
146 IB* bValue() const override { return m_ib; }
147
148 private:
149 int m_a;
150 IB* m_ib;
151};
152
153ARCANE_DI_REGISTER_PROVIDER(AImpl,
154 ProviderProperty("AImplProvider"),
155 ARCANE_DI_INTERFACES(IA),
156 ARCANE_DI_EMPTY_CONSTRUCTOR());
157
158ARCANE_DI_REGISTER_PROVIDER(BImpl,
159 ProviderProperty("BImplProvider"),
160 ARCANE_DI_INTERFACES(IB),
161 ARCANE_DI_EMPTY_CONSTRUCTOR());
162
163ARCANE_DI_REGISTER_PROVIDER(B2Impl,
164 ProviderProperty("B2ImplProvider"),
165 ARCANE_DI_INTERFACES(IB2),
166 ARCANE_DI_CONSTRUCTOR(String));
167
168ARCANE_DI_REGISTER_PROVIDER(EImpl,
169 ProviderProperty("EImplProvider"),
170 ARCANE_DI_INTERFACES(IE),
171 ARCANE_DI_CONSTRUCTOR(Int32, String));
172
173ARCANE_DI_REGISTER_PROVIDER(A2Impl,
174 ProviderProperty("A2ImplProvider"),
175 ARCANE_DI_INTERFACES(IA2),
176 ARCANE_DI_CONSTRUCTOR(int, IB*));
177
178ARCANE_DI_REGISTER_PROVIDER(CDImpl,
179 ProviderProperty("CDImplProvider2"),
180 ARCANE_DI_INTERFACES(IC, ID),
181 ARCANE_DI_CONSTRUCTOR(int));
182
183ARCANE_DI_REGISTER_PROVIDER(CDImpl,
184 ProviderProperty("CDImplProvider3"),
185 ARCANE_DI_INTERFACES(IC),
186 ARCANE_DI_EMPTY_CONSTRUCTOR());
187
188ARCANE_DI_REGISTER_PROVIDER(CDImpl,
189 ProviderProperty("CDImplProvider4"),
190 ARCANE_DI_INTERFACES(IC, ID),
191 ARCANE_DI_CONSTRUCTOR(int),
192 ARCANE_DI_CONSTRUCTOR(int, double),
193 ARCANE_DI_EMPTY_CONSTRUCTOR());
194} // namespace DI_Test
195
196TEST(DependencyInjection,TestPrintFactories)
197{
198 using namespace Arcane::DependencyInjection;
200 injector.fillWithGlobalFactories();
201
202 std::cout << "FACTORIES=" << injector.printFactories() << "\n";
203}
204
205TEST(DependencyInjection,TestBind1)
206{
207 using namespace Arcane::DependencyInjection;
208 std::cout << "INJECTOR TEST\n";
210 ITraceMng* tm = Arccore::arccoreCreateDefaultTraceMng();
212 injector.bind(ref_tm);
214 std::cout << "TM=" << tm << "TM2=" << tm2.get() << "\n";
215 ASSERT_EQ(tm,tm2.get()) << "Bad Get Reference";
216}
217
218TEST(DependencyInjection,ProcessGlobalProviders)
219{
220 using namespace Arcane::DependencyInjection;
221 using namespace DI_Test;
222
224 injector.fillWithGlobalFactories();
225
226 Ref<IA> ia = injector.createInstance<IA>();
227 EXPECT_TRUE(ia.get());
228 ASSERT_EQ(ia->value(),5);
229
230 Ref<IA> ia2 = injector.createInstance<IA>("AImplProvider");
231 EXPECT_TRUE(ia2.get());
232 ASSERT_EQ(ia2->value(),5);
233
234 Ref<IB> ib = injector.createInstance<IB>();
235 EXPECT_TRUE(ib.get());
236 ASSERT_EQ(ib->value(),12);
237}
238
239void _TestBindValue()
240{
241 using namespace Arcane::DependencyInjection;
242 using namespace DI_Test;
243
244 {
246 injector.fillWithGlobalFactories();
247 String wanted_string("Toto");
248
250
251 Ref<IB2> ib = injector.createInstance<IB2>();
252 EXPECT_TRUE(ib.get());
253 ASSERT_EQ(ib->value(), 32);
254 ASSERT_EQ(ib->stringValue(), wanted_string);
255 }
256
257 {
259 injector.fillWithGlobalFactories();
260 String wanted_string{ "Tata" };
261 Int32 wanted_int{ 25 };
262
264 injector.bind(wanted_string, "Name");
265 injector.bind(wanted_int, "Value");
266
268 //injector.bind("FalseString","AnyName");
269 //injector.bind(38,"SomeName");
270 //injector.bind(3.2,"DoubleName");
271
272 Ref<IE> ie = injector.createInstance<IE>("EImplProvider");
273 EXPECT_TRUE(ie.get());
274 ASSERT_EQ(ie->intValue(), wanted_int);
275 ASSERT_EQ(ie->stringValue(), wanted_string);
276 }
277}
278
279TEST(DependencyInjection,TestBindValue)
280{
281 try{
282 _TestBindValue();
283 }
284 catch(const Exception& ex){
285 std::cerr << "ERROR=" << ex << "\n";
286 throw;
287 }
288}
289
290TEST(DependencyInjection, ConstructorCall)
291{
292 using namespace DI_Test;
293 namespace di = Arcane::DependencyInjection;
294 using ConstructorType = di::impl::ConstructorRegisterer<int, IB*>;
295
296 di::impl::ConcreteFactory<IA2, A2Impl, ConstructorType> c2f;
297
298 int x = 3;
299
300 try {
302 IB* ib{ new BImpl() };
303 injector.bind(ib);
304 injector.bind(x);
305 Ref<IA2> a2 = c2f.createReference(injector);
307 ASSERT_EQ(a2->value(), 3);
308 ASSERT_EQ(a2->bValue(), ib);
309 }
310 catch (const Exception& ex) {
311 std::cerr << "ERROR=" << ex << "\n";
312 throw;
313 }
314}
315
316TEST(DependencyInjection,Impl2)
317{
318 using namespace DI_Test;
319 namespace di = Arcane::DependencyInjection;
320
321 try{
322 {
323 // Test avec le constructeur CDImpl(int)
325 injector.fillWithGlobalFactories();
326
327 injector.bind<int>(25);
328 Ref<IC> ic = injector.createInstance<IC>("CDImplProvider2");
330 ASSERT_EQ(ic->value(),25);
331 }
332 {
333 // Test avec le constructeur sans arguments (CDImpl())
334 // Dans ce cas la valeur IC::value() doit valoir 2
335 // (voir constructeur de CDImpl)
337 injector.fillWithGlobalFactories();
338 Ref<IC> ic = injector.createInstance<IC>("CDImplProvider3");
340 ASSERT_EQ(ic->value(),2);
341 }
342 {
343 // Test avec le constructeur avec 2 arguments (CDImpl(int,double))
344 // Dans ce cas la valeur IC::value() doit valoir 25+12
345 // (voir constructeur de CDImpl)
347 injector.fillWithGlobalFactories();
348 injector.bind<int>(25);
349 injector.bind<double>(12.0);
350 Ref<IC> ic = injector.createInstance<IC>("CDImplProvider4");
352 ASSERT_EQ(ic->value(),37);
353 }
354 }
355 catch(const Exception& ex){
356 std::cerr << "ERROR=" << ex << "\n";
357 throw;
358 }
359}
#define ARCANE_CHECK_POINTER(ptr)
Macro retournant le pointeur ptr s'il est non nul ou lancant une exception s'il est nul.
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Classe de base d'une exception.
Interface du gestionnaire de traces.
Chaîne de caractères unicode.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-