blob: 0e693c7081e11e0f098b6a4c6f420dd991cee8d9 [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
38#include <gmock/internal/gmock-internal-utils.h>
39
zhanyong.wance198ff2009-02-12 01:34:27 +000040#include <ctype.h>
shiqiane35fdd92008-12-10 05:08:54 +000041#include <ostream> // NOLINT
42#include <string>
43#include <gmock/gmock.h>
44#include <gmock/internal/gmock-port.h>
45#include <gtest/gtest.h>
46
47namespace testing {
48namespace internal {
49
zhanyong.wance198ff2009-02-12 01:34:27 +000050// 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".
54string ConvertIdentifierNameToWords(const char* id_name) {
55 string result;
56 char prev_char = '\0';
57 for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) {
58 // We don't care about the current locale as the input is
59 // guaranteed to be a valid C++ identifier name.
60 const bool starts_new_word = isupper(*p) ||
61 (!isalpha(prev_char) && islower(*p)) ||
62 (!isdigit(prev_char) && isdigit(*p));
63
64 if (isalnum(*p)) {
65 if (starts_new_word && result != "")
66 result += ' ';
67 result += tolower(*p);
68 }
69 }
70 return result;
71}
72
shiqiane35fdd92008-12-10 05:08:54 +000073// 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.
76class GoogleTestFailureReporter : public FailureReporterInterface {
77 public:
78 virtual void ReportFailure(FailureType type, const char* file, int line,
79 const string& message) {
80 AssertHelper(type == FATAL ? TPRT_FATAL_FAILURE : TPRT_NONFATAL_FAILURE,
81 file, line, message.c_str()) = Message();
82 if (type == FATAL) {
83 abort();
84 }
85 }
86};
87
88// Returns the global failure reporter. Will create a
89// GoogleTestFailureReporter and return it the first time called.
90FailureReporterInterface* GetFailureReporter() {
91 // Points to the global failure reporter used by Google Mock. gcc
92 // guarantees that the following use of failure_reporter is
93 // thread-safe. We may need to add additional synchronization to
94 // protect failure_reporter if we port Google Mock to other
95 // compilers.
96 static FailureReporterInterface* const failure_reporter =
97 new GoogleTestFailureReporter();
98 return failure_reporter;
99}
100
101// Protects global resources (stdout in particular) used by Log().
102static Mutex g_log_mutex(Mutex::NO_CONSTRUCTOR_NEEDED_FOR_STATIC_MUTEX);
103
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000104// Returns true iff a log with the given severity is visible according
105// to the --gmock_verbose flag.
106bool LogIsVisible(LogSeverity severity) {
107 if (GMOCK_FLAG(verbose) == kInfoVerbosity) {
108 // Always show the log if --gmock_verbose=info.
109 return true;
110 } else if (GMOCK_FLAG(verbose) == kErrorVerbosity) {
111 // Always hide it if --gmock_verbose=error.
112 return false;
113 } else {
114 // If --gmock_verbose is neither "info" nor "error", we treat it
115 // as "warning" (its default value).
116 return severity == WARNING;
117 }
118}
119
shiqiane35fdd92008-12-10 05:08:54 +0000120// Prints the given message to stdout iff 'severity' >= the level
121// specified by the --gmock_verbose flag. If stack_frames_to_skip >=
122// 0, also prints the stack trace excluding the top
123// stack_frames_to_skip frames. In opt mode, any positive
124// stack_frames_to_skip is treated as 0, since we don't know which
125// function calls will be inlined by the compiler and need to be
126// conservative.
127void Log(LogSeverity severity, const string& message,
128 int stack_frames_to_skip) {
zhanyong.wan9413f2f2009-05-29 19:50:06 +0000129 if (!LogIsVisible(severity))
shiqiane35fdd92008-12-10 05:08:54 +0000130 return;
shiqiane35fdd92008-12-10 05:08:54 +0000131
132 // Ensures that logs from different threads don't interleave.
133 MutexLock l(&g_log_mutex);
zhanyong.wanbf550852009-06-09 06:09:53 +0000134
135 // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is a
136 // macro.
137
shiqiane35fdd92008-12-10 05:08:54 +0000138 if (severity == WARNING) {
139 // Prints a GMOCK WARNING marker to make the warnings easily searchable.
zhanyong.wanbf550852009-06-09 06:09:53 +0000140 std::cout << "\nGMOCK WARNING:";
shiqiane35fdd92008-12-10 05:08:54 +0000141 }
142 // Pre-pends a new-line to message if it doesn't start with one.
143 if (message.empty() || message[0] != '\n') {
zhanyong.wanbf550852009-06-09 06:09:53 +0000144 std::cout << "\n";
shiqiane35fdd92008-12-10 05:08:54 +0000145 }
zhanyong.wanbf550852009-06-09 06:09:53 +0000146 std::cout << message;
shiqiane35fdd92008-12-10 05:08:54 +0000147 if (stack_frames_to_skip >= 0) {
148#ifdef NDEBUG
149 // In opt mode, we have to be conservative and skip no stack frame.
150 const int actual_to_skip = 0;
151#else
152 // In dbg mode, we can do what the caller tell us to do (plus one
153 // for skipping this function's stack frame).
154 const int actual_to_skip = stack_frames_to_skip + 1;
155#endif // NDEBUG
156
157 // Appends a new-line to message if it doesn't end with one.
158 if (!message.empty() && *message.rbegin() != '\n') {
zhanyong.wanbf550852009-06-09 06:09:53 +0000159 std::cout << "\n";
shiqiane35fdd92008-12-10 05:08:54 +0000160 }
zhanyong.wanbf550852009-06-09 06:09:53 +0000161 std::cout << "Stack trace:\n"
shiqiane35fdd92008-12-10 05:08:54 +0000162 << ::testing::internal::GetCurrentOsStackTraceExceptTop(
163 ::testing::UnitTest::GetInstance(), actual_to_skip);
164 }
zhanyong.wanbf550852009-06-09 06:09:53 +0000165 std::cout << ::std::flush;
shiqiane35fdd92008-12-10 05:08:54 +0000166}
167
168} // namespace internal
169} // namespace testing