blob: a938683ceded2f34697efd8305a67368064d0198 [file] [log] [blame]
Austin Schuh0cbef622015-09-06 17:34:52 -07001// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Austin Schuh889ac432018-10-29 22:57:02 -070029
Austin Schuh0cbef622015-09-06 17:34:52 -070030//
Austin Schuh889ac432018-10-29 22:57:02 -070031// The Google C++ Testing and Mocking Framework (Google Test)
Austin Schuh0cbef622015-09-06 17:34:52 -070032
33#include "gtest/gtest-test-part.h"
James Kuszmaule2f15292021-05-10 22:37:32 -070034
35#include "gtest/internal/gtest-port.h"
Austin Schuh0cbef622015-09-06 17:34:52 -070036#include "src/gtest-internal-inl.h"
Austin Schuh0cbef622015-09-06 17:34:52 -070037
38namespace testing {
39
40using internal::GetUnitTestImpl;
41
42// Gets the summary of the failure message by omitting the stack trace
43// in it.
44std::string TestPartResult::ExtractSummary(const char* message) {
45 const char* const stack_trace = strstr(message, internal::kStackTraceMarker);
James Kuszmaule2f15292021-05-10 22:37:32 -070046 return stack_trace == nullptr ? message : std::string(message, stack_trace);
Austin Schuh0cbef622015-09-06 17:34:52 -070047}
48
49// Prints a TestPartResult object.
50std::ostream& operator<<(std::ostream& os, const TestPartResult& result) {
James Kuszmaule2f15292021-05-10 22:37:32 -070051 return os << internal::FormatFileLocation(result.file_name(),
52 result.line_number())
53 << " "
54 << (result.type() == TestPartResult::kSuccess
55 ? "Success"
56 : result.type() == TestPartResult::kSkip
57 ? "Skipped"
58 : result.type() == TestPartResult::kFatalFailure
59 ? "Fatal failure"
60 : "Non-fatal failure")
61 << ":\n"
62 << result.message() << std::endl;
Austin Schuh0cbef622015-09-06 17:34:52 -070063}
64
65// Appends a TestPartResult to the array.
66void TestPartResultArray::Append(const TestPartResult& result) {
67 array_.push_back(result);
68}
69
70// Returns the TestPartResult at the given index (0-based).
71const TestPartResult& TestPartResultArray::GetTestPartResult(int index) const {
72 if (index < 0 || index >= size()) {
73 printf("\nInvalid index (%d) into TestPartResultArray.\n", index);
74 internal::posix::Abort();
75 }
76
James Kuszmaule2f15292021-05-10 22:37:32 -070077 return array_[static_cast<size_t>(index)];
Austin Schuh0cbef622015-09-06 17:34:52 -070078}
79
80// Returns the number of TestPartResult objects in the array.
81int TestPartResultArray::size() const {
82 return static_cast<int>(array_.size());
83}
84
85namespace internal {
86
87HasNewFatalFailureHelper::HasNewFatalFailureHelper()
88 : has_new_fatal_failure_(false),
89 original_reporter_(GetUnitTestImpl()->
90 GetTestPartResultReporterForCurrentThread()) {
91 GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(this);
92}
93
94HasNewFatalFailureHelper::~HasNewFatalFailureHelper() {
95 GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(
96 original_reporter_);
97}
98
99void HasNewFatalFailureHelper::ReportTestPartResult(
100 const TestPartResult& result) {
101 if (result.fatally_failed())
102 has_new_fatal_failure_ = true;
103 original_reporter_->ReportTestPartResult(result);
104}
105
106} // namespace internal
107
108} // namespace testing