| 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 <injeqt/injector.h> |
| 22 | #include <injeqt/module.h> |
| 23 | |
| 24 | #include <QtTest/QtTest> |
| 25 | |
| 26 | class default_constructible : public QObject |
| 27 | { |
| 28 | Q_OBJECT |
| 29 | |
| 30 | public: |
| 31 | Q_INVOKABLE default_constructible() {} |
| 32 | |
| 33 | }; |
| 34 | |
| 35 | class int_container : public QObject |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | |
| 39 | public: |
| 40 | int_container() {} |
| 41 | virtual ~int_container() {} |
| 42 | virtual int value() const = 0; |
| 43 | |
| 44 | }; |
| 45 | |
| 46 | class simple_int_container : public int_container |
| 47 | { |
| 48 | Q_OBJECT |
| 49 | |
| 50 | public: |
| 51 | simple_int_container(int value) : _value{value} {} |
| 52 | virtual ~simple_int_container() {} |
| 53 | virtual int value() const override { return _value; } |
| 54 | |
| 55 | private: |
| 56 | int _value; |
| 57 | |
| 58 | }; |
| 59 | |
| 60 | class int_container_with_ignored_setter : public int_container |
| 61 | { |
| 62 | Q_OBJECT |
| 63 | |
| 64 | public: |
| 65 | int_container_with_ignored_setter(int value) : _value{value} {} |
| 66 | virtual ~int_container_with_ignored_setter() {} |
| 67 | virtual int value() const override { return _value; } |
| 68 | default_constructible * object() const { return _object; } |
| 69 | |
| 70 | private slots: |
| 71 | INJEQT_SET void set_default_constructible(default_constructible *object) |
| 72 | { |
| 73 | _object = object; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | int _value; |
| 78 | QPointer<default_constructible> _object; |
| 79 | |
| 80 | }; |
| 81 | |
| 82 | class int_service : public QObject |
| 83 | { |
| 84 | Q_OBJECT |
| 85 | |
| 86 | public: |
| 87 | Q_INVOKABLE int_service() {} |
| 88 | int value() const { return _int_container->value(); } |
| 89 | |
| 90 | private slots: |
| 91 | INJEQT_SET void set_int_container(int_container *container) |
| 92 | { |
| 93 | _int_container = container; |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | QPointer<int_container> _int_container; |
| 98 | |
| 99 | }; |
| 100 | |
| 101 | class ready_object_behavior_test : public QObject |
| 102 | { |
| 103 | Q_OBJECT |
| 104 | |
| 105 | private slots: |
| 106 | void should_create_proper_object_structure(); |
| 107 | void should_ignore_invalid_setters_in_ready_object(); |
| 108 | void should_ignore_valid_setters_in_ready_object(); |
| 109 | |
| 110 | }; |
| 111 | |
| 112 | void ready_object_behavior_test::should_create_proper_object_structure() |
| 113 | { |
| 114 | class m : public injeqt::module |
| 115 | { |
| 116 | public: |
| 117 | m(int value) |
| 118 | { |
| 119 | _container = std::unique_ptr<simple_int_container>(new simple_int_container{value}); |
| 120 | add_type<int_service>(); |
| 121 | add_ready_object<simple_int_container>(_container.get()); |
| 122 | } |
| 123 | virtual ~m() {} |
| 124 | private: |
| 125 | std::unique_ptr<simple_int_container> _container; |
| 126 | }; |
| 127 | |
| 128 | auto modules = std::vector<std::unique_ptr<injeqt::module>>{}; |
| 129 | modules.emplace_back(std::unique_ptr<m>{new m{9}}); |
| 130 | auto injector = injeqt::injector{std::move(modules)}; |
| 131 | auto service = injector.get<int_service>(); |
| 132 | QCOMPARE(9, service->value()); |
| 133 | } |
| 134 | |
| 135 | void ready_object_behavior_test::should_ignore_invalid_setters_in_ready_object() |
| 136 | { |
| 137 | class m : public injeqt::module |
| 138 | { |
| 139 | public: |
| 140 | m(int value) |
| 141 | { |
| 142 | _container = std::unique_ptr<int_container_with_ignored_setter>(new int_container_with_ignored_setter{value}); |
| 143 | add_type<int_service>(); |
| 144 | add_ready_object<int_container_with_ignored_setter>(_container.get()); |
| 145 | } |
| 146 | virtual ~m() {} |
| 147 | private: |
| 148 | std::unique_ptr<int_container_with_ignored_setter> _container; |
| 149 | }; |
| 150 | |
| 151 | auto modules = std::vector<std::unique_ptr<injeqt::module>>{}; |
| 152 | modules.emplace_back(std::unique_ptr<m>{new m{9}}); |
| 153 | auto injector = injeqt::injector{std::move(modules)}; |
| 154 | auto service = injector.get<int_service>(); |
| 155 | auto container = injector.get<int_container_with_ignored_setter>(); |
| 156 | QCOMPARE(9, service->value()); |
| 157 | QCOMPARE(static_cast<QObject *>(nullptr), container->object()); |
| 158 | } |
| 159 | |
| 160 | void ready_object_behavior_test::should_ignore_valid_setters_in_ready_object() |
| 161 | { |
| 162 | class m : public injeqt::module |
| 163 | { |
| 164 | public: |
| 165 | m(int value) |
| 166 | { |
| 167 | _container = std::unique_ptr<int_container_with_ignored_setter>(new int_container_with_ignored_setter{value}); |
| 168 | add_type<int_service>(); |
| 169 | add_type<default_constructible>(); |
| 170 | add_ready_object<int_container_with_ignored_setter>(_container.get()); |
| 171 | } |
| 172 | virtual ~m() {} |
| 173 | private: |
| 174 | std::unique_ptr<int_container_with_ignored_setter> _container; |
| 175 | }; |
| 176 | |
| 177 | auto modules = std::vector<std::unique_ptr<injeqt::module>>{}; |
| 178 | modules.emplace_back(std::unique_ptr<m>{new m{9}}); |
| 179 | auto injector = injeqt::injector{std::move(modules)}; |
| 180 | auto service = injector.get<int_service>(); |
| 181 | auto container = injector.get<int_container_with_ignored_setter>(); |
| 182 | QCOMPARE(9, service->value()); |
| 183 | QCOMPARE(static_cast<QObject *>(nullptr), container->object()); |
| 184 | } |
| 185 | |
| 186 | QTEST_APPLESS_MAIN(ready_object_behavior_test) |
| 187 | #include "ready-object-behavior-test.moc" |