| 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 | #include <injeqt/type.h> |
| 24 | |
| 25 | #include <QtTest/QtTest> |
| 26 | #include <set> |
| 27 | |
| 28 | using injeqt::make_type; |
| 29 | using injeqt::type; |
| 30 | |
| 31 | #define ROLE_1 "role1" |
| 32 | #define ROLE_2 "role2" |
| 33 | |
| 34 | std::set<type> instantiated_types; |
| 35 | |
| 36 | class role_1_type : public QObject |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | INJEQT_TYPE_ROLE(ROLE_1) |
| 40 | |
| 41 | public: |
| 42 | Q_INVOKABLE role_1_type() { instantiated_types.insert(make_type<role_1_type>()); } |
| 43 | }; |
| 44 | |
| 45 | class no_role_required_by_role_2 : public QObject |
| 46 | { |
| 47 | Q_OBJECT |
| 48 | |
| 49 | public: |
| 50 | Q_INVOKABLE no_role_required_by_role_2() { instantiated_types.insert(make_type<no_role_required_by_role_2>()); } |
| 51 | }; |
| 52 | |
| 53 | class role_2_type : public QObject |
| 54 | { |
| 55 | Q_OBJECT |
| 56 | INJEQT_TYPE_ROLE(ROLE_2) |
| 57 | |
| 58 | public: |
| 59 | Q_INVOKABLE role_2_type() { instantiated_types.insert(make_type<role_2_type>()); } |
| 60 | |
| 61 | private slots: |
| 62 | INJEQT_SET void set_no_role_required_by_role_2(no_role_required_by_role_2 *) {} |
| 63 | |
| 64 | }; |
| 65 | |
| 66 | class instantiate_all_with_type_role_test : public QObject |
| 67 | { |
| 68 | Q_OBJECT |
| 69 | |
| 70 | private: |
| 71 | injeqt::injector create_injector(); |
| 72 | |
| 73 | private slots: |
| 74 | void should_create_all_role_instances_with_dependencied_when_requested(); |
| 75 | |
| 76 | }; |
| 77 | |
| 78 | injeqt::injector instantiate_all_with_type_role_test::create_injector() |
| 79 | { |
| 80 | class m : public injeqt::module |
| 81 | { |
| 82 | public: |
| 83 | m() |
| 84 | { |
| 85 | add_type<role_1_type>(); |
| 86 | add_type<no_role_required_by_role_2>(); |
| 87 | add_type<role_2_type>(); |
| 88 | } |
| 89 | virtual ~m() {} |
| 90 | }; |
| 91 | |
| 92 | auto modules = std::vector<std::unique_ptr<injeqt::module>>{}; |
| 93 | modules.emplace_back(std::unique_ptr<m>{new m{}}); |
| 94 | return injeqt::injector{std::move(modules)}; |
| 95 | } |
| 96 | |
| 97 | void instantiate_all_with_type_role_test::should_create_all_role_instances_with_dependencied_when_requested() |
| 98 | { |
| 99 | auto injector = create_injector(); |
| 100 | instantiated_types.clear(); |
| 101 | injector.instantiate_all_with_type_role(ROLE_2); |
| 102 | QCOMPARE(instantiated_types, (std::set<type>{make_type<role_2_type>(), make_type<no_role_required_by_role_2>()})); |
| 103 | |
| 104 | instantiated_types.clear(); |
| 105 | injector.instantiate_all_with_type_role(ROLE_1); |
| 106 | QCOMPARE(instantiated_types, (std::set<type>{make_type<role_1_type>()})); |
| 107 | } |
| 108 | |
| 109 | QTEST_APPLESS_MAIN(instantiate_all_with_type_role_test) |
| 110 | #include "instantiate-all-with-type-role-test.moc" |