gtest-test-part.cc revision aaebfcdc4005afb22b68df61b58edd1ccc002913
1e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Copyright 2008, Google Inc.
2e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// All rights reserved.
3e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian//
4e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Redistribution and use in source and binary forms, with or without
5e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// modification, are permitted provided that the following conditions are
6e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// met:
7e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian//
8e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian//     * Redistributions of source code must retain the above copyright
9e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// notice, this list of conditions and the following disclaimer.
10e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian//     * Redistributions in binary form must reproduce the above
11e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// copyright notice, this list of conditions and the following disclaimer
12e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// in the documentation and/or other materials provided with the
13e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// distribution.
14e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian//     * Neither the name of Google Inc. nor the names of its
15e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// contributors may be used to endorse or promote products derived from
16e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// this software without specific prior written permission.
17e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian//
18e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian//
30e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Author: mheule@google.com (Markus Heule)
31e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian//
32e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// The Google C++ Testing Framework (Google Test)
33e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
34e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian#include <gtest/gtest-test-part.h>
35e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
36e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Indicates that this translation unit is part of Google Test's
37e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// implementation.  It must come before gtest-internal-inl.h is
38e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// included, or there will be a compiler error.  This trick is to
39e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// prevent a user from accidentally including gtest-internal-inl.h in
40e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// his code.
410af0709b02899f9177db55eba7929e65e5834b29zhanyong.wan#define GTEST_IMPLEMENTATION_ 1
42e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian#include "src/gtest-internal-inl.h"
430af0709b02899f9177db55eba7929e65e5834b29zhanyong.wan#undef GTEST_IMPLEMENTATION_
44e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
45e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqiannamespace testing {
46e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
47aaebfcdc4005afb22b68df61b58edd1ccc002913zhanyong.wanusing internal::GetUnitTestImpl;
48aaebfcdc4005afb22b68df61b58edd1ccc002913zhanyong.wan
49e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Gets the summary of the failure message by omitting the stack trace
50e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// in it.
51e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqianinternal::String TestPartResult::ExtractSummary(const char* message) {
52e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  const char* const stack_trace = strstr(message, internal::kStackTraceMarker);
53e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  return stack_trace == NULL ? internal::String(message) :
54e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian      internal::String(message, stack_trace - message);
55e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}
56e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
57e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Prints a TestPartResult object.
58e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqianstd::ostream& operator<<(std::ostream& os, const TestPartResult& result) {
59e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  return os << result.file_name() << ":"
60e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian            << result.line_number() << ": "
61e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian            << (result.type() == TPRT_SUCCESS ? "Success" :
62e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian                result.type() == TPRT_FATAL_FAILURE ? "Fatal failure" :
63e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian                "Non-fatal failure") << ":\n"
64e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian            << result.message() << std::endl;
65e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}
66e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
67e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Constructs an empty TestPartResultArray.
68e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqianTestPartResultArray::TestPartResultArray()
69e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian    : list_(new internal::List<TestPartResult>) {
70e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}
71e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
72e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Destructs a TestPartResultArray.
73e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqianTestPartResultArray::~TestPartResultArray() {
74e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  delete list_;
75e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}
76e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
77e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Appends a TestPartResult to the array.
78e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqianvoid TestPartResultArray::Append(const TestPartResult& result) {
79e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  list_->PushBack(result);
80e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}
81e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
82e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Returns the TestPartResult at the given index (0-based).
83e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqianconst TestPartResult& TestPartResultArray::GetTestPartResult(int index) const {
84e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  if (index < 0 || index >= size()) {
85e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian    printf("\nInvalid index (%d) into TestPartResultArray.\n", index);
86f2d0d0e3d56794855d1e9a1f157457b7225e8c88zhanyong.wan    internal::posix::Abort();
87e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  }
88e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
89e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  const internal::ListNode<TestPartResult>* p = list_->Head();
90e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  for (int i = 0; i < index; i++) {
91e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian    p = p->next();
92e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  }
93e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
94e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  return p->element();
95e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}
96e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
97e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian// Returns the number of TestPartResult objects in the array.
98e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqianint TestPartResultArray::size() const {
99e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  return list_->size();
100e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}
101e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
102e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqiannamespace internal {
103e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
104e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqianHasNewFatalFailureHelper::HasNewFatalFailureHelper()
105e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian    : has_new_fatal_failure_(false),
106aaebfcdc4005afb22b68df61b58edd1ccc002913zhanyong.wan      original_reporter_(GetUnitTestImpl()->
107e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian                         GetTestPartResultReporterForCurrentThread()) {
108aaebfcdc4005afb22b68df61b58edd1ccc002913zhanyong.wan  GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(this);
109e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}
110e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
111e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqianHasNewFatalFailureHelper::~HasNewFatalFailureHelper() {
112aaebfcdc4005afb22b68df61b58edd1ccc002913zhanyong.wan  GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(
113e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian      original_reporter_);
114e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}
115e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
116e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqianvoid HasNewFatalFailureHelper::ReportTestPartResult(
117e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian    const TestPartResult& result) {
118e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  if (result.fatally_failed())
119e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian    has_new_fatal_failure_ = true;
120e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian  original_reporter_->ReportTestPartResult(result);
121e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}
122e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
123e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}  // namespace internal
124e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian
125e0865dd9199e8fffd5c2f95a68de6c1851f77c15shiqian}  // namespace testing
126