summaryrefslogtreecommitdiff
path: root/include/cppunit/extensions/TestSuiteBuilder.h
blob: 709053beb8a51f4f029afbbb6e348cb62777d939 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef CPPUNIT_EXTENSIONS_TESTSUITEBUILDER_H
#define CPPUNIT_EXTENSIONS_TESTSUITEBUILDER_H

#include <cppunit/Portability.h>
#include <memory>
#include <cppunit/TestSuite.h>
#include <cppunit/TestCaller.h>
#include <cppunit/extensions/TypeInfoHelper.h>


namespace CppUnit {

  /*! \brief Helper to add tests to a TestSuite.
   * \ingroup WritingTestFixture
   *
   * All tests added to the TestSuite are prefixed by TestSuite name. The resulting
   * TestCase name has the following pattern:
   *
   * MyTestSuiteName.myTestName
   */
  template<typename Fixture>
  class TestSuiteBuilder
  {
    public:
      typedef void (Fixture::*TestMethod)();

#if CPPUNIT_USE_TYPEINFO_NAME
      TestSuiteBuilder() : 
          m_suite( new TestSuite( 
              TypeInfoHelper::getClassName( typeid(Fixture) )  ) )
      {
      }
#endif

      TestSuiteBuilder( TestSuite *suite ) : m_suite( suite ) 
      {
      }

      TestSuiteBuilder(std::string name) : m_suite( new TestSuite(name) ) 
      {
      }

      TestSuite *suite() const
      {
        return m_suite.get();
      }

      TestSuite *takeSuite()
      {
        return m_suite.release();
      }

      void addTest( Test *test )
      {
        m_suite->addTest( test );
      }

      void addTestCaller( std::string methodName, 
                          TestMethod testMethod )
      {
          Test *test = 
              new TestCaller<Fixture>( makeTestName( methodName ), 
                                       testMethod );
          addTest( test );
      }

      void addTestCaller( std::string methodName, 
                          TestMethod testMethod, 
                          Fixture *fixture )
      {
          Test *test = 
              new TestCaller<Fixture>( makeTestName( methodName ), 
                                       testMethod,
                                       fixture);
          addTest( test );
      }

      template<typename ExceptionType>
      void addTestCallerForException( std::string methodName, 
                                      TestMethod testMethod, 
                                      Fixture *fixture,
                                      ExceptionType *dummyPointer )
      {
          Test *test = new TestCaller<Fixture,ExceptionType>( 
                                       makeTestName( methodName ), 
                                       testMethod,
                                       fixture);
          addTest( test );
      }

    
      std::string makeTestName( const std::string &methodName )
      {
        return m_suite->getName() + "." + methodName;
      }

    private:
      std::auto_ptr<TestSuite> m_suite;
  };

}  // namespace CppUnit

#endif  // CPPUNIT_EXTENSIONS_TESTSUITEBUILDER_H