diff --git a/tests/src/core/testqgsexpression.cpp b/tests/src/core/testqgsexpression.cpp index be40654d393..36130a84ce9 100644 --- a/tests/src/core/testqgsexpression.cpp +++ b/tests/src/core/testqgsexpression.cpp @@ -25,6 +25,7 @@ #include #include #include +#include "qgsexpressioncontext.h" static void _parseAndEvalExpr( int arg ) { @@ -560,21 +561,23 @@ class TestQgsExpression: public QObject f.initAttributes( 3 ); f.setAttribute( 2, QVariant( 20 ) ); + QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, fields ); + // good exp QgsExpression exp( "foo + 1" ); - bool prepareRes = exp.prepare( fields ); + bool prepareRes = exp.prepare( &context ); QCOMPARE( prepareRes, true ); QCOMPARE( exp.hasEvalError(), false ); - QVariant res = exp.evaluate( &f ); + QVariant res = exp.evaluate( &context ); QCOMPARE( res.type(), QVariant::Int ); QCOMPARE( res.toInt(), 21 ); // bad exp QgsExpression exp2( "bar + 1" ); - bool prepareRes2 = exp2.prepare( fields ); + bool prepareRes2 = exp2.prepare( &context ); QCOMPARE( prepareRes2, false ); QCOMPARE( exp2.hasEvalError(), true ); - QVariant res2 = exp2.evaluate( &f ); + QVariant res2 = exp2.evaluate( &context ); QCOMPARE( res2.type(), QVariant::Invalid ); } @@ -584,9 +587,17 @@ class TestQgsExpression: public QObject QVariant v1 = exp.evaluate(); QCOMPARE( v1.toInt(), 1 ); + Q_NOWARN_DEPRECATED_PUSH exp.setCurrentRowNumber( 100 ); + Q_NOWARN_DEPRECATED_POP QVariant v2 = exp.evaluate(); QCOMPARE( v2.toInt(), 101 ); + + QgsExpressionContext context; + context << new QgsExpressionContextScope(); + context.lastScope()->setVariable( "_rownum_", 101 ); + QVariant v3 = exp.evaluate(); + QCOMPARE( v3.toInt(), 101 ); } void eval_scale() @@ -604,17 +615,30 @@ class TestQgsExpression: public QObject { QgsFeature f( 100 ); QgsExpression exp( "$id * 2" ); + Q_NOWARN_DEPRECATED_PUSH QVariant v = exp.evaluate( &f ); + Q_NOWARN_DEPRECATED_POP QCOMPARE( v.toInt(), 200 ); + + QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() ); + QVariant v2 = exp.evaluate( &context ); + QCOMPARE( v2.toInt(), 200 ); } void eval_current_feature() { QgsFeature f( 100 ); QgsExpression exp( "$currentfeature" ); + Q_NOWARN_DEPRECATED_PUSH QVariant v = exp.evaluate( &f ); + Q_NOWARN_DEPRECATED_POP QgsFeature evalFeature = v.value(); QCOMPARE( evalFeature.id(), f.id() ); + + QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() ); + v = exp.evaluate( &context ); + evalFeature = v.value(); + QCOMPARE( evalFeature.id(), f.id() ); } void eval_feature_attribute() @@ -627,10 +651,18 @@ class TestQgsExpression: public QObject f.setAttribute( QString( "col1" ), QString( "test value" ) ); f.setAttribute( QString( "second_column" ), 5 ); QgsExpression exp( "attribute($currentfeature,'col1')" ); + Q_NOWARN_DEPRECATED_PUSH QVariant v = exp.evaluate( &f ); QCOMPARE( v.toString(), QString( "test value" ) ); QgsExpression exp2( "attribute($currentfeature,'second'||'_column')" ); v = exp2.evaluate( &f ); + Q_NOWARN_DEPRECATED_POP + QCOMPARE( v.toInt(), 5 ); + + QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() ); + v = exp.evaluate( &context ); + QCOMPARE( v.toString(), QString( "test value" ) ); + v = exp2.evaluate( &context ); QCOMPARE( v.toInt(), 5 ); } @@ -759,7 +791,14 @@ class TestQgsExpression: public QObject QgsExpression exp( string ); QCOMPARE( exp.hasParserError(), false ); QCOMPARE( exp.needsGeometry(), true ); + Q_NOWARN_DEPRECATED_PUSH QVariant out = exp.evaluate( &f ); + Q_NOWARN_DEPRECATED_POP + QCOMPARE( exp.hasEvalError(), evalError ); + QCOMPARE( out.toDouble(), result ); + + QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() ); + out = exp.evaluate( &context ); QCOMPARE( exp.hasEvalError(), evalError ); QCOMPARE( out.toDouble(), result ); } @@ -775,41 +814,77 @@ class TestQgsExpression: public QObject fPolyline.setGeometry( QgsGeometry::fromPolyline( polyline ) ); fPolygon.setGeometry( QgsGeometry::fromPolygon( polygon ) ); + QgsExpressionContext context; + + Q_NOWARN_DEPRECATED_PUSH QgsExpression exp1( "$area" ); QVariant vArea = exp1.evaluate( &fPolygon ); QCOMPARE( vArea.toDouble(), 40. ); + context.setFeature( fPolygon ); + vArea = exp1.evaluate( &context ); + QCOMPARE( vArea.toDouble(), 40. ); + QgsExpression exp2( "$length" ); QVariant vLength = exp2.evaluate( &fPolyline ); QCOMPARE( vLength.toDouble(), 10. ); + context.setFeature( fPolyline ); + vLength = exp2.evaluate( &context ); + QCOMPARE( vLength.toDouble(), 10. ); + QgsExpression exp3( "$perimeter" ); QVariant vPerimeter = exp3.evaluate( &fPolygon ); QCOMPARE( vPerimeter.toDouble(), 26. ); + context.setFeature( fPolygon ); + vPerimeter = exp3.evaluate( &context ); + QCOMPARE( vPerimeter.toDouble(), 26. ); + QgsExpression exp4( "bounds_width($geometry)" ); QVariant vBoundsWidth = exp4.evaluate( &fPolygon ); QCOMPARE( vBoundsWidth.toDouble(), 8.0 ); + vBoundsWidth = exp4.evaluate( &context ); + QCOMPARE( vBoundsWidth.toDouble(), 8.0 ); + QgsExpression exp5( "bounds_height($geometry)" ); QVariant vBoundsHeight = exp5.evaluate( &fPolygon ); QCOMPARE( vBoundsHeight.toDouble(), 5.0 ); + vBoundsHeight = exp5.evaluate( &context ); + QCOMPARE( vBoundsHeight.toDouble(), 5.0 ); + QgsExpression exp6( "xmin($geometry)" ); QVariant vXMin = exp6.evaluate( &fPolygon ); QCOMPARE( vXMin.toDouble(), 2.0 ); + vXMin = exp6.evaluate( &context ); + QCOMPARE( vXMin.toDouble(), 2.0 ); + QgsExpression exp7( "xmax($geometry)" ); QVariant vXMax = exp7.evaluate( &fPolygon ); QCOMPARE( vXMax.toDouble(), 10.0 ); + vXMax = exp7.evaluate( &context ); + QCOMPARE( vXMax.toDouble(), 10.0 ); + QgsExpression exp8( "ymin($geometry)" ); QVariant vYMin = exp8.evaluate( &fPolygon ); QCOMPARE( vYMin.toDouble(), 1.0 ); + vYMin = exp8.evaluate( &context ); + QCOMPARE( vYMin.toDouble(), 1.0 ); + QgsExpression exp9( "ymax($geometry)" ); QVariant vYMax = exp9.evaluate( &fPolygon ); QCOMPARE( vYMax.toDouble(), 6.0 ); + + exp9.evaluate( &context ); + QCOMPARE( vYMax.toDouble(), 6.0 ); + + Q_NOWARN_DEPRECATED_POP + } void eval_geometry_wkt() @@ -826,21 +901,42 @@ class TestQgsExpression: public QObject fPolyline.setGeometry( QgsGeometry::fromPolyline( polyline ) ); fPolygon.setGeometry( QgsGeometry::fromPolygon( polygon ) ); + QgsExpressionContext context; + + Q_NOWARN_DEPRECATED_PUSH QgsExpression exp1( "geomToWKT($geometry)" ); QVariant vWktLine = exp1.evaluate( &fPolyline ); QCOMPARE( vWktLine.toString(), QString( "LineString (0 0, 10 0)" ) ); + context.setFeature( fPolyline ); + vWktLine = exp1.evaluate( &context ); + QCOMPARE( vWktLine.toString(), QString( "LineString (0 0, 10 0)" ) ); + QgsExpression exp2( "geomToWKT($geometry)" ); QVariant vWktPolygon = exp2.evaluate( &fPolygon ); QCOMPARE( vWktPolygon.toString(), QString( "Polygon ((2 1, 10 1, 10 6, 2 6, 2 1))" ) ); + context.setFeature( fPolygon ); + vWktPolygon = exp2.evaluate( &context ); + QCOMPARE( vWktPolygon.toString(), QString( "Polygon ((2 1, 10 1, 10 6, 2 6, 2 1))" ) ); + QgsExpression exp3( "geomToWKT($geometry)" ); QVariant vWktPoint = exp3.evaluate( &fPoint ); QCOMPARE( vWktPoint.toString(), QString( "Point (-1.23456789 9.87654321)" ) ); + context.setFeature( fPoint ); + vWktPoint = exp3.evaluate( &context ); + QCOMPARE( vWktPoint.toString(), QString( "Point (-1.23456789 9.87654321)" ) ); + QgsExpression exp4( "geomToWKT($geometry, 3)" ); QVariant vWktPointSimplify = exp4.evaluate( &fPoint ); QCOMPARE( vWktPointSimplify.toString(), QString( "Point (-1.235 9.877)" ) ); + + vWktPointSimplify = exp4.evaluate( &context ); + QCOMPARE( vWktPointSimplify.toString(), QString( "Point (-1.235 9.877)" ) ); + + Q_NOWARN_DEPRECATED_POP + } void eval_geometry_constructor_data() @@ -893,12 +989,25 @@ class TestQgsExpression: public QObject QgsExpression exp( string ); QCOMPARE( exp.hasParserError(), false ); QCOMPARE( exp.needsGeometry(), false ); + + //deprecated method + Q_NOWARN_DEPRECATED_PUSH QVariant out = exp.evaluate( &f ); QCOMPARE( exp.hasEvalError(), evalError ); QCOMPARE( out.canConvert(), true ); QgsGeometry outGeom = out.value(); QCOMPARE( geom->equals( &outGeom ), true ); + Q_NOWARN_DEPRECATED_POP + + //replacement method + QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() ); + out = exp.evaluate( &context ); + QCOMPARE( exp.hasEvalError(), evalError ); + + QCOMPARE( out.canConvert(), true ); + outGeom = out.value(); + QCOMPARE( geom->equals( &outGeom ), true ); } void eval_geometry_access_transform_data() @@ -955,12 +1064,24 @@ class TestQgsExpression: public QObject QgsExpression exp( string ); QCOMPARE( exp.hasParserError(), false ); QCOMPARE( exp.needsGeometry(), false ); + + //deprecated method + Q_NOWARN_DEPRECATED_PUSH QVariant out = exp.evaluate( &f ); QCOMPARE( exp.hasEvalError(), evalError ); QCOMPARE( out.canConvert(), true ); QgsGeometry outGeom = out.value(); QCOMPARE( geom->equals( &outGeom ), true ); + Q_NOWARN_DEPRECATED_POP + + //replacement method + QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() ); + out = exp.evaluate( &context ); + QCOMPARE( exp.hasEvalError(), evalError ); + QCOMPARE( out.canConvert(), true ); + outGeom = out.value(); + QCOMPARE( geom->equals( &outGeom ), true ); } void eval_spatial_operator_data() @@ -1014,9 +1135,18 @@ class TestQgsExpression: public QObject QgsExpression exp( string ); QCOMPARE( exp.hasParserError(), false ); QCOMPARE( exp.needsGeometry(), true ); + + //deprecated method + Q_NOWARN_DEPRECATED_PUSH QVariant out = exp.evaluate( &f ); QCOMPARE( exp.hasEvalError(), evalError ); QCOMPARE( out.toInt(), result.toInt() ); + Q_NOWARN_DEPRECATED_POP + + QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() ); + out = exp.evaluate( &context ); + QCOMPARE( exp.hasEvalError(), evalError ); + QCOMPARE( out.toInt(), result.toInt() ); } void eval_geometry_method_data() @@ -1084,12 +1214,25 @@ class TestQgsExpression: public QObject QgsExpression exp( string ); QCOMPARE( exp.hasParserError(), false ); QCOMPARE( exp.needsGeometry(), needGeom ); + + //deprecated method + Q_NOWARN_DEPRECATED_PUSH QVariant out = exp.evaluate( &f ); QCOMPARE( exp.hasEvalError(), evalError ); QCOMPARE( out.canConvert(), true ); QgsGeometry outGeom = out.value(); QVERIFY( compareWkt( outGeom.exportToWkt(), result->exportToWkt() ) ); + Q_NOWARN_DEPRECATED_POP + + //replacement method + QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() ); + out = exp.evaluate( &context ); + QCOMPARE( exp.hasEvalError(), evalError ); + + QCOMPARE( out.canConvert(), true ); + outGeom = out.value(); + QVERIFY( compareWkt( outGeom.exportToWkt(), result->exportToWkt() ) ); delete result; }