|
| 1 | +/*************************************************************************** |
| 2 | + testqgsapppython.cpp |
| 3 | + -------------------- |
| 4 | + Date : May 2016 |
| 5 | + Copyright : (C) 2016 by Nyall Dawson |
| 6 | + Email : nyall dot dawson at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | +#include <QApplication> |
| 16 | +#include <QObject> |
| 17 | +#include <QSplashScreen> |
| 18 | +#include <QString> |
| 19 | +#include <QStringList> |
| 20 | +#include <QtTest/QtTest> |
| 21 | + |
| 22 | +#include <qgisapp.h> |
| 23 | +#include <qgsapplication.h> |
| 24 | + |
| 25 | +/** \ingroup UnitTests |
| 26 | + * This is a unit test for the QgisApp python support. |
| 27 | + */ |
| 28 | +class TestQgisAppPython : public QObject |
| 29 | +{ |
| 30 | + Q_OBJECT |
| 31 | + |
| 32 | + public: |
| 33 | + TestQgisAppPython(); |
| 34 | + |
| 35 | + private slots: |
| 36 | + void initTestCase();// will be called before the first testfunction is executed. |
| 37 | + void cleanupTestCase();// will be called after the last testfunction was executed. |
| 38 | + void init() {} // will be called before each testfunction is executed. |
| 39 | + void cleanup() {} // will be called after every testfunction. |
| 40 | + |
| 41 | + void runString(); |
| 42 | + void evalString(); |
| 43 | + |
| 44 | + private: |
| 45 | + QgisApp * mQgisApp; |
| 46 | + QString mTestDataDir; |
| 47 | +}; |
| 48 | + |
| 49 | +TestQgisAppPython::TestQgisAppPython() |
| 50 | + : mQgisApp( nullptr ) |
| 51 | +{ |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +//runs before all tests |
| 56 | +void TestQgisAppPython::initTestCase() |
| 57 | +{ |
| 58 | + // Set up the QSettings environment |
| 59 | + QCoreApplication::setOrganizationName( "QGIS" ); |
| 60 | + QCoreApplication::setOrganizationDomain( "qgis.org" ); |
| 61 | + QCoreApplication::setApplicationName( "QGIS-TEST" ); |
| 62 | + |
| 63 | + qDebug() << "TestQgisAppClipboard::initTestCase()"; |
| 64 | + // init QGIS's paths - true means that all path will be inited from prefix |
| 65 | + QgsApplication::init(); |
| 66 | + QgsApplication::initQgis(); |
| 67 | + mTestDataDir = QString( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt |
| 68 | + mQgisApp = new QgisApp(); |
| 69 | + mQgisApp->loadPythonSupport(); |
| 70 | +} |
| 71 | + |
| 72 | +//runs after all tests |
| 73 | +void TestQgisAppPython::cleanupTestCase() |
| 74 | +{ |
| 75 | + QgsApplication::exitQgis(); |
| 76 | +} |
| 77 | + |
| 78 | +void TestQgisAppPython::runString() |
| 79 | +{ |
| 80 | + QVERIFY( mQgisApp->mPythonUtils->runString( "a=1+1" ) ); |
| 81 | + QVERIFY( !mQgisApp->mPythonUtils->runString( "x" ) ); |
| 82 | + QVERIFY( !mQgisApp->mPythonUtils->runString( "" ) ); |
| 83 | +} |
| 84 | + |
| 85 | +void TestQgisAppPython::evalString() |
| 86 | +{ |
| 87 | + QString result; |
| 88 | + //good string |
| 89 | + QVERIFY( mQgisApp->mPythonUtils->evalString( "1+1", result ) ); |
| 90 | + QCOMPARE( result, QString( "2" ) ); |
| 91 | + |
| 92 | + //bad string |
| 93 | + QVERIFY( !mQgisApp->mPythonUtils->evalString( "1+", result ) ); |
| 94 | +} |
| 95 | + |
| 96 | +QTEST_MAIN( TestQgisAppPython ) |
| 97 | +#include "testqgisapppython.moc" |
0 commit comments