1e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Copyright 2008, Google Inc. 2e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// All rights reserved. 3e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// 4e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Redistribution and use in source and binary forms, with or without 5e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// modification, are permitted provided that the following conditions are 6e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// met: 7e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// 8e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// * Redistributions of source code must retain the above copyright 9e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// notice, this list of conditions and the following disclaimer. 10e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// * Redistributions in binary form must reproduce the above 11e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// copyright notice, this list of conditions and the following disclaimer 12e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// in the documentation and/or other materials provided with the 13e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// distribution. 14e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// * Neither the name of Google Inc. nor the names of its 15e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// contributors may be used to endorse or promote products derived from 16e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// this software without specific prior written permission. 17e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// 18e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// 30e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Author: mheule@google.com (Markus Heule) 31e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// 32e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 33e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian#ifndef GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ 34e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian#define GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ 35e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 36e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian#include <iosfwd> 370d27868d0faef474594682f25336229daa89d6d7zhanyong.wan#include <vector> 38dac3e879c56a50696a36f53e1e5e353e48fa665fzhanyong.wan#include "gtest/internal/gtest-internal.h" 39dac3e879c56a50696a36f53e1e5e353e48fa665fzhanyong.wan#include "gtest/internal/gtest-string.h" 40e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 41e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqiannamespace testing { 42e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 43e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// A copyable object representing the result of a test part (i.e. an 44e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()). 45e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// 46e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Don't inherit from TestPartResult as its destructor is not virtual. 4783589cca345d2f03d93b0555437aa480e0ed6699zhanyong.wanclass GTEST_API_ TestPartResult { 48e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian public: 49e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan // The possible outcomes of a test part (i.e. an assertion or an 50e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan // explicit SUCCEED(), FAIL(), or ADD_FAILURE()). 51e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan enum Type { 52e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan kSuccess, // Succeeded. 53e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan kNonFatalFailure, // Failed but the test can continue. 54e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan kFatalFailure // Failed and the test should be terminated. 55e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan }; 56e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan 57e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // C'tor. TestPartResult does NOT have a default constructor. 58e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Always use this constructor (with parameters) to create a 59e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // TestPartResult object. 60d56773b492b7b675d5c547baab815289a7815bddzhanyong.wan TestPartResult(Type a_type, 61d56773b492b7b675d5c547baab815289a7815bddzhanyong.wan const char* a_file_name, 62d56773b492b7b675d5c547baab815289a7815bddzhanyong.wan int a_line_number, 63d56773b492b7b675d5c547baab815289a7815bddzhanyong.wan const char* a_message) 64d56773b492b7b675d5c547baab815289a7815bddzhanyong.wan : type_(a_type), 6587fdda2cf24d953f3cbec1e0c266b2db9928f406jgm file_name_(a_file_name == NULL ? "" : a_file_name), 66d56773b492b7b675d5c547baab815289a7815bddzhanyong.wan line_number_(a_line_number), 67d56773b492b7b675d5c547baab815289a7815bddzhanyong.wan summary_(ExtractSummary(a_message)), 68d56773b492b7b675d5c547baab815289a7815bddzhanyong.wan message_(a_message) { 69e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian } 70e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 71e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Gets the outcome of the test part. 72e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan Type type() const { return type_; } 73e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 74e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Gets the name of the source file where the test part took place, or 75e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // NULL if it's unknown. 7687fdda2cf24d953f3cbec1e0c266b2db9928f406jgm const char* file_name() const { 7787fdda2cf24d953f3cbec1e0c266b2db9928f406jgm return file_name_.empty() ? NULL : file_name_.c_str(); 7887fdda2cf24d953f3cbec1e0c266b2db9928f406jgm } 79e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 80e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Gets the line in the source file where the test part took place, 81e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // or -1 if it's unknown. 82e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian int line_number() const { return line_number_; } 83e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 84e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Gets the summary of the failure message. 85e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian const char* summary() const { return summary_.c_str(); } 86e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 87e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Gets the message associated with the test part. 88e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian const char* message() const { return message_.c_str(); } 89e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 90e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Returns true iff the test part passed. 91e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan bool passed() const { return type_ == kSuccess; } 92e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 93e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Returns true iff the test part failed. 94e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan bool failed() const { return type_ != kSuccess; } 95e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 96e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Returns true iff the test part non-fatally failed. 97e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan bool nonfatally_failed() const { return type_ == kNonFatalFailure; } 98e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 99e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Returns true iff the test part fatally failed. 100e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan bool fatally_failed() const { return type_ == kFatalFailure; } 1018965a6a0d2165f32e6413594bba6367f271f51e7vladlosev 102e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian private: 103e5373af0cb9cae249e7bc618cae3483397332895zhanyong.wan Type type_; 104e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 105e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Gets the summary of the failure message by omitting the stack 106e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // trace in it. 10787fdda2cf24d953f3cbec1e0c266b2db9928f406jgm static std::string ExtractSummary(const char* message); 108e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 109e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // The name of the source file where the test part took place, or 11087fdda2cf24d953f3cbec1e0c266b2db9928f406jgm // "" if the source file is unknown. 11187fdda2cf24d953f3cbec1e0c266b2db9928f406jgm std::string file_name_; 112e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // The line in the source file where the test part took place, or -1 113e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // if the line number is unknown. 114e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian int line_number_; 11587fdda2cf24d953f3cbec1e0c266b2db9928f406jgm std::string summary_; // The test failure summary. 11687fdda2cf24d953f3cbec1e0c266b2db9928f406jgm std::string message_; // The test failure message. 117e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}; 118e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 119e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Prints a TestPartResult object. 120e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqianstd::ostream& operator<<(std::ostream& os, const TestPartResult& result); 121e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 122e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// An array of TestPartResult objects. 123e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// 124e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Don't inherit from TestPartResultArray as its destructor is not 125e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// virtual. 12683589cca345d2f03d93b0555437aa480e0ed6699zhanyong.wanclass GTEST_API_ TestPartResultArray { 127e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian public: 1280d27868d0faef474594682f25336229daa89d6d7zhanyong.wan TestPartResultArray() {} 129e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 130e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Appends the given TestPartResult to the array. 131e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian void Append(const TestPartResult& result); 132e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 133e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Returns the TestPartResult at the given index (0-based). 134e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian const TestPartResult& GetTestPartResult(int index) const; 135e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 136e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian // Returns the number of TestPartResult objects in the array. 137e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian int size() const; 1380d27868d0faef474594682f25336229daa89d6d7zhanyong.wan 139e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian private: 1400d27868d0faef474594682f25336229daa89d6d7zhanyong.wan std::vector<TestPartResult> array_; 141e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 142e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian GTEST_DISALLOW_COPY_AND_ASSIGN_(TestPartResultArray); 143e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}; 144e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 145e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// This interface knows how to report a test part result. 146e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqianclass TestPartResultReporterInterface { 147e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian public: 148e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian virtual ~TestPartResultReporterInterface() {} 149e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 150e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian virtual void ReportTestPartResult(const TestPartResult& result) = 0; 151e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}; 152e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 153e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqiannamespace internal { 154e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 155e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a 156e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// statement generates new fatal failures. To do so it registers itself as the 157e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// current test part result reporter. Besides checking if fatal failures were 158e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// reported, it only delegates the reporting to the former result reporter. 159e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// The original result reporter is restored in the destructor. 160e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 16183589cca345d2f03d93b0555437aa480e0ed6699zhanyong.wanclass GTEST_API_ HasNewFatalFailureHelper 16283589cca345d2f03d93b0555437aa480e0ed6699zhanyong.wan : public TestPartResultReporterInterface { 163e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian public: 164e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian HasNewFatalFailureHelper(); 165e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian virtual ~HasNewFatalFailureHelper(); 166e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian virtual void ReportTestPartResult(const TestPartResult& result); 167e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian bool has_new_fatal_failure() const { return has_new_fatal_failure_; } 168e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian private: 169e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian bool has_new_fatal_failure_; 170e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian TestPartResultReporterInterface* original_reporter_; 171e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 172e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian GTEST_DISALLOW_COPY_AND_ASSIGN_(HasNewFatalFailureHelper); 173e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}; 174e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 175e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian} // namespace internal 176e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 177e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian} // namespace testing 178e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian 179e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian#endif // GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_ 180