Blame include/cppunit/TestCaller.h

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