Menu

[r55]: / trunk / cppunit / include / cppunit / TestCaller.h  Maximize  Restore  History

Download this file

104 lines (87 with data), 2.6 kB

  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
#ifndef CPPUNIT_TESTCALLER_H
#define CPPUNIT_TESTCALLER_H
#ifndef CPPUNIT_TESTCASE_H
#include "TestCase.h"
#endif
#include <memory>
namespace CppUnit {
/** Provides access to a test case method.
* A test caller provides access to a test case method
* on a test case 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 TestCase.
*
* Here is an example:
* \code
* class MathTest : public CppUnit::TestCase {
* ...
* 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 TestCase
* class, as long as it accepts void and returns void.
* TestCallers are automatically registered in the TestRegistry.
*
* \see TestCase
*/
template <class Fixture>
class TestCaller : public TestCase
{
typedef void (Fixture::*TestMethod)();
public:
TestCaller (std::string name, TestMethod test) :
TestCase (name),
m_fixture (new Fixture ()),
m_test (test)
{}
protected:
void runTest ()
{
(m_fixture.get ()->*m_test)();
}
void setUp ()
{
m_fixture.get ()->setUp ();
}
void tearDown ()
{
m_fixture.get ()->tearDown ();
}
std::string toString () const
{
return "TestCaller " + getName();
}
private:
TestCaller (const TestCaller& other);
TestCaller& operator= (const TestCaller& other);
private:
std::auto_ptr<Fixture> m_fixture;
TestMethod m_test;
};
/** Returns a TestCaller for the specified method.
* \param name Name for the TestCaller.
* \param testMethod Method called by the TestCaller.
* \return TestCaller for the specified method.
*/
template<class Fixture>
Test *makeTestCaller( std::string name, void (Fixture::*testMethod)() )
{
return new TestCaller<Fixture>( name, testMethod );
}
} // namespace CppUnit
#endif // CPPUNIT_TESTCALLER_H
MongoDB Logo MongoDB