Blame googlemock/src/gmock-internal-utils.cc

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