summaryrefslogtreecommitdiffstats
path: root/tests/auto/testlib/selftests/expectfail/tst_expectfail.cpp
blob: 7b555b8293340cae8b58aaabee1f55134a109b3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtCore/QCoreApplication>
#include <QTest>

Q_DECLARE_METATYPE(QTest::TestFailMode)

class tst_ExpectFail: public QObject
{
    Q_OBJECT

private slots:
    void cleanupTestCase() const;
    void init() const;
    void xfailAndContinue() const;
    void xfailAndAbort() const;
    void xfailContinueSkip() const;
    void xfailAbortSkip() const;
    void xfailTwice() const;
    void xfailDataDrivenTwice() const;
    void xfailDataDrivenTwice_data() const { xfailDataDriven_data(false); }
    void xfailWithQString() const;
    void xfailDataDrivenWithQString_data() const { xfailDataDriven_data(false); }
    void xfailDataDrivenWithQString() const;
    void xfailDataDrivenWithQVerify_data() const { xfailDataDriven_data(false); }
    void xfailDataDrivenWithQVerify() const;
    void xfailDataDrivenWithQCompare_data() const { xfailDataDriven_data(false); }
    void xfailDataDrivenWithQCompare() const;
    void xfailOnWrongRow_data() const { xfailDataDriven_data(true); }
    void xfailOnWrongRow() const;
    void xfailOnAnyRow_data() const { xfailDataDriven_data(true); }
    void xfailOnAnyRow() const;
    void xfailWithoutCheck_data() const { xfailDataDriven_data(true); }
    void xfailWithoutCheck() const;
    void xpassAbort() const;
    void xpassAbortSkip() const;
    void xpassAbortXfailContinue() const;
    void xpassContinue() const;
    void xpassContinueSkip() const;
    void xpassContinueXfailAbort() const;
    void xpassAbortDataDrivenWithQVerify_data() const { xpassDataDriven_data(); }
    void xpassAbortDataDrivenWithQVerify() const;
    void xpassContinueDataDrivenWithQVerify_data() const { xpassDataDriven_data(); }
    void xpassContinueDataDrivenWithQVerify() const;
    void xpassAbortDataDrivenWithQCompare_data() const { xpassDataDriven_data(); }
    void xpassAbortDataDrivenWithQCompare() const;
    void xpassContinueDataDrivenWithQCompare_data() const { xpassDataDriven_data(); }
    void xpassContinueDataDrivenWithQCompare() const;

private:
    void xfailDataDriven_data(bool failOnly) const;
    void xpassDataDriven_data() const;
};
static int casesTested = 2;
// What the totals line's numbers *should* be:
static int passed = 2, failed = 0, skipped = 0;
// Total and passed get {init,cleanup}TestCase() in addition to the actual tests.

void tst_ExpectFail::init() const
{
    ++casesTested;
}

void tst_ExpectFail::cleanupTestCase() const
{
    qDebug("Totals should add up to %d: %d passed, %d failed, %d skipped",
           casesTested, passed, failed, skipped);
}

void tst_ExpectFail::xfailAndContinue() const
{
    ++passed;
    qDebug("begin");
    QEXPECT_FAIL("", "This should xfail", Continue);
    QVERIFY(false);
    qDebug("after");
}

void tst_ExpectFail::xfailAndAbort() const
{
    ++passed;
    qDebug("begin");
    QEXPECT_FAIL("", "This should xfail", Abort);
    QVERIFY(false);

    // If we get here the test did not correctly abort on the previous QVERIFY.
    QVERIFY2(false, "This should not be reached");
}

void tst_ExpectFail::xfailContinueSkip() const
{
    ++skipped;
    QEXPECT_FAIL("", "This should xfail then skip", Continue);
    QVERIFY(false);
    QSKIP("This skip should be reported and counted");
}

void tst_ExpectFail::xfailAbortSkip() const
{
    ++passed;
    QEXPECT_FAIL("", "This should xfail", Abort);
    QVERIFY(false);

    // If we get here the test did not correctly abort on the previous QVERIFY.
    QSKIP("This skip should not be reached");
}

void tst_ExpectFail::xfailTwice() const
{
    ++failed;
    QEXPECT_FAIL("", "Calling QEXPECT_FAIL once is fine", Continue);
    QEXPECT_FAIL("", "Calling QEXPECT_FAIL when already expecting a failure is "
                     "an error and should abort this test function", Continue);

    // If we get here the test did not correctly abort on the double call to QEXPECT_FAIL.
    QVERIFY2(false, "This should not be reached");
}

void tst_ExpectFail::xfailDataDrivenTwice() const
{
    ++failed;
    // Same with data-driven cases (twist semantics of unused shouldPass; we
    // have four combinations to test):
    QEXPECT_FAIL("Pass Abort", "Calling QEXPECT_FAIL once on a test-case is fine", Abort);
    QEXPECT_FAIL("Pass Abort", "Calling QEXPECT_FAIL when already expecting a failure is "
                               "an error and should abort this test function", Continue);
    QEXPECT_FAIL("Fail Abort", "Calling QEXPECT_FAIL once on a test-case is fine", Abort);
    QEXPECT_FAIL("Fail Abort", "Calling QEXPECT_FAIL when already expecting a failure is "
                               "an error and should abort this test function", Abort);
    QEXPECT_FAIL("Pass Continue", "Calling QEXPECT_FAIL once on a test-case is fine", Continue);
    QEXPECT_FAIL("Pass Continue", "Calling QEXPECT_FAIL when already expecting a failure is "
                                  "an error and should abort this test function", Abort);
    QEXPECT_FAIL("Fail Continue", "Calling QEXPECT_FAIL once on a test-case is fine", Continue);
    QEXPECT_FAIL("Fail Continue", "Calling QEXPECT_FAIL when already expecting a failure is "
                                  "an error and should abort this test function", Continue);

    // If we get here the test did not correctly abort on the double call to QEXPECT_FAIL.
    QVERIFY2(false, "This should not be reached");
}

void tst_ExpectFail::xfailWithQString() const
{
    ++passed;
    QEXPECT_FAIL("", QString("A string").toLatin1().constData(), Continue);
    QVERIFY(false);

    int bugNo = 5;
    QLatin1String msg("The message");
    QEXPECT_FAIL("", QString("Bug %1 (%2)").arg(bugNo).arg(msg).toLatin1().constData(), Continue);
    QVERIFY(false);
}

void tst_ExpectFail::xfailDataDrivenWithQString() const
{
    // This test does not (yet) distinguish the two Pass cases.
    QFETCH(bool, shouldPass);
    QFETCH(QTest::TestFailMode, failMode);
    if (shouldPass || failMode == QTest::Continue)
        ++skipped;
    else
        ++passed;

    QEXPECT_FAIL("Fail Abort", QString("A string").toLatin1().constData(), Abort);
    QEXPECT_FAIL("Fail Continue", QString("A string").toLatin1().constData(), Continue);
    // TODO: why aren't QVERIFY2()'s smessages seen ?
    QVERIFY2(shouldPass, "Both Fail cases should XFAIL here");
    // Fail Abort should be gone now.

    int bugNo = 5;
    QLatin1String msg("The message");
    QEXPECT_FAIL("Fail Continue", qPrintable(QString("Bug %1 (%2)").arg(bugNo).arg(msg)), Continue);
    QVERIFY2(shouldPass, "Only Fail Continue should see this");

    // FAIL is a pass, and SKIP trumps pass.
    QSKIP("Each Continue or Pass reports this and increments skip-count");
}

void tst_ExpectFail::xfailDataDrivenWithQVerify() const
{
    // This test does not (yet) distinguish the two Pass cases.
    ++passed;
    QFETCH(bool, shouldPass);
    QFETCH(QTest::TestFailMode, failMode);

    QEXPECT_FAIL("Fail Abort", "This test should xfail", Abort);
    QEXPECT_FAIL("Fail Continue", "This test should xfail", Continue);
    QVERIFY2(shouldPass, "Both Fail cases should XFAIL here");

    // If we get here, either we expected to pass or we expected to
    // fail and the failure mode was Continue.
    if (!shouldPass)
        QCOMPARE(failMode, QTest::Continue);
}

void tst_ExpectFail::xfailDataDriven_data(bool failOnly) const
{
    QTest::addColumn<bool>("shouldPass");
    QTest::addColumn<QTest::TestFailMode>("failMode");

    if (!failOnly) {
        QTest::newRow("Pass Abort")    << true  << QTest::Abort;
        QTest::newRow("Pass Continue") << true  << QTest::Continue;
    }
    QTest::newRow("Fail Abort")    << false << QTest::Abort;
    QTest::newRow("Fail Continue") << false << QTest::Continue;
}

void tst_ExpectFail::xfailDataDrivenWithQCompare() const
{
    // This test does not (yet) distinguish the two Pass cases.
    ++passed;
    QFETCH(bool, shouldPass);
    QFETCH(QTest::TestFailMode, failMode);

    QEXPECT_FAIL("Fail Abort", "This test should xfail", Abort);
    QEXPECT_FAIL("Fail Continue", "This test should xfail", Continue);

    QCOMPARE(1, shouldPass ? 1 : 2);

    // If we get here, either we expected to pass or we expected to
    // fail and the failure mode was Continue.
    if (!shouldPass)
        QCOMPARE(failMode, QTest::Continue);
}

void tst_ExpectFail::xfailOnWrongRow() const
{
    ++passed;
    qDebug("Should pass (*not* xpass), despite test-case name");
    // QEXPECT_FAIL for a row that does not exist should be ignored.
    // (It might be conditional data(), so exist in other circumstances.)
    QFETCH(QTest::TestFailMode, failMode);
    // You can't pass a variable as the last parameter of QEXPECT_FAIL,
    // because the macro adds "QTest::" in front of the last parameter.
    // That is why the following code appears to be a little strange.
    if (failMode == QTest::Abort)
        QEXPECT_FAIL("wrong row", "This xfail should be ignored", Abort);
    else
        QEXPECT_FAIL("wrong row", "This xfail should be ignored", Continue);
    QTEST(false, "shouldPass"); // _data skips the passing tests as pointless
}

void tst_ExpectFail::xfailOnAnyRow() const
{
    ++passed;
    // In a data-driven test, passing an empty first parameter to QEXPECT_FAIL
    // should mean that the failure is expected for all data rows.
    QFETCH(QTest::TestFailMode, failMode);
    // You can't pass a variable as the last parameter of QEXPECT_FAIL,
    // because the macro adds "QTest::" in front of the last parameter.
    // That is why the following code appears to be a little strange.
    if (failMode == QTest::Abort)
        QEXPECT_FAIL("", "This test should xfail", Abort);
    else
        QEXPECT_FAIL("", "This test should xfail", Continue);
    QTEST(true, "shouldPass"); // _data skips the passing tests as pointless
}

void tst_ExpectFail::xfailWithoutCheck() const
{
    ++failed;
    qDebug("Should fail (*not* xfail), despite test-case name");
    QTEST(false, "shouldPass"); // _data skips the passing tests as pass/fail is irrelevant
    QEXPECT_FAIL("Fail Abort", "Calling QEXPECT_FAIL without any subsequent check is an error",
                 Abort);
    QEXPECT_FAIL("Fail Continue", "Calling QEXPECT_FAIL without any subsequent check is an error",
                 Continue);
}

void tst_ExpectFail::xpassAbort() const
{
    ++failed;
    QEXPECT_FAIL("", "This test should xpass", Abort);
    QVERIFY(true);

    // If we get here the test did not correctly abort on the previous
    // unexpected pass.
    QVERIFY2(false, "This should not be reached");
}

void tst_ExpectFail::xpassAbortSkip() const
{
    ++failed;
    QEXPECT_FAIL("", "This test should xpass", Abort);
    QVERIFY(true);

    QSKIP("This should not be reached (and not add to skip-count)");
}

void tst_ExpectFail::xpassAbortXfailContinue() const
{
    ++failed;
    QEXPECT_FAIL("", "This test should xpass", Abort);
    QVERIFY(true);

    // If we get here the test did not correctly abort on the previous
    // unexpected pass.
    QEXPECT_FAIL("", "This should not be reached", Continue);
    QVERIFY2(false, "This should not be reached");
}

void tst_ExpectFail::xpassContinue() const
{
    ++failed;
    QEXPECT_FAIL("", "This test should xpass", Continue);
    QVERIFY(true);
    qDebug("This should be reached");
}

void tst_ExpectFail::xpassDataDriven_data() const
{
    QTest::addColumn<bool>("shouldXPass");

    QTest::newRow("XPass")  << true;
    QTest::newRow("Pass")   << false;
}

void tst_ExpectFail::xpassContinueSkip() const
{
    ++failed; // and *not* ++skipped
    QEXPECT_FAIL("", "This test should xpass", Continue);
    QVERIFY(true);
    // FIXME: QTBUG-95661 skip-count is incremented.
    QSKIP("This should be reached but not increment skip-count");
}

void tst_ExpectFail::xpassContinueXfailAbort() const
{
    ++failed;
    QEXPECT_FAIL("", "This test should xpass", Continue);
    QVERIFY(true);
    QEXPECT_FAIL("", "This test should xfail but not add to totals", Abort);
    QVERIFY(false);
    QVERIFY2(false, "This should not be reached");
}

void tst_ExpectFail::xpassAbortDataDrivenWithQVerify() const
{
    QFETCH(bool, shouldXPass);
    if (shouldXPass)
        ++failed;
    else
        ++passed;

    QEXPECT_FAIL("XPass", "This test-row should xpass", Abort);
    QVERIFY(true);

    // We should only get here if the test wasn't supposed to xpass.
    QVERIFY2(!shouldXPass, "Test failed to Abort on XPASS");
}

void tst_ExpectFail::xpassContinueDataDrivenWithQVerify() const
{
    QFETCH(bool, shouldXPass);
    if (shouldXPass)
        ++failed;
    else
        ++passed;

    QEXPECT_FAIL("XPass", "This test-row should xpass", Continue);
    QVERIFY(true);

    qDebug(shouldXPass ? "Test should Continue past XPASS" : "Test should simply PASS");
}

void tst_ExpectFail::xpassAbortDataDrivenWithQCompare() const
{
    QFETCH(bool, shouldXPass);
    if (shouldXPass)
        ++failed;
    else
        ++passed;

    QEXPECT_FAIL("XPass", "This test should xpass", Abort);
    QCOMPARE(1, 1);

    // We should only get here if the test wasn't supposed to xpass.
    QVERIFY2(!shouldXPass, "Test failed to Abort on XPASS");
}

void tst_ExpectFail::xpassContinueDataDrivenWithQCompare() const
{
    QFETCH(bool, shouldXPass);
    if (shouldXPass)
        ++failed;
    else
        ++passed;

    QEXPECT_FAIL("XPass", "This test should xpass", Continue);
    QCOMPARE(1, 1);

    qDebug(shouldXPass ? "Test should Continue past XPASS" : "Test should simply PASS");
}

QTEST_MAIN(tst_ExpectFail)
#include "tst_expectfail.moc"