Skip to content

Commit 23885e5

Browse files
committedJan 20, 2019
Add tests for native polygonstolines algorithm
1 parent 6c110c8 commit 23885e5

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
 

‎tests/src/analysis/testqgsprocessingalgs.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,21 @@
2929
#include "qgsvectorlayer.h"
3030
#include "qgscategorizedsymbolrenderer.h"
3131
#include "qgssinglesymbolrenderer.h"
32+
#include "qgsmultipolygon.h"
3233

3334
class TestQgsProcessingAlgs: public QObject
3435
{
3536
Q_OBJECT
3637

38+
private:
39+
40+
/**
41+
* Helper function to get a feature based algorithm.
42+
*/
43+
std::unique_ptr<QgsProcessingFeatureBasedAlgorithm> featureBasedAlg( const QString &id );
44+
45+
QgsFeature runForFeature( const std::unique_ptr<QgsProcessingFeatureBasedAlgorithm> &alg, QgsFeature feature, const QString &layerType );
46+
3747
private slots:
3848
void initTestCase();// will be called before the first testfunction is executed.
3949
void cleanupTestCase(); // will be called after the last testfunction was executed.
@@ -48,6 +58,8 @@ class TestQgsProcessingAlgs: public QObject
4858
void kmeansCluster();
4959
void categorizeByStyle();
5060
void extractBinary();
61+
void polygonsToLines_data();
62+
void polygonsToLines();
5163

5264
private:
5365

@@ -57,6 +69,39 @@ class TestQgsProcessingAlgs: public QObject
5769

5870
};
5971

72+
std::unique_ptr<QgsProcessingFeatureBasedAlgorithm> TestQgsProcessingAlgs::featureBasedAlg( const QString &id )
73+
{
74+
return std::unique_ptr<QgsProcessingFeatureBasedAlgorithm>( static_cast<QgsProcessingFeatureBasedAlgorithm *>( QgsApplication::processingRegistry()->createAlgorithmById( id ) ) );
75+
}
76+
77+
QgsFeature TestQgsProcessingAlgs::runForFeature( const std::unique_ptr< QgsProcessingFeatureBasedAlgorithm > &alg, QgsFeature feature, const QString &layerType )
78+
{
79+
std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
80+
QgsProject p;
81+
context->setProject( &p );
82+
83+
QgsProcessingFeedback feedback;
84+
context->setFeedback( &feedback );
85+
86+
QVariantMap parameters;
87+
88+
std::unique_ptr<QgsVectorLayer> inputLayer( qgis::make_unique<QgsVectorLayer>( layerType, QStringLiteral( "layer" ), QStringLiteral( "memory" ) ) );
89+
inputLayer->dataProvider()->addFeature( feature );
90+
91+
parameters.insert( QStringLiteral( "INPUT" ), QVariant::fromValue<QgsMapLayer *>( inputLayer.get() ) );
92+
93+
parameters.insert( QStringLiteral( "OUTPUT" ), QStringLiteral( "memory:" ) );
94+
95+
bool ok = false;
96+
auto res = alg->run( parameters, *context, &feedback, &ok );
97+
QgsFeature result;
98+
99+
std::unique_ptr<QgsVectorLayer> outputLayer( qobject_cast< QgsVectorLayer * >( context->getMapLayer( res.value( QStringLiteral( "OUTPUT" ) ).toString() ) ) );
100+
outputLayer->getFeatures().nextFeature( result );
101+
102+
return result;
103+
}
104+
60105
void TestQgsProcessingAlgs::initTestCase()
61106
{
62107
QgsApplication::init();
@@ -668,5 +713,45 @@ void TestQgsProcessingAlgs::extractBinary()
668713
}
669714

670715

716+
void TestQgsProcessingAlgs::polygonsToLines_data()
717+
{
718+
QTest::addColumn<QgsGeometry>( "sourceGeometry" );
719+
QTest::addColumn<QgsGeometry>( "expectedGeometry" );
720+
721+
QTest::newRow( "Simple Polygon" )
722+
<< QgsGeometry::fromWkt( "Polygon((1 1, 2 2, 1 3, 1 1))" )
723+
<< QgsGeometry::fromWkt( "MultiLineString ((1 1, 2 2, 1 3, 1 1))" );
724+
725+
QgsGeometry geomNoRing( qgis::make_unique<QgsMultiPolygon>() );
726+
727+
QTest::newRow( "Polygon without exterior ring" )
728+
<< geomNoRing
729+
<< QgsGeometry::fromWkt( "MultiLineString ()" );
730+
731+
QTest::newRow( "MultiPolygon" )
732+
<< QgsGeometry::fromWkt( "MultiPolygon(((1 1, 2 2, 1 3, 1 1)), ((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 3 6, 6 6, 6 3, 3 3)))" )
733+
<< QgsGeometry::fromWkt( "MultiLineString ((1 1, 2 2, 1 3, 1 1),(0 0, 0 10, 10 10, 10 0, 0 0),(3 3, 3 6, 6 6, 6 3, 3 3))" );
734+
735+
QTest::newRow( "Polygon with inner ring" )
736+
<< QgsGeometry::fromWkt( "Polygon((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 3 6, 6 6, 6 3, 3 3))" )
737+
<< QgsGeometry::fromWkt( "MultiLineString ((0 0, 0 10, 10 10, 10 0, 0 0),(3 3, 3 6, 6 6, 6 3, 3 3))" );
738+
}
739+
740+
741+
void TestQgsProcessingAlgs::polygonsToLines()
742+
{
743+
QFETCH( QgsGeometry, sourceGeometry );
744+
QFETCH( QgsGeometry, expectedGeometry );
745+
746+
std::unique_ptr< QgsProcessingFeatureBasedAlgorithm > alg( featureBasedAlg( "native:polygonstolines" ) );
747+
748+
QgsFeature feature;
749+
feature.setGeometry( sourceGeometry );
750+
751+
QgsFeature result = runForFeature( alg, feature, QStringLiteral( "Polygon" ) );
752+
753+
QVERIFY2( result.geometry().equals( expectedGeometry ), QStringLiteral( "Result: %1, Expected: %2" ).arg( result.geometry().asWkt(), expectedGeometry.asWkt() ).toUtf8().constData() );
754+
}
755+
671756
QGSTEST_MAIN( TestQgsProcessingAlgs )
672757
#include "testqgsprocessingalgs.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.