| 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 | #pragma once |
| 22 | |
| 23 | #include <injeqt/exception/exception.h> |
| 24 | |
| 25 | #include "internal/setter-method.h" |
| 26 | |
| 27 | #include <QtCore/QMetaMethod> |
| 28 | #include <QtCore/QObject> |
| 29 | #include <QtTest/QTest> |
| 30 | #include <memory> |
| 31 | |
| 32 | namespace injeqt { namespace v1 { |
| 33 | |
| 34 | template<typename T> |
| 35 | inline QMetaMethod get_method(const std::string &signature) |
| 36 | { |
| 37 | return T::staticMetaObject.method(T::staticMetaObject.indexOfMethod(signature.data())); |
| 38 | } |
| 39 | |
| 40 | template<typename T> |
| 41 | inline QMetaMethod get_constructor(const std::string &signature) |
| 42 | { |
| 43 | return T::staticMetaObject.constructor(T::staticMetaObject.indexOfConstructor(signature.data())); |
| 44 | } |
| 45 | |
| 46 | template<typename T, typename P> |
| 47 | inline injeqt::internal::setter_method make_test_setter_method(const std::string &signature) |
| 48 | { |
| 49 | return injeqt::internal::setter_method{make_type<P>(), get_method<T>(signature)}; |
| 50 | } |
| 51 | |
| 52 | template<typename T> |
| 53 | std::unique_ptr<QObject> make_object() |
| 54 | { |
| 55 | return std::unique_ptr<QObject>(new T{}); |
| 56 | } |
| 57 | |
| 58 | template<typename T> |
| 59 | inline QMetaMethod make_method(const std::string &signature) |
| 60 | { |
| 61 | return T::staticMetaObject.method(T::staticMetaObject.indexOfMethod(signature.data())); |
| 62 | } |
| 63 | |
| 64 | template<typename T> |
| 65 | void test_compare(const std::vector<std::vector<T>> objects) |
| 66 | { |
| 67 | for (decltype(objects.size()) i = 0; i < objects.size(); i++) |
| 68 | for (decltype(objects.size()) j = 0; j < objects.size(); j++) |
| 69 | if (i != j) |
| 70 | { |
| 71 | for (auto &&x : objects[i]) |
| 72 | for (auto &&y : objects[j]) |
| 73 | { |
| 74 | QVERIFY(x != y); |
| 75 | QVERIFY(y != x); |
| 76 | } |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | for (auto &&x : objects[i]) |
| 81 | for (auto &&y : objects[j]) |
| 82 | { |
| 83 | QVERIFY(x == y); |
| 84 | QVERIFY(y == x); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | template<typename T> |
| 90 | void test_compare_ex(const std::vector<std::vector<T>> objects) |
| 91 | { |
| 92 | for (decltype(objects.size()) i = 0; i < objects.size(); i++) |
| 93 | for (decltype(objects.size()) j = 0; j < objects.size(); j++) |
| 94 | if (i != j) |
| 95 | { |
| 96 | for (auto &&x : objects[i]) |
| 97 | { |
| 98 | auto first = true; |
| 99 | auto c1 = false; |
| 100 | auto c2 = false; |
| 101 | auto c3 = false; |
| 102 | auto c4 = false; |
| 103 | auto c5 = false; |
| 104 | auto c6 = false; |
| 105 | auto c7 = false; |
| 106 | auto c8 = false; |
| 107 | |
| 108 | for (auto &&y : objects[j]) |
| 109 | { |
| 110 | QVERIFY(x != y); |
| 111 | QVERIFY(y != x); |
| 112 | QVERIFY((x < y) ^ (y < x)); |
| 113 | QVERIFY((x > y) ^ (y > x)); |
| 114 | QVERIFY((x <= y) ^ (y <= x)); |
| 115 | QVERIFY((x >= y) ^ (y >= x)); |
| 116 | if (first) |
| 117 | { |
| 118 | c1 = x < y; |
| 119 | c2 = y < x; |
| 120 | c3 = x <= y; |
| 121 | c4 = y <= x; |
| 122 | c5 = x > y; |
| 123 | c6 = y > x; |
| 124 | c7 = x >= y; |
| 125 | c8 = y >= x; |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | QCOMPARE(c1, x < y); |
| 130 | QCOMPARE(c2, y < x); |
| 131 | QCOMPARE(c3, x <= y); |
| 132 | QCOMPARE(c4, y <= y); |
| 133 | QCOMPARE(c5, x > y); |
| 134 | QCOMPARE(c6, y > x); |
| 135 | QCOMPARE(c7, x >= y); |
| 136 | QCOMPARE(c8, y >= x); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | for (auto &&x : objects[i]) |
| 144 | for (auto &&y : objects[j]) |
| 145 | { |
| 146 | QVERIFY(x == y); |
| 147 | QVERIFY(y == x); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | }} |