Debian Codegraph @

test/unit/injector-core-test.cpp

raw symbols SCIP
1 /*
2 * %injeqt copyright begin%
3 * Copyright 2014 Rafał Malinowski ([email protected])
4 * %injeqt copyright end%
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "../mocks/mocked-provider.h"
22 #include "expect.h"
23 #include "utils.h"
24
25 #include <injeqt/exception/ambiguous-types.h>
26 #include <injeqt/exception/invalid-setter.h>
27 #include <injeqt/exception/unavailable-required-types.h>
28 #include <injeqt/exception/unknown-type.h>
29
30 #include "internal/injector-core.h"
31
32 #include <QtTest/QtTest>
33 #include <string>
34
35 using namespace injeqt::internal;
36 using namespace injeqt::v1;
37
38 class type_1 : public QObject
39 {
40 Q_OBJECT
41 };
42
43 class type_1_subtype_1 : public type_1
44 {
45 Q_OBJECT
46 };
47
48 class type_1_subtype_2 : public type_1
49 {
50 Q_OBJECT
51 };
52
53 class type_2 : public QObject
54 {
55 Q_OBJECT
56
57 public:
58 type_1 *o = nullptr;
59
60 private slots:
61 INJEQT_SET void set_type_1(type_1 *x) { o = x; }
62 };
63
64 class type_3 : public QObject
65 {
66 Q_OBJECT
67 };
68
69 class type_4;
70 class type_5;
71 class type_6;
72
73 class type_4 : public QObject
74 {
75 Q_OBJECT
76
77 public:
78 type_5 *o = nullptr;
79
80 private slots:
81 INJEQT_SET void set_type_5(type_5 *x) { o = x; }
82 };
83
84 class type_5 : public QObject
85 {
86 Q_OBJECT
87
88 public:
89 type_6 *o = nullptr;
90
91 private slots:
92 INJEQT_SET void set_type_6(type_6 *x) { o = x; }
93 };
94
95 class type_6 : public QObject
96 {
97 Q_OBJECT
98
99 public:
100 type_4 *o = nullptr;
101
102 private slots:
103 INJEQT_SET void set_type_4(type_4 *x) { o = x; }
104 };
105
106 class type_7 : public QObject
107 {
108 Q_OBJECT
109 };
110
111 class type_8 : public QObject
112 {
113 Q_OBJECT
114 };
115
116 class type_9 : public QObject
117 {
118 Q_OBJECT
119
120 public:
121 type_7 *o7 = nullptr;
122 type_8 *o8 = nullptr;
123
124 private slots:
125 INJEQT_SET void set_type_7(type_7 *x) { o7 = x; }
126 INJEQT_SET void set_type_8(type_8 *x) { o8 = x; }
127 };
128
129
130 class unregistered_type : public QObject
131 {
132 Q_OBJECT
133
134 public:
135 type_1 *o1;
136 type_2 *o2;
137
138 unregistered_type() : QObject{}, o1{nullptr}, o2{nullptr} {}
139
140 private slots:
141 INJEQT_SET void set_type_1(type_1 *x) { o1 = x; }
142 INJEQT_SET void set_type_1(type_2 *x) { o2 = x; }
143 };
144
145 class injector_core_test : public QObject
146 {
147 Q_OBJECT
148
149 private:
150 template<typename T>
151 T * get(injector_core &i) { return qobject_cast<T *>(i.get(make_type<T>())); }
152
153 private slots:
154 void should_create_empty_injector_core();
155 void should_create_simple_injector_core_and_return_object();
156 void should_not_accept_doubled_type();
157 void should_not_accept_type_and_subtype();
158 void should_not_accept_subtype_and_type();
159 void should_accept_two_subtypes();
160 void should_not_accept_unresolvable_dependency();
161 void should_accept_resolvable_dependency();
162 void should_accept_resolvable_supertype_dependency();
163 void should_not_accept_ambiguous_supertype_dependency();
164 void should_not_accept_unknown_required_type();
165 void should_accept_known_required_type();
166 void should_accept_known_required_supertype();
167 void should_not_accept_ambiguous_required_supertype();
168 void should_accept_cyclic_dependencies();
169 void should_accept_dependencies_that_are_required();
170 void should_inject_into_unregistered_type();
171 void should_not_inject_into_when_unknown_dependencies();
172 // TODO: https://github.com/vogel/injeqt/issues/3
173 /*
174 void should_not_accept_cyclic_required_types();
175 void should_not_accept_dependent_as_required_type();
176 */
177
178 };
179
180 void injector_core_test::should_create_empty_injector_core()
181 {
182 auto i = injector_core{};
183 expect<exception::unknown_type>({"type_1"}, [&](){
184 i.get(make_type<type_1>());
185 });
186 }
187
188 void injector_core_test::should_create_simple_injector_core_and_return_object()
189 {
190 auto type_1_provider_p = make_mocked_provider<type_1>();
191 auto type_1_provider = type_1_provider_p.get();
192 auto configuration = std::vector<std::unique_ptr<provider>>{};
193 configuration.push_back(std::move(type_1_provider_p));
194
195 auto i = injector_core{types_by_name{}, std::move(configuration)};
196 QVERIFY(type_1_provider->object() == nullptr);
197
198 auto o1 = get<type_1>(i);
199 auto o2 = get<type_1>(i);
200
201 QVERIFY(o1 != nullptr);
202 QVERIFY(o1 == o2);
203 QVERIFY(type_1_provider->object() == o1);
204 }
205
206 void injector_core_test::should_not_accept_doubled_type()
207 {
208 auto configuration = std::vector<std::unique_ptr<provider>>{};
209 configuration.push_back(make_mocked_provider<type_1>());
210 configuration.push_back(make_mocked_provider<type_1>());
211
212 expect<exception::ambiguous_types>({}, [&](){
213 auto i = injector_core{types_by_name{}, std::move(configuration)};
214 });
215 }
216
217 void injector_core_test::should_not_accept_type_and_subtype()
218 {
219 auto configuration = std::vector<std::unique_ptr<provider>>{};
220 configuration.push_back(make_mocked_provider<type_1>());
221 configuration.push_back(make_mocked_provider<type_1_subtype_1>());
222
223 expect<exception::ambiguous_types>({"type_1"}, [&](){
224 auto i = injector_core{types_by_name{}, std::move(configuration)};
225 });
226 }
227
228 void injector_core_test::should_not_accept_subtype_and_type()
229 {
230 auto configuration = std::vector<std::unique_ptr<provider>>{};
231 configuration.push_back(make_mocked_provider<type_1_subtype_1>());
232 configuration.push_back(make_mocked_provider<type_1>());
233
234 expect<exception::ambiguous_types>({"type_1"}, [&](){
235 auto i = injector_core{types_by_name{}, std::move(configuration)};
236 });
237 }
238
239 void injector_core_test::should_accept_two_subtypes()
240 {
241 auto configuration = std::vector<std::unique_ptr<provider>>{};
242 configuration.push_back(make_mocked_provider<type_1_subtype_1>());
243 configuration.push_back(make_mocked_provider<type_1_subtype_2>());
244
245 auto i = injector_core{types_by_name{}, std::move(configuration)};
246 auto o1 = get<type_1_subtype_1>(i);
247 auto o2 = get<type_1_subtype_2>(i);
248
249 QVERIFY(o1 != nullptr);
250 QVERIFY(o2 != nullptr);
251
252 expect<exception::unknown_type>({"type_1"}, [&](){
253 get<type_1>(i);
254 });
255 }
256
257 void injector_core_test::should_not_accept_unresolvable_dependency()
258 {
259 auto configuration = std::vector<std::unique_ptr<provider>>{};
260 configuration.push_back(make_mocked_provider<type_2>());
261
262 expect<exception::invalid_setter>({"set_type_1"}, [&](){ // TODO: figure out better exception name
263 auto i = injector_core{types_by_name{}, std::move(configuration)};
264 });
265 }
266
267 void injector_core_test::should_accept_resolvable_dependency()
268 {
269 auto type_1_provider_p = make_mocked_provider<type_1>();
270 auto type_1_provider = type_1_provider_p.get();
271 auto type_2_provider_p = make_mocked_provider<type_2>();
272 auto type_2_provider = type_2_provider_p.get();
273
274 auto configuration = std::vector<std::unique_ptr<provider>>{};
275 configuration.push_back(std::move(type_1_provider_p));
276 configuration.push_back(std::move(type_2_provider_p));
277
278 auto i = injector_core{types_by_name{make_type<type_1>(), make_type<type_2>()}, std::move(configuration)};
279 QVERIFY(type_1_provider->object() == nullptr);
280 QVERIFY(type_2_provider->object() == nullptr);
281
282 auto o2 = get<type_2>(i);
283 auto o1 = type_1_provider->object();
284 QVERIFY(o1 != nullptr);
285 QVERIFY(o2 != nullptr);
286 QVERIFY(o1 == get<type_1>(i));
287 QVERIFY(o1 == o2->o);
288 }
289
290 void injector_core_test::should_accept_resolvable_supertype_dependency()
291 {
292 auto type_1_provider_p = make_mocked_provider<type_1_subtype_1>();
293 auto type_1_provider = type_1_provider_p.get();
294 auto type_2_provider_p = make_mocked_provider<type_2>();
295 auto type_2_provider = type_2_provider_p.get();
296
297 auto configuration = std::vector<std::unique_ptr<provider>>{};
298 configuration.push_back(std::move(type_1_provider_p));
299 configuration.push_back(std::move(type_2_provider_p));
300
301 auto i = injector_core{types_by_name{make_type<type_1>(), make_type<type_1_subtype_1>(), make_type<type_2>()}, std::move(configuration)};
302 QVERIFY(type_1_provider->object() == nullptr);
303 QVERIFY(type_2_provider->object() == nullptr);
304
305 auto o2 = get<type_2>(i);
306 auto o1 = type_1_provider->object();
307 QVERIFY(o1 != nullptr);
308 QVERIFY(o2 != nullptr);
309 QVERIFY(o1 == get<type_1>(i));
310 QVERIFY(o1 == get<type_1_subtype_1>(i));
311 QVERIFY(o1 = o2->o);
312 }
313
314 void injector_core_test::should_not_accept_ambiguous_supertype_dependency()
315 {
316 auto configuration = std::vector<std::unique_ptr<provider>>{};
317 configuration.push_back(make_mocked_provider<type_1_subtype_1>());
318 configuration.push_back(make_mocked_provider<type_1_subtype_2>());
319 configuration.push_back(make_mocked_provider<type_2>());
320
321 expect<exception::invalid_setter>({"set_type_1"}, [&](){ // better exception name
322 auto i = injector_core{types_by_name{}, std::move(configuration)};
323 });
324 }
325
326 void injector_core_test::should_not_accept_unknown_required_type()
327 {
328 auto configuration = std::vector<std::unique_ptr<provider>>{};
329 configuration.push_back(make_mocked_provider<type_3, type_1>());
330
331 expect<exception::unavailable_required_types>({"type_1"}, [&](){
332 auto i = injector_core{types_by_name{}, std::move(configuration)};
333 });
334 }
335
336 void injector_core_test::should_accept_known_required_type()
337 {
338 auto type_1_provider_p = make_mocked_provider<type_1>();
339 auto type_1_provider = type_1_provider_p.get();
340 auto type_3_provider_p = make_mocked_provider<type_3, type_1>();
341 auto type_3_provider = type_3_provider_p.get();
342
343 auto configuration = std::vector<std::unique_ptr<provider>>{};
344 configuration.push_back(std::move(type_1_provider_p));
345 configuration.push_back(std::move(type_3_provider_p));
346
347 auto i = injector_core{types_by_name{}, std::move(configuration)};
348 QVERIFY(type_1_provider->object() == nullptr);
349 QVERIFY(type_3_provider->object() == nullptr);
350
351 auto o3 = get<type_3>(i);
352 auto o1 = type_1_provider->object();
353 QVERIFY(o3 != nullptr);
354 QVERIFY(o1 != nullptr);
355 QVERIFY(o1 == get<type_1>(i));
356 }
357
358 void injector_core_test::should_accept_known_required_supertype()
359 {
360 auto type_1_provider_p = make_mocked_provider<type_1_subtype_1>();
361 auto type_1_provider = type_1_provider_p.get();
362 auto type_3_provider_p = make_mocked_provider<type_3, type_1>();
363 auto type_3_provider = type_3_provider_p.get();
364
365 auto configuration = std::vector<std::unique_ptr<provider>>{};
366 configuration.push_back(std::move(type_1_provider_p));
367 configuration.push_back(std::move(type_3_provider_p));
368
369 auto i = injector_core{types_by_name{}, std::move(configuration)};
370 QVERIFY(type_1_provider->object() == nullptr);
371 QVERIFY(type_3_provider->object() == nullptr);
372
373 auto o3 = get<type_3>(i);
374 auto o1 = type_1_provider->object();
375 QVERIFY(o3 != nullptr);
376 QVERIFY(o1 != nullptr);
377 QVERIFY(o1 == get<type_1>(i));
378 QVERIFY(o1 == get<type_1_subtype_1>(i));
379 }
380
381 void injector_core_test::should_not_accept_ambiguous_required_supertype()
382 {
383 auto configuration = std::vector<std::unique_ptr<provider>>{};
384 configuration.push_back(make_mocked_provider<type_3, type_1>());
385 configuration.push_back(make_mocked_provider<type_1_subtype_1>());
386 configuration.push_back(make_mocked_provider<type_1_subtype_2>());
387
388 expect<exception::unavailable_required_types>({"type_1"}, [&](){
389 auto i = injector_core{types_by_name{}, std::move(configuration)};
390 });
391 }
392
393 void injector_core_test::should_accept_cyclic_dependencies()
394 {
395 auto type_4_provider_p = make_mocked_provider<type_4>();
396 auto type_4_provider = type_4_provider_p.get();
397 auto type_5_provider_p = make_mocked_provider<type_5>();
398 auto type_5_provider = type_5_provider_p.get();
399 auto type_6_provider_p = make_mocked_provider<type_6>();
400 auto type_6_provider = type_6_provider_p.get();
401
402 auto configuration = std::vector<std::unique_ptr<provider>>{};
403 configuration.push_back(std::move(type_4_provider_p));
404 configuration.push_back(std::move(type_5_provider_p));
405 configuration.push_back(std::move(type_6_provider_p));
406
407 auto i = injector_core{types_by_name{make_type<type_4>(), make_type<type_5>(), make_type<type_6>()}, std::move(configuration)};
408 QVERIFY(type_4_provider->object() == nullptr);
409 QVERIFY(type_5_provider->object() == nullptr);
410 QVERIFY(type_6_provider->object() == nullptr);
411
412 auto o5 = get<type_5>(i);
413 auto o4 = type_4_provider->object();
414 auto o6 = type_6_provider->object();
415 QVERIFY(o4 != nullptr);
416 QVERIFY(o5 != nullptr);
417 QVERIFY(o6 != nullptr);
418 QVERIFY(o4 == get<type_4>(i));
419 QVERIFY(o6 == get<type_6>(i));
420 QVERIFY(o4 == get<type_6>(i)->o);
421 QVERIFY(o5 == get<type_4>(i)->o);
422 QVERIFY(o6 == get<type_5>(i)->o);
423 }
424
425 void injector_core_test::should_accept_dependencies_that_are_required()
426 {
427 auto type_7_provider_p = make_mocked_provider<type_7>();
428 auto type_7_provider = type_7_provider_p.get();
429 auto type_8_provider_p = make_mocked_provider<type_8, type_7>();
430 auto type_8_provider = type_8_provider_p.get();
431 auto type_9_provider_p = make_mocked_provider<type_9>();
432 auto type_9_provider = type_8_provider_p.get();
433
434 auto configuration = std::vector<std::unique_ptr<provider>>{};
435 configuration.push_back(std::move(type_7_provider_p));
436 configuration.push_back(std::move(type_8_provider_p));
437 configuration.push_back(std::move(type_9_provider_p));
438
439 auto i = injector_core{types_by_name{make_type<type_7>(), make_type<type_8>(), make_type<type_9>()}, std::move(configuration)};
440 QVERIFY(type_7_provider->object() == nullptr);
441 QVERIFY(type_8_provider->object() == nullptr);
442 QVERIFY(type_9_provider->object() == nullptr);
443
444 auto o9 = get<type_9>(i);
445 auto o7 = type_7_provider->object();
446 auto o8 = type_8_provider->object();
447 QVERIFY(o9 != nullptr);
448 QVERIFY(o7 != nullptr);
449 QVERIFY(o8 != nullptr);
450 QVERIFY(o7 == get<type_7>(i));
451 QVERIFY(o8 == get<type_8>(i));
452 }
453
454 void injector_core_test::should_inject_into_unregistered_type()
455 {
456 auto type_1_provider_p = make_mocked_provider<type_1>();
457 auto type_2_provider_p = make_mocked_provider<type_2>();
458
459 auto configuration = std::vector<std::unique_ptr<provider>>{};
460 configuration.push_back(std::move(type_1_provider_p));
461 configuration.push_back(std::move(type_2_provider_p));
462
463 auto i = injector_core{types_by_name{make_type<type_1>(), make_type<type_2>()}, std::move(configuration)};
464 auto unregistered = std::unique_ptr<unregistered_type>(new unregistered_type{});
465 i.inject_into(unregistered.get());
466
467 QVERIFY(unregistered->o1 != nullptr);
468 QVERIFY(unregistered->o2 != nullptr);
469 QCOMPARE(unregistered->o1, get<type_1>(i));
470 QCOMPARE(unregistered->o2, get<type_2>(i));
471 }
472
473 void injector_core_test::should_not_inject_into_when_unknown_dependencies()
474 {
475 auto type_1_provider_p = make_mocked_provider<type_1>();
476
477 auto configuration = std::vector<std::unique_ptr<provider>>{};
478 configuration.push_back(std::move(type_1_provider_p));
479
480 auto i = injector_core{types_by_name{make_type<type_1>()}, std::move(configuration)};
481 auto unregistered = std::unique_ptr<unregistered_type>(new unregistered_type{});
482
483 expect<exception::invalid_setter>({"type_2"}, [&](){
484 i.inject_into(unregistered.get());
485 });
486 }
487
488 // TODO: https://github.com/vogel/injeqt/issues/3
489 /*
490 void injector_core_test::should_not_accept_cyclic_required_types()
491 {
492 auto configuration = std::vector<std::unique_ptr<provider>>{};
493 configuration.push_back(make_mocked_provider<type_1, type_7>());
494 configuration.push_back(make_mocked_provider<type_7, type_8>());
495 configuration.push_back(make_mocked_provider<type_8, type_1>());
496
497 expect<exception::exception>([&](){
498 auto i = injector_core{std::move(configuration)};
499 });
500 }
501
502 void injector_core_test::should_not_accept_dependent_as_required_type()
503 {
504 auto configuration = std::vector<std::unique_ptr<provider>>{};
505 configuration.push_back(make_mocked_provider<type_2>());
506 configuration.push_back(make_mocked_provider<type_1, type_2>());
507
508 expect<exception::exception>([&](){
509 auto i = injector_core{std::move(configuration)};
510 });
511 }
512 */
513
514 QTEST_APPLESS_MAIN(injector_core_test)
515 #include "injector-core-test.moc"