00001 // Copyright 2008, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // Author: mheule@google.com (Markus Heule) 00031 // 00032 00033 #ifndef GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ 00034 #define GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ 00035 00036 #include <iosfwd> 00037 #include <vector> 00038 #include "gtest/internal/gtest-internal.h" 00039 #include "gtest/internal/gtest-string.h" 00040 00041 namespace testing { 00042 00043 // A copyable object representing the result of a test part (i.e. an 00044 // assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()). 00045 // 00046 // Don't inherit from TestPartResult as its destructor is not virtual. 00047 class GTEST_API_ TestPartResult { 00048 public: 00049 // The possible outcomes of a test part (i.e. an assertion or an 00050 // explicit SUCCEED(), FAIL(), or ADD_FAILURE()). 00051 enum Type { 00052 kSuccess, // Succeeded. 00053 kNonFatalFailure, // Failed but the test can continue. 00054 kFatalFailure // Failed and the test should be terminated. 00055 }; 00056 00057 // C'tor. TestPartResult does NOT have a default constructor. 00058 // Always use this constructor (with parameters) to create a 00059 // TestPartResult object. 00060 TestPartResult(Type a_type, 00061 const char* a_file_name, 00062 int a_line_number, 00063 const char* a_message) 00064 : type_(a_type), 00065 file_name_(a_file_name == NULL ? "" : a_file_name), 00066 line_number_(a_line_number), 00067 summary_(ExtractSummary(a_message)), 00068 message_(a_message) { 00069 } 00070 00071 // Gets the outcome of the test part. 00072 Type type() const { return type_; } 00073 00074 // Gets the name of the source file where the test part took place, or 00075 // NULL if it's unknown. 00076 const char* file_name() const { 00077 return file_name_.empty() ? NULL : file_name_.c_str(); 00078 } 00079 00080 // Gets the line in the source file where the test part took place, 00081 // or -1 if it's unknown. 00082 int line_number() const { return line_number_; } 00083 00084 // Gets the summary of the failure message. 00085 const char* summary() const { return summary_.c_str(); } 00086 00087 // Gets the message associated with the test part. 00088 const char* message() const { return message_.c_str(); } 00089 00090 // Returns true iff the test part passed. 00091 bool passed() const { return type_ == kSuccess; } 00092 00093 // Returns true iff the test part failed. 00094 bool failed() const { return type_ != kSuccess; } 00095 00096 // Returns true iff the test part non-fatally failed. 00097 bool nonfatally_failed() const { return type_ == kNonFatalFailure; } 00098 00099 // Returns true iff the test part fatally failed. 00100 bool fatally_failed() const { return type_ == kFatalFailure; } 00101 00102 private: 00103 Type type_; 00104 00105 // Gets the summary of the failure message by omitting the stack 00106 // trace in it. 00107 static std::string ExtractSummary(const char* message); 00108 00109 // The name of the source file where the test part took place, or 00110 // "" if the source file is unknown. 00111 std::string file_name_; 00112 // The line in the source file where the test part took place, or -1 00113 // if the line number is unknown. 00114 int line_number_; 00115 std::string summary_; // The test failure summary. 00116 std::string message_; // The test failure message. 00117 }; 00118 00119 // Prints a TestPartResult object. 00120 std::ostream& operator<<(std::ostream& os, const TestPartResult& result); 00121 00122 // An array of TestPartResult objects. 00123 // 00124 // Don't inherit from TestPartResultArray as its destructor is not 00125 // virtual. 00126 class GTEST_API_ TestPartResultArray { 00127 public: 00128 TestPartResultArray() {} 00129 00130 // Appends the given TestPartResult to the array. 00131 void Append(const TestPartResult& result); 00132 00133 // Returns the TestPartResult at the given index (0-based). 00134 const TestPartResult& GetTestPartResult(int index) const; 00135 00136 // Returns the number of TestPartResult objects in the array. 00137 int size() const; 00138 00139 private: 00140 std::vector<TestPartResult> array_; 00141 00142 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestPartResultArray); 00143 }; 00144 00145 // This interface knows how to report a test part result. 00146 class TestPartResultReporterInterface { 00147 public: 00148 virtual ~TestPartResultReporterInterface() {} 00149 00150 virtual void ReportTestPartResult(const TestPartResult& result) = 0; 00151 }; 00152 00153 namespace internal { 00154 00155 // This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a 00156 // statement generates new fatal failures. To do so it registers itself as the 00157 // current test part result reporter. Besides checking if fatal failures were 00158 // reported, it only delegates the reporting to the former result reporter. 00159 // The original result reporter is restored in the destructor. 00160 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 00161 class GTEST_API_ HasNewFatalFailureHelper 00162 : public TestPartResultReporterInterface { 00163 public: 00164 HasNewFatalFailureHelper(); 00165 virtual ~HasNewFatalFailureHelper(); 00166 virtual void ReportTestPartResult(const TestPartResult& result); 00167 bool has_new_fatal_failure() const { return has_new_fatal_failure_; } 00168 private: 00169 bool has_new_fatal_failure_; 00170 TestPartResultReporterInterface* original_reporter_; 00171 00172 GTEST_DISALLOW_COPY_AND_ASSIGN_(HasNewFatalFailureHelper); 00173 }; 00174 00175 } // namespace internal 00176 00177 } // namespace testing 00178 00179 #endif // GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_