blob: 3c370510b203f3fd316b0ace1a2dd63404960860 [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// 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.wan53e08c42010-09-14 05:38:21 +000032#include "gmock/gmock.h"
33#include "gmock/internal/gmock-port.h"
shiqiane35fdd92008-12-10 05:08:54 +000034
35namespace testing {
36
zhanyong.wandf35a762009-04-22 22:25:31 +000037// TODO(wan@google.com): support using environment variables to
38// control the flag values, like what Google Test does.
39
zhanyong.wanbf0d0a42009-04-29 23:52:29 +000040GMOCK_DEFINE_bool_(catch_leaked_mocks, true,
zhanyong.wandf35a762009-04-22 22:25:31 +000041 "true iff Google Mock should report leaked mock objects "
42 "as failures.");
43
zhanyong.wane0d051e2009-02-19 00:33:37 +000044GMOCK_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.");
shiqiane35fdd92008-12-10 05:08:54 +000050
Alyssa Wilk6e1970e2017-08-10 09:41:09 -040051GMOCK_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
shiqiane35fdd92008-12-10 05:08:54 +000058namespace 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.
65static 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_".
jgm38513a82012-11-15 15:50:36 +000072 const std::string flag_str = std::string("--gmock_") + flag;
zhanyong.wan46642852009-09-01 19:10:50 +000073 const size_t flag_len = flag_str.length();
shiqiane35fdd92008-12-10 05:08:54 +000074 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.wandf35a762009-04-22 22:25:31 +000093// 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.
98static 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
shiqiane35fdd92008-12-10 05:08:54 +0000111// 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.
kosakc820efc2015-07-27 22:08:34 +0000116template <typename String>
shiqiane35fdd92008-12-10 05:08:54 +0000117static bool ParseGoogleMockStringFlag(const char* str, const char* flag,
kosakc820efc2015-07-27 22:08:34 +0000118 String* value) {
shiqiane35fdd92008-12-10 05:08:54 +0000119 // 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 Wilk6e1970e2017-08-10 09:41:09 -0400130static 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
shiqiane35fdd92008-12-10 05:08:54 +0000143// The internal implementation of InitGoogleMock().
144//
145// The type parameter CharType can be instantiated to either char or
146// wchar_t.
147template <typename CharType>
148void 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++) {
jgm38513a82012-11-15 15:50:36 +0000155 const std::string arg_string = StreamableToString(argv[i]);
shiqiane35fdd92008-12-10 05:08:54 +0000156 const char* const arg = arg_string.c_str();
157
158 // Do we see a Google Mock flag?
zhanyong.wandf35a762009-04-22 22:25:31 +0000159 if (ParseGoogleMockBoolFlag(arg, "catch_leaked_mocks",
160 &GMOCK_FLAG(catch_leaked_mocks)) ||
Alyssa Wilk6e1970e2017-08-10 09:41:09 -0400161 ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose)) ||
162 ParseGoogleMockIntFlag(arg, "default_mock_behavior",
163 &GMOCK_FLAG(default_mock_behavior))) {
shiqiane35fdd92008-12-10 05:08:54 +0000164 // 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.
vladlosev587c1b32011-05-20 00:42:22 +0000195GTEST_API_ void InitGoogleMock(int* argc, char** argv) {
shiqiane35fdd92008-12-10 05:08:54 +0000196 internal::InitGoogleMockImpl(argc, argv);
197}
198
199// This overloaded version can be used in Windows programs compiled in
200// UNICODE mode.
vladlosev587c1b32011-05-20 00:42:22 +0000201GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) {
shiqiane35fdd92008-12-10 05:08:54 +0000202 internal::InitGoogleMockImpl(argc, argv);
203}
204
205} // namespace testing