@@ -66,6 +66,16 @@ class TestQgsExpression: public QObject
6666 QTest::newRow ( " arithmetics" ) << " 1+2*3" << true ;
6767 QTest::newRow ( " logic" ) << " be or not be" << true ;
6868
69+ QTest::newRow ( " conditions +1" ) << " case when x then y end" << true ;
70+ QTest::newRow ( " conditions +2" ) << " case when x then y else z end" << true ;
71+ QTest::newRow ( " conditions +3" ) << " case when x then y when a then b end" << true ;
72+ QTest::newRow ( " conditions +4" ) << " case when x then y when a then b else z end" << true ;
73+
74+ QTest::newRow ( " conditions -1" ) << " case end" << false ;
75+ QTest::newRow ( " conditions -2" ) << " when x then y" << false ;
76+ QTest::newRow ( " conditions -3" ) << " case" << false ;
77+ QTest::newRow ( " conditions -4" ) << " case when x y end" << false ;
78+ QTest::newRow ( " conditions -5" ) << " case y end" << false ;
6979 }
7080 void parsing ()
7181 {
@@ -235,6 +245,12 @@ class TestQgsExpression: public QObject
235245 QTest::newRow ( " implicit text->int" ) << " '5'+2" << false << QVariant ( 7 );
236246 QTest::newRow ( " implicit text->double" ) << " '5.1'+2" << false << QVariant ( 7.1 );
237247 QTest::newRow ( " implicit text->bool" ) << " '0.1' or 0" << false << QVariant ( 1 );
248+
249+ // conditions (without base expression, i.e. CASE WHEN ... THEN ... END)
250+ QTest::newRow ( " condition when" ) << " case when 2>1 then 'good' end" << false << QVariant ( " good" );
251+ QTest::newRow ( " condition else" ) << " case when 1=0 then 'bad' else 678 end" << false << QVariant ( 678 );
252+ QTest::newRow ( " condition null" ) << " case when length(123)=0 then 111 end" << false << QVariant ();
253+ QTest::newRow ( " condition 2 when" ) << " case when 2>3 then 23 when 3>2 then 32 else 0 end" << false << QVariant ( 32 );
238254 }
239255
240256 void evaluation ()
@@ -323,8 +339,8 @@ class TestQgsExpression: public QObject
323339 void referenced_columns ()
324340 {
325341 QSet<QString> expectedCols;
326- expectedCols << " foo" << " bar" ;
327- QgsExpression exp ( " length(Bar || FOO) = 4 or foo + sqrt(bar) > 0" );
342+ expectedCols << " foo" << " bar" << " ppp " << " qqq " << " rrr " ;
343+ QgsExpression exp ( " length(Bar || FOO) = 4 or foo + sqrt(bar) > 0 or case when ppp then qqq else rrr end " );
328344 QCOMPARE ( exp.hasParserError (), false );
329345 QStringList refCols = exp.referencedColumns ();
330346 // make sure we have lower case
@@ -351,6 +367,10 @@ class TestQgsExpression: public QObject
351367 QTest::newRow ( " $perimeter" ) << " $perimeter" << true ;
352368 QTest::newRow ( " toint($perimeter)" ) << " toint($perimeter)" << true ;
353369 QTest::newRow ( " toint(123)" ) << " toint(123)" << false ;
370+ QTest::newRow ( " case 0" ) << " case when 1 then 0 end" << false ;
371+ QTest::newRow ( " case 1" ) << " case when $area > 0 then 1 end" << true ;
372+ QTest::newRow ( " case 2" ) << " case when 1 then $area end" << true ;
373+ QTest::newRow ( " case 3" ) << " case when 1 then 0 else $area end" << true ;
354374 }
355375
356376 void needs_geometry ()
@@ -359,6 +379,8 @@ class TestQgsExpression: public QObject
359379 QFETCH ( bool , needsGeom );
360380
361381 QgsExpression exp ( string );
382+ if ( exp.hasParserError () )
383+ qDebug () << " parser error! " << exp.parserErrorString ();
362384 QCOMPARE ( exp.hasParserError (), false );
363385 QCOMPARE ( exp.needsGeometry (), needsGeom );
364386 }
0 commit comments