00001 // Copyright 2007, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // Author: wan@google.com (Zhanyong Wan) 00031 00032 // Google Mock - a framework for writing C++ mock classes. 00033 // 00034 // This file defines some utilities useful for implementing Google 00035 // Mock. They are subject to change without notice, so please DO NOT 00036 // USE THEM IN USER CODE. 00037 00038 #include "gmock/internal/gmock-internal-utils.h" 00039 00040 #include <ctype.h> 00041 #include <ostream> // NOLINT 00042 #include <string> 00043 #include "gmock/gmock.h" 00044 #include "gmock/internal/gmock-port.h" 00045 #include "gtest/gtest.h" 00046 00047 namespace testing { 00048 namespace internal { 00049 00050 // Converts an identifier name to a space-separated list of lower-case 00051 // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is 00052 // treated as one word. For example, both "FooBar123" and 00053 // "foo_bar_123" are converted to "foo bar 123". 00054 GTEST_API_ string ConvertIdentifierNameToWords(const char* id_name) { 00055 string result; 00056 char prev_char = '\0'; 00057 for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) { 00058 // We don't care about the current locale as the input is 00059 // guaranteed to be a valid C++ identifier name. 00060 const bool starts_new_word = IsUpper(*p) || 00061 (!IsAlpha(prev_char) && IsLower(*p)) || 00062 (!IsDigit(prev_char) && IsDigit(*p)); 00063 00064 if (IsAlNum(*p)) { 00065 if (starts_new_word && result != "") 00066 result += ' '; 00067 result += ToLower(*p); 00068 } 00069 } 00070 return result; 00071 } 00072 00073 // This class reports Google Mock failures as Google Test failures. A 00074 // user can define another class in a similar fashion if he intends to 00075 // use Google Mock with a testing framework other than Google Test. 00076 class GoogleTestFailureReporter : public FailureReporterInterface { 00077 public: 00078 virtual void ReportFailure(FailureType type, const char* file, int line, 00079 const string& message) { 00080 AssertHelper(type == kFatal ? 00081 TestPartResult::kFatalFailure : 00082 TestPartResult::kNonFatalFailure, 00083 file, 00084 line, 00085 message.c_str()) = Message(); 00086 if (type == kFatal) { 00087 posix::Abort(); 00088 } 00089 } 00090 }; 00091 00092 // Returns the global failure reporter. Will create a 00093 // GoogleTestFailureReporter and return it the first time called. 00094 GTEST_API_ FailureReporterInterface* GetFailureReporter() { 00095 // Points to the global failure reporter used by Google Mock. gcc 00096 // guarantees that the following use of failure_reporter is 00097 // thread-safe. We may need to add additional synchronization to 00098 // protect failure_reporter if we port Google Mock to other 00099 // compilers. 00100 static FailureReporterInterface* const failure_reporter = 00101 new GoogleTestFailureReporter(); 00102 return failure_reporter; 00103 } 00104 00105 // Protects global resources (stdout in particular) used by Log(). 00106 static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex); 00107 00108 // Returns true iff a log with the given severity is visible according 00109 // to the --gmock_verbose flag. 00110 GTEST_API_ bool LogIsVisible(LogSeverity severity) { 00111 if (GMOCK_FLAG(verbose) == kInfoVerbosity) { 00112 // Always show the log if --gmock_verbose=info. 00113 return true; 00114 } else if (GMOCK_FLAG(verbose) == kErrorVerbosity) { 00115 // Always hide it if --gmock_verbose=error. 00116 return false; 00117 } else { 00118 // If --gmock_verbose is neither "info" nor "error", we treat it 00119 // as "warning" (its default value). 00120 return severity == kWarning; 00121 } 00122 } 00123 00124 // Prints the given message to stdout iff 'severity' >= the level 00125 // specified by the --gmock_verbose flag. If stack_frames_to_skip >= 00126 // 0, also prints the stack trace excluding the top 00127 // stack_frames_to_skip frames. In opt mode, any positive 00128 // stack_frames_to_skip is treated as 0, since we don't know which 00129 // function calls will be inlined by the compiler and need to be 00130 // conservative. 00131 GTEST_API_ void Log(LogSeverity severity, 00132 const string& message, 00133 int stack_frames_to_skip) { 00134 if (!LogIsVisible(severity)) 00135 return; 00136 00137 // Ensures that logs from different threads don't interleave. 00138 MutexLock l(&g_log_mutex); 00139 00140 // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is a 00141 // macro. 00142 00143 if (severity == kWarning) { 00144 // Prints a GMOCK WARNING marker to make the warnings easily searchable. 00145 std::cout << "\nGMOCK WARNING:"; 00146 } 00147 // Pre-pends a new-line to message if it doesn't start with one. 00148 if (message.empty() || message[0] != '\n') { 00149 std::cout << "\n"; 00150 } 00151 std::cout << message; 00152 if (stack_frames_to_skip >= 0) { 00153 #ifdef NDEBUG 00154 // In opt mode, we have to be conservative and skip no stack frame. 00155 const int actual_to_skip = 0; 00156 #else 00157 // In dbg mode, we can do what the caller tell us to do (plus one 00158 // for skipping this function's stack frame). 00159 const int actual_to_skip = stack_frames_to_skip + 1; 00160 #endif // NDEBUG 00161 00162 // Appends a new-line to message if it doesn't end with one. 00163 if (!message.empty() && *message.rbegin() != '\n') { 00164 std::cout << "\n"; 00165 } 00166 std::cout << "Stack trace:\n" 00167 << ::testing::internal::GetCurrentOsStackTraceExceptTop( 00168 ::testing::UnitTest::GetInstance(), actual_to_skip); 00169 } 00170 std::cout << ::std::flush; 00171 } 00172 00173 } // namespace internal 00174 } // namespace testing