/Unittests/googletest/src/gtest-test-part.cc

http://unladen-swallow.googlecode.com/ · C++ · 120 lines · 57 code · 18 blank · 45 comment · 6 complexity · e5dd97a34f762b93e5f5450ba15ad538 MD5 · raw file

  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. // The Google C++ Testing Framework (Google Test)
  33. #include <gtest/gtest-test-part.h>
  34. // Indicates that this translation unit is part of Google Test's
  35. // implementation. It must come before gtest-internal-inl.h is
  36. // included, or there will be a compiler error. This trick is to
  37. // prevent a user from accidentally including gtest-internal-inl.h in
  38. // his code.
  39. #define GTEST_IMPLEMENTATION_ 1
  40. #include "src/gtest-internal-inl.h"
  41. #undef GTEST_IMPLEMENTATION_
  42. namespace testing {
  43. using internal::GetUnitTestImpl;
  44. // Gets the summary of the failure message by omitting the stack trace
  45. // in it.
  46. internal::String TestPartResult::ExtractSummary(const char* message) {
  47. const char* const stack_trace = strstr(message, internal::kStackTraceMarker);
  48. return stack_trace == NULL ? internal::String(message) :
  49. internal::String(message, stack_trace - message);
  50. }
  51. // Prints a TestPartResult object.
  52. std::ostream& operator<<(std::ostream& os, const TestPartResult& result) {
  53. return os
  54. << result.file_name() << ":" << result.line_number() << ": "
  55. << (result.type() == TestPartResult::kSuccess ? "Success" :
  56. result.type() == TestPartResult::kFatalFailure ? "Fatal failure" :
  57. "Non-fatal failure") << ":\n"
  58. << result.message() << std::endl;
  59. }
  60. // Constructs an empty TestPartResultArray.
  61. TestPartResultArray::TestPartResultArray()
  62. : array_(new internal::Vector<TestPartResult>) {
  63. }
  64. // Destructs a TestPartResultArray.
  65. TestPartResultArray::~TestPartResultArray() {
  66. delete array_;
  67. }
  68. // Appends a TestPartResult to the array.
  69. void TestPartResultArray::Append(const TestPartResult& result) {
  70. array_->PushBack(result);
  71. }
  72. // Returns the TestPartResult at the given index (0-based).
  73. const TestPartResult& TestPartResultArray::GetTestPartResult(int index) const {
  74. if (index < 0 || index >= size()) {
  75. printf("\nInvalid index (%d) into TestPartResultArray.\n", index);
  76. internal::posix::Abort();
  77. }
  78. return array_->GetElement(index);
  79. }
  80. // Returns the number of TestPartResult objects in the array.
  81. int TestPartResultArray::size() const {
  82. return array_->size();
  83. }
  84. namespace internal {
  85. HasNewFatalFailureHelper::HasNewFatalFailureHelper()
  86. : has_new_fatal_failure_(false),
  87. original_reporter_(GetUnitTestImpl()->
  88. GetTestPartResultReporterForCurrentThread()) {
  89. GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(this);
  90. }
  91. HasNewFatalFailureHelper::~HasNewFatalFailureHelper() {
  92. GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(
  93. original_reporter_);
  94. }
  95. void HasNewFatalFailureHelper::ReportTestPartResult(
  96. const TestPartResult& result) {
  97. if (result.fatally_failed())
  98. has_new_fatal_failure_ = true;
  99. original_reporter_->ReportTestPartResult(result);
  100. }
  101. } // namespace internal
  102. } // namespace testing