| 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 injectable_super : public QObject |
| 27 | { |
| 28 | Q_OBJECT |
| 29 | |
| 30 | public: |
| 31 | Q_INVOKABLE injectable_super() {} |
| 32 | virtual ~injectable_super() {} |
| 33 | |
| 34 | }; |
| 35 | |
| 36 | class injectable_sub : public injectable_super |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | |
| 40 | public: |
| 41 | Q_INVOKABLE injectable_sub() {} |
| 42 | virtual ~injectable_sub() {} |
| 43 | |
| 44 | }; |
| 45 | |
| 46 | class super_dependnecies : public QObject |
| 47 | { |
| 48 | Q_OBJECT |
| 49 | |
| 50 | public: |
| 51 | injectable_super *v = nullptr; |
| 52 | |
| 53 | Q_INVOKABLE super_dependnecies() {} |
| 54 | virtual ~super_dependnecies() {} |
| 55 | |
| 56 | private slots: |
| 57 | INJEQT_SET void set_injectable_super(injectable_super *x) { v = x; } |
| 58 | |
| 59 | }; |
| 60 | |
| 61 | class sub_dependnecies : public QObject |
| 62 | { |
| 63 | Q_OBJECT |
| 64 | |
| 65 | public: |
| 66 | injectable_sub *v = nullptr; |
| 67 | |
| 68 | Q_INVOKABLE sub_dependnecies() {} |
| 69 | virtual ~sub_dependnecies() {} |
| 70 | |
| 71 | private slots: |
| 72 | INJEQT_SET void set_injectable_sub(injectable_sub *x) { v = x; } |
| 73 | |
| 74 | }; |
| 75 | |
| 76 | class super_sub_dependency_test : public QObject |
| 77 | { |
| 78 | Q_OBJECT |
| 79 | |
| 80 | private: |
| 81 | injeqt::injector get_injector() const; |
| 82 | |
| 83 | private slots: |
| 84 | void should_set_valid_super_dependencies(); |
| 85 | void should_set_valid_sub_dependencies(); |
| 86 | |
| 87 | }; |
| 88 | |
| 89 | injeqt::injector super_sub_dependency_test::get_injector() const |
| 90 | { |
| 91 | class m : public injeqt::module |
| 92 | { |
| 93 | public: |
| 94 | m() |
| 95 | { |
| 96 | add_type<injectable_sub>(); |
| 97 | add_type<super_dependnecies>(); |
| 98 | add_type<sub_dependnecies>(); |
| 99 | } |
| 100 | virtual ~m() {} |
| 101 | }; |
| 102 | |
| 103 | auto modules = std::vector<std::unique_ptr<injeqt::module>>{}; |
| 104 | modules.emplace_back(std::unique_ptr<m>{new m{}}); |
| 105 | return injeqt::injector{std::move(modules)}; |
| 106 | } |
| 107 | |
| 108 | void super_sub_dependency_test::should_set_valid_super_dependencies() |
| 109 | { |
| 110 | auto injector = get_injector(); |
| 111 | auto c = injector.get<super_dependnecies>(); |
| 112 | auto sub = injector.get<injectable_sub>(); |
| 113 | auto super = injector.get<injectable_super>(); |
| 114 | |
| 115 | QVERIFY(c != nullptr); |
| 116 | QVERIFY(sub != nullptr); |
| 117 | QVERIFY(super != nullptr); |
| 118 | QVERIFY(c->v == super); |
| 119 | QVERIFY(c->v == sub); |
| 120 | } |
| 121 | |
| 122 | void super_sub_dependency_test::should_set_valid_sub_dependencies() |
| 123 | { |
| 124 | auto injector = get_injector(); |
| 125 | auto c = injector.get<sub_dependnecies>(); |
| 126 | auto sub = injector.get<injectable_sub>(); |
| 127 | auto super = injector.get<injectable_super>(); |
| 128 | |
| 129 | QVERIFY(c != nullptr); |
| 130 | QVERIFY(sub != nullptr); |
| 131 | QVERIFY(super != nullptr); |
| 132 | QVERIFY(c->v == super); |
| 133 | QVERIFY(c->v == sub); |
| 134 | } |
| 135 | |
| 136 | QTEST_APPLESS_MAIN(super_sub_dependency_test) |
| 137 | #include "super-sub-dependency-test.moc" |