2008-12-10 05:08:54 +00:00
|
|
|
// Copyright 2007, Google Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
// this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
// Author: wan@google.com (Zhanyong Wan)
|
|
|
|
|
|
|
|
// Google Mock - a framework for writing C++ mock classes.
|
|
|
|
//
|
|
|
|
// This file defines some utilities useful for implementing Google
|
|
|
|
// Mock. They are subject to change without notice, so please DO NOT
|
|
|
|
// USE THEM IN USER CODE.
|
|
|
|
|
2010-09-14 05:38:21 +00:00
|
|
|
#include "gmock/internal/gmock-internal-utils.h"
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2009-02-12 01:34:27 +00:00
|
|
|
#include <ctype.h>
|
2008-12-10 05:08:54 +00:00
|
|
|
#include <ostream> // NOLINT
|
|
|
|
#include <string>
|
2010-09-14 05:38:21 +00:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gmock/internal/gmock-port.h"
|
|
|
|
#include "gtest/gtest.h"
|
2008-12-10 05:08:54 +00:00
|
|
|
|
|
|
|
namespace testing {
|
|
|
|
namespace internal {
|
|
|
|
|
2009-02-12 01:34:27 +00:00
|
|
|
// Converts an identifier name to a space-separated list of lower-case
|
|
|
|
// words. Each maximum substring of the form [A-Za-z][a-z]*|\d+ is
|
|
|
|
// treated as one word. For example, both "FooBar123" and
|
|
|
|
// "foo_bar_123" are converted to "foo bar 123".
|
2011-05-20 00:42:22 +00:00
|
|
|
GTEST_API_ string ConvertIdentifierNameToWords(const char* id_name) {
|
2009-02-12 01:34:27 +00:00
|
|
|
string result;
|
|
|
|
char prev_char = '\0';
|
|
|
|
for (const char* p = id_name; *p != '\0'; prev_char = *(p++)) {
|
|
|
|
// We don't care about the current locale as the input is
|
|
|
|
// guaranteed to be a valid C++ identifier name.
|
2010-08-31 18:28:02 +00:00
|
|
|
const bool starts_new_word = IsUpper(*p) ||
|
|
|
|
(!IsAlpha(prev_char) && IsLower(*p)) ||
|
|
|
|
(!IsDigit(prev_char) && IsDigit(*p));
|
2009-02-12 01:34:27 +00:00
|
|
|
|
2010-08-31 18:28:02 +00:00
|
|
|
if (IsAlNum(*p)) {
|
2009-02-12 01:34:27 +00:00
|
|
|
if (starts_new_word && result != "")
|
|
|
|
result += ' ';
|
2010-08-31 18:28:02 +00:00
|
|
|
result += ToLower(*p);
|
2009-02-12 01:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// This class reports Google Mock failures as Google Test failures. A
|
|
|
|
// user can define another class in a similar fashion if he intends to
|
|
|
|
// use Google Mock with a testing framework other than Google Test.
|
|
|
|
class GoogleTestFailureReporter : public FailureReporterInterface {
|
|
|
|
public:
|
|
|
|
virtual void ReportFailure(FailureType type, const char* file, int line,
|
2017-05-15 21:07:03 +00:00
|
|
|
const std::string& message) {
|
2012-05-31 20:40:56 +00:00
|
|
|
AssertHelper(type == kFatal ?
|
2009-09-18 18:17:19 +00:00
|
|
|
TestPartResult::kFatalFailure :
|
|
|
|
TestPartResult::kNonFatalFailure,
|
|
|
|
file,
|
|
|
|
line,
|
|
|
|
message.c_str()) = Message();
|
2012-05-31 20:40:56 +00:00
|
|
|
if (type == kFatal) {
|
2009-08-07 07:15:56 +00:00
|
|
|
posix::Abort();
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns the global failure reporter. Will create a
|
|
|
|
// GoogleTestFailureReporter and return it the first time called.
|
2011-05-20 00:42:22 +00:00
|
|
|
GTEST_API_ FailureReporterInterface* GetFailureReporter() {
|
2008-12-10 05:08:54 +00:00
|
|
|
// Points to the global failure reporter used by Google Mock. gcc
|
|
|
|
// guarantees that the following use of failure_reporter is
|
|
|
|
// thread-safe. We may need to add additional synchronization to
|
|
|
|
// protect failure_reporter if we port Google Mock to other
|
|
|
|
// compilers.
|
|
|
|
static FailureReporterInterface* const failure_reporter =
|
|
|
|
new GoogleTestFailureReporter();
|
|
|
|
return failure_reporter;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protects global resources (stdout in particular) used by Log().
|
2010-02-24 17:21:37 +00:00
|
|
|
static GTEST_DEFINE_STATIC_MUTEX_(g_log_mutex);
|
2008-12-10 05:08:54 +00:00
|
|
|
|
2009-05-29 19:50:06 +00:00
|
|
|
// Returns true iff a log with the given severity is visible according
|
|
|
|
// to the --gmock_verbose flag.
|
2011-05-20 00:42:22 +00:00
|
|
|
GTEST_API_ bool LogIsVisible(LogSeverity severity) {
|
2009-05-29 19:50:06 +00:00
|
|
|
if (GMOCK_FLAG(verbose) == kInfoVerbosity) {
|
|
|
|
// Always show the log if --gmock_verbose=info.
|
|
|
|
return true;
|
|
|
|
} else if (GMOCK_FLAG(verbose) == kErrorVerbosity) {
|
|
|
|
// Always hide it if --gmock_verbose=error.
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// If --gmock_verbose is neither "info" nor "error", we treat it
|
|
|
|
// as "warning" (its default value).
|
2012-05-31 20:40:56 +00:00
|
|
|
return severity == kWarning;
|
2009-05-29 19:50:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-10 05:08:54 +00:00
|
|
|
// Prints the given message to stdout iff 'severity' >= the level
|
|
|
|
// specified by the --gmock_verbose flag. If stack_frames_to_skip >=
|
|
|
|
// 0, also prints the stack trace excluding the top
|
|
|
|
// stack_frames_to_skip frames. In opt mode, any positive
|
|
|
|
// stack_frames_to_skip is treated as 0, since we don't know which
|
|
|
|
// function calls will be inlined by the compiler and need to be
|
|
|
|
// conservative.
|
2017-05-15 21:07:03 +00:00
|
|
|
GTEST_API_ void Log(LogSeverity severity, const std::string& message,
|
2011-05-20 00:42:22 +00:00
|
|
|
int stack_frames_to_skip) {
|
2009-05-29 19:50:06 +00:00
|
|
|
if (!LogIsVisible(severity))
|
2008-12-10 05:08:54 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Ensures that logs from different threads don't interleave.
|
|
|
|
MutexLock l(&g_log_mutex);
|
2009-06-09 06:09:53 +00:00
|
|
|
|
|
|
|
// "using ::std::cout;" doesn't work with Symbian's STLport, where cout is a
|
|
|
|
// macro.
|
|
|
|
|
2012-05-31 20:40:56 +00:00
|
|
|
if (severity == kWarning) {
|
2008-12-10 05:08:54 +00:00
|
|
|
// Prints a GMOCK WARNING marker to make the warnings easily searchable.
|
2009-06-09 06:09:53 +00:00
|
|
|
std::cout << "\nGMOCK WARNING:";
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
// Pre-pends a new-line to message if it doesn't start with one.
|
|
|
|
if (message.empty() || message[0] != '\n') {
|
2009-06-09 06:09:53 +00:00
|
|
|
std::cout << "\n";
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
2009-06-09 06:09:53 +00:00
|
|
|
std::cout << message;
|
2008-12-10 05:08:54 +00:00
|
|
|
if (stack_frames_to_skip >= 0) {
|
|
|
|
#ifdef NDEBUG
|
|
|
|
// In opt mode, we have to be conservative and skip no stack frame.
|
|
|
|
const int actual_to_skip = 0;
|
|
|
|
#else
|
|
|
|
// In dbg mode, we can do what the caller tell us to do (plus one
|
|
|
|
// for skipping this function's stack frame).
|
|
|
|
const int actual_to_skip = stack_frames_to_skip + 1;
|
|
|
|
#endif // NDEBUG
|
|
|
|
|
|
|
|
// Appends a new-line to message if it doesn't end with one.
|
|
|
|
if (!message.empty() && *message.rbegin() != '\n') {
|
2009-06-09 06:09:53 +00:00
|
|
|
std::cout << "\n";
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
2009-06-09 06:09:53 +00:00
|
|
|
std::cout << "Stack trace:\n"
|
2008-12-10 05:08:54 +00:00
|
|
|
<< ::testing::internal::GetCurrentOsStackTraceExceptTop(
|
|
|
|
::testing::UnitTest::GetInstance(), actual_to_skip);
|
|
|
|
}
|
2009-06-09 06:09:53 +00:00
|
|
|
std::cout << ::std::flush;
|
2008-12-10 05:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace testing
|