summaryrefslogtreecommitdiffstats
blob: 13c05257f6215a4e6805da2644a909876b865fe4 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <cppunit/Portability.h>
#include <cppunit/Exception.h>
#include <cppunit/Protector.h>
#include <cppunit/TestCase.h>
#include <cppunit/TestResult.h>
#include <stdexcept>

#if CPPUNIT_USE_TYPEINFO_NAME
#  include <typeinfo>
#endif

CPPUNIT_NS_BEGIN

/*! \brief Functor to call test case method (Implementation).
 *
 * Implementation detail.
 */
class TestCaseMethodFunctor : public Functor
{
public:
  typedef void (TestCase::*Method)();

  TestCaseMethodFunctor( TestCase *target,
                         Method method )
     : m_target( target )
     , m_method( method )
  {
  }

  bool operator()() const
  {
    (m_target->*m_method)();
    return true;
  }

private:
  TestCase *m_target;
  Method m_method;
};


/** Constructs a test case.
 *  \param name the name of the TestCase.
 **/
TestCase::TestCase( const std::string &name )
    : m_name(name)
{
}


/// Run the test and catch any exceptions that are triggered by it 
void 
TestCase::run( TestResult *result )
{
  result->startTest(this);
/*
  try {
    setUp();

    try {
      runTest();
    }
    catch ( Exception &e ) {
      Exception *copy = e.clone();
      result->addFailure( this, copy );
    }
    catch ( std::exception &e ) {
      result->addError( this, new Exception( Message( "uncaught std::exception", 
                                                      e.what() ) ) );
    }
    catch (...) {
      Exception *e = new Exception( Message( "uncaught unknown exception" ) );
      result->addError( this, e );
    }

    try {
      tearDown();
    }
    catch (...) {
      result->addError( this, new Exception( Message( "tearDown() failed" ) ) );
    }
  }
  catch (...) {
    result->addError( this, new Exception( Message( "setUp() failed" ) ) );
  }
*/
  if ( result->protect( TestCaseMethodFunctor( this, &TestCase::setUp ),
                        this,
                       "setUp() failed" ) )
  {
    result->protect( TestCaseMethodFunctor( this, &TestCase::runTest ),
                     this );
  }

  result->protect( TestCaseMethodFunctor( this, &TestCase::tearDown ),
                   this,
                   "tearDown() failed" );

  result->endTest( this );
}


/// All the work for runTest is deferred to subclasses 
void 
TestCase::runTest()
{
}


/** Constructs a test case for a suite.
 * \deprecated This constructor was used by fixture when TestFixture did not exist.
 *             Have your fixture inherits TestFixture instead of TestCase.
 * \internal
 *  This TestCase was intended for use by the TestCaller and should not
 *  be used by a test case for which run() is called.
 **/
TestCase::TestCase()
    : m_name( "" )
{
}


/// Destructs a test case
TestCase::~TestCase()
{
}


/// Returns the name of the test case
std::string 
TestCase::getName() const
{ 
  return m_name; 
}
  

CPPUNIT_NS_END