107 lines · plain
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//31// The Google C++ Testing and Mocking Framework (Google Test)32 33#include "gtest/gtest-test-part.h"34 35#include <ostream>36#include <string>37 38#include "gtest/internal/gtest-port.h"39#include "src/gtest-internal-inl.h"40 41namespace testing {42 43// Gets the summary of the failure message by omitting the stack trace44// in it.45std::string TestPartResult::ExtractSummary(const char* message) {46 const char* const stack_trace = strstr(message, internal::kStackTraceMarker);47 return stack_trace == nullptr ? message : std::string(message, stack_trace);48}49 50// Prints a TestPartResult object.51std::ostream& operator<<(std::ostream& os, const TestPartResult& result) {52 return os << internal::FormatFileLocation(result.file_name(),53 result.line_number())54 << " "55 << (result.type() == TestPartResult::kSuccess ? "Success"56 : result.type() == TestPartResult::kSkip ? "Skipped"57 : result.type() == TestPartResult::kFatalFailure58 ? "Fatal failure"59 : "Non-fatal failure")60 << ":\n"61 << result.message() << std::endl;62}63 64// Appends a TestPartResult to the array.65void TestPartResultArray::Append(const TestPartResult& result) {66 array_.push_back(result);67}68 69// Returns the TestPartResult at the given index (0-based).70const TestPartResult& TestPartResultArray::GetTestPartResult(int index) const {71 if (index < 0 || index >= size()) {72 printf("\nInvalid index (%d) into TestPartResultArray.\n", index);73 internal::posix::Abort();74 }75 76 return array_[static_cast<size_t>(index)];77}78 79// Returns the number of TestPartResult objects in the array.80int TestPartResultArray::size() const {81 return static_cast<int>(array_.size());82}83 84namespace internal {85 86HasNewFatalFailureHelper::HasNewFatalFailureHelper()87 : has_new_fatal_failure_(false),88 original_reporter_(89 GetUnitTestImpl()->GetTestPartResultReporterForCurrentThread()) {90 GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(this);91}92 93HasNewFatalFailureHelper::~HasNewFatalFailureHelper() {94 GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(95 original_reporter_);96}97 98void HasNewFatalFailureHelper::ReportTestPartResult(99 const TestPartResult& result) {100 if (result.fatally_failed()) has_new_fatal_failure_ = true;101 original_reporter_->ReportTestPartResult(result);102}103 104} // namespace internal105 106} // namespace testing107