blob: 77caf2bc21d2c5cbbd449ff0a39acb62a083b9a1 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// 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
zhanyong.wan53e08c42010-09-14 05:38:21 +000038#include "gmock/internal/gmock-internal-utils.h"
shiqiane35fdd92008-12-10 05:08:54 +000039
zhanyong.wance198ff2009-02-12 01:34:27 +000040#include <ctype.h>
shiqiane35fdd92008-12-10 05:08:54 +000041#include <ostream> // NOLINT
42#include <string>
zhanyong.wan53e08c42010-09-14 05:38:21 +000043#include "gmock/gmock.h"
44#include "gmock/internal/gmock-port.h"
45#include "gtest/gtest.h"
shiqiane35fdd92008-12-10 05:08:54 +000046
47namespace testing {
48namespace internal {
49
Gennadiy Civil225e6742018-02-12 16:42:12 -050050// Joins a vector of strings as if they are fields of a tuple; returns
51// the joined string.
52GTEST_API_ std::string JoinAsTuple(const Strings& fields) {
53 switch (fields.size()) {
54 case 0:
55 return "";
56 case 1:
57 return fields[0];
58 default:
59 std::string result = "(" + fields[0];
60 for (size_t i = 1; i < fields.size(); i++) {
61 result += ", ";
62 result += fields[i];
63 }
64 result += ")";
65 return result;
66 }
67}
68
zhanyong.wance198ff2009-02-12 01:34:27 +000069// Converts an identifier name to a space-separated list of lower-case
70// words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is
71// treated as one word. For example, both "FooBar123" and
72// "foo_bar_123" are converted to "foo bar 123".
Gennadiy Civildbf63e32018-03-05 13:28:13 -050073GTEST_API_ std::string ConvertIdentifierNameToWords(const char* id_name) {
74 std::string result;
zhanyong.wance198ff2009-02-12 01:34:27 +000075 char prev_char = '\0';
76 for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) {
77 // We don't care about the current locale as the input is
78 // guaranteed to be a valid C++ identifier name.
zhanyong.wan2516f602010-08-31 18:28:02 +000079 const bool starts_new_word = IsUpper(*p) ||
80 (!IsAlpha(prev_char) && IsLower(*p)) ||
81 (!IsDigit(prev_char) && IsDigit(*p));
zhanyong.wance198ff2009-02-12 01:34:27 +000082
zhanyong.wan2516f602010-08-31 18:28:02 +000083 if (IsAlNum(*p)) {
zhanyong.wance198ff2009-02-12 01:34:27 +000084 if (starts_new_word && result != "")
85 result += ' ';
zhanyong.wan2516f602010-08-31 18:28:02 +000086 result += ToLower(*p);
zhanyong.wance198ff2009-02-12 01:34:27 +000087 }
88 }
89 return result;
90}
91
shiqiane35fdd92008-12-10 05:08:54 +000092// This class reports Google Mock failures as Google Test failures. A
Jonathan Wakelyb70cf1a2017-09-27 13:31:13 +010093// user can define another class in a similar fashion if they intend to
shiqiane35fdd92008-12-10 05:08:54 +000094// use Google Mock with a testing framework other than Google Test.
95class GoogleTestFailureReporter : public FailureReporterInterface {
96 public:
97 virtual void ReportFailure(FailureType type, const char* file, int line,
Nico Weber09fd5b32017-05-15 17:07:03 -040098 const std::string& message) {
zhanyong.wan2fd619e2012-05-31 20:40:56 +000099 AssertHelper(type == kFatal ?
zhanyong.wanbbd6e102009-09-18 18:17:19 +0000100 TestPartResult::kFatalFailure :
101 TestPartResult::kNonFatalFailure,
102 file,
103 line,
104 message.c_str()) = Message();
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000105 if (type == kFatal) {
zhanyong.wan9571b282009-08-07 07:15:56 +0000106 posix::Abort();
shiqiane35fdd92008-12-10 05:08:54 +0000107 }
108 }
109};
110
111// Returns the global failure reporter. Will create a
112// GoogleTestFailureReporter and return it the first time called.
vladlosev587c1b32011-05-20 00:42:22 +0000113GTEST_API_ FailureReporterInterface* GetFailureReporter() {
shiqiane35fdd92008-12-10 05:08:54 +0000114 // Points to the global failure reporter used by Google Mock. gcc
115 // guarantees that the following use of failure_reporter is
116 // thread-safe. We may need to add additional synchronization to
117 // protect failure_reporter if we port Google Mock to other
118 // compilers.
119 static FailureReporterInterface* const failure_reporter =
120 new GoogleTestFailureReporter();
121 return failure_reporter;
122}
123
124// Protects global resources (stdout in particular) used by Log().
zhanyong.wan5905ba02010-02-24 17:21:37 +0000125static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex);
shiqiane35fdd92008-12-10 05:08:54 +0000126
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000127// Returns true iff a log with the given severity is visible according
128// to the --gmock_verbose flag.
vladlosev587c1b32011-05-20 00:42:22 +0000129GTEST_API_ bool LogIsVisible(LogSeverity severity) {
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000130 if (GMOCK_FLAG(verbose) == kInfoVerbosity) {
131 // Always show the log if --gmock_verbose=info.
132 return true;
133 } else if (GMOCK_FLAG(verbose) == kErrorVerbosity) {
134 // Always hide it if --gmock_verbose=error.
135 return false;
136 } else {
137 // If --gmock_verbose is neither "info" nor "error", we treat it
138 // as "warning" (its default value).
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000139 return severity == kWarning;
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000140 }
141}
142
shiqiane35fdd92008-12-10 05:08:54 +0000143// Prints the given message to stdout iff 'severity' >= the level
144// specified by the --gmock_verbose flag. If stack_frames_to_skip >=
145// 0, also prints the stack trace excluding the top
146// stack_frames_to_skip frames. In opt mode, any positive
147// stack_frames_to_skip is treated as 0, since we don't know which
148// function calls will be inlined by the compiler and need to be
149// conservative.
Nico Weber09fd5b32017-05-15 17:07:03 -0400150GTEST_API_ void Log(LogSeverity severity, const std::string& message,
vladlosev587c1b32011-05-20 00:42:22 +0000151 int stack_frames_to_skip) {
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000152 if (!LogIsVisible(severity))
shiqiane35fdd92008-12-10 05:08:54 +0000153 return;
shiqiane35fdd92008-12-10 05:08:54 +0000154
155 // Ensures that logs from different threads don't interleave.
156 MutexLock l(&g_log_mutex);
zhanyong.wanbf550852009-06-09 06:09:53 +0000157
158 // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is a
159 // macro.
160
zhanyong.wan2fd619e2012-05-31 20:40:56 +0000161 if (severity == kWarning) {
shiqiane35fdd92008-12-10 05:08:54 +0000162 // Prints a GMOCK WARNING marker to make the warnings easily searchable.
zhanyong.wanbf550852009-06-09 06:09:53 +0000163 std::cout << "\nGMOCK WARNING:";
shiqiane35fdd92008-12-10 05:08:54 +0000164 }
165 // Pre-pends a new-line to message if it doesn't start with one.
166 if (message.empty() || message[0] != '\n') {
zhanyong.wanbf550852009-06-09 06:09:53 +0000167 std::cout << "\n";
shiqiane35fdd92008-12-10 05:08:54 +0000168 }
zhanyong.wanbf550852009-06-09 06:09:53 +0000169 std::cout << message;
shiqiane35fdd92008-12-10 05:08:54 +0000170 if (stack_frames_to_skip >= 0) {
171#ifdef NDEBUG
172 // In opt mode, we have to be conservative and skip no stack frame.
173 const int actual_to_skip = 0;
174#else
175 // In dbg mode, we can do what the caller tell us to do (plus one
176 // for skipping this function's stack frame).
177 const int actual_to_skip = stack_frames_to_skip + 1;
178#endif // NDEBUG
179
180 // Appends a new-line to message if it doesn't end with one.
181 if (!message.empty() && *message.rbegin() != '\n') {
zhanyong.wanbf550852009-06-09 06:09:53 +0000182 std::cout << "\n";
shiqiane35fdd92008-12-10 05:08:54 +0000183 }
zhanyong.wanbf550852009-06-09 06:09:53 +0000184 std::cout << "Stack trace:\n"
shiqiane35fdd92008-12-10 05:08:54 +0000185 << ::testing::internal::GetCurrentOsStackTraceExceptTop(
186 ::testing::UnitTest::GetInstance(), actual_to_skip);
187 }
zhanyong.wanbf550852009-06-09 06:09:53 +0000188 std::cout << ::std::flush;
shiqiane35fdd92008-12-10 05:08:54 +0000189}
190
David Sunderlandd5725da2018-04-18 20:25:31 -0400191GTEST_API_ WithoutMatchers GetWithoutMatchers() { return WithoutMatchers(); }
David Sunderlandf437f8c2018-04-18 19:28:56 -0400192
Gennadiy Civil373481c2018-04-11 15:46:57 -0400193GTEST_API_ void IllegalDoDefault(const char* file, int line) {
Gennadiy Civildbf63e32018-03-05 13:28:13 -0500194 internal::Assert(
195 false, file, line,
196 "You are using DoDefault() inside a composite action like "
197 "DoAll() or WithArgs(). This is not supported for technical "
198 "reasons. Please instead spell out the default action, or "
199 "assign the default action to an Action variable and use "
200 "the variable in various places.");
201}
202
shiqiane35fdd92008-12-10 05:08:54 +0000203} // namespace internal
204} // namespace testing