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:

Qt Code:
  1. #ifndef POINT2D
  2. #define POINT2D
  3.  
  4. #ifdef BUILD_GEOALG
  5. #define EXPORT_GEOALG Q_DECL_EXPORT
  6. #else
  7. #define EXPORT_GEOALG
  8. #endif
  9.  
  10. #include <iostream>
  11. #include <QPointF>
  12. #include "common.hpp"
  13. #include "Vec2D.hpp"
  14.  
  15. namespace GeoAlg
  16. {
  17. //! \class Point2D
  18. //!
  19. //! \brief
  20. //! Class representing a simple geometric point in 2D
  21. // Scalar is a typedef for double
  22.  
  23. class EXPORT_GEOALG Point2D
  24. {
  25.  
  26. friend EXPORT_GEOALG std::ostream& operator<<(std::ostream& out, const Point2D& p);
  27. friend EXPORT_GEOALG const Point2D operator*(Scalar s, const Point2D& p);
  28. friend EXPORT_GEOALG const Point2D operator*(const Point2D& p, Scalar
  29. friend EXPORT_GEOALG const Point2D operator/(const Point2D& p, Scalar s);
  30. friend EXPORT_GEOALG const Point2D operator+(const Point2D& a, const Point2D& b)
  31. friend EXPORT_GEOALG const Vec2D operator-(const Point2D& a, const Point2D& b);
  32. friend EXPORT_GEOALG const Point2D asum(int, Scalar[], Point2D[]);
  33.  
  34.  
  35. friend EXPORT_GEOALG Scalar d(const Point2D& a, const Point2D& b);
  36.  
  37.  
  38. friend EXPORT_GEOALG Scalar d2(const Point2D& a, const Point2D& b);
  39.  
  40. private:
  41. Scalar _x;
  42. Scalar _y;
  43. PointError mError;
  44.  
  45. public:
  46. Point2D(Scalar x = 0.0, Scalar y = 0.0);
  47. Point2D(const QPointF& p);
  48.  
  49. Scalar x() const
  50. {
  51. return _x;
  52. }
  53.  
  54. Scalar y() const
  55. {
  56. return _y;
  57. }
  58.  
  59. void setX(Scalar x)
  60. {
  61. _x = x;
  62. }
  63.  
  64. void setY(Scalar y)
  65. {
  66. _y = y;
  67. }
  68.  
  69. Vec2D toVec2D() const { return Vec2D(x(), y()); }
  70.  
  71. const Point2D operator+(const Vec2D& v) const;
  72.  
  73. Point2D& operator+=(const Vec2D& v);
  74.  
  75. const Point2D operator-(const Vec2D& v) const;
  76.  
  77. Point2D& operator-=(const Vec2D& v);
  78.  
  79. //! test for equality
  80. bool operator==(const Point2D& p) const;
  81.  
  82. //! test for inequality
  83. bool operator!=(const Point2D& p) const;
  84.  
  85.  
  86. void clError()
  87. {
  88. mError = PE_NoError;
  89. }
  90.  
  91.  
  92. PointError error() const
  93. {
  94. return mError;
  95. }
  96.  
  97.  
  98. const char* errorString() const;
  99.  
  100.  
  101. QPointF toQPointF() const;
  102.  
  103.  
  104. bool isNull() const;
  105. };
  106. }
  107. #endif // POINT2D_HPP
To copy to clipboard, switch view to plain text mode 

So I created a simple test:
Qt Code:
  1. #ifndef TESTPOINT2D_HPP
  2. #define TESTPOINT2D_HPP
  3.  
  4. // $Rev$
  5. // $Date$
  6. // $Author$
  7. // $HeadURL$
  8.  
  9. #include <QtTest/QtTest>
  10. #include "../../src/common/geoalg/Point2D.hpp"
  11.  
  12. class TestPoint2D : public QObject
  13. {
  14. Q_OBJECT
  15.  
  16. private slots:
  17. void compareCoords_data();
  18. void compareCoords();
  19.  
  20. // void add_data();
  21. // void add();
  22. };
  23. #endif // TESTPOINT2D_HPP
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "TestPoint2D.hpp"
  2.  
  3. using namespace GeoAlg;
  4.  
  5. void TestPoint2D::compareCoords_data()
  6. {
  7. QTest::addColumn<Scalar>("pointCoord");
  8. QTest::addColumn<Scalar>("result");
  9.  
  10. QTest::newRow("simple x") << Point2D(1,1).x() << 1;
  11. QTest::newRow("simple y") << Point2D(1,1).y() << 1;
  12. QTest::newRow("null x") << Point2D(0,0).x() << 0;
  13. QTest::newRow("null y") << Point2D(0,0).y() << 0;
  14. QTest::newRow("neg. x") << Point2D(-5, -8).x() << -5;
  15. QTest::newRow("neg. y") << Point2D(-5, -8).y() << -8;
  16. QTest::newRow("decimal x") << Point2D(4.567, -8.901) << 4.567;
  17. QTest::newRow("decimal y") << Point2D(4.567, -8.901) << -8.901;
  18.  
  19. }
  20.  
  21. void TestPoint2D::compareCoords()
  22. {
  23. QFETCH(Scalar, pointCoord);
  24. QFETCH(Scalar, result);
  25.  
  26. QCOMPARE(pointCoord, result);
  27. }
  28.  
  29. QTEST_MAIN(TestPoint2D)
To copy to clipboard, switch view to plain text mode 

While compiling I get an error that is related to Qt's type system (I suppose):
Qt Code:
  1. D:\projekt\work\Tracker\b1tracker\test\GeoAlg>nmake
  2.  
  3. Microsoft (R) Program Maintenance Utility Version 9.00.21022.08
  4. Copyright (C) Microsoft Corporation. All rights reserved.
  5.  
  6. "C:\Programme\Microsoft Visual Studio 9.0\VC\BIN\nmake.exe" -f Makefile.
  7. Debug
  8.  
  9. Microsoft (R) Program Maintenance Utility Version 9.00.21022.08
  10. Copyright (C) Microsoft Corporation. All rights reserved.
  11.  
  12. D:\Qt\4.5.3\bin\moc.exe -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -D_USE_
  13. MATH_DEFINES -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -I"d:\Qt\4.
  14. 5.3\include\QtCore" -I"d:\Qt\4.5.3\include\QtGui" -I"d:\Qt\4.5.3\include" -I"d:\
  15. Qt\4.5.3\include\QtTest" -I"." -I"d:\Qt\4.5.3\include\ActiveQt" -I"debug" -I"d:\
  16. Qt\4.5.3\mkspecs\win32-msvc2008" -D_MSC_VER=1500 -DWIN32 TestPoint2D.hpp -o debu
  17. g\moc_TestPoint2D.cpp
  18. cl -c -nologo -Zm200 -Zc:wchar_t- -Zi -MDd -GR -EHsc -W3 -w34100 -w34189
  19. -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -D_USE_MATH_DEFINES -DQT_DLL -DQT_GUI_
  20. LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -I"d:\Qt\4.5.3\include\QtCore" -I"d:\Qt\4.
  21. 5.3\include\QtGui" -I"d:\Qt\4.5.3\include" -I"d:\Qt\4.5.3\include\QtTest" -I"."
  22. -I"d:\Qt\4.5.3\include\ActiveQt" -I"debug" -I"d:\Qt\4.5.3\mkspecs\win32-msvc2008
  23. " -Fodebug\ @C:\Users\kleylz\AppData\Local\Temp\nm70F3.tmp
  24. TestPoint2D.cpp
  25. d:\qt\4.5.3\include\qtcore\../../src/corelib/kernel/qmetatype.h(189) : error C20
  26. 39: 'qt_metatype_id' : is not a member of 'QMetaTypeId<T>'
  27. with
  28. [
  29. T=GeoAlg::Point2D
  30. ]
  31. d:\qt\4.5.3\include\qtcore\../../src/corelib/kernel/qmetatype.h(189) : w
  32. hile compiling class template member function 'int QMetaTypeId2<T>::qt_metatype_
  33. id(void)'
  34. with
  35. [
  36. T=GeoAlg::Point2D
  37. ]
  38. d:\qt\4.5.3\include\qtcore\../../src/corelib/kernel/qmetatype.h(199) : s
  39. ee reference to class template instantiation 'QMetaTypeId2<T>' being compiled
  40. with
  41. [
  42. T=GeoAlg::Point2D
  43. ]
  44. d:\qt\4.5.3\include\qttest\../../src/testlib/qtestdata.h(82) : see refer
  45. ence to function template instantiation 'int qMetaTypeId<T>(T *)' being compiled
  46.  
  47. with
  48. [
  49. T=GeoAlg::Point2D
  50. ]
  51. .\TestPoint2D.cpp(22) : see reference to function template instantiation
  52. 'QTestData &operator <<<GeoAlg::Point2D>(QTestData &,const T &)' being compiled
  53.  
  54. with
  55. [
  56. T=GeoAlg::Point2D
  57. ]
  58. NMAKE : fatal error U1077: '"C:\Programme\Microsoft Visual Studio 9.0\VC\BIN\cl.
  59. EXE"' : return code '0x2'
  60. Stop.
  61. NMAKE : fatal error U1077: '"C:\Programme\Microsoft Visual Studio 9.0\VC\BIN\nma
  62. ke.exe"' : return code '0x2'
To copy to clipboard, switch view to plain text mode 

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

Thanks for any hints