You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
6.3 KiB

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