Add an isField test for QgsExpression

Checks whether expression consists of solely a field reference
This commit is contained in:
vmora 2015-04-27 09:10:59 +10:00 committed by Nyall Dawson
parent ffeb61d549
commit d4319621a5
3 changed files with 20 additions and 0 deletions

View File

@ -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 );

View File

@ -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; }

View File

@ -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 )