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
|
#ifndef CPPUNIT_PARAMETERIZEDTESTCALLER_H // -*- C++ -*-
#define CPPUNIT_PARAMETERIZEDTESTCALLER_H
#include <cppunit/Exception.h>
#include <cppunit/TestCase.h>
#if CPPUNIT_USE_TYPEINFO_NAME
# include <cppunit/extensions/TypeInfoHelper.h>
#endif
CPPUNIT_NS_BEGIN
#if 0
/*! \brief Marker class indicating that no exception is expected by TestCaller.
* This class is an implementation detail. You should never use this class directly.
*/
class CPPUNIT_API NoExceptionExpected
{
private:
//! Prevent class instantiation.
NoExceptionExpected();
};
/*! \brief (Implementation) Traits used by TestCaller to expect an exception.
*
* This class is an implementation detail. You should never use this class directly.
*/
template<class ExceptionType>
struct ExpectedExceptionTraits
{
static void expectedException()
{
#if CPPUNIT_USE_TYPEINFO_NAME
throw Exception( Message(
"expected exception not thrown",
"Expected exception type: " +
TypeInfoHelper::getClassName( typeid( ExceptionType ) ) ) );
#else
throw Exception( "expected exception not thrown" );
#endif
}
};
/*! \brief (Implementation) Traits specialization used by TestCaller to
* expect no exception.
*
* This class is an implementation detail. You should never use this class directly.
*/
template<>
struct ExpectedExceptionTraits<NoExceptionExpected>
{
static void expectedException()
{
}
};
#endif
//*** FIXME: rework this when class Fixture is implemented. ***//
/*! \brief Generate a test case from a fixture method.
* \ingroup WritingTestFixture
*
* A test caller provides access to a test case method
* on a test fixture class. Test callers are useful when
* you want to run an individual test or add it to a
* suite.
* Test Callers invoke only one Test (i.e. test method) on one
* Fixture of a TestFixture.
*
* Here is an example:
* \code
* class MathTest : public CppUnit::TestFixture {
* ...
* public:
* void setUp();
* void tearDown();
*
* void testAdd();
* void testSubtract();
* };
*
* CppUnit::Test *MathTest::suite() {
* CppUnit::TestSuite *suite = new CppUnit::TestSuite;
*
* suite->addTest( new CppUnit::TestCaller<MathTest>( "testAdd", testAdd ) );
* return suite;
* }
* \endcode
*
* You can use a TestCaller to bind any test method on a TestFixture
* class, as long as it accepts void and returns void.
*
* \see TestCase
*/
template <class Fixture, typename ParameterType>
class ParameterizedTestCaller : public TestCase
{
typedef void (Fixture::*TestMethod)(const ParameterType&);
public:
/*!
* Constructor for ParameterizedTestCaller. This constructor builds a new Fixture
* instance owned by the ParameterizedTestCaller.
* \param name name of this ParameterizedTestCaller
* \param test the method this ParameterizedTestCaller calls in runTest()
*/
ParameterizedTestCaller( std::string name, TestMethod test, const std::initializer_list<ParameterType>& parameter_list ) :
TestCase( name ),
m_ownFixture( true ),
m_fixture( new Fixture() ),
m_test( test ),
m_parameter_list(parameter_list)
{
}
/*!
* Constructor for ParameterizedTestCaller.
* This constructor does not create a new Fixture instance but accepts
* an existing one as parameter. The ParameterizedTestCaller will not own the
* Fixture object.
* \param name name of this ParameterizedTestCaller
* \param test the method this ParameterizedTestCaller calls in runTest()
* \param fixture the Fixture to invoke the test method on.
*/
ParameterizedTestCaller(std::string name, TestMethod test, Fixture& fixture,
const std::initializer_list<ParameterType>& parameter_list) :
TestCase( name ),
m_ownFixture( false ),
m_fixture( &fixture ),
m_test( test ),
m_parameter_list(parameter_list)
{
}
/*!
* Constructor for ParameterizedTestCaller.
* This constructor does not create a new Fixture instance but accepts
* an existing one as parameter. The ParameterizedTestCaller will own the
* Fixture object and delete it in its destructor.
* \param name name of this ParameterizedTestCaller
* \param test the method this ParameterizedTestCaller calls in runTest()
* \param fixture the Fixture to invoke the test method on.
*/
ParameterizedTestCaller(std::string name, TestMethod test, Fixture* fixture,
const std::initializer_list<ParameterType>& parameter_list) :
TestCase( name ),
m_ownFixture( true ),
m_fixture( fixture ),
m_test( test ),
m_parameter_list(parameter_list)
{
}
~ParameterizedTestCaller()
{
if (m_ownFixture)
delete m_fixture;
}
void runTest()
{
for (auto itr = m_parameter_list.begin(), itrEnd = m_parameter_list.end();
itr != itrEnd; ++itr)
{
(m_fixture->*m_test)(*itr);
}
}
void setUp()
{
m_fixture->setUp ();
}
void tearDown()
{
m_fixture->tearDown ();
}
std::string toString() const
{
return "ParameterizedTestCaller " + getName();
}
int countTestCases() const
{
return m_parameter_list.size();
}
private:
ParameterizedTestCaller( const ParameterizedTestCaller &other );
ParameterizedTestCaller &operator =( const ParameterizedTestCaller &other );
private:
bool m_ownFixture;
Fixture *m_fixture;
TestMethod m_test;
std::initializer_list<ParameterType> m_parameter_list;
};
CPPUNIT_NS_END
#endif // CPPUNIT_TESTCALLER_H
|