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 #include "gmock/gmock.h"
32 #include "gmock/internal/gmock-port.h"
34 GMOCK_DEFINE_bool_(catch_leaked_mocks, true,
35 "true if and only if Google Mock should report leaked "
36 "mock objects as failures.");
38 GMOCK_DEFINE_string_(verbose, testing::internal::kWarningVerbosity,
39 "Controls how verbose Google Mock's output is."
41 " info - prints all messages.\n"
42 " warning - prints warnings and errors.\n"
43 " error - prints errors only.");
45 GMOCK_DEFINE_int32_(default_mock_behavior, 1,
46 "Controls the default behavior of mocks."
48 " 0 - by default, mocks act as NiceMocks.\n"
49 " 1 - by default, mocks act as NaggyMocks.\n"
50 " 2 - by default, mocks act as StrictMocks.");
55 // Parses a string as a command line flag. The string should have the
56 // format "--gmock_flag=value". When def_optional is true, the
57 // "=value" part can be omitted.
59 // Returns the value of the flag, or NULL if the parsing failed.
60 static const char* ParseGoogleMockFlagValue(const char* str,
61 const char* flag_name,
63 // str and flag must not be NULL.
64 if (str == nullptr || flag_name == nullptr) return nullptr;
66 // The flag must start with "--gmock_".
67 const std::string flag_name_str = std::string("--gmock_") + flag_name;
68 const size_t flag_name_len = flag_name_str.length();
69 if (strncmp(str, flag_name_str.c_str(), flag_name_len) != 0) return nullptr;
71 // Skips the flag name.
72 const char* flag_end = str + flag_name_len;
74 // When def_optional is true, it's OK to not have a "=value" part.
75 if (def_optional && (flag_end[0] == '\0')) {
79 // If def_optional is true and there are more characters after the
80 // flag name, or if def_optional is false, there must be a '=' after
82 if (flag_end[0] != '=') return nullptr;
84 // Returns the string after "=".
88 // Parses a string for a Google Mock bool flag, in the form of
89 // "--gmock_flag=value".
91 // On success, stores the value of the flag in *value, and returns
92 // true. On failure, returns false without changing *value.
93 static bool ParseGoogleMockFlag(const char* str, const char* flag_name,
95 // Gets the value of the flag as a string.
96 const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, true);
98 // Aborts if the parsing failed.
99 if (value_str == nullptr) return false;
101 // Converts the string value to a bool.
102 *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
106 // Parses a string for a Google Mock string flag, in the form of
107 // "--gmock_flag=value".
109 // On success, stores the value of the flag in *value, and returns
110 // true. On failure, returns false without changing *value.
111 template <typename String>
112 static bool ParseGoogleMockFlag(const char* str, const char* flag_name,
114 // Gets the value of the flag as a string.
115 const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, false);
117 // Aborts if the parsing failed.
118 if (value_str == nullptr) return false;
120 // Sets *value to the value of the flag.
125 static bool ParseGoogleMockFlag(const char* str, const char* flag_name,
127 // Gets the value of the flag as a string.
128 const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, true);
130 // Aborts if the parsing failed.
131 if (value_str == nullptr) return false;
133 // Sets *value to the value of the flag.
134 return ParseInt32(Message() << "The value of flag --" << flag_name, value_str,
138 // The internal implementation of InitGoogleMock().
140 // The type parameter CharType can be instantiated to either char or
142 template <typename CharType>
143 void InitGoogleMockImpl(int* argc, CharType** argv) {
144 // Makes sure Google Test is initialized. InitGoogleTest() is
145 // idempotent, so it's fine if the user has already called it.
146 InitGoogleTest(argc, argv);
147 if (*argc <= 0) return;
149 for (int i = 1; i != *argc; i++) {
150 const std::string arg_string = StreamableToString(argv[i]);
151 const char* const arg = arg_string.c_str();
153 // Do we see a Google Mock flag?
154 bool found_gmock_flag = false;
156 #define GMOCK_INTERNAL_PARSE_FLAG(flag_name) \
157 if (!found_gmock_flag) { \
158 auto value = GMOCK_FLAG_GET(flag_name); \
159 if (ParseGoogleMockFlag(arg, #flag_name, &value)) { \
160 GMOCK_FLAG_SET(flag_name, value); \
161 found_gmock_flag = true; \
165 GMOCK_INTERNAL_PARSE_FLAG(catch_leaked_mocks)
166 GMOCK_INTERNAL_PARSE_FLAG(verbose)
167 GMOCK_INTERNAL_PARSE_FLAG(default_mock_behavior)
169 if (found_gmock_flag) {
170 // Yes. Shift the remainder of the argv list left by one. Note
171 // that argv has (*argc + 1) elements, the last one always being
172 // NULL. The following loop moves the trailing NULL element as
174 for (int j = i; j != *argc; j++) {
175 argv[j] = argv[j + 1];
178 // Decrements the argument count.
181 // We also need to decrement the iterator as we just removed
188 } // namespace internal
190 // Initializes Google Mock. This must be called before running the
191 // tests. In particular, it parses a command line for the flags that
192 // Google Mock recognizes. Whenever a Google Mock flag is seen, it is
193 // removed from argv, and *argc is decremented.
195 // No value is returned. Instead, the Google Mock flag variables are
198 // Since Google Test is needed for Google Mock to work, this function
199 // also initializes Google Test and parses its flags, if that hasn't
201 GTEST_API_ void InitGoogleMock(int* argc, char** argv) {
202 internal::InitGoogleMockImpl(argc, argv);
205 // This overloaded version can be used in Windows programs compiled in
207 GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) {
208 internal::InitGoogleMockImpl(argc, argv);
211 // This overloaded version can be used on Arduino/embedded platforms where
212 // there is no argc/argv.
213 GTEST_API_ void InitGoogleMock() {
214 // Since Arduino doesn't have a command line, fake out the argc/argv arguments
216 const auto arg0 = "dummy";
217 char* argv0 = const_cast<char*>(arg0);
218 char** argv = &argv0;
220 internal::InitGoogleMockImpl(&argc, argv);
223 } // namespace testing