Skip to content

Commit e604460

Browse files
committed
Add unit tests for represent_value
1 parent beddd25 commit e604460

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/src/core/testqgsexpression.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,47 @@ class TestQgsExpression: public QObject
378378
QCOMPARE( exp.dump(), dump );
379379
}
380380

381+
void represent_value()
382+
{
383+
QVariantMap config;
384+
QVariantMap map;
385+
map.insert( QStringLiteral( "one" ), QStringLiteral( "1" ) );
386+
map.insert( QStringLiteral( "two" ), QStringLiteral( "2" ) );
387+
map.insert( QStringLiteral( "three" ), QStringLiteral( "3" ) );
388+
389+
config.insert( QStringLiteral( "map" ), map );
390+
mPointsLayer->setEditorWidgetSetup( 3, QgsEditorWidgetSetup( QStringLiteral( "ValueMap" ), config ) );
391+
392+
// Usage on a value map
393+
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( mPointsLayer ) );
394+
QgsExpression expression( "represent_value('Pilots', \"Pilots\")" );
395+
if ( expression.hasParserError() )
396+
qDebug() << expression.parserErrorString();
397+
Q_ASSERT( !expression.hasParserError() );
398+
if ( expression.hasEvalError() )
399+
qDebug() << expression.evalErrorString();
400+
Q_ASSERT( !expression.hasEvalError() );
401+
expression.prepare( &context );
402+
403+
QgsFeature feature;
404+
mPointsLayer->getFeatures( QgsFeatureRequest().setFilterExpression( "Pilots = 1" ) ).nextFeature( feature );
405+
context.setFeature( feature );
406+
QCOMPARE( expression.evaluate( &context ).toString(), QStringLiteral( "one" ) );
407+
408+
// Usage on a simple string
409+
QgsExpression expression2( "represent_value('Class', \"Class\")" );
410+
if ( expression2.hasParserError() )
411+
qDebug() << expression2.parserErrorString();
412+
Q_ASSERT( !expression2.hasParserError() );
413+
if ( expression2.hasEvalError() )
414+
qDebug() << expression2.evalErrorString();
415+
Q_ASSERT( !expression2.hasEvalError() );
416+
expression2.prepare( &context );
417+
mPointsLayer->getFeatures( QgsFeatureRequest().setFilterExpression( "Class = 'Jet'" ) ).nextFeature( feature );
418+
context.setFeature( feature );
419+
QCOMPARE( expression2.evaluate( &context ).toString(), QStringLiteral( "Jet" ) );
420+
}
421+
381422
void evaluation_data()
382423
{
383424
QTest::addColumn<QString>( "string" );

0 commit comments

Comments
 (0)