1 //
2 // TestCaller.h
3 //
4 
5 
6 #ifndef CppUnit_TestCaller_INCLUDED
7 #define CppUnit_TestCaller_INCLUDED
8 
9 
10 #include "CppUnit/CppUnit.h"
11 #include "Guards.h"
12 #include "TestCase.h"
13 #include <memory>
14 
15 
16 namespace CppUnit {
17 
18 
19 /*
20  * A test caller provides access to a test case method
21  * on a test case class.  Test callers are useful when
22  * you want to run an individual test or add it to a
23  * suite.
24  *
25  * Here is an example:
26  *
27  * class MathTest : public TestCase {
28  *         ...
29  *     public:
30  *         void         setUp ();
31  *         void         tearDown ();
32  *
33  *         void         testAdd ();
34  *         void         testSubtract ();
35  * };
36  *
37  * Test *MathTest::suite () {
38  *     TestSuite *suite = new TestSuite;
39  *
40  *     suite->addTest (new TestCaller<MathTest> ("testAdd", testAdd));
41  *     return suite;
42  * }
43  *
44  * You can use a TestCaller to bind any test method on a TestCase
45  * class, as long as it returns accepts void and returns void.
46  *
47  * See TestCase
48  */
49 template <class Fixture>
50 class TestCaller: public TestCase
51 {
52 	REFERENCEOBJECT (TestCaller)
53 
54 	typedef void (Fixture::*TestMethod)();
55 
56 public:
57 	TestCaller(const std::string& name, TestMethod test, Test::Type testType = Test::Normal):
TestCase(name,testType)58 		TestCase(name, testType),
59 		_test(test),
60 		_fixture(new Fixture(name))
61 	{
62 	}
63 
64 protected:
runTest()65 	void runTest()
66 	{
67 		(_fixture.get()->*_test)();
68 	}
69 
setUp()70 	void setUp()
71 	{
72 		if (!setup().empty())
73 			_fixture.get()->addSetup(setup());
74 		_fixture.get()->setUp();
75 	}
76 
tearDown()77 	void tearDown()
78 	{
79 		_fixture.get()->tearDown();
80 	}
81 
82 private:
83 	TestMethod             _test;
84 	std::unique_ptr<Fixture> _fixture;
85 };
86 
87 
88 } // namespace CppUnit
89 
90 
91 #define CppUnit_addTest(suite, cls, mth) \
92 	suite->addTest(new CppUnit::TestCaller<cls>(#mth, &cls::mth))
93 
94 #define CppUnit_addLongTest(suite, cls, mth) \
95 	suite->addTest(new CppUnit::TestCaller<cls>(#mth, &cls::mth, CppUnit::Test::Long))
96 
97 #define CppUnit_addQualifiedTest(suite, cls, mth) \
98 	suite->addTest(new CppUnit::TestCaller<cls>(#cls"::"#mth, &cls::mth))
99 
100 #define CppUnit_addLongQualifiedTest(suite, cls, mth) \
101 	suite->addTest(new CppUnit::TestCaller<cls>(#cls"::"#mth, &cls::mth, CppUnit::Test::Long))
102 
103 #endif // CppUnit_TestCaller_INCLUDED
104