| 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/empty-type.h> |
| 25 | #include <injeqt/exception/interface-not-implemented.h> |
| 26 | #include <injeqt/exception/invalid-qobject.h> |
| 27 | #include <injeqt/exception/qobject-type.h> |
| 28 | |
| 29 | #include "internal/implementation.h" |
| 30 | |
| 31 | #include <QtTest/QtTest> |
| 32 | |
| 33 | using namespace injeqt::internal; |
| 34 | using namespace injeqt::v1; |
| 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 type_1_subtype_1_subtype_1 : public type_1_subtype_1 |
| 52 | { |
| 53 | Q_OBJECT |
| 54 | }; |
| 55 | |
| 56 | class implementation_test : public QObject |
| 57 | { |
| 58 | Q_OBJECT |
| 59 | |
| 60 | private slots: |
| 61 | void should_throw_when_type_is_empty(); |
| 62 | void should_throw_when_type_is_qobject(); |
| 63 | void should_throw_when_object_is_null(); |
| 64 | void should_accept_object_of_type(); |
| 65 | void should_accept_object_of_sub_type(); |
| 66 | void should_accept_object_of_sub_sub_type(); |
| 67 | void should_not_accept_object_of_super_type(); |
| 68 | void should_not_accept_object_of_other_type(); |
| 69 | void should_properly_compare(); |
| 70 | |
| 71 | }; |
| 72 | |
| 73 | void implementation_test::should_throw_when_type_is_empty() |
| 74 | { |
| 75 | auto object = make_object<type_1>(); |
| 76 | expect<exception::empty_type>([&]{ |
| 77 | make_implementation(type{}, object.get()); |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | void implementation_test::should_throw_when_type_is_qobject() |
| 82 | { |
| 83 | auto object = make_object<type_1>(); |
| 84 | expect<exception::qobject_type>([&]{ |
| 85 | make_implementation(make_type<QObject>(), object.get()); |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | void implementation_test::should_throw_when_object_is_null() |
| 90 | { |
| 91 | expect<exception::invalid_qobject>({}, [&]{ |
| 92 | make_implementation(make_type<type_1>(), nullptr); |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | void implementation_test::should_accept_object_of_type() |
| 97 | { |
| 98 | auto object = make_object<type_1>(); |
| 99 | auto i = make_implementation(make_type<type_1>(), object.get()); |
| 100 | |
| 101 | QCOMPARE(make_type<type_1>(), i.interface_type()); |
| 102 | QCOMPARE(object.get(), i.object()); |
| 103 | } |
| 104 | |
| 105 | void implementation_test::should_accept_object_of_sub_type() |
| 106 | { |
| 107 | auto object = make_object<type_1_subtype_1>(); |
| 108 | auto i = make_implementation(make_type<type_1>(), object.get()); |
| 109 | |
| 110 | QCOMPARE(make_type<type_1>(), i.interface_type()); |
| 111 | QCOMPARE(object.get(), i.object()); |
| 112 | } |
| 113 | |
| 114 | void implementation_test::should_accept_object_of_sub_sub_type() |
| 115 | { |
| 116 | auto object = make_object<type_1_subtype_1_subtype_1>(); |
| 117 | auto i = make_implementation(make_type<type_1>(), object.get()); |
| 118 | |
| 119 | QCOMPARE(make_type<type_1>(), i.interface_type()); |
| 120 | QCOMPARE(object.get(), i.object()); |
| 121 | } |
| 122 | |
| 123 | void implementation_test::should_not_accept_object_of_super_type() |
| 124 | { |
| 125 | auto object = make_object<type_1>(); |
| 126 | expect<exception::interface_not_implemented>({"type_1", "type_1_subtype_1_subtype_1"}, [&]{ |
| 127 | make_implementation(make_type<type_1_subtype_1_subtype_1>(), object.get()); |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | void implementation_test::should_not_accept_object_of_other_type() |
| 132 | { |
| 133 | auto object = make_object<type_1>(); |
| 134 | expect<exception::interface_not_implemented>({"type_1", "type_2"}, [&]{ |
| 135 | make_implementation(make_type<type_2>(), object.get()); |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | void implementation_test::should_properly_compare() |
| 140 | { |
| 141 | auto o1a = make_object<type_1>(); |
| 142 | auto o1b = make_object<type_1>(); |
| 143 | auto o2 = make_object<type_2>(); |
| 144 | |
| 145 | auto i1a = make_implementation(make_type<type_1>(), o1a.get()); |
| 146 | auto i1b = make_implementation(make_type<type_1>(), o1a.get()); |
| 147 | auto i2a = make_implementation(make_type<type_1>(), o1b.get()); |
| 148 | auto i2b = make_implementation(make_type<type_1>(), o1b.get()); |
| 149 | auto i3a = make_implementation(make_type<type_2>(), o2.get()); |
| 150 | auto i3b = make_implementation(make_type<type_2>(), o2.get()); |
| 151 | |
| 152 | test_compare<implementation>({{i1a, i1b}, {i2a, i2b}, {i3a, i3b}}); |
| 153 | } |
| 154 | |
| 155 | QTEST_APPLESS_MAIN(implementation_test) |
| 156 | #include "implementation-test.moc" |