![]() |
|
|||
File indexing completed on 2025-08-06 08:19:54
0001 // Copyright 2008, Google Inc. 0002 // All rights reserved. 0003 // 0004 // Redistribution and use in source and binary forms, with or without 0005 // modification, are permitted provided that the following conditions are 0006 // met: 0007 // 0008 // * Redistributions of source code must retain the above copyright 0009 // notice, this list of conditions and the following disclaimer. 0010 // * Redistributions in binary form must reproduce the above 0011 // copyright notice, this list of conditions and the following disclaimer 0012 // in the documentation and/or other materials provided with the 0013 // distribution. 0014 // * Neither the name of Google Inc. nor the names of its 0015 // contributors may be used to endorse or promote products derived from 0016 // this software without specific prior written permission. 0017 // 0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0029 // 0030 // Author: wan@google.com (Zhanyong Wan) 0031 0032 #include "gmock/gmock.h" 0033 #include "gmock/internal/gmock-port.h" 0034 0035 namespace testing { 0036 0037 // TODO(wan@google.com): support using environment variables to 0038 // control the flag values, like what Google Test does. 0039 0040 GMOCK_DEFINE_bool_(catch_leaked_mocks, true, 0041 "true iff Google Mock should report leaked mock objects " 0042 "as failures."); 0043 0044 GMOCK_DEFINE_string_(verbose, internal::kWarningVerbosity, 0045 "Controls how verbose Google Mock's output is." 0046 " Valid values:\n" 0047 " info - prints all messages.\n" 0048 " warning - prints warnings and errors.\n" 0049 " error - prints errors only."); 0050 0051 namespace internal { 0052 0053 // Parses a string as a command line flag. The string should have the 0054 // format "--gmock_flag=value". When def_optional is true, the 0055 // "=value" part can be omitted. 0056 // 0057 // Returns the value of the flag, or NULL if the parsing failed. 0058 static const char* ParseGoogleMockFlagValue(const char* str, 0059 const char* flag, 0060 bool def_optional) { 0061 // str and flag must not be NULL. 0062 if (str == NULL || flag == NULL) return NULL; 0063 0064 // The flag must start with "--gmock_". 0065 const std::string flag_str = std::string("--gmock_") + flag; 0066 const size_t flag_len = flag_str.length(); 0067 if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL; 0068 0069 // Skips the flag name. 0070 const char* flag_end = str + flag_len; 0071 0072 // When def_optional is true, it's OK to not have a "=value" part. 0073 if (def_optional && (flag_end[0] == '\0')) { 0074 return flag_end; 0075 } 0076 0077 // If def_optional is true and there are more characters after the 0078 // flag name, or if def_optional is false, there must be a '=' after 0079 // the flag name. 0080 if (flag_end[0] != '=') return NULL; 0081 0082 // Returns the string after "=". 0083 return flag_end + 1; 0084 } 0085 0086 // Parses a string for a Google Mock bool flag, in the form of 0087 // "--gmock_flag=value". 0088 // 0089 // On success, stores the value of the flag in *value, and returns 0090 // true. On failure, returns false without changing *value. 0091 static bool ParseGoogleMockBoolFlag(const char* str, const char* flag, 0092 bool* value) { 0093 // Gets the value of the flag as a string. 0094 const char* const value_str = ParseGoogleMockFlagValue(str, flag, true); 0095 0096 // Aborts if the parsing failed. 0097 if (value_str == NULL) return false; 0098 0099 // Converts the string value to a bool. 0100 *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F'); 0101 return true; 0102 } 0103 0104 // Parses a string for a Google Mock string flag, in the form of 0105 // "--gmock_flag=value". 0106 // 0107 // On success, stores the value of the flag in *value, and returns 0108 // true. On failure, returns false without changing *value. 0109 template <typename String> 0110 static bool ParseGoogleMockStringFlag(const char* str, const char* flag, 0111 String* value) { 0112 // Gets the value of the flag as a string. 0113 const char* const value_str = ParseGoogleMockFlagValue(str, flag, false); 0114 0115 // Aborts if the parsing failed. 0116 if (value_str == NULL) return false; 0117 0118 // Sets *value to the value of the flag. 0119 *value = value_str; 0120 return true; 0121 } 0122 0123 // The internal implementation of InitGoogleMock(). 0124 // 0125 // The type parameter CharType can be instantiated to either char or 0126 // wchar_t. 0127 template <typename CharType> 0128 void InitGoogleMockImpl(int* argc, CharType** argv) { 0129 // Makes sure Google Test is initialized. InitGoogleTest() is 0130 // idempotent, so it's fine if the user has already called it. 0131 InitGoogleTest(argc, argv); 0132 if (*argc <= 0) return; 0133 0134 for (int i = 1; i != *argc; i++) { 0135 const std::string arg_string = StreamableToString(argv[i]); 0136 const char* const arg = arg_string.c_str(); 0137 0138 // Do we see a Google Mock flag? 0139 if (ParseGoogleMockBoolFlag(arg, "catch_leaked_mocks", 0140 &GMOCK_FLAG(catch_leaked_mocks)) || 0141 ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose))) { 0142 // Yes. Shift the remainder of the argv list left by one. Note 0143 // that argv has (*argc + 1) elements, the last one always being 0144 // NULL. The following loop moves the trailing NULL element as 0145 // well. 0146 for (int j = i; j != *argc; j++) { 0147 argv[j] = argv[j + 1]; 0148 } 0149 0150 // Decrements the argument count. 0151 (*argc)--; 0152 0153 // We also need to decrement the iterator as we just removed 0154 // an element. 0155 i--; 0156 } 0157 } 0158 } 0159 0160 } // namespace internal 0161 0162 // Initializes Google Mock. This must be called before running the 0163 // tests. In particular, it parses a command line for the flags that 0164 // Google Mock recognizes. Whenever a Google Mock flag is seen, it is 0165 // removed from argv, and *argc is decremented. 0166 // 0167 // No value is returned. Instead, the Google Mock flag variables are 0168 // updated. 0169 // 0170 // Since Google Test is needed for Google Mock to work, this function 0171 // also initializes Google Test and parses its flags, if that hasn't 0172 // been done. 0173 GTEST_API_ void InitGoogleMock(int* argc, char** argv) { 0174 internal::InitGoogleMockImpl(argc, argv); 0175 } 0176 0177 // This overloaded version can be used in Windows programs compiled in 0178 // UNICODE mode. 0179 GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) { 0180 internal::InitGoogleMockImpl(argc, argv); 0181 } 0182 0183 } // namespace testing
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |