Warning: That file was not part of the compilation database. It may have many parsing errors.
1 | /** |
---|---|
2 | * kunittest.h |
3 | * |
4 | * Copyright (C) 2004 Zack Rusin <zack@kde.org> |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ |
27 | |
28 | #ifndef KUNITTEST_RUNNER_H |
29 | #define KUNITTEST_RUNNER_H |
30 | |
31 | #include <iostream> |
32 | using namespace std; |
33 | |
34 | #include <QtCore/QObject> |
35 | #include <QtCore/QHash> |
36 | #include <QtCore/QString> |
37 | |
38 | #include "kunittest_export.h" |
39 | #include "tester.h" |
40 | |
41 | |
42 | namespace KUnitTest |
43 | { |
44 | /*! @def KUNITTEST_SUITE(suite) |
45 | * |
46 | * This macro must be used if you are not making a test-module. The macro |
47 | * defines the name of the test suite. |
48 | */ |
49 | #define KUNITTEST_SUITE(suite)\ |
50 | static const QString s_kunittest_suite = suite; |
51 | |
52 | /*! @def KUNITTEST_REGISTER_TESTER( tester ) |
53 | * @brief Automatic registration of Tester classes. |
54 | * |
55 | * This macro can be used to register the Tester into the global registry. Use |
56 | * this macro in the implementation file of your Tester class. If you keep the |
57 | * Tester classes in a shared or convenience library then you should not use this |
58 | * macro as this macro relies on the static initialization of a TesterAutoregister class. |
59 | * You can always use the static Runner::registerTester(const char *name, Tester *test) method. |
60 | */ |
61 | #define KUNITTEST_REGISTER_TESTER( tester )\ |
62 | static TesterAutoregister tester##Autoregister( QString(s_kunittest_suite + QString("::") + QString::fromLocal8Bit(#tester)).local8Bit() , new tester ()) |
63 | |
64 | #define KUNITTEST_REGISTER_NAMEDTESTER( name, tester )\ |
65 | static TesterAutoregister tester##Autoregister( QString(s_kunittest_suite + QString("::") + QString::fromLocal8Bit(name)).local8Bit() , new tester ()) |
66 | |
67 | /*! The type of the registry. */ |
68 | typedef QHash<QByteArray, Tester*> Registry; |
69 | |
70 | /*! The Runner class holds a list of registered Tester classes and is able |
71 | * to run those test cases. The Runner class follows the singleton design |
72 | * pattern, which means that you can only have one Runner instance. This |
73 | * instance can be retrieved using the Runner::self() method. |
74 | * |
75 | * The registry is an object of type Registry, it is able to map the name |
76 | * of a test to a pointer to a Tester object. The registry is also a singleton |
77 | * and can be accessed via Runner::registry(). Since there is only one registry, |
78 | * which can be accessed at all times, test cases can be added without having to |
79 | * worry if a Runner instance is present or not. This allows for a design in which |
80 | * the KUnitTest library can be kept separate from the test case sources. Test cases |
81 | * (classes inheriting from Tester) can be added using the static |
82 | * registerTester(const char *name, Tester *test) method. Allthough most users |
83 | * will want to use the KUNITTEST_REGISTER_TESTER macro. |
84 | * |
85 | * @see KUNITTEST_REGISTER_TESTER |
86 | */ |
87 | class KUNITTEST_EXPORT Runner : public QObject |
88 | { |
89 | Q_OBJECT |
90 | |
91 | public: |
92 | /*! Registers a test case. A registry will be automatically created if necessary. |
93 | * @param name The name of the test case. |
94 | * @param test A pointer to a Tester object. |
95 | */ |
96 | static void registerTester(const char *name, Tester *test); |
97 | |
98 | /*! @returns The registry holding all the Tester objects. |
99 | */ |
100 | Registry ®istry(); |
101 | |
102 | /*! @returns The global Runner instance. If necessary an instance will be created. |
103 | */ |
104 | static Runner *self(); |
105 | |
106 | /*! @returns The number of registered test cases. |
107 | */ |
108 | int numberOfTestCases(); |
109 | |
110 | /*! Load all modules found in the folder. |
111 | * @param folder The folder where to look for modules. |
112 | * @param query A regular expression. Only modules which match the query will be run. |
113 | */ |
114 | static void loadModules(const QString &folder, const QString &query); |
115 | |
116 | /*! The runner can spit out special debug messages needed by the Perl script: kunittest_debughelper. |
117 | * This script can attach the debug output of each suite to the results in the KUnitTest GUI. |
118 | * Not very useful for console minded developers, so this static method can be used to disable |
119 | * those debug messages. |
120 | * @param enabled If true the debug messages are enabled (default), otherwise they are disabled. |
121 | */ |
122 | static void setDebugCapturingEnabled(bool enabled); |
123 | |
124 | private: |
125 | Registry m_registry; |
126 | static Runner *s_self; |
127 | static bool s_debugCapturingEnabled; |
128 | |
129 | protected: |
130 | Runner(); |
131 | |
132 | public: |
133 | /*! @returns The number of finished tests. */ |
134 | int numberOfTests() const; |
135 | |
136 | /*! @returns The number of passed tests. */ |
137 | int numberOfPassedTests() const; |
138 | |
139 | /*! @returns The number of failed tests, this includes the number of expected failures. */ |
140 | int numberOfFailedTests() const; |
141 | |
142 | /*! @returns The number of failed tests which were expected. */ |
143 | int numberOfExpectedFailures() const; |
144 | |
145 | /*! @returns The number of skipped tests. */ |
146 | int numberOfSkippedTests() const; |
147 | |
148 | public Q_SLOTS: |
149 | /*! Call this slot to run all the registered tests. |
150 | * @returns The number of finished tests. |
151 | */ |
152 | int runTests(); |
153 | |
154 | /*! Call this slot to run a single test. |
155 | * @param name The name of the test case. This name has to correspond to the name |
156 | * that was used to register the test. If the KUNITTEST_REGISTER_TESTER macro was |
157 | * used to register the test case then this name is the class name. |
158 | */ |
159 | void runTest(const char *name); |
160 | |
161 | /*! Call this slot to run tests with names starting with prefix. |
162 | * @param prefix Only run tests starting with the string prefix. |
163 | */ |
164 | void runMatchingTests(const QString &prefix); |
165 | |
166 | /*! Reset the Runner in order to prepare it to run one or more tests again. |
167 | */ |
168 | void reset(); |
169 | |
170 | Q_SIGNALS: |
171 | /*! Emitted after a test is finished. |
172 | * @param name The name of the test. |
173 | * @param test A pointer to the Tester object. |
174 | */ |
175 | void finished(const char *name, Tester *test); |
176 | void invoke(); |
177 | |
178 | private: |
179 | void registerTests(); |
180 | |
181 | private: |
182 | int globalSteps; |
183 | int globalTests; |
184 | int globalPasses; |
185 | int globalFails; |
186 | int globalXFails; |
187 | int globalXPasses; |
188 | int globalSkipped; |
189 | }; |
190 | |
191 | /*! The TesterAutoregister is a helper class to allow the automatic registration |
192 | * of Tester classes. |
193 | */ |
194 | class TesterAutoregister |
195 | { |
196 | public: |
197 | /*! @param name A unique name that identifies the Tester class. |
198 | * @param test A pointer to a Tester object. |
199 | */ |
200 | TesterAutoregister(const char *name, Tester *test) |
201 | { |
202 | if ( test->objectName().isNull()) |
203 | test->setObjectName(QLatin1String(name)); |
204 | Runner::registerTester(name, test); |
205 | } |
206 | }; |
207 | |
208 | } |
209 | |
210 | #endif |
211 |
Warning: That file was not part of the compilation database. It may have many parsing errors.