Home | History | Annotate | Download | only in tests
      1 #ifndef skiatest_Test_DEFINED
      2 #define skiatest_Test_DEFINED
      3 
      4 #include "SkRefCnt.h"
      5 #include "SkString.h"
      6 #include "SkTRegistry.h"
      7 
      8 namespace 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         bool getCurrSuccess() const {
     53             return fCurrTestSuccess;
     54         }
     55 
     56     protected:
     57         virtual void onStart(Test*) {}
     58         virtual void onReport(const char desc[], Result) {}
     59         virtual void onEnd(Test*) {}
     60 
     61     private:
     62         Test* fCurrTest;
     63         int fTestCount;
     64         int fResultCount[kLastResult+1];
     65         bool fCurrTestSuccess;
     66 
     67         typedef SkRefCnt INHERITED;
     68     };
     69 
     70     class Test {
     71     public:
     72         Test();
     73         virtual ~Test();
     74 
     75         Reporter* getReporter() const { return fReporter; }
     76         void setReporter(Reporter*);
     77 
     78         const char* getName();
     79         bool run(); // returns true on success
     80 
     81     protected:
     82         virtual void onGetName(SkString*) = 0;
     83         virtual void onRun(Reporter*) = 0;
     84 
     85     private:
     86         Reporter*   fReporter;
     87         SkString    fName;
     88     };
     89 
     90     typedef SkTRegistry<Test*, void*> TestRegistry;
     91 }
     92 
     93 #define REPORTER_ASSERT(r, cond)                                        \
     94     do {                                                                \
     95         if (!(cond)) {                                                  \
     96             SkString desc;                                              \
     97             desc.printf("%s:%d: %s", __FILE__, __LINE__, #cond);      \
     98             r->reportFailed(desc);                                      \
     99         }                                                               \
    100     } while(0)
    101 
    102 
    103 #endif
    104