xref: /haiku/src/tools/cppunit/cppunit/TestCase.cpp (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 #include <cppunit/Portability.h>
2 #include <typeinfo>
3 #include <stdexcept>
4 
5 #include "cppunit/TestCase.h"
6 #include "cppunit/Exception.h"
7 #include "cppunit/TestResult.h"
8 
9 
10 using std::exception;
11 using std::string;
12 
13 namespace CppUnit {
14 
15 /// Create a default TestResult
16 CppUnit::TestResult*
17 TestCase::defaultResult()
18 {
19   return new TestResult;
20 }
21 
22 
23 /// Run the test and catch any exceptions that are triggered by it
24 void
25 TestCase::run( TestResult *result )
26 {
27   result->startTest(this);
28 
29   try {
30 	  setUp();
31 
32 	  try {
33 	    runTest();
34 	  }
35 	  catch ( Exception &e ) {
36 	    Exception *copy = e.clone();
37 	    result->addFailure( this, copy );
38 	  }
39 	  catch ( exception &e ) {
40 	    result->addError( this, new Exception( e.what() ) );
41 	  }
42 	  catch (...) {
43 	    Exception *e = new Exception( "caught unknown exception" );
44 	    result->addError( this, e );
45 	  }
46 
47 	  try {
48 	    tearDown();
49 	  }
50 	  catch (...) {
51 	    result->addError( this, new Exception( "tearDown() failed" ) );
52 	  }
53   }
54   catch (...) {
55 	  result->addError( this, new Exception( "setUp() failed" ) );
56   }
57 
58   result->endTest( this );
59 }
60 
61 
62 /// A default run method
63 TestResult *
64 TestCase::run()
65 {
66   TestResult *result = defaultResult();
67 
68   run (result);
69   return result;
70 }
71 
72 
73 /// All the work for runTest is deferred to subclasses
74 void
75 TestCase::runTest()
76 {
77 }
78 
79 
80 /** Constructs a test case.
81  *  \param name the name of the TestCase.
82  **/
83 TestCase::TestCase( string name )
84     : m_name(name)
85 {
86 }
87 
88 
89 /** Constructs a test case for a suite.
90  *  This TestCase is intended for use by the TestCaller and should not
91  *  be used by a test case for which run() is called.
92  **/
93 TestCase::TestCase()
94     : m_name( "" )
95 {
96 }
97 
98 
99 /// Destructs a test case
100 TestCase::~TestCase()
101 {
102 }
103 
104 
105 /// Returns a count of all the tests executed
106 int
107 TestCase::countTestCases() const
108 {
109   return 1;
110 }
111 
112 
113 /// Returns the name of the test case
114 string
115 TestCase::getName() const
116 {
117   return m_name;
118 }
119 
120 
121 /// Returns the name of the test case instance
122 string
123 TestCase::toString() const
124 {
125   string className;
126 
127 #if CPPUNIT_USE_TYPEINFO_NAME
128   const std::type_info& thisClass = typeid( *this );
129   className = thisClass.name();
130 #else
131   className = "TestCase";
132 #endif
133 
134   return className + "." + getName();
135 }
136 
137 
138 } // namespace CppUnit
139