Skip to content
Permalink
Browse files

Add some more unit tests

  • Loading branch information
nyalldawson committed Aug 31, 2017
1 parent f12bb74 commit f1313af9142069d63fd3f74a6daca6179e560dc2
@@ -317,6 +317,11 @@ Copy constructor
:rtype: QgsBox3d
%End

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

};


@@ -72,6 +72,11 @@ class QgsReferencedRectangle : QgsRectangle, QgsReferencedGeometryBase
Constructor for QgsReferencedRectangle.
%End

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

};


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

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

};


@@ -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() ) + ")";
@@ -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;
@@ -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 )
@@ -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 )
@@ -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
@@ -25,6 +25,7 @@
#include <qgsgeometry.h>
//header for class being tested
#include <qgspoint.h>
#include "qgsreferencedgeometry.h"

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

private:
QgsPointXY mPoint1;
@@ -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"
@@ -19,6 +19,7 @@
#include <qgsrectangle.h>
#include <qgspoint.h>
#include "qgslogger.h"
#include "qgsreferencedgeometry.h"

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

void TestQgsRectangle::manipulate()
@@ -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.
You can’t perform that action at this time.