193 lines · c
1// Copyright 2008, Google Inc.2// All rights reserved.3//4// Redistribution and use in source and binary forms, with or without5// modification, are permitted provided that the following conditions are6// met:7//8// * Redistributions of source code must retain the above copyright9// notice, this list of conditions and the following disclaimer.10// * Redistributions in binary form must reproduce the above11// copyright notice, this list of conditions and the following disclaimer12// in the documentation and/or other materials provided with the13// distribution.14// * Neither the name of Google Inc. nor the names of its15// contributors may be used to endorse or promote products derived from16// this software without specific prior written permission.17//18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE28// 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 <ostream>39#include <string>40#include <vector>41 42#include "gtest/internal/gtest-internal.h"43#include "gtest/internal/gtest-string.h"44 45GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \46/* class A needs to have dll-interface to be used by clients of class B */)47 48namespace testing {49 50// A copyable object representing the result of a test part (i.e. an51// assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()).52//53// Don't inherit from TestPartResult as its destructor is not virtual.54class GTEST_API_ TestPartResult {55 public:56 // The possible outcomes of a test part (i.e. an assertion or an57 // explicit SUCCEED(), FAIL(), or ADD_FAILURE()).58 enum Type {59 kSuccess, // Succeeded.60 kNonFatalFailure, // Failed but the test can continue.61 kFatalFailure, // Failed and the test should be terminated.62 kSkip // Skipped.63 };64 65 // C'tor. TestPartResult does NOT have a default constructor.66 // Always use this constructor (with parameters) to create a67 // TestPartResult object.68 TestPartResult(Type a_type, const char* a_file_name, int a_line_number,69 const char* a_message)70 : type_(a_type),71 file_name_(a_file_name == nullptr ? "" : a_file_name),72 line_number_(a_line_number),73 summary_(ExtractSummary(a_message)),74 message_(a_message) {}75 76 // Gets the outcome of the test part.77 Type type() const { return type_; }78 79 // Gets the name of the source file where the test part took place, or80 // NULL if it's unknown.81 const char* file_name() const {82 return file_name_.empty() ? nullptr : file_name_.c_str();83 }84 85 // Gets the line in the source file where the test part took place,86 // or -1 if it's unknown.87 int line_number() const { return line_number_; }88 89 // Gets the summary of the failure message.90 const char* summary() const { return summary_.c_str(); }91 92 // Gets the message associated with the test part.93 const char* message() const { return message_.c_str(); }94 95 // Returns true if and only if the test part was skipped.96 bool skipped() const { return type_ == kSkip; }97 98 // Returns true if and only if the test part passed.99 bool passed() const { return type_ == kSuccess; }100 101 // Returns true if and only if the test part non-fatally failed.102 bool nonfatally_failed() const { return type_ == kNonFatalFailure; }103 104 // Returns true if and only if the test part fatally failed.105 bool fatally_failed() const { return type_ == kFatalFailure; }106 107 // Returns true if and only if the test part failed.108 bool failed() const { return fatally_failed() || nonfatally_failed(); }109 110 private:111 Type type_;112 113 // Gets the summary of the failure message by omitting the stack114 // trace in it.115 static std::string ExtractSummary(const char* message);116 117 // The name of the source file where the test part took place, or118 // "" if the source file is unknown.119 std::string file_name_;120 // The line in the source file where the test part took place, or -1121 // if the line number is unknown.122 int line_number_;123 std::string summary_; // The test failure summary.124 std::string message_; // The test failure message.125};126 127// Prints a TestPartResult object.128std::ostream& operator<<(std::ostream& os, const TestPartResult& result);129 130// An array of TestPartResult objects.131//132// Don't inherit from TestPartResultArray as its destructor is not133// virtual.134class GTEST_API_ TestPartResultArray {135 public:136 TestPartResultArray() = default;137 138 // Appends the given TestPartResult to the array.139 void Append(const TestPartResult& result);140 141 // Returns the TestPartResult at the given index (0-based).142 const TestPartResult& GetTestPartResult(int index) const;143 144 // Returns the number of TestPartResult objects in the array.145 int size() const;146 147 private:148 std::vector<TestPartResult> array_;149 150 TestPartResultArray(const TestPartResultArray&) = delete;151 TestPartResultArray& operator=(const TestPartResultArray&) = delete;152};153 154// This interface knows how to report a test part result.155class GTEST_API_ TestPartResultReporterInterface {156 public:157 virtual ~TestPartResultReporterInterface() = default;158 159 virtual void ReportTestPartResult(const TestPartResult& result) = 0;160};161 162namespace internal {163 164// This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a165// statement generates new fatal failures. To do so it registers itself as the166// current test part result reporter. Besides checking if fatal failures were167// reported, it only delegates the reporting to the former result reporter.168// The original result reporter is restored in the destructor.169// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.170class GTEST_API_ HasNewFatalFailureHelper171 : public TestPartResultReporterInterface {172 public:173 HasNewFatalFailureHelper();174 ~HasNewFatalFailureHelper() override;175 void ReportTestPartResult(const TestPartResult& result) override;176 bool has_new_fatal_failure() const { return has_new_fatal_failure_; }177 178 private:179 bool has_new_fatal_failure_;180 TestPartResultReporterInterface* original_reporter_;181 182 HasNewFatalFailureHelper(const HasNewFatalFailureHelper&) = delete;183 HasNewFatalFailureHelper& operator=(const HasNewFatalFailureHelper&) = delete;184};185 186} // namespace internal187 188} // namespace testing189 190GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251191 192#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_193