gtest-test-part.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // IWYU pragma: private, include "gtest/gtest.h"
  30. // IWYU pragma: friend gtest/.*
  31. // IWYU pragma: friend gmock/.*
  32. #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
  33. #define GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_
  34. #include <iosfwd>
  35. #include <ostream>
  36. #include <string>
  37. #include <vector>
  38. #include "gtest/internal/gtest-internal.h"
  39. #include "gtest/internal/gtest-string.h"
  40. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
  41. /* class A needs to have dll-interface to be used by clients of class B */)
  42. namespace testing {
  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.
  47. class 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. kSkip // Skipped.
  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, const char* a_file_name, int a_line_number,
  61. const char* a_message)
  62. : type_(a_type),
  63. file_name_(a_file_name == nullptr ? "" : a_file_name),
  64. line_number_(a_line_number),
  65. summary_(ExtractSummary(a_message)),
  66. message_(a_message) {}
  67. // Gets the outcome of the test part.
  68. Type type() const { return type_; }
  69. // Gets the name of the source file where the test part took place, or
  70. // NULL if it's unknown.
  71. const char* file_name() const {
  72. return file_name_.empty() ? nullptr : file_name_.c_str();
  73. }
  74. // Gets the line in the source file where the test part took place,
  75. // or -1 if it's unknown.
  76. int line_number() const { return line_number_; }
  77. // Gets the summary of the failure message.
  78. const char* summary() const { return summary_.c_str(); }
  79. // Gets the message associated with the test part.
  80. const char* message() const { return message_.c_str(); }
  81. // Returns true if and only if the test part was skipped.
  82. bool skipped() const { return type_ == kSkip; }
  83. // Returns true if and only if the test part passed.
  84. bool passed() const { return type_ == kSuccess; }
  85. // Returns true if and only if the test part non-fatally failed.
  86. bool nonfatally_failed() const { return type_ == kNonFatalFailure; }
  87. // Returns true if and only if the test part fatally failed.
  88. bool fatally_failed() const { return type_ == kFatalFailure; }
  89. // Returns true if and only if the test part failed.
  90. bool failed() const { return fatally_failed() || nonfatally_failed(); }
  91. private:
  92. Type type_;
  93. // Gets the summary of the failure message by omitting the stack
  94. // trace in it.
  95. static std::string ExtractSummary(const char* message);
  96. // The name of the source file where the test part took place, or
  97. // "" if the source file is unknown.
  98. std::string file_name_;
  99. // The line in the source file where the test part took place, or -1
  100. // if the line number is unknown.
  101. int line_number_;
  102. std::string summary_; // The test failure summary.
  103. std::string message_; // The test failure message.
  104. };
  105. // Prints a TestPartResult object.
  106. std::ostream& operator<<(std::ostream& os, const TestPartResult& result);
  107. // An array of TestPartResult objects.
  108. //
  109. // Don't inherit from TestPartResultArray as its destructor is not
  110. // virtual.
  111. class GTEST_API_ TestPartResultArray {
  112. public:
  113. TestPartResultArray() = default;
  114. // Appends the given TestPartResult to the array.
  115. void Append(const TestPartResult& result);
  116. // Returns the TestPartResult at the given index (0-based).
  117. const TestPartResult& GetTestPartResult(int index) const;
  118. // Returns the number of TestPartResult objects in the array.
  119. int size() const;
  120. private:
  121. std::vector<TestPartResult> array_;
  122. TestPartResultArray(const TestPartResultArray&) = delete;
  123. TestPartResultArray& operator=(const TestPartResultArray&) = delete;
  124. };
  125. // This interface knows how to report a test part result.
  126. class GTEST_API_ TestPartResultReporterInterface {
  127. public:
  128. virtual ~TestPartResultReporterInterface() = default;
  129. virtual void ReportTestPartResult(const TestPartResult& result) = 0;
  130. };
  131. namespace internal {
  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 {
  140. public:
  141. HasNewFatalFailureHelper();
  142. ~HasNewFatalFailureHelper() override;
  143. void ReportTestPartResult(const TestPartResult& result) override;
  144. bool has_new_fatal_failure() const { return has_new_fatal_failure_; }
  145. private:
  146. bool has_new_fatal_failure_;
  147. TestPartResultReporterInterface* original_reporter_;
  148. HasNewFatalFailureHelper(const HasNewFatalFailureHelper&) = delete;
  149. HasNewFatalFailureHelper& operator=(const HasNewFatalFailureHelper&) = delete;
  150. };
  151. } // namespace internal
  152. } // namespace testing
  153. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
  154. #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_TEST_PART_H_