summaryrefslogtreecommitdiff
path: root/include/cppunit/extensions/TestSuiteBuilder.h
blob: ae1c6df265a57d050877823a9e5fd74eae7f9751 (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
#ifndef CPPUNIT_EXTENSIONS_TESTSUITEBUILDER_H
#define CPPUNIT_EXTENSIONS_TESTSUITEBUILDER_H

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


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
 * \see TestNamer.
 */
template<class Fixture>
class TestSuiteBuilder
{
public:
  typedef void (Fixture::*TestMethod)();
  TestSuiteBuilder( TestSuite *suite,
                    const TestNamer &namer ) 
    : m_suite( suite )
    , m_testNamer( namer )
  {
  }


  TestSuiteBuilder( const TestNamer &namer ) 
    : m_suite( new TestSuite( namer.getFixtureName() ) )
    , m_testNamer( namer )
  {
  }

  
  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 );
  }

  std::string makeTestName( const std::string &methodName )
  {
    return m_testNamer.getTestNameFor( methodName );
  }

private:
  std::auto_ptr<TestSuite> m_suite;
  const TestNamer &m_testNamer;
};


}  // namespace CppUnit

#endif  // CPPUNIT_EXTENSIONS_TESTSUITEBUILDER_H