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