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>
4728f6c2f2SEnji Cooper #include <vector>
4828f6c2f2SEnji Cooper
49b89a7cc2SEnji Cooper #include "gmock/gmock.h"
50b89a7cc2SEnji Cooper #include "gmock/internal/gmock-port.h"
51b89a7cc2SEnji Cooper #include "gtest/gtest.h"
52b89a7cc2SEnji Cooper
53b89a7cc2SEnji Cooper namespace testing {
54b89a7cc2SEnji Cooper namespace internal {
55b89a7cc2SEnji Cooper
56b89a7cc2SEnji Cooper // Joins a vector of strings as if they are fields of a tuple; returns
57b89a7cc2SEnji Cooper // the joined string.
JoinAsKeyValueTuple(const std::vector<const char * > & names,const Strings & values)5828f6c2f2SEnji Cooper GTEST_API_ std::string JoinAsKeyValueTuple(
5928f6c2f2SEnji Cooper const std::vector<const char*>& names, const Strings& values) {
6028f6c2f2SEnji Cooper GTEST_CHECK_(names.size() == values.size());
6128f6c2f2SEnji Cooper if (values.empty()) {
62b89a7cc2SEnji Cooper return "";
6328f6c2f2SEnji Cooper }
6428f6c2f2SEnji Cooper const auto build_one = [&](const size_t i) {
6528f6c2f2SEnji Cooper return std::string(names[i]) + ": " + values[i];
6628f6c2f2SEnji Cooper };
6728f6c2f2SEnji Cooper std::string result = "(" + build_one(0);
6828f6c2f2SEnji Cooper for (size_t i = 1; i < values.size(); i++) {
69b89a7cc2SEnji Cooper result += ", ";
7028f6c2f2SEnji Cooper result += build_one(i);
71b89a7cc2SEnji Cooper }
72b89a7cc2SEnji Cooper result += ")";
73b89a7cc2SEnji Cooper return result;
74b89a7cc2SEnji Cooper }
75b89a7cc2SEnji Cooper
76b89a7cc2SEnji Cooper // Converts an identifier name to a space-separated list of lower-case
77b89a7cc2SEnji Cooper // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is
78b89a7cc2SEnji Cooper // treated as one word. For example, both "FooBar123" and
79b89a7cc2SEnji Cooper // "foo_bar_123" are converted to "foo bar 123".
ConvertIdentifierNameToWords(const char * id_name)80b89a7cc2SEnji Cooper GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name) {
81b89a7cc2SEnji Cooper std::string result;
82b89a7cc2SEnji Cooper char prev_char = '\0';
83b89a7cc2SEnji Cooper for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) {
84b89a7cc2SEnji Cooper // We don't care about the current locale as the input is
85b89a7cc2SEnji Cooper // guaranteed to be a valid C++ identifier name.
86b89a7cc2SEnji Cooper const bool starts_new_word = IsUpper(*p) ||
87b89a7cc2SEnji Cooper (!IsAlpha(prev_char) && IsLower(*p)) ||
88b89a7cc2SEnji Cooper (!IsDigit(prev_char) && IsDigit(*p));
89b89a7cc2SEnji Cooper
90b89a7cc2SEnji Cooper if (IsAlNum(*p)) {
9128f6c2f2SEnji Cooper if (starts_new_word && !result.empty()) result += ' ';
92b89a7cc2SEnji Cooper result += ToLower(*p);
93b89a7cc2SEnji Cooper }
94b89a7cc2SEnji Cooper }
95b89a7cc2SEnji Cooper return result;
96b89a7cc2SEnji Cooper }
97b89a7cc2SEnji Cooper
98b89a7cc2SEnji Cooper // This class reports Google Mock failures as Google Test failures. A
99b89a7cc2SEnji Cooper // user can define another class in a similar fashion if they intend to
100b89a7cc2SEnji Cooper // use Google Mock with a testing framework other than Google Test.
101b89a7cc2SEnji Cooper class GoogleTestFailureReporter : public FailureReporterInterface {
102b89a7cc2SEnji Cooper public:
ReportFailure(FailureType type,const char * file,int line,const std::string & message)10328f6c2f2SEnji Cooper void ReportFailure(FailureType type, const char* file, int line,
10428f6c2f2SEnji Cooper const std::string& message) override {
10528f6c2f2SEnji Cooper AssertHelper(type == kFatal ? TestPartResult::kFatalFailure
10628f6c2f2SEnji Cooper : TestPartResult::kNonFatalFailure,
10728f6c2f2SEnji Cooper file, line, message.c_str()) = Message();
108b89a7cc2SEnji Cooper if (type == kFatal) {
109b89a7cc2SEnji Cooper posix::Abort();
110b89a7cc2SEnji Cooper }
111b89a7cc2SEnji Cooper }
112b89a7cc2SEnji Cooper };
113b89a7cc2SEnji Cooper
114b89a7cc2SEnji Cooper // Returns the global failure reporter. Will create a
115b89a7cc2SEnji Cooper // GoogleTestFailureReporter and return it the first time called.
GetFailureReporter()116b89a7cc2SEnji Cooper GTEST_API_ FailureReporterInterface* GetFailureReporter() {
117b89a7cc2SEnji Cooper // Points to the global failure reporter used by Google Mock. gcc
118b89a7cc2SEnji Cooper // guarantees that the following use of failure_reporter is
119b89a7cc2SEnji Cooper // thread-safe. We may need to add additional synchronization to
120b89a7cc2SEnji Cooper // protect failure_reporter if we port Google Mock to other
121b89a7cc2SEnji Cooper // compilers.
122b89a7cc2SEnji Cooper static FailureReporterInterface* const failure_reporter =
123b89a7cc2SEnji Cooper new GoogleTestFailureReporter();
124b89a7cc2SEnji Cooper return failure_reporter;
125b89a7cc2SEnji Cooper }
126b89a7cc2SEnji Cooper
127b89a7cc2SEnji Cooper // Protects global resources (stdout in particular) used by Log().
128b89a7cc2SEnji Cooper static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex);
129b89a7cc2SEnji Cooper
13028f6c2f2SEnji Cooper // Returns true if and only if a log with the given severity is visible
13128f6c2f2SEnji Cooper // according to the --gmock_verbose flag.
LogIsVisible(LogSeverity severity)132b89a7cc2SEnji Cooper GTEST_API_ bool LogIsVisible(LogSeverity severity) {
13328f6c2f2SEnji Cooper if (GMOCK_FLAG_GET(verbose) == kInfoVerbosity) {
134b89a7cc2SEnji Cooper // Always show the log if --gmock_verbose=info.
135b89a7cc2SEnji Cooper return true;
13628f6c2f2SEnji Cooper } else if (GMOCK_FLAG_GET(verbose) == kErrorVerbosity) {
137b89a7cc2SEnji Cooper // Always hide it if --gmock_verbose=error.
138b89a7cc2SEnji Cooper return false;
139b89a7cc2SEnji Cooper } else {
140b89a7cc2SEnji Cooper // If --gmock_verbose is neither "info" nor "error", we treat it
141b89a7cc2SEnji Cooper // as "warning" (its default value).
142b89a7cc2SEnji Cooper return severity == kWarning;
143b89a7cc2SEnji Cooper }
144b89a7cc2SEnji Cooper }
145b89a7cc2SEnji Cooper
14628f6c2f2SEnji Cooper // Prints the given message to stdout if and only if 'severity' >= the level
147b89a7cc2SEnji Cooper // specified by the --gmock_verbose flag. If stack_frames_to_skip >=
148b89a7cc2SEnji Cooper // 0, also prints the stack trace excluding the top
149b89a7cc2SEnji Cooper // stack_frames_to_skip frames. In opt mode, any positive
150b89a7cc2SEnji Cooper // stack_frames_to_skip is treated as 0, since we don't know which
151b89a7cc2SEnji Cooper // function calls will be inlined by the compiler and need to be
152b89a7cc2SEnji Cooper // conservative.
Log(LogSeverity severity,const std::string & message,int stack_frames_to_skip)153b89a7cc2SEnji Cooper GTEST_API_ void Log(LogSeverity severity, const std::string& message,
154b89a7cc2SEnji Cooper int stack_frames_to_skip) {
15528f6c2f2SEnji Cooper if (!LogIsVisible(severity)) return;
156b89a7cc2SEnji Cooper
157b89a7cc2SEnji Cooper // Ensures that logs from different threads don't interleave.
158b89a7cc2SEnji Cooper MutexLock l(&g_log_mutex);
159b89a7cc2SEnji Cooper
160b89a7cc2SEnji Cooper if (severity == kWarning) {
161b89a7cc2SEnji Cooper // Prints a GMOCK WARNING marker to make the warnings easily searchable.
162b89a7cc2SEnji Cooper std::cout << "\nGMOCK WARNING:";
163b89a7cc2SEnji Cooper }
164b89a7cc2SEnji Cooper // Pre-pends a new-line to message if it doesn't start with one.
165b89a7cc2SEnji Cooper if (message.empty() || message[0] != '\n') {
166b89a7cc2SEnji Cooper std::cout << "\n";
167b89a7cc2SEnji Cooper }
168b89a7cc2SEnji Cooper std::cout << message;
169b89a7cc2SEnji Cooper if (stack_frames_to_skip >= 0) {
170b89a7cc2SEnji Cooper #ifdef NDEBUG
171b89a7cc2SEnji Cooper // In opt mode, we have to be conservative and skip no stack frame.
172b89a7cc2SEnji Cooper const int actual_to_skip = 0;
173b89a7cc2SEnji Cooper #else
174b89a7cc2SEnji Cooper // In dbg mode, we can do what the caller tell us to do (plus one
175b89a7cc2SEnji Cooper // for skipping this function's stack frame).
176b89a7cc2SEnji Cooper const int actual_to_skip = stack_frames_to_skip + 1;
177b89a7cc2SEnji Cooper #endif // NDEBUG
178b89a7cc2SEnji Cooper
179b89a7cc2SEnji Cooper // Appends a new-line to message if it doesn't end with one.
180b89a7cc2SEnji Cooper if (!message.empty() && *message.rbegin() != '\n') {
181b89a7cc2SEnji Cooper std::cout << "\n";
182b89a7cc2SEnji Cooper }
183b89a7cc2SEnji Cooper std::cout << "Stack trace:\n"
184b89a7cc2SEnji Cooper << ::testing::internal::GetCurrentOsStackTraceExceptTop(
18528f6c2f2SEnji Cooper actual_to_skip);
186b89a7cc2SEnji Cooper }
187b89a7cc2SEnji Cooper std::cout << ::std::flush;
188b89a7cc2SEnji Cooper }
189b89a7cc2SEnji Cooper
GetWithoutMatchers()190b89a7cc2SEnji Cooper GTEST_API_ WithoutMatchers GetWithoutMatchers() { return WithoutMatchers(); }
191b89a7cc2SEnji Cooper
IllegalDoDefault(const char * file,int line)192b89a7cc2SEnji Cooper GTEST_API_ void IllegalDoDefault(const char* file, int line) {
193b89a7cc2SEnji Cooper internal::Assert(
194b89a7cc2SEnji Cooper false, file, line,
195b89a7cc2SEnji Cooper "You are using DoDefault() inside a composite action like "
196b89a7cc2SEnji Cooper "DoAll() or WithArgs(). This is not supported for technical "
197b89a7cc2SEnji Cooper "reasons. Please instead spell out the default action, or "
198b89a7cc2SEnji Cooper "assign the default action to an Action variable and use "
199b89a7cc2SEnji Cooper "the variable in various places.");
200b89a7cc2SEnji Cooper }
201b89a7cc2SEnji Cooper
UndoWebSafeEncoding(char c)20228f6c2f2SEnji Cooper constexpr char UndoWebSafeEncoding(char c) {
20328f6c2f2SEnji Cooper return c == '-' ? '+' : c == '_' ? '/' : c;
20428f6c2f2SEnji Cooper }
20528f6c2f2SEnji Cooper
UnBase64Impl(char c,const char * const base64,char carry)20628f6c2f2SEnji Cooper constexpr char UnBase64Impl(char c, const char* const base64, char carry) {
20728f6c2f2SEnji Cooper return *base64 == 0 ? static_cast<char>(65)
20828f6c2f2SEnji Cooper : *base64 == c
20928f6c2f2SEnji Cooper ? carry
21028f6c2f2SEnji Cooper : UnBase64Impl(c, base64 + 1, static_cast<char>(carry + 1));
21128f6c2f2SEnji Cooper }
21228f6c2f2SEnji Cooper
21328f6c2f2SEnji Cooper template <size_t... I>
UnBase64Impl(IndexSequence<I...>,const char * const base64)21428f6c2f2SEnji Cooper constexpr std::array<char, 256> UnBase64Impl(IndexSequence<I...>,
21528f6c2f2SEnji Cooper const char* const base64) {
21628f6c2f2SEnji Cooper return {
21728f6c2f2SEnji Cooper {UnBase64Impl(UndoWebSafeEncoding(static_cast<char>(I)), base64, 0)...}};
21828f6c2f2SEnji Cooper }
21928f6c2f2SEnji Cooper
UnBase64(const char * const base64)22028f6c2f2SEnji Cooper constexpr std::array<char, 256> UnBase64(const char* const base64) {
22128f6c2f2SEnji Cooper return UnBase64Impl(MakeIndexSequence<256>{}, base64);
22228f6c2f2SEnji Cooper }
22328f6c2f2SEnji Cooper
22428f6c2f2SEnji Cooper static constexpr char kBase64[] =
22528f6c2f2SEnji Cooper "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
22628f6c2f2SEnji Cooper static constexpr std::array<char, 256> kUnBase64 = UnBase64(kBase64);
22728f6c2f2SEnji Cooper
Base64Unescape(const std::string & encoded,std::string * decoded)22828f6c2f2SEnji Cooper bool Base64Unescape(const std::string& encoded, std::string* decoded) {
22928f6c2f2SEnji Cooper decoded->clear();
23028f6c2f2SEnji Cooper size_t encoded_len = encoded.size();
23128f6c2f2SEnji Cooper decoded->reserve(3 * (encoded_len / 4) + (encoded_len % 4));
23228f6c2f2SEnji Cooper int bit_pos = 0;
23328f6c2f2SEnji Cooper char dst = 0;
23428f6c2f2SEnji Cooper for (int src : encoded) {
23528f6c2f2SEnji Cooper if (std::isspace(src) || src == '=') {
23628f6c2f2SEnji Cooper continue;
23728f6c2f2SEnji Cooper }
23828f6c2f2SEnji Cooper char src_bin = kUnBase64[static_cast<size_t>(src)];
23928f6c2f2SEnji Cooper if (src_bin >= 64) {
24028f6c2f2SEnji Cooper decoded->clear();
24128f6c2f2SEnji Cooper return false;
24228f6c2f2SEnji Cooper }
24328f6c2f2SEnji Cooper if (bit_pos == 0) {
24428f6c2f2SEnji Cooper dst |= static_cast<char>(src_bin << 2);
24528f6c2f2SEnji Cooper bit_pos = 6;
24628f6c2f2SEnji Cooper } else {
24728f6c2f2SEnji Cooper dst |= static_cast<char>(src_bin >> (bit_pos - 2));
24828f6c2f2SEnji Cooper decoded->push_back(dst);
24928f6c2f2SEnji Cooper dst = static_cast<char>(src_bin << (10 - bit_pos));
25028f6c2f2SEnji Cooper bit_pos = (bit_pos + 6) % 8;
25128f6c2f2SEnji Cooper }
25228f6c2f2SEnji Cooper }
25328f6c2f2SEnji Cooper return true;
25428f6c2f2SEnji Cooper }
25528f6c2f2SEnji Cooper
256b89a7cc2SEnji Cooper } // namespace internal
257b89a7cc2SEnji Cooper } // namespace testing
258