Skip to content

Commit

Permalink
Add some more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 6, 2017
1 parent f12bb74 commit f1313af
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/core/geometry/qgsrectangle.sip
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ Copy constructor
:rtype: QgsBox3d
%End

operator QVariant() const;
%Docstring
Allows direct construction of QVariants from rectangles.
%End

};


Expand Down
10 changes: 10 additions & 0 deletions python/core/geometry/qgsreferencedgeometry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class QgsReferencedRectangle : QgsRectangle, QgsReferencedGeometryBase
Constructor for QgsReferencedRectangle.
%End

operator QVariant() const;
%Docstring
Allows direct construction of QVariants from rectangle.
%End

};


Expand All @@ -98,6 +103,11 @@ class QgsReferencedPointXY : QgsPointXY, QgsReferencedGeometryBase
Constructor for QgsReferencedPointXY.
%End

operator QVariant() const;
%Docstring
Allows direct construction of QVariants from point.
%End

};


Expand Down
5 changes: 5 additions & 0 deletions python/core/qgspointxy.sip
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ Divides the coordinates in this point by a scalar quantity in place
:rtype: QgsPointXY
%End

operator QVariant() const;
%Docstring
Allows direct construction of QVariants from points.
%End

SIP_PYOBJECT __repr__();
%MethodCode
QString str = "(" + QString::number( sipCpp->x() ) + "," + QString::number( sipCpp->y() ) + ")";
Expand Down
6 changes: 6 additions & 0 deletions src/core/geometry/qgsrectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ class CORE_EXPORT QgsRectangle
*/
QgsBox3d toBox3d( double zMin, double zMax ) const;

//! Allows direct construction of QVariants from rectangles.
operator QVariant() const
{
return QVariant::fromValue( *this );
}

private:

double mXmin;
Expand Down
12 changes: 12 additions & 0 deletions src/core/geometry/qgsreferencedgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class CORE_EXPORT QgsReferencedRectangle : public QgsRectangle, public QgsRefere
*/
QgsReferencedRectangle() = default;

//! Allows direct construction of QVariants from rectangle.
operator QVariant() const
{
return QVariant::fromValue( *this );
}

};

Q_DECLARE_METATYPE( QgsReferencedRectangle )
Expand All @@ -109,6 +115,12 @@ class CORE_EXPORT QgsReferencedPointXY : public QgsPointXY, public QgsReferenced
*/
QgsReferencedPointXY() = default;

//! Allows direct construction of QVariants from point.
operator QVariant() const
{
return QVariant::fromValue( *this );
}

};

Q_DECLARE_METATYPE( QgsReferencedPointXY )
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgspointxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ class CORE_EXPORT QgsPointXY
//! Divides the coordinates in this point by a scalar quantity in place
QgsPointXY &operator/=( double scalar ) { mX /= scalar; mY /= scalar; return *this; }

//! Allows direct construction of QVariants from points.
operator QVariant() const
{
return QVariant::fromValue( *this );
}

#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
Expand Down
40 changes: 40 additions & 0 deletions tests/src/core/testqgspoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <qgsgeometry.h>
//header for class being tested
#include <qgspoint.h>
#include "qgsreferencedgeometry.h"

class TestQgsPointXY: public QObject
{
Expand All @@ -51,6 +52,8 @@ class TestQgsPointXY: public QObject
void compare();
void project();
void vector(); //tests for QgsVector
void asVariant();
void referenced();

private:
QgsPointXY mPoint1;
Expand Down Expand Up @@ -760,5 +763,42 @@ void TestQgsPointXY::vector()
QCOMPARE( v1.y(), 3.0 );
}

void TestQgsPointXY::asVariant()
{
QgsPointXY p1 = QgsPointXY( 10.0, 20.0 );

//convert to and from a QVariant
QVariant var = QVariant::fromValue( p1 );
QVERIFY( var.isValid() );
QVERIFY( var.canConvert< QgsPointXY >() );
QVERIFY( !var.canConvert< QgsReferencedPointXY >() );

QgsPointXY p2 = qvariant_cast<QgsPointXY>( var );
QCOMPARE( p2.x(), p1.x() );
QCOMPARE( p2.y(), p1.y() );
}

void TestQgsPointXY::referenced()
{
QgsReferencedPointXY p1 = QgsReferencedPointXY( QgsPointXY( 10.0, 20.0 ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) );
QCOMPARE( p1.crs().authid(), QStringLiteral( "EPSG:3111" ) );
p1.setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:28356" ) ) );
QCOMPARE( p1.crs().authid(), QStringLiteral( "EPSG:28356" ) );

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

// not great - we'd ideally like this to pass, but it doesn't:
// QVERIFY( !var.canConvert< QgsPointXY >() );

QVERIFY( var.canConvert< QgsReferencedPointXY >() );

QgsReferencedPointXY p2 = qvariant_cast<QgsReferencedPointXY>( var );
QCOMPARE( p2.x(), p1.x() );
QCOMPARE( p2.y(), p1.y() );
QCOMPARE( p2.crs().authid(), QStringLiteral( "EPSG:28356" ) );
}

QGSTEST_MAIN( TestQgsPointXY )
#include "testqgspoint.moc"
44 changes: 44 additions & 0 deletions tests/src/core/testqgsrectangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <qgsrectangle.h>
#include <qgspoint.h>
#include "qgslogger.h"
#include "qgsreferencedgeometry.h"

class TestQgsRectangle: public QObject
{
Expand All @@ -27,6 +28,8 @@ class TestQgsRectangle: public QObject
void manipulate();
void regression6194();
void operators();
void asVariant();
void referenced();
};

void TestQgsRectangle::manipulate()
Expand Down Expand Up @@ -111,5 +114,46 @@ void TestQgsRectangle::operators()
QCOMPARE( rect2.width(), rect1.width() );
}

void TestQgsRectangle::asVariant()
{
QgsRectangle rect1 = QgsRectangle( 10.0, 20.0, 110.0, 220.0 );

//convert to and from a QVariant
QVariant var = QVariant::fromValue( rect1 );
QVERIFY( var.isValid() );
QVERIFY( var.canConvert< QgsRectangle >() );
QVERIFY( !var.canConvert< QgsReferencedRectangle >() );

QgsRectangle rect2 = qvariant_cast<QgsRectangle>( var );
QCOMPARE( rect2.xMinimum(), rect1.xMinimum() );
QCOMPARE( rect2.yMinimum(), rect1.yMinimum() );
QCOMPARE( rect2.height(), rect1.height() );
QCOMPARE( rect2.width(), rect1.width() );
}

void TestQgsRectangle::referenced()
{
QgsReferencedRectangle rect1 = QgsReferencedRectangle( QgsRectangle( 10.0, 20.0, 110.0, 220.0 ), QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ) );
QCOMPARE( rect1.crs().authid(), QStringLiteral( "EPSG:3111" ) );
rect1.setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:28356" ) ) );
QCOMPARE( rect1.crs().authid(), QStringLiteral( "EPSG:28356" ) );

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

// not great - we'd ideally like this to pass, but it doesn't:
// QVERIFY( !var.canConvert< QgsRectangle >() );

QVERIFY( var.canConvert< QgsReferencedRectangle >() );

QgsReferencedRectangle rect2 = qvariant_cast<QgsReferencedRectangle>( var );
QCOMPARE( rect2.xMinimum(), rect1.xMinimum() );
QCOMPARE( rect2.yMinimum(), rect1.yMinimum() );
QCOMPARE( rect2.height(), rect1.height() );
QCOMPARE( rect2.width(), rect1.width() );
QCOMPARE( rect2.crs().authid(), QStringLiteral( "EPSG:28356" ) );
}

QGSTEST_MAIN( TestQgsRectangle )
#include "testqgsrectangle.moc"

0 comments on commit f1313af

Please sign in to comment.