source: trunk/doc/src/snippets/code/src_qtestlib_qtestcase.cpp@ 788

Last change on this file since 788 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 4.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42
43void wrapInFunction()
44{
45
46//! [0]
47QVERIFY(1 + 1 == 2);
48//! [0]
49
50
51//! [1]
52QVERIFY2(1 + 1 == 2, "A breach in basic arithmetic occured.");
53//! [1]
54
55
56//! [2]
57QCOMPARE(QString("hello").toUpper(), QString("HELLO"));
58//! [2]
59
60
61//! [3]
62void TestQString::toInt_data()
63{
64 QTest::addColumn<QString>("aString");
65 QTest::addColumn<int>("expected");
66
67 QTest::newRow("positive value") << "42" << 42;
68 QTest::newRow("negative value") << "-42" << -42;
69 QTest::newRow("zero") << "0" << 0;
70}
71//! [3]
72
73
74//! [4]
75void TestQString::toInt()
76{
77 QFETCH(QString, aString);
78 QFETCH(int, expected);
79
80 QCOMPARE(aString.toInt(), expected);
81}
82//! [4]
83
84
85//! [5]
86if (sizeof(int) != 4)
87 QFAIL("This test has not been ported to this platform yet.");
88//! [5]
89
90
91//! [6]
92QFETCH(QString, myString);
93QCOMPARE(QString("hello").toUpper(), myString);
94//! [6]
95
96
97//! [7]
98QTEST(QString("hello").toUpper(), "myString");
99//! [7]
100
101
102//! [8]
103if (!QSqlDatabase::drivers().contains("SQLITE"))
104 QSKIP("This test requires the SQLITE database driver", SkipAll);
105//! [8]
106
107
108//! [9]
109QEXPECT_FAIL("", "Will fix in the next release", Continue);
110QCOMPARE(i, 42);
111QCOMPARE(j, 43);
112//! [9]
113
114
115//! [10]
116QEXPECT_FAIL("data27", "Oh my, this is soooo broken", Abort);
117QCOMPARE(i, 42);
118//! [10]
119
120
121//! [11]
122class TestQString: public QObject { ... };
123QTEST_MAIN(TestQString)
124//! [11]
125
126
127//! [12]
128#ifdef Q_WS_X11
129 QTEST_MAIN(MyX11Test)
130#else
131 // do nothing on non-X11 platforms
132 QTEST_NOOP_MAIN
133#endif
134//! [12]
135
136
137//! [13]
138QTest::keyClick(myWidget, 'a');
139//! [13]
140
141
142//! [14]
143QTest::keyClick(myWidget, Qt::Key_Escape);
144
145QTest::keyClick(myWidget, Qt::Key_Escape, Qt::ShiftModifier, 200);
146//! [14]
147
148
149//! [15]
150QTest::keyClicks(myWidget, "hello world");
151//! [15]
152
153
154//! [16]
155namespace QTest {
156 template<>
157 char *toString(const MyPoint &point)
158 {
159 QByteArray ba = "MyPoint(";
160 ba += QByteArray::number(point.x()) + ", " + QByteArray::number(point.y());
161 ba += ")";
162 return qstrdup(ba.data());
163 }
164}
165//! [16]
166
167
168//! [17]
169int i = 0;
170while (myNetworkServerNotResponding() && i++ < 50)
171 QTest::qWait(250);
172//! [17]
173
174
175//! [18]
176MyFirstTestObject test1;
177QTest::qExec(&test1);
178
179MySecondTestObject test2;
180QTest::qExec(&test2);
181//! [18]
182
183
184//! [19]
185QDir dir;
186
187QTest::ignoreMessage(QtWarningMsg, "QDir::mkdir: Empty or null file name(s)");
188dir.mkdir("");
189//! [19]
190
191
192//! [20]
193void myTestFunction_data()
194{
195 QTest::addColumn<QString>("aString");
196 QTest::newRow("just hello") << QString("hello");
197 QTest::newRow("a null string") << QString();
198}
199//! [20]
200
201
202//! [21]
203void myTestFunction_data() {
204 QTest::addColumn<int>("intval");
205 QTest::addColumn<QString>("str");
206 QTest::addColumn<double>("dbl");
207
208 QTest::newRow("row1") << 1 << "hello" << 1.5;
209}
210//! [21]
211
212
213//! [22]
214void MyTestClass::cleanup()
215{
216 if (qstrcmp(currentTestFunction(), "myDatabaseTest") == 0) {
217 // clean up all database connections
218 closeAllDatabases();
219 }
220}
221//! [22]
222
223
224//! [23]
225QTest::qSleep(250);
226//! [23]
227
228//! [24]
229QWidget widget;
230widget.show();
231QTest::qWaitForWindowShown(&widget);
232//! [24]
233
234}
235
Note: See TracBrowser for help on using the repository browser.