| 1 | /* |
| 2 | * %injeqt copyright begin% |
| 3 | * Copyright 2014 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 "expect.h" |
| 22 | |
| 23 | #include <injeqt/exception/qobject-type.h> |
| 24 | #include <injeqt/exception/unique-factory-method-not-found.h> |
| 25 | |
| 26 | #include "internal/provider-by-factory-configuration.h" |
| 27 | #include "internal/provider.h" |
| 28 | #include "internal/types.h" |
| 29 | |
| 30 | #include <QtTest/QtTest> |
| 31 | #include <memory> |
| 32 | |
| 33 | using namespace injeqt::v1; |
| 34 | using namespace injeqt::internal; |
| 35 | |
| 36 | class type_1 : public QObject |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | }; |
| 40 | |
| 41 | class type_2 : public QObject |
| 42 | { |
| 43 | Q_OBJECT |
| 44 | }; |
| 45 | |
| 46 | class type_1_subtype_1 : public type_1 |
| 47 | { |
| 48 | Q_OBJECT |
| 49 | }; |
| 50 | |
| 51 | class factory_type : public QObject |
| 52 | { |
| 53 | Q_OBJECT |
| 54 | public: |
| 55 | Q_INVOKABLE type_1 * create() { return new type_1{}; } |
| 56 | }; |
| 57 | |
| 58 | class subtype_factory_type : public QObject |
| 59 | { |
| 60 | Q_OBJECT |
| 61 | public: |
| 62 | Q_INVOKABLE type_1_subtype_1 * create() { return new type_1_subtype_1{}; } |
| 63 | }; |
| 64 | |
| 65 | class non_unique_factory_type : public QObject |
| 66 | { |
| 67 | Q_OBJECT |
| 68 | public: |
| 69 | Q_INVOKABLE type_1 * create() { return nullptr; } |
| 70 | Q_INVOKABLE type_1 * create2() { return nullptr; } |
| 71 | }; |
| 72 | |
| 73 | class provider_by_factory_configuration_test : public QObject |
| 74 | { |
| 75 | Q_OBJECT |
| 76 | |
| 77 | public: |
| 78 | provider_by_factory_configuration_test(); |
| 79 | |
| 80 | private slots: |
| 81 | void should_accept_qobject_object_type_and_throw_on_create(); |
| 82 | void should_accept_qobject_factory_type_and_throw_on_create(); |
| 83 | void should_accept_factory_without_factory_method_and_throw_on_create(); |
| 84 | void should_accept_factory_without_unique_factory_method_and_throw_on_create(); |
| 85 | void should_accept_supertype_factory_and_throw_on_create(); |
| 86 | void should_accept_valid_configuration(); |
| 87 | void should_accept_subtype_configuration(); |
| 88 | |
| 89 | private: |
| 90 | types_by_name known_types; |
| 91 | |
| 92 | }; |
| 93 | |
| 94 | provider_by_factory_configuration_test::provider_by_factory_configuration_test() |
| 95 | { |
| 96 | known_types = types_by_name{std::vector<type>{ |
| 97 | make_type<type_1>(), |
| 98 | make_type<type_1_subtype_1>(), |
| 99 | make_type<type_2>(), |
| 100 | make_type<factory_type>(), |
| 101 | make_type<subtype_factory_type>(), |
| 102 | make_type<non_unique_factory_type>() |
| 103 | }}; |
| 104 | } |
| 105 | |
| 106 | void provider_by_factory_configuration_test::should_accept_qobject_object_type_and_throw_on_create() |
| 107 | { |
| 108 | auto pc = provider_by_factory_configuration{make_type<QObject>(), make_type<factory_type>()}; |
| 109 | |
| 110 | expect<exception::qobject_type>([&](){ |
| 111 | pc.create_provider(known_types); |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | void provider_by_factory_configuration_test::should_accept_qobject_factory_type_and_throw_on_create() |
| 116 | { |
| 117 | auto pc = provider_by_factory_configuration{make_type<type_1>(), make_type<QObject>()}; |
| 118 | |
| 119 | expect<exception::qobject_type>([&](){ |
| 120 | pc.create_provider(known_types); |
| 121 | }); |
| 122 | } |
| 123 | |
| 124 | void provider_by_factory_configuration_test::should_accept_factory_without_factory_method_and_throw_on_create() |
| 125 | { |
| 126 | auto pc = provider_by_factory_configuration{make_type<type_2>(), make_type<factory_type>()}; |
| 127 | |
| 128 | expect<exception::unique_factory_method_not_found>([&](){ |
| 129 | pc.create_provider(known_types); |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | void provider_by_factory_configuration_test::should_accept_factory_without_unique_factory_method_and_throw_on_create() |
| 134 | { |
| 135 | auto pc = provider_by_factory_configuration{make_type<type_2>(), make_type<non_unique_factory_type>()}; |
| 136 | |
| 137 | expect<exception::unique_factory_method_not_found>([&](){ |
| 138 | pc.create_provider(known_types); |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | void provider_by_factory_configuration_test::should_accept_supertype_factory_and_throw_on_create() |
| 143 | { |
| 144 | auto pc = provider_by_factory_configuration{make_type<type_1_subtype_1>(), make_type<factory_type>()}; |
| 145 | |
| 146 | expect<exception::unique_factory_method_not_found>([&](){ |
| 147 | pc.create_provider(known_types); |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | void provider_by_factory_configuration_test::should_accept_valid_configuration() |
| 152 | { |
| 153 | auto pc = provider_by_factory_configuration{make_type<type_1>(), make_type<factory_type>()}; |
| 154 | auto p = pc.create_provider(known_types); |
| 155 | |
| 156 | QCOMPARE(types{make_type<factory_type>()}, p->required_types()); |
| 157 | QCOMPARE(make_type<type_1>(), p->provided_type()); |
| 158 | } |
| 159 | |
| 160 | void provider_by_factory_configuration_test::should_accept_subtype_configuration() |
| 161 | { |
| 162 | auto pc = provider_by_factory_configuration{make_type<type_1>(), make_type<subtype_factory_type>()}; |
| 163 | auto p = pc.create_provider(known_types); |
| 164 | |
| 165 | QCOMPARE(types{make_type<subtype_factory_type>()}, p->required_types()); |
| 166 | QCOMPARE(make_type<type_1_subtype_1>(), p->provided_type()); |
| 167 | } |
| 168 | |
| 169 | QTEST_APPLESS_MAIN(provider_by_factory_configuration_test) |
| 170 | #include "provider-by-factory-configuration-test.moc" |