| 1 | /* |
| 2 | * %injeqt copyright begin% |
| 3 | * Copyright 2015 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 | injeqt::injector global_injector; |
| 27 | |
| 28 | class injectable; |
| 29 | |
| 30 | class injeqt_into_me : public QObject |
| 31 | { |
| 32 | Q_OBJECT |
| 33 | |
| 34 | public: |
| 35 | injeqt_into_me() {} |
| 36 | virtual ~injeqt_into_me() {} |
| 37 | |
| 38 | private slots: |
| 39 | INJEQT_SET void set_injectable(injectable *) {} |
| 40 | |
| 41 | }; |
| 42 | |
| 43 | class injectable : public QObject |
| 44 | { |
| 45 | Q_OBJECT |
| 46 | |
| 47 | public: |
| 48 | Q_INVOKABLE injectable() {} |
| 49 | virtual ~injectable() {} |
| 50 | |
| 51 | private slots: |
| 52 | INJEQT_INIT void init() |
| 53 | { |
| 54 | injeqt_into_me o; |
| 55 | global_injector.inject_into(&o); |
| 56 | } |
| 57 | |
| 58 | }; |
| 59 | |
| 60 | class inject_into_during_init_test : public QObject |
| 61 | { |
| 62 | Q_OBJECT |
| 63 | |
| 64 | private slots: |
| 65 | void should_properly_inject_into_during_init(); |
| 66 | |
| 67 | }; |
| 68 | |
| 69 | void inject_into_during_init_test::should_properly_inject_into_during_init() |
| 70 | { |
| 71 | class m : public injeqt::module |
| 72 | { |
| 73 | public: |
| 74 | m() |
| 75 | { |
| 76 | add_type<injectable>(); |
| 77 | } |
| 78 | virtual ~m() {} |
| 79 | }; |
| 80 | |
| 81 | auto modules = std::vector<std::unique_ptr<injeqt::module>>{}; |
| 82 | modules.emplace_back(std::unique_ptr<m>{new m{}}); |
| 83 | global_injector = injeqt::injector{std::move(modules)}; |
| 84 | global_injector.get<injectable>(); |
| 85 | } |
| 86 | |
| 87 | QTEST_APPLESS_MAIN(inject_into_during_init_test) |
| 88 | #include "inject-into-during-init-test.moc" |