CppUnit - The Unit Testing Library

CppUnit project page | FAQ

TestCaller.h
Go to the documentation of this file.
1 #ifndef CPPUNIT_TESTCALLER_H // -*- C++ -*-
2 #define CPPUNIT_TESTCALLER_H
3
4 #include <cppunit/Exception.h>
5 #include <cppunit/TestCase.h>
6
7 #include <functional>
8
9
10 #if defined(CPPUNIT_USE_TYPEINFO_NAME)
11 # include <cppunit/extensions/TypeInfoHelper.h>
12 #endif
13
14
15 CPPUNIT_NS_BEGIN
16
17 #if 0
18
21 class CPPUNIT_API NoExceptionExpected
22 {
23 private:
25 NoExceptionExpected();
26 };
27
28
33 template<class ExceptionType>
34 struct ExpectedExceptionTraits
35 {
36 static void expectedException()
37 {
38 #if defined(CPPUNIT_USE_TYPEINFO_NAME)
39 throw Exception( Message(
40 "expected exception not thrown",
41 "Expected exception type: " +
42 TypeInfoHelper::getClassName( typeid( ExceptionType ) ) ) );
43 #else
44 throw Exception( "expected exception not thrown" );
45 #endif
46 }
47 };
48
49
55 template<>
56 struct ExpectedExceptionTraits<NoExceptionExpected>
57 {
58 static void expectedException()
59 {
60 }
61 };
62
63
64 #endif
65
66 //*** FIXME: rework this when class Fixture is implemented. ***//
67
68
105 template <class Fixture>
106 class TestCaller : public TestCase
107 {
108 typedef void (Fixture::*TestMethod)();
109
110 public:
117 TestCaller( std::string name, TestMethod test ) :
118 TestCase( name ),
119 m_ownFixture( true ),
120 m_fixture( new Fixture() ),
121 m_test_function( std::bind(test, m_fixture) )
122 {
123 }
124
134 TestCaller(std::string name, TestMethod test, Fixture& fixture) :
135 TestCase( name ),
136 m_ownFixture( false ),
137 m_fixture( &fixture ),
138 m_test_function( std::bind(test, &fixture) )
139 {
140 }
141
151 TestCaller(std::string name, TestMethod test, Fixture* fixture) :
152 TestCase( name ),
153 m_ownFixture( true ),
154 m_fixture( fixture ),
155 m_test_function( std::bind(test, fixture) )
156 {
157 }
158
159 TestCaller(std::string name, std::function<void()> test_function, Fixture* fixture):
160 TestCase(name),
161 m_ownFixture(true),
162 m_fixture(fixture),
163 m_test_function(test_function)
164 {
165 }
166
167 ~TestCaller()
168 {
169 if (m_ownFixture)
170 delete m_fixture;
171 }
172
173 void runTest()
174 {
175 m_test_function();
176 }
177
178 void setUp()
179 {
180 m_fixture->setUp ();
181 }
182
183 void tearDown()
184 {
185 m_fixture->tearDown ();
186 }
187
188 std::string toString() const
189 {
190 return "TestCaller " + getName();
191 }
192
193 private:
194 TestCaller( const TestCaller &other );
195 TestCaller &operator =( const TestCaller &other );
196
197 private:
198 bool m_ownFixture;
199 Fixture *m_fixture;
200 std::function<void()> m_test_function;
201 };
202
203
204
205 CPPUNIT_NS_END
206
207 #endif // CPPUNIT_TESTCALLER_H
CPPUNIT_API
#define CPPUNIT_API
Definition: CppUnitApi.h:27
TestCaller::operator=
TestCaller & operator=(const TestCaller &other)
TestCase.h
TestCaller::m_test_function
std::function< void()> m_test_function
Definition: TestCaller.h:200
TestCaller::m_fixture
Fixture * m_fixture
Definition: TestCaller.h:199
TestCaller::setUp
void setUp()
Set up context before running a test.
Definition: TestCaller.h:178
TestCaller::m_ownFixture
bool m_ownFixture
Definition: TestCaller.h:198
Exception
Exceptions thrown by failed assertions.
Definition: Exception.h:20
Message
Message associated to an Exception.
Definition: Message.h:35
TypeInfoHelper.h
TestCaller::TestMethod
void(Fixture::* TestMethod)()
Definition: TestCaller.h:108
TestCase::getName
std::string getName() const
Returns the name of the test case.
Definition: TestCase.cpp:131
TestCaller::toString
std::string toString() const
Definition: TestCaller.h:188
TestCaller::tearDown
void tearDown()
Clean up after the test run.
Definition: TestCaller.h:183
TypeInfoHelper::getClassName
static std::string getClassName(const std::type_info &info)
Get the class name of the specified type_info.
Definition: TypeInfoHelper.cpp:15
TestCaller::TestCaller
TestCaller(std::string name, TestMethod test)
Definition: TestCaller.h:117
TestCaller::TestCaller
TestCaller(std::string name, TestMethod test, Fixture *fixture)
Definition: TestCaller.h:151
TestCaller
Generate a test case from a fixture method.
Definition: TestCaller.h:107
TestCase
A single test object.
Definition: TestCase.h:29
TestCaller::TestCaller
TestCaller(std::string name, std::function< void()> test_function, Fixture *fixture)
Definition: TestCaller.h:159
CPPUNIT_NS_BEGIN
#define CPPUNIT_NS_BEGIN
Definition: Portability.h:105
CPPUNIT_NS_END
#define CPPUNIT_NS_END
Definition: Portability.h:106
TestCaller::TestCaller
TestCaller(std::string name, TestMethod test, Fixture &fixture)
Definition: TestCaller.h:134
TestCaller::TestCaller
TestCaller(const TestCaller &other)
TestCaller::runTest
void runTest()
FIXME: this should probably be pure virtual.
Definition: TestCaller.h:173
TestCaller::~TestCaller
~TestCaller()
Definition: TestCaller.h:167
Exception.h

| Send comments to: CppUnit Developers