Qt 4.8
qtestcase.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtTest module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QTESTCASE_H
43 #define QTESTCASE_H
44 
45 #include <QtTest/qtest_global.h>
46 
47 #include <QtCore/qnamespace.h>
48 #include <QtCore/qmetatype.h>
49 
51 
53 
54 QT_MODULE(Test)
55 
56 #define QVERIFY(statement) \
57 do {\
58  if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\
59  return;\
60 } while (0)
61 
62 #define QFAIL(message) \
63 do {\
64  QTest::qFail(message, __FILE__, __LINE__);\
65  return;\
66 } while (0)
67 
68 #define QVERIFY2(statement, description) \
69 do {\
70  if (statement) {\
71  if (!QTest::qVerify(true, #statement, (description), __FILE__, __LINE__))\
72  return;\
73  } else {\
74  if (!QTest::qVerify(false, #statement, (description), __FILE__, __LINE__))\
75  return;\
76  }\
77 } while (0)
78 
79 #define QCOMPARE(actual, expected) \
80 do {\
81  if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
82  return;\
83 } while (0)
84 
85 #define QSKIP(statement, mode) \
86 do {\
87  QTest::qSkip(statement, QTest::mode, __FILE__, __LINE__);\
88  return;\
89 } while (0)
90 
91 #define QEXPECT_FAIL(dataIndex, comment, mode)\
92 do {\
93  if (!QTest::qExpectFail(dataIndex, comment, QTest::mode, __FILE__, __LINE__))\
94  return;\
95 } while (0)
96 
97 #define QFETCH(type, name)\
98  type name = *static_cast<type *>(QTest::qData(#name, ::qMetaTypeId<type >()))
99 
100 #define QFETCH_GLOBAL(type, name)\
101  type name = *static_cast<type *>(QTest::qGlobalData(#name, ::qMetaTypeId<type >()))
102 
103 #define DEPENDS_ON(funcName)
104 
105 #define QTEST(actual, testElement)\
106 do {\
107  if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\
108  return;\
109 } while (0)
110 
111 #define QWARN(msg)\
112  QTest::qWarn(msg)
113 
114 class QObject;
115 class QTestData;
116 
117 #define QTEST_COMPARE_DECL(KLASS)\
118  template<> Q_TESTLIB_EXPORT char *toString<KLASS >(const KLASS &);
119 
120 namespace QTest
121 {
122  template <typename T>
123  inline char *toString(const T &)
124  {
125  return 0;
126  }
127 
128 
129  Q_TESTLIB_EXPORT char *toHexRepresentation(const char *ba, int length);
130  Q_TESTLIB_EXPORT char *toString(const char *);
131  Q_TESTLIB_EXPORT char *toString(const void *);
132 
133  Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc = 0, char **argv = 0);
134  Q_TESTLIB_EXPORT int qExec(QObject *testObject, const QStringList &arguments);
135 
136  Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description,
137  const char *file, int line);
138  Q_TESTLIB_EXPORT void qFail(const char *statementStr, const char *file, int line);
139  Q_TESTLIB_EXPORT void qSkip(const char *message, SkipMode mode, const char *file, int line);
140  Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode,
141  const char *file, int line);
142  Q_TESTLIB_EXPORT void qWarn(const char *message);
143  Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message);
144 
145  Q_TESTLIB_EXPORT void *qData(const char *tagName, int typeId);
146  Q_TESTLIB_EXPORT void *qGlobalData(const char *tagName, int typeId);
147  Q_TESTLIB_EXPORT void *qElementData(const char *elementName, int metaTypeId);
149 
150  Q_TESTLIB_EXPORT const char *currentAppName();
151 
153  Q_TESTLIB_EXPORT const char *currentDataTag();
155 
156  Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii);
158 
159  Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *msg, const char *file,
160  int line);
161  Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *msg, char *val1, char *val2,
162  const char *expected, const char *actual,
163  const char *file, int line);
164  Q_TESTLIB_EXPORT void qSleep(int ms);
165  Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name);
166 
167  template <typename T>
168  inline void addColumn(const char *name, T * = 0)
169  {
170  addColumnInternal(qMetaTypeId<T>(), name);
171  }
172  Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag);
173 
174  template <typename T>
175  inline bool qCompare(T const &t1, T const &t2, const char *actual, const char *expected,
176  const char *file, int line)
177  {
178  return (t1 == t2)
179  ? compare_helper(true, "COMPARE()", file, line)
180  : compare_helper(false, "Compared values are not the same",
181  toString<T>(t1), toString<T>(t2), actual, expected, file, line);
182  }
183 
184 
185  template <>
186  Q_TESTLIB_EXPORT bool qCompare<float>(float const &t1, float const &t2,
187  const char *actual, const char *expected, const char *file, int line);
188 
189  template <>
190  Q_TESTLIB_EXPORT bool qCompare<double>(double const &t1, double const &t2,
191  const char *actual, const char *expected, const char *file, int line);
192 
193  inline bool compare_ptr_helper(const void *t1, const void *t2, const char *actual,
194  const char *expected, const char *file, int line)
195  {
196  return (t1 == t2)
197  ? compare_helper(true, "COMPARE()", file, line)
198  : compare_helper(false, "Compared pointers are not the same",
199  toString(t1), toString(t2), actual, expected, file, line);
200  }
201 
202  Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual,
203  const char *expected, const char *file, int line);
204 
205 #ifndef qdoc
206  QTEST_COMPARE_DECL(short)
208  QTEST_COMPARE_DECL(int)
210  QTEST_COMPARE_DECL(long)
214 
215  QTEST_COMPARE_DECL(float)
216  QTEST_COMPARE_DECL(double)
217  QTEST_COMPARE_DECL(char)
218  QTEST_COMPARE_DECL(bool)
219 #endif
220 
221 #ifndef QTEST_NO_SPECIALIZATIONS
222  template <typename T1, typename T2>
223  bool qCompare(T1 const &, T2 const &, const char *, const char *, const char *, int);
224 
225 #if defined(QT_COORD_TYPE) && (defined(QT_ARCH_ARM) || defined(QT_NO_FPU) || defined(QT_ARCH_WINDOWSCE))
226  template <>
227  inline bool qCompare<qreal, float>(qreal const &t1, float const &t2, const char *actual,
228  const char *expected, const char *file, int line)
229  {
230  return qCompare<qreal>(t1, qreal(t2), actual, expected, file, line);
231  }
232 
233  template <>
234  inline bool qCompare<float, qreal>(float const &t1, qreal const &t2, const char *actual,
235  const char *expected, const char *file, int line)
236  {
237  return qCompare<qreal>(qreal(t1), t2, actual, expected, file, line);
238  }
239 
240 #elif defined(QT_COORD_TYPE) || defined(QT_ARCH_ARM) || defined(QT_NO_FPU) || defined(QT_ARCH_WINDOWSCE) || defined(QT_ARCH_SYMBIAN)
241  template <>
242  inline bool qCompare<qreal, double>(qreal const &t1, double const &t2, const char *actual,
243  const char *expected, const char *file, int line)
244  {
245  return qCompare<float>(float(t1), float(t2), actual, expected, file, line);
246  }
247 
248  template <>
249  inline bool qCompare<double, qreal>(double const &t1, qreal const &t2, const char *actual,
250  const char *expected, const char *file, int line)
251  {
252  return qCompare<float>(float(t1), float(t2), actual, expected, file, line);
253  }
254 
255 #endif
256 
257  template <typename T>
258  inline bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected,
259  const char *file, int line)
260  {
261  return compare_ptr_helper(t1, t2, actual, expected, file, line);
262  }
263  template <typename T>
264  inline bool qCompare(T *t1, T *t2, const char *actual, const char *expected,
265  const char *file, int line)
266  {
267  return compare_ptr_helper(t1, t2, actual, expected, file, line);
268  }
269 
270  template <typename T1, typename T2>
271  inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected,
272  const char *file, int line)
273  {
274  return compare_ptr_helper(t1, static_cast<const T1 *>(t2), actual, expected, file, line);
275  }
276  template <typename T1, typename T2>
277  inline bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected,
278  const char *file, int line)
279  {
280  return compare_ptr_helper(const_cast<const T1 *>(t1),
281  static_cast<const T1 *>(const_cast<const T2 *>(t2)), actual, expected, file, line);
282  }
283  template<>
284  inline bool qCompare<char>(const char *t1, const char *t2, const char *actual,
285  const char *expected, const char *file, int line)
286  {
287  return compare_string_helper(t1, t2, actual, expected, file, line);
288  }
289  template<>
290  inline bool qCompare<char>(char *t1, char *t2, const char *actual, const char *expected,
291  const char *file, int line)
292  {
293  return compare_string_helper(t1, t2, actual, expected, file, line);
294  }
295 #else /* QTEST_NO_SPECIALIZATIONS */
296 
297 // In Symbian we have QTEST_NO_SPECIALIZATIONS defined, but still float related specialization
298 // should be used. If QTEST_NO_SPECIALIZATIONS is enabled we get ambiguous overload errors.
299 #if defined(QT_ARCH_SYMBIAN)
300  template <typename T1, typename T2>
301  bool qCompare(T1 const &, T2 const &, const char *, const char *, const char *, int);
302 
303  template <>
304  inline bool qCompare<qreal, double>(qreal const &t1, double const &t2, const char *actual,
305  const char *expected, const char *file, int line)
306  {
307  return qCompare<float>(float(t1), float(t2), actual, expected, file, line);
308  }
309 
310  template <>
311  inline bool qCompare<double, qreal>(double const &t1, qreal const &t2, const char *actual,
312  const char *expected, const char *file, int line)
313  {
314  return qCompare<float>(float(t1), float(t2), actual, expected, file, line);
315  }
316 #endif
317 
318  inline bool qCompare(const char *t1, const char *t2, const char *actual,
319  const char *expected, const char *file, int line)
320  {
321  return compare_string_helper(t1, t2, actual, expected, file, line);
322  }
323 
324  inline bool qCompare(char *t1, char *t2, const char *actual, const char *expected,
325  const char *file, int line)
326  {
327  return compare_string_helper(t1, t2, actual, expected, file, line);
328  }
329 #endif
330 
331  /* The next two specializations are for MSVC that shows problems with implicit
332  conversions
333  */
334 #ifndef QTEST_NO_SPECIALIZATIONS
335  template<>
336 #endif
337  inline bool qCompare(char *t1, const char *t2, const char *actual,
338  const char *expected, const char *file, int line)
339  {
340  return compare_string_helper(t1, t2, actual, expected, file, line);
341  }
342 #ifndef QTEST_NO_SPECIALIZATIONS
343  template<>
344 #endif
345  inline bool qCompare(const char *t1, char *t2, const char *actual,
346  const char *expected, const char *file, int line)
347  {
348  return compare_string_helper(t1, t2, actual, expected, file, line);
349  }
350 
351  // NokiaX86 and RVCT do not like implicitly comparing bool with int
352 #ifndef QTEST_NO_SPECIALIZATIONS
353  template <>
354 #endif
355  inline bool qCompare(bool const &t1, int const &t2,
356  const char *actual, const char *expected, const char *file, int line)
357  {
358  return qCompare<int>(int(t1), t2, actual, expected, file, line);
359  }
360 
361 
362  template <class T>
363  inline bool qTest(const T& actual, const char *elementName, const char *actualStr,
364  const char *expected, const char *file, int line)
365  {
366  return qCompare(actual, *static_cast<const T *>(QTest::qElementData(elementName,
367  qMetaTypeId<T>())), actualStr, expected, file, line);
368  }
369 }
370 
371 #undef QTEST_COMPARE_DECL
372 
374 
376 
377 #endif
bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual, const char *expected, const char *file, int line)
Definition: qtest.h:177
#define Q_TESTLIB_EXPORT
Definition: qtest_global.h:56
int type
Definition: qmetatype.cpp:239
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static const char * currentAppName
Definition: qtestresult.cpp:72
Q_TESTLIB_EXPORT const char * currentTestFunction()
Returns the name of the test function that is currently executed.
Definition: qtestcase.cpp:2230
#define QT_MODULE(x)
Definition: qglobal.h:2783
Q_TESTLIB_EXPORT QObject * testObject()
Definition: qtestcase.cpp:2283
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message)
Ignores messages created by qDebug() or qWarning().
Definition: qtestcase.cpp:2123
SkipMode
This enum describes the modes for skipping tests during execution of the test data.
Definition: qtest_global.h:82
Q_TESTLIB_EXPORT void * qElementData(const char *elementName, int metaTypeId)
Definition: qtestcase.cpp:2144
Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode, const char *file, int line)
Definition: qtestcase.cpp:2094
void addColumn(const char *name, T *=0)
Adds a column with type T to the current test data.
Definition: qtestcase.h:168
Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name)
Definition: qtestcase.cpp:2160
Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc=0, char **argv=0)
Executes tests declared in testObject.
Definition: qtestcase.cpp:1913
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
QtMsgType
This enum describes the messages that can be sent to a message handler (QtMsgHandler).
Definition: qglobal.h:1881
Q_TESTLIB_EXPORT void qFail(const char *statementStr, const char *file, int line)
Definition: qtestcase.cpp:2067
Q_TESTLIB_EXPORT void * qData(const char *tagName, int typeId)
Definition: qtestcase.cpp:2130
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
unsigned __int64 quint64
Definition: qglobal.h:943
char * toHexRepresentation(const char *ba, int length)
Returns a pointer to a string that is the string ba represented as a space-separated sequence of hex ...
Definition: qtestcase.cpp:1667
const char * name
bool qCompare< char >(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)
Definition: qtestcase.h:284
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
char * toString(const QLatin1String &str)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qtest.h:73
unsigned int uint
Definition: qglobal.h:996
__int64 qint64
Definition: qglobal.h:942
unsigned long ulong
Definition: qglobal.h:997
Q_TESTLIB_EXPORT bool qCompare< double >(double const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line)
Definition: qtestcase.cpp:2320
Q_TESTLIB_EXPORT const char * currentDataTag()
Returns the name of the current test data.
Definition: qtestcase.cpp:2239
Q_TESTLIB_EXPORT void qWarn(const char *message)
Definition: qtestcase.cpp:2102
unsigned short ushort
Definition: qglobal.h:995
Q_TESTLIB_EXPORT bool qCompare< float >(float const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line)
Definition: qtestcase.cpp:2307
#define QTEST_COMPARE_DECL(KLASS)
Definition: qtestcase.h:117
int key
Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description, const char *file, int line)
Definition: qtestcase.cpp:2074
Q_TESTLIB_EXPORT void qSkip(const char *message, SkipMode mode, const char *file, int line)
Definition: qtestcase.cpp:2083
TestFailMode
This enum describes the modes for handling an expected failure of the QVERIFY() or QCOMPARE() macros...
Definition: qtest_global.h:83
Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key)
Convert a Qt Key to an ascii char value.
Definition: qasciikey.cpp:245
Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *msg, const char *file, int line)
Definition: qtestcase.cpp:2290
Q_TESTLIB_EXPORT void * qGlobalData(const char *tagName, int typeId)
Definition: qtestcase.cpp:2137
bool compare_ptr_helper(const void *t1, const void *t2, const char *actual, const char *expected, const char *file, int line)
Definition: qtestcase.h:193
Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii)
Convert an ascii char key value to a Qt Key value.
Definition: qasciikey.cpp:57
Q_TESTLIB_EXPORT bool currentTestFailed()
Returns true if the current test function failed, otherwise false.
Definition: qtestcase.cpp:2247
Q_TESTLIB_EXPORT QTestData & newRow(const char *dataTag)
Appends a new row to the current test data.
Definition: qtestcase.cpp:2183
Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)
Definition: qtestcase.cpp:2376
#define QT_END_HEADER
Definition: qglobal.h:137
bool qTest(const T &actual, const char *elementName, const char *actualStr, const char *expected, const char *file, int line)
Definition: qtestcase.h:363
The QTest namespace contains all the functions and declarations that are related to the QTestLib tool...
Q_TESTLIB_EXPORT void qSleep(int ms)
Sleeps for ms milliseconds, blocking execution of the test.
Definition: qtestcase.cpp:2269