1e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// Copyright 2008, Google Inc.
2e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// All rights reserved.
3e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//
4e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// Redistribution and use in source and binary forms, with or without
5e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// modification, are permitted provided that the following conditions are
6e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// met:
7e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//
8e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//     * Redistributions of source code must retain the above copyright
9e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// notice, this list of conditions and the following disclaimer.
10e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//     * Redistributions in binary form must reproduce the above
11e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// copyright notice, this list of conditions and the following disclaimer
12e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// in the documentation and/or other materials provided with the
13e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// distribution.
14e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//     * Neither the name of Google Inc. nor the names of its
15e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// contributors may be used to endorse or promote products derived from
16e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// this software without specific prior written permission.
17e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//
18e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//
30e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// Author: mheule@google.com (Markus Heule)
31e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//
32e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
33e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian#ifndef GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
34e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian#define GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
35e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
36e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian#include <iosfwd>
3793d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan#include <vector>
382620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/internal/gtest-internal.h"
392620c79810d4741922e9fa89050c0af564994f24zhanyong.wan#include "gtest/internal/gtest-string.h"
40e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
41e44602ec83c65102035ce5304ae8de0cb16e9e56shiqiannamespace testing {
42e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
43e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// A copyable object representing the result of a test part (i.e. an
44e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()).
45e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//
46e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// Don't inherit from TestPartResult as its destructor is not virtual.
47e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanclass GTEST_API_ TestPartResult {
48e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian public:
49334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  // The possible outcomes of a test part (i.e. an assertion or an
50334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  // explicit SUCCEED(), FAIL(), or ADD_FAILURE()).
51334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  enum Type {
52334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan    kSuccess,          // Succeeded.
53334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan    kNonFatalFailure,  // Failed but the test can continue.
54334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan    kFatalFailure      // Failed and the test should be terminated.
55334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  };
56334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan
57e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // C'tor.  TestPartResult does NOT have a default constructor.
58e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Always use this constructor (with parameters) to create a
59e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // TestPartResult object.
607de3401f5f8c2a61476a3623e9802f973764fc27zhanyong.wan  TestPartResult(Type a_type,
617de3401f5f8c2a61476a3623e9802f973764fc27zhanyong.wan                 const char* a_file_name,
627de3401f5f8c2a61476a3623e9802f973764fc27zhanyong.wan                 int a_line_number,
637de3401f5f8c2a61476a3623e9802f973764fc27zhanyong.wan                 const char* a_message)
647de3401f5f8c2a61476a3623e9802f973764fc27zhanyong.wan      : type_(a_type),
6503c314931649a999b0cf5deb0a434a1009157416jgm@google.com        file_name_(a_file_name == NULL ? "" : a_file_name),
667de3401f5f8c2a61476a3623e9802f973764fc27zhanyong.wan        line_number_(a_line_number),
677de3401f5f8c2a61476a3623e9802f973764fc27zhanyong.wan        summary_(ExtractSummary(a_message)),
687de3401f5f8c2a61476a3623e9802f973764fc27zhanyong.wan        message_(a_message) {
69e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  }
70e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
71e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Gets the outcome of the test part.
72334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  Type type() const { return type_; }
73e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
74e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Gets the name of the source file where the test part took place, or
75e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // NULL if it's unknown.
7603c314931649a999b0cf5deb0a434a1009157416jgm@google.com  const char* file_name() const {
7703c314931649a999b0cf5deb0a434a1009157416jgm@google.com    return file_name_.empty() ? NULL : file_name_.c_str();
7803c314931649a999b0cf5deb0a434a1009157416jgm@google.com  }
79e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
80e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Gets the line in the source file where the test part took place,
81e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // or -1 if it's unknown.
82e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  int line_number() const { return line_number_; }
83e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
84e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Gets the summary of the failure message.
85e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  const char* summary() const { return summary_.c_str(); }
86e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
87e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Gets the message associated with the test part.
88e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  const char* message() const { return message_.c_str(); }
89e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
90e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Returns true iff the test part passed.
91334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  bool passed() const { return type_ == kSuccess; }
92e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
93e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Returns true iff the test part failed.
94334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  bool failed() const { return type_ != kSuccess; }
95e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
96e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Returns true iff the test part non-fatally failed.
97334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  bool nonfatally_failed() const { return type_ == kNonFatalFailure; }
98e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
99e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Returns true iff the test part fatally failed.
100334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  bool fatally_failed() const { return type_ == kFatalFailure; }
10193fed47dbf8e6bc3d39d3f769cb5039551747257vladlosev
102e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian private:
103334aaead71ccf797a18f2128c320f0304e724860zhanyong.wan  Type type_;
104e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
105e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Gets the summary of the failure message by omitting the stack
106e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // trace in it.
10703c314931649a999b0cf5deb0a434a1009157416jgm@google.com  static std::string ExtractSummary(const char* message);
108e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
109e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // The name of the source file where the test part took place, or
11003c314931649a999b0cf5deb0a434a1009157416jgm@google.com  // "" if the source file is unknown.
11103c314931649a999b0cf5deb0a434a1009157416jgm@google.com  std::string file_name_;
112e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // The line in the source file where the test part took place, or -1
113e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // if the line number is unknown.
114e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  int line_number_;
11503c314931649a999b0cf5deb0a434a1009157416jgm@google.com  std::string summary_;  // The test failure summary.
11603c314931649a999b0cf5deb0a434a1009157416jgm@google.com  std::string message_;  // The test failure message.
117e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian};
118e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
119e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// Prints a TestPartResult object.
120e44602ec83c65102035ce5304ae8de0cb16e9e56shiqianstd::ostream& operator<<(std::ostream& os, const TestPartResult& result);
121e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
122e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// An array of TestPartResult objects.
123e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian//
124e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// Don't inherit from TestPartResultArray as its destructor is not
125e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// virtual.
126e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanclass GTEST_API_ TestPartResultArray {
127e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian public:
12893d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan  TestPartResultArray() {}
129e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
130e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Appends the given TestPartResult to the array.
131e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  void Append(const TestPartResult& result);
132e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
133e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Returns the TestPartResult at the given index (0-based).
134e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  const TestPartResult& GetTestPartResult(int index) const;
135e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
136e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  // Returns the number of TestPartResult objects in the array.
137e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  int size() const;
13893d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan
139e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian private:
14093d13a8bbcb70bfd80b0d7ae2bf9aedfc06bf0cczhanyong.wan  std::vector<TestPartResult> array_;
141e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
142e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestPartResultArray);
143e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian};
144e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
145e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// This interface knows how to report a test part result.
146e44602ec83c65102035ce5304ae8de0cb16e9e56shiqianclass TestPartResultReporterInterface {
147e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian public:
148e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  virtual ~TestPartResultReporterInterface() {}
149e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
150e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  virtual void ReportTestPartResult(const TestPartResult& result) = 0;
151e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian};
152e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
153e44602ec83c65102035ce5304ae8de0cb16e9e56shiqiannamespace internal {
154e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
155e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a
156e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// statement generates new fatal failures. To do so it registers itself as the
157e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// current test part result reporter. Besides checking if fatal failures were
158e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// reported, it only delegates the reporting to the former result reporter.
159e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// The original result reporter is restored in the destructor.
160e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
161e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wanclass GTEST_API_ HasNewFatalFailureHelper
162e4092294d7c1f38f303e8c7f67da31ba3e7e7d9azhanyong.wan    : public TestPartResultReporterInterface {
163e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian public:
164e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  HasNewFatalFailureHelper();
165e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  virtual ~HasNewFatalFailureHelper();
166e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  virtual void ReportTestPartResult(const TestPartResult& result);
167e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  bool has_new_fatal_failure() const { return has_new_fatal_failure_; }
168e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian private:
169e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  bool has_new_fatal_failure_;
170e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  TestPartResultReporterInterface* original_reporter_;
171e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
172e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian  GTEST_DISALLOW_COPY_AND_ASSIGN_(HasNewFatalFailureHelper);
173e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian};
174e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
175e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian}  // namespace internal
176e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
177e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian}  // namespace testing
178e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian
179e44602ec83c65102035ce5304ae8de0cb16e9e56shiqian#endif  // GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
180