1 // Copyright 2008, 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: mheule@google.com (Markus Heule) 31 // 32 33 #ifndef GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ 34 #define GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ 35 36 #include <iosfwd> 37 #include <gtest/internal/gtest-internal.h> 38 #include <gtest/internal/gtest-string.h> 39 40 namespace testing { 41 42 // The possible outcomes of a test part (i.e. an assertion or an 43 // explicit SUCCEED(), FAIL(), or ADD_FAILURE()). 44 enum TestPartResultType { 45 TPRT_SUCCESS, // Succeeded. 46 TPRT_NONFATAL_FAILURE, // Failed but the test can continue. 47 TPRT_FATAL_FAILURE // Failed and the test should be terminated. 48 }; 49 50 // A copyable object representing the result of a test part (i.e. an 51 // assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()). 52 // 53 // Don't inherit from TestPartResult as its destructor is not virtual. 54 class TestPartResult { 55 public: 56 // C'tor. TestPartResult does NOT have a default constructor. 57 // Always use this constructor (with parameters) to create a 58 // TestPartResult object. TestPartResult(TestPartResultType type,const char * file_name,int line_number,const char * message)59 TestPartResult(TestPartResultType type, 60 const char* file_name, 61 int line_number, 62 const char* message) 63 : type_(type), 64 file_name_(file_name), 65 line_number_(line_number), 66 summary_(ExtractSummary(message)), 67 message_(message) { 68 } 69 70 // Gets the outcome of the test part. type()71 TestPartResultType type() const { return type_; } 72 73 // Gets the name of the source file where the test part took place, or 74 // NULL if it's unknown. file_name()75 const char* file_name() const { return file_name_.c_str(); } 76 77 // Gets the line in the source file where the test part took place, 78 // or -1 if it's unknown. line_number()79 int line_number() const { return line_number_; } 80 81 // Gets the summary of the failure message. summary()82 const char* summary() const { return summary_.c_str(); } 83 84 // Gets the message associated with the test part. message()85 const char* message() const { return message_.c_str(); } 86 87 // Returns true iff the test part passed. passed()88 bool passed() const { return type_ == TPRT_SUCCESS; } 89 90 // Returns true iff the test part failed. failed()91 bool failed() const { return type_ != TPRT_SUCCESS; } 92 93 // Returns true iff the test part non-fatally failed. nonfatally_failed()94 bool nonfatally_failed() const { return type_ == TPRT_NONFATAL_FAILURE; } 95 96 // Returns true iff the test part fatally failed. fatally_failed()97 bool fatally_failed() const { return type_ == TPRT_FATAL_FAILURE; } 98 private: 99 TestPartResultType type_; 100 101 // Gets the summary of the failure message by omitting the stack 102 // trace in it. 103 static internal::String ExtractSummary(const char* message); 104 105 // The name of the source file where the test part took place, or 106 // NULL if the source file is unknown. 107 internal::String file_name_; 108 // The line in the source file where the test part took place, or -1 109 // if the line number is unknown. 110 int line_number_; 111 internal::String summary_; // The test failure summary. 112 internal::String message_; // The test failure message. 113 }; 114 115 // Prints a TestPartResult object. 116 std::ostream& operator<<(std::ostream& os, const TestPartResult& result); 117 118 // An array of TestPartResult objects. 119 // 120 // We define this class as we cannot use STL containers when compiling 121 // Google Test with MSVC 7.1 and exceptions disabled. 122 // 123 // Don't inherit from TestPartResultArray as its destructor is not 124 // virtual. 125 class TestPartResultArray { 126 public: 127 TestPartResultArray(); 128 ~TestPartResultArray(); 129 130 // Appends the given TestPartResult to the array. 131 void Append(const TestPartResult& result); 132 133 // Returns the TestPartResult at the given index (0-based). 134 const TestPartResult& GetTestPartResult(int index) const; 135 136 // Returns the number of TestPartResult objects in the array. 137 int size() const; 138 private: 139 // Internally we use a list to simulate the array. Yes, this means 140 // that random access is O(N) in time, but it's OK for its purpose. 141 internal::List<TestPartResult>* const list_; 142 143 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestPartResultArray); 144 }; 145 146 // This interface knows how to report a test part result. 147 class TestPartResultReporterInterface { 148 public: ~TestPartResultReporterInterface()149 virtual ~TestPartResultReporterInterface() {} 150 151 virtual void ReportTestPartResult(const TestPartResult& result) = 0; 152 }; 153 154 namespace internal { 155 156 // This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a 157 // statement generates new fatal failures. To do so it registers itself as the 158 // current test part result reporter. Besides checking if fatal failures were 159 // reported, it only delegates the reporting to the former result reporter. 160 // The original result reporter is restored in the destructor. 161 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 162 class HasNewFatalFailureHelper : public TestPartResultReporterInterface { 163 public: 164 HasNewFatalFailureHelper(); 165 virtual ~HasNewFatalFailureHelper(); 166 virtual void ReportTestPartResult(const TestPartResult& result); has_new_fatal_failure()167 bool has_new_fatal_failure() const { return has_new_fatal_failure_; } 168 private: 169 bool has_new_fatal_failure_; 170 TestPartResultReporterInterface* original_reporter_; 171 172 GTEST_DISALLOW_COPY_AND_ASSIGN_(HasNewFatalFailureHelper); 173 }; 174 175 } // namespace internal 176 177 } // namespace testing 178 179 #endif // GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ 180