Skip to content
Permalink
Browse files

Add an isField test for QgsExpression

Checks whether expression consists of solely a field reference
  • Loading branch information
vmora authored and nyalldawson committed Apr 27, 2015
1 parent ffeb61d commit d4319621a5b5d1955e937459409963ef1e85afe9
Showing with 20 additions and 0 deletions.
  1. +5 −0 python/core/qgsexpression.sip
  2. +5 −0 src/core/qgsexpression.h
  3. +10 −0 tests/src/core/testqgsexpression.cpp
@@ -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 );
@@ -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<const NodeColumnRef*>( rootNode() ) ;}

static bool isValid( const QString& text, const QgsFields& fields, QString &errorMessage );

void setScale( double scale ) { mScale = scale; }
@@ -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 )

0 comments on commit d431962

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