@@ -52,6 +52,7 @@ class TestQgsExpressionContext : public QObject
5252 void cache ();
5353
5454 void valuesAsMap ();
55+ void description ();
5556
5657 private:
5758
@@ -163,6 +164,7 @@ void TestQgsExpressionContext::contextScope()
163164 QVERIFY ( !scope.variable ( " test" ).isValid () );
164165 QCOMPARE ( scope.variableNames ().length (), 0 );
165166 QCOMPARE ( scope.variableCount (), 0 );
167+ QVERIFY ( scope.description ( " test" ).isEmpty () );
166168
167169 scope.setVariable ( QStringLiteral ( " test" ), 5 );
168170 QVERIFY ( scope.hasVariable ( " test" ) );
@@ -171,15 +173,19 @@ void TestQgsExpressionContext::contextScope()
171173 QCOMPARE ( scope.variableNames ().length (), 1 );
172174 QCOMPARE ( scope.variableCount (), 1 );
173175 QCOMPARE ( scope.variableNames ().at ( 0 ), QString ( " test" ) );
176+ QVERIFY ( scope.description ( " test" ).isEmpty () );
174177
175- scope.addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " readonly" ), QStringLiteral ( " readonly_test" ), true ) );
178+ scope.addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " readonly" ), QStringLiteral ( " readonly_test" ), true , false , QStringLiteral ( " readonly variable " ) ) );
176179 QVERIFY ( scope.isReadOnly ( " readonly" ) );
180+ QCOMPARE ( scope.description ( " readonly" ), QStringLiteral ( " readonly variable" ) );
177181 scope.addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " notreadonly" ), QStringLiteral ( " not_readonly_test" ), false ) );
178182 QVERIFY ( !scope.isReadOnly ( " notreadonly" ) );
179183
180184 // updating a read only variable should remain read only
181185 scope.setVariable ( QStringLiteral ( " readonly" ), " newvalue" );
182186 QVERIFY ( scope.isReadOnly ( " readonly" ) );
187+ // and keep its description
188+ QCOMPARE ( scope.description ( " readonly" ), QStringLiteral ( " readonly variable" ) );
183189
184190 // test retrieving filtered variable names
185191 scope.setVariable ( QStringLiteral ( " _hidden_" ), " hidden" );
@@ -245,6 +251,7 @@ void TestQgsExpressionContext::contextStack()
245251 QCOMPARE ( context.scopeCount (), 0 );
246252 QVERIFY ( !context.scope ( 0 ) );
247253 QVERIFY ( !context.lastScope () );
254+ QVERIFY ( context.description ( " test" ).isEmpty () );
248255
249256 // add a scope to the context
250257 QgsExpressionContextScope *testScope = new QgsExpressionContextScope ();
@@ -305,10 +312,13 @@ void TestQgsExpressionContext::contextStack()
305312 QCOMPARE ( scopes.at ( 0 ), scope1 );
306313 QCOMPARE ( scopes.at ( 1 ), scope2 );
307314
315+ QVERIFY ( context.description ( " readonly" ).isEmpty () );
316+
308317 // check isReadOnly
309- scope2->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " readonly" ), 5 , true ) );
318+ scope2->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " readonly" ), 5 , true , false , QStringLiteral ( " readonly variable " ) ) );
310319 QVERIFY ( context.isReadOnly ( " readonly" ) );
311320 QVERIFY ( !context.isReadOnly ( " test" ) );
321+ QCOMPARE ( context.description ( " readonly" ), QStringLiteral ( " readonly variable" ) );
312322
313323 // Check scopes can be popped
314324 delete context.popScope ();
@@ -735,5 +745,20 @@ void TestQgsExpressionContext::valuesAsMap()
735745 QCOMPARE ( m.value ( " v3" ).toString (), QString ( " t3" ) );
736746}
737747
748+ void TestQgsExpressionContext::description ()
749+ {
750+ // test that description falls back to default values if not set
751+ QgsExpressionContext context;
752+ QgsExpressionContextScope *scope = new QgsExpressionContextScope ();
753+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_title" ) ) );
754+ context << scope;
755+ QCOMPARE ( context.description ( " project_title" ), QgsExpression::variableHelpText ( " project_title" ) );
756+ // but if set, use that
757+ scope = new QgsExpressionContextScope ();
758+ scope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " project_title" ), QVariant (), true , true , QStringLiteral ( " my desc" ) ) );
759+ context << scope;
760+ QCOMPARE ( context.description ( " project_title" ), QStringLiteral ( " my desc" ) );
761+ }
762+
738763QGSTEST_MAIN ( TestQgsExpressionContext )
739764#include " testqgsexpressioncontext.moc"
0 commit comments