1 | // |
2 | // TestCaller.h |
3 | // |
4 | |
5 | |
6 | #ifndef Poco_CppUnit_TestCaller_INCLUDED |
7 | #define Poco_CppUnit_TestCaller_INCLUDED |
8 | |
9 | |
10 | #include "Poco/CppUnit/CppUnit.h" |
11 | #include "Poco/CppUnit/Guards.h" |
12 | #include "Poco/CppUnit/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): |
58 | TestCase(name), |
59 | _test(test), |
60 | _fixture(new Fixture(name)) |
61 | { |
62 | } |
63 | |
64 | // Returns the name of the test case instance |
65 | virtual std::string toString() |
66 | { |
67 | const std::type_info& thisClass = typeid(*this); |
68 | return std::string(thisClass.name()) + "." + name(); |
69 | } |
70 | |
71 | |
72 | protected: |
73 | void runTest() |
74 | { |
75 | (_fixture.get()->*_test)(); |
76 | } |
77 | |
78 | void setUp() |
79 | { |
80 | _fixture.get()->setUp(); |
81 | } |
82 | |
83 | void tearDown() |
84 | { |
85 | _fixture.get()->tearDown(); |
86 | } |
87 | |
88 | private: |
89 | TestMethod _test; |
90 | std::unique_ptr<Fixture> _fixture; |
91 | }; |
92 | |
93 | |
94 | } // namespace CppUnit |
95 | |
96 | |
97 | #define CppUnit_addTest(suite, cls, mth) \ |
98 | suite->addTest(new CppUnit::TestCaller<cls>(#mth, &cls::mth)) |
99 | |
100 | #define CppUnit_addQualifiedTest(suite, cls, mth) \ |
101 | suite->addTest(new CppUnit::TestCaller<cls>(#cls"::"#mth, &cls::mth)) |
102 | |
103 | #endif // Poco_CppUnit_TestCaller_INCLUDED |
104 | |