1 // Copyright (C) 2018 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
6 #ifndef GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
7 #define GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
11 #include "gtest/internal/gtest-internal.h"
12 #include "gtest/internal/gtest-string.h"
16 // A copyable object representing the result of a test part (i.e. an
17 // assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()).
19 // Don't inherit from TestPartResult as its destructor is not virtual.
20 class GTEST_API_ TestPartResult {
22 // The possible outcomes of a test part (i.e. an assertion or an
23 // explicit SUCCEED(), FAIL(), or ADD_FAILURE()).
25 kSuccess, // Succeeded.
26 kNonFatalFailure, // Failed but the test can continue.
27 kFatalFailure, // Failed and the test should be terminated.
31 // C'tor. TestPartResult does NOT have a default constructor.
32 // Always use this constructor (with parameters) to create a
33 // TestPartResult object.
34 TestPartResult(Type a_type,
35 const char* a_file_name,
37 const char* a_message)
39 file_name_(a_file_name == NULL ? "" : a_file_name),
40 line_number_(a_line_number),
41 summary_(ExtractSummary(a_message)),
45 // Gets the outcome of the test part.
46 Type type() const { return type_; }
48 // Gets the name of the source file where the test part took place, or
49 // NULL if it's unknown.
50 const char* file_name() const {
51 return file_name_.empty() ? NULL : file_name_.c_str();
54 // Gets the line in the source file where the test part took place,
55 // or -1 if it's unknown.
56 int line_number() const { return line_number_; }
58 // Gets the summary of the failure message.
59 const char* summary() const { return summary_.c_str(); }
61 // Gets the message associated with the test part.
62 const char* message() const { return message_.c_str(); }
64 // Returns true iff the test part was skipped.
65 bool skipped() const { return type_ == kSkip; }
67 // Returns true iff the test part passed.
68 bool passed() const { return type_ == kSuccess; }
70 // Returns true iff the test part non-fatally failed.
71 bool nonfatally_failed() const { return type_ == kNonFatalFailure; }
73 // Returns true iff the test part fatally failed.
74 bool fatally_failed() const { return type_ == kFatalFailure; }
76 // Returns true iff the test part failed.
77 bool failed() const { return fatally_failed() || nonfatally_failed(); }
82 // Gets the summary of the failure message by omitting the stack
84 static std::string ExtractSummary(const char* message);
86 // The name of the source file where the test part took place, or
87 // "" if the source file is unknown.
88 std::string file_name_;
89 // The line in the source file where the test part took place, or -1
90 // if the line number is unknown.
92 std::string summary_; // The test failure summary.
93 std::string message_; // The test failure message.
96 // Prints a TestPartResult object.
97 std::ostream& operator<<(std::ostream& os, const TestPartResult& result);
99 // An array of TestPartResult objects.
101 // Don't inherit from TestPartResultArray as its destructor is not
103 class GTEST_API_ TestPartResultArray {
105 TestPartResultArray() {}
107 // Appends the given TestPartResult to the array.
108 void Append(const TestPartResult& result);
110 // Returns the TestPartResult at the given index (0-based).
111 const TestPartResult& GetTestPartResult(int index) const;
113 // Returns the number of TestPartResult objects in the array.
117 std::vector<TestPartResult> array_;
119 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestPartResultArray);
122 // This interface knows how to report a test part result.
123 class TestPartResultReporterInterface {
125 virtual ~TestPartResultReporterInterface() {}
127 virtual void ReportTestPartResult(const TestPartResult& result) = 0;
132 // This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a
133 // statement generates new fatal failures. To do so it registers itself as the
134 // current test part result reporter. Besides checking if fatal failures were
135 // reported, it only delegates the reporting to the former result reporter.
136 // The original result reporter is restored in the destructor.
137 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
138 class GTEST_API_ HasNewFatalFailureHelper
139 : public TestPartResultReporterInterface {
141 HasNewFatalFailureHelper();
142 virtual ~HasNewFatalFailureHelper();
143 virtual void ReportTestPartResult(const TestPartResult& result);
144 bool has_new_fatal_failure() const { return has_new_fatal_failure_; }
146 bool has_new_fatal_failure_;
147 TestPartResultReporterInterface* original_reporter_;
149 GTEST_DISALLOW_COPY_AND_ASSIGN_(HasNewFatalFailureHelper);
152 } // namespace internal
154 } // namespace testing
156 #endif // GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_