OLD | NEW |
| (Empty) |
1 // Copyright 2005-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // Unit Test Debug Helper | |
16 | |
17 // This file contains assert override functions | |
18 // It is meant to be #included only in unittest.cc files. | |
19 | |
20 #ifndef OMAHA_TESTING_UNITTEST_DEBUG_HELPER_H__ | |
21 #define OMAHA_TESTING_UNITTEST_DEBUG_HELPER_H__ | |
22 | |
23 #include "base/basictypes.h" | |
24 #include "omaha/base/debug.h" | |
25 #include "third_party/gtest/include/gtest/gtest.h" | |
26 | |
27 namespace omaha { | |
28 | |
29 typedef int AssertResponse; | |
30 #define IGNORE_ALWAYS 0 | |
31 #define IGNORE_ONCE 0 | |
32 | |
33 extern int g_assert_count; | |
34 | |
35 // A class with no methods which will, once acquired, cause asserts to | |
36 // be routed through the given function; once released, they will return | |
37 // to the previous handler. | |
38 class UseAssertFunction { | |
39 public: | |
40 explicit UseAssertFunction(DebugAssertFunctionType *function) { | |
41 function; | |
42 old_function_ = REPLACE_ASSERT_FUNCTION(function); | |
43 } | |
44 | |
45 ~UseAssertFunction() { | |
46 REPLACE_ASSERT_FUNCTION(old_function_); | |
47 } | |
48 | |
49 bool asserts_enabled() { | |
50 return old_function_ != NULL; // "!= NULL" fixes the perf warning C4800 | |
51 } | |
52 | |
53 private: | |
54 DebugAssertFunctionType *old_function_; | |
55 DISALLOW_EVIL_CONSTRUCTORS(UseAssertFunction); | |
56 }; | |
57 | |
58 | |
59 // A class with no methods which will send asserts to GTest as failures; | |
60 // once released, it restores the assert handler to the previous handler. | |
61 // Example usage: | |
62 // int main() { | |
63 // // Report asserts to GTest, not a messagebox. | |
64 // FailOnAssert a; | |
65 // return RUN_ALL_TESTS(); | |
66 // } | |
67 class FailOnAssert { | |
68 public: | |
69 FailOnAssert() : inner_(AssertHandler) { | |
70 } | |
71 | |
72 static AssertResponse AssertHandler(const char *expression, | |
73 const char *message, const char *file, int line) { | |
74 ADD_FAILURE() << "ASSERT in " << file << "(" << line << "): " | |
75 << expression << "; \"" << message << "\""; | |
76 return IGNORE_ALWAYS; | |
77 } | |
78 | |
79 private: | |
80 UseAssertFunction inner_; | |
81 DISALLOW_EVIL_CONSTRUCTORS(FailOnAssert); | |
82 }; | |
83 | |
84 // A class with no methods which will cause asserts to be ignored; | |
85 // once released, it restores the assert handler to the previous handler. | |
86 // Example usage: | |
87 // test1() { | |
88 // IngoreAssert a; | |
89 // TestSomethingWhichMayOrMayNotAssert(); | |
90 // } | |
91 class IgnoreAsserts { | |
92 public: | |
93 IgnoreAsserts() : inner_(AssertHandler) { | |
94 } | |
95 | |
96 static AssertResponse AssertHandler(const char *, | |
97 const char *, | |
98 const char *, | |
99 int) { | |
100 return IGNORE_ALWAYS; | |
101 } | |
102 | |
103 private: | |
104 UseAssertFunction inner_; | |
105 DISALLOW_EVIL_CONSTRUCTORS(IgnoreAsserts); | |
106 }; | |
107 | |
108 // A class with no methods which will cause asserts to be counted but otherwise | |
109 // ignored; once released, a GTest assert will be run to see if any asserts | |
110 // were fired, and the assert handler will be restored to the previous handler. | |
111 // Example usage: | |
112 // test2() { | |
113 // ExpectAsserts a; | |
114 // TestSomethingWhichIsKnownToAssert(); | |
115 // } | |
116 class ExpectAsserts { | |
117 public: | |
118 ExpectAsserts() : inner_(AssertHandler), | |
119 old_assert_count_(g_assert_count) { | |
120 } | |
121 | |
122 ~ExpectAsserts() { | |
123 DeInit(); | |
124 } | |
125 | |
126 static AssertResponse AssertHandler(const char *, | |
127 const char *, | |
128 const char *, | |
129 int) { | |
130 ++g_assert_count; | |
131 return IGNORE_ONCE; | |
132 } | |
133 private: | |
134 void DeInit() { | |
135 if (inner_.asserts_enabled()) { | |
136 ASSERT_GT(g_assert_count, old_assert_count_) | |
137 << "This test was expected to trigger at least one assert."; | |
138 } | |
139 } | |
140 | |
141 int old_assert_count_; | |
142 UseAssertFunction inner_; | |
143 DISALLOW_EVIL_CONSTRUCTORS(ExpectAsserts); | |
144 }; | |
145 | |
146 } // namespace omaha | |
147 | |
148 #endif // OMAHA_TESTING_UNITTEST_DEBUG_HELPER_H__ | |
OLD | NEW |