Debian Codegraph @

test/unit/setter-method-test.cpp

raw symbols SCIP
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-setter.h>
25
26 #include "internal/types-by-name.h"
27
28 #include <QtTest/QtTest>
29
30 #ifndef Q_MOC_RUN
31 # define INVALID_SETTER_TAG
32 #endif
33
34 using namespace injeqt::internal;
35 using namespace injeqt::v1;
36
37 class injectable_type1 : public QObject
38 {
39 Q_OBJECT
40 };
41
42 class injectable_type2 : public QObject
43 {
44 Q_OBJECT
45 };
46
47 class test_type : public QObject
48 {
49 Q_OBJECT
50
51 public:
52 injectable_type1 *_1 = nullptr;
53
54 test_type() {}
55
56 Q_INVOKABLE test_type(injectable_type1 *) {}
57
58 Q_INVOKABLE INJEQT_SET void tagged_setter_method_1(injectable_type1 *a) { _1 = a; }
59 Q_INVOKABLE INJEQT_SETTER void tagged_setter_method_2(injectable_type1 *a) { _1 = a; }
60
61 public slots:
62 INJEQT_SET void tagged_setter_slot_1(injectable_type1 *a) { _1 = a; }
63 INJEQT_SETTER void tagged_setter_slot_2(injectable_type1 *a) { _1 = a; }
64 INJEQT_SETTER void invalid_setter_multi_arguments(injectable_type1 *, injectable_type2 *) { }
65 INVALID_SETTER_TAG void invalid_setter_invalid_tag(injectable_type1 *) { }
66 void invalid_setter_no_tag(injectable_type1 *) { }
67
68 signals:
69 INJEQT_SETTER void invalid_setter_signal(injectable_type1 *);
70
71 };
72
73 class setter_method_test : public QObject
74 {
75 Q_OBJECT
76
77 public:
78 setter_method_test();
79
80 private:
81 types_by_name _known_types;
82
83 private slots:
84 void should_create_empty();
85 void should_create_valid_from_tagged_setter_method();
86 void should_create_valid_from_tagged_setter_slot();
87 void should_invoke_have_results();
88 void should_throw_when_empty_method();
89 void should_throw_when_multiple_arguments();
90 void should_throw_when_invalid_tag();
91 void should_throw_when_no_tag();
92 void should_throw_when_signal();
93 void should_throw_when_constructor();
94
95 };
96
97 setter_method_test::setter_method_test() :
98 _known_types{
99 make_type<injectable_type1>(),
100 make_type<injectable_type2>(),
101 make_type<test_type>()
102 }
103 {
104 }
105
106 void setter_method_test::should_create_empty()
107 {
108 auto setter = setter_method{};
109 QVERIFY(setter.is_empty());
110 }
111
112 void setter_method_test::should_create_valid_from_tagged_setter_method()
113 {
114 auto setter = make_setter_method(_known_types, get_method<test_type>("tagged_setter_method_1(injectable_type1*)"));
115 QVERIFY(!setter.is_empty());
116 QCOMPARE(setter.object_type(), make_type<test_type>());
117 QCOMPARE(setter.parameter_type(), make_type<injectable_type1>());
118
119 setter = make_setter_method(_known_types, get_method<test_type>("tagged_setter_method_2(injectable_type1*)"));
120 QVERIFY(!setter.is_empty());
121 QCOMPARE(setter.object_type(), make_type<test_type>());
122 QCOMPARE(setter.parameter_type(), make_type<injectable_type1>());
123 }
124
125 void setter_method_test::should_create_valid_from_tagged_setter_slot()
126 {
127 auto setter = make_setter_method(_known_types, get_method<test_type>("tagged_setter_slot_1(injectable_type1*)"));
128 QVERIFY(!setter.is_empty());
129 QCOMPARE(setter.object_type(), make_type<test_type>());
130 QCOMPARE(setter.parameter_type(), make_type<injectable_type1>());
131
132 setter = make_setter_method(_known_types, get_method<test_type>("tagged_setter_slot_2(injectable_type1*)"));
133 QVERIFY(!setter.is_empty());
134 QCOMPARE(setter.object_type(), make_type<test_type>());
135 QCOMPARE(setter.parameter_type(), make_type<injectable_type1>());
136 }
137
138 void setter_method_test::should_invoke_have_results()
139 {
140 auto setter = make_setter_method(_known_types, get_method<test_type>("tagged_setter_slot_1(injectable_type1*)"));
141 auto on = make_object<test_type>();
142 auto with = make_object<injectable_type1>();
143 QCOMPARE(static_cast<QObject *>(nullptr), static_cast<test_type *>(on.get())->_1);
144
145 setter.invoke(on.get(), with.get());
146 QCOMPARE(with.get(), static_cast<test_type *>(on.get())->_1);
147 }
148
149 void setter_method_test::should_throw_when_empty_method()
150 {
151 expect<exception::invalid_setter>({"setter does not have enclosing meta object"}, [&]{
152 make_setter_method(_known_types, QMetaMethod{});
153 });
154 }
155
156 void setter_method_test::should_throw_when_multiple_arguments()
157 {
158 expect<exception::invalid_setter>({"invalid parameter count"}, [&]{
159 make_setter_method(_known_types, get_method<test_type>("invalid_setter_multi_arguments(injectable_type1*,injectable_type2*)"));
160 });
161 }
162
163 void setter_method_test::should_throw_when_invalid_tag()
164 {
165 expect<exception::invalid_setter>({"setter does not have valid tag"}, [&]{
166 make_setter_method(_known_types, get_method<test_type>("invalid_setter_invalid_tag(injectable_type1*)"));
167 });
168 }
169
170 void setter_method_test::should_throw_when_no_tag()
171 {
172 expect<exception::invalid_setter>({"setter does not have valid tag"}, [&]{
173 make_setter_method(_known_types, get_method<test_type>("invalid_setter_no_tag(injectable_type1*)"));
174 });
175 }
176
177 void setter_method_test::should_throw_when_signal()
178 {
179 expect<exception::invalid_setter>({"setter is signal"}, [&]{
180 make_setter_method(_known_types, get_method<test_type>("invalid_setter_signal(injectable_type1*)"));
181 });
182 }
183
184 void setter_method_test::should_throw_when_constructor()
185 {
186 expect<exception::invalid_setter>({"setter is constructor"}, [&]{
187 make_setter_method(_known_types, get_constructor<test_type>("test_type(injectable_type1*)"));
188 });
189 }
190
191 QTEST_APPLESS_MAIN(setter_method_test)
192 #include "setter-method-test.moc"