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// Author: mheule@google.com (Markus Heule)
31//
32
33#ifndef GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
34#define GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
35
36#include <iosfwd>
37#include <vector>
38#include "gtest/internal/gtest-internal.h"
39#include "gtest/internal/gtest-string.h"
40
41namespace testing {
42
43// A copyable object representing the result of a test part (i.e. an
44// assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()).
45//
46// Don't inherit from TestPartResult as its destructor is not virtual.
47class GTEST_API_ TestPartResult {
48 public:
49 // The possible outcomes of a test part (i.e. an assertion or an
50 // explicit SUCCEED(), FAIL(), or ADD_FAILURE()).
51 enum Type {
52 kSuccess, // Succeeded.
53 kNonFatalFailure, // Failed but the test can continue.
54 kFatalFailure // Failed and the test should be terminated.
55 };
56
57 // C'tor. TestPartResult does NOT have a default constructor.
58 // Always use this constructor (with parameters) to create a
59 // TestPartResult object.
60 TestPartResult(Type a_type,
61 const char* a_file_name,
62 int a_line_number,
63 const char* a_message)
64 : type_(a_type),
65 file_name_(a_file_name == NULL ? "" : a_file_name),
66 line_number_(a_line_number),
67 summary_(ExtractSummary(a_message)),
68 message_(a_message) {
69 }
70
71 // Gets the outcome of the test part.
72 Type type() const { return type_; }
73
74 // Gets the name of the source file where the test part took place, or
75 // NULL if it's unknown.
76 const char* file_name() const {
77 return file_name_.empty() ? NULL : file_name_.c_str();
78 }
79
80 // Gets the line in the source file where the test part took place,
81 // or -1 if it's unknown.
82 int line_number() const { return line_number_; }
83
84 // Gets the summary of the failure message.
85 const char* summary() const { return summary_.c_str(); }
86
87 // Gets the message associated with the test part.
88 const char* message() const { return message_.c_str(); }
89
90 // Returns true iff the test part passed.
91 bool passed() const { return type_ == kSuccess; }
92
93 // Returns true iff the test part failed.
94 bool failed() const { return type_ != kSuccess; }
95
96 // Returns true iff the test part non-fatally failed.
97 bool nonfatally_failed() const { return type_ == kNonFatalFailure; }
98
99 // Returns true iff the test part fatally failed.
100 bool fatally_failed() const { return type_ == kFatalFailure; }
101
102 private:
103 Type type_;
104
105 // Gets the summary of the failure message by omitting the stack
106 // trace in it.
107 static std::string ExtractSummary(const char* message);
108
109 // The name of the source file where the test part took place, or
110 // "" if the source file is unknown.
111 std::string file_name_;
112 // The line in the source file where the test part took place, or -1
113 // if the line number is unknown.
114 int line_number_;
115 std::string summary_; // The test failure summary.
116 std::string message_; // The test failure message.
117};
118
119// Prints a TestPartResult object.
120std::ostream& operator<<(std::ostream& os, const TestPartResult& result);
121
122// An array of TestPartResult objects.
123//
124// Don't inherit from TestPartResultArray as its destructor is not
125// virtual.
126class GTEST_API_ TestPartResultArray {
127 public:
128 TestPartResultArray() {}
129
130 // Appends the given TestPartResult to the array.
131 void Append(const TestPartResult& result);
132
133 // Returns the TestPartResult at the given index (0-based).
134 const TestPartResult& GetTestPartResult(int index) const;
135
136 // Returns the number of TestPartResult objects in the array.
137 int size() const;
138
139 private:
140 std::vector<TestPartResult> array_;
141
142 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestPartResultArray);
143};
144
145// This interface knows how to report a test part result.
146class TestPartResultReporterInterface {
147 public:
148 virtual ~TestPartResultReporterInterface() {}
149
150 virtual void ReportTestPartResult(const TestPartResult& result) = 0;
151};
152
153namespace internal {
154
155// This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a
156// statement generates new fatal failures. To do so it registers itself as the
157// current test part result reporter. Besides checking if fatal failures were
158// reported, it only delegates the reporting to the former result reporter.
159// The original result reporter is restored in the destructor.
160// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
161class GTEST_API_ HasNewFatalFailureHelper
162 : public TestPartResultReporterInterface {
163 public:
164 HasNewFatalFailureHelper();
165 virtual ~HasNewFatalFailureHelper();
166 virtual void ReportTestPartResult(const TestPartResult& result);
167 bool has_new_fatal_failure() const { return has_new_fatal_failure_; }
168 private:
169 bool has_new_fatal_failure_;
170 TestPartResultReporterInterface* original_reporter_;
171
172 GTEST_DISALLOW_COPY_AND_ASSIGN_(HasNewFatalFailureHelper);
173};
174
175} // namespace internal
176
177} // namespace testing
178
179#endif // GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
180