Path: blob/master/dep/googletest/include/gtest/gtest-test-part.h
4806 views
// Copyright 2008, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// IWYU pragma: private, include "gtest/gtest.h"30// IWYU pragma: friend gtest/.*31// IWYU pragma: friend gmock/.*3233#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_34#define GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_3536#include <iosfwd>37#include <ostream>38#include <string>39#include <vector>4041#include "gtest/internal/gtest-internal.h"42#include "gtest/internal/gtest-string.h"4344GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \45/* class A needs to have dll-interface to be used by clients of class B */)4647namespace testing {4849// A copyable object representing the result of a test part (i.e. an50// assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()).51//52// Don't inherit from TestPartResult as its destructor is not virtual.53class GTEST_API_ TestPartResult {54public:55// The possible outcomes of a test part (i.e. an assertion or an56// explicit SUCCEED(), FAIL(), or ADD_FAILURE()).57enum Type {58kSuccess, // Succeeded.59kNonFatalFailure, // Failed but the test can continue.60kFatalFailure, // Failed and the test should be terminated.61kSkip // Skipped.62};6364// C'tor. TestPartResult does NOT have a default constructor.65// Always use this constructor (with parameters) to create a66// TestPartResult object.67TestPartResult(Type a_type, const char* a_file_name, int a_line_number,68const char* a_message)69: type_(a_type),70file_name_(a_file_name == nullptr ? "" : a_file_name),71line_number_(a_line_number),72summary_(ExtractSummary(a_message)),73message_(a_message) {}7475// Gets the outcome of the test part.76Type type() const { return type_; }7778// Gets the name of the source file where the test part took place, or79// NULL if it's unknown.80const char* file_name() const {81return file_name_.empty() ? nullptr : file_name_.c_str();82}8384// Gets the line in the source file where the test part took place,85// or -1 if it's unknown.86int line_number() const { return line_number_; }8788// Gets the summary of the failure message.89const char* summary() const { return summary_.c_str(); }9091// Gets the message associated with the test part.92const char* message() const { return message_.c_str(); }9394// Returns true if and only if the test part was skipped.95bool skipped() const { return type_ == kSkip; }9697// Returns true if and only if the test part passed.98bool passed() const { return type_ == kSuccess; }99100// Returns true if and only if the test part non-fatally failed.101bool nonfatally_failed() const { return type_ == kNonFatalFailure; }102103// Returns true if and only if the test part fatally failed.104bool fatally_failed() const { return type_ == kFatalFailure; }105106// Returns true if and only if the test part failed.107bool failed() const { return fatally_failed() || nonfatally_failed(); }108109private:110Type type_;111112// Gets the summary of the failure message by omitting the stack113// trace in it.114static std::string ExtractSummary(const char* message);115116// The name of the source file where the test part took place, or117// "" if the source file is unknown.118std::string file_name_;119// The line in the source file where the test part took place, or -1120// if the line number is unknown.121int line_number_;122std::string summary_; // The test failure summary.123std::string message_; // The test failure message.124};125126// Prints a TestPartResult object.127std::ostream& operator<<(std::ostream& os, const TestPartResult& result);128129// An array of TestPartResult objects.130//131// Don't inherit from TestPartResultArray as its destructor is not132// virtual.133class GTEST_API_ TestPartResultArray {134public:135TestPartResultArray() = default;136137// Appends the given TestPartResult to the array.138void Append(const TestPartResult& result);139140// Returns the TestPartResult at the given index (0-based).141const TestPartResult& GetTestPartResult(int index) const;142143// Returns the number of TestPartResult objects in the array.144int size() const;145146private:147std::vector<TestPartResult> array_;148149TestPartResultArray(const TestPartResultArray&) = delete;150TestPartResultArray& operator=(const TestPartResultArray&) = delete;151};152153// This interface knows how to report a test part result.154class GTEST_API_ TestPartResultReporterInterface {155public:156virtual ~TestPartResultReporterInterface() = default;157158virtual void ReportTestPartResult(const TestPartResult& result) = 0;159};160161namespace internal {162163// This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a164// statement generates new fatal failures. To do so it registers itself as the165// current test part result reporter. Besides checking if fatal failures were166// reported, it only delegates the reporting to the former result reporter.167// The original result reporter is restored in the destructor.168// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.169class GTEST_API_ HasNewFatalFailureHelper170: public TestPartResultReporterInterface {171public:172HasNewFatalFailureHelper();173~HasNewFatalFailureHelper() override;174void ReportTestPartResult(const TestPartResult& result) override;175bool has_new_fatal_failure() const { return has_new_fatal_failure_; }176177private:178bool has_new_fatal_failure_;179TestPartResultReporterInterface* original_reporter_;180181HasNewFatalFailureHelper(const HasNewFatalFailureHelper&) = delete;182HasNewFatalFailureHelper& operator=(const HasNewFatalFailureHelper&) = delete;183};184185} // namespace internal186187} // namespace testing188189GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251190191#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_192193194