Skip to content

Commit

Permalink
Add a test unit for expressions in QgsComposerLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Mercier committed Sep 27, 2012
1 parent ff41bec commit 54fa1ce
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -101,6 +101,7 @@ ADD_QGIS_TEST(rulebasedrenderertest testqgsrulebasedrenderer.cpp)
ADD_QGIS_TEST(ziplayertest testziplayer.cpp)
ADD_QGIS_TEST(dataitemtest testqgsdataitem.cpp)
ADD_QGIS_TEST(composermaptest testqgscomposermap.cpp)
ADD_QGIS_TEST(composerlabeltest testqgscomposerlabel.cpp)
ADD_QGIS_TEST(stylev2test testqgsstylev2.cpp)
#ADD_QGIS_TEST(composerhtmltest testqgscomposerhtml.cpp )
ADD_QGIS_TEST(rectangletest testqgsrectangle.cpp)
Expand Down
164 changes: 164 additions & 0 deletions tests/src/core/testqgscomposerlabel.cpp
@@ -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 added tests/testdata/france_parts.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions tests/testdata/france_parts.prj
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
1 change: 1 addition & 0 deletions tests/testdata/france_parts.qpj
@@ -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 added tests/testdata/france_parts.shp
Binary file not shown.
Binary file added tests/testdata/france_parts.shx
Binary file not shown.

0 comments on commit 54fa1ce

Please sign in to comment.