Skip to content
Permalink
Browse files

[processing] tests for new band parameter

  • Loading branch information
alexbruy committed Aug 4, 2017
1 parent 7c706d2 commit aad566eb312abc3670e3154a907fb5ac0bd5cefd
Showing with 71 additions and 0 deletions.
  1. +71 −0 tests/src/core/testqgsprocessing.cpp
@@ -337,6 +337,7 @@ class TestQgsProcessing: public QObject
void parameterRasterOut();
void parameterFileOut();
void parameterFolderOut();
void parameterBand();
void checkParamValues();
void combineLayerExtent();
void processingFeatureSource();
@@ -3970,6 +3971,76 @@ void TestQgsProcessing::parameterFolderOut()
QCOMPARE( fromCode->defaultValue(), def->defaultValue() );
}

void TestQgsProcessing::parameterBand()
{
QgsProcessingContext context;

// not optional!
std::unique_ptr< QgsProcessingParameterBand > def( new QgsProcessingParameterBand( "non_optional", QString(), QVariant(), QString(), false ) );
QVERIFY( def->checkValueIsAcceptable( 1 ) );
QVERIFY( def->checkValueIsAcceptable( "1" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );

// string representing a band
QVariantMap params;
params.insert( "non_optional", "1" );
int band = QgsProcessingParameters::parameterAsInt( def.get(), params, context );
QCOMPARE( band, 1 );

QCOMPARE( def->valueAsPythonString( 5, context ), QStringLiteral( "5" ) );

QString code = def->asScriptCode();
QCOMPARE( code, QStringLiteral( "##non_optional=band" ) );
std::unique_ptr< QgsProcessingParameterBand > fromCode( dynamic_cast< QgsProcessingParameterBand * >( QgsProcessingParameters::parameterFromScriptCode( code ) ) );
QVERIFY( fromCode.get() );
QCOMPARE( fromCode->name(), def->name() );
QCOMPARE( fromCode->description(), QStringLiteral( "non optional" ) );
QCOMPARE( fromCode->flags(), def->flags() );
QCOMPARE( fromCode->defaultValue(), def->defaultValue() );
QCOMPARE( fromCode->parentLayerParameter(), def->parentLayerParameter() );

QVERIFY( def->dependsOnOtherParameters().isEmpty() );
def->setParentLayerParameter( "my_parent" );
QCOMPARE( def->dependsOnOtherParameters(), QStringList() << QStringLiteral( "my_parent" ) );

code = def->asScriptCode();
fromCode.reset( dynamic_cast< QgsProcessingParameterBand * >( QgsProcessingParameters::parameterFromScriptCode( code ) ) );
QVERIFY( fromCode.get() );
QCOMPARE( fromCode->name(), def->name() );
QCOMPARE( fromCode->description(), QStringLiteral( "non optional" ) );
QCOMPARE( fromCode->flags(), def->flags() );
QCOMPARE( fromCode->defaultValue(), def->defaultValue() );
QCOMPARE( fromCode->parentLayerParameter(), def->parentLayerParameter() );

// optional
def.reset( new QgsProcessingParameterBand( "optional", QString(), 1, QString(), true ) );
QVERIFY( def->checkValueIsAcceptable( 1 ) );
QVERIFY( def->checkValueIsAcceptable( "1" ) );
QVERIFY( def->checkValueIsAcceptable( "" ) );
QVERIFY( def->checkValueIsAcceptable( QVariant() ) );

params.insert( "optional", QVariant() );
band = QgsProcessingParameters::parameterAsInt( def.get(), params, context );
QCOMPARE( band, 1 );

// optional, no default
def.reset( new QgsProcessingParameterBand( "optional", QString(), QVariant(), QString(), true ) );
params.insert( "optional", QVariant() );
band = QgsProcessingParameters::parameterAsInt( def.get(), params, context );
QCOMPARE( band, 0 );

code = def->asScriptCode();
QCOMPARE( code, QStringLiteral( "##optional=optional band" ) );
fromCode.reset( dynamic_cast< QgsProcessingParameterBand * >( QgsProcessingParameters::parameterFromScriptCode( code ) ) );
QVERIFY( fromCode.get() );
QCOMPARE( fromCode->name(), def->name() );
QCOMPARE( fromCode->description(), QStringLiteral( "optional" ) );
QCOMPARE( fromCode->flags(), def->flags() );
QCOMPARE( fromCode->defaultValue(), def->defaultValue() );
QCOMPARE( fromCode->parentLayerParameter(), def->parentLayerParameter() );
}

void TestQgsProcessing::checkParamValues()
{
DummyAlgorithm a( "asd" );

0 comments on commit aad566e

Please sign in to comment.
You can’t perform that action at this time.