Test.h revision 80e39a77b16f4396eed230efea1d0b2fc8cbfb00
1#ifndef skiatest_Test_DEFINED
2#define skiatest_Test_DEFINED
3
4#include "SkRefCnt.h"
5#include "SkString.h"
6#include "SkTRegistry.h"
7
8namespace skiatest {
9
10    class Test;
11
12    class Reporter : public SkRefCnt {
13    public:
14        Reporter();
15
16        enum Result {
17            kPassed,    // must begin with 0
18            kFailed,
19            /////
20            kLastResult = kFailed
21        };
22
23        void resetReporting();
24        int countTests() const { return fTestCount; }
25        int countResults(Result r) {
26            SkASSERT((unsigned)r <= kLastResult);
27            return fResultCount[r];
28        }
29
30        void startTest(Test*);
31        void report(const char testDesc[], Result);
32        void endTest(Test*);
33
34        // helpers for tests
35        void assertTrue(bool cond, const char desc[]) {
36            if (!cond) {
37                this->report(desc, kFailed);
38            }
39        }
40        void assertFalse(bool cond, const char desc[]) {
41            if (cond) {
42                this->report(desc, kFailed);
43            }
44        }
45        void reportFailed(const char desc[]) {
46            this->report(desc, kFailed);
47        }
48        void reportFailed(const SkString& desc) {
49            this->report(desc.c_str(), kFailed);
50        }
51
52    protected:
53        virtual void onStart(Test*) {}
54        virtual void onReport(const char desc[], Result) {}
55        virtual void onEnd(Test*) {}
56
57    private:
58        Test* fCurrTest;
59        int fTestCount;
60        int fResultCount[kLastResult+1];
61
62        typedef SkRefCnt INHERITED;
63    };
64
65    class Test {
66    public:
67        Test();
68        virtual ~Test();
69
70        Reporter* getReporter() const { return fReporter; }
71        void setReporter(Reporter*);
72
73        const char* getName();
74        void run();
75
76    protected:
77        virtual void onGetName(SkString*) = 0;
78        virtual void onRun(Reporter*) = 0;
79
80    private:
81        Reporter*   fReporter;
82        SkString    fName;
83    };
84
85    typedef SkTRegistry<Test*, void*> TestRegistry;
86}
87
88#define REPORTER_ASSERT(r, cond)                                        \
89    do {                                                                \
90        if (!(cond)) {                                                  \
91            SkString desc;                                              \
92            desc.printf("%s:%d: %s", __FILE__, __LINE__, #cond);      \
93            r->reportFailed(desc);                                      \
94        }                                                               \
95    } while(0)
96
97
98#endif
99