PDA

View Full Version : QTestlib - How to add own types



AlGaN
14th October 2010, 16:08
Hello,

I need to add (unit) tests to my app and found the QTestlib API, integrating in Qt.

After reading the tutorials I started to implement a first test with a simple type used in my app: Point2D:



#ifndef POINT2D
#define POINT2D

#ifdef BUILD_GEOALG
#define EXPORT_GEOALG Q_DECL_EXPORT
#else
#define EXPORT_GEOALG
#endif

#include <iostream>
#include <QPointF>
#include "common.hpp"
#include "Vec2D.hpp"

namespace GeoAlg
{
//! \class Point2D
//!
//! \brief
//! Class representing a simple geometric point in 2D
// Scalar is a typedef for double

class EXPORT_GEOALG Point2D
{

friend EXPORT_GEOALG std::ostream& operator<<(std::ostream& out, const Point2D& p);
friend EXPORT_GEOALG const Point2D operator*(Scalar s, const Point2D& p);
friend EXPORT_GEOALG const Point2D operator*(const Point2D& p, Scalar
friend EXPORT_GEOALG const Point2D operator/(const Point2D& p, Scalar s);
friend EXPORT_GEOALG const Point2D operator+(const Point2D& a, const Point2D& b)
friend EXPORT_GEOALG const Vec2D operator-(const Point2D& a, const Point2D& b);
friend EXPORT_GEOALG const Point2D asum(int, Scalar[], Point2D[]);


friend EXPORT_GEOALG Scalar d(const Point2D& a, const Point2D& b);


friend EXPORT_GEOALG Scalar d2(const Point2D& a, const Point2D& b);

private:
Scalar _x;
Scalar _y;
PointError mError;

public:
Point2D(Scalar x = 0.0, Scalar y = 0.0);
Point2D(const QPointF& p);

Scalar x() const
{
return _x;
}

Scalar y() const
{
return _y;
}

void setX(Scalar x)
{
_x = x;
}

void setY(Scalar y)
{
_y = y;
}

Vec2D toVec2D() const { return Vec2D(x(), y()); }

const Point2D operator+(const Vec2D& v) const;

Point2D& operator+=(const Vec2D& v);

const Point2D operator-(const Vec2D& v) const;

Point2D& operator-=(const Vec2D& v);

//! test for equality
bool operator==(const Point2D& p) const;

//! test for inequality
bool operator!=(const Point2D& p) const;


void clError()
{
mError = PE_NoError;
}


PointError error() const
{
return mError;
}


const char* errorString() const;


QPointF toQPointF() const;


bool isNull() const;
};
}
#endif // POINT2D_HPP


So I created a simple test:


#ifndef TESTPOINT2D_HPP
#define TESTPOINT2D_HPP

// $Rev$
// $Date$
// $Author$
// $HeadURL$

#include <QtTest/QtTest>
#include "../../src/common/geoalg/Point2D.hpp"

class TestPoint2D : public QObject
{
Q_OBJECT

private slots:
void compareCoords_data();
void compareCoords();

// void add_data();
// void add();
};
#endif // TESTPOINT2D_HPP




#include "TestPoint2D.hpp"

using namespace GeoAlg;

void TestPoint2D::compareCoords_data()
{
QTest::addColumn<Scalar>("pointCoord");
QTest::addColumn<Scalar>("result");

QTest::newRow("simple x") << Point2D(1,1).x() << 1;
QTest::newRow("simple y") << Point2D(1,1).y() << 1;
QTest::newRow("null x") << Point2D(0,0).x() << 0;
QTest::newRow("null y") << Point2D(0,0).y() << 0;
QTest::newRow("neg. x") << Point2D(-5, -8).x() << -5;
QTest::newRow("neg. y") << Point2D(-5, -8).y() << -8;
QTest::newRow("decimal x") << Point2D(4.567, -8.901) << 4.567;
QTest::newRow("decimal y") << Point2D(4.567, -8.901) << -8.901;

}

void TestPoint2D::compareCoords()
{
QFETCH(Scalar, pointCoord);
QFETCH(Scalar, result);

QCOMPARE(pointCoord, result);
}

QTEST_MAIN(TestPoint2D)


While compiling I get an error that is related to Qt's type system (I suppose):


D:\projekt\work\Tracker\b1tracker\test\GeoAlg>nmake

Microsoft (R) Program Maintenance Utility Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.

"C:\Programme\Microsoft Visual Studio 9.0\VC\BIN\nmake.exe" -f Makefile.
Debug

Microsoft (R) Program Maintenance Utility Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.

D:\Qt\4.5.3\bin\moc.exe -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -D_USE_
MATH_DEFINES -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -I"d:\Qt\4.
5.3\include\QtCore" -I"d:\Qt\4.5.3\include\QtGui" -I"d:\Qt\4.5.3\include" -I"d:\
Qt\4.5.3\include\QtTest" -I"." -I"d:\Qt\4.5.3\include\ActiveQt" -I"debug" -I"d:\
Qt\4.5.3\mkspecs\win32-msvc2008" -D_MSC_VER=1500 -DWIN32 TestPoint2D.hpp -o debu
g\moc_TestPoint2D.cpp
cl -c -nologo -Zm200 -Zc:wchar_t- -Zi -MDd -GR -EHsc -W3 -w34100 -w34189
-DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -D_USE_MATH_DEFINES -DQT_DLL -DQT_GUI_
LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -I"d:\Qt\4.5.3\include\QtCore" -I"d:\Qt\4.
5.3\include\QtGui" -I"d:\Qt\4.5.3\include" -I"d:\Qt\4.5.3\include\QtTest" -I"."
-I"d:\Qt\4.5.3\include\ActiveQt" -I"debug" -I"d:\Qt\4.5.3\mkspecs\win32-msvc2008
" -Fodebug\ @C:\Users\kleylz\AppData\Local\Temp\nm70F3.tmp
TestPoint2D.cpp
d:\qt\4.5.3\include\qtcore\../../src/corelib/kernel/qmetatype.h(189) : error C20
39: 'qt_metatype_id' : is not a member of 'QMetaTypeId<T>'
with
[
T=GeoAlg::Point2D
]
d:\qt\4.5.3\include\qtcore\../../src/corelib/kernel/qmetatype.h(189) : w
hile compiling class template member function 'int QMetaTypeId2<T>::qt_metatype_
id(void)'
with
[
T=GeoAlg::Point2D
]
d:\qt\4.5.3\include\qtcore\../../src/corelib/kernel/qmetatype.h(199) : s
ee reference to class template instantiation 'QMetaTypeId2<T>' being compiled
with
[
T=GeoAlg::Point2D
]
d:\qt\4.5.3\include\qttest\../../src/testlib/qtestdata.h(82) : see refer
ence to function template instantiation 'int qMetaTypeId<T>(T *)' being compiled

with
[
T=GeoAlg::Point2D
]
.\TestPoint2D.cpp(22) : see reference to function template instantiation
'QTestData &operator <<<GeoAlg::Point2D>(QTestData &,const T &)' being compiled

with
[
T=GeoAlg::Point2D
]
NMAKE : fatal error U1077: '"C:\Programme\Microsoft Visual Studio 9.0\VC\BIN\cl.
EXE"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Programme\Microsoft Visual Studio 9.0\VC\BIN\nma
ke.exe"' : return code '0x2'


So my question is: Is there a way to integrate/test own types with macro QCOMPARE?

Thanks for any hints

wysota
14th October 2010, 19:22
If you want to use your own datatypes with QVariant, you need to register them properly. It's explained in QVariant docs.