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