##// END OF EJS Templates
fixed test case...
florianlink -
r20:1203bfc88874
parent child
Show More
@@ -1,325 +1,324
1 1 /*
2 2 *
3 3 * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved.
4 4 *
5 5 * This library is free software; you can redistribute it and/or
6 6 * modify it under the terms of the GNU Lesser General Public
7 7 * License as published by the Free Software Foundation; either
8 8 * version 2.1 of the License, or (at your option) any later version.
9 9 *
10 10 * This library is distributed in the hope that it will be useful,
11 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 13 * Lesser General Public License for more details.
14 14 *
15 15 * Further, this software is distributed without any warranty that it is
16 16 * free of the rightful claim of any third person regarding infringement
17 17 * or the like. Any license provided herein, whether implied or
18 18 * otherwise, applies only to this software file. Patent licenses, if
19 19 * any, provided herein do not apply to combinations of this program with
20 20 * other software, or any other product whatsoever.
21 21 *
22 22 * You should have received a copy of the GNU Lesser General Public
23 23 * License along with this library; if not, write to the Free Software
24 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 25 *
26 26 * Contact information: MeVis Research GmbH, Universitaetsallee 29,
27 27 * 28359 Bremen, Germany or:
28 28 *
29 29 * http://www.mevis.de
30 30 *
31 31 */
32 32
33 33 //----------------------------------------------------------------------------------
34 34 /*!
35 35 // \file PythonQtTests.cpp
36 36 // \author Florian Link
37 37 // \author Last changed by $Author: florian $
38 38 // \date 2006-05
39 39 */
40 40 //----------------------------------------------------------------------------------
41 41
42 42 #include "PythonQtTests.h"
43 43
44 44 void PythonQtTestSlotCalling::initTestCase()
45 45 {
46 46 _helper = new PythonQtTestSlotCallingHelper(this);
47 47 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
48 48 PythonQt::self()->addObject(main, "obj", _helper);
49 49 }
50 50
51 51 void PythonQtTestSlotCalling::init() {
52 52
53 53 }
54 54
55 55 void PythonQtTestSlotCalling::testNoArgSlotCall()
56 56 {
57 57 QVERIFY(_helper->runScript("obj.testNoArg(); obj.setPassed();\n"));
58 58 }
59 59
60 60 void PythonQtTestSlotCalling::testOverloadedCall()
61 61 {
62 62 QVERIFY(_helper->runScript("obj.overload(False); obj.setPassed();\n", 0));
63 63 QVERIFY(_helper->runScript("obj.overload(True); obj.setPassed();\n", 0));
64 64 QVERIFY(_helper->runScript("obj.overload(12.5); obj.setPassed();\n", 1));
65 65 QVERIFY(_helper->runScript("obj.overload(12); obj.setPassed();\n", 2));
66 66 QVERIFY(_helper->runScript("obj.overload('test'); obj.setPassed();\n", 3));
67 67 QVERIFY(_helper->runScript("obj.overload(u'test'); obj.setPassed();\n", 3));
68 68 QVERIFY(_helper->runScript("obj.overload(('test','test2')); obj.setPassed();\n", 4));
69 69 QVERIFY(_helper->runScript("obj.overload(obj); obj.setPassed();\n", 5));
70 70 QVERIFY(_helper->runScript("obj.overload(12,13); obj.setPassed();\n", 6));
71 71 }
72 72
73 73 void PythonQtTestSlotCalling::testPODSlotCalls()
74 74 {
75 75 QVERIFY(_helper->runScript("if obj.getBool(False)==False: obj.setPassed();\n"));
76 76 QVERIFY(_helper->runScript("if obj.getBool(True)==True: obj.setPassed();\n"));
77 77 QVERIFY(_helper->runScript("if obj.getInt(-42)==-42: obj.setPassed();\n"));
78 78 QVERIFY(_helper->runScript("if obj.getUInt(42)==42: obj.setPassed();\n"));
79 79 QVERIFY(_helper->runScript("if obj.getShort(-43)==-43: obj.setPassed();\n"));
80 80 QVERIFY(_helper->runScript("if obj.getUShort(43)==43: obj.setPassed();\n"));
81 81 QVERIFY(_helper->runScript("if obj.getChar(-12)==-12: obj.setPassed();\n"));
82 82 QVERIFY(_helper->runScript("if obj.getUChar(12)==12: obj.setPassed();\n"));
83 83 QVERIFY(_helper->runScript("if obj.getLong(-256*256*256)==-256*256*256: obj.setPassed();\n"));
84 84 QVERIFY(_helper->runScript("if obj.getULong(256*256*256)==256*256*256: obj.setPassed();\n"));
85 85 QVERIFY(_helper->runScript("if obj.getLongLong(-42)==-42: obj.setPassed();\n"));
86 86 QVERIFY(_helper->runScript("if obj.getULongLong(42)==42: obj.setPassed();\n"));
87 87 QVERIFY(_helper->runScript("if obj.getQChar(4096)==4096: obj.setPassed();\n"));
88 88 QVERIFY(_helper->runScript("if obj.getDouble(47.12)==47.12: obj.setPassed();\n"));
89 89 QVERIFY(_helper->runScript("if abs(obj.getFloat(47.11)-47.11)<0.01: obj.setPassed();\n"));
90 90 QVERIFY(_helper->runScript("if obj.getQString('testStr')=='testStr': obj.setPassed();\n"));
91 91 QVERIFY(_helper->runScript("if obj.getQString('')=='': obj.setPassed();\n"));
92 92 QVERIFY(_helper->runScript("if obj.getQStringList(('test','test2'))==('test','test2'): obj.setPassed();\n"));
93 93 }
94 94
95 95 void PythonQtTestSlotCalling::testQVariantSlotCalls()
96 96 {
97 97 QVERIFY(_helper->runScript("if obj.getQVariant(-42)==-42: obj.setPassed();\n"));
98 98 QVERIFY(_helper->runScript("if obj.getQVariant('testStr')=='testStr': obj.setPassed();\n"));
99 99 QVERIFY(_helper->runScript("if obj.getQVariant(('test','test2'))==('test','test2'): obj.setPassed();\n"));
100 100 QVERIFY(_helper->runScript("if obj.getQVariant(('test',12, 47.11))==('test',12, 47.11): obj.setPassed();\n"));
101 101 QVERIFY(_helper->runScript("if obj.getQVariant({'test':'bla','test2':47.11})=={'test':'bla','test2':47.11}: obj.setPassed();\n"));
102 102 QEXPECT_FAIL("", "Testing to pass a map and compare with a different map", Continue);
103 103 QVERIFY(_helper->runScript("if obj.getQVariant({'test':'bla2','test2':47.11})=={'test':'bla','test2':47.11}: obj.setPassed();\n"));
104 104 QVERIFY(_helper->runScript("if obj.getQVariant(obj)==obj: obj.setPassed();\n"));
105 105 }
106 106
107 107 void PythonQtTestSlotCalling::testObjectSlotCalls()
108 108 {
109 109 QVERIFY(_helper->runScript("if obj.getQObject(obj)==obj: obj.setPassed();\n"));
110 110 QVERIFY(_helper->runScript("if obj.getTestObject(obj)==obj: obj.setPassed();\n"));
111 111 QVERIFY(_helper->runScript("if obj.getNewObject().className()=='PythonQtTestSlotCallingHelper': obj.setPassed();\n"));
112 112 QEXPECT_FAIL("", "Testing to pass a QObject when another object was expected", Continue);
113 113 QVERIFY(_helper->runScript("if obj.getQWidget(obj)==obj: obj.setPassed();\n"));
114 114 }
115 115
116 116 void PythonQtTestSlotCalling::testCppFactory()
117 117 {
118 118 PythonQtTestCppFactory* f = new PythonQtTestCppFactory;
119 119 PythonQt::self()->addInstanceDecorators(new PQCppObjectDecorator);
120 120 PythonQt::self()->addInstanceDecorators(new PQCppObjectNoWrapDecorator);
121 121
122 122 PythonQt::self()->addWrapperFactory(f);
123 123 QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getHeight()==12: obj.setPassed();\n"));
124 124 QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getH()==12: obj.setPassed();\n"));
125 125 QVERIFY(_helper->runScript("pq1 = obj.createPQCppObject(12);\n"
126 126 "pq2 = obj.createPQCppObject(13);\n"
127 127 "pq3 = obj.getPQCppObject(pq1);\n"
128 128 "pq4 = obj.getPQCppObject(pq2);\n"
129 129 "if pq3.getHeight()==12 and pq4.getHeight()==13: obj.setPassed();\n"
130 130 ));
131 131
132 132 QVERIFY(_helper->runScript("if obj.createPQCppObjectNoWrap(12).getH()==12: obj.setPassed();\n"));
133 133 }
134 134
135 135
136 136 void PythonQtTestSlotCalling::testMultiArgsSlotCall()
137 137 {
138 138 QVERIFY(_helper->runScript("if obj.getMultiArgs(12,47.11,'test')==(12,47.11,'test'): obj.setPassed();\n"));
139 139 }
140 140
141 141 bool PythonQtTestSlotCallingHelper::runScript(const char* script, int expectedOverload)
142 142 {
143 143 _called = false;
144 144 _passed = false;
145 145 _calledOverload = -1;
146 146 PyRun_SimpleString(script);
147 147 return _called && _passed && _calledOverload==expectedOverload;
148 148 }
149 149
150 150
151 151 void PythonQtTestSignalHandler::initTestCase()
152 152 {
153 153 _helper = new PythonQtTestSignalHandlerHelper(this);
154 154 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
155 155 PythonQt::self()->addObject(main, "obj", _helper);
156 156 }
157 157
158 158 void PythonQtTestSignalHandler::testSignalHandler()
159 159 {
160 160 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
161 161 PyRun_SimpleString("def testIntSignal(a):\n if a==12: obj.setPassed();\n");
162 162 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(intSignal(int)), main, "testIntSignal"));
163 163 QVERIFY(_helper->emitIntSignal(12));
164 164
165 165 PyRun_SimpleString("def testFloatSignal(a):\n if a==12: obj.setPassed();\n");
166 166 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(floatSignal(float)), main, "testFloatSignal"));
167 167 QVERIFY(_helper->emitFloatSignal(12));
168 168
169 169 PyRun_SimpleString("def testVariantSignal(a):\n if a==obj.expectedVariant(): obj.setPassed();\n");
170 170 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(variantSignal(QVariant)), main, "testVariantSignal"));
171 171 _helper->setExpectedVariant(QString("Test"));
172 172 QVERIFY(_helper->emitVariantSignal(QString("Test")));
173 173 _helper->setExpectedVariant(12);
174 174 QVERIFY(_helper->emitVariantSignal(12));
175 175 _helper->setExpectedVariant(QStringList() << "test1" << "test2");
176 176 QVERIFY(_helper->emitVariantSignal(QStringList() << "test1" << "test2"));
177 177 _helper->setExpectedVariant(qVariantFromValue((QObject*)_helper));
178 178 QVERIFY(_helper->emitVariantSignal(qVariantFromValue((QObject*)_helper)));
179 179
180 180 PyRun_SimpleString("def testComplexSignal(a,b,l,o):\n if a==12 and b==13 and l==('test1','test2') and o == obj: obj.setPassed();\n");
181 181 // intentionally not normalized signal:
182 182 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(complexSignal( int, float , const QStringList , QObject*)), main, "testComplexSignal"));
183 183 QVERIFY(_helper->emitComplexSignal(12,13,QStringList() << "test1" << "test2", _helper));
184 184
185 185 // try removing the handler
186 186 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(complexSignal( int, float , const QStringList , QObject*)), main, "testComplexSignal"));
187 187 // and emit the signal, which should fail because the handler was removed
188 188 QVERIFY(!_helper->emitComplexSignal(12,13,QStringList() << "test1" << "test2", _helper));
189 189
190 190 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(intSignal(int)), main, "testIntSignal"));
191 191 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(floatSignal(float)), main, "testFloatSignal"));
192 192 QVERIFY(PythonQt::self()->removeSignalHandler(_helper, SIGNAL(variantSignal(QVariant)), main, "testVariantSignal"));
193 193
194 194 }
195 195
196 196 void PythonQtTestSignalHandler::testRecursiveSignalHandler()
197 197 {
198 198 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
199 199 PyRun_SimpleString("def testSignal1(a):\n obj.emitSignal2(a);\n");
200 200 PyRun_SimpleString("def testSignal2(a):\n obj.emitSignal3(float(a));\n");
201 201 PyRun_SimpleString("def testSignal3(a):\n if a==12: obj.setPassed();\n");
202 202 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal1(int)), main, "testSignal1"));
203 203 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal2(const QString&)), main, "testSignal2"));
204 204 QVERIFY(PythonQt::self()->addSignalHandler(_helper, SIGNAL(signal3(float)), main, "testSignal3"));
205 205 QVERIFY(_helper->emitSignal1(12));
206 206 }
207 207
208 208
209 209 void PythonQtTestApi::initTestCase()
210 210 {
211 211 _helper = new PythonQtTestApiHelper();
212 212 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
213 213 PythonQt::self()->addObject(main, "obj", _helper);
214 214 }
215 215
216 216 bool PythonQtTestApiHelper::call(const QString& function, const QVariantList& args, const QVariant& expectedResult) {
217 217 _passed = false;
218 218 QVariant r = PythonQt::self()->call(PythonQt::self()->getMainModule(), function, args);
219 219 return _passed && expectedResult==r;
220 220 }
221 221
222 222 void PythonQtTestApi::testCall()
223 223 {
224 224 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
225 225
226 226 QVERIFY(qVariantValue<QObject*>(PythonQt::self()->getVariable(main, "obj"))==_helper);
227 227
228 228 PyRun_SimpleString("def testCallNoArgs():\n obj.setPassed();\n");
229 229 QVERIFY(_helper->call("testCallNoArgs", QVariantList(), QVariant()));
230 230
231 231 PyRun_SimpleString("def testCall1(a):\n if a=='test': obj.setPassed();\n return 'test2';\n");
232 232 QVERIFY(_helper->call("testCall1", QVariantList() << QVariant("test"), QVariant(QString("test2"))));
233 233
234 234 PyRun_SimpleString("def testCall2(a, b):\n if a=='test' and b==obj: obj.setPassed();\n return obj;\n");
235 235 QVariant r = PythonQt::self()->call(PythonQt::self()->getMainModule(), "testCall2", QVariantList() << QVariant("test") << qVariantFromValue((QObject*)_helper));
236 236 QObject* p = qVariantValue<QObject*>(r);
237 237 QVERIFY(p==_helper);
238 238 }
239 239
240 240 void PythonQtTestApi::testVariables()
241 241 {
242 242 PythonQt::self()->addObject(PythonQt::self()->getMainModule(), "someObject", _helper);
243 243 QVariant v = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject");
244 244 QObject* p = qVariantValue<QObject*>(v);
245 245 QVERIFY(p==_helper);
246 246 // test for unset variable
247 247 QVariant v2 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject2");
248 248 QVERIFY(v2==QVariant());
249 249
250 250 PythonQt::self()->addVariable(PythonQt::self()->getMainModule(), "someValue", QStringList() << "test1" << "test2");
251 251 QVariant v3 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someValue");
252 252 QVERIFY(v3 == QVariant(QStringList() << "test1" << "test2"));
253 253
254 254 QStringList l = PythonQt::self()->introspection(PythonQt::self()->getMainModule(), QString::null, PythonQt::Variable);
255 255 QSet<QString> s;
256 256 // check that at least these three variables are set
257 257 s << "obj" << "someObject" << "someValue";
258 QString a;
259 foreach (a, l) {
260 QVERIFY(s.contains(a));
258 foreach (QString value, s) {
259 QVERIFY(l.indexOf(value)!=-1);
261 260 }
262 261
263 262 // insert a second time!
264 263 PythonQt::self()->addObject(PythonQt::self()->getMainModule(), "someObject", _helper);
265 264 // and remove
266 265 PythonQt::self()->removeVariable(PythonQt::self()->getMainModule(), "someObject");
267 266 // we expect to find no variable
268 267 QVariant v4 = PythonQt::self()->getVariable(PythonQt::self()->getMainModule(), "someObject");
269 268 QVERIFY(v4==QVariant());
270 269 }
271 270
272 271 void PythonQtTestApi::testImporter()
273 272 {
274 273 PythonQt::self()->setImporter(_helper);
275 274 PythonQt::self()->overwriteSysPath(QStringList() << "c:\\test");
276 275 PyRun_SimpleString("import bla\n");
277 276 }
278 277
279 278 QByteArray PythonQtTestApiHelper::readFileAsBytes(const QString& filename)
280 279 {
281 280 QByteArray b;
282 281 return b;
283 282 }
284 283
285 284 QByteArray PythonQtTestApiHelper::readSourceFile(const QString& filename, bool& ok)
286 285 {
287 286 QByteArray b;
288 287 ok = true;
289 288 return b;
290 289 }
291 290
292 291 bool PythonQtTestApiHelper::exists(const QString& filename)
293 292 {
294 293 return true;
295 294 }
296 295
297 296 QDateTime PythonQtTestApiHelper::lastModifiedDate(const QString& filename) {
298 297 return QDateTime::currentDateTime();
299 298 }
300 299
301 300
302 301 void PythonQtTestApi::testRedirect()
303 302 {
304 303 connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)), _helper, SLOT(stdOut(const QString&)));
305 304 connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)), _helper, SLOT(stdErr(const QString&)));
306 305 PyRun_SimpleString("print 'test'\n");
307 306 }
308 307
309 308 void PythonQtTestApiHelper::stdOut(const QString& s)
310 309 {
311 310 qDebug() << s;
312 311 }
313 312
314 313 void PythonQtTestApiHelper::stdErr(const QString& s)
315 314 {
316 315 qDebug() << s;
317 316 }
318 317
319 318 QObject* PythonQtTestCppFactory::create(const QByteArray& name, void *ptr)
320 319 {
321 320 if (name == "PQCppObject") {
322 321 return new PQCppObjectWrapper(ptr);
323 322 }
324 323 return NULL;
325 324 }
General Comments 0
You need to be logged in to leave comments. Login now