gmock-internal-utils.cc (googletest-release-1.11.0) | : | gmock-internal-utils.cc (googletest-release-1.12.0) | ||
---|---|---|---|---|
skipping to change at line 39 | skipping to change at line 39 | |||
// Google Mock - a framework for writing C++ mock classes. | // Google Mock - a framework for writing C++ mock classes. | |||
// | // | |||
// This file defines some utilities useful for implementing Google | // This file defines some utilities useful for implementing Google | |||
// Mock. They are subject to change without notice, so please DO NOT | // Mock. They are subject to change without notice, so please DO NOT | |||
// USE THEM IN USER CODE. | // USE THEM IN USER CODE. | |||
#include "gmock/internal/gmock-internal-utils.h" | #include "gmock/internal/gmock-internal-utils.h" | |||
#include <ctype.h> | #include <ctype.h> | |||
#include <array> | ||||
#include <cctype> | ||||
#include <cstdint> | ||||
#include <cstring> | ||||
#include <ostream> // NOLINT | #include <ostream> // NOLINT | |||
#include <string> | #include <string> | |||
#include <vector> | ||||
#include "gmock/gmock.h" | #include "gmock/gmock.h" | |||
#include "gmock/internal/gmock-port.h" | #include "gmock/internal/gmock-port.h" | |||
#include "gtest/gtest.h" | #include "gtest/gtest.h" | |||
namespace testing { | namespace testing { | |||
namespace internal { | namespace internal { | |||
// Joins a vector of strings as if they are fields of a tuple; returns | // Joins a vector of strings as if they are fields of a tuple; returns | |||
// the joined string. | // the joined string. | |||
GTEST_API_ std::string JoinAsTuple(const Strings& fields) { | GTEST_API_ std::string JoinAsKeyValueTuple( | |||
switch (fields.size()) { | const std::vector<const char*>& names, const Strings& values) { | |||
case 0: | GTEST_CHECK_(names.size() == values.size()); | |||
return ""; | if (values.empty()) { | |||
case 1: | return ""; | |||
return fields[0]; | } | |||
default: | const auto build_one = [&](const size_t i) { | |||
std::string result = "(" + fields[0]; | return std::string(names[i]) + ": " + values[i]; | |||
for (size_t i = 1; i < fields.size(); i++) { | }; | |||
result += ", "; | std::string result = "(" + build_one(0); | |||
result += fields[i]; | for (size_t i = 1; i < values.size(); i++) { | |||
} | result += ", "; | |||
result += ")"; | result += build_one(i); | |||
return result; | ||||
} | } | |||
result += ")"; | ||||
return result; | ||||
} | } | |||
// Converts an identifier name to a space-separated list of lower-case | // Converts an identifier name to a space-separated list of lower-case | |||
// words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is | // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is | |||
// treated as one word. For example, both "FooBar123" and | // treated as one word. For example, both "FooBar123" and | |||
// "foo_bar_123" are converted to "foo bar 123". | // "foo_bar_123" are converted to "foo bar 123". | |||
GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name) { | GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name) { | |||
std::string result; | std::string result; | |||
char prev_char = '\0'; | char prev_char = '\0'; | |||
for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) { | for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) { | |||
// We don't care about the current locale as the input is | // We don't care about the current locale as the input is | |||
// guaranteed to be a valid C++ identifier name. | // guaranteed to be a valid C++ identifier name. | |||
const bool starts_new_word = IsUpper(*p) || | const bool starts_new_word = IsUpper(*p) || | |||
(!IsAlpha(prev_char) && IsLower(*p)) || | (!IsAlpha(prev_char) && IsLower(*p)) || | |||
(!IsDigit(prev_char) && IsDigit(*p)); | (!IsDigit(prev_char) && IsDigit(*p)); | |||
if (IsAlNum(*p)) { | if (IsAlNum(*p)) { | |||
if (starts_new_word && result != "") | if (starts_new_word && result != "") result += ' '; | |||
result += ' '; | ||||
result += ToLower(*p); | result += ToLower(*p); | |||
} | } | |||
} | } | |||
return result; | return result; | |||
} | } | |||
// This class reports Google Mock failures as Google Test failures. A | // This class reports Google Mock failures as Google Test failures. A | |||
// user can define another class in a similar fashion if they intend to | // user can define another class in a similar fashion if they intend to | |||
// use Google Mock with a testing framework other than Google Test. | // use Google Mock with a testing framework other than Google Test. | |||
class GoogleTestFailureReporter : public FailureReporterInterface { | class GoogleTestFailureReporter : public FailureReporterInterface { | |||
public: | public: | |||
void ReportFailure(FailureType type, const char* file, int line, | void ReportFailure(FailureType type, const char* file, int line, | |||
const std::string& message) override { | const std::string& message) override { | |||
AssertHelper(type == kFatal ? | AssertHelper(type == kFatal ? TestPartResult::kFatalFailure | |||
TestPartResult::kFatalFailure : | : TestPartResult::kNonFatalFailure, | |||
TestPartResult::kNonFatalFailure, | file, line, message.c_str()) = Message(); | |||
file, | ||||
line, | ||||
message.c_str()) = Message(); | ||||
if (type == kFatal) { | if (type == kFatal) { | |||
posix::Abort(); | posix::Abort(); | |||
} | } | |||
} | } | |||
}; | }; | |||
// Returns the global failure reporter. Will create a | // Returns the global failure reporter. Will create a | |||
// GoogleTestFailureReporter and return it the first time called. | // GoogleTestFailureReporter and return it the first time called. | |||
GTEST_API_ FailureReporterInterface* GetFailureReporter() { | GTEST_API_ FailureReporterInterface* GetFailureReporter() { | |||
// Points to the global failure reporter used by Google Mock. gcc | // Points to the global failure reporter used by Google Mock. gcc | |||
skipping to change at line 128 | skipping to change at line 132 | |||
new GoogleTestFailureReporter(); | new GoogleTestFailureReporter(); | |||
return failure_reporter; | return failure_reporter; | |||
} | } | |||
// Protects global resources (stdout in particular) used by Log(). | // Protects global resources (stdout in particular) used by Log(). | |||
static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex); | static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex); | |||
// Returns true if and only if a log with the given severity is visible | // Returns true if and only if a log with the given severity is visible | |||
// according to the --gmock_verbose flag. | // according to the --gmock_verbose flag. | |||
GTEST_API_ bool LogIsVisible(LogSeverity severity) { | GTEST_API_ bool LogIsVisible(LogSeverity severity) { | |||
if (GMOCK_FLAG(verbose) == kInfoVerbosity) { | if (GMOCK_FLAG_GET(verbose) == kInfoVerbosity) { | |||
// Always show the log if --gmock_verbose=info. | // Always show the log if --gmock_verbose=info. | |||
return true; | return true; | |||
} else if (GMOCK_FLAG(verbose) == kErrorVerbosity) { | } else if (GMOCK_FLAG_GET(verbose) == kErrorVerbosity) { | |||
// Always hide it if --gmock_verbose=error. | // Always hide it if --gmock_verbose=error. | |||
return false; | return false; | |||
} else { | } else { | |||
// If --gmock_verbose is neither "info" nor "error", we treat it | // If --gmock_verbose is neither "info" nor "error", we treat it | |||
// as "warning" (its default value). | // as "warning" (its default value). | |||
return severity == kWarning; | return severity == kWarning; | |||
} | } | |||
} | } | |||
// Prints the given message to stdout if and only if 'severity' >= the level | // Prints the given message to stdout if and only if 'severity' >= the level | |||
// specified by the --gmock_verbose flag. If stack_frames_to_skip >= | // specified by the --gmock_verbose flag. If stack_frames_to_skip >= | |||
// 0, also prints the stack trace excluding the top | // 0, also prints the stack trace excluding the top | |||
// stack_frames_to_skip frames. In opt mode, any positive | // stack_frames_to_skip frames. In opt mode, any positive | |||
// stack_frames_to_skip is treated as 0, since we don't know which | // stack_frames_to_skip is treated as 0, since we don't know which | |||
// function calls will be inlined by the compiler and need to be | // function calls will be inlined by the compiler and need to be | |||
// conservative. | // conservative. | |||
GTEST_API_ void Log(LogSeverity severity, const std::string& message, | GTEST_API_ void Log(LogSeverity severity, const std::string& message, | |||
int stack_frames_to_skip) { | int stack_frames_to_skip) { | |||
if (!LogIsVisible(severity)) | if (!LogIsVisible(severity)) return; | |||
return; | ||||
// Ensures that logs from different threads don't interleave. | // Ensures that logs from different threads don't interleave. | |||
MutexLock l(&g_log_mutex); | MutexLock l(&g_log_mutex); | |||
if (severity == kWarning) { | if (severity == kWarning) { | |||
// Prints a GMOCK WARNING marker to make the warnings easily searchable. | // Prints a GMOCK WARNING marker to make the warnings easily searchable. | |||
std::cout << "\nGMOCK WARNING:"; | std::cout << "\nGMOCK WARNING:"; | |||
} | } | |||
// Pre-pends a new-line to message if it doesn't start with one. | // Pre-pends a new-line to message if it doesn't start with one. | |||
if (message.empty() || message[0] != '\n') { | if (message.empty() || message[0] != '\n') { | |||
skipping to change at line 180 | skipping to change at line 183 | |||
// In dbg mode, we can do what the caller tell us to do (plus one | // In dbg mode, we can do what the caller tell us to do (plus one | |||
// for skipping this function's stack frame). | // for skipping this function's stack frame). | |||
const int actual_to_skip = stack_frames_to_skip + 1; | const int actual_to_skip = stack_frames_to_skip + 1; | |||
#endif // NDEBUG | #endif // NDEBUG | |||
// Appends a new-line to message if it doesn't end with one. | // Appends a new-line to message if it doesn't end with one. | |||
if (!message.empty() && *message.rbegin() != '\n') { | if (!message.empty() && *message.rbegin() != '\n') { | |||
std::cout << "\n"; | std::cout << "\n"; | |||
} | } | |||
std::cout << "Stack trace:\n" | std::cout << "Stack trace:\n" | |||
<< ::testing::internal::GetCurrentOsStackTraceExceptTop( | << ::testing::internal::GetCurrentOsStackTraceExceptTop( | |||
::testing::UnitTest::GetInstance(), actual_to_skip); | ::testing::UnitTest::GetInstance(), actual_to_skip); | |||
} | } | |||
std::cout << ::std::flush; | std::cout << ::std::flush; | |||
} | } | |||
GTEST_API_ WithoutMatchers GetWithoutMatchers() { return WithoutMatchers(); } | GTEST_API_ WithoutMatchers GetWithoutMatchers() { return WithoutMatchers(); } | |||
GTEST_API_ void IllegalDoDefault(const char* file, int line) { | GTEST_API_ void IllegalDoDefault(const char* file, int line) { | |||
internal::Assert( | internal::Assert( | |||
false, file, line, | false, file, line, | |||
"You are using DoDefault() inside a composite action like " | "You are using DoDefault() inside a composite action like " | |||
"DoAll() or WithArgs(). This is not supported for technical " | "DoAll() or WithArgs(). This is not supported for technical " | |||
"reasons. Please instead spell out the default action, or " | "reasons. Please instead spell out the default action, or " | |||
"assign the default action to an Action variable and use " | "assign the default action to an Action variable and use " | |||
"the variable in various places."); | "the variable in various places."); | |||
} | } | |||
constexpr char UnBase64Impl(char c, const char* const base64, char carry) { | ||||
return *base64 == 0 ? static_cast<char>(65) | ||||
: *base64 == c ? carry | ||||
: UnBase64Impl(c, base64 + 1, carry + 1); | ||||
} | ||||
template <size_t... I> | ||||
constexpr std::array<char, 256> UnBase64Impl(IndexSequence<I...>, | ||||
const char* const base64) { | ||||
return {{UnBase64Impl(static_cast<char>(I), base64, 0)...}}; | ||||
} | ||||
constexpr std::array<char, 256> UnBase64(const char* const base64) { | ||||
return UnBase64Impl(MakeIndexSequence<256>{}, base64); | ||||
} | ||||
static constexpr char kBase64[] = | ||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||||
static constexpr std::array<char, 256> kUnBase64 = UnBase64(kBase64); | ||||
bool Base64Unescape(const std::string& encoded, std::string* decoded) { | ||||
decoded->clear(); | ||||
size_t encoded_len = encoded.size(); | ||||
decoded->reserve(3 * (encoded_len / 4) + (encoded_len % 4)); | ||||
int bit_pos = 0; | ||||
char dst = 0; | ||||
for (int src : encoded) { | ||||
if (std::isspace(src) || src == '=') { | ||||
continue; | ||||
} | ||||
char src_bin = kUnBase64[static_cast<size_t>(src)]; | ||||
if (src_bin >= 64) { | ||||
decoded->clear(); | ||||
return false; | ||||
} | ||||
if (bit_pos == 0) { | ||||
dst |= static_cast<char>(src_bin << 2); | ||||
bit_pos = 6; | ||||
} else { | ||||
dst |= static_cast<char>(src_bin >> (bit_pos - 2)); | ||||
decoded->push_back(dst); | ||||
dst = static_cast<char>(src_bin << (10 - bit_pos)); | ||||
bit_pos = (bit_pos + 6) % 8; | ||||
} | ||||
} | ||||
return true; | ||||
} | ||||
} // namespace internal | } // namespace internal | |||
} // namespace testing | } // namespace testing | |||
End of changes. 12 change blocks. | ||||
30 lines changed or deleted | 81 lines changed or added |