##// END OF EJS Templates
added new tests for new PythonQt features...
florianlink -
r37:5bd99c632a3f
parent child
Show More
@@ -48,14 +48,21 int main( int argc, char **argv )
48
48
49 PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
49 PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
50
50
51 int failCount = 0;
51 PythonQtTestApi api;
52 PythonQtTestApi api;
52 QTest::qExec(&api, argc, argv);
53 failCount += QTest::qExec(&api, argc, argv);
53 PythonQtTestSignalHandler signalHandler;
54 PythonQtTestSignalHandler signalHandler;
54 QTest::qExec(&signalHandler, argc, argv);
55 failCount += QTest::qExec(&signalHandler, argc, argv);
55 PythonQtTestSlotCalling slotCalling;
56 PythonQtTestSlotCalling slotCalling;
56 QTest::qExec(&slotCalling, argc, argv);
57 failCount += QTest::qExec(&slotCalling, argc, argv);
57
58
58 PythonQt::cleanup();
59 PythonQt::cleanup();
59 return 0;
60
61 if (failCount>0) {
62 std::cerr << "Tests failed: " << failCount << std::endl;
63 } else {
64 std::cout << "All tests passed successfully." << std::endl;
65 }
66 return failCount;
60 }
67 }
61
68
@@ -45,6 +45,7 void PythonQtTestSlotCalling::initTestCase()
45 {
45 {
46 _helper = new PythonQtTestSlotCallingHelper(this);
46 _helper = new PythonQtTestSlotCallingHelper(this);
47 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
47 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
48 main.evalScript("import PythonQt");
48 PythonQt::self()->addObject(main, "obj", _helper);
49 PythonQt::self()->addObject(main, "obj", _helper);
49 }
50 }
50
51
@@ -52,6 +53,78 void PythonQtTestSlotCalling::init() {
52
53
53 }
54 }
54
55
56 void* polymorphic_ClassB_Handler(const void* ptr, char** className) {
57 ClassB* o = (ClassB*)ptr;
58 if (o->type()==2) {
59 *className = "ClassB";
60 return (ClassB*)o;
61 }
62 if (o->type()==3) {
63 *className = "ClassC";
64 return (ClassC*)o;
65 }
66 if (o->type()==4) {
67 *className = "ClassD";
68 return (ClassD*)o;
69 }
70 return NULL;
71 }
72
73 void PythonQtTestSlotCalling::testInheritance() {
74 PythonQt::self()->registerCPPClass("ClassA",NULL,NULL, PythonQtCreateObject<ClassAWrapper>);
75 PythonQt::self()->registerCPPClass("ClassB",NULL,NULL, PythonQtCreateObject<ClassBWrapper>);
76 PythonQt::self()->registerCPPClass("ClassC",NULL,NULL, PythonQtCreateObject<ClassCWrapper>);
77 PythonQt::self()->addParentClass("ClassC", "ClassA", PythonQtUpcastingOffset<ClassC,ClassA>());
78 PythonQt::self()->addParentClass("ClassC", "ClassB", PythonQtUpcastingOffset<ClassC,ClassB>());
79 PythonQt::self()->registerClass(&ClassD::staticMetaObject, NULL, PythonQtCreateObject<ClassDWrapper>);
80 PythonQt::self()->addParentClass("ClassD", "ClassA", PythonQtUpcastingOffset<ClassD,ClassA>());
81 PythonQt::self()->addParentClass("ClassD", "ClassB", PythonQtUpcastingOffset<ClassD,ClassB>());
82
83 PythonQtObjectPtr classA = PythonQt::self()->getMainModule().getVariable("PythonQt.ClassA");
84 PythonQtObjectPtr classB = PythonQt::self()->getMainModule().getVariable("PythonQt.ClassB");
85 PythonQtObjectPtr classC = PythonQt::self()->getMainModule().getVariable("PythonQt.ClassC");
86 PythonQtObjectPtr classD = PythonQt::self()->getMainModule().getVariable("PythonQt.ClassD");
87 QVERIFY(classA);
88 QVERIFY(classB);
89 QVERIFY(classC);
90 QVERIFY(classD);
91
92 QVERIFY(_helper->runScript("a = PythonQt.ClassA();\nif obj.getClassAPtr(a).getX()==1: obj.setPassed();\n"));
93 QEXPECT_FAIL("", "ClassB can not be converted to ClassA", Continue);
94 QVERIFY(_helper->runScript("a = PythonQt.ClassB();\nif obj.getClassAPtr(a).getX()==1: obj.setPassed();\n"));
95 QVERIFY(_helper->runScript("a = PythonQt.ClassC();\nif obj.getClassAPtr(a).getX()==1: obj.setPassed();\n"));
96 QVERIFY(_helper->runScript("a = PythonQt.ClassD();\nif obj.getClassAPtr(a).getX()==1: obj.setPassed();\n"));
97
98 QEXPECT_FAIL("", "ClassA can not be converted to ClassB", Continue);
99 QVERIFY(_helper->runScript("a = PythonQt.ClassA();\nif obj.getClassBPtr(a).getY()==2: obj.setPassed();\n"));
100 QVERIFY(_helper->runScript("a = PythonQt.ClassB();\nif obj.getClassBPtr(a).getY()==2: obj.setPassed();\n"));
101 QVERIFY(_helper->runScript("a = PythonQt.ClassC();\nif obj.getClassBPtr(a).getY()==2: obj.setPassed();\n"));
102 QVERIFY(_helper->runScript("a = PythonQt.ClassD();\nif obj.getClassBPtr(a).getY()==2: obj.setPassed();\n"));
103
104 QEXPECT_FAIL("", "ClassA can not be converted to ClassC", Continue);
105 QVERIFY(_helper->runScript("a = PythonQt.ClassA();\nif obj.getClassCPtr(a).getX()==1: obj.setPassed();\n"));
106 QEXPECT_FAIL("", "ClassB can not be converted to ClassC", Continue);
107 QVERIFY(_helper->runScript("a = PythonQt.ClassB();\nif obj.getClassCPtr(a).getX()==1: obj.setPassed();\n"));
108 QVERIFY(_helper->runScript("a = PythonQt.ClassC();\nif obj.getClassCPtr(a).getX()==1: obj.setPassed();\n"));
109 QEXPECT_FAIL("", "ClassD can not be converted to ClassC", Continue);
110 QVERIFY(_helper->runScript("a = PythonQt.ClassD();\nif obj.getClassCPtr(a).getX()==1: obj.setPassed();\n"));
111
112 QVERIFY(_helper->runScript("if type(obj.createClassA())==PythonQt.ClassA: obj.setPassed();\n"));
113 QVERIFY(_helper->runScript("if type(obj.createClassB())==PythonQt.ClassB: obj.setPassed();\n"));
114 QVERIFY(_helper->runScript("if type(obj.createClassCAsA())==PythonQt.ClassA: obj.setPassed();\n"));
115 QVERIFY(_helper->runScript("if type(obj.createClassCAsB())==PythonQt.ClassB: obj.setPassed();\n"));
116 QVERIFY(_helper->runScript("if type(obj.createClassD())==PythonQt.ClassD: obj.setPassed();\n"));
117 QVERIFY(_helper->runScript("if type(obj.createClassDAsA())==PythonQt.ClassA: obj.setPassed();\n"));
118 QVERIFY(_helper->runScript("if type(obj.createClassDAsB())==PythonQt.ClassB: obj.setPassed();\n"));
119
120 PythonQt::self()->addPolymorphicHandler("ClassB", polymorphic_ClassB_Handler);
121
122 QVERIFY(_helper->runScript("if type(obj.getClassBPtr(obj.createClassB()))==PythonQt.ClassB: obj.setPassed();\n"));
123 QVERIFY(_helper->runScript("if type(obj.createClassCAsB())==PythonQt.ClassC: obj.setPassed();\n"));
124 QVERIFY(_helper->runScript("if type(obj.createClassDAsB())==PythonQt.ClassD: obj.setPassed();\n"));
125
126 }
127
55 void PythonQtTestSlotCalling::testNoArgSlotCall()
128 void PythonQtTestSlotCalling::testNoArgSlotCall()
56 {
129 {
57 QVERIFY(_helper->runScript("obj.testNoArg(); obj.setPassed();\n"));
130 QVERIFY(_helper->runScript("obj.testNoArg(); obj.setPassed();\n"));
@@ -70,6 +143,29 void PythonQtTestSlotCalling::testOverloadedCall()
70 QVERIFY(_helper->runScript("obj.overload(12,13); obj.setPassed();\n", 6));
143 QVERIFY(_helper->runScript("obj.overload(12,13); obj.setPassed();\n", 6));
71 }
144 }
72
145
146 void PythonQtTestSlotCalling::testPyObjectSlotCall()
147 {
148 QVERIFY(_helper->runScript("if obj.getPyObject(PythonQt)==PythonQt: obj.setPassed();\n"));
149 QVERIFY(_helper->runScript("if obj.getPyObject('Hello')=='Hello': obj.setPassed();\n"));
150 QVERIFY(_helper->runScript("if obj.getPyObjectFromVariant(PythonQt)==PythonQt: obj.setPassed();\n"));
151 QVERIFY(_helper->runScript("if obj.getPyObjectFromVariant2(PythonQt)==PythonQt: obj.setPassed();\n"));
152 // QVERIFY(_helper->runScript("if obj.getPyObjectFromPtr(PythonQt)==PythonQt: obj.setPassed();\n"));
153 }
154
155 void PythonQtTestSlotCalling::testCPPSlotCalls()
156 {
157 // test QColor compare operation
158 QVERIFY(_helper->runScript("if PythonQt.QtGui.QColor(1,2,3)==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();obj.testNoArg()\n"));
159 QVERIFY(_helper->runScript("if PythonQt.QtGui.QColor(1,2,3)!=PythonQt.QtGui.QColor(3,2,1): obj.setPassed();obj.testNoArg()\n"));
160
161 // test passing/returning QColors
162 QVERIFY(_helper->runScript("if obj.getQColor1(PythonQt.QtGui.QColor(1,2,3))==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
163 QVERIFY(_helper->runScript("if obj.getQColor2(PythonQt.QtGui.QColor(1,2,3))==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
164 QVERIFY(_helper->runScript("if obj.getQColor3(PythonQt.QtGui.QColor(1,2,3))==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
165 QVERIFY(_helper->runScript("if obj.getQColor4(PythonQt.QtGui.QColor(1,2,3))==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
166 QVERIFY(_helper->runScript("if obj.getQColor5()==PythonQt.QtGui.QColor(1,2,3): obj.setPassed();\n"));
167 }
168
73 void PythonQtTestSlotCalling::testPODSlotCalls()
169 void PythonQtTestSlotCalling::testPODSlotCalls()
74 {
170 {
75 QVERIFY(_helper->runScript("if obj.getBool(False)==False: obj.setPassed();\n"));
171 QVERIFY(_helper->runScript("if obj.getBool(False)==False: obj.setPassed();\n"));
@@ -209,8 +305,9 void PythonQtTestSignalHandler::testRecursiveSignalHandler()
209 void PythonQtTestApi::initTestCase()
305 void PythonQtTestApi::initTestCase()
210 {
306 {
211 _helper = new PythonQtTestApiHelper();
307 _helper = new PythonQtTestApiHelper();
212 PythonQtObjectPtr main = PythonQt::self()->getMainModule();
308 _main = PythonQt::self()->getMainModule();
213 PythonQt::self()->addObject(main, "obj", _helper);
309 _main.evalScript("import PythonQt");
310 _main.addObject("obj", _helper);
214 }
311 }
215
312
216 bool PythonQtTestApiHelper::call(const QString& function, const QVariantList& args, const QVariant& expectedResult) {
313 bool PythonQtTestApiHelper::call(const QString& function, const QVariantList& args, const QVariant& expectedResult) {
@@ -275,6 +372,42 void PythonQtTestApi::testImporter()
275 PyRun_SimpleString("import bla\n");
372 PyRun_SimpleString("import bla\n");
276 }
373 }
277
374
375 void PythonQtTestApi::testQtNamespace()
376 {
377 QVERIFY(!_main.getVariable("PythonQt.QtCore.Qt.red").toInt()==Qt::red);
378 QVERIFY(_main.getVariable("PythonQt.QtCore.Qt.FlatCap").toInt()==Qt::FlatCap);
379 QVERIFY(PythonQtObjectPtr(_main.getVariable("PythonQt.QtCore.Qt.escape")));
380 }
381
382 void PythonQtTestApi::testQColorDecorators()
383 {
384 PythonQtObjectPtr colorClass = _main.getVariable("PythonQt.QtGui.QColor");
385 QVERIFY(colorClass);
386 // verify that the class is in the correct module
387 QVERIFY(colorClass.getVariable("__module__") == "PythonQt.QtGui");
388 // test on Qt module as well:
389 colorClass = _main.getVariable("PythonQt.Qt.QColor");
390 QVERIFY(colorClass);
391 // constructors
392 QVERIFY(qVariantValue<QColor>(colorClass.call(QVariantList() << 1 << 2 << 3)) == QColor(1,2,3));
393 QVERIFY(qVariantValue<QColor>(colorClass.call()) == QColor());
394 QEXPECT_FAIL("", "Testing non-existing constructor", Continue);
395 QVERIFY(colorClass.call(QVariantList() << 1 << 2) != QVariant());
396
397 // check for decorated Cmyk enum value
398 QVERIFY(colorClass.getVariable("Cmyk").toInt() == QColor::Cmyk);
399 PythonQtObjectPtr staticMethod = colorClass.getVariable("fromRgb");
400 QVERIFY(staticMethod);
401 // direct call of static method via class
402 QVERIFY(qVariantValue<QColor>(colorClass.call("fromRgb", QVariantList() << 1 << 2 << 3)) == QColor(1,2,3));
403 // direct call of static method
404 QVERIFY(qVariantValue<QColor>(staticMethod.call(QVariantList() << 1 << 2 << 3)) == QColor(1,2,3));
405 PythonQtObjectPtr publicMethod = colorClass.getVariable("red");
406 QVERIFY(publicMethod);
407 // call with passing self in:
408 QVERIFY(colorClass.call("red", QVariantList() << QColor(255,0,0)).toInt() == 255);
409 }
410
278 QByteArray PythonQtTestApiHelper::readFileAsBytes(const QString& filename)
411 QByteArray PythonQtTestApiHelper::readFileAsBytes(const QString& filename)
279 {
412 {
280 QByteArray b;
413 QByteArray b;
@@ -63,12 +63,77 private slots:
63 void testVariables();
63 void testVariables();
64 void testRedirect();
64 void testRedirect();
65 void testImporter();
65 void testImporter();
66 void testQColorDecorators();
67 void testQtNamespace();
66
68
67 private:
69 private:
68 PythonQtTestApiHelper* _helper;
70 PythonQtTestApiHelper* _helper;
71 PythonQtObjectPtr _main;
69
72
70 };
73 };
71
74
75 class ClassA {
76 public:
77 ClassA() { x = 1; }
78 int x;
79 };
80
81 class ClassB {
82 public:
83 ClassB() { y = 2; }
84 int y;
85
86 virtual int type() { return 2; }
87 };
88
89 class ClassC : public ClassA, public ClassB {
90 public:
91 ClassC() { z = 3; }
92 int z;
93
94 virtual int type() { return 3; }
95 };
96
97 class ClassD : public QObject, public ClassA, public ClassB {
98 Q_OBJECT
99 public:
100 ClassD() { d = 4; }
101 public slots:
102 int getD() { return d; }
103 private:
104 int d;
105
106 virtual int type() { return 4; }
107 };
108
109 class ClassAWrapper : public QObject {
110 Q_OBJECT
111 public slots:
112 ClassA* new_ClassA() { return new ClassA; }
113 int getX(ClassA* o) { return o->x; }
114 };
115
116 class ClassBWrapper : public QObject {
117 Q_OBJECT
118 public slots:
119 ClassB* new_ClassB() { return new ClassB; }
120 int getY(ClassB* o) { return o->y; }
121 };
122
123 class ClassCWrapper : public QObject {
124 Q_OBJECT
125 public slots:
126 ClassC* new_ClassC() { return new ClassC; }
127 int getZ(ClassC* o) { return o->z; }
128 };
129
130 class ClassDWrapper : public QObject {
131 Q_OBJECT
132 public slots:
133 ClassD* new_ClassD() { return new ClassD; }
134 };
135
136
72 //! test the PythonQt api (helper)
137 //! test the PythonQt api (helper)
73 class PythonQtTestApiHelper : public QObject , public PythonQtImportFileInterface
138 class PythonQtTestApiHelper : public QObject , public PythonQtImportFileInterface
74 {
139 {
@@ -99,6 +164,7 private:
99 bool _passed;
164 bool _passed;
100 };
165 };
101
166
167
102 // test implementation of the wrapper factory
168 // test implementation of the wrapper factory
103 class PythonQtTestCppFactory : public PythonQtCppWrapperFactory
169 class PythonQtTestCppFactory : public PythonQtCppWrapperFactory
104 {
170 {
@@ -174,13 +240,14 private slots:
174
240
175 void testNoArgSlotCall();
241 void testNoArgSlotCall();
176 void testPODSlotCalls();
242 void testPODSlotCalls();
243 void testCPPSlotCalls();
177 void testQVariantSlotCalls();
244 void testQVariantSlotCalls();
178 void testObjectSlotCalls();
245 void testObjectSlotCalls();
179 void testMultiArgsSlotCall();
246 void testMultiArgsSlotCall();
180
247 void testPyObjectSlotCall();
181 void testOverloadedCall();
248 void testOverloadedCall();
182
183 void testCppFactory();
249 void testCppFactory();
250 void testInheritance();
184
251
185 private:
252 private:
186 PythonQtTestSlotCallingHelper* _helper;
253 PythonQtTestSlotCallingHelper* _helper;
@@ -236,6 +303,19 public slots:
236 QStringList getQStringList(const QStringList& l) { _called = true; return l; }
303 QStringList getQStringList(const QStringList& l) { _called = true; return l; }
237 QVariant getQVariant(const QVariant& var) { _called = true; return var; }
304 QVariant getQVariant(const QVariant& var) { _called = true; return var; }
238
305
306 // QColor as representative for C++ value classes
307 QColor getQColor1(const QColor& var) { _called = true; return var; }
308 QColor getQColor2(QColor& var) { _called = true; return var; }
309 QColor getQColor3(QColor* col) { _called = true; return *col; }
310 QColor getQColor4(const QVariant& color) { _called = true; return qVariantValue<QColor>(color); }
311 QColor* getQColor5() { _called = true; static QColor c(1,2,3); return &c; }
312
313 PyObject* getPyObject(PyObject* obj) { _called = true; return obj; }
314 PyObject* getPyObjectFromVariant(const QVariant& val) { _called = true; return PythonQtObjectPtr(val); };
315 QVariant getPyObjectFromVariant2(const QVariant& val) { _called = true; return val; };
316 // this does not yet work but is not required to work:
317 //PyObject* getPyObjectFromPtr(const PythonQtObjectPtr& val) { _called = true; return val; };
318
239 //! testing pointer passing
319 //! testing pointer passing
240 PythonQtTestSlotCallingHelper* getTestObject(PythonQtTestSlotCallingHelper* obj) { _called = true; return obj; }
320 PythonQtTestSlotCallingHelper* getTestObject(PythonQtTestSlotCallingHelper* obj) { _called = true; return obj; }
241 //! testing inheritance checking
321 //! testing inheritance checking
@@ -245,9 +325,6 public slots:
245 QObject* getNewObject() { _called = true; return new PythonQtTestSlotCallingHelper(NULL); }
325 QObject* getNewObject() { _called = true; return new PythonQtTestSlotCallingHelper(NULL); }
246
326
247 QVariantList getMultiArgs(int a, double b, const QString& str) { _called = true; return (QVariantList() << a << b << str); }
327 QVariantList getMultiArgs(int a, double b, const QString& str) { _called = true; return (QVariantList() << a << b << str); }
248 // more exotic, not yet tested
249 //void setByteArray(QByteArray array) { qDebug() << array.data(); }
250 //void setCharPtr(char* data) { qDebug() << data; }
251
328
252 //! cpp wrapper factory test
329 //! cpp wrapper factory test
253 PQCppObject* createPQCppObject(int h) { _called = true; return new PQCppObject(h); }
330 PQCppObject* createPQCppObject(int h) { _called = true; return new PQCppObject(h); }
@@ -261,6 +338,20 public slots:
261 //! cpp wrapper factory test
338 //! cpp wrapper factory test
262 PQCppObjectNoWrap* getPQCppObjectNoWrap(PQCppObjectNoWrap* p) { _called = true; return p; }
339 PQCppObjectNoWrap* getPQCppObjectNoWrap(PQCppObjectNoWrap* p) { _called = true; return p; }
263
340
341 ClassA* getClassAPtr(ClassA* o) { _called = true; return o; }
342 ClassB* getClassBPtr(ClassB* o) { _called = true; return o; }
343 ClassC* getClassCPtr(ClassC* o) { _called = true; return o; }
344 ClassD* getClassDPtr(ClassD* o) { _called = true; return o; }
345
346 ClassA* createClassA() { _called = true; return new ClassA; }
347 ClassB* createClassB() { _called = true; return new ClassB; }
348 ClassC* createClassC() { _called = true; return new ClassC; }
349 ClassD* createClassD() { _called = true; return new ClassD; }
350 ClassA* createClassCAsA() { _called = true; return new ClassC; }
351 ClassB* createClassCAsB() { _called = true; return new ClassC; }
352 ClassA* createClassDAsA() { _called = true; return new ClassD; }
353 ClassB* createClassDAsB() { _called = true; return new ClassD; }
354
264 private:
355 private:
265 bool _passed;
356 bool _passed;
266 bool _called;
357 bool _called;
General Comments 0
You need to be logged in to leave comments. Login now