Skip to content
Permalink
Browse files

Add some tests for QgsGeometry conversion to/from QVariant

  • Loading branch information
nyalldawson committed Jun 12, 2015
1 parent 5b2b3a4 commit 06229c288c16e92d8b36292584d9a4cc3d0a0c0d
Showing with 30 additions and 0 deletions.
  1. +30 −0 tests/src/core/testqgsgeometry.cpp
@@ -53,6 +53,7 @@ class TestQgsGeometry : public QObject

void copy();
void assignment();
void asVariant(); //test conversion to and from a QVariant

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

void TestQgsGeometry::asVariant()
{
//create a point geometry
QgsGeometry original( new QgsPointV2( 1.0, 2.0 ) );
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );

//convert to and from a QVariant
QVariant var = QVariant::fromValue( original );
QVERIFY( var.isValid() );

QgsGeometry fromVar = qvariant_cast<QgsGeometry>( var );
QCOMPARE( fromVar.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
QCOMPARE( fromVar.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );

//also check copying variant
QVariant var2 = var;
QVERIFY( var2.isValid() );
QgsGeometry fromVar2 = qvariant_cast<QgsGeometry>( var2 );
QCOMPARE( fromVar2.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
QCOMPARE( fromVar2.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );

//modify original and check detachment
original.setGeometry( new QgsPointV2( 3.0, 4.0 ) );
QgsGeometry fromVar3 = qvariant_cast<QgsGeometry>( var );
QCOMPARE( fromVar3.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
QCOMPARE( fromVar3.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
}

void TestQgsGeometry::fromQgsPoint()
{
QgsPoint point( 1.0, 2.0 );

0 comments on commit 06229c2

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