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