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