Debian Codegraph @

test/unit/required-to-satisfy-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/type.h>
25
26 #include "internal/required-to-satisfy.h"
27 #include "internal/types-model.h"
28 #include "internal/types.h"
29
30 #include <QtTest/QtTest>
31
32 using namespace injeqt::internal;
33 using namespace injeqt::v1;
34
35 class type_1 : public QObject
36 {
37 Q_OBJECT
38 };
39
40 class type_1_subtype_1 : public type_1
41 {
42 Q_OBJECT
43 };
44
45 class type_2 : public QObject
46 {
47 Q_OBJECT
48
49 public slots:
50 INJEQT_SET void set_type_1(type_1 *) {}
51 };
52
53 class type_2_subtype_1 : public type_2
54 {
55 Q_OBJECT
56 };
57
58 class type_3 : public QObject
59 {
60 Q_OBJECT
61
62 public slots:
63 INJEQT_SET void set_type_2(type_2 *) {}
64 };
65
66 class type_3_subtype_1 : public type_3
67 {
68 Q_OBJECT
69 };
70
71 class cyclic_type_3;
72
73 class cyclic_type_1 : public QObject
74 {
75 Q_OBJECT
76
77 public slots:
78 INJEQT_SET void set_type_3(cyclic_type_3 *) {}
79 };
80
81 class cyclic_type_1_subtype_1 : public cyclic_type_1
82 {
83 Q_OBJECT
84 };
85
86 class cyclic_type_2 : public QObject
87 {
88 Q_OBJECT
89
90 public slots:
91 INJEQT_SET void set_type_1(cyclic_type_1 *) {}
92 };
93
94 class cyclic_type_2_subtype_1 : public cyclic_type_2
95 {
96 Q_OBJECT
97 };
98
99 class cyclic_type_3 : public QObject
100 {
101 Q_OBJECT
102
103 public slots:
104 INJEQT_SET void set_type_2(cyclic_type_2 *) {}
105 };
106
107 class cyclic_type_3_subtype_1 : public cyclic_type_3
108 {
109 Q_OBJECT
110 };
111
112 class required_to_satisfy_test : public QObject
113 {
114 Q_OBJECT
115
116 public:
117 required_to_satisfy_test();
118
119 private slots:
120 void should_return_nothing_when_empty_dependencies();
121 void should_return_dependencies_for_simple_model_with_empty_implementations();
122 void should_return_nothing_for_simple_model_with_full_implementations();
123 void should_return_subtype_dependencies_for_inheriting_model_with_empty_implementations();
124 void should_return_nothing_for_inheriting_model_with_full_implementations();
125 void should_return_dependencies_for_inheriting_model_with_supertype_implementations();
126 void should_return_all_dependencies_for_simple_model_with_empty_implementations();
127 void should_return_partial_dependencies_for_simple_model_with_partial_implementations();
128 void should_return_all_types_with_cyclic_dependnecies_for_simple_model_with_partial_implementations();
129 void should_return_all_subtypes_with_cyclic_dependnecies_for_inheriting_model_with_partial_implementations();
130 void should_return_type_when_supertype_is_already_available();
131
132 private:
133 types_by_name known_types;
134 type type_1_type;
135 type type_1_subtype_1_type;
136 type type_2_type;
137 type type_2_subtype_1_type;
138 type type_3_type;
139 type type_3_subtype_1_type;
140 type cyclic_type_1_type;
141 type cyclic_type_1_subtype_1_type;
142 type cyclic_type_2_type;
143 type cyclic_type_2_subtype_1_type;
144 type cyclic_type_3_type;
145 type cyclic_type_3_subtype_1_type;
146 types_model empty_types_model;
147 types_model simple_types_model;
148 types_model inheriting_types_model;
149 dependencies type_1_dependencies;
150 dependencies type_1_subtype_1_dependencies;
151 dependencies type_2_dependencies;
152 dependencies type_3_dependencies;
153 dependencies cyclic_type_1_dependencies;
154 dependencies cyclic_type_2_dependencies;
155 dependencies cyclic_type_3_dependencies;
156
157 };
158
159 required_to_satisfy_test::required_to_satisfy_test() :
160 known_types{types_by_name{std::vector<type>{
161 make_type<type_1>(),
162 make_type<type_1_subtype_1>(),
163 make_type<type_2>(),
164 make_type<type_2_subtype_1>(),
165 make_type<type_3>(),
166 make_type<type_3_subtype_1>(),
167 make_type<cyclic_type_1>(),
168 make_type<cyclic_type_1_subtype_1>(),
169 make_type<cyclic_type_2>(),
170 make_type<cyclic_type_2_subtype_1>(),
171 make_type<cyclic_type_3>(),
172 make_type<cyclic_type_3_subtype_1>()
173 }}},
174 type_1_type{make_type<type_1>()},
175 type_1_subtype_1_type{make_type<type_1_subtype_1>()},
176 type_2_type{make_type<type_2>()},
177 type_2_subtype_1_type{make_type<type_2_subtype_1>()},
178 type_3_type{make_type<type_3>()},
179 type_3_subtype_1_type{make_type<type_3_subtype_1>()},
180 cyclic_type_1_type{make_type<cyclic_type_1>()},
181 cyclic_type_1_subtype_1_type{make_type<cyclic_type_1_subtype_1>()},
182 cyclic_type_2_type{make_type<cyclic_type_2>()},
183 cyclic_type_2_subtype_1_type{make_type<cyclic_type_2_subtype_1>()},
184 cyclic_type_3_type{make_type<cyclic_type_3>()},
185 cyclic_type_3_subtype_1_type{make_type<cyclic_type_3_subtype_1>()}
186 {
187 auto simple_types = std::vector<type>{
188 type_1_type,
189 type_2_type,
190 type_3_type,
191 cyclic_type_1_type,
192 cyclic_type_2_type,
193 cyclic_type_3_type
194 };
195 auto inheriting_types = std::vector<type>{
196 type_1_subtype_1_type,
197 type_2_subtype_1_type,
198 type_3_subtype_1_type,
199 cyclic_type_1_subtype_1_type,
200 cyclic_type_2_subtype_1_type,
201 cyclic_type_3_subtype_1_type
202 };
203 simple_types_model = make_types_model(known_types, simple_types, simple_types);
204 inheriting_types_model = make_types_model(known_types, inheriting_types, inheriting_types);
205 type_1_dependencies = simple_types_model.mapped_dependencies().get(type_1_type)->dependency_list();
206 type_1_subtype_1_dependencies = inheriting_types_model.mapped_dependencies().get(type_1_subtype_1_type)->dependency_list();
207 type_2_dependencies = simple_types_model.mapped_dependencies().get(type_2_type)->dependency_list();
208 type_3_dependencies = simple_types_model.mapped_dependencies().get(type_3_type)->dependency_list();
209 cyclic_type_1_dependencies = simple_types_model.mapped_dependencies().get(cyclic_type_1_type)->dependency_list();
210 cyclic_type_2_dependencies = simple_types_model.mapped_dependencies().get(cyclic_type_2_type)->dependency_list();
211 cyclic_type_3_dependencies = simple_types_model.mapped_dependencies().get(cyclic_type_3_type)->dependency_list();
212 }
213
214 void required_to_satisfy_test::should_return_nothing_when_empty_dependencies()
215 {
216 auto result = required_to_satisfy(dependencies{}, simple_types_model, {});
217 QCOMPARE(result, types{});
218 }
219
220 void required_to_satisfy_test::should_return_dependencies_for_simple_model_with_empty_implementations()
221 {
222 auto result = required_to_satisfy(type_2_dependencies, simple_types_model, {});
223 QCOMPARE(result, (types{type_1_type}));
224 }
225
226 void required_to_satisfy_test::should_return_nothing_for_simple_model_with_full_implementations()
227 {
228 auto type_1_object = make_object<type_1>();
229 auto available_implementations = implementations
230 {
231 implementation{type_1_type, type_1_object.get()}
232 };
233 auto result = required_to_satisfy(type_2_dependencies, simple_types_model, available_implementations);
234 QCOMPARE(result, (types{}));
235 }
236
237 void required_to_satisfy_test::should_return_subtype_dependencies_for_inheriting_model_with_empty_implementations()
238 {
239 auto result = required_to_satisfy(type_2_dependencies, inheriting_types_model, {});
240 QCOMPARE(result, (types{type_1_subtype_1_type}));
241 }
242
243 void required_to_satisfy_test::should_return_nothing_for_inheriting_model_with_full_implementations()
244 {
245 auto type_1_subtype_1_object = make_object<type_1_subtype_1>();
246 auto available_implementations = implementations
247 {
248 implementation{type_1_subtype_1_type, type_1_subtype_1_object.get()}
249 };
250 auto result = required_to_satisfy(type_2_dependencies, inheriting_types_model, available_implementations);
251 QCOMPARE(result, (types{}));
252 }
253
254 void required_to_satisfy_test::should_return_dependencies_for_inheriting_model_with_supertype_implementations()
255 {
256 auto type_1_object = make_object<type_1>();
257 auto available_implementations = implementations
258 {
259 implementation{type_1_type, type_1_object.get()}
260 };
261 auto result = required_to_satisfy(type_2_dependencies, inheriting_types_model, available_implementations);
262 QCOMPARE(result, (types{type_1_subtype_1_type}));
263 }
264
265 void required_to_satisfy_test::should_return_all_dependencies_for_simple_model_with_empty_implementations()
266 {
267 auto result = required_to_satisfy(type_3_dependencies, simple_types_model, {});
268 QCOMPARE(result, (types{type_1_type, type_2_type}));
269 }
270
271 void required_to_satisfy_test::should_return_partial_dependencies_for_simple_model_with_partial_implementations()
272 {
273 auto type_1_object = make_object<type_1>();
274 auto available_implementations = implementations
275 {
276 implementation{type_1_type, type_1_object.get()}
277 };
278
279 auto result = required_to_satisfy(type_3_dependencies, simple_types_model, available_implementations);
280 QCOMPARE(result, (types{type_2_type}));
281 }
282
283 void required_to_satisfy_test::should_return_all_types_with_cyclic_dependnecies_for_simple_model_with_partial_implementations()
284 {
285 auto result1 = required_to_satisfy(cyclic_type_1_dependencies, simple_types_model, {});
286 QCOMPARE(result1, (types{cyclic_type_1_type, cyclic_type_2_type, cyclic_type_3_type}));
287
288 auto result2 = required_to_satisfy(cyclic_type_2_dependencies, simple_types_model, {});
289 QCOMPARE(result2, (types{cyclic_type_1_type, cyclic_type_2_type, cyclic_type_3_type}));
290
291 auto result3 = required_to_satisfy(cyclic_type_3_dependencies, simple_types_model, {});
292 QCOMPARE(result3, (types{cyclic_type_1_type, cyclic_type_2_type, cyclic_type_3_type}));
293 }
294
295 void required_to_satisfy_test::should_return_all_subtypes_with_cyclic_dependnecies_for_inheriting_model_with_partial_implementations()
296 {
297 auto result1 = required_to_satisfy(cyclic_type_1_dependencies, inheriting_types_model, {});
298 QCOMPARE(result1, (types{cyclic_type_1_subtype_1_type, cyclic_type_2_subtype_1_type, cyclic_type_3_subtype_1_type}));
299
300 auto result2 = required_to_satisfy(cyclic_type_2_dependencies, inheriting_types_model, {});
301 QCOMPARE(result2, (types{cyclic_type_1_subtype_1_type, cyclic_type_2_subtype_1_type, cyclic_type_3_subtype_1_type}));
302
303 auto result3 = required_to_satisfy(cyclic_type_3_dependencies, inheriting_types_model, {});
304 QCOMPARE(result3, (types{cyclic_type_1_subtype_1_type, cyclic_type_2_subtype_1_type, cyclic_type_3_subtype_1_type}));
305 }
306
307 void required_to_satisfy_test::should_return_type_when_supertype_is_already_available()
308 {
309 auto type_1_object = make_object<type_1>();
310 auto available_implementations = implementations
311 {
312 implementation{type_1_type, type_1_object.get()},
313 };
314
315 auto result = required_to_satisfy(type_1_subtype_1_dependencies, inheriting_types_model, available_implementations);
316 QCOMPARE(result, (types{}));
317 }
318
319 QTEST_APPLESS_MAIN(required_to_satisfy_test);
320
321 #include "required-to-satisfy-test.moc"