/testlibs/gmock/src/gmock-internal-utils.cc

https://github.com/deltaforge/nebu-app-hadoop · C++ · 173 lines · 83 code · 16 blank · 74 comment · 28 complexity · 63a4716679686aba9a76000af9aeeffc MD5 · raw file

  1. // Copyright 2007, 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: wan@google.com (Zhanyong Wan)
  31. // Google Mock - a framework for writing C++ mock classes.
  32. //
  33. // This file defines some utilities useful for implementing Google
  34. // Mock. They are subject to change without notice, so please DO NOT
  35. // USE THEM IN USER CODE.
  36. #include "gmock/internal/gmock-internal-utils.h"
  37. #include <ctype.h>
  38. #include <ostream> // NOLINT
  39. #include <string>
  40. #include "gmock/gmock.h"
  41. #include "gmock/internal/gmock-port.h"
  42. #include "gtest/gtest.h"
  43. namespace testing {
  44. namespace internal {
  45. // Converts an identifier name to a space-separated list of lower-case
  46. // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is
  47. // treated as one word. For example, both "FooBar123" and
  48. // "foo_bar_123" are converted to "foo bar 123".
  49. string ConvertIdentifierNameToWords(const char* id_name) {
  50. string result;
  51. char prev_char = '\0';
  52. for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) {
  53. // We don't care about the current locale as the input is
  54. // guaranteed to be a valid C++ identifier name.
  55. const bool starts_new_word = IsUpper(*p) ||
  56. (!IsAlpha(prev_char) && IsLower(*p)) ||
  57. (!IsDigit(prev_char) && IsDigit(*p));
  58. if (IsAlNum(*p)) {
  59. if (starts_new_word && result != "")
  60. result += ' ';
  61. result += ToLower(*p);
  62. }
  63. }
  64. return result;
  65. }
  66. // This class reports Google Mock failures as Google Test failures. A
  67. // user can define another class in a similar fashion if he intends to
  68. // use Google Mock with a testing framework other than Google Test.
  69. class GoogleTestFailureReporter : public FailureReporterInterface {
  70. public:
  71. virtual void ReportFailure(FailureType type, const char* file, int line,
  72. const string& message) {
  73. AssertHelper(type == FATAL ?
  74. TestPartResult::kFatalFailure :
  75. TestPartResult::kNonFatalFailure,
  76. file,
  77. line,
  78. message.c_str()) = Message();
  79. if (type == FATAL) {
  80. posix::Abort();
  81. }
  82. }
  83. };
  84. // Returns the global failure reporter. Will create a
  85. // GoogleTestFailureReporter and return it the first time called.
  86. FailureReporterInterface* GetFailureReporter() {
  87. // Points to the global failure reporter used by Google Mock. gcc
  88. // guarantees that the following use of failure_reporter is
  89. // thread-safe. We may need to add additional synchronization to
  90. // protect failure_reporter if we port Google Mock to other
  91. // compilers.
  92. static FailureReporterInterface* const failure_reporter =
  93. new GoogleTestFailureReporter();
  94. return failure_reporter;
  95. }
  96. // Protects global resources (stdout in particular) used by Log().
  97. static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex);
  98. // Returns true iff a log with the given severity is visible according
  99. // to the --gmock_verbose flag.
  100. bool LogIsVisible(LogSeverity severity) {
  101. if (GMOCK_FLAG(verbose) == kInfoVerbosity) {
  102. // Always show the log if --gmock_verbose=info.
  103. return true;
  104. } else if (GMOCK_FLAG(verbose) == kErrorVerbosity) {
  105. // Always hide it if --gmock_verbose=error.
  106. return false;
  107. } else {
  108. // If --gmock_verbose is neither "info" nor "error", we treat it
  109. // as "warning" (its default value).
  110. return severity == WARNING;
  111. }
  112. }
  113. // Prints the given message to stdout iff 'severity' >= the level
  114. // specified by the --gmock_verbose flag. If stack_frames_to_skip >=
  115. // 0, also prints the stack trace excluding the top
  116. // stack_frames_to_skip frames. In opt mode, any positive
  117. // stack_frames_to_skip is treated as 0, since we don't know which
  118. // function calls will be inlined by the compiler and need to be
  119. // conservative.
  120. void Log(LogSeverity severity, const string& message,
  121. int stack_frames_to_skip) {
  122. if (!LogIsVisible(severity))
  123. return;
  124. // Ensures that logs from different threads don't interleave.
  125. MutexLock l(&g_log_mutex);
  126. // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is a
  127. // macro.
  128. if (severity == WARNING) {
  129. // Prints a GMOCK WARNING marker to make the warnings easily searchable.
  130. std::cout << "\nGMOCK WARNING:";
  131. }
  132. // Pre-pends a new-line to message if it doesn't start with one.
  133. if (message.empty() || message[0] != '\n') {
  134. std::cout << "\n";
  135. }
  136. std::cout << message;
  137. if (stack_frames_to_skip >= 0) {
  138. #ifdef NDEBUG
  139. // In opt mode, we have to be conservative and skip no stack frame.
  140. const int actual_to_skip = 0;
  141. #else
  142. // In dbg mode, we can do what the caller tell us to do (plus one
  143. // for skipping this function's stack frame).
  144. const int actual_to_skip = stack_frames_to_skip + 1;
  145. #endif // NDEBUG
  146. // Appends a new-line to message if it doesn't end with one.
  147. if (!message.empty() && *message.rbegin() != '\n') {
  148. std::cout << "\n";
  149. }
  150. std::cout << "Stack trace:\n"
  151. << ::testing::internal::GetCurrentOsStackTraceExceptTop(
  152. ::testing::UnitTest::GetInstance(), actual_to_skip);
  153. }
  154. std::cout << ::std::flush;
  155. }
  156. } // namespace internal
  157. } // namespace testing