TestFactoryRegistry.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef CPPUNIT_EXTENSIONS_TESTFACTORYREGISTRY_H
  2. #define CPPUNIT_EXTENSIONS_TESTFACTORYREGISTRY_H
  3. #include <cppunit/Portability.h>
  4. #if CPPUNIT_NEED_DLL_DECL
  5. #pragma warning( push )
  6. #pragma warning( disable: 4251) // X needs to have dll-interface to be used by clients of class Z
  7. #endif
  8. #include <cppunit/portability/CppUnitSet.h>
  9. #include <cppunit/extensions/TestFactory.h>
  10. #include <string>
  11. CPPUNIT_NS_BEGIN
  12. class TestSuite;
  13. #if CPPUNIT_NEED_DLL_DECL
  14. // template class CPPUNIT_API std::set<TestFactory *>;
  15. #endif
  16. /*! \brief Registry for TestFactory.
  17. * \ingroup CreatingTestSuite
  18. *
  19. * Notes that the registry \b DON'T assumes lifetime control for any registered tests
  20. * anymore.
  21. *
  22. * The <em>default</em> registry is the registry returned by getRegistry() with the
  23. * default name parameter value.
  24. *
  25. * To register tests, use the macros:
  26. * - CPPUNIT_TEST_SUITE_REGISTRATION(): to add tests in the default registry.
  27. * - CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(): to add tests in a named registry.
  28. *
  29. * Example 1: retreiving a suite that contains all the test registered with
  30. * CPPUNIT_TEST_SUITE_REGISTRATION().
  31. * \code
  32. * CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
  33. * CppUnit::TestSuite *suite = registry.makeTest();
  34. * \endcode
  35. *
  36. * Example 2: retreiving a suite that contains all the test registered with
  37. * \link CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ..., "Math" )\endlink.
  38. * \code
  39. * CppUnit::TestFactoryRegistry &mathRegistry = CppUnit::TestFactoryRegistry::getRegistry( "Math" );
  40. * CppUnit::TestSuite *mathSuite = mathRegistry.makeTest();
  41. * \endcode
  42. *
  43. * Example 3: creating a test suite hierarchy composed of unnamed registration and
  44. * named registration:
  45. * - All Tests
  46. * - tests registered with CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ..., "Graph" )
  47. * - tests registered with CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ..., "Math" )
  48. * - tests registered with CPPUNIT_TEST_SUITE_REGISTRATION
  49. *
  50. * \code
  51. * CppUnit::TestSuite *rootSuite = new CppUnit::TestSuite( "All tests" );
  52. * rootSuite->addTest( CppUnit::TestFactoryRegistry::getRegistry( "Graph" ).makeTest() );
  53. * rootSuite->addTest( CppUnit::TestFactoryRegistry::getRegistry( "Math" ).makeTest() );
  54. * CppUnit::TestFactoryRegistry::getRegistry().addTestToSuite( rootSuite );
  55. * \endcode
  56. *
  57. * The same result can be obtained with:
  58. * \code
  59. * CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
  60. * registry.addRegistry( "Graph" );
  61. * registry.addRegistry( "Math" );
  62. * CppUnit::TestSuite *suite = registry.makeTest();
  63. * \endcode
  64. *
  65. * Since a TestFactoryRegistry is a TestFactory, the named registries can be
  66. * registered in the unnamed registry, creating the hierarchy links.
  67. *
  68. * \see TestSuiteFactory, AutoRegisterSuite
  69. * \see CPPUNIT_TEST_SUITE_REGISTRATION, CPPUNIT_TEST_SUITE_NAMED_REGISTRATION
  70. */
  71. class CPPUNIT_API TestFactoryRegistry : public TestFactory
  72. {
  73. public:
  74. /** Constructs the registry with the specified name.
  75. * \param name Name of the registry. It is the name of TestSuite returned by
  76. * makeTest().
  77. */
  78. TestFactoryRegistry( std::string name );
  79. /// Destructor.
  80. virtual ~TestFactoryRegistry();
  81. /** Returns a new TestSuite that contains the registered test.
  82. * \return A new TestSuite which contains all the test added using
  83. * registerFactory(TestFactory *).
  84. */
  85. virtual Test *makeTest();
  86. /** Returns a named registry.
  87. *
  88. * If the \a name is left to its default value, then the registry that is returned is
  89. * the one used by CPPUNIT_TEST_SUITE_REGISTRATION(): the 'top' level registry.
  90. *
  91. * \param name Name of the registry to return.
  92. * \return Registry. If the registry does not exist, it is created with the
  93. * specified name.
  94. */
  95. static TestFactoryRegistry &getRegistry( const std::string &name = "All Tests" );
  96. /** Adds the registered tests to the specified suite.
  97. * \param suite Suite the tests are added to.
  98. */
  99. void addTestToSuite( TestSuite *suite );
  100. /** Adds the specified TestFactory to the registry.
  101. *
  102. * \param factory Factory to register.
  103. */
  104. void registerFactory( TestFactory *factory );
  105. /*! Removes the specified TestFactory from the registry.
  106. *
  107. * The specified factory is not destroyed.
  108. * \param factory Factory to remove from the registry.
  109. * \todo Address case when trying to remove a TestRegistryFactory.
  110. */
  111. void unregisterFactory( TestFactory *factory );
  112. /*! Adds a registry to the registry.
  113. *
  114. * Convenience method to help create test hierarchy. See TestFactoryRegistry detail
  115. * for examples of use. Calling this method is equivalent to:
  116. * \code
  117. * this->registerFactory( TestFactoryRegistry::getRegistry( name ) );
  118. * \endcode
  119. *
  120. * \param name Name of the registry to add.
  121. */
  122. void addRegistry( const std::string &name );
  123. /*! Tests if the registry is valid.
  124. *
  125. * This method should be used when unregistering test factory on static variable
  126. * destruction to ensure that the registry has not been already destroyed (in
  127. * that case there is no need to unregister the test factory).
  128. *
  129. * You should not concern yourself with this method unless you are writing a class
  130. * like AutoRegisterSuite.
  131. *
  132. * \return \c true if the specified registry has not been destroyed,
  133. * otherwise returns \c false.
  134. * \see AutoRegisterSuite.
  135. */
  136. static bool isValid();
  137. /** Adds the specified TestFactory with a specific name (DEPRECATED).
  138. * \param name Name associated to the factory.
  139. * \param factory Factory to register.
  140. * \deprecated Use registerFactory( TestFactory *) instead.
  141. */
  142. void registerFactory( const std::string &name,
  143. TestFactory *factory );
  144. private:
  145. TestFactoryRegistry( const TestFactoryRegistry &copy );
  146. void operator =( const TestFactoryRegistry &copy );
  147. private:
  148. typedef CppUnitSet<TestFactory *, std::less<TestFactory*> > Factories;
  149. Factories m_factories;
  150. std::string m_name;
  151. };
  152. CPPUNIT_NS_END
  153. #if CPPUNIT_NEED_DLL_DECL
  154. #pragma warning( pop )
  155. #endif
  156. #endif // CPPUNIT_EXTENSIONS_TESTFACTORYREGISTRY_H