| 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 "internal/default-constructor-method.h" |
| 25 | |
| 26 | #include <QtTest/QtTest> |
| 27 | #include <string> |
| 28 | |
| 29 | using namespace injeqt::internal; |
| 30 | using namespace injeqt::v1; |
| 31 | |
| 32 | class no_default_constructor : public QObject |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | |
| 36 | public: |
| 37 | Q_INVOKABLE no_default_constructor(int a) { (void)a; } |
| 38 | Q_INVOKABLE no_default_constructor(std::string a) { (void)a; } |
| 39 | |
| 40 | }; |
| 41 | |
| 42 | class default_not_invokable_constructor : public QObject |
| 43 | { |
| 44 | Q_OBJECT |
| 45 | |
| 46 | public: |
| 47 | default_not_invokable_constructor() {} |
| 48 | |
| 49 | }; |
| 50 | |
| 51 | class default_invokable_constructor : public QObject |
| 52 | { |
| 53 | Q_OBJECT |
| 54 | |
| 55 | public: |
| 56 | Q_INVOKABLE default_invokable_constructor(QObject *parent = nullptr) : QObject{parent} {} |
| 57 | |
| 58 | }; |
| 59 | |
| 60 | class default_invokable_constructor_no_parameters : public QObject |
| 61 | { |
| 62 | Q_OBJECT |
| 63 | |
| 64 | public: |
| 65 | Q_INVOKABLE default_invokable_constructor_no_parameters() {} |
| 66 | |
| 67 | }; |
| 68 | |
| 69 | class default_constructor_method_test : public QObject |
| 70 | { |
| 71 | Q_OBJECT |
| 72 | |
| 73 | private slots: |
| 74 | void should_return_empty_when_default_constructed(); |
| 75 | void should_return_empty_when_created_with_not_default_constructor(); |
| 76 | void should_return_empty_when_created_with_not_invokable_constructor(); |
| 77 | void should_create_valid_with_invokable_constructor(); |
| 78 | void should_create_object_with_default_constructor(); |
| 79 | void should_properly_compare(); |
| 80 | |
| 81 | }; |
| 82 | |
| 83 | void default_constructor_method_test::should_return_empty_when_default_constructed() |
| 84 | { |
| 85 | auto c = default_constructor_method{}; |
| 86 | QVERIFY(c.is_empty()); |
| 87 | } |
| 88 | |
| 89 | void default_constructor_method_test::should_return_empty_when_created_with_not_default_constructor() |
| 90 | { |
| 91 | auto c = make_default_constructor_method(make_type<no_default_constructor>()); |
| 92 | QVERIFY(c.is_empty()); |
| 93 | } |
| 94 | |
| 95 | void default_constructor_method_test::should_return_empty_when_created_with_not_invokable_constructor() |
| 96 | { |
| 97 | auto c = make_default_constructor_method(make_type<default_not_invokable_constructor>()); |
| 98 | QVERIFY(c.is_empty()); |
| 99 | } |
| 100 | |
| 101 | void default_constructor_method_test::should_create_valid_with_invokable_constructor() |
| 102 | { |
| 103 | auto c = make_default_constructor_method(make_type<default_invokable_constructor>()); |
| 104 | QVERIFY(!c.is_empty()); |
| 105 | QCOMPARE(c.object_type(), make_type<default_invokable_constructor>()); |
| 106 | } |
| 107 | |
| 108 | void default_constructor_method_test::should_create_object_with_default_constructor() |
| 109 | { |
| 110 | auto c = make_default_constructor_method(make_type<default_invokable_constructor>()); |
| 111 | QVERIFY(!c.is_empty()); |
| 112 | |
| 113 | auto object = c.invoke(); |
| 114 | auto cast = qobject_cast<default_invokable_constructor *>(object.get()); |
| 115 | QVERIFY(cast != nullptr); |
| 116 | } |
| 117 | |
| 118 | void default_constructor_method_test::should_properly_compare() |
| 119 | { |
| 120 | auto c_empty = default_constructor_method{}; |
| 121 | auto c1a = make_default_constructor_method(make_type<default_invokable_constructor>()); |
| 122 | auto c1b = make_default_constructor_method(make_type<default_invokable_constructor>()); |
| 123 | auto c2a = make_default_constructor_method(make_type<default_invokable_constructor_no_parameters>()); |
| 124 | auto c2b = make_default_constructor_method(make_type<default_invokable_constructor_no_parameters>()); |
| 125 | |
| 126 | test_compare<default_constructor_method>({{c_empty}, {c1a, c1b}, {c2a, c2b}}); |
| 127 | } |
| 128 | |
| 129 | QTEST_APPLESS_MAIN(default_constructor_method_test) |
| 130 | #include "default-constructor-method-test.moc" |