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
21class CPPUNIT_API NoExceptionExpected
22{
23private:
25 NoExceptionExpected();
26};
27
28
33template<class ExceptionType>
34struct 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
55template<>
56struct 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
105template <class Fixture>
106 class TestCaller : public TestCase
107{
108 typedef void (Fixture::*TestMethod)();
109
110public:
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
193private:
194 TestCaller( const TestCaller &other );
195 TestCaller &operator =( const TestCaller &other );
196
197private:
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
Exception.h
CPPUNIT_NS_END
#define CPPUNIT_NS_END
Definition Portability.h:106
CPPUNIT_NS_BEGIN
#define CPPUNIT_NS_BEGIN
Definition Portability.h:105
TestCase.h
TypeInfoHelper.h
Exception
Exceptions thrown by failed assertions.
Definition Exception.h:20
Message
Message associated to an Exception.A message is composed of two items:
Definition Message.h:35
TestCaller
Generate a test case from a fixture method.
Definition TestCaller.h:107
TestCaller::TestCaller
TestCaller(std::string name, std::function< void()> test_function, Fixture *fixture)
Definition TestCaller.h:159
TestCaller::tearDown
void tearDown()
Clean up after the test run.
Definition TestCaller.h:183
TestCaller::TestCaller
TestCaller(std::string name, TestMethod test, Fixture &fixture)
Definition TestCaller.h:134
TestCaller::toString
std::string toString() const
Definition TestCaller.h:188
TestCaller::TestMethod
void(Fixture::* TestMethod)()
Definition TestCaller.h:108
TestCaller::m_fixture
Fixture * m_fixture
Definition TestCaller.h:199
TestCaller::TestCaller
TestCaller(std::string name, TestMethod test, Fixture *fixture)
Definition TestCaller.h:151
TestCaller::operator=
TestCaller & operator=(const TestCaller &other)
TestCaller::~TestCaller
~TestCaller()
Definition TestCaller.h:167
TestCaller::TestCaller
TestCaller(std::string name, TestMethod test)
Definition TestCaller.h:117
TestCaller::m_ownFixture
bool m_ownFixture
Definition TestCaller.h:198
TestCaller::runTest
void runTest()
FIXME: this should probably be pure virtual.
Definition TestCaller.h:173
TestCaller::setUp
void setUp()
Set up context before running a test.
Definition TestCaller.h:178
TestCaller::TestCaller
TestCaller(const TestCaller &other)
TestCaller::m_test_function
std::function< void()> m_test_function
Definition TestCaller.h:200
TestCase
A single test object.
Definition TestCase.h:29
TestCase::getName
std::string getName() const
Returns the name of the test case.
Definition TestCase.cpp:131
TypeInfoHelper::getClassName
static std::string getClassName(const std::type_info &info)
Get the class name of the specified type_info.
Definition TypeInfoHelper.cpp:15

| Send comments to: CppUnit Developers