Skip to content

Commit

Permalink
Add an isField test for QgsExpression
Browse files Browse the repository at this point in the history
Checks whether expression consists of solely a field reference
  • Loading branch information
vmora authored and nyalldawson committed Apr 27, 2015
1 parent ffeb61d commit d431962
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/core/qgsexpression.sip
Expand Up @@ -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 );
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsexpression.h
Expand Up @@ -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; }
Expand Down
10 changes: 10 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -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 )
Expand Down

0 comments on commit d431962

Please sign in to comment.