Debian Codegraph @

test/integration/factory-behavior-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 <injeqt/injector.h>
22 #include <injeqt/module.h>
23
24 #include <QtTest/QtTest>
25
26 class default_constructible : public QObject
27 {
28 Q_OBJECT
29
30 public:
31 Q_INVOKABLE default_constructible() {}
32
33 };
34
35 class int_container : public QObject
36 {
37 Q_OBJECT
38
39 public:
40 int_container() {}
41 virtual ~int_container() {}
42 virtual int value() const = 0;
43
44 };
45
46 class simple_int_container : public int_container
47 {
48 Q_OBJECT
49
50 public:
51 simple_int_container(int value) : _value{value} {}
52 virtual ~simple_int_container() {}
53 virtual int value() const override { return _value; }
54
55 private:
56 int _value;
57
58 };
59
60 class int_container_with_ignored_setter : public int_container
61 {
62 Q_OBJECT
63
64 public:
65 int_container_with_ignored_setter(int value) : _value{value} {}
66 virtual ~int_container_with_ignored_setter() {}
67 virtual int value() const override { return _value; }
68 default_constructible * object() const { return _object; }
69
70 private slots:
71 INJEQT_SET void set_default_constructible(default_constructible *object)
72 {
73 _object = object;
74 }
75
76 private:
77 int _value;
78 QPointer<default_constructible> _object;
79
80 };
81
82 class int_service : public QObject
83 {
84 Q_OBJECT
85
86 public:
87 Q_INVOKABLE int_service() {}
88 int value() const { return _int_container->value(); }
89
90 private slots:
91 INJEQT_SET void set_int_container(int_container *container)
92 {
93 _int_container = container;
94 }
95
96 private:
97 QPointer<int_container> _int_container;
98
99 };
100
101 class int_container_factory : public QObject
102 {
103 Q_OBJECT
104
105 public:
106 int_container_factory(int value) : _value{value} {}
107 Q_INVOKABLE int_container * create_int_container() const { return new simple_int_container{_value}; }
108
109 private:
110 int _value;
111
112 };
113
114 class int_container_with_ignored_setter_factory : public QObject
115 {
116 Q_OBJECT
117
118 public:
119 int_container_with_ignored_setter_factory(int value) : _value{value} {}
120 Q_INVOKABLE int_container * create_int_container() const { return new int_container_with_ignored_setter{_value}; }
121
122 private:
123 int _value;
124
125 };
126
127 class factory_behavior_test : public QObject
128 {
129 Q_OBJECT
130
131 private slots:
132 void should_create_proper_object_structure();
133 void should_ignore_invalid_setters_in_created_by_factory_object();
134 void should_ignore_valid_setters_in_created_by_factory_object();
135
136 };
137
138 void factory_behavior_test::should_create_proper_object_structure()
139 {
140 class m : public injeqt::module
141 {
142 public:
143 m(int value)
144 {
145 _factory = std::unique_ptr<int_container_factory>(new int_container_factory{value});
146 add_type<int_service>();
147 add_factory<int_container, int_container_factory>();
148 add_ready_object<int_container_factory>(_factory.get());
149 }
150 virtual ~m() {}
151 private:
152 std::unique_ptr<int_container_factory> _factory;
153 };
154
155 auto modules = std::vector<std::unique_ptr<injeqt::module>>{};
156 modules.emplace_back(std::unique_ptr<m>{new m{9}});
157 auto injector = injeqt::injector{std::move(modules)};
158 auto service = injector.get<int_service>();
159 QCOMPARE(9, service->value());
160 }
161
162 void factory_behavior_test::should_ignore_invalid_setters_in_created_by_factory_object()
163 {
164 class m : public injeqt::module
165 {
166 public:
167 m(int value)
168 {
169 _factory = std::unique_ptr<int_container_with_ignored_setter_factory>(new int_container_with_ignored_setter_factory{value});
170 add_type<int_service>();
171 add_factory<int_container, int_container_with_ignored_setter_factory>();
172 add_ready_object<int_container_with_ignored_setter_factory>(_factory.get());
173 }
174 virtual ~m() {}
175 private:
176 std::unique_ptr<int_container_with_ignored_setter_factory> _factory;
177 };
178
179 auto modules = std::vector<std::unique_ptr<injeqt::module>>{};
180 modules.emplace_back(std::unique_ptr<m>{new m{9}});
181 auto injector = injeqt::injector{std::move(modules)};
182 auto service = injector.get<int_service>();
183 auto container = qobject_cast<int_container_with_ignored_setter *>(injector.get<int_container>());
184 QCOMPARE(9, service->value());
185 QCOMPARE(static_cast<QObject *>(nullptr), container->object());
186 }
187
188 void factory_behavior_test::should_ignore_valid_setters_in_created_by_factory_object()
189 {
190 class m : public injeqt::module
191 {
192 public:
193 m(int value)
194 {
195 _factory = std::unique_ptr<int_container_with_ignored_setter_factory>(new int_container_with_ignored_setter_factory{value});
196 add_type<int_service>();
197 add_type<default_constructible>();
198 add_factory<int_container, int_container_with_ignored_setter_factory>();
199 add_ready_object<int_container_with_ignored_setter_factory>(_factory.get());
200 }
201 virtual ~m() {}
202 private:
203 std::unique_ptr<int_container_with_ignored_setter_factory> _factory;
204 };
205
206 auto modules = std::vector<std::unique_ptr<injeqt::module>>{};
207 modules.emplace_back(std::unique_ptr<m>{new m{9}});
208 auto injector = injeqt::injector{std::move(modules)};
209 auto service = injector.get<int_service>();
210 auto container = qobject_cast<int_container_with_ignored_setter *>(injector.get<int_container>());
211 QCOMPARE(9, service->value());
212 QCOMPARE(static_cast<QObject *>(nullptr), container->object());
213 }
214
215 QTEST_APPLESS_MAIN(factory_behavior_test)
216 #include "factory-behavior-test.moc"