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)
12 #endif
13 
14 
16 
17 #if 0
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 
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:
199  Fixture *m_fixture;
200  std::function<void()> m_test_function;
201 };
202 
203 
204 
206 
207 #endif // CPPUNIT_TESTCALLER_H
#define CPPUNIT_API
Definition: CppUnitApi.h:27
#define CPPUNIT_NS_END
Definition: Portability.h:106
#define CPPUNIT_NS_BEGIN
Definition: Portability.h:105
Exceptions thrown by failed assertions.
Definition: Exception.h:20
Message associated to an Exception.A message is composed of two items:
Definition: Message.h:35
Generate a test case from a fixture method.
Definition: TestCaller.h:107
TestCaller(std::string name, std::function< void()> test_function, Fixture *fixture)
Definition: TestCaller.h:159
void tearDown()
Clean up after the test run.
Definition: TestCaller.h:183
TestCaller(std::string name, TestMethod test, Fixture &fixture)
Definition: TestCaller.h:134
std::string toString() const
Definition: TestCaller.h:188
void(Fixture::* TestMethod)()
Definition: TestCaller.h:108
Fixture * m_fixture
Definition: TestCaller.h:199
TestCaller(std::string name, TestMethod test, Fixture *fixture)
Definition: TestCaller.h:151
TestCaller & operator=(const TestCaller &other)
~TestCaller()
Definition: TestCaller.h:167
TestCaller(std::string name, TestMethod test)
Definition: TestCaller.h:117
bool m_ownFixture
Definition: TestCaller.h:198
void runTest()
FIXME: this should probably be pure virtual.
Definition: TestCaller.h:173
void setUp()
Set up context before running a test.
Definition: TestCaller.h:178
TestCaller(const TestCaller &other)
std::function< void()> m_test_function
Definition: TestCaller.h:200
A single test object.
Definition: TestCase.h:29
std::string getName() const
Returns the name of the test case.
Definition: TestCase.cpp:131
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