From 4b8baddee236f7abff413b828c286fb080244e82 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 24 Feb 2021 10:49:46 +1000 Subject: [PATCH] Add test --- tests/src/core/testqgsexpression.cpp | 59 ++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tests/src/core/testqgsexpression.cpp b/tests/src/core/testqgsexpression.cpp index 35f2ef04b28..cb587751836 100644 --- a/tests/src/core/testqgsexpression.cpp +++ b/tests/src/core/testqgsexpression.cpp @@ -4218,6 +4218,65 @@ class TestQgsExpression: public QObject QCOMPARE( exp2.referencedVariables(), QSet() << QStringLiteral( "field_name_part_var" ) << QStringLiteral( "static_feature" ) ); } + void testPrecomputedNodesWithBinaryOperators() + { + QgsFields fields; + fields.append( QgsField( QStringLiteral( "first_field" ), QVariant::Int ) ); + fields.append( QgsField( QStringLiteral( "second_field" ), QVariant::Int ) ); + + // OR operations: + + // the result of this expression will always be true, regardless of the field value - so we can precompile it to a static node! + QgsExpression exp( QStringLiteral( "\"first_field\" or (true or false)" ) ); + + // prepare the expression using static variables + QgsExpressionContext context; + context.setFields( fields ); + QVERIFY( exp.prepare( &context ) ); + QVERIFY( exp.rootNode()->hasCachedStaticValue() ); + QCOMPARE( exp.rootNode()->cachedStaticValue().toBool(), true ); + + // the result of this expression depends on the value of first_field, it can't be completely precompiled + exp = QgsExpression( QStringLiteral( "\"first_field\" or (true and false)" ) ); + QVERIFY( exp.prepare( &context ) ); + QVERIFY( !exp.rootNode()->hasCachedStaticValue() ); + + // the result of this expression will always be true, regardless of the field value + exp = QgsExpression( QStringLiteral( "(true or false) or \"first_field\"" ) ); + QVERIFY( exp.prepare( &context ) ); + QVERIFY( exp.rootNode()->hasCachedStaticValue() ); + QCOMPARE( exp.rootNode()->cachedStaticValue().toBool(), true ); + + // the result of this expression depends on the value of first_field, it can't be completely precompiled + exp = QgsExpression( QStringLiteral( "(true and false) or \"first_field\"" ) ); + QVERIFY( exp.prepare( &context ) ); + QVERIFY( !exp.rootNode()->hasCachedStaticValue() ); + + // AND operations: + + // the result of this expression will always be false, regardless of the field value - so we can precompile it to a static node! + exp = QgsExpression( QStringLiteral( "\"first_field\" AND (true AND false)" ) ); + QVERIFY( exp.prepare( &context ) ); + QVERIFY( exp.rootNode()->hasCachedStaticValue() ); + QCOMPARE( exp.rootNode()->cachedStaticValue().toBool(), false ); + + // the result of this expression depends on the value of first_field, it can't be completely precompiled + exp = QgsExpression( QStringLiteral( "\"first_field\" AND (true or false)" ) ); + QVERIFY( exp.prepare( &context ) ); + QVERIFY( !exp.rootNode()->hasCachedStaticValue() ); + + // the result of this expression will always be false, regardless of the field value + exp = QgsExpression( QStringLiteral( "(true and false) AND \"first_field\"" ) ); + QVERIFY( exp.prepare( &context ) ); + QVERIFY( exp.rootNode()->hasCachedStaticValue() ); + QCOMPARE( exp.rootNode()->cachedStaticValue().toBool(), false ); + + // the result of this expression depends on the value of first_field, it can't be completely precompiled + exp = QgsExpression( QStringLiteral( "(true or false) AND \"first_field\"" ) ); + QVERIFY( exp.prepare( &context ) ); + QVERIFY( !exp.rootNode()->hasCachedStaticValue() ); + } + }; QGSTEST_MAIN( TestQgsExpression )