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