1 |
// |
2 |
// Permission to reproduce and create derivative works from the Software ("Software Derivative Works") |
3 |
// is hereby granted to you under the copyright of Michael Feathers. Michael Feathers also grants you |
4 |
// the right to distribute the Software and Software Derivative Works. |
5 |
// |
6 |
// Michael Feathers licenses the Software to you on an "AS IS" basis, without warranty of any kind. |
7 |
// Michael Feathers HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES OR CONDITIONS, EITHER EXPRESS OR IMPLIED, |
8 |
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OR CONDITIONS OF MERCHANTABILITY, NON INFRINGEMENT |
9 |
// AND FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness |
10 |
// of using the Software and assume all risks associated with the use and distribution of this Software, |
11 |
// including but not limited to the risks of program errors, damage to or loss of data, programs or |
12 |
// equipment, and unavailability or interruption of operations. MICHAEL FEATHERS WILL NOT BE |
13 |
// LIABLE FOR ANY DIRECT DAMAGES OR FOR ANY SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC |
14 |
// CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF MICHAEL FEATHERS HAD BEEN ADVISED |
15 |
// OF THE POSSIBILITY OF SUCH DAMAGE. Michael Feathers will not be liable for the loss of, or damage |
16 |
// to, your records or data, or any damages claimed by you based on a third party claim. |
17 |
// |
18 |
// You agree to distribute the Software and any Software Derivatives under a license agreement that: |
19 |
// |
20 |
// 1) is sufficient to notify all licensees of the Software and Software Derivatives that Michael |
21 |
// Feathers assumes no liability for any claim that may arise regarding the Software or |
22 |
// Software Derivatives, and |
23 |
// 2) that disclaims all warranties, both express and implied, from Michael Feathers regarding the |
24 |
// Software and Software Derivatives. (If you include this Agreement with any distribution |
25 |
// of the Software and Software Derivatives you will have meet this requirement) You agree that |
26 |
// you will not delete any copyright notices in the Software. |
27 |
// |
28 |
// This Agreement is the exclusive statement of your rights in the Software as provided by Michael |
29 |
// Feathers. Except for the licenses granted to you in the second paragraph above, no other licenses |
30 |
// are granted hereunder, by estoppel, implication or otherwise. |
31 |
// |
32 |
#ifndef CPPUNIT_TESTCALLER_H |
33 |
#define CPPUNIT_TESTCALLER_H |
34 |
|
35 |
#include "CppUnitTestNamespace.h" |
36 |
|
37 |
BEGIN_NAMESPACE_CPPUNITTEST |
38 |
|
39 |
#include "Guards.h" |
40 |
#include "TestCase.h" |
41 |
|
42 |
/* #include <stl.h> */ |
43 |
|
44 |
/* |
45 |
* A test caller provides access to a test case method |
46 |
* on a test case class. Test callers are useful when |
47 |
* you want to run an individual test or add it to a |
48 |
* suite. |
49 |
* |
50 |
* Here is an example: |
51 |
* |
52 |
* class MathTest : public TestCase { |
53 |
* ... |
54 |
* public: |
55 |
* void setUp (); |
56 |
* void tearDown (); |
57 |
* |
58 |
* void testAdd (); |
59 |
* void testSubtract (); |
60 |
* }; |
61 |
* |
62 |
* Test *MathTest::suite () { |
63 |
* TestSuite *suite = new TestSuite; |
64 |
* |
65 |
* suite->addTest (new TestCaller<MathTest> ("testAdd", testAdd)); |
66 |
* return suite; |
67 |
* } |
68 |
* |
69 |
* You can use a TestCaller to bind any test method on a TestCase |
70 |
* class, as long as it returns accepts void and returns void. |
71 |
* |
72 |
* See TestCase |
73 |
*/ |
74 |
|
75 |
|
76 |
template <class Fixture> class TestCaller : public TestCase |
77 |
{ |
78 |
private: |
79 |
// |
80 |
// explicitly don't allow these |
81 |
TestCaller (const TestCaller<Fixture>& other); |
82 |
TestCaller<Fixture>& operator= (const TestCaller<Fixture>& other); |
83 |
|
84 |
typedef void (Fixture::*TestMethod)(); |
85 |
|
86 |
public: |
87 |
TestCaller (std::string name, TestMethod test) |
88 |
: TestCase (name), m_fixture (new Fixture (name)), m_test (test) |
89 |
{}; |
90 |
~TestCaller () {delete m_fixture;}; |
91 |
|
92 |
protected: |
93 |
void runTest () |
94 |
{ |
95 |
try { |
96 |
(m_fixture->*m_test)(); |
97 |
setCheckErrors(m_fixture->getCheckErrors()); |
98 |
} |
99 |
catch(CppUnitException& cpp) { |
100 |
setCheckErrors(m_fixture->getCheckErrors()); |
101 |
throw CppUnitException (cpp.what(), cpp.lineNumber(), |
102 |
cpp.fileName()); |
103 |
} |
104 |
catch (std::exception&) { |
105 |
setCheckErrors(m_fixture->getCheckErrors()); |
106 |
// |
107 |
// rethrow the exception |
108 |
throw; |
109 |
} |
110 |
|
111 |
} |
112 |
|
113 |
void setUp () |
114 |
{ m_fixture->setArgs(getArgs()); |
115 |
m_fixture->setUp ();} |
116 |
|
117 |
void tearDown () |
118 |
{ m_fixture->tearDown (); } |
119 |
|
120 |
private: |
121 |
Fixture* m_fixture; |
122 |
TestMethod m_test; |
123 |
|
124 |
}; |
125 |
|
126 |
END_NAMESPACE_CPPUNITTEST |
127 |
|
128 |
#endif |
129 |
|
130 |
|