AutoRegisterSuite.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef CPPUNIT_EXTENSIONS_AUTOREGISTERSUITE_H
  2. #define CPPUNIT_EXTENSIONS_AUTOREGISTERSUITE_H
  3. #include <cppunit/extensions/TestSuiteFactory.h>
  4. #include <cppunit/extensions/TestFactoryRegistry.h>
  5. #include <string>
  6. CPPUNIT_NS_BEGIN
  7. /*! \brief (Implementation) Automatically register the test suite of the specified type.
  8. *
  9. * You should not use this class directly. Instead, use the following macros:
  10. * - CPPUNIT_TEST_SUITE_REGISTRATION()
  11. * - CPPUNIT_TEST_SUITE_NAMED_REGISTRATION()
  12. *
  13. * This object will register the test returned by TestCaseType::suite()
  14. * when constructed to the test registry.
  15. *
  16. * This object is intented to be used as a static variable.
  17. *
  18. *
  19. * \param TestCaseType Type of the test case which suite is registered.
  20. * \see CPPUNIT_TEST_SUITE_REGISTRATION, CPPUNIT_TEST_SUITE_NAMED_REGISTRATION
  21. * \see CppUnit::TestFactoryRegistry.
  22. */
  23. template<class TestCaseType>
  24. class AutoRegisterSuite
  25. {
  26. public:
  27. /** Auto-register the suite factory in the global registry.
  28. */
  29. AutoRegisterSuite()
  30. : m_registry( &TestFactoryRegistry::getRegistry() )
  31. {
  32. m_registry->registerFactory( &m_factory );
  33. }
  34. /** Auto-register the suite factory in the specified registry.
  35. * \param name Name of the registry.
  36. */
  37. AutoRegisterSuite( const std::string &name )
  38. : m_registry( &TestFactoryRegistry::getRegistry( name ) )
  39. {
  40. m_registry->registerFactory( &m_factory );
  41. }
  42. ~AutoRegisterSuite()
  43. {
  44. if ( TestFactoryRegistry::isValid() )
  45. m_registry->unregisterFactory( &m_factory );
  46. }
  47. private:
  48. TestFactoryRegistry *m_registry;
  49. TestSuiteFactory<TestCaseType> m_factory;
  50. };
  51. /*! \brief (Implementation) Automatically adds a registry into another registry.
  52. *
  53. * Don't use this class. Use the macros CPPUNIT_REGISTRY_ADD() and
  54. * CPPUNIT_REGISTRY_ADD_TO_DEFAULT() instead.
  55. */
  56. class AutoRegisterRegistry
  57. {
  58. public:
  59. AutoRegisterRegistry( const std::string &which,
  60. const std::string &to )
  61. {
  62. TestFactoryRegistry::getRegistry( to ).addRegistry( which );
  63. }
  64. AutoRegisterRegistry( const std::string &which )
  65. {
  66. TestFactoryRegistry::getRegistry().addRegistry( which );
  67. }
  68. };
  69. CPPUNIT_NS_END
  70. #endif // CPPUNIT_EXTENSIONS_AUTOREGISTERSUITE_H