Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 #ifndef skiatest_Test_DEFINED | 8 #ifndef skiatest_Test_DEFINED |
| 9 #define skiatest_Test_DEFINED | 9 #define skiatest_Test_DEFINED |
| 10 | 10 |
| 11 #include "SkRefCnt.h" | 11 #include "SkRefCnt.h" |
| 12 #include "SkString.h" | 12 #include "SkString.h" |
| 13 #include "SkTRegistry.h" | 13 #include "SkTRegistry.h" |
| 14 #include "SkThread.h" | 14 #include "SkThread.h" |
| 15 #include "SkTypes.h" | 15 #include "SkTypes.h" |
| 16 | 16 |
| 17 class GrContextFactory; | 17 class GrContextFactory; |
| 18 | 18 |
| 19 namespace skiatest { | 19 namespace skiatest { |
| 20 | 20 |
| 21 class Test; | 21 class Test; |
| 22 | 22 |
| 23 /** | |
| 24 * Information about a single failure from a Test. | |
| 25 * | |
| 26 * Not intended to be created/modified directly. To create one, use one of | |
| 27 * | |
| 28 * REPORTER_ASSERT | |
| 29 * REPORTER_ASSERT_MESSAGE | |
| 30 * ERRORF | |
| 31 * | |
| 32 * described in more detail further down in this file. | |
| 33 */ | |
| 34 struct Failure { | |
| 35 const char* fileName; | |
| 36 int lineNo; | |
| 37 const char* condition; | |
| 38 SkString message; | |
| 39 | |
| 40 // Helper to combine the failure info into one string. | |
| 41 void getFailureString(SkString* result) const { | |
| 42 if (!result) { | |
| 43 return; | |
| 44 } | |
| 45 result->printf("%s:%d\t", fileName, lineNo); | |
| 46 if (!message.isEmpty()) { | |
| 47 if (condition != NULL) { | |
| 48 result->appendf("%s: %s", message.c_str(), condition); | |
| 49 } else { | |
| 50 result->append(message); | |
| 51 } | |
| 52 } else if (condition != NULL) { | |
| 53 // Condition, but no message. | |
| 54 result->append(condition); | |
| 55 } | |
| 56 } | |
| 57 }; | |
| 58 | |
| 59 | |
| 23 class Reporter : public SkRefCnt { | 60 class Reporter : public SkRefCnt { |
| 24 public: | 61 public: |
| 25 SK_DECLARE_INST_COUNT(Reporter) | 62 SK_DECLARE_INST_COUNT(Reporter) |
| 26 Reporter(); | 63 Reporter(); |
| 27 | 64 |
| 28 int countTests() const { return fTestCount; } | 65 int countTests() const { return fTestCount; } |
| 29 | 66 |
| 30 void startTest(Test*); | 67 void startTest(Test*); |
| 31 void reportFailed(const SkString& desc); | 68 void reportFailed(const Failure&); |
| 32 void endTest(Test*); | 69 void endTest(Test*); |
| 33 | 70 |
| 34 virtual bool allowExtendedTest() const { return false; } | 71 virtual bool allowExtendedTest() const { return false; } |
| 35 virtual bool verbose() const { return false; } | 72 virtual bool verbose() const { return false; } |
| 36 virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); } | 73 virtual void bumpTestCount() { sk_atomic_inc(&fTestCount); } |
| 37 | 74 |
| 38 protected: | 75 protected: |
| 39 virtual void onStart(Test*) {} | 76 virtual void onStart(Test*) {} |
| 40 virtual void onReportFailed(const SkString& desc) {} | 77 virtual void onReportFailed(const Failure&) {} |
| 41 virtual void onEnd(Test*) {} | 78 virtual void onEnd(Test*) {} |
| 42 | 79 |
| 43 private: | 80 private: |
| 44 int32_t fTestCount; | 81 int32_t fTestCount; |
| 45 | 82 |
| 46 typedef SkRefCnt INHERITED; | 83 typedef SkRefCnt INHERITED; |
| 47 }; | 84 }; |
| 48 | 85 |
| 49 class Test { | 86 class Test { |
| 50 public: | 87 public: |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15"); | 140 REPORTER_ASSERT_MESSAGE(reporter, x == 15, "x should be 15"); |
| 104 ... | 141 ... |
| 105 if (x != 15) { | 142 if (x != 15) { |
| 106 ERRORF(reporter, "x should be 15, but is %d", x); | 143 ERRORF(reporter, "x should be 15, but is %d", x); |
| 107 return; | 144 return; |
| 108 } | 145 } |
| 109 ... | 146 ... |
| 110 } | 147 } |
| 111 */ | 148 */ |
| 112 | 149 |
| 113 #define REPORTER_ASSERT(r, cond) \ | 150 #define REPORTER_ASSERT(r, cond) \ |
| 114 do { \ | 151 do { \ |
| 115 if (!(cond)) { \ | 152 if (!(cond)) { \ |
| 116 SkString desc; \ | 153 skiatest::Failure failure = { __FILE__, __LINE__, \ |
| 117 desc.printf("%s:%d\t%s", __FILE__, __LINE__, #cond); \ | 154 #cond, SkString() }; \ |
| 118 r->reportFailed(desc); \ | 155 r->reportFailed(failure); \ |
| 119 } \ | 156 } \ |
| 120 } while(0) | 157 } while(0) |
| 121 | 158 |
| 122 #define REPORTER_ASSERT_MESSAGE(r, cond, message) \ | 159 #define REPORTER_ASSERT_MESSAGE(r, cond, message) \ |
| 123 do { \ | 160 do { \ |
| 124 if (!(cond)) { \ | 161 if (!(cond)) { \ |
| 125 SkString desc; \ | 162 skiatest::Failure failure = { __FILE__, __LINE__, \ |
| 126 desc.printf("%s:%d\t%s: %s", __FILE__, __LINE__, \ | 163 #cond, SkString(message) }; \ |
| 127 message, #cond); \ | 164 r->reportFailed(failure); \ |
| 128 r->reportFailed(desc); \ | 165 } \ |
| 129 } \ | |
| 130 } while(0) | 166 } while(0) |
| 131 | 167 |
| 132 #define ERRORF(reporter, ...) \ | 168 #define ERRORF(r, ...) \ |
| 133 do { \ | 169 do { \ |
| 134 SkString desc; \ | 170 SkString desc; \ |
| 135 desc.printf("%s:%d\t", __FILE__, __LINE__); \ | 171 desc.appendf(__VA_ARGS__) ; \ |
| 136 desc.appendf(__VA_ARGS__) ; \ | 172 skiatest::Failure failure = { __FILE__, __LINE__, \ |
| 137 (reporter)->reportFailed(desc); \ | 173 NULL, SkString(desc) }; \ |
|
mtklein
2014/11/06 17:46:54
Consider "" instead of NULL here, so we never have
scroggo
2014/11/06 20:29:32
Done.
| |
| 174 r->reportFailed(failure); \ | |
| 138 } while(0) | 175 } while(0) |
| 139 | 176 |
| 140 #define DEF_TEST(name, reporter) \ | 177 #define DEF_TEST(name, reporter) \ |
| 141 static void test_##name(skiatest::Reporter*); \ | 178 static void test_##name(skiatest::Reporter*); \ |
| 142 namespace skiatest { \ | 179 namespace skiatest { \ |
| 143 class name##Class : public Test { \ | 180 class name##Class : public Test { \ |
| 144 public: \ | 181 public: \ |
| 145 static Test* Factory(void*) { return SkNEW(name##Class); } \ | 182 static Test* Factory(void*) { return SkNEW(name##Class); } \ |
| 146 protected: \ | 183 protected: \ |
| 147 virtual void onGetName(SkString* name) SK_OVERRIDE { \ | 184 virtual void onGetName(SkString* name) SK_OVERRIDE { \ |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 165 } \ | 202 } \ |
| 166 virtual void onRun(Reporter* r) SK_OVERRIDE { \ | 203 virtual void onRun(Reporter* r) SK_OVERRIDE { \ |
| 167 test_##name(r, fGrContextFactory); \ | 204 test_##name(r, fGrContextFactory); \ |
| 168 } \ | 205 } \ |
| 169 }; \ | 206 }; \ |
| 170 static TestRegistry gReg_##name##Class(name##Class::Factory); \ | 207 static TestRegistry gReg_##name##Class(name##Class::Factory); \ |
| 171 } \ | 208 } \ |
| 172 static void test_##name(skiatest::Reporter* reporter, GrContextFactory* fact ory) | 209 static void test_##name(skiatest::Reporter* reporter, GrContextFactory* fact ory) |
| 173 | 210 |
| 174 #endif | 211 #endif |
| OLD | NEW |