Debian Codegraph @

test/unit/factory-method-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/factory-method.h"
27
28 #include <QtTest/QtTest>
29 #include <string>
30
31 using namespace injeqt::internal;
32 using namespace injeqt::v1;
33
34 class result_object : public QObject
35 {
36 Q_OBJECT
37 };
38
39 class other_object : public QObject
40 {
41 Q_OBJECT
42 };
43
44 class factory_not_invokable : public QObject
45 {
46 Q_OBJECT
47
48 public:
49 result_object * create_result_object() { return nullptr; }
50
51 };
52
53 class non_unique_factory : public QObject
54 {
55 Q_OBJECT
56
57 public:
58 Q_INVOKABLE result_object * create_result_object_1() { return nullptr; }
59 Q_INVOKABLE result_object * create_result_object_2() { return nullptr; }
60
61 };
62
63 class valid_factory : public QObject
64 {
65 Q_OBJECT
66
67 public slots:
68 Q_INVOKABLE result_object * create_result_object() { return new result_object{}; }
69
70 };
71
72 class valid_factory_subtype : public valid_factory
73 {
74 Q_OBJECT
75 };
76
77 class valid_multi_factory : public QObject
78 {
79 Q_OBJECT
80
81 public slots:
82 Q_INVOKABLE result_object * create_result_object() { return new result_object{}; }
83 Q_INVOKABLE other_object * create_other_object() { return new other_object{}; }
84
85 };
86
87 class valid_factory_with_default_parameter : public QObject
88 {
89 Q_OBJECT
90
91 public slots:
92 Q_INVOKABLE result_object * create_result_object(int a = 0) { (void)a; return new result_object{}; }
93
94 };
95
96 class invalid_factory : public QObject
97 {
98 Q_OBJECT
99
100 public slots:
101 Q_INVOKABLE result_object * create_result_object(int) { return new result_object{}; }
102
103 };
104
105 class factory_method_test : public QObject
106 {
107 Q_OBJECT
108
109 public:
110 factory_method_test();
111
112 private slots:
113 void should_return_empty_when_created_with_not_invokable_factory_method();
114 void should_return_empty_when_created_with_non_unique_factory_method();
115 void should_return_empty_when_created_with_different_type_factory_method();
116 void should_return_empty_when_created_with_invalid_factory_method();
117 void should_create_empty();
118 void should_create_valid_with_invokable_factory_method();
119 void should_create_valid_subtype_with_invokable_factory_method();
120 void should_create_valid_with_invokable_factory_with_default_parameter_method();
121 void should_create_object_with_factory_method();
122 void should_properly_compare();
123
124 private:
125 types_by_name known_types;
126
127 };
128
129 factory_method_test::factory_method_test()
130 {
131 known_types = types_by_name{std::vector<type>{
132 make_type<result_object>(),
133 make_type<other_object>(),
134 make_type<factory_not_invokable>(),
135 make_type<non_unique_factory>(),
136 make_type<valid_factory>(),
137 make_type<valid_factory_subtype>(),
138 make_type<valid_multi_factory>(),
139 make_type<valid_factory_with_default_parameter>(),
140 make_type<invalid_factory>()
141 }};
142 }
143
144 void factory_method_test::should_return_empty_when_created_with_not_invokable_factory_method()
145 {
146 auto f = make_factory_method(known_types, make_type<result_object>(), make_type<factory_not_invokable>());
147 QVERIFY(f.is_empty());
148 }
149
150 void factory_method_test::should_return_empty_when_created_with_non_unique_factory_method()
151 {
152 auto f = make_factory_method(known_types, make_type<result_object>(), make_type<non_unique_factory>());
153 QVERIFY(f.is_empty());
154 }
155
156 void factory_method_test::should_return_empty_when_created_with_different_type_factory_method()
157 {
158 auto f = make_factory_method(known_types, make_type<other_object>(), make_type<valid_factory>());
159 QVERIFY(f.is_empty());
160 }
161
162 void factory_method_test::should_return_empty_when_created_with_invalid_factory_method()
163 {
164 auto f = make_factory_method(known_types, make_type<other_object>(), make_type<invalid_factory>());
165 QVERIFY(f.is_empty());
166 }
167
168 void factory_method_test::should_create_empty()
169 {
170 auto f = factory_method{};
171 QVERIFY(f.is_empty());
172 }
173
174 void factory_method_test::should_create_valid_with_invokable_factory_method()
175 {
176 auto f1 = factory_method{make_type<result_object>(), make_method<valid_factory>("create_result_object()")};
177 QVERIFY(!f1.is_empty());
178 QCOMPARE(f1.object_type(), make_type<valid_factory>());
179 QCOMPARE(f1.result_type(), make_type<result_object>());
180
181 auto f2 = make_factory_method(known_types, make_type<result_object>(), make_type<valid_factory>());
182 QVERIFY(!f2.is_empty());
183 QCOMPARE(f2.object_type(), make_type<valid_factory>());
184 QCOMPARE(f2.result_type(), make_type<result_object>());
185
186 QCOMPARE(f1, f2);
187 }
188
189 void factory_method_test::should_create_valid_subtype_with_invokable_factory_method()
190 {
191 auto f1 = factory_method{make_type<result_object>(), make_method<valid_factory_subtype>("create_result_object()")};
192 QVERIFY(!f1.is_empty());
193 QCOMPARE(f1.object_type(), make_type<valid_factory>());
194 QCOMPARE(f1.result_type(), make_type<result_object>());
195
196 auto f2 = make_factory_method(known_types, make_type<result_object>(), make_type<valid_factory_subtype>());
197 QVERIFY(!f2.is_empty());
198 QCOMPARE(f2.object_type(), make_type<valid_factory>());
199 QCOMPARE(f2.result_type(), make_type<result_object>());
200
201 QCOMPARE(f1, f2);
202 }
203
204 void factory_method_test::should_create_valid_with_invokable_factory_with_default_parameter_method()
205 {
206 auto f = make_factory_method(known_types, make_type<result_object>(), make_type<valid_factory_with_default_parameter>());
207 QVERIFY(!f.is_empty());
208 QCOMPARE(f.object_type(), make_type<valid_factory_with_default_parameter>());
209 QCOMPARE(f.result_type(), make_type<result_object>());
210 }
211
212 void factory_method_test::should_create_object_with_factory_method()
213 {
214 auto f = make_factory_method(known_types, make_type<result_object>(), make_type<valid_factory>());
215 auto factory_object = make_object<valid_factory>();
216 auto object = f.invoke(factory_object.get());
217 auto cast = qobject_cast<result_object *>(object.get());
218 QVERIFY(cast != nullptr);
219 }
220
221 void factory_method_test::should_properly_compare()
222 {
223 auto fm_empty = factory_method{};
224 auto fm1a = make_factory_method(known_types, make_type<result_object>(), make_type<valid_factory>());
225 auto fm1b = make_factory_method(known_types, make_type<result_object>(), make_type<valid_factory>());
226 auto fm2a = make_factory_method(known_types, make_type<result_object>(), make_type<valid_multi_factory>());
227 auto fm2b = make_factory_method(known_types, make_type<result_object>(), make_type<valid_multi_factory>());
228 auto fm3a = make_factory_method(known_types, make_type<other_object>(), make_type<valid_multi_factory>());
229 auto fm3b = make_factory_method(known_types, make_type<other_object>(), make_type<valid_multi_factory>());
230
231 test_compare<factory_method>({{fm_empty}, {fm1a, fm1b}, {fm2a, fm2b}, {fm3a, fm3b}});
232 }
233
234 QTEST_APPLESS_MAIN(factory_method_test)
235 #include "factory-method-test.moc"