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