• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "gmock/gmock.h"
31 
32 #include "gmock/internal/gmock-port.h"
33 
34 GMOCK_DEFINE_bool_(catch_leaked_mocks, true,
35                    "true if and only if Google Mock should report leaked "
36                    "mock objects as failures.");
37 
38 GMOCK_DEFINE_string_(verbose, testing::internal::kWarningVerbosity,
39                      "Controls how verbose Google Mock's output is."
40                      "  Valid values:\n"
41                      "  info    - prints all messages.\n"
42                      "  warning - prints warnings and errors.\n"
43                      "  error   - prints errors only.");
44 
45 GMOCK_DEFINE_int32_(default_mock_behavior, 1,
46                     "Controls the default behavior of mocks."
47                     "  Valid values:\n"
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.");
51 
52 namespace testing {
53 namespace 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.
ParseGoogleMockFlagValue(const char * str,const char * flag_name,bool def_optional)60 static const char* ParseGoogleMockFlagValue(const char* str,
61                                             const char* flag_name,
62                                             bool def_optional) {
63   // str and flag must not be NULL.
64   if (str == nullptr || flag_name == nullptr) return nullptr;
65 
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;
70 
71   // Skips the flag name.
72   const char* flag_end = str + flag_name_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 nullptr;
83 
84   // Returns the string after "=".
85   return flag_end + 1;
86 }
87 
88 // 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.
ParseGoogleMockFlag(const char * str,const char * flag_name,bool * value)93 static bool ParseGoogleMockFlag(const char* str, const char* flag_name,
94                                 bool* value) {
95   // Gets the value of the flag as a string.
96   const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, true);
97 
98   // Aborts if the parsing failed.
99   if (value_str == nullptr) 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 
106 // 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.
111 template <typename String>
ParseGoogleMockFlag(const char * str,const char * flag_name,String * value)112 static bool ParseGoogleMockFlag(const char* str, const char* flag_name,
113                                 String* value) {
114   // Gets the value of the flag as a string.
115   const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, false);
116 
117   // Aborts if the parsing failed.
118   if (value_str == nullptr) return false;
119 
120   // Sets *value to the value of the flag.
121   *value = value_str;
122   return true;
123 }
124 
ParseGoogleMockFlag(const char * str,const char * flag_name,int32_t * value)125 static bool ParseGoogleMockFlag(const char* str, const char* flag_name,
126                                 int32_t* value) {
127   // Gets the value of the flag as a string.
128   const char* const value_str = ParseGoogleMockFlagValue(str, flag_name, true);
129 
130   // Aborts if the parsing failed.
131   if (value_str == nullptr) return false;
132 
133   // Sets *value to the value of the flag.
134   return ParseInt32(Message() << "The value of flag --" << flag_name, value_str,
135                     value);
136 }
137 
138 // The internal implementation of InitGoogleMock().
139 //
140 // The type parameter CharType can be instantiated to either char or
141 // wchar_t.
142 template <typename CharType>
InitGoogleMockImpl(int * argc,CharType ** argv)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;
148 
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();
152 
153     // Do we see a Google Mock flag?
154     bool found_gmock_flag = false;
155 
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;                          \
162     }                                                   \
163   }
164 
165     GMOCK_INTERNAL_PARSE_FLAG(catch_leaked_mocks)
166     GMOCK_INTERNAL_PARSE_FLAG(verbose)
167     GMOCK_INTERNAL_PARSE_FLAG(default_mock_behavior)
168 
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
173       // well.
174       for (int j = i; j != *argc; j++) {
175         argv[j] = argv[j + 1];
176       }
177 
178       // Decrements the argument count.
179       (*argc)--;
180 
181       // We also need to decrement the iterator as we just removed
182       // an element.
183       i--;
184     }
185   }
186 }
187 
188 }  // namespace internal
189 
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.
194 //
195 // No value is returned.  Instead, the Google Mock flag variables are
196 // updated.
197 //
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
200 // been done.
InitGoogleMock(int * argc,char ** argv)201 GTEST_API_ void InitGoogleMock(int* argc, char** argv) {
202   internal::InitGoogleMockImpl(argc, argv);
203 }
204 
205 // This overloaded version can be used in Windows programs compiled in
206 // UNICODE mode.
InitGoogleMock(int * argc,wchar_t ** argv)207 GTEST_API_ void InitGoogleMock(int* argc, wchar_t** argv) {
208   internal::InitGoogleMockImpl(argc, argv);
209 }
210 
211 // This overloaded version can be used on Arduino/embedded platforms where
212 // there is no argc/argv.
InitGoogleMock()213 GTEST_API_ void InitGoogleMock() {
214   // Since Arduino doesn't have a command line, fake out the argc/argv arguments
215   int argc = 1;
216   const auto arg0 = "dummy";
217   char* argv0 = const_cast<char*>(arg0);
218   char** argv = &argv0;
219 
220   internal::InitGoogleMockImpl(&argc, argv);
221 }
222 
223 }  // namespace testing
224