Skip to content
Permalink
Browse files

Add unit test for calculator filter

  • Loading branch information
nyalldawson committed Feb 24, 2018
1 parent f0fcdb8 commit 033e2e6436d5935ea7e41ec562d11582948c3568
@@ -288,6 +288,11 @@ QgsExpressionCalculatorLocatorFilter::QgsExpressionCalculatorLocatorFilter( QObj
setUseWithoutPrefix( false );
}

QgsExpressionCalculatorLocatorFilter *QgsExpressionCalculatorLocatorFilter::clone() const
{
return new QgsExpressionCalculatorLocatorFilter();
}

void QgsExpressionCalculatorLocatorFilter::fetchResults( const QString &string, const QgsLocatorContext &, QgsFeedback * )
{
QgsExpressionContext context;
@@ -18,13 +18,14 @@
#ifndef QGSINBUILTLOCATORFILTERS_H
#define QGSINBUILTLOCATORFILTERS_H

#include "qgisapp.h"
#include "qgslocatorfilter.h"
#include "qgsexpressioncontext.h"
#include "qgsfeatureiterator.h"

class QAction;

class QgsLayerTreeLocatorFilter : public QgsLocatorFilter
class APP_EXPORT QgsLayerTreeLocatorFilter : public QgsLocatorFilter
{
Q_OBJECT

@@ -43,7 +44,7 @@ class QgsLayerTreeLocatorFilter : public QgsLocatorFilter

};

class QgsLayoutLocatorFilter : public QgsLocatorFilter
class APP_EXPORT QgsLayoutLocatorFilter : public QgsLocatorFilter
{
Q_OBJECT

@@ -112,17 +113,19 @@ class QgsActiveLayerFeaturesLocatorFilter : public QgsLocatorFilter
QIcon mLayerIcon;
};

class QgsExpressionCalculatorLocatorFilter : public QgsLocatorFilter
class APP_EXPORT QgsExpressionCalculatorLocatorFilter : public QgsLocatorFilter
{
Q_OBJECT

public:

QgsExpressionCalculatorLocatorFilter( QObject *parent = nullptr );
QgsExpressionCalculatorLocatorFilter *clone() const override;
QString name() const override { return QStringLiteral( "calculator" ); }
QString displayName() const override { return tr( "Calculator" ); }
Priority priority() const override { return Highest; }
QString prefix() const override { return QStringLiteral( "=" ); }
QgsLocatorFilter::Flags flags() const override { return QgsLocatorFilter::FlagFast; }

void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
void triggerResult( const QgsLocatorResult &result ) override;
@@ -8,6 +8,7 @@ INCLUDE_DIRECTORIES(
${CMAKE_SOURCE_DIR}/src/core/expression
${CMAKE_SOURCE_DIR}/src/core/geometry
${CMAKE_SOURCE_DIR}/src/core/layout
${CMAKE_SOURCE_DIR}/src/core/locator
${CMAKE_SOURCE_DIR}/src/core/metadata
${CMAKE_SOURCE_DIR}/src/core/raster
${CMAKE_SOURCE_DIR}/src/core/symbology
@@ -94,6 +95,7 @@ ADD_QGIS_TEST(apppythontest testqgisapppython.cpp)
ENDIF ()
ADD_QGIS_TEST(qgisappclipboard testqgisappclipboard.cpp)
ADD_QGIS_TEST(attributetabletest testqgsattributetable.cpp)
ADD_QGIS_TEST(applocatorfilters testqgsapplocatorfilters.cpp)
ADD_QGIS_TEST(fieldcalculatortest testqgsfieldcalculator.cpp)
ADD_QGIS_TEST(maptooladdfeature testqgsmaptooladdfeature.cpp)
ADD_QGIS_TEST(maptoolidentifyaction testqgsmaptoolidentifyaction.cpp)
@@ -0,0 +1,93 @@
/***************************************************************************
testqgsapplocatorfilters.cpp
--------------------------
Date : 2018-02-24
Copyright : (C) 2018 by 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 "qgstest.h"
#include "qgisapp.h"
#include "qgslocatorfilter.h"
#include "qgslocator.h"
#include "locator/qgsinbuiltlocatorfilters.h"
#include <QSignalSpy>
#include <QClipboard>

/**
* \ingroup UnitTests
* This is a unit test for the field calculator
*/
class TestQgsAppLocatorFilters : 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 testCalculator();

private:
QgisApp *mQgisApp = nullptr;

QList< QgsLocatorResult > gatherResults( QgsLocatorFilter *filter, const QString &string, const QgsLocatorContext &context );
};

//runs before all tests
void TestQgsAppLocatorFilters::initTestCase()
{
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();
mQgisApp = new QgisApp();
}

//runs after all tests
void TestQgsAppLocatorFilters::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsAppLocatorFilters::testCalculator()
{
QgsExpressionCalculatorLocatorFilter filter;

// valid expression
QList< QgsLocatorResult > results = gatherResults( &filter, QStringLiteral( "1+2" ), QgsLocatorContext() );
QCOMPARE( results.count(), 1 );
QCOMPARE( results.at( 0 ).userData.toInt(), 3 );

// trigger result
filter.triggerResult( results.at( 0 ) );
QCOMPARE( QApplication::clipboard()->text(), QStringLiteral( "3" ) );

// invalid expression
results = gatherResults( &filter, QStringLiteral( "1+" ), QgsLocatorContext() );
QVERIFY( results.empty() );

}

QList<QgsLocatorResult> TestQgsAppLocatorFilters::gatherResults( QgsLocatorFilter *filter, const QString &string, const QgsLocatorContext &context )
{
QSignalSpy spy( filter, &QgsLocatorFilter::resultFetched );
QgsFeedback f;
filter->fetchResults( string, context, &f );

QList< QgsLocatorResult > results;
for ( int i = 0; i < spy.count(); ++ i )
{
QVariant v = spy.at( i ).at( i );
QgsLocatorResult result = v.value<QgsLocatorResult>();
results.append( result );
}
return results;
}

QGSTEST_MAIN( TestQgsAppLocatorFilters )
#include "testqgsapplocatorfilters.moc"

0 comments on commit 033e2e6

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