Skip to content

Commit 06229c2

Browse files
committed
Add some tests for QgsGeometry conversion to/from QVariant
1 parent 5b2b3a4 commit 06229c2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/src/core/testqgsgeometry.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class TestQgsGeometry : public QObject
5353

5454
void copy();
5555
void assignment();
56+
void asVariant(); //test conversion to and from a QVariant
5657

5758
void fromQgsPoint();
5859
void fromQPoint();
@@ -256,6 +257,35 @@ void TestQgsGeometry::assignment()
256257
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
257258
}
258259

260+
void TestQgsGeometry::asVariant()
261+
{
262+
//create a point geometry
263+
QgsGeometry original( new QgsPointV2( 1.0, 2.0 ) );
264+
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
265+
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
266+
267+
//convert to and from a QVariant
268+
QVariant var = QVariant::fromValue( original );
269+
QVERIFY( var.isValid() );
270+
271+
QgsGeometry fromVar = qvariant_cast<QgsGeometry>( var );
272+
QCOMPARE( fromVar.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
273+
QCOMPARE( fromVar.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
274+
275+
//also check copying variant
276+
QVariant var2 = var;
277+
QVERIFY( var2.isValid() );
278+
QgsGeometry fromVar2 = qvariant_cast<QgsGeometry>( var2 );
279+
QCOMPARE( fromVar2.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
280+
QCOMPARE( fromVar2.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
281+
282+
//modify original and check detachment
283+
original.setGeometry( new QgsPointV2( 3.0, 4.0 ) );
284+
QgsGeometry fromVar3 = qvariant_cast<QgsGeometry>( var );
285+
QCOMPARE( fromVar3.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
286+
QCOMPARE( fromVar3.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
287+
}
288+
259289
void TestQgsGeometry::fromQgsPoint()
260290
{
261291
QgsPoint point( 1.0, 2.0 );

0 commit comments

Comments
 (0)