/proc/self/cwd/external/com_google_googletest/googlemock/src/gmock-internal-utils.cc
Line | Count | Source (jump to first uncovered line) |
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 | | |
31 | | // Google Mock - a framework for writing C++ mock classes. |
32 | | // |
33 | | // This file defines some utilities useful for implementing Google |
34 | | // Mock. They are subject to change without notice, so please DO NOT |
35 | | // USE THEM IN USER CODE. |
36 | | |
37 | | #include "gmock/internal/gmock-internal-utils.h" |
38 | | |
39 | | #include <ctype.h> |
40 | | #include <ostream> // NOLINT |
41 | | #include <string> |
42 | | #include "gmock/gmock.h" |
43 | | #include "gmock/internal/gmock-port.h" |
44 | | #include "gtest/gtest.h" |
45 | | |
46 | | namespace testing { |
47 | | namespace internal { |
48 | | |
49 | | // Joins a vector of strings as if they are fields of a tuple; returns |
50 | | // the joined string. |
51 | 0 | GTEST_API_ std::string JoinAsTuple(const Strings& fields) { |
52 | 0 | switch (fields.size()) { |
53 | 0 | case 0: |
54 | 0 | return ""; |
55 | 0 | case 1: |
56 | 0 | return fields[0]; |
57 | 0 | default: |
58 | 0 | std::string result = "(" + fields[0]; |
59 | 0 | for (size_t i = 1; i < fields.size(); i++) { |
60 | 0 | result += ", "; |
61 | 0 | result += fields[i]; |
62 | 0 | } |
63 | 0 | result += ")"; |
64 | 0 | return result; |
65 | 0 | } |
66 | 0 | } |
67 | | |
68 | | // Converts an identifier name to a space-separated list of lower-case |
69 | | // words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is |
70 | | // treated as one word. For example, both "FooBar123" and |
71 | | // "foo_bar_123" are converted to "foo bar 123". |
72 | 0 | GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name) { |
73 | 0 | std::string result; |
74 | 0 | char prev_char = '\0'; |
75 | 0 | for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) { |
76 | | // We don't care about the current locale as the input is |
77 | | // guaranteed to be a valid C++ identifier name. |
78 | 0 | const bool starts_new_word = IsUpper(*p) || |
79 | 0 | (!IsAlpha(prev_char) && IsLower(*p)) || |
80 | 0 | (!IsDigit(prev_char) && IsDigit(*p)); |
81 | |
|
82 | 0 | if (IsAlNum(*p)) { |
83 | 0 | if (starts_new_word && result != "") |
84 | 0 | result += ' '; |
85 | 0 | result += ToLower(*p); |
86 | 0 | } |
87 | 0 | } |
88 | 0 | return result; |
89 | 0 | } |
90 | | |
91 | | // This class reports Google Mock failures as Google Test failures. A |
92 | | // user can define another class in a similar fashion if they intend to |
93 | | // use Google Mock with a testing framework other than Google Test. |
94 | | class GoogleTestFailureReporter : public FailureReporterInterface { |
95 | | public: |
96 | | void ReportFailure(FailureType type, const char* file, int line, |
97 | 9.81k | const std::string& message) override { |
98 | 9.81k | AssertHelper(type == kFatal ? |
99 | 0 | TestPartResult::kFatalFailure : |
100 | 9.81k | TestPartResult::kNonFatalFailure, |
101 | 9.81k | file, |
102 | 9.81k | line, |
103 | 9.81k | message.c_str()) = Message(); |
104 | 9.81k | if (type == kFatal) { |
105 | 0 | posix::Abort(); |
106 | 0 | } |
107 | 9.81k | } |
108 | | }; |
109 | | |
110 | | // Returns the global failure reporter. Will create a |
111 | | // GoogleTestFailureReporter and return it the first time called. |
112 | 9.81k | GTEST_API_ FailureReporterInterface* GetFailureReporter() { |
113 | | // Points to the global failure reporter used by Google Mock. gcc |
114 | | // guarantees that the following use of failure_reporter is |
115 | | // thread-safe. We may need to add additional synchronization to |
116 | | // protect failure_reporter if we port Google Mock to other |
117 | | // compilers. |
118 | 9.81k | static FailureReporterInterface* const failure_reporter = |
119 | 9.81k | new GoogleTestFailureReporter(); |
120 | 9.81k | return failure_reporter; |
121 | 9.81k | } |
122 | | |
123 | | // Protects global resources (stdout in particular) used by Log(). |
124 | | static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex); |
125 | | |
126 | | // Returns true if and only if a log with the given severity is visible |
127 | | // according to the --gmock_verbose flag. |
128 | 99.0M | GTEST_API_ bool LogIsVisible(LogSeverity severity) { |
129 | 99.0M | if (GMOCK_FLAG(verbose) == kInfoVerbosity) { |
130 | | // Always show the log if --gmock_verbose=info. |
131 | 0 | return true; |
132 | 99.0M | } else if (GMOCK_FLAG(verbose) == kErrorVerbosity) { |
133 | | // Always hide it if --gmock_verbose=error. |
134 | 99.0M | return false; |
135 | 99.0M | } else { |
136 | | // If --gmock_verbose is neither "info" nor "error", we treat it |
137 | | // as "warning" (its default value). |
138 | 0 | return severity == kWarning; |
139 | 0 | } |
140 | 99.0M | } |
141 | | |
142 | | // Prints the given message to stdout if and only if 'severity' >= the level |
143 | | // specified by the --gmock_verbose flag. If stack_frames_to_skip >= |
144 | | // 0, also prints the stack trace excluding the top |
145 | | // stack_frames_to_skip frames. In opt mode, any positive |
146 | | // stack_frames_to_skip is treated as 0, since we don't know which |
147 | | // function calls will be inlined by the compiler and need to be |
148 | | // conservative. |
149 | | GTEST_API_ void Log(LogSeverity severity, const std::string& message, |
150 | 59.6M | int stack_frames_to_skip) { |
151 | 59.6M | if (!LogIsVisible(severity)) |
152 | 59.6M | return; |
153 | | |
154 | | // Ensures that logs from different threads don't interleave. |
155 | 0 | MutexLock l(&g_log_mutex); |
156 | |
|
157 | 0 | if (severity == kWarning) { |
158 | | // Prints a GMOCK WARNING marker to make the warnings easily searchable. |
159 | 0 | std::cout << "\nGMOCK WARNING:"; |
160 | 0 | } |
161 | | // Pre-pends a new-line to message if it doesn't start with one. |
162 | 0 | if (message.empty() || message[0] != '\n') { |
163 | 0 | std::cout << "\n"; |
164 | 0 | } |
165 | 0 | std::cout << message; |
166 | 0 | if (stack_frames_to_skip >= 0) { |
167 | | #ifdef NDEBUG |
168 | | // In opt mode, we have to be conservative and skip no stack frame. |
169 | | const int actual_to_skip = 0; |
170 | | #else |
171 | | // In dbg mode, we can do what the caller tell us to do (plus one |
172 | | // for skipping this function's stack frame). |
173 | 0 | const int actual_to_skip = stack_frames_to_skip + 1; |
174 | 0 | #endif // NDEBUG |
175 | | |
176 | | // Appends a new-line to message if it doesn't end with one. |
177 | 0 | if (!message.empty() && *message.rbegin() != '\n') { |
178 | 0 | std::cout << "\n"; |
179 | 0 | } |
180 | 0 | std::cout << "Stack trace:\n" |
181 | 0 | << ::testing::internal::GetCurrentOsStackTraceExceptTop( |
182 | 0 | ::testing::UnitTest::GetInstance(), actual_to_skip); |
183 | 0 | } |
184 | 0 | std::cout << ::std::flush; |
185 | 0 | } |
186 | | |
187 | 59.6M | GTEST_API_ WithoutMatchers GetWithoutMatchers() { return WithoutMatchers(); } |
188 | | |
189 | 0 | GTEST_API_ void IllegalDoDefault(const char* file, int line) { |
190 | 0 | internal::Assert( |
191 | 0 | false, file, line, |
192 | 0 | "You are using DoDefault() inside a composite action like " |
193 | 0 | "DoAll() or WithArgs(). This is not supported for technical " |
194 | 0 | "reasons. Please instead spell out the default action, or " |
195 | 0 | "assign the default action to an Action variable and use " |
196 | 0 | "the variable in various places."); |
197 | 0 | } |
198 | | |
199 | | } // namespace internal |
200 | | } // namespace testing |