25
25
#include < qgsfeaturerequest.h>
26
26
#include < qgsgeometry.h>
27
27
#include < qgsrenderchecker.h>
28
+ #include " qgsexpressioncontext.h"
28
29
29
30
static void _parseAndEvalExpr ( int arg )
30
31
{
@@ -560,21 +561,23 @@ class TestQgsExpression: public QObject
560
561
f.initAttributes ( 3 );
561
562
f.setAttribute ( 2 , QVariant ( 20 ) );
562
563
564
+ QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext ( f, fields );
565
+
563
566
// good exp
564
567
QgsExpression exp ( " foo + 1" );
565
- bool prepareRes = exp.prepare ( fields );
568
+ bool prepareRes = exp.prepare ( &context );
566
569
QCOMPARE ( prepareRes, true );
567
570
QCOMPARE ( exp.hasEvalError (), false );
568
- QVariant res = exp.evaluate ( &f );
571
+ QVariant res = exp.evaluate ( &context );
569
572
QCOMPARE ( res.type (), QVariant::Int );
570
573
QCOMPARE ( res.toInt (), 21 );
571
574
572
575
// bad exp
573
576
QgsExpression exp2 ( " bar + 1" );
574
- bool prepareRes2 = exp2.prepare ( fields );
577
+ bool prepareRes2 = exp2.prepare ( &context );
575
578
QCOMPARE ( prepareRes2, false );
576
579
QCOMPARE ( exp2.hasEvalError (), true );
577
- QVariant res2 = exp2.evaluate ( &f );
580
+ QVariant res2 = exp2.evaluate ( &context );
578
581
QCOMPARE ( res2.type (), QVariant::Invalid );
579
582
}
580
583
@@ -584,9 +587,17 @@ class TestQgsExpression: public QObject
584
587
QVariant v1 = exp.evaluate ();
585
588
QCOMPARE ( v1.toInt (), 1 );
586
589
590
+ Q_NOWARN_DEPRECATED_PUSH
587
591
exp.setCurrentRowNumber ( 100 );
592
+ Q_NOWARN_DEPRECATED_POP
588
593
QVariant v2 = exp.evaluate ();
589
594
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 );
590
601
}
591
602
592
603
void eval_scale ()
@@ -604,17 +615,30 @@ class TestQgsExpression: public QObject
604
615
{
605
616
QgsFeature f ( 100 );
606
617
QgsExpression exp ( " $id * 2" );
618
+ Q_NOWARN_DEPRECATED_PUSH
607
619
QVariant v = exp.evaluate ( &f );
620
+ Q_NOWARN_DEPRECATED_POP
608
621
QCOMPARE ( v.toInt (), 200 );
622
+
623
+ QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext ( f, QgsFields () );
624
+ QVariant v2 = exp.evaluate ( &context );
625
+ QCOMPARE ( v2.toInt (), 200 );
609
626
}
610
627
611
628
void eval_current_feature ()
612
629
{
613
630
QgsFeature f ( 100 );
614
631
QgsExpression exp ( " $currentfeature" );
632
+ Q_NOWARN_DEPRECATED_PUSH
615
633
QVariant v = exp.evaluate ( &f );
634
+ Q_NOWARN_DEPRECATED_POP
616
635
QgsFeature evalFeature = v.value <QgsFeature>();
617
636
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 () );
618
642
}
619
643
620
644
void eval_feature_attribute ()
@@ -627,10 +651,18 @@ class TestQgsExpression: public QObject
627
651
f.setAttribute ( QString ( " col1" ), QString ( " test value" ) );
628
652
f.setAttribute ( QString ( " second_column" ), 5 );
629
653
QgsExpression exp ( " attribute($currentfeature,'col1')" );
654
+ Q_NOWARN_DEPRECATED_PUSH
630
655
QVariant v = exp.evaluate ( &f );
631
656
QCOMPARE ( v.toString (), QString ( " test value" ) );
632
657
QgsExpression exp2 ( " attribute($currentfeature,'second'||'_column')" );
633
658
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 );
634
666
QCOMPARE ( v.toInt (), 5 );
635
667
}
636
668
@@ -759,7 +791,14 @@ class TestQgsExpression: public QObject
759
791
QgsExpression exp ( string );
760
792
QCOMPARE ( exp.hasParserError (), false );
761
793
QCOMPARE ( exp.needsGeometry (), true );
794
+ Q_NOWARN_DEPRECATED_PUSH
762
795
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 );
763
802
QCOMPARE ( exp.hasEvalError (), evalError );
764
803
QCOMPARE ( out.toDouble (), result );
765
804
}
@@ -775,41 +814,77 @@ class TestQgsExpression: public QObject
775
814
fPolyline .setGeometry ( QgsGeometry::fromPolyline ( polyline ) );
776
815
fPolygon .setGeometry ( QgsGeometry::fromPolygon ( polygon ) );
777
816
817
+ QgsExpressionContext context;
818
+
819
+ Q_NOWARN_DEPRECATED_PUSH
778
820
QgsExpression exp1 ( " $area" );
779
821
QVariant vArea = exp1.evaluate ( &fPolygon );
780
822
QCOMPARE ( vArea.toDouble (), 40 . );
781
823
824
+ context.setFeature ( fPolygon );
825
+ vArea = exp1.evaluate ( &context );
826
+ QCOMPARE ( vArea.toDouble (), 40 . );
827
+
782
828
QgsExpression exp2 ( " $length" );
783
829
QVariant vLength = exp2.evaluate ( &fPolyline );
784
830
QCOMPARE ( vLength.toDouble (), 10 . );
785
831
832
+ context.setFeature ( fPolyline );
833
+ vLength = exp2.evaluate ( &context );
834
+ QCOMPARE ( vLength.toDouble (), 10 . );
835
+
786
836
QgsExpression exp3 ( " $perimeter" );
787
837
QVariant vPerimeter = exp3.evaluate ( &fPolygon );
788
838
QCOMPARE ( vPerimeter.toDouble (), 26 . );
789
839
840
+ context.setFeature ( fPolygon );
841
+ vPerimeter = exp3.evaluate ( &context );
842
+ QCOMPARE ( vPerimeter.toDouble (), 26 . );
843
+
790
844
QgsExpression exp4 ( " bounds_width($geometry)" );
791
845
QVariant vBoundsWidth = exp4.evaluate ( &fPolygon );
792
846
QCOMPARE ( vBoundsWidth.toDouble (), 8.0 );
793
847
848
+ vBoundsWidth = exp4.evaluate ( &context );
849
+ QCOMPARE ( vBoundsWidth.toDouble (), 8.0 );
850
+
794
851
QgsExpression exp5 ( " bounds_height($geometry)" );
795
852
QVariant vBoundsHeight = exp5.evaluate ( &fPolygon );
796
853
QCOMPARE ( vBoundsHeight.toDouble (), 5.0 );
797
854
855
+ vBoundsHeight = exp5.evaluate ( &context );
856
+ QCOMPARE ( vBoundsHeight.toDouble (), 5.0 );
857
+
798
858
QgsExpression exp6 ( " xmin($geometry)" );
799
859
QVariant vXMin = exp6.evaluate ( &fPolygon );
800
860
QCOMPARE ( vXMin.toDouble (), 2.0 );
801
861
862
+ vXMin = exp6.evaluate ( &context );
863
+ QCOMPARE ( vXMin.toDouble (), 2.0 );
864
+
802
865
QgsExpression exp7 ( " xmax($geometry)" );
803
866
QVariant vXMax = exp7.evaluate ( &fPolygon );
804
867
QCOMPARE ( vXMax.toDouble (), 10.0 );
805
868
869
+ vXMax = exp7.evaluate ( &context );
870
+ QCOMPARE ( vXMax.toDouble (), 10.0 );
871
+
806
872
QgsExpression exp8 ( " ymin($geometry)" );
807
873
QVariant vYMin = exp8.evaluate ( &fPolygon );
808
874
QCOMPARE ( vYMin.toDouble (), 1.0 );
809
875
876
+ vYMin = exp8.evaluate ( &context );
877
+ QCOMPARE ( vYMin.toDouble (), 1.0 );
878
+
810
879
QgsExpression exp9 ( " ymax($geometry)" );
811
880
QVariant vYMax = exp9.evaluate ( &fPolygon );
812
881
QCOMPARE ( vYMax.toDouble (), 6.0 );
882
+
883
+ exp9.evaluate ( &context );
884
+ QCOMPARE ( vYMax.toDouble (), 6.0 );
885
+
886
+ Q_NOWARN_DEPRECATED_POP
887
+
813
888
}
814
889
815
890
void eval_geometry_wkt ()
@@ -826,21 +901,42 @@ class TestQgsExpression: public QObject
826
901
fPolyline .setGeometry ( QgsGeometry::fromPolyline ( polyline ) );
827
902
fPolygon .setGeometry ( QgsGeometry::fromPolygon ( polygon ) );
828
903
904
+ QgsExpressionContext context;
905
+
906
+ Q_NOWARN_DEPRECATED_PUSH
829
907
QgsExpression exp1 ( " geomToWKT($geometry)" );
830
908
QVariant vWktLine = exp1.evaluate ( &fPolyline );
831
909
QCOMPARE ( vWktLine.toString (), QString ( " LineString (0 0, 10 0)" ) );
832
910
911
+ context.setFeature ( fPolyline );
912
+ vWktLine = exp1.evaluate ( &context );
913
+ QCOMPARE ( vWktLine.toString (), QString ( " LineString (0 0, 10 0)" ) );
914
+
833
915
QgsExpression exp2 ( " geomToWKT($geometry)" );
834
916
QVariant vWktPolygon = exp2.evaluate ( &fPolygon );
835
917
QCOMPARE ( vWktPolygon.toString (), QString ( " Polygon ((2 1, 10 1, 10 6, 2 6, 2 1))" ) );
836
918
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
+
837
923
QgsExpression exp3 ( " geomToWKT($geometry)" );
838
924
QVariant vWktPoint = exp3.evaluate ( &fPoint );
839
925
QCOMPARE ( vWktPoint.toString (), QString ( " Point (-1.23456789 9.87654321)" ) );
840
926
927
+ context.setFeature ( fPoint );
928
+ vWktPoint = exp3.evaluate ( &context );
929
+ QCOMPARE ( vWktPoint.toString (), QString ( " Point (-1.23456789 9.87654321)" ) );
930
+
841
931
QgsExpression exp4 ( " geomToWKT($geometry, 3)" );
842
932
QVariant vWktPointSimplify = exp4.evaluate ( &fPoint );
843
933
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
+
844
940
}
845
941
846
942
void eval_geometry_constructor_data ()
@@ -893,12 +989,25 @@ class TestQgsExpression: public QObject
893
989
QgsExpression exp ( string );
894
990
QCOMPARE ( exp.hasParserError (), false );
895
991
QCOMPARE ( exp.needsGeometry (), false );
992
+
993
+ // deprecated method
994
+ Q_NOWARN_DEPRECATED_PUSH
896
995
QVariant out = exp.evaluate ( &f );
897
996
QCOMPARE ( exp.hasEvalError (), evalError );
898
997
899
998
QCOMPARE ( out.canConvert <QgsGeometry>(), true );
900
999
QgsGeometry outGeom = out.value <QgsGeometry>();
901
1000
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 );
902
1011
}
903
1012
904
1013
void eval_geometry_access_transform_data ()
@@ -955,12 +1064,24 @@ class TestQgsExpression: public QObject
955
1064
QgsExpression exp ( string );
956
1065
QCOMPARE ( exp.hasParserError (), false );
957
1066
QCOMPARE ( exp.needsGeometry (), false );
1067
+
1068
+ // deprecated method
1069
+ Q_NOWARN_DEPRECATED_PUSH
958
1070
QVariant out = exp.evaluate ( &f );
959
1071
QCOMPARE ( exp.hasEvalError (), evalError );
960
1072
961
1073
QCOMPARE ( out.canConvert <QgsGeometry>(), true );
962
1074
QgsGeometry outGeom = out.value <QgsGeometry>();
963
1075
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 );
964
1085
}
965
1086
966
1087
void eval_spatial_operator_data ()
@@ -1014,9 +1135,18 @@ class TestQgsExpression: public QObject
1014
1135
QgsExpression exp ( string );
1015
1136
QCOMPARE ( exp.hasParserError (), false );
1016
1137
QCOMPARE ( exp.needsGeometry (), true );
1138
+
1139
+ // deprecated method
1140
+ Q_NOWARN_DEPRECATED_PUSH
1017
1141
QVariant out = exp.evaluate ( &f );
1018
1142
QCOMPARE ( exp.hasEvalError (), evalError );
1019
1143
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 () );
1020
1150
}
1021
1151
1022
1152
void eval_geometry_method_data ()
@@ -1084,12 +1214,25 @@ class TestQgsExpression: public QObject
1084
1214
QgsExpression exp ( string );
1085
1215
QCOMPARE ( exp.hasParserError (), false );
1086
1216
QCOMPARE ( exp.needsGeometry (), needGeom );
1217
+
1218
+ // deprecated method
1219
+ Q_NOWARN_DEPRECATED_PUSH
1087
1220
QVariant out = exp.evaluate ( &f );
1088
1221
QCOMPARE ( exp.hasEvalError (), evalError );
1089
1222
1090
1223
QCOMPARE ( out.canConvert <QgsGeometry>(), true );
1091
1224
QgsGeometry outGeom = out.value <QgsGeometry>();
1092
1225
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 () ) );
1093
1236
1094
1237
delete result;
1095
1238
}
0 commit comments