Blame include/cppunit/extensions/TestFactoryRegistry.h

Packit 8c9aa0
#ifndef CPPUNIT_EXTENSIONS_TESTFACTORYREGISTRY_H
Packit 8c9aa0
#define CPPUNIT_EXTENSIONS_TESTFACTORYREGISTRY_H
Packit 8c9aa0
Packit 8c9aa0
#include <cppunit/Portability.h>
Packit 8c9aa0
Packit 8c9aa0
#if CPPUNIT_NEED_DLL_DECL
Packit 8c9aa0
#pragma warning( push )
Packit 8c9aa0
#pragma warning( disable: 4251)  // X needs to have dll-interface to be used by clients of class Z
Packit 8c9aa0
#endif
Packit 8c9aa0
Packit 8c9aa0
#include <set>
Packit 8c9aa0
#include <cppunit/extensions/TestFactory.h>
Packit 8c9aa0
#include <string>
Packit 8c9aa0
Packit 8c9aa0
CPPUNIT_NS_BEGIN
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
class TestSuite;
Packit 8c9aa0
Packit 8c9aa0
/*! \brief Registry for TestFactory.
Packit 8c9aa0
 * \ingroup CreatingTestSuite
Packit 8c9aa0
 *
Packit 8c9aa0
 * Notes that the registry \b DON'T assumes lifetime control for any registered tests
Packit 8c9aa0
 * anymore.
Packit 8c9aa0
 *
Packit 8c9aa0
 * The default registry is the registry returned by getRegistry() with the 
Packit 8c9aa0
 * default name parameter value.
Packit 8c9aa0
 *
Packit 8c9aa0
 * To register tests, use the macros:
Packit 8c9aa0
 * - CPPUNIT_TEST_SUITE_REGISTRATION(): to add tests in the default registry.
Packit 8c9aa0
 * - CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(): to add tests in a named registry.
Packit 8c9aa0
 *
Packit 8c9aa0
 * Example 1: retreiving a suite that contains all the test registered with
Packit 8c9aa0
 * CPPUNIT_TEST_SUITE_REGISTRATION().
Packit 8c9aa0
 * \code
Packit 8c9aa0
 * CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
Packit 8c9aa0
 * CppUnit::TestSuite *suite = registry.makeTest();
Packit 8c9aa0
 * \endcode
Packit 8c9aa0
 *
Packit 8c9aa0
 * Example 2: retreiving a suite that contains all the test registered with
Packit 8c9aa0
 * \link CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ..., "Math" )\endlink.
Packit 8c9aa0
 * \code
Packit 8c9aa0
 * CppUnit::TestFactoryRegistry &mathRegistry = CppUnit::TestFactoryRegistry::getRegistry( "Math" );
Packit 8c9aa0
 * CppUnit::TestSuite *mathSuite = mathRegistry.makeTest();
Packit 8c9aa0
 * \endcode
Packit 8c9aa0
 *
Packit 8c9aa0
 * Example 3: creating a test suite hierarchy composed of unnamed registration and
Packit 8c9aa0
 * named registration:
Packit 8c9aa0
 * - All Tests
Packit 8c9aa0
 *   - tests registered with CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ..., "Graph" )
Packit 8c9aa0
 *   - tests registered with CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ..., "Math" )
Packit 8c9aa0
 *   - tests registered with CPPUNIT_TEST_SUITE_REGISTRATION
Packit 8c9aa0
 *
Packit 8c9aa0
 * \code
Packit 8c9aa0
 * CppUnit::TestSuite *rootSuite = new CppUnit::TestSuite( "All tests" );
Packit 8c9aa0
 * rootSuite->addTest( CppUnit::TestFactoryRegistry::getRegistry( "Graph" ).makeTest() );
Packit 8c9aa0
 * rootSuite->addTest( CppUnit::TestFactoryRegistry::getRegistry( "Math" ).makeTest() );
Packit 8c9aa0
 * CppUnit::TestFactoryRegistry::getRegistry().addTestToSuite( rootSuite );
Packit 8c9aa0
 * \endcode
Packit 8c9aa0
 *
Packit 8c9aa0
 * The same result can be obtained with:
Packit 8c9aa0
 * \code
Packit 8c9aa0
 * CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
Packit 8c9aa0
 * registry.addRegistry( "Graph" );
Packit 8c9aa0
 * registry.addRegistry( "Math" );
Packit 8c9aa0
 * CppUnit::TestSuite *suite = registry.makeTest();
Packit 8c9aa0
 * \endcode
Packit 8c9aa0
 *
Packit 8c9aa0
 * Since a TestFactoryRegistry is a TestFactory, the named registries can be 
Packit 8c9aa0
 * registered in the unnamed registry, creating the hierarchy links.
Packit 8c9aa0
 *
Packit 8c9aa0
 * \see TestSuiteFactory, AutoRegisterSuite
Packit 8c9aa0
 * \see CPPUNIT_TEST_SUITE_REGISTRATION, CPPUNIT_TEST_SUITE_NAMED_REGISTRATION
Packit 8c9aa0
 */
Packit 8c9aa0
class CPPUNIT_API TestFactoryRegistry : public TestFactory
Packit 8c9aa0
{
Packit 8c9aa0
public:
Packit 8c9aa0
  /** Constructs the registry with the specified name.
Packit 8c9aa0
   * \param name Name of the registry. It is the name of TestSuite returned by
Packit 8c9aa0
   *             makeTest().
Packit 8c9aa0
   */
Packit 8c9aa0
  TestFactoryRegistry( std::string name );
Packit 8c9aa0
Packit 8c9aa0
  /// Destructor.
Packit 8c9aa0
  virtual ~TestFactoryRegistry();
Packit 8c9aa0
Packit 8c9aa0
  /** Returns a new TestSuite that contains the registered test.
Packit 8c9aa0
   * \return A new TestSuite which contains all the test added using 
Packit 8c9aa0
   * registerFactory(TestFactory *).
Packit 8c9aa0
   */
Packit 8c9aa0
  virtual Test *makeTest();
Packit 8c9aa0
Packit 8c9aa0
  /** Returns a named registry.
Packit 8c9aa0
   *
Packit 8c9aa0
   * If the \a name is left to its default value, then the registry that is returned is
Packit 8c9aa0
   * the one used by CPPUNIT_TEST_SUITE_REGISTRATION(): the 'top' level registry.
Packit 8c9aa0
   *
Packit 8c9aa0
   * \param name Name of the registry to return.
Packit 8c9aa0
   * \return Registry. If the registry does not exist, it is created with the
Packit 8c9aa0
   *         specified name.
Packit 8c9aa0
   */
Packit 8c9aa0
  static TestFactoryRegistry &getRegistry( const std::string &name = "All Tests" );
Packit 8c9aa0
Packit 8c9aa0
  /** Adds the registered tests to the specified suite.
Packit 8c9aa0
   * \param suite Suite the tests are added to.
Packit 8c9aa0
   */
Packit 8c9aa0
  void addTestToSuite( TestSuite *suite );
Packit 8c9aa0
Packit 8c9aa0
  /** Adds the specified TestFactory to the registry.
Packit 8c9aa0
   *
Packit 8c9aa0
   * \param factory Factory to register. 
Packit 8c9aa0
   */
Packit 8c9aa0
  void registerFactory( TestFactory *factory );
Packit 8c9aa0
Packit 8c9aa0
  /*! Removes the specified TestFactory from the registry.
Packit 8c9aa0
   * 
Packit 8c9aa0
   * The specified factory is not destroyed.
Packit 8c9aa0
   * \param factory Factory to remove from the registry.
Packit 8c9aa0
   * \todo Address case when trying to remove a TestRegistryFactory.
Packit 8c9aa0
   */
Packit 8c9aa0
  void unregisterFactory( TestFactory *factory );
Packit 8c9aa0
Packit 8c9aa0
  /*! Adds a registry to the registry.
Packit 8c9aa0
   * 
Packit 8c9aa0
   * Convenience method to help create test hierarchy. See TestFactoryRegistry detail
Packit 8c9aa0
   * for examples of use. Calling this method is equivalent to:
Packit 8c9aa0
   * \code
Packit 8c9aa0
   * this->registerFactory( TestFactoryRegistry::getRegistry( name ) );
Packit 8c9aa0
   * \endcode
Packit 8c9aa0
   *
Packit 8c9aa0
   * \param name Name of the registry to add.
Packit 8c9aa0
   */
Packit 8c9aa0
  void addRegistry( const std::string &name );
Packit 8c9aa0
Packit 8c9aa0
  /*! Tests if the registry is valid.
Packit 8c9aa0
   *
Packit 8c9aa0
   * This method should be used when unregistering test factory on static variable 
Packit 8c9aa0
   * destruction to ensure that the registry has not been already destroyed (in 
Packit 8c9aa0
   * that case there is no need to unregister the test factory).
Packit 8c9aa0
   *
Packit 8c9aa0
   * You should not concern yourself with this method unless you are writing a class
Packit 8c9aa0
   * like AutoRegisterSuite.
Packit 8c9aa0
   *
Packit 8c9aa0
   * \return \c true if the specified registry has not been destroyed, 
Packit 8c9aa0
   *         otherwise returns \c false.
Packit 8c9aa0
   * \see AutoRegisterSuite.
Packit 8c9aa0
   */
Packit 8c9aa0
  static bool isValid();
Packit 8c9aa0
Packit 8c9aa0
  /** Adds the specified TestFactory with a specific name (DEPRECATED).
Packit 8c9aa0
   * \param name Name associated to the factory.
Packit 8c9aa0
   * \param factory Factory to register. 
Packit 8c9aa0
   * \deprecated Use registerFactory( TestFactory *) instead.
Packit 8c9aa0
   */
Packit 8c9aa0
  void registerFactory( const std::string &name,
Packit 8c9aa0
                        TestFactory *factory );
Packit 8c9aa0
Packit 8c9aa0
private:
Packit 8c9aa0
  TestFactoryRegistry( const TestFactoryRegistry &copy );
Packit 8c9aa0
  void operator =( const TestFactoryRegistry &copy );
Packit 8c9aa0
Packit 8c9aa0
private:
Packit 8c9aa0
  typedef std::set<TestFactory *, std::less<TestFactory*> > Factories;
Packit 8c9aa0
  Factories m_factories;
Packit 8c9aa0
Packit 8c9aa0
  std::string m_name;
Packit 8c9aa0
};
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
CPPUNIT_NS_END
Packit 8c9aa0
Packit 8c9aa0
#if CPPUNIT_NEED_DLL_DECL
Packit 8c9aa0
#pragma warning( pop )
Packit 8c9aa0
#endif
Packit 8c9aa0
Packit 8c9aa0
Packit 8c9aa0
#endif  // CPPUNIT_EXTENSIONS_TESTFACTORYREGISTRY_H