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