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

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 2.9 KB
Line 
1
2void wrapInFunction()
3{
4
5//! [0]
6QVERIFY(1 + 1 == 2);
7//! [0]
8
9
10//! [1]
11QVERIFY2(1 + 1 == 2, "A breach in basic arithmetic occured.");
12//! [1]
13
14
15//! [2]
16QCOMPARE(QString("hello").toUpper(), QString("HELLO"));
17//! [2]
18
19
20//! [3]
21void TestQString::toInt_data()
22{
23 QTest::addColumn<QString>("aString");
24 QTest::addColumn<int>("expected");
25
26 QTest::newRow("positive value") << "42" << 42;
27 QTest::newRow("negative value") << "-42" << -42;
28 QTest::newRow("zero") << "0" << 0;
29}
30//! [3]
31
32
33//! [4]
34void TestQString::toInt()
35{
36 QFETCH(QString, aString);
37 QFETCH(int, expected);
38
39 QCOMPARE(aString.toInt(), expected);
40}
41//! [4]
42
43
44//! [5]
45if (sizeof(int) != 4)
46 QFAIL("This test has not been ported to this platform yet.");
47//! [5]
48
49
50//! [6]
51QFETCH(QString, myString);
52QCOMPARE(QString("hello").toUpper(), myString);
53//! [6]
54
55
56//! [7]
57QTEST(QString("hello").toUpper(), "myString");
58//! [7]
59
60
61//! [8]
62if (!QSqlDatabase::drivers().contains("SQLITE"))
63 QSKIP("This test requires the SQLITE database driver", SkipAll);
64//! [8]
65
66
67//! [9]
68QEXPECT_FAIL("", "Will fix in the next release", Continue);
69QCOMPARE(i, 42);
70QCOMPARE(j, 43);
71//! [9]
72
73
74//! [10]
75QEXPECT_FAIL("data27", "Oh my, this is soooo broken", Abort);
76QCOMPARE(i, 42);
77//! [10]
78
79
80//! [11]
81class TestQString: public QObject { ... };
82QTEST_MAIN(TestQString)
83//! [11]
84
85
86//! [12]
87#ifdef Q_WS_X11
88 QTEST_MAIN(MyX11Test)
89#else
90 // do nothing on non-X11 platforms
91 QTEST_NOOP_MAIN
92#endif
93//! [12]
94
95
96//! [13]
97QTest::keyClick(myWidget, 'a');
98//! [13]
99
100
101//! [14]
102QTest::keyClick(myWidget, Qt::Key_Escape);
103
104QTest::keyClick(myWidget, Qt::Key_Escape, Qt::ShiftModifier, 200);
105//! [14]
106
107
108//! [15]
109QTest::keyClicks(myWidget, "hello world");
110//! [15]
111
112
113//! [16]
114namespace QTest {
115 template<>
116 char *toString(const MyPoint &point)
117 {
118 QByteArray ba = "MyPoint(";
119 ba += QByteArray::number(point.x()) + ", " + QByteArray::number(point.y());
120 ba += ")";
121 return qstrdup(ba.data());
122 }
123}
124//! [16]
125
126
127//! [17]
128int i = 0;
129while (myNetworkServerNotResponding() && i++ < 50)
130 QTest::qWait(250);
131//! [17]
132
133
134//! [18]
135MyFirstTestObject test1;
136QTest::qExec(&test1);
137
138MySecondTestObject test2;
139QTest::qExec(&test2);
140//! [18]
141
142
143//! [19]
144QDir dir;
145
146QTest::ignoreMessage(QtWarningMsg, "QDir::mkdir: Empty or null file name(s)");
147dir.mkdir("");
148//! [19]
149
150
151//! [20]
152void myTestFunction_data()
153{
154 QTest::addColumn<QString>("aString");
155 QTest::newRow("just hello") << QString("hello");
156 QTest::newRow("a null string") << QString();
157}
158//! [20]
159
160
161//! [21]
162void myTestFunction_data() {
163 QTest::addColumn<int>("intval");
164 QTest::addColumn<QString>("str");
165 QTest::addColumn<double>("dbl");
166
167 QTest::newRow("row1") << 1 << "hello" << 1.5;
168}
169//! [21]
170
171
172//! [22]
173void MyTestClass::cleanup()
174{
175 if (qstrcmp(currentTestFunction(), "myDatabaseTest") == 0) {
176 // clean up all database connections
177 closeAllDatabases();
178 }
179}
180//! [22]
181
182
183//! [23]
184QTest::qSleep(250);
185//! [23]
186
187}
188
Note: See TracBrowser for help on using the repository browser.