Skip to content
Permalink
Browse files

Add hasDefaultValues method to QgsDataDefined, add some tests

  • Loading branch information
nyalldawson committed Nov 20, 2014
1 parent 6cc073d commit 44a334b095412feeeb2d3a13645ec2a067d153c0
@@ -24,6 +24,13 @@ class QgsDataDefined
const QString& field = QString() );

~QgsDataDefined();

/**Returns whether the data defined container is set to all the default
* values, ie, disabled, with empty expression and no assigned field
* @returns true if data defined container is set to default values
* @note added in QGIS 2.7
*/
bool hasDefaultValues() const;

bool isActive() const;
void setActive( bool active );
@@ -39,6 +39,11 @@ QgsDataDefined::~QgsDataDefined()
delete mExpression;
}

bool QgsDataDefined::hasDefaultValues() const
{
return ( !mActive && !mUseExpression && mExpressionString.isEmpty() && mField.isEmpty() );
}

void QgsDataDefined::setExpressionString( const QString &expr )
{
mExpressionString = expr;
@@ -46,6 +46,13 @@ class CORE_EXPORT QgsDataDefined

~QgsDataDefined();

/**Returns whether the data defined container is set to all the default
* values, ie, disabled, with empty expression and no assigned field
* @returns true if data defined container is set to default values
* @note added in QGIS 2.7
*/
bool hasDefaultValues() const;

bool isActive() const { return mActive; }
void setActive( bool active ) { mActive = active; }

@@ -104,6 +104,7 @@ ADD_QGIS_TEST(vectorlayertest testqgsvectorlayer.cpp)
ADD_QGIS_TEST(rulebasedrenderertest testqgsrulebasedrenderer.cpp)
ADD_QGIS_TEST(ziplayertest testziplayer.cpp)
ADD_QGIS_TEST(dataitemtest testqgsdataitem.cpp)
ADD_QGIS_TEST(datadefined testqgsdatadefined.cpp)
ADD_QGIS_TEST(composerobject testqgscomposerobject.cpp)
ADD_QGIS_TEST(composerutils testqgscomposerutils.cpp)
ADD_QGIS_TEST(compositiontest testqgscomposition.cpp)
@@ -0,0 +1,114 @@
/***************************************************************************
testqgsdatadefined.cpp
----------------------
Date : November 2014
Copyright : (C) 2014 Nyall Dawson
Email : nyall dot dawson at gmail 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 <QtTest>
#include <QObject>
#include <QString>
#include <QStringList>
#include <QSettings>

#include <qgsdatadefined.h>

/** \ingroup UnitTests
* Unit tests for QgsDataDefined
*/
class TestQgsDataDefined: 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.
void create();//test creating a data defined container
void gettersSetters(); //test getters and setters
void defaultValues(); //test hasDefaultValues method

private:
};

void TestQgsDataDefined::initTestCase()
{

}

void TestQgsDataDefined::cleanupTestCase()
{

}

void TestQgsDataDefined::init()
{

}

void TestQgsDataDefined::cleanup()
{

}

void TestQgsDataDefined::create()
{
QgsDataDefined* dd = new QgsDataDefined( true, true, QString( "exp" ), QString( "field" ) );
QVERIFY( dd->isActive() );
QVERIFY( dd->useExpression() );
QCOMPARE( dd->expressionString(), QString( "exp" ) );
QCOMPARE( dd->field(), QString( "field" ) );
delete dd;
}

void TestQgsDataDefined::gettersSetters()
{
QgsDataDefined dd;
dd.setActive( false );
QVERIFY( !dd.isActive() );
dd.setActive( true );
QVERIFY( dd.isActive() );

dd.setUseExpression( false );
QVERIFY( !dd.useExpression() );
dd.setUseExpression( true );
QVERIFY( dd.useExpression() );

dd.setExpressionString( QString( "expression" ) );
QCOMPARE( dd.expressionString(), QString( "expression" ) );

dd.setField( QString( "field" ) );
QCOMPARE( dd.field(), QString( "field" ) );
}

void TestQgsDataDefined::defaultValues()
{
QgsDataDefined* dd = new QgsDataDefined();
QVERIFY( dd->hasDefaultValues() );
dd->setActive( true );
QVERIFY( !dd->hasDefaultValues() );
delete dd;
dd = new QgsDataDefined();
dd->setUseExpression( true );
QVERIFY( !dd->hasDefaultValues() );
delete dd;
dd = new QgsDataDefined();
dd->setExpressionString( QString( "expression" ) );
QVERIFY( !dd->hasDefaultValues() );
delete dd;
dd = new QgsDataDefined();
dd->setField( QString( "field" ) );
QVERIFY( !dd->hasDefaultValues() );
delete dd;
}

QTEST_MAIN( TestQgsDataDefined )
#include "testqgsdatadefined.moc"

0 comments on commit 44a334b

Please sign in to comment.
You can’t perform that action at this time.