summaryrefslogtreecommitdiff
path: root/src/cppunit/TestCase.cpp
blob: f1e519847e19468ee750c5cfc0bf926fa9e6964e (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <typeinfo>
#include <stdexcept>
#include <cmath>

#include "cppunit/TestCase.h"
#include "cppunit/Exception.h"
#include "cppunit/TestResult.h"
#include "estring.h"

namespace CppUnit {

/// Create a default TestResult
CppUnit::TestResult* TestCase::defaultResult ()
{ return new TestResult; } 


/// Check for a failed general assertion 
void TestCase::assertImplementation (bool          condition,
  std::string   conditionExpression,
  long          lineNumber,
  std::string   fileName)
{ 
  if (!condition) 
    throw Exception (conditionExpression, lineNumber, fileName); 
}


/// Check for a failed equality assertion 
void TestCase::assertEquals (long        expected, 
  long        actual,
  long        lineNumber,
  std::string fileName)
{ 
  if (expected != actual) 
    assertImplementation (false, notEqualsMessage(expected, actual), lineNumber, fileName); 
}


/// Check for a failed equality assertion
void TestCase::assertEquals (double        expected, 
  double        actual, 
  double        delta,
  long          lineNumber,
  std::string   fileName)
{ 
  if (fabs (expected - actual) > delta) 
    assertImplementation (false, notEqualsMessage(expected, actual), lineNumber, fileName); 

}


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

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

  }

  tearDown ();

  result->endTest (this);

}


/// A default run method 
TestResult *TestCase::run ()
{
  TestResult *result = defaultResult ();

  run (result);
  return result;

}


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


/// Build a message about a failed equality check 
std::string TestCase::notEqualsMessage (long expected, long actual)
{ 
  return "expected: " + estring (expected) + " but was: " + estring (actual); 
}


/// Build a message about a failed equality check 
std::string TestCase::notEqualsMessage (double expected, double actual)
{ 
  return "expected: " + estring (expected) + " but was: " + estring (actual); 
}



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

/** Constructs a test case for a suite.
 *  This TestCase is 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 a count of all the tests executed
int TestCase::countTestCases () const
{ return 1; }


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


/// A hook for fixture set up
void TestCase::setUp ()
{}
  

/// A hook for fixture tear down
void TestCase::tearDown ()
{}


/// Returns the name of the test case instance
std::string 
  TestCase::toString () const
{ 
  const type_info& thisClass = typeid (*this); 
  return std::string (thisClass.name ()) + "." + getName (); 
}
  
} // namespace CppUnit