blob: 161278027d085bbd84676269dbc8841dece765be [file] [log] [blame]
Misha Brukman7ae6ff42008-12-31 17:34:06 +00001// 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// Author: mheule@google.com (Markus Heule)
31//
32// The Google C++ Testing Framework (Google Test)
33
Jay Foadb33f8e32011-07-27 09:25:14 +000034#include "gtest/gtest-test-part.h"
Misha Brukman7ae6ff42008-12-31 17:34:06 +000035
36// Indicates that this translation unit is part of Google Test's
37// implementation. It must come before gtest-internal-inl.h is
38// included, or there will be a compiler error. This trick is to
39// prevent a user from accidentally including gtest-internal-inl.h in
40// his code.
Benjamin Kramere4b9c932010-06-02 22:01:25 +000041#define GTEST_IMPLEMENTATION_ 1
Misha Brukmane5f94712009-01-01 02:05:43 +000042#include "gtest/internal/gtest-internal-inl.h"
Benjamin Kramere4b9c932010-06-02 22:01:25 +000043#undef GTEST_IMPLEMENTATION_
Misha Brukman7ae6ff42008-12-31 17:34:06 +000044
45namespace testing {
46
Benjamin Kramer190f8ee2010-06-02 22:02:11 +000047using internal::GetUnitTestImpl;
48
Misha Brukman7ae6ff42008-12-31 17:34:06 +000049// Gets the summary of the failure message by omitting the stack trace
50// in it.
51internal::String TestPartResult::ExtractSummary(const char* message) {
52 const char* const stack_trace = strstr(message, internal::kStackTraceMarker);
53 return stack_trace == NULL ? internal::String(message) :
54 internal::String(message, stack_trace - message);
55}
56
57// Prints a TestPartResult object.
58std::ostream& operator<<(std::ostream& os, const TestPartResult& result) {
Benjamin Kramer190f8ee2010-06-02 22:02:11 +000059 return os
60 << result.file_name() << ":" << result.line_number() << ": "
61 << (result.type() == TestPartResult::kSuccess ? "Success" :
62 result.type() == TestPartResult::kFatalFailure ? "Fatal failure" :
63 "Non-fatal failure") << ":\n"
64 << result.message() << std::endl;
Misha Brukman7ae6ff42008-12-31 17:34:06 +000065}
66
Misha Brukman7ae6ff42008-12-31 17:34:06 +000067// Appends a TestPartResult to the array.
68void TestPartResultArray::Append(const TestPartResult& result) {
Benjamin Kramer57240ff2010-06-02 22:02:30 +000069 array_.push_back(result);
Misha Brukman7ae6ff42008-12-31 17:34:06 +000070}
71
72// Returns the TestPartResult at the given index (0-based).
73const TestPartResult& TestPartResultArray::GetTestPartResult(int index) const {
74 if (index < 0 || index >= size()) {
75 printf("\nInvalid index (%d) into TestPartResultArray.\n", index);
Benjamin Kramer190f8ee2010-06-02 22:02:11 +000076 internal::posix::Abort();
Misha Brukman7ae6ff42008-12-31 17:34:06 +000077 }
78
Benjamin Kramer57240ff2010-06-02 22:02:30 +000079 return array_[index];
Misha Brukman7ae6ff42008-12-31 17:34:06 +000080}
81
82// Returns the number of TestPartResult objects in the array.
83int TestPartResultArray::size() const {
Benjamin Kramer57240ff2010-06-02 22:02:30 +000084 return static_cast<int>(array_.size());
Misha Brukman7ae6ff42008-12-31 17:34:06 +000085}
86
87namespace internal {
88
89HasNewFatalFailureHelper::HasNewFatalFailureHelper()
90 : has_new_fatal_failure_(false),
Benjamin Kramer190f8ee2010-06-02 22:02:11 +000091 original_reporter_(GetUnitTestImpl()->
Misha Brukman7ae6ff42008-12-31 17:34:06 +000092 GetTestPartResultReporterForCurrentThread()) {
Benjamin Kramer190f8ee2010-06-02 22:02:11 +000093 GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(this);
Misha Brukman7ae6ff42008-12-31 17:34:06 +000094}
95
96HasNewFatalFailureHelper::~HasNewFatalFailureHelper() {
Benjamin Kramer190f8ee2010-06-02 22:02:11 +000097 GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(
Misha Brukman7ae6ff42008-12-31 17:34:06 +000098 original_reporter_);
99}
100
101void HasNewFatalFailureHelper::ReportTestPartResult(
102 const TestPartResult& result) {
103 if (result.fatally_failed())
104 has_new_fatal_failure_ = true;
105 original_reporter_->ReportTestPartResult(result);
106}
107
108} // namespace internal
109
110} // namespace testing