gtest-test-part.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // The Google C++ Testing and Mocking Framework (Google Test)
  31. #include "gtest/gtest-test-part.h"
  32. #include "gtest/internal/gtest-port.h"
  33. #include "src/gtest-internal-inl.h"
  34. namespace testing {
  35. using internal::GetUnitTestImpl;
  36. // Gets the summary of the failure message by omitting the stack trace
  37. // in it.
  38. std::string TestPartResult::ExtractSummary(const char* message) {
  39. const char* const stack_trace = strstr(message, internal::kStackTraceMarker);
  40. return stack_trace == nullptr ? message : std::string(message, stack_trace);
  41. }
  42. // Prints a TestPartResult object.
  43. std::ostream& operator<<(std::ostream& os, const TestPartResult& result) {
  44. return os << internal::FormatFileLocation(result.file_name(),
  45. result.line_number())
  46. << " "
  47. << (result.type() == TestPartResult::kSuccess
  48. ? "Success"
  49. : result.type() == TestPartResult::kSkip
  50. ? "Skipped"
  51. : result.type() == TestPartResult::kFatalFailure
  52. ? "Fatal failure"
  53. : "Non-fatal failure")
  54. << ":\n"
  55. << result.message() << std::endl;
  56. }
  57. // Appends a TestPartResult to the array.
  58. void TestPartResultArray::Append(const TestPartResult& result) {
  59. array_.push_back(result);
  60. }
  61. // Returns the TestPartResult at the given index (0-based).
  62. const TestPartResult& TestPartResultArray::GetTestPartResult(int index) const {
  63. if (index < 0 || index >= size()) {
  64. printf("\nInvalid index (%d) into TestPartResultArray.\n", index);
  65. internal::posix::Abort();
  66. }
  67. return array_[static_cast<size_t>(index)];
  68. }
  69. // Returns the number of TestPartResult objects in the array.
  70. int TestPartResultArray::size() const {
  71. return static_cast<int>(array_.size());
  72. }
  73. namespace internal {
  74. HasNewFatalFailureHelper::HasNewFatalFailureHelper()
  75. : has_new_fatal_failure_(false),
  76. original_reporter_(GetUnitTestImpl()->
  77. GetTestPartResultReporterForCurrentThread()) {
  78. GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(this);
  79. }
  80. HasNewFatalFailureHelper::~HasNewFatalFailureHelper() {
  81. GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(
  82. original_reporter_);
  83. }
  84. void HasNewFatalFailureHelper::ReportTestPartResult(
  85. const TestPartResult& result) {
  86. if (result.fatally_failed())
  87. has_new_fatal_failure_ = true;
  88. original_reporter_->ReportTestPartResult(result);
  89. }
  90. } // namespace internal
  91. } // namespace testing