Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
michael@0 | 1 | // Copyright 2007, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | // |
michael@0 | 30 | // Author: wan@google.com (Zhanyong Wan) |
michael@0 | 31 | |
michael@0 | 32 | // Google Mock - a framework for writing C++ mock classes. |
michael@0 | 33 | // |
michael@0 | 34 | // This file defines some utilities useful for implementing Google |
michael@0 | 35 | // Mock. They are subject to change without notice, so please DO NOT |
michael@0 | 36 | // USE THEM IN USER CODE. |
michael@0 | 37 | |
michael@0 | 38 | #include "gmock/internal/gmock-internal-utils.h" |
michael@0 | 39 | |
michael@0 | 40 | #include <ctype.h> |
michael@0 | 41 | #include <ostream> // NOLINT |
michael@0 | 42 | #include <string> |
michael@0 | 43 | #include "gmock/gmock.h" |
michael@0 | 44 | #include "gmock/internal/gmock-port.h" |
michael@0 | 45 | #include "gtest/gtest.h" |
michael@0 | 46 | |
michael@0 | 47 | namespace testing { |
michael@0 | 48 | namespace internal { |
michael@0 | 49 | |
michael@0 | 50 | // Converts an identifier name to a space-separated list of lower-case |
michael@0 | 51 | // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is |
michael@0 | 52 | // treated as one word. For example, both "FooBar123" and |
michael@0 | 53 | // "foo_bar_123" are converted to "foo bar 123". |
michael@0 | 54 | string ConvertIdentifierNameToWords(const char* id_name) { |
michael@0 | 55 | string result; |
michael@0 | 56 | char prev_char = '\0'; |
michael@0 | 57 | for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) { |
michael@0 | 58 | // We don't care about the current locale as the input is |
michael@0 | 59 | // guaranteed to be a valid C++ identifier name. |
michael@0 | 60 | const bool starts_new_word = IsUpper(*p) || |
michael@0 | 61 | (!IsAlpha(prev_char) && IsLower(*p)) || |
michael@0 | 62 | (!IsDigit(prev_char) && IsDigit(*p)); |
michael@0 | 63 | |
michael@0 | 64 | if (IsAlNum(*p)) { |
michael@0 | 65 | if (starts_new_word && result != "") |
michael@0 | 66 | result += ' '; |
michael@0 | 67 | result += ToLower(*p); |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | return result; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | // This class reports Google Mock failures as Google Test failures. A |
michael@0 | 74 | // user can define another class in a similar fashion if he intends to |
michael@0 | 75 | // use Google Mock with a testing framework other than Google Test. |
michael@0 | 76 | class GoogleTestFailureReporter : public FailureReporterInterface { |
michael@0 | 77 | public: |
michael@0 | 78 | virtual void ReportFailure(FailureType type, const char* file, int line, |
michael@0 | 79 | const string& message) { |
michael@0 | 80 | AssertHelper(type == FATAL ? |
michael@0 | 81 | TestPartResult::kFatalFailure : |
michael@0 | 82 | TestPartResult::kNonFatalFailure, |
michael@0 | 83 | file, |
michael@0 | 84 | line, |
michael@0 | 85 | message.c_str()) = Message(); |
michael@0 | 86 | if (type == FATAL) { |
michael@0 | 87 | posix::Abort(); |
michael@0 | 88 | } |
michael@0 | 89 | } |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | // Returns the global failure reporter. Will create a |
michael@0 | 93 | // GoogleTestFailureReporter and return it the first time called. |
michael@0 | 94 | FailureReporterInterface* GetFailureReporter() { |
michael@0 | 95 | // Points to the global failure reporter used by Google Mock. gcc |
michael@0 | 96 | // guarantees that the following use of failure_reporter is |
michael@0 | 97 | // thread-safe. We may need to add additional synchronization to |
michael@0 | 98 | // protect failure_reporter if we port Google Mock to other |
michael@0 | 99 | // compilers. |
michael@0 | 100 | static FailureReporterInterface* const failure_reporter = |
michael@0 | 101 | new GoogleTestFailureReporter(); |
michael@0 | 102 | return failure_reporter; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | // Protects global resources (stdout in particular) used by Log(). |
michael@0 | 106 | static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex); |
michael@0 | 107 | |
michael@0 | 108 | // Returns true iff a log with the given severity is visible according |
michael@0 | 109 | // to the --gmock_verbose flag. |
michael@0 | 110 | bool LogIsVisible(LogSeverity severity) { |
michael@0 | 111 | if (GMOCK_FLAG(verbose) == kInfoVerbosity) { |
michael@0 | 112 | // Always show the log if --gmock_verbose=info. |
michael@0 | 113 | return true; |
michael@0 | 114 | } else if (GMOCK_FLAG(verbose) == kErrorVerbosity) { |
michael@0 | 115 | // Always hide it if --gmock_verbose=error. |
michael@0 | 116 | return false; |
michael@0 | 117 | } else { |
michael@0 | 118 | // If --gmock_verbose is neither "info" nor "error", we treat it |
michael@0 | 119 | // as "warning" (its default value). |
michael@0 | 120 | return severity == WARNING; |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | // Prints the given message to stdout iff 'severity' >= the level |
michael@0 | 125 | // specified by the --gmock_verbose flag. If stack_frames_to_skip >= |
michael@0 | 126 | // 0, also prints the stack trace excluding the top |
michael@0 | 127 | // stack_frames_to_skip frames. In opt mode, any positive |
michael@0 | 128 | // stack_frames_to_skip is treated as 0, since we don't know which |
michael@0 | 129 | // function calls will be inlined by the compiler and need to be |
michael@0 | 130 | // conservative. |
michael@0 | 131 | void Log(LogSeverity severity, const string& message, |
michael@0 | 132 | int stack_frames_to_skip) { |
michael@0 | 133 | if (!LogIsVisible(severity)) |
michael@0 | 134 | return; |
michael@0 | 135 | |
michael@0 | 136 | // Ensures that logs from different threads don't interleave. |
michael@0 | 137 | MutexLock l(&g_log_mutex); |
michael@0 | 138 | |
michael@0 | 139 | // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is a |
michael@0 | 140 | // macro. |
michael@0 | 141 | |
michael@0 | 142 | if (severity == WARNING) { |
michael@0 | 143 | // Prints a GMOCK WARNING marker to make the warnings easily searchable. |
michael@0 | 144 | std::cout << "\nGMOCK WARNING:"; |
michael@0 | 145 | } |
michael@0 | 146 | // Pre-pends a new-line to message if it doesn't start with one. |
michael@0 | 147 | if (message.empty() || message[0] != '\n') { |
michael@0 | 148 | std::cout << "\n"; |
michael@0 | 149 | } |
michael@0 | 150 | std::cout << message; |
michael@0 | 151 | if (stack_frames_to_skip >= 0) { |
michael@0 | 152 | #ifdef NDEBUG |
michael@0 | 153 | // In opt mode, we have to be conservative and skip no stack frame. |
michael@0 | 154 | const int actual_to_skip = 0; |
michael@0 | 155 | #else |
michael@0 | 156 | // In dbg mode, we can do what the caller tell us to do (plus one |
michael@0 | 157 | // for skipping this function's stack frame). |
michael@0 | 158 | const int actual_to_skip = stack_frames_to_skip + 1; |
michael@0 | 159 | #endif // NDEBUG |
michael@0 | 160 | |
michael@0 | 161 | // Appends a new-line to message if it doesn't end with one. |
michael@0 | 162 | if (!message.empty() && *message.rbegin() != '\n') { |
michael@0 | 163 | std::cout << "\n"; |
michael@0 | 164 | } |
michael@0 | 165 | std::cout << "Stack trace:\n" |
michael@0 | 166 | << ::testing::internal::GetCurrentOsStackTraceExceptTop( |
michael@0 | 167 | ::testing::UnitTest::GetInstance(), actual_to_skip); |
michael@0 | 168 | } |
michael@0 | 169 | std::cout << ::std::flush; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | } // namespace internal |
michael@0 | 173 | } // namespace testing |