| 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 | class role_1_type_1 : public QObject |
| 35 | { |
| 36 | Q_OBJECT |
| 37 | INJEQT_TYPE_ROLE(ROLE_1) |
| 38 | |
| 39 | public: |
| 40 | Q_INVOKABLE role_1_type_1() { } |
| 41 | }; |
| 42 | |
| 43 | class role_1_type_2 : public QObject |
| 44 | { |
| 45 | Q_OBJECT |
| 46 | INJEQT_TYPE_ROLE(ROLE_1) |
| 47 | |
| 48 | public: |
| 49 | Q_INVOKABLE role_1_type_2() { } |
| 50 | }; |
| 51 | |
| 52 | class role_2_type_1 : public QObject |
| 53 | { |
| 54 | Q_OBJECT |
| 55 | INJEQT_TYPE_ROLE(ROLE_2) |
| 56 | |
| 57 | public: |
| 58 | Q_INVOKABLE role_2_type_1() { } |
| 59 | }; |
| 60 | |
| 61 | class role_2_type_2 : public QObject |
| 62 | { |
| 63 | Q_OBJECT |
| 64 | INJEQT_TYPE_ROLE(ROLE_2) |
| 65 | |
| 66 | public: |
| 67 | Q_INVOKABLE role_2_type_2() { } |
| 68 | }; |
| 69 | |
| 70 | class get_all_with_type_role_test : public QObject |
| 71 | { |
| 72 | Q_OBJECT |
| 73 | |
| 74 | private: |
| 75 | injeqt::injector create_injector(); |
| 76 | |
| 77 | private slots: |
| 78 | void should_return_all_role_instances(); |
| 79 | |
| 80 | }; |
| 81 | |
| 82 | injeqt::injector get_all_with_type_role_test::create_injector() |
| 83 | { |
| 84 | class m : public injeqt::module |
| 85 | { |
| 86 | public: |
| 87 | m() |
| 88 | { |
| 89 | add_type<role_1_type_1>(); |
| 90 | add_type<role_1_type_2>(); |
| 91 | add_type<role_2_type_1>(); |
| 92 | add_type<role_2_type_2>(); |
| 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 | return injeqt::injector{std::move(modules)}; |
| 100 | } |
| 101 | |
| 102 | void get_all_with_type_role_test::should_return_all_role_instances() |
| 103 | { |
| 104 | auto injector = create_injector(); |
| 105 | auto role_1_objects = injector.get_all_with_type_role(ROLE_1); |
| 106 | QCOMPARE(role_1_objects.size(), size_t{2}); |
| 107 | QVERIFY(std::any_of(std::begin(role_1_objects), std::end(role_1_objects), [](QObject *o){ |
| 108 | return !!qobject_cast<role_1_type_1 *>(o); |
| 109 | })); |
| 110 | QVERIFY(std::any_of(std::begin(role_1_objects), std::end(role_1_objects), [](QObject *o){ |
| 111 | return !!qobject_cast<role_1_type_2 *>(o); |
| 112 | })); |
| 113 | |
| 114 | auto role_2_object = injector.get<role_2_type_1>(); |
| 115 | auto role_2_objects = injector.get_all_with_type_role(ROLE_2); |
| 116 | QCOMPARE(role_2_objects.size(), size_t{2}); |
| 117 | QVERIFY(std::any_of(std::begin(role_2_objects), std::end(role_2_objects), [](QObject *o){ |
| 118 | return !!qobject_cast<role_2_type_1 *>(o); |
| 119 | })); |
| 120 | QVERIFY(std::any_of(std::begin(role_2_objects), std::end(role_2_objects), [](QObject *o){ |
| 121 | return !!qobject_cast<role_2_type_2 *>(o); |
| 122 | })); |
| 123 | QVERIFY(std::any_of(std::begin(role_2_objects), std::end(role_2_objects), [role_2_object](QObject *o){ |
| 124 | return o == role_2_object; |
| 125 | })); |
| 126 | } |
| 127 | |
| 128 | QTEST_APPLESS_MAIN(get_all_with_type_role_test) |
| 129 | #include "get-all-with-type-role-test.moc" |