| 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 | class int_container : public QObject |
| 27 | { |
| 28 | Q_OBJECT |
| 29 | |
| 30 | public: |
| 31 | int_container() {} |
| 32 | virtual ~int_container() {} |
| 33 | virtual int value() const = 0; |
| 34 | |
| 35 | }; |
| 36 | |
| 37 | class nine_container : public int_container |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | |
| 41 | public: |
| 42 | Q_INVOKABLE nine_container() {} |
| 43 | virtual ~nine_container() {} |
| 44 | virtual int value() const override { return 9; } |
| 45 | |
| 46 | }; |
| 47 | |
| 48 | class int_service : public QObject |
| 49 | { |
| 50 | Q_OBJECT |
| 51 | |
| 52 | public: |
| 53 | Q_INVOKABLE int_service() {} |
| 54 | int value() const { return _int_container->value(); } |
| 55 | |
| 56 | private slots: |
| 57 | INJEQT_SET void set_int_container(int_container *container) |
| 58 | { |
| 59 | _int_container = container; |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | QPointer<int_container> _int_container; |
| 64 | |
| 65 | }; |
| 66 | |
| 67 | class int_sub_service : public int_service |
| 68 | { |
| 69 | Q_OBJECT |
| 70 | |
| 71 | public: |
| 72 | Q_INVOKABLE int_sub_service() {} |
| 73 | |
| 74 | }; |
| 75 | |
| 76 | class inject_into_behavior_test : public QObject |
| 77 | { |
| 78 | Q_OBJECT |
| 79 | |
| 80 | private slots: |
| 81 | void should_properly_inject_into(); |
| 82 | |
| 83 | }; |
| 84 | |
| 85 | void inject_into_behavior_test::should_properly_inject_into() |
| 86 | { |
| 87 | class m : public injeqt::module |
| 88 | { |
| 89 | public: |
| 90 | m() |
| 91 | { |
| 92 | add_type<nine_container>(); |
| 93 | } |
| 94 | virtual ~m() {} |
| 95 | }; |
| 96 | |
| 97 | auto modules = std::vector<std::unique_ptr<injeqt::module>>{}; |
| 98 | modules.emplace_back(std::unique_ptr<m>{new m{}}); |
| 99 | auto injector = injeqt::injector{std::move(modules)}; |
| 100 | |
| 101 | int_service service{}; |
| 102 | injector.inject_into(&service); |
| 103 | QCOMPARE(9, service.value()); |
| 104 | |
| 105 | int_sub_service sub_service{}; |
| 106 | injector.inject_into(&sub_service); |
| 107 | QCOMPARE(9, sub_service.value()); |
| 108 | } |
| 109 | |
| 110 | QTEST_APPLESS_MAIN(inject_into_behavior_test) |
| 111 | #include "inject-into-behavior-test.moc" |