1 /***************************************************************************
2   qgstest - %{Cpp:License:ClassName}
3 
4  ---------------------
5  begin                : 5.12.2016
6  copyright            : (C) 2016 by Matthias Kuhn
7  email                : matthias@opengis.ch
8  ***************************************************************************
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  ***************************************************************************/
16 #ifndef QGSTEST_H
17 #define QGSTEST_H
18 
19 #include <QtTest/QtTest>
20 #include "qgsrectangle.h"
21 #include "qgsapplication.h"
22 
23 #define QGSTEST_MAIN(TestObject) \
24   QT_BEGIN_NAMESPACE \
25   QTEST_ADD_GPU_BLACKLIST_SUPPORT_DEFS \
26   QT_END_NAMESPACE \
27   int main(int argc, char *argv[]) \
28   { \
29     QgsApplication app(argc, argv, false); \
30     app.init(); \
31     app.setAttribute(Qt::AA_Use96Dpi, true); \
32     QTEST_DISABLE_KEYPAD_NAVIGATION \
33     QTEST_ADD_GPU_BLACKLIST_SUPPORT \
34     TestObject tc; \
35     QTEST_SET_MAIN_SOURCE_PATH \
36     return QTest::qExec(&tc, argc, argv); \
37   }
38 
39 
40 #define QGSCOMPARENEAR(value,expected,epsilon) { \
41     bool _xxxresult = qgsDoubleNear( value, expected, epsilon ); \
42     if ( !_xxxresult  ) \
43     { \
44       qDebug( "Expecting %f got %f (diff %f > %f)", static_cast< double >( expected ), static_cast< double >( value ), std::fabs( static_cast< double >( expected ) - value ), static_cast< double >( epsilon ) ); \
45     } \
46     QVERIFY( qgsDoubleNear( value, expected, epsilon ) ); \
47   }(void)(0)
48 
49 #define QGSCOMPARENOTNEAR(value,not_expected,epsilon) { \
50     bool _xxxresult = qgsDoubleNear( value, not_expected, epsilon ); \
51     if ( _xxxresult  ) \
52     { \
53       qDebug( "Expecting %f to be differerent from %f (diff %f > %f)", static_cast< double >( value ), static_cast< double >( not_expected ), std::fabs( static_cast< double >( not_expected ) - value ), static_cast< double >( epsilon ) ); \
54     } \
55     QVERIFY( !qgsDoubleNear( value, not_expected, epsilon ) ); \
56   }(void)(0)
57 
58 #define QGSCOMPARENEARPOINT(point1,point2,epsilon) { \
59     QGSCOMPARENEAR( point1.x(), point2.x(), epsilon ); \
60     QGSCOMPARENEAR( point1.y(), point2.y(), epsilon ); \
61   }
62 
63 #define QGSCOMPARENEARRECTANGLE(rectangle1,rectangle2,epsilon) { \
64     QGSCOMPARENEAR( rectangle1.xMinimum(), rectangle2.xMinimum(), epsilon ); \
65     QGSCOMPARENEAR( rectangle1.xMaximum(), rectangle2.xMaximum(), epsilon ); \
66     QGSCOMPARENEAR( rectangle1.yMinimum(), rectangle2.yMinimum(), epsilon ); \
67     QGSCOMPARENEAR( rectangle1.yMaximum(), rectangle2.yMaximum(), epsilon ); \
68   }
69 
70 //sometimes GML attributes are in a different order - but that's ok
71 #define QGSCOMPAREGML(result,expected) { \
72     QCOMPARE( result.replace( QStringLiteral("ts=\" \" cs=\",\""), QStringLiteral("cs=\",\" ts=\" \"") ), expected ); \
73   }
74 
75 /**
76  * QGIS unit test utilities.
77  * \since QGIS 3.0
78  */
79 namespace QgsTest
80 {
81 
82   //! Returns TRUE if test is running on a CI infrastructure
isCIRun()83   bool isCIRun()
84   {
85     return qgetenv( "QGIS_CONTINUOUS_INTEGRATION_RUN" ) == QStringLiteral( "true" );
86   }
87 
runFlakyTests()88   bool runFlakyTests()
89   {
90     return qgetenv( "RUN_FLAKY_TESTS" ) == QStringLiteral( "true" );
91   }
92 }
93 
94 /**
95  * Formatting QgsRectangle for QCOMPARE pretty printing
96  */
toString(const QgsRectangle & r)97 char *toString( const QgsRectangle &r )
98 {
99   return QTest::toString( QStringLiteral( "QgsRectangle(%1, %2, %3, %4)" ).arg( QString::number( r.xMinimum() ), QString::number( r.yMinimum() ), QString::number( r.xMaximum() ), QString::number( r.yMaximum() ) ) );
100 }
101 
102 
103 #endif // QGSTEST_H
104