Skip to content

Commit f9a7820

Browse files
committedAug 22, 2015
Port QgsExpression tests to contexts
1 parent c1adf62 commit f9a7820

File tree

1 file changed

+147
-4
lines changed

1 file changed

+147
-4
lines changed
 

‎tests/src/core/testqgsexpression.cpp

Lines changed: 147 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <qgsfeaturerequest.h>
2626
#include <qgsgeometry.h>
2727
#include <qgsrenderchecker.h>
28+
#include "qgsexpressioncontext.h"
2829

2930
static void _parseAndEvalExpr( int arg )
3031
{
@@ -560,21 +561,23 @@ class TestQgsExpression: public QObject
560561
f.initAttributes( 3 );
561562
f.setAttribute( 2, QVariant( 20 ) );
562563

564+
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, fields );
565+
563566
// good exp
564567
QgsExpression exp( "foo + 1" );
565-
bool prepareRes = exp.prepare( fields );
568+
bool prepareRes = exp.prepare( &context );
566569
QCOMPARE( prepareRes, true );
567570
QCOMPARE( exp.hasEvalError(), false );
568-
QVariant res = exp.evaluate( &f );
571+
QVariant res = exp.evaluate( &context );
569572
QCOMPARE( res.type(), QVariant::Int );
570573
QCOMPARE( res.toInt(), 21 );
571574

572575
// bad exp
573576
QgsExpression exp2( "bar + 1" );
574-
bool prepareRes2 = exp2.prepare( fields );
577+
bool prepareRes2 = exp2.prepare( &context );
575578
QCOMPARE( prepareRes2, false );
576579
QCOMPARE( exp2.hasEvalError(), true );
577-
QVariant res2 = exp2.evaluate( &f );
580+
QVariant res2 = exp2.evaluate( &context );
578581
QCOMPARE( res2.type(), QVariant::Invalid );
579582
}
580583

@@ -584,9 +587,17 @@ class TestQgsExpression: public QObject
584587
QVariant v1 = exp.evaluate();
585588
QCOMPARE( v1.toInt(), 1 );
586589

590+
Q_NOWARN_DEPRECATED_PUSH
587591
exp.setCurrentRowNumber( 100 );
592+
Q_NOWARN_DEPRECATED_POP
588593
QVariant v2 = exp.evaluate();
589594
QCOMPARE( v2.toInt(), 101 );
595+
596+
QgsExpressionContext context;
597+
context << new QgsExpressionContextScope();
598+
context.lastScope()->setVariable( "_rownum_", 101 );
599+
QVariant v3 = exp.evaluate();
600+
QCOMPARE( v3.toInt(), 101 );
590601
}
591602

592603
void eval_scale()
@@ -604,17 +615,30 @@ class TestQgsExpression: public QObject
604615
{
605616
QgsFeature f( 100 );
606617
QgsExpression exp( "$id * 2" );
618+
Q_NOWARN_DEPRECATED_PUSH
607619
QVariant v = exp.evaluate( &f );
620+
Q_NOWARN_DEPRECATED_POP
608621
QCOMPARE( v.toInt(), 200 );
622+
623+
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() );
624+
QVariant v2 = exp.evaluate( &context );
625+
QCOMPARE( v2.toInt(), 200 );
609626
}
610627

611628
void eval_current_feature()
612629
{
613630
QgsFeature f( 100 );
614631
QgsExpression exp( "$currentfeature" );
632+
Q_NOWARN_DEPRECATED_PUSH
615633
QVariant v = exp.evaluate( &f );
634+
Q_NOWARN_DEPRECATED_POP
616635
QgsFeature evalFeature = v.value<QgsFeature>();
617636
QCOMPARE( evalFeature.id(), f.id() );
637+
638+
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() );
639+
v = exp.evaluate( &context );
640+
evalFeature = v.value<QgsFeature>();
641+
QCOMPARE( evalFeature.id(), f.id() );
618642
}
619643

620644
void eval_feature_attribute()
@@ -627,10 +651,18 @@ class TestQgsExpression: public QObject
627651
f.setAttribute( QString( "col1" ), QString( "test value" ) );
628652
f.setAttribute( QString( "second_column" ), 5 );
629653
QgsExpression exp( "attribute($currentfeature,'col1')" );
654+
Q_NOWARN_DEPRECATED_PUSH
630655
QVariant v = exp.evaluate( &f );
631656
QCOMPARE( v.toString(), QString( "test value" ) );
632657
QgsExpression exp2( "attribute($currentfeature,'second'||'_column')" );
633658
v = exp2.evaluate( &f );
659+
Q_NOWARN_DEPRECATED_POP
660+
QCOMPARE( v.toInt(), 5 );
661+
662+
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() );
663+
v = exp.evaluate( &context );
664+
QCOMPARE( v.toString(), QString( "test value" ) );
665+
v = exp2.evaluate( &context );
634666
QCOMPARE( v.toInt(), 5 );
635667
}
636668

@@ -759,7 +791,14 @@ class TestQgsExpression: public QObject
759791
QgsExpression exp( string );
760792
QCOMPARE( exp.hasParserError(), false );
761793
QCOMPARE( exp.needsGeometry(), true );
794+
Q_NOWARN_DEPRECATED_PUSH
762795
QVariant out = exp.evaluate( &f );
796+
Q_NOWARN_DEPRECATED_POP
797+
QCOMPARE( exp.hasEvalError(), evalError );
798+
QCOMPARE( out.toDouble(), result );
799+
800+
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() );
801+
out = exp.evaluate( &context );
763802
QCOMPARE( exp.hasEvalError(), evalError );
764803
QCOMPARE( out.toDouble(), result );
765804
}
@@ -775,41 +814,77 @@ class TestQgsExpression: public QObject
775814
fPolyline.setGeometry( QgsGeometry::fromPolyline( polyline ) );
776815
fPolygon.setGeometry( QgsGeometry::fromPolygon( polygon ) );
777816

817+
QgsExpressionContext context;
818+
819+
Q_NOWARN_DEPRECATED_PUSH
778820
QgsExpression exp1( "$area" );
779821
QVariant vArea = exp1.evaluate( &fPolygon );
780822
QCOMPARE( vArea.toDouble(), 40. );
781823

824+
context.setFeature( fPolygon );
825+
vArea = exp1.evaluate( &context );
826+
QCOMPARE( vArea.toDouble(), 40. );
827+
782828
QgsExpression exp2( "$length" );
783829
QVariant vLength = exp2.evaluate( &fPolyline );
784830
QCOMPARE( vLength.toDouble(), 10. );
785831

832+
context.setFeature( fPolyline );
833+
vLength = exp2.evaluate( &context );
834+
QCOMPARE( vLength.toDouble(), 10. );
835+
786836
QgsExpression exp3( "$perimeter" );
787837
QVariant vPerimeter = exp3.evaluate( &fPolygon );
788838
QCOMPARE( vPerimeter.toDouble(), 26. );
789839

840+
context.setFeature( fPolygon );
841+
vPerimeter = exp3.evaluate( &context );
842+
QCOMPARE( vPerimeter.toDouble(), 26. );
843+
790844
QgsExpression exp4( "bounds_width($geometry)" );
791845
QVariant vBoundsWidth = exp4.evaluate( &fPolygon );
792846
QCOMPARE( vBoundsWidth.toDouble(), 8.0 );
793847

848+
vBoundsWidth = exp4.evaluate( &context );
849+
QCOMPARE( vBoundsWidth.toDouble(), 8.0 );
850+
794851
QgsExpression exp5( "bounds_height($geometry)" );
795852
QVariant vBoundsHeight = exp5.evaluate( &fPolygon );
796853
QCOMPARE( vBoundsHeight.toDouble(), 5.0 );
797854

855+
vBoundsHeight = exp5.evaluate( &context );
856+
QCOMPARE( vBoundsHeight.toDouble(), 5.0 );
857+
798858
QgsExpression exp6( "xmin($geometry)" );
799859
QVariant vXMin = exp6.evaluate( &fPolygon );
800860
QCOMPARE( vXMin.toDouble(), 2.0 );
801861

862+
vXMin = exp6.evaluate( &context );
863+
QCOMPARE( vXMin.toDouble(), 2.0 );
864+
802865
QgsExpression exp7( "xmax($geometry)" );
803866
QVariant vXMax = exp7.evaluate( &fPolygon );
804867
QCOMPARE( vXMax.toDouble(), 10.0 );
805868

869+
vXMax = exp7.evaluate( &context );
870+
QCOMPARE( vXMax.toDouble(), 10.0 );
871+
806872
QgsExpression exp8( "ymin($geometry)" );
807873
QVariant vYMin = exp8.evaluate( &fPolygon );
808874
QCOMPARE( vYMin.toDouble(), 1.0 );
809875

876+
vYMin = exp8.evaluate( &context );
877+
QCOMPARE( vYMin.toDouble(), 1.0 );
878+
810879
QgsExpression exp9( "ymax($geometry)" );
811880
QVariant vYMax = exp9.evaluate( &fPolygon );
812881
QCOMPARE( vYMax.toDouble(), 6.0 );
882+
883+
exp9.evaluate( &context );
884+
QCOMPARE( vYMax.toDouble(), 6.0 );
885+
886+
Q_NOWARN_DEPRECATED_POP
887+
813888
}
814889

815890
void eval_geometry_wkt()
@@ -826,21 +901,42 @@ class TestQgsExpression: public QObject
826901
fPolyline.setGeometry( QgsGeometry::fromPolyline( polyline ) );
827902
fPolygon.setGeometry( QgsGeometry::fromPolygon( polygon ) );
828903

904+
QgsExpressionContext context;
905+
906+
Q_NOWARN_DEPRECATED_PUSH
829907
QgsExpression exp1( "geomToWKT($geometry)" );
830908
QVariant vWktLine = exp1.evaluate( &fPolyline );
831909
QCOMPARE( vWktLine.toString(), QString( "LineString (0 0, 10 0)" ) );
832910

911+
context.setFeature( fPolyline );
912+
vWktLine = exp1.evaluate( &context );
913+
QCOMPARE( vWktLine.toString(), QString( "LineString (0 0, 10 0)" ) );
914+
833915
QgsExpression exp2( "geomToWKT($geometry)" );
834916
QVariant vWktPolygon = exp2.evaluate( &fPolygon );
835917
QCOMPARE( vWktPolygon.toString(), QString( "Polygon ((2 1, 10 1, 10 6, 2 6, 2 1))" ) );
836918

919+
context.setFeature( fPolygon );
920+
vWktPolygon = exp2.evaluate( &context );
921+
QCOMPARE( vWktPolygon.toString(), QString( "Polygon ((2 1, 10 1, 10 6, 2 6, 2 1))" ) );
922+
837923
QgsExpression exp3( "geomToWKT($geometry)" );
838924
QVariant vWktPoint = exp3.evaluate( &fPoint );
839925
QCOMPARE( vWktPoint.toString(), QString( "Point (-1.23456789 9.87654321)" ) );
840926

927+
context.setFeature( fPoint );
928+
vWktPoint = exp3.evaluate( &context );
929+
QCOMPARE( vWktPoint.toString(), QString( "Point (-1.23456789 9.87654321)" ) );
930+
841931
QgsExpression exp4( "geomToWKT($geometry, 3)" );
842932
QVariant vWktPointSimplify = exp4.evaluate( &fPoint );
843933
QCOMPARE( vWktPointSimplify.toString(), QString( "Point (-1.235 9.877)" ) );
934+
935+
vWktPointSimplify = exp4.evaluate( &context );
936+
QCOMPARE( vWktPointSimplify.toString(), QString( "Point (-1.235 9.877)" ) );
937+
938+
Q_NOWARN_DEPRECATED_POP
939+
844940
}
845941

846942
void eval_geometry_constructor_data()
@@ -893,12 +989,25 @@ class TestQgsExpression: public QObject
893989
QgsExpression exp( string );
894990
QCOMPARE( exp.hasParserError(), false );
895991
QCOMPARE( exp.needsGeometry(), false );
992+
993+
//deprecated method
994+
Q_NOWARN_DEPRECATED_PUSH
896995
QVariant out = exp.evaluate( &f );
897996
QCOMPARE( exp.hasEvalError(), evalError );
898997

899998
QCOMPARE( out.canConvert<QgsGeometry>(), true );
900999
QgsGeometry outGeom = out.value<QgsGeometry>();
9011000
QCOMPARE( geom->equals( &outGeom ), true );
1001+
Q_NOWARN_DEPRECATED_POP
1002+
1003+
//replacement method
1004+
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() );
1005+
out = exp.evaluate( &context );
1006+
QCOMPARE( exp.hasEvalError(), evalError );
1007+
1008+
QCOMPARE( out.canConvert<QgsGeometry>(), true );
1009+
outGeom = out.value<QgsGeometry>();
1010+
QCOMPARE( geom->equals( &outGeom ), true );
9021011
}
9031012

9041013
void eval_geometry_access_transform_data()
@@ -955,12 +1064,24 @@ class TestQgsExpression: public QObject
9551064
QgsExpression exp( string );
9561065
QCOMPARE( exp.hasParserError(), false );
9571066
QCOMPARE( exp.needsGeometry(), false );
1067+
1068+
//deprecated method
1069+
Q_NOWARN_DEPRECATED_PUSH
9581070
QVariant out = exp.evaluate( &f );
9591071
QCOMPARE( exp.hasEvalError(), evalError );
9601072

9611073
QCOMPARE( out.canConvert<QgsGeometry>(), true );
9621074
QgsGeometry outGeom = out.value<QgsGeometry>();
9631075
QCOMPARE( geom->equals( &outGeom ), true );
1076+
Q_NOWARN_DEPRECATED_POP
1077+
1078+
//replacement method
1079+
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() );
1080+
out = exp.evaluate( &context );
1081+
QCOMPARE( exp.hasEvalError(), evalError );
1082+
QCOMPARE( out.canConvert<QgsGeometry>(), true );
1083+
outGeom = out.value<QgsGeometry>();
1084+
QCOMPARE( geom->equals( &outGeom ), true );
9641085
}
9651086

9661087
void eval_spatial_operator_data()
@@ -1014,9 +1135,18 @@ class TestQgsExpression: public QObject
10141135
QgsExpression exp( string );
10151136
QCOMPARE( exp.hasParserError(), false );
10161137
QCOMPARE( exp.needsGeometry(), true );
1138+
1139+
//deprecated method
1140+
Q_NOWARN_DEPRECATED_PUSH
10171141
QVariant out = exp.evaluate( &f );
10181142
QCOMPARE( exp.hasEvalError(), evalError );
10191143
QCOMPARE( out.toInt(), result.toInt() );
1144+
Q_NOWARN_DEPRECATED_POP
1145+
1146+
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() );
1147+
out = exp.evaluate( &context );
1148+
QCOMPARE( exp.hasEvalError(), evalError );
1149+
QCOMPARE( out.toInt(), result.toInt() );
10201150
}
10211151

10221152
void eval_geometry_method_data()
@@ -1084,12 +1214,25 @@ class TestQgsExpression: public QObject
10841214
QgsExpression exp( string );
10851215
QCOMPARE( exp.hasParserError(), false );
10861216
QCOMPARE( exp.needsGeometry(), needGeom );
1217+
1218+
//deprecated method
1219+
Q_NOWARN_DEPRECATED_PUSH
10871220
QVariant out = exp.evaluate( &f );
10881221
QCOMPARE( exp.hasEvalError(), evalError );
10891222

10901223
QCOMPARE( out.canConvert<QgsGeometry>(), true );
10911224
QgsGeometry outGeom = out.value<QgsGeometry>();
10921225
QVERIFY( compareWkt( outGeom.exportToWkt(), result->exportToWkt() ) );
1226+
Q_NOWARN_DEPRECATED_POP
1227+
1228+
//replacement method
1229+
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() );
1230+
out = exp.evaluate( &context );
1231+
QCOMPARE( exp.hasEvalError(), evalError );
1232+
1233+
QCOMPARE( out.canConvert<QgsGeometry>(), true );
1234+
outGeom = out.value<QgsGeometry>();
1235+
QVERIFY( compareWkt( outGeom.exportToWkt(), result->exportToWkt() ) );
10931236

10941237
delete result;
10951238
}

0 commit comments

Comments
 (0)
Please sign in to comment.