1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31
32
33#ifndef QT_NO_EXCEPTIONS
34# include <stdexcept>
35#endif
36
37
38#ifndef QT_NO_EXCEPTIONS
39
40class MyBaseException
41{
42
43};
44
45class MyDerivedException: public MyBaseException, public std::domain_error
46{
47public:
48 MyDerivedException(): std::domain_error("MyDerivedException") {}
49};
50
51
52#endif // !QT_NO_EXCEPTIONS
53
54
55class tst_VerifyExceptionThrown: public QObject
56{
57 Q_OBJECT
58private:
59 void doSomething() const {}
60
61private slots:
62// Remove all test cases if exceptions are not available
63#ifndef QT_NO_EXCEPTIONS
64 void testCorrectStdTypes() const;
65 void testCorrectStdExceptions() const;
66 void testCorrectMyExceptions() const;
67
68 void testFailInt() const;
69 void testFailStdString() const;
70 void testFailStdRuntimeError() const;
71 void testFailMyException() const;
72 void testFailMyDerivedException() const;
73
74 void testFailNoException() const;
75#endif // !QT_NO_EXCEPTIONS
76};
77
78
79
80#ifndef QT_NO_EXCEPTIONS
81
82void tst_VerifyExceptionThrown::testCorrectStdTypes() const
83{
84 QVERIFY_EXCEPTION_THROWN(throw int(5), int);
85 QVERIFY_EXCEPTION_THROWN(throw float(9.8), float);
86 QVERIFY_EXCEPTION_THROWN(throw bool(true), bool);
87 QVERIFY_EXCEPTION_THROWN(throw std::string("some string"), std::string);
88}
89
90void tst_VerifyExceptionThrown::testCorrectStdExceptions() const
91{
92 // same type
93 QVERIFY_EXCEPTION_THROWN(throw std::exception(), std::exception);
94 QVERIFY_EXCEPTION_THROWN(throw std::runtime_error("runtime error"), std::runtime_error);
95 QVERIFY_EXCEPTION_THROWN(throw std::overflow_error("overflow error"), std::overflow_error);
96
97 // inheritance
98 QVERIFY_EXCEPTION_THROWN(throw std::overflow_error("overflow error"), std::runtime_error);
99 QVERIFY_EXCEPTION_THROWN(throw std::overflow_error("overflow error"), std::exception);
100}
101
102void tst_VerifyExceptionThrown::testCorrectMyExceptions() const
103{
104 // same type
105 QVERIFY_EXCEPTION_THROWN(throw MyBaseException(), MyBaseException);
106 QVERIFY_EXCEPTION_THROWN(throw MyDerivedException(), MyDerivedException);
107
108 // inheritance
109 QVERIFY_EXCEPTION_THROWN(throw MyDerivedException(), MyBaseException);
110 QVERIFY_EXCEPTION_THROWN(throw MyDerivedException(), std::domain_error);
111}
112
113void tst_VerifyExceptionThrown::testFailInt() const
114{
115 try {
116 QVERIFY_EXCEPTION_THROWN(throw int(5), double);
117 } catch (int) {}
118}
119
120void tst_VerifyExceptionThrown::testFailStdString() const
121{
122 try {
123 QVERIFY_EXCEPTION_THROWN(throw std::string("some string"), char*);
124 } catch (const std::string &) {}
125}
126
127void tst_VerifyExceptionThrown::testFailStdRuntimeError() const
128{
129 QVERIFY_EXCEPTION_THROWN(throw std::logic_error("logic error"), std::runtime_error);
130}
131
132void tst_VerifyExceptionThrown::testFailMyException() const
133{
134 QVERIFY_EXCEPTION_THROWN(throw std::logic_error("logic error"), MyBaseException);
135}
136
137void tst_VerifyExceptionThrown::testFailMyDerivedException() const
138{
139 QVERIFY_EXCEPTION_THROWN(throw MyDerivedException(), std::runtime_error);
140}
141
142void tst_VerifyExceptionThrown::testFailNoException() const
143{
144 QVERIFY_EXCEPTION_THROWN(doSomething(), std::exception);
145}
146
147#endif // !QT_NO_EXCEPTIONS
148
149
150
151QTEST_MAIN(tst_VerifyExceptionThrown)
152
153#include "tst_verifyexceptionthrown.moc"
154

source code of qtbase/tests/auto/testlib/selftests/verifyexceptionthrown/tst_verifyexceptionthrown.cpp