| 1 | /* |
| 2 | * %injeqt copyright begin% |
| 3 | * Copyright 2016 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_object; |
| 27 | class ready_object; |
| 28 | class factory_object; |
| 29 | class by_factory_object; |
| 30 | |
| 31 | class common_object : public QObject |
| 32 | { |
| 33 | Q_OBJECT |
| 34 | |
| 35 | public: |
| 36 | common_object() |
| 37 | { |
| 38 | QVERIFY(!_common_init_called); |
| 39 | QVERIFY(!_common_done_called); |
| 40 | }; |
| 41 | |
| 42 | virtual ~common_object() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | protected: |
| 47 | bool _common_init_called = false; |
| 48 | bool _common_done_called = false; |
| 49 | |
| 50 | private slots: |
| 51 | INJEQT_INIT void init() |
| 52 | { |
| 53 | QVERIFY(!_common_init_called); |
| 54 | QVERIFY(!_common_done_called); |
| 55 | _common_init_called = true; |
| 56 | } |
| 57 | INJEQT_DONE void done() |
| 58 | { |
| 59 | QVERIFY(_common_init_called); |
| 60 | QVERIFY(!_common_done_called); |
| 61 | _common_done_called = true; |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | class default_object : public common_object |
| 66 | { |
| 67 | Q_OBJECT |
| 68 | |
| 69 | public: |
| 70 | Q_INVOKABLE default_object() |
| 71 | { |
| 72 | QVERIFY(!_init_called); |
| 73 | QVERIFY(!_done_called); |
| 74 | }; |
| 75 | |
| 76 | virtual ~default_object() |
| 77 | { |
| 78 | QVERIFY(_init_called); |
| 79 | QVERIFY(_done_called); |
| 80 | QVERIFY(_common_init_called); |
| 81 | QVERIFY(_common_done_called); |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | QPointer<ready_object> _ready_object; |
| 86 | QPointer<by_factory_object> _by_factory_object; |
| 87 | bool _init_called = false; |
| 88 | bool _done_called = false; |
| 89 | |
| 90 | private slots: |
| 91 | INJEQT_INIT void init() |
| 92 | { |
| 93 | QVERIFY(_ready_object); |
| 94 | QVERIFY(_by_factory_object); |
| 95 | QVERIFY(_common_init_called); |
| 96 | QVERIFY(!_common_done_called); |
| 97 | QVERIFY(!_init_called); |
| 98 | QVERIFY(!_done_called); |
| 99 | _init_called = true; |
| 100 | } |
| 101 | INJEQT_DONE void done() |
| 102 | { |
| 103 | QVERIFY(_ready_object); |
| 104 | QVERIFY(_by_factory_object); |
| 105 | QVERIFY(_common_init_called); |
| 106 | QVERIFY(!_common_done_called); |
| 107 | QVERIFY(_init_called); |
| 108 | QVERIFY(!_done_called); |
| 109 | _done_called = true; |
| 110 | } |
| 111 | INJEQT_SET void set_ready_object(ready_object *x) |
| 112 | { |
| 113 | QVERIFY(!_init_called); |
| 114 | QVERIFY(!_done_called); |
| 115 | _ready_object = x; |
| 116 | } |
| 117 | INJEQT_SET void set_by_factory_object(by_factory_object *x) |
| 118 | { |
| 119 | QVERIFY(!_init_called); |
| 120 | QVERIFY(!_done_called); |
| 121 | _by_factory_object = x; |
| 122 | } |
| 123 | |
| 124 | }; |
| 125 | |
| 126 | class by_factory_object : public common_object |
| 127 | { |
| 128 | Q_OBJECT |
| 129 | |
| 130 | public: |
| 131 | Q_INVOKABLE by_factory_object() |
| 132 | { |
| 133 | QVERIFY(!_common_init_called); |
| 134 | QVERIFY(!_common_done_called); |
| 135 | }; |
| 136 | |
| 137 | virtual ~by_factory_object() |
| 138 | { |
| 139 | QVERIFY(!_common_init_called); |
| 140 | QVERIFY(!_common_done_called); |
| 141 | } |
| 142 | |
| 143 | private slots: |
| 144 | INJEQT_INIT void init() |
| 145 | { |
| 146 | QVERIFY(false); |
| 147 | } |
| 148 | INJEQT_DONE void done() |
| 149 | { |
| 150 | QVERIFY(false); |
| 151 | } |
| 152 | |
| 153 | }; |
| 154 | |
| 155 | class factory : public QObject |
| 156 | { |
| 157 | Q_OBJECT |
| 158 | |
| 159 | public: |
| 160 | Q_INVOKABLE factory() {} |
| 161 | Q_INVOKABLE by_factory_object * create_by_factory_object() const { return new by_factory_object{}; } |
| 162 | |
| 163 | }; |
| 164 | |
| 165 | class ready_object : public common_object |
| 166 | { |
| 167 | Q_OBJECT |
| 168 | |
| 169 | public: |
| 170 | Q_INVOKABLE ready_object() |
| 171 | { |
| 172 | QVERIFY(!_common_init_called); |
| 173 | QVERIFY(!_common_done_called); |
| 174 | }; |
| 175 | |
| 176 | virtual ~ready_object() |
| 177 | { |
| 178 | QVERIFY(!_common_init_called); |
| 179 | QVERIFY(!_common_done_called); |
| 180 | } |
| 181 | |
| 182 | private slots: |
| 183 | INJEQT_INIT void init() |
| 184 | { |
| 185 | QVERIFY(false); |
| 186 | } |
| 187 | INJEQT_DONE void done() |
| 188 | { |
| 189 | QVERIFY(false); |
| 190 | } |
| 191 | |
| 192 | }; |
| 193 | |
| 194 | class injeqt_into_object : public common_object |
| 195 | { |
| 196 | Q_OBJECT |
| 197 | |
| 198 | public: |
| 199 | Q_INVOKABLE injeqt_into_object() |
| 200 | { |
| 201 | QVERIFY(!_common_init_called); |
| 202 | QVERIFY(!_common_done_called); |
| 203 | }; |
| 204 | |
| 205 | virtual ~injeqt_into_object() |
| 206 | { |
| 207 | QVERIFY(_init_called); |
| 208 | QVERIFY(_common_init_called); |
| 209 | QVERIFY(!_common_done_called); |
| 210 | } |
| 211 | |
| 212 | private: |
| 213 | QPointer<default_object> _default_object; |
| 214 | QPointer<ready_object> _ready_object; |
| 215 | QPointer<by_factory_object> _by_factory_object; |
| 216 | bool _init_called = false; |
| 217 | |
| 218 | private slots: |
| 219 | INJEQT_INIT void init() |
| 220 | { |
| 221 | QVERIFY(_default_object); |
| 222 | QVERIFY(_ready_object); |
| 223 | QVERIFY(_by_factory_object); |
| 224 | QVERIFY(_common_init_called); |
| 225 | QVERIFY(!_common_done_called); |
| 226 | _init_called = true; |
| 227 | } |
| 228 | INJEQT_DONE void done() |
| 229 | { |
| 230 | QVERIFY(false); |
| 231 | } |
| 232 | INJEQT_SET void set_default_object(default_object *x) |
| 233 | { |
| 234 | QVERIFY(!_init_called); |
| 235 | QVERIFY(!_common_init_called); |
| 236 | _default_object = x; |
| 237 | } |
| 238 | INJEQT_SET void set_ready_object(ready_object *x) |
| 239 | { |
| 240 | QVERIFY(!_init_called); |
| 241 | QVERIFY(!_common_init_called); |
| 242 | _ready_object = x; |
| 243 | } |
| 244 | INJEQT_SET void set_by_factory_object(by_factory_object *x) |
| 245 | { |
| 246 | QVERIFY(!_init_called); |
| 247 | QVERIFY(!_common_init_called); |
| 248 | _by_factory_object = x; |
| 249 | } |
| 250 | |
| 251 | }; |
| 252 | |
| 253 | class init_done_test : public QObject |
| 254 | { |
| 255 | Q_OBJECT |
| 256 | |
| 257 | private slots: |
| 258 | void should_properly_call_init_and_done(); |
| 259 | |
| 260 | }; |
| 261 | |
| 262 | void init_done_test::should_properly_call_init_and_done() |
| 263 | { |
| 264 | class m : public injeqt::module |
| 265 | { |
| 266 | public: |
| 267 | m() |
| 268 | { |
| 269 | _ready_object = std::unique_ptr<ready_object>(new ready_object{}); |
| 270 | add_type<default_object>(); |
| 271 | add_type<factory>(); |
| 272 | add_factory<by_factory_object, factory>(); |
| 273 | add_ready_object<ready_object>(_ready_object.get()); |
| 274 | } |
| 275 | virtual ~m() {} |
| 276 | private: |
| 277 | std::unique_ptr<ready_object> _ready_object; |
| 278 | }; |
| 279 | |
| 280 | auto modules = std::vector<std::unique_ptr<injeqt::module>>{}; |
| 281 | modules.emplace_back(std::unique_ptr<m>{new m{}}); |
| 282 | auto injector = injeqt::injector{std::move(modules)}; |
| 283 | |
| 284 | injector.get<default_object>(); |
| 285 | injector.get<by_factory_object>(); |
| 286 | injector.get<ready_object>(); |
| 287 | |
| 288 | injeqt_into_object object{}; |
| 289 | injector.inject_into(&object); |
| 290 | } |
| 291 | |
| 292 | QTEST_APPLESS_MAIN(init_done_test) |
| 293 | #include "init-done-test.moc" |