| 1 | // Copyright 2007, Google Inc. | 
|---|
| 2 | // All rights reserved. | 
|---|
| 3 | // | 
|---|
| 4 | // Redistribution and use in source and binary forms, with or without | 
|---|
| 5 | // modification, are permitted provided that the following conditions are | 
|---|
| 6 | // met: | 
|---|
| 7 | // | 
|---|
| 8 | //     * Redistributions of source code must retain the above copyright | 
|---|
| 9 | // notice, this list of conditions and the following disclaimer. | 
|---|
| 10 | //     * Redistributions in binary form must reproduce the above | 
|---|
| 11 | // copyright notice, this list of conditions and the following disclaimer | 
|---|
| 12 | // in the documentation and/or other materials provided with the | 
|---|
| 13 | // distribution. | 
|---|
| 14 | //     * Neither the name of Google Inc. nor the names of its | 
|---|
| 15 | // contributors may be used to endorse or promote products derived from | 
|---|
| 16 | // this software without specific prior written permission. | 
|---|
| 17 | // | 
|---|
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|---|
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|---|
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
|---|
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
|---|
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
|---|
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
|---|
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
|---|
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 29 |  | 
|---|
| 30 | // Google Mock - a framework for writing C++ mock classes. | 
|---|
| 31 | // | 
|---|
| 32 | // This file defines some utilities useful for implementing Google | 
|---|
| 33 | // Mock.  They are subject to change without notice, so please DO NOT | 
|---|
| 34 | // USE THEM IN USER CODE. | 
|---|
| 35 |  | 
|---|
| 36 | #include "gmock/internal/gmock-internal-utils.h" | 
|---|
| 37 |  | 
|---|
| 38 | #include <ctype.h> | 
|---|
| 39 |  | 
|---|
| 40 | #include <array> | 
|---|
| 41 | #include <cctype> | 
|---|
| 42 | #include <cstdint> | 
|---|
| 43 | #include <cstring> | 
|---|
| 44 | #include <ostream>  // NOLINT | 
|---|
| 45 | #include <string> | 
|---|
| 46 | #include <vector> | 
|---|
| 47 |  | 
|---|
| 48 | #include "gmock/gmock.h" | 
|---|
| 49 | #include "gmock/internal/gmock-port.h" | 
|---|
| 50 | #include "gtest/gtest.h" | 
|---|
| 51 |  | 
|---|
| 52 | namespace testing { | 
|---|
| 53 | namespace internal { | 
|---|
| 54 |  | 
|---|
| 55 | // Joins a vector of strings as if they are fields of a tuple; returns | 
|---|
| 56 | // the joined string. | 
|---|
| 57 | GTEST_API_ std::string JoinAsKeyValueTuple( | 
|---|
| 58 | const std::vector<const char*>& names, const Strings& values) { | 
|---|
| 59 | GTEST_CHECK_(names.size() == values.size()); | 
|---|
| 60 | if (values.empty()) { | 
|---|
| 61 | return ""; | 
|---|
| 62 | } | 
|---|
| 63 | const auto build_one = [&](const size_t i) { | 
|---|
| 64 | return std::string(names[i]) + ": "+ values[i]; | 
|---|
| 65 | }; | 
|---|
| 66 | std::string result = "("+ build_one(0); | 
|---|
| 67 | for (size_t i = 1; i < values.size(); i++) { | 
|---|
| 68 | result += ", "; | 
|---|
| 69 | result += build_one(i); | 
|---|
| 70 | } | 
|---|
| 71 | result += ")"; | 
|---|
| 72 | return result; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | // Converts an identifier name to a space-separated list of lower-case | 
|---|
| 76 | // words.  Each maximum substring of the form [A-Za-z][a-z]*|\d+ is | 
|---|
| 77 | // treated as one word.  For example, both "FooBar123" and | 
|---|
| 78 | // "foo_bar_123" are converted to "foo bar 123". | 
|---|
| 79 | GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name) { | 
|---|
| 80 | std::string result; | 
|---|
| 81 | char prev_char = '\0'; | 
|---|
| 82 | for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) { | 
|---|
| 83 | // We don't care about the current locale as the input is | 
|---|
| 84 | // guaranteed to be a valid C++ identifier name. | 
|---|
| 85 | const bool starts_new_word = IsUpper(ch: *p) || | 
|---|
| 86 | (!IsAlpha(ch: prev_char) && IsLower(ch: *p)) || | 
|---|
| 87 | (!IsDigit(ch: prev_char) && IsDigit(ch: *p)); | 
|---|
| 88 |  | 
|---|
| 89 | if (IsAlNum(ch: *p)) { | 
|---|
| 90 | if (starts_new_word && result != "") result += ' '; | 
|---|
| 91 | result += ToLower(ch: *p); | 
|---|
| 92 | } | 
|---|
| 93 | } | 
|---|
| 94 | return result; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | // This class reports Google Mock failures as Google Test failures.  A | 
|---|
| 98 | // user can define another class in a similar fashion if they intend to | 
|---|
| 99 | // use Google Mock with a testing framework other than Google Test. | 
|---|
| 100 | class GoogleTestFailureReporter : public FailureReporterInterface { | 
|---|
| 101 | public: | 
|---|
| 102 | void ReportFailure(FailureType type, const char* file, int line, | 
|---|
| 103 | const std::string& message) override { | 
|---|
| 104 | AssertHelper(type == kFatal ? TestPartResult::kFatalFailure | 
|---|
| 105 | : TestPartResult::kNonFatalFailure, | 
|---|
| 106 | file, line, message.c_str()) = Message(); | 
|---|
| 107 | if (type == kFatal) { | 
|---|
| 108 | posix::Abort(); | 
|---|
| 109 | } | 
|---|
| 110 | } | 
|---|
| 111 | }; | 
|---|
| 112 |  | 
|---|
| 113 | // Returns the global failure reporter.  Will create a | 
|---|
| 114 | // GoogleTestFailureReporter and return it the first time called. | 
|---|
| 115 | GTEST_API_ FailureReporterInterface* GetFailureReporter() { | 
|---|
| 116 | // Points to the global failure reporter used by Google Mock.  gcc | 
|---|
| 117 | // guarantees that the following use of failure_reporter is | 
|---|
| 118 | // thread-safe.  We may need to add additional synchronization to | 
|---|
| 119 | // protect failure_reporter if we port Google Mock to other | 
|---|
| 120 | // compilers. | 
|---|
| 121 | static FailureReporterInterface* const failure_reporter = | 
|---|
| 122 | new GoogleTestFailureReporter(); | 
|---|
| 123 | return failure_reporter; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | // Protects global resources (stdout in particular) used by Log(). | 
|---|
| 127 | static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex); | 
|---|
| 128 |  | 
|---|
| 129 | // Returns true if and only if a log with the given severity is visible | 
|---|
| 130 | // according to the --gmock_verbose flag. | 
|---|
| 131 | GTEST_API_ bool LogIsVisible(LogSeverity severity) { | 
|---|
| 132 | if (GMOCK_FLAG_GET(verbose) == kInfoVerbosity) { | 
|---|
| 133 | // Always show the log if --gmock_verbose=info. | 
|---|
| 134 | return true; | 
|---|
| 135 | } else if (GMOCK_FLAG_GET(verbose) == kErrorVerbosity) { | 
|---|
| 136 | // Always hide it if --gmock_verbose=error. | 
|---|
| 137 | return false; | 
|---|
| 138 | } else { | 
|---|
| 139 | // If --gmock_verbose is neither "info" nor "error", we treat it | 
|---|
| 140 | // as "warning" (its default value). | 
|---|
| 141 | return severity == kWarning; | 
|---|
| 142 | } | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | // Prints the given message to stdout if and only if 'severity' >= the level | 
|---|
| 146 | // specified by the --gmock_verbose flag.  If stack_frames_to_skip >= | 
|---|
| 147 | // 0, also prints the stack trace excluding the top | 
|---|
| 148 | // stack_frames_to_skip frames.  In opt mode, any positive | 
|---|
| 149 | // stack_frames_to_skip is treated as 0, since we don't know which | 
|---|
| 150 | // function calls will be inlined by the compiler and need to be | 
|---|
| 151 | // conservative. | 
|---|
| 152 | GTEST_API_ void Log(LogSeverity severity, const std::string& message, | 
|---|
| 153 | int stack_frames_to_skip) { | 
|---|
| 154 | if (!LogIsVisible(severity)) return; | 
|---|
| 155 |  | 
|---|
| 156 | // Ensures that logs from different threads don't interleave. | 
|---|
| 157 | MutexLock l(&g_log_mutex); | 
|---|
| 158 |  | 
|---|
| 159 | if (severity == kWarning) { | 
|---|
| 160 | // Prints a GMOCK WARNING marker to make the warnings easily searchable. | 
|---|
| 161 | std::cout << "\nGMOCK WARNING:"; | 
|---|
| 162 | } | 
|---|
| 163 | // Pre-pends a new-line to message if it doesn't start with one. | 
|---|
| 164 | if (message.empty() || message[0] != '\n') { | 
|---|
| 165 | std::cout << "\n"; | 
|---|
| 166 | } | 
|---|
| 167 | std::cout << message; | 
|---|
| 168 | if (stack_frames_to_skip >= 0) { | 
|---|
| 169 | #ifdef NDEBUG | 
|---|
| 170 | // In opt mode, we have to be conservative and skip no stack frame. | 
|---|
| 171 | const int actual_to_skip = 0; | 
|---|
| 172 | #else | 
|---|
| 173 | // In dbg mode, we can do what the caller tell us to do (plus one | 
|---|
| 174 | // for skipping this function's stack frame). | 
|---|
| 175 | const int actual_to_skip = stack_frames_to_skip + 1; | 
|---|
| 176 | #endif  // NDEBUG | 
|---|
| 177 |  | 
|---|
| 178 | // Appends a new-line to message if it doesn't end with one. | 
|---|
| 179 | if (!message.empty() && *message.rbegin() != '\n') { | 
|---|
| 180 | std::cout << "\n"; | 
|---|
| 181 | } | 
|---|
| 182 | std::cout << "Stack trace:\n" | 
|---|
| 183 | << ::testing::internal::GetCurrentOsStackTraceExceptTop( | 
|---|
| 184 | skip_count: actual_to_skip); | 
|---|
| 185 | } | 
|---|
| 186 | std::cout << ::std::flush; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | GTEST_API_ WithoutMatchers GetWithoutMatchers() { return WithoutMatchers(); } | 
|---|
| 190 |  | 
|---|
| 191 | GTEST_API_ void IllegalDoDefault(const char* file, int line) { | 
|---|
| 192 | internal::Assert( | 
|---|
| 193 | condition: false, file, line, | 
|---|
| 194 | msg: "You are using DoDefault() inside a composite action like " | 
|---|
| 195 | "DoAll() or WithArgs().  This is not supported for technical " | 
|---|
| 196 | "reasons.  Please instead spell out the default action, or " | 
|---|
| 197 | "assign the default action to an Action variable and use " | 
|---|
| 198 | "the variable in various places."); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | constexpr char UnBase64Impl(char c, const char* const base64, char carry) { | 
|---|
| 202 | return *base64 == 0 ? static_cast<char>(65) | 
|---|
| 203 | : *base64 == c | 
|---|
| 204 | ? carry | 
|---|
| 205 | : UnBase64Impl(c, base64: base64 + 1, carry: static_cast<char>(carry + 1)); | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | template <size_t... I> | 
|---|
| 209 | constexpr std::array<char, 256> UnBase64Impl(IndexSequence<I...>, | 
|---|
| 210 | const char* const base64) { | 
|---|
| 211 | return {{UnBase64Impl(c: static_cast<char>(I), base64, carry: 0)...}}; | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 | constexpr std::array<char, 256> UnBase64(const char* const base64) { | 
|---|
| 215 | return UnBase64Impl(MakeIndexSequence<256>{}, base64); | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | static constexpr char kBase64[] = | 
|---|
| 219 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | 
|---|
| 220 | static constexpr std::array<char, 256> kUnBase64 = UnBase64(base64: kBase64); | 
|---|
| 221 |  | 
|---|
| 222 | bool Base64Unescape(const std::string& encoded, std::string* decoded) { | 
|---|
| 223 | decoded->clear(); | 
|---|
| 224 | size_t encoded_len = encoded.size(); | 
|---|
| 225 | decoded->reserve(res_arg: 3 * (encoded_len / 4) + (encoded_len % 4)); | 
|---|
| 226 | int bit_pos = 0; | 
|---|
| 227 | char dst = 0; | 
|---|
| 228 | for (int src : encoded) { | 
|---|
| 229 | if (std::isspace(src) || src == '=') { | 
|---|
| 230 | continue; | 
|---|
| 231 | } | 
|---|
| 232 | char src_bin = kUnBase64[static_cast<size_t>(src)]; | 
|---|
| 233 | if (src_bin >= 64) { | 
|---|
| 234 | decoded->clear(); | 
|---|
| 235 | return false; | 
|---|
| 236 | } | 
|---|
| 237 | if (bit_pos == 0) { | 
|---|
| 238 | dst |= static_cast<char>(src_bin << 2); | 
|---|
| 239 | bit_pos = 6; | 
|---|
| 240 | } else { | 
|---|
| 241 | dst |= static_cast<char>(src_bin >> (bit_pos - 2)); | 
|---|
| 242 | decoded->push_back(c: dst); | 
|---|
| 243 | dst = static_cast<char>(src_bin << (10 - bit_pos)); | 
|---|
| 244 | bit_pos = (bit_pos + 6) % 8; | 
|---|
| 245 | } | 
|---|
| 246 | } | 
|---|
| 247 | return true; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | }  // namespace internal | 
|---|
| 251 | }  // namespace testing | 
|---|
| 252 |  | 
|---|