00001 // Copyright 2008, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // Author: wan@google.com (Zhanyong Wan) 00031 00032 #include "gmock/gmock.h" 00033 #include "gmock/internal/gmock-port.h" 00034 00035 namespace testing { 00036 00037 // TODO(wan@google.com): support using environment variables to 00038 // control the flag values, like what Google Test does. 00039 00040 GMOCK_DEFINE_bool_(catch_leaked_mocks, true, 00041 "true iff Google Mock should report leaked mock objects " 00042 "as failures."); 00043 00044 GMOCK_DEFINE_string_(verbose, internal::kWarningVerbosity, 00045 "Controls how verbose Google Mock's output is." 00046 " Valid values:\n" 00047 " info - prints all messages.\n" 00048 " warning - prints warnings and errors.\n" 00049 " error - prints errors only."); 00050 00051 namespace internal { 00052 00053 // Parses a string as a command line flag. The string should have the 00054 // format "--gmock_flag=value". When def_optional is true, the 00055 // "=value" part can be omitted. 00056 // 00057 // Returns the value of the flag, or NULL if the parsing failed. 00058 static const char* ParseGoogleMockFlagValue(const char* str, 00059 const char* flag, 00060 bool def_optional) { 00061 // str and flag must not be NULL. 00062 if (str == NULL || flag == NULL) return NULL; 00063 00064 // The flag must start with "--gmock_". 00065 const std::string flag_str = std::string("--gmock_") + flag; 00066 const size_t flag_len = flag_str.length(); 00067 if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL; 00068 00069 // Skips the flag name. 00070 const char* flag_end = str + flag_len; 00071 00072 // When def_optional is true, it's OK to not have a "=value" part. 00073 if (def_optional && (flag_end[0] == '\0')) { 00074 return flag_end; 00075 } 00076 00077 // If def_optional is true and there are more characters after the 00078 // flag name, or if def_optional is false, there must be a '=' after 00079 // the flag name. 00080 if (flag_end[0] != '=') return NULL; 00081 00082 // Returns the string after "=". 00083 return flag_end + 1; 00084 } 00085 00086 // Parses a string for a Google Mock bool flag, in the form of 00087 // "--gmock_flag=value". 00088 // 00089 // On success, stores the value of the flag in *value, and returns 00090 // true. On failure, returns false without changing *value. 00091 static bool ParseGoogleMockBoolFlag(const char* str, const char* flag, 00092 bool* value) { 00093 // Gets the value of the flag as a string. 00094 const char* const value_str = ParseGoogleMockFlagValue(str, flag, true); 00095 00096 // Aborts if the parsing failed. 00097 if (value_str == NULL) return false; 00098 00099 // Converts the string value to a bool. 00100 *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F'); 00101 return true; 00102 } 00103 00104 // Parses a string for a Google Mock string flag, in the form of 00105 // "--gmock_flag=value". 00106 // 00107 // On success, stores the value of the flag in *value, and returns 00108 // true. On failure, returns false without changing *value. 00109 static bool ParseGoogleMockStringFlag(const char* str, const char* flag, 00110 std::string* value) { 00111 // Gets the value of the flag as a string. 00112 const char* const value_str = ParseGoogleMockFlagValue(str, flag, false); 00113 00114 // Aborts if the parsing failed. 00115 if (value_str == NULL) return false; 00116 00117 // Sets *value to the value of the flag. 00118 *value = value_str; 00119 return true; 00120 } 00121 00122 // The internal implementation of InitGoogleMock(). 00123 // 00124 // The type parameter CharType can be instantiated to either char or 00125 // wchar_t. 00126 template <typename CharType> 00127 void InitGoogleMockImpl(int* argc, CharType** argv) { 00128 // Makes sure Google Test is initialized. InitGoogleTest() is 00129 // idempotent, so it's fine if the user has already called it. 00130 InitGoogleTest(argc, argv); 00131 if (*argc <= 0) return; 00132 00133 for (int i = 1; i != *argc; i++) { 00134 const std::string arg_string = StreamableToString(argv[i]); 00135 const char* const arg = arg_string.c_str(); 00136 00137 // Do we see a Google Mock flag? 00138 if (ParseGoogleMockBoolFlag(arg, "catch_leaked_mocks", 00139 &GMOCK_FLAG(catch_leaked_mocks)) || 00140 ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose))) { 00141 // Yes. Shift the remainder of the argv list left by one. Note 00142 // that argv has (*argc + 1) elements, the last one always being 00143 // NULL. The following loop moves the trailing NULL element as 00144 // well. 00145 for (int j = i; j != *argc; j++) { 00146 argv[j] = argv[j + 1]; 00147 } 00148 00149 // Decrements the argument count. 00150 (*argc)--; 00151 00152 // We also need to decrement the iterator as we just removed 00153 // an element. 00154 i--; 00155 } 00156 } 00157 } 00158 00159 } // namespace internal 00160 00161 // Initializes Google Mock. This must be called before running the 00162 // tests. In particular, it parses a command line for the flags that 00163 // Google Mock recognizes. Whenever a Google Mock flag is seen, it is 00164 // removed from argv, and *argc is decremented. 00165 // 00166 // No value is returned. Instead, the Google Mock flag variables are 00167 // updated. 00168 // 00169 // Since Google Test is needed for Google Mock to work, this function 00170 // also initializes Google Test and parses its flags, if that hasn't 00171 // been done. 00172 GTEST_API_ void InitGoogleMock(int* argc, char** argv) { 00173 internal::InitGoogleMockImpl(argc, argv); 00174 } 00175 00176 // This overloaded version can be used in Windows programs compiled in 00177 // UNICODE mode. 00178 GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) { 00179 internal::InitGoogleMockImpl(argc, argv); 00180 } 00181 00182 } // namespace testing