temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame^] | 1 | // Copyright 2007, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | // |
| 32 | // Utilities for testing Google Test itself and code that uses Google Test |
| 33 | // (e.g. frameworks built on top of Google Test). |
| 34 | |
| 35 | #ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_ |
| 36 | #define GTEST_INCLUDE_GTEST_GTEST_SPI_H_ |
| 37 | |
| 38 | #include <gtest/gtest.h> |
| 39 | |
| 40 | namespace testing { |
| 41 | |
| 42 | // A copyable object representing the result of a test part (i.e. an |
| 43 | // assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()). |
| 44 | // |
| 45 | // Don't inherit from TestPartResult as its destructor is not virtual. |
| 46 | class TestPartResult { |
| 47 | public: |
| 48 | // C'tor. TestPartResult does NOT have a default constructor. |
| 49 | // Always use this constructor (with parameters) to create a |
| 50 | // TestPartResult object. |
| 51 | TestPartResult(TestPartResultType type, |
| 52 | const char* file_name, |
| 53 | int line_number, |
| 54 | const char* message) |
| 55 | : type_(type), |
| 56 | file_name_(file_name), |
| 57 | line_number_(line_number), |
| 58 | message_(message) { |
| 59 | } |
| 60 | |
| 61 | // Gets the outcome of the test part. |
| 62 | TestPartResultType type() const { return type_; } |
| 63 | |
| 64 | // Gets the name of the source file where the test part took place, or |
| 65 | // NULL if it's unknown. |
| 66 | const char* file_name() const { return file_name_.c_str(); } |
| 67 | |
| 68 | // Gets the line in the source file where the test part took place, |
| 69 | // or -1 if it's unknown. |
| 70 | int line_number() const { return line_number_; } |
| 71 | |
| 72 | // Gets the message associated with the test part. |
| 73 | const char* message() const { return message_.c_str(); } |
| 74 | |
| 75 | // Returns true iff the test part passed. |
| 76 | bool passed() const { return type_ == TPRT_SUCCESS; } |
| 77 | |
| 78 | // Returns true iff the test part failed. |
| 79 | bool failed() const { return type_ != TPRT_SUCCESS; } |
| 80 | |
| 81 | // Returns true iff the test part non-fatally failed. |
| 82 | bool nonfatally_failed() const { return type_ == TPRT_NONFATAL_FAILURE; } |
| 83 | |
| 84 | // Returns true iff the test part fatally failed. |
| 85 | bool fatally_failed() const { return type_ == TPRT_FATAL_FAILURE; } |
| 86 | private: |
| 87 | TestPartResultType type_; |
| 88 | |
| 89 | // The name of the source file where the test part took place, or |
| 90 | // NULL if the source file is unknown. |
| 91 | internal::String file_name_; |
| 92 | // The line in the source file where the test part took place, or -1 |
| 93 | // if the line number is unknown. |
| 94 | int line_number_; |
| 95 | internal::String message_; // The test failure message. |
| 96 | }; |
| 97 | |
| 98 | // Prints a TestPartResult object. |
| 99 | std::ostream& operator<<(std::ostream& os, const TestPartResult& result); |
| 100 | |
| 101 | // An array of TestPartResult objects. |
| 102 | // |
| 103 | // We define this class as we cannot use STL containers when compiling |
| 104 | // Google Test with MSVC 7.1 and exceptions disabled. |
| 105 | // |
| 106 | // Don't inherit from TestPartResultArray as its destructor is not |
| 107 | // virtual. |
| 108 | class TestPartResultArray { |
| 109 | public: |
| 110 | TestPartResultArray(); |
| 111 | ~TestPartResultArray(); |
| 112 | |
| 113 | // Appends the given TestPartResult to the array. |
| 114 | void Append(const TestPartResult& result); |
| 115 | |
| 116 | // Returns the TestPartResult at the given index (0-based). |
| 117 | const TestPartResult& GetTestPartResult(int index) const; |
| 118 | |
| 119 | // Returns the number of TestPartResult objects in the array. |
| 120 | int size() const; |
| 121 | private: |
| 122 | // Internally we use a list to simulate the array. Yes, this means |
| 123 | // that random access is O(N) in time, but it's OK for its purpose. |
| 124 | internal::List<TestPartResult>* const list_; |
| 125 | |
| 126 | GTEST_DISALLOW_COPY_AND_ASSIGN(TestPartResultArray); |
| 127 | }; |
| 128 | |
| 129 | // This interface knows how to report a test part result. |
| 130 | class TestPartResultReporterInterface { |
| 131 | public: |
| 132 | virtual ~TestPartResultReporterInterface() {} |
| 133 | |
| 134 | virtual void ReportTestPartResult(const TestPartResult& result) = 0; |
| 135 | }; |
| 136 | |
| 137 | // This helper class can be used to mock out Google Test failure reporting |
| 138 | // so that we can test Google Test or code that builds on Google Test. |
| 139 | // |
| 140 | // An object of this class appends a TestPartResult object to the |
| 141 | // TestPartResultArray object given in the constructor whenever a |
| 142 | // Google Test failure is reported. |
| 143 | class ScopedFakeTestPartResultReporter |
| 144 | : public TestPartResultReporterInterface { |
| 145 | public: |
| 146 | // The c'tor sets this object as the test part result reporter used |
| 147 | // by Google Test. The 'result' parameter specifies where to report the |
| 148 | // results. |
| 149 | explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result); |
| 150 | |
| 151 | // The d'tor restores the previous test part result reporter. |
| 152 | virtual ~ScopedFakeTestPartResultReporter(); |
| 153 | |
| 154 | // Appends the TestPartResult object to the TestPartResultArray |
| 155 | // received in the constructor. |
| 156 | // |
| 157 | // This method is from the TestPartResultReporterInterface |
| 158 | // interface. |
| 159 | virtual void ReportTestPartResult(const TestPartResult& result); |
| 160 | private: |
| 161 | TestPartResultReporterInterface* const old_reporter_; |
| 162 | TestPartResultArray* const result_; |
| 163 | |
| 164 | GTEST_DISALLOW_COPY_AND_ASSIGN(ScopedFakeTestPartResultReporter); |
| 165 | }; |
| 166 | |
| 167 | namespace internal { |
| 168 | |
| 169 | // A helper class for implementing EXPECT_FATAL_FAILURE() and |
| 170 | // EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given |
| 171 | // TestPartResultArray contains exactly one failure that has the given |
| 172 | // type and contains the given substring. If that's not the case, a |
| 173 | // non-fatal failure will be generated. |
| 174 | class SingleFailureChecker { |
| 175 | public: |
| 176 | // The constructor remembers the arguments. |
| 177 | SingleFailureChecker(const TestPartResultArray* results, |
| 178 | TestPartResultType type, |
| 179 | const char* substr); |
| 180 | ~SingleFailureChecker(); |
| 181 | private: |
| 182 | const TestPartResultArray* const results_; |
| 183 | const TestPartResultType type_; |
| 184 | const String substr_; |
| 185 | |
| 186 | GTEST_DISALLOW_COPY_AND_ASSIGN(SingleFailureChecker); |
| 187 | }; |
| 188 | |
| 189 | } // namespace internal |
| 190 | |
| 191 | } // namespace testing |
| 192 | |
| 193 | // A macro for testing Google Test assertions or code that's expected to |
| 194 | // generate Google Test fatal failures. It verifies that the given |
| 195 | // statement will cause exactly one fatal Google Test failure with 'substr' |
| 196 | // being part of the failure message. |
| 197 | // |
| 198 | // Implementation note: The verification is done in the destructor of |
| 199 | // SingleFailureChecker, to make sure that it's done even when |
| 200 | // 'statement' throws an exception. |
| 201 | // |
| 202 | // Known restrictions: |
| 203 | // - 'statement' cannot reference local non-static variables or |
| 204 | // non-static members of the current object. |
| 205 | // - 'statement' cannot return a value. |
| 206 | // - You cannot stream a failure message to this macro. |
| 207 | #define EXPECT_FATAL_FAILURE(statement, substr) do {\ |
| 208 | class GTestExpectFatalFailureHelper {\ |
| 209 | public:\ |
| 210 | static void Execute() { statement; }\ |
| 211 | };\ |
| 212 | ::testing::TestPartResultArray gtest_failures;\ |
| 213 | ::testing::internal::SingleFailureChecker gtest_checker(\ |
| 214 | >est_failures, ::testing::TPRT_FATAL_FAILURE, (substr));\ |
| 215 | {\ |
| 216 | ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ |
| 217 | >est_failures);\ |
| 218 | GTestExpectFatalFailureHelper::Execute();\ |
| 219 | }\ |
| 220 | } while (false) |
| 221 | |
| 222 | // A macro for testing Google Test assertions or code that's expected to |
| 223 | // generate Google Test non-fatal failures. It asserts that the given |
| 224 | // statement will cause exactly one non-fatal Google Test failure with |
| 225 | // 'substr' being part of the failure message. |
| 226 | // |
| 227 | // 'statement' is allowed to reference local variables and members of |
| 228 | // the current object. |
| 229 | // |
| 230 | // Implementation note: The verification is done in the destructor of |
| 231 | // SingleFailureChecker, to make sure that it's done even when |
| 232 | // 'statement' throws an exception or aborts the function. |
| 233 | // |
| 234 | // Known restrictions: |
| 235 | // - You cannot stream a failure message to this macro. |
| 236 | #define EXPECT_NONFATAL_FAILURE(statement, substr) do {\ |
| 237 | ::testing::TestPartResultArray gtest_failures;\ |
| 238 | ::testing::internal::SingleFailureChecker gtest_checker(\ |
| 239 | >est_failures, ::testing::TPRT_NONFATAL_FAILURE, (substr));\ |
| 240 | {\ |
| 241 | ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\ |
| 242 | >est_failures);\ |
| 243 | statement;\ |
| 244 | }\ |
| 245 | } while (false) |
| 246 | |
| 247 | #endif // GTEST_INCLUDE_GTEST_GTEST_SPI_H_ |