| 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/invalid-action.h> |
| 25 | |
| 26 | #include "internal/action-method.h" |
| 27 | |
| 28 | #include <QtTest/QtTest> |
| 29 | |
| 30 | #ifndef Q_MOC_RUN |
| 31 | # define INVALID_ACTION_TAG |
| 32 | #endif |
| 33 | |
| 34 | using namespace injeqt::internal; |
| 35 | using namespace injeqt::v1; |
| 36 | |
| 37 | class test_type : public QObject |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | |
| 41 | public: |
| 42 | int v = 0; |
| 43 | |
| 44 | test_type() {} |
| 45 | |
| 46 | Q_INVOKABLE test_type(int) {} |
| 47 | |
| 48 | Q_INVOKABLE INJEQT_INIT void tagged_init_action_method() { } |
| 49 | Q_INVOKABLE INJEQT_DONE void tagged_done_action_method() { } |
| 50 | |
| 51 | public slots: |
| 52 | INJEQT_INIT void tagged_init_action_slot() { v = 1; } |
| 53 | INJEQT_DONE void tagged_done_action_slot() { } |
| 54 | INJEQT_INIT void invalid_init_action_arguments(int) { } |
| 55 | INJEQT_DONE void invalid_done_action_arguments(int) { } |
| 56 | INVALID_ACTION_TAG void invalid_action_invalid_tag() { } |
| 57 | void invalid_action_no_tag() { } |
| 58 | |
| 59 | signals: |
| 60 | INJEQT_INIT void invalid_init_action_signal(); |
| 61 | INJEQT_DONE void invalid_done_action_signal(); |
| 62 | |
| 63 | }; |
| 64 | |
| 65 | class action_method_test : public QObject |
| 66 | { |
| 67 | Q_OBJECT |
| 68 | |
| 69 | private slots: |
| 70 | void should_create_empty(); |
| 71 | void should_create_valid_from_tagged_action_method(); |
| 72 | void should_create_valid_from_tagged_action_slot(); |
| 73 | void should_invoke_have_results(); |
| 74 | void should_throw_when_empty_method(); |
| 75 | void should_throw_when_arguments(); |
| 76 | void should_throw_when_invalid_tag(); |
| 77 | void should_throw_when_no_tag(); |
| 78 | void should_throw_when_signal(); |
| 79 | void should_throw_when_constructor(); |
| 80 | |
| 81 | }; |
| 82 | |
| 83 | void action_method_test::should_create_empty() |
| 84 | { |
| 85 | auto action = action_method{}; |
| 86 | QVERIFY(action.is_empty()); |
| 87 | } |
| 88 | |
| 89 | void action_method_test::should_create_valid_from_tagged_action_method() |
| 90 | { |
| 91 | auto action = make_action_method(get_method<test_type>("tagged_init_action_method()")); |
| 92 | QVERIFY(!action.is_empty()); |
| 93 | QCOMPARE(action.object_type(), make_type<test_type>()); |
| 94 | |
| 95 | ;action = make_action_method(get_method<test_type>("tagged_done_action_method()")); |
| 96 | QVERIFY(!action.is_empty()); |
| 97 | QCOMPARE(action.object_type(), make_type<test_type>()); |
| 98 | } |
| 99 | |
| 100 | void action_method_test::should_create_valid_from_tagged_action_slot() |
| 101 | { |
| 102 | auto action = make_action_method(get_method<test_type>("tagged_init_action_slot()")); |
| 103 | QVERIFY(!action.is_empty()); |
| 104 | QCOMPARE(action.object_type(), make_type<test_type>()); |
| 105 | |
| 106 | make_action_method(get_method<test_type>("tagged_done_action_slot()")); |
| 107 | QVERIFY(!action.is_empty()); |
| 108 | QCOMPARE(action.object_type(), make_type<test_type>()); |
| 109 | } |
| 110 | |
| 111 | void action_method_test::should_invoke_have_results() |
| 112 | { |
| 113 | auto action = make_action_method(get_method<test_type>("tagged_init_action_slot()")); |
| 114 | auto on = make_object<test_type>(); |
| 115 | QCOMPARE(0, static_cast<test_type *>(on.get())->v); |
| 116 | |
| 117 | action.invoke(on.get()); |
| 118 | QCOMPARE(1, static_cast<test_type *>(on.get())->v); |
| 119 | } |
| 120 | |
| 121 | void action_method_test::should_throw_when_empty_method() |
| 122 | { |
| 123 | expect<exception::invalid_action>({"action does not have enclosing meta object"}, [&]{ |
| 124 | make_action_method(QMetaMethod{}); |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | void action_method_test::should_throw_when_arguments() |
| 129 | { |
| 130 | expect<exception::invalid_action>({"invalid parameter count"}, [&]{ |
| 131 | make_action_method(get_method<test_type>("invalid_init_action_arguments(int)")); |
| 132 | }); |
| 133 | expect<exception::invalid_action>({"invalid parameter count"}, [&]{ |
| 134 | make_action_method(get_method<test_type>("invalid_done_action_arguments(int)")); |
| 135 | }); |
| 136 | } |
| 137 | |
| 138 | void action_method_test::should_throw_when_invalid_tag() |
| 139 | { |
| 140 | expect<exception::invalid_action>({"action does not have valid tag"}, [&]{ |
| 141 | make_action_method(get_method<test_type>("invalid_action_invalid_tag()")); |
| 142 | }); |
| 143 | } |
| 144 | |
| 145 | void action_method_test::should_throw_when_no_tag() |
| 146 | { |
| 147 | expect<exception::invalid_action>({"action does not have valid tag"}, [&]{ |
| 148 | make_action_method(get_method<test_type>("invalid_action_no_tag()")); |
| 149 | }); |
| 150 | } |
| 151 | |
| 152 | void action_method_test::should_throw_when_signal() |
| 153 | { |
| 154 | expect<exception::invalid_action>({"action is signal"}, [&]{ |
| 155 | make_action_method(get_method<test_type>("invalid_init_action_signal()")); |
| 156 | }); |
| 157 | expect<exception::invalid_action>({"action is signal"}, [&]{ |
| 158 | make_action_method(get_method<test_type>("invalid_done_action_signal()")); |
| 159 | }); |
| 160 | } |
| 161 | |
| 162 | void action_method_test::should_throw_when_constructor() |
| 163 | { |
| 164 | expect<exception::invalid_action>({"action is constructor"}, [&]{ |
| 165 | make_action_method(get_constructor<test_type>("test_type(int)")); |
| 166 | }); |
| 167 | } |
| 168 | |
| 169 | QTEST_APPLESS_MAIN(action_method_test) |
| 170 | #include "action-method-test.moc" |