| 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 <QtTest/QtTest> |
| 22 | |
| 23 | #include <injeqt/type.h> |
| 24 | |
| 25 | #include "internal/type-relations.h" |
| 26 | |
| 27 | using namespace injeqt::internal; |
| 28 | using namespace injeqt::v1; |
| 29 | |
| 30 | class type_1 : public QObject |
| 31 | { |
| 32 | Q_OBJECT |
| 33 | }; |
| 34 | |
| 35 | class type_1_sub_1 : public type_1 |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | }; |
| 39 | |
| 40 | class type_1_sub_1_sub_1 : public type_1_sub_1 |
| 41 | { |
| 42 | Q_OBJECT |
| 43 | }; |
| 44 | |
| 45 | class type_1_sub_1_sub_2 : public type_1_sub_1 |
| 46 | { |
| 47 | Q_OBJECT |
| 48 | }; |
| 49 | |
| 50 | class type_1_sub_2 : public type_1 |
| 51 | { |
| 52 | Q_OBJECT |
| 53 | }; |
| 54 | |
| 55 | class type_2 : public QObject |
| 56 | { |
| 57 | Q_OBJECT |
| 58 | }; |
| 59 | |
| 60 | class type_2_sub_1 : public type_2 |
| 61 | { |
| 62 | Q_OBJECT |
| 63 | }; |
| 64 | |
| 65 | class type_relations_test : public QObject |
| 66 | { |
| 67 | Q_OBJECT |
| 68 | |
| 69 | public: |
| 70 | type_relations_test(); |
| 71 | |
| 72 | private slots: |
| 73 | void should_create_empty_relations(); |
| 74 | void should_create_empty_relations_for_no_types(); |
| 75 | void should_create_unique_relations_for_unrelated_types(); |
| 76 | void should_create_unique_relations_for_unrelated_subtypes(); |
| 77 | void should_create_unique_relations_for_type_with_supertypes(); |
| 78 | void should_create_ambiguous_relations_for_the_same_type(); |
| 79 | void should_create_mixed_relations_for_subtypes(); |
| 80 | void should_create_mixed_relations_for_type_and_subtype(); |
| 81 | void should_create_mixed_relations_for_subtype_and_type(); |
| 82 | |
| 83 | private: |
| 84 | type type_1_type; |
| 85 | type type_1_sub_1_type; |
| 86 | type type_1_sub_1_sub_1_type; |
| 87 | type type_1_sub_1_sub_2_type; |
| 88 | type type_1_sub_2_type; |
| 89 | type type_2_type; |
| 90 | type type_2_sub_1_type; |
| 91 | |
| 92 | }; |
| 93 | |
| 94 | type_relations_test::type_relations_test() : |
| 95 | type_1_type{make_type<type_1>()}, |
| 96 | type_1_sub_1_type{make_type<type_1_sub_1>()}, |
| 97 | type_1_sub_1_sub_1_type{make_type<type_1_sub_1_sub_1>()}, |
| 98 | type_1_sub_1_sub_2_type{make_type<type_1_sub_1_sub_2>()}, |
| 99 | type_1_sub_2_type{make_type<type_1_sub_2>()}, |
| 100 | type_2_type{make_type<type_2>()}, |
| 101 | type_2_sub_1_type{make_type<type_2_sub_1>()} |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | void type_relations_test::should_create_empty_relations() |
| 106 | { |
| 107 | auto result = type_relations{}; |
| 108 | |
| 109 | QCOMPARE(result.unique(), implemented_by_mapping{}); |
| 110 | QCOMPARE(result.ambiguous(), types{}); |
| 111 | } |
| 112 | |
| 113 | void type_relations_test::should_create_empty_relations_for_no_types() |
| 114 | { |
| 115 | auto result = make_type_relations({}); |
| 116 | |
| 117 | QCOMPARE(result.unique(), implemented_by_mapping{}); |
| 118 | QCOMPARE(result.ambiguous(), types{}); |
| 119 | } |
| 120 | |
| 121 | void type_relations_test::should_create_unique_relations_for_unrelated_types() |
| 122 | { |
| 123 | auto result = make_type_relations({type_1_type, type_2_type}); |
| 124 | |
| 125 | QCOMPARE(result.unique(), (implemented_by_mapping |
| 126 | { |
| 127 | implemented_by{type_1_type, type_1_type}, |
| 128 | implemented_by{type_2_type, type_2_type} |
| 129 | })); |
| 130 | QCOMPARE(result.ambiguous(), types{}); |
| 131 | } |
| 132 | |
| 133 | void type_relations_test::should_create_unique_relations_for_unrelated_subtypes() |
| 134 | { |
| 135 | auto result = make_type_relations({type_1_sub_1_sub_1_type, type_2_sub_1_type}); |
| 136 | |
| 137 | QCOMPARE(result.unique(), (implemented_by_mapping |
| 138 | { |
| 139 | implemented_by{type_1_type, type_1_sub_1_sub_1_type}, |
| 140 | implemented_by{type_1_sub_1_type, type_1_sub_1_sub_1_type}, |
| 141 | implemented_by{type_1_sub_1_sub_1_type, type_1_sub_1_sub_1_type}, |
| 142 | implemented_by{type_2_type, type_2_sub_1_type}, |
| 143 | implemented_by{type_2_sub_1_type, type_2_sub_1_type} |
| 144 | })); |
| 145 | QCOMPARE(result.ambiguous(), types{}); |
| 146 | } |
| 147 | |
| 148 | void type_relations_test::should_create_unique_relations_for_type_with_supertypes() |
| 149 | { |
| 150 | auto result = make_type_relations({type_1_sub_1_sub_1_type}); |
| 151 | |
| 152 | QCOMPARE(result.unique(), (implemented_by_mapping |
| 153 | { |
| 154 | implemented_by{type_1_type, type_1_sub_1_sub_1_type}, |
| 155 | implemented_by{type_1_sub_1_type, type_1_sub_1_sub_1_type}, |
| 156 | implemented_by{type_1_sub_1_sub_1_type, type_1_sub_1_sub_1_type} |
| 157 | })); |
| 158 | QCOMPARE(result.ambiguous(), types{}); |
| 159 | } |
| 160 | |
| 161 | void type_relations_test::should_create_ambiguous_relations_for_the_same_type() |
| 162 | { |
| 163 | auto result = make_type_relations({type_1_type, type_1_type}); |
| 164 | |
| 165 | QCOMPARE(result.unique(), implemented_by_mapping{}); |
| 166 | QCOMPARE(result.ambiguous(), types{type_1_type}); |
| 167 | } |
| 168 | |
| 169 | void type_relations_test::should_create_mixed_relations_for_subtypes() |
| 170 | { |
| 171 | auto result = make_type_relations({type_1_sub_1_sub_1_type, type_1_sub_1_sub_2_type}); |
| 172 | |
| 173 | QCOMPARE(result.unique(), (implemented_by_mapping |
| 174 | { |
| 175 | implemented_by{type_1_sub_1_sub_1_type, type_1_sub_1_sub_1_type}, |
| 176 | implemented_by{type_1_sub_1_sub_2_type, type_1_sub_1_sub_2_type} |
| 177 | })); |
| 178 | QCOMPARE(result.ambiguous(), (types{type_1_type, type_1_sub_1_type})); |
| 179 | } |
| 180 | |
| 181 | void type_relations_test::should_create_mixed_relations_for_type_and_subtype() |
| 182 | { |
| 183 | auto result = make_type_relations({type_1_type, type_1_sub_1_sub_1_type}); |
| 184 | |
| 185 | QCOMPARE(result.unique(), (implemented_by_mapping |
| 186 | { |
| 187 | implemented_by{type_1_sub_1_type, type_1_sub_1_sub_1_type}, |
| 188 | implemented_by{type_1_sub_1_sub_1_type, type_1_sub_1_sub_1_type} |
| 189 | })); |
| 190 | QCOMPARE(result.ambiguous(), types{type_1_type}); |
| 191 | } |
| 192 | |
| 193 | void type_relations_test::should_create_mixed_relations_for_subtype_and_type() |
| 194 | { |
| 195 | auto result = make_type_relations({type_1_sub_1_sub_1_type, type_1_type}); |
| 196 | |
| 197 | QCOMPARE(result.unique(), (implemented_by_mapping |
| 198 | { |
| 199 | implemented_by{type_1_sub_1_type, type_1_sub_1_sub_1_type}, |
| 200 | implemented_by{type_1_sub_1_sub_1_type, type_1_sub_1_sub_1_type} |
| 201 | })); |
| 202 | QCOMPARE(result.ambiguous(), types{type_1_type}); |
| 203 | } |
| 204 | |
| 205 | QTEST_APPLESS_MAIN(type_relations_test); |
| 206 | |
| 207 | #include "type-relations-test.moc" |