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