-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test unit for expressions in QgsComposerLabel
- Loading branch information
Hugo Mercier
committed
Sep 27, 2012
1 parent
ff41bec
commit 54fa1ce
Showing
7 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/*************************************************************************** | ||
testqgscomposerlabel.cpp | ||
---------------------- | ||
begin : Sept 2012 | ||
copyright : (C) 2012 by Hugo Mercier | ||
email : hugo dot mercier at oslandia dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsapplication.h" | ||
#include "qgscomposition.h" | ||
#include "qgscomposerlabel.h" | ||
#include "qgsmaplayerregistry.h" | ||
#include "qgsmaprenderer.h" | ||
#include "qgsvectorlayer.h" | ||
#include "qgsvectordataprovider.h" | ||
#include <QObject> | ||
#include <QtTest> | ||
|
||
class TestQgsComposerLabel: public QObject | ||
{ | ||
Q_OBJECT; | ||
private slots: | ||
void initTestCase();// will be called before the first testfunction is executed. | ||
void cleanupTestCase();// will be called after the last testfunction was executed. | ||
void init();// will be called before each testfunction is executed. | ||
void cleanup();// will be called after every testfunction. | ||
|
||
// test simple expression evaluation | ||
void evaluation(); | ||
// test expression evaluation when a feature is set | ||
void feature_evaluation(); | ||
private: | ||
QgsComposition* mComposition; | ||
QgsComposerLabel* mComposerLabel; | ||
QgsMapRenderer* mMapRenderer; | ||
QgsVectorLayer* mVectorLayer; | ||
}; | ||
|
||
void TestQgsComposerLabel::initTestCase() | ||
{ | ||
QgsApplication::init(); | ||
QgsApplication::initQgis(); | ||
|
||
//create maplayers from testdata and add to layer registry | ||
QFileInfo vectorFileInfo( QString( TEST_DATA_DIR ) + QDir::separator() + "france_parts.shp" ); | ||
mVectorLayer = new QgsVectorLayer( vectorFileInfo.filePath(), | ||
vectorFileInfo.completeBaseName(), | ||
"ogr" ); | ||
QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer*>() << mVectorLayer ); | ||
|
||
//create composition with composer map | ||
mMapRenderer = new QgsMapRenderer(); | ||
mMapRenderer->setLayerSet( QStringList() << mVectorLayer->id() ); | ||
mMapRenderer->setProjectionsEnabled( false ); | ||
mComposition = new QgsComposition( mMapRenderer ); | ||
mComposition->setPaperSize( 297, 210 ); //A4 landscape | ||
|
||
mComposerLabel = new QgsComposerLabel( mComposition ); | ||
mComposition->addComposerLabel( mComposerLabel ); | ||
} | ||
|
||
void TestQgsComposerLabel::cleanupTestCase() | ||
{ | ||
delete mComposition; | ||
delete mMapRenderer; | ||
delete mVectorLayer; | ||
} | ||
|
||
void TestQgsComposerLabel::init() | ||
{ | ||
|
||
} | ||
|
||
void TestQgsComposerLabel::cleanup() | ||
{ | ||
|
||
} | ||
|
||
void TestQgsComposerLabel::evaluation() | ||
{ | ||
{ | ||
// $CURRENT_DATE evaluation | ||
QString expected = "__" + QDate::currentDate().toString() + "__"; | ||
mComposerLabel->setText( "__$CURRENT_DATE__" ); | ||
QString evaluated = mComposerLabel->displayText(); | ||
QCOMPARE( evaluated, expected ); | ||
} | ||
{ | ||
// $CURRENT_DATE() evaluation | ||
QDateTime now = QDateTime::currentDateTime(); | ||
QString expected = "__" + now.toString( "dd" ) + "(ok)__"; | ||
mComposerLabel->setText( "__$CURRENT_DATE(dd)(ok)__" ); | ||
QString evaluated = mComposerLabel->displayText(); | ||
QCOMPARE( evaluated, expected ); | ||
} | ||
{ | ||
// $CURRENT_DATE() evaluation (inside an expression) | ||
QDate now = QDate::currentDate(); | ||
int dd = now.day(); | ||
|
||
QString expected = "__" + QString("%1").arg(dd+1, 2, 10, QChar('0')) + "(ok)__"; | ||
mComposerLabel->setText( "__[%$CURRENT_DATE(dd) + 1%](ok)__" ); | ||
QString evaluated = mComposerLabel->displayText(); | ||
QCOMPARE( evaluated, expected ); | ||
} | ||
{ | ||
// expression evaluation (without feature) | ||
QString expected = "__[NAME_1]42__"; | ||
mComposerLabel->setText( "__[%\"NAME_1\"%][%21*2%]__" ); | ||
QString evaluated = mComposerLabel->displayText(); | ||
QCOMPARE( evaluated, expected ); | ||
} | ||
} | ||
|
||
void TestQgsComposerLabel::feature_evaluation() | ||
{ | ||
QgsVectorDataProvider* provider = mVectorLayer->dataProvider(); | ||
|
||
QgsAttributeList allAttrs = provider->attributeIndexes(); | ||
provider->select( allAttrs ); | ||
QgsFeature feat; | ||
|
||
provider->nextFeature( feat ); | ||
{ | ||
// evaluation with a feature | ||
mComposerLabel->setExpressionContext( &feat, mVectorLayer ); | ||
mComposerLabel->setText( "[%\"NAME_1\"||'_ok'%]" ); | ||
QString evaluated = mComposerLabel->displayText(); | ||
QString expected = "Basse-Normandie_ok"; | ||
QCOMPARE( evaluated, expected ); | ||
} | ||
provider->nextFeature( feat ); | ||
{ | ||
// evaluation with a feature | ||
mComposerLabel->setExpressionContext( &feat, mVectorLayer ); | ||
mComposerLabel->setText( "[%\"NAME_1\"||'_ok'%]" ); | ||
QString evaluated = mComposerLabel->displayText(); | ||
QString expected = "Bretagne_ok"; | ||
QCOMPARE( evaluated, expected ); | ||
} | ||
{ | ||
// evaluation with a feature and local variables | ||
QMap<QString, QVariant> locals; | ||
locals.insert( "$test", "OK" ); | ||
|
||
mComposerLabel->setExpressionContext( &feat, mVectorLayer, locals ); | ||
mComposerLabel->setText( "[%\"NAME_1\"||$test%]" ); | ||
QString evaluated = mComposerLabel->displayText(); | ||
QString expected = "BretagneOK"; | ||
QCOMPARE( evaluated, expected ); | ||
} | ||
} | ||
|
||
QTEST_MAIN( TestQgsComposerLabel ) | ||
#include "moc_testqgscomposerlabel.cxx" |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]] |
Binary file not shown.
Binary file not shown.