Skip to content
Permalink
Browse files

Better error message when no feature available for expression evaluation

  • Loading branch information
nyalldawson committed Jul 24, 2020
1 parent cc0cfbe commit 6dbc1788b4ba451b75eb6887abe1db4d431af510
Showing with 44 additions and 0 deletions.
  1. +4 −0 src/core/expression/qgsexpressionnodeimpl.cpp
  2. +40 −0 tests/src/core/testqgsexpression.cpp
@@ -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( "Field '%1' not found" ).arg( mName ) );
@@ -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" );

0 comments on commit 6dbc178

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