1*933764d7SIngo Weinhold /*
2*933764d7SIngo Weinhold * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3*933764d7SIngo Weinhold * Distributed under the terms of the MIT License.
4*933764d7SIngo Weinhold */
5*933764d7SIngo Weinhold #ifndef TEST_H
6*933764d7SIngo Weinhold #define TEST_H
7*933764d7SIngo Weinhold
8*933764d7SIngo Weinhold
9*933764d7SIngo Weinhold #include "TestContext.h"
10*933764d7SIngo Weinhold
11*933764d7SIngo Weinhold
12*933764d7SIngo Weinhold class TestSuite;
13*933764d7SIngo Weinhold class TestVisitor;
14*933764d7SIngo Weinhold
15*933764d7SIngo Weinhold
16*933764d7SIngo Weinhold class Test {
17*933764d7SIngo Weinhold public:
18*933764d7SIngo Weinhold Test(const char* name);
19*933764d7SIngo Weinhold virtual ~Test();
20*933764d7SIngo Weinhold
Name()21*933764d7SIngo Weinhold const char* Name() const { return fName; }
22*933764d7SIngo Weinhold
Suite()23*933764d7SIngo Weinhold TestSuite* Suite() const { return fSuite; }
24*933764d7SIngo Weinhold void SetSuite(TestSuite* suite);
25*933764d7SIngo Weinhold
26*933764d7SIngo Weinhold virtual bool IsLeafTest() const;
27*933764d7SIngo Weinhold virtual status_t Setup(TestContext& context);
28*933764d7SIngo Weinhold virtual bool Run(TestContext& context) = 0;
29*933764d7SIngo Weinhold virtual bool Run(TestContext& context, const char* name);
30*933764d7SIngo Weinhold virtual void Cleanup(TestContext& context, bool setupOK);
31*933764d7SIngo Weinhold
32*933764d7SIngo Weinhold virtual Test* Visit(TestVisitor& visitor);
33*933764d7SIngo Weinhold
34*933764d7SIngo Weinhold private:
35*933764d7SIngo Weinhold const char* fName;
36*933764d7SIngo Weinhold TestSuite* fSuite;
37*933764d7SIngo Weinhold };
38*933764d7SIngo Weinhold
39*933764d7SIngo Weinhold
40*933764d7SIngo Weinhold class StandardTestDelegate {
41*933764d7SIngo Weinhold public:
42*933764d7SIngo Weinhold StandardTestDelegate();
43*933764d7SIngo Weinhold virtual ~StandardTestDelegate();
44*933764d7SIngo Weinhold
45*933764d7SIngo Weinhold virtual status_t Setup(TestContext& context);
46*933764d7SIngo Weinhold virtual void Cleanup(TestContext& context, bool setupOK);
47*933764d7SIngo Weinhold };
48*933764d7SIngo Weinhold
49*933764d7SIngo Weinhold
50*933764d7SIngo Weinhold template<typename TestClass>
51*933764d7SIngo Weinhold class StandardTest : public Test {
52*933764d7SIngo Weinhold public:
53*933764d7SIngo Weinhold StandardTest(const char* name,
54*933764d7SIngo Weinhold TestClass* object,
55*933764d7SIngo Weinhold bool (TestClass::*method)(TestContext&));
56*933764d7SIngo Weinhold virtual ~StandardTest();
57*933764d7SIngo Weinhold
58*933764d7SIngo Weinhold virtual status_t Setup(TestContext& context);
59*933764d7SIngo Weinhold virtual bool Run(TestContext& context);
60*933764d7SIngo Weinhold virtual void Cleanup(TestContext& context, bool setupOK);
61*933764d7SIngo Weinhold
62*933764d7SIngo Weinhold private:
63*933764d7SIngo Weinhold TestClass* fObject;
64*933764d7SIngo Weinhold bool (TestClass::*fMethod)(TestContext&);
65*933764d7SIngo Weinhold };
66*933764d7SIngo Weinhold
67*933764d7SIngo Weinhold
68*933764d7SIngo Weinhold template<typename TestClass>
StandardTest(const char * name,TestClass * object,bool (TestClass::* method)(TestContext &))69*933764d7SIngo Weinhold StandardTest<TestClass>::StandardTest(const char* name, TestClass* object,
70*933764d7SIngo Weinhold bool (TestClass::*method)(TestContext&))
71*933764d7SIngo Weinhold :
72*933764d7SIngo Weinhold Test(name),
73*933764d7SIngo Weinhold fObject(object),
74*933764d7SIngo Weinhold fMethod(method)
75*933764d7SIngo Weinhold {
76*933764d7SIngo Weinhold }
77*933764d7SIngo Weinhold
78*933764d7SIngo Weinhold
79*933764d7SIngo Weinhold template<typename TestClass>
~StandardTest()80*933764d7SIngo Weinhold StandardTest<TestClass>::~StandardTest()
81*933764d7SIngo Weinhold {
82*933764d7SIngo Weinhold delete fObject;
83*933764d7SIngo Weinhold }
84*933764d7SIngo Weinhold
85*933764d7SIngo Weinhold
86*933764d7SIngo Weinhold template<typename TestClass>
87*933764d7SIngo Weinhold status_t
Setup(TestContext & context)88*933764d7SIngo Weinhold StandardTest<TestClass>::Setup(TestContext& context)
89*933764d7SIngo Weinhold {
90*933764d7SIngo Weinhold return fObject->Setup(context);
91*933764d7SIngo Weinhold }
92*933764d7SIngo Weinhold
93*933764d7SIngo Weinhold
94*933764d7SIngo Weinhold template<typename TestClass>
95*933764d7SIngo Weinhold bool
Run(TestContext & context)96*933764d7SIngo Weinhold StandardTest<TestClass>::Run(TestContext& context)
97*933764d7SIngo Weinhold {
98*933764d7SIngo Weinhold return (fObject->*fMethod)(context);
99*933764d7SIngo Weinhold }
100*933764d7SIngo Weinhold
101*933764d7SIngo Weinhold
102*933764d7SIngo Weinhold template<typename TestClass>
103*933764d7SIngo Weinhold void
Cleanup(TestContext & context,bool setupOK)104*933764d7SIngo Weinhold StandardTest<TestClass>::Cleanup(TestContext& context, bool setupOK)
105*933764d7SIngo Weinhold {
106*933764d7SIngo Weinhold fObject->Cleanup(context, setupOK);
107*933764d7SIngo Weinhold }
108*933764d7SIngo Weinhold
109*933764d7SIngo Weinhold
110*933764d7SIngo Weinhold #define TEST_ASSERT(condition) \
111*933764d7SIngo Weinhold do { \
112*933764d7SIngo Weinhold if (!(condition)) { \
113*933764d7SIngo Weinhold TestContext::Current()->AssertFailed(__FILE__, __LINE__, \
114*933764d7SIngo Weinhold __PRETTY_FUNCTION__, #condition); \
115*933764d7SIngo Weinhold return false; \
116*933764d7SIngo Weinhold } \
117*933764d7SIngo Weinhold } while (false)
118*933764d7SIngo Weinhold
119*933764d7SIngo Weinhold #define TEST_ASSERT_PRINT(condition, format...) \
120*933764d7SIngo Weinhold do { \
121*933764d7SIngo Weinhold if (!(condition)) { \
122*933764d7SIngo Weinhold TestContext::Current()->AssertFailed(__FILE__, __LINE__, \
123*933764d7SIngo Weinhold __PRETTY_FUNCTION__, #condition, format); \
124*933764d7SIngo Weinhold return false; \
125*933764d7SIngo Weinhold } \
126*933764d7SIngo Weinhold } while (false)
127*933764d7SIngo Weinhold
128*933764d7SIngo Weinhold #define TEST_PROPAGATE(result) \
129*933764d7SIngo Weinhold do { \
130*933764d7SIngo Weinhold if (!(result)) \
131*933764d7SIngo Weinhold return false; \
132*933764d7SIngo Weinhold } while (false)
133*933764d7SIngo Weinhold
134*933764d7SIngo Weinhold
135*933764d7SIngo Weinhold #endif // TEST_H
136