![]() |
|
|||
File indexing completed on 2025-08-06 08:19:54
0001 // Copyright 2007, Google Inc. 0002 // All rights reserved. 0003 // 0004 // Redistribution and use in source and binary forms, with or without 0005 // modification, are permitted provided that the following conditions are 0006 // met: 0007 // 0008 // * Redistributions of source code must retain the above copyright 0009 // notice, this list of conditions and the following disclaimer. 0010 // * Redistributions in binary form must reproduce the above 0011 // copyright notice, this list of conditions and the following disclaimer 0012 // in the documentation and/or other materials provided with the 0013 // distribution. 0014 // * Neither the name of Google Inc. nor the names of its 0015 // contributors may be used to endorse or promote products derived from 0016 // this software without specific prior written permission. 0017 // 0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0029 // 0030 // Author: wan@google.com (Zhanyong Wan) 0031 0032 // Google Mock - a framework for writing C++ mock classes. 0033 // 0034 // This file defines some utilities useful for implementing Google 0035 // Mock. They are subject to change without notice, so please DO NOT 0036 // USE THEM IN USER CODE. 0037 0038 #include "gmock/internal/gmock-internal-utils.h" 0039 0040 #include <ctype.h> 0041 #include <ostream> // NOLINT 0042 #include <string> 0043 #include "gmock/gmock.h" 0044 #include "gmock/internal/gmock-port.h" 0045 #include "gtest/gtest.h" 0046 0047 namespace testing { 0048 namespace internal { 0049 0050 // Converts an identifier name to a space-separated list of lower-case 0051 // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is 0052 // treated as one word. For example, both "FooBar123" and 0053 // "foo_bar_123" are converted to "foo bar 123". 0054 GTEST_API_ string ConvertIdentifierNameToWords(const char* id_name) { 0055 string result; 0056 char prev_char = '\0'; 0057 for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) { 0058 // We don't care about the current locale as the input is 0059 // guaranteed to be a valid C++ identifier name. 0060 const bool starts_new_word = IsUpper(*p) || 0061 (!IsAlpha(prev_char) && IsLower(*p)) || 0062 (!IsDigit(prev_char) && IsDigit(*p)); 0063 0064 if (IsAlNum(*p)) { 0065 if (starts_new_word && result != "") 0066 result += ' '; 0067 result += ToLower(*p); 0068 } 0069 } 0070 return result; 0071 } 0072 0073 // This class reports Google Mock failures as Google Test failures. A 0074 // user can define another class in a similar fashion if he intends to 0075 // use Google Mock with a testing framework other than Google Test. 0076 class GoogleTestFailureReporter : public FailureReporterInterface { 0077 public: 0078 virtual void ReportFailure(FailureType type, const char* file, int line, 0079 const string& message) { 0080 AssertHelper(type == kFatal ? 0081 TestPartResult::kFatalFailure : 0082 TestPartResult::kNonFatalFailure, 0083 file, 0084 line, 0085 message.c_str()) = Message(); 0086 if (type == kFatal) { 0087 posix::Abort(); 0088 } 0089 } 0090 }; 0091 0092 // Returns the global failure reporter. Will create a 0093 // GoogleTestFailureReporter and return it the first time called. 0094 GTEST_API_ FailureReporterInterface* GetFailureReporter() { 0095 // Points to the global failure reporter used by Google Mock. gcc 0096 // guarantees that the following use of failure_reporter is 0097 // thread-safe. We may need to add additional synchronization to 0098 // protect failure_reporter if we port Google Mock to other 0099 // compilers. 0100 static FailureReporterInterface* const failure_reporter = 0101 new GoogleTestFailureReporter(); 0102 return failure_reporter; 0103 } 0104 0105 // Protects global resources (stdout in particular) used by Log(). 0106 static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex); 0107 0108 // Returns true iff a log with the given severity is visible according 0109 // to the --gmock_verbose flag. 0110 GTEST_API_ bool LogIsVisible(LogSeverity severity) { 0111 if (GMOCK_FLAG(verbose) == kInfoVerbosity) { 0112 // Always show the log if --gmock_verbose=info. 0113 return true; 0114 } else if (GMOCK_FLAG(verbose) == kErrorVerbosity) { 0115 // Always hide it if --gmock_verbose=error. 0116 return false; 0117 } else { 0118 // If --gmock_verbose is neither "info" nor "error", we treat it 0119 // as "warning" (its default value). 0120 return severity == kWarning; 0121 } 0122 } 0123 0124 // Prints the given message to stdout iff 'severity' >= the level 0125 // specified by the --gmock_verbose flag. If stack_frames_to_skip >= 0126 // 0, also prints the stack trace excluding the top 0127 // stack_frames_to_skip frames. In opt mode, any positive 0128 // stack_frames_to_skip is treated as 0, since we don't know which 0129 // function calls will be inlined by the compiler and need to be 0130 // conservative. 0131 GTEST_API_ void Log(LogSeverity severity, 0132 const string& message, 0133 int stack_frames_to_skip) { 0134 if (!LogIsVisible(severity)) 0135 return; 0136 0137 // Ensures that logs from different threads don't interleave. 0138 MutexLock l(&g_log_mutex); 0139 0140 // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is a 0141 // macro. 0142 0143 if (severity == kWarning) { 0144 // Prints a GMOCK WARNING marker to make the warnings easily searchable. 0145 std::cout << "\nGMOCK WARNING:"; 0146 } 0147 // Pre-pends a new-line to message if it doesn't start with one. 0148 if (message.empty() || message[0] != '\n') { 0149 std::cout << "\n"; 0150 } 0151 std::cout << message; 0152 if (stack_frames_to_skip >= 0) { 0153 #ifdef NDEBUG 0154 // In opt mode, we have to be conservative and skip no stack frame. 0155 const int actual_to_skip = 0; 0156 #else 0157 // In dbg mode, we can do what the caller tell us to do (plus one 0158 // for skipping this function's stack frame). 0159 const int actual_to_skip = stack_frames_to_skip + 1; 0160 #endif // NDEBUG 0161 0162 // Appends a new-line to message if it doesn't end with one. 0163 if (!message.empty() && *message.rbegin() != '\n') { 0164 std::cout << "\n"; 0165 } 0166 std::cout << "Stack trace:\n" 0167 << ::testing::internal::GetCurrentOsStackTraceExceptTop( 0168 ::testing::UnitTest::GetInstance(), actual_to_skip); 0169 } 0170 std::cout << ::std::flush; 0171 } 0172 0173 } // namespace internal 0174 } // namespace testing
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |