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