Debian Codegraph @

test/unit/injector-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 "expect.h"
22 #include "utils.h"
23
24 #include <injeqt/exception/ambiguous-types.h>
25 #include <injeqt/exception/qobject-type.h>
26 #include <injeqt/exception/unknown-type.h>
27 #include <injeqt/exception/unresolvable-dependencies.h>
28 #include <injeqt/injector.h>
29 #include <injeqt/module.h>
30 #include <injeqt/type.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 public:
42 Q_INVOKABLE type_1() {}
43 };
44
45 class type_1_subtype_1 : public type_1
46 {
47 Q_OBJECT
48 public:
49 Q_INVOKABLE type_1_subtype_1() {}
50 };
51
52 class type_1_subtype_2 : public type_1
53 {
54 Q_OBJECT
55 public:
56 Q_INVOKABLE type_1_subtype_2() {}
57 };
58
59 class require_common_type : public QObject
60 {
61 Q_OBJECT
62 public:
63 Q_INVOKABLE require_common_type() {}
64 private slots:
65 INJEQT_SET void set_type_1(type_1 *) { }
66 };
67
68 class created_by_factory : public QObject
69 {
70 Q_OBJECT
71 public:
72 created_by_factory() {}
73 };
74
75 class default_constructible_factory : public QObject
76 {
77 Q_OBJECT
78 public:
79 Q_INVOKABLE default_constructible_factory() {}
80 Q_INVOKABLE created_by_factory * create() { return new created_by_factory{}; }
81 };
82
83 class ready_type : public QObject
84 {
85 Q_OBJECT
86 };
87
88 class subinjector_object : public QObject
89 {
90 Q_OBJECT
91 public:
92 Q_INVOKABLE subinjector_object() {}
93 created_by_factory *_x;
94 private slots:
95 INJEQT_SET void set_created_by_factory(created_by_factory *x) { _x = x; }
96 };
97
98 class test_module : public module
99 {
100 public:
101 test_module()
102 {
103 _ready_object = std::unique_ptr<ready_type>{new ready_type{}};
104
105 add_type<type_1_subtype_1>();
106 add_type<default_constructible_factory>();
107 add_factory<created_by_factory, default_constructible_factory>();
108 add_ready_object<ready_type>(_ready_object.get());
109 }
110 private:
111 std::unique_ptr<ready_type> _ready_object;
112 };
113
114 class test_submodule : public module
115 {
116 public:
117 test_submodule()
118 {
119 add_type<subinjector_object>();
120 }
121 };
122
123 class disable_common_supertype_submodule : public module
124 {
125 public:
126 disable_common_supertype_submodule()
127 {
128 add_type<type_1_subtype_2>();
129 add_type<require_common_type>();
130 }
131 };
132
133 class injector_test : public QObject
134 {
135 Q_OBJECT
136
137 private slots:
138 void should_create_empty_injector();
139 void should_not_accept_qobject_type();
140 void should_not_accept_unknown_type();
141 void should_create_valid_injector();
142 void should_handle_subinjector();
143 void should_not_accept_double_superinjector();
144 void should_disable_common_type_in_superinjector();
145 void should_allow_move();
146
147 };
148
149 class not_configured_type : public QObject
150 {
151 Q_OBJECT
152 };
153
154 void injector_test::should_create_empty_injector()
155 {
156 injector{};
157 }
158
159 void injector_test::should_not_accept_qobject_type()
160 {
161 auto i = injector{};
162
163 expect<exception::qobject_type>([&](){
164 i.get<QObject>();
165 });
166 }
167
168 void injector_test::should_not_accept_unknown_type()
169 {
170 auto i = injector{};
171
172 expect<exception::unknown_type>({"default_constructible_factory"}, [&](){
173 i.get<default_constructible_factory>();
174 });
175 }
176
177 void injector_test::should_create_valid_injector()
178 {
179 auto modules = std::vector<std::unique_ptr<injeqt::module>>{};
180 modules.emplace_back(std::unique_ptr<test_module>(new test_module{}));
181 auto i = injector{std::move(modules)};
182
183 QVERIFY(i.get<created_by_factory>() != nullptr);
184 QVERIFY(i.get<default_constructible_factory>() != nullptr);
185 QVERIFY(i.get<ready_type>() != nullptr);
186
187 expect<exception::unknown_type>({"not_configured_type"}, [&](){
188 i.get<not_configured_type>();
189 });
190 }
191
192 void injector_test::should_handle_subinjector()
193 {
194 auto super_modules = std::vector<std::unique_ptr<injeqt::module>>{};
195 super_modules.emplace_back(std::unique_ptr<test_module>(new test_module{}));
196 auto super_injector = injector{std::move(super_modules)};
197
198 auto super_injectors = std::vector<injector *>{&super_injector};
199 auto sub_modules = std::vector<std::unique_ptr<injeqt::module>>{};
200 sub_modules.emplace_back(std::unique_ptr<test_submodule>(new test_submodule{}));
201 auto sub_injector = injector{super_injectors, std::move(sub_modules)};
202
203 QVERIFY(sub_injector.get<subinjector_object>() != nullptr);
204 }
205
206 void injector_test::should_not_accept_double_superinjector()
207 {
208 auto super_modules = std::vector<std::unique_ptr<injeqt::module>>{};
209 super_modules.emplace_back(std::unique_ptr<test_module>(new test_module{}));
210 auto super_injector = injector{std::move(super_modules)};
211
212 auto super_injectors = std::vector<injector *>{&super_injector, &super_injector};
213 auto sub_modules = std::vector<std::unique_ptr<injeqt::module>>{};
214 sub_modules.emplace_back(std::unique_ptr<test_submodule>(new test_submodule{}));
215
216 expect<exception::ambiguous_types>({}, [&]{
217 injector{super_injectors, std::move(sub_modules)};
218 });
219 }
220
221 void injector_test::should_disable_common_type_in_superinjector()
222 {
223 auto super_modules = std::vector<std::unique_ptr<injeqt::module>>{};
224 super_modules.emplace_back(std::unique_ptr<test_module>(new test_module{}));
225 auto super_injector = injector{std::move(super_modules)};
226
227 auto super_injectors = std::vector<injector *>{&super_injector};
228 auto sub_modules = std::vector<std::unique_ptr<injeqt::module>>{};
229 sub_modules.emplace_back(std::unique_ptr<disable_common_supertype_submodule>(new disable_common_supertype_submodule{}));
230
231 expect<exception::unresolvable_dependencies>({"type_1"}, [&]{
232 injector{super_injectors, std::move(sub_modules)};
233 });
234 }
235
236 void injector_test::should_allow_move()
237 {
238 auto modules = std::vector<std::unique_ptr<injeqt::module>>{};
239 modules.emplace_back(std::unique_ptr<test_module>(new test_module{}));
240 auto i = injector{std::move(modules)};
241 auto i2 = std::move(i);
242 auto i3 = injector{};
243 i3 = std::move(i2);
244
245 QVERIFY(i3.get<created_by_factory>() != nullptr);
246 QVERIFY(i3.get<default_constructible_factory>() != nullptr);
247 QVERIFY(i3.get<ready_type>() != nullptr);
248
249 expect<exception::unknown_type>({"not_configured_type"}, [&](){
250 i3.get<not_configured_type>();
251 });
252 }
253
254 QTEST_APPLESS_MAIN(injector_test)
255 #include "injector-test.moc"