blob: dc9d3d229f6858d5d893d03c16f4c5d2d4cf048d [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
32#include <gmock/gmock.h>
33#include <gmock/internal/gmock-port.h>
34
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
40// TODO(wan@google.com): change the default value to true after people
41// have a chance to fix their leaked mocks.
42GMOCK_DEFINE_bool_(catch_leaked_mocks, false,
43 "true iff Google Mock should report leaked mock objects "
44 "as failures.");
45
zhanyong.wane0d051e2009-02-19 00:33:37 +000046GMOCK_DEFINE_string_(verbose, internal::kWarningVerbosity,
47 "Controls how verbose Google Mock's output is."
48 " Valid values:\n"
49 " info - prints all messages.\n"
50 " warning - prints warnings and errors.\n"
51 " error - prints errors only.");
shiqiane35fdd92008-12-10 05:08:54 +000052
53namespace internal {
54
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.
58//
59// Returns the value of the flag, or NULL if the parsing failed.
60static const char* ParseGoogleMockFlagValue(const char* str,
61 const char* flag,
62 bool def_optional) {
63 // str and flag must not be NULL.
64 if (str == NULL || flag == NULL) return NULL;
65
66 // The flag must start with "--gmock_".
67 const String flag_str = String::Format("--gmock_%s", flag);
68 const size_t flag_len = flag_str.GetLength();
69 if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL;
70
71 // Skips the flag name.
72 const char* flag_end = str + flag_len;
73
74 // When def_optional is true, it's OK to not have a "=value" part.
75 if (def_optional && (flag_end[0] == '\0')) {
76 return flag_end;
77 }
78
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
81 // the flag name.
82 if (flag_end[0] != '=') return NULL;
83
84 // Returns the string after "=".
85 return flag_end + 1;
86}
87
zhanyong.wandf35a762009-04-22 22:25:31 +000088// Parses a string for a Google Mock bool flag, in the form of
89// "--gmock_flag=value".
90//
91// On success, stores the value of the flag in *value, and returns
92// true. On failure, returns false without changing *value.
93static bool ParseGoogleMockBoolFlag(const char* str, const char* flag,
94 bool* value) {
95 // Gets the value of the flag as a string.
96 const char* const value_str = ParseGoogleMockFlagValue(str, flag, true);
97
98 // Aborts if the parsing failed.
99 if (value_str == NULL) return false;
100
101 // Converts the string value to a bool.
102 *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');
103 return true;
104}
105
shiqiane35fdd92008-12-10 05:08:54 +0000106// Parses a string for a Google Mock string flag, in the form of
107// "--gmock_flag=value".
108//
109// On success, stores the value of the flag in *value, and returns
110// true. On failure, returns false without changing *value.
111static bool ParseGoogleMockStringFlag(const char* str, const char* flag,
112 String* value) {
113 // Gets the value of the flag as a string.
114 const char* const value_str = ParseGoogleMockFlagValue(str, flag, false);
115
116 // Aborts if the parsing failed.
117 if (value_str == NULL) return false;
118
119 // Sets *value to the value of the flag.
120 *value = value_str;
121 return true;
122}
123
124// The internal implementation of InitGoogleMock().
125//
126// The type parameter CharType can be instantiated to either char or
127// wchar_t.
128template <typename CharType>
129void InitGoogleMockImpl(int* argc, CharType** argv) {
130 // Makes sure Google Test is initialized. InitGoogleTest() is
131 // idempotent, so it's fine if the user has already called it.
132 InitGoogleTest(argc, argv);
133 if (*argc <= 0) return;
134
135 for (int i = 1; i != *argc; i++) {
136 const String arg_string = StreamableToString(argv[i]);
137 const char* const arg = arg_string.c_str();
138
139 // Do we see a Google Mock flag?
zhanyong.wandf35a762009-04-22 22:25:31 +0000140 if (ParseGoogleMockBoolFlag(arg, "catch_leaked_mocks",
141 &GMOCK_FLAG(catch_leaked_mocks)) ||
142 ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose))) {
shiqiane35fdd92008-12-10 05:08:54 +0000143 // Yes. Shift the remainder of the argv list left by one. Note
144 // that argv has (*argc + 1) elements, the last one always being
145 // NULL. The following loop moves the trailing NULL element as
146 // well.
147 for (int j = i; j != *argc; j++) {
148 argv[j] = argv[j + 1];
149 }
150
151 // Decrements the argument count.
152 (*argc)--;
153
154 // We also need to decrement the iterator as we just removed
155 // an element.
156 i--;
157 }
158 }
159}
160
161} // namespace internal
162
163// Initializes Google Mock. This must be called before running the
164// tests. In particular, it parses a command line for the flags that
165// Google Mock recognizes. Whenever a Google Mock flag is seen, it is
166// removed from argv, and *argc is decremented.
167//
168// No value is returned. Instead, the Google Mock flag variables are
169// updated.
170//
171// Since Google Test is needed for Google Mock to work, this function
172// also initializes Google Test and parses its flags, if that hasn't
173// been done.
174void InitGoogleMock(int* argc, char** argv) {
175 internal::InitGoogleMockImpl(argc, argv);
176}
177
178// This overloaded version can be used in Windows programs compiled in
179// UNICODE mode.
180void InitGoogleMock(int* argc, wchar_t** argv) {
181 internal::InitGoogleMockImpl(argc, argv);
182}
183
184} // namespace testing