Qt 4.8
qtest.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 QTEST_H
43 #define QTEST_H
44 
45 #include <QtTest/qtest_global.h>
46 #include <QtTest/qtestcase.h>
47 #include <QtTest/qtestdata.h>
48 #include <QtTest/qtestdata.h>
49 #include <QtTest/qbenchmark.h>
50 
51 #include <QtCore/qbytearray.h>
52 #include <QtCore/qstring.h>
53 #include <QtCore/qstringlist.h>
54 #include <QtCore/qdatetime.h>
55 #include <QtCore/qobject.h>
56 #include <QtCore/qvariant.h>
57 #include <QtCore/qurl.h>
58 
59 #include <QtCore/qpoint.h>
60 #include <QtCore/qsize.h>
61 #include <QtCore/qrect.h>
62 
63 
65 
67 
68 QT_MODULE(Test)
69 
70 namespace QTest
71 {
72 
73 template<> inline char *toString(const QLatin1String &str)
74 {
75  return qstrdup(str.latin1());
76 }
77 
78 template<> inline char *toString(const QString &str)
79 {
80  return qstrdup(str.toLatin1().constData());
81 }
82 
83 template<> inline char *toString(const QByteArray &ba)
84 {
85  return QTest::toHexRepresentation(ba.constData(), ba.length());
86 }
87 
88 template<> inline char *toString(const QTime &time)
89 {
90  return time.isValid()
91  ? qstrdup(time.toString(QLatin1String("hh:mm:ss.zzz")).toLatin1().constData())
92  : qstrdup("Invalid QTime");
93 }
94 
95 template<> inline char *toString(const QDate &date)
96 {
97  return date.isValid()
98  ? qstrdup(date.toString(QLatin1String("yyyy/MM/dd")).toLatin1().constData())
99  : qstrdup("Invalid QDate");
100 }
101 
102 template<> inline char *toString(const QDateTime &dateTime)
103 {
104  return dateTime.isValid()
105  ? qstrdup((dateTime.toString(QLatin1String("yyyy/MM/dd hh:mm:ss.zzz")) +
106  (dateTime.timeSpec() == Qt::LocalTime ? QLatin1String("[local time]") : QLatin1String("[UTC]"))).toLatin1().constData())
107  : qstrdup("Invalid QDateTime");
108 }
109 
110 template<> inline char *toString(const QChar &c)
111 {
112  return qstrdup(QString::fromLatin1("QChar: '%1' (0x%2)").arg(c).arg(QString::number(static_cast<int>(c.unicode()), 16)).toLatin1().constData());
113 }
114 
115 template<> inline char *toString(const QPoint &p)
116 {
117  return qstrdup(QString::fromLatin1("QPoint(%1,%2)").arg(p.x()).arg(p.y()).toLatin1().constData());
118 }
119 
120 template<> inline char *toString(const QSize &s)
121 {
122  return qstrdup(QString::fromLatin1("QSize(%1x%2)").arg(s.width()).arg(s.height()).toLatin1().constData());
123 }
124 
125 template<> inline char *toString(const QRect &s)
126 {
127  return qstrdup(QString::fromLatin1("QRect(%1,%2 %5x%6) (bottomright %3,%4)").arg(s.left()).arg(s.top()).arg(s.right()).arg(s.bottom()).arg(s.width()).arg(s.height()).toLatin1().constData());
128 }
129 
130 template<> inline char *toString(const QPointF &p)
131 {
132  return qstrdup(QString::fromLatin1("QPointF(%1,%2)").arg(p.x()).arg(p.y()).toLatin1().constData());
133 }
134 
135 template<> inline char *toString(const QSizeF &s)
136 {
137  return qstrdup(QString::fromLatin1("QSizeF(%1x%2)").arg(s.width()).arg(s.height()).toLatin1().constData());
138 }
139 
140 template<> inline char *toString(const QRectF &s)
141 {
142  return qstrdup(QString::fromLatin1("QRectF(%1,%2 %5x%6) (bottomright %3,%4)").arg(s.left()).arg(s.top()).arg(s.right()).arg(s.bottom()).arg(s.width()).arg(s.height()).toLatin1().constData());
143 }
144 
145 template<> inline char *toString(const QUrl &uri)
146 {
147  return qstrdup(uri.toEncoded().constData());
148 }
149 
150 template<> inline char *toString(const QVariant &v)
151 {
152  QByteArray vstring("QVariant(");
153  if (v.isValid()) {
154  QByteArray type(v.typeName());
155  if (type.isEmpty()) {
157  }
158  vstring.append(type);
159  if (!v.isNull()) {
160  vstring.append(',');
161  if (v.canConvert(QVariant::String)) {
162  vstring.append(qvariant_cast<QString>(v).toLatin1());
163  }
164  else {
165  vstring.append("<value not representable as string>");
166  }
167  }
168  }
169  vstring.append(')');
170 
171  return qstrdup(vstring.constData());
172 }
173 
174 #ifndef QTEST_NO_SPECIALIZATIONS
175 template<>
176 #endif
177 inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual,
178  const char *expected, const char *file, int line)
179 {
180  return qCompare<QString>(t1, QString(t2), actual, expected, file, line);
181 }
182 #ifndef QTEST_NO_SPECIALIZATIONS
183 template<>
184 #endif
185 inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *actual,
186  const char *expected, const char *file, int line)
187 {
188  return qCompare<QString>(QString(t1), t2, actual, expected, file, line);
189 }
190 
191 template<>
192 inline bool qCompare(QStringList const &t1, QStringList const &t2,
193  const char *actual, const char *expected, const char *file, int line)
194 {
195  char msg[1024];
196  msg[0] = '\0';
197  bool isOk = true;
198  if (t1.count() != t2.count()) {
199  qt_snprintf(msg, 1024, "Compared QStringLists have different sizes.\n"
200  " Actual (%s) size : '%d'\n"
201  " Expected (%s) size: '%d'", actual, t1.count(), expected, t2.count());
202  isOk = false;
203  }
204  const int min = qMin(t1.count(), t2.count());
205  for (int i = 0; isOk && i < min; ++i) {
206  if (t1.at(i) != t2.at(i)) {
207  qt_snprintf(msg, 1024, "Compared QStringLists differ at index %d.\n"
208  " Actual (%s) : '%s'\n"
209  " Expected (%s) : '%s'", i, actual, t1.at(i).toLatin1().constData(),
210  expected, t2.at(i).toLatin1().constData());
211  isOk = false;
212  }
213  }
214  return compare_helper(isOk, msg, file, line);
215 }
216 
217 template <typename T>
218 inline bool qCompare(QFlags<T> const &t1, T const &t2, const char *actual, const char *expected,
219  const char *file, int line)
220 {
221  return qCompare(int(t1), int(t2), actual, expected, file, line);
222 }
223 
224 template <typename T>
225 inline bool qCompare(QFlags<T> const &t1, int const &t2, const char *actual, const char *expected,
226  const char *file, int line)
227 {
228  return qCompare(int(t1), t2, actual, expected, file, line);
229 }
230 
231 }
233 
234 #define QTEST_APPLESS_MAIN(TestObject) \
235 int main(int argc, char *argv[]) \
236 { \
237  TestObject tc; \
238  return QTest::qExec(&tc, argc, argv); \
239 }
240 
241 #define QTEST_NOOP_MAIN \
242 int main(int argc, char *argv[]) \
243 { \
244  QObject tc; \
245  return QTest::qExec(&tc, argc, argv); \
246 }
247 
248 #include <QtTest/qtestsystem.h>
249 
250 #ifdef QT_GUI_LIB
251 
252 #include <QtTest/qtest_gui.h>
253 
254 #ifdef QT_KEYPAD_NAVIGATION
255 # define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
256 #else
257 # define QTEST_DISABLE_KEYPAD_NAVIGATION
258 #endif
259 
260 #define QTEST_MAIN(TestObject) \
261 int main(int argc, char *argv[]) \
262 { \
263  QApplication app(argc, argv); \
264  QTEST_DISABLE_KEYPAD_NAVIGATION \
265  TestObject tc; \
266  return QTest::qExec(&tc, argc, argv); \
267 }
268 
269 #else
270 
271 #define QTEST_MAIN(TestObject) \
272 int main(int argc, char *argv[]) \
273 { \
274  QCoreApplication app(argc, argv); \
275  TestObject tc; \
276  return QTest::qExec(&tc, argc, argv); \
277 }
278 
279 #endif // QT_GUI_LIB
280 
282 
283 #endif
bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual, const char *expected, const char *file, int line)
Definition: qtest.h:177
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
qreal right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:527
int type
Definition: qmetatype.cpp:239
unsigned char c[8]
Definition: qnumeric_p.h:62
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_MODULE(x)
Definition: qglobal.h:2783
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
QByteArray & append(char c)
Appends the character ch to this byte array.
qreal width() const
Returns the width.
Definition: qsize.h:284
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
qreal height() const
Returns the height.
Definition: qsize.h:287
qreal left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:525
int left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:240
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
bool isValid() const
Returns true if both the date and the time are valid; otherwise returns false.
Definition: qdatetime.cpp:2346
The QDate class provides date functions.
Definition: qdatetime.h:55
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
int bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:249
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
QString toString(Qt::DateFormat f=Qt::TextDate) const
Returns the datetime as a string in the format given.
Definition: qdatetime.cpp:2628
qreal x() const
Returns the x-coordinate of this point.
Definition: qpoint.h:282
int width() const
Returns the width.
Definition: qsize.h:126
The QTime class provides clock time functions.
Definition: qdatetime.h:148
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
const char * latin1() const
Returns the Latin-1 string stored in this object.
Definition: qstring.h:661
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
bool canConvert(Type t) const
Returns true if the variant&#39;s type can be cast to the requested type, t.
Definition: qvariant.cpp:2886
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
qreal height() const
Returns the height of the rectangle.
Definition: qrect.h:710
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
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
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
qreal width() const
Returns the width of the rectangle.
Definition: qrect.h:707
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
const char * typeName() const
Returns the name of the type stored in the variant.
Definition: qvariant.cpp:1984
int length() const
Same as size().
Definition: qbytearray.h:356
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QString toString(Qt::DateFormat f=Qt::TextDate) const
Returns the time as a string.
Definition: qdatetime.cpp:1653
int Q_TESTLIB_EXPORT qt_snprintf(char *str, int size, const char *format,...)
Definition: qtestcase.cpp:961
int top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:243
int userType() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1913
int right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:246
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
Q_CORE_EXPORT char * qstrdup(const char *)
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
Qt::TimeSpec timeSpec() const
Returns the time specification of the datetime.
Definition: qdatetime.cpp:2379
int height() const
Returns the height.
Definition: qsize.h:129
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287
Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *msg, const char *file, int line)
Definition: qtestcase.cpp:2290
qreal top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:526
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
The QFlags class provides a type-safe way of storing OR-combinations of enum values.
Definition: qglobal.h:2313
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
qreal bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:528
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
#define QT_END_HEADER
Definition: qglobal.h:137
QString toString(Qt::DateFormat f=Qt::TextDate) const
Returns the date as a string.
Definition: qdatetime.cpp:823
static QByteArray number(int, int base=10)
Returns a byte array containing the string equivalent of the number n to base base (10 by default)...
The QTest namespace contains all the functions and declarations that are related to the QTestLib tool...
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712
bool isValid() const
Returns true if the time is valid; otherwise returns false.
Definition: qdatetime.cpp:1566