Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Better error message when no feature available for expression evaluation
(cherry picked from commit 6dbc178)
  • Loading branch information
nyalldawson committed Jul 27, 2020
1 parent 8519e28 commit b05f732
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/expression/qgsexpressionnodeimpl.cpp
Expand Up @@ -1307,6 +1307,10 @@ QVariant QgsExpressionNodeColumnRef::evalNode( QgsExpression *parent, const QgsE
else
return feature.attribute( mName );
}
else
{
parent->setEvalErrorString( tr( "No feature available for field '%1' evaluation" ).arg( mName ) );
}
}
if ( index < 0 )
parent->setEvalErrorString( tr( "Column '%1' not found" ).arg( mName ) );
Expand Down
40 changes: 40 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -549,6 +549,46 @@ class TestQgsExpression: public QObject
QVERIFY( expression4.hasEvalError() );
}

void fieldsButNoFeature()
{
// test evaluating an expression with fields in the context but no feature
QgsExpressionContext context;
QgsExpressionContextScope *scope = new QgsExpressionContextScope();

QgsFields fields;
fields.append( QgsField( QStringLiteral( "x" ) ) );
fields.append( QgsField( QStringLiteral( "y" ) ) );
fields.append( QgsField( QStringLiteral( "z" ) ) );
scope->setFields( fields );
context.appendScope( scope );

// doesn't exist
QgsExpression expression( "\"a\"" );
QVERIFY( !expression.hasParserError() );
QVERIFY( !expression.evaluate( &context ).isValid() );
QVERIFY( expression.hasEvalError() );
QCOMPARE( expression.evalErrorString(), QStringLiteral( "Field 'a' not found" ) );
expression = QgsExpression( "\"x\"" );
QVERIFY( !expression.hasParserError() );
QVERIFY( !expression.evaluate( &context ).isValid() );
QVERIFY( expression.hasEvalError() );
QCOMPARE( expression.evalErrorString(), QStringLiteral( "No feature available for field 'x' evaluation" ) );
expression = QgsExpression( "\"y\"" );
QVERIFY( !expression.hasParserError() );
QVERIFY( !expression.evaluate( &context ).isValid() );
QVERIFY( expression.hasEvalError() );
QCOMPARE( expression.evalErrorString(), QStringLiteral( "No feature available for field 'y' evaluation" ) );

QgsFeature f( fields );
f.setValid( true );
f.setAttributes( QgsAttributes() << 1 << 2 << 3 );
scope->setFeature( f );
expression = QgsExpression( "\"z\"" );
QVERIFY( !expression.hasParserError() );
QCOMPARE( expression.evaluate( &context ).toInt(), 3 );
QVERIFY( !expression.hasEvalError() );
}

void evaluation_data()
{
QTest::addColumn<QString>( "string" );
Expand Down

0 comments on commit b05f732

Please sign in to comment.