Path: blob/main/contrib/googletest/googlemock/src/gmock.cc
48254 views
// Copyright 2008, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829#include "gmock/gmock.h"3031#include <string>3233#include "gmock/internal/gmock-port.h"3435GMOCK_DEFINE_bool_(catch_leaked_mocks, true,36"true if and only if Google Mock should report leaked "37"mock objects as failures.");3839GMOCK_DEFINE_string_(verbose, testing::internal::kWarningVerbosity,40"Controls how verbose Google Mock's output is."41" Valid values:\n"42" info - prints all messages.\n"43" warning - prints warnings and errors.\n"44" error - prints errors only.");4546GMOCK_DEFINE_int32_(default_mock_behavior, 1,47"Controls the default behavior of mocks."48" Valid values:\n"49" 0 - by default, mocks act as NiceMocks.\n"50" 1 - by default, mocks act as NaggyMocks.\n"51" 2 - by default, mocks act as StrictMocks.");5253namespace testing {54namespace internal {5556// Parses a string as a command line flag. The string should have the57// format "--gmock_flag=value". When def_optional is true, the58// "=value" part can be omitted.59//60// Returns the value of the flag, or NULL if the parsing failed.61static const char* ParseGoogleMockFlagValue(const char* str,62const char* flag_name,63bool def_optional) {64// str and flag must not be NULL.65if (str == nullptr || flag_name == nullptr) return nullptr;6667// The flag must start with "--gmock_".68const std::string flag_name_str = std::string("--gmock_") + flag_name;69const size_t flag_name_len = flag_name_str.length();70if (strncmp(str, flag_name_str.c_str(), flag_name_len) != 0) return nullptr;7172// Skips the flag name.73const char* flag_end = str + flag_name_len;7475// When def_optional is true, it's OK to not have a "=value" part.76if (def_optional && (flag_end[0] == '\0')) {77return flag_end;78}7980// If def_optional is true and there are more characters after the81// flag name, or if def_optional is false, there must be a '=' after82// the flag name.83if (flag_end[0] != '=') return nullptr;8485// Returns the string after "=".86return flag_end + 1;87}8889// Parses a string for a Google Mock bool flag, in the form of90// "--gmock_flag=value".91//92// On success, stores the value of the flag in *value, and returns93// true. On failure, returns false without changing *value.94static bool ParseGoogleMockFlag(const char* str, const char* flag_name,95bool* value) {96// Gets the value of the flag as a string.97const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, true);9899// Aborts if the parsing failed.100if (value_str == nullptr) return false;101102// Converts the string value to a bool.103*value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');104return true;105}106107// Parses a string for a Google Mock string flag, in the form of108// "--gmock_flag=value".109//110// On success, stores the value of the flag in *value, and returns111// true. On failure, returns false without changing *value.112template <typename String>113static bool ParseGoogleMockFlag(const char* str, const char* flag_name,114String* value) {115// Gets the value of the flag as a string.116const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, false);117118// Aborts if the parsing failed.119if (value_str == nullptr) return false;120121// Sets *value to the value of the flag.122*value = value_str;123return true;124}125126static bool ParseGoogleMockFlag(const char* str, const char* flag_name,127int32_t* value) {128// Gets the value of the flag as a string.129const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, true);130131// Aborts if the parsing failed.132if (value_str == nullptr) return false;133134// Sets *value to the value of the flag.135return ParseInt32(Message() << "The value of flag --" << flag_name, value_str,136value);137}138139// The internal implementation of InitGoogleMock().140//141// The type parameter CharType can be instantiated to either char or142// wchar_t.143template <typename CharType>144void InitGoogleMockImpl(int* argc, CharType** argv) {145// Makes sure Google Test is initialized. InitGoogleTest() is146// idempotent, so it's fine if the user has already called it.147InitGoogleTest(argc, argv);148if (*argc <= 0) return;149150for (int i = 1; i != *argc; i++) {151const std::string arg_string = StreamableToString(argv[i]);152const char* const arg = arg_string.c_str();153154// Do we see a Google Mock flag?155bool found_gmock_flag = false;156157#define GMOCK_INTERNAL_PARSE_FLAG(flag_name) \158if (!found_gmock_flag) { \159auto value = GMOCK_FLAG_GET(flag_name); \160if (ParseGoogleMockFlag(arg, #flag_name, &value)) { \161GMOCK_FLAG_SET(flag_name, value); \162found_gmock_flag = true; \163} \164}165166GMOCK_INTERNAL_PARSE_FLAG(catch_leaked_mocks)167GMOCK_INTERNAL_PARSE_FLAG(verbose)168GMOCK_INTERNAL_PARSE_FLAG(default_mock_behavior)169170if (found_gmock_flag) {171// Yes. Shift the remainder of the argv list left by one. Note172// that argv has (*argc + 1) elements, the last one always being173// NULL. The following loop moves the trailing NULL element as174// well.175for (int j = i; j != *argc; j++) {176argv[j] = argv[j + 1];177}178179// Decrements the argument count.180(*argc)--;181182// We also need to decrement the iterator as we just removed183// an element.184i--;185}186}187}188189} // namespace internal190191// Initializes Google Mock. This must be called before running the192// tests. In particular, it parses a command line for the flags that193// Google Mock recognizes. Whenever a Google Mock flag is seen, it is194// removed from argv, and *argc is decremented.195//196// No value is returned. Instead, the Google Mock flag variables are197// updated.198//199// Since Google Test is needed for Google Mock to work, this function200// also initializes Google Test and parses its flags, if that hasn't201// been done.202GTEST_API_ void InitGoogleMock(int* argc, char** argv) {203internal::InitGoogleMockImpl(argc, argv);204}205206// This overloaded version can be used in Windows programs compiled in207// UNICODE mode.208GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) {209internal::InitGoogleMockImpl(argc, argv);210}211212// This overloaded version can be used on Arduino/embedded platforms where213// there is no argc/argv.214GTEST_API_ void InitGoogleMock() {215// Since Arduino doesn't have a command line, fake out the argc/argv arguments216int argc = 1;217const auto arg0 = "dummy";218char* argv0 = const_cast<char*>(arg0);219char** argv = &argv0;220221internal::InitGoogleMockImpl(&argc, argv);222}223224} // namespace testing225226227