Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
gmock.cc
Go to the documentation of this file.
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
32#include "gmock/gmock.h"
34
35namespace testing {
36
37// TODO(wan@google.com): support using environment variables to
38// control the flag values, like what Google Test does.
39
40GMOCK_DEFINE_bool_(catch_leaked_mocks, true,
41 "true iff Google Mock should report leaked mock objects "
42 "as failures.");
43
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.");
50
51GMOCK_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
58namespace 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_".
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;
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
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.
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
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.
116template <typename String>
117static bool ParseGoogleMockStringFlag(const char* str, const char* flag,
118 String* value) {
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
130static 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 return ParseInt32(Message() << "The value of flag --" << flag,
140 value_str, value);
141}
142
143// 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++) {
155 const std::string arg_string = StreamableToString(argv[i]);
156 const char* const arg = arg_string.c_str();
157
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
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.
195GTEST_API_ void InitGoogleMock(int* argc, char** argv) {
197}
198
199// This overloaded version can be used in Windows programs compiled in
200// UNICODE mode.
201GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) {
203}
204
205} // namespace testing
#define GMOCK_DEFINE_int32_(name, default_val, doc)
Definition gmock-port.h:80
#define GMOCK_DEFINE_bool_(name, default_val, doc)
Definition gmock-port.h:78
#define GMOCK_DEFINE_string_(name, default_val, doc)
Definition gmock-port.h:82
#define GMOCK_FLAG(name)
Definition gmock-port.h:66
#define GTEST_API_
Definition gtest-port.h:984
char ** argv
void InitGoogleMockImpl(int *argc, CharType **argv)
Definition gmock.cc:148
std::string StreamableToString(const T &streamable)
bool ParseInt32(const Message &src_text, const char *str, Int32 *value)
GTEST_API_ void InitGoogleMock(int *argc, char **argv)
Definition gmock.cc:195
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition gtest.cc:5787
#define value
Definition pkcs11.h:157
uint16_t j