1b89a7cc2SEnji Cooper // Copyright 2007, Google Inc. 2b89a7cc2SEnji Cooper // All rights reserved. 3b89a7cc2SEnji Cooper // 4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without 5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are 6b89a7cc2SEnji Cooper // met: 7b89a7cc2SEnji Cooper // 8b89a7cc2SEnji Cooper // * Redistributions of source code must retain the above copyright 9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer. 10b89a7cc2SEnji Cooper // * Redistributions in binary form must reproduce the above 11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer 12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the 13b89a7cc2SEnji Cooper // distribution. 14b89a7cc2SEnji Cooper // * Neither the name of Google Inc. nor the names of its 15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from 16b89a7cc2SEnji Cooper // this software without specific prior written permission. 17b89a7cc2SEnji Cooper // 18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29b89a7cc2SEnji Cooper 30b89a7cc2SEnji Cooper // Google Mock - a framework for writing C++ mock classes. 31b89a7cc2SEnji Cooper // 32b89a7cc2SEnji Cooper // This file defines some utilities useful for implementing Google 33b89a7cc2SEnji Cooper // Mock. They are subject to change without notice, so please DO NOT 34b89a7cc2SEnji Cooper // USE THEM IN USER CODE. 35b89a7cc2SEnji Cooper 36b89a7cc2SEnji Cooper #include "gmock/internal/gmock-internal-utils.h" 37b89a7cc2SEnji Cooper 38b89a7cc2SEnji Cooper #include <ctype.h> 3928f6c2f2SEnji Cooper 4028f6c2f2SEnji Cooper #include <array> 4128f6c2f2SEnji Cooper #include <cctype> 4228f6c2f2SEnji Cooper #include <cstdint> 4328f6c2f2SEnji Cooper #include <cstring> 4428f6c2f2SEnji Cooper #include <iostream> 45b89a7cc2SEnji Cooper #include <ostream> // NOLINT 46b89a7cc2SEnji Cooper #include <string> 47*5ca8c28cSEnji Cooper #include <utility> 4828f6c2f2SEnji Cooper #include <vector> 4928f6c2f2SEnji Cooper 50b89a7cc2SEnji Cooper #include "gmock/gmock.h" 51b89a7cc2SEnji Cooper #include "gmock/internal/gmock-port.h" 52b89a7cc2SEnji Cooper #include "gtest/gtest.h" 53b89a7cc2SEnji Cooper 54b89a7cc2SEnji Cooper namespace testing { 55b89a7cc2SEnji Cooper namespace internal { 56b89a7cc2SEnji Cooper 57b89a7cc2SEnji Cooper // Joins a vector of strings as if they are fields of a tuple; returns 58b89a7cc2SEnji Cooper // the joined string. 5928f6c2f2SEnji Cooper GTEST_API_ std::string JoinAsKeyValueTuple( 6028f6c2f2SEnji Cooper const std::vector<const char*>& names, const Strings& values) { 6128f6c2f2SEnji Cooper GTEST_CHECK_(names.size() == values.size()); 6228f6c2f2SEnji Cooper if (values.empty()) { 63b89a7cc2SEnji Cooper return ""; 6428f6c2f2SEnji Cooper } 6528f6c2f2SEnji Cooper const auto build_one = [&](const size_t i) { 6628f6c2f2SEnji Cooper return std::string(names[i]) + ": " + values[i]; 6728f6c2f2SEnji Cooper }; 6828f6c2f2SEnji Cooper std::string result = "(" + build_one(0); 6928f6c2f2SEnji Cooper for (size_t i = 1; i < values.size(); i++) { 70b89a7cc2SEnji Cooper result += ", "; 7128f6c2f2SEnji Cooper result += build_one(i); 72b89a7cc2SEnji Cooper } 73b89a7cc2SEnji Cooper result += ")"; 74b89a7cc2SEnji Cooper return result; 75b89a7cc2SEnji Cooper } 76b89a7cc2SEnji Cooper 77b89a7cc2SEnji Cooper // Converts an identifier name to a space-separated list of lower-case 78b89a7cc2SEnji Cooper // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is 79b89a7cc2SEnji Cooper // treated as one word. For example, both "FooBar123" and 80b89a7cc2SEnji Cooper // "foo_bar_123" are converted to "foo bar 123". 81b89a7cc2SEnji Cooper GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name) { 82b89a7cc2SEnji Cooper std::string result; 83b89a7cc2SEnji Cooper char prev_char = '\0'; 84b89a7cc2SEnji Cooper for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) { 85b89a7cc2SEnji Cooper // We don't care about the current locale as the input is 86b89a7cc2SEnji Cooper // guaranteed to be a valid C++ identifier name. 87b89a7cc2SEnji Cooper const bool starts_new_word = IsUpper(*p) || 88b89a7cc2SEnji Cooper (!IsAlpha(prev_char) && IsLower(*p)) || 89b89a7cc2SEnji Cooper (!IsDigit(prev_char) && IsDigit(*p)); 90b89a7cc2SEnji Cooper 91b89a7cc2SEnji Cooper if (IsAlNum(*p)) { 9228f6c2f2SEnji Cooper if (starts_new_word && !result.empty()) result += ' '; 93b89a7cc2SEnji Cooper result += ToLower(*p); 94b89a7cc2SEnji Cooper } 95b89a7cc2SEnji Cooper } 96b89a7cc2SEnji Cooper return result; 97b89a7cc2SEnji Cooper } 98b89a7cc2SEnji Cooper 99b89a7cc2SEnji Cooper // This class reports Google Mock failures as Google Test failures. A 100b89a7cc2SEnji Cooper // user can define another class in a similar fashion if they intend to 101b89a7cc2SEnji Cooper // use Google Mock with a testing framework other than Google Test. 102b89a7cc2SEnji Cooper class GoogleTestFailureReporter : public FailureReporterInterface { 103b89a7cc2SEnji Cooper public: 10428f6c2f2SEnji Cooper void ReportFailure(FailureType type, const char* file, int line, 10528f6c2f2SEnji Cooper const std::string& message) override { 10628f6c2f2SEnji Cooper AssertHelper(type == kFatal ? TestPartResult::kFatalFailure 10728f6c2f2SEnji Cooper : TestPartResult::kNonFatalFailure, 10828f6c2f2SEnji Cooper file, line, message.c_str()) = Message(); 109b89a7cc2SEnji Cooper if (type == kFatal) { 110b89a7cc2SEnji Cooper posix::Abort(); 111b89a7cc2SEnji Cooper } 112b89a7cc2SEnji Cooper } 113b89a7cc2SEnji Cooper }; 114b89a7cc2SEnji Cooper 115b89a7cc2SEnji Cooper // Returns the global failure reporter. Will create a 116b89a7cc2SEnji Cooper // GoogleTestFailureReporter and return it the first time called. 117b89a7cc2SEnji Cooper GTEST_API_ FailureReporterInterface* GetFailureReporter() { 118b89a7cc2SEnji Cooper // Points to the global failure reporter used by Google Mock. gcc 119b89a7cc2SEnji Cooper // guarantees that the following use of failure_reporter is 120b89a7cc2SEnji Cooper // thread-safe. We may need to add additional synchronization to 121b89a7cc2SEnji Cooper // protect failure_reporter if we port Google Mock to other 122b89a7cc2SEnji Cooper // compilers. 123b89a7cc2SEnji Cooper static FailureReporterInterface* const failure_reporter = 124b89a7cc2SEnji Cooper new GoogleTestFailureReporter(); 125b89a7cc2SEnji Cooper return failure_reporter; 126b89a7cc2SEnji Cooper } 127b89a7cc2SEnji Cooper 128b89a7cc2SEnji Cooper // Protects global resources (stdout in particular) used by Log(). 129b89a7cc2SEnji Cooper static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex); 130b89a7cc2SEnji Cooper 13128f6c2f2SEnji Cooper // Returns true if and only if a log with the given severity is visible 13228f6c2f2SEnji Cooper // according to the --gmock_verbose flag. 133b89a7cc2SEnji Cooper GTEST_API_ bool LogIsVisible(LogSeverity severity) { 13428f6c2f2SEnji Cooper if (GMOCK_FLAG_GET(verbose) == kInfoVerbosity) { 135b89a7cc2SEnji Cooper // Always show the log if --gmock_verbose=info. 136b89a7cc2SEnji Cooper return true; 13728f6c2f2SEnji Cooper } else if (GMOCK_FLAG_GET(verbose) == kErrorVerbosity) { 138b89a7cc2SEnji Cooper // Always hide it if --gmock_verbose=error. 139b89a7cc2SEnji Cooper return false; 140b89a7cc2SEnji Cooper } else { 141b89a7cc2SEnji Cooper // If --gmock_verbose is neither "info" nor "error", we treat it 142b89a7cc2SEnji Cooper // as "warning" (its default value). 143b89a7cc2SEnji Cooper return severity == kWarning; 144b89a7cc2SEnji Cooper } 145b89a7cc2SEnji Cooper } 146b89a7cc2SEnji Cooper 14728f6c2f2SEnji Cooper // Prints the given message to stdout if and only if 'severity' >= the level 148b89a7cc2SEnji Cooper // specified by the --gmock_verbose flag. If stack_frames_to_skip >= 149b89a7cc2SEnji Cooper // 0, also prints the stack trace excluding the top 150b89a7cc2SEnji Cooper // stack_frames_to_skip frames. In opt mode, any positive 151b89a7cc2SEnji Cooper // stack_frames_to_skip is treated as 0, since we don't know which 152b89a7cc2SEnji Cooper // function calls will be inlined by the compiler and need to be 153b89a7cc2SEnji Cooper // conservative. 154b89a7cc2SEnji Cooper GTEST_API_ void Log(LogSeverity severity, const std::string& message, 155b89a7cc2SEnji Cooper int stack_frames_to_skip) { 15628f6c2f2SEnji Cooper if (!LogIsVisible(severity)) return; 157b89a7cc2SEnji Cooper 158b89a7cc2SEnji Cooper // Ensures that logs from different threads don't interleave. 159b89a7cc2SEnji Cooper MutexLock l(&g_log_mutex); 160b89a7cc2SEnji Cooper 161b89a7cc2SEnji Cooper if (severity == kWarning) { 162b89a7cc2SEnji Cooper // Prints a GMOCK WARNING marker to make the warnings easily searchable. 163b89a7cc2SEnji Cooper std::cout << "\nGMOCK WARNING:"; 164b89a7cc2SEnji Cooper } 165b89a7cc2SEnji Cooper // Pre-pends a new-line to message if it doesn't start with one. 166b89a7cc2SEnji Cooper if (message.empty() || message[0] != '\n') { 167b89a7cc2SEnji Cooper std::cout << "\n"; 168b89a7cc2SEnji Cooper } 169b89a7cc2SEnji Cooper std::cout << message; 170b89a7cc2SEnji Cooper if (stack_frames_to_skip >= 0) { 171b89a7cc2SEnji Cooper #ifdef NDEBUG 172b89a7cc2SEnji Cooper // In opt mode, we have to be conservative and skip no stack frame. 173b89a7cc2SEnji Cooper const int actual_to_skip = 0; 174b89a7cc2SEnji Cooper #else 175b89a7cc2SEnji Cooper // In dbg mode, we can do what the caller tell us to do (plus one 176b89a7cc2SEnji Cooper // for skipping this function's stack frame). 177b89a7cc2SEnji Cooper const int actual_to_skip = stack_frames_to_skip + 1; 178b89a7cc2SEnji Cooper #endif // NDEBUG 179b89a7cc2SEnji Cooper 180b89a7cc2SEnji Cooper // Appends a new-line to message if it doesn't end with one. 181b89a7cc2SEnji Cooper if (!message.empty() && *message.rbegin() != '\n') { 182b89a7cc2SEnji Cooper std::cout << "\n"; 183b89a7cc2SEnji Cooper } 184b89a7cc2SEnji Cooper std::cout << "Stack trace:\n" 185b89a7cc2SEnji Cooper << ::testing::internal::GetCurrentOsStackTraceExceptTop( 18628f6c2f2SEnji Cooper actual_to_skip); 187b89a7cc2SEnji Cooper } 188b89a7cc2SEnji Cooper std::cout << ::std::flush; 189b89a7cc2SEnji Cooper } 190b89a7cc2SEnji Cooper 191b89a7cc2SEnji Cooper GTEST_API_ WithoutMatchers GetWithoutMatchers() { return WithoutMatchers(); } 192b89a7cc2SEnji Cooper 193b89a7cc2SEnji Cooper GTEST_API_ void IllegalDoDefault(const char* file, int line) { 194b89a7cc2SEnji Cooper internal::Assert( 195b89a7cc2SEnji Cooper false, file, line, 196b89a7cc2SEnji Cooper "You are using DoDefault() inside a composite action like " 197b89a7cc2SEnji Cooper "DoAll() or WithArgs(). This is not supported for technical " 198b89a7cc2SEnji Cooper "reasons. Please instead spell out the default action, or " 199b89a7cc2SEnji Cooper "assign the default action to an Action variable and use " 200b89a7cc2SEnji Cooper "the variable in various places."); 201b89a7cc2SEnji Cooper } 202b89a7cc2SEnji Cooper 20328f6c2f2SEnji Cooper constexpr char UndoWebSafeEncoding(char c) { 20428f6c2f2SEnji Cooper return c == '-' ? '+' : c == '_' ? '/' : c; 20528f6c2f2SEnji Cooper } 20628f6c2f2SEnji Cooper 20728f6c2f2SEnji Cooper constexpr char UnBase64Impl(char c, const char* const base64, char carry) { 20828f6c2f2SEnji Cooper return *base64 == 0 ? static_cast<char>(65) 20928f6c2f2SEnji Cooper : *base64 == c 21028f6c2f2SEnji Cooper ? carry 21128f6c2f2SEnji Cooper : UnBase64Impl(c, base64 + 1, static_cast<char>(carry + 1)); 21228f6c2f2SEnji Cooper } 21328f6c2f2SEnji Cooper 21428f6c2f2SEnji Cooper template <size_t... I> 215*5ca8c28cSEnji Cooper constexpr std::array<char, 256> UnBase64Impl(std::index_sequence<I...>, 21628f6c2f2SEnji Cooper const char* const base64) { 21728f6c2f2SEnji Cooper return { 21828f6c2f2SEnji Cooper {UnBase64Impl(UndoWebSafeEncoding(static_cast<char>(I)), base64, 0)...}}; 21928f6c2f2SEnji Cooper } 22028f6c2f2SEnji Cooper 22128f6c2f2SEnji Cooper constexpr std::array<char, 256> UnBase64(const char* const base64) { 222*5ca8c28cSEnji Cooper return UnBase64Impl(std::make_index_sequence<256>{}, base64); 22328f6c2f2SEnji Cooper } 22428f6c2f2SEnji Cooper 22528f6c2f2SEnji Cooper static constexpr char kBase64[] = 22628f6c2f2SEnji Cooper "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 22728f6c2f2SEnji Cooper static constexpr std::array<char, 256> kUnBase64 = UnBase64(kBase64); 22828f6c2f2SEnji Cooper 22928f6c2f2SEnji Cooper bool Base64Unescape(const std::string& encoded, std::string* decoded) { 23028f6c2f2SEnji Cooper decoded->clear(); 23128f6c2f2SEnji Cooper size_t encoded_len = encoded.size(); 23228f6c2f2SEnji Cooper decoded->reserve(3 * (encoded_len / 4) + (encoded_len % 4)); 23328f6c2f2SEnji Cooper int bit_pos = 0; 23428f6c2f2SEnji Cooper char dst = 0; 23528f6c2f2SEnji Cooper for (int src : encoded) { 23628f6c2f2SEnji Cooper if (std::isspace(src) || src == '=') { 23728f6c2f2SEnji Cooper continue; 23828f6c2f2SEnji Cooper } 23928f6c2f2SEnji Cooper char src_bin = kUnBase64[static_cast<size_t>(src)]; 24028f6c2f2SEnji Cooper if (src_bin >= 64) { 24128f6c2f2SEnji Cooper decoded->clear(); 24228f6c2f2SEnji Cooper return false; 24328f6c2f2SEnji Cooper } 24428f6c2f2SEnji Cooper if (bit_pos == 0) { 24528f6c2f2SEnji Cooper dst |= static_cast<char>(src_bin << 2); 24628f6c2f2SEnji Cooper bit_pos = 6; 24728f6c2f2SEnji Cooper } else { 24828f6c2f2SEnji Cooper dst |= static_cast<char>(src_bin >> (bit_pos - 2)); 24928f6c2f2SEnji Cooper decoded->push_back(dst); 25028f6c2f2SEnji Cooper dst = static_cast<char>(src_bin << (10 - bit_pos)); 25128f6c2f2SEnji Cooper bit_pos = (bit_pos + 6) % 8; 25228f6c2f2SEnji Cooper } 25328f6c2f2SEnji Cooper } 25428f6c2f2SEnji Cooper return true; 25528f6c2f2SEnji Cooper } 25628f6c2f2SEnji Cooper 257b89a7cc2SEnji Cooper } // namespace internal 258b89a7cc2SEnji Cooper } // namespace testing 259