|
| 1 | +#include "PythonQtTestCleanup.h" |
| 2 | +#include "PythonQt.h" |
| 3 | +#include "PythonQt_QtAll.h" |
| 4 | + |
| 5 | +void PythonQtTestCleanup::initTestCase() |
| 6 | +{ |
| 7 | +} |
| 8 | + |
| 9 | +void PythonQtTestCleanup::cleanupTestCase() |
| 10 | +{ |
| 11 | +} |
| 12 | + |
| 13 | +void PythonQtTestCleanup::init() |
| 14 | +{ |
| 15 | + // Initialize before each test |
| 16 | + |
| 17 | + PythonQt::init(PythonQt::IgnoreSiteModule); |
| 18 | + PythonQt_QtAll::init(); |
| 19 | + |
| 20 | + _helper = new PythonQtTestCleanupHelper(); |
| 21 | + PythonQtObjectPtr main = PythonQt::self()->getMainModule(); |
| 22 | + PythonQt::self()->addObject(main, "obj", _helper); |
| 23 | +} |
| 24 | + |
| 25 | +void PythonQtTestCleanup::cleanup() |
| 26 | +{ |
| 27 | + // Finalize and cleanup after each test |
| 28 | + |
| 29 | + PythonQtObjectPtr main = PythonQt::self()->getMainModule(); |
| 30 | + PythonQt::self()->removeVariable(main, "obj"); |
| 31 | + delete _helper; |
| 32 | + _helper = nullptr; |
| 33 | + |
| 34 | + if (Py_IsInitialized()) { |
| 35 | + Py_Finalize(); |
| 36 | + } |
| 37 | + |
| 38 | + PythonQt::cleanup(); |
| 39 | +} |
| 40 | + |
| 41 | +void PythonQtTestCleanup::testQtEnum() |
| 42 | +{ |
| 43 | + QVERIFY(_helper->runScript( |
| 44 | + "import PythonQt.QtCore\n" \ |
| 45 | + "x = PythonQt.QtCore.QFile.ReadOnly\n" \ |
| 46 | + "obj.setPassed()" |
| 47 | + )); |
| 48 | +} |
| 49 | + |
| 50 | +void PythonQtTestCleanup::testCallQtMethodInDel() |
| 51 | +{ |
| 52 | + QVERIFY(_helper->runScript( |
| 53 | + "import PythonQt.QtCore\n" \ |
| 54 | + "class TimerWrapper(object):\n" \ |
| 55 | + " def __init__(self):\n" \ |
| 56 | + " self.timer = PythonQt.QtCore.QTimer()\n" \ |
| 57 | + " def __del__(self):\n" \ |
| 58 | + " self.timer.setSingleShot(True)\n" \ |
| 59 | + "x = TimerWrapper()\n" \ |
| 60 | + "obj.setPassed()\n" |
| 61 | + )); |
| 62 | +} |
| 63 | + |
| 64 | +bool PythonQtTestCleanupHelper::runScript(const char* script) |
| 65 | +{ |
| 66 | + _passed = false; |
| 67 | + PyRun_SimpleString(script); |
| 68 | + return _passed; |
| 69 | +} |
0 commit comments