1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: wan@google.com (Zhanyong Wan)
32 #include "gmock/gmock.h"
33 #include "gmock/internal/gmock-port.h"
37 // TODO(wan@google.com): support using environment variables to
38 // control the flag values, like what Google Test does.
40 GMOCK_DEFINE_bool_(catch_leaked_mocks, true,
41 "true iff Google Mock should report leaked mock objects "
44 GMOCK_DEFINE_string_(verbose, internal::kWarningVerbosity,
45 "Controls how verbose Google Mock's output is."
47 " info - prints all messages.\n"
48 " warning - prints warnings and errors.\n"
49 " error - prints errors only.");
51 GMOCK_DEFINE_int32_(default_mock_behavior, 1,
52 "Controls the default behavior of mocks."
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.");
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.
64 // Returns the value of the flag, or NULL if the parsing failed.
65 static const char* ParseGoogleMockFlagValue(const char* str,
68 // str and flag must not be NULL.
69 if (str == NULL || flag == NULL) return NULL;
71 // The flag must start with "--gmock_".
72 const std::string flag_str = std::string("--gmock_") + flag;
73 const size_t flag_len = flag_str.length();
74 if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL;
76 // Skips the flag name.
77 const char* flag_end = str + flag_len;
79 // When def_optional is true, it's OK to not have a "=value" part.
80 if (def_optional && (flag_end[0] == '\0')) {
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
87 if (flag_end[0] != '=') return NULL;
89 // Returns the string after "=".
93 // Parses a string for a Google Mock bool flag, in the form of
94 // "--gmock_flag=value".
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,
100 // Gets the value of the flag as a string.
101 const char* const value_str = ParseGoogleMockFlagValue(str, flag, true);
103 // Aborts if the parsing failed.
104 if (value_str == NULL) return false;
106 // Converts the string value to a bool.
107 *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
111 // Parses a string for a Google Mock string flag, in the form of
112 // "--gmock_flag=value".
114 // On success, stores the value of the flag in *value, and returns
115 // true. On failure, returns false without changing *value.
116 template <typename String>
117 static bool ParseGoogleMockStringFlag(const char* str, const char* flag,
119 // Gets the value of the flag as a string.
120 const char* const value_str = ParseGoogleMockFlagValue(str, flag, false);
122 // Aborts if the parsing failed.
123 if (value_str == NULL) return false;
125 // Sets *value to the value of the flag.
130 static bool ParseGoogleMockIntFlag(const char* str, const char* flag,
132 // Gets the value of the flag as a string.
133 const char* const value_str = ParseGoogleMockFlagValue(str, flag, true);
135 // Aborts if the parsing failed.
136 if (value_str == NULL) return false;
138 // Sets *value to the value of the flag.
139 *value = atoi(value_str);
143 // The internal implementation of InitGoogleMock().
145 // The type parameter CharType can be instantiated to either char or
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;
154 for (int i = 1; i != *argc; i++) {
155 const std::string arg_string = StreamableToString(argv[i]);
156 const char* const arg = arg_string.c_str();
158 // Do we see a Google Mock flag?
159 if (ParseGoogleMockBoolFlag(arg, "catch_leaked_mocks",
160 &GMOCK_FLAG(catch_leaked_mocks)) ||
161 ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose)) ||
162 ParseGoogleMockIntFlag(arg, "default_mock_behavior",
163 &GMOCK_FLAG(default_mock_behavior))) {
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
168 for (int j = i; j != *argc; j++) {
169 argv[j] = argv[j + 1];
172 // Decrements the argument count.
175 // We also need to decrement the iterator as we just removed
182 } // namespace internal
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.
189 // No value is returned. Instead, the Google Mock flag variables are
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
195 GTEST_API_ void InitGoogleMock(int* argc, char** argv) {
196 internal::InitGoogleMockImpl(argc, argv);
199 // This overloaded version can be used in Windows programs compiled in
201 GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) {
202 internal::InitGoogleMockImpl(argc, argv);
205 } // namespace testing