From d4319621a5b5d1955e937459409963ef1e85afe9 Mon Sep 17 00:00:00 2001 From: vmora Date: Mon, 27 Apr 2015 09:10:59 +1000 Subject: [PATCH] Add an isField test for QgsExpression Checks whether expression consists of solely a field reference --- python/core/qgsexpression.sip | 5 +++++ src/core/qgsexpression.h | 5 +++++ tests/src/core/testqgsexpression.cpp | 10 ++++++++++ 3 files changed, 20 insertions(+) diff --git a/python/core/qgsexpression.sip b/python/core/qgsexpression.sip index 2440499020fb..b92ab8b41fce 100644 --- a/python/core/qgsexpression.sip +++ b/python/core/qgsexpression.sip @@ -72,6 +72,11 @@ class QgsExpression //! @note added in 2.2 static bool hasSpecialColumn( const QString& name ); + /** Checks whether an expression consists only of a single field reference + * @note added in 2.9 + */ + bool isField() const; + static bool isValid( const QString& text, const QgsFields& fields, QString &errorMessage ); void setScale( double scale ); diff --git a/src/core/qgsexpression.h b/src/core/qgsexpression.h index 2d4822887304..a0469780e6a0 100644 --- a/src/core/qgsexpression.h +++ b/src/core/qgsexpression.h @@ -155,6 +155,11 @@ class CORE_EXPORT QgsExpression //! @note added in 2.2 static bool hasSpecialColumn( const QString& name ); + /** Checks whether an expression consists only of a single field reference + * @note added in 2.9 + */ + bool isField() const { return rootNode() && dynamic_cast( rootNode() ) ;} + static bool isValid( const QString& text, const QgsFields& fields, QString &errorMessage ); void setScale( double scale ) { mScale = scale; } diff --git a/tests/src/core/testqgsexpression.cpp b/tests/src/core/testqgsexpression.cpp index 54fc25aa7ba3..007f95425e88 100644 --- a/tests/src/core/testqgsexpression.cpp +++ b/tests/src/core/testqgsexpression.cpp @@ -1099,6 +1099,16 @@ class TestQgsExpression: public QObject QCOMPARE( QgsExpression::evaluateToDouble( QString( "a" ), 9.0 ), 9.0 ); QCOMPARE( QgsExpression::evaluateToDouble( QString(), 9.0 ), 9.0 ); } + + void eval_isField() + { + QCOMPARE( QgsExpression( "" ).isField(), false ); + QCOMPARE( QgsExpression( "42" ).isField(), false ); + QCOMPARE( QgsExpression( "foo" ).isField(), true ); + QCOMPARE( QgsExpression( "\"foo bar\"" ).isField(), true ); + QCOMPARE( QgsExpression( "sqrt(foo)" ).isField(), false ); + QCOMPARE( QgsExpression( "foo + bar" ).isField(), false ); + } }; QTEST_MAIN( TestQgsExpression )