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), 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 { return file_name_.c_str(); } 00077 00078 // Gets the line in the source file where the test part took place, 00079 // or -1 if it's unknown. 00080 int line_number() const { return line_number_; } 00081 00082 // Gets the summary of the failure message. 00083 const char* summary() const { return summary_.c_str(); } 00084 00085 // Gets the message associated with the test part. 00086 const char* message() const { return message_.c_str(); } 00087 00088 // Returns true iff the test part passed. 00089 bool passed() const { return type_ == kSuccess; } 00090 00091 // Returns true iff the test part failed. 00092 bool failed() const { return type_ != kSuccess; } 00093 00094 // Returns true iff the test part non-fatally failed. 00095 bool nonfatally_failed() const { return type_ == kNonFatalFailure; } 00096 00097 // Returns true iff the test part fatally failed. 00098 bool fatally_failed() const { return type_ == kFatalFailure; } 00099 private: 00100 Type type_; 00101 00102 // Gets the summary of the failure message by omitting the stack 00103 // trace in it. 00104 static internal::String ExtractSummary(const char* message); 00105 00106 // The name of the source file where the test part took place, or 00107 // NULL if the source file is unknown. 00108 internal::String file_name_; 00109 // The line in the source file where the test part took place, or -1 00110 // if the line number is unknown. 00111 int line_number_; 00112 internal::String summary_; // The test failure summary. 00113 internal::String message_; // The test failure message. 00114 }; 00115 00116 // Prints a TestPartResult object. 00117 std::ostream& operator<<(std::ostream& os, const TestPartResult& result); 00118 00119 // An array of TestPartResult objects. 00120 // 00121 // Don't inherit from TestPartResultArray as its destructor is not 00122 // virtual. 00123 class GTEST_API_ TestPartResultArray { 00124 public: 00125 TestPartResultArray() {} 00126 00127 // Appends the given TestPartResult to the array. 00128 void Append(const TestPartResult& result); 00129 00130 // Returns the TestPartResult at the given index (0-based). 00131 const TestPartResult& GetTestPartResult(int index) const; 00132 00133 // Returns the number of TestPartResult objects in the array. 00134 int size() const; 00135 00136 private: 00137 std::vector<TestPartResult> array_; 00138 00139 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestPartResultArray); 00140 }; 00141 00142 // This interface knows how to report a test part result. 00143 class TestPartResultReporterInterface { 00144 public: 00145 virtual ~TestPartResultReporterInterface() {} 00146 00147 virtual void ReportTestPartResult(const TestPartResult& result) = 0; 00148 }; 00149 00150 namespace internal { 00151 00152 // This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a 00153 // statement generates new fatal failures. To do so it registers itself as the 00154 // current test part result reporter. Besides checking if fatal failures were 00155 // reported, it only delegates the reporting to the former result reporter. 00156 // The original result reporter is restored in the destructor. 00157 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 00158 class GTEST_API_ HasNewFatalFailureHelper 00159 : public TestPartResultReporterInterface { 00160 public: 00161 HasNewFatalFailureHelper(); 00162 virtual ~HasNewFatalFailureHelper(); 00163 virtual void ReportTestPartResult(const TestPartResult& result); 00164 bool has_new_fatal_failure() const { return has_new_fatal_failure_; } 00165 private: 00166 bool has_new_fatal_failure_; 00167 TestPartResultReporterInterface* original_reporter_; 00168 00169 GTEST_DISALLOW_COPY_AND_ASSIGN_(HasNewFatalFailureHelper); 00170 }; 00171 00172 } // namespace internal 00173 00174 } // namespace testing 00175 00176 #endif // GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_