| 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 | #include "utils.h" |
| 23 | |
| 24 | #include <injeqt/exception/interface-not-implemented.h> |
| 25 | #include <injeqt/exception/invalid-qobject.h> |
| 26 | #include <injeqt/exception/qobject-type.h> |
| 27 | |
| 28 | #include "internal/injector-core.h" |
| 29 | #include "internal/provider-ready-configuration.h" |
| 30 | |
| 31 | #include <QtTest/QtTest> |
| 32 | #include <memory> |
| 33 | |
| 34 | using namespace injeqt::v1; |
| 35 | using namespace injeqt::internal; |
| 36 | |
| 37 | class type_1 : public QObject |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | }; |
| 41 | |
| 42 | class type_2 : public QObject |
| 43 | { |
| 44 | Q_OBJECT |
| 45 | }; |
| 46 | |
| 47 | class provider_ready_configuration_test : public QObject |
| 48 | { |
| 49 | Q_OBJECT |
| 50 | |
| 51 | private slots: |
| 52 | void should_accept_qobject_type_and_throw_on_create(); |
| 53 | void should_accept_nullptr_object_and_throw_on_create(); |
| 54 | void should_accept_not_implemented_interface_and_throw_on_create(); |
| 55 | void should_accept_valid_configuration(); |
| 56 | |
| 57 | }; |
| 58 | |
| 59 | void provider_ready_configuration_test::should_accept_qobject_type_and_throw_on_create() |
| 60 | { |
| 61 | auto pc = provider_ready_configuration{make_type<QObject>(), nullptr}; |
| 62 | |
| 63 | expect<exception::qobject_type>([&](){ |
| 64 | pc.create_provider({}); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | void provider_ready_configuration_test::should_accept_nullptr_object_and_throw_on_create() |
| 69 | { |
| 70 | auto pc = provider_ready_configuration{make_type<type_1>(), nullptr}; |
| 71 | |
| 72 | expect<exception::invalid_qobject>([&](){ |
| 73 | pc.create_provider({}); |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | void provider_ready_configuration_test::should_accept_not_implemented_interface_and_throw_on_create() |
| 78 | { |
| 79 | auto o = make_object<type_2>(); |
| 80 | auto pc = provider_ready_configuration{make_type<type_1>(), o.get()}; |
| 81 | |
| 82 | expect<exception::interface_not_implemented>([&](){ |
| 83 | pc.create_provider({}); |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | void provider_ready_configuration_test::should_accept_valid_configuration() |
| 88 | { |
| 89 | auto i = injector_core{}; |
| 90 | auto o = make_object<type_1>(); |
| 91 | auto pc = provider_ready_configuration{make_type<type_1>(), o.get()}; |
| 92 | auto p = pc.create_provider({}); |
| 93 | |
| 94 | QCOMPARE(o.get(), p->provide(i)); |
| 95 | } |
| 96 | |
| 97 | QTEST_APPLESS_MAIN(provider_ready_configuration_test) |
| 98 | #include "provider-ready-configuration-test.moc" |