1#ifndef CPPUNIT_EXTENSIONS_TESTFACTORYREGISTRY_H
2#define CPPUNIT_EXTENSIONS_TESTFACTORYREGISTRY_H
3
4#include <cppunit/Portability.h>
5
6#if CPPUNIT_NEED_DLL_DECL
7#pragma warning( push )
8#pragma warning( disable: 4251 )  // X needs to have dll-interface to be used by clients of class Z
9#endif
10
11#include <cppunit/extensions/TestFactory.h>
12#include <map>
13#include <string>
14
15namespace CppUnit {
16
17class TestSuite;
18
19#if CPPUNIT_NEED_DLL_DECL
20  template class CPPUNIT_API map<string, TestFactory *>;
21#endif
22
23
24/*! \brief Registry for TestFactory.
25 * \ingroup CreatingTestSuite
26 *
27 * Notes that the registry assumes lifetime control for any registered test.
28 *
29 * To register tests, use the macros:
30 * - CPPUNIT_TEST_SUITE_REGISTRATION(): to add tests in the unnamed registry.
31 * - CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(): to add tests in a named registry.
32 *
33 * Example 1: retreiving a suite that contains all the test registered with
34 * CPPUNIT_TEST_SUITE_REGISTRATION().
35 * \code
36 * CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
37 * CppUnit::TestSuite *suite = registry.makeTest();
38 * \endcode
39 *
40 * Example 2: retreiving a suite that contains all the test registered with
41 * \link CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ..., "Math" )\endlink.
42 * \code
43 * CppUnit::TestFactoryRegistry &mathRegistry = CppUnit::TestFactoryRegistry::getRegistry( "Math" );
44 * CppUnit::TestSuite *mathSuite = mathRegistry.makeTest();
45 * \endcode
46 *
47 * Example 3: creating a test suite hierarchy composed of unnamed registration and
48 * named registration:
49 * - All Tests
50 *   - tests registered with CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ..., "Graph" )
51 *   - tests registered with CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ..., "Math" )
52 *   - tests registered with CPPUNIT_TEST_SUITE_REGISTRATION
53 *
54 * \code
55 * CppUnit::TestSuite *rootSuite = new CppUnit::TestSuite( "All tests" );
56 * rootSuite->addTest( CppUnit::TestFactoryRegistry::getRegistry( "Graph" ).makeTest() );
57 * rootSuite->addTest( CppUnit::TestFactoryRegistry::getRegistry( "Math" ).makeTest() );
58 * CppUnit::TestFactoryRegistry::getRegistry().addTestToSuite( rootSuite );
59 * \endcode
60 *
61 * The same result can be obtained with:
62 * \code
63 * CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
64 * registry.registerFactory( CppUnit::TestFactoryRegistry::getRegistry( "Graph" ) );
65 * registry.registerFactory( CppUnit::TestFactoryRegistry::getRegistry( "Math" ) );
66 * CppUnit::TestSuite *suite = registry.makeTest();
67 * \endcode
68 *
69 * Since a TestFactoryRegistry is a TestFactory, the named registries can be
70 * registered in the unnamed registry, creating the hierarchy links.
71 *
72 * \see TestSuiteFactory, AutoRegisterSuite
73 * \see CPPUNIT_TEST_SUITE_REGISTRATION, CPPUNIT_TEST_SUITE_NAMED_REGISTRATION
74 */
75class CPPUNIT_API TestFactoryRegistry : public TestFactory
76{
77public:
78  /** Constructs the registry with the specified name.
79   * \param name Name of the registry. It is the name of TestSuite returned by
80   *             makeTest().
81   */
82  TestFactoryRegistry( std::string name = "All Tests" );
83
84  /// Destructor.
85  virtual ~TestFactoryRegistry();
86
87  /** Returns a new TestSuite that contains the registered test.
88   * \return A new TestSuite which contains all the test added using
89   * registerFactory(TestFactory *).
90   */
91  virtual Test *makeTest();
92
93  /** Returns unnamed the registry.
94   * TestSuite registered using CPPUNIT_TEST_SUITE_REGISTRATION() are registered
95   * in this registry.
96   * \return Registry which name is "All Tests".
97   */
98  static TestFactoryRegistry &getRegistry();
99
100  /** Returns a named registry.
101   * TestSuite registered using CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() are registered
102   * in the registry of the same name.
103   * \param name Name of the registry to return.
104   * \return Registry. If the registry does not exist, it is created with the
105   *         specified name.
106   */
107  static TestFactoryRegistry &getRegistry( const std::string &name );
108
109  /** Adds the registered tests to the specified suite.
110   * \param suite Suite the tests are added to.
111   */
112  void addTestToSuite( TestSuite *suite );
113
114  /** Adds the specified TestFactory with a specific name (DEPRECATED).
115   * \param name Name associated to the factory.
116   * \param factory Factory to register.
117   * \deprecated Use registerFactory( TestFactory *) instead.
118   */
119  void registerFactory( const std::string &name,
120                        TestFactory *factory );
121
122  /** Adds the specified TestFactory to the registry.
123   *
124   * \param factory Factory to register.
125   */
126  void registerFactory( TestFactory *factory );
127
128private:
129  TestFactoryRegistry( const TestFactoryRegistry &copy );
130  void operator =( const TestFactoryRegistry &copy );
131
132private:
133  typedef std::map<std::string, TestFactory *> Factories;
134  Factories m_factories;
135
136  std::string m_name;
137};
138
139
140}  // namespace CppUnit
141
142
143#if CPPUNIT_NEED_DLL_DECL
144#pragma warning( pop )
145#endif
146
147
148#endif  // CPPUNIT_EXTENSIONS_TESTFACTORYREGISTRY_H
149