summaryrefslogtreecommitdiffstats
blob: e04adb5d937486fdda08a63de9a7c0c853ec4501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef CPPUNIT_EXTENSIONS_AUTOREGISTERSUITE_H
#define CPPUNIT_EXTENSIONS_AUTOREGISTERSUITE_H

#include <cppunit/extensions/TestSuiteFactory.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <string>

CPPUNIT_NS_BEGIN


/*! \brief (Implementation) Automatically register the test suite of the specified type.
 *
 * You should not use this class directly. Instead, use the following macros:
 * - CPPUNIT_TEST_SUITE_REGISTRATION()
 * - CPPUNIT_TEST_SUITE_NAMED_REGISTRATION()
 *
 * This object will register the test returned by TestCaseType::suite()
 * when constructed to the test registry.
 *
 * This object is intented to be used as a static variable.
 *
 *
 * \param TestCaseType Type of the test case which suite is registered.
 * \see CPPUNIT_TEST_SUITE_REGISTRATION, CPPUNIT_TEST_SUITE_NAMED_REGISTRATION
 * \see CppUnit::TestFactoryRegistry.
 */
template<class TestCaseType>
class AutoRegisterSuite
{
public:
  /** Auto-register the suite factory in the global registry.
   */
  AutoRegisterSuite()
      : m_registry( &TestFactoryRegistry::getRegistry() )
  {
    m_registry->registerFactory( &m_factory );
  }

  /** Auto-register the suite factory in the specified registry.
   * \param name Name of the registry.
   */
  AutoRegisterSuite( const std::string &name )
      : m_registry( &TestFactoryRegistry::getRegistry( name ) )
  {
    m_registry->registerFactory( &m_factory );
  }

  ~AutoRegisterSuite()
  {
    if ( TestFactoryRegistry::isValid() )
      m_registry->unregisterFactory( &m_factory );
  }

private:
  TestFactoryRegistry *m_registry;
  TestSuiteFactory<TestCaseType> m_factory;
};


/*! \brief (Implementation) Automatically adds a registry into another registry.
 *
 * Don't use this class. Use the macros CPPUNIT_REGISTRY_ADD() and
 * CPPUNIT_REGISTRY_ADD_TO_DEFAULT() instead.
 */
class AutoRegisterRegistry
{
public:
  AutoRegisterRegistry( const std::string &which,
                        const std::string &to )
  {
    TestFactoryRegistry::getRegistry( to ).addRegistry( which );
  }

  AutoRegisterRegistry( const std::string &which )
  {
    TestFactoryRegistry::getRegistry().addRegistry( which );
  }
};


CPPUNIT_NS_END

#endif  // CPPUNIT_EXTENSIONS_AUTOREGISTERSUITE_H