1 // Copyright 2008, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30 #include "gmock/gmock.h" 31 32 #include <string> 33 34 #include "gmock/internal/gmock-port.h" 35 36 GMOCK_DEFINE_bool_(catch_leaked_mocks, true, 37 "true if and only if Google Mock should report leaked " 38 "mock objects as failures."); 39 40 GMOCK_DEFINE_string_(verbose, testing::internal::kWarningVerbosity, 41 "Controls how verbose Google Mock's output is." 42 " Valid values:\n" 43 " info - prints all messages.\n" 44 " warning - prints warnings and errors.\n" 45 " error - prints errors only."); 46 47 GMOCK_DEFINE_int32_(default_mock_behavior, 1, 48 "Controls the default behavior of mocks." 49 " Valid values:\n" 50 " 0 - by default, mocks act as NiceMocks.\n" 51 " 1 - by default, mocks act as NaggyMocks.\n" 52 " 2 - by default, mocks act as StrictMocks."); 53 54 namespace testing { 55 namespace internal { 56 57 // Parses a string as a command line flag. The string should have the 58 // format "--gmock_flag=value". When def_optional is true, the 59 // "=value" part can be omitted. 60 // 61 // Returns the value of the flag, or NULL if the parsing failed. 62 static const char* ParseGoogleMockFlagValue(const char* str, 63 const char* flag_name, 64 bool def_optional) { 65 // str and flag must not be NULL. 66 if (str == nullptr || flag_name == nullptr) return nullptr; 67 68 // The flag must start with "--gmock_". 69 const std::string flag_name_str = std::string("--gmock_") + flag_name; 70 const size_t flag_name_len = flag_name_str.length(); 71 if (strncmp(str, flag_name_str.c_str(), flag_name_len) != 0) return nullptr; 72 73 // Skips the flag name. 74 const char* flag_end = str + flag_name_len; 75 76 // When def_optional is true, it's OK to not have a "=value" part. 77 if (def_optional && (flag_end[0] == '\0')) { 78 return flag_end; 79 } 80 81 // If def_optional is true and there are more characters after the 82 // flag name, or if def_optional is false, there must be a '=' after 83 // the flag name. 84 if (flag_end[0] != '=') return nullptr; 85 86 // Returns the string after "=". 87 return flag_end + 1; 88 } 89 90 // Parses a string for a Google Mock bool flag, in the form of 91 // "--gmock_flag=value". 92 // 93 // On success, stores the value of the flag in *value, and returns 94 // true. On failure, returns false without changing *value. 95 static bool ParseGoogleMockFlag(const char* str, const char* flag_name, 96 bool* value) { 97 // Gets the value of the flag as a string. 98 const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, true); 99 100 // Aborts if the parsing failed. 101 if (value_str == nullptr) return false; 102 103 // Converts the string value to a bool. 104 *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F'); 105 return true; 106 } 107 108 // Parses a string for a Google Mock string flag, in the form of 109 // "--gmock_flag=value". 110 // 111 // On success, stores the value of the flag in *value, and returns 112 // true. On failure, returns false without changing *value. 113 template <typename String> 114 static bool ParseGoogleMockFlag(const char* str, const char* flag_name, 115 String* value) { 116 // Gets the value of the flag as a string. 117 const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, false); 118 119 // Aborts if the parsing failed. 120 if (value_str == nullptr) return false; 121 122 // Sets *value to the value of the flag. 123 *value = value_str; 124 return true; 125 } 126 127 static bool ParseGoogleMockFlag(const char* str, const char* flag_name, 128 int32_t* value) { 129 // Gets the value of the flag as a string. 130 const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, true); 131 132 // Aborts if the parsing failed. 133 if (value_str == nullptr) return false; 134 135 // Sets *value to the value of the flag. 136 return ParseInt32(Message() << "The value of flag --" << flag_name, value_str, 137 value); 138 } 139 140 // The internal implementation of InitGoogleMock(). 141 // 142 // The type parameter CharType can be instantiated to either char or 143 // wchar_t. 144 template <typename CharType> 145 void InitGoogleMockImpl(int* argc, CharType** argv) { 146 // Makes sure Google Test is initialized. InitGoogleTest() is 147 // idempotent, so it's fine if the user has already called it. 148 InitGoogleTest(argc, argv); 149 if (*argc <= 0) return; 150 151 for (int i = 1; i != *argc; i++) { 152 const std::string arg_string = StreamableToString(argv[i]); 153 const char* const arg = arg_string.c_str(); 154 155 // Do we see a Google Mock flag? 156 bool found_gmock_flag = false; 157 158 #define GMOCK_INTERNAL_PARSE_FLAG(flag_name) \ 159 if (!found_gmock_flag) { \ 160 auto value = GMOCK_FLAG_GET(flag_name); \ 161 if (ParseGoogleMockFlag(arg, #flag_name, &value)) { \ 162 GMOCK_FLAG_SET(flag_name, value); \ 163 found_gmock_flag = true; \ 164 } \ 165 } 166 167 GMOCK_INTERNAL_PARSE_FLAG(catch_leaked_mocks) 168 GMOCK_INTERNAL_PARSE_FLAG(verbose) 169 GMOCK_INTERNAL_PARSE_FLAG(default_mock_behavior) 170 171 if (found_gmock_flag) { 172 // Yes. Shift the remainder of the argv list left by one. Note 173 // that argv has (*argc + 1) elements, the last one always being 174 // NULL. The following loop moves the trailing NULL element as 175 // well. 176 for (int j = i; j != *argc; j++) { 177 argv[j] = argv[j + 1]; 178 } 179 180 // Decrements the argument count. 181 (*argc)--; 182 183 // We also need to decrement the iterator as we just removed 184 // an element. 185 i--; 186 } 187 } 188 } 189 190 } // namespace internal 191 192 // Initializes Google Mock. This must be called before running the 193 // tests. In particular, it parses a command line for the flags that 194 // Google Mock recognizes. Whenever a Google Mock flag is seen, it is 195 // removed from argv, and *argc is decremented. 196 // 197 // No value is returned. Instead, the Google Mock flag variables are 198 // updated. 199 // 200 // Since Google Test is needed for Google Mock to work, this function 201 // also initializes Google Test and parses its flags, if that hasn't 202 // been done. 203 GTEST_API_ void InitGoogleMock(int* argc, char** argv) { 204 internal::InitGoogleMockImpl(argc, argv); 205 } 206 207 // This overloaded version can be used in Windows programs compiled in 208 // UNICODE mode. 209 GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) { 210 internal::InitGoogleMockImpl(argc, argv); 211 } 212 213 // This overloaded version can be used on Arduino/embedded platforms where 214 // there is no argc/argv. 215 GTEST_API_ void InitGoogleMock() { 216 // Since Arduino doesn't have a command line, fake out the argc/argv arguments 217 int argc = 1; 218 const auto arg0 = "dummy"; 219 char* argv0 = const_cast<char*>(arg0); 220 char** argv = &argv0; 221 222 internal::InitGoogleMockImpl(&argc, argv); 223 } 224 225 } // namespace testing 226