michael@0: // Copyright 2007, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Author: wan@google.com (Zhanyong Wan) michael@0: michael@0: // Google Mock - a framework for writing C++ mock classes. michael@0: // michael@0: // This file defines some utilities useful for implementing Google michael@0: // Mock. They are subject to change without notice, so please DO NOT michael@0: // USE THEM IN USER CODE. michael@0: michael@0: #include "gmock/internal/gmock-internal-utils.h" michael@0: michael@0: #include michael@0: #include // NOLINT michael@0: #include michael@0: #include "gmock/gmock.h" michael@0: #include "gmock/internal/gmock-port.h" michael@0: #include "gtest/gtest.h" michael@0: michael@0: namespace testing { michael@0: namespace internal { michael@0: michael@0: // Converts an identifier name to a space-separated list of lower-case michael@0: // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is michael@0: // treated as one word. For example, both "FooBar123" and michael@0: // "foo_bar_123" are converted to "foo bar 123". michael@0: string ConvertIdentifierNameToWords(const char* id_name) { michael@0: string result; michael@0: char prev_char = '\0'; michael@0: for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) { michael@0: // We don't care about the current locale as the input is michael@0: // guaranteed to be a valid C++ identifier name. michael@0: const bool starts_new_word = IsUpper(*p) || michael@0: (!IsAlpha(prev_char) && IsLower(*p)) || michael@0: (!IsDigit(prev_char) && IsDigit(*p)); michael@0: michael@0: if (IsAlNum(*p)) { michael@0: if (starts_new_word && result != "") michael@0: result += ' '; michael@0: result += ToLower(*p); michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: // This class reports Google Mock failures as Google Test failures. A michael@0: // user can define another class in a similar fashion if he intends to michael@0: // use Google Mock with a testing framework other than Google Test. michael@0: class GoogleTestFailureReporter : public FailureReporterInterface { michael@0: public: michael@0: virtual void ReportFailure(FailureType type, const char* file, int line, michael@0: const string& message) { michael@0: AssertHelper(type == FATAL ? michael@0: TestPartResult::kFatalFailure : michael@0: TestPartResult::kNonFatalFailure, michael@0: file, michael@0: line, michael@0: message.c_str()) = Message(); michael@0: if (type == FATAL) { michael@0: posix::Abort(); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: // Returns the global failure reporter. Will create a michael@0: // GoogleTestFailureReporter and return it the first time called. michael@0: FailureReporterInterface* GetFailureReporter() { michael@0: // Points to the global failure reporter used by Google Mock. gcc michael@0: // guarantees that the following use of failure_reporter is michael@0: // thread-safe. We may need to add additional synchronization to michael@0: // protect failure_reporter if we port Google Mock to other michael@0: // compilers. michael@0: static FailureReporterInterface* const failure_reporter = michael@0: new GoogleTestFailureReporter(); michael@0: return failure_reporter; michael@0: } michael@0: michael@0: // Protects global resources (stdout in particular) used by Log(). michael@0: static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex); michael@0: michael@0: // Returns true iff a log with the given severity is visible according michael@0: // to the --gmock_verbose flag. michael@0: bool LogIsVisible(LogSeverity severity) { michael@0: if (GMOCK_FLAG(verbose) == kInfoVerbosity) { michael@0: // Always show the log if --gmock_verbose=info. michael@0: return true; michael@0: } else if (GMOCK_FLAG(verbose) == kErrorVerbosity) { michael@0: // Always hide it if --gmock_verbose=error. michael@0: return false; michael@0: } else { michael@0: // If --gmock_verbose is neither "info" nor "error", we treat it michael@0: // as "warning" (its default value). michael@0: return severity == WARNING; michael@0: } michael@0: } michael@0: michael@0: // Prints the given message to stdout iff 'severity' >= the level michael@0: // specified by the --gmock_verbose flag. If stack_frames_to_skip >= michael@0: // 0, also prints the stack trace excluding the top michael@0: // stack_frames_to_skip frames. In opt mode, any positive michael@0: // stack_frames_to_skip is treated as 0, since we don't know which michael@0: // function calls will be inlined by the compiler and need to be michael@0: // conservative. michael@0: void Log(LogSeverity severity, const string& message, michael@0: int stack_frames_to_skip) { michael@0: if (!LogIsVisible(severity)) michael@0: return; michael@0: michael@0: // Ensures that logs from different threads don't interleave. michael@0: MutexLock l(&g_log_mutex); michael@0: michael@0: // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is a michael@0: // macro. michael@0: michael@0: if (severity == WARNING) { michael@0: // Prints a GMOCK WARNING marker to make the warnings easily searchable. michael@0: std::cout << "\nGMOCK WARNING:"; michael@0: } michael@0: // Pre-pends a new-line to message if it doesn't start with one. michael@0: if (message.empty() || message[0] != '\n') { michael@0: std::cout << "\n"; michael@0: } michael@0: std::cout << message; michael@0: if (stack_frames_to_skip >= 0) { michael@0: #ifdef NDEBUG michael@0: // In opt mode, we have to be conservative and skip no stack frame. michael@0: const int actual_to_skip = 0; michael@0: #else michael@0: // In dbg mode, we can do what the caller tell us to do (plus one michael@0: // for skipping this function's stack frame). michael@0: const int actual_to_skip = stack_frames_to_skip + 1; michael@0: #endif // NDEBUG michael@0: michael@0: // Appends a new-line to message if it doesn't end with one. michael@0: if (!message.empty() && *message.rbegin() != '\n') { michael@0: std::cout << "\n"; michael@0: } michael@0: std::cout << "Stack trace:\n" michael@0: << ::testing::internal::GetCurrentOsStackTraceExceptTop( michael@0: ::testing::UnitTest::GetInstance(), actual_to_skip); michael@0: } michael@0: std::cout << ::std::flush; michael@0: } michael@0: michael@0: } // namespace internal michael@0: } // namespace testing