1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/gtest/gmock/src/gmock-internal-utils.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,173 @@ 1.4 +// Copyright 2007, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 +// 1.33 +// Author: wan@google.com (Zhanyong Wan) 1.34 + 1.35 +// Google Mock - a framework for writing C++ mock classes. 1.36 +// 1.37 +// This file defines some utilities useful for implementing Google 1.38 +// Mock. They are subject to change without notice, so please DO NOT 1.39 +// USE THEM IN USER CODE. 1.40 + 1.41 +#include "gmock/internal/gmock-internal-utils.h" 1.42 + 1.43 +#include <ctype.h> 1.44 +#include <ostream> // NOLINT 1.45 +#include <string> 1.46 +#include "gmock/gmock.h" 1.47 +#include "gmock/internal/gmock-port.h" 1.48 +#include "gtest/gtest.h" 1.49 + 1.50 +namespace testing { 1.51 +namespace internal { 1.52 + 1.53 +// Converts an identifier name to a space-separated list of lower-case 1.54 +// words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is 1.55 +// treated as one word. For example, both "FooBar123" and 1.56 +// "foo_bar_123" are converted to "foo bar 123". 1.57 +string ConvertIdentifierNameToWords(const char* id_name) { 1.58 + string result; 1.59 + char prev_char = '\0'; 1.60 + for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) { 1.61 + // We don't care about the current locale as the input is 1.62 + // guaranteed to be a valid C++ identifier name. 1.63 + const bool starts_new_word = IsUpper(*p) || 1.64 + (!IsAlpha(prev_char) && IsLower(*p)) || 1.65 + (!IsDigit(prev_char) && IsDigit(*p)); 1.66 + 1.67 + if (IsAlNum(*p)) { 1.68 + if (starts_new_word && result != "") 1.69 + result += ' '; 1.70 + result += ToLower(*p); 1.71 + } 1.72 + } 1.73 + return result; 1.74 +} 1.75 + 1.76 +// This class reports Google Mock failures as Google Test failures. A 1.77 +// user can define another class in a similar fashion if he intends to 1.78 +// use Google Mock with a testing framework other than Google Test. 1.79 +class GoogleTestFailureReporter : public FailureReporterInterface { 1.80 + public: 1.81 + virtual void ReportFailure(FailureType type, const char* file, int line, 1.82 + const string& message) { 1.83 + AssertHelper(type == FATAL ? 1.84 + TestPartResult::kFatalFailure : 1.85 + TestPartResult::kNonFatalFailure, 1.86 + file, 1.87 + line, 1.88 + message.c_str()) = Message(); 1.89 + if (type == FATAL) { 1.90 + posix::Abort(); 1.91 + } 1.92 + } 1.93 +}; 1.94 + 1.95 +// Returns the global failure reporter. Will create a 1.96 +// GoogleTestFailureReporter and return it the first time called. 1.97 +FailureReporterInterface* GetFailureReporter() { 1.98 + // Points to the global failure reporter used by Google Mock. gcc 1.99 + // guarantees that the following use of failure_reporter is 1.100 + // thread-safe. We may need to add additional synchronization to 1.101 + // protect failure_reporter if we port Google Mock to other 1.102 + // compilers. 1.103 + static FailureReporterInterface* const failure_reporter = 1.104 + new GoogleTestFailureReporter(); 1.105 + return failure_reporter; 1.106 +} 1.107 + 1.108 +// Protects global resources (stdout in particular) used by Log(). 1.109 +static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex); 1.110 + 1.111 +// Returns true iff a log with the given severity is visible according 1.112 +// to the --gmock_verbose flag. 1.113 +bool LogIsVisible(LogSeverity severity) { 1.114 + if (GMOCK_FLAG(verbose) == kInfoVerbosity) { 1.115 + // Always show the log if --gmock_verbose=info. 1.116 + return true; 1.117 + } else if (GMOCK_FLAG(verbose) == kErrorVerbosity) { 1.118 + // Always hide it if --gmock_verbose=error. 1.119 + return false; 1.120 + } else { 1.121 + // If --gmock_verbose is neither "info" nor "error", we treat it 1.122 + // as "warning" (its default value). 1.123 + return severity == WARNING; 1.124 + } 1.125 +} 1.126 + 1.127 +// Prints the given message to stdout iff 'severity' >= the level 1.128 +// specified by the --gmock_verbose flag. If stack_frames_to_skip >= 1.129 +// 0, also prints the stack trace excluding the top 1.130 +// stack_frames_to_skip frames. In opt mode, any positive 1.131 +// stack_frames_to_skip is treated as 0, since we don't know which 1.132 +// function calls will be inlined by the compiler and need to be 1.133 +// conservative. 1.134 +void Log(LogSeverity severity, const string& message, 1.135 + int stack_frames_to_skip) { 1.136 + if (!LogIsVisible(severity)) 1.137 + return; 1.138 + 1.139 + // Ensures that logs from different threads don't interleave. 1.140 + MutexLock l(&g_log_mutex); 1.141 + 1.142 + // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is a 1.143 + // macro. 1.144 + 1.145 + if (severity == WARNING) { 1.146 + // Prints a GMOCK WARNING marker to make the warnings easily searchable. 1.147 + std::cout << "\nGMOCK WARNING:"; 1.148 + } 1.149 + // Pre-pends a new-line to message if it doesn't start with one. 1.150 + if (message.empty() || message[0] != '\n') { 1.151 + std::cout << "\n"; 1.152 + } 1.153 + std::cout << message; 1.154 + if (stack_frames_to_skip >= 0) { 1.155 +#ifdef NDEBUG 1.156 + // In opt mode, we have to be conservative and skip no stack frame. 1.157 + const int actual_to_skip = 0; 1.158 +#else 1.159 + // In dbg mode, we can do what the caller tell us to do (plus one 1.160 + // for skipping this function's stack frame). 1.161 + const int actual_to_skip = stack_frames_to_skip + 1; 1.162 +#endif // NDEBUG 1.163 + 1.164 + // Appends a new-line to message if it doesn't end with one. 1.165 + if (!message.empty() && *message.rbegin() != '\n') { 1.166 + std::cout << "\n"; 1.167 + } 1.168 + std::cout << "Stack trace:\n" 1.169 + << ::testing::internal::GetCurrentOsStackTraceExceptTop( 1.170 + ::testing::UnitTest::GetInstance(), actual_to_skip); 1.171 + } 1.172 + std::cout << ::std::flush; 1.173 +} 1.174 + 1.175 +} // namespace internal 1.176 +} // namespace testing