Blame googlemock/src/gmock.cc

Packit bd1cd8
// Copyright 2008, 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
#include "gmock/gmock.h"
Packit bd1cd8
#include "gmock/internal/gmock-port.h"
Packit bd1cd8
Packit bd1cd8
namespace testing {
Packit bd1cd8
Packit bd1cd8
// TODO(wan@google.com): support using environment variables to
Packit bd1cd8
// control the flag values, like what Google Test does.
Packit bd1cd8
Packit bd1cd8
GMOCK_DEFINE_bool_(catch_leaked_mocks, true,
Packit bd1cd8
                   "true iff Google Mock should report leaked mock objects "
Packit bd1cd8
                   "as failures.");
Packit bd1cd8
Packit bd1cd8
GMOCK_DEFINE_string_(verbose, internal::kWarningVerbosity,
Packit bd1cd8
                     "Controls how verbose Google Mock's output is."
Packit bd1cd8
                     "  Valid values:\n"
Packit bd1cd8
                     "  info    - prints all messages.\n"
Packit bd1cd8
                     "  warning - prints warnings and errors.\n"
Packit bd1cd8
                     "  error   - prints errors only.");
Packit bd1cd8
Packit bd1cd8
namespace internal {
Packit bd1cd8
Packit bd1cd8
// Parses a string as a command line flag.  The string should have the
Packit bd1cd8
// format "--gmock_flag=value".  When def_optional is true, the
Packit bd1cd8
// "=value" part can be omitted.
Packit bd1cd8
//
Packit bd1cd8
// Returns the value of the flag, or NULL if the parsing failed.
Packit bd1cd8
static const char* ParseGoogleMockFlagValue(const char* str,
Packit bd1cd8
                                            const char* flag,
Packit bd1cd8
                                            bool def_optional) {
Packit bd1cd8
  // str and flag must not be NULL.
Packit bd1cd8
  if (str == NULL || flag == NULL) return NULL;
Packit bd1cd8
Packit bd1cd8
  // The flag must start with "--gmock_".
Packit bd1cd8
  const std::string flag_str = std::string("--gmock_") + flag;
Packit bd1cd8
  const size_t flag_len = flag_str.length();
Packit bd1cd8
  if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL;
Packit bd1cd8
Packit bd1cd8
  // Skips the flag name.
Packit bd1cd8
  const char* flag_end = str + flag_len;
Packit bd1cd8
Packit bd1cd8
  // When def_optional is true, it's OK to not have a "=value" part.
Packit bd1cd8
  if (def_optional && (flag_end[0] == '\0')) {
Packit bd1cd8
    return flag_end;
Packit bd1cd8
  }
Packit bd1cd8
Packit bd1cd8
  // If def_optional is true and there are more characters after the
Packit bd1cd8
  // flag name, or if def_optional is false, there must be a '=' after
Packit bd1cd8
  // the flag name.
Packit bd1cd8
  if (flag_end[0] != '=') return NULL;
Packit bd1cd8
Packit bd1cd8
  // Returns the string after "=".
Packit bd1cd8
  return flag_end + 1;
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Parses a string for a Google Mock bool flag, in the form of
Packit bd1cd8
// "--gmock_flag=value".
Packit bd1cd8
//
Packit bd1cd8
// On success, stores the value of the flag in *value, and returns
Packit bd1cd8
// true.  On failure, returns false without changing *value.
Packit bd1cd8
static bool ParseGoogleMockBoolFlag(const char* str, const char* flag,
Packit bd1cd8
                                    bool* value) {
Packit bd1cd8
  // Gets the value of the flag as a string.
Packit bd1cd8
  const char* const value_str = ParseGoogleMockFlagValue(str, flag, true);
Packit bd1cd8
Packit bd1cd8
  // Aborts if the parsing failed.
Packit bd1cd8
  if (value_str == NULL) return false;
Packit bd1cd8
Packit bd1cd8
  // Converts the string value to a bool.
Packit bd1cd8
  *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
Packit bd1cd8
  return true;
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Parses a string for a Google Mock string flag, in the form of
Packit bd1cd8
// "--gmock_flag=value".
Packit bd1cd8
//
Packit bd1cd8
// On success, stores the value of the flag in *value, and returns
Packit bd1cd8
// true.  On failure, returns false without changing *value.
Packit bd1cd8
template <typename String>
Packit bd1cd8
static bool ParseGoogleMockStringFlag(const char* str, const char* flag,
Packit bd1cd8
                                      String* value) {
Packit bd1cd8
  // Gets the value of the flag as a string.
Packit bd1cd8
  const char* const value_str = ParseGoogleMockFlagValue(str, flag, false);
Packit bd1cd8
Packit bd1cd8
  // Aborts if the parsing failed.
Packit bd1cd8
  if (value_str == NULL) return false;
Packit bd1cd8
Packit bd1cd8
  // Sets *value to the value of the flag.
Packit bd1cd8
  *value = value_str;
Packit bd1cd8
  return true;
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// The internal implementation of InitGoogleMock().
Packit bd1cd8
//
Packit bd1cd8
// The type parameter CharType can be instantiated to either char or
Packit bd1cd8
// wchar_t.
Packit bd1cd8
template <typename CharType>
Packit bd1cd8
void InitGoogleMockImpl(int* argc, CharType** argv) {
Packit bd1cd8
  // Makes sure Google Test is initialized.  InitGoogleTest() is
Packit bd1cd8
  // idempotent, so it's fine if the user has already called it.
Packit bd1cd8
  InitGoogleTest(argc, argv);
Packit bd1cd8
  if (*argc <= 0) return;
Packit bd1cd8
Packit bd1cd8
  for (int i = 1; i != *argc; i++) {
Packit bd1cd8
    const std::string arg_string = StreamableToString(argv[i]);
Packit bd1cd8
    const char* const arg = arg_string.c_str();
Packit bd1cd8
Packit bd1cd8
    // Do we see a Google Mock flag?
Packit bd1cd8
    if (ParseGoogleMockBoolFlag(arg, "catch_leaked_mocks",
Packit bd1cd8
                                &GMOCK_FLAG(catch_leaked_mocks)) ||
Packit bd1cd8
        ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose))) {
Packit bd1cd8
      // Yes.  Shift the remainder of the argv list left by one.  Note
Packit bd1cd8
      // that argv has (*argc + 1) elements, the last one always being
Packit bd1cd8
      // NULL.  The following loop moves the trailing NULL element as
Packit bd1cd8
      // well.
Packit bd1cd8
      for (int j = i; j != *argc; j++) {
Packit bd1cd8
        argv[j] = argv[j + 1];
Packit bd1cd8
      }
Packit bd1cd8
Packit bd1cd8
      // Decrements the argument count.
Packit bd1cd8
      (*argc)--;
Packit bd1cd8
Packit bd1cd8
      // We also need to decrement the iterator as we just removed
Packit bd1cd8
      // an element.
Packit bd1cd8
      i--;
Packit bd1cd8
    }
Packit bd1cd8
  }
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
}  // namespace internal
Packit bd1cd8
Packit bd1cd8
// Initializes Google Mock.  This must be called before running the
Packit bd1cd8
// tests.  In particular, it parses a command line for the flags that
Packit bd1cd8
// Google Mock recognizes.  Whenever a Google Mock flag is seen, it is
Packit bd1cd8
// removed from argv, and *argc is decremented.
Packit bd1cd8
//
Packit bd1cd8
// No value is returned.  Instead, the Google Mock flag variables are
Packit bd1cd8
// updated.
Packit bd1cd8
//
Packit bd1cd8
// Since Google Test is needed for Google Mock to work, this function
Packit bd1cd8
// also initializes Google Test and parses its flags, if that hasn't
Packit bd1cd8
// been done.
Packit bd1cd8
GTEST_API_ void InitGoogleMock(int* argc, char** argv) {
Packit bd1cd8
  internal::InitGoogleMockImpl(argc, argv);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// This overloaded version can be used in Windows programs compiled in
Packit bd1cd8
// UNICODE mode.
Packit bd1cd8
GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) {
Packit bd1cd8
  internal::InitGoogleMockImpl(argc, argv);
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
}  // namespace testing