shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 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 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 32 | #include "gmock/gmock.h" |
| 33 | #include "gmock/internal/gmock-port.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 34 | |
| 35 | namespace testing { |
| 36 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 37 | // TODO(wan@google.com): support using environment variables to |
| 38 | // control the flag values, like what Google Test does. |
| 39 | |
zhanyong.wan | bf0d0a4 | 2009-04-29 23:52:29 +0000 | [diff] [blame] | 40 | GMOCK_DEFINE_bool_(catch_leaked_mocks, true, |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 41 | "true iff Google Mock should report leaked mock objects " |
| 42 | "as failures."); |
| 43 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 44 | GMOCK_DEFINE_string_(verbose, internal::kWarningVerbosity, |
| 45 | "Controls how verbose Google Mock's output is." |
| 46 | " Valid values:\n" |
| 47 | " info - prints all messages.\n" |
| 48 | " warning - prints warnings and errors.\n" |
| 49 | " error - prints errors only."); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 50 | |
Alyssa Wilk | 6e1970e | 2017-08-10 09:41:09 -0400 | [diff] [blame^] | 51 | GMOCK_DEFINE_int32_(default_mock_behavior, 1, |
| 52 | "Controls the default behavior of mocks." |
| 53 | " Valid values:\n" |
| 54 | " 0 - by default, mocks act as NiceMocks.\n" |
| 55 | " 1 - by default, mocks act as NaggyMocks.\n" |
| 56 | " 2 - by default, mocks act as StrictMocks."); |
| 57 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 58 | namespace internal { |
| 59 | |
| 60 | // Parses a string as a command line flag. The string should have the |
| 61 | // format "--gmock_flag=value". When def_optional is true, the |
| 62 | // "=value" part can be omitted. |
| 63 | // |
| 64 | // Returns the value of the flag, or NULL if the parsing failed. |
| 65 | static const char* ParseGoogleMockFlagValue(const char* str, |
| 66 | const char* flag, |
| 67 | bool def_optional) { |
| 68 | // str and flag must not be NULL. |
| 69 | if (str == NULL || flag == NULL) return NULL; |
| 70 | |
| 71 | // The flag must start with "--gmock_". |
jgm | 38513a8 | 2012-11-15 15:50:36 +0000 | [diff] [blame] | 72 | const std::string flag_str = std::string("--gmock_") + flag; |
zhanyong.wan | 4664285 | 2009-09-01 19:10:50 +0000 | [diff] [blame] | 73 | const size_t flag_len = flag_str.length(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 74 | if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL; |
| 75 | |
| 76 | // Skips the flag name. |
| 77 | const char* flag_end = str + flag_len; |
| 78 | |
| 79 | // When def_optional is true, it's OK to not have a "=value" part. |
| 80 | if (def_optional && (flag_end[0] == '\0')) { |
| 81 | return flag_end; |
| 82 | } |
| 83 | |
| 84 | // If def_optional is true and there are more characters after the |
| 85 | // flag name, or if def_optional is false, there must be a '=' after |
| 86 | // the flag name. |
| 87 | if (flag_end[0] != '=') return NULL; |
| 88 | |
| 89 | // Returns the string after "=". |
| 90 | return flag_end + 1; |
| 91 | } |
| 92 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 93 | // Parses a string for a Google Mock bool flag, in the form of |
| 94 | // "--gmock_flag=value". |
| 95 | // |
| 96 | // On success, stores the value of the flag in *value, and returns |
| 97 | // true. On failure, returns false without changing *value. |
| 98 | static bool ParseGoogleMockBoolFlag(const char* str, const char* flag, |
| 99 | bool* value) { |
| 100 | // Gets the value of the flag as a string. |
| 101 | const char* const value_str = ParseGoogleMockFlagValue(str, flag, true); |
| 102 | |
| 103 | // Aborts if the parsing failed. |
| 104 | if (value_str == NULL) return false; |
| 105 | |
| 106 | // Converts the string value to a bool. |
| 107 | *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F'); |
| 108 | return true; |
| 109 | } |
| 110 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 111 | // Parses a string for a Google Mock string flag, in the form of |
| 112 | // "--gmock_flag=value". |
| 113 | // |
| 114 | // On success, stores the value of the flag in *value, and returns |
| 115 | // true. On failure, returns false without changing *value. |
kosak | c820efc | 2015-07-27 22:08:34 +0000 | [diff] [blame] | 116 | template <typename String> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 117 | static bool ParseGoogleMockStringFlag(const char* str, const char* flag, |
kosak | c820efc | 2015-07-27 22:08:34 +0000 | [diff] [blame] | 118 | String* value) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 119 | // Gets the value of the flag as a string. |
| 120 | const char* const value_str = ParseGoogleMockFlagValue(str, flag, false); |
| 121 | |
| 122 | // Aborts if the parsing failed. |
| 123 | if (value_str == NULL) return false; |
| 124 | |
| 125 | // Sets *value to the value of the flag. |
| 126 | *value = value_str; |
| 127 | return true; |
| 128 | } |
| 129 | |
Alyssa Wilk | 6e1970e | 2017-08-10 09:41:09 -0400 | [diff] [blame^] | 130 | static bool ParseGoogleMockIntFlag(const char* str, const char* flag, |
| 131 | int* value) { |
| 132 | // Gets the value of the flag as a string. |
| 133 | const char* const value_str = ParseGoogleMockFlagValue(str, flag, true); |
| 134 | |
| 135 | // Aborts if the parsing failed. |
| 136 | if (value_str == NULL) return false; |
| 137 | |
| 138 | // Sets *value to the value of the flag. |
| 139 | *value = atoi(value_str); |
| 140 | return true; |
| 141 | } |
| 142 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 143 | // The internal implementation of InitGoogleMock(). |
| 144 | // |
| 145 | // The type parameter CharType can be instantiated to either char or |
| 146 | // wchar_t. |
| 147 | template <typename CharType> |
| 148 | void InitGoogleMockImpl(int* argc, CharType** argv) { |
| 149 | // Makes sure Google Test is initialized. InitGoogleTest() is |
| 150 | // idempotent, so it's fine if the user has already called it. |
| 151 | InitGoogleTest(argc, argv); |
| 152 | if (*argc <= 0) return; |
| 153 | |
| 154 | for (int i = 1; i != *argc; i++) { |
jgm | 38513a8 | 2012-11-15 15:50:36 +0000 | [diff] [blame] | 155 | const std::string arg_string = StreamableToString(argv[i]); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 156 | const char* const arg = arg_string.c_str(); |
| 157 | |
| 158 | // Do we see a Google Mock flag? |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 159 | if (ParseGoogleMockBoolFlag(arg, "catch_leaked_mocks", |
| 160 | &GMOCK_FLAG(catch_leaked_mocks)) || |
Alyssa Wilk | 6e1970e | 2017-08-10 09:41:09 -0400 | [diff] [blame^] | 161 | ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose)) || |
| 162 | ParseGoogleMockIntFlag(arg, "default_mock_behavior", |
| 163 | &GMOCK_FLAG(default_mock_behavior))) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 164 | // Yes. Shift the remainder of the argv list left by one. Note |
| 165 | // that argv has (*argc + 1) elements, the last one always being |
| 166 | // NULL. The following loop moves the trailing NULL element as |
| 167 | // well. |
| 168 | for (int j = i; j != *argc; j++) { |
| 169 | argv[j] = argv[j + 1]; |
| 170 | } |
| 171 | |
| 172 | // Decrements the argument count. |
| 173 | (*argc)--; |
| 174 | |
| 175 | // We also need to decrement the iterator as we just removed |
| 176 | // an element. |
| 177 | i--; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | } // namespace internal |
| 183 | |
| 184 | // Initializes Google Mock. This must be called before running the |
| 185 | // tests. In particular, it parses a command line for the flags that |
| 186 | // Google Mock recognizes. Whenever a Google Mock flag is seen, it is |
| 187 | // removed from argv, and *argc is decremented. |
| 188 | // |
| 189 | // No value is returned. Instead, the Google Mock flag variables are |
| 190 | // updated. |
| 191 | // |
| 192 | // Since Google Test is needed for Google Mock to work, this function |
| 193 | // also initializes Google Test and parses its flags, if that hasn't |
| 194 | // been done. |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 195 | GTEST_API_ void InitGoogleMock(int* argc, char** argv) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 196 | internal::InitGoogleMockImpl(argc, argv); |
| 197 | } |
| 198 | |
| 199 | // This overloaded version can be used in Windows programs compiled in |
| 200 | // UNICODE mode. |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 201 | GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 202 | internal::InitGoogleMockImpl(argc, argv); |
| 203 | } |
| 204 | |
| 205 | } // namespace testing |