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.
31 #include "gmock/gmock.h"
32 #include "gmock/internal/gmock-port.h"
36 // FIXME: support using environment variables to
37 // control the flag values, like what Google Test does.
39 GMOCK_DEFINE_bool_(catch_leaked_mocks, true,
40 "true iff Google Mock should report leaked mock objects "
43 GMOCK_DEFINE_string_(verbose, internal::kWarningVerbosity,
44 "Controls how verbose Google Mock's output is."
46 " info - prints all messages.\n"
47 " warning - prints warnings and errors.\n"
48 " error - prints errors only.");
50 GMOCK_DEFINE_int32_(default_mock_behavior, 1,
51 "Controls the default behavior of mocks."
53 " 0 - by default, mocks act as NiceMocks.\n"
54 " 1 - by default, mocks act as NaggyMocks.\n"
55 " 2 - by default, mocks act as StrictMocks.");
59 // Parses a string as a command line flag. The string should have the
60 // format "--gmock_flag=value". When def_optional is true, the
61 // "=value" part can be omitted.
63 // Returns the value of the flag, or NULL if the parsing failed.
64 static const char* ParseGoogleMockFlagValue(const char* str,
67 // str and flag must not be NULL.
68 if (str == NULL || flag == NULL) return NULL;
70 // The flag must start with "--gmock_".
71 const std::string flag_str = std::string("--gmock_") + flag;
72 const size_t flag_len = flag_str.length();
73 if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL;
75 // Skips the flag name.
76 const char* flag_end = str + flag_len;
78 // When def_optional is true, it's OK to not have a "=value" part.
79 if (def_optional && (flag_end[0] == '\0')) {
83 // If def_optional is true and there are more characters after the
84 // flag name, or if def_optional is false, there must be a '=' after
86 if (flag_end[0] != '=') return NULL;
88 // Returns the string after "=".
92 // Parses a string for a Google Mock bool flag, in the form of
93 // "--gmock_flag=value".
95 // On success, stores the value of the flag in *value, and returns
96 // true. On failure, returns false without changing *value.
97 static bool ParseGoogleMockBoolFlag(const char* str, const char* flag,
99 // Gets the value of the flag as a string.
100 const char* const value_str = ParseGoogleMockFlagValue(str, flag, true);
102 // Aborts if the parsing failed.
103 if (value_str == NULL) return false;
105 // Converts the string value to a bool.
106 *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
110 // Parses a string for a Google Mock string flag, in the form of
111 // "--gmock_flag=value".
113 // On success, stores the value of the flag in *value, and returns
114 // true. On failure, returns false without changing *value.
115 template <typename String>
116 static bool ParseGoogleMockStringFlag(const char* str, const char* flag,
118 // Gets the value of the flag as a string.
119 const char* const value_str = ParseGoogleMockFlagValue(str, flag, false);
121 // Aborts if the parsing failed.
122 if (value_str == NULL) return false;
124 // Sets *value to the value of the flag.
129 static bool ParseGoogleMockIntFlag(const char* str, const char* flag,
131 // Gets the value of the flag as a string.
132 const char* const value_str = ParseGoogleMockFlagValue(str, flag, true);
134 // Aborts if the parsing failed.
135 if (value_str == NULL) return false;
137 // Sets *value to the value of the flag.
138 return ParseInt32(Message() << "The value of flag --" << flag,
142 // The internal implementation of InitGoogleMock().
144 // The type parameter CharType can be instantiated to either char or
146 template <typename CharType>
147 void InitGoogleMockImpl(int* argc, CharType** argv) {
148 // Makes sure Google Test is initialized. InitGoogleTest() is
149 // idempotent, so it's fine if the user has already called it.
150 InitGoogleTest(argc, argv);
151 if (*argc <= 0) return;
153 for (int i = 1; i != *argc; i++) {
154 const std::string arg_string = StreamableToString(argv[i]);
155 const char* const arg = arg_string.c_str();
157 // Do we see a Google Mock flag?
158 if (ParseGoogleMockBoolFlag(arg, "catch_leaked_mocks",
159 &GMOCK_FLAG(catch_leaked_mocks)) ||
160 ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose)) ||
161 ParseGoogleMockIntFlag(arg, "default_mock_behavior",
162 &GMOCK_FLAG(default_mock_behavior))) {
163 // Yes. Shift the remainder of the argv list left by one. Note
164 // that argv has (*argc + 1) elements, the last one always being
165 // NULL. The following loop moves the trailing NULL element as
167 for (int j = i; j != *argc; j++) {
168 argv[j] = argv[j + 1];
171 // Decrements the argument count.
174 // We also need to decrement the iterator as we just removed
181 } // namespace internal
183 // Initializes Google Mock. This must be called before running the
184 // tests. In particular, it parses a command line for the flags that
185 // Google Mock recognizes. Whenever a Google Mock flag is seen, it is
186 // removed from argv, and *argc is decremented.
188 // No value is returned. Instead, the Google Mock flag variables are
191 // Since Google Test is needed for Google Mock to work, this function
192 // also initializes Google Test and parses its flags, if that hasn't
194 GTEST_API_ void InitGoogleMock(int* argc, char** argv) {
195 internal::InitGoogleMockImpl(argc, argv);
198 // This overloaded version can be used in Windows programs compiled in
200 GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) {
201 internal::InitGoogleMockImpl(argc, argv);
204 } // namespace testing