@@ -337,6 +337,7 @@ class TestQgsProcessing: public QObject
337
337
void parameterRasterOut ();
338
338
void parameterFileOut ();
339
339
void parameterFolderOut ();
340
+ void parameterBand ();
340
341
void checkParamValues ();
341
342
void combineLayerExtent ();
342
343
void processingFeatureSource ();
@@ -3970,6 +3971,76 @@ void TestQgsProcessing::parameterFolderOut()
3970
3971
QCOMPARE ( fromCode->defaultValue (), def->defaultValue () );
3971
3972
}
3972
3973
3974
+ void TestQgsProcessing::parameterBand ()
3975
+ {
3976
+ QgsProcessingContext context;
3977
+
3978
+ // not optional!
3979
+ std::unique_ptr< QgsProcessingParameterBand > def ( new QgsProcessingParameterBand ( " non_optional" , QString (), QVariant (), QString (), false ) );
3980
+ QVERIFY ( def->checkValueIsAcceptable ( 1 ) );
3981
+ QVERIFY ( def->checkValueIsAcceptable ( " 1" ) );
3982
+ QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
3983
+ QVERIFY ( !def->checkValueIsAcceptable ( QVariant () ) );
3984
+
3985
+ // string representing a band
3986
+ QVariantMap params;
3987
+ params.insert ( " non_optional" , " 1" );
3988
+ int band = QgsProcessingParameters::parameterAsInt ( def.get (), params, context );
3989
+ QCOMPARE ( band, 1 );
3990
+
3991
+ QCOMPARE ( def->valueAsPythonString ( 5 , context ), QStringLiteral ( " 5" ) );
3992
+
3993
+ QString code = def->asScriptCode ();
3994
+ QCOMPARE ( code, QStringLiteral ( " ##non_optional=band" ) );
3995
+ std::unique_ptr< QgsProcessingParameterBand > fromCode ( dynamic_cast < QgsProcessingParameterBand * >( QgsProcessingParameters::parameterFromScriptCode ( code ) ) );
3996
+ QVERIFY ( fromCode.get () );
3997
+ QCOMPARE ( fromCode->name (), def->name () );
3998
+ QCOMPARE ( fromCode->description (), QStringLiteral ( " non optional" ) );
3999
+ QCOMPARE ( fromCode->flags (), def->flags () );
4000
+ QCOMPARE ( fromCode->defaultValue (), def->defaultValue () );
4001
+ QCOMPARE ( fromCode->parentLayerParameter (), def->parentLayerParameter () );
4002
+
4003
+ QVERIFY ( def->dependsOnOtherParameters ().isEmpty () );
4004
+ def->setParentLayerParameter ( " my_parent" );
4005
+ QCOMPARE ( def->dependsOnOtherParameters (), QStringList () << QStringLiteral ( " my_parent" ) );
4006
+
4007
+ code = def->asScriptCode ();
4008
+ fromCode.reset ( dynamic_cast < QgsProcessingParameterBand * >( QgsProcessingParameters::parameterFromScriptCode ( code ) ) );
4009
+ QVERIFY ( fromCode.get () );
4010
+ QCOMPARE ( fromCode->name (), def->name () );
4011
+ QCOMPARE ( fromCode->description (), QStringLiteral ( " non optional" ) );
4012
+ QCOMPARE ( fromCode->flags (), def->flags () );
4013
+ QCOMPARE ( fromCode->defaultValue (), def->defaultValue () );
4014
+ QCOMPARE ( fromCode->parentLayerParameter (), def->parentLayerParameter () );
4015
+
4016
+ // optional
4017
+ def.reset ( new QgsProcessingParameterBand ( " optional" , QString (), 1 , QString (), true ) );
4018
+ QVERIFY ( def->checkValueIsAcceptable ( 1 ) );
4019
+ QVERIFY ( def->checkValueIsAcceptable ( " 1" ) );
4020
+ QVERIFY ( def->checkValueIsAcceptable ( " " ) );
4021
+ QVERIFY ( def->checkValueIsAcceptable ( QVariant () ) );
4022
+
4023
+ params.insert ( " optional" , QVariant () );
4024
+ band = QgsProcessingParameters::parameterAsInt ( def.get (), params, context );
4025
+ QCOMPARE ( band, 1 );
4026
+
4027
+ // optional, no default
4028
+ def.reset ( new QgsProcessingParameterBand ( " optional" , QString (), QVariant (), QString (), true ) );
4029
+ params.insert ( " optional" , QVariant () );
4030
+ band = QgsProcessingParameters::parameterAsInt ( def.get (), params, context );
4031
+ QCOMPARE ( band, 0 );
4032
+
4033
+ code = def->asScriptCode ();
4034
+ QCOMPARE ( code, QStringLiteral ( " ##optional=optional band" ) );
4035
+ fromCode.reset ( dynamic_cast < QgsProcessingParameterBand * >( QgsProcessingParameters::parameterFromScriptCode ( code ) ) );
4036
+ QVERIFY ( fromCode.get () );
4037
+ QCOMPARE ( fromCode->name (), def->name () );
4038
+ QCOMPARE ( fromCode->description (), QStringLiteral ( " optional" ) );
4039
+ QCOMPARE ( fromCode->flags (), def->flags () );
4040
+ QCOMPARE ( fromCode->defaultValue (), def->defaultValue () );
4041
+ QCOMPARE ( fromCode->parentLayerParameter (), def->parentLayerParameter () );
4042
+ }
4043
+
3973
4044
void TestQgsProcessing::checkParamValues ()
3974
4045
{
3975
4046
DummyAlgorithm a ( " asd" );
0 commit comments