@@ -2329,34 +2329,88 @@ void TestQgsProcessing::combineLayerExtent()
23292329
23302330void TestQgsProcessing::processingFeatureSource ()
23312331{
2332- QVariant source ( QStringLiteral ( " test.shp" ) );
2333- QgsProcessingFeatureSourceDefinition fs ( source , true );
2334- QCOMPARE ( fs.source , source );
2332+ QString sourceString = QStringLiteral ( " test.shp" );
2333+ QgsProcessingFeatureSourceDefinition fs ( sourceString , true );
2334+ QCOMPARE ( fs.source . staticValue (). toString (), sourceString );
23352335 QVERIFY ( fs.selectedFeaturesOnly );
23362336
23372337 // test storing QgsProcessingFeatureSource in variant and retrieving
23382338 QVariant fsInVariant = QVariant::fromValue ( fs );
23392339 QVERIFY ( fsInVariant.isValid () );
23402340
23412341 QgsProcessingFeatureSourceDefinition fromVar = qvariant_cast<QgsProcessingFeatureSourceDefinition>( fsInVariant );
2342- QCOMPARE ( fromVar.source , source );
2342+ QCOMPARE ( fromVar.source . staticValue (). toString (), sourceString );
23432343 QVERIFY ( fromVar.selectedFeaturesOnly );
2344+
2345+ // test evaluating parameter as source
2346+ QgsVectorLayer *layer = new QgsVectorLayer ( " Point" , " v1" , " memory" );
2347+ QgsFeature f ( 10001 );
2348+ f.setGeometry ( QgsGeometry ( new QgsPoint ( 1 , 2 ) ) );
2349+ layer->dataProvider ()->addFeatures ( QgsFeatureList () << f );
2350+
2351+ QgsProject p;
2352+ p.addMapLayer ( layer );
2353+ QgsProcessingContext context;
2354+ context.setProject ( &p );
2355+
2356+ // first using static string definition
2357+ QgsProcessingParameterDefinition *def = new QgsProcessingParameterString ( QStringLiteral ( " layer" ) );
2358+ QVariantMap params;
2359+ params.insert ( QStringLiteral ( " layer" ), QgsProcessingFeatureSourceDefinition ( layer->id (), false ) );
2360+ std::unique_ptr< QgsFeatureSource > source ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
2361+ // can't directly match it to layer, so instead just get the feature and test that it matches what we expect
2362+ QgsFeature f2;
2363+ QVERIFY ( source.get () );
2364+ QVERIFY ( source->getFeatures ().nextFeature ( f2 ) );
2365+ QCOMPARE ( f2.geometry (), f.geometry () );
2366+
2367+ // next using property based definition
2368+ params.insert ( QStringLiteral ( " layer" ), QgsProcessingFeatureSourceDefinition ( QgsProperty::fromExpression ( QStringLiteral ( " trim('%1' + ' ')" ).arg ( layer->id () ) ), false ) );
2369+ source.reset ( QgsProcessingParameters::parameterAsSource ( def, params, context ) );
2370+ // can't directly match it to layer, so instead just get the feature and test that it matches what we expect
2371+ QVERIFY ( source.get () );
2372+ QVERIFY ( source->getFeatures ().nextFeature ( f2 ) );
2373+ QCOMPARE ( f2.geometry (), f.geometry () );
23442374}
23452375
23462376void TestQgsProcessing::processingFeatureSink ()
23472377{
2348- QVariant sink ( QStringLiteral ( " test.shp" ) );
2349- QgsProcessingFeatureSink fs ( sink , true );
2350- QCOMPARE ( fs.sink , sink );
2378+ QString sinkString ( QStringLiteral ( " test.shp" ) );
2379+ QgsProcessingFeatureSink fs ( sinkString , true );
2380+ QCOMPARE ( fs.sink . staticValue (). toString (), sinkString );
23512381 QVERIFY ( fs.loadIntoProject );
23522382
23532383 // test storing QgsProcessingFeatureSink in variant and retrieving
23542384 QVariant fsInVariant = QVariant::fromValue ( fs );
23552385 QVERIFY ( fsInVariant.isValid () );
23562386
23572387 QgsProcessingFeatureSink fromVar = qvariant_cast<QgsProcessingFeatureSink>( fsInVariant );
2358- QCOMPARE ( fromVar.sink , sink );
2388+ QCOMPARE ( fromVar.sink . staticValue (). toString (), sinkString );
23592389 QVERIFY ( fromVar.loadIntoProject );
2390+
2391+ // test evaluating parameter as sink
2392+ QgsProject p;
2393+ QgsProcessingContext context;
2394+ context.setProject ( &p );
2395+
2396+ // first using static string definition
2397+ QgsProcessingParameterDefinition *def = new QgsProcessingParameterString ( QStringLiteral ( " layer" ) );
2398+ QVariantMap params;
2399+ params.insert ( QStringLiteral ( " layer" ), QgsProcessingFeatureSink ( " memory:test" , false ) );
2400+ QString dest;
2401+ std::unique_ptr< QgsFeatureSink > sink ( QgsProcessingParameters::parameterAsSink ( def, params, QgsFields (), QgsWkbTypes::Point, QgsCoordinateReferenceSystem ( " EPSG:3111" ), context, dest ) );
2402+ QVERIFY ( sink.get () );
2403+ QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString ( dest, context, false ) );
2404+ QVERIFY ( layer );
2405+ QCOMPARE ( layer->crs ().authid (), QStringLiteral ( " EPSG:3111" ) );
2406+
2407+ // next using property based definition
2408+ params.insert ( QStringLiteral ( " layer" ), QgsProcessingFeatureSink ( QgsProperty::fromExpression ( QStringLiteral ( " trim('memory' + ':test2')" ) ), false ) );
2409+ sink.reset ( QgsProcessingParameters::parameterAsSink ( def, params, QgsFields (), QgsWkbTypes::Point, QgsCoordinateReferenceSystem ( " EPSG:3113" ), context, dest ) );
2410+ QVERIFY ( sink.get () );
2411+ QgsVectorLayer *layer2 = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString ( dest, context, false ) );
2412+ QVERIFY ( layer2 );
2413+ QCOMPARE ( layer2->crs ().authid (), QStringLiteral ( " EPSG:3113" ) );
23602414}
23612415
23622416QGSTEST_MAIN ( TestQgsProcessing )
0 commit comments